begin with lower case letter
followed by parenthesized parameter list
prof(), inClass(),
teaches(), isStudent()
Constants
begin with lower case letter
pruim,
math221,
math156,
joeSchmo,
janeDoe
Variables
begin with capital letter
P,
Stud, S
Logical operators
<=,
and,
not
Examples are obvious, but see below for restrictions on
<=. Notice, there is no "or".
Implied quantifiers
Invisible
See below for explanation
Note: all names (of variables, constants, and predicates) are
case sensitive, but are internally truncated at 10 characters.
Prolog lines
There are three kinds of lines in a prolog program. (Note that
each is ended by a period.)
Facts: A fact consists of a predicate followed by
its paramter list.
The paramters may be either constants or variables. Variables
are quantified by an implied universal quantifier (forall).
They are called facts becuase they
state a fact about some object or collection of objects.
In the example above, each of the first four lines
is a fact stating a certain relationship between two constant objects.
The line equal(X,X) is universally quantified and
means that for every X, X is equal to X.
Query:
A Toy Prolog program can only have one of these, and it must be
the last line. (Some other flavors of prolog allow for multiple
queries in one program.)
Queries are indicated by an initial ?
followed by a predicate (or a conjuction of predicates and negated
predicates). You can think of any variables appearing
in the query as being quantified by an implied existential quantifier, or
a "search quantifier". Toy Prolog will search for all the ways it can find
to satisfy the predicate by trying different settings of the variables.
(Actually, prolog uses some logical rules, most importantly resolution,
so that it can deal with the rules as well as the facts.)
Using just facts and a query, we can can already do some interesting
things.
Example 2
Rules.
It is the rules that make Prolog interesting.
Rules have the form A <= B, where A is
a predicate and B is a conjuction of predicates and negated predicates.
The paramters may be either constants or variables. Variables are quantified
by an implied universal quantifier (forall).
In the Example 1, the line
teaches(P,Stud) <= prof(P,X) and inClass(Stud,X).
is a rule. It is equivalent to
forall Stud forall P forall X
[prof(P,X) and inClass(Stud,X) -> teach(P,Stud) ].
That is, for any course, student and professor, if the professor teaches
the course and the student is in the course,
then the professor teaches the student.
Now let's look at the query in Example 1.
That query is equivalent to asking
"Do there exist any students that pruim teaches?"
The result of running the program is
to display all the students taught by pruim.
Prolog uses the facts and rules to see when in can conclude of
a student that pruim teaches the student. Notice again the
order in which things appear. In particular, if there is
more than one way to conclude that pruim teaches some student,
that student appears twice in the list.
Note: It is also possible to query predicates with
more than one variable paramter or none (all constants).
For example,