There is a prolog program. Let's look at it again.
%% exercise
assign(X, Y) -->
left(X), [:=], right(Y), [;].
left(X, Start, End) :-
[ X | End ] = Start,
atomic(X).
right(Y, Start, End) :-
[ Y | End ] = Start,
number(Y).
I run it as follow.
| ?- assign(x, 3, A, []).
A = [x,:=,3,;]
yes
| ?- assign(L, R, [x, :=, 3, ;], T).
L = x
R = 3
T = []
yes
| ?- assign(L, R, [x, :=, 3, ;, y, :=, 2, ;], T).
L = x
R = 3
T = [y,:=,2,;]
yes
Every predicate has a alike following control flow.
+-----------+
call -->| |--> exit
| predicate |
fail <--| |<-- redo
+-----------+
|
exception <----+
repeat:
+-----------+
call -->| +->|--> exit
| repeat | |
fail <--| +--|<-- redo
+-----------+
|
exception <----+
fail:
+-----------+
call -->|--+ |--> exit
| | fail |
fail <--|<-+ |<-- redo
+-----------+
|
exception <----+
So, repeat, ..., fail. will cause a loop
%% exercise
assign(X, Y) -->
left(X), [:=], right(Y), [;].
left(X, Start, End) :-
[ X | End ] = Start,
atomic(X).
right(Y, Start, End) :-
[ Y | End ] = Start,
number(Y).
I run it as follow.
| ?- assign(x, 3, A, []).
A = [x,:=,3,;]
yes
| ?- assign(L, R, [x, :=, 3, ;], T).
L = x
R = 3
T = []
yes
| ?- assign(L, R, [x, :=, 3, ;, y, :=, 2, ;], T).
L = x
R = 3
T = [y,:=,2,;]
yes
Every predicate has a alike following control flow.
+-----------+
call -->| |--> exit
| predicate |
fail <--| |<-- redo
+-----------+
|
exception <----+
repeat:
+-----------+
call -->| +->|--> exit
| repeat | |
fail <--| +--|<-- redo
+-----------+
|
exception <----+
fail:
+-----------+
call -->|--+ |--> exit
| | fail |
fail <--|<-+ |<-- redo
+-----------+
|
exception <----+
So, repeat, ..., fail. will cause a loop