Prolog and Logic Programming

Relations

Another relation

Functions vs. Relations

Prolog deals with relations

Language metaphors

Knuth on program 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 scoped within 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 be homogeneous

Unification of terms

Unification of terms S and T

More unification examples

Unification is implicit in 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 only works forward in Prolog

Constraint Logic Programming

Importance of Constraint 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 bottom rule 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 not always what we want

delete_all example

A query for delete_all

delete_all can succeed in any of three ways...

delete_all has multiple matching rules

Third rule contained implicit assumption

Use a “cut” — !

The query again

Cut divides problem into 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 elements to a difference list

Unifications in dl_app

Another way to query