source: src/Clight/frontend_misc.ma @ 2572

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

Progress on toCminorCorrectness.

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