Logic and Proof: Computer Science Tripos Part IB Michaelmas Term
Logic and Proof: Computer Science Tripos Part IB Michaelmas Term
Contents
1 Introduction 2 Propositional Logic 3 Gentzens Logical Calculi 4 Binary Decision Diagrams 5 First-Order Logic 6 Formal Reasoning in First-Order Logic 7 Davis-Putnam & Propositional Resolution 8 Skolem Functions and Herbrands Theorem 9 Unication 10 Resolution and Prolog 11 Modal Logics 12 Tableaux-Based Methods 1 6 11 16 21 26 32 38 43 49 54 59
Introduction to Logic
Logic concerns statements in some language. Slide 101 The language can be informal (say English) or formal. Some statements are true, others false or meaningless. Logic concerns relationships between statements: consistency, entailment, . . . Logical proofs model human reasoning (supposedly).
Statements
Statements are declarative assertions: Slide 102 Black is the colour of my true loves hair. They are not greetings, questions or commands: What is the colour of my true loves hair? I wish my true love had hair. Get a haircut!
Lawrence C. Paulson
University of Cambridge
Schematic Statements
The meta-variables Slide 103
Z is the colour of Y .
Schematic statements can express general statements, or questions: What things are black?
Lawrence C. Paulson
University of Cambridge
Consistency, or Satisability
A set S of statements is consistent if some interpretation satises all elements of S at the same time. Otherwise S is inconsistent. Slide 105 Examples of inconsistent sets:
A if every interpretation that satises all elements of S, also satises A. We write S | A. = {X part of Y, Y part of Z} |= X part of Z {n = 1, n = 2, . . .} |= n is NOT a positive integer
S |= A if and only if {A} S is inconsistent |= A if and only if A is valid, if and only if {A} is inconsistent.
Lawrence C. Paulson
University of Cambridge
Inference
We want to check A is valid. Checking all interpretations can be effective but what if there are Slide 107 innitely many? Let {A1 , . . . , An }
Lawrence C. Paulson
University of Cambridge
propositional logic is traditional boolean algebra. Slide 109 rst-order logic can say for all and there exists. higher-order logic reasons about sets and functions. modal/temporal logics reason about what must, or may, happen. type theories support constructive mathematics. All have been used to prove correctness of computer systems.
Consider this denition: Slide 110 The least integer not denable using eight words Greater than The number of atoms in the entire Universe Also greater than The least integer not denable using eight words
Lawrence C. Paulson
University of Cambridge
II
P, Q, R, . . . t
Slide 201
f A AB AB AB AB
A and B A or B
if A then B
A if and only if B
A B t t f f t f t f
A f f t t
AB t f f f
AB t t t f
AB t f t t
AB t f f t
Lawrence C. Paulson
University of Cambridge
II
A is valid (a tautology ) if every interpretation satises A. Write | A = S is satisable if some interpretation satises every formula in S.
A B means simply A B.
Slide 204
A A
Lawrence C. Paulson
University of Cambridge
II
Equivalences
AA AB
Slide 205
A BA A (B C) (A B) (A C) f A f
(A B) C A (B C) Af At A A
AB
Slide 206
(A B) (B A) A B
AB
A (A B) (A B)
A A B A B
Lawrence C. Paulson
University of Cambridge
II
A (B C)
Slide 207 4. Simplify:
(A B) (A C) (B A) (C A)
(B C) A
Delete any disjunction containing P and P Delete any disjunction that includes another: for example, in (P Q) P, delete P Q. Replace (P A) (P A) by A
PQQR
Slide 208 1. Elim : 2. Push in: 3. Push in: 4. Simplify: Not a tautology: try P
(P Q) (Q R) (P Q) (Q R) (P Q R) (Q Q R) P Q R t, Q f , R f
Lawrence C. Paulson
University of Cambridge
II
10
((P Q) P) P
1. Elim : Slide 209 2. Push in:
Lawrence C. Paulson
University of Cambridge
III
11
AB B
(A ((D A) A))
Slide 302
(1)
((A (D A)) (A A)) by S A ((D A) A) by K (A (D A)) (A A) by MP, (1), (2) A (D A) by K A A by MP, (3), (4)
(2) (3) (4) (5)
Lawrence C. Paulson
University of Cambridge
III
12
Slide 303
A is deducible from the set S if there is a nite proof of A starting from elements of S. Write S A.
Soundness Theorem. If S
Completeness Theorem. If S
The context of assumptions may vary. Each logical connective is dened independently. Slide 304 The introduction rule for shows how to deduce A B:
A B AB
The elimination rules for shows what to deduce from A B:
AB A
AB B
Lawrence C. Paulson
University of Cambridge
III
13
. . . Am then B1 . . . Bn
, A A, (cut)
Slide 306
, A A, A, B, A B,
(l)
A, , A
(r)
(l)
, A , B , A B
(r)
Lawrence C. Paulson
University of Cambridge
III
14
Slide 307
A, B, A B, , A B, A B,
(l)
, A, B , A B
(r)
(l)
A, , B , A B
(r)
Slide 308
Lawrence C. Paulson
University of Cambridge
III
15
Slide 309
(r)
A Failed Proof
Slide 310
Lawrence C. Paulson
University of Cambridge
IV
16
Q) R
P
Slide 402
Q R 0 0 0 R 1 R 0 1
Q R 0 1
Lawrence C. Paulson
University of Cambridge
IV
17
P Q R Q R Q
Slide 403
No duplicates
No redundant tests
Do not construct full tree! Slide 404 Do not expand , , (exclusive OR) to other connectives. Treat Z as Z
f or Z t.
Recursively convert operands to BDDs. Combine operand BDDs, respecting the ordering and sharing. Delete redundant variable tests.
Lawrence C. Paulson
University of Cambridge
IV
18
= if (P, X, Y) and Z = if (P , X , Y )
If P = P then recursively convert if (P, X X , Y Y ). If P < P then recursively convert if (P, X Z , Y Z ). If P > P then recursively convert if (P , Z X , Z Y ).
t, reduce to negation.
If Z = if (P, X, Y) then recursively convert if (P, X, Y). if Z = t then return f , and if Z = f then return t.
In effect we copy the BDD but swap t and f at the leaves.
Lawrence C. Paulson
University of Cambridge
IV
19
P
Slide 407
P 0 1
Q 1
Canonical Form of P
QQR
P Q
Slide 408
P Q R R 1 0 1 Q
P Q 0 1
Lawrence C. Paulson
University of Cambridge
IV
20
Never build the same BDD twice, but share pointers. Advantages: Slide 409
If X Y , then the addresses of X and Y are equal. Can see if if (P, X, Y) is redundant by checking if X = Y . Can quickly simplify special cases like X X.
Never convert X Y twice, but keep a table of known canonical forms.
Final Observations
The variable ordering is crucial. Consider this formula:
(P1 Q1 ) (Pn Qn )
A good ordering is P1 Slide 410 linear. With P1
EXPONENTIAL .
Many digital circuits have small BDDs: adders, but not multipliers. BDDs can solve problems in hundreds of variables. The general case remains hard (it is NP-complete).
Lawrence C. Paulson
University of Cambridge
21
Slide 501
f(t1 , . . . , tn )
where f is an n-place function symbol and t1 , . . ., tn are terms. We choose the language, adopting any desired function symbols.
Lawrence C. Paulson
University of Cambridge
22
The prover ACL2 uses this logic and has been used in major hardware proofs.
Lawrence C. Paulson
University of Cambridge
23
x A for all x, the formula A holds x A there exists x such that A holds
Slide 505 Syntactic variations:
f(x)dx
x (man(x) mortal(x))
Slide 506 All mothers are female:
x female(mother(x))
There exists a unique x such that A, sometimes written !x A
Lawrence C. Paulson
University of Cambridge
24
D is a non-empty set, called the domain or universe. I maps symbols to real elements, functions and relations: c a constant symbol f an n-place function symbol P an n-place relation symbol I[c] D I[f] Dn D I[P] Dn
: variables D.
IV [t] extends V to a term t by the obvious recursion: IV [x] = V(x) IV [c] = I[c] IV [f(t1 , . . . , tn )] = I[f](IV [t1 ], . . . , IV [tn ])
def def def
if x is a variable
Lawrence C. Paulson
University of Cambridge
25
|=I,V P(t)
Slide 509
if IV [t]
if | I,V{m/x} =
|=I A
if | I,V =
Formula A is satisable if | I =
A for some I .
Lawrence C. Paulson
University of Cambridge
VI
26
(x = y)
(y (x = y)) [y/x]
IS NOT EQUIVALENT TO
y (y = y)
Lawrence C. Paulson
University of Cambridge
VI
27
(x A)
Slide 603
x A x A A[t/x] x (A B) x (A B).
x A (x A) (x B)
B UT WE DO NOT HAVE
(x A) (x B)
(x A) B (x A) B (x A) B
x (A B) x (A B) x (A B)
Lawrence C. Paulson
University of Cambridge
VI
28
Reasoning by Equivalences
x (x = a P(x))
x (x = a P(a)) x (x = a) P(a)
Slide 605
P(a) z (P(z) P(a) P(b)) z P(z) P(a) P(b) z P(z) P(a) P(b) P(a) P(b) t
A[t/x], x A,
Slide 606
(l)
, A , x A
(r)
Rule (l) can create many instances of x A Rule (r) holds provided N OT allowed to prove
(r)
T HIS IS NONSENSE !
Lawrence C. Paulson
University of Cambridge
VI
29
(l) (r)
Slide 608
P Q(y), P P, Q(y) Q(y) (l) P, P Q(y) Q(y) (l) P, x (P Q(x)) Q(y) (r) P, x (P Q(x)) y Q(y) (r) x (P Q(x)) P y Q(y)
In (l), we must replace x by y.
Lawrence C. Paulson
University of Cambridge
VI
30
A, x A,
Slide 609 Rule (l) holds provided
(l)
, A[t/x] , x A
(r)
Rule (r) can create many instances of x A For example, to prove this counter-intuitive formula:
Slide 610
P(x) P(x), Q(x) (r) P(x) P(x) Q(x) (r) P(x) y (P(y) Q(y)) similar (l) x P(x) y (P(y) Q(y)) x Q(x) y . . . x P(x) x Q(x) y (P(y) Q(y))
(l) (l)
Second subtree proves x Q(x) y (P(y) Q(y)) similarly In (r), we must replace y by x.
Lawrence C. Paulson
University of Cambridge
VI
31
A Failed Proof
Slide 611
P(x), Q(y) P(x) Q(x) (r) P(x), Q(y) z (P(z) Q(z)) (l) P(x), x Q(x) z (P(z) Q(z)) (l) x P(x), x Q(x) z (P(z) Q(z)) (l) x P(x) x Q(x) z (P(z) Q(z))
We cannot use (l) twice with the same variable This attempt renames the x in x Q(x), to get y Q(y)
Lawrence C. Paulson
University of Cambridge
VII
32
Clause Form
Clause: a disjunction of literals
K1 Km L1 Ln
Slide 701 Set notation: Kowalski notation:
{K1 , . . . , Km , L1 , . . . , Ln } K1 , , Km L1 , , Ln L1 , , Ln K1 , , Km
Am
2. This is the set of clauses A1 , . . ., Am 3. Transform the clause set, preserving consistency Deducing the empty clause refutes A. An empty clause set (all clauses deleted) means A is satisable. The basis for SAT SOLVERS and RESOLUTION PROVERS.
Lawrence C. Paulson
University of Cambridge
VII
33
1. Delete tautological clauses: 2. For each unit clause {L}, Slide 703
{P, P, . . .}
Davis-Putnam on a Non-Tautology
Consider P
QQR {Q} {R} {Q} {R} initial clauses {R} unit Q {R} unit P (also pure)
unit R (also pure)
{P, Q} {P}
Clauses satisable by P
t, Q f , R f
Lawrence C. Paulson
University of Cambridge
VII
34
{Q, R}
{R, Q} {R}
{Q} if P is true
unit Q unit R
{Q, R} {Q}
{R}
{Q} {Q}
Progressed from joke to killer technology in 10 years. Princetons zChaff has solved problems with more than one
Slide 706 million variables and 10 million clauses.
Lawrence C. Paulson
University of Cambridge
VII
35
{B}
{B, C1 , . . . , Cn } {C1 , . . . , Cn }
{B}
{B}
QQP
B)
A B [P Q Q P] (P Q) (Q P) (P Q) (Q P)
Clauses:
{P}
{Q}
{Q, P}
Resolve {P} and {Q, P} getting {Q}. Resolve {Q} and {Q} getting : we have refuted the negation.
Lawrence C. Paulson
University of Cambridge
VII
36
Another Example
Refute [(P From (P Slide 709
Q) (P R) P (Q R)]
Q) (P R), get clauses {P, Q} and {P, R}. (Q R)] get clauses {P} and {Q, R}.
From [P
Resolve {P} and {P, Q} getting {Q}. Resolve {P} and {P, R} getting {R}. Resolve {Q} and {Q, R} getting {R}. Resolve {R} and {R} getting , contradiction.
At start, all clauses are passive. None are active. Slide 710 1. Transfer a clause (current) from passive to active. 2. Form all resolvents between current and an active clause. 3. Use new clauses to simplify both passive and active. 4. Put the new clauses into passive. Repeat until CONTRADICTION found or passive becomes empty.
Lawrence C. Paulson
University of Cambridge
VII
37
Renements of Resolution
Subsumption: Slide 711 Preprocessing: Indexing: deleting redundant clauses removing tautologies, symmetries . . . elaborate data structures for speed
Ordered resolution: restrictions to focus the search Weighting: Set of Support: giving priority to the smallest clauses working on the goal, not the axioms
Lawrence C. Paulson
University of Cambridge
VIII
38
Herbrand models: Reduce the class of interpretations Herbrands Thm: Contradictions have nite, ground proofs Unication: Automatically nd the right instantiations
(x A)
Slide 802
x A x A
(x A)
(x A) B (x A) B
and the similar rules for
x (A B) x (A B)
Lawrence C. Paulson
University of Cambridge
VIII
39
= 0).
x1 x2 xk y A
Slide 803 Choose a fresh k-place function symbol, say f Delete y and replace y by f(x1 , x2 , . . . , xk ). We get
x1 x2 xk A[f(x1 , x2 , . . . , xk )/y]
Repeat until no quantiers remain
y P(y)]
Slide 804
[x [P(x) y P(y)]] negated goal x [P(x) y P(y)] x y [P(x) P(y)] x [P(x) P(f(x))] {P(x)} {P(f(x))}
conversion to NNF pulling out Skolem term f(x) Final clauses
Lawrence C. Paulson
University of Cambridge
VIII
40
Correctness of Skolemization
The formula x y A is consistent
for all x D there is some y D such that A holds ^ some function f in D D yields suitable values of y ^ A[f(x)/y] holds in some I extending I so that f denotes f the formula x A[f(x)/y] is consistent.
Dont panic if you cant follow this reasoning!
def
Hi+1 = Hi {f(t1 , . . . , tn ) | t1 , . . . , tn Hi
and f is an n-place function symbol in S}
def
H =
def i0
Hi
Herbrand Universe
H contains the terms expressible using the function symbols of S. Hi contains just the terms with at most i nested function applications.
Lawrence C. Paulson
University of Cambridge
VIII
41
HB = {P(t1 , . . . , tn ) | t1 , . . . , tn H
and P is an n-place predicate symbol in S} Slide 807
def
even(1) even(2)
Slide 808
clauses
H = {1, 2, 1 1, 1 2, 2 1, 2 2, 1 (1 1), . . .} HB = {even(1), even(2), even(1 1), even(1 2), . . .} I[even] = {even(2), even(1 2), even(2 1), even(2 2), . . .}
(for model where means product; could instead use sum!)
Lawrence C. Paulson
University of Cambridge
VIII
42
S is unsatisable no Herbrand interpretation satises S Holds because some Herbrand model mimicks every real model We must consider only a small class of models Herbrand models are syntactic, easily processed by computer
Herbrands Theorem
Let S be a set of clauses.
Finite: we can compute it Instance: result of substituting for variables Ground: no variables remainits propositional!
Example:
Lawrence C. Paulson
University of Cambridge
IX
43
Unication
Finding a common instance of two terms. Lots of applications:
Theorem proving: resolution and other procedures Tools for reasoning with equations Tools for satisfying multiple constraints Polymorphic type-checking (ML and other functional languages)
Its an intuitive generalization of pattern-matching.
= xi .
Lawrence C. Paulson
University of Cambridge
IX
44
Composing Substitutions
Composition of and , written , satises for all terms t
t( ) = (t)
Slide 903 It is dened by (for all relevant x)
= [ (x) / x, . . . ]
Consequences include []
def
= , and associativity :
( ) = ( )
t( ) = t = u = u( )
A most general unier of f(a, x) and f(y, g(z)) is [a/y, g(z)/x]. The common instance is f(a, g(z)).
Lawrence C. Paulson
University of Cambridge
IX
45
Represent terms by binary trees. Each term is a Variable x, y . . ., Constant Slide 905 S KETCH OF THE A LGORITHM . Constants do not unify with different Constants. Constants do not unify with Pairs. Variable x and term t: unier is [t/x], unless x occurs in t Cannot unify f(x) with x!
a, b . . ., or Pair (t, t )
unies t with u .
We unify the left sides, then the right sides. In an implementation, substitutions are formed by updating pointers. Composition happens automatically as more pointers are updated.
Lawrence C. Paulson
University of Cambridge
IX
46
Mathematical justication
Its easy to check that unies (t, t
) with (u, u ):
(t, t )( ) = (t, t )
Slide 907
f(x, b)
Slide 908
f(x, x) f(a, b)
None Fail
Remember, the output is a substitution. The algorithm yields a most general unier.
Lawrence C. Paulson
University of Cambridge
IX
47
Theorem-Proving Example 1
Theorem-Proving Example 2
Lawrence C. Paulson
University of Cambridge
IX
48
Variations on Unication
Efcient unication algorithms: near-linear time Indexing & Discrimination networks: fast retrieval of a uniable term Slide 911 Associative/commutative unication
Example: unify a + (y + c) with (c + x) + b, get [a/x, b/y] Algorithm is very complicated The number of uniers can be exponential
Unication in many other theories (often undecidable!)
Lawrence C. Paulson
University of Cambridge
49
provided B
= D
Slide 1002
{B1 , . . . , Bk , A1 , . . . , Am } {B1 , A1 , . . . , Am }
Example: Prove x y (P(y, x) The clauses are Factoring yields
provided B1
= = Bk
Lawrence C. Paulson
University of Cambridge
50
A Non-Trivial Proof
{P, P} {P, P}
Resolve {P, Q(x)} with {P, Q(a)} getting {P, P} Factor getting {P} getting
Substitution laws like {x = y, f(x) = f(y)} for each f. Substitution laws like {x = y, P(x), P(y)} for each P.
In practice, we need something special: the paramodulation rule
{B[t ], A1 , . . . , Am } {t = u, C1 , . . . , Cn } {B[u], A1 , . . . , Am , C1 , . . . , Cn }
(if t
= t )
Lawrence C. Paulson
University of Cambridge
51
Prolog Clauses
Prolog clauses have a restricted form, with at most one positive literal. The denite clauses form the program. Procedure B with body Slide 1005 commands A1 , . . . , Am is
B A1 , . . . , Am
The single goal clause is like the execution stack, with say m tasks left to be done.
A1 , . . . , Am
Prolog Execution
Linear resolution:
Lawrence C. Paulson
University of Cambridge
52
parent(elizabeth,charles). parent(elizabeth,andrew).
Slide 1007
parent(charles,william). parent(charles,henry). parent(andrew,beatrice). parent(andrew,eugenia). grand(X,Z) :- parent(X,Y), parent(Y,Z). cousin(X,Y) :- grand(Z,X), grand(Z,Y).
Prolog Execution
:- cousin(X,Y). :- grand(Z1,X), grand(Z1,Y). :- parent(Z1,Y2), parent(Y2,X), grand(Z1,Y). * :- parent(charles,X), grand(elizabeth,Y). X=william :- grand(elizabeth,Y). :- parent(elizabeth,Y5), parent(Y5,Y). * :- parent(andrew,Y). Y=beatrice :- .
Slide 1008
Lawrence C. Paulson
University of Cambridge
53
A1 A2 , . . . , Am A2 A3 , . . . , Am , A1
. . .
Am A1 , . . . , Am1
Extension rule: when proving goal P , assume P .
Saturation (that is, resolution): E, Gandalf, SPASS, Vampire, . . . Slide 1010 Higher-Order Logic: TPS, LEO Model Elimination: Prolog Technology Theorem Prover, SETHEO Parallel ME: PARTHENON, PARTHEO Tableau (sequent) based: LeanTAP, 3TAP, . . .
Lawrence C. Paulson
University of Cambridge
XI
54
Modal Operators
W : set of possible worlds (machine states, future times, . . .) R: accessibility relation between worlds
Slide 1101
For a particular frame (W, R) An interpretation I maps the propositional letters to subsets of W Slide 1102
A means A is true in world w w w w w P 2A 3A w I(P) A and w B v v A for all v such that R(w, v) A for some v such that R(w, v)
A B w
Lawrence C. Paulson
University of Cambridge
XI
55
w
Slide 1103
|=W,R,I A |=W,R A
|= A means |=W,R A for all frames; A is universally valid . . . but typically we constrain R to be, say, transitive
All tautologies are universally valid
Extend your favourite propositional proof system with Dist Slide 1104
A 2A
Treat 3 as a denition
3A = 2A
def
Lawrence C. Paulson
University of Cambridge
XI
56
Start with pure modal logic, which is called K Add axioms to constrain the accessibility relation: Slide 1105 T 4 B
2A A 2A 22A A 23A
A, 2A,
Slide 1106
(2l)
, A , 2A , A , 3A
(2r)
A, 3A,
def
(3l)
(3r)
= {2B | 2B } = {3B | 3B }
def
Lawrence C. Paulson
University of Cambridge
XI
57
Slide 1107
B) (2A 2B)
Slide 1108
23A
also 22A
2A
2 3 23 32 232 323
Lawrence C. Paulson
University of Cambridge
XI
58
Slide 1109
A 3A A 23A
(3r) (2r)
(3r) (3l)
Lawrence C. Paulson
University of Cambridge
XII
59
Slide 1201
(2 3)
Left and right: so 14 rules (or 18) plus basic sequent, cut
(2 3)
A, A,
Slide 1202
(basic)
A, A, (cut) A, B, A B, A, x A,
A, B, A B, A[t/x], x A,
Rule (l) holds provided
(l)
(l)
(l)
(l)
Lawrence C. Paulson
University of Cambridge
XII
60
A, 2A,
Slide 1203
def
(2l)
A, 3A,
(3l)
= {2B | 2B }
From 14 (or 18) rules to 4 (or 6)
Left-side only system uses proof by contradiction Right-side only system is an exact dual
Proving x (P
Q(x)) P y Q(y)
P y Q(y), x (P Q(x))
Slide 1204
P, Q(y), P P, Q(y), Q(y) (l) P, Q(y), P Q(y) (l) P, Q(y), x (P Q(x)) (l) P, y Q(y), x (P Q(x)) (l) P y Q(y), x (P Q(x))
Lawrence C. Paulson
University of Cambridge
XII
61
Adding Unication
Slide 1205
A[z/x], x A,
(l)
Updating a variable affects entire proof tree What about rule (l)? Skolemize!
P(y)]:
Lawrence C. Paulson
University of Cambridge
XII
62
A Proof of x y [P(x)
P(y)]
Slide 1207
y f(z) (basic) P(y), P(f(y)), P(z), P(f(z)) (l) P(y), P(f(y)), P(z) P(f(z)) (l) P(y), P(f(y)), x [P(x) P(f(x))] (l) P(y) P(f(y)), x [P(x) P(f(x))] (l) x [P(x) P(f(x))]
Unication chooses the term for (l)
A Failed Proof
Try to prove x [P(x) Q(x)] x P(x) x Q(x) NNF : x P(x) x Q(x), Skolemize: Slide 1208
x [P(x) Q(x)]
ya y b??? P(a), Q(b), P(y) P(a), Q(b), Q(y) P(a), Q(b), P(y) Q(y) (l) P(a), Q(b), x [P(x) Q(x)] (l) P(a) Q(b), x [P(x) Q(x)]
(l)
Lawrence C. Paulson
University of Cambridge
XII
63
Slide 1209
Lawrence C. Paulson
University of Cambridge