Control constructs

These predicates have the effect of controlling the execution flow of a Prolog demonstration.

1. call/1

call(G) is true iff G represents a goal which is true. When G contains ! as a subgoal, the effect of ! shall not extend outside G.

Executing a call has the effect that (a) if G should fail, then the call will fail, and (b) G can be re-executed, and (c) any cut inside G is local to G.

Templates and modes for the predicate are as follows:

call(+callable_term)

1.1 Example tests

Let's start with some simple tests verifying success of failure of single goals.

GoalTheorysuccess(String goal,String theory)
call(!).nulltrue

GoalTheorysuccess(String goal,String theory)
call(fail). falsenullfalse
call((fail, X)). falsenullfalse
call((fail, call(1))).nullfalse
b(_).b(X) :- Y = (write(X), X), call(Y). a(1). a(2).false
b(3).b(X) :- Y = (write(X), X), call(Y). a(1). a(2).false

Tests With Exception

GoalTheorysuccess(String goal)Type Of Error
call((write(3), X)).nulltrue
call((write(3), call(1))).nulltrue
call(X).nulltrue
call(1).nulltrue
call((fail, 1)).nulltrue
call((write(3), 1)).nulltrue
call((1 ; true)).nulltrue

2. (;)/2 (disjunction)

';'(Either, Or) is true iff Either is true or Or is true.

Note that (;)/2 is re-executable.

Executing a disjunction has the effect that (a) if Either shoud fail, then Or will be executed on backtracking, and (b) disjunction is transparent to cut.

Templates and modes for the predicate are as follows:

';'(goal, goal)

Note that ';' is an predefined infix operator.

1.1 Example tests

Let's start with some simple tests verifying success of failure of single goals.

GoalTheorysuccess(String goal,String theory)
';'(true, fail).nulltrue
';'(!, call(3)).nulltrue

GoalTheorysuccess(String goal,String theory)
';'((!, fail), true).nullfalse
Goal Theory Variable Solution success(String goal,String theory,String variable,Strng solution)
';'((X = 1, !), X = 2).nullX1true
','(';'(X = 1, X = 2), ';'(true, !)).nullX1true

3. (->)/2 (if-then)

'->'(If, Then) is true iff (1) If is true, and (2) Then is true for the first solution of If.

Note that (->)/2 is re-executable.

Executing an if-then has the effect that (a) if If shoud fail, then if-then will fail, and (b) if If should succeed, then Then will be executed, and (c) if If should succeed and Then later fails, the If will not be re-executed because of the cut which has been executed, and (d) the If in an if-then is not transparent to cut, and (e) a cut in Then is transparent to if-then.

Templates and modes for the predicate are as follows:

'->'(goal, goal)

Note that '->' is an predefined infix operator.

1.1 Example tests

Let's start with some simple tests verifying success of failure of single goals.

GoalTheorysuccess(String goal,String theory)
'->'(true, true).nulltrue

GoalTheorysuccess(String goal,String theory)
'->'(true, fail).nullfalse
'->'(fail, true).nullfalse
Goal Theory Variable Solution success(String goal,String theory,String variable,Strng solution)
'->'(true, X = 1).nullX1true
'->'(';'(X = 1, X = 2), true).nullX1true
'->'(true, ';'(X = 1, X = 2)).nullX1true
'->'(true, ';'(X = 1, X = 2)).nullX2true

No error or exception is thrown by the engine while solving a query with (->)/2.

4. (;)/2 (if-then-else)

(;)/2 serves two different functions depending on whether or not the first argument is a compound term with functor (->)/2.

';'('->'(If, Then), Else) is true iff (1a) If is true, and (1b) Then is true for the first solution of If, or (2) If is false and Else is true.

Note that (;)/2 is re-executable. The cut prevents and if-then-else from being re-executed a second time.

Executing an if-then-else has the effect that (a) if If shoud fail, then if-then-else will be re-executed, and (b) if If should succeed, then Then will be executed, and (c) if If should succeed and Then later fails, the if-the-else will not be re-executed because of the cut which has been executed, and (d) the If in an if-then-else is not transparent to cut, and (e) a cut in Then is transparent to Then.

Re-executing an if-then-else has the effect that (a) the Else will be executed, and (b) if Else later fails, the if-then-else will not be re-executed again because of the cut which has been executed, and (c) a cut in Else is transparent to Else.

Templates and modes for the predicate are as follows:

';'('->'(goal, goal), goal)

Note that ';' '->' are predefined infix operators so that (If -> Then ; Else) is parsed as ';'('->'(If, Then), Else).

1.1 Example tests

Let's start with some simple tests verifying success of failure of single goals.

GoalTheorysuccess(String goal,String theory)
';'('->'(true, true), fail).nulltrue
';'('->'(fail, true), true).nulltrue
';'('->'((!, fail), true), true).nulltrue

GoalTheorysuccess(String goal,String theory)
';'('->'(true, fail), fail).nullfalse
';'('->'(fail, true), fail).nullfalse
Goal Theory Variable Solution success(String goal,String theory,String variable,Strng solution)
';'('->'(true, X = 1), X = 2).nullX1true
';'('->'(fail, X = 1), X = 2).nullX2true
';'('->'(true, ';'(X = 1, X = 2)), true).nullX1true
';'('->'(true, ';'(X = 1, X = 2)), true).nullX2true
';'('->'(';'(X = 1, X = 2), true), true).nullX1true