0% found this document useful (0 votes)
16 views22 pages

cs350 - 2021 - Chapter16 (1) - 1

Uploaded by

pancholi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views22 pages

cs350 - 2021 - Chapter16 (1) - 1

Uploaded by

pancholi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Chapter 16

Logic Programming Languages

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

• Declarative versus procedural approaches:


– Only specification of results are stated
– Detailed procedures for producing them are not
stated.
• Separation of knowledge base and
inference engine
– Program is a knowledge base (i.e., a set of facts
and rules)
– Interpreter is an inference engine.

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:

• Horn clause (i.e., logic expression of a


special form): contains only OR and NOT on
atomic expression.
• LHS :- RHS
– LHS is either empty or a single predicate
– RHS contains only logic conjunction (i.e., AND).
• Interpretation:
if RHS then LHS

1-5
Prolog Program

p -> q = NOT p OR q

p1 AND p2 AND … AND pn -> q


=
NOT(p1 AND p2 AND … AND pn ) OR q
=
NOT p1 OR NOT p2 OR … OR pn OR q

Copyright © 2012 Addison-Wesley. All rights reserved. 1-6


Prolog Program

From logic expressions to PROLOG program

Logic expression PROLOG program


<- :-
AND ,

p1 AND … AND pn -> q q :- p1, …, pn.

Copyright © 2012 Addison-Wesley. All rights reserved. 1-7


Prolog Program:

• Facts
:- RHS
We simply write as
RHS
• Rules
LHS :- RHS

1-8
Facts

• Headless Horn clauses


female(shelley).
male(bill).
father(bill, jake).

1-9
Rules

ancestor(mary,shelley):- mother(mary,shelley).

parent(X,Y):- mother(X,Y).
parent(X,Y):- father(X,Y).

grandparent(X,Z):- parent(X,Y), parent(Y,Z).

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).

grandparent(X,Z):- parent(X,Y), parent(Y,Z).

grandparent(X, Z) ======== grandparent(pam, ann)


pam/X, ann/Z

ent(X,Y) parent(Y,Z) parent(pam, bob) parent(bob, a

=
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

• Queries are called goals


• If a goal is a compound proposition, each of the
facts is a subgoal
• To prove a goal is true, must find a chain of
inference rules and/or facts. For goal Q:
P2 :- P1
P3 :- P2

Q :- Pn
• Process of proving a subgoal called matching,
satisfying, or resolution

1-14
Prolog Interpreter

• For each query (also call goal), the program build a


“proof tree” in a top-down manner.
• The process is very similar to down-down parsing.
– For rule
g :- p1, p2, …, pm,
we can create subtree with g as the root and p1, p2, …, pm
as children.
– The leaves of the tree are facts.

1-15
Prolog Interpreter

• Build the tree by trying rules one by one. If one


rule fails, try another one, until you find the
correct rule.

1-16
Example

1-17
Example

1-18
Example

1-19
Prolog Interpreter

• Binary resolution rule


p OR s
NOT p OR r
-----------------
s OR r

• Prolog interpreter as a sequence of binary


resolution to find a contradiction
– Change Prolog programs into Horn clauses
– Negate the query (i.e., goal)
– Using the binary resolution rule to find a contradiction

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).

NOT parent(X, Y) OR predecessor(X,Y)


NOT parent(X, Z) OR NOT predecessor(Z, Y) OR predecessor(X,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
------------------------------

You might also like