Search code examples
prologiso-prolog

SWI Prolog description of call/1: "clauses may have variables as subclauses"?


The description of call/1 says:

call(:Goal)

Invoke Goal as a goal. Note that clauses may have variables as subclauses, which is identical to call/1.

I don't understand "clauses may have variables as subclauses".

Can anyone give an example?


Solution

  • Body conversion (7.6.2 Converting a term to the body of a clause) à la ISO core standard requires that variables directly inside control constructs inside a body of a clause are wrapped by call/1.

    Here is an example:

    ?- [user].
    and(X,Y) :- X,Y.
    ^D
    ?- listing.
    and(X,Y) :- call(X), call(Y).
    

    The effect is that calling a ! has no effect, since the cut is confined inside call/1.

    This is seen here:

    ?- [user].
    p(a).
    p(b).
    ?- p(X), !.
    X = a
    ?- and(p(X),!).
    X = a ;
    X = b
    

    Edit 31.12.2019:
    A Prolog system might want to add (*->)/2 (soft cut) to those control constructs whose arguments are converted thus amending 7.6.2.


    From ISO/IEC 13211-1: 1995 Prolog

    7.6.2 Converting a term to the body of a clause

    A term T can be converted to a goal G which is the body of a clause:

    a)

    If T is a variable then G is the control construct call (7.8.3), whose argument is T.

    b)

    If T is a term whose principal functor appears in table 9 then G is the corresponding control construct. If the principal functor of T is call/1 or catch/3 or throw/1 then the arguments of T and G are identical, else if the principal functor of T is (',')/2 or (;)/2 or (->)/2 then each argument of T shall also be converted to a goal.

    c)

    If T is an atom or compound term whose principal functor FT does not appear in table 9 then G is a predication whose predicate indicator is PT, and the arguments, if any, of T and G are identical.

    And

    7.8.3 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.

    And

    Table 9 on page 37: Principal functors and control constructs

    • (',')/2 goal arguments converted
    • (;)/2 goal arguments converted
    • (->)/2 goal arguments converted
    • !/0
    • call/1 goal arguments not converted
    • true/0
    • fail/0
    • catch/3 goal arguments not converted
    • throw/1