Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "P ROGRAMMING L ANGUAGES : L OGIC P ROGRAMMING. S LIDES R EFERENCES Kenneth C. Louden, Functional Programming, Programming Languages: Principles and Practice,"— Presentation transcript:

1 P ROGRAMMING L ANGUAGES : L OGIC P ROGRAMMING

2 S LIDES R EFERENCES 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 2

3 I NTRODUCTION As we mentioned before, paradigms of programming languages are: Imperative Functional Object-oriented Declarative (Logical) 3

4 H ISTORY 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 4

5 A RTIFICIAL I NTELLIGENCE (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 5

6 L OGIC P ROGRAMMING 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. 6

7 L OGIC AND L OGIC P ROGRAMMING 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). 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)). 7

8 E XAMPLES 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. 8

9 E XAMPLES 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. 9

10 (F IRST ORDER ) P REDICATE C ALCULUS Starts with a set of axioms ( true assertions), stated using the following elements: Constants. Usually numbers or names. In the examples, Bill and 0 are constants. Predicates. Names for functions that are true or false, like Boolean functions in a program. Predicates can take a number of arguments. In the examples, ancestor and factorial are predicates. Functions. First-order predicate calculus distinguishes between functions that are true or false—these are the predicates—and all other functions, which represent non-Boolean values. * is a function in the examples. (Factorial - a function - is indirectly expressed as a predicate.) 10

11 P REDICATE C ALCULUS ( CONTINUED ) Variables that stand for as yet unspecified quantities. In the examples, A and m are variables. Connectives. Operations and, or, and not; implication "  " and equivalence "  " (derivable from the previous three). Quantifiers. These are operations that introduce variables: "for all" - the universal quantifier, and "there exists" - the existential quantifier. Punctuation symbols: left and right parentheses, the comma, and the period. Note: Arguments to predicates and functions can only be terms: combinations of variables, constants, and functions. 11

12 E XAMPLES WRITTEN IN P RED. C ALC.: 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). 12

13 H ORN C LAUSES 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). 13

14 P ROLOG Modified 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). 14

15 U NFORTUNATE E RRORS : ?- 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 15

16 W HAT ' S W RONG ???? 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 16

17 A RITHMETIC 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 17

18 S TACK P ROBLEMS 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! 18

19 P ROLOG ' S E XECUTION S TRATEGY 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. 19

20 S OLVING 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. 20

21 L ISTS 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 = 1 Y = 2 21

22 P ATTERN M ATCHING 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. 22

23 P ROLOG S TRUCTURE 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. 23

24 Q UERIES P ROLOG S TRUCTURE – Q UERIES 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. 24

25 DISJUNCTION AND CONJUNCTION P ROLOG S TRUCTURE – DISJUNCTION AND CONJUNCTION. semi-colon Use a semi-colon to request subsequent answers.  In other words, a semi-colon signifies disjunction.   A comma signifies conjunction.  25

26 H OW P ROLOG W ORKS : E XAMPLE 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. 26

27 H OW P ROLOG W ORKS, 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) 27

28 H OW P ROLOG W ORKS, CONT. Prolog answers queries based off of the database that has been given. ?enrolled (chris-clark, ee271) yes ?enrolled (X, ee271) alan-cheng chris-clark ?teaches (X, alan-cheng) louden ?teaches (X, chris-clark) louden Jeske greenwood 28

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).` 29 F AMILY D ATA BASE

30 ?- [kinship]. % kinship compiled 0.00 sec, 3,016 bytes Yes ?- 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 30

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. UNIFICATION PROLOG STRUCTURE - UNIFICATION 31

32 Example of Unification 32

33 B ACKTRACKING P ROLOG S TRUCTURE - B ACKTRACKING 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. 33

34 C LAUSES Programs are constructed from A number of clauses : :- Clauses have three forms: hypotheses (facts) conditions (rules) goals Both and are composed of relationships (also called predications or literals ) assertions (database) questions 34

35 R ELATIONSHIPS 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, … compounds Horn clause form: At most one relationship in 35

36 A S MALL E XAMPLE We write: likes(ann,X) :- toy(X), plays(ann,X). toy(doll). toy(train). plays(ann,train). likes(john,Y) :- likes(ann,Y). 36

37 A S MALL E XAMPLE – W HAT I T M EANS 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 37

38 A S MALL E XAMPLE – W HAT I T M EANS For example: likes(ann,X) :- toy(X), plays(ann,X). 38

39 P ROLOG L ISTS : 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) 39

40 P ROLOG L ISTS : 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 40

41 P ROLOG L ISTS : 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 41

42 P ROLOG L ISTS : 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] 42

43 P ROLOG L ISTS : 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 43

44 P ROLOG L ISTS : 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). 44

45 P ROLOG L ISTS : 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). 45

46 P ROLOG IS MUCH M ORE T HAN J UST I NFORMATION : 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). 46

47 C OMMENTS IN P ROLOG Single line comments use the “%” character Multi-line comments use /* and */ 47

48 S IMPLE I NPUT -O UTPUT IN P ROLOG Simple I/O in Prolog Use the write statement write(‘hello’) write(‘Hello’), write(‘World’) Use a Newline write(‘hello’), nl, write(‘World’) 48

49 R EADING AND WRITING IN P ROLOG Reading a value from stdin Prolog Syntax: read(X) Example read(X), write(X). 49

50 A RITHMETIC IN P ROLOG Using Arithmetic Different to what you may have seen with other languages. Operators > + - * / Arithmetic is done via evaluation then unification 50

51 A RITHMETIC Arithmetic Example X is Y compute Y then unify X and Y X is Y * 2 N is N - 1 51

52 T HE DIFFERENCE BETWEEN IDENTITY RELATION AND UNIFICATION X == Y This 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 = Y This is unification It is true if X is unifiable with Y 52

53 D IFFERENT “ ASSIGNMENT ” OPERATORS IN P ROLOG X=:=Y This means “compute X, compute Y, and see if they both have the same value” both X and Y must be arithmetic expressions X is Y This means compute Y and then unify X and Y Y must be an arithmetic expression X can either be an arithmetic expression (of the same form), or a variable 53

54 C OMPARISON OF ASSIGNMENTS Arithmetic Exercises 1. 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. 54

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). 55 R ECURSION IN P ROLOG

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

57 C ONTROL S TRUCTURES IN P ROLOG Looping…Repeat until user enters “end” command_loop:- repeat, write('Enter command (end to exit): '), read(X), write(X), % new line nl, % new line X = end. 57

58 P RIMITIVES AND C ONSTRUCTORS Few primitives and No constructors. Data types and data structures are defined implicitly by their properties. 58

59 E XAMPLE ( 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) 59

60 P RINCIPLES 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 60

61 D ATA S TRUCTURES 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 61

62 LISP IN P ROLOG 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)))? 62

63 C OMPONENT S ELECTION IN P ROLOG 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)) )) 63

64 C UT ! C UT ! ‘!’ : 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 1 st 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 64


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

Similar presentations


Ads by Google