cs350 - 2021 - Chapter16 (1) - 1
cs350 - 2021 - Chapter16 (1) - 1
1-1
Program and Interpreter
• Program:
– Programs in logic languages are expressed in a
form of symbolic logic
• Logic expressions are used to define Facts and
Rules
• Interpreter:
– Use a logical inferencing process to produce
results
• Building a proof tree/inference tree (similar to the
process of building a parser tree in top-down
manner)
1-2
Two Important Issues
1-3
Declarative Semantics
• Declarative semantics
– There is a simple way to determine the meaning
of each statement
• Programming is nonprocedural
– Programs do not state now a result is to be
computed, but rather the form of the result
1-4
Prolog Program:
1-5
Prolog Program
p -> q = NOT p OR q
• Facts
:- RHS
We simply write as
RHS
• Rules
LHS :- RHS
1-8
Facts
1-9
Rules
ancestor(mary,shelley):- mother(mary,shelley).
parent(X,Y):- mother(X,Y).
parent(X,Y):- father(X,Y).
1-10
Prolog Program
parent(pam, bob).
parent(tom, bob).
parent(tom, liz).
parent(bob, ann).
parent(bob, pat).
parent(pat, jim).
AND/OR Graph
grandparent(X,Z):- parent(X,Y), parent(Y,Z).
grandparent(X, Z)
parent(X,Y) parent(Y,Z)
1-11
Prolog Program
parent(pam, bob).
parent(tom, bob). = match with instance
parent(tom, liz). /substitution
parent(bob, ann).
parent(bob, pat).
parent(pat, jim).
=
bob/Y 1-12
Prolog Program
parent(pam, bob).
parent(tom, bob).
parent(tom, liz).
parent(bob, ann).
parent(bob, pat).
parent(pat, jim).
predecessor(X,Y):- parent(X,Y).
predecessor(X,Y):- parent(X,Z), predecessor(Z,Y).
1-13
Prolog Interpreter
1-14
Prolog Interpreter
1-15
Prolog Interpreter
1-16
Example
1-17
Example
1-18
Example
1-19
Prolog Interpreter
1-20
Prolog Program
parent(pam, bob).
parent(tom, bob).
parent(tom, liz).
parent(bob, ann).
parent(bob, pat).
parent(pat, jim).
predecessor(X,Y):- parent(X,Y).
predecessor(X,Y):- parent(X,Z), predecessor(Z,Y).
1-21
Prolog Program
predecessor(X,Y):- parent(X,Y).
predecessor(X,Y):- parent(X,Z), predecessor(Z,Y).
NOT parent(X, Y) OR predecessor(X,Y)
NOT parent(X, Z) OR NOT predecessor(Z, Y) OR predecessor(X,Y)
NOT predecessor(pam, jim)
NOT parent(X, Z) OR NOT predecessor(Z, Y) OR predecessor(X,Y)
-----------------------------
NOT parent(pam, Z) OR NOT predecessor(Z, jim) {pam/X, jim/Y)
parent(pam, bob) {bob/Z}
-----------------------------
NOT predecessor(bob, jim)
NOT parent(X, Z) OR NOT predecessor(Z, Y) OR predecessor(X,Y)
-----------------------------
NOT parent(bob, Z) OR NOT predecessor(Z, jim) {bob/X, jim/Y)
parent(bob, pat) {pat/Z}
-----------------------------
NOT predecessor(pat, jim)
predecessor(pat, jim) 1-22
------------------------------