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

3.10.4 Relation Definitions

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

Head :: Commit <= Body

where Head is an atom or simple compound term, Commit is a conjunct of goals, and Body is a conjunct of goals. Conjuncts are separated by &. Both the :: Commit and <= Body parts of the rule are optional. The heads of each rule of a relation have the same functor and arity.

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.

greater: (!num, !num, ?num) <=
greater(A, B, C) :: A > B <= C = A
greater(A, B, C) :: B > A <= C = B

sum_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]