# Recursive travel

**URL:** <https://swi-prolog.discourse.group/t/recursive-travel/1281>\
**Category:** Help!\
**Tags:** discussion\
**Created:** [September 18, 2019, 10:51am UTC](https://swi-prolog.discourse.group/t/recursive-travel/1281 "2019-09-18T10:51:53Z")\
**Posts on this page:** 8\
**Page:** 2

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [September 19, 2019, 7:15pm UTC](https://swi-prolog.discourse.group/t/recursive-travel/1281/22 "2019-09-19T19:15:08Z")

</div>

> [@DaJa](#):
>
> Also, which English words am I supposed to explain? What is the rule of thumb?

The rule of thumb I use when explaining English words/phrases is that if a standard dictionary ([Merriam-Webster](https://www.merriam-webster.com/)) defines it one way and I am using it a way not defined in a standard dictionary but one that is typically found in a dictionary like [urban dictionary](https://www.urbandictionary.com) then I typically link to the urban dictionary definition, or where the definition as I mean is defined.

Don’t worry about it so much as if someone doesn’t understand you, they will post a question. I just like to do it because it is easy and I know how hard it is sometimes to understand a language when you are not a native speaker.

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [September 19, 2019, 7:43pm UTC](https://swi-prolog.discourse.group/t/recursive-travel/1281/23 "2019-09-19T19:43:46Z")

</div>

> [@DaJa](#):
>
> Why would the whole clause fail if we happened to already have traversed Valmont?

A clause will contain a [head](https://www.metalevel.at/prolog/concepts) and possibly a body. A head without a body is a fact.

In the clause

```prolog
travel_01_helper(X,Y,Path0,[(Mode,Y)|Path0]) :-
    (
        (byCar(X,Y),Mode = car)
    ;
        (byTrain(X,Y),Mode = train)
    ;
        (byPlane(X,Y),Mode = plane)
    ),
    \+ member((_,Y),Path0).

```

The head is

`travel_01_helper(X,Y,Path0,[(Mode,Y)|Path0])`

and the body is

```prolog
    (
        (byCar(X,Y),Mode = car)
    ;
        (byTrain(X,Y),Mode = train)
    ;
        (byPlane(X,Y),Mode = plane)
    ),
    \+ member((_,Y),Path0).

```

Now to help in understanding how a body of a clause works, here are examples that are very simple and get more complex.

Remember that with logic,  
if any part of `and` expression is false, the entire expression is `false`  
if any part of `or` expression is true, the entire expression is `true`

Remember that in Prolog  
`,` means logical `and`  
`;` means logical `or`  
`\+` means logical `not`  
`()` group logical expressions.

When SWI-Prolog returns a result that ends with `.` there are **no** more possible answers. When SWI-Prolog returns a result that ends with `;` there are more possible answers, so press the space bar to continue seeing them.

Prolog will keep seeking more answers as long as there is a possible way for the clause (logic) to be true.

Example clauses. Results of running follow.

```prolog
% true
clause_1 :-
    true.

 % false
 clause_2 :-
    false.

 % true and true
 clause_3 :-
    true,
    true.

 % true and false
 clause_4 :-
    true,
    false.

% false and true
clause_5 :-
   false,
   true.

 % true or true
 clause_6 :-
    true;
    true.

 % true or true but done with predicates other than true which output to the screen when executed.
clause_7 :-
    format('1',[]);
    format('2',[]).

 % true or false
 clause_8 :-
    true;
    false.

 % false or true
 clause_9 :-
    false;
    true.

 % false or false
 clause_10 :-
    false;
    false.

% not true
clause_11 :-
    \+ true.

% not false
clause_12 :-
    \+ false.

% true or true or false.
clause_13 :-
    true ; true ; false.

% true or false or true
clause_14 :-
    true ; false ; true.

% false or true or true
clause_15 :-
    false ; true ; true.

% (true or false or false) and true
clause_16 :-
    (true ; false ; false) , true.

% (true or false or false) and false
clause_17 :-
    (true ; false ; false) , false.

% (true or false or false) and not true
clause_18 :-
    (true ; false ; false) , \+ true.

```

Results of running.

```prolog
% true
?- clause_1.
true.

% false
?- clause_2.
false.

% true and true
?- clause_3.
true.

% true and false
?- clause_4.
false.

% false and true
% Prolog failed on the first part of the and so there was no need to do the second part as the result will always be false.
?- clause_5.
false.

% true or true
% Prolog will try and give you all the true solutions. In this case there are two and they are both true. See next example
?- clause_6.
true ;
true.

% true or true but using format/1.
?- clause_7.
1
true ;
2
true.

% true or false
% Prolog was successful on the first part of the or, so then it did the next part and failed.
% Prolog will keep seeking more answers as long as there is a possible way for the clause (logic) to be true.
?- clause_8.
true ;
false.

% false or true
% Prolog failed on the first part of the or but continued to the next part of the or and succeeded.
% Prolog will keep seeking more answers as long as there is a possible way for the clause (logic) to be true.
?- clause_9.
true.

% false or false
% Prolog failed on the first part of the or but continued to the next part of the or and failed also, so the result is false.
?- clause_10.
false.

% not true
?- clause_11.
false.

% not false
?- clause_12.
true.

% true or true or false.
% Prolog succeeded on the first part of the or, continued to the second part of the or which succeeded, and failed on the third part of the or.
% Prolog will keep seeking more answers as long as there is a possible way for the clause (logic) to be true.
?- clause_13.
true ;
true ;
false.

% true or false or true
% Prolog succeeded on the first part of the or, continued to the second part of the or which failed (which was not reported to the screen), and succeeded on the third part of the or.
% Prolog will keep seeking more answers as long as there is a possible way for the clause (logic) to be true.
?- clause_14.
true ;
true.

% false or true or true
% Prolog failed on the first part of the or (which was not reported to the screen), continued to the second part of the or which succeeded, and succeeded on the third part of the or.
?- clause_15.
true ;
true.

% (true or false or false) and true
% Prolog succeeded on the first part (true or false or false) using the first true, continued to the second part of the and which succeeded.
?- clause_16.
true ;
true.

% (true or false or false) and false
% Prolog succeeded on the first part (true or false or false) using the first true, continued to the second part of the and which failed.
% This is just a more complex version of clause_4.
?- clause_17.
false.

% (true or false or false) and not true
% Prolog succeeded on the first part (true or false or false) using the first true, continued to the second part of the and which succeeded, but then applied not to the second part which was true, which resulted in false which made the entire clause false.
% This is just a more complex version of clause_17.
% This clause is like the one you asked about in the question.
?- clause_18.
false.

```

Remember that logic expressions are expressions and just like math expressions, there are some substitutions that can be done that will not change the meaning of the code. However in Prolog there are also changes that can dramatically change the meaning of the code.

In other words, the only thing that matters to Prolog when it evaluates the statements in a clause are the statements/predicates are seen as a logic expression and the expression has to evaluate to true. All the rest is [side-effects](https://en.wikipedia.org/wiki/Side_effect_(computer_science)) of binding values to variables. So when you ask about the values concerning the location names, those don’t matter if the logic of the clause results in false. Only when the logic of a clause is true will the binding of the variables be returned.

* * *

@DaJa

When I wrote the code you are reviewing in detail I wrote it more to show that your `O-O` should have been `-O`. I knew the code was not the best but did not expect you to examine in such detail.

Here is a much better version that if you plan to study in detail would be better for you.

```prolog
travel_02(X,Y,Path) :-
    travel_02_helper(X,Y,[(start,X)],Path0),
    reverse(Path0,Path).

travel_02_mode(X,Y,Mode) :-
    (
        (byCar(X,Y),Mode = car)
    ;
        (byTrain(X,Y),Mode = train)
    ;
        (byPlane(X,Y),Mode = plane)
    ).

% base case
travel_02_helper(X,X,Path,Path).

% recursive case
travel_02_helper(X,Z,Path0,Path) :-
    travel_02_mode(X,Y,Mode),
    \+ member((_,Y),Path0),
    travel_02_helper(Y,Z,[(Mode,Y)|Path0],Path).

```

It returns the same results as the earlier version, but as I noted, logic expressions are like math expressions and certain substitutions are allowed.

```prolog
?- travel_02(valmont,raglan,Path).
Path = [(start, valmont), (car, saarbruecken), (train, frankfurt), (plane, bangkok), (plane, auckland), (car, hamilton), (car, raglan)] ;
Path = [(start, valmont), (car, saarbruecken), (train, frankfurt), (plane, singapore), (plane, auckland), (car, hamilton), (car, raglan)] ;
Path = [(start, valmont), (car, saarbruecken), (train, paris), (plane, losAngeles), (plane, auckland), (car, hamilton), (car, raglan)] ;
Path = [(start, valmont), (car, metz), (train, frankfurt), (plane, bangkok), (plane, auckland), (car, hamilton), (car, raglan)] ;
Path = [(start, valmont), (car, metz), (train, frankfurt), (plane, singapore), (plane, auckland), (car, hamilton), (car, raglan)] ;
Path = [(start, valmont), (car, metz), (train, paris), (plane, losAngeles), (plane, auckland), (car, hamilton), (car, raglan)] ;
false.

```

---

<div class="post-metadata">

**Author:** ![DaJa](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/daja/32/574_2.png) [@DaJa](https://swi-prolog.discourse.group/u/DaJa)\
**Post date:** [September 20, 2019, 4:23am UTC](https://swi-prolog.discourse.group/t/recursive-travel/1281/24 "2019-09-20T04:23:17Z")

</div>

You are such a nice person to have taken all this time and effort to explain basic concepts and write beautiful code 🙇‍♀️!

Thank you!

I went through it and I think I start to grasp how the recursive calls are bouncing back and forth.

1. I noticed that if I delete the below part of

```prolog
reverse(Path0,Path).

```

the code is not printing. I looked up reverse/2, and it should just reverse two atoms or lists. Why does reverse/2 does the printing of the path?

1. When the code calls \+member, is it an example of what is called negation as failure?

```prolog
Call: (15) lists:member((_6100, hamilton), [(plane, auckland), (plane, bangkok), (train, frankfurt), (car, saarbruecken), (start, valmont)]) ? creep
Fail: (15) lists:member((_6100, hamilton), [(plane, auckland), (plane, bangkok), (train, frankfurt), (car, saarbruecken), (start, valmont)]) ? creep

```

1. A question more specific to this code: why do we need this clause? Can’t we go twice through the same place with different vehicles? Could the list members be:

```prolog
 [(plane, auckland), (plane, bangkok), .... some other places...., (car, aukland)]

```

or does it not make logical sense at all?

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [September 20, 2019, 6:43am UTC](https://swi-prolog.discourse.group/t/recursive-travel/1281/25 "2019-09-20T06:43:09Z")

</div>

> [@DaJa](#):
>
> When the code calls \+member, is it an example of what is called negation as failure?

I assume you mean `\+ member`.

Negation, including negation-as-failure, can be a rather large topic. For now, you might consider this definition of `not_member`(\*):

```prolog
not_member(_, []).
not_member(X, [Y|Ys]) :-
    X \= Y, % or: \+ X = Y
    not_member(X, Ys).

```

(this can be derived from the definition of “member”:

```prolog
member(X, [X|_]).
member(X, [_|Ys]) :- member(X, Ys).

```

).

These uses of `\+` and `\=` depend on the arguments being “sufficiently ground”. For example `X=1, \+ member(X, [2,3,4])` works as expected but `\+ member(X, [2,3,4]), X=1` doesn’t. (There are ways to fix this, but that’s a somewhat more advanced topic.)

(\*) Yes, this definition of `not_member` is not optimal, but it suffices for now.

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [September 20, 2019, 8:48am UTC](https://swi-prolog.discourse.group/t/recursive-travel/1281/26 "2019-09-20T08:48:59Z")

</div>

> [@DaJa](#):
>
> Why does reverse/2 does the printing of the path?

`reverse/2` is not doing the printing of the paths.

What

```prolog
reverse(Path0,Path).

```

does is to take in the bound variable `Path0` which is a list and the unbound variable `Path` , and reverses the list in `Path0` creating a new path and then binds the new path to the variable `Path`.

When `Path` is then returned to

```prolog
travel_02(X,Y,Path) :-

```

in

```prolog
travel_02(X,Y,Path) :-
    travel_02_helper(X,Y,[(start,X)],Path0),
    reverse(Path0,Path).

```

because the clause succeeded, Prolog returns the result of the query/goal to the [Toplevel](https://www.metalevel.at/prolog/concepts) which displays the values bound to the unbound values when the goal was executed, in this case `Path`

* * *

In Prolog variables are [immutable](https://en.wikipedia.org/wiki/Immutable_object) meaning they can only be bound to a value once.

So how do you change the value of something that can only have a value assigned to it once?

You create a new variable which is unbound, then using the old value make the change and bind the new value to a new variable.

In Prolog the style of doing this is to use the same variable name, e.g. `Path`. When you want to pass in a value, change it and pass out the change with a predicate the custom is to name the in coming value with a variable name that ends with `0` then for each change in the predicate use the next number, e.g. `Path1`, `Path2`, etc. For the value being returned the name is not given a number, e.g. `Path`.

When you removed the line

```prolog
reverse(Path0,Path)

```

you broke the path for the variable, so the result from

```prolog
travel_02_helper(X,Y,[(start,X)],Path0),

```

which was

```prolog
Path0

```

was not returned to

```prolog
travel_02(X,Y,Path)

```

If you want to remove the line

```prolog
reverse(Path0,Path)

```

then you have to change the line

```prolog
travel_02_helper(X,Y,[(start,X)],Path0)

```

to

```prolog
travel_02_helper(X,Y,[(start,X)],Path)

```

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [September 20, 2019, 9:10am UTC](https://swi-prolog.discourse.group/t/recursive-travel/1281/27 "2019-09-20T09:10:47Z")

</div>

> [@DaJa](#):
>
> A question more specific to this code: why do we need this clause?

To avoid an infinite loop. In other words, if just these facts where in the code

```prolog
byBus(auckland,hamilton).
byBus(hamilton,auckland).

```

and this code is used

```prolog
travel_03(X,Y,Path) :-
    travel_03_helper(X,Y,[(start,X)],Path0),
    reverse(Path0,Path).

travel_03_mode(X,Y,Mode) :-
    (byBus(X,Y),Mode = bus).

travel_03_helper(X,X,Path,Path).
travel_03_helper(X,Z,Path0,Path) :-
    travel_03_mode(X,Y,Mode),
    travel_03_helper(Y,Z,[(Mode,Y)|Path0],Path).

```

and this query run

```prolog
?- travel_03(X,Y,Path).
X = Y,
Path = [(start, Y)] ;
X = auckland,
Y = hamilton,
Path = [(start, auckland), (bus, hamilton)] ;
X = Y, Y = auckland,
Path = [(start, auckland), (bus, hamilton), (bus, auckland)] ;
X = auckland,
Y = hamilton,
Path = [(start, auckland), (bus, hamilton), (bus, auckland), (bus, hamilton)] ;
X = Y, Y = auckland,
Path = [(start, auckland), (bus, hamilton), (bus, auckland), (bus, hamilton), (bus, auckland)] 

```

then the path could go from `auckland` to `hamilton` then from `hamilton` to `auckland` and keep repeating. Prolog would never be able to return an answer; it is stuck in this loop.

* * *

I am so use to doing graph traversal routines that I automatically add in the condition to avoid loops. Upon checking the data as given at LPN, they avoid the loop problem by not creating connections that can form a loop. The real world is not like this; this was a contrived exercise to teach recursion. I never tried the code without the statement.

```prolog
travel_04(X,Y,Path) :-
    travel_04_helper(X,Y,[(start,X)],Path0),
    reverse(Path0,Path).

travel_04_mode(X,Y,Mode) :-
    (
        (byCar(X,Y),Mode = car)
    ;
        (byTrain(X,Y),Mode = train)
    ;
        (byPlane(X,Y),Mode = plane)
    ).

travel_04_helper(X,X,Path,Path).
travel_04_helper(X,Z,Path0,Path) :-
    travel_04_mode(X,Y,Mode),
    % \+ member((_,Y),Path0),
    travel_04_helper(Y,Z,[(Mode,Y)|Path0],Path).

```

```prolog
?- travel_04(valmont,raglan,Path).
Path = [(start, valmont), (car, saarbruecken), (train, frankfurt), (plane, bangkok), (plane, auckland), (car, hamilton), (car, raglan)] ;
Path = [(start, valmont), (car, saarbruecken), (train, frankfurt), (plane, singapore), (plane, auckland), (car, hamilton), (car, raglan)] ;
Path = [(start, valmont), (car, saarbruecken), (train, paris), (plane, losAngeles), (plane, auckland), (car, hamilton), (car, raglan)] ;
Path = [(start, valmont), (car, metz), (train, frankfurt), (plane, bangkok), (plane, auckland), (car, hamilton), (car, raglan)] ;
Path = [(start, valmont), (car, metz), (train, frankfurt), (plane, singapore), (plane, auckland), (car, hamilton), (car, raglan)] ;
Path = [(start, valmont), (car, metz), (train, paris), (plane, losAngeles), (plane, auckland), (car, hamilton), (car, raglan)] ;
false.

```

but as shown, and as you have quested, it is not needed for this exercise.

If you look at graph traversal [pseudocode](https://en.wikipedia.org/wiki/Graph_traversal#Pseudocode) you will see that it also checks to see if a node/location was previously visited (`label v as explored`), and if so, does not go through the node/location again.

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [September 20, 2019, 9:34am UTC](https://swi-prolog.discourse.group/t/recursive-travel/1281/28 "2019-09-20T09:34:08Z")

</div>

> [@DaJa](#):
>
> I think I start to grasp how the recursive calls are bouncing back and forth.

Do not think of it as bouncing back and forth, think of it as pushing a new context on a stack with new variables for the called goal/predicate.

If you look at the trace output

```prolog

[trace] ?- travel_01(valmont,frankfurt,Path).
   Call: (10) travel_01(valmont, frankfurt, _596)
   Unify: (10) travel_01(valmont, frankfurt, _596)
   Call: (11) travel_01_helper(valmont, frankfurt, [(start, valmont)], _856)
   Unify: (11) travel_01_helper(valmont, frankfurt, [(start, valmont)], [(_842, frankfurt), (start, valmont)])
   Call: (12) byCar(valmont, frankfurt)
   Fail: (12) byCar(valmont, frankfurt)
   Redo: (11) travel_01_helper(valmont, frankfurt, [(start, valmont)], [(_842, frankfurt), (start, valmont)])
   Call: (12) byTrain(valmont, frankfurt)
   Fail: (12) byTrain(valmont, frankfurt)
   Redo: (11) travel_01_helper(valmont, frankfurt, [(start, valmont)], [(_842, frankfurt), (start, valmont)])
   Call: (12) byPlane(valmont, frankfurt)
   Fail: (12) byPlane(valmont, frankfurt)
   Redo: (11) travel_01_helper(valmont, frankfurt, [(start, valmont)], _856)
   Unify: (11) travel_01_helper(valmont, frankfurt, [(start, valmont)], _856)
   Call: (12) byCar(valmont, _852)
   Unify: (12) byCar(valmont, saarbruecken)
   Exit: (12) byCar(valmont, saarbruecken)
   Call: (12) _850=car
   Exit: (12) car=car
   Call: (12) travel_01_helper(saarbruecken, frankfurt, [(car, saarbruecken), (start, valmont)], _868)
   Unify: (12) travel_01_helper(saarbruecken, frankfurt, [(car, saarbruecken), (start, valmont)], [(_854, frankfurt), (car, saarbruecken), (start, valmont)])
   Call: (13) byCar(saarbruecken, frankfurt)
   Fail: (13) byCar(saarbruecken, frankfurt)
   Redo: (12) travel_01_helper(saarbruecken, frankfurt, [(car, saarbruecken), (start, valmont)], [(_854, frankfurt), (car, saarbruecken), (start, valmont)])
   Call: (13) byTrain(saarbruecken, frankfurt)
   Unify: (13) byTrain(saarbruecken, frankfurt)
   Exit: (13) byTrain(saarbruecken, frankfurt)
   Call: (13) _854=train
   Exit: (13) train=train
   Call: (13) lists:member((_860, frankfurt), [(car, saarbruecken), (start, valmont)])
   Unify: (13) lists:member((_860, frankfurt), [(car, saarbruecken), (start, valmont)])
   Fail: (13) lists:member((_860, frankfurt), [(car, saarbruecken), (start, valmont)])
   Redo: (12) travel_01_helper(saarbruecken, frankfurt, [(car, saarbruecken), (start, valmont)], [(train, frankfurt), (car, saarbruecken), (start, valmont)])
   Exit: (12) travel_01_helper(saarbruecken, frankfurt, [(car, saarbruecken), (start, valmont)], [(train, frankfurt), (car, saarbruecken), (start, valmont)])
   Exit: (11) travel_01_helper(valmont, frankfurt, [(start, valmont)], [(train, frankfurt), (car, saarbruecken), (start, valmont)])
   Call: (11) lists:reverse([(train, frankfurt), (car, saarbruecken), (start, valmont)], _596)
   Unify: (11) lists:reverse([(train, frankfurt), (car, saarbruecken), (start, valmont)], _596)
   Exit: (11) lists:reverse([(train, frankfurt), (car, saarbruecken), (start, valmont)], [(start, valmont), (car, saarbruecken), (train, frankfurt)])
   Exit: (10) travel_01(valmont, frankfurt, [(start, valmont), (car, saarbruecken), (train, frankfurt)])
Path = [(start, valmont), (car, saarbruecken), (train, frankfurt)] .

```

Notice that the numbers in `()` are going up each time a call is made to the same predicate and then go down when the predicate is exited. When I was learning to look at trace output I would indent the output to line up the numbers in the `()`, then it was easier to understand.

Also notice that with each call to the same predicate, the unbound variables get a different system generated variable.

```prolog
Call: (11) travel_01_helper(valmont, frankfurt,[(start, valmont)],_856)
Call: (12) travel_01_helper(saarbruecken, frankfurt, [(car, saarbruecken), (start, valmont)], _868)

```

Notice that the unbound variable `Path` in each call for

```prolog
travel_02_helper(X,Z,Path0,Path)

```

is a different system generated variable, e.g.

```prolog
_856 and _868

```

---

<div class="post-metadata">

**Author:** ![Wisermans](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/wisermans/32/3113_2.png) [@Wisermans](https://swi-prolog.discourse.group/u/Wisermans)\
**Post date:** [June 23, 2021, 1:08pm UTC](https://swi-prolog.discourse.group/t/recursive-travel/1281/29 "2021-06-23T13:08:06Z")

</div>

Some nice examples with different ways to proceed can be found on [StackOverFlow](https://stackoverflow.com/questions/67747443/prolog-how-would-i-recursively-build-a-list)

[Previous page](https://swi-prolog.discourse.group/t/recursive-travel/1281.md?page=1)
