21
CS4026 Formal Models of Computation Part II The Logic Model Lecture 1 – Programming in Logic

CS4026 Formal Models of Computation Part II The Logic Model Lecture 1 – Programming in Logic

Embed Size (px)

Citation preview

Page 1: CS4026 Formal Models of Computation Part II The Logic Model Lecture 1 – Programming in Logic

CS4026Formal Models of

ComputationPart II

The Logic ModelLecture 1 – Programming in Logic

Page 2: CS4026 Formal Models of Computation Part II The Logic Model Lecture 1 – Programming in Logic

formal models of computation 2

Logic Programming: the visionA 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 timetableh(event,room,time) – event happens in room at

time…

Page 3: CS4026 Formal Models of Computation Part II The Logic Model Lecture 1 – Programming in Logic

formal models of computation 3

FormallyThe 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 ??

Page 4: CS4026 Formal Models of Computation Part II The Logic Model Lecture 1 – Programming in Logic

formal models of computation 4

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 is a logical consequence of A) means that B is true in any interpretation that makes A true

Page 5: CS4026 Formal Models of Computation Part II The Logic Model Lecture 1 – Programming in Logic

formal models of computation 5

Inference Procedures• An inference procedure is a mechanical way

of determining logical consequences.

A ` B: the procedure derives B from A

• Two desirable properties:

– Completeness: If A ² B then A ` B– Soundness: If A ` B then A ² B

• Given a complete and sound inference procedure, we can use this to calculate logical consequences

Page 6: CS4026 Formal Models of Computation Part II The Logic Model Lecture 1 – Programming in Logic

formal models of computation 6

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 by A Æ : B ² , where is the formula that is never true (“false”)

• Saying this differently: A Æ : B is not satisfiable

• So now only need refutation soundness/ completeness (where the consequence is )

Page 7: CS4026 Formal Models of Computation Part II The Logic Model Lecture 1 – Programming in Logic

formal models of computation 7

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.

Page 8: CS4026 Formal Models of Computation Part II The Logic Model Lecture 1 – Programming in Logic

formal models of computation 8

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

• 9 can be replaced by using new constant/ function symbols,e.g. by 8 x 9 y p(y) ) 8 x p(f(x))

• 8 can 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)

Page 9: CS4026 Formal Models of Computation Part II The Logic Model Lecture 1 – Programming in Logic

formal models of computation 9

ClausesOnce 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

Page 10: CS4026 Formal Models of Computation Part II The Logic Model Lecture 1 – Programming in Logic

formal models of computation 10

Example: Translating Logic to Clauses8 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)) )

Page 11: CS4026 Formal Models of Computation Part II The Logic Model Lecture 1 – Programming in Logic

formal models of computation 11

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)).

Page 12: CS4026 Formal Models of Computation Part II The Logic Model Lecture 1 – Programming in Logic

formal models of computation 12

Skolemisation• In producing clauses, removing 9 is the most

tricky operation.

9 x. centre(x,universe) ) centre(k23,universe)

8 x. 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.

Page 13: CS4026 Formal Models of Computation Part II The Logic Model Lecture 1 – Programming in Logic

formal models of computation 13

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

Page 14: CS4026 Formal Models of Computation Part II The Logic Model Lecture 1 – Programming in Logic

formal models of computation 14

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.

Page 15: CS4026 Formal Models of Computation Part II The Logic Model Lecture 1 – Programming in Logic

formal models of computation 15

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)

Page 16: CS4026 Formal Models of Computation Part II The Logic Model Lecture 1 – Programming in Logic

formal models of computation 16

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

Page 17: CS4026 Formal Models of Computation Part II The Logic Model Lecture 1 – Programming in Logic

formal models of computation 17

ExampleProgram:

a :- b, c. b :- d. c :- . d :- .Goal: :- a.SLD refutation:

:- a.:- b, c.

:- d, c. :- c. :- . (empty clause – “false”)

Page 18: CS4026 Formal Models of Computation Part II The Logic Model Lecture 1 – Programming in Logic

formal models of computation 18

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.

Page 19: CS4026 Formal Models of Computation Part II The Logic Model Lecture 1 – Programming in Logic

formal models of computation 19

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=johnuncle(x,john) uncle(motherof(z),john) YES, x=motherof(z)uncle(motherof(z),john) uncle(motherof(mary),john) YES, z=maryuncle(mary,john) uncle(john,mary) NO

Page 20: CS4026 Formal Models of Computation Part II The Logic Model Lecture 1 – Programming in Logic

formal models of computation 20

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.

Page 21: CS4026 Formal Models of Computation Part II The Logic Model Lecture 1 – Programming in Logic

formal models of computation 21

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.