Professional Documents
Culture Documents
6 Prolog2 Searchtree
6 Prolog2 Searchtree
Programming Paradigms
1
• Course overview
• Introduction to programming
paradigms
• Review: The object-oriented
Topics paradigm in Java
• Imperative and concurrent
programming paradigm: Go.
• Logic paradigm: Prolog.
• Functional paradigm: Scheme.
• Quiz 2 is available ends on
Announcement Thursday Feb 4th
The slides posted through the term are based of the
slides offered by:
Prof. Robert Laganiere
Prof. Jochen Lang
Acknowledgment Demo code:
https://www.site.uottawa.ca/~jl
ang/CSI2120/demoCode.html
Logic Programming in Prolog
• Predicate calculus
– Predicates
– Horn clauses
– Unification
– Resolution
• Search Trees
– Backtracking
Prolog
• Prolog Programming
• Learn Prolog Now!
Predicates in Prolog
loves(vincent,mia).
loves(marcellus,mia).
loves(pumpkin,honey_bunny).
loves(honey_bunny,pumpkin).
jealous(X,Y) :- loves(X,Z),loves(Y,Z).
Predicates in Prolog
loves(vincent,mia).
loves(marcellus,mia).
loves(pumpkin,honey_bunny).
loves(honey_bunny,pumpkin).
jealous(X,Y) :- loves(X,Z),loves(Y,Z).
Variables allow us to
Compute more than yes/no answers
Compress the program.
More on Facts
Arity of Predicates
Predicates can have an arbitrary number of arguments
domestic/1 isYellow/1 % 1 argument
faster/2 like/2 eat/2 % 2 arguments
takes/3 % 3 arguments
Database
• a collection of facts (part of a program)
Prolog and Horn Clauses
https://www.swi-prolog.org/pldoc/man?section=singleton
Predicates in Prolog
• 𝒃 ← 𝒂 𝟏 ∧ 𝒂𝟐 ∧ ⋯ ∧ 𝒂𝟑
– All terms 𝒂𝟏 , 𝒂𝟐 , … , 𝒂𝟑 in the body of the predicate
have to be true for the head to be true. Or,
𝒂𝟏 , 𝒂𝟐 , … , 𝒂𝟑 being true, implies b is true.
• 𝒃 ←
– This is a fact because truth is always implied.
• ←𝒂
– Without a head, it is goal for which correctness still
needs to be proven. This may be considered a
question in logic programming in Prolog. Proofing
correctness requires deductive reasoning.
Horn Clauses
2021-02-04
Resolution
• Example:
* brother (X, Y): - man (X), child (X, Z), child (Y, Z), X \ = Y.
where \ = represents the difference predicate.
* brother (paul, Who) : attempt to unify with the head of the
clause brother (X, Y)
• Definition : process by which we try to make two formulas
identical by giving values to the variables they contain.
• Aslo known as matching
2021-02-04
Unification
2021-02-04
Unification
? - flies (duck)
? - bird (duck)
2021-02-04
Unification
2021-02-04
Demonstration steps
2021-02-04
Demonstration steps
2021-02-04
% Peter's children?
Example
? - parent (peter, C).
C = marc;
C = louise;
No
? - k (Y).
CSI2520
A first Example
f(a).
f(b).
g(a).
g(b).
h(b).
k(X) :- f(X),g(X),h(X).
http://cs.union.edu/~striegnk/learn-prolog-now/html/node20.html#sec.l2.proofsearch
A Simple Example
?- trace.
Yes
[trace] 2 ?- k(X).
Call: (6) k(_G348) ?
Call: (7) f(_G348) ?
Exit: (7) f(a) ?
Call: (7) g(a) ?
Exit: (7) g(a) ?
Call: (7) h(a) ?
Fail: (7) h(a) ?
Fail: (7) g(a) ?
Redo: (7) f(_G348) ?
Exit: (7) f(b) ?
Call: (7) g(b) ?
Exit: (7) g(b) ?
Call: (7) h(b) ?
Exit: (7) h(b) ?
Exit: (6) k(b) ?
X = b
Yes
notrace.
http://cs.union.edu/~striegnk/learn-prolog-now/html/node22.html#sec.l2.praxis
voter(Y)
Another Example
Y=X
name(joe). name(X), citizen(X),
name(jane). adult(X)
citizen(jane). X=joe
citizen(joe). X=jane
citizen(joe), adult(joe)
adult(jane).
voter(X):- citizen(jane), adult(jane)
name(X),
citizen(X),
adult(joe) adult(jane)
adult(X).
?- voter(Y).
Another Example
Building categories:
parent(building,farmbuilding).
parent(farmbuilding,barn).
parent(farmbuilding,silo).
parent(farmbuilding,house).
parent(barn,horsebarn).
parent(barn,cowbarn).
typeof(X,Y):- parent(Z,X),typeof(Z,Y).
typeof(X,Y):- parent(Y,X).
?- typeof(cowbarn,A).
typeof(cowbarn, A)
X=cowbarn, Y=A _______X=cowbarn, ______Y = A
Z=barn A=barn
typeof(barn,A)
______X=barn, _____Y = A
_X=barn, _Y = A
_Z=farmbuilding A = farmbuilding
typeof(farmbuilding,A)
__Z=building A = building
typeof(building,A)
____X=building, ___Y = A
___X=building, __Y = A
?- conflict(X,Y).
http://cs.union.edu/~striegnk/learn-prolog-now/html/node20.html#sec.l2.proofsearch
Summary
• Predicate calculus
– Predicates
– Horn clauses
– Resolution
• Search Trees
– Backtracking