// ----------------------------------------------------------------- // logic.txt: propositional logic, minimization, satisfiability // Concrete Abstractions: Propositional Logic // (c) 2020, Wolfgang Schreiner, RISC, JKU Linz, Austria // ----------------------------------------------------------------- // ----------------------------------------------------------------- // Boolean algebra // ----------------------------------------------------------------- theorem trueFalse() ⇔ ⊤ ≠ ⊥; val notT = print Map[Bool,Bool](⊥) with [⊥] = ⊤; val andT = print Map[Tuple[Bool,Bool],Bool](⊥) with [⟨⊤,⊤⟩] = ⊤; val orT = print Map[Tuple[Bool,Bool],Bool](⊤) with [⟨⊥,⊥⟩] = ⊥; val impT = print Map[Tuple[Bool,Bool],Bool](⊤) with [⟨⊤,⊥⟩] = ⊥; val equT = print Map[Tuple[Bool,Bool],Bool](⊥) with [⟨⊤,⊤⟩] = ⊤ with [⟨⊥,⊥⟩] = ⊤; theorem notT(b:Bool) ⇔ (¬b) = notT[b]; theorem andT(b1:Bool,b2:Bool) ⇔ (b1 ∧ b2) = andT[⟨b1,b2⟩]; theorem orT(b1:Bool,b2:Bool) ⇔ (b1 ∨ b2) = orT[⟨b1,b2⟩]; theorem impT(b1:Bool,b2:Bool) ⇔ (b1 ⇒ b2) = impT[⟨b1,b2⟩]; theorem equT(b1:Bool,b2:Bool) ⇔ (b1 ⇔ b2) = equT[⟨b1,b2⟩]; theorem notTrue() ⇔ (¬⊤) = ⊥; theorem notFalse() ⇔ (¬⊥) = ⊤; theorem notNot(b:Bool) ⇔ (¬(¬b)) = b; theorem andTrue(b:Bool) ⇔ (b ∧ ⊤) = b; theorem andFalse(b:Bool) ⇔ (b ∧ ⊥) = ⊥; theorem andIdempotent(b:Bool) ⇔ (b ∧ b) = b; theorem andNot(b:Bool) ⇔ (b ∧ ¬b) = ⊥; theorem andCommutative(b1:Bool,b2:Bool) ⇔ (b1 ∧ b2) = (b2 ∧ b1); theorem andAssociative(b1:Bool,b2:Bool,b3:Bool) ⇔ (b1 ∧ (b2 ∧ b3)) = ((b1 ∧ b2) ∧ b3); theorem andAbsorption(b1:Bool,b2:Bool) ⇔ (b1 ∧ (b1 ∨ b2)) = b1; theorem andDistributive(b1:Bool,b2:Bool,b3:Bool) ⇔ (b1 ∧ (b2 ∨ b3)) = ((b1 ∧ b2) ∨ (b1 ∧ b3)); theorem andDeMorgan(b1:Bool,b2:Bool,b3:Bool) ⇔ (¬(b1 ∧ b2)) = (¬b1 ∨ ¬b2); theorem orTrue(b:Bool) ⇔ (b ∨ ⊤) = ⊤; theorem orFalse(b:Bool) ⇔ (b ∨ ⊥) = b; theorem orIdempotent(b:Bool) ⇔ (b ∨ b) = b; theorem orNot(b:Bool) ⇔ (b ∨ ¬b) = ⊤; theorem orCommutative(b1:Bool,b2:Bool) ⇔ (b1 ∨ b2) = (b2 ∨ b1); theorem orAssociative(b1:Bool,b2:Bool,b3:Bool) ⇔ (b1 ∨ (b2 ∨ b3)) = ((b1 ∨ b2) ∨ b3); theorem orAbsorption(b1:Bool,b2:Bool) ⇔ (b1 ∨ (b1 ∧ b2)) = b1; theorem orDistributive(b1:Bool,b2:Bool,b3:Bool) ⇔ (b1 ∨ (b2 ∧ b3)) = ((b1 ∨ b2) ∧ (b1 ∨ b3)); theorem orDeMorgan(b1:Bool,b2:Bool,b3:Bool) ⇔ (¬(b1 ∨ b2)) = (¬b1 ∧ ¬b2); theorem impliesDef(b1:Bool, b2:Bool) ⇔ (b1 ⇒ b2) = (¬b1 ∨ b2); theorem equivDef(b1:Bool, b2:Bool) ⇔ (b1 ⇔ b2) = ((b1 ⇒ b2) ∧ (b2 ⇒ b1)); theorem impliesTrue1(b:Bool) ⇔ (b ⇒ ⊤) = ⊤; theorem impliesTrue2(b:Bool) ⇔ (⊤ ⇒ b) = b; theorem impliesFalse1(b:Bool) ⇔ (b ⇒ ⊥) = (¬b); theorem impliesFalse2(b:Bool) ⇔ (⊥ ⇒ b) = ⊤; theorem impliesOr(b1:Bool,b2:Bool) ⇔ (b1 ⇒ b2) = (¬b1 ∨ b2); theorem impliesReverse(b1:Bool,b2:Bool) ⇔ (b1 ⇒ b2) = (¬b2 ⇒ ¬b1); theorem impliesNot(b1:Bool,b2:Bool) ⇔ (¬(b1 ⇒ b2)) = (b1 ∧ ¬b2); theorem impliesAnd(b1:Bool,b2:Bool,b3:Bool) ⇔ (b1 ⇒ (b2 ∧ b3)) = ((b1 ⇒ b2) ∧ (b1 ⇒ b3)); theorem impliesOr(b1:Bool,b2:Bool,b3:Bool) ⇔ ((b1 ∨ b2) ⇒ b3) = ((b1 ⇒ b3) ∧ (b2 ⇒ b3)); theorem equivTrue(b:Bool) ⇔ (b ⇔ ⊤) = b; theorem equivFalse(b:Bool) ⇔ (b ⇔ ⊥) = (¬b); theorem equivReflexive(b:Bool) ⇔ (b ⇒ b) = ⊤; theorem equivSymmetric(b1:Bool,b2:Bool) ⇔ (b1 ⇔ b2) = (b2 ⇔ b1); theorem equivAndOr(b1:Bool,b2:Bool) ⇔ (b1 ⇔ b2) = ((b1 ∧ b2) ∨ (¬b1 ∧ ¬b2)); theorem equivNot(b1:Bool,b2:Bool) ⇔ (¬(b1 ⇔ b2)) = ((b1 ∧ ¬b2) ∨ (¬b1 ∧ b2)); // ----------------------------------------------------------------- // disjunctive and conjunctive normal forms // ----------------------------------------------------------------- val N:ℕ; // maximum index of propositional variables type Literal = ℤ[-(N+1),(N+1)] with value ≠ 0; type Clause = Set[Literal] with ∀l∈value. -l ∉ value; type NormalForm = Set[Clause]; type Assignment = Set[Literal] with ∀l∈value. -l ∉ value; pred complete(a:Assignment) ⇔ ∀l:Literal. l ∈ a ∨ -l ∈ a; // the satisfaction relation for disjunctive normal forms pred satDNF(a:Assignment, l:Literal) ⇔ -l ∉ a; pred satDNF(a:Assignment, c:Clause) ⇔ ∀l∈c. satDNF(a,l); pred satDNF(a:Assignment, f:NormalForm) ⇔ ∃c∈f. satDNF(a,c); // the satisfaction relation for conjunctive normal forms pred satCNF(a:Assignment, l:Literal) ⇔ l ∈ a; pred satCNF(a:Assignment, c:Clause) ⇔ ∃l∈c. satCNF(a,l); pred satCNF(a:Assignment, f:NormalForm) ⇔ ∀c∈f. satCNF(a,c); // satisfiability and the validity of normal forms pred satDNF(f:NormalForm) ⇔ ∃a:Assignment. satDNF(a,f); pred satCNF(f:NormalForm) ⇔ ∃a:Assignment. satCNF(a,f); pred validDNF(f:NormalForm) ⇔ ∀a:Assignment. satDNF(a,f); pred validCNF(f:NormalForm) ⇔ ∀a:Assignment. satCNF(a,f); // true and false val trueDNF:NormalForm = { ∅[Literal] }; theorem validTrueDNF() ⇔ validDNF(trueDNF); val falseDNF:NormalForm = ∅[Clause]; theorem notSatFalseDNF() ⇔ ¬satDNF(falseDNF); val trueCNF:NormalForm = ∅[Clause]; theorem validTrueCNF() ⇔ validCNF(trueCNF); val falseCNF:NormalForm = { ∅[Literal] }; theorem notSatFalseCNF() ⇔ ¬satCNF(falseCNF); // the negation of both kinds of normal forms fun not(f:NormalForm):NormalForm = { c | c:Clause with ∀d∈f. ∃l∈d. -l∈c }; theorem satNotDNF(a:Assignment,f:NormalForm) requires complete(a); ⇔ (satDNF(a,not(f)) ⇔ ¬satDNF(a,f)); theorem satNotCNF(a:Assignment,f:NormalForm) requires complete(a); ⇔ (satCNF(a,not(f)) ⇔ ¬satCNF(a,f)); // duality of satisfiability and validity theorem validSatDNF(f:NormalForm) ⇔ validDNF(f) ⇔ ¬satDNF(not(f)); theorem validSatCNF(f:NormalForm) ⇔ validCNF(f) ⇔ ¬satCNF(not(f)); // logical equivalence pred equivDNF(f1:NormalForm,f2:NormalForm) ⇔ ∀a:Assignment. satDNF(a,f1) ⇔ satDNF(a,f2); pred equivCNF(f1:NormalForm,f2:NormalForm) ⇔ ∀a:Assignment. satCNF(a,f1) ⇔ satCNF(a,f2); // complete normal forms pred complete(f:NormalForm) ⇔ ∀c∈f. complete(c); // uniqueness of complete normal forms theorem canonicalDNF(f1:NormalForm,f2:NormalForm) requires complete(f1) ∧ complete(f2); ⇔ equivDNF(f1,f2) ⇒ f1 = f2; theorem canonicalCNF(f1:NormalForm,f2:NormalForm) requires complete(f1) ∧ complete(f2); ⇔ equivCNF(f1,f2) ⇒ f1 = f2; // ----------------------------------------------------------------- // minimization of a propositional formula in disjunctive normal form // ----------------------------------------------------------------- // size of a normal form (the total number of and/or-connectives in it) fun size(f:NormalForm):ℕ[2^(3^(N+1))] = |f| + ∑c∈f.|c|; // core of the Quine–McCluskey algorithm multiple fun minimize(f:NormalForm):NormalForm requires ∀c∈f with ¬complete(c). ∃l:Literal with l∉c. c∪{l}∈f ∧ c∪{-l}∈f; ensures equivDNF(f,result); ensures ∀r:NormalForm with equivDNF(f,r). size(result) ≤ size(r); decreases 3^(N+1)-|f|; modular; = choose c1∈f, l∈c1, c2∈f with -l∈c2 ∧ c1\{l}=c2\{-l} ∧ c1\{l}∉f in minimize(f∪{c1\{l}}) else let f0 = { c | c∈f with ¬∃c0∈f with c0≠c. c0⊆c } in let cs = { c | c∈f0 with ∃a:Assignment with satDNF(a,c). ∀c0∈f0 with c0≠c. ¬satDNF(a,c0) } in choose cs0∈Set(f0\cs) with ∀a:Assignment with satDNF(a,f0). satDNF(a,cs ∪ cs0) ∧ (∀cs1∈Set(f0\cs) with (∀a:Assignment with satDNF(a,f0). satDNF(a,cs ∪ cs1)). size(cs0) ≤ size(cs1)) in cs ∪ cs0; // Quine–McCluskey algorithm fun QMC(f:NormalForm):NormalForm ensures equivDNF(f,result); ensures ∀r:NormalForm with equivDNF(f,r). size(result) ≤ size(r); = minimize({a | a:Assignment with complete(a) ∧ satDNF(a,f)}); // ----------------------------------------------------------------- // satisfiability of a propositional formula in conjunctive normal form // ----------------------------------------------------------------- // satisfiability problem for disjunctive normal form is trivial theorem satDNFNonEmpty(f:NormalForm) ⇔ satDNF(f) ⇔ f ≠ ∅[Clause]; // the literals of a formula fun literals(f:NormalForm):Set[Literal] = {l | l:Literal with ∃c∈f. l∈c}; // the result of setting a literal l in formula f to true fun substitute(f:NormalForm,l:Literal):NormalForm = {c\{-l} | c∈f with l∉c}; // DPLL algorithm (core form without optimizations) multiple pred DPLL0(f:NormalForm) ensures result ⇔ satCNF(f); decreases |literals(f)|; ⇔ if f = ∅[Clause] then ⊤ else if ∅[Literal] ∈ f then ⊥ else choose l∈literals(f) in DPLL0(substitute(f,l)) ∨ DPLL0(substitute(f,-l)); // DPLL algorithm (with unit propagation and pure literal elimination) multiple pred DPLL1(f:NormalForm) ensures result ⇔ satCNF(f); decreases |literals(f)|; ⇔ if f = ∅[Clause] then ⊤ else if ∅[Literal] ∈ f then ⊥ else choose c∈f with |c|=1, l∈c in DPLL1(substitute(f,l)) else choose l∈literals(f) with ∀c∈f. -l∉c in DPLL1(f\{c| c∈f with l∈c}) else choose l∈literals(f) in DPLL1(substitute(f,l)) ∨ DPLL1(substitute(f,-l)); // the variables in a formula fun vars(f:NormalForm): Set[ℕ[N+1]] = { if l>0 then l else -l | l ∈ literals(f) }; // the number of nodes in the search tree for f fun nsize(f:NormalForm): ℕ[2^(N+2)-1] = 2^(|vars(f)|+1)-1; // DPLL algorithm (iterative form without optimizations) proc DPLL2(f:NormalForm): Bool ensures result ⇔ satCNF(f); { var satisfiable: Bool ≔ ⊥; var stack: Array[N+2,NormalForm] ≔ Array[N+2,NormalForm](∅[Clause]); var number: ℕ[N+2] ≔ 0; stack[number] ≔ f; number ≔ number+1; while ¬satisfiable ∧ number>0 do invariant 0 ≤ number ∧ number ≤ N+2; invariant satCNF(f) ⇔ satisfiable ∨ ∃i:ℕ[N+2] with i 0 ⇒ |vars(stack[number-1])| ≤ |vars(f)|-number+1; decreases if satisfiable then 0 else ∑k:ℕ[N+1] with k x ∈ a; trueP -> ⊤; falseP -> ⊥; notP(p:Prop) -> notT[truth(p,a)]; andP(p1:Prop,p2:Prop) -> andT[⟨truth(p1,a), truth(p2,a)⟩]; orP(p1:Prop,p2:Prop) -> orT [⟨truth(p1,a), truth(p2,a)⟩]; impP(p1:Prop,p2:Prop) -> impT[⟨truth(p1,a), truth(p2,a)⟩]; equP(p1:Prop,p2:Prop) -> equT[⟨truth(p1,a), truth(p2,a)⟩]; }; pred satisfies(a:Ass,p:Prop) decreases height(p); ⇔ match p with { varP(x:Var) -> x ∈ a; trueP -> ⊤; falseP -> ⊥; notP(p:Prop) -> ¬satisfies(a,p); andP(p1:Prop,p2:Prop) -> satisfies(a,p1) ∧ satisfies(a,p2); orP(p1:Prop,p2:Prop) -> satisfies(a,p1) ∨ satisfies(a,p2); impP(p1:Prop,p2:Prop) -> satisfies(a,p1) ⇒ satisfies(a,p2); equP(p1:Prop,p2:Prop) -> satisfies(a,p1) ⇔ satisfies(a,p2); }; theorem semantics(p:Prop,a:Ass) ⇔ satisfies(a,p) ⇔ truth(p,a) = ⊤; pred equiv(p1:Prop,p2:Prop) ⇔ ∀a:Ass. satisfies(a,p1) ⇔ satisfies(a,p2); theorem andDeMorgan(p1:Prop,p2:Prop) requires height(p1) ≤ P-2 ∧ height(p2) ≤ P-2; ⇔ equiv(Prop!notP(Prop!andP(p1,p2)), Prop!orP(Prop!notP(p1),Prop!notP(p2))); fun props(n:ℕ[P]):Set[Prop] decreases n; = if n = 0 then { Prop!varP(x) | x:Var } ∪ { Prop!trueP } ∪ { Prop!falseP } else let fs = props(n-1) in fs ∪ { Prop!notP(f) | f ∈ fs } ∪ { Prop!andP(f1,f2) | f1 ∈ fs, f2 ∈ fs } ∪ { Prop!orP(f1,f2) | f1 ∈ fs, f2 ∈ fs } ∪ { Prop!impP(f1,f2) | f1 ∈ fs, f2 ∈ fs } ∪ { Prop!equP(f1,f2) | f1 ∈ fs, f2 ∈ fs }; proc checkDeMorgan():() { val props = props(P-2); print |props|; check andDeMorgan with p1∈props, p2∈props; } // conversion of a formula assignment to a normal form assignment fun assignment(a:Ass):Assignment = { l | l:Literal with (l > 0 ∧ l-1 ∈ a) ∨ (l < 0 ∧ -(l+1) ∉ a) }; // negation of a normal form assignment fun negate(a:Assignment):Assignment = { -l | l ∈ a }; // equivalence of propositional formula and normal form pred equivDNF(p:Prop,f:NormalForm) ⇔ ∀a:Ass. satisfies(a,p) ⇔ satDNF(assignment(a),f); pred equivCNF(p:Prop,f:NormalForm) ⇔ ∀a:Ass. satisfies(a,p) ⇔ satCNF(assignment(a),f); // conversion of a formula into its disjunctive normal form fun DNF(p:Prop):NormalForm ensures equivDNF(p,result); = { assignment(a) | a:Ass with satisfies(a,p) }; // conversion of a formula into its conjunctive normal form fun CNF(p:Prop):NormalForm ensures equivCNF(p,result); = { negate(assignment(a)) | a:Ass with ¬satisfies(a,p) }; // ----------------------------------------------------------------- // end of file // -----------------------------------------------------------------