1 / 89

Prolog Lecture Notes 3rd Week

Prolog Lecture Notes 3rd Week. Logical expressions and prolog Operators Lists. 1. smart(X):-teaches(X,computing). 2. teaches(john,math132). 3. wife(X,john),teaches(X,comp111). 4. mathematics(math132). 5. computing(comp111). 1. likes(X,Y):-mother(X,Y), good(Y). 2. woman(X):- mother(X).

lyle
Download Presentation

Prolog Lecture Notes 3rd Week

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Prolog Lecture Notes 3rd Week Logical expressions and prolog Operators Lists

  2. 1. smart(X):-teaches(X,computing). 2. teaches(john,math132). 3. wife(X,john),teaches(X,comp111). 4. mathematics(math132). 5. computing(comp111).

  3. 1. likes(X,Y):-mother(X,Y), good(Y). 2. woman(X):- mother(X). 3. woman(ann). 4. husband(X,ann),good(X).

  4. A Database • “A car manufacturer uses parts supplied by different companies around the world. Each part has a unique code number, a name, a size, and a price. Different quantities of each part may be ordered from different suppliers.”

  5. A Database • The company needs access to this information to answer questions that arise when considering supplier policy. For Example: • What types of parts are obtained from suppliers in London? • Where are the suppliers of wheels based? • How do we turn this into Prolog?

  6. Program Design • To design suitable data structures we can look for the relevant entities and relationships. • What are the entities in this situation? • What are the relationships of interest?

  7. Entities • In this case: parts and suppliers • These have certain attributes • Each part has: code, name, size, price • Each supplier has: name, address • Possible Prolog terms: • part(Code, Name, Size, Price) • supplier(SName, Address)

  8. Relationships • In this case: who-supplies-what(Links supplier to part, with a quantity) • Possible Prolog terms: • supplier_part(Code, SName, Quantity) • Improvements?

  9. Program (Supplier) % supplier(SNum, SName, City) supplier(10, leman, newyork). supplier(22, adams, london). supplier(46, fuji, tokyo). :: Note: Whatever follows ‘%’ is a comment.

  10. Program (Part) % part(PNum, PName, Size, Price) part(k10, cam, 6, 20). part(m15, shaft, 14, 50). part(s21, wheel, 25, 132). ::

  11. Program (Relationship) % supplier_part(SNum, PNum, Quantity) supplier_part(10, m15, 300). supplier_part(22, k10, 200). supplier_part(22, m15, 100). supplier_part(46, s21, 500). ::

  12. Forming a Query • How can we find which types of parts are supplied from London? • First add to the program an extra rule to describe the relationship between part names and cities of suppliers: part_city(PName, City) :- part(PNum, PName, _, _), supplier_part(SNum, PNum, _), supplier(SNum, _, City).

  13. Forming a Query • Now we can ask for solutions to a specific question by using a query. ?- part_city(Part, london). Part = cam ; Part = shaft ; no

  14. Alternatively sname(10, leman). sname(22, adams). sname(46, fuji). : scity(10, newyork). scity(22, london). scity(46, tokyo). : pname(k10, cam). pname(m15, shaft). pname(s21, wheel). : supplier_part(10, m15, 300). supplier_part(22, k10, 200). supplier_part(22, m15, 100). supplier_part(46, s21, 500). :

  15. Forming a Query • The rule to describe the relationship between part names and cities of suppliers is different in this design: part_city(PName, City) :- pname(PNum, PName), supplier_part(SNum, PNum, _), scity(SNum, City).

  16. Forming a Query • But the queries can be identical: ?- part_city(Part, london). • (This is ‘data abstraction’ in action) • With this new design it is easy to add extra attributes to some records: sstatus(46, unreliable).

  17. Exercise • The secretary of a lonely-hearts club wishes to store the following information about club members: Name: Henry Law Sex: Male Height: 1.78m Weight: 75kg Hobbies: jogging, skiing, singing

  18. Exercise (cont.) • She also wants to be able to answer questions such as: 1. How heavy is Mary Jones? 2. Who is interested in campanology? • Design a Prolog database to store this information and answer these questions.

  19. Logic Puzzles “Three friends of different nationalities, who play different sports, took the ACSC300 course. Michael plays basketball and did better than the American. Simon, the Israeli, did better than the tennis player.The cricket player did best of all.” 1. Who is the Australian? 2. What sport does Sarah play? How do we turn this into Prolog?

  20. Program Design • To design suitable data structures we can look for the relevant entities and relationships (as in the database design). • What are the entities in this situation? • What are the relationships of interest?

  21. Entities • In this case: people • Each with three attributes • name • nationality • sport • Prolog term: person(Name, Nation, Sport)

  22. Relationships • In this case: order • Could be described by: • adding a score, or ranking attribute • building a sequence, or list • Prolog term: sequence(P1, P2, P3)

  23. Program possible_sequence(S) :- did_better(S, person(michael, _, basketball), person(_, america, _)), did_better(S, person(simon, israel, _), person(_, _, tennis)), did_best(S, person(_, _, cricket)). did_better(sequence(P1, P2, _), P1, P2). did_better(sequence(P1, _, P2), P1, P2). did_better(sequence(_, P1, P2), P1, P2). did_best(sequence(P, _, _), P).

  24. Forming a Query • First we add rules to describe solutions: solution(Name, Nation, Sport) :- possible_sequence(S), contains(S, person(Name, Nation, Sport)). contains(sequence(P, _, _), P). contains(sequence(_, P, _), P). contains(sequence(_, _, P), P).

  25. Forming a Query • Now we can ask for solutions to specific questions: ?- solution(Name, australia, _). ?- solution(sarah, _, Sport). • In this way we can use the solution rule to answer any question.

  26. Operator notation (1) • Since Prolog uses functors for mathematical expressions, an expression like 2×a + b×c can be represented in Prolog by +( *(2,a), *(b,c)) However, Prolog allows the infix notation for some operators, allowing the expression to be written: 2*a + b*c The * operator of course binds “”stronger” than the +, which is defined by Prolog’s precedence: the principal functor has the highest precedence. So a + b * c means in Prolog: +(a,*(b,c)), and not *( +(a,b), c) In the latter case, use parentheses: (a + b) * c

  27. Operator notation (2) Precedence: number between 0 and 1200 (in Amzi! Prolog), with as rule • Principal functor has highest precedence For example: + precedence 500, - precedence 400. Then: a + b * c highest precedence +, so + is the principal functor, so +(a, *(b,c)) is the Prolog interpretation. • The precedence of a structure is the precedence of the principal functor of the structure; • The precedence of a non-structured term is 0.

  28. bw 0 and 1200

  29. Operator notation (3) • Associativity is one of the following atoms: xfx yfx fx xf xfy yfy fy yf in which x and y are arguments, and f is the operator. So ?f? indicates an infix operator, ?f indicates a postfix operator, and f? indicates a prefix operator. Further: x: precedence of argument < precedence of f; y: precedence of argument ≤ precedence of f.

  30. Operator notation (4) Defining an operator using op/3. E.g.: ?- op(500, yfx, -). This defines the – operator as an infix operator in such a way that a – b – c means (a – b) – c and not a – (b – c) Check: - - precedence 500 precedence 500 - - c a precedence 0 precedence 0 a b a b OK Not OK precedence 500 precedence 500

  31. Operator notation (5) An operator may be defined once as an infix and once as either prefix or postfix, so ?- op(500, xfy, +) % + is an infix operator ?- op(700, fx, +) % + is now an infix and a prefix operator ?- op(700, xf, +) % + is now an infix and a postfix operator

  32. Operator notation (6) • Some Predefined Prolog operators: :- (op(1200, xfx, [:-, -->])). :- (op(1200, fx, [?-, :-])). :- (op(1100, xfy, ';')). :- (op(1050, xfy, ->)). :- (op(1000, xfy, ',')). :- (op(900, fy, [not])). :- (op(700, xfx, [=, \=, is, =.., ==, \==, =:=, ~=, =\=, <, >, =<, >=])). :- (op(600, xfy, :)). :- (op(500, yfx, [+, -, /\, \/, xor])). :- (op(400, yfx, [/, //, *, >>, <<])). :- (op(200, xfx, **)). :- (op(200, xfy, ^)). :- (op(200, fy, [+, -, \])).

  33. Operator notation (7) • Sometimes it is useful to define your own operators. For instance, de Morgan’s theorem ~(A & B) ~A | ~B could be defined in Prolog as: equiv( not( and(A,B), or( not(A), not(B))). Better is to introduce the following operators: :- op(800, xfx, <===>). :- op(700, xfy, v). :- op(600, xfy, &). :- op(500, fy, ~). Now de Morgan’s theorem can be written as: ~(A & B) <==> ~A v ~B.

  34. Operator notation (8) Some remarks on operators: • Special operator notation can be useful, especially when the infix, prefix and postfix notation are used in a “natural” way. • Operators are just functors! So they are not necessarily connected to actions. However, for predefined actions (e.g., arithmetic) see the next slides. • Besides predefined operators it can be useful to define your own operators, specifying the precedence, associativity and the name of the operator. Moreover, you have to define their meaning!

  35. Operator example • a-b-c how do we interpret this? (a-b)-c OR a-(b-c) • Normally the first • Since – is defined as yfx • not not pnot(not p) • is legal if not is defined as fy • not legal if it is fx • See Figure 3.8 for more predefined operators.

  36. Operations on Lists • Lists vs Sets: analoguous except • The order is important in lists • The element repetition is allowed in lists • Common operations on lists are similiar to set operations • Membership check • Concatenation • Adding/deleting elements to/from

More Related