Professional Documents
Culture Documents
Logic Slides
Logic Slides
Computer Science Tripos Part IB Logic concerns statements in some language. Statements are declarative assertions:
Michaelmas Term
The language can be natural (English, Latin, . . . ) or formal. Black is the colour of my true love’s hair.
Lawrence C Paulson Some statements are true, others false or meaningless. They are not greetings, questions or commands:
Computer Laboratory Logic concerns relationships between statements: consistency, What is the colour of my true love’s hair?
University of Cambridge
entailment, . . . I wish my true love had hair.
lp15@cam.ac.uk Logical proofs model human reasoning (supposedly). Get a haircut!
Copyright
c 2011 by Lawrence C. Paulson
I Logic and Proof 103 I Logic and Proof 104 I Logic and Proof 105
Consistency, or Satisfiability
Schematic Statements
Interpretations and Validity
A set S of statements is consistent if some interpretation
Now let the variables X, Y , Z, . . . range over ‘real’ objects satisfies all elements of S at the same time. Otherwise S is
An interpretation maps meta-variables to real objects: inconsistent.
Black is the colour of X’s hair.
The interpretation Y 7→ coal satisfies the statement Examples of inconsistent sets:
Black is the colour of Y .
Black is the colour of Y . {X part of Y, Y part of Z, X NOT part of Z}
Z is the colour of Y .
but the interpretation Y 7→ strawberries does not!
Schematic statements can even express questions: {n is a positive integer, n 6= 1, n 6= 2, . . .}
A statement A is valid if all interpretations satisfy A.
What things are black? Satisfiable means the same as consistent.
Inference
Entailment, or Logical Consequence Schematic Inference Rules
I Logic and Proof 109 I Logic and Proof 110 II Logic and Proof 201
II Logic and Proof 205 II Logic and Proof 206 II Logic and Proof 207
III Logic and Proof 302 III Logic and Proof 303 III Logic and Proof 304
III Logic and Proof 308 III Logic and Proof 309 III Logic and Proof 310
Reasons about all and some individuals: f(t1 , . . . , tn ) n-place relation symbol and t1 , . . ., tn are terms.
All men are mortal Socrates is a man where f is an n-place function symbol and t1 , . . ., tn are A formula is built up from atomic formulæ using ¬, ∧, ∨, and
Socrates is mortal so forth.
terms.
Cannot reason about all functions or all relations, etc. We choose the language, adopting any desired function Later, we can add quantifiers.
symbols.
IV Logic and Proof 404 IV Logic and Proof 405 IV Logic and Proof 406
The Power of Quantifier-Free FOL Universal and Existential Quantifiers The Expressiveness of Quantifiers
It is surprisingly expressive, if we include strong induction rules. All men are mortal:
∀x A for all x, the formula A holds
We can easily prove the equivalence of mathematical ∃x A there exists x such that A holds ∀x (man(x) → mortal(x))
functions:
All mothers are female:
Syntactic variations:
p(z, 0) = 1 q(z, 1) = z
∀x female(mother(x))
p(z, n + 1) = p(z, n) × z q(z, 2 × n) = q(z × z, n) ∀xyz A abbreviates ∀x ∀y ∀z A
There exists a unique x such that A, sometimes written ∃!x A
q(z, 2 × n + 1) = q(z × z, n) × z ∀z . A ∧ B is an alternative to ∀z (A ∧ B)
R ∃x [A(x) ∧ ∀y (A(y) → y = x)]
The prover ACL2 uses this logic to do major hardware proofs. The variable x is bound in ∀x A; compare with f(x)dx
IV Logic and Proof 407 IV Logic and Proof 408 IV Logic and Proof 409
IV Logic and Proof 410 IV Logic and Proof 411 V Logic and Proof 501
∀y (y = y)
V Logic and Proof 505 V Logic and Proof 506 V Logic and Proof 507
V Logic and Proof 511 VI Logic and Proof 601 VI Logic and Proof 602
1. Delete tautological clauses: {P, ¬P, . . .} Consider P ∨Q→Q∨R {¬Q, R} {¬R, P} {¬R, Q} {¬P, Q, R} {P, Q} {¬P, ¬Q}
2. For each unit clause {L}, Clauses are {P, Q} {¬Q} {¬R} {¬Q, R} {¬R, Q} {Q, R} {¬Q} if P is true
• delete all clauses containing L {¬R} {R} unit ¬Q
• delete ¬L from all clauses {P, Q} {¬Q} {¬R} initial clauses
{} unit R
3. Delete all clauses containing pure literals {P} {¬R} unit ¬Q
{¬Q, R} {¬R} {¬R, Q} {Q} if P is false
4. Perform a case split on some literal; STOP if a model is {¬R} unit P (also pure)
{¬Q} {Q} unit ¬R
found unit ¬R (also pure)
{} unit ¬Q
DPLL is a decision procedure: it finds a contradiction or a A LL CLAUSES DELETED ! Clauses satisfiable by
Both cases yield contradictions: the clauses are
model. P 7→ t, Q 7→ f, R 7→ f INCONSISTENT !
VI Logic and Proof 606 VI Logic and Proof 607 VI Logic and Proof 608
SAT solvers in the Real World The Resolution Rule Simple Example: Proving P ∧Q→Q∧P
Another Example
The Saturation Algorithm
Heuristics and Hacks for Resolution
Refute ¬[(P ∨ Q) ∧ (P ∨ R) → P ∨ (Q ∧ R)]
At start, all clauses are passive. None are active.
From (P ∨ Q) ∧ (P ∨ R), get clauses {P, Q} and {P, R}. Orderings to focus the search on specific literals
1. Transfer a clause (current) from passive to active.
From ¬ [P ∨ (Q ∧ R)] get clauses {¬P} and {¬Q, ¬R}. Subsumption, or deleting redundant clauses
2. Form all resolvents between current and an active clause. Indexing: elaborate data structures for speed
Resolve {¬P} and {P, Q} getting {Q}.
3. Use new clauses to simplify both passive and active. Preprocessing: removing tautologies, symmetries . . .
Resolve {¬P} and {P, R} getting {R}.
4. Put the new clauses into passive. Weighting: giving priority to “good” clauses over those
Resolve {Q} and {¬Q, ¬R} getting {¬R}. containing unwanted constants
Repeat until CONTRADICTION found or passive becomes empty.
Resolve {R} and {¬R} getting , contradiction.
VII Logic and Proof 701 VII Logic and Proof 702 VII Logic and Proof 703
Example of Conversion to Clauses Correctness of Skolemization Simplifying the Search for Models
For proving ∃x [P(x) → ∀y P(y)] The formula ∀x ∃y A is consistent S is satisfiable if even one model makes all of its clauses true.
⇐⇒ it holds in some interpretation I = (D, I) There are infinitely many models to consider!
¬ [∃x [P(x) → ∀y P(y)]] negated goal ⇐⇒ for all x ∈ D there is some y ∈ D such that A holds Also many duplicates: “states of the USA” and “the integers 1
∀x [P(x) ∧ ∃y ¬P(y)] conversion to NNF ⇐⇒ some function f^ in D → D yields suitable values of y to 50”
∀x ∃y [P(x) ∧ ¬P(y)] pulling ∃ out ⇐⇒ A[f(x)/y] holds in some I ′ extending I so that f Fortunately, nice models exist.
VII Logic and Proof 707 VII Logic and Proof 708 VII Logic and Proof 709
The Herbrand Universe for a Set of Clauses S The Herbrand Semantics of Terms
The Herbrand Semantics of Predicates
def
H0 = the set of constants in S (must be non-empty) In an Herbrand model, every constant stands for itself.
def
Every function symbol stands for a term-forming operation: An Herbrand interpretation defines an n-place predicate P to
Hi+1 = Hi ∪ {f(t1 , . . . , tn ) | t1 , . . . , tn ∈ Hi
denote a truth-valued function in Hn → {t, f}, making
and f is an n-place function symbol in S}
f denotes the function that puts ‘f’ in front of the given
P(t1 , . . . , tn ) true . . .
[ arguments.
def
H= Hi Herbrand Universe • if and only if the formula P(t1 , . . . , tn ) holds in our
In an Herbrand model, X + 0 can never equal X.
i≥0 desired “real” interpretation I of the clauses.
Hi contains just the terms with at most i nested function
• Thus, an Herbrand interpretation can imitate any other
applications.
Every ground term denotes itself. interpretation.
H consists of the terms in S that contain no variables (ground
This is the promised uniform structure!
terms).
VII Logic and Proof 710 VII Logic and Proof 711 VII Logic and Proof 712
VIII Logic and Proof 801 VIII Logic and Proof 802 VIII Logic and Proof 803
Unification
Substitutions: A Mathematical Treatment Composing Substitutions
• Polymorphic type-checking (ML and other functional P(t, u)θ = P(tθ, uθ) (in literals) Consequences include θ ◦ [] = θ, and associativity :
languages) {L1 , . . . , Lm }θ = {L1 θ, . . . , Lm θ} (in clauses)
(φ ◦ θ) ◦ σ = φ ◦ (θ ◦ σ)
It is an intuitive generalization of pattern-matching.
VIII Logic and Proof 804 VIII Logic and Proof 805 VIII Logic and Proof 806
θ is more general than φ if φ = θ ◦ σ for some substitution σ. Each term is a Variable x, y . . ., Constant a, b . . ., or Pair θ ◦ θ ′ unifies (t, t ′ ) with (u, u ′ )
θ is most general if it is more general than every other unifier. (t, t ′ ) if θ unifies t with u and θ ′ unifies t ′ θ with u ′ θ.
We unify the left sides, then the right sides.
If θ unifies t and u then so does θ ◦ σ: S KETCH OF THE A LGORITHM .
In an implementation, substitutions are formed by updating
Constants do not unify with different Constants or with Pairs.
t(θ ◦ σ) = tθσ = uθσ = u(θ ◦ σ) pointers.
Variable x and term t: if x occurs in t, FAIL. Otherwise, unifier
Composition happens automatically as more pointers are
A most general unifier of f(a, x) and f(y, g(z)) is [a/y, g(z)/x]. is [t/x].
updated.
The common instance is f(a, g(z)). Cannot unify f(· · · x · · · ) with x!
VIII Logic and Proof 807 VIII Logic and Proof 808 VIII Logic and Proof 809
Mathematical Justification
Four Unification Examples
Theorem-Proving Example 1
It’s easy to check that θ ◦ θ ′ unifies (t, t ′ ) with (u, u ′ ):
f(x, b) f(x, x) f(x, x) j(x, x, z)
(t, t ′ )(θ ◦ θ ′ ) = (t, t ′ )θθ ′ definition of substitution (∃y ∀x R(x, y)) → (∀x ∃y R(x, y))
f(a, y) f(a, b) f(y, g(y)) j(w, a, h(w))
′ ′ ′ After negation, the clauses are {R(x, a)} and {¬R(b, y)}.
= (tθθ , t θθ ) substituting into the pair
f(a, b) None None j(a, a, h(a))
′ ′ ′
= (uθθ , t θθ ) tθ = uθ The literals R(x, a) and R(b, y) have unifier [b/x, a/y].
[a/x, b/y] Fail Fail [a/w, a/x, h(a)/z]
′ ′ ′ ′ ′ ′ ′
= (uθθ , u θθ ) t θθ = u θθ We have the contradiction R(b, a) and ¬R(b, a).
Remember, the output is a substitution.
= (u, u ′ )(θ ◦ θ ′ ) definition of substitution T HE THEOREM IS PROVED BY CONTRADICTION !
The algorithm naturally yields a most general unifier.
′ ′
In fact θ ◦ θ is even a most general unifier, if θ and θ are!
VIII Logic and Proof 810 VIII Logic and Proof 811 IX Logic and Proof 901
Variations on Unification
The Binary Resolution Rule
Theorem-Proving Example 2 Efficient unification algorithms: near-linear time
IX Logic and Proof 902 IX Logic and Proof 903 IX Logic and Proof 904
∃x [P → Q(x)] ∧ ∃x [Q(x) → P] → ∃x [P ↔ Q(x)] In theory, it’s enough to add the equality axioms:
This inference collapses unifiable literals in one clause:
Clauses are • The reflexive, symmetric and transitive laws.
{B1 , . . . , Bk , A1 , . . . , Am }
provided B1 σ = · · · = Bk σ {P, ¬Q(b)} {P, Q(x)} {¬P, ¬Q(x)} {¬P, Q(a)} • Substitution laws like {x 6= y, f(x) = f(y)} for each f.
{B1 , A1 , . . . , Am }σ
Resolve {P, ¬Q(b)} with {P, Q(x)} getting {P, P} • Substitution laws like {x 6= y, ¬P(x), P(y)} for each P.
Example: Prove ∀x ∃y ¬(P(y, x) ↔ ¬P(y, y))
Factor {P, P} getting {P} In practice, we need something special: the paramodulation
The clauses are {¬P(y, a), ¬P(y, y)} {P(y, y), P(y, a)} Resolve {¬P, ¬Q(x)} with {¬P, Q(a)} getting {¬P, ¬P} rule
Factoring yields {¬P(a, a)} {P(a, a)} Factor {¬P, ¬P} getting {¬P} {B[t ′ ], A1 , . . . , Am } {t = u, C1 , . . . , Cn }
Resolution yields the empty clause!
Resolve {P} with {¬P} getting {B[u], A1 , . . . , Am , C1 , . . . , Cn }σ (if tσ = t ′ σ)
IX Logic and Proof 905 IX Logic and Proof 906 IX Logic and Proof 907
Prolog clauses have a restricted form, with at most one positive Linear resolution:
literal. • Always resolve some program clause with the goal clause. parent(elizabeth,charles).
parent(elizabeth,andrew).
The definite clauses form the program. Procedure B with body • The result becomes the new goal clause.
“commands” A1 , . . . , Am is parent(charles,william).
Try the program clauses in left-to-right order.
parent(charles,henry).
B ← A1 , . . . , Am Solve the goal clause’s literals in left-to-right order.
parent(andrew,beatrice).
The single goal clause is like the “execution stack”, with say m Use depth-first search. (Performs backtracking, using little
parent(andrew,eugenia).
tasks left to be done. space.)
Do unification without occurs check. (U NSOUND, but needed grand(X,Z) :- parent(X,Y), parent(Y,Z).
← A1 , . . . , Am cousin(X,Y) :- grand(Z,X), grand(Z,Y).
for speed)
IX Logic and Proof 908 IX Logic and Proof 909 IX Logic and Proof 910
BDDs: Binary Decision Diagrams Decision Diagram for (P ∨ Q) ∧ R Converting a Decision Diagram to a BDD
X Logic and Proof 1004 X Logic and Proof 1005 X Logic and Proof 1006
0 1 0 1 0 1 0 1 0 1
Never convert X ∧ Y twice, but keep a hash table of known
canonical forms. This prevents redundant computations.
X Logic and Proof 1010 XI Logic and Proof 1101 XI Logic and Proof 1102
w
A means A is true in world w Dist ✷(A → B) → (✷A → ✷B) Add axioms to constrain the accessibility relation:
|=W,R,I A means w
A for all w in W Inference Rule: Necessitation T ✷A → A (reflexive) logic T
|=W,R A means w
A for all w and all I A 4 ✷A → ✷✷A (transitive) logic S4
✷A
|= A means |=W,R A for all frames; A is universally valid B A → ✷✸A (symmetric) logic S5
. . . but typically we constrain R to be, say, transitive. Treat ✸ as a definition And countless others!
def
✸A = ¬✷¬A We mainly look at S4, which resembles a logic of time.
All propositional tautologies are universally valid!
XI Logic and Proof 1106 XI Logic and Proof 1107 XI Logic and Proof 1108
XII Logic and Proof 1203 XII Logic and Proof 1204 XII Logic and Proof 1205
Move the right-side formula to the left and convert to NNF: Rule (∀l) now inserts a new free variable:
A, Γ ⇒ A, Γ ∗ ⇒
(✷l) (✸l)
P ∧ ∃y ¬Q(y), ∀x (¬P ∨ Q(x)) ⇒ A[z/x], Γ ⇒
✷A, Γ ⇒ ✸A, Γ ⇒ (∀l)
∀x A, Γ ⇒
def
Γ ∗ = {✷B | ✷B ∈ Γ } Erase non-✷ assumptions
P, ¬Q(y), ¬P ⇒ P, ¬Q(y), Q(y) ⇒ Let unification instantiate any free variable
(∨l)
From 14 (or 18) rules to 4 (or 6) P, ¬Q(y), ¬P ∨ Q(y) ⇒
(∀l) In ¬A, B, Γ ⇒ try unifying A with B to make a basic sequent
P, ¬Q(y), ∀x (¬P ∨ Q(x)) ⇒
Left-side only system uses proof by contradiction (∃l)
P, ∃y ¬Q(y), ∀x (¬P ∨ Q(x)) ⇒ Updating a variable affects entire proof tree
Right-side only system is an exact dual (∧l)
P ∧ ∃y ¬Q(y), ∀x (¬P ∨ Q(x)) ⇒ What about rule (∃l)? D O NOT USE IT ! Instead, Skolemize!
XII Logic and Proof 1206 XII Logic and Proof 1207 XII Logic and Proof 1208
[∀y ∃z Q(y, z)] ∧ ∃x P(x) to [∀y Q(y, f(y))] ∧ P(a) NNF : ∃x ¬P(x) ∧ ∃x ¬Q(x), ∀x [P(x) ∨ Q(x)] ⇒
y 7→ f(z) Skolemize: ¬P(a) ∧ ¬Q(b), ∀x [P(x) ∨ Q(x)] ⇒
(basic)
It’s better to push quantifiers in (called miniscoping) P(y), ¬P(f(y)), P(z), ¬P(f(z)) ⇒
(∧l)
Example: proving ∃x ∀y [P(x) → P(y)]: P(y), ¬P(f(y)), P(z) ∧ ¬P(f(z)) ⇒
(∀l) y 7→ a y 7→ b???
Negate; convert to NNF : ∀x ∃y [P(x) ∧ ¬P(y)] P(y), ¬P(f(y)), ∀x [P(x) ∧ ¬P(f(x))] ⇒
(∧l) ¬P(a), ¬Q(b), P(y) ⇒ ¬P(a), ¬Q(b), Q(y) ⇒
P(y) ∧ ¬P(f(y)), ∀x [P(x) ∧ ¬P(f(x))] ⇒ (∨l)
Push in the ∃y : ∀x [P(x) ∧ ∃y ¬P(y)] (∀l) ¬P(a), ¬Q(b), P(y) ∨ Q(y) ⇒
∀x [P(x) ∧ ¬P(f(x))] ⇒ (∀l)
Push in the ∀x : (∀x P(x)) ∧ (∃y ¬P(y)) ¬P(a), ¬Q(b), ∀x [P(x) ∨ Q(x)] ⇒
(∧l)
Skolemize: ∀x P(x) ∧ ¬P(a) Unification chooses the term for (∀l) ¬P(a) ∧ ¬Q(b), ∀x [P(x) ∨ Q(x)] ⇒
prove((A,B),UnExp,Lits,FreeV,VarLim) :- !, and
prove(A,[B|UnExp],Lits,FreeV,VarLim).
prove((A;B),UnExp,Lits,FreeV,VarLim) :- !, or
prove(A,UnExp,Lits,FreeV,VarLim),
prove(B,UnExp,Lits,FreeV,VarLim).
prove(all(X,Fml),UnExp,Lits,FreeV,VarLim) :- !, forall
\+ length(FreeV,VarLim),
copy_term((X,Fml,FreeV),(X1,Fml1,FreeV)),
append(UnExp,[all(X,Fml)],UnExp1),
prove(Fml1,UnExp1,Lits,[X1|FreeV],VarLim).
prove(Lit,_,[L|Lits],_,_) :- literals; negation
(Lit = -Neg; -Lit = Neg) ->
(unify(Neg,L); prove(Lit,[],Lits,_,_)).
prove(Lit,[Next|UnExp],Lits,FreeV,VarLim) :-
next formula
prove(Next,UnExp,[Lit|Lits],FreeV,VarLim).