1 | include "basics/types.ma". |
---|
2 | include "ASM/BitVector.ma". |
---|
3 | |
---|
4 | inductive BitVectorTrie (A: Type[0]): nat → Type[0] ≝ |
---|
5 | Leaf: A → BitVectorTrie A O |
---|
6 | | Node: ∀n: nat. BitVectorTrie A n → BitVectorTrie A n → BitVectorTrie A (S n) |
---|
7 | | Stub: ∀n: nat. BitVectorTrie A n. |
---|
8 | |
---|
9 | let rec fold (A, B: Type[0]) (n: nat) (f: BitVector n → A → B → B) |
---|
10 | (t: BitVectorTrie A n) (b: B) on t: B ≝ |
---|
11 | (match t return λx.λ_.x = n → B with |
---|
12 | [ Leaf l ⇒ λ_.f (zero ?) l b |
---|
13 | | Node h l r ⇒ λK. |
---|
14 | fold A B h (λx.f ((VCons ? h false x)⌈(S h) ↦ n⌉)) l |
---|
15 | (fold A B h (λx.f ((VCons ? h true x)⌈(S h) ↦ n⌉)) r b) |
---|
16 | | Stub _ ⇒ λ_.b |
---|
17 | ]) (refl ? n). |
---|
18 | @K |
---|
19 | qed. |
---|
20 | |
---|
21 | lemma Sm_leq_n_m_leq_n: |
---|
22 | ∀m, n: nat. |
---|
23 | S m ≤ n → m ≤ n. |
---|
24 | #m #n /2/ |
---|
25 | qed. |
---|
26 | |
---|
27 | let rec bvtfold_aux |
---|
28 | (a, b: Type[0]) (f: BitVector 16 → a → b → b) (seed: b) (n: nat) |
---|
29 | on n: n ≤ 16 → BitVectorTrie a n → BitVector (16 - n) → b ≝ |
---|
30 | match n return λn: nat. n ≤ 16 → BitVectorTrie a n → BitVector (16 - n) → b with |
---|
31 | [ O ⇒ λinvariant: 0 ≤ 16. λtrie: BitVectorTrie a 0. λpath: BitVector 16. |
---|
32 | match trie return λx: nat. λtrie': BitVectorTrie a x. ∀prf: x = 0. b with |
---|
33 | [ Leaf l ⇒ λproof. f path l seed |
---|
34 | | Stub s ⇒ λproof. seed |
---|
35 | | Node n' l r ⇒ λabsrd. ⊥ |
---|
36 | ] (refl … 0) |
---|
37 | | S n' ⇒ λinvariant: S n' ≤ 16. λtrie: BitVectorTrie a (S n'). λpath: BitVector (16 - S n'). |
---|
38 | match trie return λx: nat. λtrie': BitVectorTrie a x. ∀prf: x = S n'. b with |
---|
39 | [ Leaf l ⇒ λabsrd. ⊥ |
---|
40 | | Stub s ⇒ λproof. seed |
---|
41 | | Node n'' l r ⇒ λproof. |
---|
42 | bvtfold_aux a b f (bvtfold_aux a b f seed n' ? (l⌈BitVectorTrie a n'' ↦ BitVectorTrie a n'⌉) ((false:::path)⌈S (16 - S n') ↦ 16 - n'⌉)) n' ? (r⌈BitVectorTrie a n'' ↦ BitVectorTrie a n'⌉) ((true:::path)⌈S (16 - S n') ↦ 16 - n'⌉) |
---|
43 | ] (refl … (S n')) |
---|
44 | ]. |
---|
45 | [ 1, 2: destruct(absrd) |
---|
46 | | 3,8: >minus_S_S <minus_Sn_m // @le_S_S_to_le // |
---|
47 | | 4,7: destruct(proof) % |
---|
48 | | 5,6: @Sm_leq_n_m_leq_n // ] |
---|
49 | qed. |
---|
50 | |
---|
51 | (* these two can probably be generalized w/r/t the second type and |
---|
52 | * some sort of equality relationship *) |
---|
53 | lemma fold_eq: |
---|
54 | ∀A: Type[0]. |
---|
55 | ∀n: nat. |
---|
56 | ∀f. |
---|
57 | ∀t. |
---|
58 | ∀P, Q: Prop. |
---|
59 | (P → Q) → (∀a,t',P,Q.(P → Q) → f a t' P → f a t' Q) → fold A ? n f t P → fold A ? n f t Q. |
---|
60 | #A #n #f #t #P #Q #H |
---|
61 | generalize in match (refl ? n); generalize in match H; -H; generalize in match Q; -Q; generalize in match P; -P; |
---|
62 | elim t in f ⊢ (? → ? → ? → ???% → ? → ???%%%? → ???%%%?); |
---|
63 | [ #a #f #P #Q #HPQ #_ #Hf #HP whd in HP; whd @(Hf (zero 0) a P Q HPQ HP) |
---|
64 | | #h #l #r #Hl #Hr #f #P #Q #HPQ #_ #Hf #HP normalize normalize in HP; @(Hl ? (fold A Prop h (λx.f (true:::x)) r P) (fold A Prop h (λx.f (true:::x)) r Q) ? (refl ? h) ?) |
---|
65 | [ @(Hr ? P Q HPQ (refl ? h) ?) |
---|
66 | #a #t' #X #Y #HXY #Hff @(Hf (true:::a) t' X Y HXY Hff) |
---|
67 | | #a #t' #X #Y #HXY #Hff @(Hf (false:::a) t' X Y HXY Hff) ] |
---|
68 | | #h #f #P #Q #HPQ #_ #Hf #HP whd in HP; whd @(HPQ HP) ] |
---|
69 | @HP |
---|
70 | qed. |
---|
71 | |
---|
72 | lemma fold_init: |
---|
73 | ∀A:Type[0]. |
---|
74 | ∀n:nat. |
---|
75 | ∀f. |
---|
76 | ∀t. |
---|
77 | ∀P: Prop. |
---|
78 | (∀a,t',P.f a t' P → P) → fold A Prop n f t P → P. |
---|
79 | #A #n #f #t #P #H generalize in match (refl ? n); generalize in match H; -H; generalize in match P; -P; |
---|
80 | elim t in f ⊢ (? → ? → ???% → ???%%%? → ?); -t |
---|
81 | [ #a #f #P #Hf #_ normalize @(Hf [[]]) |
---|
82 | | #h #l #r #Hl #Hr #f #P #Hf #_ normalize #HP @(Hr (λx.f (true:::x))) |
---|
83 | [ #a #t' #X @(Hf (true:::a) t' X) | @(refl ? h) | @(Hl (λx.f (false:::x))) |
---|
84 | [ #a #t' #X @(Hf (false:::a) t' X) | @(refl ? h) | @HP ] |
---|
85 | ] |
---|
86 | | #h #f #P #Hf #_ normalize // |
---|
87 | |
---|
88 | |
---|
89 | ] |
---|
90 | qed. |
---|
91 | |
---|
92 | definition forall |
---|
93 | ≝ |
---|
94 | λA.λn.λt:BitVectorTrie A n.λP.fold ? ? ? (λk.λa.λacc.(P k a) ∧ acc) t True. |
---|
95 | |
---|
96 | lemma forall_nodel: |
---|
97 | ∀A:Type[0]. |
---|
98 | ∀n:nat. |
---|
99 | ∀l,r. |
---|
100 | ∀P:BitVector (S n) → A → Prop. |
---|
101 | forall A (S n) (Node ? n l r) P → forall A n l (λx.λa.P (false:::x) a). |
---|
102 | #A #n #l #r #P #Hl |
---|
103 | whd @(fold_eq A n ? ? (fold A ? n (λk.λa.λacc.P (true:::k) a∧acc) r True) True) |
---|
104 | [ // |
---|
105 | | #n #t' #X #Y #HXY #HX %1 |
---|
106 | [ @(proj1 ? ? HX) | @HXY @(proj2 ? ? HX) ] |
---|
107 | | whd in Hl; @Hl ] |
---|
108 | qed. |
---|
109 | |
---|
110 | lemma forall_noder: |
---|
111 | ∀A:Type[0]. |
---|
112 | ∀n:nat. |
---|
113 | ∀l,r. |
---|
114 | ∀P:BitVector (S n) → A → Prop. |
---|
115 | forall A (S n) (Node ? n l r) P → forall A n r (λx.λa.P (true:::x) a). |
---|
116 | #A #n #l #r #P #Hr |
---|
117 | whd @(fold_init A n (λk.λa.λacc.P (false:::k) a∧acc) l) |
---|
118 | [ #n #t' #P #HP @(proj2 ? ? HP) |
---|
119 | | @Hr |
---|
120 | ] |
---|
121 | qed. |
---|
122 | |
---|
123 | lemma forall_node: |
---|
124 | ∀A.∀n.∀l,r.∀P:BitVector (S n) → A → Prop. |
---|
125 | forall A n l (λx.λa.P (false:::x) a) → forall A n r (λx.λa.P (true:::x) a) → |
---|
126 | forall A (S n) (Node ? n l r) P. |
---|
127 | #A #n #l #r #P #Hl #Hr |
---|
128 | normalize @(fold_eq … True) |
---|
129 | [ #_ @Hr |
---|
130 | | #x #t' #X #Y #HXY #HP %1 [ @(proj1 … HP) | @HXY @(proj2 … HP) ] |
---|
131 | | @Hl |
---|
132 | ] |
---|
133 | qed. |
---|
134 | |
---|
135 | let rec lookup_opt (A: Type[0]) (n: nat) |
---|
136 | (b: BitVector n) (t: BitVectorTrie A n) on t |
---|
137 | : option A ≝ |
---|
138 | (match t return λx.λ_. BitVector x → option A with |
---|
139 | [ Leaf l ⇒ λ_.Some ? l |
---|
140 | | Node h l r ⇒ λb. lookup_opt A ? (tail … b) (if head' … b then r else l) |
---|
141 | | Stub _ ⇒ λ_.None ? |
---|
142 | ]) b. |
---|
143 | |
---|
144 | definition member ≝ |
---|
145 | λA. |
---|
146 | λn. |
---|
147 | λb: BitVector n. |
---|
148 | λt: BitVectorTrie A n. |
---|
149 | match lookup_opt A n b t with |
---|
150 | [ None ⇒ false |
---|
151 | | _ ⇒ true |
---|
152 | ]. |
---|
153 | |
---|
154 | definition member_p ≝ |
---|
155 | λA. |
---|
156 | λn. |
---|
157 | λb: BitVector n. |
---|
158 | λt: BitVectorTrie A n. |
---|
159 | match lookup_opt A n b t with |
---|
160 | [ None ⇒ False |
---|
161 | | _ ⇒ True |
---|
162 | ]. |
---|
163 | |
---|
164 | lemma forall_lookup: |
---|
165 | ∀A. |
---|
166 | ∀n. |
---|
167 | ∀t:BitVectorTrie A n. |
---|
168 | ∀P:BitVector n → A → Prop. |
---|
169 | forall A n t P → ∀a:A.∀b.lookup_opt A n b t = Some ? a → P b a. |
---|
170 | #A #n #t #P generalize in match (refl ? n); elim t in P ⊢ (???% → ??%%? → ? → ? → ??(??%%%)? → ?); |
---|
171 | [ #x #f #_ #Hf #a #b whd in Hf; #Hb normalize in Hb; destruct >(BitVector_O b) @(proj1 ? ? Hf) |
---|
172 | | #h #l #r #Hl #Hr #f #_ #Hf #a #b #Hb cases (BitVector_Sn h b) |
---|
173 | #hd #bla elim bla -bla #tl #Htl >Htl in Hb; #Hb cases hd in Hb; |
---|
174 | [ #Hb normalize in Hb; @(Hr (λx.λa.f (true:::x) a) (refl ? h)) |
---|
175 | [ @(forall_noder A h l r f Hf) |
---|
176 | | @Hb |
---|
177 | ] |
---|
178 | | #Hb normalize in Hb; @(Hl (λx.λa.f (false:::x) a) (refl ? h)) |
---|
179 | [ @(forall_nodel A h l r f Hf) |
---|
180 | | @Hb |
---|
181 | ] |
---|
182 | ] |
---|
183 | | #n #f #_ #Hf #a #b #Hb normalize in Hb; destruct |
---|
184 | qed. |
---|
185 | |
---|
186 | lemma lookup_forall: |
---|
187 | ∀A:Type[0].∀n.∀t:BitVectorTrie A n.∀P:BitVector n → A → Prop. |
---|
188 | (∀a:A.∀b:BitVector n.lookup_opt A n b t = Some ? a → P b a) → forall A n t P. |
---|
189 | #A #n #t elim t |
---|
190 | [ #x #P #HP normalize %1 [ @HP normalize @refl | // ] |
---|
191 | | #h #l #r #Hl #Hr #P #HP @forall_node |
---|
192 | [ @Hl #a #b #Hlookup @HP normalize @Hlookup |
---|
193 | | @Hr #a #b #Hlookup @HP normalize @Hlookup |
---|
194 | ] |
---|
195 | | #n #P #HP normalize // |
---|
196 | ] |
---|
197 | qed. |
---|
198 | |
---|
199 | let rec lookup (A: Type[0]) (n: nat) |
---|
200 | (b: BitVector n) (t: BitVectorTrie A n) (a: A) on b |
---|
201 | : A ≝ |
---|
202 | (match b return λx.λ_. x = n → A with |
---|
203 | [ VEmpty ⇒ |
---|
204 | (match t return λx.λ_. O = x → A with |
---|
205 | [ Leaf l ⇒ λ_.l |
---|
206 | | Node h l r ⇒ λK.⊥ |
---|
207 | | Stub s ⇒ λ_.a |
---|
208 | ]) |
---|
209 | | VCons o hd tl ⇒ |
---|
210 | match t return λx.λ_. (S o) = x → A with |
---|
211 | [ Leaf l ⇒ λK.⊥ |
---|
212 | | Node h l r ⇒ |
---|
213 | match hd with |
---|
214 | [ true ⇒ λK. lookup A h (tl⌈o ↦ h⌉) r a |
---|
215 | | false ⇒ λK. lookup A h (tl⌈o ↦ h⌉) l a |
---|
216 | ] |
---|
217 | | Stub s ⇒ λ_. a] |
---|
218 | ]) (refl ? n). |
---|
219 | [1,2: |
---|
220 | destruct |
---|
221 | |*: |
---|
222 | @ injective_S |
---|
223 | // |
---|
224 | ] |
---|
225 | qed. |
---|
226 | |
---|
227 | alias id "bvt_lookup" = "cic:/matita/cerco/ASM/BitVectorTrie/lookup.fix(0,2,5)". |
---|
228 | |
---|
229 | let rec prepare_trie_for_insertion (A: Type[0]) (n: nat) (b: BitVector n) (a:A) on b : BitVectorTrie A n ≝ |
---|
230 | match b with |
---|
231 | [ VEmpty ⇒ Leaf A a |
---|
232 | | VCons o hd tl ⇒ |
---|
233 | match hd with |
---|
234 | [ true ⇒ Node A o (Stub A o) (prepare_trie_for_insertion A o tl a) |
---|
235 | | false ⇒ Node A o (prepare_trie_for_insertion A o tl a) (Stub A o) |
---|
236 | ] |
---|
237 | ]. |
---|
238 | |
---|
239 | let rec insert (A: Type[0]) (n: nat) (b: BitVector n) (a: A) on b: BitVectorTrie A n → BitVectorTrie A n ≝ |
---|
240 | (match b with |
---|
241 | [ VEmpty ⇒ λ_. Leaf A a |
---|
242 | | VCons o hd tl ⇒ λt. |
---|
243 | match t return λy.λ_. S o = y → BitVectorTrie A (S o) with |
---|
244 | [ Leaf l ⇒ λprf.⊥ |
---|
245 | | Node p l r ⇒ λprf. |
---|
246 | match hd with |
---|
247 | [ true ⇒ Node A o (l⌈p ↦ o⌉) (insert A o tl a (r⌈p ↦ o⌉)) |
---|
248 | | false ⇒ Node A o (insert A o tl a (l⌈p ↦ o⌉)) (r⌈p ↦ o⌉) |
---|
249 | ] |
---|
250 | | Stub p ⇒ λprf. (prepare_trie_for_insertion A ? (hd:::tl) a) |
---|
251 | ] (refl ? (S o)) |
---|
252 | ]). |
---|
253 | [ destruct |
---|
254 | |*: |
---|
255 | @ injective_S |
---|
256 | // |
---|
257 | ] |
---|
258 | qed. |
---|
259 | |
---|
260 | alias id "bvt_insert" = "cic:/matita/cerco/ASM/BitVectorTrie/insert.fix(0,2,5)". |
---|
261 | |
---|
262 | let rec update (A: Type[0]) (n: nat) (b: BitVector n) (a: A) on b: BitVectorTrie A n → option (BitVectorTrie A n) ≝ |
---|
263 | (match b with |
---|
264 | [ VEmpty ⇒ λt. match t return λy.λ_. O = y → option (BitVectorTrie A O) with |
---|
265 | [ Leaf _ ⇒ λ_. Some ? (Leaf A a) |
---|
266 | | Stub _ ⇒ λ_. None ? |
---|
267 | | Node _ _ _ ⇒ λprf. ⊥ |
---|
268 | ] (refl ? O) |
---|
269 | | VCons o hd tl ⇒ λt. |
---|
270 | match t return λy.λ_. S o = y → option (BitVectorTrie A (S o)) with |
---|
271 | [ Leaf l ⇒ λprf.⊥ |
---|
272 | | Node p l r ⇒ λprf. |
---|
273 | match hd with |
---|
274 | [ true ⇒ option_map ?? (λv. Node A o (l⌈p ↦ o⌉) v) (update A o tl a (r⌈p ↦ o⌉)) |
---|
275 | | false ⇒ option_map ?? (λv. Node A o v (r⌈p ↦ o⌉)) (update A o tl a (l⌈p ↦ o⌉)) |
---|
276 | ] |
---|
277 | | Stub p ⇒ λprf. None ? |
---|
278 | ] (refl ? (S o)) |
---|
279 | ]). |
---|
280 | [ 1,2: destruct |
---|
281 | |*: |
---|
282 | @ injective_S @sym_eq @prf |
---|
283 | ] |
---|
284 | qed. |
---|
285 | |
---|
286 | let rec merge (A: Type[0]) (n: nat) (b: BitVectorTrie A n) on b: BitVectorTrie A n → BitVectorTrie A n ≝ |
---|
287 | match b return λx. λ_. BitVectorTrie A x → BitVectorTrie A x with |
---|
288 | [ Stub _ ⇒ λc. c |
---|
289 | | Leaf l ⇒ λc. match c with [ Leaf a ⇒ Leaf ? a | _ ⇒ Leaf ? l ] |
---|
290 | | Node p l r ⇒ |
---|
291 | λc. |
---|
292 | (match c return λx. λ_. x = (S p) → BitVectorTrie A (S p) with |
---|
293 | [ Node p' l' r' ⇒ λprf. Node ? ? (merge ?? l (l'⌈p' ↦ p⌉)) (merge ?? r (r'⌈p' ↦ p⌉)) |
---|
294 | | Stub _ ⇒ λprf. Node ? p l r |
---|
295 | | Leaf _ ⇒ λabsd. ? |
---|
296 | ] (refl ? (S p))) |
---|
297 | ]. |
---|
298 | [1: |
---|
299 | destruct(absd) |
---|
300 | |2,3: |
---|
301 | @ injective_S |
---|
302 | assumption |
---|
303 | ] |
---|
304 | qed. |
---|
305 | |
---|
306 | lemma BitVectorTrie_O: |
---|
307 | ∀A:Type[0].∀v:BitVectorTrie A 0.(∃w. v ≃ Leaf A w) ∨ v ≃ Stub A 0. |
---|
308 | #A #v generalize in match (refl … O); cases v in ⊢ (??%? → (?(??(λ_.?%%??)))(?%%??)); |
---|
309 | [ #w #_ %1 %[@w] % |
---|
310 | | #n #l #r #abs @⊥ destruct(abs) |
---|
311 | | #n #EQ %2 >EQ %] |
---|
312 | qed. |
---|
313 | |
---|
314 | lemma BitVectorTrie_Sn: |
---|
315 | ∀A:Type[0].∀n.∀v:BitVectorTrie A (S n).(∃l,r. v ≃ Node A n l r) ∨ v ≃ Stub A (S n). |
---|
316 | #A #n #v generalize in match (refl … (S n)); cases v in ⊢ (??%? → (?(??(λ_.??(λ_.?%%??))))%); |
---|
317 | [ #m #abs @⊥ destruct(abs) |
---|
318 | | #m #l #r #EQ %1 <(injective_S … EQ) %[@l] %[@r] // |
---|
319 | | #m #EQ %2 // ] |
---|
320 | qed. |
---|
321 | |
---|
322 | lemma lookup_prepare_trie_for_insertion_hit: |
---|
323 | ∀A:Type[0].∀a,v:A.∀n.∀b:BitVector n. |
---|
324 | lookup … b (prepare_trie_for_insertion … b v) a = v. |
---|
325 | #A #a #v #n #b elim b // #m #hd #tl #IH cases hd normalize // |
---|
326 | qed. |
---|
327 | |
---|
328 | lemma lookup_insert_hit: |
---|
329 | ∀A:Type[0].∀a,v:A.∀n.∀b:BitVector n.∀t:BitVectorTrie A n. |
---|
330 | lookup … b (insert … b v t) a = v. |
---|
331 | #A #a #v #n #b elim b -b -n // |
---|
332 | #n #hd #tl #IH #t cases(BitVectorTrie_Sn … t) |
---|
333 | [ * #l * #r #JMEQ >JMEQ cases hd normalize // |
---|
334 | | #JMEQ >JMEQ cases hd normalize @lookup_prepare_trie_for_insertion_hit ] |
---|
335 | qed. |
---|
336 | |
---|
337 | lemma lookup_prepare_trie_for_insertion_miss: |
---|
338 | ∀A:Type[0].∀a,v:A.∀n.∀c,b:BitVector n. |
---|
339 | (notb (eq_bv ? b c)) → lookup … b (prepare_trie_for_insertion … c v) a = a. |
---|
340 | #A #a #v #n #c elim c |
---|
341 | [ #b >(BitVector_O … b) normalize #abs @⊥ // |
---|
342 | | #m #hd #tl #IH #b cases(BitVector_Sn … b) #hd' * #tl' #JMEQ >JMEQ |
---|
343 | cases hd cases hd' normalize |
---|
344 | [2,3: #_ cases tl' // |
---|
345 | |*: change with (bool_to_Prop (notb (eq_bv ???)) → ?) /2/ ]] |
---|
346 | qed. |
---|
347 | |
---|
348 | lemma lookup_insert_miss: |
---|
349 | ∀A:Type[0].∀a,v:A.∀n.∀c,b:BitVector n.∀t:BitVectorTrie A n. |
---|
350 | (notb (eq_bv ? b c)) → lookup … b (insert … c v t) a = lookup … b t a. |
---|
351 | #A #a #v #n #c elim c -c -n |
---|
352 | [ #b #t #DIFF @⊥ whd in DIFF; >(BitVector_O … b) in DIFF; // |
---|
353 | | #n #hd #tl #IH #b cases(BitVector_Sn … b) #hd' * #tl' #JMEQ >JMEQ |
---|
354 | #t cases(BitVectorTrie_Sn … t) |
---|
355 | [ * #l * #r #JMEQ >JMEQ cases hd cases hd' #H normalize in H; |
---|
356 | [1,4: change with (bool_to_Prop (notb (eq_bv ???))) in H; ] normalize // @IH // |
---|
357 | | #JMEQ >JMEQ cases hd cases hd' #H normalize in H; |
---|
358 | [1,4: change with (bool_to_Prop (notb (eq_bv ???))) in H; ] normalize |
---|
359 | [3,4: cases tl' // | *: @lookup_prepare_trie_for_insertion_miss //]]] |
---|
360 | qed. |
---|
361 | |
---|
362 | lemma lookup_stub: |
---|
363 | ∀A.∀n.∀b.∀a. |
---|
364 | lookup A n b (Stub A ?) a = a. |
---|
365 | #A #n #b #a cases n in b ⊢ (??(??%%%?)?); |
---|
366 | [ #b >(BitVector_O b) normalize @refl |
---|
367 | | #h #b cases (BitVector_Sn h b) #hd #X elim X -X; #tl #Hb >Hb cases hd |
---|
368 | [ normalize @refl |
---|
369 | | normalize @refl |
---|
370 | ] |
---|
371 | ] |
---|
372 | qed. |
---|
373 | |
---|
374 | lemma lookup_opt_lookup_miss: |
---|
375 | ∀A:Type[0].∀n:nat.∀b:BitVector n.∀t:BitVectorTrie A n. |
---|
376 | lookup_opt A n b t = None A → ∀x.lookup A n b t x = x. |
---|
377 | #A #n #b #t generalize in match (refl ? n); elim t in b ⊢ (???% → ??(??%%%)? → ? → ?); |
---|
378 | [ #a #B #_ #H #x normalize in H; >(BitVector_O B) normalize destruct |
---|
379 | | #h #l #r #Hl #Hr #b #_ #H #x cases (BitVector_Sn h b) #hd #X elim X -X; #tl #Hb |
---|
380 | >Hb >Hb in H; cases hd |
---|
381 | [ normalize #Hlookup @(Hr ? (refl ? h)) @Hlookup |
---|
382 | | normalize #Hlookup @(Hl ? (refl ? h)) @Hlookup |
---|
383 | ] |
---|
384 | | #n #B #_ #H #x @lookup_stub |
---|
385 | ] |
---|
386 | qed. |
---|
387 | |
---|
388 | lemma lookup_opt_lookup_hit: |
---|
389 | ∀A:Type[0].∀n:nat.∀b:BitVector n.∀t:BitVectorTrie A n.∀a:A. |
---|
390 | lookup_opt A n b t = Some A a → ∀x.lookup A n b t x = a. |
---|
391 | #A #n #b #t #a generalize in match (refl ? n); elim t in b ⊢ (???% → ??(??%%%)? → ? → ?); |
---|
392 | [ #a #B #_ #H #x normalize in H; >(BitVector_O B) normalize destruct @refl |
---|
393 | | #h #l #r #Hl #Hr #b #_ #H #x cases (BitVector_Sn h b) #hd #X elim X -X; #tl #Hb |
---|
394 | >Hb >Hb in H; cases hd |
---|
395 | [ normalize #Hlookup @(Hr ? (refl ? h)) @Hlookup |
---|
396 | | normalize #Hlookup @(Hl ? (refl ? h)) @Hlookup |
---|
397 | ] |
---|
398 | | #n #B #_ #H #x normalize in H; destruct |
---|
399 | ] |
---|
400 | qed. |
---|
401 | |
---|
402 | lemma lookup_lookup_opt_hit: |
---|
403 | ∀A.∀n.∀b.∀t.∀x,a. |
---|
404 | lookup A n b t x = a → x ≠ a → lookup_opt A n b t = Some A a. |
---|
405 | #A #n #b #t #x #a generalize in match (refl ? n); elim t in b ⊢ (???% → ? → ?); |
---|
406 | [ #z #B #_ #H #Hx >(BitVector_O B) in H; normalize #H >H @refl |
---|
407 | | #h #l #r #Hl #Hr #B #_ #H #Hx cases (BitVector_Sn h B) #hd #X elim X; -X #tl #HB |
---|
408 | >HB >HB in H; cases hd |
---|
409 | [ normalize #H >(Hr tl (refl ? h) H Hx) @refl |
---|
410 | | normalize #H >(Hl tl (refl ? h) H Hx) @refl |
---|
411 | ] |
---|
412 | | #n #B #_ #H #Hx cases B in H; |
---|
413 | [ normalize #Hx' | #n' #b #v normalize #Hx' ] |
---|
414 | @⊥ @(absurd (eq ? x a)) [1,3: @Hx' |2,4: @Hx ] |
---|
415 | ] |
---|
416 | qed. |
---|
417 | |
---|
418 | lemma lookup_opt_lookup: |
---|
419 | ∀A,n,b,t1,t2,x. |
---|
420 | lookup_opt A n b t1 = lookup_opt A n b t2 → lookup A n b t1 x = lookup A n b t2 x. |
---|
421 | #A #n #b #t1 #t2 #x lapply (refl ? (lookup_opt A n b t2)) |
---|
422 | cases (lookup_opt A n b t2) in ⊢ (???% → %); |
---|
423 | [ #H2 #H1 >(lookup_opt_lookup_miss … H1) >(lookup_opt_lookup_miss … H2) // |
---|
424 | | #y #H2 #H1 >(lookup_opt_lookup_hit … y H1) >(lookup_opt_lookup_hit … y H2) // |
---|
425 | ] |
---|
426 | qed. |
---|
427 | |
---|
428 | lemma lookup_opt_prepare_trie_for_insertion_hit: |
---|
429 | ∀A:Type[0].∀v:A.∀n.∀b:BitVector n. |
---|
430 | lookup_opt … b (prepare_trie_for_insertion … b v) = Some A v. |
---|
431 | #A #v #n #b elim b // #m #hd #tl #IH cases hd normalize // |
---|
432 | qed. |
---|
433 | |
---|
434 | lemma lookup_opt_prepare_trie_for_insertion_miss: |
---|
435 | ∀A:Type[0].∀v:A.∀n.∀c,b:BitVector n. |
---|
436 | (notb (eq_bv ? b c)) → lookup_opt … b (prepare_trie_for_insertion … c v) = None ?. |
---|
437 | #A #v #n #c elim c |
---|
438 | [ #b >(BitVector_O … b) normalize #abs @⊥ // |
---|
439 | | #m #hd #tl #IH #b cases(BitVector_Sn … b) #hd' * #tl' #JMEQ >JMEQ |
---|
440 | cases hd cases hd' normalize |
---|
441 | [2,3: #_ cases tl' // |
---|
442 | |*: change with (bool_to_Prop (notb (eq_bv ???)) → ?) @IH ]] |
---|
443 | qed. |
---|
444 | |
---|
445 | lemma lookup_opt_insert_hit: |
---|
446 | ∀A:Type[0].∀v:A.∀n.∀b:BitVector n.∀t:BitVectorTrie A n. |
---|
447 | lookup_opt … b (insert … b v t) = Some A v. |
---|
448 | #A #v #n #b #t elim t in b ⊢ (??(??%%%)?); |
---|
449 | [ #x #b >(BitVector_O b) normalize @refl |
---|
450 | | #h #l #r #Hl #Hr #b cases (BitVector_Sn h b) #hd #X elim X -X; #tl #Hb >Hb cases hd |
---|
451 | [ normalize @Hr |
---|
452 | | normalize @Hl |
---|
453 | ] |
---|
454 | | #n' #b cases n' in b ⊢ ?; |
---|
455 | [ #b >(BitVector_O b) normalize @refl |
---|
456 | | #m #b cases (BitVector_Sn m b) #hd #X elim X -X; #tl #Hb >Hb cases hd |
---|
457 | normalize @lookup_opt_prepare_trie_for_insertion_hit |
---|
458 | ] |
---|
459 | ] |
---|
460 | qed. |
---|
461 | |
---|
462 | lemma lookup_opt_insert_miss: |
---|
463 | ∀A:Type[0].∀v:A.∀n.∀c,b:BitVector n.∀t:BitVectorTrie A n. |
---|
464 | (notb (eq_bv ? b c)) → lookup_opt … b (insert … c v t) = lookup_opt … b t. |
---|
465 | #A #v #n #c elim c -c -n |
---|
466 | [ #b #t #DIFF @⊥ whd in DIFF; >(BitVector_O … b) in DIFF; // |
---|
467 | | #n #hd #tl #IH #b cases(BitVector_Sn … b) #hd' * #tl' #JMEQ >JMEQ |
---|
468 | #t cases(BitVectorTrie_Sn … t) |
---|
469 | [ * #l * #r #JMEQ >JMEQ cases hd cases hd' #H normalize in H; |
---|
470 | [1,4: change with (bool_to_Prop (notb (eq_bv ???))) in H; ] normalize // @IH // |
---|
471 | | #JMEQ >JMEQ cases hd cases hd' #H normalize in H; |
---|
472 | [1,4: change with (bool_to_Prop (notb (eq_bv ???))) in H; ] normalize |
---|
473 | [3,4: cases tl' // | *: @lookup_opt_prepare_trie_for_insertion_miss //]]] |
---|
474 | qed. |
---|
475 | |
---|
476 | lemma insert_lookup_opt: |
---|
477 | ∀A:Type[0].∀v,a:A.∀n.∀c,b:BitVector n.∀t:BitVectorTrie A n. |
---|
478 | lookup_opt … b (insert … c v t) = Some A a → lookup_opt … b t = Some A a ∨ a = v. |
---|
479 | #A #v #a #n #c elim c -c; -n; |
---|
480 | [ #b #t #Hl normalize in Hl; %2 destruct (Hl) @refl |
---|
481 | | #n #hd #tl #Hind #b cases (BitVector_Sn … b) #hd' * #tl' #Heq >Heq |
---|
482 | #t cases (BitVectorTrie_Sn … t) |
---|
483 | [ * #l * #r #Heq2 >Heq2 cases hd cases hd' #H normalize in H; normalize |
---|
484 | [1,4: @(Hind tl' ? H) |
---|
485 | |2,3: %1 @H |
---|
486 | ] |
---|
487 | | #Heq2 >Heq2 cases hd cases hd' #H normalize in H; normalize |
---|
488 | [1,4: lapply (refl ? (eq_bv ? tl' tl)) cases (eq_bv ? tl' tl) in ⊢ (???% → %); #Heq3 |
---|
489 | [1,3: >(eq_bv_eq … Heq3) in H; >lookup_opt_prepare_trie_for_insertion_hit #X destruct (X) %2 // |
---|
490 | |2,4: >(lookup_opt_prepare_trie_for_insertion_miss) in H; |
---|
491 | [1,3: #X %1 // |
---|
492 | |2,4: >Heq3 // |
---|
493 | ] |
---|
494 | ] |
---|
495 | |2,3: destruct (H) |
---|
496 | ] |
---|
497 | qed. |
---|
498 | |
---|
499 | lemma forall_insert_inv1: |
---|
500 | ∀A.∀n.∀b.∀a.∀t.∀P. |
---|
501 | forall A n (insert A n b a t) P → P b a. |
---|
502 | #A #n #b #a #t #P #H @(forall_lookup ? ? (insert A n b a t)) |
---|
503 | [ @H |
---|
504 | | >(lookup_opt_insert_hit A ? n b) @(refl ? (Some A a)) |
---|
505 | ] |
---|
506 | qed. |
---|
507 | |
---|
508 | lemma forall_insert_inv2a: |
---|
509 | ∀A:Type[0].∀n:nat.∀b.∀a.∀t.∀P. |
---|
510 | lookup_opt A n b t = (None A) → forall A n (insert A n b a t) P → forall A n t P. |
---|
511 | #A #n #b #a #t generalize in match (refl ? n); elim t in b ⊢ (???% → ? → ??(??%%%)? → ??%%% → ??%%%); |
---|
512 | [ #x #b #_ #P >(BitVector_O b) normalize #H destruct |
---|
513 | | #h #l #r #Hl #Hr #b #_ #P cases (BitVector_Sn h b) #hd #X elim X -X; #tl #Hb >Hb cases hd #Hlookup #H |
---|
514 | [ normalize in H; normalize |
---|
515 | @(fold_eq … (fold A ? ? (λx.λa0.λacc.P (true:::x) a0∧acc) (insert … tl a r) True) … H) |
---|
516 | [ #Hfold @(Hr tl (refl ? h) ? Hlookup Hfold) |
---|
517 | | #x #t' #X #Y #HXY #HP %1 [ @(proj1 ? ? HP) | @(HXY (proj2 ? ?HP)) ] |
---|
518 | ] |
---|
519 | | normalize in H; normalize |
---|
520 | @(fold_eq … True) |
---|
521 | [ #_ @(fold_init A h (λx.λa0.λacc.P (false:::x) a0 ∧ acc) (insert A h tl a l)) |
---|
522 | [ #z #t' #X #HX @(proj2 ? ? HX) |
---|
523 | | @H ] |
---|
524 | | #z #t' #X #Y #HXY #HP %1 [ @(proj1 ? ? HP) | @(HXY (proj2 ? ? HP)) ] |
---|
525 | | @(Hl tl (refl ? h) ? Hlookup) normalize |
---|
526 | @(fold_eq … (fold A ? ? (λx.λa0.λacc.P (true:::x) a0∧acc) r True)) |
---|
527 | [ // |
---|
528 | | #z #t' #X #Y #HXY #HP %1 [ @(proj1 ? ? HP) | @(HXY (proj2 ? ? HP)) ] |
---|
529 | | @H |
---|
530 | ] |
---|
531 | ] |
---|
532 | ] |
---|
533 | | #n #b #_ #P #Hlookup #Hf normalize // ] |
---|
534 | qed. |
---|
535 | |
---|
536 | lemma forall_insert_inv2b: |
---|
537 | ∀A:Type[0].∀n:nat.∀b:BitVector n.∀a:A.∀t.∀P:(BitVector n → A → Prop). |
---|
538 | (∀x.(lookup_opt A n b t = Some A x) → P b x) → forall A n (insert A n b a t) P → forall A n t P. |
---|
539 | #A #n #b #a #t generalize in match (refl ? n); elim t in b ⊢ (???% → % → ? → ??%%% → ?); |
---|
540 | [ #x #b #_ #P >(BitVector_O b) normalize #HP #Hf %1 [ @HP @refl | @(proj2 ? ? Hf) ] |
---|
541 | | #h #l #r #Hl #Hr #b #_ cases (BitVector_Sn h b) #hd #X elim X -X; #tl #Hb >Hb cases hd #P #HP #Hf |
---|
542 | [ normalize in Hf; normalize |
---|
543 | @(fold_eq … (fold A ? ? (λx.λa0.λacc.P (true:::x) a0∧acc) (insert … tl a r) True) … Hf) |
---|
544 | [ #Hfold @(Hr tl (refl ? h) ? HP Hfold) |
---|
545 | | #x #t' #X #Y #HXY #HP %1 [ @(proj1 ? ? HP) | @(HXY (proj2 ? ? HP)) ] |
---|
546 | ] |
---|
547 | | normalize in H; normalize |
---|
548 | @(fold_eq … True) |
---|
549 | [ #_ @(fold_init A h (λx.λa0.λacc.P (false:::x) a0 ∧ acc) (insert A h tl a l)) |
---|
550 | [ #z #t' #X #HX @(proj2 ? ? HX) |
---|
551 | | @Hf ] |
---|
552 | | #z #t' #X #Y #HXY #HP %1 [ @(proj1 ? ? HP) | @(HXY (proj2 ? ? HP)) ] |
---|
553 | | @(Hl tl (refl ? h) ? HP) normalize |
---|
554 | @(fold_eq … (fold A ? ? (λx.λa0.λacc.P (true:::x) a0∧acc) r True)) |
---|
555 | [ // |
---|
556 | | #z #t' #X #Y #HXY #HP %1 [ @(proj1 ? ? HP) | @(HXY (proj2 ? ? HP)) ] |
---|
557 | | @Hf |
---|
558 | ] |
---|
559 | ] |
---|
560 | ] |
---|
561 | | #n #b #_ #P #Hlookup #Hf normalize // ] |
---|
562 | qed. |
---|
563 | |
---|
564 | lemma forall_prepare_tree_for_insertion: |
---|
565 | ∀A:Type[0].∀h:nat.∀b:BitVector h.∀a:A.∀P. |
---|
566 | P b a → |
---|
567 | forall A h (prepare_trie_for_insertion A h b a) P. |
---|
568 | #A #h #b elim b |
---|
569 | [ #a #P #HP normalize %1 [ @HP | // ] |
---|
570 | | #h #x #tl #Ha #a #P cases x #HP normalize @Ha @HP |
---|
571 | ] |
---|
572 | qed. |
---|
573 | |
---|
574 | lemma forall_insert: |
---|
575 | ∀A:Type[0].∀n:nat.∀b:BitVector n.∀a:A.∀t.∀P. |
---|
576 | forall A n t P → P b a → forall A n (insert A n b a t) P. |
---|
577 | #A #n #b #a #t generalize in match (refl ? n); elim t in b ⊢ (???% → % → ??%%% → %%? → ??%%%); |
---|
578 | [ #x #b #_ #P >(BitVector_O b) normalize #H1 #H2 /2/ |
---|
579 | | #h #l #r #Hl #Hr #b #_ cases (BitVector_Sn h b) #hd #X elim X -X; #tl #Hb >Hb cases hd #P #Hf #HP |
---|
580 | [ normalize in Hf; normalize |
---|
581 | @(fold_eq A … (fold A … (λx.λa0.λacc.P (true:::x) a0∧acc) r True) … Hf) |
---|
582 | [ #Hp @(Hr tl (refl ? h) ? Hp HP) |
---|
583 | | #z #t' #X #Y #HXY #HP %1 [ @(proj1 ? ? HP) | @(HXY (proj2 ? ? HP)) ] |
---|
584 | ] |
---|
585 | | normalize in Hf; normalize |
---|
586 | @(fold_eq … True) |
---|
587 | [ #_ @(fold_init A h (λx.λa0.λacc.P (false:::x) a0∧acc) l) |
---|
588 | [ #z #t' #X #HX @(proj2 ? ? HX) |
---|
589 | | @Hf ] |
---|
590 | | #z #t' #X #Y #HXY #HP %1 [ @(proj1 ? ? HP) | @(HXY (proj2 ? ? HP)) ] |
---|
591 | | @(Hl tl (refl ? h) ? ? HP) |
---|
592 | normalize @(fold_eq … (fold A ? ? (λx.λa0.λacc.P (true:::x) a0∧acc) r True)) |
---|
593 | [ // |
---|
594 | | #z #t' #X #Y #HXY #HP %1 [ @(proj1 ? ? HP) | @(HXY (proj2 ? ? HP)) ] |
---|
595 | | @Hf |
---|
596 | ] |
---|
597 | ] |
---|
598 | ] |
---|
599 | | #n #b #_ elim b in t ⊢ (% → ? → ? → ??%%%); |
---|
600 | [ #b #P #Hf #HP normalize %1 [ @HP | // ] |
---|
601 | | #h #hd #tl #H #b #P #Hf cases hd #HP normalize @(forall_prepare_tree_for_insertion A h tl a ? HP) |
---|
602 | ] |
---|
603 | ] |
---|
604 | qed. |
---|
605 | |
---|
606 | lemma update_fail : ∀A,n,b,a,t. |
---|
607 | update A n b a t = None ? → |
---|
608 | lookup_opt A n b t = None ?. |
---|
609 | #A #n elim n |
---|
610 | [ #b @(vector_inv_n … b) #a #t cases (BitVectorTrie_O … t) |
---|
611 | [ * #x #E >E normalize #NE destruct |
---|
612 | | #E >E normalize // |
---|
613 | ] |
---|
614 | | #m #IH #b @(vector_inv_n … b) #hd #tl #a #t cases (BitVectorTrie_Sn … t) |
---|
615 | [ * #t1 * #t2 #E >E cases hd whd in ⊢ (??%? → ??%?); |
---|
616 | #X lapply (option_map_none … X) @IH |
---|
617 | | #E >E normalize // |
---|
618 | ] |
---|
619 | ] qed. |
---|
620 | |
---|
621 | lemma update_lookup_opt_same : ∀A,n,b,a,t,t'. |
---|
622 | update A n b a t = Some ? t' → |
---|
623 | lookup_opt A n b t' = Some ? a. |
---|
624 | #A #n elim n |
---|
625 | [ #b #a #t #t' @(vector_inv_n … b) |
---|
626 | cases (BitVectorTrie_O … t) |
---|
627 | [ * #x #E >E normalize #E' destruct @refl |
---|
628 | | #E >E normalize #E' destruct |
---|
629 | ] |
---|
630 | | #m #IH #b #a #t #t' |
---|
631 | @(vector_inv_n … b) #bhd #btl |
---|
632 | cases (BitVectorTrie_Sn … t) |
---|
633 | [ * #t1 * #t2 #E' >E' |
---|
634 | whd in ⊢ (??%? → ??%?); cases bhd #U |
---|
635 | cases (option_map_some ????? U) |
---|
636 | #tn' * #U' #E'' <E'' |
---|
637 | whd in ⊢ (??%?); whd in ⊢ (??(???%%)?); |
---|
638 | @(IH … U') |
---|
639 | | #E >E normalize #E' destruct |
---|
640 | ] |
---|
641 | ] qed. |
---|
642 | |
---|
643 | lemma update_lookup_opt_other : ∀A,n,b,a,t,t'. |
---|
644 | update A n b a t = Some ? t' → |
---|
645 | ∀b'. b ≠ b' → |
---|
646 | lookup_opt A n b' t = lookup_opt A n b' t'. |
---|
647 | #A #n elim n |
---|
648 | [ #b #a #t #t' #E #b' |
---|
649 | @(vector_inv_n … b) @(vector_inv_n … b') |
---|
650 | * #NE cases (NE (refl ??)) |
---|
651 | | #m #IH #b #a #t #t' |
---|
652 | @(vector_inv_n … b) #bhd #btl |
---|
653 | cases (BitVectorTrie_Sn … t) |
---|
654 | [ * #t1 * #t2 #E >E whd in ⊢ (??%? → ?); cases bhd |
---|
655 | #U cases (option_map_some ????? U) #tn' * #U' #E' <E' |
---|
656 | #b' @(vector_inv_n … b') #bhd' #btl' |
---|
657 | cases bhd' |
---|
658 | [ 2,3: #_ @refl |
---|
659 | | *: #NE whd in ⊢ (??%%); whd in ⊢ (??(???%%)(???%%)); |
---|
660 | @(IH … U') % #E'' >E'' in NE; * #H @H @refl |
---|
661 | ] |
---|
662 | | #E >E whd in ⊢ (??%? → ?); #NE destruct |
---|
663 | ] |
---|
664 | ] qed. |
---|