Next: , Previous: , Up: Programs   [Contents][Index]


3.9.5 Relation Definitions

A relation definition consists of a sequence of rules (clauses) of the form

Head :: Commit <= Body

Head is simple compound term of the form RelNm(Term1,...,Termk), where RelNm is an atom. 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 have 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

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, C) <= B > A & C = B


Next: , Previous: , Up: Programs   [Contents][Index]