Next: Standard PROLOG Indexing Up: An Introduction to Previous: Compiling a Single

Combining Multiple Clauses Into a Procedure

The two clauses in the previous section define the binary relation less:


  less(0,N).
  less(s(M), s(N)) :- less(M, N).

The WAM code sequences for these two clauses can be combined without any changes to form the WAM code for the complete procedure:


  less/2
      try_me_else 2
      get_constant 0, X1
      proceed

  2   trust_me_else_fail
      allocate 0

      get_structure s/1, X1   % less(s(
      unify_variable X3       %        M),
      get_structure s/1, X2   %            s(
      unify_variable X4       %              N)) :-

      put_value X3, X1        % 
      put_value X4, X2
      call less/2, 0          %                      less(M, N).

      deallocate
      proceed

Three WAM instructions are needed for combining clauses in this way:

try_me_else L:
allocate a new choice point frame on the stack setting its next clause field to L
retry_me_else L:
having backtracked to the current choice point, reset all the necessary information from it and update its next clause field to L
trust_me_else_fail L:
having backtracked to the current choice point, reset all the necessary information from it, discard it, and reset the latest choice point (the B register) to its predecessor

It is not necessary for the reader to understand the way these instructions work internally. It is only important to realize that for all queries and calling procedures always all clauses of a procedure are ultimately ``tried''.

For instance, the query less(0,s(0)) compiles to


      put_constant 0, X1
      put_structure s/1, X2
      unify_constant 0
      execute less/2

It first tries the fact (succeeding) and on backtracking tries the rule (failing).

Preparing the use of indexing header code in the next section, let us note that try L, retry L, trust L can be used instead of try_me_else L, retry_me_else L, and trust_me_else_fail L by the following equivalence:



Next: Standard PROLOG Indexing Up: An Introduction to Previous: Compiling a Single


Michael Sintek - sintek@dfki.uni-kl.de