p rogramming l anguages : l ogic p rogramming. s lides r eferences kenneth c. louden, functional...

62
PROGRAMMING LANGUAGES: LOGIC PROGRAMMING

Upload: phebe-harrington

Post on 18-Dec-2015

223 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: P ROGRAMMING L ANGUAGES : L OGIC P ROGRAMMING. S LIDES R EFERENCES Kenneth C. Louden, Functional Programming, Programming Languages: Principles and Practice,

PROGRAMMING LANGUAGES:

LOGIC PROGRAMMING

Page 2: P ROGRAMMING L ANGUAGES : L OGIC P ROGRAMMING. S LIDES R EFERENCES Kenneth C. Louden, Functional Programming, Programming Languages: Principles and Practice,

2

SLIDES REFERENCES

Kenneth C. Louden, Functional Programming, Programming Languages: Principles and Practice, Ch4.

COMP313A Programming Languages, University of Waikato, New Zealand.

Philip O’Keefe, Logic Programming, Memorial University, Canada

Page 3: P ROGRAMMING L ANGUAGES : L OGIC P ROGRAMMING. S LIDES R EFERENCES Kenneth C. Louden, Functional Programming, Programming Languages: Principles and Practice,

3

INTRODUCTION

As we mentioned before, paradigms of programming languages are:ImperativeFunctionalObject-orientedDeclarative (Logical)

Page 4: P ROGRAMMING L ANGUAGES : L OGIC P ROGRAMMING. S LIDES R EFERENCES Kenneth C. Louden, Functional Programming, Programming Languages: Principles and Practice,

4

HISTORY

Logic Programming can be traced back to 1958 by John McCarthy

He proposed a hypothetical computer program named advice taker

Probable first proposal to represent information as logic for a program

John McCarthy (1927-2011)• A computer scientist @ Stanford Uni• Received Turing award for contributions

in Artificial Intelligence• Inventor of the LISP

Page 5: P ROGRAMMING L ANGUAGES : L OGIC P ROGRAMMING. S LIDES R EFERENCES Kenneth C. Louden, Functional Programming, Programming Languages: Principles and Practice,

5

ARTIFICIAL INTELLIGENCE (AI)

Knowledge representation is one of the most important subareas of AI

For an entity to behave intelligently it must be supplied sufficient knowledge

This mean an unambiguous language capable of expressing this knowledge must be used

This information must be able to be manipulated to answer queries

A declarative language fits these requirements well

Page 6: P ROGRAMMING L ANGUAGES : L OGIC P ROGRAMMING. S LIDES R EFERENCES Kenneth C. Louden, Functional Programming, Programming Languages: Principles and Practice,

6

LOGIC PROGRAMMING

Uses a set of logical assertions (i.e. statements that are either true or false), as a program (the facts).

Execution is initiated by a query or goal, which the system attempts to prove true or false, based on the existing set of assertions.

For this reason, logic programming systems are sometimes called deductive databases.

Two main "weirdnesses": no explicit functions, no explicit execution control.

Page 7: P ROGRAMMING L ANGUAGES : L OGIC P ROGRAMMING. S LIDES R EFERENCES Kenneth C. Louden, Functional Programming, Programming Languages: Principles and Practice,

7

LOGIC AND LOGIC PROGRAMMING First-order predicate calculus

Logic statements

ExamplesJohn is a man. man(John).John is a human. human(John).Tristan is the son of Margaret. son(Margaret,Tristan).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)).

Page 8: P ROGRAMMING L ANGUAGES : L OGIC P ROGRAMMING. S LIDES R EFERENCES Kenneth C. Louden, Functional Programming, Programming Languages: Principles and Practice,

8

EXAMPLES Computing ancestors:

A parent is an ancestor.If A is an ancestor of B, and B is an ancestor of C, then A is an ancestor of C. (a typical relation: called ??)A mother is a parent.A father is a parent.Bill is the father of Jill.Jill is the mother of Sam.Bob is the father of Sam.

Computing the factorial function:The factorial of 0 is 1.If m is the factorial of n - 1, then n * m is the factorial of n.

Page 9: P ROGRAMMING L ANGUAGES : L OGIC P ROGRAMMING. S LIDES R EFERENCES Kenneth C. Louden, Functional Programming, Programming Languages: Principles and Practice,

9

EXAMPLES Computing ancestors:

A parent is an ancestor.If A is an ancestor of B, and B is an ancestor of C, then A is an ancestor of C. (a typical relation: called ??)A mother is a parent.A father is a parent.Bill is the father of Jill.Jill is the mother of Sam.Bob is the father of Sam.

Computing the factorial function:The factorial of 0 is 1.If m is the factorial of n - 1, then n * m is the factorial of n.

Page 10: P ROGRAMMING L ANGUAGES : L OGIC P ROGRAMMING. S LIDES R EFERENCES Kenneth C. Louden, Functional Programming, Programming Languages: Principles and Practice,

12

EXAMPLES WRITTEN IN PRED. CALC.: Ancestors:

For all X and Y, parent(X,Y) ancestor(X,Y). For all A, B, and C, ancestor(A,B) and ancestor(B,C) ancestor(A,C). For all X and Y, mother(X,Y) parent(X,Y).For all X and Y, father(X,Y) parent(X,Y).Father(Bill,Jill).Mother(Jill,Sam).Father(Bob,Sam).

Factorials:Factorial(0,1).For all n and m, factorial(n-1,m) factorial(n,n*m).

Page 11: P ROGRAMMING L ANGUAGES : L OGIC P ROGRAMMING. S LIDES R EFERENCES Kenneth C. Louden, Functional Programming, Programming Languages: Principles and Practice,

13

HORN CLAUSES A clause (i.e., a disjunction of literals) is called a Horn clause

if it contains at most one positive literal. Horn clauses are usually written as

L1,...,Ln=>L(=¬L1 v ... v ¬Ln v L) or L1,...,Ln=>(=¬L1 v ... v ¬Ln), where n>=0 and L is the

only positive literal. Drop the quantifiers ( , ). i.e., assume them implicitly.

Distinguish variables from constants, predicates, and functions by upper/lower case:parent(X,Y) ancestor(X,Y). ancestor(A,B) and ancestor(B,C) ancestor(A,C). mother(X,Y) parent(X,Y).father(X,Y) parent(X,Y).father(bill,jill).mother(jill,sam).father(bob,sam).

factorial(0,1).factorial(N-1,M) factorial(N,N*M).

Page 12: P ROGRAMMING L ANGUAGES : L OGIC P ROGRAMMING. S LIDES R EFERENCES Kenneth C. Louden, Functional Programming, Programming Languages: Principles and Practice,

14

PROLOGModified Horn clause syntax: write the clauses

backward, with :- as the (backward) arrow, comma as "and" and semicolon as "or":ancestor(X,Y) :- parent(X,Y).ancestor(X,Y) :- ancestor(X,Z), ancestor(Z,Y).

parent(X,Y) :- mother(X,Y).parent(X,Y) :- father(X,Y).father(bill,jill).mother(jill,sam).father(bob,sam).factorial(0,1).factorial(N,N*M) :- factorial(N-1,M).

Page 13: P ROGRAMMING L ANGUAGES : L OGIC P ROGRAMMING. S LIDES R EFERENCES Kenneth C. Louden, Functional Programming, Programming Languages: Principles and Practice,

15

UNFORTUNATE ERRORS:?- ancestor(bill,sam).

Yes.

?- ancestor(bill,X).

X = jill ;

X = sam ;

ERROR: Out of local stack

?- ancestor(X,bob).

ERROR: Out of local stack

?- factorial(2,2).

No

?- factorial(0,X).

X = 1 ;

ERROR: Out of global stack

Page 14: P ROGRAMMING L ANGUAGES : L OGIC P ROGRAMMING. S LIDES R EFERENCES Kenneth C. Louden, Functional Programming, Programming Languages: Principles and Practice,

16

WHAT'S WRONG????

ancestor(X,Y) :- parent(X,Y).ancestor(X,Y) :- ancestor(X,Z), ancestor(Z,Y).

parent(X,Y) :- mother(X,Y).parent(X,Y) :- father(X,Y).father(bill,jill).mother(jill,sam).father(bob,sam).factorial(0,1).factorial(N,N*M) :- factorial(N-1,M).

Immediate recursion

No arithmetic is actually computed

Page 15: P ROGRAMMING L ANGUAGES : L OGIC P ROGRAMMING. S LIDES R EFERENCES Kenneth C. Louden, Functional Programming, Programming Languages: Principles and Practice,

17

ARITHMETIC Easy to fix - arithmetic expressions must

be explicitly forced:?- 2*3 = 6.No?- 2*3 = 2*3.Yes?- 6 is 2*3.Yes?- 2*3 is 6.No

Rewrite factorial like this:factorial(0,1).factorial(N,P) :- N1 is N-1, factorial(N1,M), P is M*N.

"is" forces its right-hand argument

But not its left-hand argument

Page 16: P ROGRAMMING L ANGUAGES : L OGIC P ROGRAMMING. S LIDES R EFERENCES Kenneth C. Louden, Functional Programming, Programming Languages: Principles and Practice,

18

STACK PROBLEMS A BIT HARDER: Stack problems still exist for factorial:?- factorial(7,X).X = 5040 ;ERROR: Out of local stack

To figure this out, we must understand the procedural interpretation of Horn clauses:a :- b, c, d.represents the definition of a (boolean) function named a. The body of a is given by the clauses on the right hand side: b, then c, then d.

Note similarity to recursive-descent parsing! Also note sequencing!

Page 17: P ROGRAMMING L ANGUAGES : L OGIC P ROGRAMMING. S LIDES R EFERENCES Kenneth C. Louden, Functional Programming, Programming Languages: Principles and Practice,

19

PROLOG'S EXECUTION STRATEGY Given a query or goal, Prolog tries to

pattern match the goal with the left-hand sides of all clauses, in a sequential top-down fashion.

Any lhs that matches causes the rhs terms to be set up sequentially as subgoals, which Prolog immediately tries to match in turn with lhs terms.

Thus, Prolog's execution path is top-down, left-to-right, depth-first. All intermediate results are kept for backtracking purposes.

Page 18: P ROGRAMMING L ANGUAGES : L OGIC P ROGRAMMING. S LIDES R EFERENCES Kenneth C. Louden, Functional Programming, Programming Languages: Principles and Practice,

20

SOLVING THE STACK PROBLEMS

For the ancestor problem, just remove the left recursion:ancestor(X,Y) :- parent(X,Y).ancestor(X,Y) :- ancestor(X,Z), ancestor(Z,Y). parent(X,Z)

For the factorial problem, it involves preventing reuse of the recursive rule:factorial(0,1).factorial(N,P) :- not(N=0), N1 is N-1, factorial(N1,M), P is M*N.

Page 19: P ROGRAMMING L ANGUAGES : L OGIC P ROGRAMMING. S LIDES R EFERENCES Kenneth C. Louden, Functional Programming, Programming Languages: Principles and Practice,

21

LISTS Prolog uses almost the same list syntax

as Haskell or ML: [1,2,3] Head and tail pattern syntax is different:

[H|T] versus (h:t) in Haskell or (h::t) in ML Also, as many elements as desired can

be written using commas before the bar:[1,2,3|[4]] is the list [1,2,3,4].

Example:?- [1,2,3] = [X,Y|_].X = 1Y = 2

Page 20: P ROGRAMMING L ANGUAGES : L OGIC P ROGRAMMING. S LIDES R EFERENCES Kenneth C. Louden, Functional Programming, Programming Languages: Principles and Practice,

22

PATTERN MATCHING

Pattern matching is more general in Prolog than it is in Haskell: variables can be repeated in patterns, implying

that they must be the same value:?- [1,1] = [X,X].X = 1?- [1,2] = [X,X].No

Other operations can be more general too.

Page 21: P ROGRAMMING L ANGUAGES : L OGIC P ROGRAMMING. S LIDES R EFERENCES Kenneth C. Louden, Functional Programming, Programming Languages: Principles and Practice,

23

PROLOG STRUCTURE

Prolog facts – a database of predicates and associations.

Prolog rules – define new predicates by using Prolog facts.

Note: Prolog considers capital letters to denote

variables, not predicates.

Page 22: P ROGRAMMING L ANGUAGES : L OGIC P ROGRAMMING. S LIDES R EFERENCES Kenneth C. Louden, Functional Programming, Programming Languages: Principles and Practice,

24

PROLOG STRUCTURE – QUERIES

A query searches the database for the first fact that satisfies its goal.

If a fact is found then it either unifies the variable with a constant or Prolog returns yes.

If a fact is not found that meets that condition then Prolog returns no.

Page 23: P ROGRAMMING L ANGUAGES : L OGIC P ROGRAMMING. S LIDES R EFERENCES Kenneth C. Louden, Functional Programming, Programming Languages: Principles and Practice,

25

PROLOG STRUCTURE – DISJUNCTION AND CONJUNCTION. Use a semi-colon to request subsequent

answers.

In other words, a semi-colon signifies disjunction.

A comma signifies conjunction.

Page 24: P ROGRAMMING L ANGUAGES : L OGIC P ROGRAMMING. S LIDES R EFERENCES Kenneth C. Louden, Functional Programming, Programming Languages: Principles and Practice,

26

HOW PROLOG WORKS: EXAMPLE 1

Example of data base:instructor (louden, ee271)

instructor (louden, ee171)

instructor (louden, ee478)

enrolled (alan-cheng, ee475)

enrolled (matthew, ee171)

enrolled (alan-cheng,ee171)

enrolled (alan-cheng,ee271)

enrolled (chris-clark,ee271)

enrolled (edison-tsai, ee171)

enrolled (chris-clark, ee171)

This is the database of Prolog facts.

Page 25: P ROGRAMMING L ANGUAGES : L OGIC P ROGRAMMING. S LIDES R EFERENCES Kenneth C. Louden, Functional Programming, Programming Languages: Principles and Practice,

27

HOW PROLOG WORKS, CONT.

Prolog rules:teaches (P,S) :- instructor (P,C), enrolled (S,C)

This is to say that an instructor only teaches if he teaches a class and students are enrolled in that class.

teaches (Professor, Student) :- instructor (Professor, Class), enrolled (Student,Class)

teaches (Professor, Student) instructor (Professor, Class), enrolled (Student,Class)

instructor (Professor, Class), enrolled (Student,Class) teaches (Professor, Student)

Page 26: P ROGRAMMING L ANGUAGES : L OGIC P ROGRAMMING. S LIDES R EFERENCES Kenneth C. Louden, Functional Programming, Programming Languages: Principles and Practice,

28

HOW PROLOG WORKS, CONT. Prolog answers queries based off of the

database that has been given.?enrolled (chris-clark, ee271) yes

?enrolled (X, ee271)alan-chengchris-clark

?teaches (X, alan-cheng)louden

?teaches (X, chris-clark)loudenJeskegreenwood

Page 27: P ROGRAMMING L ANGUAGES : L OGIC P ROGRAMMING. S LIDES R EFERENCES Kenneth C. Louden, Functional Programming, Programming Languages: Principles and Practice,

29

parent(X,Y) :- father(X,Y).parent(X,Y) :- mother(X,Y).grandparent(X,Z) :- parent(X,Y), parent(Y,Z).ancestor(X,Z) :- parent(X,Z).ancestor(X,Y) :- parent(X,Y), ancestor(Y,Z).sibling(X,Y) :- mother(M,X), mother(M,Y), father(F,X), father(F,Y), X \= Y.cousin(X,Y) :- parent(U,X), parent(V,Y), sibling(U,V).

father(albert, jeffrey).mother(alice, jeffrey).father(albert, george).mother(alice, george).father(john, mary).mother(sue, mary).father(george, cindy).mother(mary, cindy).father(george, victor).mother(mary, victor).`

FAMILY DATA BASE

Page 28: P ROGRAMMING L ANGUAGES : L OGIC P ROGRAMMING. S LIDES R EFERENCES Kenneth C. Louden, Functional Programming, Programming Languages: Principles and Practice,

30

?- [kinship].% kinship compiled 0.00 sec, 3,016 bytesYes

?- ancestor(X, cindy), sibling(X, jeffrey).X = george Yes

?- grandparent(albert, victor).Yes

?- cousin(alice, john).No

?- sibling(A,B).A = jeffrey, B = george ; A = george, B = jeffrey ; A = cindy, B = victor ; A = victor, B = cindy ; No

SWI Prolog

Page 29: P ROGRAMMING L ANGUAGES : L OGIC P ROGRAMMING. S LIDES R EFERENCES Kenneth C. Louden, Functional Programming, Programming Languages: Principles and Practice,

31

1. Process of making one predicate same as another.

2. A query resolves by unifying all of its elements.

3. A constant unifies with itself and any variable.

4. Scope is limited to the rule in which a variable occurs.

5. When a variable is unified with a constant in a rule, all instances of that variable in that rule are unified to that constant.

PROLOG STRUCTURE - UNIFICATION

Page 30: P ROGRAMMING L ANGUAGES : L OGIC P ROGRAMMING. S LIDES R EFERENCES Kenneth C. Louden, Functional Programming, Programming Languages: Principles and Practice,

32

Example of Unification

Page 31: P ROGRAMMING L ANGUAGES : L OGIC P ROGRAMMING. S LIDES R EFERENCES Kenneth C. Louden, Functional Programming, Programming Languages: Principles and Practice,

33

PROLOG STRUCTURE - BACKTRACKING

1. In more complex examples, Prolog uses backtracking to find possible solutions.

2. Prolog will attempt to resolve the first fact of its rule, unifying any variables with the first constant that satisfies that fact

3. It then attempts to resolve the rest of that rules facts.

4. If it is unable to do so under those conditions it “backs up” and tries again with the next available unifying constant.

Page 32: P ROGRAMMING L ANGUAGES : L OGIC P ROGRAMMING. S LIDES R EFERENCES Kenneth C. Louden, Functional Programming, Programming Languages: Principles and Practice,

34

CLAUSES

Programs are constructed from A number of

clauses: <head> :- <body>

Clauses have three forms:

hypotheses (facts)

conditions (rules)

goals

Both <head> and <body> are composed of

relationships (also called predications or

literals)

assertions (database)

questions

Page 33: P ROGRAMMING L ANGUAGES : L OGIC P ROGRAMMING. S LIDES R EFERENCES Kenneth C. Louden, Functional Programming, Programming Languages: Principles and Practice,

35

RELATIONSHIPS

Represent properties of and relations among the individuals

A relationship is application of a predicate to one or more terms

Terms:atoms (or constants): john, 25, …variables (begin with uppercase letters): X, …

compoundsHorn clause form: At most one

relationship in <head>

Page 34: P ROGRAMMING L ANGUAGES : L OGIC P ROGRAMMING. S LIDES R EFERENCES Kenneth C. Louden, Functional Programming, Programming Languages: Principles and Practice,

36

A SMALL EXAMPLE

We write:

likes(ann,X) :- toy(X), plays(ann,X).toy(doll). toy(train).plays(ann,train).likes(john,Y) :- likes(ann,Y).

Page 35: P ROGRAMMING L ANGUAGES : L OGIC P ROGRAMMING. S LIDES R EFERENCES Kenneth C. Louden, Functional Programming, Programming Languages: Principles and Practice,

37

A SMALL EXAMPLE – WHAT IT MEANS

There are three important logical symbols

:- if

, and

; or

X and Y are variables ann, john, doll and train are constants likes, toy and plays are predicate symbols

Page 36: P ROGRAMMING L ANGUAGES : L OGIC P ROGRAMMING. S LIDES R EFERENCES Kenneth C. Louden, Functional Programming, Programming Languages: Principles and Practice,

38

A SMALL EXAMPLE – WHAT IT MEANS

For example:

likes(ann,X) :- toy(X), plays(ann,X).

<---Head---> <-------Body------->

Page 37: P ROGRAMMING L ANGUAGES : L OGIC P ROGRAMMING. S LIDES R EFERENCES Kenneth C. Louden, Functional Programming, Programming Languages: Principles and Practice,

39

PROLOG LISTS: CONCEPT Lists are a collection of terms inside [ and ]

[ bmw, ford, benz]

loc_list([apple, orange, cake], kitchen). loc_list([desk, computer], office). loc_list([flashlight, envelope], desk). loc_list([stamp, key], envelope). loc_list(['washing

machine'], cellar). loc_list([nani], 'washing machine'). loc_list([], hall)

Page 38: P ROGRAMMING L ANGUAGES : L OGIC P ROGRAMMING. S LIDES R EFERENCES Kenneth C. Louden, Functional Programming, Programming Languages: Principles and Practice,

40

PROLOG LISTS: MEMBER AND APPEND

List functions [H|T]

separate list into head and tail member

test if X is a member of a list append

append two lists to form a third list

Page 39: P ROGRAMMING L ANGUAGES : L OGIC P ROGRAMMING. S LIDES R EFERENCES Kenneth C. Louden, Functional Programming, Programming Languages: Principles and Practice,

41

PROLOG LISTS: HEAD AND TAIL

Head and Tail of a List Syntax

[H|T] Examples ?- [a|[b,c,d]] = [a,b,c,d]. %We check identity

yes ?- [a|b,c,d] = [a,b,c,d].

no

Page 40: P ROGRAMMING L ANGUAGES : L OGIC P ROGRAMMING. S LIDES R EFERENCES Kenneth C. Louden, Functional Programming, Programming Languages: Principles and Practice,

42

PROLOG LISTS: MORE ON HEAD AND TAIL

More Examples?- [H|T] = [apple, orange, refrigerator].

H = apple T = [orange, refrigerator]

?- [H|T] = [a, b, c, d, e]. H = a T = [b, c, d, e]

?- [H|T] = [apples, bananas]. H = apples T = [bananas]

Page 41: P ROGRAMMING L ANGUAGES : L OGIC P ROGRAMMING. S LIDES R EFERENCES Kenneth C. Louden, Functional Programming, Programming Languages: Principles and Practice,

43

PROLOG LISTS: MORE ON HEAD, TAIL AND LIST STRUCTURE More Examples

?- [One, Two | T] = [apple, sprouts, fridge, milk]. One = apple Two = sprouts T = [fridge, milk]

?- [a|[b|[c|[d|[]]]]] = [a,b,c,d]. yes

Page 42: P ROGRAMMING L ANGUAGES : L OGIC P ROGRAMMING. S LIDES R EFERENCES Kenneth C. Louden, Functional Programming, Programming Languages: Principles and Practice,

44

PROLOG LISTS: MEMBER

Testing if an element is in a list. Syntax

member(X, L). Example

member(apple, [apple, orange, cake]). member(X, CarList).

Full Predicate defined as:member(H,[H|T]). member(X,[H|T]) :- member(X,T).

Page 43: P ROGRAMMING L ANGUAGES : L OGIC P ROGRAMMING. S LIDES R EFERENCES Kenneth C. Louden, Functional Programming, Programming Languages: Principles and Practice,

45

PROLOG LISTS: APPEND

Appending two lists to form a third. Syntax

append(L1, L2, L3). Example

append( [a,b,c], [d,e,f], X).X = [a,b,c,d,e,f]

Full predicate defined as:append([],X,X). append([H|T1],X,[H|T2]) :- append(T1,X,T2).

Page 44: P ROGRAMMING L ANGUAGES : L OGIC P ROGRAMMING. S LIDES R EFERENCES Kenneth C. Louden, Functional Programming, Programming Languages: Principles and Practice,

46

PROLOG IS MUCH MORE THAN JUST INFORMATION: LISTS AND TREES Prolog rules can also be used write

programs that do more than find the answers to simple database queries. append([], L, L). append([H|T], L, [H|L1]) :- append(T, L, L1).

This will append a list to another list recursively.

A binary tree can be defined as follows tree(nil). tree(node(_ , Left, Right) :- tree(left),

tree(right).

Page 45: P ROGRAMMING L ANGUAGES : L OGIC P ROGRAMMING. S LIDES R EFERENCES Kenneth C. Louden, Functional Programming, Programming Languages: Principles and Practice,

47

COMMENTS IN PROLOG

Single line comments use the “%” character

Multi-line comments use /* and */

Page 46: P ROGRAMMING L ANGUAGES : L OGIC P ROGRAMMING. S LIDES R EFERENCES Kenneth C. Louden, Functional Programming, Programming Languages: Principles and Practice,

48

SIMPLE INPUT-OUTPUT IN PROLOG

Simple I/O in Prolog Use the write statement

write(‘hello’) write(‘Hello’), write(‘World’)

Use a Newline write(‘hello’), nl, write(‘World’)

Page 47: P ROGRAMMING L ANGUAGES : L OGIC P ROGRAMMING. S LIDES R EFERENCES Kenneth C. Louden, Functional Programming, Programming Languages: Principles and Practice,

49

READING AND WRITING IN PROLOG

Reading a value from stdin

Prolog Syntax: read(X)

Exampleread(X), write(X).

Page 48: P ROGRAMMING L ANGUAGES : L OGIC P ROGRAMMING. S LIDES R EFERENCES Kenneth C. Louden, Functional Programming, Programming Languages: Principles and Practice,

50

ARITHMETIC IN PROLOG

Using Arithmetic Different to what you may have seen with other

languages. Operators

< <= == != => > + - * /

Arithmetic is done via evaluation then unification

Page 49: P ROGRAMMING L ANGUAGES : L OGIC P ROGRAMMING. S LIDES R EFERENCES Kenneth C. Louden, Functional Programming, Programming Languages: Principles and Practice,

51

ARITHMETIC

Arithmetic Example

X is Y compute Y then unify X and Y

X is Y * 2 N is N - 1

Page 50: P ROGRAMMING L ANGUAGES : L OGIC P ROGRAMMING. S LIDES R EFERENCES Kenneth C. Louden, Functional Programming, Programming Languages: Principles and Practice,

52

THE DIFFERENCE BETWEEN IDENTITY RELATION AND UNIFICATION

X == YThis is the identity relation. In order for

this to be true, X and Y must both be identical variables (i.e. have the same name), or both be identical constants, or both be identical operations applied to identical terms

X = YThis is unification It is true if X is unifiable with Y

Page 51: P ROGRAMMING L ANGUAGES : L OGIC P ROGRAMMING. S LIDES R EFERENCES Kenneth C. Louden, Functional Programming, Programming Languages: Principles and Practice,

53

DIFFERENT “ASSIGNMENT” OPERATORS IN PROLOG

X=:=YThis means “compute X, compute Y, and

see if they both have the same value”both X and Y must be arithmetic

expressions

X is YThis means compute Y and then unify X

and YY must be an arithmetic expressionX can either be an arithmetic expression

(of the same form), or a variable

Page 52: P ROGRAMMING L ANGUAGES : L OGIC P ROGRAMMING. S LIDES R EFERENCES Kenneth C. Louden, Functional Programming, Programming Languages: Principles and Practice,

54

COMPARISON OF ASSIGNMENTS

Arithmetic Exercises1. X = 2, Y is X+1 2. X = 2, Y = X+1 3. X = 2, Y == X+1 4. X = 2, Y =:= X+1 5. X = 2, 3 =:= X+1

Check these on the Prolog system.

Page 53: P ROGRAMMING L ANGUAGES : L OGIC P ROGRAMMING. S LIDES R EFERENCES Kenneth C. Louden, Functional Programming, Programming Languages: Principles and Practice,

55

Arithmetic Examples: GCD

gcd(X,X,X). gcd(X,Y,Z) :- X<Y, Y1 is Y-X, gcd(X,Y1,Z).gcd(X,Y,Z) :- X>Y, X1 is X-Y, gcd(X1,Y,Z).

RECURSION IN PROLOG

Page 54: P ROGRAMMING L ANGUAGES : L OGIC P ROGRAMMING. S LIDES R EFERENCES Kenneth C. Louden, Functional Programming, Programming Languages: Principles and Practice,

56

EXAMPLE: FACTORIAL

fact(0,1).

fact(X,F) :- X>0, X1 is X-1, fact(X1,F1), F is X*F1.

Page 55: P ROGRAMMING L ANGUAGES : L OGIC P ROGRAMMING. S LIDES R EFERENCES Kenneth C. Louden, Functional Programming, Programming Languages: Principles and Practice,

57

CONTROL STRUCTURES IN PROLOG

Looping…Repeat until user enters “end”

command_loop:- repeat, write('Enter command (end to exit): '), read(X), write(X), nl, % new lineX = end.

Page 56: P ROGRAMMING L ANGUAGES : L OGIC P ROGRAMMING. S LIDES R EFERENCES Kenneth C. Louden, Functional Programming, Programming Languages: Principles and Practice,

58

PRIMITIVES AND CONSTRUCTORS

Few primitives and No constructors.

Data types and data structures are defined implicitly by their properties.

Page 57: P ROGRAMMING L ANGUAGES : L OGIC P ROGRAMMING. S LIDES R EFERENCES Kenneth C. Louden, Functional Programming, Programming Languages: Principles and Practice,

59

EXAMPLE (DATATYPE)

Natural number arithmetic

sum(succ(X), Y, succ(Z)) :- sum(X,Y,Z).sum(0,X,X).dif(X,Y,Z) :- sum(Z,Y,X).

:-sum(succ(succ(0)),succ(succ(succ(0))),A).A = succ(succ(succ(succ(succ(0)))))

Very inefficient! Use of ‘is’ operator (unidirectional)

Page 58: P ROGRAMMING L ANGUAGES : L OGIC P ROGRAMMING. S LIDES R EFERENCES Kenneth C. Louden, Functional Programming, Programming Languages: Principles and Practice,

60

PRINCIPLES OF LOGIC PROGRAMMING LANGUAGES

Simplicity Small number of built-in data types and

operations

Regularity Uniform treatment of all data types as predicates

and terms

Page 59: P ROGRAMMING L ANGUAGES : L OGIC P ROGRAMMING. S LIDES R EFERENCES Kenneth C. Louden, Functional Programming, Programming Languages: Principles and Practice,

61

DATA STRUCTURES FROM OTHER LANGUAGES

Compound terms can represent data structures

Example: Lists in LISP

(car (cons X L)) = X(cdr (cons X L)) = L(cons (car L) (cdr L)) = L, for nonnull L

Page 60: P ROGRAMMING L ANGUAGES : L OGIC P ROGRAMMING. S LIDES R EFERENCES Kenneth C. Louden, Functional Programming, Programming Languages: Principles and Practice,

62

LISP IN PROLOG

Using compound terms:car( cons(X,L), X).cdr( cons(X,L), L).list(nil).list(cons(X,L)) :- list(L).null(nil).

What about null(L)?

How to accomplish (car (cons ‘(a b) ‘(c d)))?

Page 61: P ROGRAMMING L ANGUAGES : L OGIC P ROGRAMMING. S LIDES R EFERENCES Kenneth C. Louden, Functional Programming, Programming Languages: Principles and Practice,

63

COMPONENT SELECTION IN PROLOG

Implicitly done by pattern matching (unification).append( [ ], L, L).append( X.P, L, X.Q) :- append(P,L,Q).

Compare with Scheme append:(define append (M L) (if (null M) L (cons (car M) (append (cdr M) L)) ))

Page 62: P ROGRAMMING L ANGUAGES : L OGIC P ROGRAMMING. S LIDES R EFERENCES Kenneth C. Louden, Functional Programming, Programming Languages: Principles and Practice,

64

CUT!

‘!’: Discard choice points of parent frame and frames created after the parent frame.

Always is satisfied. Used to guarantee termination or control

execution order.

i.e. in the goal :- p(X,a), ! Only produce the 1st answer to X Probably only one X satisfies p and trying to find another

one leads to an infinite search!

i.e. in the rule color(X,red) :- red(X), !. Don’t try other choices of red (mentioned above) and

color if X satisfies red Similar to then part of a if-then-elseif

Fisher, J.R., Prolog Tutorial, http://www.csupomona.edu/~jrfisher/www/prolog_tutorial/contents.html