source: src/Clight/switchRemoval.ma @ 2698

Last change on this file since 2698 was 2680, checked in by mckinna, 8 years ago

proofs which previously succeeded fail, thanks to fold on positive_map no longer unfolding (enough); marked with XXX
Correction: do more normalisation/conversion explicitly
Real solution: idnetify what's changed to expose this fragility (in normalisation?)

  • Property svn:executable set to *
File size: 153.0 KB
Line 
1include "Clight/Csyntax.ma".
2include "Clight/fresh.ma".
3include "common/Identifiers.ma".
4include "utilities/extralib.ma".
5include "Clight/Cexec.ma".
6include "Clight/CexecInd.ma".
7include "Clight/frontend_misc.ma".
8include "Clight/memoryInjections.ma".
9include "Clight/MemProperties.ma".
10include "basics/lists/list.ma".
11include "basics/lists/listb.ma".
12
13(* -----------------------------------------------------------------------------
14   ----------------------------------------------------------------------------*)
15
16(* -----------------------------------------------------------------------------
17   Documentation
18   ----------------------------------------------------------------------------*)
19
20(* This file implements transformation of switches to linear sequences of
21 * if/then/else. The implementation roughly follows the lines of the prototype.
22 * /!\ We assume that the program is well-typed (the type of the evaluated
23 * expression must match the constants on each branch of the switch). /!\ *)
24
25(* Documentation. Let the follwing be our input switch construct:
26   // --------------------------------- 
27   switch(e) {
28   case v1:
29     stmt1
30   case v2:
31     stmt2
32   .
33   .
34   .
35   default:
36     stmt_default
37   }
38   // --------------------------------- 
39 
40   Note that stmt1,stmt2, ... stmt_default may contain "break" statements, wich have the effect of exiting
41   the switch statement. In the absence of break, the execution falls through each case sequentially.
42 
43   Given such a statement, we produce an equivalent sequence of if-then-elses chained by gotos:
44
45   // --------------------------------- 
46   fresh = e;
47   if(fresh == v1) {
48     stmt1';
49     goto lbl_case2;
50   }
51   if(fresh == v2) {
52     lbl_case2:
53     stmt2';
54     goto lbl_case2;
55   }   
56   ...
57   stmt_default';
58   exit_label:
59   // ---------------------------------   
60
61   where stmt1', stmt2', ... stmt_default' are the statements where all top-level [break] statements
62   were replaced by [goto exit_label]. Note that fresh, lbl_casei are fresh identifiers and labels.
63*)
64
65
66(* -----------------------------------------------------------------------------
67   Definitions allowing to state that the program resulting of the transformation
68   is switch-free.
69   ---------------------------------------------------------------------------- *)
70
71(* Property of a Clight statement of containing no switch. Could be generalized into a kind of
72 * statement_P, if useful elsewhere. *)
73let rec switch_free (st : statement) : Prop ≝
74match st with
75[ Sskip ⇒ True
76| Sassign _ _ ⇒ True
77| Scall _ _ _ ⇒ True
78| Ssequence s1 s2 ⇒ switch_free s1 ∧ switch_free s2
79| Sifthenelse e s1 s2 ⇒ switch_free s1 ∧ switch_free s2
80| Swhile e body ⇒ switch_free body
81| Sdowhile e body ⇒ switch_free body
82| Sfor s1 _ s2 s3 ⇒ switch_free s1 ∧ switch_free s2 ∧ switch_free s3
83| Sbreak ⇒ True
84| Scontinue ⇒ True
85| Sreturn _ ⇒ True
86| Sswitch _ _ ⇒ False
87| Slabel _ body ⇒ switch_free body
88| Sgoto _ ⇒ True
89| Scost _ body ⇒ switch_free body
90].
91
92(* Property of a list of labeled statements of being switch-free *)
93let rec branches_switch_free (sts : labeled_statements) : Prop ≝
94match sts with
95[ LSdefault st =>
96  switch_free st
97| LScase _ _ st tl =>
98  switch_free st ∧ branches_switch_free tl
99].
100
101let rec branches_ind
102  (sts : labeled_statements)
103  (H   : labeled_statements → Prop) 
104  (defcase : ∀st. H (LSdefault st))
105  (indcase : ∀sz.∀int.∀st.∀sub_cases. H sub_cases → H (LScase sz int st sub_cases)) ≝
106match sts with
107[ LSdefault st ⇒
108  defcase st
109| LScase sz int st tl ⇒
110  indcase sz int st tl (branches_ind tl H defcase indcase)
111].
112
113(* -----------------------------------------------------------------------------
114   Switch-removal code for statements, functions and fundefs.
115   ----------------------------------------------------------------------------*)
116
117(* Converts the directly accessible ("free") breaks to gotos toward the [lab] label.  *)
118let rec convert_break_to_goto (st : statement) (lab : label) : statement ≝
119match st with
120[ Sbreak ⇒
121  Sgoto lab
122| Ssequence s1 s2 ⇒
123  Ssequence (convert_break_to_goto s1 lab) (convert_break_to_goto s2 lab)
124| Sifthenelse e iftrue iffalse ⇒
125  Sifthenelse e (convert_break_to_goto iftrue lab) (convert_break_to_goto iffalse lab)
126| Sfor init e update body ⇒
127  Sfor (convert_break_to_goto init lab) e update body
128| Slabel l body ⇒
129  Slabel l (convert_break_to_goto body lab)
130| Scost cost body ⇒
131  Scost cost (convert_break_to_goto body lab)
132| _ ⇒ st
133].
134
135(* Converting breaks preserves switch-freeness. *)
136lemma convert_break_lift : ∀s,label . switch_free s → switch_free (convert_break_to_goto s label).
137#s elim s //
138[ 1: #s1 #s2 #Hind1 #Hind2 #label * #Hsf1 #Hsf2 /3/
139| 2: #e #s1 #s2 #Hind1 #Hind2 #label * #Hsf1 #Hsf2 /3/
140| 3: #s1 #e #s2 #s3 #Hind1 #Hind2 #Hind3 #label * * #Hsf1 #Hsf2 #Hsf3 normalize
141     try @conj try @conj /3/
142| 4: #l #s0 #Hind #lab #Hsf whd in Hsf; normalize /2/
143| 5: #l #s0 #Hind #lab #Hsf whd in Hsf; normalize /3/
144] qed.
145
146(*  (def_case : ident × sf_statement) *)
147
148let rec produce_cond
149  (e : expr)
150  (switch_cases : labeled_statements)
151  (u : universe SymbolTag)
152  (exit : label) on switch_cases : statement × label × (universe SymbolTag) ≝
153match switch_cases with
154[ LSdefault st ⇒ 
155  let 〈lab,u1〉 ≝ fresh ? u in
156  let st' ≝ convert_break_to_goto st exit in
157  〈Slabel lab st', lab, u1〉
158| LScase sz tag st other_cases ⇒
159  let 〈sub_statements, sub_label, u1〉 ≝ produce_cond e other_cases u exit in
160  let st' ≝ convert_break_to_goto st exit in
161  let 〈lab, u2〉 ≝ fresh ? u1 in
162  let test ≝ Expr (Ebinop Oeq e (Expr (Econst_int sz tag) (typeof e))) (Tint I32 Signed) in
163  let case_statement ≝
164       Sifthenelse test
165        (Slabel lab (Ssequence st' (Sgoto sub_label)))
166        Sskip
167  in
168  〈Ssequence case_statement sub_statements, lab, u2〉
169].
170
171definition simplify_switch ≝
172   λ(e : expr).
173   λ(switch_cases : labeled_statements).
174   λ(uv : universe SymbolTag).
175 let 〈exit_label, uv1〉            ≝ fresh ? uv in
176 let 〈result, useless_label, uv2〉 ≝ produce_cond e switch_cases uv1 exit_label in
177 〈Ssequence result (Slabel exit_label Sskip), uv2〉.
178
179lemma produce_cond_switch_free : ∀l.∀H:branches_switch_free l.∀e,lab,u.switch_free (\fst (\fst (produce_cond e l u lab))).
180#l @(labeled_statements_ind … l)
181[ 1: #s #Hsf #e #lab #u normalize cases (fresh ??) #lab0 #u1
182     normalize in Hsf ⊢ %; @(convert_break_lift … Hsf)
183| 2: #sz #i #hd #tl #Hind whd in ⊢ (% → ?); * #Hsf_hd #Hsf_tl
184     #e #lab #u normalize
185     lapply (Hind Hsf_tl e lab u)
186     cases (produce_cond e tl u lab) * #cond #lab' #u' #Hsf normalize nodelta
187     cases (fresh ??) #lab0 #u2 normalize nodelta
188     normalize try @conj try @conj try @conj try //
189     @(convert_break_lift … Hsf_hd)
190] qed.
191
192lemma simplify_switch_switch_free : ∀e,l. ∀H:branches_switch_free l. ∀u. switch_free (\fst (simplify_switch e l u)).
193#e #l cases l
194[ 1: #def normalize #H #u cases (fresh ? u) #exit_label #uv normalize cases (fresh ? uv) #lab #uv' normalize nodelta
195     whd @conj whd
196     [ 1: @convert_break_lift assumption
197     | 2: @I ]
198| 2: #sz #i #case #tl normalize * #Hsf #Hsftl #u
199     cases (fresh ? u) #exit_label #uv1 normalize nodelta
200     lapply (produce_cond_switch_free tl Hsftl e exit_label uv1)
201     cases (produce_cond e tl uv1 exit_label)
202     * #cond #lab #u1 #Hsf_cond normalize nodelta
203     cases (fresh ??) #lab0 #u2 normalize nodelta
204     normalize @conj try @conj try @conj try @conj try //
205     @(convert_break_lift ?? Hsf)
206] qed.
207
208(* Instead of using tuples, we use a special type to pack the results of [switch_removal]. We do that in
209   order to circumvent the associativity problems in notations. *)
210(*
211record swret (A : Type[0]) : Type[0] ≝ {
212  ret_st  : A;
213  ret_acc : list (ident × type);
214  ret_u   : universe SymbolTag
215}.
216
217notation > "vbox('let' 〈ident v1, ident v2, ident v3〉 ≝ e in break e')" with precedence 48
218for @{ (λ${ident v1}.λ${ident v2}.λ${ident v3}. ${e'})
219          (ret_st ? ${e})
220          (ret_acc ? ${e})
221          (ret_u ? ${e}) }.
222
223definition ret ≝ λe1,e2,e3. mk_swret statement e1 e2 e3. *)
224     
225(* Recursively convert a statement into a switch-free one. We /provide/ directly to the function a list
226   of identifiers (supposedly fresh). The actual task of producing this identifier is decoupled in another
227   'twin' function. It is then proved that feeding [switch_removal] with the correct amount of free variables
228   allows it to proceed without failing. This is all in order to ease the proof of simulation. *)
229let rec switch_removal
230  (st : statement)           (* the statement in which we will remove switches *)
231  (u : universe SymbolTag)   (* a fresh label and ident generator *)
232  : statement × (list (ident × type)) × (universe SymbolTag) ≝
233match st with
234[ Sskip       ⇒ 〈st, [ ], u〉
235| Sassign _ _ ⇒ 〈st, [ ], u〉
236| Scall _ _ _ ⇒ 〈st, [ ], u〉
237| Ssequence s1 s2 ⇒
238  let 〈s1', acc1, u'〉 ≝ switch_removal s1 u in
239  let 〈s2', acc2, u''〉 ≝ switch_removal s2 u' in
240  〈Ssequence s1' s2', acc1 @ acc2, u''〉
241| Sifthenelse e s1 s2 ⇒
242  let 〈s1', acc1, u'〉 ≝ switch_removal s1 u in
243  let 〈s2', acc2, u''〉 ≝ switch_removal s2 u' in
244  〈Sifthenelse e s1' s2', acc1 @ acc2, u''〉
245| Swhile e body ⇒
246  let 〈body', acc, u'〉 ≝ switch_removal body u in
247  〈Swhile e body', acc, u'〉
248| Sdowhile e body ⇒
249  let 〈body', acc, u'〉 ≝ switch_removal body u in
250  〈Sdowhile e body', acc, u'〉
251| Sfor s1 e s2 s3 ⇒
252  let 〈s1', acc1, u'〉 ≝ switch_removal s1 u in
253  let 〈s2', acc2, u''〉 ≝ switch_removal s2 u' in
254  let 〈s3', acc3, u'''〉 ≝ switch_removal s3 u'' in
255  〈Sfor s1' e s2' s3', acc1 @ acc2 @ acc3, u'''〉
256| Sbreak ⇒
257  〈st, [ ], u〉
258| Scontinue ⇒
259  〈st, [ ], u〉
260| Sreturn _ ⇒
261  〈st, [ ], u〉
262| Sswitch e branches ⇒   
263  let 〈sf_branches, acc, u'〉 ≝ switch_removal_branches branches u in
264  let 〈switch_tmp, u''〉 ≝ fresh ? u' in
265  let ident         ≝ Expr (Evar switch_tmp) (typeof e) in
266  let assign        ≝ Sassign ident e in
267  let 〈result, u'''〉 ≝ simplify_switch ident sf_branches u'' in
268  〈Ssequence assign result, (〈switch_tmp, typeof e〉 :: acc), u'''〉
269| Slabel label body ⇒
270  let 〈body', acc, u'〉 ≝ switch_removal body u in
271  〈Slabel label body', acc, u'〉
272| Sgoto _ ⇒
273  〈st, [ ], u〉
274| Scost cost body ⇒
275  let 〈body', acc, u'〉 ≝ switch_removal body u in
276  〈Scost cost body', acc, u'〉
277]
278
279and switch_removal_branches
280  (l : labeled_statements)
281  (u : universe SymbolTag)
282 : (labeled_statements × (list (ident × type)) × (universe SymbolTag)) ≝
283match l with
284[ LSdefault st ⇒
285  let 〈st', acc1, u'〉 ≝ switch_removal st u in
286  〈LSdefault st', acc1, u'〉
287| LScase sz int st tl ⇒
288  let 〈tl_result, acc1, u'〉 ≝ switch_removal_branches tl u in
289  let 〈st', acc2, u''〉 ≝ switch_removal st u' in
290  〈LScase sz int st' tl_result, acc1 @ acc2, u''〉
291].
292
293definition ret_st : ∀A:Type[0]. (A × (list (ident × type)) × (universe SymbolTag)) → A ≝
294λA,x.
295  let 〈s,vars,u〉 ≝ x in s.
296
297definition ret_vars : ∀A:Type[0]. (A × (list (ident × type)) × (universe SymbolTag)) → list (ident × type) ≝
298λA,x.
299  let 〈s,vars,u〉 ≝ x in vars.
300
301definition ret_u : ∀A:Type[0]. (A × (list (ident × type)) × (universe SymbolTag)) → (universe SymbolTag) ≝
302λA,x.
303  let 〈s,vars,u〉 ≝ x in u.
304
305(* Proof that switch_removal_switch_free does its job. *)
306lemma switch_removal_switch_free : ∀st,u. switch_free (ret_st ? (switch_removal st u)).
307#st @(statement_ind2 ? (λls. ∀u. branches_switch_free (ret_st ? (switch_removal_branches ls u))) … st)
308try //
309[ 1: #s1 #s2 #H1 #H2 #u normalize
310     lapply (H1 u)
311     cases (switch_removal s1 u) * #st1 #vars1 #u' normalize #HA
312     lapply (H2 u')
313     cases (switch_removal s2 u') * #st2 #vars2 #u'' normalize #HB
314     @conj assumption
315| *:
316  (* TODO the first few cases show that the lemma is routinely proved. TBF later. *)
317  @cthulhu ]
318qed.
319
320(* -----------------------------------------------------------------------------
321   Switch-removal code for programs.
322   ----------------------------------------------------------------------------*) 
323
324(* The functions in fresh.ma do not consider labels. Using [universe_for_program p] may lead to
325 * name clashes for labels. We have no choice but to actually run through the function and to
326 * compute the maximum of labels+identifiers. This way we can generate both fresh variables and
327 * fresh labels using the same univ. While we're at it we also consider record fields.
328 * Cost labels are not considered, though. They already live in a separate universe.
329 *
330 * Important note: this is partially redundant with fresh.ma. We take care of avoiding name clashes,
331 * but in the end it might be good to move the following functions into fresh.ma.
332 *)
333
334(* Least element in the total order of identifiers. *)
335definition least_identifier ≝ an_identifier SymbolTag one.
336
337(* This is certainly overkill: variables adressed in an expression should be declared in the
338 * enclosing function's prototype. *)
339let rec max_of_expr (e : expr) : ident ≝
340match e with
341[ Expr ed _ ⇒
342  match ed with
343  [ Econst_int _ _ ⇒ least_identifier
344  | Evar id ⇒ id
345  | Ederef e1 ⇒ max_of_expr e1
346  | Eaddrof e1 ⇒ max_of_expr e1
347  | Eunop _ e1 ⇒ max_of_expr e1
348  | Ebinop _ e1 e2 ⇒ max_id (max_of_expr e1) (max_of_expr e2)
349  | Ecast _ e1 ⇒ max_of_expr e1
350  | Econdition e1 e2 e3 ⇒ 
351    max_id (max_of_expr e1) (max_id (max_of_expr e2) (max_of_expr e3))
352  | Eandbool e1 e2 ⇒
353    max_id (max_of_expr e1) (max_of_expr e2)
354  | Eorbool e1 e2 ⇒
355    max_id (max_of_expr e1) (max_of_expr e2) 
356  | Esizeof _ ⇒ least_identifier
357  | Efield r f ⇒ max_id f (max_of_expr r)
358  | Ecost _ e1 ⇒ max_of_expr e1
359  ]
360].
361
362(* Reasoning about this promises to be a serious pain. Especially the Scall case. *)
363let rec max_of_statement (s : statement) : ident ≝
364match s with
365[ Sskip ⇒ least_identifier
366| Sassign e1 e2 ⇒ max_id (max_of_expr e1) (max_of_expr e2)
367| Scall r f args ⇒
368  let retmax ≝
369    match r with
370    [ None ⇒ least_identifier
371    | Some e ⇒ max_of_expr e ]
372  in
373  max_id (max_of_expr f)
374         (max_id retmax
375                 (foldr ?? (λelt,acc. max_id (max_of_expr elt) acc) least_identifier args) )
376| Ssequence s1 s2 ⇒
377  max_id (max_of_statement s1) (max_of_statement s2)
378| Sifthenelse e s1 s2 ⇒
379  max_id (max_of_expr e) (max_id (max_of_statement s1) (max_of_statement s2))
380| Swhile e body ⇒
381  max_id (max_of_expr e) (max_of_statement body)
382| Sdowhile e body ⇒
383  max_id (max_of_expr e) (max_of_statement body)
384| Sfor init test incr body ⇒
385  max_id (max_id (max_of_statement init) (max_of_expr test)) (max_id (max_of_statement incr) (max_of_statement body))
386| Sbreak ⇒ least_identifier
387| Scontinue ⇒ least_identifier
388| Sreturn opt ⇒
389  match opt with
390  [ None ⇒ least_identifier
391  | Some e ⇒ max_of_expr e
392  ]
393| Sswitch e ls ⇒
394  max_id (max_of_expr e) (max_of_ls ls)
395| Slabel lab body ⇒
396  max_id lab (max_of_statement body)
397| Sgoto lab ⇒
398  lab
399| Scost _ body ⇒
400  max_of_statement body
401]
402and max_of_ls (ls : labeled_statements) : ident ≝
403match ls with
404[ LSdefault s ⇒ max_of_statement s
405| LScase _ _ s ls' ⇒ max_id (max_of_ls ls') (max_of_statement s)
406].
407
408definition max_id_of_function : function → ident ≝
409λf. max_id (max_of_statement (fn_body f)) (max_id_of_fn f).
410
411(* We compute fresh universes on a function-by function basis, since there can't
412 * be cross-functions gotos or stuff like that. *)
413definition function_switch_removal : function → function × (list (ident × type)) ≝
414λf.
415  let u ≝ universe_of_max (max_id_of_function f) in
416  let 〈st, vars, u'〉 ≝ switch_removal (fn_body f) u in
417  let result ≝ mk_function (fn_return f) (fn_params f) (vars @ (fn_vars f)) st in
418  〈result, vars〉.
419
420let rec fundef_switch_removal (f : clight_fundef) : clight_fundef ≝
421match f with
422[ CL_Internal f ⇒
423  CL_Internal (\fst (function_switch_removal f))
424| CL_External _ _ _ ⇒
425  f
426].
427
428let rec program_switch_removal (p : clight_program) : clight_program ≝
429 let prog_funcs ≝ prog_funct ?? p in
430 let sf_funcs   ≝ map ?? (λcl_fundef.
431    let 〈fun_id, fun_def〉 ≝ cl_fundef in
432    〈fun_id, fundef_switch_removal fun_def〉
433  ) prog_funcs in
434 mk_program ??
435  (prog_vars … p)
436  sf_funcs
437  (prog_main … p).
438
439(* -----------------------------------------------------------------------------
440   Applying two relations on all substatements and all subexprs (directly under).
441   ---------------------------------------------------------------------------- *)
442
443let rec substatement_P (s1 : statement) (P : statement → Prop) (Q : expr → Prop) : Prop ≝
444match s1 with
445[ Sskip ⇒ True
446| Sassign e1 e2 ⇒ Q e1 ∧ Q e2
447| Scall r f args ⇒
448  match r with
449  [ None ⇒ Q f ∧ (All … Q args)
450  | Some r ⇒ Q r ∧ Q f ∧ (All … Q args)
451  ]
452| Ssequence sub1 sub2 ⇒ P sub1 ∧ P sub2
453| Sifthenelse e sub1 sub2 ⇒ P sub1 ∧ P sub2
454| Swhile e sub ⇒ Q e ∧ P sub
455| Sdowhile e sub ⇒ Q e ∧ P sub
456| Sfor sub1 cond sub2 sub3 ⇒ P sub1 ∧ Q cond ∧ P sub2 ∧ P sub3
457| Sbreak ⇒ True
458| Scontinue ⇒ True
459| Sreturn r ⇒
460  match r with
461  [ None ⇒ True
462  | Some r ⇒ Q r ]
463| Sswitch e ls ⇒ Q e ∧ (substatement_ls ls P)
464| Slabel _ sub ⇒ P sub
465| Sgoto _ ⇒ True
466| Scost _ sub ⇒ P sub
467]
468and substatement_ls ls (P : statement → Prop) : Prop ≝
469match ls with
470[ LSdefault sub ⇒ P sub
471| LScase _ _ sub tl ⇒ P sub ∧ (substatement_ls tl P)
472].
473
474(* -----------------------------------------------------------------------------
475   Freshness conservation results on switch removal.
476   ---------------------------------------------------------------------------- *)
477
478(* Similar stuff in toCminor.ma. *)
479lemma fresh_for_univ_still_fresh :
480   ∀u,i. fresh_for_univ SymbolTag i u → ∀v,u'. 〈v, u'〉 = fresh ? u → fresh_for_univ ? i u'.
481* #p * #i #H1 #v * #p' lapply H1 normalize
482#H1 #H2 destruct (H2) /2/ qed.
483
484definition fresher_than_or_equal : universe SymbolTag → universe SymbolTag → Prop ≝
485λu1,u2.
486  match u1 with
487  [ mk_universe p1 ⇒
488    match u2 with
489    [ mk_universe p2 ⇒ p2 ≤ p1 ] ].
490   
491definition fte ≝ fresher_than_or_equal.
492
493lemma transitive_fte : ∀u1,u2,u3. fte u1 u2 → fte u2 u3 → fte u1 u3.
494* #u1 * #u2 * #u3 normalize /2 by transitive_le/
495qed.
496
497lemma reflexive_fte : ∀u. fte u u.
498* // qed.
499
500lemma fresher_for_univ : ∀u1,u2. fte u1 u2 → ∀i. fresh_for_univ ? i u2 → fresh_for_univ ? i u1.
501* #p * #p' normalize #H * #i normalize
502/2 by transitive_le/
503qed.
504
505lemma fresh_fte : ∀u2,u1,fv. fresh ? u2 = 〈fv,u1〉 → fte u1 u2.
506* #u1 * #u2 * #fv normalize #H1 destruct //
507qed.
508
509lemma produce_cond_fte : ∀e,exit,ls,u. fte (\snd (produce_cond e ls u exit)) u.
510#e #exit #ls @(branches_ind … ls)
511[ 1: #st #u normalize lapply (fresh_fte u)
512     cases (fresh ? u) #lab #u1 #H lapply (H u1 lab (refl ??)) normalize //
513| 2: #sz #i #hd #tl #Hind #u normalize
514     lapply (Hind u) cases (produce_cond e tl u exit) *
515     #subcond #sublabel #u1 #Hfte normalize
516     lapply (fresh_fte u1)
517     cases (fresh ? u1) #lab #u2 #H2 lapply (H2 u2 lab (refl ??))
518     #Hfte' normalize cases u2 in Hfte'; #u2
519     cases u in Hfte; #u cases u1 #u1 normalize
520     /2 by transitive_le/
521] qed.
522
523lemma produce_cond_fresh : ∀e,exit,ls,u,i. fresh_for_univ ? i u → fresh_for_univ ? i (\snd (produce_cond e ls u exit)).
524#e #exit #ls #u #i @fresher_for_univ @produce_cond_fte qed.
525
526lemma simplify_switch_fte : ∀u,e,ls.
527  fte (\snd (simplify_switch e ls u)) u.
528#u #e #ls normalize
529lapply (fresh_fte u)
530cases (fresh ? u)
531#exit_label #uv1 #Haux lapply (Haux uv1 exit_label (refl ??)) -Haux #Haux
532normalize
533lapply (produce_cond_fte e exit_label ls uv1)
534cases (produce_cond ????) * #stm #label #uv2 normalize nodelta
535cases uv2 #uv2 cases uv1 in Haux; #uv1 cases u #u normalize
536/2 by transitive_le/
537qed.
538
539lemma simplify_switch_fresh : ∀u,i,e,ls.
540 fresh_for_univ ? i u →
541 fresh_for_univ ? i (\snd (simplify_switch e ls u)).
542#u #i #e #ls @fresher_for_univ @simplify_switch_fte qed.
543
544lemma switch_removal_fte : ∀st,u.
545  fte (ret_u ? (switch_removal … st u)) u.
546#st @(statement_ind2 ? (λls. ∀u. fte (ret_u ? (switch_removal_branches ls u)) u) … st)
547try /2 by reflexive_fte/
548[ 1: #s1 #s2 #Hind1 #Hind2 #u normalize
549     lapply (Hind1 u)
550     cases (switch_removal s1 u) * #s1' #fvs1 #u'  normalize nodelta
551     lapply (Hind2 u')
552     cases (switch_removal s2 u') * #s2' #fvs2 #u'' normalize
553     #HA #HB @(transitive_fte … HA HB)
554| 2: #e #s1 #s2 #Hind1 #Hind2 #u normalize
555     lapply (Hind1 u)
556     cases (switch_removal s1 u) * #s1' #fvs1 #u'  normalize nodelta
557     lapply (Hind2 u')
558     cases (switch_removal s2 u') * #s2' #fvs2 #u'' normalize
559     #HA #HB @(transitive_fte … HA HB)
560| 3,7,8: #e #s #Hind #u normalize
561     lapply (Hind u)
562     cases (switch_removal s u) * #s' #fvs #u' normalize #H @H
563| 4: #e #s #Hind #u normalize
564     lapply (Hind u)
565     cases (switch_removal s u) * #s' #fvs #u' normalize #H @H
566| 5: #s1 #e #s2 #s3 #Hind1 #Hind2 #Hind3 #u normalize
567     lapply (Hind1 u) cases (switch_removal s1 u) * #s1' #fvs1 #u' #Hfte1
568     normalize nodelta
569     lapply (Hind2 u') cases (switch_removal s2 u') * #s2' #fvs2 #u'' #Hfte2
570     normalize nodelta
571     lapply (Hind3 u'') cases (switch_removal s3 u'') * #s2' #fvs2 #u'' #Hfte3
572     normalize nodelta
573     /3 by transitive_fte/
574| 6: #e #ls #Hind #u whd in match (switch_removal ??);
575     lapply (Hind u)
576     cases (switch_removal_branches ls u) * #ls #fvs #u' #Hfte1
577     normalize nodelta
578     lapply (fresh_fte … u') cases (fresh ? u') #fv #u'' #H lapply (H u'' fv (refl ??)) #Hfte2
579     normalize nodelta
580     lapply (simplify_switch_fte u'' (Expr (Evar fv) (typeof e)) ls)
581     cases (simplify_switch ???)
582     normalize nodelta
583     #st' #u''' #Hfte3
584     /3 by transitive_fte/
585| 9: #s #H #u normalize
586     lapply (H u) cases (switch_removal s u) * #st' #fvs normalize #u' #H @H
587| 10: #sz #i #st #ls #Hind1 #Hind2 #u normalize
588     lapply (Hind2 u) cases (switch_removal_branches ls u) * #ls' #fvs' #u'
589     normalize nodelta #Hfte1
590     lapply (Hind1 … u') cases (switch_removal st u') * #st' #fvs'' #u''
591     normalize nodelta #Hfte2
592     /3 by transitive_fte/
593] qed.     
594
595lemma switch_removal_fresh : ∀u,i,st.
596  fresh_for_univ ? i u →
597  fresh_for_univ ? i (ret_u … (switch_removal st u)).
598#u #i #st @fresher_for_univ @switch_removal_fte qed.
599
600(* -----------------------------------------------------------------------------
601   Simulation proof and related voodoo.
602   ----------------------------------------------------------------------------*)
603(*
604definition expr_lvalue_ind_combined ≝
605λP,Q,ci,cf,lv,vr,dr,ao,uo,bo,ca,cd,ab,ob,sz,fl,co,xx.
606conj ??
607 (expr_lvalue_ind P Q ci cf lv vr dr ao uo bo ca cd ab ob sz fl co xx)
608 (lvalue_expr_ind P Q ci cf lv vr dr ao uo bo ca cd ab ob sz fl co xx).*)
609 
610let rec expr_ind2
611    (P : expr → Prop) (Q : expr_descr → type → Prop)
612    (IE : ∀ed. ∀t. Q ed t → P (Expr ed t))
613    (Iconst_int : ∀sz, i, t. Q (Econst_int sz i) t)
614    (Ivar : ∀id, t. Q (Evar id) t)
615    (Ideref : ∀e, t. P e → Q (Ederef e) t)
616    (Iaddrof : ∀e, t. P e → Q (Eaddrof e) t)
617    (Iunop : ∀op,arg,t. P arg → Q (Eunop op arg) t)
618    (Ibinop : ∀op,arg1,arg2,t. P arg1 → P arg2 → Q (Ebinop op arg1 arg2) t)
619    (Icast : ∀castt, e, t. P e →  Q (Ecast castt e) t) 
620    (Icond : ∀e1,e2,e3,t. P e1 → P e2 → P e3 → Q (Econdition e1 e2 e3) t)
621    (Iandbool : ∀e1,e2,t. P e1 → P e2 → Q (Eandbool e1 e2) t)
622    (Iorbool : ∀e1,e2,t. P e1 → P e2 → Q (Eorbool e1 e2) t)
623    (Isizeof : ∀sizeoft,t. Q (Esizeof sizeoft) t)
624    (Ifield : ∀e,f,t. P e → Q (Efield e f) t)
625    (Icost : ∀c,e,t. P e → Q (Ecost c e) t)
626    (e : expr) on e : P e ≝
627match e with
628[ Expr ed t ⇒ IE ed t (expr_desc_ind2 P Q IE Iconst_int Ivar Ideref Iaddrof Iunop Ibinop Icast Icond Iandbool Iorbool Isizeof Ifield Icost ed t) ]
629
630and expr_desc_ind2
631    (P : expr → Prop) (Q : expr_descr → type → Prop)
632    (IE : ∀ed. ∀t. Q ed t → P (Expr ed t))
633    (Iconst_int : ∀sz, i, t. Q (Econst_int sz i) t)
634    (Ivar : ∀id, t. Q (Evar id) t)
635    (Ideref : ∀e, t. P e → Q (Ederef e) t)
636    (Iaddrof : ∀e, t. P e → Q (Eaddrof e) t)
637    (Iunop : ∀op,arg,t. P arg → Q (Eunop op arg) t)
638    (Ibinop : ∀op,arg1,arg2,t. P arg1 → P arg2 → Q (Ebinop op arg1 arg2) t)
639    (Icast : ∀castt, e, t. P e →  Q (Ecast castt e) t) 
640    (Icond : ∀e1,e2,e3,t. P e1 → P e2 → P e3 → Q (Econdition e1 e2 e3) t)
641    (Iandbool : ∀e1,e2,t. P e1 → P e2 → Q (Eandbool e1 e2) t)
642    (Iorbool : ∀e1,e2,t. P e1 → P e2 → Q (Eorbool e1 e2) t)
643    (Isizeof : ∀sizeoft,t. Q (Esizeof sizeoft) t)
644    (Ifield : ∀e,f,t. P e → Q (Efield e f) t)
645    (Icost : ∀c,e,t. P e → Q (Ecost c e) t)
646    (ed : expr_descr) (t : type) on ed : Q ed t ≝
647match ed with
648[ Econst_int sz i ⇒ Iconst_int sz i t
649| Evar id ⇒ Ivar id t
650| Ederef e ⇒ Ideref e t (expr_ind2 P Q IE Iconst_int Ivar Ideref Iaddrof Iunop Ibinop Icast Icond Iandbool Iorbool Isizeof Ifield Icost  e)
651| Eaddrof e ⇒ Iaddrof e t (expr_ind2 P Q IE Iconst_int Ivar Ideref Iaddrof Iunop Ibinop Icast Icond Iandbool Iorbool Isizeof Ifield Icost  e)
652| Eunop op e ⇒ Iunop op e t (expr_ind2 P Q IE Iconst_int Ivar Ideref Iaddrof Iunop Ibinop Icast Icond Iandbool Iorbool Isizeof Ifield Icost  e)
653| Ebinop op e1 e2 ⇒ Ibinop op e1 e2 t (expr_ind2 P Q IE Iconst_int Ivar Ideref Iaddrof Iunop Ibinop Icast Icond Iandbool Iorbool Isizeof Ifield Icost  e1) (expr_ind2 P Q IE Iconst_int Ivar Ideref Iaddrof Iunop Ibinop Icast Icond Iandbool Iorbool Isizeof Ifield Icost  e2)
654| Ecast castt e ⇒ Icast castt e t (expr_ind2 P Q IE Iconst_int Ivar Ideref Iaddrof Iunop Ibinop Icast Icond Iandbool Iorbool Isizeof Ifield Icost  e)
655| Econdition e1 e2 e3 ⇒ Icond e1 e2 e3 t (expr_ind2 P Q IE Iconst_int Ivar Ideref Iaddrof Iunop Ibinop Icast Icond Iandbool Iorbool Isizeof Ifield Icost  e1) (expr_ind2 P Q IE Iconst_int Ivar Ideref Iaddrof Iunop Ibinop Icast Icond Iandbool Iorbool Isizeof Ifield Icost  e2) (expr_ind2 P Q IE Iconst_int Ivar Ideref Iaddrof Iunop Ibinop Icast Icond Iandbool Iorbool Isizeof Ifield Icost  e3)
656| Eandbool e1 e2 ⇒ Iandbool e1 e2 t (expr_ind2 P Q IE Iconst_int Ivar Ideref Iaddrof Iunop Ibinop Icast Icond Iandbool Iorbool Isizeof Ifield Icost  e1) (expr_ind2 P Q IE Iconst_int Ivar Ideref Iaddrof Iunop Ibinop Icast Icond Iandbool Iorbool Isizeof Ifield Icost  e2)
657| Eorbool e1 e2 ⇒ Iorbool e1 e2 t (expr_ind2 P Q IE Iconst_int Ivar Ideref Iaddrof Iunop Ibinop Icast Icond Iandbool Iorbool Isizeof Ifield Icost  e1) (expr_ind2 P Q IE Iconst_int Ivar Ideref Iaddrof Iunop Ibinop Icast Icond Iandbool Iorbool Isizeof Ifield Icost  e2)
658| Esizeof sizeoft ⇒ Isizeof sizeoft t
659| Efield e field ⇒ Ifield e field t (expr_ind2 P Q IE Iconst_int Ivar Ideref Iaddrof Iunop Ibinop Icast Icond Iandbool Iorbool Isizeof Ifield Icost  e)
660| Ecost c e ⇒ Icost c e t (expr_ind2 P Q IE Iconst_int Ivar Ideref Iaddrof Iunop Ibinop Icast Icond Iandbool Iorbool Isizeof Ifield Icost e)
661].
662
663(* Correctness: we can't use a lock-step simulation result. The exec_step for Sswitch will be matched
664   by a non-constant number of evaluations in the converted program. More precisely,
665   [seq_of_labeled_statement (select_switch sz n sl)] will be matched by all the steps
666   necessary to execute all the "if-then-elses" corresponding to cases /before/ [n]. *)
667   
668(* A version of simplify_switch hiding the ugly projs *)
669definition fresh_for_expression ≝
670  λe,u. fresh_for_univ SymbolTag (max_of_expr e) u.
671
672definition fresh_for_statement ≝
673  λs,u. fresh_for_univ SymbolTag (max_of_statement s) u.
674
675(* needed during mutual induction ... *)
676definition fresh_for_labeled_statements ≝
677  λls,u. fresh_for_univ ? (max_of_ls ls) u.
678   
679definition fresh_for_function ≝
680  λf,u. fresh_for_univ SymbolTag (max_id_of_function f) u.
681
682(* misc properties of the max function on positives. *)
683
684lemma max_id_one_neutral : ∀v. max_id v (an_identifier ? one) = v.
685* #p whd in ⊢ (??%?); >max_one_neutral // qed.
686
687lemma max_id_commutative : ∀v1, v2. max_id v1 v2 = max_id v2 v1.
688* #p1 * #p2 whd in match (max_id ??) in ⊢ (??%%);
689>commutative_max // qed.
690
691lemma max_id_associative : ∀v1, v2, v3. max_id (max_id v1 v2) v3 = max_id v1 (max_id v2 v3).
692* #a * #b * #c whd in match (max_id ??) in ⊢ (??%%); >associative_max //
693qed.
694
695lemma fresh_max_split : ∀a,b,u. fresh_for_univ SymbolTag (max_id a b) u → fresh_for_univ ? a u ∧ fresh_for_univ ? b u.
696* #a * #b * #u normalize
697lapply (pos_compare_to_Prop a b)
698cases (pos_compare a b) whd in ⊢ (% → ?); #Hab
699[ 1: >(le_to_leb_true a b) try /2/ #Hbu @conj /2/
700| 2: destruct >reflexive_leb /2/
701| 3: >(not_le_to_leb_false a b) try /2/ #Hau @conj /2/
702] qed.
703
704lemma fresh_to_substatements :
705  ∀s,u. fresh_for_statement s u →
706        substatement_P s (λs'. fresh_for_statement s' u) (λe. fresh_for_expression e u).
707#s #u cases s
708whd in match (fresh_for_statement ??);
709whd in match (substatement_P ???); try /2/
710[ 1: #e1 #e2
711     whd in match (fresh_for_statement ??);
712     whd in match (substatement_P ???);
713     #H lapply (fresh_max_split … H) * /2 by conj/     
714| 2: #e1 #e2 #args
715     whd in match (fresh_for_statement ??);
716     whd in match (substatement_P ???);
717     cases e1 normalize nodelta
718     [ 1: #H lapply (fresh_max_split … H) * #HA #HB @conj try @HA
719          elim args in HB; try /2 by I/ #hd #tl normalize nodelta #Hind #HB
720          elim (fresh_max_split … HB) #HC #HD
721          whd in match (foldr ?????) in HD;
722          elim (fresh_max_split … HD) #HE #HF
723          @conj try assumption
724          @Hind >max_id_commutative >max_id_one_neutral @HF
725     | 2: #expr #H cases (fresh_max_split … H) #HA normalize nodelta #HB
726          cases (fresh_max_split … HB) #HC #HD @conj try @conj try // elim args in HD; try //
727          #e #l #Hind #HD
728          whd in match (foldr ?????) in HD;
729          elim (fresh_max_split … HD) #HE #HF
730          @conj try assumption
731          @Hind @HF ]
732| 3: #stmt1 #stmt2 whd in ⊢ (% → %); @fresh_max_split
733| 4: #e #s1 #s2 whd in ⊢ (% → %); #H lapply (fresh_max_split … H) *
734     #H1 @fresh_max_split
735| 5: #e1 #s whd in ⊢ (% → %); #H @(fresh_max_split … H)
736| 6: #e1 #s whd in ⊢ (% → %); #H @(fresh_max_split … H)
737| 7: #s1 #e #s2 #s3 whd in ⊢ (% → %); #H lapply (fresh_max_split … H) * #H1 #H2
738     @conj try @conj try @I try @conj try @I
739     elim (fresh_max_split … H1) elim (fresh_max_split … H2) /2/
740| 8: #opt cases opt try /2/
741| 9: #e #ls #H whd @conj lapply (fresh_max_split … H) * #HA #HB try // lapply HB
742     @(labeled_statements_ind … ls)
743     [ 1: #s' #H' //
744     | 2: #sz #i #s' #tl #Hind #H lapply (fresh_max_split … H) * #H1 #H2 whd @conj
745          [ 1: //
746          | 2: @Hind @H1 ] ]
747| 10: #lab #stmt #H whd lapply (fresh_max_split … H) * //
748] qed.
749
750(* Auxilliary commutation lemma used in [substatement_fresh]. *)
751lemma foldl_max : ∀l,a,b.
752  foldl ?? (λacc,elt.max_id (max_of_expr elt) acc) (max_id a b) l =
753  max_id a (foldl ?? (λacc,elt.max_id (max_of_expr elt) acc) b l).
754#l elim l
755[ 1: * #a * #b whd in match (foldl ?????) in ⊢ (??%%); @refl
756| 2: #hd #tl #Hind #a #b whd in match (foldl ?????) in ⊢ (??%%);
757     <Hind <max_id_commutative >max_id_associative >(max_id_commutative b ?) @refl
758] qed.
759
760(* --------------------------------------------------------------------------- *)
761(* Memory extensions (limited form on memoryInjection.ma). Note that we state the
762   properties at the back-end level. *)
763(* --------------------------------------------------------------------------- *) 
764
765(* A writeable_block is a block that is:
766   . valid
767   . non-empty (i.e. it has been allocated a non-empty size)
768*)
769record nonempty_block (m : mem) (b : block) : Prop ≝
770{
771  wb_valid    : valid_block m b;
772  wb_nonempty : low (blocks m b) < high (blocks m b)
773}.
774
775(* Type stating that m2 is an extension of m1, parameterised by a list of blocks where we can write freely *)
776record sr_memext (m1 : mem) (m2 : mem) (writeable : list block) : Prop ≝
777{ (*  Non-empty blocks are preserved as they are. This entails [load_sim]. *)
778  me_nonempty : ∀b. nonempty_block m1 b → nonempty_block m2 b ∧ blocks m1 b = blocks m2 b;
779  (* These blocks are valid in [m2] *)
780  me_writeable_valid : ∀b. meml ? b writeable → nonempty_block m2 b;
781  (* blocks in [m1] than can be validly pointed to cannot be in [me_writeable]. *)
782  me_not_writeable : ∀b. nonempty_block m1 b → ¬ (meml ? b writeable)
783 
784  (* This field is not entailed [me_not_writeable] and is necessary to prove valid
785     pointer conservation after a [free]. *)
786
787  (* Extension blocks contain nothing in [m1] *)
788  (* me_not_mapped : ∀b. meml … b me_writeable → blocks m1 b = empty_block OZ OZ;  *)
789  (* Valid pointers are still valid in m2. One could think that this is superfluous as
790     being implied by me_inj, and it is but for a small detail: valid_pointer allows a pointer
791     to be one off the end of a block bound. The internal check in beloadv does not.
792     valid_pointer should be understood as "pointer making sense" rather than "pointer from
793     which you can load stuff". [mi_valid_pointers] is used for instance when proving the
794     semantics preservation for equality testing (where we check that the pointers we
795     compare are valid before being equal).
796  *)
797(*  me_valid_pointers : ∀p.
798                       valid_pointer m1 p = true →
799                       valid_pointer m2 p = true *)
800}.
801
802(* Since we removed end_pointers, we can prove some stuff that was previously given as a field of
803   sr_memext. *)
804lemma me_not_writeable_ptr :
805  ∀m1,m2,writeable.
806  sr_memext m1 m2 writeable →
807  ∀p. valid_pointer m1 p = true → ¬ (meml ? (pblock p) writeable).
808#m1 #m2 #writeable #Hext #p #Hvalid
809cut (nonempty_block m1 (pblock p))
810[ 1: cases (valid_pointer_to_Prop … Hvalid) * #HA #HB #HC % //
811     /2 by Zle_to_Zlt_to_Zlt/
812| 2: @(me_not_writeable … Hext) ]
813qed.
814
815(* If we have a memory extension, we can simulate loads *)
816lemma sr_memext_load_sim : ∀m1,m2,writeable. sr_memext m1 m2 writeable → load_sim m1 m2.
817#m1 #m2 #writeable #Hext #ptr #bev whd in match (beloadv ??) in ⊢ (% → %);
818#H cut (nonempty_block m1 (pblock ptr) ∧
819         Zle (low (blocks m1 (pblock ptr)))
820               (Z_of_unsigned_bitvector 16 (offv (poff ptr))) ∧
821         Zlt (Z_of_unsigned_bitvector 16 (offv (poff ptr)))
822              (high (blocks m1 (pblock ptr))) ∧
823        bev = (contents (blocks m1 (pblock ptr)) (Z_of_unsigned_bitvector 16 (offv (poff ptr)))))
824[ @conj try @conj try @conj try %
825  [ 1: @Zltb_true_to_Zlt ]
826  cases (Zltb (block_id (pblock ptr)) (nextblock m1)) in H; normalize nodelta
827  [ 1: //
828  | 2,4,6,8,10: #Habsurd destruct ]
829  generalize in match (Z_of_unsigned_bitvector offset_size (offv (poff ptr))); #z
830  lapply (Zleb_true_to_Zle (low (blocks m1 (pblock ptr))) z)
831  lapply (Zltb_true_to_Zlt z (high (blocks m1 (pblock ptr))))
832  cases (Zleb (low (blocks m1 (pblock ptr))) z)
833  cases (Zltb z (high (blocks m1 (pblock ptr)))) #H1 #H2
834  [ 2,3,4,6,7,8,10,11,12,14,15,16: normalize #Habsurd destruct ] normalize #Heq
835  lapply (H1 (refl ??)) lapply (H2 (refl ??))
836  #Hle #Hlt destruct try assumption try @refl
837  @(Zle_to_Zlt_to_Zlt … Hle Hlt) ]
838* * * #Hnonempty #Hlow #Hhigh #Hres lapply (me_nonempty … Hext … Hnonempty) *
839* #Hvalid #Hlt #Hblocks_eq
840>(Zlt_to_Zltb_true … Hvalid) normalize <Hblocks_eq
841>(Zle_to_Zleb_true … Hlow) >(Zlt_to_Zltb_true … Hhigh) normalize
842>Hres @refl
843qed.
844
845lemma me_valid_pointers :
846  ∀m1,m2,writeable.
847  sr_memext m1 m2 writeable →
848  ∀p. valid_pointer m1 p = true → valid_pointer m2 p = true.
849* #contents1 #nextblock1 #Hnextblock_pos1
850* #contents2 #nextblock2 #Hnextblock_pos2 #writeable #Hmemext * #pb #po #Hvalid
851cut (nonempty_block (mk_mem contents1 nextblock1 Hnextblock_pos1) pb)
852[ 1: cases (valid_pointer_to_Prop … Hvalid) * #HA #HB #HC % //
853     /2 by Zle_to_Zlt_to_Zlt/ ]
854#Hnonempty lapply (me_nonempty … Hmemext … Hnonempty) * * #Hvalid_block #Hlow_lt_high
855#Hcontents_eq normalize >(Zlt_to_Zltb_true … Hvalid_block) normalize nodelta
856<Hcontents_eq cases (valid_pointer_to_Prop … Hvalid) * #_ #Hle #Hlt
857>(Zle_to_Zleb_true … Hle) normalize nodelta
858>(Zlt_to_Zltb_true … Hlt) @refl
859qed.
860
861(* --------------------------------------------------------------------------- *)
862(* Some lemmas on environments and their contents *)
863
864
865(* Definition of environment inclusion and equivalence *)
866(* Environment "inclusion". *)
867definition environment_sim ≝ λenv1,env2.
868  ∀id, res. lookup SymbolTag block env1 id = Some ? res →
869            lookup SymbolTag block env2 id = Some ? res.
870           
871lemma environment_sim_invert_aux : ∀en1,en2.
872  (∀id,res. lookup_opt block id en1 = Some ? res → lookup_opt ? id en2 = Some ? res) →
873  ∀id. lookup_opt ? id en2 = None ? → lookup_opt ? id en1 = None ?.
874#en1 elim en1 try //
875#opt1 #left1 #right1 #Hindl1 #Hindr1 #en2 #Hsim
876normalize #id elim id normalize nodelta
877[ 1: #Hlookup cases opt1 in Hsim; try // #res #Hsim lapply (Hsim one res (refl ??))
878     #Hlookup2 >Hlookup2 in Hlookup; #H @H
879| 2: #id' cases en2 in Hsim;
880     [ 1: normalize  #Hsim #_ #_ lapply (Hsim (p1 id')) normalize nodelta
881          cases (lookup_opt block id' right1) try //
882          #res #Hsim' lapply (Hsim' ? (refl ??)) #Habsurd destruct
883     | 2: #opt2 #left2 #right2 #Hsim #Hlookup whd in ⊢ ((??%?) → ?); #Hlookup'
884          @(Hindr1 right2) try // #id0 #res0
885          lapply (Hsim (p1 id0) res0) normalize #Hsol #H @Hsol @H ]
886| 3: #id' cases en2 in Hsim;
887     [ 1: normalize  #Hsim #_ #_ lapply (Hsim (p0 id')) normalize nodelta
888          cases (lookup_opt block id' left1) try //
889          #res #Hsim' lapply (Hsim' ? (refl ??)) #Habsurd destruct
890     | 2: #opt2 #left2 #right2 #Hsim #Hlookup whd in ⊢ ((??%?) → ?); #Hlookup'
891          @(Hindl1 left2) try // #id0 #res0
892          lapply (Hsim (p0 id0) res0) normalize #Hsol #H @Hsol @H ]
893] qed.         
894
895lemma environment_sim_invert :
896  ∀en1,en2. environment_sim en1 en2 →
897  ∀id. lookup SymbolTag block en2 id = None ? →
898       lookup SymbolTag block en1 id = None ?.
899* #en1 * #en2 #Hsim * #id @environment_sim_invert_aux
900#id' #res #Hlookup normalize in Hsim;
901lapply (Hsim (an_identifier … id') res) normalize #Hsol @Hsol @Hlookup
902qed.
903
904(* Environment equivalence. *)
905definition environment_eq ≝ λenv1,env2. environment_sim env1 env2 ∧ environment_sim env2 env1.
906
907lemma symmetric_environment_eq : ∀env1,env2. environment_eq env1 env2 → environment_eq env2 env1.
908#env1 #env2 * #Hsim1 #Hsim2 % // qed.
909
910lemma reflexive_environment_eq : ∀env. environment_eq env env.
911#env % // qed.
912
913(* An environment [e2] is a disjoint extension of [e1] iff (the new bindings are
914   fresh and [e2] is equivalent to adding extension blocks to [e1]).  *)
915definition disjoint_extension ≝
916  λ(e1, e2 : env).
917  λ(new_vars : list (ident × type)).
918 (∀id. mem_assoc_env id new_vars = true → lookup ?? e1 id = None ?) ∧          (* extension not in e1 *)
919 (∀id. mem_assoc_env id new_vars = true → ∃res.lookup ?? e2 id = Some ? res) ∧ (* all of the extension is in e2 *)
920 (∀id. mem_assoc_env id new_vars = false → lookup ?? e1 id = lookup ?? e2 id). (* only [new_vars] extends e2 *)
921 
922lemma disjoint_extension_to_inclusion :
923  ∀e1,e2,vars. disjoint_extension e1 e2 vars →
924  environment_sim e1 e2.
925#e1 #e2 #vars * * #HA #HB #HC whd #id #res
926@(match (mem_assoc_env id vars) return λx.(mem_assoc_env id vars = x) → ?
927with
928[ true ⇒ λH. ?
929| false ⇒ λH. ?
930] (refl ? (mem_assoc_env id vars)))
931[ 1: #Hlookup lapply (HA ? H) #Habsurd >Habsurd in Hlookup; #H destruct
932| 2: #Hlookup <(HC ? H) assumption ]
933qed.
934
935(* Small aux lemma to decompose folds on maps with list accumulators *)
936lemma fold_to_concat : ∀A:Type[0].∀m:positive_map A.∀l.∀f.
937 (fold ?? (λx,a,el. 〈an_identifier SymbolTag (f x), a〉::el) m l)
938 = (fold ?? (λx,a,el. 〈an_identifier SymbolTag (f x), a〉::el) m []) @ l.
939#A #m elim m
940[ 1: #l #f normalize @refl
941| 2: #optblock #left #right
942     #Hind1 #Hind2 #l #f
943     whd in match (fold ?????) in ⊢ (??%%);
944     cases optblock
945     [ 1: normalize (* XXX nodelta not enough here *) >Hind1 >Hind2 >Hind2 in ⊢ (???%);
946          >associative_append @refl
947     | 2: #block normalize (* XXX nodelta not enough here *) >Hind1 >Hind2 >Hind2 in ⊢ (???%);
948          >Hind1 in ⊢ (???%); >append_cons <associative_append @refl
949     ]
950] qed.
951
952lemma map_proj_fold : ∀A:Type[0].∀m:positive_map A. ∀f. ∀l.
953  map ?? (λx.\snd  x) (fold ?? (λx,a,el.〈an_identifier SymbolTag x,a〉::el) m l) =
954  map ?? (λx.\snd  x) (fold ?? (λx,a,el.〈an_identifier SymbolTag (f x),a〉::el) m l).
955#A #m elim m
956[ 1: #f #l normalize @refl
957| 2: #opt #left #right #Hind1 #Hind2 #f #l
958      normalize cases opt
959      [ 1: normalize nodelta >fold_to_concat >fold_to_concat in ⊢ (???%);
960           <map_append <map_append <Hind2 <Hind2 in ⊢ (???%);
961           <Hind1 <Hind1 in ⊢ (???%); @refl
962      | 2: #elt normalize nodelta >fold_to_concat >fold_to_concat in ⊢ (???%);
963           <map_append <map_append <Hind2 <Hind2 in ⊢ (???%);
964           <(Hind1 p0) <Hind1 in ⊢ (???%);
965           >(fold_to_concat ?? (〈an_identifier SymbolTag one,elt〉::l))
966           >(fold_to_concat ?? (〈an_identifier SymbolTag (f one),elt〉::l))
967           <map_append <map_append normalize in match (map ??? (cons ???)); @refl
968      ]
969] qed.
970
971lemma lookup_entails_block : ∀en:env.∀id,res.
972  lookup SymbolTag block en id = Some ? res →
973  meml ? res (blocks_of_env en).
974 * #map * #id #res
975whd in match (blocks_of_env ?);
976whd in match (elements ???);
977whd in match (lookup ????);
978normalize nodelta
979lapply res lapply id -id -res
980elim map
981[ 1: #id #res normalize #Habsurd destruct (Habsurd)
982| 2: #optblock #left #right #Hind1 #Hind2 #id #res #Hind3
983     whd in match (fold ?????);
984     cases optblock in Hind3;
985     [ 1: normalize nodelta
986          whd in match (lookup_opt ???);
987          cases id normalize nodelta
988          [ 1: #Habsurd
989             (* XXX nodelta not enough here *) change with (None ? = Some …) in Habsurd;
990                destruct (Habsurd)
991          | 2: #p #Hright lapply (Hind2 … Hright)
992                normalize >fold_to_concat in ⊢ (? → %);
993                <map_append #Haux @mem_append_backwards %1
994                <map_proj_fold @Haux
995          | 3: #p #Hleft lapply (Hind1 … Hleft)
996                normalize >fold_to_concat in ⊢ (? → %);
997                <map_append #Haux @mem_append_backwards %2
998                <map_proj_fold @Haux ]
999     | 2: #blo whd in match (lookup_opt ???);
1000          normalize >fold_to_concat <map_append
1001          cases id normalize nodelta
1002          [ 1: #Heq destruct (Heq)
1003               @mem_append_backwards %2 >fold_to_concat
1004               <map_append @mem_append_backwards %2 normalize %1 @refl
1005          | 2: #p #Hlookup lapply (Hind2 … Hlookup) #H
1006               @mem_append_backwards %1
1007               <map_proj_fold @H
1008          | 3: #p #Hlookup lapply (Hind1 … Hlookup) #H
1009               @mem_append_backwards %2 >fold_to_concat
1010               <map_append @mem_append_backwards %1
1011               <map_proj_fold @H
1012          ]
1013     ]
1014] qed.
1015
1016lemma blocks_of_env_cons :
1017  ∀en,id,hd. mem ? hd (blocks_of_env (add SymbolTag block en id hd)).
1018#en #id #hd @(lookup_entails_block … id)
1019cases id #p elim p try /2/ qed.
1020
1021lemma in_blocks_exists_id : ∀env.∀bl. meml … bl (blocks_of_env env) → ∃id. lookup SymbolTag block env id = Some ? bl.
1022#env elim env #m elim m
1023[ 1: #bl normalize @False_ind
1024| 2: #opt #left #right #Hind1 #Hind2 #bl normalize
1025     >fold_to_concat <map_append #H
1026     elim (mem_append_forward ???? H)
1027     [ 1: <map_proj_fold -H #H lapply (Hind2 … H)
1028          * * #id #Hlookup normalize in Hlookup;
1029          %{(an_identifier SymbolTag (p1 id))} normalize nodelta @Hlookup
1030     | 2: <map_proj_fold cases opt
1031          [ 1: normalize -H #H lapply (Hind1 … H)
1032               * * #id #Hlookup normalize in Hlookup;
1033               %{(an_identifier SymbolTag (p0 id))} normalize nodelta @Hlookup
1034          | 2: #bl' normalize >fold_to_concat <map_append normalize
1035               #H' elim (mem_append_forward ???? H')
1036               [ 1: -H #H lapply (Hind1 … H) * * #id normalize #Hlookup
1037                    %{(an_identifier ? (p0 id))} normalize nodelta @Hlookup
1038               | 2: normalize * [ 2: @False_ind ]
1039                    #Heq destruct (Heq)
1040                    %{(an_identifier SymbolTag one)} @refl
1041               ]
1042          ]
1043     ]
1044] qed.
1045
1046let rec inclusion_elim
1047  (A : Type[0])
1048  (m1, m2 : positive_map A)
1049  (P : positive_map A → positive_map A → Prop)
1050  (H1 : ∀m2. P (pm_leaf A) m2)
1051  (H2 : ∀opt1,left1,right1. P left1 (pm_leaf A) → P right1 (pm_leaf A) → P (pm_node A opt1 left1 right1) (pm_leaf A))
1052  (H3 : ∀opt1,left1,right1,opt2,left2,right2. P left1 left2 → P right1 right2 → P (pm_node A opt1 left1 right1) (pm_node A opt2 left2 right2))
1053  on m1 : P m1 m2 ≝
1054match m1 with
1055[ pm_leaf ⇒
1056  H1 m2
1057| pm_node opt1 left1 right1 ⇒
1058  match m2 with
1059  [ pm_leaf ⇒
1060    H2 opt1 left1 right1 (inclusion_elim A left1 (pm_leaf A) P H1 H2 H3) (inclusion_elim A right1 (pm_leaf A) P H1 H2 H3)
1061  | pm_node opt2 left2 right2 ⇒
1062    H3 opt1 left1 right1 opt2 left2 right2 (inclusion_elim A left1 left2 P H1 H2 H3) (inclusion_elim A right1 right2 P H1 H2 H3)
1063  ]
1064].
1065
1066(* Environment inclusion entails block inclusion. *)
1067lemma environment_sim_blocks_inclusion :
1068  ∀env1, env2. environment_sim env1 env2 → lset_inclusion ? (blocks_of_env env1) (blocks_of_env env2). 
1069* #m1 * #m2 @(inclusion_elim … m1 m2) -m1 -m2
1070[ 1: #m2 normalize #_ @I
1071| 2: #opt1 #left1 #right1 #Hind1 #Hind2 #Hsim
1072      normalize >fold_to_concat in ⊢ (???%); <map_append @All_append
1073      [ 1: <map_proj_fold @Hind2 #id #res elim id #id' #Hlookup @(Hsim (an_identifier ? (p1 id')) res Hlookup)
1074      | 2: cases opt1 in Hsim;
1075           [ 1: normalize nodelta #Hsim
1076                <map_proj_fold @Hind1 #id #res elim id #id' #Hlookup @(Hsim (an_identifier ? (p0 id')) res Hlookup)
1077           | 2: #bl #Hsim lapply (Hsim (an_identifier ? one) bl ?) normalize try @refl
1078                #Habsurd destruct (Habsurd)
1079           ]
1080      ]
1081| 3: #opt1 #left1 #right1 #opt2 #left2 #right2 #Hind1 #Hind2 #Hsim
1082     normalize >fold_to_concat >fold_to_concat in ⊢ (???%);
1083     <map_append <map_append in ⊢ (???%); @All_append
1084     [ 1: cases opt2; normalize nodelta
1085          [ 1: <map_proj_fold <map_proj_fold in ⊢ (???%); <(map_proj_fold ?? p0)
1086               cut (environment_sim (an_id_map SymbolTag block right1) (an_id_map SymbolTag block right2))
1087               [ 1: * #id' #res #Hlookup
1088                    lapply (Hsim (an_identifier ? (p1 id')) res) normalize #H @H @Hlookup ]
1089               #Hsim' lapply (Hind2 Hsim') @All_mp
1090               #a #Hmem @mem_append_backwards %1 @Hmem
1091          | 2: #bl <map_proj_fold <map_proj_fold in ⊢ (???%); <(map_proj_fold ?? p0)
1092               cut (environment_sim (an_id_map SymbolTag block right1) (an_id_map SymbolTag block right2))
1093               [ 1: * #id' #res #Hlookup
1094                    lapply (Hsim (an_identifier ? (p1 id')) res) normalize #H @H @Hlookup ]
1095               #Hsim' lapply (Hind2 Hsim') @All_mp
1096               #a #Hmem @mem_append_backwards %1 @Hmem ]
1097     | 2: cut (environment_sim (an_id_map SymbolTag block left1) (an_id_map SymbolTag block left2))
1098          [ 1: * #id' #res #Hlookup
1099               lapply (Hsim (an_identifier ? (p0 id')) res) normalize #H @H @Hlookup ] #Hsim'
1100          lapply (Hind1 … Hsim')
1101          <map_proj_fold <map_proj_fold in ⊢ (? → (???%)); <(map_proj_fold ?? p0)
1102          cases opt1 in Hsim; normalize nodelta
1103          [ 1: #Hsim @All_mp #a #Hmem @mem_append_backwards %2
1104               cases opt2 normalize nodelta
1105               [ 1: @Hmem
1106               | 2: #bl >fold_to_concat <map_append @mem_append_backwards %1 @Hmem ]
1107          | 2: #bl #Hsim #Hmem >fold_to_concat in ⊢ (???%); <map_append @All_append
1108               [ 2: normalize @conj try @I
1109                    cases opt2 in Hsim;
1110                     [ 1: #Hsim lapply (Hsim (an_identifier ? one) bl (refl ??))
1111                          normalize in ⊢ (% → ?); #Habsurd destruct (Habsurd)
1112                     | 2: #bl2 #Hsim lapply (Hsim (an_identifier ? one) bl (refl ??))
1113                          normalize in ⊢ (% → ?); #Heq >Heq normalize nodelta
1114                          @mem_append_backwards %2 >fold_to_concat <map_append
1115                          @mem_append_backwards %2 normalize %1 @refl ]
1116               | 1: cases opt2 in Hsim;
1117                     [ 1: #Hsim lapply (Hsim (an_identifier ? one) bl (refl ??))
1118                          normalize in ⊢ (% → ?); #Habsurd destruct (Habsurd)
1119                     | 2: #bl2 #Hsim lapply (Hsim (an_identifier ? one) bl (refl ??))
1120                          normalize in ⊢ (% → ?); #Heq lapply (Hind1 Hsim')
1121                          @All_mp #a #Hmem >Heq normalize nodelta
1122                          @mem_append_backwards %2 >fold_to_concat <map_append
1123                          @mem_append_backwards %1 @Hmem ] ]
1124          ]
1125     ]
1126] qed.
1127
1128
1129(* equivalent environments yield "equal" sets of block (cf. frontend_misc.ma)  *)
1130lemma environment_eq_blocks_eq : ∀env1,env2.
1131  environment_eq env1 env2 →
1132  lset_eq ? (blocks_of_env env1) (blocks_of_env env2).
1133#env1 #env2 * #Hsim1 #Hsim2 @conj
1134@environment_sim_blocks_inclusion assumption
1135qed.
1136
1137(* [env_codomain env vars] is the image of vars through env seen as a function. *)
1138definition env_codomain : env → list (ident×type) → lset block ≝ λe,l.
1139  foldi … (λid,block,acc.
1140    if mem_assoc_env … id l then
1141      block :: acc
1142    else
1143      acc
1144  ) e [ ].
1145
1146(* --------------------------------------------------------------------------- *)
1147
1148(* Two equivalent memories yield a trivial memory extension. *)
1149lemma memory_eq_to_mem_ext : ∀m1,m2. memory_eq m1 m2 → sr_memext m1 m2 [ ].
1150* #contents1 #nextblock1 #Hpos * #contents2 #nextblock2 #Hpos' normalize *
1151#Hnextblock #Hcontents_eq destruct %
1152[ 1: #b #H @conj try % elim H try //
1153| 2: #b *
1154| 3: #b #_ normalize % // ]
1155qed.
1156
1157(* memory extensions form a preorder relation *)
1158
1159lemma memory_ext_transitive :
1160  ∀m1,m2,m3,writeable1,writeable2.
1161  sr_memext m1 m2 writeable1 →
1162  sr_memext m2 m3 writeable2 →
1163  sr_memext m1 m3 (writeable1 @ writeable2).
1164#m1 #m2 #m3 #writeable1 #writeable2 #H12 #H23 %
1165[ 1: #b #Hnonempty1
1166     lapply (me_nonempty … H12 b Hnonempty1) * #Hnonempty2 #Hblocks_eq
1167     lapply (me_nonempty … H23 b Hnonempty2) * #Hnonempty3 #Hblocks_eq' @conj
1168     try assumption >Hblocks_eq >Hblocks_eq' @refl
1169| 2: #b #Hmem lapply (mem_append_forward ???? Hmem) *
1170     [ 1: #Hmem12 lapply (me_writeable_valid … H12 b Hmem12) #Hnonempty2
1171          elim (me_nonempty … H23 … Hnonempty2) //
1172     | 2: #Hmem23 @(me_writeable_valid … H23 b Hmem23) ]
1173| 3: #b #Hvalid % #Hmem lapply (mem_append_forward ???? Hmem) *
1174     [ 1: #Hmem12
1175          lapply (me_not_writeable … H12 … Hvalid) * #H @H assumption
1176     | 2: #Hmem23 lapply (me_nonempty … H12 … Hvalid) * #Hvalid2
1177          lapply (me_not_writeable … H23 … Hvalid2) * #H #_ @H assumption
1178     ]
1179] qed.     
1180
1181lemma memory_ext_reflexive : ∀m. sr_memext m m [ ].
1182#m % /2/ #b * qed.
1183
1184(* --------------------------------------------------------------------------- *)
1185(* Lemmas relating memory extensions to [free] *)
1186
1187lemma beloadv_free_simulation :
1188  ∀m1,m2,writeable,block,ptr,res.
1189  ∀mem_hyp : sr_memext m1 m2 writeable.
1190  beloadv (free m1 block) ptr = Some ? res → beloadv (free m2 block) ptr = Some ? res.
1191* #contents1 #nextblock1 #nextpos1 * #contents2 #nextblock2 #nextpos2 #writeable
1192* (* #br *) #bid * * (* #pr *) #pid #poff #res #Hext
1193(*#Hme_nonempty #Hme_writeable #Hme_nonempty #Hvalid_conserv*)
1194#Hload
1195lapply (beloadv_free_beloadv … Hload) #Hload_before_free
1196lapply (beloadv_free_blocks_neq … Hload) #Hblocks_neq
1197@beloadv_free_beloadv2
1198[ 1: @Hblocks_neq ]
1199@(sr_memext_load_sim … Hext) assumption
1200qed.
1201
1202
1203(* Lifting the property of being valid after a free to memory extensions *)
1204lemma valid_pointer_free : ∀m1,m2,writeable. sr_memext m1 m2 writeable → ∀p,b. valid_pointer (free m1 b) p = true → valid_pointer (free m2 b) p = true.
1205#m1 #m2 #writeable #Hext #p #b #Hvalid
1206lapply (valid_free_to_valid … Hvalid) #Hvalid_before_free
1207lapply (me_valid_pointers … Hext … Hvalid_before_free)
1208lapply (valid_after_free … Hvalid) #Hneq
1209whd in match (free ??);
1210whd in match (update_block ????);
1211whd in match (valid_pointer ??) in ⊢ (% → %);
1212whd in match (low_bound ??) in ⊢ (% → %);
1213whd in match (high_bound ??) in ⊢ (% → %);
1214>(not_eq_block_rev … Hneq) normalize nodelta #H @H
1215qed.
1216
1217lemma nonempty_block_mismatch : ∀m,b,bl.
1218  nonempty_block (free m bl) b →
1219  nonempty_block m b ∧ b ≠ bl.
1220#m #b #bl #Hnonempty
1221@(eq_block_elim … b bl)
1222[ 1: #Heq >Heq in Hnonempty; * #_ normalize
1223     cases (block_region bl) normalize >eqZb_reflexive normalize *
1224| 2: #Hneq @conj try assumption elim Hnonempty #Hvalid #Hlh %
1225     [ 1: lapply Hvalid normalize //
1226     | 2: lapply Hlh normalize
1227          @(eqZb_elim … (block_id b) (block_id bl))
1228          [ 1,3: * normalize *
1229          | 2,4: #_ // ] ] ]
1230qed.
1231
1232lemma eqb_to_eq_block : ∀a,b : block. a == b = eq_block a b.
1233#a #b lapply (eqb_true ? a b) @(eq_block_elim … a b)
1234#H #I
1235try /2 by neq_block_eq_block_false, true_to_andb_true/
1236cases I #J #K @K @H
1237qed.
1238
1239(* We can free in both sides of a memory extension if we take care of removing
1240   the freed block from the set of writeable blocks. *)
1241lemma free_memory_ext :
1242  ∀m1,m2,writeable,bl.
1243   sr_memext m1 m2 writeable →
1244   sr_memext (free m1 bl) (free m2 bl) (lset_remove ? writeable bl).
1245#m1 #m2 #writeable #bl #Hext %
1246[ 1: #b #Hnonempty lapply (nonempty_block_mismatch … Hnonempty)
1247     * #Hnonempty' #Hblocks_neq lapply (me_nonempty … Hext … Hnonempty') *     
1248     #Hnonempty2 #Hcontents_eq @conj
1249     [ 1: % try @(wb_valid … Hnonempty2)
1250          whd in match (free ??);
1251          whd in match (update_block ?????);
1252          >(neq_block_eq_block_false … Hblocks_neq) normalize
1253          try @(wb_nonempty … Hnonempty2)
1254     | 2: whd in match (free ??) in ⊢ (??%%);
1255          whd in match (update_block ?????) in ⊢ (??%%);
1256          >(neq_block_eq_block_false … Hblocks_neq)
1257          normalize nodelta assumption ]         
1258| 2: #b #Hmem
1259     cut (mem block b writeable ∧ b ≠ bl)
1260     [ elim writeable in Hmem;
1261       [ 1: normalize *
1262       | 2: #hd #tl #Hind whd in match (filter ???);
1263            >eqb_to_eq_block
1264            @(eq_block_elim … hd bl) normalize in match (notb ?); normalize nodelta
1265            [ 1: #Heq #H whd in match (meml ???); elim (Hind H) #H0 #H1 @conj
1266                 [ 1: %2 ] assumption
1267            | 2: #Hneq whd in match (meml ???) in ⊢ (% → %); *
1268                 [ 1: #H destruct /3 by conj, or_introl, refl/
1269                 | 2: #H elim (Hind H) #H1 #H2 /3 by conj, or_intror, refl/ ] ] ]
1270     ] * #Hmem2 #Hblocks_neq
1271    lapply (me_writeable_valid … Hext … Hmem2) * #Hvalid #Hnonempty %
1272    try assumption whd in match (free ??); whd in match (update_block ?????);
1273    >(neq_block_eq_block_false … Hblocks_neq) @Hnonempty
1274| 3: #p #Hvalid lapply (nonempty_block_mismatch … Hvalid) * #Hnonempty #Hblocks_neq
1275     % #Hmem lapply (me_not_writeable … Hext … Hnonempty) * #H @H
1276     elim writeable in Hmem;
1277     [ 1: *
1278     | 2: #hd #tl #Hind whd in match (filter ???) in ⊢ (% → ?); >eqb_to_eq_block
1279          @(eq_block_elim … hd bl) normalize in match (notb ?); normalize nodelta
1280          [ 1: #Heq #H normalize %2 @(Hind H)
1281          | 2: #Hblocks_neq whd in match (meml ???); *
1282               [ 1: #Hneq normalize %1 assumption
1283               | 2: #Hmem normalize %2 @(Hind Hmem) ]
1284          ]
1285     ]
1286] qed.     
1287
1288
1289(* Freeing from an extension block is ok. *)
1290lemma memext_free_extended_conservation :
1291  ∀m1,m2 : mem.
1292  ∀writeable.
1293  ∀mem_hyp : sr_memext m1 m2 writeable.
1294  ∀b. meml ? b writeable →
1295  sr_memext m1 (free m2 b) (lset_remove … writeable b).
1296#m1 #m2 #writeable #Hext #b #Hb_writeable %
1297[ 1: #bl #Hnonempty lapply (me_not_writeable … Hext … Hnonempty) #Hnot_mem
1298     lapply (mem_not_mem_neq … Hb_writeable Hnot_mem) #Hblocks_neq
1299     @conj
1300     [ 2: whd in match (free ??); whd in match (update_block ?????);
1301          >(neq_block_eq_block_false … (sym_neq … Hblocks_neq)) normalize
1302          elim (me_nonempty … Hext … Hnonempty) //
1303     | 1: % elim (me_nonempty … Hext … Hnonempty) * try //
1304          #Hvalid2 #Hlh #Hcontents_eq whd in match (free ??);
1305          whd in match (update_block ?????);
1306          >(neq_block_eq_block_false … (sym_neq … Hblocks_neq)) normalize assumption
1307    ]
1308| 2: #b' #Hmem (* contradiction in first premise *)
1309     cut (mem block b' writeable ∧ b' ≠ b)
1310     [ elim writeable in Hmem;
1311       [ 1: normalize @False_ind
1312       | 2: #hd #tl #Hind whd in match (filter ???); >eqb_to_eq_block
1313            @(eq_block_elim … hd b) normalize in match (notb ?); normalize nodelta
1314            [ 1: #Heq #H whd in match (meml ???); destruct
1315                 elim (Hind H) #Hmem #Hneq @conj try assumption %2 assumption
1316            | 2: #Hneq whd in match (meml ???) in ⊢ (% → %); *
1317                 [ 1: #H @conj [ 1: %1 @H | 2: destruct @Hneq ]
1318                 | 2: #H elim (Hind H) #Hmem #Hneq' @conj try assumption %2 assumption ]
1319     ] ] ]
1320     * #Hb' #Hneq lapply (me_writeable_valid … Hext … Hb') #Hvalid %
1321     [ 1: @(wb_valid … Hvalid)
1322     | 2: whd in match (free ??);
1323          whd in match (update_block ?????);
1324          >(neq_block_eq_block_false … Hneq)
1325          @(wb_nonempty … Hvalid) ]
1326| 3: #b' #Hnonempty % #Hmem
1327     cut (mem block b' writeable ∧ b' ≠ b)
1328     [ elim writeable in Hmem;
1329       [ 1: normalize @False_ind
1330       | 2: #hd #tl #Hind whd in match (filter ???); >eqb_to_eq_block
1331            @(eq_block_elim … hd b) normalize in match (notb ?); normalize nodelta
1332            [ 1: #Heq #H whd in match (meml ???); destruct
1333                 elim (Hind H) #Hmem #Hneq @conj try assumption %2 assumption
1334            | 2: #Hneq whd in match (meml ???) in ⊢ (% → %); *
1335                 [ 1: #H @conj [ 1: %1 @H | 2: destruct @Hneq ]
1336                 | 2: #H elim (Hind H) #Hmem #Hneq' @conj try assumption %2 assumption ]
1337     ] ] ] * #Hmem' #Hblocks_neq
1338     lapply (me_not_writeable … Hext … Hnonempty) * #H @H assumption
1339] qed.
1340 
1341 
1342lemma disjoint_extension_nil_eq_set :
1343  ∀env1,env2.
1344   disjoint_extension env1 env2 [ ] →
1345   lset_eq ? (blocks_of_env env1) (blocks_of_env env2).
1346#env1 #env whd in ⊢ (% → ?); * * #_ #_ #H normalize in H;
1347@environment_eq_blocks_eq whd @conj
1348#id #res >(H id) //
1349qed.
1350
1351lemma free_list_equivalent_sets :
1352  ∀m,set1,set2.
1353  lset_eq ? set1 set2 →
1354  memory_eq (free_list m set1) (free_list m set2).
1355#m #set1 #set2 #Heq whd in match (free_list ??) in ⊢ (?%%);
1356@(lset_eq_fold block_DeqSet mem memory_eq  … Heq)
1357[ 1: @reflexive_memory_eq
1358| 2: @transitive_memory_eq
1359| 3: @symmetric_memory_eq
1360| 4: #x #acc1 #acc2
1361     whd in match (free ??) in ⊢ (? → (?%%));
1362     whd in match (memory_eq ??) in ⊢ (% → %); *
1363     #Hnextblock_eq #Hcontents_eq @conj try assumption
1364     #b normalize >Hcontents_eq @refl
1365| 5: #x1 #x2 #acc normalize @conj try @refl
1366     * (* * *) #id normalize nodelta cases (block_region x1)
1367     cases (block_region x2) normalize nodelta
1368     @(eqZb_elim id (block_id x1)) #Hx1 normalize nodelta
1369     @(eqZb_elim id (block_id x2)) #Hx2 normalize nodelta try @refl
1370| 6: * (* #r *) #i * #contents #nextblock #Hpos @conj
1371     [ 1: @refl
1372     | 2: #b normalize (* cases (block_region b) normalize
1373          cases r normalize *) cases (eqZb (block_id b) i)
1374          normalize @refl
1375     ]
1376] qed.
1377
1378lemma foldr_identity : ∀A:Type[0]. ∀l:list A. foldr A ? (λx:A.λl0.x::l0) [] l = l.
1379#A #l elim l //
1380#hd #tl #Hind whd in match (foldr ?????); >Hind @refl
1381qed.
1382
1383lemma mem_not_mem_diff_aux :
1384  ∀s1,s2,s3,hd.
1385     mem ? hd s1 →
1386     ¬(mem ? hd s2) →
1387     mem block hd (lset_difference ? s1 (s2@(filter block_DeqSet (λx:block_DeqSet.¬x==hd) s3))).
1388#s1 #s2 #s3 #hd #Hmem #Hnotmem lapply Hmem lapply s1 -s1
1389elim s3
1390[ 1: #s1 >append_nil elim s1 try //
1391     #hd' #tl' #Hind *
1392     [ 1: #Heq >lset_difference_unfold
1393          @(match hd'∈s2 return λx. (hd'∈s2 = x) → ? with
1394            [ true ⇒ λH. ?
1395            | false ⇒ λH. ?
1396            ] (refl ? (hd'∈s2))) normalize nodelta
1397          [ 1: lapply (memb_to_mem … H) #Hmem elim Hnotmem #H' destruct
1398               @(False_ind … (H' Hmem))
1399          | 2: whd %1 assumption ]
1400     | 2: #Hmem >lset_difference_unfold
1401          @(match hd'∈s2 return λx. (hd'∈s2 = x) → ? with
1402            [ true ⇒ λH. ?
1403            | false ⇒ λH. ?
1404            ] (refl ? (hd'∈s2))) normalize nodelta
1405          [ 1:  @Hind @Hmem
1406          | 2: %2 @Hind @Hmem ] ]
1407| 2: #hd' #tl' #H #s1 #Hmem >filter_cons_unfold >eqb_to_eq_block
1408    @(eq_block_elim … hd' hd)
1409    [ 1:  >notb_true normalize nodelta #Heq @H @Hmem
1410    | 2: #Hneq >notb_false normalize nodelta
1411          >lset_difference_permute >(cons_to_append … hd')
1412          >associative_append >lset_difference_unfold2 >nil_append
1413          >lset_difference_permute @H
1414          elim s1 in Hmem; try //
1415          #hd'' #tl'' #Hind *
1416          [ 1: #Heq destruct whd in match (lset_remove ???);
1417               >eqb_to_eq_block >(neq_block_eq_block_false … (sym_neq … Hneq))
1418               >notb_false normalize nodelta %1 @refl
1419          | 2: #Hmem whd in match (lset_remove ???);
1420                cases (¬(hd''==hd')) normalize nodelta
1421                [ 1: %2 @Hind @Hmem
1422                | 2: @Hind @Hmem ] ] ]
1423] qed.
1424
1425(* freeing equivalent sets of blocks on memory extensions yields memory extensions *)
1426lemma free_equivalent_sets :
1427  ∀m1,m2,writeable,set1,set2.
1428  lset_eq ? set1 set2 →
1429  sr_memext m1 m2 writeable →
1430  sr_memext (free_list m1 set1) (free_list m2 set2) (lset_difference ? writeable set1).
1431#m1 #m2 #writeable #set1 #set2 #Heq #Hext
1432lapply (free_list_equivalent_sets m2 … (symmetric_lset_eq … Heq))
1433#Heq
1434lapply (memory_eq_to_mem_ext … (symmetric_memory_eq … Heq)) #Hext'
1435lapply (memory_ext_transitive (free_list m1 set1) (free_list m2 set1) (free_list m2 set2) (filter block_eq (λwb:block_eq.¬wb∈set1) writeable) [ ] ? Hext')
1436[ 2: >append_nil #H @H ]
1437elim set1
1438[ 1: whd in match (free_list ??); whd in match (free_list ??);
1439     normalize >foldr_identity @Hext
1440| 2: #hd #tl #Hind >free_list_cons >free_list_cons
1441     lapply (free_memory_ext … hd … Hind)
1442     cut ((lset_remove block_eq (filter block_eq (λwb:block_eq.¬wb∈tl) writeable) hd) =
1443          (filter block_eq (λwb:block_eq.¬wb∈hd::tl) writeable))
1444     [ whd in match (lset_remove ???); elim writeable //
1445        #hd' #tl' #Hind_cut >filter_cons_unfold >filter_cons_unfold
1446        whd in match (memb ???) in ⊢ (???%); >eqb_to_eq_block
1447        (* elim (eqb_true block_DeqSet hd' hd)*)
1448        @(eq_block_elim … hd' hd) normalize nodelta
1449        [ 1: #Heq
1450             cases (¬hd'∈tl) normalize nodelta
1451             [ 1: whd in match (foldr ?????); >Heq >eqb_to_eq_block >eq_block_b_b normalize in match (notb ?);
1452                  normalize nodelta
1453                  lapply Hind_cut destruct #H @H
1454             | 2: lapply Hind_cut destruct #H @H ]
1455        | 2: #Hneq cases (¬hd'∈tl) normalize nodelta try assumption
1456             whd in match (foldr ?????); >eqb_to_eq_block
1457             >(neq_block_eq_block_false … Hneq)
1458             normalize in match (notb ?); normalize nodelta >Hind_cut @refl
1459        ]
1460    ]
1461    #Heq >Heq #H @H
1462] qed.
1463
1464(* Remove a writeable block. *)
1465lemma memory_ext_weaken :
1466  ∀m1,m2,hd,writeable.
1467    sr_memext m1 m2 (hd :: writeable) →
1468    sr_memext m1 m2 writeable.
1469#m1 #m2 #hd #writeable *
1470#Hnonempty #Hwriteable_valid #Hnot_writeable %
1471try assumption
1472[ 1: #b #Hmem @Hwriteable_valid whd %2 assumption
1473| 2: #b #Hnonempty % #Hmem elim (Hnot_writeable … Hnonempty) #H @H whd %2 @Hmem
1474] qed.
1475
1476(* Perform a "rewrite" using lset_eq on the writeable blocks *)
1477lemma memory_ext_writeable_eq :
1478  ∀m1,m2,wr1,wr2.
1479    sr_memext m1 m2 wr1 →
1480    lset_eq ? wr1 wr2 →
1481    sr_memext m1 m2 wr2.
1482#m1 #m2 #wr1 #wr2 #Hext #Hset_eq %
1483[ 1: @(me_nonempty … Hext)
1484| 2:  #b #Hmem lapply (lset_eq_mem … (symmetric_lset_eq … Hset_eq) … Hmem)
1485      @(me_writeable_valid … Hext)
1486| 3: #b #Hnonempty % #Hmem
1487     lapply (lset_eq_mem … (symmetric_lset_eq … Hset_eq) … Hmem) #Hmem'
1488     lapply (me_not_writeable … Hext … Hnonempty) * #H @H assumption
1489] qed.     
1490
1491
1492         
1493lemma memory_eq_to_memory_ext_pre :
1494  ∀m1,m1',m2,writeable.
1495  memory_eq m1 m1' →
1496  sr_memext m1' m2 writeable →
1497  sr_memext m1 m2 writeable.
1498#m1 #m1' #m2 #writeable #Heq #Hext
1499lapply (memory_eq_to_mem_ext … Heq) #Hext'
1500@(memory_ext_transitive … Hext' Hext)
1501qed.
1502
1503lemma memory_eq_to_memory_ext_post :
1504  ∀m1,m2,m2',writeable.
1505  memory_eq m2' m2 →
1506  sr_memext m1 m2' writeable →
1507  sr_memext m1 m2 writeable.
1508#m1 #m2 #m2' #writeable #Heq #Hext
1509lapply (memory_eq_to_mem_ext … Heq) #Hext'
1510lapply (memory_ext_transitive … Hext Hext') >append_nil #H @H
1511qed.
1512
1513
1514lemma memext_free_extended_environment :
1515  ∀m1,m2,writeable.
1516   sr_memext m1 m2 writeable →
1517   ∀env,env_ext,vars.
1518    disjoint_extension env env_ext vars →
1519    lset_inclusion ? (lset_difference ? (blocks_of_env env_ext) (blocks_of_env env)) writeable →
1520    sr_memext
1521      (free_list m1 (blocks_of_env env))
1522      (free_list m2 (blocks_of_env env_ext))
1523      (lset_difference ? writeable (blocks_of_env env_ext)).
1524#m1 #m2 #writeable #Hext #env #env_ext #vars #Hdisjoint #Hext_in_writeable
1525(* Disjoint extension induces environment "inclusion", i.e. simulation *)
1526lapply (disjoint_extension_to_inclusion … Hdisjoint) #Hincl
1527(* Environment inclusion entails set inclusion on the mapped blocks *)
1528lapply (environment_sim_blocks_inclusion … Hincl) #Hblocks_incl
1529(* This set inclusion can be decomposed on a common part and an extended part. *)
1530lapply (lset_inclusion_difference ??? Hblocks_incl)
1531* #extended_part * * #Hset_eq #Henv_disjoint_ext #Hextended_eq
1532lapply (lset_difference_lset_eq … writeable … Hset_eq) #HeqA
1533cut (lset_inclusion ? extended_part writeable)
1534[ 1: cases Hextended_eq #HinclA #_ @(transitive_lset_inclusion … HinclA … Hext_in_writeable) ]
1535-Hext_in_writeable #Hext_in_writeable
1536@(memory_ext_writeable_eq ????? (symmetric_lset_eq … HeqA))
1537lapply (free_list_equivalent_sets m2 ?? Hset_eq) #Hmem_eq
1538@(memory_eq_to_memory_ext_post … (symmetric_memory_eq … Hmem_eq))
1539(* Add extended ⊆ (lset_difference block_eq writeable (blocks_of_env env @ tl)) in Hind *)
1540cut (∀x. mem ? x extended_part → ¬ (mem ? x (blocks_of_env env)))
1541[ 1: cases Hextended_eq #Hincl_ext #_ @(lset_not_mem_difference … Hincl_ext) ]
1542lapply (proj2 … Hset_eq) lapply Hext_in_writeable
1543@(WF_rect ????? (filtered_list_wf block_DeqSet extended_part))
1544*
1545[ 1: #_ #_ #_ #_ #_ >append_nil
1546     @(free_equivalent_sets ???? (blocks_of_env env) (reflexive_lset_eq ??) Hext)
1547| 2: #hd #tl #Hwf_step #Hind_wf #Hext_in_writeable #Hset_incl #Hin_ext_not_in_env
1548     cut (lset_eq ? (blocks_of_env env@hd::tl) (hd::(blocks_of_env env@tl)))
1549     [ 1: >cons_to_append >cons_to_append in ⊢ (???%);
1550          @lset_eq_concrete_to_lset_eq // ]
1551     #Hpermute
1552     lapply (free_list_equivalent_sets m2 ?? Hpermute) #Hmem_eq2
1553     @(memory_eq_to_memory_ext_post … (symmetric_memory_eq … Hmem_eq2))
1554     >free_list_cons
1555     lapply (lset_difference_lset_eq … writeable … Hpermute) #HeqB
1556     @(memory_ext_writeable_eq ????? (symmetric_lset_eq … HeqB))
1557     >lset_difference_unfold2
1558     lapply (lset_difference_lset_remove_commute ? hd writeable (blocks_of_env env@tl))
1559     #Heq_commute >Heq_commute
1560     (* lapply (memory_ext_writeable_eq ????? (symmetric_lset_eq … Heq_commute)) *)
1561     lapply (Hind_wf (filter … (λx.¬(x==hd)) tl) ????)
1562     [ 2: @(transitive_lset_inclusion … Hset_incl)
1563          @lset_inclusion_concat_monotonic
1564          @cons_preserves_inclusion
1565          @lset_inclusion_filter_self
1566     | 3: @(transitive_lset_inclusion … Hext_in_writeable)
1567          @cons_preserves_inclusion
1568          @lset_inclusion_filter_self
1569     | 4: whd whd in ⊢ (???%);
1570          lapply (eqb_true ? hd hd) * #_ #H >(H (refl ??)) normalize in match (notb ?);
1571          normalize nodelta @refl
1572     | 1: #x #H @Hin_ext_not_in_env %2 elim tl in H; //
1573          #hd' #tl' #Hind >filter_cons_unfold >eqb_to_eq_block @(eq_block_elim … hd' hd)
1574          >notb_true >notb_false normalize nodelta
1575          [ 1: #Heq >Heq #H %2 @Hind @H
1576          | 2: #Hneq *
1577               [ 1: #Heq destruct %1 @refl
1578               | 2: #H %2 @Hind @H ] ]
1579     ]
1580     #Hext_ind
1581     lapply (Hin_ext_not_in_env … hd (or_introl … (refl ??)))
1582     #Hnot_in_env     
1583     lapply (memext_free_extended_conservation … Hext_ind hd ?)
1584     [ 1: @mem_not_mem_diff_aux try assumption elim Hext_in_writeable #H #_ @H ]
1585     -Hext_ind #Hext_ind
1586     cut (memory_eq (free (free_list m2 (blocks_of_env env@filter block_DeqSet (λx:block_DeqSet.¬x==hd) tl)) hd)
1587                    (free (free_list m2 (blocks_of_env env@tl)) hd))
1588     [ 1: <free_list_cons <free_list_cons
1589          @free_list_equivalent_sets @lset_eq_concrete_to_lset_eq
1590          >cons_to_append >cons_to_append in ⊢ (???%);
1591          @(transitive_lset_eq_concrete … (switch_lset_eq_concrete ????))
1592          @symmetric_lset_eq_concrete
1593          @(transitive_lset_eq_concrete ????? (switch_lset_eq_concrete ????))
1594          @lset_eq_to_lset_eq_concrete
1595          elim (blocks_of_env env)
1596          [ 1: @symmetric_lset_eq @lset_eq_filter
1597          | 2: #hd0 #tl0 #Hind >cons_to_append
1598               >associative_append in ⊢ (??%%);
1599               >associative_append in ⊢ (??%%);
1600               @cons_monotonic_eq @Hind ] ]
1601      #Hmem_eq3 @(memory_eq_to_memory_ext_post … Hmem_eq3)
1602      @(memory_ext_writeable_eq … Hext_ind)
1603      <lset_difference_lset_remove_commute <lset_difference_lset_remove_commute     
1604      <lset_difference_unfold2 <lset_difference_unfold2
1605      @lset_difference_lset_eq
1606      (* Note: exactly identical to the proof in the cut. *)
1607      @lset_eq_concrete_to_lset_eq
1608      >cons_to_append >cons_to_append in ⊢ (???%);
1609      @(transitive_lset_eq_concrete … (switch_lset_eq_concrete ????))
1610      @symmetric_lset_eq_concrete
1611      @(transitive_lset_eq_concrete ????? (switch_lset_eq_concrete ????))
1612      @lset_eq_to_lset_eq_concrete
1613      elim (blocks_of_env env)
1614      [ 1: @symmetric_lset_eq @lset_eq_filter
1615      | 2: #hd0 #tl0 #Hind >cons_to_append
1616           >associative_append in ⊢ (??%%);
1617           >associative_append in ⊢ (??%%);
1618           @cons_monotonic_eq @Hind ]
1619] qed.
1620
1621(* --------------------------------------------------------------------------- *)
1622(* Some lemmas allowing to reason on writes to extended memories. *)
1623
1624(* Writing in the extended part of the memory preserves the extension (that's the point) *)
1625lemma bestorev_writeable_memory_ext :
1626  ∀m1,m2,writeable.
1627  ∀Hext:sr_memext m1 m2 writeable.
1628  ∀wb,wo,v. meml ? wb writeable →
1629  ∀m2'.bestorev m2 (mk_pointer wb wo) v = Some ? m2' →
1630  sr_memext m1 m2' writeable.
1631#m1 * #contents2 #nextblock2 #Hpos2 #writeable #Hext #wb #wo #v #Hmem #m2'
1632whd in ⊢ ((??%?) → ?);
1633lapply (me_writeable_valid … Hext ? Hmem) * whd in ⊢ (% → ?); #Hlt
1634>(Zlt_to_Zltb_true … Hlt) normalize nodelta #Hnonempty2 #H
1635lapply (if_opt_inversion ???? H) -H * #Hzltb
1636lapply (andb_inversion … Hzltb) * #Hleb #Hltb -Hzltb
1637#Heq destruct %
1638[ 1: #b #Hnonempty1
1639     lapply (me_nonempty … Hext b Hnonempty1) * * #Hvalid_b #Hnonempty_b
1640     #Heq @conj
1641     [ 1: % whd whd in Hvalid_b; try @Hvalid_b
1642          normalize cases (block_region b) normalize nodelta
1643          cases (block_region wb) normalize nodelta try //
1644          @(eqZb_elim … (block_id b) (block_id wb)) normalize nodelta
1645          try //
1646     | 2: whd in ⊢ (??%%);
1647          @(eq_block_elim … b wb) normalize nodelta // #Heq_b_wb
1648          lapply (me_not_writeable … Hext b Hnonempty1) destruct (Heq_b_wb)
1649          * #H @(False_ind … (H Hmem)) ]
1650| 2: #b #Hmem_writeable
1651     lapply (me_writeable_valid … Hext … Hmem_writeable) #H %
1652     [ 1: normalize cases H //
1653     | 2: cases H normalize #Hb_lt #Hb_nonempty2
1654          (*
1655            cases (block_region b)
1656            cases (block_region wb) *)
1657          @(eqZb_elim … (block_id b) (block_id wb)) normalize nodelta
1658          // ]
1659| 3: #b #Hnonempty
1660     lapply (me_not_writeable … Hext … Hnonempty) //
1661] qed.
1662
1663(* If we manage to write in a block, then it is nonempty *)
1664lemma bestorev_success_nonempty :
1665  ∀m,wb,wo,v,m'.
1666  bestorev m (mk_pointer wb wo) v = Some ? m' →
1667  nonempty_block m wb.
1668#m #wb #wo #v #m' normalize #Hstore
1669cases (if_opt_inversion ???? Hstore) -Hstore #block_valid1 #H
1670cases (if_opt_inversion ???? H) -H #nonempty #H %
1671[ 1: whd @Zltb_true_to_Zlt assumption
1672| 2: generalize in match (Z_of_unsigned_bitvector 16 (offv wo)) in nonempty; #z #H'
1673     cut (Zleb (low (blocks m wb)) z = true)
1674     [ 1: lapply H' cases (Zleb (low (blocks m wb)) z) // normalize #H @H ]
1675     #Hleb >Hleb in H'; normalize nodelta #Hlt
1676     lapply (Zleb_true_to_Zle … Hleb) lapply (Zltb_true_to_Zlt … Hlt)
1677     /2 by Zle_to_Zlt_to_Zlt/
1678] qed.
1679
1680(* If we manage to write in a block, it is still non-empty after the write *)
1681lemma bestorev_success_nonempty2 :
1682  ∀m,wb,wo,v,m'.
1683  bestorev m (mk_pointer wb wo) v = Some ? m' →
1684  nonempty_block m' wb.
1685#m #wb #wo #v #m' normalize #Hstore
1686cases (if_opt_inversion ???? Hstore) -Hstore #block_valid1 #H
1687cases (if_opt_inversion ???? H) -H #nonempty #H %
1688[ 1: whd destruct @Zltb_true_to_Zlt assumption
1689| 2: generalize in match (Z_of_unsigned_bitvector 16 (offv wo)) in nonempty; #z #H'
1690     cut (Zleb (low (blocks m wb)) z = true)
1691     [ 1: lapply H' cases (Zleb (low (blocks m wb)) z) // normalize #H @H ]
1692     #Hleb >Hleb in H'; normalize nodelta #Hlt
1693     lapply (Zleb_true_to_Zle … Hleb) lapply (Zltb_true_to_Zlt … Hlt)
1694     destruct cases (block_region wb) normalize >eqZb_z_z normalize
1695     /2 by Zle_to_Zlt_to_Zlt/
1696] qed.
1697
1698(* A nonempty block stays nonempty after a write. *)
1699lemma nonempty_block_update_ok :
1700  ∀m,b,wb,offset,v.
1701  nonempty_block m b →
1702  nonempty_block
1703    (mk_mem
1704      (update_block ? wb
1705        (mk_block_contents (low (blocks m wb)) (high (blocks m wb))
1706          (update beval offset v (contents (blocks m wb)))) (blocks m))
1707            (nextblock m) (nextblock_pos m)) b.
1708#m #b #wb #offset #v * #Hvalid #Hnonempty % //
1709cases b in Hvalid Hnonempty; (* #br *) #bid cases wb (* #wbr *) #wbid normalize
1710(* cases br *) normalize nodelta (* cases wbr normalize nodelta // *)
1711@(eqZb_elim … bid wbid) // #Heq #Hlt normalize //
1712qed.
1713
1714lemma nonempty_block_update_ok2 :
1715  ∀m,b,wb,offset,v.
1716  nonempty_block
1717    (mk_mem
1718      (update_block ? wb
1719        (mk_block_contents (low (blocks m wb)) (high (blocks m wb))
1720          (update beval offset v (contents (blocks m wb)))) (blocks m))
1721            (nextblock m) (nextblock_pos m)) b →
1722  nonempty_block m b.
1723#m #b #wb #offset #v * #Hvalid #Hnonempty % //
1724cases b in Hvalid Hnonempty; (* #br *) #bid cases wb (* #wbr *) #wbid normalize
1725(* cases br normalize nodelta cases wbr normalize nodelta // *)
1726@(eqZb_elim … bid wbid) // #Heq #Hlt normalize //
1727qed.
1728
1729(* Writing in the non-extended part of the memory preserves the extension as long
1730 * as it's done identically in both memories. *)
1731lemma bestorev_not_writeable_memory_ext :
1732  ∀m1,m2,writeable.
1733  ∀Hext:sr_memext m1 m2 writeable.
1734  ∀wb,wo,v.
1735  ∀m1'. bestorev m1 (mk_pointer wb wo) v = Some ? m1' → 
1736  ∃m2'. bestorev m2 (mk_pointer wb wo) v = Some ? m2' ∧
1737        sr_memext m1' m2' writeable.
1738#m1 #m2 #writeable #Hext #wb #wo #v #m1' #Hstore1
1739lapply (bestorev_success_nonempty … Hstore1) #Hwb_nonempty
1740cases (me_nonempty … Hext … Hwb_nonempty) #Hwb_nonempty2 #Hblocks_eq
1741cut (∃m2'. bestorev m2 (mk_pointer wb wo) v=Some mem m2')
1742[ cases Hwb_nonempty2 #Hwb_valid #Hnonempty normalize
1743  normalize in Hwb_valid; >(Zlt_to_Zltb_true … Hwb_valid) normalize nodelta
1744  whd in Hstore1:(??%%); normalize
1745  cases (if_opt_inversion ???? Hstore1) -Hstore1 #block_valid1 #H
1746  cases (if_opt_inversion ???? H) #Hin_bounds1 #Hm1' -H
1747  cases (andb_inversion … Hin_bounds1) #Hleb1 #Hltb1 -Hin_bounds1
1748  >Hblocks_eq in Hleb1 Hltb1 ⊢ %; #Hleb1 #Hltb1 >Hleb1 >Hltb1
1749  normalize nodelta /2 by ex_intro/ ]
1750* #m2' #Hstore2 %{m2'} @conj try assumption
1751whd in Hstore1:(??%%);
1752whd in Hstore2:(??%%);
1753cases (if_opt_inversion ???? Hstore1) -Hstore1 #block_valid1 #H
1754cases (if_opt_inversion ???? H) #Hin_bounds1 #Hm1' -H
1755cases (if_opt_inversion ???? Hstore2) -Hstore2 #block_valid2 #H
1756cases (if_opt_inversion ???? H) #Hin_bounds2 #Hm2' -H
1757cases (andb_inversion … Hin_bounds1) #Hleb1 #Hltb1 -Hin_bounds1
1758cases (andb_inversion … Hin_bounds2) #Hleb2 #Hltb2 -Hin_bounds2
1759cut (valid_pointer m1 (mk_pointer wb wo))
1760[ 1: normalize >block_valid1 normalize nodelta >Hleb1 normalize nodelta
1761     >Hltb1 @I ]
1762#Hvalid
1763lapply (me_not_writeable_ptr … Hext … Hvalid) #Hnot_in_writeable
1764destruct %
1765[ 1: #b #Hnonempty lapply (me_nonempty … Hext … (nonempty_block_update_ok2 … Hnonempty)) * #HA #HB
1766     @conj
1767     [ 1: @nonempty_block_update_ok //
1768     | 2: normalize (* cases b in HB; #br #bid cases wb #wbr #wbid
1769          cases br cases wbr normalize nodelta *)
1770          @(eqZb_elim … (block_id b) (block_id wb)) normalize nodelta //
1771          #Hid_eq cut (b = wb)
1772          [ cases b in Hid_eq; cases wb #wid #bid #H >H @refl ]
1773          #Hblock_eq destruct (Hblock_eq) >HB @refl ]
1774| 2: #b #Hmem lapply (me_writeable_valid … Hext … Hmem) @nonempty_block_update_ok
1775| 3: #b #Hnonempty lapply (nonempty_block_update_ok2 … Hnonempty)
1776     @(me_not_writeable … Hext)
1777] qed.
1778
1779(* If we successfuly store something in the first memory, then we can store it in the
1780 * second one and the memory extension is preserved. *)
1781lemma memext_store_value_of_type :
1782       ∀m, m_ext, m', writeable, ty, loc, off, v.
1783       sr_memext m m_ext writeable →
1784       store_value_of_type ty m loc off v = Some ? m' →
1785       ∃m_ext'. store_value_of_type ty m_ext loc off v = Some ? m_ext' ∧
1786                sr_memext m' m_ext' writeable.
1787#m #m_ext #m' #writeable #ty #loc #off #v #Hext
1788(* case analysis on access mode of [ty] *)
1789cases ty
1790[ | #sz #sg | #ptr_ty | #array_ty #array_sz | #domain #codomain
1791| #structname #fieldspec | #unionname #fieldspec | #id ]
1792whd in ⊢ ((??%?) → (?%?));
1793[ 1,4,5,6,7: #Habsurd destruct ]
1794whd in ⊢ (? → (??(λ_.?(??%?)?)));
1795lapply loc lapply off lapply Hext lapply m_ext lapply m lapply m' -loc -off -Hext -m_ext -m -m'
1796elim (fe_to_be_values ??)
1797[ 1,3,5: #m' #m #m_ext #Hext #off #loc normalize in ⊢ (% → ?); #Heq destruct (Heq) %{m_ext} @conj normalize //
1798| 2,4,6: #hd #tl #Hind #m' #m #m_ext #Hext #off #loc whd in ⊢ ((??%?) → ?); #H
1799         cases (some_inversion ????? H) #m'' * #Hstore_eq #Hstoren_eq
1800         lapply (bestorev_not_writeable_memory_ext … Hext … Hstore_eq)
1801         * #m_ext'' * #Hstore_eq2 #Hext2
1802         lapply (Hind … Hext2 … Hstoren_eq) -Hind * #m_ext' *
1803         #Hstoren' #Hext3
1804         %{m_ext'} @conj try assumption
1805         whd in ⊢ (??%%); >Hstore_eq2 normalize nodelta
1806         @Hstoren'
1807] qed.
1808
1809lemma memext_store_value_of_type' :
1810       ∀m, m_ext, m', writeable, ty, ptr, v.
1811       sr_memext m m_ext writeable →
1812       store_value_of_type' ty m ptr v = Some ? m' →
1813       ∃m_ext'. store_value_of_type' ty m_ext ptr v = Some ? m_ext' ∧
1814                sr_memext m' m_ext' writeable.
1815#m #m_ext #m' #writeable #ty #p #v #Hext cases p #b #o
1816@memext_store_value_of_type @Hext qed.
1817
1818lemma memext_store_value_of_type_writeable :
1819  ∀m1,m2,writeable.
1820  ∀Hext:sr_memext m1 m2 writeable.
1821  ∀wb. meml ? wb writeable →
1822  ∀ty,off,v,m2'. store_value_of_type ty m2 wb off v = Some ? m2' →
1823  sr_memext m1 m2' writeable.
1824#m1 #m2 #writeable #Hext #wb #Hmem
1825#ty #off #v #m2'
1826cases ty
1827[ | #sz #sg | #ptr_ty | #array_ty #array_sz | #domain #codomain
1828| #structname #fieldspec | #unionname #fieldspec | #id ]
1829whd in ⊢ ((??%?) → ?);
1830[ 1,4,5,6,7: #Habsurd destruct ]
1831lapply Hext lapply m1 lapply m2 lapply m2' lapply off -Hext -m1 -m2 -m2' -off -ty
1832elim (fe_to_be_values ??)
1833[ 1,3,5: #o #m2' #m2 #m1 #Hext normalize #Heq destruct assumption
1834| *: #hd #tl #Hind #o #m2_end #m2 #m1 #Hext whd in match (storen ???); #Hbestorev
1835     cases (some_inversion ????? Hbestorev) #m2' * #Hbestorev #Hstoren
1836     lapply (bestorev_writeable_memory_ext … Hext …  o hd Hmem … Hbestorev) #Hext'
1837     @(Hind … Hstoren) //
1838] qed.   
1839
1840(* In proofs, [disjoint_extension] is not enough. When a variable lookup arises, if
1841 * the variable is not in a local environment, then we search into the global one.
1842 * A proper "extension" of a local environment should be such that the extension
1843 * does not collide with a given global env.
1844 * To see the details of why we need that, see [exec_lvalue'], Evar id case.
1845 *)
1846definition ext_fresh_for_genv ≝
1847λ(ext : list (ident × type)). λ(ge : genv).
1848  ∀id. mem_assoc_env id ext = true → find_symbol … ge id = None ?.
1849
1850(* "generic" simulation relation on [res ?] *)
1851definition res_sim ≝
1852  λ(A : Type[0]).
1853  λ(r1, r2 : res A).
1854  ∀a. r1 = OK ? a → r2 = OK ? a.
1855
1856(* Specialisation of [res_sim] to match [exec_expr] *)
1857definition exec_expr_sim ≝ res_sim (val × trace).
1858
1859(* Specialisation of [res_sim] to match [exec_lvalue] *)
1860definition exec_lvalue_sim ≝ res_sim (block × offset × trace).
1861
1862lemma load_value_of_type_dec : ∀ty, m, b, o. load_value_of_type ty m b o = None ? ∨ ∃r. load_value_of_type ty m b o = Some ? r.
1863#ty #m #b #o cases (load_value_of_type ty m b o)
1864[ 1: %1 // | 2: #v %2 /2 by ex_intro/ ] qed.
1865
1866(* Simulation relations. *)
1867inductive switch_cont_sim : list (ident × type) → cont → cont → Prop ≝
1868| swc_stop :
1869    ∀new_vars. switch_cont_sim new_vars Kstop Kstop
1870| swc_seq : ∀s,k,k',u,s',new_vars.
1871    fresh_for_statement s u →
1872    switch_cont_sim new_vars k k' →
1873    s' = ret_st ? (switch_removal s u) →
1874    lset_inclusion ? (ret_vars ? (switch_removal s u)) new_vars →
1875    switch_cont_sim new_vars (Kseq s k) (Kseq s' k')
1876| swc_while : ∀e,s,k,k',u,s',new_vars.
1877    fresh_for_statement (Swhile e s) u →
1878    switch_cont_sim new_vars k k' →
1879    s' = ret_st ? (switch_removal s u) →   
1880    lset_inclusion ? (ret_vars ? (switch_removal s u)) new_vars →   
1881    switch_cont_sim new_vars (Kwhile e s k) (Kwhile e s' k')
1882| swc_dowhile : ∀e,s,k,k',u,s',new_vars.
1883    fresh_for_statement (Sdowhile e s) u →
1884    switch_cont_sim new_vars k k' →
1885    s' = ret_st ? (switch_removal s u) →       
1886    lset_inclusion ? (ret_vars ? (switch_removal s u)) new_vars →   
1887    switch_cont_sim new_vars (Kdowhile e s k) (Kdowhile e s' k')
1888| swc_for1 : ∀e,s1,s2,k,k',u,s',new_vars.
1889    fresh_for_statement (Sfor Sskip e s1 s2) u →
1890    switch_cont_sim new_vars k k' →
1891    s' = (ret_st ? (switch_removal (Sfor Sskip e s1 s2) u)) →
1892    lset_inclusion ? (ret_vars ? (switch_removal (Sfor Sskip e s1 s2) u)) new_vars →   
1893    switch_cont_sim new_vars (Kseq (Sfor Sskip e s1 s2) k) (Kseq s' k')
1894| swc_for2 : ∀e,s1,s2,k,k',u,result1,result2,new_vars.
1895    fresh_for_statement (Sfor Sskip e s1 s2) u →
1896    switch_cont_sim new_vars k k' →
1897    result1 = ret_st ? (switch_removal s1 u) →
1898    result2 = ret_st ? (switch_removal s2 (ret_u ? (switch_removal s1 u))) →
1899    lset_inclusion ? (ret_vars ? (switch_removal (Sfor Sskip e s1 s2) u)) new_vars →
1900    switch_cont_sim new_vars (Kfor2 e s1 s2 k) (Kfor2 e result1 result2 k')
1901| swc_for3 : ∀e,s1,s2,k,k',u,result1,result2,new_vars.
1902    fresh_for_statement (Sfor Sskip e s1 s2) u →
1903    switch_cont_sim new_vars k k' →
1904    result1 = ret_st ? (switch_removal s1 u) →
1905    result2 = ret_st ? (switch_removal s2 (ret_u ? (switch_removal s1 u))) →
1906    lset_inclusion ? (ret_vars ? (switch_removal (Sfor Sskip e s1 s2) u)) new_vars →
1907    switch_cont_sim new_vars (Kfor3 e s1 s2 k) (Kfor3 e result1 result2 k')
1908| swc_switch : ∀k,k',new_vars.
1909    switch_cont_sim new_vars k k' →
1910    switch_cont_sim new_vars (Kswitch k) (Kswitch k')
1911| swc_call : ∀en,en',r,f,k,k',old_vars,new_vars. (* Warning: possible caveat with environments here. *)       
1912    switch_cont_sim old_vars k k' →
1913    old_vars = \snd (function_switch_removal f) →
1914    disjoint_extension en en' old_vars →
1915    switch_cont_sim
1916      new_vars
1917      (Kcall r f en k)
1918      (Kcall r (\fst (function_switch_removal f)) en' k').
1919
1920record switch_removal_globals (F:Type[0]) (t:F → F) (ge:genv_t F) (ge':genv_t F) : Prop ≝ {
1921  rg_find_symbol: ∀s.
1922    find_symbol ? ge s = find_symbol ? ge' s;
1923  rg_find_funct: ∀v,f.
1924    find_funct ? ge v = Some ? f →
1925    find_funct ? ge' v = Some ? (t f);
1926  rg_find_funct_ptr: ∀b,f.
1927    find_funct_ptr ? ge b = Some ? f →
1928    find_funct_ptr ? ge' b = Some ? (t f)
1929}.
1930
1931inductive switch_state_sim (ge : genv) : state → state → Prop ≝
1932| sws_state :
1933 (* current statement *)
1934 ∀sss_statement  : statement.
1935 (* label universe *)
1936 ∀sss_lu         : universe SymbolTag.
1937 (* [sss_lu] must be fresh *)
1938 ∀sss_lu_fresh   : fresh_for_statement sss_statement sss_lu.
1939 (* current function *)
1940 ∀sss_func       : function.
1941 (* current function after translation *)
1942 ∀sss_func_tr    : function.
1943 (* variables generated during conversion of the function *)
1944 ∀sss_new_vars   : list (ident × type).
1945 (* statement of the transformation *)
1946 ∀sss_func_hyp   : 〈sss_func_tr, sss_new_vars〉 = function_switch_removal sss_func.
1947 (* memory state before conversion *)
1948 ∀sss_m          : mem.
1949 (* memory state after conversion *)
1950 ∀sss_m_ext      : mem.
1951 (* environment before conversion *)
1952 ∀sss_env        : env.
1953 (* environment after conversion *)
1954 ∀sss_env_ext    : env.
1955 (* continuation before conversion *)
1956 ∀sss_k          : cont.
1957 (* continuation after conversion *)
1958 ∀sss_k_ext      : cont.
1959 (* writeable blocks *)
1960 ∀sss_writeable  : list block.
1961 (* memory "injection" *)
1962 ∀sss_mem_hyp    : sr_memext sss_m sss_m_ext sss_writeable.
1963 (* The extended environment does not interfer with the old one. *)
1964 ∀sss_env_hyp    : disjoint_extension sss_env sss_env_ext sss_new_vars.
1965 (* The new variables are allocated to a size corresponding to their types. *)
1966 ∀sss_new_alloc  :
1967    (∀v.meml ? v sss_new_vars →
1968      ∀vb. lookup … sss_env_ext (\fst v) = Some ? vb →
1969           valid_block sss_m_ext vb ∧
1970           low (blocks sss_m_ext vb) = OZ ∧
1971           high (blocks sss_m_ext vb) = sizeof (\snd v)).
1972 (* Exit label for the enclosing switch, if any. Use for [break] conversion in switches. *)
1973 ∀sss_enclosing_label : option label.
1974 (* Extension blocks are writeable. *)
1975 ∀sss_ext_write  : lset_inclusion ? (lset_difference ? (blocks_of_env sss_env_ext) (blocks_of_env sss_env)) sss_writeable.
1976 (* conversion of the current statement, using the variables produced during the conversion of the current function *)
1977 ∀sss_result_rec.
1978 ∀sss_result_hyp : switch_removal sss_statement sss_lu = sss_result_rec.
1979 ∀sss_result.
1980 sss_result = ret_st ? sss_result_rec →
1981 (* inclusion of the locally produced new variables in the global new variables *)
1982 lset_inclusion ? (ret_vars ? sss_result_rec) sss_new_vars →
1983 (* simulation between the continuations before and after conversion. *)
1984 ∀sss_k_hyp      : switch_cont_sim sss_new_vars sss_k sss_k_ext.
1985 ext_fresh_for_genv sss_new_vars ge →
1986    switch_state_sim
1987      ge
1988      (State sss_func sss_statement sss_k sss_env sss_m)
1989      (State sss_func_tr sss_result sss_k_ext sss_env_ext sss_m_ext)
1990| sws_callstate :
1991 ∀ssc_vf.
1992 ∀ssc_fd.
1993 ∀ssc_args.
1994 ∀ssc_k.
1995 ∀ssc_k_ext.
1996 ∀ssc_m.
1997 ∀ssc_m_ext.
1998 ∀ssc_writeable.
1999 ∀ssc_mem_hyp : sr_memext ssc_m ssc_m_ext ssc_writeable.
2000 (match ssc_fd with
2001  [ CL_Internal ssc_f ⇒
2002    switch_cont_sim (\snd (function_switch_removal ssc_f)) ssc_k ssc_k_ext
2003  | _ ⇒ True ]) →
2004    switch_state_sim ge (Callstate ssc_vf ssc_fd ssc_args ssc_k ssc_m)
2005                        (Callstate ssc_vf (fundef_switch_removal ssc_fd) ssc_args ssc_k_ext ssc_m_ext)
2006| sws_returnstate :
2007 ∀ssr_result.
2008 ∀ssr_k       : cont.
2009 ∀ssr_k_ext   : cont.
2010 ∀ssr_m       : mem.
2011 ∀ssr_m_ext   : mem.
2012 ∀ssr_writeable : list block.
2013 ∀ssr_mem_hyp : sr_memext ssr_m ssr_m_ext ssr_writeable.
2014 ∀ssr_vars.
2015    switch_cont_sim ssr_vars ssr_k ssr_k_ext →
2016    switch_state_sim ge (Returnstate ssr_result ssr_k ssr_m) (Returnstate ssr_result ssr_k_ext ssr_m_ext)
2017| sws_finalstate : ∀r.
2018    switch_state_sim ge (Finalstate r) (Finalstate r).
2019
2020lemma call_cont_swremoval : ∀k,k',vars.
2021  switch_cont_sim vars k k' →
2022  switch_cont_sim vars (call_cont k) (call_cont k').
2023#k0 #k0' #vars #K elim K /2/
2024qed.
2025
2026(* [eventually ge P s tr] states that after a finite number of [exec_step], the
2027   property P on states will be verified. *)
2028inductive eventually (ge : genv) (P : state → Prop) : state → trace → Prop ≝
2029| eventually_base : ∀s,t,s'.
2030    exec_step ge s = Value io_out io_in ? 〈t, s'〉 →
2031    P s' →
2032    eventually ge P s t
2033| eventually_step : ∀s,t,s',t',trace.
2034    exec_step ge s = Value io_out io_in ? 〈t, s'〉 →
2035    eventually ge P s' t' →
2036    trace = (t ⧺ t') →
2037    eventually ge P s trace.
2038   
2039(* [eventually] is not so nice to use directly, we would like to make the mandatory
2040 * [exec_step ge s = Value ??? 〈t, s'] visible - and in the end we would like not
2041   to give [s'] ourselves, but matita to compute it. Hence this little intro-wrapper. *)     
2042lemma eventually_now : ∀ge,P,s,t.
2043  (∃s'.exec_step ge s = Value io_out io_in ? 〈t,s'〉 ∧ P s') →
2044   eventually ge P s t.
2045#ge #P #s #t * #s' * #Hexec #HP %1{… Hexec HP}  (* %{E0} normalize >(append_nil ? t) %1{????? Hexec HP} *)
2046qed.
2047
2048lemma eventually_later : ∀ge,P,s,t.
2049   (∃s',tstep.exec_step ge s = Value io_out io_in ? 〈tstep,s'〉 ∧ ∃tnext. t = tstep ⧺ tnext ∧ eventually ge P s' tnext) →
2050    eventually ge P s t.
2051#ge #P #s #t * #s' * #tstep * #Hexec_step * #tnext * #Heq #Heventually %2{s tstep s' tnext … Heq}
2052try assumption
2053qed.
2054
2055lemma exec_lvalue_expr_elim :
2056  ∀r1,r2,Pok,Qok.
2057  exec_lvalue_sim r1 r2 →
2058  (∀val,trace.
2059    match Pok 〈val,trace〉 with
2060    [ Error err ⇒ True
2061    | OK pvt ⇒
2062      let 〈pval,ptrace〉 ≝ pvt in
2063      match Qok 〈val,trace〉 with
2064      [ Error err ⇒ False
2065      | OK qvt ⇒
2066        let 〈qval,qtrace〉 ≝ qvt in
2067        ptrace = qtrace ∧ pval = qval
2068      ]
2069    ]) → 
2070  exec_expr_sim
2071    (match r1 with [ OK x ⇒ Pok x | Error err ⇒ Error ? err ])
2072    (match r2 with [ OK x ⇒ Qok x | Error err ⇒ Error ? err ]).
2073#r1 #r2 #Pok #Qok whd in ⊢ (% → ?);
2074elim r1
2075[ 2:  #error #_ #_ normalize #a #Habsurd destruct (Habsurd)
2076| 1: normalize nodelta #a #H lapply (H a (refl ??))
2077     #Hr2 >Hr2 normalize #H #a' elim a * #b #o #tr
2078     lapply (H 〈b, o〉 tr) #H1 #H2 >H2 in H1;
2079     normalize nodelta elim a' in H2; #pval #ptrace #Hpok
2080     normalize nodelta cases (Qok 〈b,o,tr〉)
2081     [ 2: #error normalize #Habsurd @(False_ind … Habsurd)
2082     | 1: * #qval #qtrace normalize nodelta * #Htrace #Hval
2083          destruct @refl
2084] ] qed.
2085
2086
2087lemma exec_expr_expr_elim :
2088  ∀r1,r2,Pok,Qok.
2089  exec_expr_sim r1 r2 →
2090  (∀val,trace.
2091    match Pok 〈val,trace〉 with
2092    [ Error err ⇒ True
2093    | OK pvt ⇒
2094      let 〈pval,ptrace〉 ≝ pvt in
2095      match Qok 〈val,trace〉 with
2096      [ Error err ⇒ False
2097      | OK qvt ⇒
2098        let 〈qval,qtrace〉 ≝ qvt in
2099        ptrace = qtrace ∧ pval = qval
2100      ]
2101    ]) →
2102  exec_expr_sim
2103    (match r1 with [ OK x ⇒ Pok x | Error err ⇒ Error ? err ])
2104    (match r2 with [ OK x ⇒ Qok x | Error err ⇒ Error ? err ]).
2105#r1 #r2 #Pok #Qok whd in ⊢ (% → ?);
2106elim r1
2107[ 2: #error #_ #_ normalize #a1 #Habsurd destruct (Habsurd)
2108| 1: normalize nodelta #a #H lapply (H a (refl ??))
2109     #Hr2 >Hr2 normalize nodelta #H
2110     elim a in Hr2; #val #trace
2111     lapply (H … val trace)
2112     cases (Pok 〈val, trace〉)
2113     [ 2: #error normalize #_ #_ #a' #Habsurd destruct (Habsurd)
2114     | 1: * #pval #ptrace normalize nodelta
2115          cases (Qok 〈val,trace〉)
2116          [ 2: #error normalize #Hfalse @(False_ind … Hfalse)
2117          | 1: * #qval #qtrace normalize nodelta * #Htrace_eq #Hval_eq
2118               #Hr2_eq destruct #a #H @H
2119] ] ] qed.
2120
2121lemma load_value_of_type_inj : ∀m1,m2,writeable,b,off,ty.
2122    sr_memext m1 m2 writeable → ∀v.
2123    load_value_of_type ty m1 b off = Some ? v →
2124    load_value_of_type ty m2 b off = Some ? v.
2125#m1 #m2 #writeable #b #off #ty #Hinj #v
2126@(load_sim_fe ?? (sr_memext_load_sim … Hinj) (mk_pointer b off))
2127qed.
2128
2129(* Conservation of the semantics of binary operators under memory extensions.
2130   Tried to factorise it a bit but the play with indexes just becomes too messy.  *)
2131lemma sim_sem_binary_operation : ∀op,v1,v2,e1,e2,m1,m2,target_type,writeable.
2132  ∀Hext:sr_memext m1 m2 writeable. ∀res.
2133  sem_binary_operation op v1 (typeof e1) v2 (typeof e2) m1 target_type = Some ? res →
2134  sem_binary_operation op v1 (typeof e1) v2 (typeof e2) m2 target_type = Some ? res.
2135#op #v1 #v2 #e1 #e2 #m1 #m2 #target_type #writeable #Hmemext #res cases op
2136whd in match (sem_binary_operation ???????);
2137try //
2138whd in match (sem_binary_operation ???????);
2139lapply (me_valid_pointers … Hmemext)
2140lapply (me_not_writeable_ptr … Hmemext)
2141elim m1 in Hmemext; #contents1 #nextblocks1 #Hnextpos1
2142elim m2 #contents2 #nextblocks2 #Hnextpos2
2143* #Hnonempty #Hwriteable #Hnot_writeable #Hnot_writeable_ptr #Hvalid
2144whd in match (sem_cmp ??????);
2145whd in match (sem_cmp ??????);
2146[ 1,2: cases (classify_cmp (typeof e1) (typeof e2))
2147     normalize nodelta
2148     [ 1,4: #sz #sg try //
2149     | 2,5: #opt #ty
2150          cases v1 normalize nodelta
2151          [ 1,5: | 2,6: #sz #i | 3,7: | 4,8: #ptr ]
2152          [ 1,2,3,4: #Habsurd destruct (Habsurd)
2153          | 5,6: #H @H ]
2154          cases v2 normalize nodelta
2155          [ 1,5: | 2,6: #sz' #i' | 3,7: | 4,8: #ptr' ]
2156          [ 1,2,3,4: #Habsurd destruct (Habsurd)
2157          | 5,6: #H @H ]
2158          lapply (Hvalid ptr)
2159          cases (valid_pointer (mk_mem contents1 nextblocks1 Hnextpos1) ptr)
2160          [ 2,4: >andb_lsimpl_false normalize nodelta cases (eq_block ??) #_ normalize #Habsurd destruct (Habsurd) ]
2161          #Hvalid' >(Hvalid' (refl ??))
2162          lapply (Hvalid ptr')
2163          cases (valid_pointer (mk_mem contents1 nextblocks1 Hnextpos1) ptr')
2164          [ 2,4: >andb_lsimpl_true #_ normalize nodelta cases (eq_block ??) normalize nodelta #Habsurd destruct (Habsurd) ]
2165          #H' >(H' (refl ??)) >andb_lsimpl_true normalize nodelta #H @H
2166     | 3,6: #ty1 #ty2 #H @H ]
2167| 3,4: cases (classify_cmp (typeof e1) (typeof e2))
2168     normalize nodelta
2169     [ 1,4: #sz #sg try //
2170     | 2,5: #opt #ty
2171          cases v1 normalize nodelta
2172          [ 1,5: | 2,6: #sz #i | 3,7: | 4,8: #ptr ]
2173          [ 1,2,3,4: #Habsurd destruct (Habsurd)
2174          | 5,6: #H @H ]
2175          cases v2 normalize nodelta
2176          [ 1,5: | 2,6: #sz' #i' | 3,7: | 4,8: #ptr' ]
2177          [ 1,2,3,4: #Habsurd destruct (Habsurd)
2178          | 5,6: #H @H ]
2179          lapply (Hvalid ptr)
2180          cases (valid_pointer (mk_mem contents1 nextblocks1 Hnextpos1) ptr)
2181          [ 2,4: >andb_lsimpl_false normalize nodelta cases (eq_block ??) #_ normalize #Habsurd destruct (Habsurd) ]
2182          #Hvalid' >(Hvalid' (refl ??))
2183          lapply (Hvalid ptr')
2184          cases (valid_pointer (mk_mem contents1 nextblocks1 Hnextpos1) ptr')
2185          [ 2,4: >andb_lsimpl_true #_ normalize nodelta cases (eq_block ??) normalize nodelta #Habsurd destruct (Habsurd) ]
2186          #H' >(H' (refl ??)) >andb_lsimpl_true normalize nodelta #H @H
2187     | 3,6: #ty1 #ty2 #H @H ]     
2188| 5,6: cases (classify_cmp (typeof e1) (typeof e2))
2189     normalize nodelta
2190     [ 1,4: #sz #sg try //
2191     | 2,5: #opt #ty
2192          cases v1 normalize nodelta
2193          [ 1,5: | 2,6: #sz #i | 3,7: | 4,8: #ptr ]
2194          [ 1,2,3,4: #Habsurd destruct (Habsurd)
2195          | 5,6: #H @H ]
2196          cases v2 normalize nodelta
2197          [ 1,5: | 2,6: #sz' #i' | 3,7: | 4,8: #ptr' ]
2198          [ 1,2,3,4: #Habsurd destruct (Habsurd)
2199          | 5,6: #H @H ]
2200          lapply (Hvalid ptr)
2201          cases (valid_pointer (mk_mem contents1 nextblocks1 Hnextpos1) ptr)
2202          [ 2,4: >andb_lsimpl_false normalize nodelta cases (eq_block ??) #_ normalize #Habsurd destruct (Habsurd) ]
2203          #Hvalid' >(Hvalid' (refl ??))
2204          lapply (Hvalid ptr')
2205          cases (valid_pointer (mk_mem contents1 nextblocks1 Hnextpos1) ptr')
2206          [ 2,4: >andb_lsimpl_true #_ normalize nodelta cases (eq_block ??) normalize nodelta #Habsurd destruct (Habsurd) ]
2207          #H' >(H' (refl ??)) >andb_lsimpl_true normalize nodelta #H @H
2208     | 3,6: #ty1 #ty2 #H @H ]
2209] qed.
2210
2211(* Simulation relation on expressions *)
2212lemma sim_related_globals : ∀ge,ge',en1,m1,en2,m2,writeable,ext.
2213  ∀Hext:sr_memext m1 m2 writeable.
2214  switch_removal_globals ? fundef_switch_removal ge ge' →
2215  disjoint_extension en1 en2 ext →
2216(*  disjoint_extension en1 en2 ext Hext → *)
2217  ext_fresh_for_genv ext ge →
2218  (∀e. exec_expr_sim (exec_expr ge en1 m1 e) (exec_expr ge' en2 m2 e)) ∧
2219  (∀ed, ty. exec_lvalue_sim (exec_lvalue' ge en1 m1 ed ty) (exec_lvalue' ge' en2 m2 ed ty)).
2220#ge #ge' #en1 #m1 #en2 #m2 #writeable #ext #Hext #Hrelated #Hdisjoint (* #Hdisjoint *) #Hext_fresh_for_genv
2221@expr_lvalue_ind_combined
2222[ 1: #csz #cty #i #a1
2223     whd in match (exec_expr ????); elim cty
2224     [ | #sz #sg | #ty | #ty #n | #tl #ty | #id #fl | #id #fl | #ty ]
2225     normalize nodelta
2226     [ 2: cases (eq_intsize csz sz) normalize nodelta
2227          [ 1: #H destruct (H) /4 by ex_intro, conj, vint_eq/
2228          | 2: #Habsurd destruct (Habsurd) ]
2229     | 3,4,5: #_ #H destruct (H)
2230     | *: #H destruct (H) ]
2231| 2: *
2232  [ #sz #i | #var_id | #e1 | #e1 | #op #e1 | #op #e1 #e2 | #cast_ty #e1
2233  | #cond #iftrue #iffalse | #e1 #e2 | #e1 #e2 | #sizeofty | #e1 #field | #cost #e1 ]
2234  #ty whd in ⊢ (% → ?); #Hind try @I
2235  whd in match (Plvalue ???);
2236  [ 1,2,3: whd in match (exec_expr ????); whd in match (exec_expr ????); #a1
2237       cases (exec_lvalue' ge en1 m1 ? ty) in Hind;
2238       [ 2,4,6: #error #_ normalize in ⊢ (% → ?); #Habsurd destruct (Habsurd)
2239       | 1,3,5: normalize nodelta #b1 #H lapply (H b1 (refl ??)) #Heq >Heq       
2240           normalize nodelta
2241           elim b1 * #bl1 #o1 #tr1 (* elim b2 * #bl2 #o2 #tr2 *)
2242           whd in match (load_value_of_type' ???);
2243           whd in match (load_value_of_type' ???);
2244           lapply (load_value_of_type_inj m1 m2 writeable bl1 o1 ty Hext)
2245           cases (load_value_of_type ty m1 bl1 o1)
2246           [ 1,3,5: #_ #Habsurd normalize in Habsurd; destruct (Habsurd)
2247           | 2,4,6: #v #H normalize in ⊢ (% → ?); #Heq destruct (Heq)
2248                    >(H v (refl ??)) @refl
2249  ] ] ]
2250| 3: #v #ty whd * * #b #o #tr
2251     whd in match (exec_lvalue' ?????);
2252     whd in match (exec_lvalue' ?????); cases Hdisjoint *
2253     #HA #HB #HC lapply (HA v) lapply (HB v) lapply (HC v) -HA -HB -HC
2254     lapply (Hext_fresh_for_genv v)
2255     cases (mem_assoc_env v ext) #Hglobal
2256     [ 1: >(Hglobal (refl ??)) #_ #HB #HA >(HA (refl ??)) normalize
2257          #Habsurd destruct
2258     | 2: normalize nodelta #Hsim #_ #_
2259          cases (lookup ?? en1 v) in Hsim; normalize nodelta
2260          [ 1: #Hlookup2 <(Hlookup2 (refl ??)) normalize nodelta
2261               lapply (rg_find_symbol … Hrelated v) #Heq_find_sym >Heq_find_sym
2262               #H @H
2263          | 2: #blo #Hlookup2 <(Hlookup2 (refl ??)) #Heq normalize nodelta @Heq ] ]
2264| 4: #e #ty whd in ⊢ (% → %);
2265     whd in match (exec_lvalue' ?????);
2266     whd in match (exec_lvalue' ?????);
2267     cases (exec_expr ge en1 m1 e)
2268     [ 1: * #v1 #tr1 #H elim (H 〈v1,tr1〉 (refl ??)) * #v1' #tr1' #H @H
2269     | 2: #error #_ normalize #a1 #Habsurd destruct (Habsurd) ]
2270| 5: #ty #e #ty'
2271     #Hsim @(exec_lvalue_expr_elim … Hsim)
2272     cases ty
2273     [ | #sz #sg | #ty | #ty #n | #tl #ty | #id #fl | #id #fl | #ty ]
2274     * #b #o normalize nodelta try /2 by I/
2275     #tr @conj try @refl
2276| 6: #ty #op #e
2277     #Hsim @(exec_expr_expr_elim … Hsim) #v #trace
2278     cases (sem_unary_operation op v (typeof e)) normalize nodelta
2279     try @I
2280     #v @conj @refl
2281| 7: #ty #op #e1 #e2 #Hsim1 #Hsim2
2282     @(exec_expr_expr_elim … Hsim1) #v #trace
2283     cases (exec_expr ge en1 m1 e2) in Hsim2;
2284     [ 2: #error // ]
2285     * #pval #ptrace normalize in ⊢ (% → ?); #Hsim2
2286     whd in match (m_bind ?????);
2287     >(Hsim2 ? (refl ??))
2288     whd in match (m_bind ?????);
2289     lapply (sim_sem_binary_operation op v pval e1 e2 m1 m2 ty writeable Hext)
2290     cases (sem_binary_operation op v (typeof e1) pval (typeof e2) m1 ty)
2291     [ 1: #_ // ] #val #H >(H val (refl ??))
2292     normalize destruct @conj @refl
2293| 8: #ty #cast_ty #e #Hsim @(exec_expr_expr_elim … Hsim)
2294     #v #tr
2295     cut (exec_cast m1 v (typeof e) cast_ty = exec_cast m2 v (typeof e) cast_ty)
2296     [ @refl ]
2297     #Heq >Heq     
2298     cases (exec_cast m2 v (typeof e) cast_ty)
2299     [ 2: //
2300     | 1: #v normalize @conj @refl ]
2301| 9: #ty #e1 #e2 #e3 #Hsim1 #Hsim2 #Hsim3
2302     @(exec_expr_expr_elim … Hsim1) #v #tr
2303     cases (exec_bool_of_val ? (typeof e1)) #b
2304     [ 2: normalize @I ]
2305     cases b normalize nodelta
2306     whd in match (m_bind ?????);
2307     whd in match (m_bind ?????);
2308     normalize nodelta
2309     [ 1: (* true branch *)
2310          cases (exec_expr ge en1 m1 e2) in Hsim2;
2311          [ 2: #error normalize #_ @I
2312          | 1: * #e2v #e2tr normalize #H >(H ? (refl ??)) normalize nodelta
2313               @conj @refl ]
2314     | 2: (* false branch *)
2315          cases (exec_expr ge en1 m1 e3) in Hsim3;
2316          [ 2: #error normalize #_ @I
2317          | 1: * #e3v #e3tr normalize #H >(H ? (refl ??)) normalize nodelta
2318               @conj @refl ] ]
2319| 10,11: #ty #e1 #e2 #Hsim1 #Hsim2
2320     @(exec_expr_expr_elim … Hsim1) #v #tr
2321     cases (exec_bool_of_val v (typeof e1))
2322     [ 2,4: #error normalize @I ]
2323     *
2324     whd in match (m_bind ?????);
2325     whd in match (m_bind ?????);
2326     [ 2,3: cases (cast_bool_to_target ty ?) normalize // #v @conj try @refl ]
2327     cases (exec_expr ge en1 m1 e2) in Hsim2;
2328     [ 2,4: #error #_ normalize @I ]
2329     * #v2 #tr2 whd in ⊢ (% → %); #H2 normalize nodelta >(H2 ? (refl ??))
2330     normalize nodelta
2331     cases (exec_bool_of_val v2 (typeof e2))
2332     [ 2,4: #error normalize @I ]
2333     *
2334     whd in match (m_bind ?????);
2335     cases (cast_bool_to_target ty ?) normalize // #v @conj try @refl
2336| 12: #ty #ty' cases ty
2337     [ | #sz #sg | #ty | #ty #n | #tl #ty | #id #fl | #id #fl | #ty ]
2338     whd in match (exec_expr ????); whd #a #H @H
2339| 13: #ty #ed #aggregty #i #Hsim whd * * #b #o #tr
2340    whd in match (exec_lvalue' ?????);
2341    whd in match (exec_lvalue' ge' en2 m2 (Efield (Expr ed aggregty) i) ty);
2342    whd in match (typeof ?);
2343    cases aggregty in Hsim;
2344    [ | #sz #sg | #ty | #ty #n | #tl #ty | #id #fl | #id #fl | #ty ]
2345    normalize nodelta #Hsim
2346    [ 1,2,3,4,5,8: #Habsurd destruct (Habsurd) ]
2347    whd in match (m_bind ?????);
2348    whd in match (m_bind ?????);
2349    whd in match (exec_lvalue ge en1 m1 (Expr ed ?));
2350    cases (exec_lvalue' ge en1 m1 ed ?) in Hsim;
2351    [ 2,4: #error #_ normalize in ⊢ (% → ?); #Habsurd destruct (Habsurd) ]
2352    * * #b1 #o1 #tr1 whd in ⊢ (% → ?); #H
2353    whd in match (exec_lvalue ge' en2 m2 (Expr ed ?));   
2354     >(H ? (refl ??)) normalize nodelta #H @H
2355| 14: #ty #l #e #Hsim
2356     @(exec_expr_expr_elim … Hsim) #v #tr normalize nodelta @conj //
2357| 15: *
2358  [ #sz #i | #var_id | #e1 | #e1 | #op #e1 | #op #e1 #e2 | #cast_ty #e1
2359  | #cond #iftrue #iffalse | #e1 #e2 | #e1 #e2 | #sizeofty | #e1 #field | #cost #e1 ]
2360  #ty normalize in ⊢ (% → ?);
2361  [ 2,3,12: @False_ind
2362  | *: #_ normalize #a1 #Habsurd @Habsurd ]
2363] qed.
2364
2365lemma exec_lvalue_sim_aux : ∀ge,ge',env,env_ext,m,m_ext.
2366  (∀ed,ty. exec_lvalue_sim (exec_lvalue' ge env m ed ty)
2367                           (exec_lvalue' ge' env_ext m_ext ed ty)) →
2368  ∀e. exec_lvalue_sim (exec_lvalue ge env m e)
2369                      (exec_lvalue ge' env_ext m_ext e).
2370#ge #ge' #env #env_ext #m #m_ext #H * #ed #ty @H qed.
2371
2372lemma exec_expr_sim_to_exec_exprlist :
2373  ∀ge,ge',en1,en2,m1,m2.
2374  (∀e. exec_expr_sim (exec_expr ge en1 m1 e) (exec_expr ge' en2 m2 e)) →
2375   ∀l. res_sim ? (exec_exprlist ge en1 m1 l) (exec_exprlist ge' en2 m2 l).
2376#ge #ge' #en1 #en2 #m1 #m2 #Hsim #l elim l
2377[ 1: whd #a #Heq normalize in Heq ⊢ %; destruct @refl
2378| 2: #hd #tl #Hind whd * #lv #tr whd in ⊢ ((??%?) → (??%?));
2379     lapply (Hsim hd)
2380     cases (exec_expr ge en1 m1 hd)
2381     [ 2: #error normalize #_ #Habsurd destruct (Habsurd)
2382     | 1: * #v #vtr whd in ⊢ (% → ?); #Hsim >(Hsim ? (refl ??))
2383          normalize nodelta
2384          cases (exec_exprlist ge en1 m1 tl) in Hind;
2385          [ 2: #error normalize #_ #Habsurd destruct (Habsurd)
2386          | 1: #a normalize #H >(H ? (refl ??)) #Heq destruct normalize @refl
2387          ]
2388     ]
2389] qed.
2390
2391(* The return type of any function is invariant under switch removal *)
2392lemma fn_return_simplify : ∀f. fn_return (\fst (function_switch_removal f)) = fn_return f.
2393#f elim f #ty #args #vars #body whd in match (function_switch_removal ?);
2394cases (switch_removal ??) * #stmt #fvs #u @refl
2395qed.
2396
2397(* Similar stuff for fundefs *)
2398lemma fundef_type_simplify : ∀clfd. type_of_fundef clfd = type_of_fundef (fundef_switch_removal clfd).
2399* // * #ty #args #vars #body whd in ⊢ (??%%);
2400whd in match (function_switch_removal ?); cases (switch_removal ??) * #st #u
2401normalize nodelta #u @refl
2402qed.
2403
2404lemma while_fresh_lift : ∀e,s,u.
2405   fresh_for_expression e u → fresh_for_statement s u → fresh_for_statement (Swhile e s) u.
2406#e #s * #u whd in ⊢ (% → % → %); whd in match (max_of_statement (Swhile ??));
2407cases (max_of_expr e) #e cases (max_of_statement s) #s normalize
2408cases (leb e s) try /2/
2409qed.
2410
2411(*
2412lemma while_commute : ∀e0, s0, us0. Swhile e0 (switch_removal s0 us0) = (sw_rem (Swhile e0 s0) us0).
2413#e0 #s0 #us0 normalize
2414cases (switch_removal s0 us0) * #body #newvars #u' normalize //
2415qed.*)
2416
2417lemma dowhile_fresh_lift : ∀e,s,u.
2418   fresh_for_expression e u → fresh_for_statement s u → fresh_for_statement (Sdowhile e s) u.
2419#e #s * #u whd in ⊢ (% → % → %); whd in match (max_of_statement (Sdowhile ??));
2420cases (max_of_expr e) #e cases (max_of_statement s) #s normalize
2421cases (leb e s) try /2/
2422qed.
2423
2424(*
2425lemma dowhile_commute : ∀e0, s0, us0. Sdowhile e0 (sw_rem s0 us0) = (sw_rem (Sdowhile e0 s0) us0).
2426#e0 #s0 #us0 normalize
2427cases (switch_removal s0 us0) * #body #newvars #u' normalize //
2428qed.*)
2429
2430lemma for_fresh_lift : ∀cond,step,body,u.
2431  fresh_for_statement step u →
2432  fresh_for_statement body u →
2433  fresh_for_expression cond u →
2434  fresh_for_statement (Sfor Sskip cond step body) u.
2435#cond #step #body #u
2436whd in ⊢ (% → % → % → %); whd in match (max_of_statement (Sfor ????));
2437cases (max_of_statement step) #s
2438cases (max_of_statement body) #b
2439cases (max_of_expr cond) #c
2440whd in match (max_of_statement Sskip);
2441>(max_id_commutative least_identifier)
2442>max_id_one_neutral normalize nodelta
2443normalize elim u #u
2444cases (leb s b) cases (leb c b) cases (leb c s) try /2/
2445qed.
2446
2447(*
2448lemma for_commute : ∀e,stm1,stm2,u,uA.
2449   (uA=\snd  (switch_removal stm1 u)) →
2450   sw_rem (Sfor Sskip e stm1 stm2) u = (Sfor Sskip e (sw_rem stm1 u) (sw_rem stm2 uA)).
2451#e #stm1 #stm2 #u #uA #HuA
2452whd in match (sw_rem (Sfor ????) u);
2453whd in match (switch_removal ??);   
2454destruct
2455normalize in match (\snd (switch_removal Sskip u));
2456whd in match (sw_rem stm1 u);
2457cases (switch_removal stm1 u)
2458* #stm1' #fresh_vars #uA normalize nodelta
2459whd in match (sw_rem stm2 uA);
2460cases (switch_removal stm2 uA)
2461* #stm2' #fresh_vars2 #uB normalize nodelta
2462//
2463qed.*)
2464
2465lemma simplify_is_not_skip : ∀s. s ≠ Sskip → ∀u. ∃pf. is_Sskip (ret_st ? (switch_removal s u)) = inr ?? pf.
2466*
2467[ 1: * #H @(False_ind … (H (refl ??))) ]
2468try /2/
2469[ 1: #s1 #s2 #_ #u normalize
2470     cases (switch_removal ? ?) * #a #b #c normalize nodelta
2471     cases (switch_removal ? ?) * #e #f #g normalize nodelta
2472     /2 by ex_intro/
2473| 2: #e #s1 #s2 #_ #u normalize
2474     cases (switch_removal ? ?) * #a #b #c normalize nodelta
2475     cases (switch_removal ? ?) * #e #f #g normalize nodelta
2476     /2 by ex_intro/
2477| 3,4: #e #s #_ #u normalize
2478     cases (switch_removal ? ?) * #e #f #g normalize nodelta
2479     /2 by ex_intro/
2480| 5: #s1 #e #s2 #s3 #_ #u normalize     
2481     cases (switch_removal ? ?) * #a #b #c normalize nodelta
2482     cases (switch_removal ? ?) * #e #f #g normalize nodelta     
2483     cases (switch_removal ? ?) * #h #i #j normalize nodelta
2484     /2 by ex_intro/
2485| 6: #e #ls #_ #u normalize
2486     cases (switch_removal_branches ? ?) * #a #b #c normalize nodelta
2487     cases (fresh ??) #e #f normalize nodelta
2488     cases (fresh ? f) #g #h normalize nodelta
2489     cases (produce_cond ????) * #k #l #m normalize nodelta
2490     /2 by ex_intro/
2491| 7,8: #ls #st #_ #u normalize
2492     cases (switch_removal ? ?) * #e #f #g normalize nodelta     
2493     /2 by ex_intro/
2494] qed.
2495
2496(*
2497lemma sw_rem_commute : ∀stm,u.
2498  (\fst (\fst (switch_removal stm u))) = sw_rem stm u.
2499#stm #u whd in match (sw_rem stm u); // qed.
2500*)
2501
2502lemma fresh_for_statement_inv :
2503  ∀u,s. fresh_for_statement s u →
2504        match u with
2505        [ mk_universe p ⇒ le (p0 one) p ].
2506* #p #s whd in match (fresh_for_statement ??);
2507cases (max_of_statement s) #s
2508normalize /2/ qed.
2509
2510lemma fresh_for_Sskip :
2511  ∀u,s. fresh_for_statement s u → fresh_for_statement Sskip u.
2512#u #s #H lapply (fresh_for_statement_inv … H) elim u /2/ qed.
2513
2514lemma fresh_for_Sbreak :
2515  ∀u,s. fresh_for_statement s u → fresh_for_statement Sbreak u.
2516#u #s #H lapply (fresh_for_statement_inv … H) elim u /2/ qed.
2517
2518lemma fresh_for_Scontinue :
2519  ∀u,s. fresh_for_statement s u → fresh_for_statement Scontinue u.
2520#u #s #H lapply (fresh_for_statement_inv … H) elim u /2/ qed.
2521
2522(*
2523lemma switch_removal_eq : ∀s,u. ∃res,fvs,u'. switch_removal s u = 〈res, fvs, u'〉.
2524#s #u elim (switch_removal s u) * #res #fvs #u'
2525%{res} %{fvs} %{u'} //
2526qed.
2527
2528lemma switch_removal_branches_eq : ∀switchcases, u. ∃res,fvs,u'. switch_removal_branches switchcases u = 〈res, fvs, u'〉.
2529#switchcases #u elim (switch_removal_branches switchcases u) * #res #fvs #u'
2530%{res} %{fvs} %{u'} //
2531qed.
2532*)
2533
2534lemma produce_cond_eq : ∀e,ls,u,exit_label. ∃s,lab,u'. produce_cond e ls u exit_label = 〈s,lab,u'〉.
2535#e #ls #u #exit_label cases (produce_cond e ls u exit_label) *
2536#s #lab #u' %{s} %{lab} %{u'} //
2537qed.
2538
2539(* TODO: this lemma ought to be in a more central place, along with its kin of SimplifiCasts.ma ... *)
2540lemma neq_intsize : ∀s1,s2. s1 ≠ s2 → eq_intsize s1 s2 = false.
2541* * *
2542[ 1,5,9: #H @(False_ind … (H (refl ??)))
2543| *: #_ normalize @refl ]
2544qed.
2545
2546lemma exec_expr_int : ∀ge,e,m,expr.
2547    (∃sz,n,tr. exec_expr ge e m expr = (OK ? 〈Vint sz n, tr〉)) ∨ (∀sz,n,tr. exec_expr ge e m expr ≠ (OK ? 〈Vint sz n, tr〉)).
2548#ge #e #m #expr cases (exec_expr ge e m expr)
2549[ 2: #error %2 #sz #n #tr % #H destruct (H)
2550| 1: * #val #trace cases val
2551     [ 2: #sz #n %1 %{sz} %{n} %{trace} @refl
2552     | 3: | 4: #ptr ]
2553     %2 #sz #n #tr % #H destruct (H)
2554] qed.
2555
2556lemma switch_removal_elim : ∀s,u. ∃s',fvs',u'. switch_removal s u = 〈s',fvs',u'〉.
2557#s #u cases (switch_removal s u) * #s' #fvs' #u'
2558%{s'} %{fvs'} %{u'} @refl
2559qed.
2560
2561lemma switch_removal_branches_elim : ∀ls,u. ∃ls',fvs',u'. switch_removal_branches ls u = 〈ls',fvs',u'〉.
2562#ls #u cases (switch_removal_branches ls u) * * #ls' #fvs' #u' /4 by ex_intro/ qed.
2563
2564lemma fresh_elim : ∀u. ∃fv',u'. fresh SymbolTag u = 〈fv', u'〉. #u /3 by ex_intro/ qed.
2565
2566lemma simplify_switch_elim : ∀e,ls,u. ∃res,u'. simplify_switch e ls u = 〈res, u'〉.
2567#e #ls #u cases (simplify_switch e ls u) #res #u /3 by ex_intro/ qed.
2568
2569lemma store_int_success :
2570       ∀b,m,sz,sg,i. valid_block m b → low (blocks m b) = OZ → high (blocks m b) = sizeof (Tint sz sg) →
2571                     ∃m'. store_value_of_type (Tint sz sg) m b zero_offset (Vint sz i) = Some ? m'.
2572#b #m #sz #sg
2573cases sz
2574[ 1: #i #Hvalid #Hlow #Hhigh
2575     whd in match (store_value_of_type ?????);
2576     whd in match (fe_to_be_values ??);
2577     normalize nodelta     
2578     normalize in match (size_intsize ?);
2579     whd in match (bytes_of_bitvector ??);     
2580     lapply (vsplit_eq2 ? 8 0 i) * #li * #ri #Heq_i
2581      <(vsplit_prod … Heq_i) normalize nodelta
2582      >(BitVector_O … ri) whd in match (storen ???);
2583      lapply (valid_pointer_to_bestorev_ok m (mk_pointer b zero_offset) (BVByte li) ?)
2584      [ 1: whd in match (valid_pointer ??); >(Zlt_to_Zltb_true ?? Hvalid) >andb_lsimpl_true
2585           >unfold_low_bound >unfold_high_bound >Hlow >Hhigh
2586           >(Zle_to_Zleb_true … (reflexive_Zle OZ)) normalize nodelta
2587           @Zlt_to_Zltb_true // ]
2588      * #m' #Hbestorev >Hbestorev %{m'} @refl
2589| 2:  #i #Hvalid #Hlow #Hhigh
2590     whd in match (store_value_of_type ?????);
2591     whd in match (fe_to_be_values ??);
2592     normalize nodelta     
2593     normalize in match (size_intsize ?);
2594     whd in match (bytes_of_bitvector ??);             
2595     lapply (vsplit_eq2 ? 8 (1*8) i) * #li * #ri #Heq_i
2596     <(vsplit_prod … Heq_i) normalize nodelta whd in match (storen ???);
2597      lapply (valid_pointer_to_bestorev_ok m (mk_pointer b zero_offset) (BVByte li) ?)
2598      [ 1: whd in match (valid_pointer ??); >(Zlt_to_Zltb_true ?? Hvalid) >andb_lsimpl_true
2599           >unfold_low_bound >unfold_high_bound >Hlow >Hhigh
2600           >(Zle_to_Zleb_true … (reflexive_Zle OZ)) normalize nodelta
2601           @Zlt_to_Zltb_true // ]
2602      * #m0 #Hbestorev >Hbestorev normalize nodelta
2603      whd in match (bytes_of_bitvector ??);         
2604      lapply (vsplit_eq2 ? 8 (0*8) ri) * #rli * #rri #Heq_ri
2605      <(vsplit_prod … Heq_ri) normalize nodelta
2606      cases (mem_bounds_invariant_after_bestorev … Hbestorev) * * * #Hnext0 #Hblocks0 #_ #_ #_
2607      lapply (valid_pointer_to_bestorev_ok m0
2608                (mk_pointer b (mk_offset
2609                     [[false; false; false; false; false; false; false; false; 
2610                       false; false; false; false; false; false; false; true]]))
2611                 (BVByte rli) ?)
2612      [ 1: whd in match (valid_pointer ??); >Hnext0 >(Zlt_to_Zltb_true ?? Hvalid) >andb_lsimpl_true
2613           cases (Hblocks0 b) #HA #HB
2614           >unfold_low_bound >unfold_high_bound >HA >HB >Hlow >Hhigh normalize nodelta
2615           @Zlt_to_Zltb_true normalize // ]
2616      * #m1 #Hbestorev1 %{m1} whd in ⊢ (??(???%)?); whd in match (storen ???);
2617      normalize in match (shift_pointer ???); >Hbestorev1 normalize nodelta
2618      @refl
2619| 3:  #i #Hvalid #Hlow #Hhigh
2620     whd in match (store_value_of_type ?????);
2621     whd in match (fe_to_be_values ??);
2622     normalize nodelta     
2623     normalize in match (size_intsize ?);
2624     whd in match (bytes_of_bitvector ??);             
2625     lapply (vsplit_eq2 ? 8 (3*8) i) * #iA * #iB #Heq_i
2626     <(vsplit_prod … Heq_i) normalize nodelta whd in match (storen ???);
2627      lapply (valid_pointer_to_bestorev_ok m (mk_pointer b zero_offset) (BVByte iA) ?)
2628      [ 1: whd in match (valid_pointer ??); >(Zlt_to_Zltb_true ?? Hvalid) >andb_lsimpl_true
2629           >unfold_low_bound >unfold_high_bound >Hlow >Hhigh
2630           >(Zle_to_Zleb_true … (reflexive_Zle OZ)) normalize nodelta
2631           @Zlt_to_Zltb_true // ]
2632      * #m0 #Hbestorev >Hbestorev normalize nodelta
2633      whd in match (bytes_of_bitvector ??);
2634      lapply (vsplit_eq2 ? 8 (2*8) iB) * #iC * #iD #Heq_iB
2635      <(vsplit_prod … Heq_iB) normalize nodelta
2636      cases (mem_bounds_invariant_after_bestorev … Hbestorev) * * * #Hnext0 #Hblocks0 #_ #_ #_   
2637      lapply (valid_pointer_to_bestorev_ok m0
2638                (shift_pointer 2 (mk_pointer b zero_offset) (bitvector_of_nat 2 1))               
2639                (BVByte iC) ?)
2640      [ 1: whd in match (valid_pointer ??); >Hnext0 >(Zlt_to_Zltb_true ?? Hvalid) >andb_lsimpl_true
2641           cases (Hblocks0 b) #HA #HB
2642           >unfold_low_bound >unfold_high_bound >HA >HB >Hlow >Hhigh normalize nodelta
2643           @Zlt_to_Zltb_true normalize // ]
2644      * #m1 #Hbestorev1 whd in ⊢ (??(λ_.??(???%)?)); whd in match (storen ???);
2645      normalize in match (shift_pointer 2 (mk_pointer b zero_offset) (bitvector_of_nat 2 1));
2646      >Hbestorev1 normalize nodelta
2647      lapply (vsplit_eq2 ? 8 (1*8) iD) * #iE * #iF #Heq_iD
2648      whd in match (bytes_of_bitvector ??);
2649      <(vsplit_prod … Heq_iD) normalize nodelta
2650      whd in ⊢ (??(λ_.??(???%)?));
2651      whd in match (storen ???);
2652      cases (mem_bounds_invariant_after_bestorev … Hbestorev1) * * * #Hnext1 #Hblocks1 #_ #_ #_
2653      lapply (valid_pointer_to_bestorev_ok m1
2654                (shift_pointer 2 (mk_pointer b (mk_offset
2655                   [[ false; false; false; false; false; false; false; false; false; false;
2656                      false; false; false; false; false; true ]]))
2657                (bitvector_of_nat 2 1))
2658                (BVByte iE) ?)
2659      [ 1: normalize in match (shift_pointer ???); whd in match (valid_pointer ??);
2660           >Hnext1 >Hnext0 >(Zlt_to_Zltb_true ?? Hvalid)
2661           >andb_lsimpl_true cases (Hblocks1 b) #HA #HB cases (Hblocks0 b) #HC #HD
2662           >unfold_low_bound >unfold_high_bound >HA >HB >HC >HD >Hlow >Hhigh normalize nodelta
2663           @Zlt_to_Zltb_true normalize // ]
2664      * #m2 #Hbestorev2 >Hbestorev2 normalize nodelta
2665      whd in match (bytes_of_bitvector ??);
2666      lapply (vsplit_eq2 ? 8 (0*8) iF) * #iG * #iH #Heq_iF
2667      <(vsplit_prod … Heq_iF) normalize nodelta
2668      >(BitVector_O … iH) whd in ⊢ (??(λ_.??(???%)?));
2669      whd in match (storen ???);     
2670      cases (mem_bounds_invariant_after_bestorev … Hbestorev2) * * * #Hnext2 #Hblocks2 #_ #_ #_
2671      lapply (valid_pointer_to_bestorev_ok m2
2672                (shift_pointer 2 (shift_pointer 2 (mk_pointer b (mk_offset
2673                   [[ false; false; false; false; false; false; false; false; false; false;
2674                      false; false; false; false; false; true ]]))
2675                (bitvector_of_nat 2 1)) (bitvector_of_nat 2 1))
2676                (BVByte iG) ?)
2677      [ 1: normalize in match (shift_pointer ???); whd in match (valid_pointer ??);
2678           >Hnext2 >Hnext1 >Hnext0 >(Zlt_to_Zltb_true ?? Hvalid)
2679           >andb_lsimpl_true cases (Hblocks2 b) #HA #HB cases (Hblocks1 b) #HC #HD cases (Hblocks0 b) #HE #HF
2680           >unfold_low_bound >unfold_high_bound >HA >HB >HC >HD >HE >HF >Hlow >Hhigh normalize nodelta
2681           @Zlt_to_Zltb_true normalize // ]         
2682      * #m3 #Hbestorev3 >Hbestorev3 normalize nodelta %{m3} @refl
2683] qed.           
2684
2685
2686(* Main theorem.
2687   9th November 2012
2688   We decided to interrupt the development of this particular proof. What follows is a description of what
2689   has to be done in order to finish it.
2690   
2691   What has been done up to now is the simulation proof for all "easy" cases, that do not interact with the
2692   switch removal per se, plus a bit of switch. This still implies propagating the memory extension through
2693   each statement (except switch), as well as various invariants that are needed for the switch case.
2694
2695   The proof for the switch case has been started. Here is how I picture the simulation proof.
2696   The simulation proof must be broken down in several steps. The source statement executes as this for the first step :
2697
2698   mem, env, k
2699   -----------------------------------------------------
2700   switch(e) case_list ===>
2701      e ⇓ Vint i,
2702      case_list' ← select_switch i case_list;
2703   Result = State  (seq_of_labeled_statement case_list') (Kswitch k) env mem
2704     
2705   The resulting statement executes like this.
2706   
2707   mem ⊕ writeable, env ⊕ ext, k'
2708   fresh ∈ dom(ext)
2709   ext(fresh) ∈ writeable
2710   -----------------------------------------------------
2711   fresh = e;
2712   if(e == case0) {       ---
2713     substatement0;         |
2714     goto next0;            |         
2715   } else { };              |
2716   if(e == case1) {         |-  = converted_cases
2717     label next0:           |
2718     substatement1;         |
2719     goto next1;            |
2720   } else { };            ---
2721        ... ===>   
2722   Result = State (fresh = e) (Kseq converted_cases k) (env ⊕ ext) (mem ⊕ writeable)
2723           ===>
2724        fresh ⇓ Loc l;
2725        e ⇓ Vint i;
2726        m' → store_value_of_type' (typeof a1) m l (Vint i)
2727   Result = State Sskip (Kseq converted_cases k) (env ⊕ ext) (m' ⊕ writeable)
2728          ===>
2729   Result = State converted_cases k (env ⊕ ext) (m' ⊕ writeable)
2730   This has been done. But this state is still not equivalent with the source one.
2731   TODO 1: we must prove that after a finite number of Ssequence in [converted_cases], we
2732           stumble upon a "if(e == casen) { blahblah } else {}; foo" that corresponds to "(seq_of_labeled_statement case_list')"
2733           (remember that "case_list'" has been truncated to the case corresponding to "i").
2734   TODO 2: the resulting pair of states will not be in the standard simulation relation currently defined in
2735            [switch_state_sim]. We must come up with an additional set of relations with enough informations
2736            to handle the gotos :
2737            1. the gotos from one if to the other avoiding the execution of conditions
2738            2. most importantly, the gotos into which "break"s have been converted !
2739            This particular subset of the simulation will need some equations allowing to prove that
2740            the current continuation actually contains a label corresponding to the break.
2741            Note that when encountering e.g. a while loop inside a converted case, breaks should stop
2742            beeing converted to gotos and we should go to the 'standard' simulation relation.
2743   TODO 3: some standard cases remain after that, nothing special (halt case ...).
2744   
2745   This should be about it. TODO 1 and 2 will probably require some form of induction over switch cases ...
2746*)
2747
2748theorem switch_removal_correction :
2749  ∀ge,ge'.
2750  switch_removal_globals ? fundef_switch_removal ge ge' →
2751  ∀s1,s1',tr,s2.
2752  switch_state_sim ge s1 s1' →
2753  exec_step ge s1 = Value … 〈tr,s2〉 → 
2754  ∃n. after_n_steps (S n) … clight_exec ge' s1' (λ_. true)
2755  (λtr',s2'. tr = tr' ∧ switch_state_sim ge' s2 s2').
2756#ge #ge' #Hrelated #s1 #s1' #tr #s2 #Hsim_state
2757inversion Hsim_state
2758[ 1: (* regular state *)
2759  #sss_statement #sss_lu #sss_lu_fresh #sss_func #sss_func_tr #sss_new_vars
2760  #sss_func_hyp #sss_m #sss_m_ext #sss_env #sss_env_ext #sss_k #sss_k_ext #sss_writeable #sss_mem_hyp
2761  #sss_env_hyp #sss_new_alloc #sss_enclosing_label #sss_writeable_hyp #sss_result_rec #sss_result_hyp
2762  #sss_result #sss_result_proj #sss_incl #sss_k_hyp #Hext_fresh_for_ge
2763  #Hs1_eq #Hs1_eq'
2764  elim (sim_related_globals … ge ge'
2765             sss_env sss_m sss_env_ext sss_m_ext sss_writeable sss_new_vars
2766             sss_mem_hyp Hrelated sss_env_hyp Hext_fresh_for_ge)
2767  #Hsim_expr #Hsim_lvalue #_
2768  (* II. Case analysis on the statement. *)
2769  cases sss_statement in sss_lu_fresh sss_result_hyp;
2770  (* Perform the intros for the statements *)
2771  [ 1: | 2: #lhs #rhs | 3: #retv #func #args | 4: #stm1 #stm2 | 5: #cond #iftrue #iffalse | 6: #cond #body
2772  | 7: #cond #body | 8: #init #cond #step #body | 9,10: | 11: #retval | 12: #cond #switchcases | 13: #lab #body
2773  | 14: #lab | 15: #cost #body ]
2774  #sss_lu_fresh #sss_result_hyp
2775  [ 1: (* Skip statement *)
2776    whd in match (switch_removal ??) in sss_result_hyp; >sss_result_proj <sss_result_hyp
2777    (* III. Case analysis on the continuation. *)
2778    inversion sss_k_hyp normalize nodelta
2779    [ 1: #new_vars #Hnew_vars_eq #Hk #Hk' #_ #Hexec_step %{0} whd whd in ⊢ (??%?);
2780         >(prod_eq_lproj ????? sss_func_hyp)
2781         >fn_return_simplify
2782         whd in match (exec_step ??) in Hexec_step;
2783         (* IV. Case analysis on the return type *)
2784         cases (fn_return sss_func) in Hexec_step;         
2785         [ | #sz #sg | #ptr_ty | #array_ty #array_sz | #domain #codomain
2786         | #structname #fieldspec | #unionname #fieldspec | #id ]
2787         normalize nodelta
2788         whd in ⊢ ((??%?) → ?);
2789         [ 1: #H destruct (H) % try @refl
2790              /3 by sws_returnstate, swc_stop, memext_free_extended_environment, memory_ext_writeable_eq/
2791         | *: #Habsurd destruct (Habsurd) ]
2792    | 2: #s #k #k' #u #s' #new_vars #Hfresh #Hsimcont #Heq_s' #Hincl #_ #Hnew_vars_eq #Hsss_k #Hsss_k_ext #Hsss_k_hyp
2793         #Hexec_step %{0} whd
2794         >(prod_eq_lproj ????? sss_func_hyp)
2795         whd in match (exec_step ??) in Hexec_step; destruct (Hexec_step) @conj try @refl
2796         <sss_func_hyp
2797         lapply (jmeq_to_eq ??? Hnew_vars_eq) #Hnew_vars_eq' destruct (Hnew_vars_eq')
2798         %1{u (refl ? (switch_removal s u))} try assumption try @refl         
2799         #id #Hmem lapply (Hext_fresh_for_ge id Hmem) #Hfind <(rg_find_symbol … Hrelated id) @Hfind
2800    | 3: #cond #body #k #k' #fgen #s' #new_vars #Hfresh #Hsimcont #Heq_s' #Hincl #_ #Hnew_vars_eq #Hsss_k #Hsss_k_ext #_
2801         lapply (jmeq_to_eq ??? Hnew_vars_eq) #Hnew_vars_eq' destruct (Hnew_vars_eq')
2802         #Hexec_step %{0} whd whd in Hexec_step;
2803         >(prod_eq_lproj ????? sss_func_hyp)
2804         whd in match (exec_step ??) in Hexec_step; destruct (Hexec_step) @conj try @refl         
2805         %1{ ((switch_removal (Swhile cond body) fgen))} try assumption try @refl
2806         [ 1: <sss_func_hyp @refl
2807         | 2: destruct normalize cases (switch_removal ??) * #body' #fvs' #u' @refl
2808         | 3: whd in match (switch_removal ??);
2809              cases (switch_removal body fgen) in Hincl; * #body' #fvs' #fgen' normalize nodelta #H @H
2810         | 4: #id #Hmem <(rg_find_symbol … Hrelated) @Hext_fresh_for_ge @Hmem ]
2811    | 4: #cond #body #k #k' #u #s' #new_vars #Hfresh #Hsimcont #Heq_s' #Hincl #_ #Hnew_vars_eq #Hsss_k #Hsss_k_ext #_
2812         lapply (jmeq_to_eq ??? Hnew_vars_eq) #Hnew_vars_eq' destruct (Hnew_vars_eq')   
2813         #Hexec_step %{0} whd whd in Hexec_step:(??%?) ⊢ (??%?);
2814         cases (bindIO_inversion ??????? Hexec_step) #x1 * #Hexec
2815         >(Hsim_expr … Hexec)
2816         >bindIO_Value cases (exec_bool_of_val ??)
2817         [ 2: #err normalize in ⊢ (% → ?); #Habsurd destruct (Habsurd) ]
2818         #b whd in match (m_bind ?????); whd in match (m_bind ?????);
2819         cases b normalize nodelta #H whd in H:(??%%) ⊢ %; destruct (H)
2820         try @conj try @refl
2821         [ 1: %{u … (switch_removal (Sdowhile cond body) u)} try assumption try //
2822              [ 1: destruct normalize cases (switch_removal body u) * #body' #fvs' #u' @refl
2823              | 2: whd in match (switch_removal ??);
2824                   cases (switch_removal body u) in Hincl; * #body' #fvs' #u' normalize nodelta #H @H
2825              | 3: #id #Hmem <(rg_find_symbol … Hrelated) @Hext_fresh_for_ge @Hmem ]
2826         | 2: %{u … (switch_removal Sskip u) } try assumption try //
2827              [ 1: @(fresh_for_Sskip … Hfresh)
2828              | 2: #id #Hmem <(rg_find_symbol … Hrelated) @Hext_fresh_for_ge @Hmem ] ]
2829    | 5: #cond #stmt1 #stmt2 #k #k' #u #s' #new_vars #Hfresh #Hsimcont #Heq_s' #Hincl #_
2830         #Hnew_vars_eq #Hsss_k #Hsss_k_ext #_
2831         lapply (jmeq_to_eq ??? Hnew_vars_eq) #Hnew_vars_eq' destruct (Hnew_vars_eq')
2832         #Hexec_step %{0} whd whd in Hresult:(??%?) Hexec_step:(??%?); destruct (Hexec_step)
2833         @conj try @refl
2834         %{u … new_vars … sss_mem_hyp … (switch_removal (Sfor Sskip cond stmt1 stmt2) u)} try // try assumption
2835         #id #Hmem <(rg_find_symbol … Hrelated) @Hext_fresh_for_ge @Hmem
2836    | 6: #cond #stmt1 #stmt2 #k #k' #u #result1 #result2 #new_vars
2837         #Hfresh #Hsimcont #Hresult1 #Hresult2 #Hincl #_ #Hnew_vars_eq #Hsss_k #Hsss_k_ext #_
2838         lapply (jmeq_to_eq ??? Hnew_vars_eq) #Hnew_vars_eq' destruct (Hnew_vars_eq')
2839         #Hexec %{0} whd in Hexec:(??%?) ⊢ %; destruct (Hexec) @conj try @refl
2840         %1{u … new_vars … sss_writeable (switch_removal stmt1 u)} try assumption try //
2841         [ 1: lapply (fresh_to_substatements … Hfresh) normalize * * //
2842         | 2: whd in match (switch_removal ??) in Hincl;
2843              cases (switch_removal stmt1 u) in Hincl; * #stmt1' #fvs1' #u' normalize nodelta
2844              cases (switch_removal stmt2 u') * #stmt2' #fvs2' #u'' normalize nodelta
2845              whd in match (ret_vars ??); /2 by All_append_l/
2846         | 3: @(swc_for3 … u) //
2847         | 4: #id #Hmem <(rg_find_symbol … Hrelated) @Hext_fresh_for_ge @Hmem ]
2848    | 7: #cond #stmt1 #stmt2 #k #k' #u #result1 #result2 #new_vars
2849         #Hfresh #Hsimcont #Hresult1 #Hresult2 #Hincl #_ #Hnew_vars_eq #Hsss_k #Hsss_k_ext #_
2850         lapply (jmeq_to_eq ??? Hnew_vars_eq) #Hnew_vars_eq' destruct (Hnew_vars_eq')
2851         #Hexec %{0} whd in Hexec:(??%?) ⊢ %; destruct (Hexec) @conj try @refl
2852         %1{u … new_vars … sss_writeable … (switch_removal (Sfor Sskip cond stmt1 stmt2) u)}
2853         try // try assumption
2854         [ 1: whd in match (switch_removal ??) in ⊢ (??%%); destruct normalize
2855              cases (switch_removal stmt1 u) * #stmt1' #fvs1' #u' normalize
2856              cases (switch_removal stmt2 u') * #stmt2' #fvs2' #u'' @refl
2857         | 2: #id #Hmem <(rg_find_symbol … Hrelated) @Hext_fresh_for_ge @Hmem ]
2858    | 8: #k #k' #new_vars #Hsimcont #_ #Hnew_vars_eq #Hsss_k #Hsss_k_ext #_
2859         lapply (jmeq_to_eq ??? Hnew_vars_eq) #Hnew_vars_eq' destruct (Hnew_vars_eq')
2860         #Hexec %{0} whd in Hexec:(??%?) ⊢ %; destruct (Hexec) @conj try @refl
2861         %1{sss_lu … new_vars … sss_writeable} try // try assumption
2862         [ 1: destruct (sss_result_hyp) @refl
2863         | 2: #id #Hmem <(rg_find_symbol … Hrelated) @Hext_fresh_for_ge @Hmem ]
2864    | 9: #en #en' #r #f #k #k' #old_vars #new_vars #Hsimcont #Hnew_vars_eq #Hdisjoint_k #_
2865         #Hnew_vars_eq #Hsss_k #Hsss_k_ext #_
2866         lapply (jmeq_to_eq ??? Hnew_vars_eq) #Hnew_vars_eq' destruct (Hnew_vars_eq')
2867         #Hexec %{0} whd in Hexec:(??%?) ⊢ %; whd in ⊢ (??%?);
2868         >(prod_eq_lproj ????? sss_func_hyp) >fn_return_simplify
2869         cases (fn_return sss_func) in Hexec; normalize nodelta
2870         [ | #sz #sg | #ptr_ty | #array_ty #array_sz | #domain #codomain
2871         | #structname #fieldspec | #unionname #fieldspec | #id ]         
2872(*         [ 1: | 2: #sz #sg | 3: #fsz | 4: #ptr_ty | 5: #array_ty #array_sz | 6: #domain #codomain
2873         | 7: #structname #fieldspec | 8: #unionname #fieldspec | 9: #id ] *)
2874         #Hexec whd in Hexec:(??%?); destruct (Hexec) whd @conj try @refl
2875         /3 by sws_returnstate, swc_call, memext_free_extended_environment/
2876    ]
2877  | 2: (* Assign statement *)
2878       lapply (exec_lvalue_sim_aux … Hsim_lvalue) #Hsim
2879       #Hexec %{0} whd in sss_result_hyp:(??%?);
2880       cases (bindIO_inversion ??????? Hexec) #xl * #Heq_lhs #Hexec_lhs
2881       cases (bindIO_inversion ??????? Hexec_lhs) #xr * #Heq_rhs #Hexec_rhs -Hexec_lhs
2882       cases (bindIO_inversion ??????? Hexec_rhs) #m' * #Heq_store #Hexec_store -Hexec_rhs
2883       whd whd in Hexec_store:(??%%) ⊢ (??%?); >sss_result_proj <sss_result_hyp normalize nodelta
2884       >(Hsim … Heq_lhs) whd in match (m_bind ?????);
2885       >(Hsim_expr … Heq_rhs) >bindIO_Value
2886       lapply (memext_store_value_of_type' sss_m sss_m_ext m' sss_writeable (typeof lhs) (\fst  xl) (\fst  xr) sss_mem_hyp ?)
2887       [ 1: cases (store_value_of_type' ????) in Heq_store;
2888            [ 1: normalize #Habsurd destruct (Habsurd)
2889            | 2: #m normalize #Heq destruct (Heq) @refl ] ]
2890       * #m_ext' * #Heq_store' #Hnew_ext >Heq_store' whd in match (m_bind ?????);
2891       whd destruct @conj try @refl
2892       %1{sss_lu … sss_new_vars … sss_writeable … (switch_removal Sskip  sss_lu) }
2893       try // try assumption
2894       [ 1: @(fresh_for_Sskip … sss_lu_fresh)
2895       | 3: #id #Hmem <(rg_find_symbol … Hrelated) @Hext_fresh_for_ge @Hmem
2896       | 2: #v #Hmem #vb #Hlookup lapply (sss_new_alloc v Hmem vb Hlookup) * * #Hvb #Hlow #Hhigh           
2897            cut (store_value_of_type' (typeof lhs) sss_m (\fst  xl) (\fst  xr) = Some ? m')
2898            [ cases (store_value_of_type' (typeof lhs) sss_m (\fst  xl) (\fst  xr)) in Heq_store;
2899              [ whd in ⊢ ((??%%) → ?); #Habsurd destruct
2900              | #m0 whd in ⊢ ((??%%) → ?); #Heq destruct (Heq) @refl ] ]             
2901            #Hstore lapply (mem_bounds_after_store_value_of_type' … Heq_store') *
2902            #HA #HB cases (HB vb) #Hlow' #Hhigh' @conj try @conj
2903            [ 2: >Hlow' in Hlow; //
2904            | 3: >Hhigh' in Hhigh; //
2905            | 1: whd >HA @Hvb ] ]
2906  | 3: (* Call statement *)
2907       #Hexec %{0} whd in sss_result_hyp:(??%?); destruct (sss_result_hyp)
2908       whd whd in ⊢ (??%?); >sss_result_proj normalize nodelta
2909       whd in Hexec:(??%?);
2910       cases (bindIO_inversion ??????? Hexec) #xfunc * #Heq_func #Hexec_func
2911       cases (bindIO_inversion ??????? Hexec_func) #xargs * #Heq_args #Hexec_args
2912       cases (bindIO_inversion ??????? Hexec_args) #called_fundef * #Heq_fundef #Hexec_typeeq
2913       cases (bindIO_inversion ??????? Hexec_typeeq) #Htype_eq * #Heq_assert #Hexec_ret
2914       >(Hsim_expr … Heq_func) whd in match (m_bind ?????);
2915       >(exec_expr_sim_to_exec_exprlist … Hsim_expr … Heq_args)
2916       whd in ⊢ (??%?);
2917       >(rg_find_funct … Hrelated … (opt_to_io_Value … Heq_fundef))
2918       whd in ⊢ (??%?); <fundef_type_simplify >Heq_assert
2919       whd in ⊢ (??%?); -Hexec -Hexec_func -Hexec_args -Hexec_typeeq lapply Hexec_ret -Hexec_ret
2920       @(option_ind … retv) normalize nodelta
2921       [ 1: whd in ⊢ ((??%%) → (??%%)); #Heq whd destruct (Heq) @conj try @refl
2922            %2{sss_writeable … sss_mem_hyp}
2923            cases called_fundef
2924            [ 2: #id #tl #ty @I
2925            | 1: #called_function whd
2926                 cut (sss_func_tr = \fst (function_switch_removal sss_func))
2927                 [ 1: <sss_func_hyp @refl ] #H >H -H
2928                 cut (sss_new_vars = \snd (function_switch_removal sss_func))
2929                 [ 1: <sss_func_hyp @refl ] #H >H -H
2930                 @(swc_call … sss_k_hyp) try assumption
2931                 <sss_func_hyp @refl ]
2932       | 2: #ret_expr #Hexec_ret_expr
2933            cases (bindIO_inversion ??????? Hexec_ret_expr) #xret * #Heq_ret
2934            whd in ⊢ ((??%%) → (??%%)); #H destruct (H)
2935            >(exec_lvalue_sim_aux … Hsim_lvalue … Heq_ret)
2936            whd in ⊢ (??%?); whd @conj try @refl
2937            cut (sss_func_tr = \fst (function_switch_removal sss_func))
2938            [ 1: <sss_func_hyp @refl ] #H >H -H
2939            @(sws_callstate … sss_writeable … sss_mem_hyp)
2940            cases called_fundef
2941            [ 2: #id #tl #ty @I
2942            | 1: #called_function whd
2943                 cut (sss_func_tr = \fst (function_switch_removal sss_func))
2944                 [ 1: <sss_func_hyp @refl ] #H >H -H
2945                 cut (sss_new_vars = \snd (function_switch_removal sss_func))
2946                 [ 1: <sss_func_hyp @refl ] #H >H -H
2947                 @(swc_call … sss_k_hyp) try assumption
2948                 <sss_func_hyp @refl ] ]
2949  | 4: (* Sequence statement *)
2950       #Hexec %{0} whd in sss_result_hyp:(??%?); whd whd in Hexec:(??%?) ⊢ (??%?); destruct (Hexec)
2951       >sss_result_proj <sss_result_hyp
2952       cases (switch_removal_elim stm1 sss_lu) #stm1' * #fvs1' * #u' #HeqA >HeqA normalize nodelta
2953       cases (switch_removal_elim stm2 u') #stm2' * #fvs2' * #u'' #HeqB >HeqB normalize nodelta
2954       normalize @conj try @refl %1{sss_lu … sss_func_hyp … sss_writeable … sss_mem_hyp … HeqA}
2955       try // try assumption
2956       [ 1: lapply (fresh_to_substatements … sss_lu_fresh) normalize * //
2957       | 2: lapply sss_incl <sss_result_hyp >HeqA normalize nodelta >HeqB normalize nodelta
2958            /2 by All_append_l/
2959       | 4: #id #Hmem <(rg_find_symbol … Hrelated) @Hext_fresh_for_ge @Hmem ]
2960       @(swc_seq … u') try //
2961       [ 2: >HeqB @refl
2962       | 1: lapply (fresh_to_substatements … sss_lu_fresh) normalize * #_ @fresher_for_univ
2963            lapply (switch_removal_fte stm1 sss_lu) >HeqA #H @H
2964       | 3: lapply sss_incl <sss_result_hyp >HeqA normalize nodelta >HeqB normalize nodelta
2965            /2 by All_append_r/
2966       ]
2967  | 5: (* If-then-else *)
2968       #Hexec %{0} whd in sss_result_hyp:(??%?) Hexec:(??%?); >sss_result_proj <sss_result_hyp
2969       cases (switch_removal_elim iftrue sss_lu) #iftrue' * #fvs1' * #u' #HeqA >HeqA normalize nodelta
2970       cases (switch_removal_elim iffalse u') #iffalse' * #fvs2' * #u'' #HeqB >HeqB normalize nodelta
2971       whd whd in ⊢ (??%?);
2972       cases (bindIO_inversion ??????? Hexec) #condres * #Heq_cond #Hexec_cond
2973       cases (bindIO_inversion ??????? Hexec_cond) #b * #Heq_bool #Hresult
2974       whd in Hresult:(??%%); destruct (Hresult)
2975       >(Hsim_expr … Heq_cond) >bindIO_Value
2976       >Heq_bool whd in match (m_bind ?????); whd @conj try @refl
2977       cases b normalize nodelta
2978       [ 1: %1{sss_lu … sss_func_hyp … sss_writeable … sss_mem_hyp … HeqA} try assumption try //
2979             [ 1: cases (fresh_to_substatements … sss_lu_fresh) normalize //
2980             | 2: lapply sss_incl <sss_result_hyp >HeqA normalize nodelta >HeqB normalize nodelta
2981                  /2 by All_append_l/
2982             | 3: #id #Hmem <(rg_find_symbol … Hrelated) @Hext_fresh_for_ge @Hmem ]
2983       | 2: %1{u' … sss_func_hyp … sss_writeable … sss_mem_hyp … HeqB} try assumption try //
2984             [ 1: cases (fresh_to_substatements … sss_lu_fresh) normalize #_
2985                   @fresher_for_univ lapply (switch_removal_fte iftrue sss_lu) >HeqA #H @H
2986             | 2: lapply sss_incl <sss_result_hyp >HeqA normalize nodelta >HeqB normalize nodelta
2987                  /2 by All_append_r/                   
2988             | 3: #id #Hmem <(rg_find_symbol … Hrelated) @Hext_fresh_for_ge @Hmem ] ]
2989  | 6: (* While loop *)
2990       #Hexec %{0} whd in sss_result_hyp:(??%?) Hexec:(??%?); >sss_result_proj <sss_result_hyp
2991       >sss_result_proj <sss_result_hyp whd
2992       cases (bindIO_inversion ??????? Hexec) #condres * #Heq_cond #Hexec_cond
2993       cases (bindIO_inversion ??????? Hexec_cond) #b * #Heq_bool whd in ⊢ ((??%%) → ?);
2994       cases (switch_removal_elim body sss_lu) #body' * #fvs1' * #u' #HeqA >HeqA normalize nodelta
2995       whd in ⊢ (? → (??%?));
2996       >(Hsim_expr … Heq_cond) >bindIO_Value >Heq_bool
2997       whd in match (m_bind ?????); cases b normalize nodelta #Hresult destruct (Hresult)
2998       whd @conj try @refl
2999       [ 1: %1{sss_lu … sss_func_hyp … sss_writeable … sss_mem_hyp … HeqA} try assumption try //
3000             [ 1: cases (fresh_to_substatements … sss_lu_fresh) normalize //
3001             | 2: lapply sss_incl <sss_result_hyp >HeqA normalize nodelta #H @H
3002             | 4: #id #Hmem <(rg_find_symbol … Hrelated) @Hext_fresh_for_ge @Hmem
3003             | 3: @(swc_while … sss_lu) try //
3004                  [ 1: >HeqA @refl
3005                  | 2: lapply sss_incl <sss_result_hyp >HeqA normalize nodelta #H @H ]
3006             ]
3007       | 2: %{… sss_func_hyp … (switch_removal Sskip u')} try assumption try //
3008            [ 1: lapply (switch_removal_fte body sss_lu) >HeqA #Hfte whd in match (ret_u ??) in Hfte;
3009                 @(fresher_for_univ … Hfte) @(fresh_for_Sskip … sss_lu_fresh)
3010            | 2: #id #Hmem <(rg_find_symbol … Hrelated) @Hext_fresh_for_ge @Hmem ] ]
3011  | 7: (* do while loop *)
3012       #Hexec %{0} whd in sss_result_hyp:(??%?) Hexec:(??%?); >sss_result_proj <sss_result_hyp
3013       >sss_result_proj <sss_result_hyp whd destruct (Hexec) whd in ⊢ (??%?);
3014       cases (switch_removal_elim body sss_lu) #body' * #fvs1' * #u' #HeqA >HeqA normalize nodelta
3015       whd @conj try @refl
3016       %1{sss_lu … sss_func_hyp … (switch_removal body sss_lu) }
3017       try assumption try //
3018       [ 1:  lapply (fresh_to_substatements … sss_lu_fresh) normalize * //
3019       | 2: >HeqA @refl
3020       | 3: lapply sss_incl <sss_result_hyp >HeqA normalize nodelta #H @H
3021       | 5: #id #Hmem <(rg_find_symbol … Hrelated) @Hext_fresh_for_ge @Hmem
3022       | 4: @(swc_dowhile … sss_lu) try assumption try //
3023            [ 1: >HeqA @refl
3024            | 2: lapply sss_incl <sss_result_hyp >HeqA normalize nodelta #H @H           
3025            ] ]       
3026  | 8: (* for loop *)
3027       #Hexec %{0} whd in sss_result_hyp:(??%?) Hexec:(??%?); >sss_result_proj <sss_result_hyp
3028       >sss_result_proj <sss_result_hyp whd destruct (Hexec) whd in ⊢ (??%?);
3029       cases (switch_removal_elim init sss_lu) #init' * #fvs1' * #u' #HeqA >HeqA normalize nodelta
3030       cases (switch_removal_elim step u') #step' * #fvs2' * #u'' #HeqB >HeqB normalize nodelta
3031       cases (switch_removal_elim body u'') #body' * #fvs3' * #u''' #HeqC >HeqC normalize nodelta
3032       lapply Hexec
3033       @(match is_Sskip init with
3034       [ inl Heq ⇒ ?
3035       | inr Hneq ⇒ ?
3036       ]) normalize nodelta
3037       [ 2: lapply (simplify_is_not_skip … Hneq sss_lu) >HeqA * #pf
3038            whd in match (ret_st ??) in ⊢ ((??%%) → ?); #Hneq >Hneq normalize nodelta
3039            #Hexec' whd in Hexec':(??%%); destruct (Hexec') whd @conj try @refl
3040            %1{sss_lu … sss_func_hyp (switch_removal init sss_lu)} try assumption try //
3041            [ 1: lapply (fresh_to_substatements … sss_lu_fresh) normalize * * * //
3042            | 2: >HeqA @refl
3043            | 3: lapply sss_incl <sss_result_hyp >HeqA normalize nodelta
3044                 >HeqB normalize nodelta >HeqC normalize nodelta
3045                 /2 by All_append_l/
3046            | 4: @(swc_for1 … u') try assumption try //
3047                 [ 1: lapply (fresh_to_substatements … sss_lu_fresh) * * * #HW #HX #HY #HZ
3048                      @for_fresh_lift
3049                      [ 1: @(fresher_for_univ … HY)
3050                      | 2: @(fresher_for_univ … HZ)
3051                      | 3: @(fresher_for_univ … HX) ]
3052                      lapply (switch_removal_fte init sss_lu) >HeqA #Hs @Hs
3053                 | 2: normalize >HeqB normalize nodelta >HeqC @refl
3054                 | 3: lapply sss_incl <sss_result_hyp
3055                      whd in match (ret_vars ??) in ⊢ (% → %);
3056                      whd in match (switch_removal ??) in ⊢ (% → %);
3057                      >HeqA normalize nodelta >HeqB normalize nodelta >HeqC
3058                      normalize nodelta #H /2 by All_append_r/
3059                  ] ]
3060       | 1: -Hexec #Hexec' cases (bindIO_inversion ??????? Hexec') #condres * #Heq_cond #Hexec_cond
3061            cases (bindIO_inversion ??????? Hexec_cond) #b * #Heq_bool
3062            destruct (Heq) normalize in HeqA; lapply HeqA #HeqA' destruct (HeqA')
3063            normalize nodelta
3064            >(Hsim_expr … Heq_cond) whd in ⊢ ((??%?) → ?); #Hexec'
3065            whd in match (m_bind ?????); >Heq_bool
3066            cases b in Hexec'; normalize nodelta whd in match (bindIO ??????);
3067            normalize #Hexec'' destruct (Hexec'') @conj try @refl
3068            [ 1: %1{u'' … sss_func_hyp (switch_removal body u'')} try assumption try //
3069                 [ 1: lapply (fresh_to_substatements … sss_lu_fresh) * * * #_ #_ #_
3070                      @fresher_for_univ lapply (switch_removal_fte step u') >HeqB
3071                      #H @H
3072                 | 2: >HeqC @refl
3073                 | 3: lapply sss_incl <sss_result_hyp
3074                      whd in match (ret_vars ??) in ⊢ (% → %);
3075                      whd in match (switch_removal ??) in ⊢ (% → %); normalize nodelta
3076                      >HeqB normalize nodelta >HeqC normalize nodelta
3077                      /2 by All_append_r/
3078                 | 4: @(swc_for2 … u') try assumption
3079                      [ 1: >HeqB @refl
3080                      | 2: >HeqB >HeqC @refl
3081                      | 3: lapply sss_incl <sss_result_hyp
3082                           whd in match (ret_vars ??) in ⊢ (% → %);
3083                           whd in match (switch_removal ??) in ⊢ (% → %); normalize nodelta
3084                           >HeqB normalize nodelta >HeqC normalize nodelta #H @H
3085                      ]
3086                 ]
3087            | 2: %1{u' … sss_func_hyp … (switch_removal Sskip u')} try assumption try //
3088                 [ 1: @(fresh_for_Sskip … sss_lu_fresh) ] ] ]
3089        #id #Hmem <(rg_find_symbol … Hrelated) @Hext_fresh_for_ge @Hmem
3090  | 9: (* break *)
3091       (* sss_enclosing_label TODO : switch case *)
3092       #Hexec %{0} whd whd in sss_result_hyp:(??%?); >sss_result_proj <sss_result_hyp normalize nodelta
3093       lapply Hexec -Hexec
3094       inversion sss_k_hyp
3095       [ 1: #new_vars #Hv #Hk #Hk' #_ whd in ⊢ ((??%?) → (??%?)); #Habsurd destruct (Habsurd)
3096       | 2: #sk #sss_k' #sss_k_ext' #uk #sk' #new_vars #Hfresh_suk #Hsimk' #Hsk_eq' #Hincl #_ #Hnew_vars_eq
3097            #Hk #Hk' #_ whd in ⊢ ((??%?) → (??%?)); #Heq destruct (Heq) whd @conj try @refl
3098            destruct
3099            %1{sss_lu … (switch_removal Sbreak sss_lu)} try assumption try //
3100       | 3,4: #e #sk #sss_k' #sss_k_ext' #uk #sk' #new_vars #Hfresh_suk #Hsimk' #Hsk_eq' #Hincl #_
3101            #Hnew_vars #Hk #Hk' #_ whd in ⊢ ((??%?) → (??%?)); #Heq destruct (Heq) whd @conj try @refl
3102            destruct
3103            %1{sss_lu … (switch_removal Sskip sss_lu)} try assumption try //
3104       | 5: #e #s1k #s2k #sss_k' #sss_k_ext' #uk #sk' #new_vars #Hfresh_suk #Hsimk' #Hsk_eq' #Hincl #_
3105            #Hnew_vars #Hk #Hk' #_ whd in ⊢ ((??%?) → (??%?)); #Heq destruct (Heq) whd @conj try @refl
3106            destruct
3107            %1{sss_lu … (switch_removal Sbreak sss_lu)} try assumption try //
3108       | 6,7: #e #s1k #s2k #sss_k' #sss_k_ext' #uk #result1 #result2 #new_vars #Hfresh_suk #Hsimk'
3109            #Hres1 #Hres2 #Hincl #_ #Hnew_vars
3110            #Hk #Hk' #_ whd in ⊢ ((??%?) → (??%?)); #Heq destruct (Heq) whd @conj try @refl
3111            destruct
3112            %1{sss_lu … (switch_removal Sskip sss_lu)} try assumption try //
3113       | 8: #sss_k' #sss_k_ext' #new_vars #Hsimk' #_ #Hnew_vars #Hk #Hk' #_ whd in ⊢ ((??%?) → (??%?));
3114            #Heq destruct (Heq) whd @conj try @refl destruct
3115            %1{sss_lu … (switch_removal Sskip sss_lu)} try assumption try //
3116       | 9: #enk #enk' #rk #fk #sss_k' #sss_k_ext' #old_vars #new_vars #Hsimk' #Hold #Hdisjoint #_
3117            #Hnew_vars #Hk #Hk' #_ whd in ⊢ ((??%?) → (??%?));
3118            #Heq destruct (Heq) ]
3119       #id #Hmem <(rg_find_symbol … Hrelated) @Hext_fresh_for_ge @Hmem
3120  | 10: (* continue *)
3121       #Hexec %{0} whd whd in sss_result_hyp:(??%?); >sss_result_proj <sss_result_hyp normalize nodelta
3122       lapply Hexec -Hexec
3123       inversion sss_k_hyp
3124       [ 1: #new_vars #Hv #Hk #Hk' #_ whd in ⊢ ((??%?) → (??%?)); #Habsurd destruct (Habsurd)
3125       | 2: #sk #sss_k' #sss_k_ext' #uk #sk' #new_vars #Hfresh_suk #Hsimk' #Hsk_eq' #Hincl #_ #Hnew_vars_eq
3126            #Hk #Hk' #_ whd in ⊢ ((??%?) → (??%?)); #Heq destruct (Heq) whd @conj try @refl
3127            destruct
3128            %1{sss_lu … (switch_removal Scontinue sss_lu)} try assumption try //
3129       | 3: #ek #sk #sss_k' #sss_k_ext' #uk #sk' #new_vars #Hfresh_suk #Hsimk' #Hsk_eq' #Hincl #_
3130            #Hnew_vars #Hk #Hk' #_ whd in ⊢ ((??%?) → (??%?)); #Heq destruct (Heq) whd @conj try @refl
3131            destruct
3132            %1{uk … (switch_removal (Swhile ek sk) uk)} try assumption try //
3133            [ 1: normalize cases (switch_removal sk uk) * #sk' #fvs' #uk' @refl
3134            | 2: whd in match (switch_removal ??); lapply Hincl
3135                 cases (switch_removal sk uk) * #body' #fvs' #uk'
3136                 /2 by All_append_r/ ]                 
3137       | 4: #ek #sk #sss_k' #sss_k_ext' #uk #sk' #new_vars #Hfresh_suk #Hsimk' #Hsk_eq' #Hincl #_
3138            #Hnew_vars_eq #Hk #Hk' #_ whd in ⊢ ((??%?) → (??%?)); #Hexec
3139            cases (bindIO_inversion ??????? Hexec) #condres * #Heq_cond #Hexec_cond
3140            cases (bindIO_inversion ??????? Hexec_cond) #b * #Heq_bool #Hexec_bool
3141            >(Hsim_expr … Heq_cond) >bindIO_Value >Heq_bool whd in match (m_bind ?????);
3142            cases b in Hexec_bool; normalize nodelta whd in ⊢ ((??%?) → ?);
3143            #Heq whd whd in Heq:(??%%); destruct (Heq) @conj try @refl
3144            [ 1: destruct %1{uk … (switch_removal (Sdowhile ek sk) uk)} try assumption try //
3145                 [ 1: normalize cases (switch_removal sk uk) * #body' #fvs' #uk' @refl
3146                 | 2: whd in match (switch_removal ??); lapply Hincl cases (switch_removal sk uk)
3147                      * #body' #fvs' #uk' #H @H
3148                 ]
3149            | 2: destruct %1{uk … (switch_removal Sskip uk)} try assumption try //
3150                 try @(fresh_for_Sskip … Hfresh_suk) ]
3151       | 5: #e #s1k #s2k #sss_k' #sss_k_ext' #uk #sk' #new_vars #Hfresh_suk #Hsimk' #Hsk_eq' #Hincl #_
3152            #Hnew_vars #Hk #Hk' #_ whd in ⊢ ((??%?) → (??%?)); #Heq destruct (Heq) whd @conj try @refl
3153            destruct %1{sss_lu … (switch_removal Scontinue sss_lu)} try assumption try //
3154       | 6,7: #e #s1k #s2k #sss_k' #sss_k_ext' #uk #result1 #result2 #new_vars #Hfresh_suk #Hsimk' #Hres1 #Hres2 #Hincl #_
3155            #Hnew_vars #Hk #Hk' #_ whd in ⊢ ((??%?) → (??%?)); #Heq destruct (Heq) whd @conj try @refl
3156            destruct %1{uk … (switch_removal s1k uk)} try assumption try //
3157            [ 1: cases (fresh_to_substatements … Hfresh_suk) * * //
3158            | 2: lapply Hincl whd in match (ret_vars ??) in ⊢ (% → ?);
3159                 whd in match (switch_removal ??);
3160                 cases (switch_removal s1k uk) * #s1k' #fvs1' #uk' normalize nodelta
3161                 cases (switch_removal s2k uk') * #s2k' #fvs2' #uk'' normalize nodelta
3162                 /2 by All_append_l/
3163            | 3: @(swc_for3 … uk) try assumption try //
3164            ]
3165       | 8: #sss_k' #sss_k_ext' #new_vars #Hsimk #_ #Hnew_vars_eq #Hk #Hk' #_
3166            whd in ⊢ ((??%?) → (??%?)); #Heq destruct (Heq)
3167            whd @conj try @refl destruct
3168            %1{sss_lu … (switch_removal Scontinue sss_lu)} try assumption try //
3169       | 9: #enk #enk' #rk #fk #sss_k' #sss_k_ext' #old_vars #new_vars #Hsimk' #Hold_vars_eq #Hdisjoint
3170             #_ #Hnew_vars_eq #Hk #Hk' #_ whd in ⊢ ((??%?) → (??%?));
3171            #Heq destruct (Heq) ]
3172       #id #Hmem <(rg_find_symbol … Hrelated) @Hext_fresh_for_ge @Hmem
3173  | 11: (* return *)
3174        #Hexec %{0} whd whd in sss_result_hyp:(??%?) Hexec:(??%?); lapply Hexec -Hexec
3175        >sss_result_proj <sss_result_hyp normalize nodelta
3176        cases retval in sss_lu_fresh sss_result_hyp; normalize nodelta
3177        [ 1: #sss_lu_fresh #sss_result_hyp whd in ⊢ (? → (??%?));
3178             >(prod_eq_lproj ????? sss_func_hyp)
3179             >fn_return_simplify
3180             cases (fn_return sss_func) normalize nodelta
3181             [ | #sz #sg | #ptr_ty | #array_ty #array_sz | #domain #codomain
3182             | #structname #fieldspec | #unionname #fieldspec | #id ]
3183             [ 1: whd in ⊢ ((??%%) → ?); #Heq destruct (Heq) whd @conj try @refl
3184                  /3 by sws_returnstate, call_cont_swremoval, memext_free_extended_environment, memory_ext_writeable_eq/
3185             | *: #Habsurd destruct (Habsurd) ]
3186        | 2: #ret_expr #sss_lu_fresh #sss_result_hyp whd in ⊢ (? → (??%?));
3187             >(prod_eq_lproj ????? sss_func_hyp)
3188             >fn_return_simplify
3189             @(match type_eq_dec (fn_return sss_func) Tvoid with
3190               [ inl H ⇒ ?
3191               | inr H ⇒ ? ]) normalize nodelta
3192             [ 1: #Habsurd destruct (Habsurd)
3193             | 2: #Hexec
3194                   cases (bindIO_inversion ??????? Hexec) #retres * #Heq_ret #Hexec_ret
3195                   whd in Hexec_ret:(??%%); destruct (Hexec_ret)
3196                   >(Hsim_expr … Heq_ret) whd in match (m_bind ?????); whd
3197                   @conj try @refl
3198                   /3 by sws_returnstate, call_cont_swremoval, memext_free_extended_environment, memory_ext_writeable_eq/
3199             ] ]
3200  | 12: (* switch ! at long last *)
3201        #Hexec whd in sss_result_hyp:(??%?) Hexec:(??%?); lapply Hexec -Hexec
3202        >sss_result_proj <sss_result_hyp normalize nodelta #Hexec
3203        cases (bindIO_inversion ??????? Hexec) * #condval #condtrace -Hexec
3204        cases condval normalize nodelta
3205        [ 1: * #_ #Habsurd normalize in Habsurd; destruct (Habsurd)
3206        | 3: * #_ #Habsurd normalize in Habsurd; destruct (Habsurd)
3207        | 4: #ptr * #_ #Habsurd normalize in Habsurd; destruct (Habsurd) ]
3208        #sz #i * #Hexec_eq #Heq
3209        cut (∃sg. typeof cond = Tint sz sg) whd in Heq:(??%%); destruct (Heq)
3210        [ 1: cases (typeof cond) in Heq; normalize nodelta
3211             [ | #sz' #sg' | #ptrty | #arrayty #arraysz | #domain #codomain
3212             | #structname #fieldspec | #unionname #fieldspec | #id ]
3213             [ 2: cases (sz_eq_dec ??) normalize nodelta #H
3214                  [ 2: #Habsurd destruct
3215                  | 1: destruct (H) #_ %{sg'} try @refl ]
3216             | *: #Habsurd destruct (Habsurd) ] ]
3217        * #sg #Htypeof_cond >Htypeof_cond in Heq; normalize nodelta >sz_eq_identity normalize nodelta
3218        #Heq whd in Heq:(??%%);
3219        cases (bindIO_inversion ??????? Heq) #switchcases_truncated * #Heq1 #Heq2 -Heq
3220        whd in Heq1:(??%%); whd in Heq2:(??%%);
3221        cut (select_switch sz i switchcases = Some ? switchcases_truncated)
3222        [ 1: cases (select_switch sz i switchcases) in Heq1; normalize nodelta
3223             [ 1: #Habsurd destruct | 2: #ls #Heq destruct (Heq) @refl ] ]
3224        -Heq1 #Heq_select_switch destruct (Heq2)
3225        cases (switch_removal_branches_elim … switchcases sss_lu) #switchcases' * #fvs' * #u' #Hbranch_eq
3226        >Hbranch_eq normalize nodelta
3227        cases (fresh_elim … u') #new * #u'' #Hfresh_eq >Hfresh_eq normalize nodelta
3228        cases (simplify_switch_elim (Expr (Evar new) (Tint sz sg)) switchcases' u'') #simplified * #u'''
3229        #Hswitch_eq >Hswitch_eq normalize nodelta
3230        %{2} whd whd in ⊢ (??%?);
3231        (* A. Execute lhs of assign, i.e. fresh variable that will hold value of condition *)
3232        whd in match (exec_lvalue ????);
3233        (* show that the resulting ident is in the memory extension and that the lookup succeeds *)
3234        >Hbranch_eq in sss_result_hyp; normalize nodelta
3235        >Hfresh_eq normalize nodelta >Hswitch_eq normalize nodelta >Htypeof_cond >Hswitch_eq
3236        normalize nodelta #sss_result_hyp
3237        <sss_result_hyp in sss_incl; whd in match (ret_vars ??); #sss_incl
3238        cases sss_env_hyp *
3239        #Hlookup_new_in_old
3240        #Hlookup_new_in_new
3241        #Hlookup_old
3242        cut (mem_assoc_env new sss_new_vars=true)
3243        [ 1: cases sss_incl #Hmem #_ elim sss_new_vars in Hmem;
3244             [ 1: @False_ind
3245             | 2: * #hdv #hdty #tl #Hind whd in ⊢ (% →  (??%?)); *
3246                  [ 1: #Heq destruct (Heq)
3247                       cases (identifier_eq_i_i … hdv) #Hrefl #Heq >Heq -Heq normalize nodelta
3248                       @refl
3249                  | 2: #Hmem lapply (Hind Hmem) #Hmem_in_tl
3250                  cases (identifier_eq ? new hdv) normalize nodelta
3251                  [ 1: #_ @refl | 2: #_ @Hmem_in_tl ] ] ] ]
3252       #Hnew_in_new_vars
3253       lapply (Hlookup_new_in_new new Hnew_in_new_vars)                 
3254       * #res #Hlookup >Hlookup normalize nodelta whd in match (bindIO ??????);
3255       (* B. Reduce rhs of assign, i.e. the condition. Do this using simulation hypothesis. *)
3256       >(Hsim_expr … Hexec_eq) >bindIO_Value
3257       (* C. Execute assign. We must prove that this cannot fail. In order for the proof to proceed, we need
3258             to set up things so that loading from that fresh location will yield exactly the stored value. *)
3259       normalize in match store_value_of_type'; normalize nodelta
3260       whd in match (typeof ?);
3261       lapply (sss_new_alloc 〈new,Tint sz sg〉 ? res Hlookup)
3262       [ 1: cases sss_incl // ] * * #Hvalid #Hlow #Hhigh
3263       lapply (store_int_success … i … Hvalid Hlow Hhigh) * #m_ext' #Hstore
3264       lapply (store_value_load_value_compatible … Hstore) // #Hload_value_correct
3265       >Hstore whd in match (m_bind ?????); whd @conj try //
3266       cut (mem block res sss_writeable)
3267       [ 1: @cthulhu ]
3268       (* lapply (memext_store_value_of_type_writeable … sss_mem_hyp … Hstore) *)       
3269       @cthulhu               
3270   | *: @cthulhu ]
3271 | *: @cthulhu ] qed.
Note: See TracBrowser for help on using the repository browser.