Important concepts from Prolog

Download Report

Transcript Important concepts from Prolog

Prolog Concepts
A declarative language



Most programming languages are imperative—you tell the
computer what to do to solve a problem
Prolog is declarative—you give it the data it needs, and it solves
the problem for you
Consider:

append([], List, List).
append([Head | Tail], List, [Head | More]) :append(Tail, List, More).

This defines what it means append lists




You can use it to append two lists
You can use it to find what to append to a list to get another list
You can use it to find what list to append to to get another list
You can test whether the relation holds between three lists
2
A constraint satisfaction language

Prolog is also one of a number of constraint satisfaction
languages


You tell it the constraints on a problem (e.g. the solution must
be someone who is both female and rich), and it finds a
solution that satisfies those constraints
A “logic puzzle” is nothing more than a set of
constraints (e.g. every man has a different tie)
3
A homoiconic language



Prolog is homoiconic—that is, there is no distinction
between “statements” in the language and the “data”
that the languages processes
When you provide “input” to a Prolog program (e.g.
for an adventure game), you use the same syntax as
when you write the program
We haven’t emphasized homoiconicity in Prolog,
because it is more important in some of the other
languages we will be studying
4
Prolog does backtracking

You don’t have to implement backtracking in Prolog;
the language does it for you

The only other “language” I know of that does this is Regular
Expressions, which are a “sublanguage” of most modern
programming languages
loves(chuck, X)
call
fail
female(X)
rich(X)
exit
redo
5
Prolog uses unification

The basic rules of unification are:




Any value can be unified with itself
A variable can be unified with another variable
A variable can be unified with any value
Two different structures can be unified if their constituents
can be unified




Lists are a particularly important kind of structure
A variable can be unified with a structure containing that same
variable
Unification is reflexive, symmetric, and transitive
Parameter transmission is by unification, not assignment
6
Prolog is a theorem prover


Prolog is an implementation of resolution theorem
proving in a programming language
Because it is based on logic, there is very little that is
ad hoc or arbitrary about Prolog
7
Resolution

A clause is a disjunction (“or”) of zero or more literals, some or all of which
may be negated


sinks(X)  dissolves(X, water)  ¬denser(X, water)
Any expression in the predicate calculus can be put into clause form



X  Y can be rewritten as X  Y
Conjunctions (“ands”) can be rewritten as separate clauses
The existential quantifier  can be replaced by a Skolem function




If the existential quantifier is under control of a universal quantifier, replace it with a
function of the universally quantified variable
Otherwise, just replace with a constant (whose value need not be known)
Universal quantifiers, , can be dropped
Here is the resolution principle:


X  someLiterals
X  someOtherLiterals
---------------------------------------------conclude: someLiterals  someOtherLiterals
Clauses are closed under resolution
From
and
8
The End
9