10.02.2013 Views

A User's Guide to gringo, clasp, clingo, and iclingo

A User's Guide to gringo, clasp, clingo, and iclingo

A User's Guide to gringo, clasp, clingo, and iclingo

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

of the function symbol is compared lexicographically. If again the name differs, then<br />

arguments are compared component wise. Finally, integers are always smaller than<br />

constants <strong>and</strong> constants are always smaller than function symbols. �<br />

Note that a built-in comparison predicate cannot bind variables, i.e., when checking<br />

whether a rule is safe, comparison predicates are not considered <strong>to</strong> be positive.<br />

3.1.6 Assignments<br />

The built-in predicates := <strong>and</strong> = can be used in the body of a rule <strong>to</strong> unify a term on<br />

their right-h<strong>and</strong> side <strong>to</strong> a (non-ground) term or variable on its left-h<strong>and</strong> side, respectively.<br />

Example 3.4. The next program demonstrates how terms can be assigned <strong>to</strong> variables: The unique answer set of the<br />

1 num(1). num(2). num(3). num(4). num(5).<br />

program is obtained via call:<br />

<strong>gringo</strong> -t<br />

3 squares(XX,YY,Z) :-<br />

4 XX := X*X, YY := Y*Y, Z := XX+YY, Y1 := Y+1,<br />

5 Y1*Y1 == Z, num(X), num(Y), X < Y.<br />

Line 3 contains four assignments, where the right-h<strong>and</strong> sides directly or indirectly depend<br />

on X <strong>and</strong> Y. These two variables are bound in Line 5 via a<strong>to</strong>ms of predicate num/1.<br />

Also observe the different usage <strong>and</strong> role of built-in comparison predicate ==. �<br />

Example 3.5. The second program demonstrates the usage of :=, which allows for<br />

terms on the left h<strong>and</strong> side: The unique answer set of the<br />

1 sym(f(a,1,2)). sym(f(a,1,3)). sym(f(b,d)).<br />

2 sym((a,1,2)). sym((a,1,3)). sym((b,d)).<br />

4 unifyf(X) :- f(a,X,X+1) := F, sym(F).<br />

5 unifyt(X) :- (a,X,X+1) := T, sym(T).<br />

Here the term f(a,X,X+1) is unified with every function symbol provided by sym/1.<br />

Note the usage of X+1 in the term. <strong>gringo</strong> does not try <strong>to</strong> unify any term containing<br />

arithmetic but in this example X occurs also directly as second argument of the argument<br />

<strong>and</strong> can thus be unified with. The term X + 1 is merely a test that is deferred <strong>and</strong><br />

checked later. For example, the fourth line is equivalent <strong>to</strong>:<br />

6 unifyf(X) :- f(a,X,Y) := F, sym(F), Y == X + 1.<br />

Note that assignments <strong>to</strong> some extent can bind variables. Of course cyclic assignments<br />

cannot bind variables. For example the rule p(X) :- X = Y, Y = X. is rejected<br />

by <strong>gringo</strong>. Either X or Y has <strong>to</strong> be provided by some positive predicate in this case.<br />

Additionally, unification is restricted <strong>to</strong> ground terms on the right h<strong>and</strong> side of the<br />

assignment, that is, all variables on the right h<strong>and</strong> side have <strong>to</strong> be bound by some other<br />

predicate.<br />

3.1.7 Intervals<br />

In Line 1 of Example 3.4, there are five facts num(k) over consecutive integers k. For<br />

a more compact representation, <strong>gringo</strong> <strong>and</strong> <strong>clingo</strong> support integer intervals of the<br />

14<br />

�<br />

program is obtained via call:<br />

<strong>gringo</strong> -t

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!