source: src/Clight/frontend_misc.ma @ 2565

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

Cl to Cm progress.

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