source: src/Clight/frontend_misc.ma @ 2578

Last change on this file since 2578 was 2578, checked in by garnier, 7 years ago

Progress on CL to CM, fixed some stuff in memory injections.

File size: 93.5 KB
Line 
1(* Various small homeless results. *)
2
3include "Clight/TypeComparison.ma".
4include "Clight/Csem.ma".
5include "common/Pointers.ma".
6include "basics/jmeq.ma".
7include "basics/star.ma". (* well-founded relations *)
8include "common/IOMonad.ma".
9include "common/IO.ma".
10include "basics/lists/listb.ma".
11include "basics/lists/list.ma".
12
13
14(* --------------------------------------------------------------------------- *)
15(* [cthulhu] plays the same role as daemon. It should be droppable. *)
16(* --------------------------------------------------------------------------- *)
17
18axiom cthulhu : ∀A:Prop. A. (* Because of the nightmares. *)
19
20(* --------------------------------------------------------------------------- *)
21(* Misc. *)
22(* --------------------------------------------------------------------------- *)
23
24lemma eq_intsize_identity : ∀id. eq_intsize id id = true.
25* normalize //
26qed.
27
28lemma sz_eq_identity : ∀s. sz_eq_dec s s = inl ?? (refl ? s).
29* normalize //
30qed.
31
32lemma type_eq_identity : ∀t. type_eq t t = true.
33#t normalize elim (type_eq_dec t t)
34[ 1: #Heq normalize //
35| 2: #H destruct elim H #Hcontr elim (Hcontr (refl ? t)) ] qed.
36
37lemma type_neq_not_identity : ∀t1, t2. t1 ≠ t2 → type_eq t1 t2 = false.
38#t1 #t2 #Hneq normalize elim (type_eq_dec t1 t2)
39[ 1: #Heq destruct elim Hneq #Hcontr elim (Hcontr (refl ? t2))
40| 2: #Hneq' normalize // ] qed.
41
42lemma le_S_O_absurd : ∀x:nat. S x ≤ 0 → False. /2 by absurd/ qed.
43
44lemma some_inj : ∀A : Type[0]. ∀a,b : A. Some ? a = Some ? b → a = b. #A #a #b #H destruct (H) @refl qed.
45
46lemma prod_eq_lproj : ∀A,B : Type[0]. ∀a : A. ∀b : B. ∀c : A × B. 〈a,b〉 = c → a = \fst c.
47#A #B #a #b * #a' #b' #H destruct @refl
48qed.
49
50lemma prod_eq_rproj : ∀A,B : Type[0]. ∀a : A. ∀b : B. ∀c : A × B. 〈a,b〉 = c → b = \snd c.
51#A #B #a #b * #a' #b' #H destruct @refl
52qed.
53
54lemma bindIO_Error : ∀err,f. bindIO io_out io_in (val×trace) (trace×state) (Error … err) f = Wrong io_out io_in (trace×state) err.
55// qed.
56
57lemma bindIO_Value : ∀v,f. bindIO io_out io_in (val×trace) (trace×state) (Value … v) f = (f v).
58// qed.
59
60lemma bindIO_elim :
61         ∀A.
62         ∀P : (IO io_out io_in A) → Prop.
63         ∀e : res A. (*IO io_out io_in A.*)
64         ∀f.
65         (∀v. (e:IO io_out io_in A) = OK … v →  P (f v)) →
66         (∀err. (e:IO io_out io_in A) = Error … err →  P (Wrong ??? err)) →
67         P (bindIO io_out io_in ? A (e:IO io_out io_in A) f).
68#A #P * try /2/ qed.
69
70lemma opt_to_io_Value :
71  ∀A,B,C,err,x,res. opt_to_io A B C err x = return res → x = Some ? res.
72#A #B #C #err #x cases x normalize
73[ 1: #res #Habsurd destruct
74| 2: #c #res #Heq destruct @refl ]
75qed. 
76
77lemma some_inversion :
78  ∀A,B:Type[0].
79  ∀e:option A.
80  ∀res:B.
81  ∀f:A → option B.
82 match e with
83 [ None ⇒ None ?
84 | Some x ⇒ f x ] = Some ? res →
85 ∃x. e = Some ? x ∧ f x = Some ? res.
86 #A #B #e #res #f cases e normalize nodelta
87[ 1: #Habsurd destruct (Habsurd)
88| 2: #a #Heq %{a} @conj >Heq @refl ]
89qed.
90
91lemma res_inversion :
92  ∀A,B:Type[0].
93  ∀e:option A.
94  ∀errmsg.
95  ∀result:B.
96  ∀f:A → res B.
97 match e with
98 [ None ⇒ Error ? errmsg
99 | Some x ⇒ f x ] = OK ? result →
100 ∃x. e = Some ? x ∧ f x = OK ? result.
101 #A #B #e #errmsg #result #f cases e normalize nodelta
102[ 1: #Habsurd destruct (Habsurd)
103| 2: #a #Heq %{a} @conj >Heq @refl ]
104qed.
105
106lemma cons_inversion :
107  ∀A,B:Type[0].
108  ∀e:list A.
109  ∀res:B.
110  ∀f:A → list A → option B.
111 match e with
112 [ nil ⇒ None ?
113 | cons hd tl ⇒ f hd tl ] = Some ? res →
114 ∃hd,tl. e = hd::tl ∧ f hd tl = Some ? res.
115#A #B #e #res #f cases e normalize nodelta
116[ 1: #Habsurd destruct (Habsurd)
117| 2: #hd #tl #Heq %{hd} %{tl} @conj >Heq @refl ]
118qed.
119
120lemma if_opt_inversion :
121  ∀A:Type[0].
122  ∀x.
123  ∀y:A.
124  ∀e:bool.
125 (if e then
126    x
127  else
128    None ?) = Some ? y →
129 e = true ∧ x = Some ? y.
130#A #x #y * normalize
131#H destruct @conj @refl
132qed.
133
134lemma opt_to_res_inversion :
135  ∀A:Type[0]. ∀errmsg. ∀opt. ∀val. opt_to_res A errmsg opt = OK ? val →
136  opt = Some ? val.
137#A #errmsg *
138[ 1: #val normalize #Habsurd destruct
139| 2: #res #val normalize #Heq destruct @refl ]
140qed.
141
142lemma andb_inversion :
143  ∀a,b : bool. andb a b = true → a = true ∧ b = true.
144* * normalize /2 by conj, refl/ qed. 
145
146lemma identifier_eq_i_i : ∀tag,i. ∃pf. identifier_eq tag i i = inl … pf.
147#tag #i cases (identifier_eq ? i i)
148[ 1: #H %{H} @refl
149| 2: * #Habsurd @(False_ind … (Habsurd … (refl ? i))) ]
150qed.
151
152lemma intsize_eq_inversion :
153  ∀sz,sz'.
154  ∀A:Type[0].
155  ∀ok,not_ok.
156  intsize_eq_elim' sz sz' (λsz,sz'. res A)
157                          (OK ? ok)
158                          (Error ? not_ok) = (OK ? ok) →
159  sz = sz'.
160* * try // normalize
161#A #ok #not_ok #Habsurd destruct
162qed.
163
164lemma intsize_eq_elim_dec :
165  ∀sz1,sz2.
166  ∀P: ∀sz1,sz2. Type[0].
167  ((∀ifok,iferr. intsize_eq_elim' sz1 sz1 P ifok iferr = ifok) ∧ sz1 = sz2) ∨
168  ((∀ifok,iferr. intsize_eq_elim' sz1 sz2 P ifok iferr = iferr) ∧ sz1 ≠ sz2).
169* * #P normalize
170try /3 by or_introl, conj, refl/
171%2 @conj try //
172% #H destruct
173qed.
174
175lemma typ_eq_elim :
176  ∀t1,t2.
177  ∀(P: (t1=t2)+(t1≠t2) → Prop).
178  (∀H:t1 = t2. P (inl ?? H)) → (∀H:t1 ≠ t2. P (inr ?? H)) → P (typ_eq t1 t2).
179#t1 #t2 #P #Hl #Hr
180@(match typ_eq t1 t2
181  with
182  [ inl H ⇒ Hl H
183  | inr H ⇒ Hr H ])
184qed.
185
186
187lemma eq_nat_dec_true : ∀n. eq_nat_dec n n = inl ?? (refl ? n).
188#n elim n try //
189#n' #Hind whd in ⊢ (??%?); >Hind @refl
190qed.
191
192lemma type_eq_dec_true : ∀ty. type_eq_dec ty ty = inl ?? (refl ? ty).
193#ty cases (type_eq_dec ty ty) #H
194destruct (H) try @refl @False_ind cases H #J @J @refl qed.
195
196lemma typ_eq_refl : ∀t. typ_eq t t = inl ?? (refl ? t).
197*
198[ * * normalize @refl
199| @refl ]
200qed.
201
202lemma intsize_eq_elim_inversion :
203  ∀A:Type[0].
204  ∀sz1,sz2.
205  ∀elt1,f,errmsg,res. 
206  intsize_eq_elim ? sz1 sz2 bvint elt1 f (Error A errmsg) = OK ? res →
207  ∃H:sz1 = sz2. OK ? res = (f (eq_rect_r ? sz1 sz2 (sym_eq ??? H) ? elt1)).
208#A * * #elt1 #f #errmsg #res normalize #H destruct (H)
209%{(refl ??)} normalize nodelta >H @refl
210qed.
211
212lemma inttyp_eq_elim_true' :
213  ∀sz,sg,P,p1,p2.
214  inttyp_eq_elim' sz sz sg sg P p1 p2 = p1.
215* * #P #p1 #p2 normalize try @refl
216qed.
217
218
219(* --------------------------------------------------------------------------- *)
220(* Useful facts on various boolean operations. *)
221(* --------------------------------------------------------------------------- *)
222 
223lemma andb_lsimpl_true : ∀x. andb true x = x. // qed.
224lemma andb_lsimpl_false : ∀x. andb false x = false. normalize // qed.
225lemma andb_comm : ∀x,y. andb x y = andb y x. // qed.
226lemma notb_true : notb true = false. // qed.
227lemma notb_false : notb false = true. % #H destruct qed.
228lemma notb_fold : ∀x. if x then false else true = (¬x). // qed.
229
230(* --------------------------------------------------------------------------- *)
231(* Useful facts on Z. *)
232(* --------------------------------------------------------------------------- *)
233
234lemma Zltb_to_Zleb_true : ∀a,b. Zltb a b = true → Zleb a b = true.
235#a #b #Hltb lapply (Zltb_true_to_Zlt … Hltb) #Hlt @Zle_to_Zleb_true
236/3 by Zlt_to_Zle, transitive_Zle/ qed.
237
238lemma Zle_to_Zle_to_eq : ∀a,b. Zle a b → Zle b a → a = b.
239#a #b elim b
240[ 1: elim a // #a' normalize [ 1: @False_ind | 2: #_ @False_ind ] ]
241#b' elim a normalize
242[ 1: #_ @False_ind
243| 2: #a' #H1 #H2 >(le_to_le_to_eq … H1 H2) @refl
244| 3: #a' #_ @False_ind
245| 4: @False_ind
246| 5: #a' @False_ind
247| 6: #a' #H2 #H1 >(le_to_le_to_eq … H1 H2) @refl
248] qed.
249
250lemma Zleb_true_to_Zleb_true_to_eq : ∀a,b. (Zleb a b = true) → (Zleb b a = true) → a = b.
251#a #b #H1 #H2
252/3 by Zle_to_Zle_to_eq, Zleb_true_to_Zle/
253qed.
254
255lemma Zltb_dec : ∀a,b. (Zltb a b = true) ∨ (Zltb a b = false ∧ Zleb b a = true).
256#a #b
257lapply (Z_compare_to_Prop … a b)
258cases a
259[ 1: | 2,3: #a' ]
260cases b
261whd in match (Z_compare OZ OZ); normalize nodelta
262[ 2,3,5,6,8,9: #b' ]
263whd in match (Zleb ? ?);
264try /3 by or_introl, or_intror, conj, I, refl/
265whd in match (Zltb ??);
266whd in match (Zleb ??); #_
267[ 1: cases (decidable_le (succ a') b')
268     [ 1: #H lapply (le_to_leb_true … H) #H %1 assumption
269     | 2:  #Hnotle lapply (not_le_to_lt … Hnotle) #Hlt %2  @conj try @le_to_leb_true
270           /2 by monotonic_pred/ @(not_le_to_leb_false … Hnotle) ]
271| 2: cases (decidable_le (succ b') a')
272     [ 1: #H lapply (le_to_leb_true … H) #H %1 assumption
273     | 2:  #Hnotle lapply (not_le_to_lt … Hnotle) #Hlt %2  @conj try @le_to_leb_true
274           /2 by monotonic_pred/ @(not_le_to_leb_false … Hnotle) ]
275] qed.
276
277lemma Zleb_unsigned_OZ : ∀bv. Zleb OZ (Z_of_unsigned_bitvector 16 bv) = true.
278#bv elim bv try // #n' * #tl normalize /2/ qed.
279
280lemma Zltb_unsigned_OZ : ∀bv. Zltb (Z_of_unsigned_bitvector 16 bv) OZ = false.
281#bv elim bv try // #n' * #tl normalize /2/ qed.
282
283lemma Z_of_unsigned_not_neg : ∀bv.
284  (Z_of_unsigned_bitvector 16 bv = OZ) ∨ (∃p. Z_of_unsigned_bitvector 16 bv = pos p).
285#bv elim bv
286[ 1: normalize %1 @refl
287| 2: #n #hd #tl #Hind cases hd
288     [ 1: normalize %2 /2 by ex_intro/
289     | 2: cases Hind normalize [ 1: #H %1 @H | 2: * #p #H >H %2 %{p} @refl ]
290     ]
291] qed.
292
293lemma free_not_in_bounds : ∀x. if Zleb (pos one) x
294                                then Zltb x OZ 
295                                else false = false.
296#x lapply (Zltb_to_Zleb_true x OZ)
297elim (Zltb_dec … x OZ)
298[ 1: #Hlt >Hlt #H lapply (H (refl ??)) elim x
299     [ 2,3: #x' ] normalize in ⊢ (% → ?);
300     [ 1: #Habsurd destruct (Habsurd)
301     | 2,3: #_ @refl ]
302| 2: * #Hlt #Hle #_ >Hlt cases (Zleb (pos one) x) @refl ]
303qed.
304
305lemma free_not_valid : ∀x. if Zleb (pos one) x
306                            then Zltb x OZ 
307                            else false = false.
308#x
309cut (Zle (pos one) x ∧ Zlt x OZ → False)
310[ * #H1 #H2 lapply (transitive_Zle … (monotonic_Zle_Zsucc … H1) (Zlt_to_Zle … H2)) #H @H ] #Hguard
311cut (Zleb (pos one) x = true ∧ Zltb x OZ = true → False)
312[ * #H1 #H2 @Hguard @conj /2 by Zleb_true_to_Zle/ @Zltb_true_to_Zlt assumption ]
313cases (Zleb (pos one) x) cases (Zltb x OZ)
314/2 by refl/ #H @(False_ind … (H (conj … (refl ??) (refl ??))))
315qed.
316
317(* follows from (0 ≤ a < b → mod a b = a) *)
318axiom Z_of_unsigned_bitvector_of_small_Z :
319  ∀z. OZ ≤ z → z < Z_two_power_nat 16 → Z_of_unsigned_bitvector 16 (bitvector_of_Z 16 z) = z.
320
321theorem Zle_to_Zlt_to_Zlt: ∀n,m,p:Z. n ≤ m → m < p → n < p.
322#n #m #p #Hle #Hlt /4 by monotonic_Zle_Zplus_r, Zle_to_Zlt, Zlt_to_Zle, transitive_Zle/
323qed.
324
325(* --------------------------------------------------------------------------- *)
326(* Useful facts on blocks. *)
327(* --------------------------------------------------------------------------- *)
328
329lemma eq_block_to_refl : ∀b1,b2. eq_block b1 b2 = true → b1 = b2.
330#b1 #b2 @(eq_block_elim … b1 b2)
331[ //
332| #_ #Habsurd destruct ] qed.
333
334lemma not_eq_block : ∀b1,b2. b1 ≠ b2 → eq_block b1 b2 = false.
335#b1 #b2 #Hneq
336@(eq_block_elim … b1 b2)
337[ 1: #Heq destruct elim Hneq #H @(False_ind … (H (refl ??)))
338| 2: #_ @refl ] qed.
339
340lemma not_eq_block_rev : ∀b1,b2. b2 ≠ b1 → eq_block b1 b2 = false.
341#b1 #b2 #Hneq
342@(eq_block_elim … b1 b2)
343[ 1: #Heq destruct elim Hneq #H @(False_ind … (H (refl ??)))
344| 2: #_ @refl ] qed.
345
346definition block_DeqSet : DeqSet ≝ mk_DeqSet block eq_block ?.
347* #r1 #id1 * #r2 #id2 @(eqZb_elim … id1 id2)
348[ 1: #Heq >Heq cases r1 cases r2 normalize
349     >eqZb_z_z normalize try // @conj #H destruct (H)
350     try @refl
351| 2: #Hneq cases r1 cases r2 normalize
352     >(eqZb_false … Hneq) normalize @conj
353     #H destruct (H) elim Hneq #H @(False_ind … (H (refl ??)))
354] qed.
355
356(* --------------------------------------------------------------------------- *)
357(* General results on lists. *)
358(* --------------------------------------------------------------------------- *)
359
360let rec mem_assoc_env (i : ident) (l : list (ident×type)) on l : bool ≝
361match l with
362[ nil ⇒ false
363| cons hd tl ⇒
364  let 〈id, ty〉 ≝ hd in
365  match identifier_eq SymbolTag i id with
366  [ inl Hid_eq ⇒ true
367  | inr Hid_neq ⇒ mem_assoc_env i tl 
368  ]
369].
370
371(* If mem succeeds in finding an element, then the list can be partitioned around this element. *)
372lemma list_mem_split : ∀A. ∀l : list A. ∀x : A. mem … x l → ∃l1,l2. l = l1 @ [x] @ l2.
373#A #l elim l
374[ 1: normalize #x @False_ind
375| 2: #hd #tl #Hind #x whd in ⊢ (% → ?); *
376     [ 1: #Heq %{(nil ?)} %{tl} destruct @refl
377     | 2: #Hmem lapply (Hind … Hmem) * #l1 * #l2 #Heq_tl >Heq_tl
378          %{(hd :: l1)} %{l2} @refl
379     ]
380] qed.
381
382lemma cons_to_append : ∀A. ∀hd : A. ∀l : list A. hd :: l = [hd] @ l. #A #hd #l @refl qed.
383
384lemma fold_append :
385  ∀A,B:Type[0]. ∀l1, l2 : list A. ∀f:A → B → B. ∀seed. foldr ?? f seed (l1 @ l2) = foldr ?? f (foldr ?? f seed l2) l1.
386#A #B #l1 elim l1 //
387#hd #tl #Hind #l2 #f #seed normalize >Hind @refl
388qed.
389
390lemma filter_append : ∀A:Type[0]. ∀l1,l2 : list A. ∀f. filter ? f (l1 @ l2) = (filter ? f l1) @ (filter ? f l2).
391#A #l1 elim l1 //
392#hd #tl #Hind #l2 #f
393>cons_to_append >associative_append
394normalize cases (f hd) normalize
395<Hind @refl
396qed.
397
398lemma filter_cons_unfold : ∀A:Type[0]. ∀f. ∀hd,tl.
399  filter ? f (hd :: tl) =
400  if f hd then
401    (hd :: filter A f tl)
402  else
403    (filter A f tl).
404#A #f #hd #tl elim tl // qed.
405
406
407lemma filter_elt_empty : ∀A:DeqSet. ∀elt,l. filter (carr A) (λx.¬(x==elt)) l = [ ] → All (carr A) (λx. x = elt) l.
408#A #elt #l elim l
409[ 1: //
410| 2: #hd #tl #Hind >filter_cons_unfold
411     lapply (eqb_true A hd elt)
412     cases (hd==elt) normalize nodelta
413     [ 2: #_ #Habsurd destruct
414     | 1: * #H1 #H2 #Heq lapply (Hind Heq) #Hall whd @conj //
415          @H1 @refl
416     ]
417] qed.
418
419lemma nil_append : ∀A. ∀(l : list A). [ ] @ l = l. // qed.
420
421alias id "meml" = "cic:/matita/basics/lists/list/mem.fix(0,2,1)".
422
423lemma mem_append : ∀A:Type[0]. ∀elt : A. ∀l1,l2. mem … elt (l1 @ l2) ↔ (mem … elt l1) ∨ (mem … elt l2).
424#A #elt #l1 elim l1
425[ 1: #l2 normalize @conj [ 1: #H %2 @H | 2: * [ 1: @False_ind | 2: #H @H ] ]
426| 2: #hd #tl #Hind #l2 @conj
427     [ 1: whd in match (meml ???); *
428          [ 1: #Heq >Heq %1 normalize %1 @refl
429          | 2: #H1 lapply (Hind l2) * #HA #HB normalize
430               elim (HA H1)
431               [ 1: #H %1 %2 assumption | 2: #H %2 assumption ]
432          ]
433     | 2: normalize *
434          [ 1: * /2 by or_introl, or_intror/
435               #H %2 elim (Hind l2) #_ #H' @H' %1 @H
436          | 2: #H %2 elim (Hind l2) #_ #H' @H' %2 @H ]
437     ]
438] qed.
439
440lemma mem_append_forward : ∀A:Type[0]. ∀elt : A. ∀l1,l2. mem … elt (l1 @ l2) → (mem … elt l1) ∨ (mem … elt l2).
441#A #elt #l1 #l2 #H elim (mem_append … elt l1 l2) #H' #_ @H' @H qed.
442
443lemma mem_append_backwards : ∀A:Type[0]. ∀elt : A. ∀l1,l2. (mem … elt l1) ∨ (mem … elt l2) → mem … elt (l1 @ l2) .
444#A #elt #l1 #l2 #H elim (mem_append … elt l1 l2) #_ #H' @H' @H qed.
445
446(* "Observational" equivalence on list implies concrete equivalence. Useful to
447 * prove equality from some reasoning on indexings. Needs a particular induction
448 * principle. *)
449 
450let rec double_list_ind
451  (A : Type[0])
452  (P : list A → list A → Prop)
453  (base_nil  : P [ ] [ ])
454  (base_l1   : ∀hd1,l1. P (hd1::l1) [ ])
455  (base_l2   : ∀hd2,l2. P [ ] (hd2::l2))
456  (ind  : ∀hd1,hd2,tl1,tl2. P tl1 tl2 → P (hd1 :: tl1) (hd2 :: tl2))
457  (l1, l2 : list A) on l1 ≝
458match l1 with
459[ nil ⇒
460  match l2 with
461  [ nil ⇒ base_nil
462  | cons hd2 tl2 ⇒ base_l2 hd2 tl2 ]
463| cons hd1 tl1 ⇒ 
464  match l2 with
465  [ nil ⇒ base_l1 hd1 tl1
466  | cons hd2 tl2 ⇒
467    ind hd1 hd2 tl1 tl2 (double_list_ind A P base_nil base_l1 base_l2 ind tl1 tl2)
468  ]
469]. 
470
471lemma nth_eq_tl :
472  ∀A:Type[0]. ∀l1,l2:list A. ∀hd1,hd2.
473  (∀i. nth_opt A i (hd1::l1) = nth_opt A i (hd2::l2)) →
474  (∀i. nth_opt A i l1 = nth_opt A i l2).
475#A #l1 #l2 @(double_list_ind … l1 l2)
476[ 1: #hd1 #hd2 #_ #i elim i try /2/
477| 2: #hd1' #l1' #hd1 #hd2 #H lapply (H 1) normalize #Habsurd destruct
478| 3: #hd2' #l2' #hd1 #hd2 #H lapply (H 1) normalize #Habsurd destruct
479| 4: #hd1' #hd2' #tl1' #tl2' #H #hd1 #hd2
480     #Hind
481     @(λi. Hind (S i))
482] qed.     
483
484
485lemma nth_eq_to_eq :
486  ∀A:Type[0]. ∀l1,l2:list A. (∀i. nth_opt A i l1 = nth_opt A i l2) → l1 = l2.
487#A #l1 elim l1
488[ 1: #l2 #H lapply (H 0) normalize
489     cases l2
490     [ 1: //
491     | 2: #hd2 #tl2 normalize #Habsurd destruct ]
492| 2: #hd1 #tl1 #Hind *
493     [ 1: #H lapply (H 0) normalize #Habsurd destruct
494     | 2: #hd2 #tl2 #H lapply (H 0) normalize #Heq destruct (Heq)
495          >(Hind tl2) try @refl @(nth_eq_tl … H)
496     ]
497] qed.
498
499(* --------------------------------------------------------------------------- *)
500(* General results on vectors. *)
501(* --------------------------------------------------------------------------- *)
502
503(* copied from AssemblyProof, TODO get rid of the redundant stuff. *)
504lemma Vector_O: ∀A. ∀v: Vector A 0. v ≃ VEmpty A.
505 #A #v generalize in match (refl … 0); cases v in ⊢ (??%? → ?%%??); //
506 #n #hd #tl #abs @⊥ destruct (abs)
507qed.
508
509lemma Vector_Sn: ∀A. ∀n.∀v:Vector A (S n).
510 ∃hd.∃tl.v ≃ VCons A n hd tl.
511 #A #n #v generalize in match (refl … (S n)); cases v in ⊢ (??%? → ??(λ_.??(λ_.?%%??)));
512 [ #abs @⊥ destruct (abs)
513 | #m #hd #tl #EQ <(injective_S … EQ) %[@hd] %[@tl] // ]
514qed.
515
516lemma vector_append_zero:
517  ∀A,m.
518  ∀v: Vector A m.
519  ∀q: Vector A 0.
520    v = q@@v.
521  #A #m #v #q
522  >(Vector_O A q) %
523qed.
524
525corollary prod_vector_zero_eq_left:
526  ∀A, n.
527  ∀q: Vector A O.
528  ∀r: Vector A n.
529    〈q, r〉 = 〈[[ ]], r〉.
530  #A #n #q #r
531  generalize in match (Vector_O A q …);
532  #hyp
533  >hyp in ⊢ (??%?);
534  %
535qed.
536 
537lemma vsplit_eq : ∀A. ∀m,n. ∀v : Vector A ((S m) + n).  ∃v1:Vector A (S m). ∃v2:Vector A n. v = v1 @@ v2.
538# A #m #n elim m
539[ 1: normalize #v
540  elim (Vector_Sn ?? v) #hd * #tl #Eq
541  @(ex_intro … (hd ::: [[]])) @(ex_intro … tl)
542  >Eq normalize //
543| 2: #n' #Hind #v
544  elim (Vector_Sn ?? v) #hd * #tl #Eq
545  elim (Hind tl)
546  #tl1 * #tl2 #Eq_tl
547  @(ex_intro … (hd ::: tl1))
548  @(ex_intro … tl2) 
549  destruct normalize //
550] qed.
551
552lemma vsplit_zero:
553  ∀A,m.
554  ∀v: Vector A m.
555    〈[[]], v〉 = vsplit A 0 m v.
556  #A #m #v
557  elim v
558  [ %
559  | #n #hd #tl #ih
560    normalize in ⊢ (???%); %
561  ]
562qed.
563
564lemma vsplit_zero2:
565  ∀A,m.
566  ∀v: Vector A m.
567    〈[[]], v〉 = vsplit' A 0 m v.
568  #A #m #v
569  elim v
570  [ %
571  | #n #hd #tl #ih
572    normalize in ⊢ (???%); %
573  ]
574qed.
575
576lemma vsplit_eq2 : ∀A. ∀m,n : nat. ∀v : Vector A (m + n).  ∃v1:Vector A m. ∃v2:Vector A n. v = v1 @@ v2.
577# A #m #n elim m
578[ 1: normalize #v @(ex_intro … (VEmpty ?)) @(ex_intro … v) normalize //
579| 2: #n' #Hind #v
580  elim (Vector_Sn ?? v) #hd * #tl #Eq
581  elim (Hind tl)
582  #tl1 * #tl2 #Eq_tl
583  @(ex_intro … (hd ::: tl1))
584  @(ex_intro … tl2) 
585  destruct normalize //
586] qed.
587
588(* This is not very nice. Note that this axiom was copied verbatim from ASM/AssemblyProof.ma.
589 * TODO sync with AssemblyProof.ma, in a better world we shouldn't have to copy all of this. *)
590axiom vsplit_succ:
591  ∀A, m, n.
592  ∀l: Vector A m.
593  ∀r: Vector A n.
594  ∀v: Vector A (m + n).
595  ∀hd.
596    v = l@@r → (〈l, r〉 = vsplit ? m n v → 〈hd:::l, r〉 = vsplit ? (S m) n (hd:::v)).
597
598axiom vsplit_succ2:
599  ∀A, m, n.
600  ∀l: Vector A m.
601  ∀r: Vector A n.
602  ∀v: Vector A (m + n).
603  ∀hd.
604    v = l@@r → (〈l, r〉 = vsplit' ? m n v → 〈hd:::l, r〉 = vsplit' ? (S m) n (hd:::v)).
605     
606lemma vsplit_prod2:
607  ∀A,m,n.
608  ∀p: Vector A (m + n).
609  ∀v: Vector A m.
610  ∀q: Vector A n.
611    p = v@@q → 〈v, q〉 = vsplit' A m n p.
612  #A #m
613  elim m
614  [ #n #p #v #q #hyp
615    >hyp <(vector_append_zero A n q v)
616    >(prod_vector_zero_eq_left A …)
617    @vsplit_zero2
618  | #r #ih #n #p #v #q #hyp
619    >hyp
620    cases (Vector_Sn A r v)
621    #hd #exists
622    cases exists
623    #tl #jmeq >jmeq
624    @vsplit_succ2 [1: % |2: @ih % ]
625  ]
626qed.
627
628lemma vsplit_prod:
629  ∀A,m,n.
630  ∀p: Vector A (m + n).
631  ∀v: Vector A m.
632  ∀q: Vector A n.
633    p = v@@q → 〈v, q〉 = vsplit A m n p.
634  #A #m
635  elim m
636  [ #n #p #v #q #hyp
637    >hyp <(vector_append_zero A n q v)
638    >(prod_vector_zero_eq_left A …)
639    @vsplit_zero
640  | #r #ih #n #p #v #q #hyp
641    >hyp
642    cases (Vector_Sn A r v)
643    #hd #exists
644    cases exists
645    #tl #jmeq >jmeq
646    @vsplit_succ [1: % |2: @ih % ]
647  ]
648qed.
649
650(* --------------------------------------------------------------------------- *)
651(* Some more stuff on bitvectors. *)
652(* --------------------------------------------------------------------------- *)
653
654axiom commutative_multiplication :
655  ∀n. ∀v1,v2:BitVector n.
656  multiplication ? v1 v2 = multiplication ? v2 v1.
657
658lemma commutative_short_multiplication :
659  ∀n. ∀v1,v2:BitVector n.
660  short_multiplication ? v1 v2 = short_multiplication ? v2 v1.
661#n #v1 #v2 whd in ⊢ (??%%); >commutative_multiplication @refl
662qed.
663
664lemma sign_ext_same_size : ∀n,v. sign_ext n n v = v.
665#n #v whd in match (sign_ext ???); >nat_compare_eq @refl
666qed.
667
668axiom sign_ext_zero : ∀sz1,sz2. sign_ext sz1 sz2 (zero sz1) = zero sz2.
669
670axiom zero_ext_zero : ∀sz1,sz2. zero_ext sz1 sz2 (zero sz1) = zero sz2.
671
672axiom multiplication_zero : ∀n:nat. ∀v : BitVector n. multiplication … (zero ?) v = (zero ?).
673
674axiom short_multiplication_zero : ∀n. ∀v:BitVector n. short_multiplication ? (zero ?) v = (zero ?).
675
676(* lemma eq_v_to_eq_Z : ∀n. ∀v1,v2:BitVector n. (Z_of_bitvector … v1) = (Z_of_bitvector eq_bv … v1 v2. *)
677
678
679(* --------------------------------------------------------------------------- *)
680(* Generic properties of equivalence relations *)
681(* --------------------------------------------------------------------------- *)
682
683lemma triangle_diagram :
684  ∀acctype : Type[0].
685  ∀eqrel : acctype → acctype → Prop.
686  ∀refl_eqrel  : reflexive ? eqrel.
687  ∀trans_eqrel : transitive ? eqrel.
688  ∀sym_eqrel   : symmetric ? eqrel.
689  ∀a,b,c. eqrel a b → eqrel a c → eqrel b c.
690#acctype #eqrel #R #T #S #a #b #c
691#H1 #H2 @(T … (S … H1) H2)
692qed.
693
694lemma cotriangle_diagram :
695  ∀acctype : Type[0].
696  ∀eqrel : acctype → acctype → Prop.
697  ∀refl_eqrel  : reflexive ? eqrel.
698  ∀trans_eqrel : transitive ? eqrel.
699  ∀sym_eqrel   : symmetric ? eqrel.
700  ∀a,b,c. eqrel b a → eqrel c a → eqrel b c.
701#acctype #eqrel #R #T #S #a #b #c
702#H1 #H2 @S @(T … H2 (S … H1))
703qed.
704
705(* --------------------------------------------------------------------------- *)
706(* Quick and dirty implementation of finite sets relying on lists. The
707 * implementation is split in two: an abstract equivalence defined using inclusion
708 * of lists, and a concrete one where equivalence is defined as the closure of
709 * duplication, contraction and transposition of elements. We rely on the latter
710 * to prove stuff on folds over sets.  *)
711(* --------------------------------------------------------------------------- *)
712
713definition lset ≝ λA:Type[0]. list A.
714
715(* The empty set. *)
716definition empty_lset ≝ λA:Type[0]. nil A.
717
718(* Standard operations. *)
719definition lset_union ≝ λA:Type[0].λl1,l2 : lset A. l1 @ l2.
720
721definition lset_remove ≝ λA:DeqSet.λl:lset (carr A).λelt:carr A. (filter … (λx.¬x==elt) l).
722
723definition lset_difference ≝ λA:DeqSet.λl1,l2:lset (carr A). (filter … (λx.¬ (memb ? x l2)) l1).
724
725(* Standard predicates on sets *)
726definition lset_in ≝ λA:Type[0].λx : A. λl : lset A. mem … x l.
727
728definition lset_disjoint ≝ λA:Type[0].λl1, l2 : lset A.
729  ∀x,y. mem … x l1 → mem … y l2 → x ≠ y.
730 
731definition lset_inclusion ≝ λA:Type[0].λl1,l2.
732  All A (λx1. mem … x1 l2) l1.
733
734(* Definition of abstract set equivalence. *)
735definition lset_eq ≝ λA:Type[0].λl1,l2.
736  lset_inclusion A l1 l2 ∧
737  lset_inclusion A l2 l1.
738
739(* Properties of inclusion. *) 
740lemma reflexive_lset_inclusion : ∀A. ∀l. lset_inclusion A l l.
741#A #l elim l try //
742#hd #tl #Hind whd @conj
743[ 1: %1 @refl
744| 2: whd in Hind; @(All_mp … Hind)
745     #a #H whd %2 @H
746] qed.
747
748lemma transitive_lset_inclusion : ∀A. ∀l1,l2,l3. lset_inclusion A l1 l2 → lset_inclusion A l2 l3 → lset_inclusion A l1 l3 .
749#A #l1 #l2 #l3
750#Hincl1 #Hincl2 @(All_mp … Hincl1)
751whd in Hincl2;
752#a elim l2 in Hincl2;
753[ 1: normalize #_ @False_ind
754| 2: #hd #tl #Hind whd in ⊢ (% → ?);
755     * #Hmem #Hincl_tl whd in ⊢ (% → ?);
756     * [ 1: #Heq destruct @Hmem
757       | 2: #Hmem_tl @Hind assumption ]
758] qed.
759
760lemma cons_preserves_inclusion : ∀A. ∀l1,l2. lset_inclusion A l1 l2 → ∀x. lset_inclusion A l1 (x::l2).
761#A #l1 #l2 #Hincl #x @(All_mp … Hincl) #a #Hmem whd %2 @Hmem qed.
762
763lemma cons_monotonic_inclusion : ∀A. ∀l1,l2. lset_inclusion A l1 l2 → ∀x. lset_inclusion A (x::l1) (x::l2).
764#A #l1 #l2 #Hincl #x whd @conj
765[ 1: /2 by or_introl/
766| 2: @(All_mp … Hincl) #a #Hmem whd %2 @Hmem ] qed.
767
768lemma lset_inclusion_concat : ∀A. ∀l1,l2. lset_inclusion A l1 l2 → ∀l3. lset_inclusion A l1 (l3 @ l2).
769#A #l1 #l2 #Hincl #l3 elim l3
770try /2 by cons_preserves_inclusion/
771qed.
772
773lemma lset_inclusion_concat_monotonic : ∀A. ∀l1,l2. lset_inclusion A l1 l2 → ∀l3. lset_inclusion A (l3 @ l1) (l3 @ l2).
774#A #l1 #l2 #Hincl #l3 elim l3
775try @Hincl #hd #tl #Hind @cons_monotonic_inclusion @Hind
776qed.
777 
778(* lset_eq is an equivalence relation. *)
779lemma reflexive_lset_eq : ∀A. ∀l. lset_eq A l l. /2 by conj/ qed.
780
781lemma transitive_lset_eq : ∀A. ∀l1,l2,l3. lset_eq A l1 l2 → lset_eq A l2 l3 → lset_eq A l1 l3.
782#A #l1 #l2 #l3 * #Hincl1 #Hincl2 * #Hincl3 #Hincl4
783@conj @(transitive_lset_inclusion ??l2) assumption
784qed.
785
786lemma symmetric_lset_eq : ∀A. ∀l1,l2. lset_eq A l1 l2 → lset_eq A l2 l1.
787#A #l1 #l2 * #Hincl1 #Hincl2 @conj assumption
788qed.
789
790(* Properties of inclusion vs union and equality. *)
791lemma lset_union_inclusion : ∀A:Type[0]. ∀a,b,c. 
792  lset_inclusion A a c → lset_inclusion ? b c → lset_inclusion ? (lset_union ? a b) c.
793#A #a #b #c #H1 #H2 whd whd in match (lset_union ???);
794@All_append assumption qed.
795
796lemma lset_inclusion_union : ∀A:Type[0]. ∀a,b,c. 
797  lset_inclusion A a b ∨ lset_inclusion A a c → lset_inclusion ? a (lset_union ? b c).
798#A #a #b #c *
799[ 1: @All_mp #elt #Hmem @mem_append_backwards %1 @Hmem
800| 2: @All_mp #elt #Hmem @mem_append_backwards %2 @Hmem
801] qed.
802
803lemma lset_inclusion_eq : ∀A : Type[0]. ∀a,b,c : lset A.
804  lset_eq ? a b → lset_inclusion ? b c → lset_inclusion ? a c.
805#A #a #b #c * #H1 #H2 #H3 @(transitive_lset_inclusion … H1 H3)
806qed.
807
808lemma lset_inclusion_eq2 : ∀A : Type[0]. ∀a,b,c : lset A.
809  lset_eq ? b c → lset_inclusion ? a b → lset_inclusion ? a c.
810#A #a #b #c * #H1 #H2 #H3 @(transitive_lset_inclusion … H3 H1)
811qed.
812
813(* Properties of lset_eq and mem *)
814lemma lset_eq_mem :
815  ∀A:Type[0].
816  ∀s1,s2 : lset A.
817  lset_eq ? s1 s2 →
818  ∀b.mem ? b s1 → mem ? b s2.
819#A #s1 #s2 * #Hincl12 #_ #b
820whd in Hincl12; elim s1 in Hincl12;
821[ 1: normalize #_ *
822| 2: #hd #tl #Hind whd in ⊢ (% → % → ?); * #Hmem' #Hall * #Heq
823     [ 1: destruct (Heq) assumption
824     | 2: @Hind assumption ]
825] qed.
826
827lemma lset_eq_memb :
828  ∀A : DeqSet.
829  ∀s1,s2 : lset (carr A).
830  lset_eq ? s1 s2 →
831  ∀b.memb ? b s1 = true → memb ? b s2 = true.
832#A #s1 #s2 #Heq #b
833lapply (memb_to_mem A s1 b) #H1 #H2
834lapply (H1 H2) #Hmem lapply (lset_eq_mem … Heq ? Hmem) @mem_to_memb
835qed.
836
837lemma lset_eq_memb_eq :
838  ∀A : DeqSet.
839  ∀s1,s2 : lset (carr A).
840  lset_eq ? s1 s2 →
841  ∀b.memb ? b s1 = memb ? b s2.
842#A #s1 #s2 #Hlset_eq #b
843lapply (lset_eq_memb … Hlset_eq b)
844lapply (lset_eq_memb … (symmetric_lset_eq … Hlset_eq) b) 
845cases (b∈s1)
846[ 1: #_ #H lapply (H (refl ??)) #Hmem >H @refl
847| 2: cases (b∈s2) // #H lapply (H (refl ??)) #Habsurd destruct
848] qed.
849
850lemma lset_eq_filter_eq :
851  ∀A : DeqSet.
852  ∀s1,s2 : lset (carr A).
853  lset_eq ? s1 s2 → 
854  ∀l.
855     (filter ? (λwb.¬wb∈s1) l) =
856     (filter ? (λwb.¬wb∈s2) l).
857#A #s1 #s2 #Heq #l elim l
858[ 1: @refl
859| 2: #hd #tl #Hind >filter_cons_unfold >filter_cons_unfold
860      >(lset_eq_memb_eq … Heq) cases (hd∈s2)
861      normalize in match (notb ?); normalize nodelta
862      try @Hind >Hind @refl
863] qed.
864
865lemma cons_monotonic_eq : ∀A. ∀l1,l2 : lset A. lset_eq A l1 l2 → ∀x. lset_eq A (x::l1) (x::l2).
866#A #l1 #l2 #Heq #x cases Heq #Hincl1 #Hincl2
867@conj
868[ 1: @cons_monotonic_inclusion
869| 2: @cons_monotonic_inclusion ]
870assumption
871qed.
872
873(* Properties of difference and remove *)
874lemma lset_difference_unfold :
875  ∀A : DeqSet.
876  ∀s1, s2 : lset (carr A).
877  ∀hd. lset_difference A (hd :: s1) s2 =
878    if hd∈s2 then
879      lset_difference A s1 s2
880    else
881      hd :: (lset_difference A s1 s2).
882#A #s1 #s2 #hd normalize
883cases (hd∈s2) @refl
884qed.
885
886lemma lset_difference_unfold2 :
887  ∀A : DeqSet.
888  ∀s1, s2 : lset (carr A).
889  ∀hd. lset_difference A s1 (hd :: s2) =
890       lset_difference A (lset_remove ? s1 hd) s2.
891#A #s1
892elim s1
893[ 1: //
894| 2: #hd1 #tl1 #Hind #s2 #hd
895     whd in match (lset_remove ???);
896     whd in match (lset_difference A ??);
897     whd in match (memb ???);
898     lapply (eqb_true … hd1 hd)
899     cases (hd1==hd) normalize nodelta
900     [ 1: * #H #_ lapply (H (refl ??)) -H #H
901           @Hind
902     | 2: * #_ #Hguard >lset_difference_unfold
903          cases (hd1∈s2) normalize in match (notb ?); normalize nodelta
904          <Hind @refl ]
905] qed.         
906
907lemma lset_difference_disjoint :
908 ∀A : DeqSet.
909 ∀s1,s2 : lset (carr A).
910  lset_disjoint A s1 (lset_difference A s2 s1).
911#A #s1 elim s1
912[ 1: #s2 normalize #x #y *
913| 2: #hd1 #tl1 #Hind #s2 >lset_difference_unfold2 #x #y
914     whd in ⊢ (% → ?); *
915     [ 2: @Hind
916     | 1: #Heq >Heq elim s2
917          [ 1: normalize *
918          | 2: #hd2 #tl2 #Hind2 whd in match (lset_remove ???);
919               lapply (eqb_true … hd2 hd1)
920               cases (hd2 == hd1) normalize in match (notb ?); normalize nodelta * #H1 #H2
921               [ 1: @Hind2
922               | 2: >lset_difference_unfold cases (hd2 ∈ tl1) normalize nodelta try @Hind2
923                     whd in ⊢ (% → ?); *
924                     [ 1: #Hyhd2 destruct % #Heq lapply (H2 … (sym_eq … Heq)) #Habsurd destruct
925                     | 2: @Hind2 ]
926               ]
927          ]
928    ]
929] qed.
930
931
932lemma lset_remove_split : ∀A : DeqSet. ∀l1,l2 : lset A. ∀elt. lset_remove A (l1 @ l2) elt = (lset_remove … l1 elt) @ (lset_remove … l2 elt).
933#A #l1 #l2 #elt /2 by filter_append/ qed.
934
935lemma lset_inclusion_remove :
936  ∀A : DeqSet.
937  ∀s1, s2 : lset A.
938  lset_inclusion ? s1 s2 →
939  ∀elt. lset_inclusion ? (lset_remove ? s1 elt) (lset_remove ? s2 elt).
940#A #s1 elim s1
941[ 1: normalize //
942| 2: #hd1 #tl1 #Hind1 #s2 * #Hmem #Hincl
943     elim (list_mem_split ??? Hmem) #s2A * #s2B #Hs2_eq destruct #elt
944     whd in match (lset_remove ???);
945     @(match (hd1 == elt)
946       return λx. (hd1 == elt = x) → ?
947       with
948       [ true ⇒ λH. ?
949       | false ⇒ λH. ? ] (refl ? (hd1 == elt))) normalize in match (notb ?);
950     normalize nodelta
951     [ 1:  @Hind1 @Hincl
952     | 2: whd @conj
953          [ 2: @(Hind1 … Hincl)
954          | 1: >lset_remove_split >lset_remove_split
955               normalize in match (lset_remove A [hd1] elt);
956               >H normalize nodelta @mem_append_backwards %2
957               @mem_append_backwards %1 normalize %1 @refl ]
958     ]
959] qed.
960
961lemma lset_difference_lset_eq :
962  ∀A : DeqSet. ∀a,b,c.
963   lset_eq A b c →
964   lset_eq A (lset_difference A a b) (lset_difference A a c).
965#A #a #b #c #Heq
966whd in match (lset_difference ???) in ⊢ (??%%);   
967elim a
968[ 1: normalize @conj @I
969| 2: #hda #tla #Hind whd in match (foldr ?????) in ⊢ (??%%);
970     >(lset_eq_memb_eq … Heq hda) cases (hda∈c)
971     normalize in match (notb ?); normalize nodelta
972     try @Hind @cons_monotonic_eq @Hind
973] qed.
974
975lemma lset_difference_lset_remove_commute :
976  ∀A:DeqSet.
977  ∀elt,s1,s2.
978  (lset_difference A (lset_remove ? s1 elt) s2) =
979  (lset_remove A (lset_difference ? s1 s2) elt).
980#A #elt #s1 #s2
981elim s1 try //
982#hd #tl #Hind
983>lset_difference_unfold
984whd in match (lset_remove ???);
985@(match (hd==elt) return λx. (hd==elt) = x → ?
986  with
987  [ true ⇒ λHhd. ?
988  | false ⇒ λHhd. ?
989  ] (refl ? (hd==elt)))
990@(match (hd∈s2) return λx. (hd∈s2) = x → ?
991  with
992  [ true ⇒ λHmem. ?
993  | false ⇒ λHmem. ?
994  ] (refl ? (hd∈s2)))
995>notb_true >notb_false normalize nodelta try //
996try @Hind
997[ 1:  whd in match (lset_remove ???) in ⊢ (???%); >Hhd
998     elim (eqb_true ? elt elt) #_ #H >(H (refl ??))
999     normalize in match (notb ?); normalize nodelta @Hind
1000| 2: >lset_difference_unfold >Hmem @Hind
1001| 3: whd in match (lset_remove ???) in ⊢ (???%);
1002     >lset_difference_unfold >Hhd >Hmem
1003     normalize in match (notb ?);
1004     normalize nodelta >Hind @refl
1005] qed.
1006
1007(* Inversion lemma on emptyness *)
1008lemma lset_eq_empty_inv : ∀A. ∀l. lset_eq A l [ ] → l = [ ].
1009#A #l elim l //
1010#hd' #tl' normalize #Hind * * @False_ind
1011qed.
1012
1013(* Inversion lemma on singletons *)
1014lemma lset_eq_singleton_inv : ∀A,hd,l. lset_eq A [hd] (hd::l) → All ? (λx.x=hd) l.
1015#A #hd #l
1016* #Hincl1 whd in ⊢ (% → ?); * #_ @All_mp
1017normalize #a * [ 1: #H @H | 2: @False_ind ]
1018qed.
1019
1020(* Permutation of two elements on top of the list is ok. *)
1021lemma lset_eq_permute : ∀A,l,x1,x2. lset_eq A (x1 :: x2 :: l) (x2 :: x1 :: l).
1022#A #l #x1 #x2 @conj normalize
1023[ 1: /5 by cons_preserves_inclusion, All_mp, or_introl, or_intror, conj/
1024| 2: /5 by cons_preserves_inclusion, All_mp, or_introl, or_intror, conj/
1025] qed.
1026
1027(* "contraction" of an element. *)
1028lemma lset_eq_contract : ∀A,l,x. lset_eq A (x :: x :: l) (x :: l).
1029#A #l #x @conj
1030[ 1: /5 by or_introl, conj, transitive_lset_inclusion/
1031| 2: /5 by cons_preserves_inclusion, cons_monotonic_inclusion/ ]
1032qed.
1033
1034(* We don't need more than one instance of each element. *)
1035lemma lset_eq_filter : ∀A:DeqSet.∀tl.∀hd.
1036  lset_eq A (hd :: (filter ? (λy.notb (eqb A y hd)) tl)) (hd :: tl).
1037#A #tl elim tl
1038[ 1: #hd normalize /4 by or_introl, conj, I/
1039| 2: #hd' #tl' #Hind #hd >filter_cons_unfold
1040     lapply (eqb_true A hd' hd) cases (hd'==hd)
1041     [ 2: #_ normalize in match (notb ?); normalize nodelta
1042          lapply (cons_monotonic_eq … (Hind hd) hd') #H
1043          lapply (lset_eq_permute ? tl' hd' hd) #H'
1044          @(transitive_lset_eq ? ? (hd'::hd::tl') ? … H')
1045          @(transitive_lset_eq ? ?? (hd'::hd::tl') … H)
1046          @lset_eq_permute
1047     | 1: * #Heq #_ >(Heq (refl ??)) normalize in match (notb ?); normalize nodelta
1048          lapply (Hind hd) #H
1049          @(transitive_lset_eq ? ? (hd::tl') (hd::hd::tl') H)
1050          @conj
1051          [ 1: whd @conj /2 by or_introl/ @cons_preserves_inclusion @cons_preserves_inclusion
1052               @reflexive_lset_inclusion
1053          | 2: whd @conj /2 by or_introl/ ]
1054     ]
1055] qed.
1056
1057lemma lset_inclusion_filter_self :
1058  ∀A:DeqSet.∀l,pred.
1059    lset_inclusion A (filter ? pred l) l.
1060#A #l #pred elim l
1061[ 1: normalize @I
1062| 2: #hd #tl #Hind whd in match (filter ???);
1063     cases (pred hd) normalize nodelta
1064     [ 1: @cons_monotonic_inclusion @Hind
1065     | 2: @cons_preserves_inclusion @Hind ]
1066] qed.   
1067
1068lemma lset_inclusion_filter_monotonic :
1069  ∀A:DeqSet.∀l1,l2. lset_inclusion (carr A) l1 l2 →
1070  ∀elt. lset_inclusion A (filter ? (λx.¬(x==elt)) l1) (filter ? (λx.¬(x==elt)) l2).
1071#A #l1 elim l1
1072[ 1: #l2 normalize //
1073| 2: #hd1 #tl1 #Hind #l2 whd in ⊢ (% → ?); * #Hmem1 #Htl1_incl #elt
1074     whd >filter_cons_unfold
1075     lapply (eqb_true A hd1 elt) cases (hd1==elt)
1076     [ 1: * #H1 #_ lapply (H1 (refl ??)) #H1_eq >H1_eq in Hmem1; #Hmem
1077          normalize in match (notb ?); normalize nodelta @Hind assumption
1078     | 2: * #_ #Hneq normalize in match (notb ?); normalize nodelta
1079          whd @conj
1080          [ 1: elim l2 in Hmem1; try //
1081               #hd2 #tl2 #Hincl whd in ⊢ (% → ?); *
1082               [ 1: #Heq >Heq in Hneq; normalize
1083                    lapply (eqb_true A hd2 elt) cases (hd2==elt)
1084                    [ 1: * #H #_ #H2 lapply (H2 (H (refl ??))) #Habsurd destruct (Habsurd)
1085                    | 2: #_ normalize nodelta #_ /2 by or_introl/ ]
1086               | 2: #H lapply (Hincl H) #Hok
1087                    normalize cases (hd2==elt) normalize nodelta
1088                    [ 1: @Hok
1089                    | 2: %2 @Hok ] ]
1090          | 2: @Hind assumption ] ] ]
1091qed.
1092
1093(* removing an element of two equivalent sets conserves equivalence. *)
1094lemma lset_eq_filter_monotonic :
1095  ∀A:DeqSet.∀l1,l2. lset_eq (carr A) l1 l2 →
1096  ∀elt. lset_eq A (filter ? (λx.¬(x==elt)) l1) (filter ? (λx.¬(x==elt)) l2).
1097#A #l1 #l2 * #Hincl1 #Hincl2 #elt @conj
1098/2 by lset_inclusion_filter_monotonic/
1099qed.
1100
1101(* ---------------- Concrete implementation of sets --------------------- *)
1102
1103(* We can give an explicit characterization of equivalent sets: they are permutations with repetitions, i,e.
1104   a composition of transpositions and duplications. We restrict ourselves to DeqSets. *)
1105inductive iso_step_lset (A : DeqSet) : lset A → lset A → Prop ≝
1106| lset_transpose : ∀a,x,b,y,c. iso_step_lset A (a @ [x] @ b @ [y] @ c) (a @ [y] @ b @ [x] @ c)
1107| lset_weaken : ∀a,x,b. iso_step_lset A (a @ [x] @ b) (a @ [x] @ [x] @ b)
1108| lset_contract : ∀a,x,b. iso_step_lset A (a @ [x] @ [x] @ b) (a @ [x] @ b).
1109
1110(* The equivalence is the reflexive, transitive and symmetric closure of iso_step_lset *)
1111inductive lset_eq_concrete (A : DeqSet) : lset A → lset A → Prop ≝
1112| lset_trans : ∀a,b,c. iso_step_lset A a b → lset_eq_concrete A b c → lset_eq_concrete A a c
1113| lset_refl  : ∀a. lset_eq_concrete A a a.
1114
1115(* lset_eq_concrete is symmetric and transitive *)
1116lemma transitive_lset_eq_concrete : ∀A,l1,l2,l3. lset_eq_concrete A l1 l2 → lset_eq_concrete A l2 l3 → lset_eq_concrete A l1 l3.
1117#A #l1 #l2 #l3 #Hequiv
1118elim Hequiv //
1119#a #b #c #Hstep #Hequiv #Hind #Hcl3 lapply (Hind Hcl3) #Hbl3
1120@(lset_trans ???? Hstep Hbl3)
1121qed.
1122
1123lemma symmetric_step : ∀A,l1,l2. iso_step_lset A l1 l2 → iso_step_lset A l2 l1.
1124#A #l1 #l2 * /2/ qed.
1125
1126lemma symmetric_lset_eq_concrete : ∀A,l1,l2. lset_eq_concrete A l1 l2 → lset_eq_concrete A l2 l1.
1127#A #l1 #l2 #H elim H //
1128#a #b #c #Hab #Hbc #Hcb
1129@(transitive_lset_eq_concrete ???? Hcb ?)
1130@(lset_trans … (symmetric_step ??? Hab) (lset_refl …))
1131qed.
1132 
1133(* lset_eq_concrete is conserved by cons. *)
1134lemma equivalent_step_cons : ∀A,l1,l2. iso_step_lset A l1 l2 → ∀x. iso_step_lset A (x :: l1) (x :: l2).
1135#A #l1 #l2 * // qed. (* That // was impressive. *)
1136
1137lemma lset_eq_concrete_cons : ∀A,l1,l2. lset_eq_concrete A l1 l2 → ∀x. lset_eq_concrete A (x :: l1) (x :: l2).
1138#A #l1 #l2 #Hequiv elim Hequiv try //
1139#a #b #c #Hab #Hbc #Hind #x %1{(equivalent_step_cons ??? Hab x) (Hind x)}
1140qed.
1141
1142lemma absurd_list_eq_append : ∀A.∀x.∀l1,l2:list A. [ ] = l1 @ [x] @ l2 → False.
1143#A #x #l1 #l2 elim l1 normalize
1144[ 1: #Habsurd destruct
1145| 2: #hd #tl #_ #Habsurd destruct
1146] qed.
1147
1148(* Inversion lemma for emptyness, step case *)
1149lemma equivalent_iso_step_empty_inv : ∀A,l. iso_step_lset A l [] → l = [ ].
1150#A #l elim l //
1151#hd #tl #Hind #H inversion H
1152[ 1: #a #x #b #y #c #_ #Habsurd
1153      @(False_ind … (absurd_list_eq_append ? y … Habsurd))
1154| 2: #a #x #b #_ #Habsurd
1155      @(False_ind … (absurd_list_eq_append ? x … Habsurd))
1156| 3: #a #x #b #_ #Habsurd
1157      @(False_ind … (absurd_list_eq_append ? x … Habsurd))
1158] qed.
1159
1160(* Same thing for non-emptyness *)
1161lemma equivalent_iso_step_cons_inv : ∀A,l1,l2. l1 ≠ [ ] → iso_step_lset A l1 l2 → l2 ≠ [ ].
1162#A #l1 elim l1
1163[ 1: #l2 * #H @(False_ind … (H (refl ??)))
1164| 2: #hd #tl #H #l2 #_ #Hstep % #Hl2 >Hl2 in Hstep; #Hstep
1165     lapply (equivalent_iso_step_empty_inv … Hstep) #Habsurd destruct
1166] qed.
1167
1168lemma lset_eq_concrete_cons_inv : ∀A,l1,l2. l1 ≠ [ ] → lset_eq_concrete A l1 l2 → l2 ≠ [ ].
1169#A #l1 #l2 #Hl1 #H lapply Hl1 elim H
1170[ 2: #a #H @H
1171| 1: #a #b #c #Hab #Hbc #H #Ha lapply (equivalent_iso_step_cons_inv … Ha Hab) #Hb @H @Hb
1172] qed.
1173
1174lemma lset_eq_concrete_empty_inv : ∀A,l1,l2. l1 = [ ] → lset_eq_concrete A l1 l2 → l2 = [ ].
1175#A #l1 #l2 #Hl1 #H lapply Hl1 elim H //
1176#a #b #c #Hab #Hbc #Hbc_eq #Ha >Ha in Hab; #H_b lapply (equivalent_iso_step_empty_inv … ?? (symmetric_step … H_b))
1177#Hb @Hbc_eq @Hb
1178qed.
1179
1180(* Square equivalence diagram *)
1181lemma square_lset_eq_concrete :
1182  ∀A. ∀a,b,a',b'. lset_eq_concrete A a b → lset_eq_concrete A a a' → lset_eq_concrete A b b' → lset_eq_concrete A a' b'.
1183#A #a #b #a' #b' #H1 #H2 #H3
1184@(transitive_lset_eq_concrete ???? (symmetric_lset_eq_concrete … H2))
1185@(transitive_lset_eq_concrete ???? H1)
1186@H3
1187qed.
1188
1189(* Make the transposition of elements visible at top-level *)
1190lemma transpose_lset_eq_concrete :
1191  ∀A. ∀x,y,a,b,c,a',b',c'.
1192  lset_eq_concrete A (a @ [x] @ b @ [y] @ c) (a' @ [x] @ b' @ [y] @ c') →
1193  lset_eq_concrete A (a @ [y] @ b @ [x] @ c) (a' @ [y] @ b' @ [x] @ c').
1194#A #x #y #a #b #c #a' #b' #c
1195#H @(square_lset_eq_concrete ????? H) /2 by lset_trans, lset_refl, lset_transpose/
1196qed.
1197
1198lemma switch_lset_eq_concrete :
1199  ∀A. ∀a,b,c. lset_eq_concrete A (a@[b]@c) ([b]@a@c).
1200#A #a elim a //
1201#hda #tla #Hind #b #c lapply (Hind hda c) #H
1202lapply (lset_eq_concrete_cons … H b)
1203#H' normalize in H' ⊢ %; @symmetric_lset_eq_concrete
1204/3 by lset_transpose, lset_trans, symmetric_lset_eq_concrete/
1205qed.
1206
1207(* Folding a commutative and idempotent function on equivalent sets yields the same result. *)
1208lemma lset_eq_concrete_fold :
1209  ∀A : DeqSet.
1210  ∀acctype : Type[0].
1211  ∀l1,l2 : list (carr A).
1212  lset_eq_concrete A l1 l2 →
1213  ∀f:carr A → acctype → acctype.
1214  (∀x1,x2. ∀acc. f x1 (f x2 acc) = f x2 (f x1 acc)) →
1215  (∀x.∀acc. f x (f x acc) = f x acc) →
1216  ∀acc. foldr ?? f acc l1 = foldr ?? f acc l2.
1217#A #acctype #l1 #l2 #Heq #f #Hcomm #Hidem
1218elim Heq
1219try //
1220#a' #b' #c' #Hstep #Hbc #H #acc <H -H
1221elim Hstep
1222[ 1: #a #x #b #y #c
1223     >fold_append >fold_append >fold_append >fold_append
1224     >fold_append >fold_append >fold_append >fold_append
1225     normalize
1226     cut (f x (foldr A acctype f (f y (foldr A acctype f acc c)) b) =
1227          f y (foldr A acctype f (f x (foldr A acctype f acc c)) b)) [   
1228     elim c
1229     [ 1: normalize elim b
1230          [ 1: normalize >(Hcomm x y) @refl
1231          | 2: #hdb #tlb #Hind normalize
1232               >(Hcomm x hdb) >(Hcomm y hdb) >Hind @refl ]
1233     | 2: #hdc #tlc #Hind normalize elim b
1234          [ 1: normalize >(Hcomm x y) @refl
1235          | 2: #hdb #tlb #Hind normalize
1236               >(Hcomm x hdb) >(Hcomm y hdb) >Hind @refl ] ]
1237     ]
1238     #Hind >Hind @refl
1239| 2: #a #x #b
1240     >fold_append >fold_append >fold_append >(fold_append ?? ([x]@[x]))
1241     normalize >Hidem @refl
1242| 3: #a #x #b
1243     >fold_append  >(fold_append ?? ([x]@[x])) >fold_append >fold_append
1244     normalize >Hidem @refl
1245] qed.
1246
1247(* Folding over equivalent sets yields equivalent results, for any equivalence. *)
1248lemma inj_to_fold_inj :
1249  ∀A,acctype : Type[0].
1250  ∀eqrel : acctype → acctype → Prop.
1251  ∀refl_eqrel  : reflexive ? eqrel.
1252  ∀trans_eqrel : transitive ? eqrel.
1253  ∀sym_eqrel   : symmetric ? eqrel.
1254  ∀f           : A → acctype → acctype.
1255  (∀x:A.∀acc1:acctype.∀acc2:acctype.eqrel acc1 acc2→eqrel (f x acc1) (f x acc2)) →
1256  ∀l : list A. ∀acc1, acc2 : acctype. eqrel acc1 acc2 → eqrel (foldr … f acc1 l) (foldr … f acc2 l).
1257#A #acctype #eqrel #R #T #S #f #Hinj #l elim l
1258//
1259#hd #tl #Hind #acc1 #acc2 #Heq normalize @Hinj @Hind @Heq
1260qed.
1261
1262(* We need to extend the above proof to arbitrary equivalence relation instead of
1263   just standard equality. *)
1264lemma lset_eq_concrete_fold_ext :
1265  ∀A : DeqSet.
1266  ∀acctype : Type[0].
1267  ∀eqrel : acctype → acctype → Prop.
1268  ∀refl_eqrel  : reflexive ? eqrel.
1269  ∀trans_eqrel : transitive ? eqrel.
1270  ∀sym_eqrel   : symmetric ? eqrel.
1271  ∀f:carr A → acctype → acctype.
1272  (∀x,acc1,acc2. eqrel acc1 acc2 → eqrel (f x acc1) (f x acc2)) →
1273  (∀x1,x2. ∀acc. eqrel (f x1 (f x2 acc)) (f x2 (f x1 acc))) →
1274  (∀x.∀acc. eqrel (f x (f x acc)) (f x acc)) →
1275  ∀l1,l2 : list (carr A).
1276  lset_eq_concrete A l1 l2 → 
1277  ∀acc. eqrel (foldr ?? f acc l1) (foldr ?? f acc l2).
1278#A #acctype #eqrel #R #T #S #f #Hinj #Hcomm #Hidem #l1 #l2 #Heq
1279elim Heq
1280try //
1281#a' #b' #c' #Hstep #Hbc #H inversion Hstep
1282[ 1: #a #x #b #y #c #HlA #HlB #_ #acc
1283     >HlB in H; #H @(T … ? (H acc))
1284     >fold_append >fold_append >fold_append >fold_append
1285     >fold_append >fold_append >fold_append >fold_append
1286     normalize
1287     cut (eqrel (f x (foldr ? acctype f (f y (foldr ? acctype f acc c)) b))
1288                (f y (foldr ? acctype f (f x (foldr ? acctype f acc c)) b)))
1289     [ 1:
1290     elim c
1291     [ 1: normalize elim b
1292          [ 1: normalize @(Hcomm x y)
1293          | 2: #hdb #tlb #Hind normalize
1294               lapply (Hinj hdb ?? Hind) #Hind'
1295               lapply (T … Hind' (Hcomm ???)) #Hind''
1296               @S @(triangle_diagram ? eqrel R T S … Hind'') @Hcomm ]
1297     | 2: #hdc #tlc #Hind normalize elim b
1298          [ 1: normalize @(Hcomm x y)
1299          | 2: #hdb #tlb #Hind normalize
1300               lapply (Hinj hdb ?? Hind) #Hind'
1301               lapply (T … Hind' (Hcomm ???)) #Hind''
1302               @S @(triangle_diagram ? eqrel R T S … Hind'') @Hcomm ]
1303     ] ]
1304     #Hind @(inj_to_fold_inj … eqrel R T S ? Hinj … Hind)
1305| 2: #a #x #b #HeqA #HeqB #_ #acc >HeqB in H; #H @(T … (H acc))
1306     >fold_append >fold_append >fold_append >(fold_append ?? ([x]@[x]))
1307     normalize @(inj_to_fold_inj … eqrel R T S ? Hinj) @S @Hidem
1308| 3: #a #x #b #HeqA #HeqB #_ #acc >HeqB in H; #H @(T … (H acc))
1309     >fold_append >(fold_append ?? ([x]@[x])) >fold_append >fold_append
1310     normalize @(inj_to_fold_inj … eqrel R T S ? Hinj) @Hidem
1311] qed.
1312
1313(* Prepare some well-founded induction principles on lists. The idea is to perform
1314   an induction on the sequence of filterees of a list : taking the first element,
1315   filtering it out of the tail, etc. We give such principles for pairs of lists
1316   and isolated lists.  *)
1317
1318(* The two lists [l1,l2] share at least the head of l1. *)
1319definition head_shared ≝ λA. λl1,l2 : list A.
1320match l1 with
1321[ nil ⇒ l2 = (nil ?)
1322| cons hd _ ⇒  mem … hd l2
1323].
1324
1325(* Relation on pairs of lists, as described above. *)
1326definition filtered_lists : ∀A:DeqSet. relation (list (carr A)×(list (carr A))) ≝
1327λA:DeqSet. λll1,ll2.
1328let 〈la1,lb1〉 ≝ ll1 in
1329let 〈la2,lb2〉 ≝ ll2 in
1330match la2 with
1331[ nil ⇒ False
1332| cons hda2 tla2 ⇒
1333    head_shared ? la2 lb2 ∧
1334    la1 = filter … (λx.¬(x==hda2)) tla2 ∧
1335    lb1 = filter … (λx.¬(x==hda2)) lb2
1336].
1337
1338(* Notice the absence of plural : this relation works on a simple list, not a pair. *)
1339definition filtered_list : ∀A:DeqSet. relation (list (carr A)) ≝
1340λA:DeqSet. λl1,l2.
1341match l2 with
1342[ nil ⇒ False
1343| cons hd2 tl2 ⇒
1344    l1 = filter … (λx.¬(x==hd2)) l2
1345].
1346
1347(* Relation on lists based on their lengths. We know this one is well-founded. *)
1348definition length_lt : ∀A:DeqSet. relation (list (carr A)) ≝
1349λA:DeqSet. λl1,l2. lt (length ? l1) (length ? l2).
1350
1351(* length_lt can be extended on pairs by just measuring the first component *)
1352definition length_fst_lt : ∀A:DeqSet. relation (list (carr A) × (list (carr A))) ≝
1353λA:DeqSet. λll1,ll2. lt (length ? (\fst ll1)) (length ? (\fst ll2)).
1354
1355lemma filter_length : ∀A. ∀l. ∀f. |filter A f l| ≤ |l|.
1356#A #l #f elim l //
1357#hd #tl #Hind whd in match (filter ???); cases (f hd) normalize nodelta
1358[ 1: /2 by le_S_S/
1359| 2: @le_S @Hind
1360] qed.
1361
1362(* The order on lists defined by their length is wf *)
1363lemma length_lt_wf : ∀A. ∀l. WF (list (carr A)) (length_lt A) l.
1364#A #l % elim l
1365[ 1: #a normalize in ⊢ (% → ?); #H lapply (le_S_O_absurd ? H) @False_ind
1366| 2: #hd #tl #Hind #a #Hlt % #a' #Hlt' @Hind whd in Hlt Hlt'; whd
1367@(transitive_le … Hlt') @(monotonic_pred … Hlt)
1368qed.
1369
1370(* Order on pairs of list by measuring the first proj *)
1371lemma length_fst_lt_wf : ∀A. ∀ll. WF ? (length_fst_lt A) ll.
1372#A * #l1 #l2 % elim l1
1373[ 1: * #a1 #a2 normalize in ⊢ (% → ?); #H lapply (le_S_O_absurd ? H) @False_ind
1374| 2: #hd1 #tl1 #Hind #a #Hlt % #a' #Hlt' @Hind whd in Hlt Hlt'; whd
1375@(transitive_le … Hlt') @(monotonic_pred … Hlt)
1376qed.
1377
1378lemma length_to_filtered_lists : ∀A. subR ? (filtered_lists A) (length_fst_lt A).
1379#A whd * #a1 #a2 * #b1 #b2 elim b1
1380[ 1: @False_ind
1381| 2: #hd1 #tl1 #Hind whd in ⊢ (% → ?); * * #Hmem #Ha1 #Ha2 whd
1382     >Ha1 whd in match (length ??) in ⊢ (??%); @le_S_S @filter_length
1383] qed.
1384
1385lemma length_to_filtered_list : ∀A. subR ? (filtered_list A) (length_lt A).
1386#A whd #a #b elim b
1387[ 1: @False_ind
1388| 2: #hd #tl #Hind whd in ⊢ (% → ?); whd in match (filter ???);
1389     lapply (eqb_true ? hd hd) * #_ #H >(H (refl ??)) normalize in match (notb ?);
1390     normalize nodelta #Ha whd @le_S_S >Ha @filter_length ]
1391qed.
1392
1393(* Prove well-foundedness by embedding in lt *)
1394lemma filtered_lists_wf : ∀A. ∀ll. WF ? (filtered_lists A) ll.
1395#A #ll @(WF_antimonotonic … (length_to_filtered_lists A)) @length_fst_lt_wf
1396qed.
1397
1398lemma filtered_list_wf : ∀A. ∀l. WF ? (filtered_list A) l.
1399#A #l @(WF_antimonotonic … (length_to_filtered_list A)) @length_lt_wf
1400qed.
1401
1402definition Acc_elim : ∀A,R,x. WF A R x → (∀a. R a x → WF A R a) ≝
1403λA,R,x,acc.
1404match acc with
1405[ wf _ a0 ⇒ a0 ].
1406
1407(* Stolen from Coq. Warped to avoid prop-to-type restriction. *)
1408let rec WF_rect
1409  (A : Type[0])
1410  (R : A → A → Prop)
1411  (P : A → Type[0])
1412  (f : ∀ x : A.
1413       (∀ y : A. R y x → WF ? R y) →
1414       (∀ y : A. R y x → P y) → P x)
1415  (x : A)
1416  (a : WF A R x) on a : P x ≝
1417f x (Acc_elim … a) (λy:A. λRel:R y x. WF_rect A R P f y ((Acc_elim … a) y Rel)).
1418
1419lemma lset_eq_concrete_filter : ∀A:DeqSet.∀tl.∀hd.
1420  lset_eq_concrete A (hd :: (filter ? (λy.notb (eqb A y hd)) tl)) (hd :: tl).
1421#A #tl elim tl
1422[ 1: #hd //
1423| 2: #hd' #tl' #Hind #hd >filter_cons_unfold
1424     lapply (eqb_true A hd' hd)
1425     cases (hd'==hd)
1426     [ 2: #_ normalize in match (notb false); normalize nodelta
1427          >cons_to_append >(cons_to_append … hd')
1428          >cons_to_append in ⊢ (???%); >(cons_to_append … hd') in ⊢ (???%);
1429          @(transpose_lset_eq_concrete ? hd' hd [ ] [ ] (filter A (λy:A.¬y==hd) tl') [ ] [ ] tl')
1430          >nil_append >nil_append >nil_append >nil_append
1431          @lset_eq_concrete_cons >nil_append >nil_append
1432          @Hind
1433     | 1: * #H1 #_ lapply (H1 (refl ??)) #Heq normalize in match (notb ?); normalize nodelta
1434          >Heq >cons_to_append >cons_to_append in ⊢ (???%); >cons_to_append in ⊢ (???(???%));
1435          @(square_lset_eq_concrete A ([hd]@filter A (λy:A.¬y==hd) tl') ([hd]@tl'))
1436          [ 1: @Hind
1437          | 2: %2
1438          | 3: %1{???? ? (lset_refl ??)} /2 by lset_weaken/ ]
1439     ]
1440] qed.
1441
1442
1443(* The "abstract", observational definition of set equivalence implies the concrete one. *)
1444
1445lemma lset_eq_to_lset_eq_concrete_aux :
1446  ∀A,ll.
1447    head_shared … (\fst ll) (\snd ll) →
1448    lset_eq (carr A) (\fst ll) (\snd ll) →
1449    lset_eq_concrete A (\fst ll) (\snd ll).
1450#A #ll @(WF_ind ????? (filtered_lists_wf A ll))
1451* *
1452[ 1: #b2 #Hwf #Hind_wf whd in ⊢ (% → ?); #Hb2 >Hb2 #_ %2
1453| 2: #hd1 #tl1 #b2 #Hwf #Hind_wf whd in ⊢ (% → ?); #Hmem
1454     lapply (list_mem_split ??? Hmem) * #l2A * #l2B #Hb2 #Heq
1455     destruct
1456     lapply (Hind_wf 〈filter … (λx.¬x==hd1) tl1,filter … (λx.¬x==hd1) (l2A@l2B)〉)
1457     cut (filtered_lists A 〈filter A (λx:A.¬x==hd1) tl1,filter A (λx:A.¬x==hd1) (l2A@l2B)〉 〈hd1::tl1,l2A@[hd1]@l2B〉)
1458     [ @conj try @conj try @refl whd
1459       [ 1: /2 by /
1460       | 2: >filter_append in ⊢ (???%); >filter_append in ⊢ (???%);
1461            whd in match (filter ?? [hd1]);
1462            elim (eqb_true A hd1 hd1) #_ #H >(H (refl ??)) normalize in match (notb ?);
1463            normalize nodelta <filter_append @refl ] ]
1464     #Hfiltered #Hind_aux lapply (Hind_aux Hfiltered) -Hind_aux
1465     cut (lset_eq A (filter A (λx:A.¬x==hd1) tl1) (filter A (λx:A.¬x==hd1) (l2A@l2B)))
1466     [ 1: lapply (lset_eq_filter_monotonic … Heq hd1)
1467          >filter_cons_unfold >filter_append >(filter_append … [hd1])
1468          whd in match (filter ?? [hd1]);
1469          elim (eqb_true A hd1 hd1) #_ #H >(H (refl ??)) normalize in match (notb ?);
1470          normalize nodelta <filter_append #Hsol @Hsol ]
1471     #Hset_eq
1472     cut (head_shared A (filter A (λx:A.¬x==hd1) tl1) (filter A (λx:A.¬x==hd1) (l2A@l2B)))
1473     [ 1: lapply Hset_eq cases (filter A (λx:A.¬x==hd1) tl1)
1474          [ 1: whd in ⊢ (% → ?); * #_ elim (filter A (λx:A.¬x==hd1) (l2A@l2B)) //
1475               #hd' #tl' normalize #Hind * @False_ind
1476          | 2: #hd' #tl' * #Hincl1 #Hincl2 whd elim Hincl1 #Hsol #_ @Hsol ] ]
1477     #Hshared #Hind_aux lapply (Hind_aux Hshared Hset_eq)
1478     #Hconcrete_set_eq
1479     >cons_to_append
1480     @(transitive_lset_eq_concrete ? ([hd1]@tl1) ([hd1]@l2A@l2B) (l2A@[hd1]@l2B))
1481     [ 2: @symmetric_lset_eq_concrete @switch_lset_eq_concrete ]
1482     lapply (lset_eq_concrete_cons … Hconcrete_set_eq hd1) #Hconcrete_cons_eq
1483     @(square_lset_eq_concrete … Hconcrete_cons_eq)
1484     [ 1: @(lset_eq_concrete_filter ? tl1 hd1)
1485     | 2: @(lset_eq_concrete_filter ? (l2A@l2B) hd1) ]
1486] qed.
1487
1488lemma lset_eq_to_lset_eq_concrete : ∀A,l1,l2. lset_eq (carr A) l1 l2 → lset_eq_concrete A l1 l2.
1489#A *
1490[ 1: #l2 #Heq >(lset_eq_empty_inv … (symmetric_lset_eq … Heq)) //
1491| 2: #hd1 #tl1 #l2 #Hincl lapply Hincl lapply (lset_eq_to_lset_eq_concrete_aux ? 〈hd1::tl1,l2〉) #H @H
1492     whd elim Hincl * //
1493] qed.
1494
1495
1496(* The concrete one implies the abstract one. *)
1497lemma lset_eq_concrete_to_lset_eq : ∀A,l1,l2. lset_eq_concrete A l1 l2 → lset_eq A l1 l2.
1498#A #l1 #l2 #Hconcrete
1499elim Hconcrete try //
1500#a #b #c #Hstep #Heq_bc_concrete #Heq_bc
1501cut (lset_eq A a b)
1502[ 1: elim Hstep
1503     [ 1: #a' elim a'
1504          [ 2: #hda #tla #Hind #x #b' #y #c' >cons_to_append
1505               >(associative_append ? [hda] tla ?)
1506               >(associative_append ? [hda] tla ?)
1507               @cons_monotonic_eq >nil_append >nil_append @Hind
1508          | 1: #x #b' #y #c' >nil_append >nil_append
1509               elim b' try //
1510               #hdb #tlc #Hind >append_cons >append_cons in ⊢ (???%);
1511               >associative_append >associative_append
1512               lapply (cons_monotonic_eq … Hind hdb) #Hind'               
1513               @(transitive_lset_eq ? ([x]@[hdb]@tlc@[y]@c') ([hdb]@[x]@tlc@[y]@c'))
1514               /2 by transitive_lset_eq/ ]
1515     | 2: #a' elim a'
1516          [ 2: #hda #tla #Hind #x #b' >cons_to_append
1517               >(associative_append ? [hda] tla ?)
1518               >(associative_append ? [hda] tla ?)
1519               @cons_monotonic_eq >nil_append >nil_append @Hind
1520          | 1: #x #b' >nil_append >nil_append @conj normalize
1521               [ 1: @conj [ 1: %1 @refl ] elim b'
1522                    [ 1: @I
1523                    | 2: #hdb #tlb #Hind normalize @conj
1524                         [ 1: %2 %2 %1 @refl
1525                         | 2: @(All_mp … Hind) #a0 *
1526                              [ 1: #Heq %1 @Heq
1527                              | 2: * /2 by or_introl, or_intror/ ] ] ]
1528                    #H %2 %2 %2 @H
1529               | 2: @conj try @conj try /2 by or_introl, or_intror/ elim b'
1530                    [ 1: @I
1531                    | 2: #hdb #tlb #Hind normalize @conj
1532                         [ 1: %2 %1 @refl
1533                         | 2: @(All_mp … Hind) #a0 *
1534                              [ 1: #Heq %1 @Heq
1535                              | 2: #H %2 %2 @H ] ] ] ] ]
1536     | 3: #a #x #b elim a try @lset_eq_contract
1537          #hda #tla #Hind @cons_monotonic_eq @Hind ] ]
1538#Heq_ab @(transitive_lset_eq … Heq_ab Heq_bc)
1539qed.
1540
1541lemma lset_eq_fold :
1542  ∀A : DeqSet.
1543  ∀acctype : Type[0].
1544  ∀eqrel : acctype → acctype → Prop.
1545  ∀refl_eqrel  : reflexive ? eqrel.
1546  ∀trans_eqrel : transitive ? eqrel.
1547  ∀sym_eqrel   : symmetric ? eqrel.
1548  ∀f:carr A → acctype → acctype.
1549  (∀x,acc1,acc2. eqrel acc1 acc2 → eqrel (f x acc1) (f x acc2)) →
1550  (∀x1,x2. ∀acc. eqrel (f x1 (f x2 acc)) (f x2 (f x1 acc))) →
1551  (∀x.∀acc. eqrel (f x (f x acc)) (f x acc)) →
1552  ∀l1,l2 : list (carr A).
1553  lset_eq A l1 l2 → 
1554  ∀acc. eqrel (foldr ?? f acc l1) (foldr ?? f acc l2).
1555#A #acctype #eqrel #refl_eqrel #trans_eqrel #sym_eqrel #f #Hinj #Hsym #Hcontract #l1 #l2 #Heq #acc
1556lapply (lset_eq_to_lset_eq_concrete … Heq) #Heq_concrete
1557@(lset_eq_concrete_fold_ext A acctype eqrel refl_eqrel trans_eqrel sym_eqrel f Hinj Hsym Hcontract l1 l2 Heq_concrete acc)
1558qed.
1559
1560(* Additional lemmas on lsets *)
1561
1562lemma lset_difference_empty :
1563  ∀A : DeqSet.
1564  ∀s1. lset_difference A s1 [ ] = s1.
1565#A #s1 elim s1 try //
1566#hd #tl #Hind >lset_difference_unfold >Hind @refl
1567qed.
1568
1569lemma lset_not_mem_difference :
1570  ∀A : DeqSet. ∀s1,s2,s3. lset_inclusion (carr A) s1 (lset_difference ? s2 s3) → ∀x. mem ? x s1 → ¬(mem ? x s3).
1571#A #s1 #s2 #s3 #Hincl #x #Hmem
1572lapply (lset_difference_disjoint ? s3 s2) whd in ⊢ (% → ?); #Hdisjoint % #Hmem_s3
1573elim s1 in Hincl Hmem;
1574[ 1: #_ *
1575| 2: #hd #tl #Hind whd in ⊢ (% → %); * #Hmem_hd #Hall *
1576     [ 2: #Hmem_x_tl @Hind assumption
1577     | 1: #Heq lapply (Hdisjoint … Hmem_s3 Hmem_hd) * #H @H @Heq ]
1578] qed.
1579
1580lemma lset_mem_inclusion_mem :
1581  ∀A,s1,s2,elt.
1582  mem A elt s1 → lset_inclusion ? s1 s2 → mem ? elt s2.
1583#A #s1 elim s1
1584[ 1: #s2 #elt *
1585| 2: #hd #tl #Hind #s2 #elt *
1586     [ 1: #Heq destruct * //
1587     | 2: #Hmem_tl * #Hmem #Hall elim tl in Hall Hmem_tl;
1588          [ 1: #_ *
1589          | 2: #hd' #tl' #Hind * #Hmem' #Hall *
1590               [ 1: #Heq destruct @Hmem'
1591               | 2: #Hmem'' @Hind assumption ] ] ] ]
1592qed.
1593
1594lemma lset_remove_inclusion :
1595  ∀A : DeqSet. ∀s,elt.
1596    lset_inclusion A (lset_remove ? s elt) s.
1597#A #s elim s try // qed.
1598
1599lemma lset_difference_remove_inclusion :
1600  ∀A : DeqSet. ∀s1,s2,elt.
1601    lset_inclusion A
1602      (lset_difference ? (lset_remove ? s1 elt) s2) 
1603      (lset_difference ? s1 s2).
1604#A #s elim s try // qed.
1605
1606lemma lset_difference_permute :
1607  ∀A : DeqSet. ∀s1,s2,s3.
1608    lset_difference A s1 (s2 @ s3) =
1609    lset_difference A s1 (s3 @ s2).
1610#A #s1 #s2 elim s2 try //
1611#hd #tl #Hind #s3 >lset_difference_unfold2 >lset_difference_lset_remove_commute
1612>Hind elim s3 try //
1613#hd' #tl' #Hind' >cons_to_append >associative_append
1614>associative_append >(cons_to_append … hd tl)
1615>lset_difference_unfold2 >lset_difference_lset_remove_commute >nil_append
1616>lset_difference_unfold2 >lset_difference_lset_remove_commute >nil_append
1617<Hind' generalize in match (lset_difference ???); #foo
1618whd in match (lset_remove ???); whd in match (lset_remove ???) in ⊢ (??(?????%)?);
1619whd in match (lset_remove ???) in ⊢ (???%); whd in match (lset_remove ???) in ⊢ (???(?????%));
1620elim foo
1621[ 1: normalize @refl
1622| 2: #hd'' #tl'' #Hind normalize
1623      @(match (hd''==hd') return λx. ((hd''==hd') = x) → ? with
1624        [ true ⇒ λH. ?
1625        | false ⇒ λH. ?
1626        ] (refl ? (hd''==hd')))
1627      @(match (hd''==hd) return λx. ((hd''==hd) = x) → ? with
1628        [ true ⇒ λH'. ?
1629        | false ⇒ λH'. ?
1630        ] (refl ? (hd''==hd)))
1631      normalize nodelta
1632      try @Hind
1633[ 1: normalize >H normalize nodelta @Hind
1634| 2: normalize >H' normalize nodelta @Hind
1635| 3: normalize >H >H' normalize nodelta >Hind @refl
1636] qed.
1637
1638
1639
1640lemma lset_disjoint_dec :
1641  ∀A : DeqSet.
1642  ∀s1,elt,s2.
1643  mem ? elt s1 ∨ mem ? elt (lset_difference A (elt :: s2) s1).
1644#A #s1 #elt #s2
1645@(match elt ∈ s1 return λx. ((elt ∈ s1) = x) → ?
1646  with
1647  [ false ⇒ λHA. ?
1648  | true ⇒ λHA. ? ] (refl ? (elt ∈ s1)))
1649[ 1: lapply (memb_to_mem … HA) #Hmem
1650     %1 @Hmem
1651| 2: %2 elim s1 in HA;
1652     [ 1: #_ whd %1 @refl
1653     | 2: #hd1 #tl1 #Hind normalize in ⊢ (% → ?);
1654          >lset_difference_unfold
1655          >lset_difference_unfold2
1656          lapply (eqb_true ? elt hd1) whd in match (memb ???) in ⊢ (? → ? → %);
1657          cases (elt==hd1) normalize nodelta
1658          [ 1: #_ #Habsurd destruct
1659          | 2: #HA #HB >HB normalize nodelta %1 @refl ] ] ]
1660qed.
1661
1662lemma mem_filter : ∀A : DeqSet. ∀l,elt1,elt2.
1663  mem A elt1 (filter A (λx:A.¬x==elt2) l) → mem A elt1 l.
1664#A #l elim l try // #hd #tl #Hind #elt1 #elt2 /2 by lset_mem_inclusion_mem/
1665qed.
1666
1667lemma lset_inclusion_difference_aux :
1668  ∀A : DeqSet. ∀s1,s2.
1669  lset_inclusion A s1 s2 →
1670  (lset_eq A s2 (s1@lset_difference A s2 s1)).
1671#A #s1
1672@(WF_ind ????? (filtered_list_wf A s1))
1673*
1674[ 1: #_ #_ #s2 #_ >nil_append >lset_difference_empty @reflexive_lset_eq
1675| 2: #hd1 #tl1 #Hwf #Hind #s2 * #Hmem #Hincl
1676     lapply (Hind (filter ? (λx.¬x==hd1) tl1) ?)
1677     [ 1: whd normalize
1678          >(proj2 … (eqb_true ? hd1 hd1) (refl ??)) normalize nodelta @refl ]
1679     #Hind_wf     
1680     elim (list_mem_split ??? Hmem) #s2A * #s2B #Heq >Heq
1681     >cons_to_append in ⊢ (???%); >associative_append
1682     >lset_difference_unfold2
1683     >nil_append
1684     >lset_remove_split >lset_remove_split
1685     normalize in match (lset_remove ? [hd1] hd1);
1686     >(proj2 … (eqb_true ? hd1 hd1) (refl ??)) normalize nodelta
1687     >nil_append <lset_remove_split >lset_difference_lset_remove_commute
1688     lapply (Hind_wf (lset_remove A (s2A@s2B) hd1) ?)
1689     [ 1: lapply (lset_inclusion_remove … Hincl hd1)
1690          >Heq @lset_inclusion_eq2
1691          >lset_remove_split >lset_remove_split >lset_remove_split
1692          normalize in match (lset_remove ? [hd1] hd1);
1693          >(proj2 … (eqb_true ? hd1 hd1) (refl ??)) normalize nodelta
1694          >nil_append @reflexive_lset_eq ]
1695     #Hind >lset_difference_lset_remove_commute in Hind; <lset_remove_split #Hind
1696     @lset_eq_concrete_to_lset_eq
1697     lapply (lset_eq_to_lset_eq_concrete … (cons_monotonic_eq … Hind hd1)) #Hindc
1698     @(square_lset_eq_concrete ????? Hindc) -Hindc -Hind
1699     [ 1: @(transitive_lset_eq_concrete ?? ([hd1]@s2A@s2B) (s2A@[hd1]@s2B))
1700          [ 2: @symmetric_lset_eq_concrete @switch_lset_eq_concrete
1701          | 1: @lset_eq_to_lset_eq_concrete @lset_eq_filter ]
1702     | 2: @lset_eq_to_lset_eq_concrete @(transitive_lset_eq A … (lset_eq_filter ? ? hd1 …))
1703          elim (s2A@s2B)
1704          [ 1: normalize in match (lset_difference ???); @reflexive_lset_eq
1705          | 2: #hd2 #tl2 #Hind >lset_difference_unfold >lset_difference_unfold
1706               @(match (hd2∈filter A (λx:A.¬x==hd1) tl1)
1707                 return λx. ((hd2∈filter A (λx:A.¬x==hd1) tl1) = x) → ?
1708                 with
1709                 [ false ⇒ λH. ?
1710                 | true ⇒ λH. ?
1711                 ] (refl ? (hd2∈filter A (λx:A.¬x==hd1) tl1))) normalize nodelta
1712               [ 1: lapply (memb_to_mem … H) #Hfilter >(mem_to_memb … (mem_filter … Hfilter))
1713                    normalize nodelta @Hind
1714               | 2: @(match (hd2∈tl1)
1715                      return λx. ((hd2∈tl1) = x) → ?
1716                      with
1717                      [ false ⇒ λH'. ?
1718                      | true ⇒ λH'. ?
1719                      ] (refl ? (hd2∈tl1))) normalize nodelta
1720                      [ 1: (* We have hd2 = hd1 *)
1721                            cut (hd2 = hd1)
1722                            [ elim tl1 in H H';
1723                              [ 1: normalize #_ #Habsurd destruct (Habsurd)
1724                              | 2: #hdtl1 #tltl1 #Hind normalize in ⊢ (% → % → ?);
1725                                    lapply (eqb_true ? hdtl1 hd1)
1726                                    cases (hdtl1==hd1) normalize nodelta
1727                                    [ 1: * #H >(H (refl ??)) #_
1728                                         lapply (eqb_true ? hd2 hd1)
1729                                         cases (hd2==hd1) normalize nodelta *
1730                                         [ 1: #H' >(H' (refl ??)) #_ #_ #_ @refl
1731                                         | 2: #_ #_ @Hind ]
1732                                    | 2: #_ normalize lapply (eqb_true ? hd2 hdtl1)
1733                                         cases (hd2 == hdtl1) normalize nodelta *
1734                                         [ 1: #_ #_ #Habsurd destruct (Habsurd)
1735                                         | 2: #_ #_ @Hind ] ] ] ]
1736                           #Heq_hd2hd1 destruct (Heq_hd2hd1)
1737                           @lset_eq_concrete_to_lset_eq lapply (lset_eq_to_lset_eq_concrete … Hind)
1738                           #Hind' @(square_lset_eq_concrete … Hind')
1739                           [ 2: @lset_refl
1740                           | 1: >cons_to_append >cons_to_append in ⊢ (???%);
1741                                @(transitive_lset_eq_concrete … ([hd1]@[hd1]@tl1@lset_difference A tl2 (filter A (λx:A.¬x==hd1) tl1)))
1742                                [ 1: @lset_eq_to_lset_eq_concrete @symmetric_lset_eq @lset_eq_contract
1743                                | 2: >(cons_to_append … hd1 (lset_difference ???))
1744                                     @lset_eq_concrete_cons >nil_append >nil_append
1745                                     @symmetric_lset_eq_concrete @switch_lset_eq_concrete ] ]
1746                        | 2: @(match hd2 == hd1
1747                               return λx. ((hd2 == hd1) = x) → ?
1748                               with
1749                               [ true ⇒ λH''. ?
1750                               | false ⇒ λH''. ?
1751                               ] (refl ? (hd2 == hd1)))
1752                             [ 1: whd in match (lset_remove ???) in ⊢ (???%);
1753                                  >H'' normalize nodelta >((proj1 … (eqb_true …)) H'')
1754                                  @(transitive_lset_eq … Hind)
1755                                  @(transitive_lset_eq … (hd1::hd1::tl1@lset_difference A tl2 (filter A (λx:A.¬x==hd1) tl1)))
1756                                  [ 2: @lset_eq_contract ]                                                                   
1757                                  @lset_eq_concrete_to_lset_eq @lset_eq_concrete_cons                                 
1758                                  @switch_lset_eq_concrete
1759                             | 2: whd in match (lset_remove ???) in ⊢ (???%);
1760                                  >H'' >notb_false normalize nodelta
1761                                  @lset_eq_concrete_to_lset_eq
1762                                  lapply (lset_eq_to_lset_eq_concrete … Hind) #Hindc
1763                                  lapply (lset_eq_concrete_cons … Hindc hd2) #Hindc' -Hindc
1764                                  @(square_lset_eq_concrete … Hindc')
1765                                  [ 1: @symmetric_lset_eq_concrete
1766                                       >cons_to_append >cons_to_append in ⊢ (???%);
1767                                       >(cons_to_append … hd2) >(cons_to_append … hd1) in ⊢ (???%);
1768                                       @(switch_lset_eq_concrete ? ([hd1]@tl1) hd2 ?)
1769                                  | 2: @symmetric_lset_eq_concrete @(switch_lset_eq_concrete ? ([hd1]@tl1) hd2 ?)
1770                                  ]
1771                              ]
1772                        ]
1773                    ]
1774             ]
1775      ]
1776] qed.             
1777                                                       
1778lemma lset_inclusion_difference :
1779  ∀A : DeqSet.
1780  ∀s1,s2 : lset (carr A).
1781    lset_inclusion ? s1 s2 →
1782    ∃s2'. lset_eq ? s2 (s1 @ s2') ∧
1783          lset_disjoint ? s1 s2' ∧
1784          lset_eq ? s2' (lset_difference ? s2 s1).
1785#A #s1 #s2 #Hincl %{(lset_difference A s2 s1)} @conj try @conj
1786[ 1: @lset_inclusion_difference_aux @Hincl
1787| 2: /2 by lset_difference_disjoint/
1788| 3,4: @reflexive_lset_inclusion ]
1789qed.
1790
1791(* --------------------------------------------------------------------------- *)
1792(* Stuff on bitvectors, previously in memoryInjections.ma *)
1793(* --------------------------------------------------------------------------- *)
1794(* --------------------------------------------------------------------------- *)   
1795(* Some general lemmas on bitvectors (offsets /are/ bitvectors) *)
1796(* --------------------------------------------------------------------------- *)
1797 
1798lemma add_with_carries_n_O : ∀n,bv. add_with_carries n bv (zero n) false = 〈bv,zero n〉.
1799#n #bv whd in match (add_with_carries ????); elim bv //
1800#n #hd #tl #Hind whd in match (fold_right2_i ????????);
1801>Hind normalize
1802cases n in tl;
1803[ 1: #tl cases hd normalize @refl
1804| 2: #n' #tl cases hd normalize @refl ]
1805qed.
1806
1807lemma addition_n_0 : ∀n,bv. addition_n n bv (zero n) = bv.
1808#n #bv whd in match (addition_n ???);
1809>add_with_carries_n_O //
1810qed.
1811
1812lemma replicate_Sn : ∀A,sz,elt.
1813  replicate A (S sz) elt = elt ::: (replicate A sz elt).
1814// qed.
1815
1816lemma zero_Sn : ∀n. zero (S n) = false ::: (zero n). // qed.
1817
1818lemma negation_bv_Sn : ∀n. ∀xa. ∀a : BitVector n. negation_bv … (xa ::: a) = (notb xa) ::: (negation_bv … a).
1819#n #xa #a normalize @refl qed.
1820
1821(* useful facts on carry_of *)
1822lemma carry_of_TT : ∀x. carry_of true true x = true. // qed.
1823lemma carry_of_TF : ∀x. carry_of true false x = x. // qed.
1824lemma carry_of_FF : ∀x. carry_of false false x = false. // qed.
1825lemma carry_of_lcomm : ∀x,y,z. carry_of x y z = carry_of y x z. * * * // qed.
1826lemma carry_of_rcomm : ∀x,y,z. carry_of x y z = carry_of x z y. * * * // qed.
1827
1828
1829
1830definition one_bv ≝ λn. (\fst (add_with_carries … (zero n) (zero n) true)).
1831
1832lemma one_bv_Sn_aux : ∀n. ∀bits,flags : BitVector (S n).
1833    add_with_carries … (zero (S n)) (zero (S n)) true = 〈bits, flags〉 →
1834    add_with_carries … (zero (S (S n))) (zero (S (S n))) true = 〈false ::: bits, false ::: flags〉.
1835#n elim n
1836[ 1: #bits #flags elim (BitVector_Sn … bits) #hd_bits * #tl_bits #Heq_bits
1837     elim (BitVector_Sn … flags) #hd_flags * #tl_flags #Heq_flags
1838     >(BitVector_O … tl_flags) >(BitVector_O … tl_bits)
1839     normalize #Heq destruct (Heq) @refl
1840| 2: #n' #Hind #bits #flags elim (BitVector_Sn … bits) #hd_bits * #tl_bits #Heq_bits
1841     destruct #Hind >add_with_carries_Sn >replicate_Sn
1842     whd in match (zero ?) in Hind; lapply Hind
1843     elim (add_with_carries (S (S n'))
1844            (false:::replicate bool (S n') false)
1845            (false:::replicate bool (S n') false) true) #bits #flags #Heq destruct
1846            normalize >add_with_carries_Sn in Hind;
1847     elim (add_with_carries (S n') (replicate bool (S n') false)
1848                    (replicate bool (S n') false) true) #flags' #bits'
1849     normalize
1850     cases (match bits' in Vector return λsz:ℕ.(λfoo:Vector bool sz.bool) with 
1851            [VEmpty⇒true|VCons (sz:ℕ)   (cy:bool)   (tl:(Vector bool sz))⇒cy])
1852     normalize #Heq destruct @refl
1853] qed.     
1854
1855lemma one_bv_Sn : ∀n. one_bv (S (S n)) = false ::: (one_bv (S n)).
1856#n lapply (one_bv_Sn_aux n)
1857whd in match (one_bv ?) in ⊢ (? → (??%%));
1858elim (add_with_carries (S n) (zero (S n)) (zero (S n)) true) #bits #flags
1859#H lapply (H bits flags (refl ??)) #H2 >H2 @refl
1860qed.
1861
1862lemma increment_to_addition_n_aux : ∀n. ∀a : BitVector n.
1863    add_with_carries ? a (zero n) true = add_with_carries ? a (one_bv n) false.
1864#n   
1865elim n
1866[ 1: #a >(BitVector_O … a) normalize @refl
1867| 2: #n' cases n'
1868     [ 1: #Hind #a elim (BitVector_Sn ? a) #xa * #tl #Heq destruct
1869          >(BitVector_O … tl) normalize cases xa @refl
1870     | 2: #n'' #Hind #a elim (BitVector_Sn ? a) #xa * #tl #Heq destruct
1871          >one_bv_Sn >zero_Sn
1872          lapply (Hind tl)
1873          >add_with_carries_Sn >add_with_carries_Sn
1874          #Hind >Hind elim (add_with_carries (S n'') tl (one_bv (S n'')) false) #bits #flags
1875          normalize nodelta elim (BitVector_Sn … flags) #flags_hd * #flags_tl #Hflags_eq >Hflags_eq
1876          normalize nodelta @refl
1877] qed.         
1878
1879(* In order to use associativity on increment, we hide it under addition_n. *)
1880lemma increment_to_addition_n : ∀n. ∀a : BitVector n. increment ? a = addition_n ? a (one_bv n).
1881#n
1882whd in match (increment ??) in ⊢ (∀_.??%?);
1883whd in match (addition_n ???) in ⊢ (∀_.???%);
1884#a lapply (increment_to_addition_n_aux n a)
1885#Heq >Heq cases (add_with_carries n a (one_bv n) false) #bits #flags @refl
1886qed.
1887
1888(* Explicit formulation of addition *)
1889
1890(* Explicit formulation of the last carry bit *)
1891let rec ith_carry (n : nat) (a,b : BitVector n) (init : bool) on n : bool ≝
1892match n return λx. BitVector x → BitVector x → bool with
1893[ O ⇒ λ_,_. init
1894| S x ⇒ λa',b'.
1895  let hd_a ≝ head' … a' in
1896  let hd_b ≝ head' … b' in
1897  let tl_a ≝ tail … a' in
1898  let tl_b ≝ tail … b' in
1899  carry_of hd_a hd_b (ith_carry x tl_a tl_b init)
1900] a b.
1901
1902lemma ith_carry_unfold : ∀n. ∀init. ∀a,b : BitVector (S n).
1903  ith_carry ? a b init = (carry_of (head' … a) (head' … b) (ith_carry ? (tail … a) (tail … b) init)).
1904#n #init #a #b @refl qed.
1905
1906lemma ith_carry_Sn : ∀n. ∀init. ∀xa,xb. ∀a,b : BitVector n.
1907  ith_carry ? (xa ::: a) (xb ::: b) init = (carry_of xa xb (ith_carry ? a b init)). // qed.
1908
1909(* correction of [ith_carry] *)
1910lemma ith_carry_ok : ∀n. ∀init. ∀a,b,res_ab,flags_ab : BitVector (S n).
1911  〈res_ab,flags_ab〉 = add_with_carries ? a b init →
1912  head' … flags_ab = ith_carry ? a b init.
1913#n elim n
1914[ 1: #init #a #b #res_ab #flags_ab
1915     elim (BitVector_Sn … a) #hd_a * #tl_a #Heq_a
1916     elim (BitVector_Sn … b) #hd_b * #tl_b #Heq_b
1917     elim (BitVector_Sn … res_ab) #hd_res * #tl_res #Heq_res
1918     elim (BitVector_Sn … flags_ab) #hd_flags * #tl_flags #Heq_flags
1919     destruct
1920     >(BitVector_O … tl_a) >(BitVector_O … tl_b)
1921     cases hd_a cases hd_b cases init normalize #Heq destruct (Heq)
1922     @refl
1923| 2: #n' #Hind #init #a #b #res_ab #flags_ab
1924     elim (BitVector_Sn … a) #hd_a * #tl_a #Heq_a
1925     elim (BitVector_Sn … b) #hd_b * #tl_b #Heq_b
1926     elim (BitVector_Sn … res_ab) #hd_res * #tl_res #Heq_res
1927     elim (BitVector_Sn … flags_ab) #hd_flags * #tl_flags #Heq_flags
1928     destruct
1929     lapply (Hind … init tl_a tl_b tl_res tl_flags)
1930     >add_with_carries_Sn >(ith_carry_Sn (S n'))
1931     elim (add_with_carries (S n') tl_a tl_b init) #res_ab #flags_ab
1932     elim (BitVector_Sn … flags_ab) #hd_flags_ab * #tl_flags_ab #Heq_flags_ab >Heq_flags_ab
1933     normalize nodelta cases hd_flags_ab normalize nodelta
1934     whd in match (head' ? (S n') ?); #H1 #H2
1935     destruct (H2) lapply (H1 (refl ??)) whd in match (head' ???); #Heq <Heq @refl
1936] qed.
1937
1938(* Explicit formulation of ith bit of an addition, with explicit initial carry bit. *)
1939definition ith_bit ≝ λ(n : nat).λ(a,b : BitVector n).λinit.
1940match n return λx. BitVector x → BitVector x → bool with
1941[ O ⇒ λ_,_. init
1942| S x ⇒ λa',b'.
1943  let hd_a ≝ head' … a' in
1944  let hd_b ≝ head' … b' in
1945  let tl_a ≝ tail … a' in
1946  let tl_b ≝ tail … b' in
1947  xorb (xorb hd_a hd_b) (ith_carry x tl_a tl_b init)
1948] a b.
1949
1950lemma ith_bit_unfold : ∀n. ∀init. ∀a,b : BitVector (S n).
1951  ith_bit ? a b init =  xorb (xorb (head' … a) (head' … b)) (ith_carry ? (tail … a) (tail … b) init).
1952#n #a #b // qed.
1953
1954lemma ith_bit_Sn : ∀n. ∀init. ∀xa,xb. ∀a,b : BitVector n.
1955  ith_bit ? (xa ::: a) (xb ::: b) init =  xorb (xorb xa xb) (ith_carry ? a b init). // qed.
1956
1957(* correction of ith_bit *)
1958lemma ith_bit_ok : ∀n. ∀init. ∀a,b,res_ab,flags_ab : BitVector (S n).
1959  〈res_ab,flags_ab〉 = add_with_carries ? a b init →
1960  head' … res_ab = ith_bit ? a b init.
1961#n
1962cases n
1963[ 1: #init #a #b #res_ab #flags_ab
1964     elim (BitVector_Sn … a) #hd_a * #tl_a #Heq_a
1965     elim (BitVector_Sn … b) #hd_b * #tl_b #Heq_b
1966     elim (BitVector_Sn … res_ab) #hd_res * #tl_res #Heq_res
1967     elim (BitVector_Sn … flags_ab) #hd_flags * #tl_flags #Heq_flags
1968     destruct
1969     >(BitVector_O … tl_a) >(BitVector_O … tl_b)
1970     >(BitVector_O … tl_flags) >(BitVector_O … tl_res)
1971     normalize cases init cases hd_a cases hd_b normalize #Heq destruct @refl
1972| 2: #n' #init #a #b #res_ab #flags_ab
1973     elim (BitVector_Sn … a) #hd_a * #tl_a #Heq_a
1974     elim (BitVector_Sn … b) #hd_b * #tl_b #Heq_b
1975     elim (BitVector_Sn … res_ab) #hd_res * #tl_res #Heq_res
1976     elim (BitVector_Sn … flags_ab) #hd_flags * #tl_flags #Heq_flags
1977     destruct
1978     lapply (ith_carry_ok … init tl_a tl_b tl_res tl_flags)
1979     #Hcarry >add_with_carries_Sn elim (add_with_carries ? tl_a tl_b init) in Hcarry;
1980     #res #flags normalize nodelta elim (BitVector_Sn … flags) #hd_flags' * #tl_flags' #Heq_flags'
1981     >Heq_flags' normalize nodelta cases hd_flags' normalize nodelta #H1 #H2 destruct (H2)
1982     cases hd_a cases hd_b >ith_bit_Sn whd in match (head' ???) in H1 ⊢ %;
1983     <(H1 (refl ??)) @refl
1984] qed.
1985
1986(* Transform a function from bit-vectors to bits into a vector by folding *)
1987let rec bitvector_fold (n : nat) (v : BitVector n) (f : ∀sz. BitVector sz → bool) on v : BitVector n ≝
1988match v with
1989[ VEmpty ⇒ VEmpty ?
1990| VCons sz elt tl ⇒
1991  let bit ≝ f ? v in
1992  bit ::: (bitvector_fold ? tl f)
1993].
1994
1995(* Two-arguments version *)
1996let rec bitvector_fold2 (n : nat) (v1, v2 : BitVector n) (f : ∀sz. BitVector sz → BitVector sz → bool) on v1 : BitVector n ≝
1997match v1  with
1998[ VEmpty ⇒ λ_. VEmpty ?
1999| VCons sz elt tl ⇒ λv2'.
2000  let bit ≝ f ? v1 v2 in
2001  bit ::: (bitvector_fold2 ? tl (tail … v2') f)
2002] v2.
2003
2004lemma bitvector_fold2_Sn : ∀n,x1,x2,v1,v2,f.
2005  bitvector_fold2 (S n) (x1 ::: v1) (x2 ::: v2) f = (f ? (x1 ::: v1) (x2 ::: v2)) ::: (bitvector_fold2 … v1 v2 f). // qed.
2006
2007(* These functions pack all the relevant information (including carries) directly. *)
2008definition addition_n_direct ≝ λn,v1,v2,init. bitvector_fold2 n v1 v2 (λn,v1,v2. ith_bit n v1 v2 init).
2009
2010lemma addition_n_direct_Sn : ∀n,x1,x2,v1,v2,init.
2011  addition_n_direct (S n) (x1 ::: v1) (x2 ::: v2) init = (ith_bit ? (x1 ::: v1) (x2 ::: v2) init) ::: (addition_n_direct … v1 v2 init). // qed.
2012 
2013lemma tail_Sn : ∀n. ∀x. ∀a : BitVector n. tail … (x ::: a) = a. // qed.
2014
2015(* Prove the equivalence of addition_n_direct with add_with_carries *)
2016lemma addition_n_direct_ok : ∀n,carry,v1,v2.
2017  (\fst (add_with_carries n v1 v2 carry)) = addition_n_direct n v1 v2 carry.
2018#n elim n
2019[ 1: #carry #v1 #v2 >(BitVector_O … v1) >(BitVector_O … v2) normalize @refl
2020| 2: #n' #Hind #carry #v1 #v2
2021     elim (BitVector_Sn … v1) #hd1 * #tl1 #Heq1
2022     elim (BitVector_Sn … v2) #hd2 * #tl2 #Heq2
2023     lapply (Hind carry tl1 tl2)
2024     lapply (ith_bit_ok ? carry v1 v2)
2025     lapply (ith_carry_ok ? carry v1 v2)
2026     destruct
2027     #Hind >addition_n_direct_Sn
2028     >ith_bit_Sn >add_with_carries_Sn
2029     elim (add_with_carries n' tl1 tl2 carry) #bits #flags normalize nodelta
2030     cases (match flags in Vector return λsz:ℕ.(λfoo:Vector bool sz.bool) with 
2031            [VEmpty⇒carry|VCons (sz:ℕ)   (cy:bool)   (tl:(Vector bool sz))⇒cy])
2032     normalize nodelta #Hcarry' lapply (Hcarry' ?? (refl ??))
2033     whd in match head'; normalize nodelta
2034     #H1 #H2 >H1 >H2 @refl
2035] qed.
2036
2037lemma addition_n_direct_ok2 : ∀n,carry,v1,v2.
2038  (let 〈a,b〉 ≝ add_with_carries n v1 v2 carry in a) = addition_n_direct n v1 v2 carry.
2039#n #carry #v1 #v2 <addition_n_direct_ok
2040cases (add_with_carries ????) //
2041qed.
2042 
2043(* trivially lift associativity to our new setting *)     
2044lemma associative_addition_n_direct : ∀n. ∀carry1,carry2. ∀v1,v2,v3 : BitVector n.
2045  addition_n_direct ? (addition_n_direct ? v1 v2 carry1) v3 carry2 =
2046  addition_n_direct ? v1 (addition_n_direct ? v2 v3 carry1) carry2.
2047#n #carry1 #carry2 #v1 #v2 #v3
2048<addition_n_direct_ok <addition_n_direct_ok
2049<addition_n_direct_ok <addition_n_direct_ok
2050lapply (associative_add_with_carries … carry1 carry2 v1 v2 v3)
2051elim (add_with_carries n v2 v3 carry1) #bits #carries normalize nodelta
2052elim (add_with_carries n v1 v2 carry1) #bits' #carries' normalize nodelta
2053#H @(sym_eq … H)
2054qed.
2055
2056lemma commutative_addition_n_direct : ∀n. ∀v1,v2 : BitVector n.
2057  addition_n_direct ? v1 v2 false = addition_n_direct ? v2 v1 false.
2058#n #v1 #v2 /by associative_addition_n, addition_n_direct_ok/
2059qed.
2060
2061definition increment_direct ≝ λn,v. addition_n_direct n v (one_bv ?) false.
2062definition twocomp_neg_direct ≝ λn,v. increment_direct n (negation_bv n v).
2063
2064
2065(* fold andb on a bitvector. *)
2066let rec andb_fold (n : nat) (b : BitVector n) on b : bool ≝
2067match b with
2068[ VEmpty ⇒ true
2069| VCons sz elt tl ⇒
2070  andb elt (andb_fold ? tl)
2071].
2072
2073lemma andb_fold_Sn : ∀n. ∀x. ∀b : BitVector n. andb_fold (S n) (x ::: b) = andb x (andb_fold … n b). // qed.
2074
2075lemma andb_fold_inversion : ∀n. ∀elt,x. andb_fold (S n) (elt ::: x) = true → elt = true ∧ andb_fold n x = true.
2076#n #elt #x cases elt normalize #H @conj destruct (H) try assumption @refl
2077qed.
2078
2079lemma ith_increment_carry : ∀n. ∀a : BitVector (S n).
2080  ith_carry … a (one_bv ?) false = andb_fold … a.
2081#n elim n
2082[ 1: #a elim (BitVector_Sn … a) #hd * #tl #Heq >Heq >(BitVector_O … tl)
2083     cases hd normalize @refl
2084| 2: #n' #Hind #a
2085     elim (BitVector_Sn … a) #hd * #tl #Heq >Heq
2086     lapply (Hind … tl) #Hind >one_bv_Sn
2087     >ith_carry_Sn whd in match (andb_fold ??);
2088     cases hd >Hind @refl
2089] qed.
2090
2091lemma ith_increment_bit : ∀n. ∀a : BitVector (S n).
2092  ith_bit … a (one_bv ?) false = xorb (head' … a) (andb_fold … (tail … a)).
2093#n #a
2094elim (BitVector_Sn … a) #hd * #tl #Heq >Heq
2095whd in match (head' ???);
2096-a cases n in tl;
2097[ 1: #tl >(BitVector_O … tl) cases hd normalize try //
2098| 2: #n' #tl >one_bv_Sn >ith_bit_Sn
2099     >ith_increment_carry >tail_Sn
2100     cases hd try //
2101] qed.
2102
2103(* Lemma used to prove involutivity of two-complement negation *)
2104lemma twocomp_neg_involutive_aux : ∀n. ∀v : BitVector (S n).
2105   (andb_fold (S n) (negation_bv (S n) v) =
2106    andb_fold (S n) (negation_bv (S n) (addition_n_direct (S n) (negation_bv (S n) v) (one_bv (S n)) false))).
2107#n elim n
2108[ 1: #v elim (BitVector_Sn … v) #hd * #tl #Heq >Heq >(BitVector_O … tl) cases hd @refl
2109| 2: #n' #Hind #v elim (BitVector_Sn … v) #hd * #tl #Heq >Heq
2110     lapply (Hind tl) -Hind #Hind >negation_bv_Sn >one_bv_Sn >addition_n_direct_Sn
2111     >andb_fold_Sn >ith_bit_Sn >negation_bv_Sn >andb_fold_Sn <Hind
2112     cases hd normalize nodelta
2113     [ 1: >xorb_false >(xorb_comm false ?) >xorb_false
2114     | 2: >xorb_false >(xorb_comm true ?) >xorb_true ]
2115     >ith_increment_carry
2116     cases (andb_fold (S n') (negation_bv (S n') tl)) @refl
2117] qed.
2118   
2119(* Test of the 'direct' approach: proof of the involutivity of two-complement negation. *)
2120lemma twocomp_neg_involutive : ∀n. ∀v : BitVector n. twocomp_neg_direct ? (twocomp_neg_direct ? v) = v.
2121#n elim n
2122[ 1: #v >(BitVector_O v) @refl
2123| 2: #n' cases n'
2124     [ 1: #Hind #v elim (BitVector_Sn … v) #hd * #tl #Heq >Heq
2125          >(BitVector_O … tl) normalize cases hd @refl
2126     | 2: #n'' #Hind #v elim (BitVector_Sn … v) #hd * #tl #Heq >Heq
2127          lapply (Hind tl) -Hind #Hind <Hind in ⊢ (???%);
2128          whd in match twocomp_neg_direct; normalize nodelta
2129          whd in match increment_direct; normalize nodelta
2130          >(negation_bv_Sn ? hd tl) >one_bv_Sn >(addition_n_direct_Sn ? (¬hd) false ??)
2131          >ith_bit_Sn >negation_bv_Sn >addition_n_direct_Sn >ith_bit_Sn
2132          generalize in match (addition_n_direct (S n'')
2133                                                   (negation_bv (S n'')
2134                                                   (addition_n_direct (S n'') (negation_bv (S n'') tl) (one_bv (S n'')) false))
2135                                                   (one_bv (S n'')) false); #tail
2136          >ith_increment_carry >ith_increment_carry
2137          cases hd normalize nodelta
2138          [ 1: normalize in match (xorb false false); >(xorb_comm false ?) >xorb_false >xorb_false
2139          | 2: normalize in match (xorb true false); >(xorb_comm true ?) >xorb_true >xorb_false ]
2140          <twocomp_neg_involutive_aux
2141          cases (andb_fold (S n'') (negation_bv (S n'') tl)) @refl
2142      ]
2143] qed.
2144
2145lemma bitvector_cons_inj_inv : ∀n. ∀a,b. ∀va,vb : BitVector n. a ::: va = b ::: vb → a =b ∧ va = vb.
2146#n #a #b #va #vb #H destruct (H) @conj @refl qed.
2147
2148lemma bitvector_cons_eq : ∀n. ∀a,b. ∀v : BitVector n. a = b → a ::: v = b ::: v. // qed.
2149
2150(* Injectivity of increment *)
2151lemma increment_inj : ∀n. ∀a,b : BitVector n.
2152  increment_direct ? a = increment_direct ? b →
2153  a = b ∧ (ith_carry n a (one_bv n) false = ith_carry n b (one_bv n) false).
2154#n whd in match increment_direct; normalize nodelta elim n
2155[ 1: #a #b >(BitVector_O … a) >(BitVector_O … b) normalize #_ @conj //
2156| 2: #n' cases n'
2157   [ 1: #_ #a #b
2158        elim (BitVector_Sn … a) #hd_a * #tl_a #Heq_a >Heq_a
2159        elim (BitVector_Sn … b) #hd_b * #tl_b #Heq_b >Heq_b
2160        >(BitVector_O … tl_a) >(BitVector_O … tl_b) cases hd_a cases hd_b
2161        normalize #H @conj try //
2162   | 2: #n'' #Hind #a #b
2163        elim (BitVector_Sn … a) #hd_a * #tl_a #Heq_a >Heq_a
2164        elim (BitVector_Sn … b) #hd_b * #tl_b #Heq_b >Heq_b
2165        lapply (Hind … tl_a tl_b) -Hind #Hind
2166        >one_bv_Sn >addition_n_direct_Sn >ith_bit_Sn
2167        >addition_n_direct_Sn >ith_bit_Sn >xorb_false >xorb_false
2168        #H elim (bitvector_cons_inj_inv … H) #Heq1 #Heq2
2169        lapply (Hind Heq2) * #Heq3 #Heq4
2170        cut (hd_a = hd_b)
2171        [ 1: >Heq4 in Heq1; #Heq5 lapply (xorb_inj (ith_carry ? tl_b (one_bv ?) false) hd_a hd_b)
2172             * #Heq6 #_ >xorb_comm in Heq6; >(xorb_comm  ? hd_b) #Heq6 >(Heq6 Heq5)
2173             @refl ]
2174        #Heq5 @conj [ 1: >Heq3 >Heq5 @refl ]
2175        >ith_carry_Sn >ith_carry_Sn >Heq4 >Heq5 @refl
2176] qed.
2177
2178(* Inverse of injecivity of increment, does not lose information (cf increment_inj) *)
2179lemma increment_inj_inv : ∀n. ∀a,b : BitVector n.
2180  a = b → increment_direct ? a = increment_direct ? b. // qed.
2181
2182(* A more general result. *)
2183lemma addition_n_direct_inj : ∀n. ∀x,y,delta: BitVector n.
2184  addition_n_direct ? x delta false = addition_n_direct ? y delta false →
2185  x = y ∧ (ith_carry n x delta false = ith_carry n y delta false).
2186#n elim n
2187[ 1: #x #y #delta >(BitVector_O … x) >(BitVector_O … y) >(BitVector_O … delta) * @conj @refl
2188| 2: #n' #Hind #x #y #delta
2189     elim (BitVector_Sn … x) #hdx * #tlx #Heqx >Heqx
2190     elim (BitVector_Sn … y) #hdy * #tly #Heqy >Heqy
2191     elim (BitVector_Sn … delta) #hdd * #tld #Heqd >Heqd
2192     >addition_n_direct_Sn >ith_bit_Sn
2193     >addition_n_direct_Sn >ith_bit_Sn
2194     >ith_carry_Sn >ith_carry_Sn
2195     lapply (Hind … tlx tly tld) -Hind #Hind #Heq
2196     elim (bitvector_cons_inj_inv … Heq) #Heq_hd #Heq_tl
2197     lapply (Hind Heq_tl) -Hind * #HindA #HindB
2198     >HindA >HindB >HindB in Heq_hd; #Heq_hd
2199     cut (hdx = hdy)
2200     [ 1: cases hdd in Heq_hd; cases (ith_carry n' tly tld false)
2201          cases hdx cases hdy normalize #H try @H try @refl
2202          >H try @refl ]
2203     #Heq_hd >Heq_hd @conj @refl
2204] qed.
2205
2206(* We also need it the other way around. *)
2207lemma addition_n_direct_inj_inv : ∀n. ∀x,y,delta: BitVector n.
2208  x ≠ y → (* ∧ (ith_carry n x delta false = ith_carry n y delta false). *)
2209   addition_n_direct ? x delta false ≠ addition_n_direct ? y delta false.
2210#n elim n
2211[ 1: #x #y #delta >(BitVector_O … x) >(BitVector_O … y) >(BitVector_O … delta) * #H @(False_ind … (H (refl ??)))
2212| 2: #n' #Hind #x #y #delta
2213     elim (BitVector_Sn … x) #hdx * #tlx #Heqx >Heqx
2214     elim (BitVector_Sn … y) #hdy * #tly #Heqy >Heqy
2215     elim (BitVector_Sn … delta) #hdd * #tld #Heqd >Heqd
2216     #Hneq
2217     cut (hdx ≠ hdy ∨ tlx ≠ tly)
2218     [ @(eq_bv_elim … tlx tly)
2219       [ #Heq_tl >Heq_tl >Heq_tl in Hneq;
2220         #Hneq cut (hdx ≠ hdy) [ % #Heq_hd >Heq_hd in Hneq; *
2221                                 #H @H @refl ]
2222         #H %1 @H
2223       | #H %2 @H ] ]
2224     -Hneq #Hneq
2225     >addition_n_direct_Sn >addition_n_direct_Sn
2226     >ith_bit_Sn >ith_bit_Sn cases Hneq
2227     [ 1: #Hneq_hd
2228          lapply (addition_n_direct_inj … tlx tly tld)         
2229          @(eq_bv_elim … (addition_n_direct ? tlx tld false) (addition_n_direct ? tly tld false))
2230          [ 1: #Heq -Hind #Hind elim (Hind Heq) #Heq_tl >Heq_tl #Heq_carry >Heq_carry
2231               % #Habsurd elim (bitvector_cons_inj_inv … Habsurd) -Habsurd
2232               lapply Hneq_hd
2233               cases hdx cases hdd cases hdy cases (ith_carry ? tly tld false)
2234               normalize in ⊢ (? → % → ?); #Hneq_hd #Heq_hd #_
2235               try @(absurd … Heq_hd Hneq_hd)
2236               elim Hneq_hd -Hneq_hd #Hneq_hd @Hneq_hd
2237               try @refl try assumption try @(sym_eq … Heq_hd)
2238          | 2: #Htl_not_eq #_ % #Habsurd elim (bitvector_cons_inj_inv … Habsurd) #_
2239               elim Htl_not_eq -Htl_not_eq #HA #HB @HA @HB ]
2240     | 2: #Htl_not_eq lapply (Hind tlx tly tld Htl_not_eq) -Hind #Hind
2241          % #Habsurd elim (bitvector_cons_inj_inv … Habsurd) #_
2242          elim Hind -Hind #HA #HB @HA @HB ]
2243] qed.
2244
2245lemma carry_notb : ∀a,b,c. notb (carry_of a b c) = carry_of (notb a) (notb b) (notb c). * * * @refl qed.
2246
2247lemma increment_to_carry_aux : ∀n. ∀a : BitVector (S n).
2248   ith_carry (S n) a (one_bv (S n)) false
2249   = ith_carry (S n) a (zero (S n)) true.
2250#n elim n
2251[ 1: #a elim (BitVector_Sn ? a) #hd_a * #tl_a #Heq >Heq >(BitVector_O … tl_a) @refl
2252| 2: #n' #Hind #a elim (BitVector_Sn ? a) #hd_a * #tl_a #Heq >Heq
2253     lapply (Hind tl_a) #Hind
2254     >one_bv_Sn >zero_Sn >ith_carry_Sn >ith_carry_Sn >Hind @refl
2255] qed.
2256
2257lemma neutral_addition_n_direct_aux : ∀n. ∀v. ith_carry n v (zero n) false = false.
2258#n elim n //
2259#n' #Hind #v elim (BitVector_Sn … v) #hd * #tl #Heq >Heq >zero_Sn
2260>ith_carry_Sn >(Hind tl) cases hd @refl.
2261qed.
2262
2263lemma neutral_addition_n_direct : ∀n. ∀v : BitVector n.
2264  addition_n_direct ? v (zero ?) false = v.
2265#n elim n
2266[ 1: #v >(BitVector_O … v) normalize @refl
2267| 2: #n' #Hind #v elim (BitVector_Sn … v) #hd * #tl #Heq >Heq
2268     lapply (Hind … tl) #H >zero_Sn >addition_n_direct_Sn
2269     >ith_bit_Sn >H >xorb_false >neutral_addition_n_direct_aux
2270     >xorb_false @refl
2271] qed.
2272
2273lemma increment_to_carry_zero : ∀n. ∀a : BitVector n. addition_n_direct ? a (one_bv ?) false = addition_n_direct ? a (zero ?) true.
2274#n elim n
2275[ 1: #a >(BitVector_O … a) normalize @refl
2276| 2: #n' cases n'
2277     [ 1: #_ #a elim (BitVector_Sn … a) #hd_a * #tl_a #Heq >Heq >(BitVector_O … tl_a) cases hd_a @refl
2278     | 2: #n'' #Hind #a
2279          elim (BitVector_Sn … a) #hd_a * #tl_a #Heq >Heq
2280          lapply (Hind tl_a) -Hind #Hind
2281          >one_bv_Sn >zero_Sn >addition_n_direct_Sn >ith_bit_Sn
2282          >addition_n_direct_Sn >ith_bit_Sn
2283          >xorb_false >Hind @bitvector_cons_eq
2284          >increment_to_carry_aux @refl
2285     ]
2286] qed.
2287
2288lemma increment_to_carry : ∀n. ∀a,b : BitVector n.
2289  addition_n_direct ? a (addition_n_direct ? b (one_bv ?) false) false = addition_n_direct ? a b true.
2290#n #a #b >increment_to_carry_zero <associative_addition_n_direct
2291>neutral_addition_n_direct @refl
2292qed.
2293
2294lemma increment_direct_ok : ∀n,v. increment_direct n v = increment n v.
2295#n #v whd in match (increment ??);
2296>addition_n_direct_ok <increment_to_carry_zero @refl
2297qed.
2298
2299(* Prove -(a + b) = -a + -b *)
2300lemma twocomp_neg_plus : ∀n. ∀a,b : BitVector n.
2301  twocomp_neg_direct ? (addition_n_direct ? a b false) = addition_n_direct ? (twocomp_neg_direct … a) (twocomp_neg_direct … b) false.
2302whd in match twocomp_neg_direct; normalize nodelta
2303lapply increment_inj_inv
2304whd in match increment_direct; normalize nodelta
2305#H #n #a #b
2306<associative_addition_n_direct @H
2307>associative_addition_n_direct >(commutative_addition_n_direct ? (one_bv n))
2308>increment_to_carry
2309-H lapply b lapply a -b -a
2310cases n
2311[ 1: #a #b >(BitVector_O … a) >(BitVector_O … b) @refl
2312| 2: #n' #a #b
2313     cut (negation_bv ? (addition_n_direct ? a b false)
2314           = addition_n_direct ? (negation_bv ? a) (negation_bv ? b) true ∧
2315          notb (ith_carry ? a b false) = (ith_carry ? (negation_bv ? a) (negation_bv ? b) true))
2316     [ -n lapply b lapply a elim n'
2317     [ 1: #a #b elim (BitVector_Sn … a) #hd_a * #tl_a #Heqa >Heqa >(BitVector_O … tl_a)
2318          elim (BitVector_Sn … b) #hd_b * #tl_b #Heqb >Heqb >(BitVector_O … tl_b)
2319          cases hd_a cases hd_b normalize @conj @refl
2320     | 2: #n #Hind #a #b
2321          elim (BitVector_Sn … a) #hd_a * #tl_a #Heqa >Heqa
2322          elim (BitVector_Sn … b) #hd_b * #tl_b #Heqb >Heqb
2323          lapply (Hind tl_a tl_b) * #H1 #H2
2324          @conj
2325          [ 2: >ith_carry_Sn >negation_bv_Sn >negation_bv_Sn >ith_carry_Sn
2326               >carry_notb >H2 @refl
2327          | 1: >addition_n_direct_Sn >ith_bit_Sn >negation_bv_Sn
2328               >negation_bv_Sn >negation_bv_Sn
2329               >addition_n_direct_Sn >ith_bit_Sn >H1 @bitvector_cons_eq
2330               >xorb_lneg >xorb_rneg >notb_notb
2331               <xorb_rneg >H2 @refl
2332          ]
2333      ] ]
2334      * #H1 #H2 @H1
2335] qed.
2336
2337lemma addition_n_direct_neg : ∀n. ∀a.
2338 (addition_n_direct n a (negation_bv n a) false) = replicate ?? true
2339 ∧ (ith_carry n a (negation_bv n a) false = false).
2340#n elim n
2341[ 1: #a >(BitVector_O … a) @conj @refl
2342| 2: #n' #Hind #a elim (BitVector_Sn … a) #hd * #tl #Heq >Heq
2343     lapply (Hind … tl) -Hind * #HA #HB
2344     @conj
2345     [ 2: >negation_bv_Sn >ith_carry_Sn >HB cases hd @refl
2346     | 1: >negation_bv_Sn >addition_n_direct_Sn
2347          >ith_bit_Sn >HB >xorb_false >HA
2348          @bitvector_cons_eq elim hd @refl
2349     ]
2350] qed.
2351
2352(* -a + a = 0 *)
2353lemma bitvector_opp_direct : ∀n. ∀a : BitVector n. addition_n_direct ? a (twocomp_neg_direct ? a) false = (zero ?).
2354whd in match twocomp_neg_direct;
2355whd in match increment_direct;
2356normalize nodelta
2357#n #a <associative_addition_n_direct
2358elim (addition_n_direct_neg … a) #H #_ >H
2359-H -a
2360cases n try //
2361#n'
2362cut ((addition_n_direct (S n') (replicate bool ? true) (one_bv ?) false = (zero (S n')))
2363       ∧ (ith_carry ? (replicate bool (S n') true) (one_bv (S n')) false = true))
2364[ elim n'
2365     [ 1: @conj @refl
2366     | 2: #n' * #HA #HB @conj
2367          [ 1: >replicate_Sn >one_bv_Sn  >addition_n_direct_Sn
2368               >ith_bit_Sn >HA >zero_Sn @bitvector_cons_eq >HB @refl
2369          | 2: >replicate_Sn >one_bv_Sn >ith_carry_Sn >HB @refl ]
2370     ]
2371] * #H1 #H2 @H1
2372qed.
2373
2374(* Lift back the previous result to standard operations. *)
2375lemma twocomp_neg_direct_ok : ∀n. ∀v. twocomp_neg_direct ? v = two_complement_negation n v.
2376#n #v whd in match twocomp_neg_direct; normalize nodelta
2377whd in match increment_direct; normalize nodelta
2378whd in match two_complement_negation; normalize nodelta
2379>increment_to_addition_n <addition_n_direct_ok
2380whd in match addition_n; normalize nodelta
2381elim (add_with_carries ????) #a #b @refl
2382qed.
2383
2384lemma two_complement_negation_plus : ∀n. ∀a,b : BitVector n.
2385  two_complement_negation ? (addition_n ? a b) = addition_n ? (two_complement_negation ? a) (two_complement_negation ? b).
2386#n #a #b
2387lapply (twocomp_neg_plus ? a b)
2388>twocomp_neg_direct_ok >twocomp_neg_direct_ok >twocomp_neg_direct_ok
2389<addition_n_direct_ok <addition_n_direct_ok
2390whd in match addition_n; normalize nodelta
2391elim (add_with_carries n a b false) #bits #flags normalize nodelta
2392elim (add_with_carries n (two_complement_negation n a) (two_complement_negation n b) false) #bits' #flags'
2393normalize nodelta #H @H
2394qed.
2395
2396lemma bitvector_opp_addition_n : ∀n. ∀a : BitVector n. addition_n ? a (two_complement_negation ? a) = (zero ?).
2397#n #a lapply (bitvector_opp_direct ? a)
2398>twocomp_neg_direct_ok <addition_n_direct_ok
2399whd in match (addition_n ???);
2400elim (add_with_carries n a (two_complement_negation n a) false) #bits #flags #H @H
2401qed.
2402
2403lemma neutral_addition_n : ∀n. ∀a : BitVector n. addition_n ? a (zero ?) = a.
2404#n #a
2405lapply (neutral_addition_n_direct n a)
2406<addition_n_direct_ok
2407whd in match (addition_n ???);
2408elim (add_with_carries n a (zero n) false) #bits #flags #H @H
2409qed.
2410
2411lemma injective_addition_n : ∀n. ∀x,y,delta : BitVector n.
2412  addition_n ? x delta = addition_n ? y delta → x = y. 
2413#n #x #y #delta 
2414lapply (addition_n_direct_inj … x y delta)
2415<addition_n_direct_ok <addition_n_direct_ok
2416whd in match addition_n; normalize nodelta
2417elim (add_with_carries n x delta false) #bitsx #flagsx
2418elim (add_with_carries n y delta false) #bitsy #flagsy
2419normalize #H1 #H2 elim (H1 H2) #Heq #_ @Heq
2420qed.
2421
2422lemma injective_inv_addition_n : ∀n. ∀x,y,delta : BitVector n.
2423  x ≠ y → addition_n ? x delta ≠ addition_n ? y delta. 
2424#n #x #y #delta 
2425lapply (addition_n_direct_inj_inv … x y delta)
2426<addition_n_direct_ok <addition_n_direct_ok
2427whd in match addition_n; normalize nodelta
2428elim (add_with_carries n x delta false) #bitsx #flagsx
2429elim (add_with_carries n y delta false) #bitsy #flagsy
2430normalize #H1 #H2 @(H1 H2)
2431qed.
2432
Note: See TracBrowser for help on using the repository browser.