source: src/joint/StatusSimulationUtils.ma @ 3362

Last change on this file since 3362 was 3262, checked in by piccolo, 7 years ago

reverted status_simulation_utils

File size: 70.2 KB
Line 
1(**************************************************************************)
2(*       ___                                                              *)
3(*      ||M||                                                             *)
4(*      ||A||       A project by Andrea Asperti                           *)
5(*      ||T||                                                             *)
6(*      ||I||       Developers:                                           *)
7(*      ||T||         The HELM team.                                      *)
8(*      ||A||         http://helm.cs.unibo.it                             *)
9(*      \   /                                                             *)
10(*       \ /        This file is distributed under the terms of the       *)
11(*        v         GNU General Public License Version 2                  *)
12(*                                                                        *)
13(**************************************************************************)
14 
15include "joint/semanticsUtils.ma".
16include "joint/Traces.ma".
17include "common/StatusSimulation.ma".
18include "joint/semantics_blocks.ma".
19include "utilities/listb_extra.ma".
20include "utilities/lists.ma".
21
22lemma set_no_pc_eta:
23 ∀P.∀st1: state_pc P. set_no_pc P st1 st1 = st1.
24#P * //
25qed.
26
27lemma pc_of_label_eq :
28  ∀pars: sem_graph_params.
29  ∀globals,ge,bl,i_fn,lbl.
30  fetch_internal_function ? globals ge bl = return i_fn →
31  pc_of_label pars globals ge bl lbl =
32    OK ? (pc_of_point pars bl lbl).
33#p #globals #ge #bl #i_fn #lbl #EQf
34whd in match pc_of_label;
35normalize nodelta >EQf >m_return_bind %
36qed.
37
38
39lemma bind_new_bind_new_instantiates' :
40∀B,X : Type[0]. ∀ m : bind_new B X. ∀ x : X. ∀l : list B.∀P.
41bind_new_instantiates B X x m l → bind_new_P' ?? P m →
42P l x.
43#B #X #m elim m normalize nodelta
44[#x #y * normalize // #B #l' #P *
45| #f #IH #x #l elim l normalize [#P *] #hd #tl normalize #_ #P #H #Hr @(IH … H)
46 @Hr
47]
48qed.
49
50lemma bind_new_bind_new_instantiates :
51∀B,X : Type[0]. ∀ m : bind_new B X. ∀ x : X. ∀l : list B.∀P.
52bind_new_instantiates B X x m l → bind_new_P ?? P m →
53P x.
54#B #X #m elim m normalize nodelta
55[#x #y * normalize // #B #l' #P *
56| #f #IH #x #l elim l normalize [#P *] #hd #tl normalize #_ #P #H #Hr @(IH … H)
57 @Hr
58]
59qed.
60
61let rec bind_instantiate B X (b : bind_new B X) (l : list B) on b : (option X) ≝
62  match b with
63  [ bret B ⇒
64    match l with
65    [ nil ⇒ Some ? B
66    | _ ⇒ None ?
67    ]
68  | bnew f ⇒
69    match l with
70    [ nil ⇒ None ?
71    | cons r l' ⇒
72      bind_instantiate B X (f r) l'
73    ]
74  ].
75 
76lemma bind_instantiates_to_instantiate : ∀B,X.∀b : bind_new B X.
77∀l : list B.∀x : X.
78bind_instantiate B X b l = Some ? x →
79bind_new_instantiates B X x b l.
80#B #X #b elim b
81[#x1 * [2: #hd #tl] #x whd in ⊢ (??%? → ?); #EQ destruct(EQ) %
82|#f #IH * [2: #hd #tl] #x whd in ⊢ (??%? → ?); [2: #EQ destruct(EQ)] #H
83 whd @IH assumption
84]
85qed.
86
87lemma bind_instantiate_instantiates : ∀B,X.∀b : bind_new B X.
88∀l : list B.∀x : X.
89bind_new_instantiates B X x b l →
90bind_instantiate B X b l = Some ? x.
91#B #X #b elim b
92[ #x1 * [2: #hd #tl] #x whd in ⊢ (% → ?); [*] #EQ destruct(EQ) %
93| #f #IH * [2: #hd #tl] #x whd in ⊢ (% → ?); [2: *] #H whd in ⊢ (??%?); @IH @H
94]
95qed.
96
97coercion bind_instantiate_instantiates : ∀B,X.∀b : bind_new B X.
98∀l : list B.∀x : X.
99∀prf : bind_new_instantiates B X x b l.
100bind_instantiate B X b l = Some ? x ≝
101bind_instantiate_instantiates
102on _prf : bind_new_instantiates ?????
103to eq (option ?) (bind_instantiate ????) (Some ??).
104
105definition lbl_funct_type ≝  block → label → (list label).
106definition regs_funct_type ≝ block → label → (list register).
107
108
109definition preamble_length ≝
110λP_in : sem_graph_params.λP_out : sem_graph_params.λprog : joint_program P_in.
111λstack_size : (ident → option ℕ).
112λinit : ∀globals.joint_closed_internal_function P_in globals
113         →bound_b_graph_translate_data P_in P_out globals.
114λinit_regs : block → list register.
115λf_regs : regs_funct_type.λbl : block.λlbl : label.
116! bl ← code_block_of_block bl ;
117! 〈id,fn〉 ← res_to_opt … (fetch_internal_function …
118                           (joint_globalenv P_in prog stack_size) bl);
119! stmt ← stmt_at P_in … (joint_if_code … fn) lbl;
120! data ← bind_instantiate ?? (init … fn) (init_regs bl);
121match stmt with
122[ sequential step nxt ⇒
123    ! step_block ← bind_instantiate ?? (f_step … data lbl step) (f_regs bl lbl);
124    return |\fst (\fst step_block)|
125| final fin ⇒
126    ! fin_block ← bind_instantiate ?? (f_fin … data lbl fin) (f_regs bl lbl);
127    return |\fst fin_block|
128| FCOND abs _ _ _ ⇒ Ⓧabs
129].
130
131
132definition sigma_label : ∀p_in,p_out : sem_graph_params.
133joint_program p_in → (ident → option ℕ) →
134(∀globals.joint_closed_internal_function p_in globals
135         →bound_b_graph_translate_data p_in p_out globals) →
136(block → list register) → lbl_funct_type → regs_funct_type →
137block → label → option label ≝
138λp_in,p_out,prog,stack_size,init,init_regs,f_lbls,f_regs,bl,searched.
139! bl ← code_block_of_block bl ;
140! 〈id,fn〉 ← res_to_opt … (fetch_internal_function …
141                           (joint_globalenv p_in prog stack_size) bl);
142! 〈res,s〉 ←
143 find ?? (joint_if_code ?? fn)
144  (λlbl.λ_.match preamble_length … prog stack_size init init_regs f_regs bl lbl with
145             [ None ⇒ false
146             | Some n ⇒ match nth_opt ? n (lbl::(f_lbls bl lbl)) with
147                         [ None ⇒ false
148                         | Some x ⇒ eq_identifier … searched x
149                         ]
150             ]);
151return res.
152
153                                         
154                         
155
156lemma partial_inj_sigma_label :
157∀p_in,p_out,prog,stack_size,init,init_regs.
158∀f_lbls : lbl_funct_type.∀f_regs,bl,lbl1,lbl2.
159sigma_label p_in p_out prog stack_size init init_regs f_lbls f_regs bl lbl1 ≠ None ?→
160sigma_label p_in p_out prog stack_size init init_regs f_lbls f_regs bl lbl1 =
161sigma_label p_in p_out prog stack_size init init_regs f_lbls f_regs bl lbl2 →
162lbl1 = lbl2.
163#p_in #p_out #prog #stack_size #init #init_regs #f_lbls #f_regs #bl #lbl1 #lbl2
164inversion(sigma_label ????????? lbl1)
165[ #_ * #H @⊥ @H %]
166#lbl1' #H @('bind_inversion H) -H #bl' #EQbl'
167#H @('bind_inversion H) -H * #f #fn #H lapply(res_eq_from_opt ??? H) -H
168#EQfn #H @('bind_inversion H) -H * #res #stmt #H1 whd in ⊢ (??%? → ?);
169#EQ destruct(EQ) #_ #H lapply(sym_eq ??? H) -H #H @('bind_inversion H) -H
170#bl'' >EQbl' #EQ destruct(EQ) >EQfn >m_return_bind #H @('bind_inversion H) -H
171* #lbl2' #stmt' #H2 whd in ⊢ (??%? → ?); #EQ destruct(EQ)
172lapply(find_predicate ?????? H1) lapply(find_predicate ?????? H2)
173cases (preamble_length ?????????) normalize nodelta [*] #n cases(nth_opt ???)
174normalize nodelta
175[*] #lbl @eq_identifier_elim [2: #_ *] #EQ destruct(EQ) #_ @eq_identifier_elim
176[2: #_ *] #EQ destruct(EQ) #_ %
177qed.
178
179definition sigma_pc_opt : 
180∀p_in,p_out : sem_graph_params.
181joint_program p_in → (ident → option ℕ) →
182(∀globals.joint_closed_internal_function p_in globals
183         →bound_b_graph_translate_data p_in p_out globals) →
184(block → list register) → lbl_funct_type → regs_funct_type →
185program_counter → option program_counter ≝
186λp_in,p_out,prog,stack_size,init,init_regs,f_lbls,f_regs,pc.
187let target_point ≝ point_of_pc p_out pc in
188if (orb (eqZb   (block_id (pc_block pc)) (-1)) (eqZb (block_id (pc_block pc)) OZ)) then
189 return pc
190else
191 ! source_point ← sigma_label p_in p_out prog stack_size init init_regs
192                   f_lbls f_regs (pc_block pc) target_point;
193 return pc_of_point p_in (pc_block pc) source_point.
194
195
196lemma sigma_stored_pc_inj :
197∀p_in,p_out,prog,stack_size,init,init_regs,f_lbls,f_regs,pc,pc'.
198sigma_pc_opt p_in p_out prog stack_size init init_regs f_lbls f_regs pc ≠ None ? →
199sigma_pc_opt p_in p_out prog stack_size init init_regs f_lbls f_regs pc =
200sigma_pc_opt p_in p_out prog stack_size init init_regs f_lbls f_regs pc' →
201pc = pc'.
202#p_in #p_out #prog #stack_size #init #init_regs #f_lbls #f_regs
203* #bl1 #p1 * #bl2 #p2
204inversion(sigma_pc_opt ??????) [#_ * #H @⊥ @H %] #pc1
205whd in match sigma_pc_opt in ⊢ (% → ?); normalize nodelta
206@if_elim normalize nodelta #Hbl
207[2: #H @('bind_inversion H) -H * #pt1 #EQpt1]
208whd in ⊢ (??%? → ?); #EQ destruct(EQ)
209#_ #H lapply(sym_eq ??? H) -H whd in match sigma_pc_opt;
210normalize nodelta @if_elim #Hbl1 normalize nodelta
211[2,4: #H @('bind_inversion H) -H * #pt2 #EQpt2] whd in match pc_of_point;
212normalize nodelta whd in match (offset_of_point ??);
213whd in ⊢ (??%% → ?); #EQ destruct(EQ)
214[2,3: @⊥ lapply Hbl lapply Hbl1 @eqZb_elim normalize nodelta  #_ [1,3: **]
215  @eqZb_elim #_ **  |4: %]
216whd in match (offset_of_point ??) in EQpt2;
217<EQpt1 in EQpt2; #H lapply(partial_inj_sigma_label … (sym_eq ??? H))
218[ >EQpt1 % #EQ -prog destruct(EQ)] whd in match point_of_pc; normalize nodelta
219whd in match (point_of_offset ??); whd in match (point_of_offset ??);
220#EQ -prog destruct(EQ) %
221qed.
222
223
224definition sigma_stored_pc ≝
225λp_in,p_out,prog,stack_size,init,init_regs,f_lbls,f_regs,pc.
226  match sigma_pc_opt p_in p_out prog stack_size init init_regs f_lbls f_regs pc with
227      [None ⇒ null_pc (pc_offset … pc) | Some x ⇒ x].
228
229definition joint_state_relation ≝
230λP_in,P_out.program_counter → state P_in → state P_out → Prop.
231
232definition joint_state_pc_relation ≝ λP_in,P_out.state_pc P_in → state_pc P_out → Prop.
233
234definition seq_commutation_statement ≝
235  λP_in,P_out : sem_graph_params.
236  λprog : joint_program P_in.λstack_sizes : ident → option ℕ.
237  λinit : (∀globals.joint_closed_internal_function P_in globals
238         →bound_b_graph_translate_data P_in P_out globals).
239  λinit_regs : block → list register. λf_lbls : lbl_funct_type.
240  λf_regs : regs_funct_type.λst_no_pc_rel : joint_state_relation P_in P_out.
241  λst_rel : joint_state_pc_relation P_in P_out.
242  let trans_prog ≝ b_graph_transform_program P_in P_out init prog in
243  ∀st1,st1' : joint_abstract_status (mk_prog_params P_in prog stack_sizes) .
244  ∀st2 : joint_abstract_status (mk_prog_params P_out trans_prog stack_sizes).
245  ∀f,fn,stmt,nxt.
246  block_id … (pc_block (pc … st1)) ≠ -1 →
247  let seq ≝ (step_seq P_in ? stmt) in
248  fetch_statement P_in … (joint_globalenv P_in prog stack_sizes) (pc … st1) =
249  return 〈f, fn,  sequential … seq nxt〉 → 
250  eval_state P_in … (joint_globalenv P_in prog stack_sizes)
251  st1 = return st1' →
252  st_rel st1 st2 →
253  ∀t_fn.
254  fetch_internal_function …
255     (joint_globalenv P_out trans_prog stack_sizes) (pc_block (pc … st2)) =
256  return 〈f,t_fn〉 →
257  bind_new_P' ??
258     (λregs1.λdata.bind_new_P' ??
259      (λregs2.λblp.
260       ∃l : list (joint_seq P_out (globals ? (mk_prog_params P_out trans_prog stack_sizes))).
261                            blp = (ensure_step_block ?? l) ∧
262       ∃st2_fin_no_pc.
263           repeat_eval_seq_no_pc ? (mk_prog_params P_out trans_prog stack_sizes) f
264              l  (st_no_pc … st2)= return st2_fin_no_pc ∧
265           st_no_pc_rel (pc … st1') (st_no_pc … st1') st2_fin_no_pc
266      ) (f_step … data (point_of_pc P_in (pc … st1)) seq)
267     ) (init ? fn)
268.
269
270definition cond_commutation_statement ≝
271λP_in,P_out : sem_graph_params.
272  λprog : joint_program P_in.λstack_sizes : ident → option ℕ.
273  λinit : (∀globals.joint_closed_internal_function P_in globals
274         →bound_b_graph_translate_data P_in P_out globals).
275  λinit_regs : block → list register. λf_lbls : lbl_funct_type.
276  λf_regs : regs_funct_type.λst_no_pc_rel : joint_state_relation P_in P_out.
277  λst_rel : joint_state_pc_relation P_in P_out.
278 let trans_prog ≝ b_graph_transform_program P_in P_out init prog in
279    ∀st1 : joint_abstract_status (mk_prog_params P_in prog stack_sizes) .
280    ∀st2 : joint_abstract_status (mk_prog_params P_out trans_prog stack_sizes).
281    ∀f,fn,a,ltrue,lfalse,bv,b.
282    block_id … (pc_block (pc … st1)) ≠ -1 →
283    let cond ≝ (COND P_in ? a ltrue) in
284    fetch_statement P_in … (joint_globalenv P_in prog stack_sizes) (pc … st1) =
285    return 〈f, fn,  sequential … cond lfalse〉 → 
286    acca_retrieve … P_in (st_no_pc … st1) a = return bv →
287    bool_of_beval … bv = return b →
288    st_rel st1 st2 →
289    ∀t_fn.
290    fetch_internal_function …
291     (joint_globalenv P_out trans_prog stack_sizes) (pc_block (pc … st2)) =
292     return 〈f,t_fn〉 →
293    bind_new_P' ??
294     (λregs1.λdata.bind_new_P' ??
295     (λregs2.λblp.(\snd blp) = [ ] ∧
296        ∀mid.
297          stmt_at P_out … (joint_if_code ?? t_fn) mid
298          = return sequential P_out ? ((\snd (\fst blp)) mid) lfalse→
299         ∃st2_pre_mid_no_pc.
300            repeat_eval_seq_no_pc ? (mk_prog_params P_out trans_prog stack_sizes) f
301             (map_eval ?? (\fst (\fst blp)) mid) (st_no_pc ? st2)
302            = return st2_pre_mid_no_pc ∧
303            let new_pc ≝ if b then
304                           (pc_of_point P_in (pc_block (pc … st1)) ltrue)
305                         else
306                           (pc_of_point P_in (pc_block (pc … st1)) lfalse) in
307            st_no_pc_rel new_pc (st_no_pc … st1) (st2_pre_mid_no_pc) ∧
308            ∃a'. ((\snd (\fst blp)) mid)  = COND P_out ? a' ltrue ∧
309            ∃bv'. acca_retrieve … P_out st2_pre_mid_no_pc a' = return bv' ∧
310                  bool_of_beval … bv' = return b
311   )  (f_step … data (point_of_pc P_in (pc … st1)) cond)   
312  ) (init ? fn).
313 
314definition return_commutation_statement ≝
315λP_in,P_out : sem_graph_params.
316  λprog : joint_program P_in.λstack_sizes : ident → option ℕ.
317  λinit : (∀globals.joint_closed_internal_function P_in globals
318         →bound_b_graph_translate_data P_in P_out globals).
319  λinit_regs : block → list register. λf_lbls : lbl_funct_type.
320  λf_regs : regs_funct_type.λst_no_pc_rel : joint_state_relation P_in P_out.
321  λst_rel : joint_state_pc_relation P_in P_out.
322  let trans_prog ≝ b_graph_transform_program P_in P_out init prog in
323  ∀st1 : joint_abstract_status (mk_prog_params P_in prog stack_sizes) .
324  ∀st2 : joint_abstract_status (mk_prog_params P_out trans_prog stack_sizes).
325  ∀f,fn.
326  block_id … (pc_block (pc … st1)) ≠ -1 →
327  fetch_statement P_in … (joint_globalenv P_in prog stack_sizes) (pc … st1) =
328  return 〈f, fn,  final P_in ? (RETURN …)〉 → 
329  ∀n. stack_sizes f = return n →
330  let curr_ret ≝ joint_if_result … fn in
331  ∀st_pop,pc_pop.
332  pop_frame ?? P_in ? (joint_globalenv P_in prog stack_sizes) f curr_ret
333   (st_no_pc … st1) = return 〈st_pop,pc_pop〉 →
334  ∀nxt.∀f1,fn1,id,args,dest.
335    fetch_statement P_in … (joint_globalenv P_in prog stack_sizes) pc_pop  =
336    return 〈f1,fn1,sequential P_in … (CALL P_in ? id args dest) nxt〉 →
337  st_rel st1 st2 →
338  ∀t_fn.
339  fetch_internal_function …
340     (joint_globalenv P_out trans_prog stack_sizes) (pc_block (pc … st2)) =
341  return 〈f,t_fn〉 →
342  bind_new_P' ??
343     (λregs1.λdata.
344      bind_new_P' ??
345      (λregs2.λblp.
346       \snd blp = (RETURN …) ∧
347       ∃st_fin. repeat_eval_seq_no_pc ? (mk_prog_params P_out trans_prog stack_sizes) f
348              (\fst blp)  (st_no_pc … st2)= return st_fin ∧
349        ∃t_st_pop,t_pc_pop.
350        pop_frame ?? P_out ? (joint_globalenv P_out trans_prog stack_sizes) f
351         (joint_if_result … t_fn) st_fin = return 〈t_st_pop,t_pc_pop〉 ∧
352        sigma_stored_pc P_in P_out prog stack_sizes init init_regs f_lbls f_regs
353         t_pc_pop = pc_pop ∧
354        if eqZb (block_id (pc_block pc_pop)) (-1) then
355            st_no_pc_rel (pc_of_point P_in (pc_block pc_pop) nxt)
356             (decrement_stack_usage ? n st_pop) (decrement_stack_usage ? n t_st_pop) ∧
357            next_of_call_pc P_out … (joint_globalenv P_out trans_prog stack_sizes)
358             t_pc_pop = return nxt
359        else
360            bind_new_P' ??
361            (λregs4.λdata1.
362               bind_new_P' ??
363               (λregs3.λblp1.
364                 ∃st2'. repeat_eval_seq_no_pc ? (mk_prog_params P_out trans_prog stack_sizes) f1
365                      (\snd blp1) (decrement_stack_usage ? n t_st_pop) = return st2' ∧
366                      st_no_pc_rel (pc_of_point P_in (pc_block pc_pop) nxt)
367                       (decrement_stack_usage ? n st_pop) st2'
368               ) (f_step … data1 (point_of_pc P_in pc_pop) (CALL P_in ? id args dest))
369            ) (init ? fn1)
370          ) (f_fin … data (point_of_pc P_in (pc … st1)) (RETURN …))
371     ) (init ? fn).
372
373definition pre_main_commutation_statement ≝
374λP_in,P_out : sem_graph_params.
375  λprog : joint_program P_in.λstack_sizes : ident → option ℕ.
376  λinit : (∀globals.joint_closed_internal_function P_in globals
377         →bound_b_graph_translate_data P_in P_out globals).
378  λinit_regs : block → list register. λf_lbls : lbl_funct_type.
379  λf_regs : regs_funct_type.λst_no_pc_rel : joint_state_relation P_in P_out.
380  λst_rel : joint_state_pc_relation P_in P_out.
381let trans_prog ≝ b_graph_transform_program P_in P_out init prog in
382    ∀st1,st1' : joint_abstract_status (mk_prog_params P_in prog stack_sizes) .
383    ∀st2 : joint_abstract_status (mk_prog_params P_out trans_prog stack_sizes).
384    block_id … (pc_block (pc … st1)) = -1 →
385    eval_state P_in … (joint_globalenv P_in prog stack_sizes)
386     st1 = return st1' →
387    st_rel st1 st2 →
388    joint_classify … (mk_prog_params P_in prog stack_sizes) st1 =
389     joint_classify … (mk_prog_params P_out trans_prog stack_sizes) st2 ∧
390    ∃st2'. st_rel st1' st2' ∧
391    eval_state P_out …
392     (joint_globalenv P_out trans_prog stack_sizes) st2 = return st2' ∧
393    as_label (joint_status P_in prog stack_sizes) st1' =
394     as_label (joint_status P_out trans_prog stack_sizes) st2'.
395
396definition call_commutation_statement ≝
397λP_in,P_out : sem_graph_params.
398  λprog : joint_program P_in.λstack_sizes : ident → option ℕ.
399  λinit : (∀globals.joint_closed_internal_function P_in globals
400         →bound_b_graph_translate_data P_in P_out globals).
401  λinit_regs : block → list register. λf_lbls : lbl_funct_type.
402  λf_regs : regs_funct_type.λst_no_pc_rel : joint_state_relation P_in P_out.
403  λst_rel : joint_state_pc_relation P_in P_out.
404let trans_prog ≝ b_graph_transform_program P_in P_out init prog in
405  ∀st1 : joint_abstract_status (mk_prog_params P_in prog stack_sizes) .
406  ∀st2 : joint_abstract_status (mk_prog_params P_out trans_prog stack_sizes).
407  ∀f,fn,id,arg,dest,nxt.
408  block_id … (pc_block (pc … st1)) ≠ -1 →
409  fetch_statement P_in … (joint_globalenv P_in prog stack_sizes) (pc … st1) =
410  return 〈f, fn,  sequential P_in ? (CALL P_in ? id arg dest) nxt〉 →
411  ∀bl.
412   block_of_call P_in … (joint_globalenv P_in prog stack_sizes) id (st_no_pc … st1)
413      = return bl →
414  ∀f1,fn1.
415   fetch_internal_function …
416    (joint_globalenv P_in prog stack_sizes) bl =  return 〈f1,fn1〉 →
417  ∀st1_pre.
418  save_frame … P_in (kind_of_call P_in id) dest st1 = return st1_pre →
419  ∀n.stack_sizes f1 = return n → 
420  ∀st1'.
421  setup_call ?? P_in n (joint_if_params … fn1) arg st1_pre = return st1' →
422  st_rel st1 st2 → 
423  ∀t_fn1.
424  fetch_internal_function … (joint_globalenv P_out trans_prog stack_sizes) bl =
425  return 〈f1,t_fn1〉 →
426  bind_new_P' ??
427    (λregs1.λdata.
428     bind_new_P' ??
429      (λregs2.λblp.
430        ∀pc',t_fn,id',arg',dest',nxt1.
431          sigma_stored_pc P_in P_out prog stack_sizes init init_regs
432           f_lbls f_regs pc' = (pc … st1) →
433          fetch_statement P_out … (joint_globalenv P_out trans_prog stack_sizes) pc'
434          = return 〈f,t_fn,
435                    sequential P_out ? ((\snd (\fst blp)) (point_of_pc P_out pc')) nxt1〉→
436          ((\snd (\fst blp)) (point_of_pc P_out pc')) = (CALL P_out ? id' arg' dest') → 
437       ∃st2_pre_call.
438        repeat_eval_seq_no_pc ? (mk_prog_params P_out trans_prog stack_sizes) f
439          (map_eval ?? (\fst (\fst blp)) (point_of_pc P_out pc')) (st_no_pc ? st2) = return st2_pre_call ∧
440       block_of_call P_out … (joint_globalenv P_out trans_prog stack_sizes) id'
441        st2_pre_call = return bl ∧
442       ∃st2_pre.
443        save_frame … P_out (kind_of_call P_out id') dest'
444         (mk_state_pc ? st2_pre_call pc' (last_pop … st2)) = return st2_pre ∧
445       ∃st2_after_call.
446         setup_call ?? P_out n (joint_if_params … t_fn1) arg' st2_pre
447          = return st2_after_call ∧
448       bind_new_P' ??
449         (λregs11.λdata1.
450          ∃st2'.
451           repeat_eval_seq_no_pc ? (mk_prog_params P_out trans_prog stack_sizes) f1
452           (added_prologue … data1) (increment_stack_usage P_out n st2_after_call) =
453           return st2' ∧
454           st_no_pc_rel (pc_of_point P_in bl (joint_if_entry … fn1))
455            (increment_stack_usage P_in n st1') st2'               
456         ) (init ? fn1)
457     )
458     (f_step … data (point_of_pc P_in (pc … st1)) (CALL P_in ? id arg dest))
459    ) (init ? fn).
460   
461definition goto_commutation_statement ≝
462λP_in,P_out : sem_graph_params.
463  λprog : joint_program P_in.λstack_sizes : ident → option ℕ.
464  λinit : (∀globals.joint_closed_internal_function P_in globals
465         →bound_b_graph_translate_data P_in P_out globals).
466  λinit_regs : block → list register. λf_lbls : lbl_funct_type.
467  λf_regs : regs_funct_type.λst_no_pc_rel : joint_state_relation P_in P_out.
468  λst_rel : joint_state_pc_relation P_in P_out.
469let trans_prog ≝ b_graph_transform_program P_in P_out init prog in
470  ∀st1 : joint_abstract_status (mk_prog_params P_in prog stack_sizes) .
471  ∀st2 : joint_abstract_status (mk_prog_params P_out trans_prog stack_sizes).
472  ∀f,fn,lbl.
473  block_id … (pc_block (pc … st1)) ≠ -1 →
474  fetch_statement P_in … (joint_globalenv P_in prog stack_sizes) (pc … st1) =
475  return 〈f, fn,  final P_in ? (GOTO ? lbl)〉 →
476  st_rel st1 st2 →
477  ∀t_fn.
478  fetch_internal_function …
479     (joint_globalenv P_out trans_prog stack_sizes) (pc_block (pc … st2)) =
480  return 〈f,t_fn〉 →
481  bind_new_P' ??
482     (λregs1.λdata.
483      bind_new_P' ??
484      (λregs2.λblp.
485        ∃st_fin. repeat_eval_seq_no_pc ? (mk_prog_params P_out trans_prog stack_sizes) f
486              (\fst blp)  (st_no_pc … st2)= return st_fin ∧
487        \snd blp = GOTO ? lbl ∧
488        st_no_pc_rel (pc_of_point P_in (pc_block (pc … st1)) lbl)
489           (st_no_pc … st1) (st_fin)
490      ) (f_fin … data (point_of_pc P_in (pc … st1)) (GOTO ? lbl))
491     ) (init ? fn).
492     
493definition tailcall_commutation_statement ≝
494λP_in,P_out : sem_graph_params.
495  λprog : joint_program P_in.λstack_sizes : ident → option ℕ.
496  λinit : (∀globals.joint_closed_internal_function P_in globals
497         →bound_b_graph_translate_data P_in P_out globals).
498  λinit_regs : block → list register. λf_lbls : lbl_funct_type.
499  λf_regs : regs_funct_type.λst_no_pc_rel : joint_state_relation P_in P_out.
500  λst_rel : joint_state_pc_relation P_in P_out.
501  let trans_prog ≝ b_graph_transform_program P_in P_out init prog in
502  ∀st1 : joint_abstract_status (mk_prog_params P_in prog stack_sizes) .
503  ∀st2 : joint_abstract_status (mk_prog_params P_out trans_prog stack_sizes).
504  ∀f,fn,has_tail,id,arg.
505  block_id … (pc_block (pc … st1)) ≠ -1 →
506  fetch_statement P_in … (joint_globalenv P_in prog stack_sizes) (pc … st1) =
507  return 〈f, fn,  final P_in ? (TAILCALL P_in has_tail id arg)〉 →
508  ∀bl.
509   block_of_call P_in … (joint_globalenv P_in prog stack_sizes) id (st_no_pc … st1)
510      = return bl →
511   ∀f1,fn1.
512   fetch_internal_function …
513    (joint_globalenv P_in prog stack_sizes) bl =  return 〈f1,fn1〉 →
514   ∀ssize_f.stack_sizes f = return ssize_f →
515   ∀ssize_f1.stack_sizes f1 = return ssize_f1 →   
516   ∀st1'.
517    setup_call ?? P_in ssize_f1 (joint_if_params … fn1) arg
518     (decrement_stack_usage P_in ssize_f (st_no_pc … st1)) = return st1' →
519   st_rel st1 st2 → 
520   ∀t_fn1.
521   fetch_internal_function … (joint_globalenv P_out trans_prog stack_sizes) bl =
522   return 〈f1,t_fn1〉 →
523   bind_new_P' ??
524    (λregs1.λdata.
525     bind_new_P' ??
526      (λregs2.λblp.
527       ∃ has_tail',id',arg'.
528       (\snd blp) = TAILCALL P_out has_tail' id' arg' ∧
529       ∃st2_pre_call.
530        repeat_eval_seq_no_pc ? (mk_prog_params P_out trans_prog stack_sizes) f
531         (\fst blp) (st_no_pc ? st2) = return st2_pre_call ∧
532        block_of_call P_out … (joint_globalenv P_out trans_prog stack_sizes) id'
533         st2_pre_call = return bl ∧
534       ∃st2_after.
535        setup_call ?? P_out ssize_f1 (joint_if_params … t_fn1) arg'
536         (decrement_stack_usage P_out ssize_f st2_pre_call) = return st2_after ∧
537       bind_new_P' ??
538         (λregs11.λdata1.
539          ∃st2'.
540           repeat_eval_seq_no_pc ? (mk_prog_params P_out trans_prog stack_sizes) f1
541           (added_prologue … data1) (increment_stack_usage P_out ssize_f1 st2_after) =
542           return st2' ∧
543           st_no_pc_rel (pc_of_point P_in bl (joint_if_entry … fn1))
544            (increment_stack_usage P_in ssize_f1 st1') st2'               
545         ) (init ? fn1)
546     ) (f_fin … data (point_of_pc P_in (pc … st1)) (TAILCALL P_in has_tail id arg))
547   ) (init ? fn).
548
549record good_state_relation (P_in : sem_graph_params)
550   (P_out : sem_graph_params) (prog : joint_program P_in)
551   (stack_sizes : ident → option ℕ)
552   (init : ∀globals.joint_closed_internal_function P_in globals
553         →bound_b_graph_translate_data P_in P_out globals)
554   (init_regs : block → list register) (f_lbls : lbl_funct_type)
555   (f_regs : regs_funct_type)
556   (st_no_pc_rel : joint_state_relation P_in P_out)
557   (st_rel : joint_state_pc_relation P_in P_out) : Prop ≝
558{ good_translation :> b_graph_transform_program_props P_in P_out stack_sizes init
559                     prog init_regs f_lbls f_regs
560; fetch_ok_sigma_state_ok :
561   ∀st1,st2,f,fn. st_rel st1 st2 →
562    fetch_internal_function …
563     (joint_globalenv P_in prog stack_sizes) (pc_block (pc … st1)) 
564     = return 〈f,fn〉 →
565     st_no_pc_rel (pc … st1) (st_no_pc … st1) (st_no_pc … st2)
566; fetch_ok_pc_ok :
567  ∀st1,st2,f,fn.st_rel st1 st2 →
568   fetch_internal_function …
569     (joint_globalenv P_in prog stack_sizes) (pc_block (pc … st1)) 
570     = return 〈f,fn〉 →
571   pc … st1 = pc … st2
572; fetch_ok_sigma_last_pop_ok :
573  ∀st1,st2,f,fn.st_rel st1 st2 →
574   fetch_internal_function …
575     (joint_globalenv P_in prog stack_sizes) (pc_block (pc … st1)) 
576     = return 〈f,fn〉 →
577   (last_pop … st1) = sigma_stored_pc P_in P_out prog stack_sizes init init_regs
578                       f_lbls f_regs (last_pop … st2)
579; st_rel_def :
580  ∀st1,st2,pc,lp1,lp2,f,fn.
581  fetch_internal_function …
582     (joint_globalenv P_in prog stack_sizes) (pc_block pc) = return 〈f,fn〉 →
583   st_no_pc_rel pc st1 st2 →
584   lp1 = sigma_stored_pc P_in P_out prog stack_sizes init init_regs
585          f_lbls f_regs lp2 →
586  st_rel (mk_state_pc ? st1 pc lp1) (mk_state_pc ? st2 pc lp2)
587; pre_main_ok : pre_main_commutation_statement P_in P_out prog stack_sizes init
588                    init_regs f_lbls f_regs st_no_pc_rel st_rel
589; pre_main_no_return :
590    ∀f,fn,pc. block_id (pc_block pc) = -1 →
591    fetch_statement P_in … (joint_globalenv P_in prog stack_sizes) pc =
592      return 〈f,fn,final P_in ? (RETURN …)〉 → False
593; cond_commutation : cond_commutation_statement P_in P_out prog stack_sizes init
594                    init_regs f_lbls f_regs st_no_pc_rel st_rel
595; seq_commutation : seq_commutation_statement P_in P_out prog stack_sizes init
596                    init_regs f_lbls f_regs st_no_pc_rel st_rel
597;  call_is_call :∀f,fn,bl.
598  fetch_internal_function …
599     (joint_globalenv P_in prog stack_sizes) bl = return 〈f,fn〉 →
600   ∀id,args,dest,lbl.
601    bind_new_P' ??
602     (λregs1.λdata.bind_new_P' ??
603      (λregs2.λblp.
604        ∀lbl.∃id',args',dest'.((\snd (\fst blp)) lbl) = CALL P_out ? id' args' dest')
605      (f_step … data lbl (CALL P_in ? id args dest)))
606     (init ? fn)
607; cost_commutation :
608  let trans_prog ≝ b_graph_transform_program P_in P_out init prog in
609  ∀st1,st2,pc.∀f,fn,c,nxt.
610  block_id … (pc_block pc) ≠ -1 →
611  st_no_pc_rel pc st1 st2 →
612  fetch_statement P_in … (joint_globalenv P_in prog stack_sizes) pc =
613  return 〈f, fn,  sequential ?? (COST_LABEL ?? c) nxt〉 → 
614  st_no_pc_rel (pc_of_point P_in (pc_block pc) nxt) st1 st2
615; return_commutation : return_commutation_statement P_in P_out prog stack_sizes init
616                    init_regs f_lbls f_regs st_no_pc_rel st_rel
617; call_commutation : call_commutation_statement P_in P_out prog stack_sizes init
618                    init_regs f_lbls f_regs st_no_pc_rel st_rel
619; goto_commutation : goto_commutation_statement P_in P_out prog stack_sizes init
620                    init_regs f_lbls f_regs st_no_pc_rel st_rel
621; tailcall_commutation : tailcall_commutation_statement P_in P_out prog stack_sizes
622                   init init_regs f_lbls f_regs st_no_pc_rel st_rel
623; as_result_ok :
624  let trans_prog ≝ b_graph_transform_program P_in P_out init prog in   
625  ∀st1 : joint_abstract_status (mk_prog_params P_in prog stack_sizes) .
626  ∀st2 : joint_abstract_status (mk_prog_params P_out trans_prog stack_sizes).
627  st_rel st1 st2 →
628  as_result … st1 = as_result … st2
629; as_label_premain_ok :
630  let trans_prog ≝ b_graph_transform_program P_in P_out init prog in   
631  ∀st1 : joint_abstract_status (mk_prog_params P_in prog stack_sizes) .
632  ∀st2 : joint_abstract_status (mk_prog_params P_out trans_prog stack_sizes).
633  block_id … (pc_block (pc … st1)) = -1 →
634  st_rel st1 st2 → as_label … st1 = as_label … st2
635}.
636
637record good_init_relation (P_in : sem_graph_params) (P_out : sem_graph_params)
638(prog : joint_program P_in) (stack_sizes : ident → option ℕ)
639(init : ∀globals.joint_closed_internal_function P_in globals
640         →bound_b_graph_translate_data P_in P_out globals)
641(st_no_pc_rel : joint_state_relation P_in P_out) : Prop ≝
642{ good_empty :
643   ∀m0.init_mem … (λx.x) prog = return m0 →
644   let 〈m,spb〉 as H ≝ alloc … m0 0 external_ram_size in
645   let trans_prog ≝ b_graph_transform_program P_in P_out init prog in
646   let globals_size ≝ globals_stacksize … prog in
647   let globals_size' ≝ globals_stacksize … trans_prog in
648   let spp ≝ mk_pointer spb (mk_offset (bitvector_of_Z 16 (-S (globals_size)))) in
649   let spp' ≝ mk_pointer spb (mk_offset (bitvector_of_Z 16 (-S (globals_size')))) in
650   ∀prf,prf'.
651   let st ≝ mk_state P_in (Some ? (empty_framesT …)) empty_is
652                (BBbit false) (empty_regsT … «spp,prf») m 0 in
653   let st' ≝ mk_state P_out (Some ? (empty_framesT …)) empty_is
654                (BBbit false) (empty_regsT … «spp',prf'») m 0 in
655   st_no_pc_rel init_pc (set_sp … «spp,prf» st) (set_sp … «spp',prf'» st')
656; good_init_cost_label :
657  let trans_prog ≝ b_graph_transform_program P_in P_out init prog in
658  match fetch_statement P_in … (joint_globalenv P_in prog stack_sizes) init_pc with
659  [ OK x ⇒ match cost_label_of_stmt … (\snd x) with
660           [ Some c ⇒ ∃y.
661                     fetch_statement P_out …
662                      (joint_globalenv P_out trans_prog stack_sizes) init_pc =
663                     OK ? y ∧
664                     cost_label_of_stmt … (\snd y) = Some ? c
665           | None ⇒ (∃e'.
666                     fetch_statement P_out …
667                      (joint_globalenv P_out trans_prog stack_sizes) init_pc =
668                     Error ? e') ∨
669                    (∃x.
670                     fetch_statement P_out …
671                      (joint_globalenv P_out trans_prog stack_sizes) init_pc =
672                       OK ? x ∧ cost_label_of_stmt … (\snd x) = None ?)
673           ]
674  | Error e ⇒
675    (∃e'.
676     fetch_statement P_out … (joint_globalenv P_out trans_prog stack_sizes) init_pc =
677      Error ? e') ∨
678    (∃x.
679      fetch_statement P_out … (joint_globalenv P_out trans_prog stack_sizes) init_pc =
680      OK ? x ∧ cost_label_of_stmt … (\snd x) = None ?)
681  ]
682}.
683
684
685lemma code_block_of_block_eq : ∀bl : Σb.block_region b = Code.
686code_block_of_block bl = return bl.
687* #bl #prf whd in match code_block_of_block; normalize nodelta @match_reg_elim
688[ >prf in ⊢ (% → ?); #ABS destruct(ABS)] #prf1 %
689qed.
690
691(*TO BE MOVED*)
692lemma Exists_append1 : ∀A.∀l1,l2 : list A.∀P.Exists A P l1 → Exists A P (l1@l2).
693#A #l1 elim l1 [#l2 #P *] #hd #tl #IH *
694[#P normalize // ] #hd1 #tl1 #P normalize * [#H % assumption | #H %2 @IH assumption]
695qed.
696
697lemma Exists_append2 : ∀A.∀l1,l2 : list A.∀P.Exists A P l2 → Exists A P (l1@l2).
698#A #l1 #l2 lapply l1 -l1 elim l2 [#l1 #a *] #hd #tl #IH *
699[#a normalize // ] #hd1 #tl1 #a normalize *
700[ #H %2 >append_cons @Exists_append1 elim tl1 [% assumption] #hd2 #tl2 #H1 normalize %2 //
701| #H %2 >append_cons @IH assumption]
702qed.
703
704lemma seq_list_in_code_length : ∀p : params. ∀globals : list ident.
705∀code : codeT p globals.∀src : code_point p.∀l1,l2,dst.
706seq_list_in_code p globals code src l1 l2 dst → |l1| = |l2|.
707#p #globals #code #src #l1 lapply src -src elim l1
708[ #src * [ #dst #_ %] #hd #tl #dst whd in ⊢ (% → ?); * #EQ destruct]
709#hd #tl #IH #src * [ #dst whd in ⊢ (% → ?); * #mid * #rest ** #EQ destruct]
710#hd1 #tl1 #dst whd in ⊢ (% → ?); * #mid * #rest ** #EQ destruct * #nxt1 * #EQstnt
711#EQsucc #H whd in ⊢ (??%%); @eq_f @(IH … H)
712qed.
713
714lemma fetch_stmt_ok_succ_ok : ∀p : sem_graph_params.
715∀prog : joint_program p.∀stack_size,f,fn,stmt,pc,pc',lbl.
716In ? (stmt_labels p ? stmt) lbl→
717fetch_statement p … (joint_globalenv p prog stack_size) pc = return 〈f,fn,stmt〉 →
718pc' = pc_of_point p (pc_block pc) lbl →
719∃stmt'.fetch_statement p … (joint_globalenv p prog stack_size) pc' = return 〈f,fn,stmt'〉.
720#p #prog #stack_size #f #fn #stmt #pc #pc' #lbl #Hlbl #EQfetch
721cases(fetch_statement_inv … EQfetch) #EQfn normalize nodelta #EQstmt
722#EQ destruct(EQ) lapply(code_is_closed … (pi2 ?? fn) … EQstmt) *
723cases(decidable_In ? (stmt_explicit_labels … stmt) lbl ?)
724[3: * cases lbl #x #y cases(decidable_eq_pos … x y)
725    [#EQ destruct % % | * #H %2 % #H1 @H destruct %]
726| whd in ⊢ (% → ?); #H1 #H2 cases(Exists_All … H1 H2) #lbl1 * #EQ destruct
727  whd in match code_has_label; whd in match code_has_point; normalize nodelta
728  inversion(stmt_at ????) [#_ *] #stmt' #EQstmt' #_ #_ %{stmt'}
729  whd in match fetch_statement; normalize nodelta >EQfn >m_return_bind
730  >point_of_pc_of_point >EQstmt' %
731| #H lapply(In_all ??? H) -H cases(Exists_append … Hlbl)
732  [ cases stmt [ #step #nxt | #fin | *] whd in match stmt_implicit_label;
733    normalize nodelta [2: *] * [2: *] #EQ destruct(EQ) #_ #_
734    whd in match stmt_forall_succ; whd in match code_has_point; normalize nodelta
735    inversion(stmt_at ????) [#_ *] #stmt' #EQstmt' #_ %{stmt'}
736    whd in match fetch_statement; normalize nodelta >EQfn >m_return_bind
737    >point_of_pc_of_point >EQstmt' %
738  | #H1 #H2 cases(Exists_All … H1 H2) #x * #EQ destruct * #H @⊥ @H %
739  ]
740]
741qed.
742
743
744lemma fetch_stmt_ok_nxt_ok : ∀p : sem_graph_params.
745∀prog : joint_program p.∀stack_size,f,fn,stmt,bl,pt,nxt.
746fetch_internal_function … (joint_globalenv p prog stack_size) bl =
747return 〈f,fn〉→
748stmt_at p … (joint_if_code … fn) pt = return sequential p ? stmt nxt →
749∃stmt'.
750stmt_at p … (joint_if_code … fn) nxt = return stmt'.
751#p #prog #stack_size #f #fn #stmt #bl #pt #nxt #EQfn #EQstmt
752cases(fetch_stmt_ok_succ_ok …
753       prog stack_size f fn (sequential p … stmt nxt) (pc_of_point p bl pt)
754       (pc_of_point p bl nxt) nxt ???)
755[ #stmt' #H cases(fetch_statement_inv … H) -H #_ >point_of_pc_of_point normalize nodelta
756  #EQstmt' %{stmt'} assumption
757| whd in match stmt_labels; normalize nodelta % %
758| whd in match fetch_statement; normalize nodelta >EQfn >m_return_bind
759  >point_of_pc_of_point >EQstmt %
760| %
761]
762qed.
763
764
765lemma sigma_label_spec : ∀p_in,p_out,prog,stack_size,init,init_regs.
766∀f_lbls : lbl_funct_type.∀f_regs.
767b_graph_transform_program_props p_in p_out stack_size init prog init_regs f_lbls f_regs →
768∀id,fn.
769∀bl:Σb.block_region b = Code. ∀pt,stmt.
770block_id … bl ≠ -1 →
771fetch_internal_function …
772   (joint_globalenv p_in prog stack_size) bl = return 〈id,fn〉 →
773stmt_at p_in … (joint_if_code … fn) pt = return stmt → 
774∃n.preamble_length … prog stack_size init init_regs f_regs bl pt = return n ∧
775match n with
776[ O ⇒ sigma_label p_in p_out prog stack_size init init_regs f_lbls f_regs bl pt = return pt
777| S m ⇒ ∃lbl.nth_opt ? m (f_lbls bl pt) = return lbl ∧
778    sigma_label p_in p_out prog stack_size init init_regs f_lbls f_regs bl lbl = return pt     
779].
780#p_in #p_out #prog #stack_size #init #init_regs #f_lbls #f_regs #good #id #fn
781#bl #pt #stmt * #Hbl #EQfn #EQstmt   
782lapply(b_graph_transform_program_fetch_internal_function … good … bl id fn)
783@eqZb_elim [ #EQ >EQ in Hbl; #H @⊥ @H %] #_ normalize nodelta #H cases(H EQfn) -H
784#data * #t_fn ** #EQt_fn #Hinit * #_ #_ #_ #pp_labs #_ #fresh_lab #_ #_ #_ #H
785lapply(H … EQstmt) -H normalize nodelta cases stmt in EQstmt; -stmt
786[ #j_step #nxt | #fin | * ] #EQstmt normalize nodelta **
787[ * #pre_instr #instr #post_instr | #pre_instr #instr] *
788[ cases(added_prologue ????) [2: #hd_instr #tl_instrs ] normalize nodelta
789 [ @eq_identifier_elim #EQentry normalize nodelta
790   [ whd in ⊢ (% → ?); inversion (f_regs ??) [2: #x #y #_ #_ *] #EQregs normalize nodelta
791     whd in ⊢ (???% → ?); #EQ destruct(EQ)
792   |*: #Hregs
793   ]
794 | #Hregs
795 ]
796| #Hregs
797]
798#syntax_spec
799[4: cases(added_prologue ????) [2: #hd_instr #tl_instrs ] normalize nodelta ] #_
800[1,2,4,5: %{(|pre_instr|)} | %{O}]
801cut(? : Prop)
802[3,6,9,12,15: #EQn %{EQn} whd in EQn; normalize nodelta
803 [1,2,3,4: cases pre_instr in Hregs syntax_spec EQn; [2,4,6,8: #hd #tl] #Hregs #syntax_spec
804           whd in match (length ??); #EQn whd in match (length ??); normalize nodelta]
805 [5,6,7,8,9: whd in match sigma_label; normalize nodelta >code_block_of_block_eq
806     >m_return_bind >EQfn >m_return_bind inversion(find ????)
807     [1,3,5,7,9: #EQfind @⊥ lapply(find_none ?????? EQfind EQstmt) >EQn normalize nodelta
808     @eq_identifier_elim [1,3,5,7,9: #_ *] * #H #_ @H % ] * #lbl1 #stmt1 #EQfind
809     >m_return_bind @eq_f lapply(find_predicate ?????? EQfind) whd in match preamble_length;
810     normalize nodelta >code_block_of_block_eq >m_return_bind >EQfn >m_return_bind
811     whd in match (stmt_at ????); >(find_lookup ?????? EQfind) >m_return_bind >Hinit
812     >m_return_bind cases stmt1 in EQfind; -stmt1
813     [1,4,7,10,13: #j_step1 #nxt1 |2,5,8,11,14: #fin1 |*: *] #EQfind normalize nodelta
814     cases(bind_instantiate ????) [1,3,5,7,9,11,13,15,17,19: *]
815     [1,2,3,4,5: ** #pre_instr1 #instr1 #post_instr1 |*: * #pre_instr1 #instr1 ]
816     >m_return_bind cases pre_instr1 -pre_instr1 [2,4,6,8,10,12,14,16,18,20: #hd #tl]
817     whd in match (length ??); normalize nodelta
818     [11,12,13,14,15,16,17,18,19,20: @eq_identifier_elim [2,4,6,8,10,12,14,16,18,20: #_ *]
819     #EQ #_ >EQ %]
820     whd in match (nth_opt ???); inversion(nth_opt ???) [1,3,5,7,9,11,13,15,17,19: #_ *]
821     #lbl2 #EQlbl2 normalize nodelta @eq_identifier_elim [2,4,6,8,10,12,14,16,18,20: #_ *]
822     #EQ lapply EQlbl2 destruct(EQ) #EQlbl2 @⊥
823     cases(Exists_All … (nth_opt_Exists … EQlbl2) (fresh_lab lbl1)) #x * #EQ destruct(EQ)
824     ** #H #_ @H  @(code_is_in_universe … (pi2 ?? fn)) whd in match code_has_label;
825     whd in match code_has_point; normalize nodelta >EQstmt @I
826 |*: cases syntax_spec -syntax_spec #pre * #mid1 [3,4: * #mid2 * #post] ** [1,2: *]
827     cases pre -pre [1,3,5,7: #_ * #x * #y ** #ABS destruct(ABS)] #hd1 #tl1 whd in ⊢ (??%% → ?);
828     #EQ destruct(EQ) -EQ #pre_spec whd in ⊢ (% → ?);
829     [1,2: * #nxt1 * #EQt_stmt change with nxt1 in ⊢ (??%? → ?); #EQ destruct(EQ) #post_spec
830     |*:   #EQt_stmt
831     ]
832     %{mid1} cut(? : Prop)
833     [3,6,9,12: #EQnth_opt %{EQnth_opt} whd in match sigma_label; normalize nodelta
834       >code_block_of_block_eq >m_return_bind >EQfn >m_return_bind inversion(find ????)
835       [1,3,5,7: #EQfind @⊥ lapply(find_none ?????? EQfind EQstmt) >EQn normalize nodelta
836         whd in match (nth_opt ???); >EQnth_opt normalize nodelta @eq_identifier_elim
837         [1,3,5,7: #_ *] * #H #_ @H % ] * #lbl1 #stmt1 #EQfind >m_return_bind @eq_f
838       lapply(find_predicate ?????? EQfind) whd in match preamble_length;
839       normalize nodelta >code_block_of_block_eq >m_return_bind >EQfn >m_return_bind
840       whd in match (stmt_at ????); >(find_lookup ?????? EQfind) >m_return_bind >Hinit
841       >m_return_bind cases stmt1 in EQfind; -stmt1
842       [1,4,7,10: #j_step1 #nxt1 |2,5,8,11: #fin1 |*: *] #EQfind normalize nodelta
843       cases(bind_instantiate ????) [1,3,5,7,9,11,13,15: *]
844       [1,2,3,4: ** #pre_instr1 #instr1 #post_instr1 |*: * #pre_instr1 #instr1]
845       >m_return_bind cases pre_instr1 -pre_instr1 [2,4,6,8,10,12,14,16: #hd1 #tl1]
846       whd in match (length ??); normalize nodelta whd in match (nth_opt ???);
847       [1,2,3,4,5,6,7,8: inversion(nth_opt ???) [1,3,5,7,9,11,13,15: #_ *] #lbl2
848         #EQlbl2 normalize nodelta @eq_identifier_elim [2,4,6,8,10,12,14,16: #_ *]
849         #EQ lapply EQlbl2 destruct(EQ) #EQlbl2 #_  @(proj2 … pp_labs ?? lbl2)
850         @Exists_memb [1,3,5,7,9,11,13,15: @(nth_opt_Exists … EQlbl2)]
851         >e0 @Exists_append2 % %
852       |*: @eq_identifier_elim [2,4,6,8,10,12,14,16: #_ *] #EQ destruct(EQ) @⊥
853         lapply(fresh_lab hd1) >e0 #H cases(append_All … H) #_ * -H ** #H #_ #_ @H
854         @(code_is_in_universe … (pi2 ?? fn)) whd in match code_has_label;
855         whd in match code_has_point; normalize nodelta whd in match (stmt_at ????);
856         >(find_lookup ?????? EQfind) @I
857       ]   
858     |2,5,8,11: >e0 cases pre_spec #fst * #rest ** #EQ destruct(EQ)
859                whd in ⊢ (% → ?); * #nxt1 * #_ change with nxt1 in ⊢ (??%? → ?);
860                #EQ destruct(EQ) #H lapply(seq_list_in_code_length … H)
861                [1,2: >length_map] -H #H >H >nth_opt_append_r cases(|rest|)
862                try % try( #n %) #n <minus_n_n %
863     |*:
864     ]
865  ]
866 |2,5,8,11,14: whd in match preamble_length; normalize nodelta >code_block_of_block_eq
867   >m_return_bind >EQfn >m_return_bind >EQstmt >m_return_bind >Hinit >m_return_bind
868   normalize nodelta
869   [1,2,3,4: >Hregs %
870   | >EQregs <EQentry in EQstmt; cases(entry_is_cost … (pi2 ?? fn)) #succ * #c
871     #EQstmt >EQstmt whd in ⊢ (???% → ?); #EQ destruct(EQ) >(f_step_on_cost … data)
872     whd in match (bind_instantiate ????); %
873   ]
874 |*:
875 ]
876qed.
877
878lemma pc_block_eq : ∀p_in,p_out,prog,stack_sizes,init,init_regs,f_lbls,
879f_regs,pc.
880sigma_pc_opt p_in p_out prog stack_sizes init
881   init_regs f_lbls f_regs pc ≠ None ? →
882 pc_block … pc =
883 pc_block … (sigma_stored_pc p_in p_out prog stack_sizes init
884                                           init_regs f_lbls f_regs pc).
885#p_in #p_out #prog #stack_sizes #init #init_regs #f_lbls #f_regs #pc
886whd in match sigma_stored_pc; normalize nodelta
887inversion(sigma_pc_opt ?????????) [ #_ * #ABS @⊥ @ABS %] #pc1
888whd in match sigma_pc_opt; normalize nodelta @if_elim
889[ #_ whd in ⊢ (??%? → ?); #EQ destruct(EQ) #_ %] #_
890#H @('bind_inversion H) -H #lbl #_ whd in ⊢ (??%? → ?); #EQ destruct #_ %
891qed.
892
893definition stmt_get_next : ∀p,globals.joint_statement p globals → option (succ p) ≝
894λp,globals,stmt.
895match stmt with
896[sequential stmt nxt ⇒ Some ? nxt
897| _ ⇒ None ?
898].
899
900
901definition sigma_next : ∀p_in,p_out : sem_graph_params.
902joint_program p_in → (ident → option ℕ) →
903(∀globals.joint_closed_internal_function p_in globals
904         →bound_b_graph_translate_data p_in p_out globals) →
905(block → list register) → lbl_funct_type → regs_funct_type →
906block → label → option label ≝
907λp_in,p_out,prog,stack_size,init,init_regs,f_lbls,f_regs,bl,searched.
908! bl ← code_block_of_block bl ;
909! 〈id,fn〉 ← res_to_opt … (fetch_internal_function …
910                           (joint_globalenv p_in prog stack_size) bl);
911! 〈res,s〉 ←
912 find ?? (joint_if_code ?? fn)
913  (λlbl.λstmt.match stmt_get_next … stmt with
914    [ None ⇒ false
915    | Some nxt ⇒
916       match preamble_length … prog stack_size init init_regs f_regs bl lbl with
917        [ None ⇒ false
918        | Some n ⇒ match nth_opt ? n ((f_lbls bl lbl) @ [nxt]) with
919                         [ None ⇒ false
920                         | Some x ⇒ eq_identifier … searched x
921                         ]
922        ]
923    ]);
924stmt_get_next … s.
925
926lemma fetch_internal_function_no_zero :
927∀p,prog,stack_size,bl.
928  block_id (pi1 … bl) = 0 →
929  fetch_internal_function … (joint_globalenv p prog stack_size) bl =
930  Error ? [MSG BadFunction].
931#p #prg #stack_size #bl #Hbl whd in match fetch_internal_function;
932whd in match fetch_function; normalize nodelta @eqZb_elim
933[ >Hbl #EQ @⊥ cases(not_eq_OZ_neg one) #H @H assumption ]
934#_ normalize nodelta cases(symbol_for_block ???) [%] #id >m_return_bind
935cases bl in Hbl; * #id #prf #EQ destruct(EQ)
936change with (mk_block OZ) in match (mk_block ?);
937cut(find_funct_ptr
938    (fundef (joint_closed_internal_function p (prog_names p prg)))
939    (joint_globalenv p prg stack_size) (mk_block OZ) = None ?) [%]
940#EQ >EQ %
941qed.
942
943lemma fetch_statement_sigma_stored_pc :
944∀p_in,p_out,prog,stack_sizes,
945init,init_regs,f_lbls,f_regs,pc,f,fn,stmt.
946b_graph_transform_program_props p_in p_out stack_sizes
947  init prog init_regs f_lbls f_regs →
948block_id … (pc_block pc) ≠ -1 →
949let trans_prog ≝ b_graph_transform_program p_in p_out init prog in
950fetch_statement p_in … (joint_globalenv p_in prog stack_sizes) pc =
951return 〈f,fn,stmt〉 →
952∃data.bind_instantiate ?? (init … fn) (init_regs (pc_block pc)) = return data ∧
953match stmt with
954[ sequential step nxt ⇒
955    ∃step_block : step_block p_out (prog_names … trans_prog).
956    bind_instantiate ?? (f_step … data (point_of_pc p_in pc) step)
957                 (f_regs (pc_block pc) (point_of_pc p_in pc)) = return step_block ∧
958    ∃pc'.sigma_stored_pc p_in p_out prog stack_sizes init
959                                           init_regs f_lbls f_regs pc' = pc ∧
960    ∃fn',nxt',l1,l2.
961    fetch_statement p_out … (joint_globalenv p_out trans_prog stack_sizes) pc' =
962    if not_emptyb … (added_prologue … data) ∧
963       eq_identifier … (point_of_pc p_in pc) (joint_if_entry … fn)
964    then OK ? 〈f,fn',sequential ?? (NOOP …) nxt'〉
965    else OK ? 〈f,fn',sequential ?? ((\snd(\fst step_block)) (point_of_pc p_in pc')) nxt'〉 ∧
966    seq_list_in_code p_out (prog_names … trans_prog) (joint_if_code … fn') (point_of_pc p_out pc)
967     (map_eval … (\fst (\fst step_block)) (point_of_pc p_out pc'))
968     l1 (point_of_pc p_out pc')
969    ∧ seq_list_in_code p_out ? (joint_if_code … fn') nxt' (\snd step_block) l2 nxt
970    ∧ sigma_next p_in p_out prog stack_sizes init init_regs f_lbls f_regs (pc_block pc) nxt' = return nxt
971| final fin ⇒
972    ∃fin_block.bind_instantiate ?? (f_fin … data (point_of_pc p_in pc) fin)
973                  (f_regs (pc_block pc) (point_of_pc p_in pc)) = return fin_block ∧
974    ∃pc'.sigma_stored_pc p_in p_out prog stack_sizes init
975                                           init_regs f_lbls f_regs pc' = pc ∧
976    ∃fn'.fetch_statement p_out …
977       (joint_globalenv p_out trans_prog stack_sizes) pc'
978       = return 〈f,fn',final ?? (\snd fin_block)〉           
979| FCOND abs _ _ _ ⇒ Ⓧabs
980].
981#p_in #p_out #prog #stack_sizes #init #init_regs #f_lbls #f_regs #pc #f #fn #stmt
982#good #Hbl #EQfetch cases(fetch_statement_inv … EQfetch) #EQfn normalize nodelta
983#EQstmt
984lapply(b_graph_transform_program_fetch_internal_function … good … (pc_block pc) f fn)
985@eqZb_elim [ #EQ >EQ in Hbl; * #H @⊥ @H %] #_ normalize nodelta #H cases(H EQfn) -H
986#data * #t_fn ** #EQt_fn #Hinit * #_ #_ #_ #pp_labs #_ #fresh_labs #_ #_ #_ #H
987lapply(H … EQstmt) -H normalize nodelta #H #_ %{data} >Hinit %{(refl …)}
988-EQfetch cases stmt in EQstmt H;
989[ #step #nxt | #fin | *] normalize nodelta #EQstmt -stmt
990[ cases(added_prologue ??? data) [2: #hd #tl] normalize nodelta
991  [ @eq_identifier_elim #EQentry normalize nodelta ] ]
992* #block *
993[ whd in ⊢ (% → ?); inversion(f_regs ??) [2: #x #y #_ #_ *] #EQregs normalize nodelta
994  #EQ destruct(EQ) whd in ⊢ (% → ?); * #pre * #mid1 * #mid2 * #post *** #EQmid1
995  whd in ⊢ (% → ?); * #EQ1 #EQ2 destruct(EQ1 EQ2) whd in ⊢ (% → ?); * #nxt1
996  * #EQt_stmt change with nxt1 in ⊢ (??%? → ?); #EQ destruct(EQ) whd in ⊢ (% → ?);
997  * #EQ1 #EQ2 destruct(EQ1 EQ2) whd in EQmid1 : (??%%); destruct(EQmid1)
998|*: #Hregs #syntax_spec
999]
1000[ whd in match (point_of_pc ??) in EQstmt EQentry; <EQentry in EQstmt;
1001  cases(entry_is_cost … (pi2 ?? fn)) #nxt1 * #c #EQstmt >EQstmt #EQ destruct(EQ)
1002  % [2: % [ >(f_step_on_cost … data) in ⊢ (??%?); % ] |] %{pc}
1003  whd in match sigma_stored_pc; whd in match sigma_pc_opt; normalize nodelta
1004  @eqZb_elim [ #EQ >EQ in Hbl; * #H @⊥ @H %] #_ @eqZb_elim
1005  [ #EQ >fetch_internal_function_no_zero in EQt_fn; [2: assumption] whd in ⊢ (???% → ?);
1006    #ABS destruct(ABS) ] normalize nodelta #_
1007|*: %{block} >Hregs %{(refl …)}
1008]
1009cases(sigma_label_spec … good … Hbl EQfn EQstmt) * [2,4,6,8: #n ] * #EQpreamble
1010normalize nodelta [1,2,3,4: * #lbl * #EQnth_opt] #EQsigma_lab
1011[   whd in match (point_of_pc ??) in e0; <EQentry in e0; #e0 >e0 in EQnth_opt;
1012    whd in ⊢ (??%% → ?); #EQ destruct(EQ)
1013|5: whd in match (point_of_pc ??); <EQentry >EQsigma_lab >m_return_bind
1014    normalize nodelta >EQentry % [ cases pc #bl #off %] %{t_fn} %{nxt} %{[ ]} %{[ ]}
1015    whd in match fetch_statement; normalize nodelta >EQt_fn >m_return_bind
1016    >EQt_stmt >m_return_bind %
1017    [ % [ % [ @eq_identifier_elim [#_ %] * #H @⊥ @H % | %{(refl …) (refl …)}] | %{(refl …) (refl …)}]]
1018    whd in match sigma_next; normalize nodelta >code_block_of_block_eq
1019    >m_return_bind >EQfn >m_return_bind inversion(find ????)
1020    [ #EQfind @⊥ lapply(find_none … EQfind EQstmt) normalize nodelta
1021    >EQpreamble normalize  nodelta >EQentry >e0 normalize nodelta
1022    @eq_identifier_elim [#_ *] * #H #_ @H %]
1023    * #lbl1 #stmt1 #EQfind >m_return_bind lapply(find_predicate ?????? EQfind)
1024    inversion(stmt_get_next … stmt1) [#_ *] #nxt1 #EQnxt1 normalize nodelta
1025    inversion(preamble_length ?????????) [#_ *] #m whd in match preamble_length;
1026    normalize nodelta >code_block_of_block_eq >m_return_bind >EQfn >m_return_bind
1027    whd in match (stmt_at ????); >(find_lookup ?????? EQfind) >m_return_bind
1028    >Hinit >m_return_bind cases stmt1 in EQfind; [#j_step #succ | #fin | *]
1029    #EQfind normalize nodelta cases(bind_instantiate ???)
1030    [1,3: whd in ⊢ (??%% → ?); #EQ destruct] #bl1 >m_return_bind whd in ⊢ (??%? → ?);
1031    #EQ destruct(EQ) inversion(nth_opt ???) [1,3: #_ *] #lbl2 #EQlbl2 normalize nodelta
1032    @eq_identifier_elim [2,4: #_ *] #EQ lapply EQlbl2 destruct(EQ) #EQlbl2
1033    cases(Exists_append … (nth_opt_Exists ???? EQlbl2)) [2,4: * [2,4: *] #EQ >EQ #_ %]
1034    #H @⊥ cases(Exists_All … H (fresh_labs lbl1)) #x * #EQ destruct(EQ) ** -H #H #_
1035    @H @(code_is_in_universe … (pi2 ?? fn)) whd in match code_has_label;
1036    whd in match code_has_point; normalize nodelta
1037    cases(fetch_stmt_ok_nxt_ok … EQfn EQstmt) #stmt2 #EQ >EQ @I
1038|2,3,4: %{(pc_of_point p_out (pc_block pc) lbl)}
1039|6,7,8: %{pc}
1040]
1041whd in match sigma_stored_pc; whd in match sigma_pc_opt; normalize nodelta
1042@eqZb_elim [1,3,5,7,9,11: #H >H in Hbl; * #H1 @⊥ @H1 %] #_ normalize nodelta
1043@eqZb_elim
1044[1,3,5,7,9,11: #EQ >fetch_internal_function_no_zero in EQt_fn; [2,4,6,8,10,12: assumption]
1045  whd in ⊢ (???% → ?); #ABS destruct(ABS) ] #_ normalize nodelta
1046[1,2,3: >point_of_pc_of_point] >EQsigma_lab >m_return_bind >(pc_of_point_of_pc … pc)
1047%{(refl …)} %{t_fn} cases block in Hregs syntax_spec; -block
1048[1,2,4,5: * #pre #instr #post |*: #pre #instr ] #Hregs *
1049[1,2,3,4: #l1 * #mid1 * #mid2 * #l2 ***
1050|*: #l1 * #mid **
1051]
1052#EQmid #EQpre whd in ⊢ (% → ?);
1053[1,2,3,4: * #nxt1 *] #EQt_stmt
1054[1,2,3,4: change with nxt1 in ⊢ (??%? → ?); #EQ destruct(EQ) #EQpost %{mid2} %{l1} %{l2} %]
1055[1,3,5,7,9,10: whd in match fetch_statement; normalize nodelta >EQt_fn >m_return_bind
1056 @('bind_inversion EQpreamble) #bl1 >code_block_of_block_eq whd in ⊢ (??%? → ?);
1057 #EQ destruct(EQ) >EQfn >m_return_bind >EQstmt >m_return_bind >Hinit >m_return_bind
1058 normalize nodelta >Hregs >m_return_bind cases pre in Hregs EQpre; -pre
1059 [1,3,6,8,9,12: [3,4,6: #x #y] #_ #_ whd in match (length ??); whd in ⊢ (??%? → ?);
1060    #EQ destruct(EQ)]
1061 [1,2,5: #hd1 #tl1 ] #Hregs cases l1 in EQmid;
1062 [1,3,5,8,10,12: [4,5,6: #x #y] #_ whd in ⊢ (% → ?); [1,2,3: * #EQ1 #EQ2 destruct]
1063    * #mid * #rest ** #EQ destruct(EQ)]
1064 [1,2,3: #hd2 #tl2] whd in ⊢ (??%% → ?); #EQ destruct(EQ) whd in ⊢ (% → ?);
1065 [4,5,6: * #_ #EQ #_ >EQ >EQt_stmt [2,3: % [ %{(refl …)} %{(refl …) (refl …)} ] assumption]
1066   @eq_identifier_elim [2: #_ % [ %{(refl …)} %{(refl …) (refl …)} | assumption] ]
1067   #EQ <EQ in EQentry; * #H @⊥ @H %]
1068 * #mid' * #rest ** #EQ1 destruct(EQ1) #H1 #H2 whd in match (length ??);
1069 whd in ⊢ (??%? → ?); #EQ1 destruct(EQ1) >e0 in EQnth_opt;
1070 lapply(seq_list_in_code_length … H2) [1,2: >length_map] #EQ1 >EQ1
1071 >nth_opt_append_r [2,4,6: %] cut(|rest|-|rest|=O) [1,3,5: cases(|rest|) //]
1072 #EQ2 >EQ2 whd in ⊢ (??%% → ?); #EQ3 -EQ2 -EQ1
1073 [1,2: destruct(EQ3) >point_of_pc_of_point >EQt_stmt
1074    [2: >point_of_pc_of_point % [%{(refl …)} whd %{mid'} %{rest} % [2: assumption] % [2: assumption] %]
1075       assumption]
1076    @eq_identifier_elim [#EQ4 <EQ4 in EQentry; * #H3 @⊥ @H3 %] #_ >point_of_pc_of_point %
1077    [ %{(refl …)} whd %{mid'} %{rest} % [ %{(refl …)} assumption ] assumption | assumption ] ]
1078 destruct(EQ3) >point_of_pc_of_point >EQt_stmt %]
1079whd in match sigma_next; normalize nodelta >code_block_of_block_eq >m_return_bind
1080>EQfn >m_return_bind inversion(find ????)
1081[1,3,5,7: #EQfind @⊥ lapply(find_none … EQfind EQstmt) normalize nodelta >EQpreamble
1082  normalize nodelta cases post in Hregs EQpost;
1083  [1,3,5,7: #Hregs * #EQ1 #EQ2 destruct(EQ1 EQ2) @('bind_inversion EQpreamble)
1084    #bl' >code_block_of_block_eq whd in ⊢ (??%? → ?); #EQ destruct(EQ) >EQfn
1085    >m_return_bind >EQstmt >m_return_bind >Hinit >m_return_bind normalize nodelta
1086    >Hregs >m_return_bind cases pre in EQpre Hregs;
1087    [1,3,6,8: [3,4: #x #y] #_ #_ whd in match (length ??); whd in ⊢ (??%? → ?); #EQ destruct
1088    |2,4: #fst #remaining] *
1089    [1,2: #lab1 * #rest ** #EQ destruct(EQ) * #nxt' * #EQpc change with nxt' in ⊢ (??%? → ?);
1090      #EQ destruct(EQ) #Hrest #Hregs whd in match (length ??); whd in ⊢ (??%? → ?);
1091      #EQ destruct(EQ) whd in EQmid : (??%%); destruct(EQmid) >e0
1092      lapply(seq_list_in_code_length … Hrest) >length_map #EQ >EQ
1093      >nth_opt_append_r
1094      [2,4: >length_append whd in match (length ? [mid1]);
1095             whd in match (length ? [ ]); cases(|rest|) //]
1096      >length_append whd in match (length ? [mid1]); whd in match (length ? [ ]);
1097      cut(S (|rest|) - (|rest| + 1) = O)
1098      [1,3: cases(|rest|) // #m normalize cases m // #m1 normalize nodelta
1099            cut(m1 + 1 = S m1) [1,3: //] #EQ1 >EQ1 <minus_n_n % ]
1100      #EQ1 >EQ1 normalize nodelta @eq_identifier_elim [1,3: #_ *] * #H #_ @H %
1101    |*: #EQ1 #EQ2 destruct(EQ1 EQ2) #EQregs #_ whd in EQmid : (??%%); destruct(EQmid)
1102        >e0 normalize nodelta @eq_identifier_elim [1,3: #_ *] * #H #_ @H %
1103    ]
1104  |*: #fst #rems #Hregs * #lab1 * #rest ** #EQ destruct(EQ) * #nxt1 * #EQmid2
1105      change with nxt1 in ⊢ (??%? → ?); #EQ destruct(EQ) #EQrest @('bind_inversion EQpreamble)
1106    #bl' >code_block_of_block_eq whd in ⊢ (??%? → ?); #EQ destruct(EQ) >EQfn
1107      >m_return_bind >EQstmt >m_return_bind >Hinit >m_return_bind normalize nodelta
1108      >Hregs >m_return_bind cases pre in EQpre Hregs;
1109      [1,3,6,8: [3,4: #x #y] #_ #_ whd in ⊢ (??%? → ?); whd in match (length ??);
1110        #EQ destruct|2,4: #hd1 #tl1] *
1111      [1,2: #lab1 * #rest1 ** #EQ destruct(EQ) * #nxt1 * #EQpc
1112        change with nxt1 in ⊢ (??%? → ?); #EQ destruct(EQ) #EQrest1 #Hregs
1113        whd in match (length ??); whd in ⊢ (??%? → ?); #EQ destruct(EQ)
1114      |*: #EQ1 #EQ2 destruct(EQ1 EQ2) #Hregs #_
1115      ]
1116      whd in EQmid : (??%%); destruct(EQmid) >e0 normalize nodelta
1117      [3,4: @eq_identifier_elim [1,3: #_ *] * #H #_ @H %]
1118      lapply(seq_list_in_code_length … EQrest1) >length_map #EQ >EQ
1119      >nth_opt_append_l [2,4: >length_append whd in match (length ? (mid1::?));
1120      whd in match (length ? (mid2::rest)); cases(|rest1|) //] >append_cons
1121      >append_cons >nth_opt_append_l
1122      [2,4: >length_append >length_append whd in match (length ? [ ? ]);
1123            whd in match (length ? [ ]); cases(|rest1|) // ]
1124      >nth_opt_append_r
1125      [2,4: >length_append whd in match (length ? [ ? ]); whd in match (length ? [ ]);
1126            cases(|rest1|) // ]
1127      >length_append whd in match (length ? [ ? ]); whd in match (length ? [ ]);
1128      cut(S(|rest1|) - (|rest1|+1) = 0)
1129      [1,3: cases(|rest1|) // #m normalize cases m // #m1 normalize nodelta
1130            cut(m1 + 1 = S m1) [1,3: //] #EQ1 >EQ1 <minus_n_n % ] #EQ1 >EQ1
1131      normalize nodelta @eq_identifier_elim [1,3: #_ *] * #H #_ @H %
1132  ]
1133|*: * #lab2 * [1,4,7,10: #j_step #nxt1 |2,5,8,11: #fin1 |*: *] #EQfind
1134    lapply(find_predicate ?????? EQfind) normalize nodelta [5,6,7,8: *]
1135    cases(preamble_length ?????????) normalize nodelta [1,3,5,7: *] #n
1136    inversion(nth_opt ???) normalize nodelta [1,3,5,7: #_ *] #lab1 #EQlab1
1137    @eq_identifier_elim [2,4,6,8: #_ *] #EQ destruct(EQ)
1138    cases(Exists_append … (nth_opt_Exists ???? EQlab1))
1139    [2,4,6,8: * [2,4,6,8: *] #EQ destruct(EQ) cases post in Hregs EQpost;
1140      [1,3,5,7: #Hregs * #EQ1 #EQ2 destruct(EQ1 EQ2) #_ %] #hd1 #tl1 #Hregs *
1141      #lab3 * #rest3 ** #EQ destruct(EQ) * #nxt2 * #EQt_lab1
1142      change with nxt2 in ⊢ (??%? → ?); #EQ destruct(EQ) #EQrest3
1143      @('bind_inversion EQpreamble) #bl' >code_block_of_block_eq
1144      whd in ⊢ (??%? → ?); #EQ destruct(EQ) >EQfn >m_return_bind >EQstmt
1145      >m_return_bind >Hinit >m_return_bind normalize nodelta >Hregs
1146      >m_return_bind cases pre in Hregs EQpre;
1147      [1,3,6,8: [3,4: #x #y] #_ #_ whd in match (length ??); whd in ⊢ (??%? → ?);
1148        #EQ destruct(EQ) |2,4: #hd1 #tl1] #Hregs *
1149      [1,2: #lab4 * #rest4 ** #EQ destruct(EQ) * #nxt2 * #EQpc
1150        change with nxt2 in ⊢ (??%? → ?); #EQ destruct(EQ) #EQrest4
1151        whd in match (length ??); whd in ⊢ (??%? → ?); #EQ destruct(EQ) #_
1152      |*: #EQ1 #EQ2 destruct(EQ1 EQ2) #_ #_
1153      ]
1154      whd in EQmid : (??%%); destruct(EQmid) @⊥ lapply(fresh_labs (point_of_pc p_in pc))
1155      >e0
1156      [1,2: #H cases(append_All … H) #_ * #_ *** -H #H #_ #_ @H
1157      |*: *** #H #_ #_ @H
1158      ]
1159      @(code_is_in_universe … (pi2 ?? fn)) whd in match code_has_label;
1160      whd in match code_has_point; normalize nodelta
1161      cases(fetch_stmt_ok_nxt_ok … EQfn (find_lookup ?????? EQfind))
1162      #stmt' #EQstmt' >EQstmt' @I
1163    |*: #Hlab2 cases post in Hregs EQpost;
1164        [1,3,5,7: #Hregs * #EQ1 #EQ2 destruct(EQ1 EQ2)
1165          cases(Exists_All … Hlab2 (fresh_labs lab2)) #x * #EQ destruct(EQ) ** #H
1166          @⊥ @H @(code_is_in_universe … (pi2 ?? fn)) whd in match code_has_label;
1167          whd in match code_has_point; normalize nodelta
1168          cases(fetch_stmt_ok_nxt_ok … EQfn EQstmt) #stmt' #EQstmt' >EQstmt' @I
1169        |*: #hd1 #tl1 #Hregs * #lab3 * #rest3 ** #EQ destruct(EQ) * #nxt2 *
1170          #EQt_lab1 change with nxt2 in ⊢ (??%? → ?); #EQ destruct(EQ) #EQrest3
1171          @('bind_inversion EQpreamble) #bl' >code_block_of_block_eq
1172          whd in ⊢ (??%? → ?); #EQ destruct(EQ) >EQfn >m_return_bind >EQstmt
1173          >m_return_bind >Hinit >m_return_bind normalize nodelta >Hregs
1174          >m_return_bind cases pre in Hregs EQpre;
1175          [1,3,6,8: [3,4: #x #y] #_ #_ whd in match (length ??); whd in ⊢ (??%? → ?);
1176             #EQ destruct(EQ)
1177          |2,4: #hd1 #tl1]
1178          #Hregs *
1179          [1,2: #lab4 * #rest4 ** #EQ destruct(EQ) * #nxt2 * #EQpc
1180            change with nxt2 in ⊢ (??%? → ?); #EQ destruct(EQ) #EQrest4
1181            whd in match (length ??); whd in ⊢ (??%? → ?); #EQ destruct(EQ) #_
1182          |*: #EQ1 #EQ2 destruct(EQ1 EQ2) #_ #_
1183          ]
1184          whd in EQmid : (??%%); destruct(EQmid) cases(pp_labs) #_ #H
1185          lapply(H lab2 (point_of_pc p_in pc) lab1 ? ?)
1186          [3,6,9,12: #EQ destruct(EQ) whd in match (stmt_at ????) in EQstmt;
1187             >(find_lookup ?????? EQfind) in EQstmt; #EQ destruct(EQ) %
1188          |1,4,7,10: >e0 [3,4: whd in match (memb ???); @eqb_elim
1189            [2,4: * #H @⊥ @H %] #_ @I] >memb_append_l2 [1,3: @I]
1190             whd in match (memb ???); @if_elim [1,3: #_ %] #_
1191             whd in match (memb ???); @eqb_elim [1,3: #_ %] * #H1 @⊥ @H1 %
1192          |*:  @Exists_memb assumption
1193          ]
1194        ]
1195     ]
1196]
1197qed.
1198
1199
1200definition make_is_relation_from_beval : (beval → beval → Prop) →
1201internal_stack → internal_stack → Prop≝
1202λR,is1,is2.
1203match is1 with
1204[ empty_is ⇒ match is2 with [ empty_is ⇒ True | _ ⇒ False]
1205| one_is b ⇒ match is2 with [ one_is b' ⇒ R b b' | _ ⇒ False ]
1206| both_is b1 b2 ⇒ match is2 with [ both_is b1' b2' ⇒ R b1 b1' ∧ R b2 b2' | _ ⇒ False ]
1207].
1208
1209lemma is_push_ok : ∀Rbeval : beval → beval → Prop.
1210∀Ristack1 : internal_stack → internal_stack → Prop.
1211∀Ristack2 : internal_stack → internal_stack → Prop.
1212(∀is1,is2.Ristack1 is1 is2 → make_is_relation_from_beval Rbeval is1 is2) →
1213(∀bv1,bv2.Ristack1 empty_is empty_is → Rbeval bv1 bv2 →
1214                                   Ristack2 (one_is bv1) (one_is bv2)) →
1215(∀bv1,bv2,bv3,bv4.Rbeval bv1 bv2 → Rbeval bv3 bv4 →
1216                         Ristack2 (both_is bv3 bv1) (both_is bv4 bv2)) →
1217                         gen_preserving2 ?? gen_res_preserve …
1218                              Ristack1 Rbeval Ristack2 is_push is_push.
1219#Rbeval #Ristack1 #Ristack2 #H #H1 #H2 #is1 #is2 #bv1 #bv2 #H3 #H4
1220whd in match is_push; normalize nodelta cases is1 in H3; normalize nodelta
1221[2:#x|3: #x #y #_ @res_preserve_error_gen]
1222cases is2 normalize nodelta
1223 [1,3,5,6: [| #z #w | #z | #z #w] #H5 cases(H … H5) | #y] #H5 @m_gen_return
1224 [@H2 [assumption | @(H … H5) ] | @H1 assumption]
1225qed.
1226(*
1227lemma is_push_ok : ∀R : beval → beval → Prop.
1228               gen_preserving2 ?? gen_res_preserve …
1229                       (make_is_relation_from_beval R) R
1230                       (make_is_relation_from_beval R)
1231                       is_push is_push.
1232#R @is_push_ok_gen // #bv1 #bv2 #bv3 #bv4 #H #H1 %{H1 H}
1233qed.
1234*)
1235lemma is_pop_ok: ∀Rbeval : beval → beval → Prop.
1236∀Ristack1 : internal_stack → internal_stack → Prop.
1237∀Ristack2 : internal_stack → internal_stack → Prop.
1238(∀is1,is2.Ristack1 is1 is2 → make_is_relation_from_beval Rbeval is1 is2) →
1239Ristack2 empty_is empty_is →
1240(∀bv1,bv2.Rbeval bv1 bv2 → Ristack2 (one_is bv1) (one_is bv2)) →
1241          gen_preserving ?? gen_res_preserve …
1242                              Ristack1
1243                              (λx,y.Rbeval (\fst x) (\fst y) ∧
1244                                Ristack2 (\snd x) (\snd y)) is_pop is_pop.
1245#Rbeval #Ristack1 #Ristack2 #H #H1 #H2 #is1 #is2 whd in match is_pop; normalize nodelta
1246cases is1 normalize nodelta [#_ @res_preserve_error_gen] #x [|#y] cases is2
1247[1,3,4,5: [|#x #y||#x] #H3 cases(H … H3) | #y | #z #w] #H3 normalize nodelta
1248@m_gen_return [ % [ @(H … H3) | assumption ] | cases(H … H3) #H4 #H5 %{H5} @(H2 … H4)
1249qed.
1250
1251(*
1252lemma is_pop_ok1 : ∀R : beval → beval → Prop.
1253           gen_preserving ?? gen_res_preserve …
1254                         (make_is_relation_from_beval R)
1255                         (λx,y.R (\fst x) (\fst y) ∧
1256                               (make_is_relation_from_beval R) (\snd x) (\snd y))
1257                         is_pop is_pop.
1258#R @is_pop_ok //
1259qed.
1260
1261
1262definition make_weak_state_relation ≝
1263λp_in,p_out.λR : (beval → beval → Prop).λst1 : state p_in.λst2 : state p_out.
1264(make_is_relation_from_beval R) (istack … st1) (istack … st2).
1265*)
1266
1267
1268lemma push_ok : ∀p_in,p_out,Rbeval,Rstate1,Rstate2.
1269(∀is1,is2,st1,st2.istack ? st1 = is1 → istack ? st2 = is2 → Rstate1 st1 st2 →
1270                  make_is_relation_from_beval Rbeval is1 is2) →
1271(∀st1,st2,bv1,bv2. Rstate1 st1 st2 → Rbeval bv1 bv2 →
1272 Rstate2 (set_istack p_in (one_is bv1) st1) (set_istack p_out (one_is bv2) st2)) →
1273(∀st1,st2,bv1,bv2,bv3,bv4.Rstate1 st1 st2 → Rbeval bv1 bv2 → Rbeval bv3 bv4 →
1274 Rstate2 (set_istack p_in (both_is bv1 bv3) st1) (set_istack p_out (both_is bv2 bv4) st2)) →
1275                    gen_preserving2 ?? gen_res_preserve … Rstate1 Rbeval Rstate2
1276                                   (push p_in) (push p_out).
1277#p_in #p_out #Rbeval #Rstate1 #Rstate2 #H #H1 #H2 #st1 #st2 #bv1 #bv2 #H3 #H4
1278whd in match push; normalize nodelta
1279@(m_gen_bind_inversion … (make_is_relation_from_beval Rbeval))
1280[ @(is_push_ok Rbeval (make_is_relation_from_beval Rbeval)) //
1281  [ #bv1 #bv2 #bv3 #bv4 #H5 #H6 whd %{H6 H5}
1282  | @(H … H3) %
1283  ]
1284| * [|#x|#x1 #x2] * [1,4,7:|2,5,8: #y |*: #y1 #y2] #H5 #H6 whd in ⊢ (% → ?);
1285  [1,2,3,4,6,7,8,9: * [2: #H7 #H8] | #H7] @m_gen_return
1286  [ @(H2 … H3) assumption
1287  | cases(istack … st1) in H5; [2,3: #z [2: #w]] whd in ⊢ (??%% → ?);
1288    #EQ destruct(EQ)
1289  | @(H1 … H3) assumption
1290  ]
1291]
1292qed.
1293
1294
1295lemma pop_ok : ∀p_in,p_out,Rbeval,Rstate1,Rstate2.
1296(∀is1,is2,st1,st2.istack ? st1 = is1 → istack ? st2 = is2 → Rstate1 st1 st2 →
1297                  make_is_relation_from_beval Rbeval is1 is2) →
1298(∀st1,st2.Rstate1 st1 st2 →
1299 Rstate2 (set_istack p_in (empty_is) st1) (set_istack p_out (empty_is) st2)) →
1300(∀st1,st2,bv1,bv2. Rstate1 st1 st2 → Rbeval bv1 bv2 →
1301 Rstate2 (set_istack p_in (one_is bv1) st1) (set_istack p_out (one_is bv2) st2)) →
1302               gen_preserving ?? gen_res_preserve …
1303                 Rstate1
1304                (λx,y.Rbeval (\fst x) (\fst y) ∧
1305                 Rstate2(\snd x) (\snd y))
1306                (pop p_in) (pop p_out).
1307#p_in #p_out #Rbeval #Rstate1 #Rstate2 #H #H1 #H2 #st1 #st2 #H3
1308whd in match pop; normalize nodelta
1309@(m_gen_bind_inversion … (λx,y.Rbeval (\fst x) (\fst y) ∧
1310           (make_is_relation_from_beval Rbeval (\snd x) (\snd y)))) 
1311[ @(is_pop_ok Rbeval (make_is_relation_from_beval Rbeval)) // @(H … H3) %
1312| * #bv1 * [|#x|#x1 #x2] * #bv2 *
1313[1,4,7:|2,5,8: #y
1314|*: #y1 #y2 [1,2: #_ #_ * #_ *] cases(istack … st1) [|#z|#z #w] whd in ⊢ (??%% → ?); #EQ destruct ]
1315#_ #_ * #H4 [2,3,4,6: *| #_ | whd in ⊢ (% → ?); #H5] @m_gen_return
1316% // [ @(H1 … H3) | @(H2 … H3) assumption]
1317qed.
1318
1319(* 
1320lemma next_of_call_pc_ok : ∀P_in,P_out : sem_graph_params.
1321∀init,prog,stack_sizes,init_regs,f_lbls,f_regs.
1322b_graph_transform_program_props P_in P_out stack_sizes
1323  init prog init_regs f_lbls f_regs →
1324let trans_prog ≝ b_graph_transform_program P_in P_out init prog in
1325∀bl :Σb.block_region b =Code.block_id bl ≠ -1 →
1326∀f,fn.
1327fetch_internal_function … (joint_globalenv P_in prog stack_sizes) bl =
1328 return 〈f,fn〉 →
1329(∀id,args,dest,lbl.
1330  bind_new_P' ??
1331  (λregs1.λdata.bind_new_P' ??
1332   (λregs2.λblp.
1333     ∀lbl.∃id',args',dest'.((\snd (\fst blp)) lbl) = CALL P_out ? id' args' dest')
1334   (f_step … data lbl (CALL P_in ? id args dest)))
1335  (init ? fn)) →
1336gen_preserving ?? gen_res_preserve ????
1337 (λpc1,pc2 : Σpc.pc_block pc = bl.
1338           sigma_stored_pc P_in P_out prog stack_sizes init
1339                                           init_regs f_lbls f_regs pc2 = pc1)
1340 (λn1,n2.sigma_next P_in P_out prog stack_sizes init init_regs f_lbls f_regs bl n2 = return n1)
1341 (next_of_call_pc P_in … (joint_globalenv P_in prog stack_sizes))
1342 (next_of_call_pc P_out … (joint_globalenv P_out trans_prog stack_sizes)).
1343#p_in #p_out #init #prog #stack_sizes #init_regs #f_lbls #f_regs #good #bl #Hbl
1344#f #fn #EQfn #Hcall #pc1 #pc2 #Hpc1pc2 #lbl1 #H @('bind_inversion H) -H ** #f1 #fn1 *
1345[ * [#c| #id #args #dest | #r #lb | #seq ] #nxt | #fin | *]
1346whd in match fetch_statement; normalize nodelta >(pi2 ?? pc1) >EQfn >m_return_bind
1347#H @('bind_inversion H) -H #stmt #H lapply(opt_eq_from_res ???? H) -H
1348#EQstmt whd in ⊢ (??%% → ??%% → ?); #EQ1 #EQ2 destruct(EQ1 EQ2)
1349cases(fetch_statement_sigma_stored_pc … pc1 f1 fn1 … good …)
1350[3: >(pi2 ?? pc1) assumption
1351|4: whd in match fetch_statement; normalize nodelta >(pi2 ?? pc1) in ⊢ (??%?);
1352    >EQfn in ⊢ (??%?); >m_return_bind in ⊢ (??%?); >EQstmt in ⊢ (??%?); % |2:]
1353#data * #Hdata normalize nodelta * #st_bl * #Hst_bl * #pc' * #EQpc' * #t_fn
1354* #nxt1 * #l1 * #l2 *** #EQt_fetch #_ #_ #nxt1rel %{nxt1} % [2: <(pi2 ?? pc1) assumption]
1355whd in match next_of_call_pc; normalize nodelta <EQpc' in Hpc1pc2;
1356#H lapply(sym_eq ??? H) -H whd in match sigma_stored_pc; normalize nodelta
1357inversion(sigma_pc_opt ?????????)
1358[ #ABS @⊥ whd in match sigma_stored_pc in EQpc'; normalize nodelta in EQpc';
1359  >ABS in EQpc'; normalize nodelta #EQ <(pi2 ?? pc1) in EQfn;
1360  >fetch_internal_function_no_zero [2: <EQ %] whd in ⊢ (???% → ?); #EQ1 destruct(EQ1) ]
1361#sigma_pc' #EQsigma_pc' normalize nodelta inversion(sigma_pc_opt ?????????)
1362[ #_ normalize nodelta #EQ destruct(EQ) @⊥ lapply EQt_fetch @if_elim #_ #EQf
1363  cases(fetch_statement_inv … EQf) >fetch_internal_function_no_zero [1,3: #EQ destruct]
1364  >(pc_block_eq p_in p_out prog stack_sizes init init_regs f_lbls f_regs)
1365  [1,3: whd in match sigma_stored_pc; normalize nodelta >EQsigma_pc' %
1366  |*: >EQsigma_pc' % #EQ destruct
1367  ]
1368]
1369#pc3 #EQpc3 normalize nodelta #EQ destruct(EQ) <EQsigma_pc' in EQpc3; #H
1370lapply(sym_eq ??? H) -H #EQp lapply(sigma_stored_pc_inj … EQp) [>EQsigma_pc' % #EQ destruct]
1371#EQ destruct(EQ) >EQt_fetch @eq_identifier_elim
1372[ #EQ1 >EQ1 in EQstmt; cases(entry_is_cost … (pi2 ?? fn1)) #nxt2 * #c #ABS >ABS #EQ1 destruct(EQ1) ]
1373#_ cases(not_emptyb ??) normalize nodelta >m_return_bind normalize nodelta
1374lapply(bind_new_bind_new_instantiates … (bind_instantiates_to_instantiate … Hst_bl)
1375       (bind_new_bind_new_instantiates … (bind_instantiates_to_instantiate … Hdata)
1376        (Hcall id args dest (point_of_pc p_in pc1))))
1377#H cases(H (point_of_pc p_in pc2)) #id' * #args' * #dest' #EQ >EQ %
1378qed.
1379*)
1380
1381
1382definition JointStatusSimulation :
1383∀p_in,p_out : sem_graph_params.
1384∀ prog.∀stack_sizes.
1385∀ f_lbls, f_regs. ∀init_regs.∀init.∀st_no_pc_rel,st_rel.
1386good_state_relation p_in p_out prog stack_sizes init init_regs f_lbls f_regs
1387                    st_no_pc_rel st_rel →
1388let trans_prog ≝ b_graph_transform_program p_in p_out init prog in
1389status_rel (joint_abstract_status (mk_prog_params p_in prog stack_sizes))
1390           (joint_abstract_status (mk_prog_params p_out trans_prog stack_sizes)) ≝
1391λp_in,p_out,prog,stack_sizes,f_lbls,f_regs,init_regs,init,st_no_pc_rel,st_rel,good.
1392   mk_status_rel ??
1393    (* sem_rel ≝ *) (λs1 : (joint_abstract_status (mk_prog_params p_in ??)).
1394                     λs2 : (joint_abstract_status (mk_prog_params p_out ??)).st_rel s1 s2)
1395    (* call_rel ≝ *) 
1396       (λs1:Σs: (joint_abstract_status (mk_prog_params p_in ??)).as_classifier ? s cl_call
1397          .λs2:Σs:(joint_abstract_status (mk_prog_params p_out ??)).as_classifier ? s cl_call
1398           .pc ? s1 =
1399        sigma_stored_pc p_in p_out prog stack_sizes init init_regs f_lbls f_regs (pc ? s2)).
Note: See TracBrowser for help on using the repository browser.