Download as pdf or txt
Download as pdf or txt
You are on page 1of 36

CSI2120 A

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

 A prolog program consists of a collection of facts


and rules.
• A fact is a predicate terminated by a period “.”
• Rules encode ways of deriving or computing a
new fact.

 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

Facts that are false in the real world can be used.


• faster(snails,cheetahs).

Database
• a collection of facts (part of a program)
Prolog and Horn Clauses

• Facts and rules are Horn Clauses.


• In general F :- F1, F2,…, Fn.
– meaning F if F1 and F2 and …and Fn
– F is an atomic formula (must be unique)
– Fi are terms / literals or their negation
• F is the head of the clause
• F1, F2,…, Fn together are the body of the clause
• To prove F in Prolog, it must be true (proven) that F1,
F2,…, and Fn are true .
• Horn's clauses are the only formulas allowed in Prolog
Horn Clauses

• A rule is a clause where the body is non-empty while a fact is


a clause with an empty body. Most rules contain variables.
• Prolog Definition with an anonymous variable, written as “_”
salary(X) :- employed(Y,X). % Ok but with
% a warning
– Or with an anonymous variable
salary(X) :- employed(_,X).
• Facts and rules are predicates.

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

• Horn clauses can express nearly all logic expressions, all


mathematical algorithms.
• It enables one to establish the truth of a hypothesis by
establishing the truth of terms but it does not allow one to
prove the falsehood of a hypothesis. False in logic
programming only means that the goal can not be proven
correct.
Prolog programs

• Prolog programs: succession of predicate declarations


• No order to follow
• From a program, you can ask questions
* Eg: brother (paul, X). To find paul's brothers.
* That is, determine the values of the variables (X)
such that the question is deductible from the facts
and predicates of the program.
• We are talking about problem solving or from
problem demonstration.

2021-02-04
Resolution

• Prolog execution is based on the Resolution proof method.


• Resolution is a technique of producing a new clause by
resolving two clauses that contain a complimentary literal and
Resolution produces proof by contradiction.
Unification

• 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

• Definition : process by which we try to make two formulas


identical by giving values to the variables they contain.

1. The predicate name much match.


* So foo (X, X) can match foo (2,3), but can never match
eat (2,3)
2. Once the predicates names the arity of the predicates much
match (number of terms).
3. If the predicate names and arities match then each of the k-
terms much match. So for foo(t1, t2, t3) to match foo(s1,
s2, s3) we must have that t1 matches s1, t2 matches s2,
and t3 matches t3.

2021-02-04
Unification

• Result: it is a unifying (or substitution or matching), a set of


variable assignments.
* Example: {X = paul, Qui = Y}
• The result is not necessarily unique, but represents the most
general unifier.
• Unification can succeed or fail.
* e (X, X) and e (2,3) cannot be unified.
Unification

flies (X): - bird (X).


bird (duck).

? - flies (duck)
? - bird (duck)

2021-02-04
Unification

• Unification predicate: "="


* a (B, C) = a (2,3). gives the result:
YES {B = 2, C = 3}
* a (X, Y, L) = a (Y, 2, carole). gives for result :
YES {X = 2, Y = 2, L = carole}
* a (X, X, Y) = a (Y, u, v). gives the result:
NO

2021-02-04
Demonstration steps

• If the unification fails: failure situation on the rule


considered to demonstrate the formula.
• If the unification succeeds: substitution of the variables
present in the tail of the clause by the corresponding
values of the variables of the unifier.

2021-02-04
Demonstration steps

• Demonstration of this set of formulas in the order of


their quotation to enrich the system with the
values obtained from the variables.
• At the end, the set of value-variable pairs of the
variables present in the initial question forms the
solution displayed by Prolog.

2021-02-04
% Peter's children?

Example
? - parent (peter, C).
C = marc;
C = louise;
No

% of facts in Prolog % parent predicate / 2: % Louise's parents?


man (john). parent (john, peter). ? - parent (P, louise).
man (peter). parent (peter, marc). P = peter;
man (marc). parent (peter, louise). P = mary;
wife (mary). parent (mary, marc). No
woman (louise). parent (mary, louise).
% Does John have children?
% questioning the facts ? - parent (john, _).
? - man (john). Yes
Yes
% Does Marc have children?
? - man (mary). ? - parent (marc, _).
No No

? - man (simon). % define a Prolog rule


No hasChildren (P): - parent (P, _).

? - man (M). ? - hasChildren (peter).


M = john; Yes
M = peter;
M = marc; ? - hasChildren (marc).
No No
Search Trees

• Search trees represent queries


– Root of the tree is the question
– Nodes (or vertices) are decisions and show which
goals still need to be satisfied
– Transitions (along edges) from one node to the next
are the result of an unification between a goal and a
fact or the head of a rule.
– The edges are a step in the proof (unifications).
Nodes in Search Tree

– Goals are ordered from left to right following the


order in the rules. Goals are stated in a node.
– Leaf nodes which contain one or several goals are
failure nodes. The first (left-most) goal caused the
failure.
– Empty leaf nodes are success nodes. The path from
the root to the leaf node contains the unifications
and steps necessary for the proof. These can be
found on the edges.
Solution Strategy of Prolog

• Prolog builds the search tree from the question as a root


node. The tree is built in a depth-first fashion.
• An empty (leaf) node is a proof or a solution
– Search can continue for other solutions by
backtracking and traversing unexplored branches
• An non-empty leaf node is a failure
– A solution may still be found by backtracking and
traversing unexplored branches
Backtracking

• If there are no more new nodes found, there are no


more solutions and Prolog answers no.
• Termination is not guaranteed. It is easy to write rules
that cause an infinite recursion.
• The order in which solutions are produced depends on
the order in predicates, in particular:
– the order of the literals in the body of clause
– the order of the predicates
f (a).
f (b).
g (a).
g (b).
h (b).
k (X): - f (X), g (X), h (X).

? - 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

parent(Z,cowbarn),typeof(Z,A) parent( A, cowbarn)

Z=barn A=barn

typeof(barn,A)

______X=barn, _____Y = A
_X=barn, _Y = A

parent(_Z,barn),typeof(_Z,A) parent( A, barn)

_Z=farmbuilding A = farmbuilding

typeof(farmbuilding,A)

__X=farmbuidling, __Y = A _____X=farmbuilding, ____Y = A

parent(__Z,farmbuilding),typeof(__Z,A) parent(A, farmbuilding)

__Z=building A = building

typeof(building,A)

____X=building, ___Y = A
___X=building, __Y = A

parent(___Z,building),typeof(___Z,A) parent(A, building)


Another Example: 3 Versions of
French Nobleman Version C
Version A father(charles,jean).
father(charles,jean). noble(X):- father(Y,X),
noble(henri). noble(Y).
noble(louis). noble(henri).
noble(charles). noble(louis).
noble(X):- father(Y,X), noble(charles).
noble(Y).
Version B
father(charles,jean).
noble(henri).
noble(louis).
noble(charles).
?- noble(jean). noble(X):- noble(Y),
father(Y,X).
Source: R. Laganière
A Last Example
likes(peter,jane).
likes(paul,jane).
conflict(X,Y) :- likes(X,Z),likes(Y,Z).

?- conflict(X,Y).

• How many solutions? …4

http://cs.union.edu/~striegnk/learn-prolog-now/html/node20.html#sec.l2.proofsearch
Summary

• Predicate calculus
– Predicates
– Horn clauses
– Resolution
• Search Trees
– Backtracking

You might also like