These notes are simply my slightly cut down, more comprehensible version of the Learn Prolog Now book by Patrick Blackburn, Johan Bos and Kristina Striegnitz.
Facts, Rules And Queries
A Prolog programs are Knowledge bases.
Prolog had only three basic constructs: Facts, rules and queries
Knowledge bases are made up of Facts and Rules. Here is an example KB made with just five facts.
woman(mia). woman(jody). woman(yolanda). playsAirGuitar(jody). party.
Note that the first letters of the names (mia), properties (woman) and propositions (party) are all lower case.
So how can we use this? Well by using queries! If we wanted to see if mia was a woman, we could ask
prolog will reply to this with
But thats easy, the fact mia is a woman is explicitly recored in the knowledge base! Anyway, We don't actually type in the ?-, thats already supplied by the implementation of prolog we are using. Also, we need the full stop, or prolog will keep on working on the query until it finds one.
Now, what if we try something which is false, like this
now prolog comes back to us with a big fat
This is because the KB is so simple there is no way for prolog to know if she does, so it concludes that she doesn't, and returns no.
So to recap, A prolog program is a knowledge base made up of Facts and Rules (Still to be covered) and you can use the KB using queries like in the format ?- testquery.
Now if we wanted to make a more complex knowledge base, which of course you do, you can use rules. Rules state that information is conditionally true.
For example consider this KB
happy(yolanda). listens2Music(mia). listens2Music(yolanda):-happy(yolanda). playsAirGuitar(mia):-listenes2Music(mia). playsAirGuitar(yolanda):-listens2Music(yolanda).
in this KB there are only two facts, the last 3 lines are rules.
The first rule, for example, says that yolanda listens to music if she is happy.
In general :- should be read as 'if'. The part to the left of the :- is known as the head, the part to the right is known as the body. And in general rules says if the body is true, then the head is true too.
So the query
returns yes, as although it cant find an explicit fact which says mia plays air guitar it can find a rule which says
and, prolog also knows that
There for it knows that mia is in fact playing air guitar, and it returns true.
The facts and rules in a KB are known as clauses. So you can say that the KB contains 5 clauses. You could also say it contains 3 predicates, These are:
listens2Music happy playsAirGuitar
And predicates are the things which we really care about in a prolog program.
Now rules can also contain more than one item in there body, For example think of this KB
happy(vincent). listens2Music(butch). playsAirGuitar(Vincent):- listens2Music(vincent), happy(vincent). playsAirGuitar(butch):- happy(butch). playsAirGuitar(butch):- listens2Music(butch).
Now, in this example there is a rule with 2 parts two its body, this is known as having 2 goals. Each goal is separated by a coma and this is how prolog expresses conjunction (and).
There are also 2 rules which share the same head, this is prologs method of disjunction (either, or), so playsAirGuitar(butch) returns true, as listening2Music(butch) is defined.
There is another way to define disjunction and it involves the semi colon (;). For example the rules could be rewritten as
playsAirGuitar(butch):- happy(butch); listens2Music(butch).
The semicolon means 'or'. So if it better to use a semicolon or two rules? Well semicolons can make messy code, but are also more efficient as prolog only has to deal with one rule.
So far we have learnt that:
- :- means implication
- , means conjunction
- ; means disjunction
so it should be quite clear that logic is rather important to Prolog (Programming with logic)
Now, If I defined another KB as:
woman(mia). woman(jody). woman(yolanda). loves(vincent,mia). loves(marsellus,mia). loves(pumpkin,honey_bunny). loves(honey_bunny, pumpkin).
Now other than the use of two names in the relation loves, this KB is nothing special. But the way were going to query it is. We are going to use Variables to get back all possible answers. For example:
Here the X is a variable (Anything beginning with an upper-case letter is a variable, which is why everything has had to start with lower case in the KB's so far) and is acting as a placeholder for information. So prolog will answer the above query by working from top to bottom trying to unify with
woman(X). Now the first fact which it unifies with is
woman(mia). so it instantiates or binds X to mia and returns
X = mia
So its not just returned yes or no, its actually returned the variable binding. But thats not it, Variables can be unified with different things and there is still more information about women in the KB. You can access this information with the semicolon:
X = mia ;
As ; means or, so this query means 'are there any alternatives?'. So prolog works through from where it stopped until it finds something else that unifies. In this case it returns:
X = mia ; X = jody
If we press ; again, we get:
X = mia ; X = jody ; X = yolanda
What happens if we press ; a third time? As there are no other facts about women prolog returns 'no'.
What about if we try a more complex query like this.
?- loves(marsellus, X), woman(X).
Remember that , means and so there has to be something which unify with both of these queries. As we know, marsellus loves mia, and mia is woman, so X is bound to mia and prolog returns
X = mia
Now for one last KB.
loves(vincent, mia). loves(marsellus, mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin). jealous(X,Y):- loves(X,Z), loves(Y,Z).
The last query asks if there are two people (X and Y) who both love the same person (Z). So if we ask
`?- jealous(marsellus, W).
we get back
W = vincent
as marsellus and vincent both love mia.
So far we have seen facts, rules and queries, but what are they built out of? Well there built from terms. There are 4 kinds of terms in Prolog:
- Complex Terms
Atoms and Numbers are grouped together as constants, and constants and variables together make up the simple terms of prolog.
Prolog uses lower-case letter, Upper-Case letter, digits, the underscore and some special characters. A string is an unbroken sequence of characters.
An Atom is either:
1) A string of characters starting with lower case letters.
2) An arbitrary sequence of characters enclosed in single quotes. The text inside the quotes is known as the atom name.
3) A string of special characters. e.g.: @= and ====> and ; and :-
Prolog rarely uses real floating point numbers, but integers are quite useful for some tasks like counting.
A variable is a string beginning with an Upper-Case letter or an underscore. The variable _ is a special case and is the anonymous variable. This is talked about later in the book.
Now Constants, Numbers and Variables are the building blocks, but they can be used together to make complex terms (often called structures). They are made from a functor followed by a sequence of arguments in brackets. The functor must be an atom, but arguments can be anything.
A good example of a complex term is
in this example playsAirGuitar is the functor and jody is the argument. Complex terms can be nested, so you can end up with a term like this:
The number of arguments that a complex term has is called its arity. The above example has 2 arguments, so it is said to be 2 arity. Complex terms with different arities but the same functor are different predicates in the eyes of prolog. It is good practice to talk about predicates in terms of there arity. So the hide predicate above would really be
and playsAirGuitar would be
Prolog has 3 main constructs
Facts end in a full stop.
Rules consist of a head and at least one body. And the head is only true if the body is true.
Queries start with ?- but that is written in by the console.
- :- Means implication (if)
- , Means conjunction (and)
- ; Means disjunction (or)
Prolog has 4 kinds of terms:
- Complex Terms
And atom is either a string beginning with a lower case letter, a string encased in single quotes or a string of special characters.
A Variable is a string beginning with an Upper case letter or an underscore. Just an under score is a special anonymous variable.
Prolog rarely uses floating point numbers, but integers are always implemented for counting and similar tasks.
Complex terms are built of the three simple terms and are built in the format
Where the functor is an atom and the arguments can be any kind of term, even a complex term.
The number of arguments a complex term has is called its arity. You usually specify a complex term with its arity, for example if I had the term
I would call it
loves/2 as it had 2 arguments.