1 Declarative Programming Motivation Warm Fuzzies What is Logic?... Logic Programming? Mechanics of...

31
1 Declarative Programming Motivation Warm Fuzzies What is Logic? ... Logic Programming? Mechanics of Prolog Terms, Substitution, Unification, Horn Clauses, Proof process Example: List Processing Theoretical Foundations Semantics Logic / Theorem Proving … Resolution Other Issues Search Strategies Declarative/Procedural, ... “Impure'' Operators” --- NOT, ! Utilities ? Constraint Programming ? Bayesian Belief Nets

Transcript of 1 Declarative Programming Motivation Warm Fuzzies What is Logic?... Logic Programming? Mechanics of...

Page 1: 1 Declarative Programming Motivation Warm Fuzzies What is Logic?... Logic Programming? Mechanics of Prolog Terms, Substitution, Unification, Horn Clauses,

1

Declarative Programming Motivation

Warm Fuzzies What is Logic? ... Logic Programming?

Mechanics of Prolog Terms, Substitution, Unification, Horn Clauses, Proof

process Example: List Processing Theoretical Foundations

Semantics Logic / Theorem Proving … Resolution

Other Issues Search Strategies Declarative/Procedural, ... “Impure'' Operators” --- NOT, ! Utilities

? Constraint Programming ? Bayesian Belief Nets

Page 2: 1 Declarative Programming Motivation Warm Fuzzies What is Logic?... Logic Programming? Mechanics of Prolog Terms, Substitution, Unification, Horn Clauses,

2

Entailment

Spse we assert 1, 2, …on(a,b), on(a,b) ⇒ above(a,b), …

Then. .. you should believe 1, 2, …

What else should we believe?(Should we believe some β – eg, “above(a,b)”?)

Entailment addresses this:You should believe all (and only) entailments. 

Later: Present ALGORITHM forcomputing these entailments

a

b

c

Page 3: 1 Declarative Programming Motivation Warm Fuzzies What is Logic?... Logic Programming? Mechanics of Prolog Terms, Substitution, Unification, Horn Clauses,

3

Interpretation

Vocabulary: (literals)onab: if a is immediately on b

onbc: if b is immediately on c aboveab: a is (somewhere) above b

aboveac: a is (somewhere) above c

 An “interpretation” (aka “world”)assigns

True xor False to each literal

 n literals ⇒ 2 n possible worlds, I

b

a

c

onab onac

aboveab

aboveac

I1 + + + + …

I2 + + + 0 …

I3 + + 0 + …

I4 + + 0 0 …

I5 + 0 + + …

I6 + 0 + 0 …

I7 + 0 0 + …

I8 + 0 0 0 …

I9 0 + + + …

I10 0 + + 0 …

I11 0 + 0 + …

I12 0 + 0 0 …

I13 0 0 + + …

I14 0 0 + 0 …

I15 0 0 0 + …

I16 0 0 0 0 …

Page 4: 1 Declarative Programming Motivation Warm Fuzzies What is Logic?... Logic Programming? Mechanics of Prolog Terms, Substitution, Unification, Horn Clauses,

4

Models

Write Ii

if is True (+) in world Ii

So I1 onbc

⊭I6 onac 

onab onac

aboveab

aboveac

I1 + + + + …

I2 + + + 0 …

I3 + + 0 + …

I4 + + 0 0 …

I5 + 0 + + …

I6 + 0 + 0 …

I7 + 0 0 + …

I8 + 0 0 0 …

I9 0 + + + …

I10 0 + + 0 …

I11 0 + 0 + …

I12 0 + 0 0 …

I13 0 0 + + …

I14 0 0 + 0 …

I15 0 0 0 + …

I16 0 0 0 0 …

Let I () = { I ∣⊨I }

Page 5: 1 Declarative Programming Motivation Warm Fuzzies What is Logic?... Logic Programming? Mechanics of Prolog Terms, Substitution, Unification, Horn Clauses,

5

Models

Write Ii

if is True (+) in world Ii

So I1 onbc

⊭I6 onbc 

onab onac

aboveab

aboveac

I1 + + + + …

I2 + + + 0 …

I3 + + 0 + …

I4 + + 0 0 …

I5 + 0 + + …

I6 + 0 + 0 …

I7 + 0 0 + …

I8 + 0 0 0 …

I9 0 + + + …

I10 0 + + 0 …

I11 0 + 0 + …

I12 0 + 0 0 …

I13 0 0 + + …

I14 0 0 + 0 …

I15 0 0 0 + …

I16 0 0 0 0 …

Let I () = { I ∣⊨I }

I (onab)

Page 6: 1 Declarative Programming Motivation Warm Fuzzies What is Logic?... Logic Programming? Mechanics of Prolog Terms, Substitution, Unification, Horn Clauses,

6

Models

Before seeing anything, REAL-WORLD

I= {I1,…,I16}

onab onac

aboveab

aboveac

I1 + + + + …

I2 + + + 0 …

I3 + + 0 + …

I4 + + 0 0 …

I5 + 0 + + …

I6 + 0 + 0 …

I7 + 0 0 + …

I8 + 0 0 0 …

I9 0 + + + …

I10 0 + + 0 …

I11 0 + 0 + …

I12 0 + 0 0 …

I13 0 0 + + …

I14 0 0 + 0 …

I15 0 0 0 + …

I16 0 0 0 0 …

Page 7: 1 Declarative Programming Motivation Warm Fuzzies What is Logic?... Logic Programming? Mechanics of Prolog Terms, Substitution, Unification, Horn Clauses,

7

Models

Before seeing anything, REAL-WORLD

I= {I1,…,I16} Asserting means

REAL-WORLD I() 

Eg: Asserting onab means

REAL-WORLD ∈ I(onab) = {I1,…,I8}

Each assertion ELIMINATES some possible worlds

onab onac

aboveab

aboveac

I1 + + + + …

I2 + + + 0 …

I3 + + 0 + …

I4 + + 0 0 …

I5 + 0 + + …

I6 + 0 + 0 …

I7 + 0 0 + …

I8 + 0 0 0 …II99 0 + + + …II1010 0 + + 0 …II1111 0 + 0 + …II1212 0 + 0 0 …II1313 0 0 + + …II1414 0 0 + 0 …II1515 0 0 0 + …II1616 0 0 0 0 …

Page 8: 1 Declarative Programming Motivation Warm Fuzzies What is Logic?... Logic Programming? Mechanics of Prolog Terms, Substitution, Unification, Horn Clauses,

8

I (onab)

onab onac

aboveab

aboveac

I1 + + + + …

I2 + + + 0 …

I3 + + 0 + …

I4 + + 0 0 …

I5 + 0 + + …

I6 + 0 + 0 …

I7 + 0 0 + …

I8 + 0 0 0 …

I9 0 + + + …

I10 0 + + 0 …

I11 0 + 0 + …

I12 0 + 0 0 …

I13 0 0 + + …

I14 0 0 + 0 …

I15 0 0 0 + …

I16 0 0 0 0 …

Page 9: 1 Declarative Programming Motivation Warm Fuzzies What is Logic?... Logic Programming? Mechanics of Prolog Terms, Substitution, Unification, Horn Clauses,

9

onab onac

aboveab

aboveac

I1 + + + + …

I2 + + + 0 …

I3 + + 0 + …

I4 + + 0 0 …

I5 + 0 + + …

I6 + 0 + 0 …

I7 + 0 0 + …

I8 + 0 0 0 …

I9 0 + + + …

I10 0 + + 0 …

I11 0 + 0 + …

I12 0 + 0 0 …

I13 0 0 + + …

I14 0 0 + 0 …

I15 0 0 0 + …

I16 0 0 0 0 …

I (¬onab)

Page 10: 1 Declarative Programming Motivation Warm Fuzzies What is Logic?... Logic Programming? Mechanics of Prolog Terms, Substitution, Unification, Horn Clauses,

10

onab onac

aboveab

aboveac

I1 + + + + …

I2 + + + 0 …

I3 + + 0 + …

I4 + + 0 0 …

I5 + 0 + + …

I6 + 0 + 0 …

I7 + 0 0 + …

I8 + 0 0 0 …

I9 0 + + + …

I10 0 + + 0 …

I11 0 + 0 + …

I12 0 + 0 0 …

I13 0 0 + + …

I14 0 0 + 0 …

I15 0 0 0 + …

I16 0 0 0 0 …

I (onac)

I (onab)

Page 11: 1 Declarative Programming Motivation Warm Fuzzies What is Logic?... Logic Programming? Mechanics of Prolog Terms, Substitution, Unification, Horn Clauses,

11

onab onbc

aboveab

aboveac

I1 + + + + …

I2 + + + 0 …

I3 + + 0 + …

I4 + + 0 0 …

I5 + 0 + + …

I6 + 0 + 0 …

I7 + 0 0 + …

I8 + 0 0 0 …

I9 0 + + + …

I10 0 + + 0 …

I11 0 + 0 + …

I12 0 + 0 0 …

I13 0 0 + + …

I14 0 0 + 0 …

I15 0 0 0 + …

I16 0 0 0 0 …

I (onac & onab)

Page 12: 1 Declarative Programming Motivation Warm Fuzzies What is Logic?... Logic Programming? Mechanics of Prolog Terms, Substitution, Unification, Horn Clauses,

12

onab onbc

aboveab

aboveac

I1 + + + + …

I2 + + + 0 …

I3 + + 0 + …

I4 + + 0 0 …

I5 + 0 + + …

I6 + 0 + 0 …

I7 + 0 0 + …

I8 + 0 0 0 …

I9 0 + + + …

I10 0 + + 0 …

I11 0 + 0 + …

I12 0 + 0 0 …

I13 0 0 + + …

I14 0 0 + 0 …

I15 0 0 0 + …

I16 0 0 0 0 …

I (¬onac v

aboveab)

Page 13: 1 Declarative Programming Motivation Warm Fuzzies What is Logic?... Logic Programming? Mechanics of Prolog Terms, Substitution, Unification, Horn Clauses,

13

Boolean Combinations

I(χ) = {I ∣⊨ I χ} I (onab) = {I1,…,I8}

I (¬onab) = {I9,…,I16}

I (onab on⋀ ac) = {I1,…,I8}∩ {I1,…,I4,I9,…,I12} = {I1,…,I4}

I (¬onab above⋁ ab) = {I9,…,I16} ∪ {I1,I2,I5,I6,I9,I10,I13,I14} = {I1,I2,I5,I6,I9,…,I16}

 

Note: I( ¬ ) = I – I()

I( ⋁ ξ) = I () ∪ I (ξ)

I( ⋀ ξ) = I () ∩ I(ξ)

onab onbc

aboveab

aboveac

I1 + + + + …

I2 + + + 0 …

I3 + + 0 + …

I4 + + 0 0 …

I5 + 0 + + …

I6 + 0 + 0 …

I7 + 0 0 + …

I8 + 0 0 0 …

I9 0 + + + …

I10 0 + + 0 …

I11 0 + 0 + …

I12 0 + 0 0 …

I13 0 0 + + …

I14 0 0 + 0 …

I15 0 0 0 + …

I16 0 0 0 0 …

Page 14: 1 Declarative Programming Motivation Warm Fuzzies What is Logic?... Logic Programming? Mechanics of Prolog Terms, Substitution, Unification, Horn Clauses,

14

What else to believe?

Assert ¬onab above⋁ ab

⇒ REAL-WORLD I (¬onab above⋁ ab)

= {I1, I2, I5, I6, I9, …, I16 }

onab onbc

aboveab

aboveac

I1 + + + + …

I2 + + + 0 …

I3 + + 0 + …

I4 + + 0 0 …

I5 + 0 + + …

I6 + 0 + 0 …

I7 + 0 0 + …

I8 + 0 0 0 …

I9 0 + + + …

I10 0 + + 0 …

I11 0 + 0 + …

I12 0 + 0 0 …

I13 0 0 + + …

I14 0 0 + 0 …

I15 0 0 0 + …

I16 0 0 0 0 …

Assert onab

REAL-WORLD

I (¬onab above⋁ ab) ⋂ I(onab)

= {I1, I2, I5,I6}

Page 15: 1 Declarative Programming Motivation Warm Fuzzies What is Logic?... Logic Programming? Mechanics of Prolog Terms, Substitution, Unification, Horn Clauses,

15

What else to believe?

onab onbc

aboveab

aboveac

I1 + + + + …

I2 + + + 0 …

I3 + + 0 + …

I4 + + 0 0 …

I5 + 0 + + …

I6 + 0 + 0 …

I7 + 0 0 + …

I8 + 0 0 0 …

I9 0 + + + …

I10 0 + + 0 …

I11 0 + 0 + …

I12 0 + 0 0 …

I13 0 0 + + …

I14 0 0 + 0 …

I15 0 0 0 + …

I16 0 0 0 0 …

REAL-WORLD

I (¬onab above⋁ ab) ⋂ I(onab) = {I1, I2, I5,I6}

In each such world,

aboveab also holds!

I ( [¬onab above⋁ ab] & onab) I(aboveab )

⇒ We should believe aboveab !

Page 16: 1 Declarative Programming Motivation Warm Fuzzies What is Logic?... Logic Programming? Mechanics of Prolog Terms, Substitution, Unification, Horn Clauses,

16

Entailment Given set of assertions = {1, … }

set of (remaining) possible worlds = I ()

Spse holds in each world I I ( )

I ( ) I ( )

Then you should believe … as is true, in every “still possible” world !

Write this “ entails ” “ is entailed by ”

Page 17: 1 Declarative Programming Motivation Warm Fuzzies What is Logic?... Logic Programming? Mechanics of Prolog Terms, Substitution, Unification, Horn Clauses,

17

Answering Queries1. Semantical Approach

Entailment specifies what we should believe.

Given n literals, write n 2n table.Let W = all 2n rowsFor each assertion i

let W := W ⋂ I (i) (ie, eliminate every row that does not satisfy i)Check column.If ``+'' in each row (of W) then Answer “YES: {i} ''

else Answer “IDK: {i} ⊭ ''

Problem: HUGE table!( in predicate calculus)

To decide

{i}

Page 18: 1 Declarative Programming Motivation Warm Fuzzies What is Logic?... Logic Programming? Mechanics of Prolog Terms, Substitution, Unification, Horn Clauses,

18

Answering Queries2. Syntactic Approach

Proof Process (aka ``derivation'', ``deduction'') is mechanic process... for deciding whether conclusion follows from premises

First consider “forward chaining”: KB ↝ KB' Implemented by ...

Apply sequence of individual (sound) Inference Rules to initial set of premises,

to find new ones

Sound preserves truthIf believe ``antecedent'', must believe conclusion

Inference Rules HornClause

Page 19: 1 Declarative Programming Motivation Warm Fuzzies What is Logic?... Logic Programming? Mechanics of Prolog Terms, Substitution, Unification, Horn Clauses,

19

Derivation Process

- -- -man(s) --- --- --man(X) mortal(X) - - -- - - - -- -

KB0 r1

- -- -man(s) --- --- --man(X) mortal(X) - - -- - mortal(s)- - -- -

KB1

r2

- -- -man(s) --- --- --man(X)

mortal(X) - - -- - mortal(s)- - -- -cat(p)

KB2

r3

r9

- -- -man(s) --- --- --man(X)

mortal(X) - - -- - mortal(s)- - -- -cat(p)

KBk

Page 20: 1 Declarative Programming Motivation Warm Fuzzies What is Logic?... Logic Programming? Mechanics of Prolog Terms, Substitution, Unification, Horn Clauses,

20

New Facts from Old:A sound Inference rule

If “PQ”“P”

KB KB

Then can add

“Q” to KB

Called “Modus Ponens”

Written: [MP] P QP

Q

Page 21: 1 Declarative Programming Motivation Warm Fuzzies What is Logic?... Logic Programming? Mechanics of Prolog Terms, Substitution, Unification, Horn Clauses,

21

Sound Inference Rules

[MP] P Q P Q

[ D] P Q¬PQ

[E] x. (x) (A) for any

A

[MT] P Q¬Q¬P

[&I] PQP Q

[&E] P QP

[RC] P QQ RP R

[ I] PP Q

[MG] P Q¬P QQ

[ ] P¬ PContradiction

Page 22: 1 Declarative Programming Motivation Warm Fuzzies What is Logic?... Logic Programming? Mechanics of Prolog Terms, Substitution, Unification, Horn Clauses,

22

Sound Rules of Inference

KB0

irj KB1

If irj is sound [aka Truth-Preserving], then

KB0 ⊨ KB1

… I (KB0) I (KB1)

So… RW ∊ KB0 RW ∊ KB1

… if believe KB0 , must believe KB1 !

Inference rule irj maps KB0 into KB1

Page 23: 1 Declarative Programming Motivation Warm Fuzzies What is Logic?... Logic Programming? Mechanics of Prolog Terms, Substitution, Unification, Horn Clauses,

23I (KB1)

Sound Rules of Inference

Possible worlds

I (KB0)

If irj is sound [aka Truth-Preserving], then

KB0 ⊨ KB1

… I (KB0) I (KB1)

So… RW ∊ KB0 RW ∊ KB1

Hence: If believe KB0 , must believe KB1.RW

Page 24: 1 Declarative Programming Motivation Warm Fuzzies What is Logic?... Logic Programming? Mechanics of Prolog Terms, Substitution, Unification, Horn Clauses,

24

Sound Rules of Inference – con’t

Possible worlds

I (KB0)

I (KB1)

• In general, KB1 = KB0 + . So KB1

⊨ KB0

… KB1, KB0 hold in EXACTLY same worlds:

• If each rij is sound, then sequence ri1 … rin is sound.

Page 25: 1 Declarative Programming Motivation Warm Fuzzies What is Logic?... Logic Programming? Mechanics of Prolog Terms, Substitution, Unification, Horn Clauses,

25

Answering Queries

If {rij} j sound, then KB0 ⊧ KBN

• Adding Truths (Forward Chaining) Given KB0 produce KBN s.t.

ri1KB0 KB1 KBN… riN

• Answering Questions (Backward Chaining)

Given KB0 , ; determine if KB ⊧ Requires sound <ri j >j s.t.ri1KB0 KB

1

KBN… riN

and KBN

Page 26: 1 Declarative Programming Motivation Warm Fuzzies What is Logic?... Logic Programming? Mechanics of Prolog Terms, Substitution, Unification, Horn Clauses,

26

Q: How to reason? Given KB, q, determine if KB ⊨ q ?

A: Select Inference Rule IR Select Facts(s) {Fi} from KB Apply rule IR to Facts {Fi} to get new Fact Add to KB Repeat until find = q

Issues: 1. Lots of Inference Rules… Which one to use, when?

2. Is overall system “complete”? If answer, guaranteed to find it?

How to Reason?

Page 27: 1 Declarative Programming Motivation Warm Fuzzies What is Logic?... Logic Programming? Mechanics of Prolog Terms, Substitution, Unification, Horn Clauses,

27

?

Properties of Derivation Process

• ⊢ is SOUND

⊢ ⊨ Produces only “true” results

• ⊢ is COMPLETE

⊢ ⊨ Produces all “true” results

• ⊢ is DECIDABLE

⊢? returns Y or N in finite time

Page 28: 1 Declarative Programming Motivation Warm Fuzzies What is Logic?... Logic Programming? Mechanics of Prolog Terms, Substitution, Unification, Horn Clauses,

28

Degenerate ⊦

For any , ⊂ WFFs:

⊬N ⊢P Notice

⊢N is SOUND (returns everything logically entailed)

⊢P is COMPLETE (returns everything logically entailed)

⊢N , ⊢P is DECIDABLE (answer every question)

f’sure

Just say “No”

Page 29: 1 Declarative Programming Motivation Warm Fuzzies What is Logic?... Logic Programming? Mechanics of Prolog Terms, Substitution, Unification, Horn Clauses,

29

• For any sufficiently complicated domain as complex as arithmetic

• No ⊢ can be

SOUND, COMPLETE, DECIDABLE!!

• Reduction to halting Problem.

Fundamental Limitation

Not Predicate Calculus’s fault:

Reasoning is inherently undecidable,

no manner what formalism used

Page 30: 1 Declarative Programming Motivation Warm Fuzzies What is Logic?... Logic Programming? Mechanics of Prolog Terms, Substitution, Unification, Horn Clauses,

30

• Deals only with WORST-case! “Typical” case can be better.

TradeOffs (to increase efficiency): ? Sacrifice SOUNDness? No – too severe.

? Sacrifice COMPLETEness? Reasonable ... Specific proposals: - Use only (incomplete set of) Inference Rules - Use complete set of Inference Rules, but limit depth (…stop expanding nodes…) ? Sacrifice EXPRESSIVEness? [EXPRESSIVEness what can be distinguished.] Common approach! (After all, Logic’s distinctions caused problems!)

Disallow “Ⅴ” “¬” “” …

Responses

Page 31: 1 Declarative Programming Motivation Warm Fuzzies What is Logic?... Logic Programming? Mechanics of Prolog Terms, Substitution, Unification, Horn Clauses,

31

o DataBase Systems

≈Sound, Complete, Limited Expressiveness

• Prolog ≈Sound, complete, Limited Expressiveness

o General Theorem Provers ≈Sound, Complete, Complete Expressiveness

o Production System (Emycin, OPS) ≈Sound, ≈ Complete, Limited Expressivenesso Frame systems ?Sound?, ?Complete?, Limited Expressiveness

o Description Languages Sound, Complete, Limited Expressiveness

o Truth Maintenance

Implemented Systems