Loading in 5 sec....

COMP313A Programming LanguagesPowerPoint Presentation

COMP313A Programming Languages

- 239 Views
- Updated On :

COMP313A Programming Languages. Logic Programming (1). Lecture Outline. Conceptual foundations of Logic Programming The Basics of Logic Programming Predicate Calculus A little bit of logic programming Prolog. Conceptual Foundations. What versus how specification versus implementation

Related searches for COMP313A Programming Languages

Download Presentation
## PowerPoint Slideshow about 'COMP313A Programming Languages' - Roberta

**An Image/Link below is provided (as is) to download presentation**

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript

### COMP313A Programming Languages

Logic Programming (1)

Lecture Outline

- Conceptual foundations of Logic Programming
- The Basics of Logic Programming
- Predicate Calculus

- A little bit of logic programming
- Prolog

Conceptual 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

An 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))

Example continuedImplementation

- Need to know how to split a list into right and left sublists
- How to order the elements stored in the list

A solution in C++

int binary_search(const int val, const int size, const int array[]){

int high, low, mid;

if size <= 0{

return (-1);

}

high = size;

low = 0;

for(;;) {

mid = (high + low) / 2;

if (mid = low){

return (val != array[low]) ?-1:mid;

}

if (val < array[mid]) {

high = mid;

}

else if (val > array[mid]) {

low = mid;

}

else return mid

}}}

A 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

A 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)

Logic 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) -> 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) -> 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) -> natural(successor(x)).

- Logic statements

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

First-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) Ù

…

First-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)) Ú

…

First-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)

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

Using 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

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

- 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 isn’t killed by is food
- Bill eats peanuts and is still alive
- Sue eats everything Bill eats
- Prove that John likes peanuts using backward chaining

A little bit of Prolog predicate logic

- 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).

Prolog predicate logic

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

Download Presentation

Connecting to Server..