Next: Action Definitions, Previous: Action Sequences, Up: Programs [Contents][Index]
A relation definition consists of a sequence of rules (clauses) of the form
Head
::
Commit<=
Body
Head is a compound term of the form RelNm(Term1,...,Termk),
where RelNm is usually an atom but might also be a compound term when defining a higher-order relation. Function expressions are not allowed in the arguments of the rule head. Commit
is a simple conjunction, and Body is a complex conjunction. Both the
::
Commit and <=
Body
parts of the rule are optional.
The rules for each relation name RelNm must be contiguous and have the same
number of arguments in the Head.
When a goal Goal with the same functor and arity as Head
is called, the rules of the relation are tried in order. If Goal
unifies with the head of the rule then the Commit part is called.
If that succeeds then this rule is committed to (i.e. no subsequent rules
are tried on backtracking) and Goal succeeds if and only if
Body succeeds. If Body is not present it is treated as being
the goal true
.
If Commit is not present then Goal succeeds if Body succeeds but, on backtracking, subsequent rules will be tried.
The rule has the same semantics as the Prolog rule
Head
:-
Commit, !,
Body
if Commit is given and
Head
:-
Body
otherwise.
Note, however, that cut (!
) is not part of QuLog.
As examples, the definitions of the relations greater
and sum_list
are given below.
rel greater(!num, !num, ?num) greater(A, B, C) :: A > B <= C = A greater(A, B, C) :: B >= A <= C = B rel sum_list(!list(num), ?num) sum_list([], 0) sum_list([H,..T], N) <= sum_list(T, M) & N = H+M
Note that in N = H+M
, H+M
is evaluated before unification
and that the second rule of greater
could have been written as
greater(A, B, B)
because the second rule will not be chosen (on backtracking) if A > B
.
Next: Action Definitions, Previous: Action Sequences, Up: Programs [Contents][Index]