What is the difference between goals without and without call/2 in prolog?

89 views Asked by At

What is the difference between these two goals?

foo(A) :- A, !, fail.
foo(A).
foo(A) :- call(A), !, fail.
foo(A).

They seem to behave identically? What is the purpose of call/1 and is there an example of when it matters?

1

There are 1 answers

6
gusbro On

There is no difference in the output WAM code generated by those snippets of code, as evidenced by issuing

gplc -w snippet1.pl
gplc -w snippet2.pl

and checking the generated wbc file (where snippetN.pl contains either sample code). So it seems is a programmer's choice which one to use. It might be useful to use call/1 for portability reasons.

Recall however the existence of call/N with N > 1 which is useful if you want to call a goal which has arguments.

For the curious, this is the generated wam file: predicate(foo/1,1,static,private,monofile,global,2).

clause(:-(foo(A),','(A,','(!,fail))),[
    allocate(1),
    get_variable(y(0),1),
    put_atom(foo,1),
    put_integer(1,2),
    put_atom(true,3),
    call(('$call')/4),
    cut(y(0)),
    fail]).


clause(foo(_),[
    proceed]).

The only difference is in the header for the clause which in the other snippet reads:

clause(:-(foo(A),','(call(A),','(!,fail))),[