210 likes | 365 Views
Formal Models of Computation Part II The Logic Model. Lecture 1 – Programming in Logic. Logic Programming: the vision. A computational problem can be expressed as a set of statements in logic: Describing the domain and its constraints Describing what the solution must be like
E N D
Formal Models of ComputationPart IIThe Logic Model Lecture 1 – Programming in Logic
Logic Programming: the vision A computational problem can be expressed as a set of statements in logic: • Describing the domain and its constraints • Describing what the solution must be like Example: devising a timetable Might use concepts such as: bestT(x) – x is the best timetable h(event,room,time) – event happens in room at time … formal models of computation
Formally The problem/requirements: • A logical statement A, e.g. (8 x bestT(x) ´ …) Æ (8 r 8 t 8 e 8 e1. h(e,r,t) Æ h(e1,r,t) ¾ e=e1) Æ … The solution: • A logical statement B, e.g. bestT(…) We want to know: • A²B ?? formal models of computation
Satisfiability and Consequence in Logic • A formula A is satisfiable , ²A, if there is an interpretation of the formula that is true in the world. Finding such an interpretation involves: • Deciding what each predicate symbol means • Deciding what each function symbol means • Deciding what each constant symbol means • Checking the complex claim of the formula • A ²B (B isa logical consequence ofA)means that B is true in any interpretation that makes A true formal models of computation
Inference Procedures • An inference procedure is a mechanical way of determining logical consequences. A`B: the procedure derivesBfromA • Two desirable properties: • Completeness: If A²B then A`B • Soundness: If A`Bthen A ²B • Given a complete and sound inference procedure, we can use this to calculate logical consequences formal models of computation
Refutation • To test, A²B, we could (using `) try deriving logical consequences from A and look to see if B is there. But: • Not guided by the nature of B • There are infinitely many logical consequences… • A²B can be replaced byAÆ :B², where is the formula that is never true (“false”) • Saying this differently: AÆ :Bis not satisfiable • So now only need refutation soundness/ completeness (where the consequence is ) formal models of computation
Computational approaches to deduction • Resolution (Robinson 1965) • Assumes that statements are expressed in a simpler, more uniform, notation: clauses • Practical approaches mostly use some kind of Resolution. • Resolution and the translation from logic into clauses is sound and refutation complete. formal models of computation
Equivalences in Predicate Calculus • ¾ and ´ can be replaced, e.g. by (a ¾ b) ): a Ç b • :can be moved “inwards” to only modify a predicate directly, e.g. by :(a Ç b) ): a Æ: b • 9can be replaced by using new constant/ function symbols, e.g. by 8 x 9 y p(y) )8 x p(f(x)) • 8can be moved to the “outside” of formulae, e.g. by a Æ8 x p(x) )8 x (a Æ p(x)) • Æcan be distributed over Ç (CNF) e.g. by a Ç (b Æ c) ) (a Ç b) Æ (a Ç c) formal models of computation
Clauses Once the above translations have taken place: • We can miss out explicit 8 signs (because all variables are universally quantified at the outside) • The logical formula is now of the form: (l11Ç l12Ç l1k) Æ (l21Ç l2m) Æ Where each literal lij is either positive - a predicate applied to arguments, e.g. n(x,f(y)), or negative - the negation of a predicate, e.g. : n(x,f(y)) A formula (l11Ç l12Ç l1k) is called a clause. It can also be written: (l11; l12; ) :- l13, l14, . where l11 , l12 are the positive literals and l13, l14 are the (unnegated!) negative ones formal models of computation
Example: Translating Logic to Clauses 8 x. n(x) ´ ( x=lowest Ç n(min(x,1)) ) ) 8 x. (: n(x) Ç x=lowest Ç n(min(x,1))) Æ (: (x=lowest Ç n(min(x,1))) Ç n(x)) ) 8 x. (: n(x) Ç x=lowest Ç n(min(x,1))) Æ ((: x=lowest Æ: n(min(x,1))) Ç n(x)) ) 8 x. (: n(x) Ç x=lowest Ç n(min(x,1))) Æ ((: x=lowest Ç n(x)) Æ (: n(min(x,1)) Ç n(x)) ) formal models of computation
8 x. (: n(x) Ç x=lowest Ç n(min(x,1))) Æ ((: x=lowest Ç n(x)) Æ (: n(min(x,1)) Ç n(x)) ) Remove 8.Clauses are: (: n(x) Ç x=lowest Ç n(min(x,1))) (: x=lowest Ç n(x)) (: n(min(x,1)) Ç n(x)) Write these as: x=lowest; n(min(x,1)) :- n(x). n(x) :- x=lowest. n(x) :- n(min(x,1)). formal models of computation
Skolemisation • In producing clauses, removing 9 is the most tricky operation. 9x. centre(x,universe) ) centre(k23,universe) 8x. man(x) ¾9 y. mother(x,y) ) 8 x. man(x) ¾ mother(x,k24(x)) • This affects the true interpretations of a formula. But does not affect whether a formula is satisfiable, so is harmless if we are doing refutation. formal models of computation
Horn Clauses • A clause with at most one positive literal is called a Horn clause (at most one thing left of the “:-”) • It can be shown (using the methods of Part III of this course) that Horn clauses are all we need in order to do computation. • A Horn clause is headed if it has something to the left of the “:-”, otherwise it is headless. In our example: x=lowest; n(min(x,1)) :- n(x). n(x) :- x=lowest. n(x) :- n(min(x,1)). The last two clauses are headed Horn clauses formal models of computation
Computational interpretation of HCs The set of headed clauses with a given predicate on the left can be thought of as defining a procedure to establish whether it holds: n(x) :- x=lowest. (8 x. n(x) ½ x=lowest) n(x) :- n(min(x,1)). (8 x. n(x) ½ n(min(x,1))) uncle(x,y) :- parent(x,z), brother(z,y). (8 x. 8 y. uncle(x,y) ½9 z. parent(x,z) Æ brother(z,y)) A headless clause can be thought of as defining a goal : :- uncle(mary,x). :- n(x), n(min(1,x)). A procedure allows goals to be reduced to simpler subgoals. This is the basis of languages like Prolog. formal models of computation
How to do Logic Programming (version 1) • Encode the problem in logic: A (the program) • Encode the solution in logic: B(the goal) • Translate A into clausal form – gives a set of headed Horn clauses • Translate :B into clausal form – gives a headless Horn clause • Attempt to refute AÆ:B using resolution • Read off the answer (to be described) formal models of computation
SLD resolution • SLD resolution is a refutation complete version of resolution for Horn Clauses • A current goal (headless clause) is repeatedly reduced to a (hopefully simpler) new goal, using a headed clause from the program: :- g1, g2, gn. (selected literal) g2 :- g21, g22, g2m. (selected clause) gives rise to the new goal: :- g1, g21, g22, g2m, gn. • SLD resolution does not tell you which literal to choose or which clause to choose formal models of computation
Example Program: a :- b, c. b :- d. c :- . d :- . Goal: :- a. SLD refutation: :- a. :- b, c. :- d, c. :- c. :- . (empty clause – “false”) formal models of computation
Substitutions and Answers • An SLD refutation is allowed to associate values with variables in clauses (because a 8 variable can be anything): uncle(x,y) :- parent(x,z), brother(z,y). parent(mary,jean). brother(jean,tom). :- uncle(mary,w). x=mary, y=w :- parent(mary,z), brother(z,w). z=jean :- brother(jean,w). w=tom :- . The assumed values for variables are kept in an increasing substitution, which can be used to read off the answer. formal models of computation
Unification • To deal with variable values, the matching criterion for SLD resolution needs to be more general than identity. A selected literal is required to unify with the head of a clause, and this produces a new substitution. • Unification – computes the minimal assignments to variables that will make two terms identical, e.g. uncle(mary,john) uncle(mary,x) YES, x=john uncle(x,john) uncle(motherof(z),john) YES, x=motherof(z) uncle(motherof(z),john) uncle(motherof(mary),john) YES, z=mary uncle(mary,john) uncle(john,mary) NO formal models of computation
Control • SLD resolution does not specify: • Which literal in the goal to resolve away next • Which clause whose head unifies with the literal should be used • Various selection functions can be defined, but there is no “perfect” approach • Bad choices can lead to inefficiency/ infinite loops. • Finding the best solution path is a search problem. • In practice, a programmer has some idea about how the search should be controlled: “Algorithm = logic + control” • Logic programming languages provide extra control mechanisms separate from the logic. formal models of computation
How to do Logic Programming (version 2) • Encode the problem in logic: A (the program) • Encode the solution in logic: B(the goal) • Translate A into clausal form – gives a set of headed Horn clauses • Translate :B into clausal form – gives a headless Horn clause • Use SLD resolution (with your favourite control/ selection functions) to reduce AÆ:B to • Read off the answer from the substitution built • In practice, most people write the clauses for A and B directly. formal models of computation