source: src/Clight/frontend_misc.ma @ 2468

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

Floats are gone from the front-end. Some trace amount might remain in RTL/RTLabs, but this should be easily fixable.
Also, work-in-progress in Clight/memoryInjections.ma

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