Prolog andLogic Programming
Relations
Another relation
Functions vs. Relations
Prolog deals with relations
Language metaphors
Knuth onprogram correctness
History of Prolog
Not for general purpose programming
Why restrict applicability of a language?
Example applications
Eight Queens:A typical search problem
A Solution to Eight Queens
Eight Queens in Prolog
Query for solution
A Prolog Program
Example facts
Queries
Syntax for facts
Variables
Variables are scopedwithin a query
More facts
A simple rule
Two interpretations of rule
Predicate logic and English
Implication
Our friend the list
Declaration of append rule
“Return value”is an argument of the rule
Terminology
Lists
Lists need not behomogeneous
Unification of terms
Unification of terms S and T
More unification examples
Unification is implicitin rule application
Unification in append rule
Trace of app([1,2,3],A,[1,2,3,4])
Trace of app([1,2,3],[4],Z)
Arithmetic in Prolog
Arithmetic onlyworks forward in Prolog
ConstraintLogic Programming
Importance ofConstraint Logic Programming
CLP(R) can do arithmetic in all directions!
member rule
Definition of member
The declarative interpretation falls short…
More uses of member
How Prolog tries to prove
Evidence of top to bottomrule order
Order of rules matters!
Trace of mem1(2,[1,2,3])
Trace of mem2(2,[1,2,3])
mem1 was faster
PPT Slide
PPT Slide
PPT Slide
PPT Slide
Prolog is not an oracle!
PPT Slide
PPT Slide
PPT Slide
PPT Slide
PPT Slide
PPT Slide
PPT Slide
PPT Slide
PPT Slide
PPT Slide
PPT Slide
PPT Slide
PPT Slide
PPT Slide
PPT Slide
PPT Slide
Search algorithm
Multiple sub-goals
Another search tree…
Tracing through the tree
PPT Slide
PPT Slide
PPT Slide
PPT Slide
PPT Slide
PPT Slide
PPT Slide
PPT Slide
PPT Slide
PPT Slide
PPT Slide
PPT Slide
PPT Slide
PPT Slide
Backtracking is notalways what we want
delete_all example
A query for delete_all
delete_all can succeedin any of three ways...
delete_all has multiple matching rules
Third rule contained implicit assumption
Use a “cut” — !
The query again
Cut divides probleminto backtracking regions
Controversy over cut
cut and not
!, fail combination
Tree view of a cut
Tree view of a cut
Difference lists—an incomplete data structure
Hole variables for accessing end of the list
Appending elementsto a difference list
Unifications in dl_app
Another way to query