1 / 36

Declarative Programming

Declarative Programming. Basic syntax and sample programs. Autumn 2014. Syntax of terms. Term. Variable. Constant. Structure. Names an individual. Stands for an individual unable to be named when program is written. Names an individual that has parts. likes(john, mary)

frankclark
Télécharger la présentation

Declarative Programming

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. Declarative Programming Basic syntax and sample programs Autumn 2014

  2. Syntax of terms Term Variable Constant Structure Names an individual Stands for an individual unable to be named when program is written Names an individual that has parts likes(john, mary) book(dickens, Z, cricket) f(x) [1, 3, g(a), 7, 9] -(+(15, 17), t) 15 + 17 - t Atom Number X Gross_pay Diagnosis _257 _ alpha17 gross_pay john_smith dyspepsia + =/= ’12Q&A’ 0 1 57 1.618 2.04e-27 -13.6

  3. Symbols used • Uppercase letters A,B,C,...,Z • Lowercase letters a,b,c,...,z • Digits 0,1,2,...,9 • Special symbols +,–,*,/,\,<,>,=,:,.,,,&,_,~,[,],(,),... (there are some that may not be allowed - e.g. %)

  4. Atoms • string of letters, digits and _, starting with a lowercase letter • string of special symbols • string of any symbols, delimited by ‘

  5. Numbers Depend from implementation... Some examples: 0 1 57 1.618 2.04e-27 -13.6

  6. Variables • string of letters, digits and _, starting with an uppercase letter or _ • variable _ is called anonymous variable and has a special semantic meaning

  7. Structures The parents of Spot are Fido and Rover. parents(spot, fido, rover) components (any terms) Functor (an atom) of arity 3. It is possible to depict the term as a tree: parents spot fido rover

  8. Structures Some atoms have built-in operator declarations so they may be written in a syntactically convenient form. The meaning is not affected. This example looks like an arithmetic expression, but might not be. It is just a term. =/= =/=(15+X, (0*a)+(2<<5)) + + << * X 15 0 a 2 5

  9. More about operators • Any atom may be designated an operator. The only purpose is for convenience; the only effect is how the term containing the atom is parsed. Operators are ‘syntactic sugar’. • Operators have three properties: position, precedence and associativity.

  10. Examples of operator properties Position Operator Syntax Normal Syntax Prefix: -2 -(2) Infix: 5+17 +(17,5) Postfix: N! !(N) Associativity: left, right, none. X+Y+Z is parsed as (X+Y)+Z because addition is left-associative. Precedence: an integer. X+Y*Z is parsed as X+(Y*Z) because multiplication has higher precedence.

  11. The last point about structures… Constants are structures of arity 0. badger means the same as badger()

  12. Prolog programs • Programs consist of procedures. • Procedures consist of clauses. • Each clause is a fact or a rule. • Programs are executed by posing queries. An example…

  13. Example Predicate Procedure for elephant Facts elephant(george). elephant(mary). elephant(X) :- grey(X), mammal(X), hasTrunk(X). Clauses Rule

  14. Example ?- elephant(george). yes ?- elephant(jane). no Queries Replies

  15. Clauses: Facts and Rules ‘if’ ‘provided that’ Head:-Body. This is a rule. Head. This is a fact. Full stop at the end.

  16. Body of a (rule) clause contains goals Body Head likes(mary, X) :- human(X), honest(X). Goals

  17. Interpretation of clauses Clauses can be given a declarative reading or a procedural reading. Form of clause: H :- G1, G2, …, Gn. “That H is provable follows from goals G1, G2, …, Gn being provable.” Declarative reading: Procedural reading: “To execute procedure H, the procedures called by goals G1, G2, …, Gn are executed first.”

  18. Sample program 1 likes(john,mary). likes(john,julie). likes(julie,john). likes(peter,jane). likes(jane,icecream). likes(jane,cat).

  19. Sample program 1 likes(john,mary). likes(john,julie). likes(julie,john). likes(peter,jane). likes(jane,icecream). likes(jane,cat). ?-likes(john,mary).

  20. Sample program 1 likes(john,mary). likes(john,julie). likes(julie,john). likes(peter,jane). likes(jane,icecream). likes(jane,cat). ?-likes(john,mary). yes

  21. Sample program 1 likes(john,mary). likes(john,julie). likes(julie,john). likes(peter,jane). likes(jane,icecream). likes(jane,cat). ?-likes(jane,cat). yes

  22. Sample program 1 likes(john,mary). likes(john,julie). likes(julie,john). likes(peter,jane). likes(jane,icecream). likes(jane,cat). ?-likes(julie,cat). no

  23. Sample program 1 likes(john,mary). likes(john,julie). likes(julie,john). likes(peter,jane). likes(jane,icecream). likes(jane,cat). ?-likes(jane,X). X = icecream ? yes

  24. Sample program 1 likes(john,mary). likes(john,julie). likes(julie,john). likes(peter,jane). likes(jane,icecream). likes(jane,cat). ?-likes(jane,X). X = icecream ? ; X = cat ? ; no

  25. Sample program 1 likes(john,mary). likes(john,julie). likes(julie,john). likes(peter,jane). likes(jane,icecream). likes(jane,cat). ?-likes(X,Y),likes(Y,X). X = john, Y = julie ? yes

  26. Sample program 1 likes(john,mary). likes(john,julie). likes(julie,john). likes(peter,jane). likes(jane,icecream). likes(jane,cat). friends(X,Y) :- likes(X,Y),likes(Y,X). ?-friends(X,Y). X = john, Y = julie ? yes

  27. Sample program 2 parent(john,peter). parent(christine,peter). parent(jane,peter). parent(john,mary). parent(christine,mary). parent(jane,mary). parent(mary,frances). parent(mary,george). parent(frances,edward). parent(edward,isabel).

  28. Sample program 2 parent(john,peter). parent(christine,peter). parent(jane,peter). parent(john,mary). parent(christine,mary). parent(jane,mary). parent(mary,frances). parent(mary,george). parent(frances,edward). parent(edward,isabel). ?-parent(jane,peter). yes

  29. Sample program 2 parent(john,peter). parent(christine,peter). parent(jane,peter). parent(john,mary). parent(christine,mary). parent(jane,mary). parent(mary,frances). parent(mary,george). parent(frances,edward). parent(edward,isabel). siblings(X,Y) :- parent(X,Z),parent(Y,Z). ?-siblings(john,X). X = christine ? yes

  30. Sample program 2 parent(john,peter). parent(christine,peter). parent(jane,peter). parent(john,mary). parent(christine,mary). parent(jane,mary). parent(mary,frances). parent(mary,george). parent(frances,edward). parent(edward,isabel). grandparent(X,Y) :- parent(X,Z),parent(Z,Y). ?-grandparent(christine,X). X = frances ? ; X = george ? ; no

  31. Sample program 2 parent(john,peter). parent(christine,peter). parent(jane,peter). parent(john,mary). parent(christine,mary). parent(jane,mary). parent(mary,frances). parent(mary,george). parent(frances,edward). parent(edward,isabel). ancestor(X,Y) :- parent(X,Y). ancestor(X,Y) :- parent(X,Z),ancestor(Z,Y). ?-ancestor(christine,isabel). yes

  32. Sample program 2 parent(john,peter). parent(christine,peter). parent(jane,peter). parent(john,mary). parent(christine,mary). parent(jane,mary). parent(mary,frances). parent(mary,george). parent(frances,edward). parent(edward,isabel). ancestor(X,Y) :- parent(X,Y). ancestor(X,Y) :- ancestor(X,Z),parent(Z,Y). ?-ancestor(christine,isabel). yes

  33. Sample program 2 parent(john,peter). parent(christine,peter). parent(jane,peter). parent(john,mary). parent(christine,mary). parent(jane,mary). parent(mary,frances). parent(mary,george). parent(frances,edward). parent(edward,isabel). ancestor(X,Y) :- ancestor(X,Z),parent(Z,Y). ancestor(X,Y) :- parent(X,Y). ?-ancestor(christine,isabel).

  34. How PROLOG answers queries How to answer a query ?-Q.? • find the first fact P. or rule P:-R1,...,Rn. with lhs P unifiable with Q • if P corresponds to fact, return yes together with the instantiation values of variables from Q • if P corresponds to rule, answer the queries R1,..,Rn (in this order, keeping the instantiated variables from the previous ones) • if all queries R1,..,Rn are successful, return yes • otherwise find next fact or rule unifiable with Q • if there are no more such facts or rules, return no

  35. How PROLOG answers queries • Goals and sub-goals: • A logical sentence to be proved: succeed (satisfy) or fail. • A goal could be a query or the conditions of a rule: the relationship between queries and rules • Rule: “goal:-goal”, the relationship is “true”, but the truth values of condition and conclusion are unknown • Prolog breaks a complex goal into a sequence of sub-goals to evaluate the truth value of a (complex) goal

  36. How PROLOG answers queries • Searching, Matching (unifying) and backtracking • S-M-B is the way in which goals are established • A goal is always broken into ground sub-goals consisting of individual predicates, which are then searched from left to right with due regard for the logical meaning. ?-a,b;c,d. • Searching: systematically (depth-first) searches the knowledge to find a match for a ground sub-goal.

More Related