source: src/Clight/frontend_misc.ma @ 2510

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

Some progress on the Cl -> Cm front

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