source: src/ASM/BitVectorTrie.ma @ 1034

Last change on this file since 1034 was 1034, checked in by boender, 8 years ago

various & sundry fold/forall lemmas

File size: 14.3 KB
Line 
1include "basics/types.ma".
2
3include "utilities/option.ma".
4include "ASM/BitVector.ma".
5
6inductive BitVectorTrie (A: Type[0]): nat → Type[0] ≝
7  Leaf: A → BitVectorTrie A O
8| Node: ∀n: nat. BitVectorTrie A n → BitVectorTrie A n → BitVectorTrie A (S n)
9| Stub: ∀n: nat. BitVectorTrie A n.
10
11let rec fold (A, B: Type[0]) (n: nat) (f: BitVector n → A → B → B)
12 (t: BitVectorTrie A n) (b: B) on t: B ≝
13 (match t return λx.λ_.x = n → B with
14  [ Leaf l ⇒ λ_.f (zero ?) l b
15  | Node h l r ⇒ λK.
16    fold A B h (λx.f ((VCons ? h false x)⌈(S h) ↦ n⌉)) l
17      (fold A B h (λx.f ((VCons ? h true x)⌈(S h) ↦ n⌉)) r b)
18  | Stub _ ⇒ λ_.b
19  ]) (refl ? n).
20 @K
21qed.
22
23(* these two can probably be generalized w/r/t the second type and
24 * some sort of equality relationship *)
25lemma fold_eq:
26  ∀A: Type[0].
27  ∀n: nat.
28  ∀f.
29  ∀t.
30  ∀P, Q: Prop.
31  (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.
32 #A #n #f #t #P #Q #H
33 generalize in match (refl ? n) generalize in match H -H; generalize in match Q -Q; generalize in match P -P;
34 elim t in f ⊢ (? → ? → ? → ???% → ? → ???%%%? → ???%%%?)
35 [ #a #f #P #Q #HPQ #_ #Hf #HP whd in HP; whd @(Hf (zero 0) a P Q HPQ HP)
36 | #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) ?)
37   [ @(Hr ? P Q HPQ (refl ? h) ?)
38     #a #t' #X #Y #HXY #Hff @(Hf (true:::a) t' X Y HXY Hff)
39   | #a #t' #X #Y #HXY #Hff @(Hf (false:::a) t' X Y HXY Hff) ]
40 | #h #f #P #Q #HPQ #_ #Hf #HP whd in HP; whd @(HPQ HP) ]
41  @HP
42qed.
43 
44lemma fold_init:
45  ∀A:Type[0].
46  ∀n:nat.
47  ∀f.
48  ∀t.
49  ∀P: Prop.
50  (∀a,t',P.f a t' P → P) → fold A Prop n f t P → P.
51 #A #n #f #t #P #H generalize in match (refl ? n) generalize in match H -H; generalize in match P -P;
52 elim t in f ⊢ (? → ? → ???% → ???%%%? → ?) -t
53 [ #a #f #P #Hf #_ normalize @(Hf [[]])
54 | #h #l #r #Hl #Hr #f #P #Hf #_ normalize #HP @(Hr (λx.f (true:::x)))
55   [ #a #t' #X @(Hf (true:::a) t' X) | @(refl ? h) | @(Hl (λx.f (false:::x)))
56     [ #a #t' #X @(Hf (false:::a) t' X) | @(refl ? h) | @HP ]
57   ]
58 | #h #f #P #Hf #_ normalize //
59 ]
60qed.
61 
62definition forall
63 ≝
64  λA.λn.λt:BitVectorTrie A n.λP.fold ? ? ? (λk.λa.λacc.(P k a) ∧ acc) t True.
65 
66lemma forall_nodel:
67  ∀A:Type[0].
68  ∀n:nat.
69  ∀l,r.
70  ∀P:BitVector (S n) → A → Prop.
71  forall A (S n) (Node ? n l r) P → forall A n l (λx.λa.P (false:::x) a).
72 #A #n #l #r #P #Hl
73 whd @(fold_eq A n ? ? (fold A ? n (λk.λa.λacc.P (true:::k) a∧acc) r True) True)
74 [ //
75 | #n #t' #X #Y #HXY #HX %1
76   [ @(proj1 ? ? HX) | @HXY @(proj2 ? ? HX) ]
77 | whd in Hl @Hl ]
78qed.
79 
80lemma forall_noder:
81  ∀A:Type[0].
82  ∀n:nat.
83  ∀l,r.
84  ∀P:BitVector (S n) → A → Prop.
85  forall A (S n) (Node ? n l r) P → forall A n r (λx.λa.P (true:::x) a).
86 #A #n #l #r #P #Hr
87 whd @(fold_init A n (λk.λa.λacc.P (false:::k) a∧acc) l) 
88 [ #n #t' #P #HP @(proj2 ? ? HP)
89 | @Hr
90 ]
91qed.
92
93let rec lookup_opt (A: Type[0]) (n: nat)
94                (b: BitVector n) (t: BitVectorTrie A n) on t
95       : option A ≝
96 (match t return λx.λ_. BitVector x → option A with
97  [ Leaf l ⇒ λ_.Some ? l
98  | Node h l r ⇒ λb. lookup_opt A ? (tail … b) (if head' … b then r else l)
99  | Stub _ ⇒ λ_.None ?
100  ]) b.
101 
102lemma forall_lookup:
103 ∀A.
104  ∀n.
105  ∀t:BitVectorTrie A n.
106  ∀P:BitVector n → A → Prop.
107  forall A n t P → ∀a:A.∀b.lookup_opt A n b t = Some ? a → P b a.
108 #A #n #t #P generalize in match (refl ? n) elim t in P ⊢ (???% → ??%%? → ? → ? → ??(??%%%)? → ?)
109 [ #x #f #_ #Hf #a #b whd in Hf; #Hb normalize in Hb; destruct >(BitVector_O b) @(proj1 ? ? Hf)
110 | #h #l #r #Hl #Hr #f #_ #Hf #a #b #Hb cases (BitVector_Sn h b)
111   #hd #bla elim bla -bla #tl #Htl >Htl in Hb; #Hb cases hd in Hb;
112   [ #Hb normalize in Hb; @(Hr (λx.λa.f (true:::x) a) (refl ? h))
113     [ @(forall_noder A h l r f Hf)
114     | @Hb
115     ]
116   | #Hb normalize in Hb; @(Hl (λx.λa.f (false:::x) a) (refl ? h))
117     [ @(forall_nodel A h l r f Hf)
118     | @Hb
119     ]
120   ]
121 | #n #f #_ #Hf #a #b #Hb normalize in Hb; destruct
122qed.
123
124let rec lookup (A: Type[0]) (n: nat)
125                (b: BitVector n) (t: BitVectorTrie A n) (a: A) on b
126       : A ≝
127  (match b return λx.λ_. x = n → A with
128    [ VEmpty ⇒
129      (match t return λx.λ_. O = x → A with
130        [ Leaf l ⇒ λ_.l
131        | Node h l r ⇒ λK.⊥
132        | Stub s ⇒ λ_.a
133        ])
134    | VCons o hd tl ⇒
135      match t return λx.λ_. (S o) = x → A with
136        [ Leaf l ⇒ λK.⊥
137        | Node h l r ⇒
138           match hd with
139             [ true ⇒ λK. lookup A h (tl⌈o ↦ h⌉) r a
140             | false ⇒ λK. lookup A h (tl⌈o ↦ h⌉) l a
141             ]
142        | Stub s ⇒ λ_. a]
143    ]) (refl ? n).
144  [1,2:
145    destruct
146  |*:
147    @ injective_S
148    //
149  ]
150qed.
151
152let rec prepare_trie_for_insertion (A: Type[0]) (n: nat) (b: BitVector n) (a:A) on b : BitVectorTrie A n ≝
153   match b with
154    [ VEmpty ⇒ Leaf A a
155    | VCons o hd tl ⇒
156      match hd with
157        [ true ⇒  Node A o (Stub A o) (prepare_trie_for_insertion A o tl a)
158        | false ⇒ Node A o (prepare_trie_for_insertion A o tl a) (Stub A o)
159        ]
160    ].
161
162let rec insert (A: Type[0]) (n: nat) (b: BitVector n) (a: A) on b: BitVectorTrie A n → BitVectorTrie A n ≝
163  (match b with
164    [ VEmpty ⇒ λ_. Leaf A a
165    | VCons o hd tl ⇒ λt.
166          match t return λy.λ_. S o = y → BitVectorTrie A (S o) with
167            [ Leaf l ⇒ λprf.⊥
168            | Node p l r ⇒ λprf.
169               match hd with
170                [ true ⇒  Node A o (l⌈p ↦ o⌉) (insert A o tl a (r⌈p ↦ o⌉))
171                | false ⇒ Node A o (insert A o tl a (l⌈p ↦ o⌉)) (r⌈p ↦ o⌉)
172                ]
173            | Stub p ⇒ λprf. (prepare_trie_for_insertion A ? (hd:::tl) a)
174            ] (refl ? (S o))
175    ]).
176  [ destruct
177  |*:
178    @ injective_S
179    //
180  ]
181qed.
182 
183let rec update (A: Type[0]) (n: nat) (b: BitVector n) (a: A) on b: BitVectorTrie A n → option (BitVectorTrie A n) ≝
184  (match b with
185    [ VEmpty ⇒ λt. match t return λy.λ_. O = y → option (BitVectorTrie A O) with
186                   [ Leaf _ ⇒ λ_. Some ? (Leaf A a)
187                   | Stub _ ⇒ λ_. None ?
188                   | Node _ _ _ ⇒ λprf. ⊥
189                   ] (refl ? O)
190    | VCons o hd tl ⇒ λt.
191          match t return λy.λ_. S o = y → option (BitVectorTrie A (S o)) with
192            [ Leaf l ⇒ λprf.⊥
193            | Node p l r ⇒ λprf.
194               match hd with
195                [ true ⇒  option_map ?? (λv. Node A o (l⌈p ↦ o⌉) v) (update A o tl a (r⌈p ↦ o⌉))
196                | false ⇒ option_map ?? (λv. Node A o v (r⌈p ↦ o⌉)) (update A o tl a (l⌈p ↦ o⌉))
197                ]
198            | Stub p ⇒ λprf. None ?
199            ] (refl ? (S o))
200    ]).
201  [ 1,2: destruct
202  |*:
203    @ injective_S @sym_eq @prf
204  ]
205qed.
206
207let rec merge (A: Type[0]) (n: nat) (b: BitVectorTrie A n) on b: BitVectorTrie A n → BitVectorTrie A n ≝
208  match b return λx. λ_. BitVectorTrie A x → BitVectorTrie A x with
209  [ Stub _ ⇒ λc. c
210  | Leaf l ⇒ λc. match c with [ Leaf a ⇒ Leaf ? a | _ ⇒ Leaf ? l ]
211  | Node p l r ⇒
212    λc.
213    (match c return λx. λ_. x = (S p) → BitVectorTrie A (S p) with
214    [ Node p' l' r' ⇒ λprf. Node ? ? (merge ?? l (l'⌈p' ↦ p⌉)) (merge ?? r (r'⌈p' ↦ p⌉))
215    | Stub _ ⇒ λprf. Node ? p l r
216    | Leaf _ ⇒ λabsd. ?
217    ] (refl ? (S p)))
218  ].
219  [1:
220      destruct(absd)
221  |2,3:
222      @ injective_S
223        assumption
224  ]
225qed.
226
227lemma BitVectorTrie_O:
228 ∀A:Type[0].∀v:BitVectorTrie A 0.(∃w. v ≃ Leaf A w) ∨ v ≃ Stub A 0.
229 #A #v generalize in match (refl … O) cases v in ⊢ (??%? → (?(??(λ_.?%%??)))(?%%??))
230  [ #w #_ %1 %[@w] %
231  | #n #l #r #abs @⊥ destruct(abs)
232  | #n #EQ %2 >EQ %]
233qed.
234
235lemma BitVectorTrie_Sn:
236 ∀A:Type[0].∀n.∀v:BitVectorTrie A (S n).(∃l,r. v ≃ Node A n l r) ∨ v ≃ Stub A (S n).
237 #A #n #v generalize in match (refl … (S n)) cases v in ⊢ (??%? → (?(??(λ_.??(λ_.?%%??))))%)
238  [ #m #abs @⊥ destruct(abs)
239  | #m #l #r #EQ %1 <(injective_S … EQ) %[@l] %[@r] //
240  | #m #EQ %2 // ]
241qed.
242
243lemma lookup_prepare_trie_for_insertion_hit:
244 ∀A:Type[0].∀a,v:A.∀n.∀b:BitVector n.
245  lookup … b (prepare_trie_for_insertion … b v) a = v.
246 #A #a #v #n #b elim b // #m #hd #tl #IH cases hd normalize //
247qed.
248 
249lemma lookup_insert_hit:
250 ∀A:Type[0].∀a,v:A.∀n.∀b:BitVector n.∀t:BitVectorTrie A n.
251  lookup … b (insert … b v t) a = v.
252 #A #a #v #n #b elim b -b -n //
253 #n #hd #tl #IH #t cases(BitVectorTrie_Sn … t)
254  [ * #l * #r #JMEQ >JMEQ cases hd normalize //
255  | #JMEQ >JMEQ cases hd normalize @lookup_prepare_trie_for_insertion_hit ]
256qed.
257
258lemma lookup_prepare_trie_for_insertion_miss:
259 ∀A:Type[0].∀a,v:A.∀n.∀c,b:BitVector n.
260  (notb (eq_bv ? b c)) → lookup … b (prepare_trie_for_insertion … c v) a = a.
261 #A #a #v #n #c elim c
262  [ #b >(BitVector_O … b) normalize #abs @⊥ //
263  | #m #hd #tl #IH #b cases(BitVector_Sn … b) #hd' * #tl' #JMEQ >JMEQ
264    cases hd cases hd' normalize
265    [2,3: #_ cases tl' //
266    |*: change with (bool_to_Prop (notb (eq_bv ???)) → ?) /2/ ]]
267qed.
268 
269lemma lookup_insert_miss:
270 ∀A:Type[0].∀a,v:A.∀n.∀c,b:BitVector n.∀t:BitVectorTrie A n.
271  (notb (eq_bv ? b c)) → lookup … b (insert … c v t) a = lookup … b t a.
272 #A #a #v #n #c elim c -c -n
273  [ #b #t #DIFF @⊥ whd in DIFF; >(BitVector_O … b) in DIFF //
274  | #n #hd #tl #IH #b cases(BitVector_Sn … b) #hd' * #tl' #JMEQ >JMEQ
275    #t cases(BitVectorTrie_Sn … t)
276    [ * #l * #r #JMEQ >JMEQ cases hd cases hd' #H normalize in H;
277     [1,4: change in H with (bool_to_Prop (notb (eq_bv ???))) ] normalize // @IH //
278    | #JMEQ >JMEQ cases hd cases hd' #H normalize in H;
279     [1,4: change in H with (bool_to_Prop (notb (eq_bv ???))) ] normalize
280     [3,4: cases tl' // | *: @lookup_prepare_trie_for_insertion_miss //]]]
281qed.
282
283lemma lookup_stub:
284 ∀A.∀n.∀b.∀a.
285 lookup A n b (Stub A ?) a = a.
286 #A #n #b #a cases n in b ⊢ (??(??%%%?)?)
287 [ #b >(BitVector_O b) normalize @refl
288 | #h #b cases (BitVector_Sn h b) #hd #X elim X -X; #tl #Hb >Hb cases hd
289   [ normalize @refl
290   | normalize @refl
291   ]
292 ]   
293qed.   
294
295lemma lookup_opt_lookup:
296  ∀A:Type[0].∀n:nat.∀b:BitVector n.∀t:BitVectorTrie A n.∀a:A.
297  lookup_opt A n b t = Some A a → ∀x.lookup A n b t x = a.
298 #A #n #b #t #a generalize in match (refl ? n) elim t in b ⊢ (???% → ??(??%%%)? → ? → ?)
299 [ #a #B #_ #H #x normalize in H; >(BitVector_O B) normalize destruct @refl
300 | #h #l #r #Hl #Hr #b #_ #H #x cases (BitVector_Sn h b) #hd #X elim X; -X; #tl #Hb
301   >Hb >Hb in H; cases hd
302   [ normalize #Hlookup @(Hr ? (refl ? h)) @Hlookup
303   | normalize #Hlookup @(Hl ? (refl ? h)) @Hlookup
304   ]
305 | #n #B #_ #H #x normalize in H; destruct
306 ]
307qed.
308
309lemma lookup_opt_prepare_trie_for_insertion_hit:
310 ∀A:Type[0].∀v:A.∀n.∀b:BitVector n.
311  lookup_opt … b (prepare_trie_for_insertion … b v) = Some A v.
312 #A #v #n #b elim b // #m #hd #tl #IH cases hd normalize //
313qed.
314
315lemma lookup_opt_insert_hit:
316 ∀A:Type[0].∀v:A.∀n.∀b:BitVector n.∀t:BitVectorTrie A n.
317  lookup_opt … b (insert … b v t) = Some A v.
318 #A #v #n #b #t elim t in b ⊢ (??(??%%%)?)
319 [ #x #b >(BitVector_O b) normalize @refl
320 | #h #l #r #Hl #Hr #b cases (BitVector_Sn h b) #hd #X elim X -X; #tl #Hb >Hb cases hd
321   [ normalize @Hr
322   | normalize @Hl
323   ]
324 | #n' #b cases n' in b ⊢ ?
325   [ #b >(BitVector_O b) normalize @refl
326   | #m #b cases (BitVector_Sn m b) #hd #X elim X -X; #tl #Hb >Hb cases hd
327     normalize @lookup_opt_prepare_trie_for_insertion_hit
328   ]
329 ]
330qed.
331   
332lemma forall_insert_inv1:
333  ∀A.∀n.∀b.∀a.∀t.∀P.
334  forall A n (insert A n b a t) P → P b a.
335 #A #n #b #a #t #P #H @(forall_lookup ? ? (insert A n b a t))
336 [ @H
337 | >(lookup_opt_insert_hit A ? n b) @(refl ? (Some A a))
338 ]
339qed.
340
341lemma forall_insert_inv2a:
342  ∀A:Type[0].∀n:nat.∀b.∀a.∀t.∀P.
343  lookup_opt A n b t = (None A)  → forall A n (insert A n b a t) P → forall A n t P.
344 #A #n #b #a #t generalize in match (refl ? n) elim t in b ⊢ (???% → ? → ??(??%%%)? → ??%%% → ??%%%)
345 [ #x #b #_ #P >(BitVector_O b) normalize #H destruct
346 | #h #l #r #Hl #Hr #b #_ #P cases (BitVector_Sn h b) #hd #X elim X -X; #tl #Hb >Hb cases hd #Hlookup #H
347   [ normalize in H; normalize
348     @(fold_eq … (fold A ? ? (λx.λa0.λacc.P (true:::x) a0∧acc) (insert … tl a r) True) … H)
349     [ #Hfold @(Hr tl (refl ? h) ? Hlookup Hfold)
350     | #x #t' #X #Y #HXY #HP %1 [ @(proj1 ? ? HP) | @(HXY (proj2 ? ?HP)) ]
351     ]
352   | normalize in H; normalize     
353     @(fold_eq … True)
354     [ #_ @(fold_init A h (λx.λa0.λacc.P (false:::x) a0 ∧ acc) (insert A h tl a l))
355       [ #z #t' #X #HX @(proj2 ? ? HX)
356       | @H ]
357     | #z #t' #X #Y #HXY #HP %1 [ @(proj1 ? ? HP) | @(HXY (proj2 ? ? HP)) ]
358     | @(Hl tl (refl ? h) ? Hlookup) normalize
359       @(fold_eq … (fold A ? ? (λx.λa0.λacc.P (true:::x) a0∧acc) r True))
360       [ //
361       | #z #t' #X #Y #HXY #HP %1 [ @(proj1 ? ? HP) | @(HXY (proj2 ? ? HP)) ]
362       | @H
363       ]
364     ]
365   ]
366 | #n #b #_ #P #Hlookup #Hf normalize // ]
367qed. 
368
369lemma forall_insert_inv2b:
370  ∀A:Type[0].∀n:nat.∀b:BitVector n.∀a:A.∀t.∀P:(BitVector n → A → Prop).
371  (∀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.
372 #A #n #b #a #t generalize in match (refl ? n) elim t in b ⊢ (???% → % → ? → ??%%% → ?)
373 [ #x #b #_ #P >(BitVector_O b) normalize #HP #Hf %1 [ @HP @refl | @(proj2 ? ? Hf) ]
374 | #h #l #r #Hl #Hr #b #_ cases (BitVector_Sn h b) #hd #X elim X -X; #tl #Hb >Hb cases hd #P #HP #Hf
375   [ normalize in Hf; normalize
376     @(fold_eq … (fold A ? ? (λx.λa0.λacc.P (true:::x) a0∧acc) (insert … tl a r) True) … Hf)
377     [ #Hfold @(Hr tl (refl ? h) ? HP Hfold)
378     | #x #t' #X #Y #HXY #HP %1 [ @(proj1 ? ? HP) | @(HXY (proj2 ? ? HP)) ]
379     ]
380   | normalize in H; normalize     
381     @(fold_eq … True)
382     [ #_ @(fold_init A h (λx.λa0.λacc.P (false:::x) a0 ∧ acc) (insert A h tl a l))
383       [ #z #t' #X #HX @(proj2 ? ? HX)
384       | @Hf ]
385     | #z #t' #X #Y #HXY #HP %1 [ @(proj1 ? ? HP) | @(HXY (proj2 ? ? HP)) ]
386     | @(Hl tl (refl ? h) ? HP) normalize
387       @(fold_eq … (fold A ? ? (λx.λa0.λacc.P (true:::x) a0∧acc) r True))
388       [ //
389       | #z #t' #X #Y #HXY #HP %1 [ @(proj1 ? ? HP) | @(HXY (proj2 ? ? HP)) ]
390       | @Hf
391       ]
392     ]
393   ]
394 | #n #b #_ #P #Hlookup #Hf normalize // ]
395qed.
396   
Note: See TracBrowser for help on using the repository browser.