Computer Science and Software Engineering...

30
Computer Science and Software Engineering University of Wisconsin - Platteville Computer Science and Software Engineering University of Wisconsin - Platteville 6. Inference and resolution CS 3030 Lecture Notes Yan Shi UW-Platteville Read: Textbook Chapter 8 Part of the slides are from textbook slides

Transcript of Computer Science and Software Engineering...

Computer Science and Software Engineering

University of Wisconsin - Platteville

Computer Science and Software Engineering

University of Wisconsin - Platteville

6. Inference and

resolution

CS 3030 Lecture Notes

Yan Shi

UW-Platteville

Read: Textbook Chapter 8Part of the slides are from textbook slides

Overview

Issue so far:— How to reason with FOPL?

— How to automate the reasoning process?

Solution: resolution— Normal forms

— Resolution rule

— Proof by refutation

— Unification

— Horn clauses

Normal Forms

Conjunctive Normal Form (CNF):A1 A2 … An

Where each clause Ai is of formB1 B2 … Bn

Where each Bi is a literal (basic symbol of propositional logic)— A set of or phrases anded together!— Example: A ( B C) ( A B D)

Disjunctive Normal Form (DNF):— A set of and phrases ored together

Converting to CNF

Any wff can be converted to CNF by applying the following equivalences in the exact order:

(1) A ↔ B (A → B) Λ (B → A)(2) A → B ¬A v B(3) ¬(A Λ B) ¬A v ¬B(4) ¬(A v B) ¬A Λ ¬B(5) ¬¬A A (6) A v (B Λ C) (A v B) Λ (A v C)

Importantly, this can be converted into an algorithm – this will be useful when we come to automating resolution.

Example:— (A B) C— A (B C)

5

Clauses

Having converted a wff to CNF, it is usual to write it as a set of clauses.

E.g.:

(A → B) → C

In CNF is:

(A V C) Λ (¬B V C)

In clause form, we write:

{(A, C), (¬B, C)}

6

The Resolution Rule

The resolution rule is written as follows:

A v B ¬ B v C

A v C

This tells us that if we have two clauses that have a literal and its negation, we can combine them by removing that literal.

— E.g.: {(A, C), (¬A, D)} ╞ {C, D}

Resolvent: logical consequence of two clauses.

More examples:— { (B,C,D,E), D, (¬D, F)}

7

Resolution Refutation

Let us resolve: {(¬A, B), (¬A, ¬B, C), A, ¬C}

We begin by resolving the first clause with the second clause, thus eliminating B and ¬B:

{(¬A, C), A, ¬ C}

{C, ¬C}

Now we can resolve both remaining literals, which gives falsum:

^

If we reach falsum, we have proved that our initial set of clauses were inconsistent.

This is written:

{(¬A, B), (¬A, ¬B, C), A, ¬C} ╞ ^

8

Proof by Refutation

If we want to prove that a logical argument is valid, we negate its conclusion, convert it to clause form, and then try to derive falsum using resolution.

If we derive falsum, then our clauses were inconsistent, meaning the original argument was valid, since we negated its conclusion.

9

Proof by Refutation - Example

Our argument is: (A Λ ¬B) → CA Λ ¬ BC

Negate the conclusion and convert to clauses:{(¬A, B, C), A, ¬B, ¬C}

Now resolve:{(B, C), ¬B, ¬C}{C, ¬C}^

We have reached falsum, so our original argument was valid.— Note: it is not necessary to have all clauses resolved, see textbook

example Ch 8.10.

10

Refutation Proofs in Tree Form

It is often sensible to represent a refutation proof in tree form:

In this case, the

proof has failed, as we

are left with E instead of falsum.

11

Example: Graph Coloring

Resolution refutation can be used to determine if a solution exists for a particular combinatorial problem.

For example, for graph coloring, we represent the assignment of colors to the nodes and the constraints regarding edges as propositions, and attempt to prove that the complete set of clauses is consistent.

This does not tell us how to color the graph, simply that it is possible.

12

Example: Graph Coloring (2)

Available colors are r (red), g (green) and b (blue).

Ar means that node A has been coloured red.

Each node must have exactly one color:

Ar v Ag v Ab¬Ar v ¬Ag ( Ar → ¬Ag)

¬Ag v ¬Ab¬Ab v ¬Ar

If (A,B) is an edge, then:

¬Ar v ¬Br¬Ab v ¬Bb¬Ag v ¬Bg

Now we construct the complete set of clauses for our graph, and try to see if they are consistent, using resolution.

McDonald’s Addicts

Adam, Brian and Chris go to McDonald’s for lunch every day. They eat only beef burgers or chicken sandwiches.

13

McDonald’s Addicts

1) If Adam buys beef burgers, Brian will definitely buy chicken sandwiches.

2) Either Adam or Chris will have beef burgers.

3) Between Brian and Chris, at least one of them does not eat chicken sandwiches.

4/15/2015 10:50:46 AM 14

Who had a beef burger yesterday and who had a chicken sandwich today?

15

Normal Forms in Predicate Calculus

A FOPC expression can be put in prenex normal form by converting it to CNF, with the quantifiers moved to the front.

For example, the wff:

(x)(A(x) → B(x)) → (y)(A(y) Λ B(y))

Converts to prenex normal form as:

(x)(y)(((A(x) v A(y)) Λ (¬B(x) v A(y)) Λ (A(x) v B(y)) Λ (¬B(x) v B(y))))

HOW???

Rules to convert to

prenex normal form

In addition to rules 1-6, apply:

A further set of rules 17-20 can be generated by replacing with in rules 13-16

only when B doesn’t contain x

17

Skolemization

Before resolution can be applied, must be removed, using skolemization.

Variables that are existentially quantified are replaced by a constant:

(x) P(x)

is converted to:

P(c)

c is a constant that must not already exist in the expression.

18

Skolem functions

If the existentially quantified variable is within the scope of a universally quantified variable, it must be replaced by a skolem function, which is a function of the universally quantified variable.

Sounds complicated, but is actually simple:

(x)(y)(P(x,y))

Is Skolemized to give:

(x)(P(x,f(x))

After skolemization, is dropped, and the expression converted to clauses in the usual manner.

Example:

— (x)(y)(z)(P(x)Q(y,z))

— (w)(x)(y)(z)(P(x) Q(w,y,z))

19

Unification (1)

To resolve clauses we often need to make substitutions. For example:

{(P(w,x)), (¬P(y,z))} To resolve, we need to substitute w with y, and x with z, giving:

{(P(y,z)), (¬P(y,z))} Now these resolve to give falsum.

A substitution that enables us to resolve a set of clauses is called a unifier.

We write the unifier we used above as: new/old{y/w, z/x}

Unification: given two terms a and b, find a substitution σ such that applying σ to both a and b gives the same result: aσ = bσ

20

Unification (2)

Given multiple substitutions, we must define how to combine them:— "{x/y, A/z} {B/x}" means apply "{x/y,A/z}" then "{B/x}"— combination is "{B/y,A/z,B/x}"

A unifier (u) is a most general unifier (mgu) if any other unifier can be formed by the composition of u with some other unifier.— example: F(x,y) and F(u,v)— Unifiers: {A/x, A/y, A/u, A/v}, {u/x, y/v}, {w/x, z/y, w/u, z/v},…

Unification algorithm (ch 8.6.5): an algorithm can be generated which obtains a most general unifier for any set of clauses.

Unification (3)

What is against the substitution rule?— A constant cannot be replaced by a variable: {x/C} wrong!!!— A variable x cannot be replaced by a term that contains x:

{f(x)/x} wrong!!!

In general, mgu(a, b) is {b/a} if a is a variable not occurring in b

Example:— Married(Alice, x) , Married(Alice, y): {y/x}— Married(Alice, x) , Married (Alice, Bob): {Bob/x}— Married(x, y) , Married(sister(x), y) : ???— Married(x, brother(y)), Married(Alice, father(Chris)): ???— Parents(x, father(x), mother(x)), Parents(Bill, father(Bill), y) :

{Bill/x, mother(Bill)/y}

22

The Resolution Algorithm

1 First, negate the conclusion and add it to the list of assumptions.

2 Now convert the assumptions into Prenex Normal Form

3 Next, skolemize the resulting expression

4 Now convert the expression into a set of clauses

5 Now resolve the clauses using suitable unifiers.

This algorithm means we can write programs that automatically prove theorems using resolution.

Example

Consider the following axioms: — All dogs bark at night. — Anyone who has any cats will not have any mice. — Light sleepers do not have anything which barks at night. — John has either a cat or a dog or both.

Is it true that if John is a light sleeper, then John does not have any mice?

Note: Use predicate only when necessary. In this example, do we need P(x) to indicate x is a person?

Remember: — and are often used together; — and are often used together.

24

Horn Clauses in PROLOG (1)

PROLOG uses resolution.

A Horn clause has at most one positive literal:

A v ¬B v ¬C v ¬D v ¬E …

This can also be written as an implication:

B Λ C Λ D Λ E → A

In PROLOG, this is written:

A :- B, C, D, E

Conclusion on the left, Premises on the right.

25

Horn Clauses in PROLOG (2)

Horn clauses can express rules:

A :- B, C, D

Or facts:

A :-

Or goals:

:- B, C, D, E

If a set of clauses is valid, PROLOG will definitely prove it using resolution and depth first search.

Convert FOPL to Prolog

Syntax:— AB B:-A— Λ ,

— constants: lower case— variables: upper case— Sentences end with a period.

Each fact or rule is a sentence

— comments: % to end of line

Semantics:— Variables appearing on left-hand-side of rules are implicitly

universally quantified. — Variables appearing on the right-hand-side are implicitly

existentially quantified — Prolog constructs a database of facts and rules.

Prolog Example: Family

% facts:

father(bill, jake). % read bill is father of jake

father(bill, shelly).

father(george, bill).

mother(mary, jake).

mother(mary, shelly).

mother(peg, mary).

% rules:

parent(X,Y) :- mother(X,Y).

parent(X, Y) :- father(X, Y).

grandparent(X,Y) :- parent(X,Z), parent(Z,Y).

sibling(X,Y) :- parent(Z,X), parent(Z,Y).

Prolog Example: Hanoi Tower

move(1,X,Y,_) :-

write('Move top disk from '),

write(X),

write(' to '),

write(Y),

nl.

move(N,X,Y,Z) :-

N>1,

M is N-1,

move(M,X,Z,Y),

move(1,X,Y,_),

move(M,Z,Y,X).

% write to the screen% _ is a anonymous variable

% comparison operators: <, >, =<, >=. “=“ has other meaning!

% use “is” for assignment

Using Prolog

On IO, type prolog To load a file: ?- consult(‘family.pl’)

To ask questions: ?- grandparent(peg,jake).

— Press“;” to see the next solution.— Press”a” to see all solutions.— Press RETURN to stop.

To exit: ?- halt. (or ctrl-d)

Resources to learn prolog:— http://www.learnprolognow.org/— Learning prolog via examples

http://kti.mff.cuni.cz/~bartak/prolog.old/learning.html— GNU prolog manual www.gprolog.org/manual/html_node/

Summary

CNF is useful to solve many logic problems— Key is to translate natural language into logic language,

convert to CNFs and then resolve!

The resolution algorithm— Proof by refutation— Prenex normal form— Skolemization— Unification— Resolution rule

Prolog— Horn clauses— Facts and rules— How to use prolog.