source: src/Clight/switchRemoval.ma @ 2608

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

Regions are no more stored in blocks. block_region now tests the id, it being below 0 implying Code region, XData otherwise.
Changes propagated through the front-end and common. Some more work might be required in the back-end, but it should be
trivial to fix related problems.

Motivation: no way to /elegantly/ prove that two blocks with the same id but different regions are non-sensical.
Prevented some proofs to go through in memory injections.

  • Property svn:executable set to *
File size: 152.8 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 nodelta >Hind1 >Hind2 >Hind2 in ⊢ (???%);
946          >associative_append @refl
947     | 2: #block normalize nodelta >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 destruct (Habsurd)
989          | 2: #p #Hright lapply (Hind2 … Hright)
990                normalize >fold_to_concat in ⊢ (? → %);
991                <map_append #Haux @mem_append_backwards %1
992                <map_proj_fold @Haux
993          | 3: #p #Hleft lapply (Hind1 … Hleft)
994                normalize >fold_to_concat in ⊢ (? → %);
995                <map_append #Haux @mem_append_backwards %2
996                <map_proj_fold @Haux ]
997     | 2: #blo whd in match (lookup_opt ???);
998          normalize >fold_to_concat <map_append
999          cases id normalize nodelta
1000          [ 1: #Heq destruct (Heq)
1001               @mem_append_backwards %2 >fold_to_concat
1002               <map_append @mem_append_backwards %2 normalize %1 @refl
1003          | 2: #p #Hlookup lapply (Hind2 … Hlookup) #H
1004               @mem_append_backwards %1
1005               <map_proj_fold @H
1006          | 3: #p #Hlookup lapply (Hind1 … Hlookup) #H
1007               @mem_append_backwards %2 >fold_to_concat
1008               <map_append @mem_append_backwards %1
1009               <map_proj_fold @H
1010          ]
1011     ]
1012] qed.
1013
1014lemma blocks_of_env_cons :
1015  ∀en,id,hd. mem ? hd (blocks_of_env (add SymbolTag block en id hd)).
1016#en #id #hd @(lookup_entails_block … id)
1017cases id #p elim p try /2/ qed.
1018
1019lemma in_blocks_exists_id : ∀env.∀bl. meml … bl (blocks_of_env env) → ∃id. lookup SymbolTag block env id = Some ? bl.
1020#env elim env #m elim m
1021[ 1: #bl normalize @False_ind
1022| 2: #opt #left #right #Hind1 #Hind2 #bl normalize
1023     >fold_to_concat <map_append #H
1024     elim (mem_append_forward ???? H)
1025     [ 1: <map_proj_fold -H #H lapply (Hind2 … H)
1026          * * #id #Hlookup normalize in Hlookup;
1027          %{(an_identifier SymbolTag (p1 id))} normalize nodelta @Hlookup
1028     | 2: <map_proj_fold cases opt
1029          [ 1: normalize -H #H lapply (Hind1 … H)
1030               * * #id #Hlookup normalize in Hlookup;
1031               %{(an_identifier SymbolTag (p0 id))} normalize nodelta @Hlookup
1032          | 2: #bl' normalize >fold_to_concat <map_append normalize
1033               #H' elim (mem_append_forward ???? H')
1034               [ 1: -H #H lapply (Hind1 … H) * * #id normalize #Hlookup
1035                    %{(an_identifier ? (p0 id))} normalize nodelta @Hlookup
1036               | 2: normalize * [ 2: @False_ind ]
1037                    #Heq destruct (Heq)
1038                    %{(an_identifier SymbolTag one)} @refl
1039               ]
1040          ]
1041     ]
1042] qed.
1043
1044let rec inclusion_elim
1045  (A : Type[0])
1046  (m1, m2 : positive_map A)
1047  (P : positive_map A → positive_map A → Prop)
1048  (H1 : ∀m2. P (pm_leaf A) m2)
1049  (H2 : ∀opt1,left1,right1. P left1 (pm_leaf A) → P right1 (pm_leaf A) → P (pm_node A opt1 left1 right1) (pm_leaf A))
1050  (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))
1051  on m1 : P m1 m2 ≝
1052match m1 with
1053[ pm_leaf ⇒
1054  H1 m2
1055| pm_node opt1 left1 right1 ⇒
1056  match m2 with
1057  [ pm_leaf ⇒
1058    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)
1059  | pm_node opt2 left2 right2 ⇒
1060    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)
1061  ]
1062].
1063
1064(* Environment inclusion entails block inclusion. *)
1065lemma environment_sim_blocks_inclusion :
1066  ∀env1, env2. environment_sim env1 env2 → lset_inclusion ? (blocks_of_env env1) (blocks_of_env env2). 
1067* #m1 * #m2 @(inclusion_elim … m1 m2) -m1 -m2
1068[ 1: #m2 normalize #_ @I
1069| 2: #opt1 #left1 #right1 #Hind1 #Hind2 #Hsim
1070      normalize >fold_to_concat in ⊢ (???%); <map_append @All_append
1071      [ 1: <map_proj_fold @Hind2 #id #res elim id #id' #Hlookup @(Hsim (an_identifier ? (p1 id')) res Hlookup)
1072      | 2: cases opt1 in Hsim;
1073           [ 1: normalize nodelta #Hsim
1074                <map_proj_fold @Hind1 #id #res elim id #id' #Hlookup @(Hsim (an_identifier ? (p0 id')) res Hlookup)
1075           | 2: #bl #Hsim lapply (Hsim (an_identifier ? one) bl ?) normalize try @refl
1076                #Habsurd destruct (Habsurd)
1077           ]
1078      ]
1079| 3: #opt1 #left1 #right1 #opt2 #left2 #right2 #Hind1 #Hind2 #Hsim
1080     normalize >fold_to_concat >fold_to_concat in ⊢ (???%);
1081     <map_append <map_append in ⊢ (???%); @All_append
1082     [ 1: cases opt2; normalize nodelta
1083          [ 1: <map_proj_fold <map_proj_fold in ⊢ (???%); <(map_proj_fold ?? p0)
1084               cut (environment_sim (an_id_map SymbolTag block right1) (an_id_map SymbolTag block right2))
1085               [ 1: * #id' #res #Hlookup
1086                    lapply (Hsim (an_identifier ? (p1 id')) res) normalize #H @H @Hlookup ]
1087               #Hsim' lapply (Hind2 Hsim') @All_mp
1088               #a #Hmem @mem_append_backwards %1 @Hmem
1089          | 2: #bl <map_proj_fold <map_proj_fold in ⊢ (???%); <(map_proj_fold ?? p0)
1090               cut (environment_sim (an_id_map SymbolTag block right1) (an_id_map SymbolTag block right2))
1091               [ 1: * #id' #res #Hlookup
1092                    lapply (Hsim (an_identifier ? (p1 id')) res) normalize #H @H @Hlookup ]
1093               #Hsim' lapply (Hind2 Hsim') @All_mp
1094               #a #Hmem @mem_append_backwards %1 @Hmem ]
1095     | 2: cut (environment_sim (an_id_map SymbolTag block left1) (an_id_map SymbolTag block left2))
1096          [ 1: * #id' #res #Hlookup
1097               lapply (Hsim (an_identifier ? (p0 id')) res) normalize #H @H @Hlookup ] #Hsim'
1098          lapply (Hind1 … Hsim')
1099          <map_proj_fold <map_proj_fold in ⊢ (? → (???%)); <(map_proj_fold ?? p0)
1100          cases opt1 in Hsim; normalize nodelta
1101          [ 1: #Hsim @All_mp #a #Hmem @mem_append_backwards %2
1102               cases opt2 normalize nodelta
1103               [ 1: @Hmem
1104               | 2: #bl >fold_to_concat <map_append @mem_append_backwards %1 @Hmem ]
1105          | 2: #bl #Hsim #Hmem >fold_to_concat in ⊢ (???%); <map_append @All_append
1106               [ 2: normalize @conj try @I
1107                    cases opt2 in Hsim;
1108                     [ 1: #Hsim lapply (Hsim (an_identifier ? one) bl (refl ??))
1109                          normalize in ⊢ (% → ?); #Habsurd destruct (Habsurd)
1110                     | 2: #bl2 #Hsim lapply (Hsim (an_identifier ? one) bl (refl ??))
1111                          normalize in ⊢ (% → ?); #Heq >Heq normalize nodelta
1112                          @mem_append_backwards %2 >fold_to_concat <map_append
1113                          @mem_append_backwards %2 normalize %1 @refl ]
1114               | 1: cases opt2 in Hsim;
1115                     [ 1: #Hsim lapply (Hsim (an_identifier ? one) bl (refl ??))
1116                          normalize in ⊢ (% → ?); #Habsurd destruct (Habsurd)
1117                     | 2: #bl2 #Hsim lapply (Hsim (an_identifier ? one) bl (refl ??))
1118                          normalize in ⊢ (% → ?); #Heq lapply (Hind1 Hsim')
1119                          @All_mp #a #Hmem >Heq normalize nodelta
1120                          @mem_append_backwards %2 >fold_to_concat <map_append
1121                          @mem_append_backwards %1 @Hmem ] ]
1122          ]
1123     ]
1124] qed.
1125
1126
1127(* equivalent environments yield "equal" sets of block (cf. frontend_misc.ma)  *)
1128lemma environment_eq_blocks_eq : ∀env1,env2.
1129  environment_eq env1 env2 →
1130  lset_eq ? (blocks_of_env env1) (blocks_of_env env2).
1131#env1 #env2 * #Hsim1 #Hsim2 @conj
1132@environment_sim_blocks_inclusion assumption
1133qed.
1134
1135(* [env_codomain env vars] is the image of vars through env seen as a function. *)
1136definition env_codomain : env → list (ident×type) → lset block ≝ λe,l.
1137  foldi … (λid,block,acc.
1138    if mem_assoc_env … id l then
1139      block :: acc
1140    else
1141      acc
1142  ) e [ ].
1143
1144(* --------------------------------------------------------------------------- *)
1145
1146(* Two equivalent memories yield a trivial memory extension. *)
1147lemma memory_eq_to_mem_ext : ∀m1,m2. memory_eq m1 m2 → sr_memext m1 m2 [ ].
1148* #contents1 #nextblock1 #Hpos * #contents2 #nextblock2 #Hpos' normalize *
1149#Hnextblock #Hcontents_eq destruct %
1150[ 1: #b #H @conj try % elim H try //
1151| 2: #b *
1152| 3: #b #_ normalize % // ]
1153qed.
1154
1155(* memory extensions form a preorder relation *)
1156
1157lemma memory_ext_transitive :
1158  ∀m1,m2,m3,writeable1,writeable2.
1159  sr_memext m1 m2 writeable1 →
1160  sr_memext m2 m3 writeable2 →
1161  sr_memext m1 m3 (writeable1 @ writeable2).
1162#m1 #m2 #m3 #writeable1 #writeable2 #H12 #H23 %
1163[ 1: #b #Hnonempty1
1164     lapply (me_nonempty … H12 b Hnonempty1) * #Hnonempty2 #Hblocks_eq
1165     lapply (me_nonempty … H23 b Hnonempty2) * #Hnonempty3 #Hblocks_eq' @conj
1166     try assumption >Hblocks_eq >Hblocks_eq' @refl
1167| 2: #b #Hmem lapply (mem_append_forward ???? Hmem) *
1168     [ 1: #Hmem12 lapply (me_writeable_valid … H12 b Hmem12) #Hnonempty2
1169          elim (me_nonempty … H23 … Hnonempty2) //
1170     | 2: #Hmem23 @(me_writeable_valid … H23 b Hmem23) ]
1171| 3: #b #Hvalid % #Hmem lapply (mem_append_forward ???? Hmem) *
1172     [ 1: #Hmem12
1173          lapply (me_not_writeable … H12 … Hvalid) * #H @H assumption
1174     | 2: #Hmem23 lapply (me_nonempty … H12 … Hvalid) * #Hvalid2
1175          lapply (me_not_writeable … H23 … Hvalid2) * #H #_ @H assumption
1176     ]
1177] qed.     
1178
1179lemma memory_ext_reflexive : ∀m. sr_memext m m [ ].
1180#m % /2/ #b * qed.
1181
1182(* --------------------------------------------------------------------------- *)
1183(* Lemmas relating memory extensions to [free] *)
1184
1185lemma beloadv_free_simulation :
1186  ∀m1,m2,writeable,block,ptr,res.
1187  ∀mem_hyp : sr_memext m1 m2 writeable.
1188  beloadv (free m1 block) ptr = Some ? res → beloadv (free m2 block) ptr = Some ? res.
1189* #contents1 #nextblock1 #nextpos1 * #contents2 #nextblock2 #nextpos2 #writeable
1190* (* #br *) #bid * * (* #pr *) #pid #poff #res #Hext
1191(*#Hme_nonempty #Hme_writeable #Hme_nonempty #Hvalid_conserv*)
1192#Hload
1193lapply (beloadv_free_beloadv … Hload) #Hload_before_free
1194lapply (beloadv_free_blocks_neq … Hload) #Hblocks_neq
1195@beloadv_free_beloadv2
1196[ 1: @Hblocks_neq ]
1197@(sr_memext_load_sim … Hext) assumption
1198qed.
1199
1200
1201(* Lifting the property of being valid after a free to memory extensions *)
1202lemma 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.
1203#m1 #m2 #writeable #Hext #p #b #Hvalid
1204lapply (valid_free_to_valid … Hvalid) #Hvalid_before_free
1205lapply (me_valid_pointers … Hext … Hvalid_before_free)
1206lapply (valid_after_free … Hvalid) #Hneq
1207whd in match (free ??);
1208whd in match (update_block ????);
1209whd in match (valid_pointer ??) in ⊢ (% → %);
1210whd in match (low_bound ??) in ⊢ (% → %);
1211whd in match (high_bound ??) in ⊢ (% → %);
1212>(not_eq_block_rev … Hneq) normalize nodelta #H @H
1213qed.
1214
1215lemma nonempty_block_mismatch : ∀m,b,bl.
1216  nonempty_block (free m bl) b →
1217  nonempty_block m b ∧ b ≠ bl.
1218#m #b #bl #Hnonempty
1219@(eq_block_elim … b bl)
1220[ 1: #Heq >Heq in Hnonempty; * #_ normalize
1221     cases (block_region bl) normalize >eqZb_reflexive normalize *
1222| 2: #Hneq @conj try assumption elim Hnonempty #Hvalid #Hlh %
1223     [ 1: lapply Hvalid normalize //
1224     | 2: lapply Hlh normalize
1225          @(eqZb_elim … (block_id b) (block_id bl))
1226          [ 1,3: * normalize *
1227          | 2,4: #_ // ] ] ]
1228qed.
1229
1230lemma eqb_to_eq_block : ∀a,b : block. a == b = eq_block a b.
1231#a #b lapply (eqb_true ? a b) @(eq_block_elim … a b)
1232#H #I
1233try /2 by neq_block_eq_block_false, true_to_andb_true/
1234cases I #J #K @K @H
1235qed.
1236
1237(* We can free in both sides of a memory extension if we take care of removing
1238   the freed block from the set of writeable blocks. *)
1239lemma free_memory_ext :
1240  ∀m1,m2,writeable,bl.
1241   sr_memext m1 m2 writeable →
1242   sr_memext (free m1 bl) (free m2 bl) (lset_remove ? writeable bl).
1243#m1 #m2 #writeable #bl #Hext %
1244[ 1: #b #Hnonempty lapply (nonempty_block_mismatch … Hnonempty)
1245     * #Hnonempty' #Hblocks_neq lapply (me_nonempty … Hext … Hnonempty') *     
1246     #Hnonempty2 #Hcontents_eq @conj
1247     [ 1: % try @(wb_valid … Hnonempty2)
1248          whd in match (free ??);
1249          whd in match (update_block ?????);
1250          >(neq_block_eq_block_false … Hblocks_neq) normalize
1251          try @(wb_nonempty … Hnonempty2)
1252     | 2: whd in match (free ??) in ⊢ (??%%);
1253          whd in match (update_block ?????) in ⊢ (??%%);
1254          >(neq_block_eq_block_false … Hblocks_neq)
1255          normalize nodelta assumption ]         
1256| 2: #b #Hmem
1257     cut (mem block b writeable ∧ b ≠ bl)
1258     [ elim writeable in Hmem;
1259       [ 1: normalize *
1260       | 2: #hd #tl #Hind whd in match (filter ???);
1261            >eqb_to_eq_block
1262            @(eq_block_elim … hd bl) normalize in match (notb ?); normalize nodelta
1263            [ 1: #Heq #H whd in match (meml ???); elim (Hind H) #H0 #H1 @conj
1264                 [ 1: %2 ] assumption
1265            | 2: #Hneq whd in match (meml ???) in ⊢ (% → %); *
1266                 [ 1: #H destruct /3 by conj, or_introl, refl/
1267                 | 2: #H elim (Hind H) #H1 #H2 /3 by conj, or_intror, refl/ ] ] ]
1268     ] * #Hmem2 #Hblocks_neq
1269    lapply (me_writeable_valid … Hext … Hmem2) * #Hvalid #Hnonempty %
1270    try assumption whd in match (free ??); whd in match (update_block ?????);
1271    >(neq_block_eq_block_false … Hblocks_neq) @Hnonempty
1272| 3: #p #Hvalid lapply (nonempty_block_mismatch … Hvalid) * #Hnonempty #Hblocks_neq
1273     % #Hmem lapply (me_not_writeable … Hext … Hnonempty) * #H @H
1274     elim writeable in Hmem;
1275     [ 1: *
1276     | 2: #hd #tl #Hind whd in match (filter ???) in ⊢ (% → ?); >eqb_to_eq_block
1277          @(eq_block_elim … hd bl) normalize in match (notb ?); normalize nodelta
1278          [ 1: #Heq #H normalize %2 @(Hind H)
1279          | 2: #Hblocks_neq whd in match (meml ???); *
1280               [ 1: #Hneq normalize %1 assumption
1281               | 2: #Hmem normalize %2 @(Hind Hmem) ]
1282          ]
1283     ]
1284] qed.     
1285
1286
1287(* Freeing from an extension block is ok. *)
1288lemma memext_free_extended_conservation :
1289  ∀m1,m2 : mem.
1290  ∀writeable.
1291  ∀mem_hyp : sr_memext m1 m2 writeable.
1292  ∀b. meml ? b writeable →
1293  sr_memext m1 (free m2 b) (lset_remove … writeable b).
1294#m1 #m2 #writeable #Hext #b #Hb_writeable %
1295[ 1: #bl #Hnonempty lapply (me_not_writeable … Hext … Hnonempty) #Hnot_mem
1296     lapply (mem_not_mem_neq … Hb_writeable Hnot_mem) #Hblocks_neq
1297     @conj
1298     [ 2: whd in match (free ??); whd in match (update_block ?????);
1299          >(neq_block_eq_block_false … (sym_neq … Hblocks_neq)) normalize
1300          elim (me_nonempty … Hext … Hnonempty) //
1301     | 1: % elim (me_nonempty … Hext … Hnonempty) * try //
1302          #Hvalid2 #Hlh #Hcontents_eq whd in match (free ??);
1303          whd in match (update_block ?????);
1304          >(neq_block_eq_block_false … (sym_neq … Hblocks_neq)) normalize assumption
1305    ]
1306| 2: #b' #Hmem (* contradiction in first premise *)
1307     cut (mem block b' writeable ∧ b' ≠ b)
1308     [ elim writeable in Hmem;
1309       [ 1: normalize @False_ind
1310       | 2: #hd #tl #Hind whd in match (filter ???); >eqb_to_eq_block
1311            @(eq_block_elim … hd b) normalize in match (notb ?); normalize nodelta
1312            [ 1: #Heq #H whd in match (meml ???); destruct
1313                 elim (Hind H) #Hmem #Hneq @conj try assumption %2 assumption
1314            | 2: #Hneq whd in match (meml ???) in ⊢ (% → %); *
1315                 [ 1: #H @conj [ 1: %1 @H | 2: destruct @Hneq ]
1316                 | 2: #H elim (Hind H) #Hmem #Hneq' @conj try assumption %2 assumption ]
1317     ] ] ]
1318     * #Hb' #Hneq lapply (me_writeable_valid … Hext … Hb') #Hvalid %
1319     [ 1: @(wb_valid … Hvalid)
1320     | 2: whd in match (free ??);
1321          whd in match (update_block ?????);
1322          >(neq_block_eq_block_false … Hneq)
1323          @(wb_nonempty … Hvalid) ]
1324| 3: #b' #Hnonempty % #Hmem
1325     cut (mem block b' writeable ∧ b' ≠ b)
1326     [ elim writeable in Hmem;
1327       [ 1: normalize @False_ind
1328       | 2: #hd #tl #Hind whd in match (filter ???); >eqb_to_eq_block
1329            @(eq_block_elim … hd b) normalize in match (notb ?); normalize nodelta
1330            [ 1: #Heq #H whd in match (meml ???); destruct
1331                 elim (Hind H) #Hmem #Hneq @conj try assumption %2 assumption
1332            | 2: #Hneq whd in match (meml ???) in ⊢ (% → %); *
1333                 [ 1: #H @conj [ 1: %1 @H | 2: destruct @Hneq ]
1334                 | 2: #H elim (Hind H) #Hmem #Hneq' @conj try assumption %2 assumption ]
1335     ] ] ] * #Hmem' #Hblocks_neq
1336     lapply (me_not_writeable … Hext … Hnonempty) * #H @H assumption
1337] qed.
1338 
1339 
1340lemma disjoint_extension_nil_eq_set :
1341  ∀env1,env2.
1342   disjoint_extension env1 env2 [ ] →
1343   lset_eq ? (blocks_of_env env1) (blocks_of_env env2).
1344#env1 #env whd in ⊢ (% → ?); * * #_ #_ #H normalize in H;
1345@environment_eq_blocks_eq whd @conj
1346#id #res >(H id) //
1347qed.
1348
1349lemma free_list_equivalent_sets :
1350  ∀m,set1,set2.
1351  lset_eq ? set1 set2 →
1352  memory_eq (free_list m set1) (free_list m set2).
1353#m #set1 #set2 #Heq whd in match (free_list ??) in ⊢ (?%%);
1354@(lset_eq_fold block_DeqSet mem memory_eq  … Heq)
1355[ 1: @reflexive_memory_eq
1356| 2: @transitive_memory_eq
1357| 3: @symmetric_memory_eq
1358| 4: #x #acc1 #acc2
1359     whd in match (free ??) in ⊢ (? → (?%%));
1360     whd in match (memory_eq ??) in ⊢ (% → %); *
1361     #Hnextblock_eq #Hcontents_eq @conj try assumption
1362     #b normalize >Hcontents_eq @refl
1363| 5: #x1 #x2 #acc normalize @conj try @refl
1364     * (* * *) #id normalize nodelta cases (block_region x1)
1365     cases (block_region x2) normalize nodelta
1366     @(eqZb_elim id (block_id x1)) #Hx1 normalize nodelta
1367     @(eqZb_elim id (block_id x2)) #Hx2 normalize nodelta try @refl
1368| 6: * (* #r *) #i * #contents #nextblock #Hpos @conj
1369     [ 1: @refl
1370     | 2: #b normalize (* cases (block_region b) normalize
1371          cases r normalize *) cases (eqZb (block_id b) i)
1372          normalize @refl
1373     ]
1374] qed.
1375
1376lemma foldr_identity : ∀A:Type[0]. ∀l:list A. foldr A ? (λx:A.λl0.x::l0) [] l = l.
1377#A #l elim l //
1378#hd #tl #Hind whd in match (foldr ?????); >Hind @refl
1379qed.
1380
1381lemma mem_not_mem_diff_aux :
1382  ∀s1,s2,s3,hd.
1383     mem ? hd s1 →
1384     ¬(mem ? hd s2) →
1385     mem block hd (lset_difference ? s1 (s2@(filter block_DeqSet (λx:block_DeqSet.¬x==hd) s3))).
1386#s1 #s2 #s3 #hd #Hmem #Hnotmem lapply Hmem lapply s1 -s1
1387elim s3
1388[ 1: #s1 >append_nil elim s1 try //
1389     #hd' #tl' #Hind *
1390     [ 1: #Heq >lset_difference_unfold
1391          @(match hd'∈s2 return λx. (hd'∈s2 = x) → ? with
1392            [ true ⇒ λH. ?
1393            | false ⇒ λH. ?
1394            ] (refl ? (hd'∈s2))) normalize nodelta
1395          [ 1: lapply (memb_to_mem … H) #Hmem elim Hnotmem #H' destruct
1396               @(False_ind … (H' Hmem))
1397          | 2: whd %1 assumption ]
1398     | 2: #Hmem >lset_difference_unfold
1399          @(match hd'∈s2 return λx. (hd'∈s2 = x) → ? with
1400            [ true ⇒ λH. ?
1401            | false ⇒ λH. ?
1402            ] (refl ? (hd'∈s2))) normalize nodelta
1403          [ 1:  @Hind @Hmem
1404          | 2: %2 @Hind @Hmem ] ]
1405| 2: #hd' #tl' #H #s1 #Hmem >filter_cons_unfold >eqb_to_eq_block
1406    @(eq_block_elim … hd' hd)
1407    [ 1:  >notb_true normalize nodelta #Heq @H @Hmem
1408    | 2: #Hneq >notb_false normalize nodelta
1409          >lset_difference_permute >(cons_to_append … hd')
1410          >associative_append >lset_difference_unfold2 >nil_append
1411          >lset_difference_permute @H
1412          elim s1 in Hmem; try //
1413          #hd'' #tl'' #Hind *
1414          [ 1: #Heq destruct whd in match (lset_remove ???);
1415               >eqb_to_eq_block >(neq_block_eq_block_false … (sym_neq … Hneq))
1416               >notb_false normalize nodelta %1 @refl
1417          | 2: #Hmem whd in match (lset_remove ???);
1418                cases (¬(hd''==hd')) normalize nodelta
1419                [ 1: %2 @Hind @Hmem
1420                | 2: @Hind @Hmem ] ] ]
1421] qed.
1422
1423(* freeing equivalent sets of blocks on memory extensions yields memory extensions *)
1424lemma free_equivalent_sets :
1425  ∀m1,m2,writeable,set1,set2.
1426  lset_eq ? set1 set2 →
1427  sr_memext m1 m2 writeable →
1428  sr_memext (free_list m1 set1) (free_list m2 set2) (lset_difference ? writeable set1).
1429#m1 #m2 #writeable #set1 #set2 #Heq #Hext
1430lapply (free_list_equivalent_sets m2 … (symmetric_lset_eq … Heq))
1431#Heq
1432lapply (memory_eq_to_mem_ext … (symmetric_memory_eq … Heq)) #Hext'
1433lapply (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')
1434[ 2: >append_nil #H @H ]
1435elim set1
1436[ 1: whd in match (free_list ??); whd in match (free_list ??);
1437     normalize >foldr_identity @Hext
1438| 2: #hd #tl #Hind >free_list_cons >free_list_cons
1439     lapply (free_memory_ext … hd … Hind)
1440     cut ((lset_remove block_eq (filter block_eq (λwb:block_eq.¬wb∈tl) writeable) hd) =
1441          (filter block_eq (λwb:block_eq.¬wb∈hd::tl) writeable))
1442     [ whd in match (lset_remove ???); elim writeable //
1443        #hd' #tl' #Hind_cut >filter_cons_unfold >filter_cons_unfold
1444        whd in match (memb ???) in ⊢ (???%); >eqb_to_eq_block
1445        (* elim (eqb_true block_DeqSet hd' hd)*)
1446        @(eq_block_elim … hd' hd) normalize nodelta
1447        [ 1: #Heq
1448             cases (¬hd'∈tl) normalize nodelta
1449             [ 1: whd in match (foldr ?????); >Heq >eqb_to_eq_block >eq_block_b_b normalize in match (notb ?);
1450                  normalize nodelta
1451                  lapply Hind_cut destruct #H @H
1452             | 2: lapply Hind_cut destruct #H @H ]
1453        | 2: #Hneq cases (¬hd'∈tl) normalize nodelta try assumption
1454             whd in match (foldr ?????); >eqb_to_eq_block
1455             >(neq_block_eq_block_false … Hneq)
1456             normalize in match (notb ?); normalize nodelta >Hind_cut @refl
1457        ]
1458    ]
1459    #Heq >Heq #H @H
1460] qed.
1461
1462(* Remove a writeable block. *)
1463lemma memory_ext_weaken :
1464  ∀m1,m2,hd,writeable.
1465    sr_memext m1 m2 (hd :: writeable) →
1466    sr_memext m1 m2 writeable.
1467#m1 #m2 #hd #writeable *
1468#Hnonempty #Hwriteable_valid #Hnot_writeable %
1469try assumption
1470[ 1: #b #Hmem @Hwriteable_valid whd %2 assumption
1471| 2: #b #Hnonempty % #Hmem elim (Hnot_writeable … Hnonempty) #H @H whd %2 @Hmem
1472] qed.
1473
1474(* Perform a "rewrite" using lset_eq on the writeable blocks *)
1475lemma memory_ext_writeable_eq :
1476  ∀m1,m2,wr1,wr2.
1477    sr_memext m1 m2 wr1 →
1478    lset_eq ? wr1 wr2 →
1479    sr_memext m1 m2 wr2.
1480#m1 #m2 #wr1 #wr2 #Hext #Hset_eq %
1481[ 1: @(me_nonempty … Hext)
1482| 2:  #b #Hmem lapply (lset_eq_mem … (symmetric_lset_eq … Hset_eq) … Hmem)
1483      @(me_writeable_valid … Hext)
1484| 3: #b #Hnonempty % #Hmem
1485     lapply (lset_eq_mem … (symmetric_lset_eq … Hset_eq) … Hmem) #Hmem'
1486     lapply (me_not_writeable … Hext … Hnonempty) * #H @H assumption
1487] qed.     
1488
1489
1490         
1491lemma memory_eq_to_memory_ext_pre :
1492  ∀m1,m1',m2,writeable.
1493  memory_eq m1 m1' →
1494  sr_memext m1' m2 writeable →
1495  sr_memext m1 m2 writeable.
1496#m1 #m1' #m2 #writeable #Heq #Hext
1497lapply (memory_eq_to_mem_ext … Heq) #Hext'
1498@(memory_ext_transitive … Hext' Hext)
1499qed.
1500
1501lemma memory_eq_to_memory_ext_post :
1502  ∀m1,m2,m2',writeable.
1503  memory_eq m2' m2 →
1504  sr_memext m1 m2' writeable →
1505  sr_memext m1 m2 writeable.
1506#m1 #m2 #m2' #writeable #Heq #Hext
1507lapply (memory_eq_to_mem_ext … Heq) #Hext'
1508lapply (memory_ext_transitive … Hext Hext') >append_nil #H @H
1509qed.
1510
1511
1512lemma memext_free_extended_environment :
1513  ∀m1,m2,writeable.
1514   sr_memext m1 m2 writeable →
1515   ∀env,env_ext,vars.
1516    disjoint_extension env env_ext vars →
1517    lset_inclusion ? (lset_difference ? (blocks_of_env env_ext) (blocks_of_env env)) writeable →
1518    sr_memext
1519      (free_list m1 (blocks_of_env env))
1520      (free_list m2 (blocks_of_env env_ext))
1521      (lset_difference ? writeable (blocks_of_env env_ext)).
1522#m1 #m2 #writeable #Hext #env #env_ext #vars #Hdisjoint #Hext_in_writeable
1523(* Disjoint extension induces environment "inclusion", i.e. simulation *)
1524lapply (disjoint_extension_to_inclusion … Hdisjoint) #Hincl
1525(* Environment inclusion entails set inclusion on the mapped blocks *)
1526lapply (environment_sim_blocks_inclusion … Hincl) #Hblocks_incl
1527(* This set inclusion can be decomposed on a common part and an extended part. *)
1528lapply (lset_inclusion_difference ??? Hblocks_incl)
1529* #extended_part * * #Hset_eq #Henv_disjoint_ext #Hextended_eq
1530lapply (lset_difference_lset_eq … writeable … Hset_eq) #HeqA
1531cut (lset_inclusion ? extended_part writeable)
1532[ 1: cases Hextended_eq #HinclA #_ @(transitive_lset_inclusion … HinclA … Hext_in_writeable) ]
1533-Hext_in_writeable #Hext_in_writeable
1534@(memory_ext_writeable_eq ????? (symmetric_lset_eq … HeqA))
1535lapply (free_list_equivalent_sets m2 ?? Hset_eq) #Hmem_eq
1536@(memory_eq_to_memory_ext_post … (symmetric_memory_eq … Hmem_eq))
1537(* Add extended ⊆ (lset_difference block_eq writeable (blocks_of_env env @ tl)) in Hind *)
1538cut (∀x. mem ? x extended_part → ¬ (mem ? x (blocks_of_env env)))
1539[ 1: cases Hextended_eq #Hincl_ext #_ @(lset_not_mem_difference … Hincl_ext) ]
1540lapply (proj2 … Hset_eq) lapply Hext_in_writeable
1541@(WF_rect ????? (filtered_list_wf block_DeqSet extended_part))
1542*
1543[ 1: #_ #_ #_ #_ #_ >append_nil
1544     @(free_equivalent_sets ???? (blocks_of_env env) (reflexive_lset_eq ??) Hext)
1545| 2: #hd #tl #Hwf_step #Hind_wf #Hext_in_writeable #Hset_incl #Hin_ext_not_in_env
1546     cut (lset_eq ? (blocks_of_env env@hd::tl) (hd::(blocks_of_env env@tl)))
1547     [ 1: >cons_to_append >cons_to_append in ⊢ (???%);
1548          @lset_eq_concrete_to_lset_eq // ]
1549     #Hpermute
1550     lapply (free_list_equivalent_sets m2 ?? Hpermute) #Hmem_eq2
1551     @(memory_eq_to_memory_ext_post … (symmetric_memory_eq … Hmem_eq2))
1552     >free_list_cons
1553     lapply (lset_difference_lset_eq … writeable … Hpermute) #HeqB
1554     @(memory_ext_writeable_eq ????? (symmetric_lset_eq … HeqB))
1555     >lset_difference_unfold2
1556     lapply (lset_difference_lset_remove_commute ? hd writeable (blocks_of_env env@tl))
1557     #Heq_commute >Heq_commute
1558     (* lapply (memory_ext_writeable_eq ????? (symmetric_lset_eq … Heq_commute)) *)
1559     lapply (Hind_wf (filter … (λx.¬(x==hd)) tl) ????)
1560     [ 2: @(transitive_lset_inclusion … Hset_incl)
1561          @lset_inclusion_concat_monotonic
1562          @cons_preserves_inclusion
1563          @lset_inclusion_filter_self
1564     | 3: @(transitive_lset_inclusion … Hext_in_writeable)
1565          @cons_preserves_inclusion
1566          @lset_inclusion_filter_self
1567     | 4: whd whd in ⊢ (???%);
1568          lapply (eqb_true ? hd hd) * #_ #H >(H (refl ??)) normalize in match (notb ?);
1569          normalize nodelta @refl
1570     | 1: #x #H @Hin_ext_not_in_env %2 elim tl in H; //
1571          #hd' #tl' #Hind >filter_cons_unfold >eqb_to_eq_block @(eq_block_elim … hd' hd)
1572          >notb_true >notb_false normalize nodelta
1573          [ 1: #Heq >Heq #H %2 @Hind @H
1574          | 2: #Hneq *
1575               [ 1: #Heq destruct %1 @refl
1576               | 2: #H %2 @Hind @H ] ]
1577     ]
1578     #Hext_ind
1579     lapply (Hin_ext_not_in_env … hd (or_introl … (refl ??)))
1580     #Hnot_in_env     
1581     lapply (memext_free_extended_conservation … Hext_ind hd ?)
1582     [ 1: @mem_not_mem_diff_aux try assumption elim Hext_in_writeable #H #_ @H ]
1583     -Hext_ind #Hext_ind
1584     cut (memory_eq (free (free_list m2 (blocks_of_env env@filter block_DeqSet (λx:block_DeqSet.¬x==hd) tl)) hd)
1585                    (free (free_list m2 (blocks_of_env env@tl)) hd))
1586     [ 1: <free_list_cons <free_list_cons
1587          @free_list_equivalent_sets @lset_eq_concrete_to_lset_eq
1588          >cons_to_append >cons_to_append in ⊢ (???%);
1589          @(transitive_lset_eq_concrete … (switch_lset_eq_concrete ????))
1590          @symmetric_lset_eq_concrete
1591          @(transitive_lset_eq_concrete ????? (switch_lset_eq_concrete ????))
1592          @lset_eq_to_lset_eq_concrete
1593          elim (blocks_of_env env)
1594          [ 1: @symmetric_lset_eq @lset_eq_filter
1595          | 2: #hd0 #tl0 #Hind >cons_to_append
1596               >associative_append in ⊢ (??%%);
1597               >associative_append in ⊢ (??%%);
1598               @cons_monotonic_eq @Hind ] ]
1599      #Hmem_eq3 @(memory_eq_to_memory_ext_post … Hmem_eq3)
1600      @(memory_ext_writeable_eq … Hext_ind)
1601      <lset_difference_lset_remove_commute <lset_difference_lset_remove_commute     
1602      <lset_difference_unfold2 <lset_difference_unfold2
1603      @lset_difference_lset_eq
1604      (* Note: exactly identical to the proof in the cut. *)
1605      @lset_eq_concrete_to_lset_eq
1606      >cons_to_append >cons_to_append in ⊢ (???%);
1607      @(transitive_lset_eq_concrete … (switch_lset_eq_concrete ????))
1608      @symmetric_lset_eq_concrete
1609      @(transitive_lset_eq_concrete ????? (switch_lset_eq_concrete ????))
1610      @lset_eq_to_lset_eq_concrete
1611      elim (blocks_of_env env)
1612      [ 1: @symmetric_lset_eq @lset_eq_filter
1613      | 2: #hd0 #tl0 #Hind >cons_to_append
1614           >associative_append in ⊢ (??%%);
1615           >associative_append in ⊢ (??%%);
1616           @cons_monotonic_eq @Hind ]
1617] qed.
1618
1619(* --------------------------------------------------------------------------- *)
1620(* Some lemmas allowing to reason on writes to extended memories. *)
1621
1622(* Writing in the extended part of the memory preserves the extension (that's the point) *)
1623lemma bestorev_writeable_memory_ext :
1624  ∀m1,m2,writeable.
1625  ∀Hext:sr_memext m1 m2 writeable.
1626  ∀wb,wo,v. meml ? wb writeable →
1627  ∀m2'.bestorev m2 (mk_pointer wb wo) v = Some ? m2' →
1628  sr_memext m1 m2' writeable.
1629#m1 * #contents2 #nextblock2 #Hpos2 #writeable #Hext #wb #wo #v #Hmem #m2'
1630whd in ⊢ ((??%?) → ?);
1631lapply (me_writeable_valid … Hext ? Hmem) * whd in ⊢ (% → ?); #Hlt
1632>(Zlt_to_Zltb_true … Hlt) normalize nodelta #Hnonempty2 #H
1633lapply (if_opt_inversion ???? H) -H * #Hzltb
1634lapply (andb_inversion … Hzltb) * #Hleb #Hltb -Hzltb
1635#Heq destruct %
1636[ 1: #b #Hnonempty1
1637     lapply (me_nonempty … Hext b Hnonempty1) * * #Hvalid_b #Hnonempty_b
1638     #Heq @conj
1639     [ 1: % whd whd in Hvalid_b; try @Hvalid_b
1640          normalize cases (block_region b) normalize nodelta
1641          cases (block_region wb) normalize nodelta try //
1642          @(eqZb_elim … (block_id b) (block_id wb)) normalize nodelta
1643          try //
1644     | 2: whd in ⊢ (??%%);
1645          @(eq_block_elim … b wb) normalize nodelta // #Heq_b_wb
1646          lapply (me_not_writeable … Hext b Hnonempty1) destruct (Heq_b_wb)
1647          * #H @(False_ind … (H Hmem)) ]
1648| 2: #b #Hmem_writeable
1649     lapply (me_writeable_valid … Hext … Hmem_writeable) #H %
1650     [ 1: normalize cases H //
1651     | 2: cases H normalize #Hb_lt #Hb_nonempty2
1652          (*
1653            cases (block_region b)
1654            cases (block_region wb) *)
1655          @(eqZb_elim … (block_id b) (block_id wb)) normalize nodelta
1656          // ]
1657| 3: #b #Hnonempty
1658     lapply (me_not_writeable … Hext … Hnonempty) //
1659] qed.
1660
1661(* If we manage to write in a block, then it is nonempty *)
1662lemma bestorev_success_nonempty :
1663  ∀m,wb,wo,v,m'.
1664  bestorev m (mk_pointer wb wo) v = Some ? m' →
1665  nonempty_block m wb.
1666#m #wb #wo #v #m' normalize #Hstore
1667cases (if_opt_inversion ???? Hstore) -Hstore #block_valid1 #H
1668cases (if_opt_inversion ???? H) -H #nonempty #H %
1669[ 1: whd @Zltb_true_to_Zlt assumption
1670| 2: generalize in match (Z_of_unsigned_bitvector 16 (offv wo)) in nonempty; #z #H'
1671     cut (Zleb (low (blocks m wb)) z = true)
1672     [ 1: lapply H' cases (Zleb (low (blocks m wb)) z) // normalize #H @H ]
1673     #Hleb >Hleb in H'; normalize nodelta #Hlt
1674     lapply (Zleb_true_to_Zle … Hleb) lapply (Zltb_true_to_Zlt … Hlt)
1675     /2 by Zle_to_Zlt_to_Zlt/
1676] qed.
1677
1678(* If we manage to write in a block, it is still non-empty after the write *)
1679lemma bestorev_success_nonempty2 :
1680  ∀m,wb,wo,v,m'.
1681  bestorev m (mk_pointer wb wo) v = Some ? m' →
1682  nonempty_block m' wb.
1683#m #wb #wo #v #m' normalize #Hstore
1684cases (if_opt_inversion ???? Hstore) -Hstore #block_valid1 #H
1685cases (if_opt_inversion ???? H) -H #nonempty #H %
1686[ 1: whd destruct @Zltb_true_to_Zlt assumption
1687| 2: generalize in match (Z_of_unsigned_bitvector 16 (offv wo)) in nonempty; #z #H'
1688     cut (Zleb (low (blocks m wb)) z = true)
1689     [ 1: lapply H' cases (Zleb (low (blocks m wb)) z) // normalize #H @H ]
1690     #Hleb >Hleb in H'; normalize nodelta #Hlt
1691     lapply (Zleb_true_to_Zle … Hleb) lapply (Zltb_true_to_Zlt … Hlt)
1692     destruct cases (block_region wb) normalize >eqZb_z_z normalize
1693     /2 by Zle_to_Zlt_to_Zlt/
1694] qed.
1695
1696(* A nonempty block stays nonempty after a write. *)
1697lemma nonempty_block_update_ok :
1698  ∀m,b,wb,offset,v.
1699  nonempty_block m b →
1700  nonempty_block
1701    (mk_mem
1702      (update_block ? wb
1703        (mk_block_contents (low (blocks m wb)) (high (blocks m wb))
1704          (update beval offset v (contents (blocks m wb)))) (blocks m))
1705            (nextblock m) (nextblock_pos m)) b.
1706#m #b #wb #offset #v * #Hvalid #Hnonempty % //
1707cases b in Hvalid Hnonempty; (* #br *) #bid cases wb (* #wbr *) #wbid normalize
1708(* cases br *) normalize nodelta (* cases wbr normalize nodelta // *)
1709@(eqZb_elim … bid wbid) // #Heq #Hlt normalize //
1710qed.
1711
1712lemma nonempty_block_update_ok2 :
1713  ∀m,b,wb,offset,v.
1714  nonempty_block
1715    (mk_mem
1716      (update_block ? wb
1717        (mk_block_contents (low (blocks m wb)) (high (blocks m wb))
1718          (update beval offset v (contents (blocks m wb)))) (blocks m))
1719            (nextblock m) (nextblock_pos m)) b →
1720  nonempty_block m b.
1721#m #b #wb #offset #v * #Hvalid #Hnonempty % //
1722cases b in Hvalid Hnonempty; (* #br *) #bid cases wb (* #wbr *) #wbid normalize
1723(* cases br normalize nodelta cases wbr normalize nodelta // *)
1724@(eqZb_elim … bid wbid) // #Heq #Hlt normalize //
1725qed.
1726
1727(* Writing in the non-extended part of the memory preserves the extension as long
1728 * as it's done identically in both memories. *)
1729lemma bestorev_not_writeable_memory_ext :
1730  ∀m1,m2,writeable.
1731  ∀Hext:sr_memext m1 m2 writeable.
1732  ∀wb,wo,v.
1733  ∀m1'. bestorev m1 (mk_pointer wb wo) v = Some ? m1' → 
1734  ∃m2'. bestorev m2 (mk_pointer wb wo) v = Some ? m2' ∧
1735        sr_memext m1' m2' writeable.
1736#m1 #m2 #writeable #Hext #wb #wo #v #m1' #Hstore1
1737lapply (bestorev_success_nonempty … Hstore1) #Hwb_nonempty
1738cases (me_nonempty … Hext … Hwb_nonempty) #Hwb_nonempty2 #Hblocks_eq
1739cut (∃m2'. bestorev m2 (mk_pointer wb wo) v=Some mem m2')
1740[ cases Hwb_nonempty2 #Hwb_valid #Hnonempty normalize
1741  normalize in Hwb_valid; >(Zlt_to_Zltb_true … Hwb_valid) normalize nodelta
1742  whd in Hstore1:(??%%); normalize
1743  cases (if_opt_inversion ???? Hstore1) -Hstore1 #block_valid1 #H
1744  cases (if_opt_inversion ???? H) #Hin_bounds1 #Hm1' -H
1745  cases (andb_inversion … Hin_bounds1) #Hleb1 #Hltb1 -Hin_bounds1
1746  >Hblocks_eq in Hleb1 Hltb1 ⊢ %; #Hleb1 #Hltb1 >Hleb1 >Hltb1
1747  normalize nodelta /2 by ex_intro/ ]
1748* #m2' #Hstore2 %{m2'} @conj try assumption
1749whd in Hstore1:(??%%);
1750whd in Hstore2:(??%%);
1751cases (if_opt_inversion ???? Hstore1) -Hstore1 #block_valid1 #H
1752cases (if_opt_inversion ???? H) #Hin_bounds1 #Hm1' -H
1753cases (if_opt_inversion ???? Hstore2) -Hstore2 #block_valid2 #H
1754cases (if_opt_inversion ???? H) #Hin_bounds2 #Hm2' -H
1755cases (andb_inversion … Hin_bounds1) #Hleb1 #Hltb1 -Hin_bounds1
1756cases (andb_inversion … Hin_bounds2) #Hleb2 #Hltb2 -Hin_bounds2
1757cut (valid_pointer m1 (mk_pointer wb wo))
1758[ 1: normalize >block_valid1 normalize nodelta >Hleb1 normalize nodelta
1759     >Hltb1 @I ]
1760#Hvalid
1761lapply (me_not_writeable_ptr … Hext … Hvalid) #Hnot_in_writeable
1762destruct %
1763[ 1: #b #Hnonempty lapply (me_nonempty … Hext … (nonempty_block_update_ok2 … Hnonempty)) * #HA #HB
1764     @conj
1765     [ 1: @nonempty_block_update_ok //
1766     | 2: normalize (* cases b in HB; #br #bid cases wb #wbr #wbid
1767          cases br cases wbr normalize nodelta *)
1768          @(eqZb_elim … (block_id b) (block_id wb)) normalize nodelta //
1769          #Hid_eq cut (b = wb)
1770          [ cases b in Hid_eq; cases wb #wid #bid #H >H @refl ]
1771          #Hblock_eq destruct (Hblock_eq) >HB @refl ]
1772| 2: #b #Hmem lapply (me_writeable_valid … Hext … Hmem) @nonempty_block_update_ok
1773| 3: #b #Hnonempty lapply (nonempty_block_update_ok2 … Hnonempty)
1774     @(me_not_writeable … Hext)
1775] qed.
1776
1777(* If we successfuly store something in the first memory, then we can store it in the
1778 * second one and the memory extension is preserved. *)
1779lemma memext_store_value_of_type :
1780       ∀m, m_ext, m', writeable, ty, loc, off, v.
1781       sr_memext m m_ext writeable →
1782       store_value_of_type ty m loc off v = Some ? m' →
1783       ∃m_ext'. store_value_of_type ty m_ext loc off v = Some ? m_ext' ∧
1784                sr_memext m' m_ext' writeable.
1785#m #m_ext #m' #writeable #ty #loc #off #v #Hext
1786(* case analysis on access mode of [ty] *)
1787cases ty
1788[ | #sz #sg | #ptr_ty | #array_ty #array_sz | #domain #codomain
1789| #structname #fieldspec | #unionname #fieldspec | #id ]
1790whd in ⊢ ((??%?) → (?%?));
1791[ 1,4,5,6,7: #Habsurd destruct ]
1792whd in ⊢ (? → (??(λ_.?(??%?)?)));
1793lapply loc lapply off lapply Hext lapply m_ext lapply m lapply m' -loc -off -Hext -m_ext -m -m'
1794elim (fe_to_be_values ??)
1795[ 1,3,5: #m' #m #m_ext #Hext #off #loc normalize in ⊢ (% → ?); #Heq destruct (Heq) %{m_ext} @conj normalize //
1796| 2,4,6: #hd #tl #Hind #m' #m #m_ext #Hext #off #loc whd in ⊢ ((??%?) → ?); #H
1797         cases (some_inversion ????? H) #m'' * #Hstore_eq #Hstoren_eq
1798         lapply (bestorev_not_writeable_memory_ext … Hext … Hstore_eq)
1799         * #m_ext'' * #Hstore_eq2 #Hext2
1800         lapply (Hind … Hext2 … Hstoren_eq) -Hind * #m_ext' *
1801         #Hstoren' #Hext3
1802         %{m_ext'} @conj try assumption
1803         whd in ⊢ (??%%); >Hstore_eq2 normalize nodelta
1804         @Hstoren'
1805] qed.
1806
1807lemma memext_store_value_of_type' :
1808       ∀m, m_ext, m', writeable, ty, ptr, v.
1809       sr_memext m m_ext writeable →
1810       store_value_of_type' ty m ptr v = Some ? m' →
1811       ∃m_ext'. store_value_of_type' ty m_ext ptr v = Some ? m_ext' ∧
1812                sr_memext m' m_ext' writeable.
1813#m #m_ext #m' #writeable #ty #p #v #Hext cases p #b #o
1814@memext_store_value_of_type @Hext qed.
1815
1816lemma memext_store_value_of_type_writeable :
1817  ∀m1,m2,writeable.
1818  ∀Hext:sr_memext m1 m2 writeable.
1819  ∀wb. meml ? wb writeable →
1820  ∀ty,off,v,m2'. store_value_of_type ty m2 wb off v = Some ? m2' →
1821  sr_memext m1 m2' writeable.
1822#m1 #m2 #writeable #Hext #wb #Hmem
1823#ty #off #v #m2'
1824cases ty
1825[ | #sz #sg | #ptr_ty | #array_ty #array_sz | #domain #codomain
1826| #structname #fieldspec | #unionname #fieldspec | #id ]
1827whd in ⊢ ((??%?) → ?);
1828[ 1,4,5,6,7: #Habsurd destruct ]
1829lapply Hext lapply m1 lapply m2 lapply m2' lapply off -Hext -m1 -m2 -m2' -off -ty
1830elim (fe_to_be_values ??)
1831[ 1,3,5: #o #m2' #m2 #m1 #Hext normalize #Heq destruct assumption
1832| *: #hd #tl #Hind #o #m2_end #m2 #m1 #Hext whd in match (storen ???); #Hbestorev
1833     cases (some_inversion ????? Hbestorev) #m2' * #Hbestorev #Hstoren
1834     lapply (bestorev_writeable_memory_ext … Hext …  o hd Hmem … Hbestorev) #Hext'
1835     @(Hind … Hstoren) //
1836] qed.   
1837
1838(* In proofs, [disjoint_extension] is not enough. When a variable lookup arises, if
1839 * the variable is not in a local environment, then we search into the global one.
1840 * A proper "extension" of a local environment should be such that the extension
1841 * does not collide with a given global env.
1842 * To see the details of why we need that, see [exec_lvalue'], Evar id case.
1843 *)
1844definition ext_fresh_for_genv ≝
1845λ(ext : list (ident × type)). λ(ge : genv).
1846  ∀id. mem_assoc_env id ext = true → find_symbol … ge id = None ?.
1847
1848(* "generic" simulation relation on [res ?] *)
1849definition res_sim ≝
1850  λ(A : Type[0]).
1851  λ(r1, r2 : res A).
1852  ∀a. r1 = OK ? a → r2 = OK ? a.
1853
1854(* Specialisation of [res_sim] to match [exec_expr] *)
1855definition exec_expr_sim ≝ res_sim (val × trace).
1856
1857(* Specialisation of [res_sim] to match [exec_lvalue] *)
1858definition exec_lvalue_sim ≝ res_sim (block × offset × trace).
1859
1860lemma 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.
1861#ty #m #b #o cases (load_value_of_type ty m b o)
1862[ 1: %1 // | 2: #v %2 /2 by ex_intro/ ] qed.
1863
1864(* Simulation relations. *)
1865inductive switch_cont_sim : list (ident × type) → cont → cont → Prop ≝
1866| swc_stop :
1867    ∀new_vars. switch_cont_sim new_vars Kstop Kstop
1868| swc_seq : ∀s,k,k',u,s',new_vars.
1869    fresh_for_statement s u →
1870    switch_cont_sim new_vars k k' →
1871    s' = ret_st ? (switch_removal s u) →
1872    lset_inclusion ? (ret_vars ? (switch_removal s u)) new_vars →
1873    switch_cont_sim new_vars (Kseq s k) (Kseq s' k')
1874| swc_while : ∀e,s,k,k',u,s',new_vars.
1875    fresh_for_statement (Swhile e s) u →
1876    switch_cont_sim new_vars k k' →
1877    s' = ret_st ? (switch_removal s u) →   
1878    lset_inclusion ? (ret_vars ? (switch_removal s u)) new_vars →   
1879    switch_cont_sim new_vars (Kwhile e s k) (Kwhile e s' k')
1880| swc_dowhile : ∀e,s,k,k',u,s',new_vars.
1881    fresh_for_statement (Sdowhile e s) u →
1882    switch_cont_sim new_vars k k' →
1883    s' = ret_st ? (switch_removal s u) →       
1884    lset_inclusion ? (ret_vars ? (switch_removal s u)) new_vars →   
1885    switch_cont_sim new_vars (Kdowhile e s k) (Kdowhile e s' k')
1886| swc_for1 : ∀e,s1,s2,k,k',u,s',new_vars.
1887    fresh_for_statement (Sfor Sskip e s1 s2) u →
1888    switch_cont_sim new_vars k k' →
1889    s' = (ret_st ? (switch_removal (Sfor Sskip e s1 s2) u)) →
1890    lset_inclusion ? (ret_vars ? (switch_removal (Sfor Sskip e s1 s2) u)) new_vars →   
1891    switch_cont_sim new_vars (Kseq (Sfor Sskip e s1 s2) k) (Kseq s' k')
1892| swc_for2 : ∀e,s1,s2,k,k',u,result1,result2,new_vars.
1893    fresh_for_statement (Sfor Sskip e s1 s2) u →
1894    switch_cont_sim new_vars k k' →
1895    result1 = ret_st ? (switch_removal s1 u) →
1896    result2 = ret_st ? (switch_removal s2 (ret_u ? (switch_removal s1 u))) →
1897    lset_inclusion ? (ret_vars ? (switch_removal (Sfor Sskip e s1 s2) u)) new_vars →
1898    switch_cont_sim new_vars (Kfor2 e s1 s2 k) (Kfor2 e result1 result2 k')
1899| swc_for3 : ∀e,s1,s2,k,k',u,result1,result2,new_vars.
1900    fresh_for_statement (Sfor Sskip e s1 s2) u →
1901    switch_cont_sim new_vars k k' →
1902    result1 = ret_st ? (switch_removal s1 u) →
1903    result2 = ret_st ? (switch_removal s2 (ret_u ? (switch_removal s1 u))) →
1904    lset_inclusion ? (ret_vars ? (switch_removal (Sfor Sskip e s1 s2) u)) new_vars →
1905    switch_cont_sim new_vars (Kfor3 e s1 s2 k) (Kfor3 e result1 result2 k')
1906| swc_switch : ∀k,k',new_vars.
1907    switch_cont_sim new_vars k k' →
1908    switch_cont_sim new_vars (Kswitch k) (Kswitch k')
1909| swc_call : ∀en,en',r,f,k,k',old_vars,new_vars. (* Warning: possible caveat with environments here. *)       
1910    switch_cont_sim old_vars k k' →
1911    old_vars = \snd (function_switch_removal f) →
1912    disjoint_extension en en' old_vars →
1913    switch_cont_sim
1914      new_vars
1915      (Kcall r f en k)
1916      (Kcall r (\fst (function_switch_removal f)) en' k').
1917
1918record switch_removal_globals (F:Type[0]) (t:F → F) (ge:genv_t F) (ge':genv_t F) : Prop ≝ {
1919  rg_find_symbol: ∀s.
1920    find_symbol ? ge s = find_symbol ? ge' s;
1921  rg_find_funct: ∀v,f.
1922    find_funct ? ge v = Some ? f →
1923    find_funct ? ge' v = Some ? (t f);
1924  rg_find_funct_ptr: ∀b,f.
1925    find_funct_ptr ? ge b = Some ? f →
1926    find_funct_ptr ? ge' b = Some ? (t f)
1927}.
1928
1929inductive switch_state_sim (ge : genv) : state → state → Prop ≝
1930| sws_state :
1931 (* current statement *)
1932 ∀sss_statement  : statement.
1933 (* label universe *)
1934 ∀sss_lu         : universe SymbolTag.
1935 (* [sss_lu] must be fresh *)
1936 ∀sss_lu_fresh   : fresh_for_statement sss_statement sss_lu.
1937 (* current function *)
1938 ∀sss_func       : function.
1939 (* current function after translation *)
1940 ∀sss_func_tr    : function.
1941 (* variables generated during conversion of the function *)
1942 ∀sss_new_vars   : list (ident × type).
1943 (* statement of the transformation *)
1944 ∀sss_func_hyp   : 〈sss_func_tr, sss_new_vars〉 = function_switch_removal sss_func.
1945 (* memory state before conversion *)
1946 ∀sss_m          : mem.
1947 (* memory state after conversion *)
1948 ∀sss_m_ext      : mem.
1949 (* environment before conversion *)
1950 ∀sss_env        : env.
1951 (* environment after conversion *)
1952 ∀sss_env_ext    : env.
1953 (* continuation before conversion *)
1954 ∀sss_k          : cont.
1955 (* continuation after conversion *)
1956 ∀sss_k_ext      : cont.
1957 (* writeable blocks *)
1958 ∀sss_writeable  : list block.
1959 (* memory "injection" *)
1960 ∀sss_mem_hyp    : sr_memext sss_m sss_m_ext sss_writeable.
1961 (* The extended environment does not interfer with the old one. *)
1962 ∀sss_env_hyp    : disjoint_extension sss_env sss_env_ext sss_new_vars.
1963 (* The new variables are allocated to a size corresponding to their types. *)
1964 ∀sss_new_alloc  :
1965    (∀v.meml ? v sss_new_vars →
1966      ∀vb. lookup … sss_env_ext (\fst v) = Some ? vb →
1967           valid_block sss_m_ext vb ∧
1968           low (blocks sss_m_ext vb) = OZ ∧
1969           high (blocks sss_m_ext vb) = sizeof (\snd v)).
1970 (* Exit label for the enclosing switch, if any. Use for [break] conversion in switches. *)
1971 ∀sss_enclosing_label : option label.
1972 (* Extension blocks are writeable. *)
1973 ∀sss_ext_write  : lset_inclusion ? (lset_difference ? (blocks_of_env sss_env_ext) (blocks_of_env sss_env)) sss_writeable.
1974 (* conversion of the current statement, using the variables produced during the conversion of the current function *)
1975 ∀sss_result_rec.
1976 ∀sss_result_hyp : switch_removal sss_statement sss_lu = sss_result_rec.
1977 ∀sss_result.
1978 sss_result = ret_st ? sss_result_rec →
1979 (* inclusion of the locally produced new variables in the global new variables *)
1980 lset_inclusion ? (ret_vars ? sss_result_rec) sss_new_vars →
1981 (* simulation between the continuations before and after conversion. *)
1982 ∀sss_k_hyp      : switch_cont_sim sss_new_vars sss_k sss_k_ext.
1983 ext_fresh_for_genv sss_new_vars ge →
1984    switch_state_sim
1985      ge
1986      (State sss_func sss_statement sss_k sss_env sss_m)
1987      (State sss_func_tr sss_result sss_k_ext sss_env_ext sss_m_ext)
1988| sws_callstate :
1989 ∀ssc_fd.
1990 ∀ssc_args.
1991 ∀ssc_k.
1992 ∀ssc_k_ext.
1993 ∀ssc_m.
1994 ∀ssc_m_ext.
1995 ∀ssc_writeable.
1996 ∀ssc_mem_hyp : sr_memext ssc_m ssc_m_ext ssc_writeable.
1997 (match ssc_fd with
1998  [ CL_Internal ssc_f ⇒
1999    switch_cont_sim (\snd (function_switch_removal ssc_f)) ssc_k ssc_k_ext
2000  | _ ⇒ True ]) →
2001    switch_state_sim ge (Callstate ssc_fd ssc_args ssc_k ssc_m)
2002                        (Callstate (fundef_switch_removal ssc_fd) ssc_args ssc_k_ext ssc_m_ext)
2003| sws_returnstate :
2004 ∀ssr_result.
2005 ∀ssr_k       : cont.
2006 ∀ssr_k_ext   : cont.
2007 ∀ssr_m       : mem.
2008 ∀ssr_m_ext   : mem.
2009 ∀ssr_writeable : list block.
2010 ∀ssr_mem_hyp : sr_memext ssr_m ssr_m_ext ssr_writeable.
2011 ∀ssr_vars.
2012    switch_cont_sim ssr_vars ssr_k ssr_k_ext →
2013    switch_state_sim ge (Returnstate ssr_result ssr_k ssr_m) (Returnstate ssr_result ssr_k_ext ssr_m_ext)
2014| sws_finalstate : ∀r.
2015    switch_state_sim ge (Finalstate r) (Finalstate r).
2016
2017lemma call_cont_swremoval : ∀k,k',vars.
2018  switch_cont_sim vars k k' →
2019  switch_cont_sim vars (call_cont k) (call_cont k').
2020#k0 #k0' #vars #K elim K /2/
2021qed.
2022
2023(* [eventually ge P s tr] states that after a finite number of [exec_step], the
2024   property P on states will be verified. *)
2025inductive eventually (ge : genv) (P : state → Prop) : state → trace → Prop ≝
2026| eventually_base : ∀s,t,s'.
2027    exec_step ge s = Value io_out io_in ? 〈t, s'〉 →
2028    P s' →
2029    eventually ge P s t
2030| eventually_step : ∀s,t,s',t',trace.
2031    exec_step ge s = Value io_out io_in ? 〈t, s'〉 →
2032    eventually ge P s' t' →
2033    trace = (t ⧺ t') →
2034    eventually ge P s trace.
2035   
2036(* [eventually] is not so nice to use directly, we would like to make the mandatory
2037 * [exec_step ge s = Value ??? 〈t, s'] visible - and in the end we would like not
2038   to give [s'] ourselves, but matita to compute it. Hence this little intro-wrapper. *)     
2039lemma eventually_now : ∀ge,P,s,t.
2040  (∃s'.exec_step ge s = Value io_out io_in ? 〈t,s'〉 ∧ P s') →
2041   eventually ge P s t.
2042#ge #P #s #t * #s' * #Hexec #HP %1{… Hexec HP}  (* %{E0} normalize >(append_nil ? t) %1{????? Hexec HP} *)
2043qed.
2044
2045lemma eventually_later : ∀ge,P,s,t.
2046   (∃s',tstep.exec_step ge s = Value io_out io_in ? 〈tstep,s'〉 ∧ ∃tnext. t = tstep ⧺ tnext ∧ eventually ge P s' tnext) →
2047    eventually ge P s t.
2048#ge #P #s #t * #s' * #tstep * #Hexec_step * #tnext * #Heq #Heventually %2{s tstep s' tnext … Heq}
2049try assumption
2050qed.
2051
2052lemma exec_lvalue_expr_elim :
2053  ∀r1,r2,Pok,Qok.
2054  exec_lvalue_sim r1 r2 →
2055  (∀val,trace.
2056    match Pok 〈val,trace〉 with
2057    [ Error err ⇒ True
2058    | OK pvt ⇒
2059      let 〈pval,ptrace〉 ≝ pvt in
2060      match Qok 〈val,trace〉 with
2061      [ Error err ⇒ False
2062      | OK qvt ⇒
2063        let 〈qval,qtrace〉 ≝ qvt in
2064        ptrace = qtrace ∧ pval = qval
2065      ]
2066    ]) → 
2067  exec_expr_sim
2068    (match r1 with [ OK x ⇒ Pok x | Error err ⇒ Error ? err ])
2069    (match r2 with [ OK x ⇒ Qok x | Error err ⇒ Error ? err ]).
2070#r1 #r2 #Pok #Qok whd in ⊢ (% → ?);
2071elim r1
2072[ 2:  #error #_ #_ normalize #a #Habsurd destruct (Habsurd)
2073| 1: normalize nodelta #a #H lapply (H a (refl ??))
2074     #Hr2 >Hr2 normalize #H #a' elim a * #b #o #tr
2075     lapply (H 〈b, o〉 tr) #H1 #H2 >H2 in H1;
2076     normalize nodelta elim a' in H2; #pval #ptrace #Hpok
2077     normalize nodelta cases (Qok 〈b,o,tr〉)
2078     [ 2: #error normalize #Habsurd @(False_ind … Habsurd)
2079     | 1: * #qval #qtrace normalize nodelta * #Htrace #Hval
2080          destruct @refl
2081] ] qed.
2082
2083
2084lemma exec_expr_expr_elim :
2085  ∀r1,r2,Pok,Qok.
2086  exec_expr_sim r1 r2 →
2087  (∀val,trace.
2088    match Pok 〈val,trace〉 with
2089    [ Error err ⇒ True
2090    | OK pvt ⇒
2091      let 〈pval,ptrace〉 ≝ pvt in
2092      match Qok 〈val,trace〉 with
2093      [ Error err ⇒ False
2094      | OK qvt ⇒
2095        let 〈qval,qtrace〉 ≝ qvt in
2096        ptrace = qtrace ∧ pval = qval
2097      ]
2098    ]) →
2099  exec_expr_sim
2100    (match r1 with [ OK x ⇒ Pok x | Error err ⇒ Error ? err ])
2101    (match r2 with [ OK x ⇒ Qok x | Error err ⇒ Error ? err ]).
2102#r1 #r2 #Pok #Qok whd in ⊢ (% → ?);
2103elim r1
2104[ 2: #error #_ #_ normalize #a1 #Habsurd destruct (Habsurd)
2105| 1: normalize nodelta #a #H lapply (H a (refl ??))
2106     #Hr2 >Hr2 normalize nodelta #H
2107     elim a in Hr2; #val #trace
2108     lapply (H … val trace)
2109     cases (Pok 〈val, trace〉)
2110     [ 2: #error normalize #_ #_ #a' #Habsurd destruct (Habsurd)
2111     | 1: * #pval #ptrace normalize nodelta
2112          cases (Qok 〈val,trace〉)
2113          [ 2: #error normalize #Hfalse @(False_ind … Hfalse)
2114          | 1: * #qval #qtrace normalize nodelta * #Htrace_eq #Hval_eq
2115               #Hr2_eq destruct #a #H @H
2116] ] ] qed.
2117
2118lemma load_value_of_type_inj : ∀m1,m2,writeable,b,off,ty.
2119    sr_memext m1 m2 writeable → ∀v.
2120    load_value_of_type ty m1 b off = Some ? v →
2121    load_value_of_type ty m2 b off = Some ? v.
2122#m1 #m2 #writeable #b #off #ty #Hinj #v
2123@(load_sim_fe ?? (sr_memext_load_sim … Hinj) (mk_pointer b off))
2124qed.
2125
2126(* Conservation of the semantics of binary operators under memory extensions.
2127   Tried to factorise it a bit but the play with indexes just becomes too messy.  *)
2128lemma sim_sem_binary_operation : ∀op,v1,v2,e1,e2,m1,m2,target_type,writeable.
2129  ∀Hext:sr_memext m1 m2 writeable. ∀res.
2130  sem_binary_operation op v1 (typeof e1) v2 (typeof e2) m1 target_type = Some ? res →
2131  sem_binary_operation op v1 (typeof e1) v2 (typeof e2) m2 target_type = Some ? res.
2132#op #v1 #v2 #e1 #e2 #m1 #m2 #target_type #writeable #Hmemext #res cases op
2133whd in match (sem_binary_operation ???????);
2134try //
2135whd in match (sem_binary_operation ???????);
2136lapply (me_valid_pointers … Hmemext)
2137lapply (me_not_writeable_ptr … Hmemext)
2138elim m1 in Hmemext; #contents1 #nextblocks1 #Hnextpos1
2139elim m2 #contents2 #nextblocks2 #Hnextpos2
2140* #Hnonempty #Hwriteable #Hnot_writeable #Hnot_writeable_ptr #Hvalid
2141whd in match (sem_cmp ??????);
2142whd in match (sem_cmp ??????);
2143[ 1,2: cases (classify_cmp (typeof e1) (typeof e2))
2144     normalize nodelta
2145     [ 1,4: #sz #sg try //
2146     | 2,5: #opt #ty
2147          cases v1 normalize nodelta
2148          [ 1,5: | 2,6: #sz #i | 3,7: | 4,8: #ptr ]
2149          [ 1,2,3,4: #Habsurd destruct (Habsurd)
2150          | 5,6: #H @H ]
2151          cases v2 normalize nodelta
2152          [ 1,5: | 2,6: #sz' #i' | 3,7: | 4,8: #ptr' ]
2153          [ 1,2,3,4: #Habsurd destruct (Habsurd)
2154          | 5,6: #H @H ]
2155          lapply (Hvalid ptr)
2156          cases (valid_pointer (mk_mem contents1 nextblocks1 Hnextpos1) ptr)
2157          [ 2,4: >andb_lsimpl_false normalize nodelta cases (eq_block ??) #_ normalize #Habsurd destruct (Habsurd) ]
2158          #Hvalid' >(Hvalid' (refl ??))
2159          lapply (Hvalid ptr')
2160          cases (valid_pointer (mk_mem contents1 nextblocks1 Hnextpos1) ptr')
2161          [ 2,4: >andb_lsimpl_true #_ normalize nodelta cases (eq_block ??) normalize nodelta #Habsurd destruct (Habsurd) ]
2162          #H' >(H' (refl ??)) >andb_lsimpl_true normalize nodelta #H @H
2163     | 3,6: #ty1 #ty2 #H @H ]
2164| 3,4: cases (classify_cmp (typeof e1) (typeof e2))
2165     normalize nodelta
2166     [ 1,4: #sz #sg try //
2167     | 2,5: #opt #ty
2168          cases v1 normalize nodelta
2169          [ 1,5: | 2,6: #sz #i | 3,7: | 4,8: #ptr ]
2170          [ 1,2,3,4: #Habsurd destruct (Habsurd)
2171          | 5,6: #H @H ]
2172          cases v2 normalize nodelta
2173          [ 1,5: | 2,6: #sz' #i' | 3,7: | 4,8: #ptr' ]
2174          [ 1,2,3,4: #Habsurd destruct (Habsurd)
2175          | 5,6: #H @H ]
2176          lapply (Hvalid ptr)
2177          cases (valid_pointer (mk_mem contents1 nextblocks1 Hnextpos1) ptr)
2178          [ 2,4: >andb_lsimpl_false normalize nodelta cases (eq_block ??) #_ normalize #Habsurd destruct (Habsurd) ]
2179          #Hvalid' >(Hvalid' (refl ??))
2180          lapply (Hvalid ptr')
2181          cases (valid_pointer (mk_mem contents1 nextblocks1 Hnextpos1) ptr')
2182          [ 2,4: >andb_lsimpl_true #_ normalize nodelta cases (eq_block ??) normalize nodelta #Habsurd destruct (Habsurd) ]
2183          #H' >(H' (refl ??)) >andb_lsimpl_true normalize nodelta #H @H
2184     | 3,6: #ty1 #ty2 #H @H ]     
2185| 5,6: cases (classify_cmp (typeof e1) (typeof e2))
2186     normalize nodelta
2187     [ 1,4: #sz #sg try //
2188     | 2,5: #opt #ty
2189          cases v1 normalize nodelta
2190          [ 1,5: | 2,6: #sz #i | 3,7: | 4,8: #ptr ]
2191          [ 1,2,3,4: #Habsurd destruct (Habsurd)
2192          | 5,6: #H @H ]
2193          cases v2 normalize nodelta
2194          [ 1,5: | 2,6: #sz' #i' | 3,7: | 4,8: #ptr' ]
2195          [ 1,2,3,4: #Habsurd destruct (Habsurd)
2196          | 5,6: #H @H ]
2197          lapply (Hvalid ptr)
2198          cases (valid_pointer (mk_mem contents1 nextblocks1 Hnextpos1) ptr)
2199          [ 2,4: >andb_lsimpl_false normalize nodelta cases (eq_block ??) #_ normalize #Habsurd destruct (Habsurd) ]
2200          #Hvalid' >(Hvalid' (refl ??))
2201          lapply (Hvalid ptr')
2202          cases (valid_pointer (mk_mem contents1 nextblocks1 Hnextpos1) ptr')
2203          [ 2,4: >andb_lsimpl_true #_ normalize nodelta cases (eq_block ??) normalize nodelta #Habsurd destruct (Habsurd) ]
2204          #H' >(H' (refl ??)) >andb_lsimpl_true normalize nodelta #H @H
2205     | 3,6: #ty1 #ty2 #H @H ]
2206] qed.
2207
2208(* Simulation relation on expressions *)
2209lemma sim_related_globals : ∀ge,ge',en1,m1,en2,m2,writeable,ext.
2210  ∀Hext:sr_memext m1 m2 writeable.
2211  switch_removal_globals ? fundef_switch_removal ge ge' →
2212  disjoint_extension en1 en2 ext →
2213(*  disjoint_extension en1 en2 ext Hext → *)
2214  ext_fresh_for_genv ext ge →
2215  (∀e. exec_expr_sim (exec_expr ge en1 m1 e) (exec_expr ge' en2 m2 e)) ∧
2216  (∀ed, ty. exec_lvalue_sim (exec_lvalue' ge en1 m1 ed ty) (exec_lvalue' ge' en2 m2 ed ty)).
2217#ge #ge' #en1 #m1 #en2 #m2 #writeable #ext #Hext #Hrelated #Hdisjoint (* #Hdisjoint *) #Hext_fresh_for_genv
2218@expr_lvalue_ind_combined
2219[ 1: #csz #cty #i #a1
2220     whd in match (exec_expr ????); elim cty
2221     [ | #sz #sg | #ty | #ty #n | #tl #ty | #id #fl | #id #fl | #ty ]
2222     normalize nodelta
2223     [ 2: cases (eq_intsize csz sz) normalize nodelta
2224          [ 1: #H destruct (H) /4 by ex_intro, conj, vint_eq/
2225          | 2: #Habsurd destruct (Habsurd) ]
2226     | 3,4,5: #_ #H destruct (H)
2227     | *: #H destruct (H) ]
2228| 2: *
2229  [ #sz #i | #var_id | #e1 | #e1 | #op #e1 | #op #e1 #e2 | #cast_ty #e1
2230  | #cond #iftrue #iffalse | #e1 #e2 | #e1 #e2 | #sizeofty | #e1 #field | #cost #e1 ]
2231  #ty whd in ⊢ (% → ?); #Hind try @I
2232  whd in match (Plvalue ???);
2233  [ 1,2,3: whd in match (exec_expr ????); whd in match (exec_expr ????); #a1
2234       cases (exec_lvalue' ge en1 m1 ? ty) in Hind;
2235       [ 2,4,6: #error #_ normalize in ⊢ (% → ?); #Habsurd destruct (Habsurd)
2236       | 1,3,5: normalize nodelta #b1 #H lapply (H b1 (refl ??)) #Heq >Heq       
2237           normalize nodelta
2238           elim b1 * #bl1 #o1 #tr1 (* elim b2 * #bl2 #o2 #tr2 *)
2239           whd in match (load_value_of_type' ???);
2240           whd in match (load_value_of_type' ???);
2241           lapply (load_value_of_type_inj m1 m2 writeable bl1 o1 ty Hext)
2242           cases (load_value_of_type ty m1 bl1 o1)
2243           [ 1,3,5: #_ #Habsurd normalize in Habsurd; destruct (Habsurd)
2244           | 2,4,6: #v #H normalize in ⊢ (% → ?); #Heq destruct (Heq)
2245                    >(H v (refl ??)) @refl
2246  ] ] ]
2247| 3: #v #ty whd * * #b #o #tr
2248     whd in match (exec_lvalue' ?????);
2249     whd in match (exec_lvalue' ?????); cases Hdisjoint *
2250     #HA #HB #HC lapply (HA v) lapply (HB v) lapply (HC v) -HA -HB -HC
2251     lapply (Hext_fresh_for_genv v)
2252     cases (mem_assoc_env v ext) #Hglobal
2253     [ 1: >(Hglobal (refl ??)) #_ #HB #HA >(HA (refl ??)) normalize
2254          #Habsurd destruct
2255     | 2: normalize nodelta #Hsim #_ #_
2256          cases (lookup ?? en1 v) in Hsim; normalize nodelta
2257          [ 1: #Hlookup2 <(Hlookup2 (refl ??)) normalize nodelta
2258               lapply (rg_find_symbol … Hrelated v) #Heq_find_sym >Heq_find_sym
2259               #H @H
2260          | 2: #blo #Hlookup2 <(Hlookup2 (refl ??)) #Heq normalize nodelta @Heq ] ]
2261| 4: #e #ty whd in ⊢ (% → %);
2262     whd in match (exec_lvalue' ?????);
2263     whd in match (exec_lvalue' ?????);
2264     cases (exec_expr ge en1 m1 e)
2265     [ 1: * #v1 #tr1 #H elim (H 〈v1,tr1〉 (refl ??)) * #v1' #tr1' #H @H
2266     | 2: #error #_ normalize #a1 #Habsurd destruct (Habsurd) ]
2267| 5: #ty #e #ty'
2268     #Hsim @(exec_lvalue_expr_elim … Hsim)
2269     cases ty
2270     [ | #sz #sg | #ty | #ty #n | #tl #ty | #id #fl | #id #fl | #ty ]
2271     * #b #o normalize nodelta try /2 by I/
2272     #tr @conj try @refl
2273| 6: #ty #op #e
2274     #Hsim @(exec_expr_expr_elim … Hsim) #v #trace
2275     cases (sem_unary_operation op v (typeof e)) normalize nodelta
2276     try @I
2277     #v @conj @refl
2278| 7: #ty #op #e1 #e2 #Hsim1 #Hsim2
2279     @(exec_expr_expr_elim … Hsim1) #v #trace
2280     cases (exec_expr ge en1 m1 e2) in Hsim2;
2281     [ 2: #error // ]
2282     * #pval #ptrace normalize in ⊢ (% → ?); #Hsim2
2283     whd in match (m_bind ?????);
2284     >(Hsim2 ? (refl ??))
2285     whd in match (m_bind ?????);
2286     lapply (sim_sem_binary_operation op v pval e1 e2 m1 m2 ty writeable Hext)
2287     cases (sem_binary_operation op v (typeof e1) pval (typeof e2) m1 ty)
2288     [ 1: #_ // ] #val #H >(H val (refl ??))
2289     normalize destruct @conj @refl
2290| 8: #ty #cast_ty #e #Hsim @(exec_expr_expr_elim … Hsim)
2291     #v #tr
2292     cut (exec_cast m1 v (typeof e) cast_ty = exec_cast m2 v (typeof e) cast_ty)
2293     [ @refl ]
2294     #Heq >Heq     
2295     cases (exec_cast m2 v (typeof e) cast_ty)
2296     [ 2: //
2297     | 1: #v normalize @conj @refl ]
2298| 9: #ty #e1 #e2 #e3 #Hsim1 #Hsim2 #Hsim3
2299     @(exec_expr_expr_elim … Hsim1) #v #tr
2300     cases (exec_bool_of_val ? (typeof e1)) #b
2301     [ 2: normalize @I ]
2302     cases b normalize nodelta
2303     whd in match (m_bind ?????);
2304     whd in match (m_bind ?????);
2305     normalize nodelta
2306     [ 1: (* true branch *)
2307          cases (exec_expr ge en1 m1 e2) in Hsim2;
2308          [ 2: #error normalize #_ @I
2309          | 1: * #e2v #e2tr normalize #H >(H ? (refl ??)) normalize nodelta
2310               @conj @refl ]
2311     | 2: (* false branch *)
2312          cases (exec_expr ge en1 m1 e3) in Hsim3;
2313          [ 2: #error normalize #_ @I
2314          | 1: * #e3v #e3tr normalize #H >(H ? (refl ??)) normalize nodelta
2315               @conj @refl ] ]
2316| 10,11: #ty #e1 #e2 #Hsim1 #Hsim2
2317     @(exec_expr_expr_elim … Hsim1) #v #tr
2318     cases (exec_bool_of_val v (typeof e1))
2319     [ 2,4: #error normalize @I ]
2320     *
2321     whd in match (m_bind ?????);
2322     whd in match (m_bind ?????);
2323     [ 2,3: cases (cast_bool_to_target ty ?) normalize // #v @conj try @refl ]
2324     cases (exec_expr ge en1 m1 e2) in Hsim2;
2325     [ 2,4: #error #_ normalize @I ]
2326     * #v2 #tr2 whd in ⊢ (% → %); #H2 normalize nodelta >(H2 ? (refl ??))
2327     normalize nodelta
2328     cases (exec_bool_of_val v2 (typeof e2))
2329     [ 2,4: #error normalize @I ]
2330     *
2331     whd in match (m_bind ?????);
2332     cases (cast_bool_to_target ty ?) normalize // #v @conj try @refl
2333| 12: #ty #ty' cases ty
2334     [ | #sz #sg | #ty | #ty #n | #tl #ty | #id #fl | #id #fl | #ty ]
2335     whd in match (exec_expr ????); whd #a #H @H
2336| 13: #ty #ed #aggregty #i #Hsim whd * * #b #o #tr
2337    whd in match (exec_lvalue' ?????);
2338    whd in match (exec_lvalue' ge' en2 m2 (Efield (Expr ed aggregty) i) ty);
2339    whd in match (typeof ?);
2340    cases aggregty in Hsim;
2341    [ | #sz #sg | #ty | #ty #n | #tl #ty | #id #fl | #id #fl | #ty ]
2342    normalize nodelta #Hsim
2343    [ 1,2,3,4,5,8: #Habsurd destruct (Habsurd) ]
2344    whd in match (m_bind ?????);
2345    whd in match (m_bind ?????);
2346    whd in match (exec_lvalue ge en1 m1 (Expr ed ?));
2347    cases (exec_lvalue' ge en1 m1 ed ?) in Hsim;
2348    [ 2,4: #error #_ normalize in ⊢ (% → ?); #Habsurd destruct (Habsurd) ]
2349    * * #b1 #o1 #tr1 whd in ⊢ (% → ?); #H
2350    whd in match (exec_lvalue ge' en2 m2 (Expr ed ?));   
2351     >(H ? (refl ??)) normalize nodelta #H @H
2352| 14: #ty #l #e #Hsim
2353     @(exec_expr_expr_elim … Hsim) #v #tr normalize nodelta @conj //
2354| 15: *
2355  [ #sz #i | #var_id | #e1 | #e1 | #op #e1 | #op #e1 #e2 | #cast_ty #e1
2356  | #cond #iftrue #iffalse | #e1 #e2 | #e1 #e2 | #sizeofty | #e1 #field | #cost #e1 ]
2357  #ty normalize in ⊢ (% → ?);
2358  [ 2,3,12: @False_ind
2359  | *: #_ normalize #a1 #Habsurd @Habsurd ]
2360] qed.
2361
2362lemma exec_lvalue_sim_aux : ∀ge,ge',env,env_ext,m,m_ext.
2363  (∀ed,ty. exec_lvalue_sim (exec_lvalue' ge env m ed ty)
2364                           (exec_lvalue' ge' env_ext m_ext ed ty)) →
2365  ∀e. exec_lvalue_sim (exec_lvalue ge env m e)
2366                      (exec_lvalue ge' env_ext m_ext e).
2367#ge #ge' #env #env_ext #m #m_ext #H * #ed #ty @H qed.
2368
2369lemma exec_expr_sim_to_exec_exprlist :
2370  ∀ge,ge',en1,en2,m1,m2.
2371  (∀e. exec_expr_sim (exec_expr ge en1 m1 e) (exec_expr ge' en2 m2 e)) →
2372   ∀l. res_sim ? (exec_exprlist ge en1 m1 l) (exec_exprlist ge' en2 m2 l).
2373#ge #ge' #en1 #en2 #m1 #m2 #Hsim #l elim l
2374[ 1: whd #a #Heq normalize in Heq ⊢ %; destruct @refl
2375| 2: #hd #tl #Hind whd * #lv #tr whd in ⊢ ((??%?) → (??%?));
2376     lapply (Hsim hd)
2377     cases (exec_expr ge en1 m1 hd)
2378     [ 2: #error normalize #_ #Habsurd destruct (Habsurd)
2379     | 1: * #v #vtr whd in ⊢ (% → ?); #Hsim >(Hsim ? (refl ??))
2380          normalize nodelta
2381          cases (exec_exprlist ge en1 m1 tl) in Hind;
2382          [ 2: #error normalize #_ #Habsurd destruct (Habsurd)
2383          | 1: #a normalize #H >(H ? (refl ??)) #Heq destruct normalize @refl
2384          ]
2385     ]
2386] qed.
2387
2388(* The return type of any function is invariant under switch removal *)
2389lemma fn_return_simplify : ∀f. fn_return (\fst (function_switch_removal f)) = fn_return f.
2390#f elim f #ty #args #vars #body whd in match (function_switch_removal ?);
2391cases (switch_removal ??) * #stmt #fvs #u @refl
2392qed.
2393
2394(* Similar stuff for fundefs *)
2395lemma fundef_type_simplify : ∀clfd. type_of_fundef clfd = type_of_fundef (fundef_switch_removal clfd).
2396* // * #ty #args #vars #body whd in ⊢ (??%%);
2397whd in match (function_switch_removal ?); cases (switch_removal ??) * #st #u
2398normalize nodelta #u @refl
2399qed.
2400
2401lemma while_fresh_lift : ∀e,s,u.
2402   fresh_for_expression e u → fresh_for_statement s u → fresh_for_statement (Swhile e s) u.
2403#e #s * #u whd in ⊢ (% → % → %); whd in match (max_of_statement (Swhile ??));
2404cases (max_of_expr e) #e cases (max_of_statement s) #s normalize
2405cases (leb e s) try /2/
2406qed.
2407
2408(*
2409lemma while_commute : ∀e0, s0, us0. Swhile e0 (switch_removal s0 us0) = (sw_rem (Swhile e0 s0) us0).
2410#e0 #s0 #us0 normalize
2411cases (switch_removal s0 us0) * #body #newvars #u' normalize //
2412qed.*)
2413
2414lemma dowhile_fresh_lift : ∀e,s,u.
2415   fresh_for_expression e u → fresh_for_statement s u → fresh_for_statement (Sdowhile e s) u.
2416#e #s * #u whd in ⊢ (% → % → %); whd in match (max_of_statement (Sdowhile ??));
2417cases (max_of_expr e) #e cases (max_of_statement s) #s normalize
2418cases (leb e s) try /2/
2419qed.
2420
2421(*
2422lemma dowhile_commute : ∀e0, s0, us0. Sdowhile e0 (sw_rem s0 us0) = (sw_rem (Sdowhile e0 s0) us0).
2423#e0 #s0 #us0 normalize
2424cases (switch_removal s0 us0) * #body #newvars #u' normalize //
2425qed.*)
2426
2427lemma for_fresh_lift : ∀cond,step,body,u.
2428  fresh_for_statement step u →
2429  fresh_for_statement body u →
2430  fresh_for_expression cond u →
2431  fresh_for_statement (Sfor Sskip cond step body) u.
2432#cond #step #body #u
2433whd in ⊢ (% → % → % → %); whd in match (max_of_statement (Sfor ????));
2434cases (max_of_statement step) #s
2435cases (max_of_statement body) #b
2436cases (max_of_expr cond) #c
2437whd in match (max_of_statement Sskip);
2438>(max_id_commutative least_identifier)
2439>max_id_one_neutral normalize nodelta
2440normalize elim u #u
2441cases (leb s b) cases (leb c b) cases (leb c s) try /2/
2442qed.
2443
2444(*
2445lemma for_commute : ∀e,stm1,stm2,u,uA.
2446   (uA=\snd  (switch_removal stm1 u)) →
2447   sw_rem (Sfor Sskip e stm1 stm2) u = (Sfor Sskip e (sw_rem stm1 u) (sw_rem stm2 uA)).
2448#e #stm1 #stm2 #u #uA #HuA
2449whd in match (sw_rem (Sfor ????) u);
2450whd in match (switch_removal ??);   
2451destruct
2452normalize in match (\snd (switch_removal Sskip u));
2453whd in match (sw_rem stm1 u);
2454cases (switch_removal stm1 u)
2455* #stm1' #fresh_vars #uA normalize nodelta
2456whd in match (sw_rem stm2 uA);
2457cases (switch_removal stm2 uA)
2458* #stm2' #fresh_vars2 #uB normalize nodelta
2459//
2460qed.*)
2461
2462lemma simplify_is_not_skip : ∀s. s ≠ Sskip → ∀u. ∃pf. is_Sskip (ret_st ? (switch_removal s u)) = inr ?? pf.
2463*
2464[ 1: * #H @(False_ind … (H (refl ??))) ]
2465try /2/
2466[ 1: #s1 #s2 #_ #u normalize
2467     cases (switch_removal ? ?) * #a #b #c normalize nodelta
2468     cases (switch_removal ? ?) * #e #f #g normalize nodelta
2469     /2 by ex_intro/
2470| 2: #e #s1 #s2 #_ #u normalize
2471     cases (switch_removal ? ?) * #a #b #c normalize nodelta
2472     cases (switch_removal ? ?) * #e #f #g normalize nodelta
2473     /2 by ex_intro/
2474| 3,4: #e #s #_ #u normalize
2475     cases (switch_removal ? ?) * #e #f #g normalize nodelta
2476     /2 by ex_intro/
2477| 5: #s1 #e #s2 #s3 #_ #u normalize     
2478     cases (switch_removal ? ?) * #a #b #c normalize nodelta
2479     cases (switch_removal ? ?) * #e #f #g normalize nodelta     
2480     cases (switch_removal ? ?) * #h #i #j normalize nodelta
2481     /2 by ex_intro/
2482| 6: #e #ls #_ #u normalize
2483     cases (switch_removal_branches ? ?) * #a #b #c normalize nodelta
2484     cases (fresh ??) #e #f normalize nodelta
2485     cases (fresh ? f) #g #h normalize nodelta
2486     cases (produce_cond ????) * #k #l #m normalize nodelta
2487     /2 by ex_intro/
2488| 7,8: #ls #st #_ #u normalize
2489     cases (switch_removal ? ?) * #e #f #g normalize nodelta     
2490     /2 by ex_intro/
2491] qed.
2492
2493(*
2494lemma sw_rem_commute : ∀stm,u.
2495  (\fst (\fst (switch_removal stm u))) = sw_rem stm u.
2496#stm #u whd in match (sw_rem stm u); // qed.
2497*)
2498
2499lemma fresh_for_statement_inv :
2500  ∀u,s. fresh_for_statement s u →
2501        match u with
2502        [ mk_universe p ⇒ le (p0 one) p ].
2503* #p #s whd in match (fresh_for_statement ??);
2504cases (max_of_statement s) #s
2505normalize /2/ qed.
2506
2507lemma fresh_for_Sskip :
2508  ∀u,s. fresh_for_statement s u → fresh_for_statement Sskip u.
2509#u #s #H lapply (fresh_for_statement_inv … H) elim u /2/ qed.
2510
2511lemma fresh_for_Sbreak :
2512  ∀u,s. fresh_for_statement s u → fresh_for_statement Sbreak u.
2513#u #s #H lapply (fresh_for_statement_inv … H) elim u /2/ qed.
2514
2515lemma fresh_for_Scontinue :
2516  ∀u,s. fresh_for_statement s u → fresh_for_statement Scontinue u.
2517#u #s #H lapply (fresh_for_statement_inv … H) elim u /2/ qed.
2518
2519(*
2520lemma switch_removal_eq : ∀s,u. ∃res,fvs,u'. switch_removal s u = 〈res, fvs, u'〉.
2521#s #u elim (switch_removal s u) * #res #fvs #u'
2522%{res} %{fvs} %{u'} //
2523qed.
2524
2525lemma switch_removal_branches_eq : ∀switchcases, u. ∃res,fvs,u'. switch_removal_branches switchcases u = 〈res, fvs, u'〉.
2526#switchcases #u elim (switch_removal_branches switchcases u) * #res #fvs #u'
2527%{res} %{fvs} %{u'} //
2528qed.
2529*)
2530
2531lemma produce_cond_eq : ∀e,ls,u,exit_label. ∃s,lab,u'. produce_cond e ls u exit_label = 〈s,lab,u'〉.
2532#e #ls #u #exit_label cases (produce_cond e ls u exit_label) *
2533#s #lab #u' %{s} %{lab} %{u'} //
2534qed.
2535
2536(* TODO: this lemma ought to be in a more central place, along with its kin of SimplifiCasts.ma ... *)
2537lemma neq_intsize : ∀s1,s2. s1 ≠ s2 → eq_intsize s1 s2 = false.
2538* * *
2539[ 1,5,9: #H @(False_ind … (H (refl ??)))
2540| *: #_ normalize @refl ]
2541qed.
2542
2543lemma exec_expr_int : ∀ge,e,m,expr.
2544    (∃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〉)).
2545#ge #e #m #expr cases (exec_expr ge e m expr)
2546[ 2: #error %2 #sz #n #tr % #H destruct (H)
2547| 1: * #val #trace cases val
2548     [ 2: #sz #n %1 %{sz} %{n} %{trace} @refl
2549     | 3: | 4: #ptr ]
2550     %2 #sz #n #tr % #H destruct (H)
2551] qed.
2552
2553lemma switch_removal_elim : ∀s,u. ∃s',fvs',u'. switch_removal s u = 〈s',fvs',u'〉.
2554#s #u cases (switch_removal s u) * #s' #fvs' #u'
2555%{s'} %{fvs'} %{u'} @refl
2556qed.
2557
2558lemma switch_removal_branches_elim : ∀ls,u. ∃ls',fvs',u'. switch_removal_branches ls u = 〈ls',fvs',u'〉.
2559#ls #u cases (switch_removal_branches ls u) * * #ls' #fvs' #u' /4 by ex_intro/ qed.
2560
2561lemma fresh_elim : ∀u. ∃fv',u'. fresh SymbolTag u = 〈fv', u'〉. #u /3 by ex_intro/ qed.
2562
2563lemma simplify_switch_elim : ∀e,ls,u. ∃res,u'. simplify_switch e ls u = 〈res, u'〉.
2564#e #ls #u cases (simplify_switch e ls u) #res #u /3 by ex_intro/ qed.
2565
2566lemma store_int_success :
2567       ∀b,m,sz,sg,i. valid_block m b → low (blocks m b) = OZ → high (blocks m b) = sizeof (Tint sz sg) →
2568                     ∃m'. store_value_of_type (Tint sz sg) m b zero_offset (Vint sz i) = Some ? m'.
2569#b #m #sz #sg
2570cases sz
2571[ 1: #i #Hvalid #Hlow #Hhigh
2572     whd in match (store_value_of_type ?????);
2573     whd in match (fe_to_be_values ??);
2574     normalize nodelta     
2575     normalize in match (size_intsize ?);
2576     whd in match (bytes_of_bitvector ??);     
2577     lapply (vsplit_eq2 ? 8 0 i) * #li * #ri #Heq_i
2578      <(vsplit_prod … Heq_i) normalize nodelta
2579      >(BitVector_O … ri) whd in match (storen ???);
2580      lapply (valid_pointer_to_bestorev_ok m (mk_pointer b zero_offset) (BVByte li) ?)
2581      [ 1: whd in match (valid_pointer ??); >(Zlt_to_Zltb_true ?? Hvalid) >andb_lsimpl_true
2582           >unfold_low_bound >unfold_high_bound >Hlow >Hhigh
2583           >(Zle_to_Zleb_true … (reflexive_Zle OZ)) normalize nodelta
2584           @Zlt_to_Zltb_true // ]
2585      * #m' #Hbestorev >Hbestorev %{m'} @refl
2586| 2:  #i #Hvalid #Hlow #Hhigh
2587     whd in match (store_value_of_type ?????);
2588     whd in match (fe_to_be_values ??);
2589     normalize nodelta     
2590     normalize in match (size_intsize ?);
2591     whd in match (bytes_of_bitvector ??);             
2592     lapply (vsplit_eq2 ? 8 (1*8) i) * #li * #ri #Heq_i
2593     <(vsplit_prod … Heq_i) normalize nodelta whd in match (storen ???);
2594      lapply (valid_pointer_to_bestorev_ok m (mk_pointer b zero_offset) (BVByte li) ?)
2595      [ 1: whd in match (valid_pointer ??); >(Zlt_to_Zltb_true ?? Hvalid) >andb_lsimpl_true
2596           >unfold_low_bound >unfold_high_bound >Hlow >Hhigh
2597           >(Zle_to_Zleb_true … (reflexive_Zle OZ)) normalize nodelta
2598           @Zlt_to_Zltb_true // ]
2599      * #m0 #Hbestorev >Hbestorev normalize nodelta
2600      whd in match (bytes_of_bitvector ??);         
2601      lapply (vsplit_eq2 ? 8 (0*8) ri) * #rli * #rri #Heq_ri
2602      <(vsplit_prod … Heq_ri) normalize nodelta
2603      cases (mem_bounds_invariant_after_bestorev … Hbestorev) * * * #Hnext0 #Hblocks0 #_ #_ #_
2604      lapply (valid_pointer_to_bestorev_ok m0
2605                (mk_pointer b (mk_offset
2606                     [[false; false; false; false; false; false; false; false; 
2607                       false; false; false; false; false; false; false; true]]))
2608                 (BVByte rli) ?)
2609      [ 1: whd in match (valid_pointer ??); >Hnext0 >(Zlt_to_Zltb_true ?? Hvalid) >andb_lsimpl_true
2610           cases (Hblocks0 b) #HA #HB
2611           >unfold_low_bound >unfold_high_bound >HA >HB >Hlow >Hhigh normalize nodelta
2612           @Zlt_to_Zltb_true normalize // ]
2613      * #m1 #Hbestorev1 %{m1} whd in ⊢ (??(???%)?); whd in match (storen ???);
2614      normalize in match (shift_pointer ???); >Hbestorev1 normalize nodelta
2615      @refl
2616| 3:  #i #Hvalid #Hlow #Hhigh
2617     whd in match (store_value_of_type ?????);
2618     whd in match (fe_to_be_values ??);
2619     normalize nodelta     
2620     normalize in match (size_intsize ?);
2621     whd in match (bytes_of_bitvector ??);             
2622     lapply (vsplit_eq2 ? 8 (3*8) i) * #iA * #iB #Heq_i
2623     <(vsplit_prod … Heq_i) normalize nodelta whd in match (storen ???);
2624      lapply (valid_pointer_to_bestorev_ok m (mk_pointer b zero_offset) (BVByte iA) ?)
2625      [ 1: whd in match (valid_pointer ??); >(Zlt_to_Zltb_true ?? Hvalid) >andb_lsimpl_true
2626           >unfold_low_bound >unfold_high_bound >Hlow >Hhigh
2627           >(Zle_to_Zleb_true … (reflexive_Zle OZ)) normalize nodelta
2628           @Zlt_to_Zltb_true // ]
2629      * #m0 #Hbestorev >Hbestorev normalize nodelta
2630      whd in match (bytes_of_bitvector ??);
2631      lapply (vsplit_eq2 ? 8 (2*8) iB) * #iC * #iD #Heq_iB
2632      <(vsplit_prod … Heq_iB) normalize nodelta
2633      cases (mem_bounds_invariant_after_bestorev … Hbestorev) * * * #Hnext0 #Hblocks0 #_ #_ #_   
2634      lapply (valid_pointer_to_bestorev_ok m0
2635                (shift_pointer 2 (mk_pointer b zero_offset) (bitvector_of_nat 2 1))               
2636                (BVByte iC) ?)
2637      [ 1: whd in match (valid_pointer ??); >Hnext0 >(Zlt_to_Zltb_true ?? Hvalid) >andb_lsimpl_true
2638           cases (Hblocks0 b) #HA #HB
2639           >unfold_low_bound >unfold_high_bound >HA >HB >Hlow >Hhigh normalize nodelta
2640           @Zlt_to_Zltb_true normalize // ]
2641      * #m1 #Hbestorev1 whd in ⊢ (??(λ_.??(???%)?)); whd in match (storen ???);
2642      normalize in match (shift_pointer 2 (mk_pointer b zero_offset) (bitvector_of_nat 2 1));
2643      >Hbestorev1 normalize nodelta
2644      lapply (vsplit_eq2 ? 8 (1*8) iD) * #iE * #iF #Heq_iD
2645      whd in match (bytes_of_bitvector ??);
2646      <(vsplit_prod … Heq_iD) normalize nodelta
2647      whd in ⊢ (??(λ_.??(???%)?));
2648      whd in match (storen ???);
2649      cases (mem_bounds_invariant_after_bestorev … Hbestorev1) * * * #Hnext1 #Hblocks1 #_ #_ #_
2650      lapply (valid_pointer_to_bestorev_ok m1
2651                (shift_pointer 2 (mk_pointer b (mk_offset
2652                   [[ false; false; false; false; false; false; false; false; false; false;
2653                      false; false; false; false; false; true ]]))
2654                (bitvector_of_nat 2 1))
2655                (BVByte iE) ?)
2656      [ 1: normalize in match (shift_pointer ???); whd in match (valid_pointer ??);
2657           >Hnext1 >Hnext0 >(Zlt_to_Zltb_true ?? Hvalid)
2658           >andb_lsimpl_true cases (Hblocks1 b) #HA #HB cases (Hblocks0 b) #HC #HD
2659           >unfold_low_bound >unfold_high_bound >HA >HB >HC >HD >Hlow >Hhigh normalize nodelta
2660           @Zlt_to_Zltb_true normalize // ]
2661      * #m2 #Hbestorev2 >Hbestorev2 normalize nodelta
2662      whd in match (bytes_of_bitvector ??);
2663      lapply (vsplit_eq2 ? 8 (0*8) iF) * #iG * #iH #Heq_iF
2664      <(vsplit_prod … Heq_iF) normalize nodelta
2665      >(BitVector_O … iH) whd in ⊢ (??(λ_.??(???%)?));
2666      whd in match (storen ???);     
2667      cases (mem_bounds_invariant_after_bestorev … Hbestorev2) * * * #Hnext2 #Hblocks2 #_ #_ #_
2668      lapply (valid_pointer_to_bestorev_ok m2
2669                (shift_pointer 2 (shift_pointer 2 (mk_pointer b (mk_offset
2670                   [[ false; false; false; false; false; false; false; false; false; false;
2671                      false; false; false; false; false; true ]]))
2672                (bitvector_of_nat 2 1)) (bitvector_of_nat 2 1))
2673                (BVByte iG) ?)
2674      [ 1: normalize in match (shift_pointer ???); whd in match (valid_pointer ??);
2675           >Hnext2 >Hnext1 >Hnext0 >(Zlt_to_Zltb_true ?? Hvalid)
2676           >andb_lsimpl_true cases (Hblocks2 b) #HA #HB cases (Hblocks1 b) #HC #HD cases (Hblocks0 b) #HE #HF
2677           >unfold_low_bound >unfold_high_bound >HA >HB >HC >HD >HE >HF >Hlow >Hhigh normalize nodelta
2678           @Zlt_to_Zltb_true normalize // ]         
2679      * #m3 #Hbestorev3 >Hbestorev3 normalize nodelta %{m3} @refl
2680] qed.           
2681
2682
2683(* Main theorem.
2684   9th November 2012
2685   We decided to interrupt the development of this particular proof. What follows is a description of what
2686   has to be done in order to finish it.
2687   
2688   What has been done up to now is the simulation proof for all "easy" cases, that do not interact with the
2689   switch removal per se, plus a bit of switch. This still implies propagating the memory extension through
2690   each statement (except switch), as well as various invariants that are needed for the switch case.
2691
2692   The proof for the switch case has been started. Here is how I picture the simulation proof.
2693   The simulation proof must be broken down in several steps. The source statement executes as this for the first step :
2694
2695   mem, env, k
2696   -----------------------------------------------------
2697   switch(e) case_list ===>
2698      e ⇓ Vint i,
2699      case_list' ← select_switch i case_list;
2700   Result = State  (seq_of_labeled_statement case_list') (Kswitch k) env mem
2701     
2702   The resulting statement executes like this.
2703   
2704   mem ⊕ writeable, env ⊕ ext, k'
2705   fresh ∈ dom(ext)
2706   ext(fresh) ∈ writeable
2707   -----------------------------------------------------
2708   fresh = e;
2709   if(e == case0) {       ---
2710     substatement0;         |
2711     goto next0;            |         
2712   } else { };              |
2713   if(e == case1) {         |-  = converted_cases
2714     label next0:           |
2715     substatement1;         |
2716     goto next1;            |
2717   } else { };            ---
2718        ... ===>   
2719   Result = State (fresh = e) (Kseq converted_cases k) (env ⊕ ext) (mem ⊕ writeable)
2720           ===>
2721        fresh ⇓ Loc l;
2722        e ⇓ Vint i;
2723        m' → store_value_of_type' (typeof a1) m l (Vint i)
2724   Result = State Sskip (Kseq converted_cases k) (env ⊕ ext) (m' ⊕ writeable)
2725          ===>
2726   Result = State converted_cases k (env ⊕ ext) (m' ⊕ writeable)
2727   This has been done. But this state is still not equivalent with the source one.
2728   TODO 1: we must prove that after a finite number of Ssequence in [converted_cases], we
2729           stumble upon a "if(e == casen) { blahblah } else {}; foo" that corresponds to "(seq_of_labeled_statement case_list')"
2730           (remember that "case_list'" has been truncated to the case corresponding to "i").
2731   TODO 2: the resulting pair of states will not be in the standard simulation relation currently defined in
2732            [switch_state_sim]. We must come up with an additional set of relations with enough informations
2733            to handle the gotos :
2734            1. the gotos from one if to the other avoiding the execution of conditions
2735            2. most importantly, the gotos into which "break"s have been converted !
2736            This particular subset of the simulation will need some equations allowing to prove that
2737            the current continuation actually contains a label corresponding to the break.
2738            Note that when encountering e.g. a while loop inside a converted case, breaks should stop
2739            beeing converted to gotos and we should go to the 'standard' simulation relation.
2740   TODO 3: some standard cases remain after that, nothing special (halt case ...).
2741   
2742   This should be about it. TODO 1 and 2 will probably require some form of induction over switch cases ...
2743*)
2744
2745theorem switch_removal_correction :
2746  ∀ge,ge'.
2747  switch_removal_globals ? fundef_switch_removal ge ge' →
2748  ∀s1,s1',tr,s2.
2749  switch_state_sim ge s1 s1' →
2750  exec_step ge s1 = Value … 〈tr,s2〉 → 
2751  ∃n. after_n_steps (S n) … clight_exec ge' s1' (λ_. true)
2752  (λtr',s2'. tr = tr' ∧ switch_state_sim ge' s2 s2').
2753#ge #ge' #Hrelated #s1 #s1' #tr #s2 #Hsim_state
2754inversion Hsim_state
2755[ 1: (* regular state *)
2756  #sss_statement #sss_lu #sss_lu_fresh #sss_func #sss_func_tr #sss_new_vars
2757  #sss_func_hyp #sss_m #sss_m_ext #sss_env #sss_env_ext #sss_k #sss_k_ext #sss_writeable #sss_mem_hyp
2758  #sss_env_hyp #sss_new_alloc #sss_enclosing_label #sss_writeable_hyp #sss_result_rec #sss_result_hyp
2759  #sss_result #sss_result_proj #sss_incl #sss_k_hyp #Hext_fresh_for_ge
2760  #Hs1_eq #Hs1_eq'
2761  elim (sim_related_globals … ge ge'
2762             sss_env sss_m sss_env_ext sss_m_ext sss_writeable sss_new_vars
2763             sss_mem_hyp Hrelated sss_env_hyp Hext_fresh_for_ge)
2764  #Hsim_expr #Hsim_lvalue #_
2765  (* II. Case analysis on the statement. *)
2766  cases sss_statement in sss_lu_fresh sss_result_hyp;
2767  (* Perform the intros for the statements *)
2768  [ 1: | 2: #lhs #rhs | 3: #retv #func #args | 4: #stm1 #stm2 | 5: #cond #iftrue #iffalse | 6: #cond #body
2769  | 7: #cond #body | 8: #init #cond #step #body | 9,10: | 11: #retval | 12: #cond #switchcases | 13: #lab #body
2770  | 14: #lab | 15: #cost #body ]
2771  #sss_lu_fresh #sss_result_hyp
2772  [ 1: (* Skip statement *)
2773    whd in match (switch_removal ??) in sss_result_hyp; >sss_result_proj <sss_result_hyp
2774    (* III. Case analysis on the continuation. *)
2775    inversion sss_k_hyp normalize nodelta
2776    [ 1: #new_vars #Hnew_vars_eq #Hk #Hk' #_ #Hexec_step %{0} whd whd in ⊢ (??%?);
2777         >(prod_eq_lproj ????? sss_func_hyp)
2778         >fn_return_simplify
2779         whd in match (exec_step ??) in Hexec_step;
2780         (* IV. Case analysis on the return type *)
2781         cases (fn_return sss_func) in Hexec_step;         
2782         [ | #sz #sg | #ptr_ty | #array_ty #array_sz | #domain #codomain
2783         | #structname #fieldspec | #unionname #fieldspec | #id ]
2784         normalize nodelta
2785         whd in ⊢ ((??%?) → ?);
2786         [ 1: #H destruct (H) % try @refl
2787              /3 by sws_returnstate, swc_stop, memext_free_extended_environment, memory_ext_writeable_eq/
2788         | *: #Habsurd destruct (Habsurd) ]
2789    | 2: #s #k #k' #u #s' #new_vars #Hfresh #Hsimcont #Heq_s' #Hincl #_ #Hnew_vars_eq #Hsss_k #Hsss_k_ext #Hsss_k_hyp
2790         #Hexec_step %{0} whd
2791         >(prod_eq_lproj ????? sss_func_hyp)
2792         whd in match (exec_step ??) in Hexec_step; destruct (Hexec_step) @conj try @refl
2793         <sss_func_hyp
2794         lapply (jmeq_to_eq ??? Hnew_vars_eq) #Hnew_vars_eq' destruct (Hnew_vars_eq')
2795         %1{u (refl ? (switch_removal s u))} try assumption try @refl         
2796         #id #Hmem lapply (Hext_fresh_for_ge id Hmem) #Hfind <(rg_find_symbol … Hrelated id) @Hfind
2797    | 3: #cond #body #k #k' #fgen #s' #new_vars #Hfresh #Hsimcont #Heq_s' #Hincl #_ #Hnew_vars_eq #Hsss_k #Hsss_k_ext #_
2798         lapply (jmeq_to_eq ??? Hnew_vars_eq) #Hnew_vars_eq' destruct (Hnew_vars_eq')
2799         #Hexec_step %{0} whd whd in Hexec_step;
2800         >(prod_eq_lproj ????? sss_func_hyp)
2801         whd in match (exec_step ??) in Hexec_step; destruct (Hexec_step) @conj try @refl         
2802         %1{ ((switch_removal (Swhile cond body) fgen))} try assumption try @refl
2803         [ 1: <sss_func_hyp @refl
2804         | 2: destruct normalize cases (switch_removal ??) * #body' #fvs' #u' @refl
2805         | 3: whd in match (switch_removal ??);
2806              cases (switch_removal body fgen) in Hincl; * #body' #fvs' #fgen' normalize nodelta #H @H
2807         | 4: #id #Hmem <(rg_find_symbol … Hrelated) @Hext_fresh_for_ge @Hmem ]
2808    | 4: #cond #body #k #k' #u #s' #new_vars #Hfresh #Hsimcont #Heq_s' #Hincl #_ #Hnew_vars_eq #Hsss_k #Hsss_k_ext #_
2809         lapply (jmeq_to_eq ??? Hnew_vars_eq) #Hnew_vars_eq' destruct (Hnew_vars_eq')   
2810         #Hexec_step %{0} whd whd in Hexec_step:(??%?) ⊢ (??%?);
2811         cases (bindIO_inversion ??????? Hexec_step) #x1 * #Hexec
2812         >(Hsim_expr … Hexec)
2813         >bindIO_Value cases (exec_bool_of_val ??)
2814         [ 2: #err normalize in ⊢ (% → ?); #Habsurd destruct (Habsurd) ]
2815         #b whd in match (m_bind ?????); whd in match (m_bind ?????);
2816         cases b normalize nodelta #H whd in H:(??%%) ⊢ %; destruct (H)
2817         try @conj try @refl
2818         [ 1: %{u … (switch_removal (Sdowhile cond body) u)} try assumption try //
2819              [ 1: destruct normalize cases (switch_removal body u) * #body' #fvs' #u' @refl
2820              | 2: whd in match (switch_removal ??);
2821                   cases (switch_removal body u) in Hincl; * #body' #fvs' #u' normalize nodelta #H @H
2822              | 3: #id #Hmem <(rg_find_symbol … Hrelated) @Hext_fresh_for_ge @Hmem ]
2823         | 2: %{u … (switch_removal Sskip u) } try assumption try //
2824              [ 1: @(fresh_for_Sskip … Hfresh)
2825              | 2: #id #Hmem <(rg_find_symbol … Hrelated) @Hext_fresh_for_ge @Hmem ] ]
2826    | 5: #cond #stmt1 #stmt2 #k #k' #u #s' #new_vars #Hfresh #Hsimcont #Heq_s' #Hincl #_
2827         #Hnew_vars_eq #Hsss_k #Hsss_k_ext #_
2828         lapply (jmeq_to_eq ??? Hnew_vars_eq) #Hnew_vars_eq' destruct (Hnew_vars_eq')
2829         #Hexec_step %{0} whd whd in Hresult:(??%?) Hexec_step:(??%?); destruct (Hexec_step)
2830         @conj try @refl
2831         %{u … new_vars … sss_mem_hyp … (switch_removal (Sfor Sskip cond stmt1 stmt2) u)} try // try assumption
2832         #id #Hmem <(rg_find_symbol … Hrelated) @Hext_fresh_for_ge @Hmem
2833    | 6: #cond #stmt1 #stmt2 #k #k' #u #result1 #result2 #new_vars
2834         #Hfresh #Hsimcont #Hresult1 #Hresult2 #Hincl #_ #Hnew_vars_eq #Hsss_k #Hsss_k_ext #_
2835         lapply (jmeq_to_eq ??? Hnew_vars_eq) #Hnew_vars_eq' destruct (Hnew_vars_eq')
2836         #Hexec %{0} whd in Hexec:(??%?) ⊢ %; destruct (Hexec) @conj try @refl
2837         %1{u … new_vars … sss_writeable (switch_removal stmt1 u)} try assumption try //
2838         [ 1: lapply (fresh_to_substatements … Hfresh) normalize * * //
2839         | 2: whd in match (switch_removal ??) in Hincl;
2840              cases (switch_removal stmt1 u) in Hincl; * #stmt1' #fvs1' #u' normalize nodelta
2841              cases (switch_removal stmt2 u') * #stmt2' #fvs2' #u'' normalize nodelta
2842              whd in match (ret_vars ??); /2 by All_append_l/
2843         | 3: @(swc_for3 … u) //
2844         | 4: #id #Hmem <(rg_find_symbol … Hrelated) @Hext_fresh_for_ge @Hmem ]
2845    | 7: #cond #stmt1 #stmt2 #k #k' #u #result1 #result2 #new_vars
2846         #Hfresh #Hsimcont #Hresult1 #Hresult2 #Hincl #_ #Hnew_vars_eq #Hsss_k #Hsss_k_ext #_
2847         lapply (jmeq_to_eq ??? Hnew_vars_eq) #Hnew_vars_eq' destruct (Hnew_vars_eq')
2848         #Hexec %{0} whd in Hexec:(??%?) ⊢ %; destruct (Hexec) @conj try @refl
2849         %1{u … new_vars … sss_writeable … (switch_removal (Sfor Sskip cond stmt1 stmt2) u)}
2850         try // try assumption
2851         [ 1: whd in match (switch_removal ??) in ⊢ (??%%); destruct normalize
2852              cases (switch_removal stmt1 u) * #stmt1' #fvs1' #u' normalize
2853              cases (switch_removal stmt2 u') * #stmt2' #fvs2' #u'' @refl
2854         | 2: #id #Hmem <(rg_find_symbol … Hrelated) @Hext_fresh_for_ge @Hmem ]
2855    | 8: #k #k' #new_vars #Hsimcont #_ #Hnew_vars_eq #Hsss_k #Hsss_k_ext #_
2856         lapply (jmeq_to_eq ??? Hnew_vars_eq) #Hnew_vars_eq' destruct (Hnew_vars_eq')
2857         #Hexec %{0} whd in Hexec:(??%?) ⊢ %; destruct (Hexec) @conj try @refl
2858         %1{sss_lu … new_vars … sss_writeable} try // try assumption
2859         [ 1: destruct (sss_result_hyp) @refl
2860         | 2: #id #Hmem <(rg_find_symbol … Hrelated) @Hext_fresh_for_ge @Hmem ]
2861    | 9: #en #en' #r #f #k #k' #old_vars #new_vars #Hsimcont #Hnew_vars_eq #Hdisjoint_k #_
2862         #Hnew_vars_eq #Hsss_k #Hsss_k_ext #_
2863         lapply (jmeq_to_eq ??? Hnew_vars_eq) #Hnew_vars_eq' destruct (Hnew_vars_eq')
2864         #Hexec %{0} whd in Hexec:(??%?) ⊢ %; whd in ⊢ (??%?);
2865         >(prod_eq_lproj ????? sss_func_hyp) >fn_return_simplify
2866         cases (fn_return sss_func) in Hexec; normalize nodelta
2867         [ | #sz #sg | #ptr_ty | #array_ty #array_sz | #domain #codomain
2868         | #structname #fieldspec | #unionname #fieldspec | #id ]         
2869(*         [ 1: | 2: #sz #sg | 3: #fsz | 4: #ptr_ty | 5: #array_ty #array_sz | 6: #domain #codomain
2870         | 7: #structname #fieldspec | 8: #unionname #fieldspec | 9: #id ] *)
2871         #Hexec whd in Hexec:(??%?); destruct (Hexec) whd @conj try @refl
2872         /3 by sws_returnstate, swc_call, memext_free_extended_environment/
2873    ]
2874  | 2: (* Assign statement *)
2875       lapply (exec_lvalue_sim_aux … Hsim_lvalue) #Hsim
2876       #Hexec %{0} whd in sss_result_hyp:(??%?);
2877       cases (bindIO_inversion ??????? Hexec) #xl * #Heq_lhs #Hexec_lhs
2878       cases (bindIO_inversion ??????? Hexec_lhs) #xr * #Heq_rhs #Hexec_rhs -Hexec_lhs
2879       cases (bindIO_inversion ??????? Hexec_rhs) #m' * #Heq_store #Hexec_store -Hexec_rhs
2880       whd whd in Hexec_store:(??%%) ⊢ (??%?); >sss_result_proj <sss_result_hyp normalize nodelta
2881       >(Hsim … Heq_lhs) whd in match (m_bind ?????);
2882       >(Hsim_expr … Heq_rhs) >bindIO_Value
2883       lapply (memext_store_value_of_type' sss_m sss_m_ext m' sss_writeable (typeof lhs) (\fst  xl) (\fst  xr) sss_mem_hyp ?)
2884       [ 1: cases (store_value_of_type' ????) in Heq_store;
2885            [ 1: normalize #Habsurd destruct (Habsurd)
2886            | 2: #m normalize #Heq destruct (Heq) @refl ] ]
2887       * #m_ext' * #Heq_store' #Hnew_ext >Heq_store' whd in match (m_bind ?????);
2888       whd destruct @conj try @refl
2889       %1{sss_lu … sss_new_vars … sss_writeable … (switch_removal Sskip  sss_lu) }
2890       try // try assumption
2891       [ 1: @(fresh_for_Sskip … sss_lu_fresh)
2892       | 3: #id #Hmem <(rg_find_symbol … Hrelated) @Hext_fresh_for_ge @Hmem
2893       | 2: #v #Hmem #vb #Hlookup lapply (sss_new_alloc v Hmem vb Hlookup) * * #Hvb #Hlow #Hhigh           
2894            cut (store_value_of_type' (typeof lhs) sss_m (\fst  xl) (\fst  xr) = Some ? m')
2895            [ cases (store_value_of_type' (typeof lhs) sss_m (\fst  xl) (\fst  xr)) in Heq_store;
2896              [ whd in ⊢ ((??%%) → ?); #Habsurd destruct
2897              | #m0 whd in ⊢ ((??%%) → ?); #Heq destruct (Heq) @refl ] ]             
2898            #Hstore lapply (mem_bounds_after_store_value_of_type' … Heq_store') *
2899            #HA #HB cases (HB vb) #Hlow' #Hhigh' @conj try @conj
2900            [ 2: >Hlow' in Hlow; //
2901            | 3: >Hhigh' in Hhigh; //
2902            | 1: whd >HA @Hvb ] ]
2903  | 3: (* Call statement *)
2904       #Hexec %{0} whd in sss_result_hyp:(??%?); destruct (sss_result_hyp)
2905       whd whd in ⊢ (??%?); >sss_result_proj normalize nodelta
2906       whd in Hexec:(??%?);
2907       cases (bindIO_inversion ??????? Hexec) #xfunc * #Heq_func #Hexec_func
2908       cases (bindIO_inversion ??????? Hexec_func) #xargs * #Heq_args #Hexec_args
2909       cases (bindIO_inversion ??????? Hexec_args) #called_fundef * #Heq_fundef #Hexec_typeeq
2910       cases (bindIO_inversion ??????? Hexec_typeeq) #Htype_eq * #Heq_assert #Hexec_ret
2911       >(Hsim_expr … Heq_func) whd in match (m_bind ?????);
2912       >(exec_expr_sim_to_exec_exprlist … Hsim_expr … Heq_args)
2913       whd in ⊢ (??%?);
2914       >(rg_find_funct … Hrelated … (opt_to_io_Value … Heq_fundef))
2915       whd in ⊢ (??%?); <fundef_type_simplify >Heq_assert
2916       whd in ⊢ (??%?); -Hexec -Hexec_func -Hexec_args -Hexec_typeeq lapply Hexec_ret -Hexec_ret
2917       @(option_ind … retv) normalize nodelta
2918       [ 1: whd in ⊢ ((??%%) → (??%%)); #Heq whd destruct (Heq) @conj try @refl
2919            %2{sss_writeable … sss_mem_hyp}
2920            cases called_fundef
2921            [ 2: #id #tl #ty @I
2922            | 1: #called_function whd
2923                 cut (sss_func_tr = \fst (function_switch_removal sss_func))
2924                 [ 1: <sss_func_hyp @refl ] #H >H -H
2925                 cut (sss_new_vars = \snd (function_switch_removal sss_func))
2926                 [ 1: <sss_func_hyp @refl ] #H >H -H
2927                 @(swc_call … sss_k_hyp) try assumption
2928                 <sss_func_hyp @refl ]
2929       | 2: #ret_expr #Hexec_ret_expr
2930            cases (bindIO_inversion ??????? Hexec_ret_expr) #xret * #Heq_ret
2931            whd in ⊢ ((??%%) → (??%%)); #H destruct (H)
2932            >(exec_lvalue_sim_aux … Hsim_lvalue … Heq_ret)
2933            whd in ⊢ (??%?); whd @conj try @refl
2934            cut (sss_func_tr = \fst (function_switch_removal sss_func))
2935            [ 1: <sss_func_hyp @refl ] #H >H -H
2936            @(sws_callstate … sss_writeable … sss_mem_hyp)
2937            cases called_fundef
2938            [ 2: #id #tl #ty @I
2939            | 1: #called_function whd
2940                 cut (sss_func_tr = \fst (function_switch_removal sss_func))
2941                 [ 1: <sss_func_hyp @refl ] #H >H -H
2942                 cut (sss_new_vars = \snd (function_switch_removal sss_func))
2943                 [ 1: <sss_func_hyp @refl ] #H >H -H
2944                 @(swc_call … sss_k_hyp) try assumption
2945                 <sss_func_hyp @refl ] ]
2946  | 4: (* Sequence statement *)
2947       #Hexec %{0} whd in sss_result_hyp:(??%?); whd whd in Hexec:(??%?) ⊢ (??%?); destruct (Hexec)
2948       >sss_result_proj <sss_result_hyp
2949       cases (switch_removal_elim stm1 sss_lu) #stm1' * #fvs1' * #u' #HeqA >HeqA normalize nodelta
2950       cases (switch_removal_elim stm2 u') #stm2' * #fvs2' * #u'' #HeqB >HeqB normalize nodelta
2951       normalize @conj try @refl %1{sss_lu … sss_func_hyp … sss_writeable … sss_mem_hyp … HeqA}
2952       try // try assumption
2953       [ 1: lapply (fresh_to_substatements … sss_lu_fresh) normalize * //
2954       | 2: lapply sss_incl <sss_result_hyp >HeqA normalize nodelta >HeqB normalize nodelta
2955            /2 by All_append_l/
2956       | 4: #id #Hmem <(rg_find_symbol … Hrelated) @Hext_fresh_for_ge @Hmem ]
2957       @(swc_seq … u') try //
2958       [ 2: >HeqB @refl
2959       | 1: lapply (fresh_to_substatements … sss_lu_fresh) normalize * #_ @fresher_for_univ
2960            lapply (switch_removal_fte stm1 sss_lu) >HeqA #H @H
2961       | 3: lapply sss_incl <sss_result_hyp >HeqA normalize nodelta >HeqB normalize nodelta
2962            /2 by All_append_r/
2963       ]
2964  | 5: (* If-then-else *)
2965       #Hexec %{0} whd in sss_result_hyp:(??%?) Hexec:(??%?); >sss_result_proj <sss_result_hyp
2966       cases (switch_removal_elim iftrue sss_lu) #iftrue' * #fvs1' * #u' #HeqA >HeqA normalize nodelta
2967       cases (switch_removal_elim iffalse u') #iffalse' * #fvs2' * #u'' #HeqB >HeqB normalize nodelta
2968       whd whd in ⊢ (??%?);
2969       cases (bindIO_inversion ??????? Hexec) #condres * #Heq_cond #Hexec_cond
2970       cases (bindIO_inversion ??????? Hexec_cond) #b * #Heq_bool #Hresult
2971       whd in Hresult:(??%%); destruct (Hresult)
2972       >(Hsim_expr … Heq_cond) >bindIO_Value
2973       >Heq_bool whd in match (m_bind ?????); whd @conj try @refl
2974       cases b normalize nodelta
2975       [ 1: %1{sss_lu … sss_func_hyp … sss_writeable … sss_mem_hyp … HeqA} try assumption try //
2976             [ 1: cases (fresh_to_substatements … sss_lu_fresh) normalize //
2977             | 2: lapply sss_incl <sss_result_hyp >HeqA normalize nodelta >HeqB normalize nodelta
2978                  /2 by All_append_l/
2979             | 3: #id #Hmem <(rg_find_symbol … Hrelated) @Hext_fresh_for_ge @Hmem ]
2980       | 2: %1{u' … sss_func_hyp … sss_writeable … sss_mem_hyp … HeqB} try assumption try //
2981             [ 1: cases (fresh_to_substatements … sss_lu_fresh) normalize #_
2982                   @fresher_for_univ lapply (switch_removal_fte iftrue sss_lu) >HeqA #H @H
2983             | 2: lapply sss_incl <sss_result_hyp >HeqA normalize nodelta >HeqB normalize nodelta
2984                  /2 by All_append_r/                   
2985             | 3: #id #Hmem <(rg_find_symbol … Hrelated) @Hext_fresh_for_ge @Hmem ] ]
2986  | 6: (* While loop *)
2987       #Hexec %{0} whd in sss_result_hyp:(??%?) Hexec:(??%?); >sss_result_proj <sss_result_hyp
2988       >sss_result_proj <sss_result_hyp whd
2989       cases (bindIO_inversion ??????? Hexec) #condres * #Heq_cond #Hexec_cond
2990       cases (bindIO_inversion ??????? Hexec_cond) #b * #Heq_bool whd in ⊢ ((??%%) → ?);
2991       cases (switch_removal_elim body sss_lu) #body' * #fvs1' * #u' #HeqA >HeqA normalize nodelta
2992       whd in ⊢ (? → (??%?));
2993       >(Hsim_expr … Heq_cond) >bindIO_Value >Heq_bool
2994       whd in match (m_bind ?????); cases b normalize nodelta #Hresult destruct (Hresult)
2995       whd @conj try @refl
2996       [ 1: %1{sss_lu … sss_func_hyp … sss_writeable … sss_mem_hyp … HeqA} try assumption try //
2997             [ 1: cases (fresh_to_substatements … sss_lu_fresh) normalize //
2998             | 2: lapply sss_incl <sss_result_hyp >HeqA normalize nodelta #H @H
2999             | 4: #id #Hmem <(rg_find_symbol … Hrelated) @Hext_fresh_for_ge @Hmem
3000             | 3: @(swc_while … sss_lu) try //
3001                  [ 1: >HeqA @refl
3002                  | 2: lapply sss_incl <sss_result_hyp >HeqA normalize nodelta #H @H ]
3003             ]
3004       | 2: %{… sss_func_hyp … (switch_removal Sskip u')} try assumption try //
3005            [ 1: lapply (switch_removal_fte body sss_lu) >HeqA #Hfte whd in match (ret_u ??) in Hfte;
3006                 @(fresher_for_univ … Hfte) @(fresh_for_Sskip … sss_lu_fresh)
3007            | 2: #id #Hmem <(rg_find_symbol … Hrelated) @Hext_fresh_for_ge @Hmem ] ]
3008  | 7: (* do while loop *)
3009       #Hexec %{0} whd in sss_result_hyp:(??%?) Hexec:(??%?); >sss_result_proj <sss_result_hyp
3010       >sss_result_proj <sss_result_hyp whd destruct (Hexec) whd in ⊢ (??%?);
3011       cases (switch_removal_elim body sss_lu) #body' * #fvs1' * #u' #HeqA >HeqA normalize nodelta
3012       whd @conj try @refl
3013       %1{sss_lu … sss_func_hyp … (switch_removal body sss_lu) }
3014       try assumption try //
3015       [ 1:  lapply (fresh_to_substatements … sss_lu_fresh) normalize * //
3016       | 2: >HeqA @refl
3017       | 3: lapply sss_incl <sss_result_hyp >HeqA normalize nodelta #H @H
3018       | 5: #id #Hmem <(rg_find_symbol … Hrelated) @Hext_fresh_for_ge @Hmem
3019       | 4: @(swc_dowhile … sss_lu) try assumption try //
3020            [ 1: >HeqA @refl
3021            | 2: lapply sss_incl <sss_result_hyp >HeqA normalize nodelta #H @H           
3022            ] ]       
3023  | 8: (* for loop *)
3024       #Hexec %{0} whd in sss_result_hyp:(??%?) Hexec:(??%?); >sss_result_proj <sss_result_hyp
3025       >sss_result_proj <sss_result_hyp whd destruct (Hexec) whd in ⊢ (??%?);
3026       cases (switch_removal_elim init sss_lu) #init' * #fvs1' * #u' #HeqA >HeqA normalize nodelta
3027       cases (switch_removal_elim step u') #step' * #fvs2' * #u'' #HeqB >HeqB normalize nodelta
3028       cases (switch_removal_elim body u'') #body' * #fvs3' * #u''' #HeqC >HeqC normalize nodelta
3029       lapply Hexec
3030       @(match is_Sskip init with
3031       [ inl Heq ⇒ ?
3032       | inr Hneq ⇒ ?
3033       ]) normalize nodelta
3034       [ 2: lapply (simplify_is_not_skip … Hneq sss_lu) >HeqA * #pf
3035            whd in match (ret_st ??) in ⊢ ((??%%) → ?); #Hneq >Hneq normalize nodelta
3036            #Hexec' whd in Hexec':(??%%); destruct (Hexec') whd @conj try @refl
3037            %1{sss_lu … sss_func_hyp (switch_removal init sss_lu)} try assumption try //
3038            [ 1: lapply (fresh_to_substatements … sss_lu_fresh) normalize * * * //
3039            | 2: >HeqA @refl
3040            | 3: lapply sss_incl <sss_result_hyp >HeqA normalize nodelta
3041                 >HeqB normalize nodelta >HeqC normalize nodelta
3042                 /2 by All_append_l/
3043            | 4: @(swc_for1 … u') try assumption try //
3044                 [ 1: lapply (fresh_to_substatements … sss_lu_fresh) * * * #HW #HX #HY #HZ
3045                      @for_fresh_lift
3046                      [ 1: @(fresher_for_univ … HY)
3047                      | 2: @(fresher_for_univ … HZ)
3048                      | 3: @(fresher_for_univ … HX) ]
3049                      lapply (switch_removal_fte init sss_lu) >HeqA #Hs @Hs
3050                 | 2: normalize >HeqB normalize nodelta >HeqC @refl
3051                 | 3: lapply sss_incl <sss_result_hyp
3052                      whd in match (ret_vars ??) in ⊢ (% → %);
3053                      whd in match (switch_removal ??) in ⊢ (% → %);
3054                      >HeqA normalize nodelta >HeqB normalize nodelta >HeqC
3055                      normalize nodelta #H /2 by All_append_r/
3056                  ] ]
3057       | 1: -Hexec #Hexec' cases (bindIO_inversion ??????? Hexec') #condres * #Heq_cond #Hexec_cond
3058            cases (bindIO_inversion ??????? Hexec_cond) #b * #Heq_bool
3059            destruct (Heq) normalize in HeqA; lapply HeqA #HeqA' destruct (HeqA')
3060            normalize nodelta
3061            >(Hsim_expr … Heq_cond) whd in ⊢ ((??%?) → ?); #Hexec'
3062            whd in match (m_bind ?????); >Heq_bool
3063            cases b in Hexec'; normalize nodelta whd in match (bindIO ??????);
3064            normalize #Hexec'' destruct (Hexec'') @conj try @refl
3065            [ 1: %1{u'' … sss_func_hyp (switch_removal body u'')} try assumption try //
3066                 [ 1: lapply (fresh_to_substatements … sss_lu_fresh) * * * #_ #_ #_
3067                      @fresher_for_univ lapply (switch_removal_fte step u') >HeqB
3068                      #H @H
3069                 | 2: >HeqC @refl
3070                 | 3: lapply sss_incl <sss_result_hyp
3071                      whd in match (ret_vars ??) in ⊢ (% → %);
3072                      whd in match (switch_removal ??) in ⊢ (% → %); normalize nodelta
3073                      >HeqB normalize nodelta >HeqC normalize nodelta
3074                      /2 by All_append_r/
3075                 | 4: @(swc_for2 … u') try assumption
3076                      [ 1: >HeqB @refl
3077                      | 2: >HeqB >HeqC @refl
3078                      | 3: lapply sss_incl <sss_result_hyp
3079                           whd in match (ret_vars ??) in ⊢ (% → %);
3080                           whd in match (switch_removal ??) in ⊢ (% → %); normalize nodelta
3081                           >HeqB normalize nodelta >HeqC normalize nodelta #H @H
3082                      ]
3083                 ]
3084            | 2: %1{u' … sss_func_hyp … (switch_removal Sskip u')} try assumption try //
3085                 [ 1: @(fresh_for_Sskip … sss_lu_fresh) ] ] ]
3086        #id #Hmem <(rg_find_symbol … Hrelated) @Hext_fresh_for_ge @Hmem
3087  | 9: (* break *)
3088       (* sss_enclosing_label TODO : switch case *)
3089       #Hexec %{0} whd whd in sss_result_hyp:(??%?); >sss_result_proj <sss_result_hyp normalize nodelta
3090       lapply Hexec -Hexec
3091       inversion sss_k_hyp
3092       [ 1: #new_vars #Hv #Hk #Hk' #_ whd in ⊢ ((??%?) → (??%?)); #Habsurd destruct (Habsurd)
3093       | 2: #sk #sss_k' #sss_k_ext' #uk #sk' #new_vars #Hfresh_suk #Hsimk' #Hsk_eq' #Hincl #_ #Hnew_vars_eq
3094            #Hk #Hk' #_ whd in ⊢ ((??%?) → (??%?)); #Heq destruct (Heq) whd @conj try @refl
3095            destruct
3096            %1{sss_lu … (switch_removal Sbreak sss_lu)} try assumption try //
3097       | 3,4: #e #sk #sss_k' #sss_k_ext' #uk #sk' #new_vars #Hfresh_suk #Hsimk' #Hsk_eq' #Hincl #_
3098            #Hnew_vars #Hk #Hk' #_ whd in ⊢ ((??%?) → (??%?)); #Heq destruct (Heq) whd @conj try @refl
3099            destruct
3100            %1{sss_lu … (switch_removal Sskip sss_lu)} try assumption try //
3101       | 5: #e #s1k #s2k #sss_k' #sss_k_ext' #uk #sk' #new_vars #Hfresh_suk #Hsimk' #Hsk_eq' #Hincl #_
3102            #Hnew_vars #Hk #Hk' #_ whd in ⊢ ((??%?) → (??%?)); #Heq destruct (Heq) whd @conj try @refl
3103            destruct
3104            %1{sss_lu … (switch_removal Sbreak sss_lu)} try assumption try //
3105       | 6,7: #e #s1k #s2k #sss_k' #sss_k_ext' #uk #result1 #result2 #new_vars #Hfresh_suk #Hsimk'
3106            #Hres1 #Hres2 #Hincl #_ #Hnew_vars
3107            #Hk #Hk' #_ whd in ⊢ ((??%?) → (??%?)); #Heq destruct (Heq) whd @conj try @refl
3108            destruct
3109            %1{sss_lu … (switch_removal Sskip sss_lu)} try assumption try //
3110       | 8: #sss_k' #sss_k_ext' #new_vars #Hsimk' #_ #Hnew_vars #Hk #Hk' #_ whd in ⊢ ((??%?) → (??%?));
3111            #Heq destruct (Heq) whd @conj try @refl destruct
3112            %1{sss_lu … (switch_removal Sskip sss_lu)} try assumption try //
3113       | 9: #enk #enk' #rk #fk #sss_k' #sss_k_ext' #old_vars #new_vars #Hsimk' #Hold #Hdisjoint #_
3114            #Hnew_vars #Hk #Hk' #_ whd in ⊢ ((??%?) → (??%?));
3115            #Heq destruct (Heq) ]
3116       #id #Hmem <(rg_find_symbol … Hrelated) @Hext_fresh_for_ge @Hmem
3117  | 10: (* continue *)
3118       #Hexec %{0} whd whd in sss_result_hyp:(??%?); >sss_result_proj <sss_result_hyp normalize nodelta
3119       lapply Hexec -Hexec
3120       inversion sss_k_hyp
3121       [ 1: #new_vars #Hv #Hk #Hk' #_ whd in ⊢ ((??%?) → (??%?)); #Habsurd destruct (Habsurd)
3122       | 2: #sk #sss_k' #sss_k_ext' #uk #sk' #new_vars #Hfresh_suk #Hsimk' #Hsk_eq' #Hincl #_ #Hnew_vars_eq
3123            #Hk #Hk' #_ whd in ⊢ ((??%?) → (??%?)); #Heq destruct (Heq) whd @conj try @refl
3124            destruct
3125            %1{sss_lu … (switch_removal Scontinue sss_lu)} try assumption try //
3126       | 3: #ek #sk #sss_k' #sss_k_ext' #uk #sk' #new_vars #Hfresh_suk #Hsimk' #Hsk_eq' #Hincl #_
3127            #Hnew_vars #Hk #Hk' #_ whd in ⊢ ((??%?) → (??%?)); #Heq destruct (Heq) whd @conj try @refl
3128            destruct
3129            %1{uk … (switch_removal (Swhile ek sk) uk)} try assumption try //
3130            [ 1: normalize cases (switch_removal sk uk) * #sk' #fvs' #uk' @refl
3131            | 2: whd in match (switch_removal ??); lapply Hincl
3132                 cases (switch_removal sk uk) * #body' #fvs' #uk'
3133                 /2 by All_append_r/ ]                 
3134       | 4: #ek #sk #sss_k' #sss_k_ext' #uk #sk' #new_vars #Hfresh_suk #Hsimk' #Hsk_eq' #Hincl #_
3135            #Hnew_vars_eq #Hk #Hk' #_ whd in ⊢ ((??%?) → (??%?)); #Hexec
3136            cases (bindIO_inversion ??????? Hexec) #condres * #Heq_cond #Hexec_cond
3137            cases (bindIO_inversion ??????? Hexec_cond) #b * #Heq_bool #Hexec_bool
3138            >(Hsim_expr … Heq_cond) >bindIO_Value >Heq_bool whd in match (m_bind ?????);
3139            cases b in Hexec_bool; normalize nodelta whd in ⊢ ((??%?) → ?);
3140            #Heq whd whd in Heq:(??%%); destruct (Heq) @conj try @refl
3141            [ 1: destruct %1{uk … (switch_removal (Sdowhile ek sk) uk)} try assumption try //
3142                 [ 1: normalize cases (switch_removal sk uk) * #body' #fvs' #uk' @refl
3143                 | 2: whd in match (switch_removal ??); lapply Hincl cases (switch_removal sk uk)
3144                      * #body' #fvs' #uk' #H @H
3145                 ]
3146            | 2: destruct %1{uk … (switch_removal Sskip uk)} try assumption try //
3147                 try @(fresh_for_Sskip … Hfresh_suk) ]
3148       | 5: #e #s1k #s2k #sss_k' #sss_k_ext' #uk #sk' #new_vars #Hfresh_suk #Hsimk' #Hsk_eq' #Hincl #_
3149            #Hnew_vars #Hk #Hk' #_ whd in ⊢ ((??%?) → (??%?)); #Heq destruct (Heq) whd @conj try @refl
3150            destruct %1{sss_lu … (switch_removal Scontinue sss_lu)} try assumption try //
3151       | 6,7: #e #s1k #s2k #sss_k' #sss_k_ext' #uk #result1 #result2 #new_vars #Hfresh_suk #Hsimk' #Hres1 #Hres2 #Hincl #_
3152            #Hnew_vars #Hk #Hk' #_ whd in ⊢ ((??%?) → (??%?)); #Heq destruct (Heq) whd @conj try @refl
3153            destruct %1{uk … (switch_removal s1k uk)} try assumption try //
3154            [ 1: cases (fresh_to_substatements … Hfresh_suk) * * //
3155            | 2: lapply Hincl whd in match (ret_vars ??) in ⊢ (% → ?);
3156                 whd in match (switch_removal ??);
3157                 cases (switch_removal s1k uk) * #s1k' #fvs1' #uk' normalize nodelta
3158                 cases (switch_removal s2k uk') * #s2k' #fvs2' #uk'' normalize nodelta
3159                 /2 by All_append_l/
3160            | 3: @(swc_for3 … uk) try assumption try //
3161            ]
3162       | 8: #sss_k' #sss_k_ext' #new_vars #Hsimk #_ #Hnew_vars_eq #Hk #Hk' #_
3163            whd in ⊢ ((??%?) → (??%?)); #Heq destruct (Heq)
3164            whd @conj try @refl destruct
3165            %1{sss_lu … (switch_removal Scontinue sss_lu)} try assumption try //
3166       | 9: #enk #enk' #rk #fk #sss_k' #sss_k_ext' #old_vars #new_vars #Hsimk' #Hold_vars_eq #Hdisjoint
3167             #_ #Hnew_vars_eq #Hk #Hk' #_ whd in ⊢ ((??%?) → (??%?));
3168            #Heq destruct (Heq) ]
3169       #id #Hmem <(rg_find_symbol … Hrelated) @Hext_fresh_for_ge @Hmem
3170  | 11: (* return *)
3171        #Hexec %{0} whd whd in sss_result_hyp:(??%?) Hexec:(??%?); lapply Hexec -Hexec
3172        >sss_result_proj <sss_result_hyp normalize nodelta
3173        cases retval in sss_lu_fresh sss_result_hyp; normalize nodelta
3174        [ 1: #sss_lu_fresh #sss_result_hyp whd in ⊢ (? → (??%?));
3175             >(prod_eq_lproj ????? sss_func_hyp)
3176             >fn_return_simplify
3177             cases (fn_return sss_func) normalize nodelta
3178             [ | #sz #sg | #ptr_ty | #array_ty #array_sz | #domain #codomain
3179             | #structname #fieldspec | #unionname #fieldspec | #id ]
3180             [ 1: whd in ⊢ ((??%%) → ?); #Heq destruct (Heq) whd @conj try @refl
3181                  /3 by sws_returnstate, call_cont_swremoval, memext_free_extended_environment, memory_ext_writeable_eq/
3182             | *: #Habsurd destruct (Habsurd) ]
3183        | 2: #ret_expr #sss_lu_fresh #sss_result_hyp whd in ⊢ (? → (??%?));
3184             >(prod_eq_lproj ????? sss_func_hyp)
3185             >fn_return_simplify
3186             @(match type_eq_dec (fn_return sss_func) Tvoid with
3187               [ inl H ⇒ ?
3188               | inr H ⇒ ? ]) normalize nodelta
3189             [ 1: #Habsurd destruct (Habsurd)
3190             | 2: #Hexec
3191                   cases (bindIO_inversion ??????? Hexec) #retres * #Heq_ret #Hexec_ret
3192                   whd in Hexec_ret:(??%%); destruct (Hexec_ret)
3193                   >(Hsim_expr … Heq_ret) whd in match (m_bind ?????); whd
3194                   @conj try @refl
3195                   /3 by sws_returnstate, call_cont_swremoval, memext_free_extended_environment, memory_ext_writeable_eq/
3196             ] ]
3197  | 12: (* switch ! at long last *)
3198        #Hexec whd in sss_result_hyp:(??%?) Hexec:(??%?); lapply Hexec -Hexec
3199        >sss_result_proj <sss_result_hyp normalize nodelta #Hexec
3200        cases (bindIO_inversion ??????? Hexec) * #condval #condtrace -Hexec
3201        cases condval normalize nodelta
3202        [ 1: * #_ #Habsurd normalize in Habsurd; destruct (Habsurd)
3203        | 3: * #_ #Habsurd normalize in Habsurd; destruct (Habsurd)
3204        | 4: #ptr * #_ #Habsurd normalize in Habsurd; destruct (Habsurd) ]
3205        #sz #i * #Hexec_eq #Heq
3206        cut (∃sg. typeof cond = Tint sz sg) whd in Heq:(??%%); destruct (Heq)
3207        [ 1: cases (typeof cond) in Heq; normalize nodelta
3208             [ | #sz' #sg' | #ptrty | #arrayty #arraysz | #domain #codomain
3209             | #structname #fieldspec | #unionname #fieldspec | #id ]
3210             [ 2: cases (sz_eq_dec ??) normalize nodelta #H
3211                  [ 2: #Habsurd destruct
3212                  | 1: destruct (H) #_ %{sg'} try @refl ]
3213             | *: #Habsurd destruct (Habsurd) ] ]
3214        * #sg #Htypeof_cond >Htypeof_cond in Heq; normalize nodelta >sz_eq_identity normalize nodelta
3215        #Heq whd in Heq:(??%%);
3216        cases (bindIO_inversion ??????? Heq) #switchcases_truncated * #Heq1 #Heq2 -Heq
3217        whd in Heq1:(??%%); whd in Heq2:(??%%);
3218        cut (select_switch sz i switchcases = Some ? switchcases_truncated)
3219        [ 1: cases (select_switch sz i switchcases) in Heq1; normalize nodelta
3220             [ 1: #Habsurd destruct | 2: #ls #Heq destruct (Heq) @refl ] ]
3221        -Heq1 #Heq_select_switch destruct (Heq2)
3222        cases (switch_removal_branches_elim … switchcases sss_lu) #switchcases' * #fvs' * #u' #Hbranch_eq
3223        >Hbranch_eq normalize nodelta
3224        cases (fresh_elim … u') #new * #u'' #Hfresh_eq >Hfresh_eq normalize nodelta
3225        cases (simplify_switch_elim (Expr (Evar new) (Tint sz sg)) switchcases' u'') #simplified * #u'''
3226        #Hswitch_eq >Hswitch_eq normalize nodelta
3227        %{2} whd whd in ⊢ (??%?);
3228        (* A. Execute lhs of assign, i.e. fresh variable that will hold value of condition *)
3229        whd in match (exec_lvalue ????);
3230        (* show that the resulting ident is in the memory extension and that the lookup succeeds *)
3231        >Hbranch_eq in sss_result_hyp; normalize nodelta
3232        >Hfresh_eq normalize nodelta >Hswitch_eq normalize nodelta >Htypeof_cond >Hswitch_eq
3233        normalize nodelta #sss_result_hyp
3234        <sss_result_hyp in sss_incl; whd in match (ret_vars ??); #sss_incl
3235        cases sss_env_hyp *
3236        #Hlookup_new_in_old
3237        #Hlookup_new_in_new
3238        #Hlookup_old
3239        cut (mem_assoc_env new sss_new_vars=true)
3240        [ 1: cases sss_incl #Hmem #_ elim sss_new_vars in Hmem;
3241             [ 1: @False_ind
3242             | 2: * #hdv #hdty #tl #Hind whd in ⊢ (% →  (??%?)); *
3243                  [ 1: #Heq destruct (Heq)
3244                       cases (identifier_eq_i_i … hdv) #Hrefl #Heq >Heq -Heq normalize nodelta
3245                       @refl
3246                  | 2: #Hmem lapply (Hind Hmem) #Hmem_in_tl
3247                  cases (identifier_eq ? new hdv) normalize nodelta
3248                  [ 1: #_ @refl | 2: #_ @Hmem_in_tl ] ] ] ]
3249       #Hnew_in_new_vars
3250       lapply (Hlookup_new_in_new new Hnew_in_new_vars)                 
3251       * #res #Hlookup >Hlookup normalize nodelta whd in match (bindIO ??????);
3252       (* B. Reduce rhs of assign, i.e. the condition. Do this using simulation hypothesis. *)
3253       >(Hsim_expr … Hexec_eq) >bindIO_Value
3254       (* C. Execute assign. We must prove that this cannot fail. In order for the proof to proceed, we need
3255             to set up things so that loading from that fresh location will yield exactly the stored value. *)
3256       normalize in match store_value_of_type'; normalize nodelta
3257       whd in match (typeof ?);
3258       lapply (sss_new_alloc 〈new,Tint sz sg〉 ? res Hlookup)
3259       [ 1: cases sss_incl // ] * * #Hvalid #Hlow #Hhigh
3260       lapply (store_int_success … i … Hvalid Hlow Hhigh) * #m_ext' #Hstore
3261       lapply (store_value_load_value_compatible … Hstore) // #Hload_value_correct
3262       >Hstore whd in match (m_bind ?????); whd @conj try //
3263       cut (mem block res sss_writeable)
3264       [ 1: @cthulhu ]
3265       (* lapply (memext_store_value_of_type_writeable … sss_mem_hyp … Hstore) *)       
3266       @cthulhu               
3267   | *: @cthulhu ]
3268 | *: @cthulhu ] qed.
Note: See TracBrowser for help on using the repository browser.