Previous Up Next

3.2  Predicates, Goals and Queries

Where other programming languages have procedures and functions, Prolog and ECLiPSe have predicates. A predicate is something that has a truth value, so it is similar to a function with a boolean result. A predicate definition simply defines what is true. A predicate invocation (or call) checks whether something is true or false. A simple example is the predicate integer/1, which has a built-in definition. It can be called to check whether something is an integer:

integer(123)           is true
integer(atom)          is false
integer([1,2])         is false
A predicate call like the above is also called a goal. A starting goal that the user of a program provides is called a query. To show queries and their results, we will from now on use the following notation:

?- integer(123).
Yes.
?- integer(atom).
No.
?- integer([1,2]).
No.
A query can simply be typed at the eclipse prompt, or entered into the query field in a tkeclipse window. Note that it is not necessary to enter the ?- prefix. On a console input, is however necessary to terminate the query with a full-stop (a dot followed by a newline). After executing the query, the system will print one of the answers Yes or No.

3.2.1  Conjunction and Disjunction

Goals can be combined to form conjunctions (AND) or disjunctions (OR). Because this is so common, Prolog uses the comma for AND and the semicolon for OR. The following shows two examples of conjunction, the first one is true because both conjuncts are true, the second is false:

?- integer(5), integer(7).
Yes.
?- integer(5), integer(hello).
No.
In contrast, a disjunction is only false if both disjuncts are false:

?- ( integer(hello) ; integer(5) ).
Yes.
?- ( integer(hello) ; integer(world) ).
No.
As in this example, it is advisable to always surround disjunctions with parentheses. While not strictly necessary in this example, they are often required to clarify the structure.

In practice, when answering queries with disjunctions, the system will actually give a separate Yes answer for every way in which the query can be satisfied (i.e. proven to be true). For example, the following disjunction can be satisfied in two ways, therefore system will give two Yes answers:

?- ( integer(5) ; integer(7) ).
Yes (0.00s cpu, solution 1, maybe more)
Yes (0.02s cpu, solution 2)
The second answer will only be given after the user has explicitely asked for more solutions. Sometimes the system cannot decide whether an answer is the last one. In that case, asking for more solutions may lead to an alternative No answer, like in the following example:

?- ( integer(5) ; integer(hello) ).
Yes (0.00s cpu, solution 1, maybe more)
No (0.02s cpu)
Of course, as long as there was at least one Yes answer, the query as a whole was true.


Previous Up Next