Académique Documents
Professionnel Documents
Culture Documents
SWI‐Prolog
CSE 3302
Programming Languages • http://www.swi‐prolog.org/
• Available for:
Logic Programming: Linux, Windows, MacOS
Prolog
Chengkai Li
Spring 2008
Prolog History of Prolog
• Prolog:
“Programming in Logic” (PROgrammation en LOgique)
• One (and maybe the only one) successful logic
programming languages
programming languages first Prolog interpreter by
Colmerauer and Roussel
• Useful in AI applications, expert systems, natural language
processing, database query languages
• Declarative instead of procedural: “What” instead of
“How”
1972 1977 1980 1980s/1990s 2005
Lecture 21 – Prolog, Spring 2008 CSE3302 Programming Languages, UT‐Arlington 3 4
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
©Chengkai Li, 2008
History of Prolog History of Prolog
1972 1977 1980 1980s/1990s 2005 1972 1977 1980 1980s/1990s 2005
© Patrick Blackburn, Johan Bos & Kristina Striegnitz 5 © Patrick Blackburn, Johan Bos & Kristina Striegnitz 6
1
4/17/2008
History of Prolog History of Prolog
1972 1977 1980 1980s/1990s 2005 1972 1977 1980 1980s/1990s 2005
© Patrick Blackburn, Johan Bos & Kristina Striegnitz 7 © Patrick Blackburn, Johan Bos & Kristina Striegnitz 8
Logic Programming Example
• Program • Axioms:
Axioms (facts): true statements 0 is a natural number. (Facts)
For all x, if x is a natural number, then so is the successor of x.
• Input to Program
query (goal): statement true (theorems) or false? • Query (goal).
Is 2 natural number? (can be proved by facts)
• Thus Is ‐1 a natural number? (cannot be proved)
Logic programming systems = deductive databases
datalog
Another example First‐Order Predicate Calculus
• Axioms: • Logic used in logic programming:
The factorial of 0 is 1. (Facts) First‐order predicate calculus
If m is the factorial of n ‐ 1, then n * m is the factorial of n. First‐order predicate logic
Predicate logic
• Q
Query: First‐order logic
The factorial of 2 is 3?
∀x (x ≠ x+1)
• Second‐order logic
∀S ∀ x (x ∈S ∨ x∉S)
2
4/17/2008
First‐Order Predicate Calculus: First‐Order Predicate Calculus:
Example statements
Symbols in statements:
• natural(0)
• Constants (a.k.a. atoms)
∀ X, natural(X) → natural(successor(x))
numbers (e.g., 0) or names (e.g., bill).
• Predicates
• ∀ X and Y, parent(X,Y) → ancestor(X,Y).
Boolean functions (true/false) . Can have arguments. (e.g. parent(X,Y)).
∀ A, B, and C, ancestor(A,B) and ancestor(B,C) →
ancestor(A,C).
( ) • Functions
∀ X and Y, mother(X,Y) → parent(X,Y). non‐Boolean functions (successor(X) ).
∀ X and Y, father(X,Y) → parent(X,Y). • Variables
father(bill,jill). e.g., X.
mother(jill,sam).
• Connectives (operations)
father(bob,sam).
and, or, not
implication (→):a→b (b or not a)
• factorial(0,1).
∀ N and M, factorial(N-1,M) → factorial(N,N*M). equivalence (↔) : a↔b (a→b and b→a)
First‐Order Predicate Calculus:
Problem Solving
statements (cont’d) • Program = Data + Algorithms
• Quantifiers • Program = Object.Message(Object)
universal quantifier "for all“ ∀ • Program = Functions Functions
existential quantifier "there exists" ∃ • Algorithm = Logic + Control
bound variable (a variable introduced by a quantifier)
free variable Programmers: Logic programming systems:
• Punctuation symbols facts/axioms/statements prove goals from axioms
parentheses (for changing associativity and precedence.)
comma • The holy grail: we specify the logic itself, the system proves.
period – Not totally realized by logic programming languages. Programmers
must be aware of how the system proves, in order to write efficient, or
• Arguments to predicates and functions can only be terms: even correct programs.
– Contain constants, variables, and functions. • Prove goals from facts:
– Cannot have predicates, qualifiers, or connectives. – Resolution and Unification
Lecture 21 – Prolog, Spring 2008 CSE3302 Programming Languages, UT‐Arlington 15 Lecture 21 – Prolog, Spring 2008 CSE3302 Programming Languages, UT‐Arlington 16
©Chengkai Li, 2008 ©Chengkai Li, 2008
Horn Clauses:
Horn Clause
• First‐order logic too complicated for an effective logic
Example
programming system. • First‐Order Logic:
• Horn Clause: a fragment of first‐order logic natural(0).
b ← a1 and a2 and a3 … and an. ∀ X, natural(X) → natural(successor(x)).
body no “or”
or and no quantifier
head
b ←. fact • Horn Clause:
←b. query natural(0).
natural(successor(x)) ← natural(X).
• Variables in head: universally quantified
Variables in body only: existentially quantified
• Need “or” in head? Multiple clauses
Lecture 21 – Prolog, Spring 2008 CSE3302 Programming Languages, UT‐Arlington 17 Lecture 21 – Prolog, Spring 2008 CSE3302 Programming Languages, UT‐Arlington 18
©Chengkai Li, 2008 ©Chengkai Li, 2008
3
4/17/2008
Horn Clauses: Horn Clauses:
Example Example
• First‐Order Logic: • Horn Clause:
factorial(0,1). ancestor(X,Y) ← parent(X,Y).
∀ N and ∀ M, factorial(N-1,M) → factorial(N,N*M). ancestor(A,C) ← ancestor(A,B) and ancestor(B,C).
parent(X,Y) ← mother(X,Y).
parent(X,Y) ← father(X,Y).
( ,j )
father(bill,jill).
mother(jill,sam).
• Horn Clause: father(bob,sam).
factorial(0,1).
factorial(N,N*M) ← factorial(N-1,M).
Horn Clauses:
Prolog syntax
Example
• First‐Order Logic: • :‐ for ←
∀ X, mammal(X) → legs(X,2) or legs(X,4). , for and
ancestor(X,Y) :- parent(X,Y).
ancestor(X Y) :-
ancestor(X,Y) : ancestor(X,Z),
ancestor(X Z) ancestor(Z
ancestor(Z,Y).
Y)
• Horn Clause: parent(X,Y) :- mother(X,Y).
legs(X,4) ← mammal(X) and not legs(X,2).
parent(X,Y) :- father(X,Y).
legs(X,2) ← mammal(X) and not legs(X,4).
father(bill,jill).
mother(jill,sam).
father(bob,sam).
Resolution
• Resolution: Using a clause, replace its head in
the second clause by its body, if they “match”.
if bi matches a;
b ← b1, …, a1, …, an, …, bm.
4
4/17/2008
Problem solving in logic
Resolution: Another view
programming systems
• Program:
• Resolution: Combine two clauses, and cancel
– Statements/Facts (clauses).
matching statements on both sides. • Goals:
– Headless clauses, with a list of subgoals.
• a ← a1, …, an.
• Problem solving by resolution:
b ← b1, …, bi, …, bm. – Matching subgoals with the heads in the facts, and replacing the
subgoals by the corresponding bodies.
a, b ← a1, …, an, b1, …, bi, …, bm. – Cancelling matching statements.
– Recursively do this, till we eliminate all goals. (Thus original goals
proved.)
Example Example
• Program: • Program:
mammal(human). legs(X,2) ← mammal(X), arms(X,2).
legs(X,4) ← mammal(X), arms(X,0).
• Goal: mammal(horse).
← mammal(human).
mammal(human) arms(horse 0)
arms(horse,0).
• Goal:
← legs(horse,4).
• Proving:
mammal(human) ← mammal(human). • Proving: ?
←.
Unification Unification: Example
• Unification: Pattern matching to make statements identical • Euclid’s algorithm for greatest common divisor
(when there are variables).
• Set variables equal to patterns: instantiated. • Program:
gcd(U,0,U).
• In previous example: gcd(U,V,W) ← not zero(V), gcd(V, U mod V, W).
legs(X,4) and legs(horse,4) are unified.
(X is instantiated with horse.)
• Goals:
← gcd(15,10,X).
5
4/17/2008
Things unspecified Example
• The order to resolve subgoals. • Program:
• The order to use clauses to resolve subgoals. ancestor(X,Y) :- ancestor(X,Z), parent(Z,Y).
ancestor(X,Y) :- parent(X,Y).
parent(X,Y) :- mother(X,Y).
• Possible to implement systems that don’t depend on the parent(X,Y) :- father(X,Y).
order, but too inefficient.
d b i ffi i father(bill,jill).
mother(jill,sam).
father(bob,sam).
• Thus programmers must know the orders used by the
• Goals:
language implementations. (Search Strategies)
← ancestor(bill,sam).
← ancestor(X,bob).