source: src/joint/StatusSimulationUtils.ma @ 3259

Last change on this file since 3259 was 3259, checked in by piccolo, 6 years ago

changed ERTL semantics:
1) added manipulation of stack pointer directly in the semantics
2) added values of Callee Saved in frames

File size: 70.4 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 … (joint_globalenv P_in prog stack_sizes) … n (joint_if_params … fn1)
422   arg f1 st1_pre = return st1' →
423  st_rel st1 st2 → 
424  ∀t_fn1.
425  fetch_internal_function … (joint_globalenv P_out trans_prog stack_sizes) bl =
426  return 〈f1,t_fn1〉 →
427  bind_new_P' ??
428    (λregs1.λdata.
429     bind_new_P' ??
430      (λregs2.λblp.
431        ∀pc',t_fn,id',arg',dest',nxt1.
432          sigma_stored_pc P_in P_out prog stack_sizes init init_regs
433           f_lbls f_regs pc' = (pc … st1) →
434          fetch_statement P_out … (joint_globalenv P_out trans_prog stack_sizes) pc'
435          = return 〈f,t_fn,
436                    sequential P_out ? ((\snd (\fst blp)) (point_of_pc P_out pc')) nxt1〉→
437          ((\snd (\fst blp)) (point_of_pc P_out pc')) = (CALL P_out ? id' arg' dest') → 
438       ∃st2_pre_call.
439        repeat_eval_seq_no_pc ? (mk_prog_params P_out trans_prog stack_sizes) f
440          (map_eval ?? (\fst (\fst blp)) (point_of_pc P_out pc')) (st_no_pc ? st2) = return st2_pre_call ∧
441       block_of_call P_out … (joint_globalenv P_out trans_prog stack_sizes) id'
442        st2_pre_call = return bl ∧
443       ∃st2_pre.
444        save_frame … P_out (kind_of_call P_out id') dest'
445         (mk_state_pc ? st2_pre_call pc' (last_pop … st2)) = return st2_pre ∧
446       ∃st2_after_call.
447         setup_call ?? P_out … (joint_globalenv P_out trans_prog stack_sizes) … n
448          (joint_if_params … t_fn1) arg' f1 st2_pre
449          = return st2_after_call ∧
450       bind_new_P' ??
451         (λregs11.λdata1.
452          ∃st2'.
453           repeat_eval_seq_no_pc ? (mk_prog_params P_out trans_prog stack_sizes) f1
454           (added_prologue … data1) (increment_stack_usage P_out n st2_after_call) =
455           return st2' ∧
456           st_no_pc_rel (pc_of_point P_in bl (joint_if_entry … fn1))
457            (increment_stack_usage P_in n st1') st2'               
458         ) (init ? fn1)
459     )
460     (f_step … data (point_of_pc P_in (pc … st1)) (CALL P_in ? id arg dest))
461    ) (init ? fn).
462   
463definition goto_commutation_statement ≝
464λP_in,P_out : sem_graph_params.
465  λprog : joint_program P_in.λstack_sizes : ident → option ℕ.
466  λinit : (∀globals.joint_closed_internal_function P_in globals
467         →bound_b_graph_translate_data P_in P_out globals).
468  λinit_regs : block → list register. λf_lbls : lbl_funct_type.
469  λf_regs : regs_funct_type.λst_no_pc_rel : joint_state_relation P_in P_out.
470  λst_rel : joint_state_pc_relation P_in P_out.
471let trans_prog ≝ b_graph_transform_program P_in P_out init prog in
472  ∀st1 : joint_abstract_status (mk_prog_params P_in prog stack_sizes) .
473  ∀st2 : joint_abstract_status (mk_prog_params P_out trans_prog stack_sizes).
474  ∀f,fn,lbl.
475  block_id … (pc_block (pc … st1)) ≠ -1 →
476  fetch_statement P_in … (joint_globalenv P_in prog stack_sizes) (pc … st1) =
477  return 〈f, fn,  final P_in ? (GOTO ? lbl)〉 →
478  st_rel st1 st2 →
479  ∀t_fn.
480  fetch_internal_function …
481     (joint_globalenv P_out trans_prog stack_sizes) (pc_block (pc … st2)) =
482  return 〈f,t_fn〉 →
483  bind_new_P' ??
484     (λregs1.λdata.
485      bind_new_P' ??
486      (λregs2.λblp.
487        ∃st_fin. repeat_eval_seq_no_pc ? (mk_prog_params P_out trans_prog stack_sizes) f
488              (\fst blp)  (st_no_pc … st2)= return st_fin ∧
489        \snd blp = GOTO ? lbl ∧
490        st_no_pc_rel (pc_of_point P_in (pc_block (pc … st1)) lbl)
491           (st_no_pc … st1) (st_fin)
492      ) (f_fin … data (point_of_pc P_in (pc … st1)) (GOTO ? lbl))
493     ) (init ? fn).
494     
495definition tailcall_commutation_statement ≝
496λP_in,P_out : sem_graph_params.
497  λprog : joint_program P_in.λstack_sizes : ident → option ℕ.
498  λinit : (∀globals.joint_closed_internal_function P_in globals
499         →bound_b_graph_translate_data P_in P_out globals).
500  λinit_regs : block → list register. λf_lbls : lbl_funct_type.
501  λf_regs : regs_funct_type.λst_no_pc_rel : joint_state_relation P_in P_out.
502  λst_rel : joint_state_pc_relation P_in P_out.
503  let trans_prog ≝ b_graph_transform_program P_in P_out init prog in
504  ∀st1 : joint_abstract_status (mk_prog_params P_in prog stack_sizes) .
505  ∀st2 : joint_abstract_status (mk_prog_params P_out trans_prog stack_sizes).
506  ∀f,fn,has_tail,id,arg.
507  block_id … (pc_block (pc … st1)) ≠ -1 →
508  fetch_statement P_in … (joint_globalenv P_in prog stack_sizes) (pc … st1) =
509  return 〈f, fn,  final P_in ? (TAILCALL P_in has_tail id arg)〉 →
510  ∀bl.
511   block_of_call P_in … (joint_globalenv P_in prog stack_sizes) id (st_no_pc … st1)
512      = return bl →
513   ∀f1,fn1.
514   fetch_internal_function …
515    (joint_globalenv P_in prog stack_sizes) bl =  return 〈f1,fn1〉 →
516   ∀ssize_f.stack_sizes f = return ssize_f →
517   ∀ssize_f1.stack_sizes f1 = return ssize_f1 →   
518   ∀st1'.
519    setup_call ?? P_in … (joint_globalenv P_in prog stack_sizes) … ssize_f1
520     (joint_if_params … fn1) arg f1
521     (decrement_stack_usage P_in ssize_f (st_no_pc … st1)) = return st1' →
522   st_rel st1 st2 → 
523   ∀t_fn1.
524   fetch_internal_function … (joint_globalenv P_out trans_prog stack_sizes) bl =
525   return 〈f1,t_fn1〉 →
526   bind_new_P' ??
527    (λregs1.λdata.
528     bind_new_P' ??
529      (λregs2.λblp.
530       ∃ has_tail',id',arg'.
531       (\snd blp) = TAILCALL P_out has_tail' id' arg' ∧
532       ∃st2_pre_call.
533        repeat_eval_seq_no_pc ? (mk_prog_params P_out trans_prog stack_sizes) f
534         (\fst blp) (st_no_pc ? st2) = return st2_pre_call ∧
535        block_of_call P_out … (joint_globalenv P_out trans_prog stack_sizes) id'
536         st2_pre_call = return bl ∧
537       ∃st2_after.
538        setup_call ?? P_out … (joint_globalenv P_out trans_prog stack_sizes) …
539         ssize_f1 (joint_if_params … t_fn1) arg' f1
540         (decrement_stack_usage P_out ssize_f st2_pre_call) = return st2_after ∧
541       bind_new_P' ??
542         (λregs11.λdata1.
543          ∃st2'.
544           repeat_eval_seq_no_pc ? (mk_prog_params P_out trans_prog stack_sizes) f1
545           (added_prologue … data1) (increment_stack_usage P_out ssize_f1 st2_after) =
546           return st2' ∧
547           st_no_pc_rel (pc_of_point P_in bl (joint_if_entry … fn1))
548            (increment_stack_usage P_in ssize_f1 st1') st2'               
549         ) (init ? fn1)
550     ) (f_fin … data (point_of_pc P_in (pc … st1)) (TAILCALL P_in has_tail id arg))
551   ) (init ? fn).
552
553record good_state_relation (P_in : sem_graph_params)
554   (P_out : sem_graph_params) (prog : joint_program P_in)
555   (stack_sizes : ident → option ℕ)
556   (init : ∀globals.joint_closed_internal_function P_in globals
557         →bound_b_graph_translate_data P_in P_out globals)
558   (init_regs : block → list register) (f_lbls : lbl_funct_type)
559   (f_regs : regs_funct_type)
560   (st_no_pc_rel : joint_state_relation P_in P_out)
561   (st_rel : joint_state_pc_relation P_in P_out) : Prop ≝
562{ good_translation :> b_graph_transform_program_props P_in P_out stack_sizes init
563                     prog init_regs f_lbls f_regs
564; fetch_ok_sigma_state_ok :
565   ∀st1,st2,f,fn. st_rel st1 st2 →
566    fetch_internal_function …
567     (joint_globalenv P_in prog stack_sizes) (pc_block (pc … st1)) 
568     = return 〈f,fn〉 →
569     st_no_pc_rel (pc … st1) (st_no_pc … st1) (st_no_pc … st2)
570; fetch_ok_pc_ok :
571  ∀st1,st2,f,fn.st_rel st1 st2 →
572   fetch_internal_function …
573     (joint_globalenv P_in prog stack_sizes) (pc_block (pc … st1)) 
574     = return 〈f,fn〉 →
575   pc … st1 = pc … st2
576; fetch_ok_sigma_last_pop_ok :
577  ∀st1,st2,f,fn.st_rel st1 st2 →
578   fetch_internal_function …
579     (joint_globalenv P_in prog stack_sizes) (pc_block (pc … st1)) 
580     = return 〈f,fn〉 →
581   (last_pop … st1) = sigma_stored_pc P_in P_out prog stack_sizes init init_regs
582                       f_lbls f_regs (last_pop … st2)
583; st_rel_def :
584  ∀st1,st2,pc,lp1,lp2,f,fn.
585  fetch_internal_function …
586     (joint_globalenv P_in prog stack_sizes) (pc_block pc) = return 〈f,fn〉 →
587   st_no_pc_rel pc st1 st2 →
588   lp1 = sigma_stored_pc P_in P_out prog stack_sizes init init_regs
589          f_lbls f_regs lp2 →
590  st_rel (mk_state_pc ? st1 pc lp1) (mk_state_pc ? st2 pc lp2)
591; pre_main_ok : pre_main_commutation_statement P_in P_out prog stack_sizes init
592                    init_regs f_lbls f_regs st_no_pc_rel st_rel
593; pre_main_no_return :
594    ∀f,fn,pc. block_id (pc_block pc) = -1 →
595    fetch_statement P_in … (joint_globalenv P_in prog stack_sizes) pc =
596      return 〈f,fn,final P_in ? (RETURN …)〉 → False
597; cond_commutation : cond_commutation_statement P_in P_out prog stack_sizes init
598                    init_regs f_lbls f_regs st_no_pc_rel st_rel
599; seq_commutation : seq_commutation_statement P_in P_out prog stack_sizes init
600                    init_regs f_lbls f_regs st_no_pc_rel st_rel
601;  call_is_call :∀f,fn,bl.
602  fetch_internal_function …
603     (joint_globalenv P_in prog stack_sizes) bl = return 〈f,fn〉 →
604   ∀id,args,dest,lbl.
605    bind_new_P' ??
606     (λregs1.λdata.bind_new_P' ??
607      (λregs2.λblp.
608        ∀lbl.∃id',args',dest'.((\snd (\fst blp)) lbl) = CALL P_out ? id' args' dest')
609      (f_step … data lbl (CALL P_in ? id args dest)))
610     (init ? fn)
611; cost_commutation :
612  let trans_prog ≝ b_graph_transform_program P_in P_out init prog in
613  ∀st1,st2,pc.∀f,fn,c,nxt.
614  block_id … (pc_block pc) ≠ -1 →
615  st_no_pc_rel pc st1 st2 →
616  fetch_statement P_in … (joint_globalenv P_in prog stack_sizes) pc =
617  return 〈f, fn,  sequential ?? (COST_LABEL ?? c) nxt〉 → 
618  st_no_pc_rel (pc_of_point P_in (pc_block pc) nxt) st1 st2
619; return_commutation : return_commutation_statement P_in P_out prog stack_sizes init
620                    init_regs f_lbls f_regs st_no_pc_rel st_rel
621; call_commutation : call_commutation_statement P_in P_out prog stack_sizes init
622                    init_regs f_lbls f_regs st_no_pc_rel st_rel
623; goto_commutation : goto_commutation_statement P_in P_out prog stack_sizes init
624                    init_regs f_lbls f_regs st_no_pc_rel st_rel
625; tailcall_commutation : tailcall_commutation_statement P_in P_out prog stack_sizes
626                   init init_regs f_lbls f_regs st_no_pc_rel st_rel
627; as_result_ok :
628  let trans_prog ≝ b_graph_transform_program P_in P_out init prog in   
629  ∀st1 : joint_abstract_status (mk_prog_params P_in prog stack_sizes) .
630  ∀st2 : joint_abstract_status (mk_prog_params P_out trans_prog stack_sizes).
631  st_rel st1 st2 →
632  as_result … st1 = as_result … st2
633; as_label_premain_ok :
634  let trans_prog ≝ b_graph_transform_program P_in P_out init prog in   
635  ∀st1 : joint_abstract_status (mk_prog_params P_in prog stack_sizes) .
636  ∀st2 : joint_abstract_status (mk_prog_params P_out trans_prog stack_sizes).
637  block_id … (pc_block (pc … st1)) = -1 →
638  st_rel st1 st2 → as_label … st1 = as_label … st2
639}.
640
641record good_init_relation (P_in : sem_graph_params) (P_out : sem_graph_params)
642(prog : joint_program P_in) (stack_sizes : ident → option ℕ)
643(init : ∀globals.joint_closed_internal_function P_in globals
644         →bound_b_graph_translate_data P_in P_out globals)
645(st_no_pc_rel : joint_state_relation P_in P_out) : Prop ≝
646{ good_empty :
647   ∀m0.init_mem … (λx.x) prog = return m0 →
648   let 〈m,spb〉 as H ≝ alloc … m0 0 external_ram_size in
649   let trans_prog ≝ b_graph_transform_program P_in P_out init prog in
650   let globals_size ≝ globals_stacksize … prog in
651   let globals_size' ≝ globals_stacksize … trans_prog in
652   let spp ≝ mk_pointer spb (mk_offset (bitvector_of_Z 16 (-S (globals_size)))) in
653   let spp' ≝ mk_pointer spb (mk_offset (bitvector_of_Z 16 (-S (globals_size')))) in
654   ∀prf,prf'.
655   let st ≝ mk_state P_in (Some ? (empty_framesT …)) empty_is
656                (BBbit false) (empty_regsT … «spp,prf») m 0 in
657   let st' ≝ mk_state P_out (Some ? (empty_framesT …)) empty_is
658                (BBbit false) (empty_regsT … «spp',prf'») m 0 in
659   st_no_pc_rel init_pc (set_sp … «spp,prf» st) (set_sp … «spp',prf'» st')
660; good_init_cost_label :
661  let trans_prog ≝ b_graph_transform_program P_in P_out init prog in
662  match fetch_statement P_in … (joint_globalenv P_in prog stack_sizes) init_pc with
663  [ OK x ⇒ match cost_label_of_stmt … (\snd x) with
664           [ Some c ⇒ ∃y.
665                     fetch_statement P_out …
666                      (joint_globalenv P_out trans_prog stack_sizes) init_pc =
667                     OK ? y ∧
668                     cost_label_of_stmt … (\snd y) = Some ? c
669           | None ⇒ (∃e'.
670                     fetch_statement P_out …
671                      (joint_globalenv P_out trans_prog stack_sizes) init_pc =
672                     Error ? e') ∨
673                    (∃x.
674                     fetch_statement P_out …
675                      (joint_globalenv P_out trans_prog stack_sizes) init_pc =
676                       OK ? x ∧ cost_label_of_stmt … (\snd x) = None ?)
677           ]
678  | Error e ⇒
679    (∃e'.
680     fetch_statement P_out … (joint_globalenv P_out trans_prog stack_sizes) init_pc =
681      Error ? e') ∨
682    (∃x.
683      fetch_statement P_out … (joint_globalenv P_out trans_prog stack_sizes) init_pc =
684      OK ? x ∧ cost_label_of_stmt … (\snd x) = None ?)
685  ]
686}.
687
688
689lemma code_block_of_block_eq : ∀bl : Σb.block_region b = Code.
690code_block_of_block bl = return bl.
691* #bl #prf whd in match code_block_of_block; normalize nodelta @match_reg_elim
692[ >prf in ⊢ (% → ?); #ABS destruct(ABS)] #prf1 %
693qed.
694
695(*TO BE MOVED*)
696lemma Exists_append1 : ∀A.∀l1,l2 : list A.∀P.Exists A P l1 → Exists A P (l1@l2).
697#A #l1 elim l1 [#l2 #P *] #hd #tl #IH *
698[#P normalize // ] #hd1 #tl1 #P normalize * [#H % assumption | #H %2 @IH assumption]
699qed.
700
701lemma Exists_append2 : ∀A.∀l1,l2 : list A.∀P.Exists A P l2 → Exists A P (l1@l2).
702#A #l1 #l2 lapply l1 -l1 elim l2 [#l1 #a *] #hd #tl #IH *
703[#a normalize // ] #hd1 #tl1 #a normalize *
704[ #H %2 >append_cons @Exists_append1 elim tl1 [% assumption] #hd2 #tl2 #H1 normalize %2 //
705| #H %2 >append_cons @IH assumption]
706qed.
707
708lemma seq_list_in_code_length : ∀p : params. ∀globals : list ident.
709∀code : codeT p globals.∀src : code_point p.∀l1,l2,dst.
710seq_list_in_code p globals code src l1 l2 dst → |l1| = |l2|.
711#p #globals #code #src #l1 lapply src -src elim l1
712[ #src * [ #dst #_ %] #hd #tl #dst whd in ⊢ (% → ?); * #EQ destruct]
713#hd #tl #IH #src * [ #dst whd in ⊢ (% → ?); * #mid * #rest ** #EQ destruct]
714#hd1 #tl1 #dst whd in ⊢ (% → ?); * #mid * #rest ** #EQ destruct * #nxt1 * #EQstnt
715#EQsucc #H whd in ⊢ (??%%); @eq_f @(IH … H)
716qed.
717
718lemma fetch_stmt_ok_succ_ok : ∀p : sem_graph_params.
719∀prog : joint_program p.∀stack_size,f,fn,stmt,pc,pc',lbl.
720In ? (stmt_labels p ? stmt) lbl→
721fetch_statement p … (joint_globalenv p prog stack_size) pc = return 〈f,fn,stmt〉 →
722pc' = pc_of_point p (pc_block pc) lbl →
723∃stmt'.fetch_statement p … (joint_globalenv p prog stack_size) pc' = return 〈f,fn,stmt'〉.
724#p #prog #stack_size #f #fn #stmt #pc #pc' #lbl #Hlbl #EQfetch
725cases(fetch_statement_inv … EQfetch) #EQfn normalize nodelta #EQstmt
726#EQ destruct(EQ) lapply(code_is_closed … (pi2 ?? fn) … EQstmt) *
727cases(decidable_In ? (stmt_explicit_labels … stmt) lbl ?)
728[3: * cases lbl #x #y cases(decidable_eq_pos … x y)
729    [#EQ destruct % % | * #H %2 % #H1 @H destruct %]
730| whd in ⊢ (% → ?); #H1 #H2 cases(Exists_All … H1 H2) #lbl1 * #EQ destruct
731  whd in match code_has_label; whd in match code_has_point; normalize nodelta
732  inversion(stmt_at ????) [#_ *] #stmt' #EQstmt' #_ #_ %{stmt'}
733  whd in match fetch_statement; normalize nodelta >EQfn >m_return_bind
734  >point_of_pc_of_point >EQstmt' %
735| #H lapply(In_all ??? H) -H cases(Exists_append … Hlbl)
736  [ cases stmt [ #step #nxt | #fin | *] whd in match stmt_implicit_label;
737    normalize nodelta [2: *] * [2: *] #EQ destruct(EQ) #_ #_
738    whd in match stmt_forall_succ; whd in match code_has_point; normalize nodelta
739    inversion(stmt_at ????) [#_ *] #stmt' #EQstmt' #_ %{stmt'}
740    whd in match fetch_statement; normalize nodelta >EQfn >m_return_bind
741    >point_of_pc_of_point >EQstmt' %
742  | #H1 #H2 cases(Exists_All … H1 H2) #x * #EQ destruct * #H @⊥ @H %
743  ]
744]
745qed.
746
747
748lemma fetch_stmt_ok_nxt_ok : ∀p : sem_graph_params.
749∀prog : joint_program p.∀stack_size,f,fn,stmt,bl,pt,nxt.
750fetch_internal_function … (joint_globalenv p prog stack_size) bl =
751return 〈f,fn〉→
752stmt_at p … (joint_if_code … fn) pt = return sequential p ? stmt nxt →
753∃stmt'.
754stmt_at p … (joint_if_code … fn) nxt = return stmt'.
755#p #prog #stack_size #f #fn #stmt #bl #pt #nxt #EQfn #EQstmt
756cases(fetch_stmt_ok_succ_ok …
757       prog stack_size f fn (sequential p … stmt nxt) (pc_of_point p bl pt)
758       (pc_of_point p bl nxt) nxt ???)
759[ #stmt' #H cases(fetch_statement_inv … H) -H #_ >point_of_pc_of_point normalize nodelta
760  #EQstmt' %{stmt'} assumption
761| whd in match stmt_labels; normalize nodelta % %
762| whd in match fetch_statement; normalize nodelta >EQfn >m_return_bind
763  >point_of_pc_of_point >EQstmt %
764| %
765]
766qed.
767
768
769lemma sigma_label_spec : ∀p_in,p_out,prog,stack_size,init,init_regs.
770∀f_lbls : lbl_funct_type.∀f_regs.
771b_graph_transform_program_props p_in p_out stack_size init prog init_regs f_lbls f_regs →
772∀id,fn.
773∀bl:Σb.block_region b = Code. ∀pt,stmt.
774block_id … bl ≠ -1 →
775fetch_internal_function …
776   (joint_globalenv p_in prog stack_size) bl = return 〈id,fn〉 →
777stmt_at p_in … (joint_if_code … fn) pt = return stmt → 
778∃n.preamble_length … prog stack_size init init_regs f_regs bl pt = return n ∧
779match n with
780[ O ⇒ sigma_label p_in p_out prog stack_size init init_regs f_lbls f_regs bl pt = return pt
781| S m ⇒ ∃lbl.nth_opt ? m (f_lbls bl pt) = return lbl ∧
782    sigma_label p_in p_out prog stack_size init init_regs f_lbls f_regs bl lbl = return pt     
783].
784#p_in #p_out #prog #stack_size #init #init_regs #f_lbls #f_regs #good #id #fn
785#bl #pt #stmt * #Hbl #EQfn #EQstmt   
786lapply(b_graph_transform_program_fetch_internal_function … good … bl id fn)
787@eqZb_elim [ #EQ >EQ in Hbl; #H @⊥ @H %] #_ normalize nodelta #H cases(H EQfn) -H
788#data * #t_fn ** #EQt_fn #Hinit * #_ #_ #_ #pp_labs #_ #fresh_lab #_ #_ #_ #H
789lapply(H … EQstmt) -H normalize nodelta cases stmt in EQstmt; -stmt
790[ #j_step #nxt | #fin | * ] #EQstmt normalize nodelta **
791[ * #pre_instr #instr #post_instr | #pre_instr #instr] *
792[ cases(added_prologue ????) [2: #hd_instr #tl_instrs ] normalize nodelta
793 [ @eq_identifier_elim #EQentry normalize nodelta
794   [ whd in ⊢ (% → ?); inversion (f_regs ??) [2: #x #y #_ #_ *] #EQregs normalize nodelta
795     whd in ⊢ (???% → ?); #EQ destruct(EQ)
796   |*: #Hregs
797   ]
798 | #Hregs
799 ]
800| #Hregs
801]
802#syntax_spec
803[4: cases(added_prologue ????) [2: #hd_instr #tl_instrs ] normalize nodelta ] #_
804[1,2,4,5: %{(|pre_instr|)} | %{O}]
805cut(? : Prop)
806[3,6,9,12,15: #EQn %{EQn} whd in EQn; normalize nodelta
807 [1,2,3,4: cases pre_instr in Hregs syntax_spec EQn; [2,4,6,8: #hd #tl] #Hregs #syntax_spec
808           whd in match (length ??); #EQn whd in match (length ??); normalize nodelta]
809 [5,6,7,8,9: whd in match sigma_label; normalize nodelta >code_block_of_block_eq
810     >m_return_bind >EQfn >m_return_bind inversion(find ????)
811     [1,3,5,7,9: #EQfind @⊥ lapply(find_none ?????? EQfind EQstmt) >EQn normalize nodelta
812     @eq_identifier_elim [1,3,5,7,9: #_ *] * #H #_ @H % ] * #lbl1 #stmt1 #EQfind
813     >m_return_bind @eq_f lapply(find_predicate ?????? EQfind) whd in match preamble_length;
814     normalize nodelta >code_block_of_block_eq >m_return_bind >EQfn >m_return_bind
815     whd in match (stmt_at ????); >(find_lookup ?????? EQfind) >m_return_bind >Hinit
816     >m_return_bind cases stmt1 in EQfind; -stmt1
817     [1,4,7,10,13: #j_step1 #nxt1 |2,5,8,11,14: #fin1 |*: *] #EQfind normalize nodelta
818     cases(bind_instantiate ????) [1,3,5,7,9,11,13,15,17,19: *]
819     [1,2,3,4,5: ** #pre_instr1 #instr1 #post_instr1 |*: * #pre_instr1 #instr1 ]
820     >m_return_bind cases pre_instr1 -pre_instr1 [2,4,6,8,10,12,14,16,18,20: #hd #tl]
821     whd in match (length ??); normalize nodelta
822     [11,12,13,14,15,16,17,18,19,20: @eq_identifier_elim [2,4,6,8,10,12,14,16,18,20: #_ *]
823     #EQ #_ >EQ %]
824     whd in match (nth_opt ???); inversion(nth_opt ???) [1,3,5,7,9,11,13,15,17,19: #_ *]
825     #lbl2 #EQlbl2 normalize nodelta @eq_identifier_elim [2,4,6,8,10,12,14,16,18,20: #_ *]
826     #EQ lapply EQlbl2 destruct(EQ) #EQlbl2 @⊥
827     cases(Exists_All … (nth_opt_Exists … EQlbl2) (fresh_lab lbl1)) #x * #EQ destruct(EQ)
828     ** #H #_ @H  @(code_is_in_universe … (pi2 ?? fn)) whd in match code_has_label;
829     whd in match code_has_point; normalize nodelta >EQstmt @I
830 |*: cases syntax_spec -syntax_spec #pre * #mid1 [3,4: * #mid2 * #post] ** [1,2: *]
831     cases pre -pre [1,3,5,7: #_ * #x * #y ** #ABS destruct(ABS)] #hd1 #tl1 whd in ⊢ (??%% → ?);
832     #EQ destruct(EQ) -EQ #pre_spec whd in ⊢ (% → ?);
833     [1,2: * #nxt1 * #EQt_stmt change with nxt1 in ⊢ (??%? → ?); #EQ destruct(EQ) #post_spec
834     |*:   #EQt_stmt
835     ]
836     %{mid1} cut(? : Prop)
837     [3,6,9,12: #EQnth_opt %{EQnth_opt} whd in match sigma_label; normalize nodelta
838       >code_block_of_block_eq >m_return_bind >EQfn >m_return_bind inversion(find ????)
839       [1,3,5,7: #EQfind @⊥ lapply(find_none ?????? EQfind EQstmt) >EQn normalize nodelta
840         whd in match (nth_opt ???); >EQnth_opt normalize nodelta @eq_identifier_elim
841         [1,3,5,7: #_ *] * #H #_ @H % ] * #lbl1 #stmt1 #EQfind >m_return_bind @eq_f
842       lapply(find_predicate ?????? EQfind) whd in match preamble_length;
843       normalize nodelta >code_block_of_block_eq >m_return_bind >EQfn >m_return_bind
844       whd in match (stmt_at ????); >(find_lookup ?????? EQfind) >m_return_bind >Hinit
845       >m_return_bind cases stmt1 in EQfind; -stmt1
846       [1,4,7,10: #j_step1 #nxt1 |2,5,8,11: #fin1 |*: *] #EQfind normalize nodelta
847       cases(bind_instantiate ????) [1,3,5,7,9,11,13,15: *]
848       [1,2,3,4: ** #pre_instr1 #instr1 #post_instr1 |*: * #pre_instr1 #instr1]
849       >m_return_bind cases pre_instr1 -pre_instr1 [2,4,6,8,10,12,14,16: #hd1 #tl1]
850       whd in match (length ??); normalize nodelta whd in match (nth_opt ???);
851       [1,2,3,4,5,6,7,8: inversion(nth_opt ???) [1,3,5,7,9,11,13,15: #_ *] #lbl2
852         #EQlbl2 normalize nodelta @eq_identifier_elim [2,4,6,8,10,12,14,16: #_ *]
853         #EQ lapply EQlbl2 destruct(EQ) #EQlbl2 #_  @(proj2 … pp_labs ?? lbl2)
854         @Exists_memb [1,3,5,7,9,11,13,15: @(nth_opt_Exists … EQlbl2)]
855         >e0 @Exists_append2 % %
856       |*: @eq_identifier_elim [2,4,6,8,10,12,14,16: #_ *] #EQ destruct(EQ) @⊥
857         lapply(fresh_lab hd1) >e0 #H cases(append_All … H) #_ * -H ** #H #_ #_ @H
858         @(code_is_in_universe … (pi2 ?? fn)) whd in match code_has_label;
859         whd in match code_has_point; normalize nodelta whd in match (stmt_at ????);
860         >(find_lookup ?????? EQfind) @I
861       ]   
862     |2,5,8,11: >e0 cases pre_spec #fst * #rest ** #EQ destruct(EQ)
863                whd in ⊢ (% → ?); * #nxt1 * #_ change with nxt1 in ⊢ (??%? → ?);
864                #EQ destruct(EQ) #H lapply(seq_list_in_code_length … H)
865                [1,2: >length_map] -H #H >H >nth_opt_append_r cases(|rest|)
866                try % try( #n %) #n <minus_n_n %
867     |*:
868     ]
869  ]
870 |2,5,8,11,14: whd in match preamble_length; normalize nodelta >code_block_of_block_eq
871   >m_return_bind >EQfn >m_return_bind >EQstmt >m_return_bind >Hinit >m_return_bind
872   normalize nodelta
873   [1,2,3,4: >Hregs %
874   | >EQregs <EQentry in EQstmt; cases(entry_is_cost … (pi2 ?? fn)) #succ * #c
875     #EQstmt >EQstmt whd in ⊢ (???% → ?); #EQ destruct(EQ) >(f_step_on_cost … data)
876     whd in match (bind_instantiate ????); %
877   ]
878 |*:
879 ]
880qed.
881
882lemma pc_block_eq : ∀p_in,p_out,prog,stack_sizes,init,init_regs,f_lbls,
883f_regs,pc.
884sigma_pc_opt p_in p_out prog stack_sizes init
885   init_regs f_lbls f_regs pc ≠ None ? →
886 pc_block … pc =
887 pc_block … (sigma_stored_pc p_in p_out prog stack_sizes init
888                                           init_regs f_lbls f_regs pc).
889#p_in #p_out #prog #stack_sizes #init #init_regs #f_lbls #f_regs #pc
890whd in match sigma_stored_pc; normalize nodelta
891inversion(sigma_pc_opt ?????????) [ #_ * #ABS @⊥ @ABS %] #pc1
892whd in match sigma_pc_opt; normalize nodelta @if_elim
893[ #_ whd in ⊢ (??%? → ?); #EQ destruct(EQ) #_ %] #_
894#H @('bind_inversion H) -H #lbl #_ whd in ⊢ (??%? → ?); #EQ destruct #_ %
895qed.
896
897definition stmt_get_next : ∀p,globals.joint_statement p globals → option (succ p) ≝
898λp,globals,stmt.
899match stmt with
900[sequential stmt nxt ⇒ Some ? nxt
901| _ ⇒ None ?
902].
903
904
905definition sigma_next : ∀p_in,p_out : sem_graph_params.
906joint_program p_in → (ident → option ℕ) →
907(∀globals.joint_closed_internal_function p_in globals
908         →bound_b_graph_translate_data p_in p_out globals) →
909(block → list register) → lbl_funct_type → regs_funct_type →
910block → label → option label ≝
911λp_in,p_out,prog,stack_size,init,init_regs,f_lbls,f_regs,bl,searched.
912! bl ← code_block_of_block bl ;
913! 〈id,fn〉 ← res_to_opt … (fetch_internal_function …
914                           (joint_globalenv p_in prog stack_size) bl);
915! 〈res,s〉 ←
916 find ?? (joint_if_code ?? fn)
917  (λlbl.λstmt.match stmt_get_next … stmt with
918    [ None ⇒ false
919    | Some nxt ⇒
920       match preamble_length … prog stack_size init init_regs f_regs bl lbl with
921        [ None ⇒ false
922        | Some n ⇒ match nth_opt ? n ((f_lbls bl lbl) @ [nxt]) with
923                         [ None ⇒ false
924                         | Some x ⇒ eq_identifier … searched x
925                         ]
926        ]
927    ]);
928stmt_get_next … s.
929
930lemma fetch_internal_function_no_zero :
931∀p,prog,stack_size,bl.
932  block_id (pi1 … bl) = 0 →
933  fetch_internal_function … (joint_globalenv p prog stack_size) bl =
934  Error ? [MSG BadFunction].
935#p #prg #stack_size #bl #Hbl whd in match fetch_internal_function;
936whd in match fetch_function; normalize nodelta @eqZb_elim
937[ >Hbl #EQ @⊥ cases(not_eq_OZ_neg one) #H @H assumption ]
938#_ normalize nodelta cases(symbol_for_block ???) [%] #id >m_return_bind
939cases bl in Hbl; * #id #prf #EQ destruct(EQ)
940change with (mk_block OZ) in match (mk_block ?);
941cut(find_funct_ptr
942    (fundef (joint_closed_internal_function p (prog_names p prg)))
943    (joint_globalenv p prg stack_size) (mk_block OZ) = None ?) [%]
944#EQ >EQ %
945qed.
946
947lemma fetch_statement_sigma_stored_pc :
948∀p_in,p_out,prog,stack_sizes,
949init,init_regs,f_lbls,f_regs,pc,f,fn,stmt.
950b_graph_transform_program_props p_in p_out stack_sizes
951  init prog init_regs f_lbls f_regs →
952block_id … (pc_block pc) ≠ -1 →
953let trans_prog ≝ b_graph_transform_program p_in p_out init prog in
954fetch_statement p_in … (joint_globalenv p_in prog stack_sizes) pc =
955return 〈f,fn,stmt〉 →
956∃data.bind_instantiate ?? (init … fn) (init_regs (pc_block pc)) = return data ∧
957match stmt with
958[ sequential step nxt ⇒
959    ∃step_block : step_block p_out (prog_names … trans_prog).
960    bind_instantiate ?? (f_step … data (point_of_pc p_in pc) step)
961                 (f_regs (pc_block pc) (point_of_pc p_in pc)) = return step_block ∧
962    ∃pc'.sigma_stored_pc p_in p_out prog stack_sizes init
963                                           init_regs f_lbls f_regs pc' = pc ∧
964    ∃fn',nxt',l1,l2.
965    fetch_statement p_out … (joint_globalenv p_out trans_prog stack_sizes) pc' =
966    if not_emptyb … (added_prologue … data) ∧
967       eq_identifier … (point_of_pc p_in pc) (joint_if_entry … fn)
968    then OK ? 〈f,fn',sequential ?? (NOOP …) nxt'〉
969    else OK ? 〈f,fn',sequential ?? ((\snd(\fst step_block)) (point_of_pc p_in pc')) nxt'〉 ∧
970    seq_list_in_code p_out (prog_names … trans_prog) (joint_if_code … fn') (point_of_pc p_out pc)
971     (map_eval … (\fst (\fst step_block)) (point_of_pc p_out pc'))
972     l1 (point_of_pc p_out pc')
973    ∧ seq_list_in_code p_out ? (joint_if_code … fn') nxt' (\snd step_block) l2 nxt
974    ∧ sigma_next p_in p_out prog stack_sizes init init_regs f_lbls f_regs (pc_block pc) nxt' = return nxt
975| final fin ⇒
976    ∃fin_block.bind_instantiate ?? (f_fin … data (point_of_pc p_in pc) fin)
977                  (f_regs (pc_block pc) (point_of_pc p_in pc)) = return fin_block ∧
978    ∃pc'.sigma_stored_pc p_in p_out prog stack_sizes init
979                                           init_regs f_lbls f_regs pc' = pc ∧
980    ∃fn'.fetch_statement p_out …
981       (joint_globalenv p_out trans_prog stack_sizes) pc'
982       = return 〈f,fn',final ?? (\snd fin_block)〉           
983| FCOND abs _ _ _ ⇒ Ⓧabs
984].
985#p_in #p_out #prog #stack_sizes #init #init_regs #f_lbls #f_regs #pc #f #fn #stmt
986#good #Hbl #EQfetch cases(fetch_statement_inv … EQfetch) #EQfn normalize nodelta
987#EQstmt
988lapply(b_graph_transform_program_fetch_internal_function … good … (pc_block pc) f fn)
989@eqZb_elim [ #EQ >EQ in Hbl; * #H @⊥ @H %] #_ normalize nodelta #H cases(H EQfn) -H
990#data * #t_fn ** #EQt_fn #Hinit * #_ #_ #_ #pp_labs #_ #fresh_labs #_ #_ #_ #H
991lapply(H … EQstmt) -H normalize nodelta #H #_ %{data} >Hinit %{(refl …)}
992-EQfetch cases stmt in EQstmt H;
993[ #step #nxt | #fin | *] normalize nodelta #EQstmt -stmt
994[ cases(added_prologue ??? data) [2: #hd #tl] normalize nodelta
995  [ @eq_identifier_elim #EQentry normalize nodelta ] ]
996* #block *
997[ whd in ⊢ (% → ?); inversion(f_regs ??) [2: #x #y #_ #_ *] #EQregs normalize nodelta
998  #EQ destruct(EQ) whd in ⊢ (% → ?); * #pre * #mid1 * #mid2 * #post *** #EQmid1
999  whd in ⊢ (% → ?); * #EQ1 #EQ2 destruct(EQ1 EQ2) whd in ⊢ (% → ?); * #nxt1
1000  * #EQt_stmt change with nxt1 in ⊢ (??%? → ?); #EQ destruct(EQ) whd in ⊢ (% → ?);
1001  * #EQ1 #EQ2 destruct(EQ1 EQ2) whd in EQmid1 : (??%%); destruct(EQmid1)
1002|*: #Hregs #syntax_spec
1003]
1004[ whd in match (point_of_pc ??) in EQstmt EQentry; <EQentry in EQstmt;
1005  cases(entry_is_cost … (pi2 ?? fn)) #nxt1 * #c #EQstmt >EQstmt #EQ destruct(EQ)
1006  % [2: % [ >(f_step_on_cost … data) in ⊢ (??%?); % ] |] %{pc}
1007  whd in match sigma_stored_pc; whd in match sigma_pc_opt; normalize nodelta
1008  @eqZb_elim [ #EQ >EQ in Hbl; * #H @⊥ @H %] #_ @eqZb_elim
1009  [ #EQ >fetch_internal_function_no_zero in EQt_fn; [2: assumption] whd in ⊢ (???% → ?);
1010    #ABS destruct(ABS) ] normalize nodelta #_
1011|*: %{block} >Hregs %{(refl …)}
1012]
1013cases(sigma_label_spec … good … Hbl EQfn EQstmt) * [2,4,6,8: #n ] * #EQpreamble
1014normalize nodelta [1,2,3,4: * #lbl * #EQnth_opt] #EQsigma_lab
1015[   whd in match (point_of_pc ??) in e0; <EQentry in e0; #e0 >e0 in EQnth_opt;
1016    whd in ⊢ (??%% → ?); #EQ destruct(EQ)
1017|5: whd in match (point_of_pc ??); <EQentry >EQsigma_lab >m_return_bind
1018    normalize nodelta >EQentry % [ cases pc #bl #off %] %{t_fn} %{nxt} %{[ ]} %{[ ]}
1019    whd in match fetch_statement; normalize nodelta >EQt_fn >m_return_bind
1020    >EQt_stmt >m_return_bind %
1021    [ % [ % [ @eq_identifier_elim [#_ %] * #H @⊥ @H % | %{(refl …) (refl …)}] | %{(refl …) (refl …)}]]
1022    whd in match sigma_next; normalize nodelta >code_block_of_block_eq
1023    >m_return_bind >EQfn >m_return_bind inversion(find ????)
1024    [ #EQfind @⊥ lapply(find_none … EQfind EQstmt) normalize nodelta
1025    >EQpreamble normalize  nodelta >EQentry >e0 normalize nodelta
1026    @eq_identifier_elim [#_ *] * #H #_ @H %]
1027    * #lbl1 #stmt1 #EQfind >m_return_bind lapply(find_predicate ?????? EQfind)
1028    inversion(stmt_get_next … stmt1) [#_ *] #nxt1 #EQnxt1 normalize nodelta
1029    inversion(preamble_length ?????????) [#_ *] #m whd in match preamble_length;
1030    normalize nodelta >code_block_of_block_eq >m_return_bind >EQfn >m_return_bind
1031    whd in match (stmt_at ????); >(find_lookup ?????? EQfind) >m_return_bind
1032    >Hinit >m_return_bind cases stmt1 in EQfind; [#j_step #succ | #fin | *]
1033    #EQfind normalize nodelta cases(bind_instantiate ???)
1034    [1,3: whd in ⊢ (??%% → ?); #EQ destruct] #bl1 >m_return_bind whd in ⊢ (??%? → ?);
1035    #EQ destruct(EQ) inversion(nth_opt ???) [1,3: #_ *] #lbl2 #EQlbl2 normalize nodelta
1036    @eq_identifier_elim [2,4: #_ *] #EQ lapply EQlbl2 destruct(EQ) #EQlbl2
1037    cases(Exists_append … (nth_opt_Exists ???? EQlbl2)) [2,4: * [2,4: *] #EQ >EQ #_ %]
1038    #H @⊥ cases(Exists_All … H (fresh_labs lbl1)) #x * #EQ destruct(EQ) ** -H #H #_
1039    @H @(code_is_in_universe … (pi2 ?? fn)) whd in match code_has_label;
1040    whd in match code_has_point; normalize nodelta
1041    cases(fetch_stmt_ok_nxt_ok … EQfn EQstmt) #stmt2 #EQ >EQ @I
1042|2,3,4: %{(pc_of_point p_out (pc_block pc) lbl)}
1043|6,7,8: %{pc}
1044]
1045whd in match sigma_stored_pc; whd in match sigma_pc_opt; normalize nodelta
1046@eqZb_elim [1,3,5,7,9,11: #H >H in Hbl; * #H1 @⊥ @H1 %] #_ normalize nodelta
1047@eqZb_elim
1048[1,3,5,7,9,11: #EQ >fetch_internal_function_no_zero in EQt_fn; [2,4,6,8,10,12: assumption]
1049  whd in ⊢ (???% → ?); #ABS destruct(ABS) ] #_ normalize nodelta
1050[1,2,3: >point_of_pc_of_point] >EQsigma_lab >m_return_bind >(pc_of_point_of_pc … pc)
1051%{(refl …)} %{t_fn} cases block in Hregs syntax_spec; -block
1052[1,2,4,5: * #pre #instr #post |*: #pre #instr ] #Hregs *
1053[1,2,3,4: #l1 * #mid1 * #mid2 * #l2 ***
1054|*: #l1 * #mid **
1055]
1056#EQmid #EQpre whd in ⊢ (% → ?);
1057[1,2,3,4: * #nxt1 *] #EQt_stmt
1058[1,2,3,4: change with nxt1 in ⊢ (??%? → ?); #EQ destruct(EQ) #EQpost %{mid2} %{l1} %{l2} %]
1059[1,3,5,7,9,10: whd in match fetch_statement; normalize nodelta >EQt_fn >m_return_bind
1060 @('bind_inversion EQpreamble) #bl1 >code_block_of_block_eq whd in ⊢ (??%? → ?);
1061 #EQ destruct(EQ) >EQfn >m_return_bind >EQstmt >m_return_bind >Hinit >m_return_bind
1062 normalize nodelta >Hregs >m_return_bind cases pre in Hregs EQpre; -pre
1063 [1,3,6,8,9,12: [3,4,6: #x #y] #_ #_ whd in match (length ??); whd in ⊢ (??%? → ?);
1064    #EQ destruct(EQ)]
1065 [1,2,5: #hd1 #tl1 ] #Hregs cases l1 in EQmid;
1066 [1,3,5,8,10,12: [4,5,6: #x #y] #_ whd in ⊢ (% → ?); [1,2,3: * #EQ1 #EQ2 destruct]
1067    * #mid * #rest ** #EQ destruct(EQ)]
1068 [1,2,3: #hd2 #tl2] whd in ⊢ (??%% → ?); #EQ destruct(EQ) whd in ⊢ (% → ?);
1069 [4,5,6: * #_ #EQ #_ >EQ >EQt_stmt [2,3: % [ %{(refl …)} %{(refl …) (refl …)} ] assumption]
1070   @eq_identifier_elim [2: #_ % [ %{(refl …)} %{(refl …) (refl …)} | assumption] ]
1071   #EQ <EQ in EQentry; * #H @⊥ @H %]
1072 * #mid' * #rest ** #EQ1 destruct(EQ1) #H1 #H2 whd in match (length ??);
1073 whd in ⊢ (??%? → ?); #EQ1 destruct(EQ1) >e0 in EQnth_opt;
1074 lapply(seq_list_in_code_length … H2) [1,2: >length_map] #EQ1 >EQ1
1075 >nth_opt_append_r [2,4,6: %] cut(|rest|-|rest|=O) [1,3,5: cases(|rest|) //]
1076 #EQ2 >EQ2 whd in ⊢ (??%% → ?); #EQ3 -EQ2 -EQ1
1077 [1,2: destruct(EQ3) >point_of_pc_of_point >EQt_stmt
1078    [2: >point_of_pc_of_point % [%{(refl …)} whd %{mid'} %{rest} % [2: assumption] % [2: assumption] %]
1079       assumption]
1080    @eq_identifier_elim [#EQ4 <EQ4 in EQentry; * #H3 @⊥ @H3 %] #_ >point_of_pc_of_point %
1081    [ %{(refl …)} whd %{mid'} %{rest} % [ %{(refl …)} assumption ] assumption | assumption ] ]
1082 destruct(EQ3) >point_of_pc_of_point >EQt_stmt %]
1083whd in match sigma_next; normalize nodelta >code_block_of_block_eq >m_return_bind
1084>EQfn >m_return_bind inversion(find ????)
1085[1,3,5,7: #EQfind @⊥ lapply(find_none … EQfind EQstmt) normalize nodelta >EQpreamble
1086  normalize nodelta cases post in Hregs EQpost;
1087  [1,3,5,7: #Hregs * #EQ1 #EQ2 destruct(EQ1 EQ2) @('bind_inversion EQpreamble)
1088    #bl' >code_block_of_block_eq whd in ⊢ (??%? → ?); #EQ destruct(EQ) >EQfn
1089    >m_return_bind >EQstmt >m_return_bind >Hinit >m_return_bind normalize nodelta
1090    >Hregs >m_return_bind cases pre in EQpre Hregs;
1091    [1,3,6,8: [3,4: #x #y] #_ #_ whd in match (length ??); whd in ⊢ (??%? → ?); #EQ destruct
1092    |2,4: #fst #remaining] *
1093    [1,2: #lab1 * #rest ** #EQ destruct(EQ) * #nxt' * #EQpc change with nxt' in ⊢ (??%? → ?);
1094      #EQ destruct(EQ) #Hrest #Hregs whd in match (length ??); whd in ⊢ (??%? → ?);
1095      #EQ destruct(EQ) whd in EQmid : (??%%); destruct(EQmid) >e0
1096      lapply(seq_list_in_code_length … Hrest) >length_map #EQ >EQ
1097      >nth_opt_append_r
1098      [2,4: >length_append whd in match (length ? [mid1]);
1099             whd in match (length ? [ ]); cases(|rest|) //]
1100      >length_append whd in match (length ? [mid1]); whd in match (length ? [ ]);
1101      cut(S (|rest|) - (|rest| + 1) = O)
1102      [1,3: cases(|rest|) // #m normalize cases m // #m1 normalize nodelta
1103            cut(m1 + 1 = S m1) [1,3: //] #EQ1 >EQ1 <minus_n_n % ]
1104      #EQ1 >EQ1 normalize nodelta @eq_identifier_elim [1,3: #_ *] * #H #_ @H %
1105    |*: #EQ1 #EQ2 destruct(EQ1 EQ2) #EQregs #_ whd in EQmid : (??%%); destruct(EQmid)
1106        >e0 normalize nodelta @eq_identifier_elim [1,3: #_ *] * #H #_ @H %
1107    ]
1108  |*: #fst #rems #Hregs * #lab1 * #rest ** #EQ destruct(EQ) * #nxt1 * #EQmid2
1109      change with nxt1 in ⊢ (??%? → ?); #EQ destruct(EQ) #EQrest @('bind_inversion EQpreamble)
1110    #bl' >code_block_of_block_eq whd in ⊢ (??%? → ?); #EQ destruct(EQ) >EQfn
1111      >m_return_bind >EQstmt >m_return_bind >Hinit >m_return_bind normalize nodelta
1112      >Hregs >m_return_bind cases pre in EQpre Hregs;
1113      [1,3,6,8: [3,4: #x #y] #_ #_ whd in ⊢ (??%? → ?); whd in match (length ??);
1114        #EQ destruct|2,4: #hd1 #tl1] *
1115      [1,2: #lab1 * #rest1 ** #EQ destruct(EQ) * #nxt1 * #EQpc
1116        change with nxt1 in ⊢ (??%? → ?); #EQ destruct(EQ) #EQrest1 #Hregs
1117        whd in match (length ??); whd in ⊢ (??%? → ?); #EQ destruct(EQ)
1118      |*: #EQ1 #EQ2 destruct(EQ1 EQ2) #Hregs #_
1119      ]
1120      whd in EQmid : (??%%); destruct(EQmid) >e0 normalize nodelta
1121      [3,4: @eq_identifier_elim [1,3: #_ *] * #H #_ @H %]
1122      lapply(seq_list_in_code_length … EQrest1) >length_map #EQ >EQ
1123      >nth_opt_append_l [2,4: >length_append whd in match (length ? (mid1::?));
1124      whd in match (length ? (mid2::rest)); cases(|rest1|) //] >append_cons
1125      >append_cons >nth_opt_append_l
1126      [2,4: >length_append >length_append whd in match (length ? [ ? ]);
1127            whd in match (length ? [ ]); cases(|rest1|) // ]
1128      >nth_opt_append_r
1129      [2,4: >length_append whd in match (length ? [ ? ]); whd in match (length ? [ ]);
1130            cases(|rest1|) // ]
1131      >length_append whd in match (length ? [ ? ]); whd in match (length ? [ ]);
1132      cut(S(|rest1|) - (|rest1|+1) = 0)
1133      [1,3: cases(|rest1|) // #m normalize cases m // #m1 normalize nodelta
1134            cut(m1 + 1 = S m1) [1,3: //] #EQ1 >EQ1 <minus_n_n % ] #EQ1 >EQ1
1135      normalize nodelta @eq_identifier_elim [1,3: #_ *] * #H #_ @H %
1136  ]
1137|*: * #lab2 * [1,4,7,10: #j_step #nxt1 |2,5,8,11: #fin1 |*: *] #EQfind
1138    lapply(find_predicate ?????? EQfind) normalize nodelta [5,6,7,8: *]
1139    cases(preamble_length ?????????) normalize nodelta [1,3,5,7: *] #n
1140    inversion(nth_opt ???) normalize nodelta [1,3,5,7: #_ *] #lab1 #EQlab1
1141    @eq_identifier_elim [2,4,6,8: #_ *] #EQ destruct(EQ)
1142    cases(Exists_append … (nth_opt_Exists ???? EQlab1))
1143    [2,4,6,8: * [2,4,6,8: *] #EQ destruct(EQ) cases post in Hregs EQpost;
1144      [1,3,5,7: #Hregs * #EQ1 #EQ2 destruct(EQ1 EQ2) #_ %] #hd1 #tl1 #Hregs *
1145      #lab3 * #rest3 ** #EQ destruct(EQ) * #nxt2 * #EQt_lab1
1146      change with nxt2 in ⊢ (??%? → ?); #EQ destruct(EQ) #EQrest3
1147      @('bind_inversion EQpreamble) #bl' >code_block_of_block_eq
1148      whd in ⊢ (??%? → ?); #EQ destruct(EQ) >EQfn >m_return_bind >EQstmt
1149      >m_return_bind >Hinit >m_return_bind normalize nodelta >Hregs
1150      >m_return_bind cases pre in Hregs EQpre;
1151      [1,3,6,8: [3,4: #x #y] #_ #_ whd in match (length ??); whd in ⊢ (??%? → ?);
1152        #EQ destruct(EQ) |2,4: #hd1 #tl1] #Hregs *
1153      [1,2: #lab4 * #rest4 ** #EQ destruct(EQ) * #nxt2 * #EQpc
1154        change with nxt2 in ⊢ (??%? → ?); #EQ destruct(EQ) #EQrest4
1155        whd in match (length ??); whd in ⊢ (??%? → ?); #EQ destruct(EQ) #_
1156      |*: #EQ1 #EQ2 destruct(EQ1 EQ2) #_ #_
1157      ]
1158      whd in EQmid : (??%%); destruct(EQmid) @⊥ lapply(fresh_labs (point_of_pc p_in pc))
1159      >e0
1160      [1,2: #H cases(append_All … H) #_ * #_ *** -H #H #_ #_ @H
1161      |*: *** #H #_ #_ @H
1162      ]
1163      @(code_is_in_universe … (pi2 ?? fn)) whd in match code_has_label;
1164      whd in match code_has_point; normalize nodelta
1165      cases(fetch_stmt_ok_nxt_ok … EQfn (find_lookup ?????? EQfind))
1166      #stmt' #EQstmt' >EQstmt' @I
1167    |*: #Hlab2 cases post in Hregs EQpost;
1168        [1,3,5,7: #Hregs * #EQ1 #EQ2 destruct(EQ1 EQ2)
1169          cases(Exists_All … Hlab2 (fresh_labs lab2)) #x * #EQ destruct(EQ) ** #H
1170          @⊥ @H @(code_is_in_universe … (pi2 ?? fn)) whd in match code_has_label;
1171          whd in match code_has_point; normalize nodelta
1172          cases(fetch_stmt_ok_nxt_ok … EQfn EQstmt) #stmt' #EQstmt' >EQstmt' @I
1173        |*: #hd1 #tl1 #Hregs * #lab3 * #rest3 ** #EQ destruct(EQ) * #nxt2 *
1174          #EQt_lab1 change with nxt2 in ⊢ (??%? → ?); #EQ destruct(EQ) #EQrest3
1175          @('bind_inversion EQpreamble) #bl' >code_block_of_block_eq
1176          whd in ⊢ (??%? → ?); #EQ destruct(EQ) >EQfn >m_return_bind >EQstmt
1177          >m_return_bind >Hinit >m_return_bind normalize nodelta >Hregs
1178          >m_return_bind cases pre in Hregs EQpre;
1179          [1,3,6,8: [3,4: #x #y] #_ #_ whd in match (length ??); whd in ⊢ (??%? → ?);
1180             #EQ destruct(EQ)
1181          |2,4: #hd1 #tl1]
1182          #Hregs *
1183          [1,2: #lab4 * #rest4 ** #EQ destruct(EQ) * #nxt2 * #EQpc
1184            change with nxt2 in ⊢ (??%? → ?); #EQ destruct(EQ) #EQrest4
1185            whd in match (length ??); whd in ⊢ (??%? → ?); #EQ destruct(EQ) #_
1186          |*: #EQ1 #EQ2 destruct(EQ1 EQ2) #_ #_
1187          ]
1188          whd in EQmid : (??%%); destruct(EQmid) cases(pp_labs) #_ #H
1189          lapply(H lab2 (point_of_pc p_in pc) lab1 ? ?)
1190          [3,6,9,12: #EQ destruct(EQ) whd in match (stmt_at ????) in EQstmt;
1191             >(find_lookup ?????? EQfind) in EQstmt; #EQ destruct(EQ) %
1192          |1,4,7,10: >e0 [3,4: whd in match (memb ???); @eqb_elim
1193            [2,4: * #H @⊥ @H %] #_ @I] >memb_append_l2 [1,3: @I]
1194             whd in match (memb ???); @if_elim [1,3: #_ %] #_
1195             whd in match (memb ???); @eqb_elim [1,3: #_ %] * #H1 @⊥ @H1 %
1196          |*:  @Exists_memb assumption
1197          ]
1198        ]
1199     ]
1200]
1201qed.
1202
1203
1204definition make_is_relation_from_beval : (beval → beval → Prop) →
1205internal_stack → internal_stack → Prop≝
1206λR,is1,is2.
1207match is1 with
1208[ empty_is ⇒ match is2 with [ empty_is ⇒ True | _ ⇒ False]
1209| one_is b ⇒ match is2 with [ one_is b' ⇒ R b b' | _ ⇒ False ]
1210| both_is b1 b2 ⇒ match is2 with [ both_is b1' b2' ⇒ R b1 b1' ∧ R b2 b2' | _ ⇒ False ]
1211].
1212
1213lemma is_push_ok : ∀Rbeval : beval → beval → Prop.
1214∀Ristack1 : internal_stack → internal_stack → Prop.
1215∀Ristack2 : internal_stack → internal_stack → Prop.
1216(∀is1,is2.Ristack1 is1 is2 → make_is_relation_from_beval Rbeval is1 is2) →
1217(∀bv1,bv2.Ristack1 empty_is empty_is → Rbeval bv1 bv2 →
1218                                   Ristack2 (one_is bv1) (one_is bv2)) →
1219(∀bv1,bv2,bv3,bv4.Rbeval bv1 bv2 → Rbeval bv3 bv4 →
1220                         Ristack2 (both_is bv3 bv1) (both_is bv4 bv2)) →
1221                         gen_preserving2 ?? gen_res_preserve …
1222                              Ristack1 Rbeval Ristack2 is_push is_push.
1223#Rbeval #Ristack1 #Ristack2 #H #H1 #H2 #is1 #is2 #bv1 #bv2 #H3 #H4
1224whd in match is_push; normalize nodelta cases is1 in H3; normalize nodelta
1225[2:#x|3: #x #y #_ @res_preserve_error_gen]
1226cases is2 normalize nodelta
1227 [1,3,5,6: [| #z #w | #z | #z #w] #H5 cases(H … H5) | #y] #H5 @m_gen_return
1228 [@H2 [assumption | @(H … H5) ] | @H1 assumption]
1229qed.
1230(*
1231lemma is_push_ok : ∀R : beval → beval → Prop.
1232               gen_preserving2 ?? gen_res_preserve …
1233                       (make_is_relation_from_beval R) R
1234                       (make_is_relation_from_beval R)
1235                       is_push is_push.
1236#R @is_push_ok_gen // #bv1 #bv2 #bv3 #bv4 #H #H1 %{H1 H}
1237qed.
1238*)
1239lemma is_pop_ok: ∀Rbeval : beval → beval → Prop.
1240∀Ristack1 : internal_stack → internal_stack → Prop.
1241∀Ristack2 : internal_stack → internal_stack → Prop.
1242(∀is1,is2.Ristack1 is1 is2 → make_is_relation_from_beval Rbeval is1 is2) →
1243Ristack2 empty_is empty_is →
1244(∀bv1,bv2.Rbeval bv1 bv2 → Ristack2 (one_is bv1) (one_is bv2)) →
1245          gen_preserving ?? gen_res_preserve …
1246                              Ristack1
1247                              (λx,y.Rbeval (\fst x) (\fst y) ∧
1248                                Ristack2 (\snd x) (\snd y)) is_pop is_pop.
1249#Rbeval #Ristack1 #Ristack2 #H #H1 #H2 #is1 #is2 whd in match is_pop; normalize nodelta
1250cases is1 normalize nodelta [#_ @res_preserve_error_gen] #x [|#y] cases is2
1251[1,3,4,5: [|#x #y||#x] #H3 cases(H … H3) | #y | #z #w] #H3 normalize nodelta
1252@m_gen_return [ % [ @(H … H3) | assumption ] | cases(H … H3) #H4 #H5 %{H5} @(H2 … H4)
1253qed.
1254
1255(*
1256lemma is_pop_ok1 : ∀R : beval → beval → Prop.
1257           gen_preserving ?? gen_res_preserve …
1258                         (make_is_relation_from_beval R)
1259                         (λx,y.R (\fst x) (\fst y) ∧
1260                               (make_is_relation_from_beval R) (\snd x) (\snd y))
1261                         is_pop is_pop.
1262#R @is_pop_ok //
1263qed.
1264
1265
1266definition make_weak_state_relation ≝
1267λp_in,p_out.λR : (beval → beval → Prop).λst1 : state p_in.λst2 : state p_out.
1268(make_is_relation_from_beval R) (istack … st1) (istack … st2).
1269*)
1270
1271
1272lemma push_ok : ∀p_in,p_out,Rbeval,Rstate1,Rstate2.
1273(∀is1,is2,st1,st2.istack ? st1 = is1 → istack ? st2 = is2 → Rstate1 st1 st2 →
1274                  make_is_relation_from_beval Rbeval is1 is2) →
1275(∀st1,st2,bv1,bv2. Rstate1 st1 st2 → Rbeval bv1 bv2 →
1276 Rstate2 (set_istack p_in (one_is bv1) st1) (set_istack p_out (one_is bv2) st2)) →
1277(∀st1,st2,bv1,bv2,bv3,bv4.Rstate1 st1 st2 → Rbeval bv1 bv2 → Rbeval bv3 bv4 →
1278 Rstate2 (set_istack p_in (both_is bv1 bv3) st1) (set_istack p_out (both_is bv2 bv4) st2)) →
1279                    gen_preserving2 ?? gen_res_preserve … Rstate1 Rbeval Rstate2
1280                                   (push p_in) (push p_out).
1281#p_in #p_out #Rbeval #Rstate1 #Rstate2 #H #H1 #H2 #st1 #st2 #bv1 #bv2 #H3 #H4
1282whd in match push; normalize nodelta
1283@(m_gen_bind_inversion … (make_is_relation_from_beval Rbeval))
1284[ @(is_push_ok Rbeval (make_is_relation_from_beval Rbeval)) //
1285  [ #bv1 #bv2 #bv3 #bv4 #H5 #H6 whd %{H6 H5}
1286  | @(H … H3) %
1287  ]
1288| * [|#x|#x1 #x2] * [1,4,7:|2,5,8: #y |*: #y1 #y2] #H5 #H6 whd in ⊢ (% → ?);
1289  [1,2,3,4,6,7,8,9: * [2: #H7 #H8] | #H7] @m_gen_return
1290  [ @(H2 … H3) assumption
1291  | cases(istack … st1) in H5; [2,3: #z [2: #w]] whd in ⊢ (??%% → ?);
1292    #EQ destruct(EQ)
1293  | @(H1 … H3) assumption
1294  ]
1295]
1296qed.
1297
1298
1299lemma pop_ok : ∀p_in,p_out,Rbeval,Rstate1,Rstate2.
1300(∀is1,is2,st1,st2.istack ? st1 = is1 → istack ? st2 = is2 → Rstate1 st1 st2 →
1301                  make_is_relation_from_beval Rbeval is1 is2) →
1302(∀st1,st2.Rstate1 st1 st2 →
1303 Rstate2 (set_istack p_in (empty_is) st1) (set_istack p_out (empty_is) st2)) →
1304(∀st1,st2,bv1,bv2. Rstate1 st1 st2 → Rbeval bv1 bv2 →
1305 Rstate2 (set_istack p_in (one_is bv1) st1) (set_istack p_out (one_is bv2) st2)) →
1306               gen_preserving ?? gen_res_preserve …
1307                 Rstate1
1308                (λx,y.Rbeval (\fst x) (\fst y) ∧
1309                 Rstate2(\snd x) (\snd y))
1310                (pop p_in) (pop p_out).
1311#p_in #p_out #Rbeval #Rstate1 #Rstate2 #H #H1 #H2 #st1 #st2 #H3
1312whd in match pop; normalize nodelta
1313@(m_gen_bind_inversion … (λx,y.Rbeval (\fst x) (\fst y) ∧
1314           (make_is_relation_from_beval Rbeval (\snd x) (\snd y)))) 
1315[ @(is_pop_ok Rbeval (make_is_relation_from_beval Rbeval)) // @(H … H3) %
1316| * #bv1 * [|#x|#x1 #x2] * #bv2 *
1317[1,4,7:|2,5,8: #y
1318|*: #y1 #y2 [1,2: #_ #_ * #_ *] cases(istack … st1) [|#z|#z #w] whd in ⊢ (??%% → ?); #EQ destruct ]
1319#_ #_ * #H4 [2,3,4,6: *| #_ | whd in ⊢ (% → ?); #H5] @m_gen_return
1320% // [ @(H1 … H3) | @(H2 … H3) assumption]
1321qed.
1322
1323(* 
1324lemma next_of_call_pc_ok : ∀P_in,P_out : sem_graph_params.
1325∀init,prog,stack_sizes,init_regs,f_lbls,f_regs.
1326b_graph_transform_program_props P_in P_out stack_sizes
1327  init prog init_regs f_lbls f_regs →
1328let trans_prog ≝ b_graph_transform_program P_in P_out init prog in
1329∀bl :Σb.block_region b =Code.block_id bl ≠ -1 →
1330∀f,fn.
1331fetch_internal_function … (joint_globalenv P_in prog stack_sizes) bl =
1332 return 〈f,fn〉 →
1333(∀id,args,dest,lbl.
1334  bind_new_P' ??
1335  (λregs1.λdata.bind_new_P' ??
1336   (λregs2.λblp.
1337     ∀lbl.∃id',args',dest'.((\snd (\fst blp)) lbl) = CALL P_out ? id' args' dest')
1338   (f_step … data lbl (CALL P_in ? id args dest)))
1339  (init ? fn)) →
1340gen_preserving ?? gen_res_preserve ????
1341 (λpc1,pc2 : Σpc.pc_block pc = bl.
1342           sigma_stored_pc P_in P_out prog stack_sizes init
1343                                           init_regs f_lbls f_regs pc2 = pc1)
1344 (λn1,n2.sigma_next P_in P_out prog stack_sizes init init_regs f_lbls f_regs bl n2 = return n1)
1345 (next_of_call_pc P_in … (joint_globalenv P_in prog stack_sizes))
1346 (next_of_call_pc P_out … (joint_globalenv P_out trans_prog stack_sizes)).
1347#p_in #p_out #init #prog #stack_sizes #init_regs #f_lbls #f_regs #good #bl #Hbl
1348#f #fn #EQfn #Hcall #pc1 #pc2 #Hpc1pc2 #lbl1 #H @('bind_inversion H) -H ** #f1 #fn1 *
1349[ * [#c| #id #args #dest | #r #lb | #seq ] #nxt | #fin | *]
1350whd in match fetch_statement; normalize nodelta >(pi2 ?? pc1) >EQfn >m_return_bind
1351#H @('bind_inversion H) -H #stmt #H lapply(opt_eq_from_res ???? H) -H
1352#EQstmt whd in ⊢ (??%% → ??%% → ?); #EQ1 #EQ2 destruct(EQ1 EQ2)
1353cases(fetch_statement_sigma_stored_pc … pc1 f1 fn1 … good …)
1354[3: >(pi2 ?? pc1) assumption
1355|4: whd in match fetch_statement; normalize nodelta >(pi2 ?? pc1) in ⊢ (??%?);
1356    >EQfn in ⊢ (??%?); >m_return_bind in ⊢ (??%?); >EQstmt in ⊢ (??%?); % |2:]
1357#data * #Hdata normalize nodelta * #st_bl * #Hst_bl * #pc' * #EQpc' * #t_fn
1358* #nxt1 * #l1 * #l2 *** #EQt_fetch #_ #_ #nxt1rel %{nxt1} % [2: <(pi2 ?? pc1) assumption]
1359whd in match next_of_call_pc; normalize nodelta <EQpc' in Hpc1pc2;
1360#H lapply(sym_eq ??? H) -H whd in match sigma_stored_pc; normalize nodelta
1361inversion(sigma_pc_opt ?????????)
1362[ #ABS @⊥ whd in match sigma_stored_pc in EQpc'; normalize nodelta in EQpc';
1363  >ABS in EQpc'; normalize nodelta #EQ <(pi2 ?? pc1) in EQfn;
1364  >fetch_internal_function_no_zero [2: <EQ %] whd in ⊢ (???% → ?); #EQ1 destruct(EQ1) ]
1365#sigma_pc' #EQsigma_pc' normalize nodelta inversion(sigma_pc_opt ?????????)
1366[ #_ normalize nodelta #EQ destruct(EQ) @⊥ lapply EQt_fetch @if_elim #_ #EQf
1367  cases(fetch_statement_inv … EQf) >fetch_internal_function_no_zero [1,3: #EQ destruct]
1368  >(pc_block_eq p_in p_out prog stack_sizes init init_regs f_lbls f_regs)
1369  [1,3: whd in match sigma_stored_pc; normalize nodelta >EQsigma_pc' %
1370  |*: >EQsigma_pc' % #EQ destruct
1371  ]
1372]
1373#pc3 #EQpc3 normalize nodelta #EQ destruct(EQ) <EQsigma_pc' in EQpc3; #H
1374lapply(sym_eq ??? H) -H #EQp lapply(sigma_stored_pc_inj … EQp) [>EQsigma_pc' % #EQ destruct]
1375#EQ destruct(EQ) >EQt_fetch @eq_identifier_elim
1376[ #EQ1 >EQ1 in EQstmt; cases(entry_is_cost … (pi2 ?? fn1)) #nxt2 * #c #ABS >ABS #EQ1 destruct(EQ1) ]
1377#_ cases(not_emptyb ??) normalize nodelta >m_return_bind normalize nodelta
1378lapply(bind_new_bind_new_instantiates … (bind_instantiates_to_instantiate … Hst_bl)
1379       (bind_new_bind_new_instantiates … (bind_instantiates_to_instantiate … Hdata)
1380        (Hcall id args dest (point_of_pc p_in pc1))))
1381#H cases(H (point_of_pc p_in pc2)) #id' * #args' * #dest' #EQ >EQ %
1382qed.
1383*)
1384
1385
1386definition JointStatusSimulation :
1387∀p_in,p_out : sem_graph_params.
1388∀ prog.∀stack_sizes.
1389∀ f_lbls, f_regs. ∀init_regs.∀init.∀st_no_pc_rel,st_rel.
1390good_state_relation p_in p_out prog stack_sizes init init_regs f_lbls f_regs
1391                    st_no_pc_rel st_rel →
1392let trans_prog ≝ b_graph_transform_program p_in p_out init prog in
1393status_rel (joint_abstract_status (mk_prog_params p_in prog stack_sizes))
1394           (joint_abstract_status (mk_prog_params p_out trans_prog stack_sizes)) ≝
1395λp_in,p_out,prog,stack_sizes,f_lbls,f_regs,init_regs,init,st_no_pc_rel,st_rel,good.
1396   mk_status_rel ??
1397    (* sem_rel ≝ *) (λs1 : (joint_abstract_status (mk_prog_params p_in ??)).
1398                     λs2 : (joint_abstract_status (mk_prog_params p_out ??)).st_rel s1 s2)
1399    (* call_rel ≝ *) 
1400       (λs1:Σs: (joint_abstract_status (mk_prog_params p_in ??)).as_classifier ? s cl_call
1401          .λs2:Σs:(joint_abstract_status (mk_prog_params p_out ??)).as_classifier ? s cl_call
1402           .pc ? s1 =
1403        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.