Skip to content

Commit bf2cc75

Browse files
committed
membership
1 parent b97a996 commit bf2cc75

2 files changed

Lines changed: 27 additions & 8 deletions

File tree

DatapathVerification/BitHeap/BitHeap.lean

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ structure AdderResult where
2828
sum : Circuit
2929
carry : Circuit
3030

31+
def get (h : BitHeap) (column : Nat) : Column :=
32+
h.columns.getD column (Column.empty)
33+
34+
instance : Membership Circuit BitHeap where
35+
mem h c :=
36+
∃ (col : Nat), c ∈ h.get col
37+
3138
def removeBit (column : Nat) (c : Circuit) (h : BitHeap) : BitHeap :=
3239
⟨h.columns.modify column (fun col => col.erase c)⟩
3340

@@ -64,33 +71,42 @@ def fullAdder (column : Nat) (i j k : Circuit) (h : BitHeap) : AdderResult :=
6471
@[simp]
6572
theorem eval_heap_addBit (column : Nat) (c : Circuit) (h : BitHeap) (env : BitEnv) :
6673
(h.addBit column c).eval env = h.eval env + 2^column * (c.eval env).toInt := by
67-
simp [BitHeap.eval, BitHeap.addBit]
68-
by_cases h : (h.columns.getD column (Column.empty)).contains c
69-
<;> sorry
74+
sorry
7075

7176
@[simp]
72-
theorem eval_heap_removeBit (column : Nat) (c : Circuit) (h : BitHeap) (env : BitEnv) :
77+
theorem eval_heap_removeBit (column : Nat) (c : Circuit) (h : BitHeap) (env : BitEnv) (h1 : c ∈ h.get column) :
7378
(h.removeBit column c).eval env = h.eval env - 2^(column) * (c.eval env).toInt := by
7479
simp [BitHeap.eval, BitHeap.removeBit]
7580
sorry
7681

82+
@[simp]
83+
theorem get_removeBit_of_ne (column : Nat) (h : BitHeap) (i j : Circuit)
84+
(h1 : i ∈ h.get column) (hne : i ≠ j) :
85+
i ∈ (removeBit column j h).get column := by
86+
sorry
87+
7788
@[simp]
7889
theorem toNat_and (a b : Bool) :
7990
(a && b).toNat = a.toNat * b.toNat := by
8091
cases a <;> cases b <;> simp
8192

82-
theorem halfAdder_correct (column : Nat) (i j : Circuit) (h : BitHeap) :
93+
theorem halfAdder_correct (column : Nat) (i j : Circuit) (h : BitHeap) (h1 : i ∈ h.get column) (h2 : j ∈ h.get column) (hne : i ≠ j):
8394
∀ (env : BitEnv), (h.halfAdder column i j).heap.eval env = h.eval env := by
8495
intros env
85-
simp [halfAdder]
96+
have h3 := get_removeBit_of_ne column h j i h2 hne.symm
97+
simp [halfAdder, h1, h3]
8698
generalize hvi : i.eval env = vi
8799
generalize hvj : j.eval env = vj
88100
rcases vi <;> rcases vj <;> grind
89101

90-
theorem fullAdder_correct (column : Nat) (i j k : Circuit) (h : BitHeap) :
102+
theorem fullAdder_correct (column : Nat) (i j k : Circuit) (h : BitHeap)
103+
(h1 : i ∈ h.get column) (h2 : j ∈ h.get column) (h3 : k ∈ h.get column) (hne : i ≠ j) (hne2 : i ≠ k) (hne3 : j ≠ k) :
91104
∀ (env : BitEnv), (h.fullAdder column i j k).heap.eval env = h.eval env := by
92105
intros env
93-
simp [fullAdder]
106+
have h4 := get_removeBit_of_ne column h j i h2 hne.symm
107+
have h5 := get_removeBit_of_ne column (removeBit column i h) k
108+
have h6 := h5 j (get_removeBit_of_ne column h k i h3 hne2.symm) hne3.symm
109+
simp [fullAdder, h1, h4, h6]
94110
generalize hvi : i.eval env = vi
95111
generalize hvj : j.eval env = vj
96112
generalize hvk : k.eval env = vk

DatapathVerification/BitHeap/Column.lean

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ namespace Column
1717
def contains (col : Column) (c : Circuit) : Bool :=
1818
col.elems.contains c
1919

20+
instance : Membership Circuit Column where
21+
mem col c := c ∈ col.elems
22+
2023
def empty : Column := ⟨Std.HashSet.emptyWithCapacity 0
2124

2225
def eval (col : Column) (env : BitEnv) : Nat :=

0 commit comments

Comments
 (0)