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:
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: