Strings
Strings are usually written as a sequence of characters surrounded by quotes. They are often internally represented as lists of ASCII codes.
Facts
Programming in Prolog is very different from programming in a procedural language. In Prolog you supply a database of facts and rules; you can then perform queries on the database. The basic unit of Prolog is the predicate, which is defined to be true. A predicate consists of a head and a number of arguments. For example:
cat(tom).
Here 'cat' is the head, and 'tom' is the argument. Here are some sample queries you could ask a Prolog interpreter basing on this fact:
?- cat(tom).
yes.
?- cat(X).
X = tom;
no.
Predicates are usually defined to express some fact the program knows about the world. In most of the cases, the usage of predicates requires a certain convention. Thus, which version of the two below would signify that Pat is the father of Sally?
father(sally,pat).
father(pat,sally).
In both cases 'father' is the head and 'sally' and 'pat' are arguments. However in the first case, Sally comes first in the argument list, and in the second, Pat comes first (the order in the argument list matters). The first case is an example of a definition in Verb Subject Object order, and the second of Verb Object Subject order. Since Prolog does not understand English, both versions are fine so far as it is concerned; however it is good programming style to stick to either convention during the writing of a single program, so that to avoid writing something like
father(pat,sally).
father(jessica,james).
Some predicates are built in into the language, and allow a Prolog program to perform routine activities (such as input/output, using graphics and otherwise communicating with the operating system). For example, the predicate write can be used for output to the screen. Thus,
write('Hello')
will display the word 'Hello' on the screen.
Rules
The second type of statements in Prolog is rules. An example of a rule is
light(on) :- switch(on).
The ":-" means "if"; this rule means light(on) is true if switch(on) is true. Rules can also make use of variables; variables begin with capital letters while constants begin with lower case letters. For example,
father(X,Y) :- parent(X,Y),male(Y).
This means "if someone is a parent of someone and he's male, he is a father". The ancedent and consequent are in reverse order to that normally found in logic. It is possible to place multiple predicates in a consequent, joined with conjunction, for example:
a,b,c :- d.
which is simply equivalent to three separate rules:
a :- d.
b :- d.
c :- d.
What is not allowed are rules like:
a;b :- c.
that is "if c then a or b". This is because of the restriction to Horn clauses.
Implementations
Ciao Prolog (http://www.clip.dia.fi.upm.es/Software/Ciao)