Facts with Arguments

More complicated facts consist of a relation and the items that this refers to. These items are called arguments. Facts can have arbitrary number of arguments from zero upwards. A general model is shown below:


relation(<argument1>,<argument2>,....,<argumentN> ).

Relation names must begin with a lowercase letter


likes(john,mary).  

The above fact says that a relationship likes links john and mary. This fact may be read as either john likes mary or mary likes john. This reversibility can be very useful to the programmer, however it can also lead to some pitfalls. Thus you should always be clear and consistent how you intend to interpret the relation and when.

Finally, remember names of the relations are defined by you. With the exception of a few relations that the system has built into it, the system only knows about relations that you tell it about.

Facts with Arguments Examples 1

An example database. It details who eats what in some world model.


eats(fred,oranges).                           /* "Fred eats oranges" */

eats(fred,t_bone_steaks). /* "Fred eats T-bone steaks" */

eats(tony,apples). /* "Tony eats apples" */

eats(john,apples). /* "John eats apples" */

eats(john,grapefruit). /* "John eats grapefruit" */

If we now ask some queries we would get the following interaction:


?- eats(fred,oranges).         /* does this match anything in the database? */

yes /* yes, matches the first clause in the database */

?- eats(john,apples). /* do we have a fact that says john eats apples? */

yes /* yes we do, clause 4 of our eats database */

?- eats(mike,apples). /* how about this query, does mike eat apples */

no /* not according to the above database. */

?- eats(fred,apples). /* does fred eat apples */

no /* again no, we don't know whether fred eats apples */

Facts with Arguments Examples 2

Let us consider another database. This time we will use the predicate age to indicate the ages of various individuals.


age(john,32).                 /*  John is 32 years old */

age(agnes,41). /* Agnes is 41 */

age(george,72). /* George is 72 */

age(ian,2). /* Ian is 2 */

age(thomas,25). /* Thomas is 25 */

If we now ask some queries we would get the following interaction:


?- age(ian,2).           /* is Ian 2 years old? */

yes /* yes, matches against the fourth clause of age */

?- agnes(41). /* for some relation agnes are they 41 */

no /* No. In the database above we only know about the relation */

/* age, not about the relation agnes, so the query fails */

?- age(ian,two) /* is Ian two years old? */

no /* No. two and 2 are not the same and therefore don't match */

This is the end of the facts with arguments topic.


Next Topic . Return to Introduction Menu