source: src/Clight/frontend_misc.ma @ 2496

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

Some tentative work on the simulation proof for expressions, in order to adjust the invariant
on memories.

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