Lecturer Xinming Simon Ou CIS 505 Programming Languages Fall 2010 Kansas State University 1 What we have covered so far Imperative paradigm Computes effects Functional paradigm Computes values ID: 164204
Download Presentation The PPT/PDF document "Chapter 8: The Logical Paradigm" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Chapter 8: The Logical Paradigm
Lecturer: Xinming (Simon) OuCIS 505: Programming LanguagesFall 2010Kansas State University
1Slide2
What we have covered so far:
Imperative paradigmComputes effectsFunctional paradigmComputes valuesUsed ML as an example
Logical paradigm
Computes relations
Use Prolog as an example
2Slide3
Example relations
Parent relation:parent(A, B) means A is B’s parente.g. parent(bill, mary
).
parent(mary, john).Ancestor relation
Can be defined
inductively:
A is B’s ancestor if A is B’s parent. A is B’s ancestor if A is C’s ancestor, and C is B’s ancestorThe resulting relation is the smallest one that satisfies the above two rules.
3Slide4
Some definitions for Prolog
Atoms:Any sequence of alpha-numeric characters that starts with a lower-case letter, or a single-quoted string, or a number
e.g.
mary
, john01, ‘Mary Doe’, 100Variables:Any sequence of alpha-numeric characters that starts with an upper-case letter, or an underscore “_”
e.g. Mary, _
mary
, _Literalpredicate(t1, …, tk), where ti
is either an atom, a variable, or a data-structure (function applied to parameters).
e.g. parent(mary, john). parent(mother(john), john). ancestor(mother(father(john)), john).
4Slide5
Horn Clauses
A Horn clause is a logical clause with a single positive literal: L0∨ L1
∨
… LnThis is equivalent to L
1
∧
…∧ Ln => L0
In Prolog, we use “,” to mean logical and, and write implication “backward”. Each clause is concluded with a “.”
L0 :- L1, …, Ln.Example:
ancestor(A
, B) :-
parent(A, B). ancestor(A, B) :- parent(A, C), ancestor(C, B).We call the left-hand side of the clause its head, and the right-hand side of the clause its body.A clause may have an empty body. e.g. parent(mother(X), X).
5Slide6
Variables in Clauses
All variables are implicitly universally bound at the beginning of the clausee.g. ancestor(A, B) :- parent(A, B).
Logically it is equivalent to:
Forall A, B. parent(A,B
) =>
ancestor(A
, B) Thus A and B can be instantiated with any term.
An underscore “_” is a wild card and can match anything.
e.g.
isParent(A) :- ancestor(A, _). 6Slide7
Query in Prolog
A query is in the form of a literal. The answer to the query is all the instantiations of the variables that make the literal true.e.g. ? - ancestor(X,Y). X = bill
Y =
mary
; X = mary
Y = john;
X = bill
Y = john; noLogically it is equivalent to “exists X, Y. ancestor(X,Y)?”
7Slide8
Execution Semantics of Prolog
When a query is issued, it is “compared” against the head of all the clauses one by one.If a “match” is found, the body of the clause becomes the new goalsThis process will iterate and may either succeed or fail.In either case the execution will backtrack to the first “choice point”, and try another match.This is called “SLD resolution”
8Slide9
Z2=john
X=mary
Y=john
Y=john
X=bill
Y=mary
Example SLD resolution
ancestor(X,Y
) :-
parent(X,Y
).
ancestor(X,Y
) :-
parent(X,Z
),
ancestor(Z,Y
)
.
parent(bill,mary
).
parent(mary,john
).
?-
parent(X,Y
).
?-
Success
?-
Success
?-
parent(X,Z
),
ancestor(Z,Y
).
?-
ancestor(X
, Y).
X=bill
Z=mary
?-
ancestor
(mary,Y
).
?-
parent
(mary,Y).
?-
Success
?- parent
(mary,Z2), ancestor(Z2,Y).
…Failure
…Failure
?-
ancestor(john,Y).
X=maryZ=john
?-
ancestor(john,Y).
9Slide10
Logic deduction as a program
The advantage of Prolog is that it has both a logic meaning, and an execution semanticsIdeally you do not need to think about the SLD resolution process when writing Prolog codeA Prolog program is simply a collection of logical statements. A query is simply asking whether a fact can be derived as a logical consequence of the statements.
However…
When the result does not match your expectation, knowing the SLD resolution process will help in debugging.
Moreover, Prolog is not always declarative, which we will see in the next lecture.
10Slide11
Practice XSB
We will be using the XSB Prolog systemIs installed on all the departmental Linux machinesCan be downloaded from: http://xsb.sourceforge.net/
Installation is relatively hassle-free. However, if you need to compile XSB under Mac OS X Snow Leopard, please let me know and there will be special instructions.
11Slide12
The first simple Prolog program
Put the following Prolog statements in a text file named “ancestor.P”parent(bill,
mary
).
parent(mary, john).ancestor(A, B) :- parent(A
, B).
ancestor(A
, B) :- parent(A, C), ancestor(C, B).Load the file in XSB:bash-3.2$
xsb
[
xsb_configuration loaded][sysinitrc loaded]XSB Version 3.2 (Kopi Lewak) of March 15, 2009
[i386-apple-darwin10.4.0; mode: optimal; engine:
slg-wam
; scheduling: local; word size: 64]| ?- [ancestor].12Slide13
Experiment with it
Issue various queries:e.g. ?- ancestor(X,Y). ?- ancestor(bill, X).
?-
ancestor(john
, X). …Change the order of the clauses and see what will happen:
parent(bill
,
mary).parent(mary, john).ancestor(A, B) :- parent(A, C), ancestor(C
, B).
ancestor(A
, B) :- parent(A, B).13