Lecture1
Lecture1
A Gentle Introduction
An Example
“Prolog” is all about programming in logic.
Socrates is a man.
All men are mortal.
Therefore, Socrates is mortal.
Facts, rules, and queries
Fact: Socrates is a man.
man(socrates).
Rule: All men are mortal.
mortal(X) :- man(X).
Query: Is Socrates mortal?
mortal(socrates).
Running Prolog I
Create your "database" (program) in
any editor.
Save it as text only, with a .P extension
Here's the complete "program":
man(socrates).
mortal(X) :- man(X).
Running Prolog II
Prolog is completely interactive.
Begin by invoking the Prolog
interpreter.
Then load your program.
Then, ask your question at the prompt:
? mortal(socrates).
Prolog responds:
Yes
Syntax 1: Structures or Terms
Example structures:
– sunshine
– man(socrates)
– path(garden, south, sundial)
<structure> ::= <name>| <name> (
<arguments> )
<arguments> ::= <argument> |
<argument> , <arguments>
Syntax II: Base Clauses
Base clauses are like simple facts.
Example base clauses:
loves(john, mary).
loves(mary, bill).
loves(john, mary).
loves(mary, bill).
loves(chuck, X) :- female(X), rich(X).
X, Socrates, _result
?- female(X).
X = jane ;
X = mary
Yes
Readings
loves(chuck, X) :- female(X), rich(X).
Declarative reading: Chuck loves X if X
is female and rich.
Approximate procedural reading: To
find an X that Chuck loves, first find a
female X, then check that X is rich.
Declarative readings are almost always
preferred.
Non-monotonicity
Prolog’s facts and rules can be changed
at anytime.
assert(man(plato)).
assert((loves(chuck,X) :- female(X), rich(X))).
retract(man(plato)).
retract((loves(chuck,X) :- female(X), rich(X))).
Common problems
Capitalization is extremely important!
No space between a functor and its
argument list:
man(socrates), not man (socrates).
Don’t forget the period! (But you can
put it on the next line.)
A Simple Prolog Model
Imagine prolog as a system which has a database composed of
two components:
Note:
read :- as IF
read , as AND
a . marks the end of input
Prolog Database
parent(adam,able)
parent(adam,cain) Facts comprising the
“extensional database”
male(adam)
…
father(X,Y) :- parent(X,Y),
Rules comprising the
male(X). “intensional database”
sibling(X,Y) :- ...
Extensional vs. Intensional
parent(adam,able)
The terms extensional and intensional parent(adam,cain)
are borrowed from the language male(adam)
philosophers use for epistemology. …
Extension refers to whatever extends,
i.e., “is quantifiable in space as well as in father(X,Y) :- parent(X,Y),
time”. male(X).
Intension is an antonym of extension, sibling(X,Y) :- ...
referring to “that class of existence which
may be quantifiable in time but not in
space.” Facts comprising the
“extensional database”
NOT intentional with a “t”, which has to
do with “will, volition, desire, plan, …” Rules comprising the
For KBs and DBs we use “intensional database”
• extensional to refer to that which is
explicitly represented (e.g., a fact), and
• intensional to refer to that which is
represented abstractly, e.g., by a rule of
inference.
A Simple Prolog Session
| ?- assert(parent(adam,able)).
Yes
| ?- assert(parent(eve,able)).
Yes
| ?- assert(male(adam)).
Yes
| ?- parent(adam,able).
Yes
How to Satisfy a Goal
Here is an informal description of how Prolog satisfies a goal (like
father(adam,X)). Suppose the goal is G:
Conjunction: if G = P,Q then first satisfy P, carry any variable
bindings forward to Q, and then satisfy Q.
There’s no real distinction between rules and facts, which are just rules
whose bodies are the trivial condition true. These are equivalent:
parent(adam,cain)
parent(adam,cain) :- true.