Title: COMP313A Programming Languages
1COMP313A Programming Languages
2Lecture Outline
- Conceptual foundations of Logic Programming
- The Basics of Logic Programming
- Predicate Calculus
- A little bit of logic programming
- Prolog
3Conceptual Foundations
- What versus how
- specification versus implementation
- Declarative Programming
- Programmer declares the logical properties that
describe the property to be solved - From this a solution is inferred
- Inference engine
4An example
Searching for an element in a list Predicate
is_in(x,L) true whenever element x is in the list
L.
For all elements x and lists L is_in(x,L) IFF L
x or L L1 . L2
and (is_in (x,L1) or is_in(x,
L2))
5Example continuedImplementation
- Need to know how to split a list into right and
left sublists - How to order the elements stored in the list
6A solution in C
int binary_search(const int val, const int size,
const int array) int high, low, mid if size
lt 0 return (-1) high size low
0 for() mid (high low) / 2 if
(mid low) return (val ! arraylow)
?-1mid if (val lt arraymid) high
mid else if (val gt arraymid) low
mid else return mid
7A Declarative Solution
- Given an element x and a list L, to prove that x
is in L, - proceed as follows
- Prove that L is x
- Otherwise split L into L1 . L2 and prove one of
the following - (2.1) x is in L1, or
- (2.2) x is in L2
8A sorting example
A predicate sort(X,Y) Sort(X,Y) is true if the
nonempty list Y is the sorted version of X Use
two auxiliary predicates permutation(X,Y) and
is_sorted(Y)
For all integer lists X,Y sort(X,Y) iff
permutation(X,Y) and sorted(Y)
9Logic and Logic Programming
- First-order predicate calculus
- Logic statements
- Examples
- John is a man. man(John).
- John is a human. human(John).
- Tristan is the son of Margaret.
son(Margaret,Tristan). - A horse is a mammal. loathes(Margaret,
Heavy_Metal). - 0 is a natural number . natural(0).
- Mammals have four legs and no arms or two legs
and two arms. - For all X, mammal (x) -gt legs(x,4) and arms(x,0)
or legs(x,2) and arms(x,2). - Humans have two legs and two arms.
- For all X, human(x) -gt legs(x,2) and arms(x,2).
- If x is a natural number then so is the successor
of x. - For all x, natural(x) -gt natural(successor(x)).
10First-Order Predicate Calculus
- Constants
- Predicates
- Functions
- Variables that stand for as yet unamed quantities
- Atomic sentences
- Connectives construct more complex sentences
- Quantifiers
- Punctuation
- Arguments to predicates can only be terms
variables, constants and functions
11First-Order Predicate Calculus cont
- Quanitifiers
- Universal, existential
- Express properties of entire collections of
objects - Universal quantifiers make statements about every
object, "x - A cat is a mammal
- "x Cat(x) Þ Mammal(x)
- Cat(Spot) Þ Mammal(Spot) Ù
- Cat(Rebecca) Þ Mammal(Rebecca) Ù
- Cat(Felix) Þ Mammal(Felix) Ù
- Cat(Richard) Þ Mammal(Richard) Ù
- Cat(John) Þ Mammal(John) Ù
-
12First-Order Predicate Calculus cont
- Existential Quantifiers make statements about
some objects, x - Spot has a sister who is a cat
- x Sister(x, Spot) Ù Cat(x)
- (Sister(Spot, Spot) Ù Cat(Spot)) Ú
- (Sister(Rebecca, Spot) Ù Cat(Rebecca)) Ú
- (Sister(Felix, Spot) Ù Cat(Felix)) Ú
- (Sister(Richard, Spot) Ù Cat(Richard)) Ú
- (Sister(John, Spot) Ù Cat(John)) Ú
13First-Order Predicate Calculus cont
- Connections between and "
- Negation
- Everyone dislikes rugby º Noone likes rugby
- "x ØLikes (x, rugby) º Øx Likes(x, rugby)
- Everyone likes icecream º Noone dislikes icecream
- "x Likes (x, icecream) º Øx ØLikes(x, icecream)
14First-Order Predicate Calculus cont
- " is a conjunction over the universe of objects
- Is a disjunction over the universe of objects
- "x ØP º Øx P
- Ø "x P º x ØP
- "x P º Øx ØP
- Ø "x ØP º x P
15De Morgans Laws
- ØPÙØQ º (PÚQ)
- Ø(PÙQ) º ØPÚØQ
- PÙQ º Ø(ØPÚØQ)
- Ø(ØPÙØQ) º PÚ Q
16Using First-Order Predicate Calculus
- Marcus was a man
- Marcus was a Pompeian
- All Pompeians were Romans
- Caesar was a ruler
- All Romans were either loyal to Caesar or hated
him
17- Everyone is loyal to someone
- People only try to assassinate rulers they are
not loyal to - Marcus tried to assassinate Caesar
- Was Marcus loyal to Caesar?
- Prove Ø loyalto(Marcus, Caesar)
18- Turn the following sentences into formulae in
first order predicate logic - John likes all kinds of food
- Apples are food
- Chicken is food
- Anything anyone eats and isnt killed by is food
- Bill eats peanuts and is still alive
- Sue eats everything Bill eats
- Prove that John likes peanuts using backward
chaining
19A little bit of Prolog
- Objects and relations between objects
- Facts and rules
- parent(pam, bob). parent(tom,bob).
- parent(tom, liz). parent(bob, ann).
- parent(bob, pat). parent(pat, jim).
- ? parent(bob, pat).
- ? parent(bob, liz).
- ? parent(bob, ben).
- ? parent(bob, X).
- ? parent(X, Y).
20Prolog
- grandparent (X,Y) - parent(X, Z), parent(Z, Y).
- For all X and Y
- X is the grandparent of Y if
- X is a parent of Z and
- Z is a parent of Y
- sister (X,Y) - parent(Z, X), parent(Z, Y),
female(X) - For all X and Y
- X is the sister of Y if
- Z is the parent of both X and Y and
- X is a female