"""Boolean algebra as an equational theory: Huntington's axioms (1904). Operations: x + y (sum), x * y (product), n(x) (complement), constants 0, 1. Every theorem has a proof built with tactics and checked by the kernel. """ from kernel import Kernel from syntax import equation from tactics import calc, rw, prove, variables SIGNATURE = {"+": 2, "*": 2, "n": 1, "0": 0, "1": 0} AXIOMS = [ ("H1", "x + y = y + x"), ("H2", "x * y = y * x"), ("H3", "x + y * z = (x + y) * (x + z)"), ("H4", "x * (y + z) = x * y + x * z"), ("H5", "x + 0 = x"), ("H6", "x * 1 = x"), ("H7", "x + n(x) = 1"), ("H8", "x * n(x) = 0"), ] def theory(): """A new kernel with Huntington's axioms (no theorems yet).""" j = Kernel(SIGNATURE) for name, text in AXIOMS: l, r = equation(text) j.axiom(name, variables(r, variables(l)), l, r) return j # Theorems in order: each may use only the earlier ones. THEOREMS = [ ("add_idem", "x + x = x", calc( "x + x", ("(x + x) * 1", rw("H6")), ("(x + x) * (x + n(x))", rw("H7")), ("x + x * n(x)", rw("H3")), ("x + 0", rw("H8")), ("x", rw("H5")))), ("mul_idem", "x * x = x", calc( "x * x", ("x * x + 0", rw("H5")), ("x * x + x * n(x)", rw("H8")), ("x * (x + n(x))", rw("H4")), ("x * 1", rw("H7")), ("x", rw("H6")))), ("add_one", "x + 1 = 1", calc( "x + 1", ("(x + 1) * 1", rw("H6")), ("1 * (x + 1)", rw("H2")), ("(x + n(x)) * (x + 1)", rw("H7")), ("x + n(x) * 1", rw("H3")), ("x + n(x)", rw("H6")), ("1", rw("H7")))), ("mul_zero", "x * 0 = 0", calc( "x * 0", ("x * 0 + 0", rw("H5")), ("0 + x * 0", rw("H1")), ("x * n(x) + x * 0", rw("H8")), ("x * (n(x) + 0)", rw("H4")), ("x * n(x)", rw("H5")), ("0", rw("H8")))), ("absorb", "x + x * y = x", calc( "x + x * y", ("x * 1 + x * y", rw("H6")), ("x * (1 + y)", rw("H4")), ("x * (y + 1)", rw("H1")), ("x * 1", rw("add_one")), ("x", rw("H6")))), ("compl_compl", "n(n(x)) = x", calc( "n(n(x))", ("n(n(x)) * 1", rw("H6")), ("n(n(x)) * (x + n(x))", rw("H7")), ("n(n(x)) * x + n(n(x)) * n(x)", rw("H4")), ("n(n(x)) * x + 0", rw("H2 n(n(x)) n(x)", "H8")), ("n(n(x)) * x + x * n(x)", rw("H8")), ("x * n(n(x)) + x * n(x)", rw("H2")), ("x * (n(n(x)) + n(x))", rw("H4")), ("x * 1", rw("H1", "H7")), ("x", rw("H6")))), ] def theory_with_theorems(): """A kernel with the axioms and the theorems; returns their proofs too.""" j = theory() proofs = {} for name, text, tactic in THEOREMS: proofs[name] = prove(j, name, text, tactic) return j, proofs