source: src/ASM/BitVectorTrie.ma @ 1632

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