1 / 343

The Course Logic Programming ID2213

The Course Logic Programming ID2213. Thomas Sjöland sjoland@kth.se SCS, Software and Computer Systems ICT - School of Information and Communication Technology KTH, The Royal Institute of Technology. Outline of lectures. W35: F1: Theory, Elementary Programs, unification

Télécharger la présentation

The Course Logic Programming ID2213

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. The Course Logic Programming ID2213 Thomas Sjöland sjoland@kth.se SCS, Software and Computer Systems ICT - School of Information and Communication Technology KTH, The Royal Institute of Technology

  2. Outline of lectures W35: F1: Theory, Elementary Programs, unification Theory, Model Theory of LP, proof trees and search trees W36: F2: Programming Style, recursion, equality primitives, representation Advanced Recursive Techniques, accumulators, diff-structures, ADT W37: F3: Search Based Programming, cut and negation Concurrency, Search Based Programming, state space, puzzles, games W38: F4: Logic programming and Grammars, parsing with DCG W39: F5: Program Transformation. Higher-order programming. Metaprogramming, Expert Systems W40: F6: Case study: A compiler for a simple stack machine W41: F7: Case study: Support for reasoning about electronic circuits Red1: Project presentation 4 hours W42: Written Examination

  3. F1: Theory and simple programs Sterling and Shapiro ch. 1,2,4,5,6 Nilsson and Maluszynski ch.1,2,3,6

  4. Theory for Logic Programming

  5. Outline Informal introduction to logic programming theory Data in logic programs: Individual constants, term constructors, logical variables, compound terms, trees and lists Equality theory, Unification Logic Programs: Definite (Horn) Clauses Model theory (least Herbrand model, term interpretation) Proof theory and operational semantics of Prolog (SLD-resolution, proof trees) Simple databases Recursive rules

  6. Logic Programming • Using proofs to compute • To each proof you can order a computation • To each computation you can order a proof • Representation of • knowledge and computations • - algorithms • - functions • - relations

  7. Data in Logic Programs Programs express facts about a world of objects Constants Functors Numbers Compounded structures (normally finite) Lists Trees

  8. Objects in Logic Programs Individual constants a b foo 4711 -37 34.5 Functors structure names of trees and graphs same syntax as non-numerical constants Arity (number of arguments): term/4, a/0 Syntax example: term(a,b,-4711,other(b,a))

  9. Logical Variables - Syntax Syntax: begin with a capital letter (or '_') X Y Z Foo_Bar_ Variables can occur wherever constants or structures occur Range over logical objects _ is "anonymous" or "void"

  10. Programs are Theories sets of relations (predicates) over objects The classical form of a definition is as a clausal form where a positive literal P has exactly one occurrence: P or not Q1 or ... or not Qn This can be written as P if Q1 & ... & ... Qn. If all goals Qiare true the clause becomesP.

  11. Program = Definitions + Query The general form of a relation definition is P if (Q11 & ... & Q1n) or ...or (Qm1 & ... & Qmn). 1..m and 1..n are index sets large enough to cover all goal atoms,Qij

  12. Program = Definitions + Query Elementary literals (atoms) true, false, X=Y cannot be redefined (only used in queries and definition bodies) Defined literals (p above)

  13. Definite Clauses: Facts Facts: statements of form P :- true. Also written simply as P. Example: brother(nils,karl). Means that the binary relation brother holds between individual constants nils and karl.

  14. Definite Clauses: Rules Rules: conditional formulae of the form P :- Q1,....,Qn. P is called the head and Q1,...,Qn the body of the clause and P, Q1,...,Qn are atomic formulas (relation symbols). Some of the Qi may be predefined relation symbols (=, <) ":-" is read as "if", "," is read as "and"

  15. Definite Clauses: Rules, example Example of a rule: grandfather(X,Y) :- father(X,Z), father(Z,Y). The binary relation grandfather holds between two individuals represented by variables X and Y if the relation father holds between X and some Z and between that Z and Y.

  16. Clause Syntax Example : p(17). p(X) :- X<8, q(X). p(X) :- q(X), X=s(Y), p(Y). In english the above example could be stated as follows: - The property p holds for the constant 17. - The property p holds for a term denoted by the variable X if X<8 and q holds for X. - The property p holds for X if q holds for X, X equals a term s(Y) and p holds for Y.

  17. Programs are Theories Definitions are collections of facts and rules - sets of relations (predicates) over the objects e.g. (for predicate p/2 using q/2 and r/2) p(foo,bar). p(Foo,Bar) :- q(Foo,Baz), r(Baz,Bar). Functions are special cases of relations (deterministic)

  18. Query, Goal formula to be verified (or falsified) Questions posed to the system are of the form ?- Q1,...,Qn. for example ?- q(Foo,Baz), r(Baz,Bar). If the system succeeds to prove the formula, the values of the variables (the bindings) that are the result are shown, otherwise the proof attempt has failed or possibly loops. Note that more than one solution is possible.

  19. How Prolog works • A user query ?- p(Args). • is proven using resolution • - look for all definition clauses of p • - pick one, save others as alternatives • - match the arguments in Args with the terms in the head of the clause, create necessary variable bindings • - if the matching unification fails, try next alternative • - else prove the goal clauses of the body from left to right • - if all proofs are done, the bindings are presented

  20. Database for family relationships parent(Parent, Child), male(Person) and female(Person) parent(erik, jonas). male(erik).parent(erik, eva). male(jonas).parent(lena, jonas). female(lena). ?- parent(lena, jonas). Yes ?-parent(X,Y).X=erik, Y=jonas; X=erik, Y=eva; X= lena, Y= jonas ?- parent(X, jonas). X=erik; X=lena

  21. Logical variables - semantics Variables can occur wherever constants or structures occur. Range over logical objects. Bound to structures and to other variables. The semantics is "single-assignment" - starts "unbound" - once bound, stays the same in the whole proof

  22. Example cont.: rules father(Dad, Child):- parent(Dad, Child), male(Dad).mother(Mum, Child):- parent(Mum, Child), female(Mum).?- father(X,Y). ?- mother(erik, jonas).X=erik, Y=jonas NoX=erik, Y=eva ?- mother(Erik, jonas).Yes. Why? %sibling(Person1, Person2) :- ...sibling(X,Y) :- parent(Z,X), parent(Z,Y). %cousin(Person1, Person2):- ...cousin(X,Y) :- parent(Z,X), parent(U,Y), sibling(Z,U).

  23. syntactic sugar ';' The symbol ';' can be used to avoid defining auxiliary predicates or to reduce the number of clauses. ';' is read as "or". A clause of the form P :- Q1, (Q2 ; Q3), Q4. is the same as P :- Q1, Q, Q4. Q :- Q2. Q :- Q3.

  24. Equality theory - Substitutions X equals Y iff X is an unbound variable or Y is an unbound variable or X and Y are (bound to) the same constant or X and Y are terms with the same functor and arity e.g. X is term(Z1,..,Zn) and Y is term(U1,...,Un) and for all arguments 1=<i=<n: Zi equals Ui.

  25. Substitutions A substitution is a function Subst: Var -> Term Substitutions can be applied to terms or substitutions and also to formulas We may represent a substitution as a conjunction of simple equalities v=t where a variable v occurs on the left hand side at most once or as a set {v/t | v=t} meaning a function that replaces v with t for each v/t in the set

  26. Unifier A unifier is a substitution s suchthatssºts (applying s tosand totcreates identical terms)

  27. Most general unifier A unifier s is more general than a unifier d iff there exists another unifier w such that s w º d A unifier s is the most general unifier of two terms iff s is more general than any other unifier of the two terms

  28. Most general unifier, example Example : t(X,Y,Z) and t(U,V,W) are unified by {X/a,Y/b,Z/c, U/a,V/b,W/c} consider for instance the mgus in this case {X/U,Y/V,Z/W}and{U/X,V/Y,W/Z}

  29. Unification procedure An algorithm that constructs most general unifiers for two terms in an environment is a unification procedure. Since the most general unifier is unique (modulo renaming of variables), unification can also be understood as a function unify : Subst x Term x Term -> Subst

  30. Theory and simple programs (cont) Operational Semantics, SLD Sterling and Shapiro ch. 1,2,4,5,6 Nilsson and Maluszynski ch.1,2,3,6

  31. Example cont.: structured data Use compound (not atomic) terms for the description of persons. parent(erik, jonas). parent(erik, eva). parent(lena, jonas). male(person(erik,50,180cm)). male(person(jonas,25,_)). father(Dad,Child) :- Dad = person(DadName,_,_), Child=person(ChildName,_,_), parent(DadName, ChildName), male(Dad).?-father(person(_,50,_), person(X,_,_)). X=jonas (second solution: X = eva) NB: how does the unification algorithm work here?

  32. Logical Variables in Programs Variables and parameters are implicitly quantified syntax: variables start with capital letter p(X,Y) :- q(X,Z), r(Z,Y). is understood as forallX,Y:(p(X,Y) <- exists Z:(q(X,Z), r(Z,Y))) Parameters (X,Y) are often confusingly named "global variables" as opposed to "local variables" (Z) but if X is global and Y is local, what is Y, if X=Y occurs in program?

  33. Example cont.: recursive rules ancestor(Ancestor, Descendant) :- parent(Ancestor, Descendant). ancestor(Ancestor, Descendant) :- parent(Ancestor, Person), ancestor(Person, Descendant). parent(ulf, erik). … ?- ancestor(X, Y).

  34. Declarative vs procedural A logic program can be understood in either of two ways: it can be seen as a set of Horn clauses specifying facts about data (a theory). This is the declarative or model-theoreticalreading of a logic program. What? it can be viewed as a program describing a particular execution (how to find a proof). This is the procedural or proof-theoretical reading of a logic program. How?

  35. Modus Ponens • P Q:-P • -------------- • Q

  36. Given a database: p :- q,r. q :- q1, q2. q1. q2. r. Proof methods to prove p: Forward chaining - use modus ponens to accumulate known truths, starting from facts. Backward chaining - prove p by proving q and then proving r etc. (used in prolog) Proof methods with Horn clauses

  37. Model Theory: Herbrand interpretation When reading a program as a specification we need to determine the meaning of the symbols. A term interpretation, or "Herbrand interpretation" is an association of a unique function to each functor occurring in the program and an association of sets of tuples of terms to relations. An interpretation is a model for a program if all statements in the interpretation are true.

  38. Model Theory: Least Herbrand Model The least Herbrand model is the least term interpretation such that it is a model. For definite clauses such a unique model always exists.

  39. Least Herbrand Model computed The model can be inductively built up from the relation symbols and the terms built from constants and terms in the program by constructing a fixpoint. Use the monotone Tp-operator. (N&M p. 29 ch 2.4), ground(P) is the set of all ground instances of clauses in a program P (assume always at least one functor or constant and only finite structures). Tp(I) := {A0 | A0:-A1,...,Am in ground(P) & {A1,...,Am} subset I }. Start from the empty theory and determine the least fixpoint for I=Tp(I) U I. Note that the model does not contain variables.

  40. p :- q,r. q :- q1, q2. q1. q2. r. s. 0: {} 1: {q1,q2,r,s} 2: {q1,q2,r,s,q} 3: {q1,q2,r,s,q,p} 4: {q1,q2,r,s,q,p} Done The fixpoint is the model {q1,q2,r,s,q,p} Constructing a model with Tp

  41. p(X) :- q(X,Y),r(X). q(X,Y) :- q1(X,Y), q2(Y,X). q1(a,b). q2(b,a). r(a). Herbrand universe: {a,b} 0: {} 1: {q1(a,b),q2(b,a),r(a)} 2: {q1(a,b),q2(b,a),r(a),q(a,b)} 3: {q1(a,b),q2(b,a),r(a),q(a,b),p(a)} 4: {q1(a,b),q2(b,a),r(a),q(a,b),p(a)} Done The fixpoint is the model: {q1(a,b),q2(b,a),r(a),q(a,b),p(a)} Constructing a model with Tp

  42. Infinite structures Assuming that the least Herbrand Model defines the intended meaning of the program, unification must preserve the property that infinite (cyclic) terms are not allowed. This requires an occurs-check in the unification algorithm prohibiting for example X=f(X) from generating X=f(f(f(f(f(f(f(........ This is very inefficient so occurs-check is the responsibility of the programmer. In critical cases a special test must be performed after the unification. Note that SICStus Prolog uses rational treesinX=f(X) Theoretically sound unification: unify_with_occurs_check/2

  43. Proof theory:Execution is search for a proof or failure, generating an or-tree restrictions on the variables are shown as bindings of the variables

  44. Search trees and proof trees proof Search tree Proof tree

  45. SLD-resolution rule <- A1,..,Ai-1,Ai,Ai+1,...,Am B0 <- B1,...,Bn ------------------------------------------ <- (A1,...,Ai-1,B1,...,Bn,Ai+1,...,Am)s Where P is a program, A1,...,Amare atomic formulas (goals), B0<- B1,...,Bnis a (renamed) clause in P and s=mgu(Ai,B0)

  46. Goal and clause selection A goal selection function specifies which goal Aiis selected by the SLD-rule. The order in which clauses are chosen is determined with a clause selection rule.

  47. Soundness of SLD-resolution Any query (goal) that is provable with SLD-resolution is a logical consequence of the program.

  48. Completeness of SLD-resolution Any query (goal) that is (true) in the least Herbrand model is provable with SLD-resolution. In the case of an infinite SLD-tree, the selection function has to be fair (as in breadth first search). For finite SLD-trees left-first-with-backtracking as used in Prolog gives a complete method.

  49. Conclusion LP can be used as a uniform language for representing databases, e.g. data structure and queries can be written in a single language LP extends traditional databases by having recursive rules and structured data facilities

  50. F2: Logic Programming Style Sterling and Shapiro ch. 2,6,7,13 (except 2.4, 2.5, 3.6, 6.3, 7.6, 13.3, 13.4) Nilsson and Maluszynski ch.7 (except 7.3)

More Related