@@ -24,6 +24,106 @@ python3 -m pip install -e .
2424./install.sh # install extra solvers
2525```
2626
27+ ## Getting Started
28+
29+ ``` python
30+ import kdrag as kd
31+ import kdrag.smt as smt # smt is literally a reexporting of z3
32+
33+ # Anything Z3 can do on it's own, we can "prove" with no extra work
34+ p,q = smt.Bools(" p q" )
35+ simple_taut = kd.lemma(smt.Implies(p, smt.Or(p, q)))
36+
37+ # The returned objects are `Proof`, not smt.ExprRef` formulas
38+ assert kd.kernel.is_proof(simple_taut)
39+ assert not isinstance (simple_taut, smt.ExprRef)
40+
41+ try :
42+ false_lemma = kd.lemma(smt.Implies(p, smt.And(p, q)))
43+ assert False # This will not be reached
44+ except kd.kernel.LemmaError as e:
45+ pass
46+
47+ # Z3 also supports things like Reals, Ints, BitVectors and strings
48+ x = smt.Real(" x" )
49+ real_trich = kd.lemma(smt.ForAll([x], smt.Or(x < 0 , x == 0 , 0 < x)))
50+
51+ x = smt.BitVec(" x" , 32 )
52+ or_idem = kd.lemma(smt.ForAll([x], x | x == x))
53+
54+ # ###############
55+
56+ # Knuckledragger also support algebraic datatypes and induction
57+ Nat = kd.Inductive(" Nat" , strict = False )
58+ Zero = Nat.declare(" Zero" )
59+ Succ = Nat.declare(" Succ" , (" pred" , Nat))
60+ Nat = Nat.create()
61+
62+ # We can define an addition function
63+ n,m = smt.Consts(" n m" , Nat)
64+ add = smt.Function(" add" , Nat, Nat, Nat)
65+ add = kd.define(" add" , [n,m], kd.cond(
66+ (n.is_Zero, m),
67+ (n.is_Succ, Nat.Succ(add(n.pred, m)))
68+ ))
69+
70+ # There is a notation overloading mechanism modelled after python's singledispatch
71+ kd.notation.add.register(Nat, add)
72+
73+ # The definitional lemma is not available to the solver unless you give it
74+ add_zero_x = kd.lemma(smt.ForAll([n], Nat.Zero + n == n), by = [add.defn])
75+ add_succ_x = kd.lemma(smt.ForAll([n,m], Nat.Succ(n) + m == Nat.Succ(n + m)), by = [add.defn])
76+
77+ # More involved proofs can be more easily done in an interactive tactic
78+ l = kd.Lemma(smt.ForAll([n], n + Nat.Zero == n))
79+ _n = l.fixes()
80+ l.induct(_n)
81+ l.auto(by = [add.defn])
82+ l.auto(by = [add.defn])
83+ add_x_zero = l.qed()
84+
85+ # #############
86+
87+ # But we can also build our own sorts and axiomatic theories.
88+ # https://en.wikipedia.org/wiki/Group_(mathematics)
89+ G = smt.DeclareSort(" G" )
90+ mul = smt.Function(" mul" , G, G, G)
91+ e = smt.Const(" e" , G)
92+ inv = smt.Function(" inv" , G, G)
93+
94+ kd.notation.mul.register(G, mul)
95+
96+ x, y, z = smt.Consts(" x y z" , G)
97+ mul_assoc = kd.axiom(smt.ForAll([x, y, z], x * (y * z) == (x * y) * z))
98+ id_left = kd.axiom(smt.ForAll([x], e * x == x))
99+ inv_left = kd.axiom(smt.ForAll([x], inv(x) * x == e))
100+
101+ # The Calc tactic can allow one to write explicit equational proofs
102+ c = kd.Calc([x], x * inv(x))
103+ c.eq(e * (x * inv(x)), by = [id_left])
104+ c.eq((inv(inv(x)) * inv(x)) * (x * inv(x)), by = [inv_left])
105+ c.eq(inv(inv(x)) * ((inv(x) * x) * inv(x)), by = [mul_assoc])
106+ c.eq(inv(inv(x)) * (e * inv(x)), by = [inv_left])
107+ c.eq(inv(inv(x)) * inv(x), by = [id_left])
108+ c.eq(e, by = [inv_left])
109+ inv_right = c.qed()
110+ ```
111+
112+ For more on using z3py
113+
114+ - < https://ericpony.github.io/z3py-tutorial/guide-examples.htm >
115+ - The z3 guide < https://microsoft.github.io/z3guide/ >
116+ - The z3py [ documentation] ( https://z3prover.github.io/api/html/namespacez3py.html )
117+ - < https://github.com/philzook58/z3_tutorial > ([ video] ( https://www.youtube.com/watch?v=56IIrBZy9Rc&feature=youtu.be&ab_channel=BroadInstitute ) )
118+
119+ For more on interactive theorem proving (This is a lot to take in)
120+
121+ - [ Software Foundations] ( https://softwarefoundations.cis.upenn.edu/ ) - Coq
122+ - [ Theorem Proving in Lean 4] ( https://lean-lang.org/theorem_proving_in_lean4/title_page.html )
123+ - [ Mathematics in Lean] ( https://leanprover-community.github.io/mathematics_in_lean/ )
124+ - [ Isabelle Tutorial] ( https://isabelle.in.tum.de/documentation.html )
125+ - [ HOL Light Tutorial] ( https://hol-light.github.io/tutorial.pdf )
126+
27127## Blog Posts
28128
29129- [ 'Lean-style' Tactics in Knuckledragger] ( https://www.philipzucker.com/knuckle_lemma/ )
0 commit comments