@@ -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+
3138def 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]
6572theorem 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]
7889theorem 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
0 commit comments