source: LTS/variable_stack_pass.ma @ 3574

Last change on this file since 3574 was 3574, checked in by piccolo, 4 years ago

assembly pass in place

File size: 11.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 "stack.ma".
16
17let rec trans_expr (e : expr) on e : list guard_combinators ≝
18match e with
19[ Var v ⇒ [push_var v]
20| Const n ⇒ [push_const n]
21| Plus e1 e2 ⇒ trans_expr e1 @ trans_expr e2 @ [push_plus]
22| Minus e1 e2 ⇒ trans_expr e1 @ trans_expr e2 @ [push_minus]
23].
24
25let rec trans_cond  (c : condition) on c : list guard_combinators ≝
26match c with
27[ Eq e1 e2 ⇒ trans_expr e1 @ trans_expr e2 @ [push_eq]
28| Not c ⇒ trans_cond c @ [push_not]
29].
30
31definition list_combinators_to_instructions : list guard_combinators → stack_Instructions → stack_Instructions ≝
32λl,i.foldr … (λc,instr.SEQ stack_state_params flat_labels (push c) (None ?) instr) i l.
33
34let rec get_expr_bound (e : expr) on e : ℕ ≝
35match e with
36[ Var v ⇒ S v
37| Const n ⇒ O
38| Plus e1 e2 ⇒ max (get_expr_bound e1) (get_expr_bound e2)
39| Minus e1 e2 ⇒ max (get_expr_bound e1) (get_expr_bound e2)
40].
41
42let rec get_cond_bound (c : condition) on c : ℕ ≝
43match c with
44[Eq e1 e2 ⇒ max (get_expr_bound e1) (get_expr_bound e2)
45| Not c ⇒ get_cond_bound c
46].
47
48let rec trans_var_instr (i : frame_Instructions) on i : (stack_Instructions × ℕ) ≝
49match i with
50[ EMPTY ⇒ 〈EMPTY …,O〉
51| RETURN exp ⇒ 〈list_combinators_to_instructions (trans_expr exp) (RETURN … it),get_expr_bound exp〉
52| SEQ seq opt_l instr ⇒
53  let 〈t_instr,c_bound〉 ≝ trans_var_instr … instr in
54  match seq with
55  [ Seq_i seq' ⇒
56     match seq' with
57     [ sNop ⇒ 〈t_instr,c_bound〉
58     | sAss v e ⇒ 〈list_combinators_to_instructions (trans_expr e) (SEQ … (pop_Ass v) opt_l t_instr),
59                max (get_expr_bound e) c_bound〉
60     ]
61  | PopReg v ⇒ 〈SEQ stack_instr_params ? (popreg v) opt_l t_instr,max (S v) c_bound〉
62  ]
63| COND cond ltrue i_true lfalse i_false instr ⇒
64  let 〈t_i_true,i_true_bound〉 ≝ trans_var_instr … i_true in
65  let 〈t_i_false,i_false_bound〉 ≝ trans_var_instr … i_false in
66  let 〈t_instr,c_bound〉 ≝ trans_var_instr … instr in
67  〈list_combinators_to_instructions (trans_cond cond) (COND … it ltrue t_i_true lfalse t_i_false t_instr),
68   max (get_cond_bound cond) (max i_true_bound (max i_false_bound c_bound))〉
69| LOOP cond ltrue i_true lfalse instr ⇒
70  let 〈t_i_true,i_true_bound〉 ≝ trans_var_instr … i_true in
71  let 〈t_instr,c_bound〉 ≝ trans_var_instr … instr in
72  〈LOOP stack_state_params flat_labels (trans_cond … cond) ltrue t_i_true lfalse t_instr,
73   max (get_cond_bound cond) (max i_true_bound c_bound)〉
74| CALL f act_p opt_l instr ⇒
75  let 〈t_instr,c_bound〉 ≝ trans_var_instr … instr in
76  〈list_combinators_to_instructions (trans_expr act_p) (CALL … f it opt_l t_instr),
77   max (get_expr_bound act_p) c_bound〉
78| IO lin io lout instr ⇒ ?
79].
80cases io
81qed.
82
83definition trans_var_prog : Program frame_env_params frame_instr_params flat_labels →
84(Program stack_env_params stack_instr_params flat_labels × ℕ) ≝
85λprog.
86let 〈t_main,m_bound〉 ≝ trans_var_instr … (main … prog) in
87〈(mk_Program …
88  (foldr …
89   (λx,tail.(let 〈t_body,bound〉 ≝ trans_var_instr … (f_body … x) in
90     mk_env_item …       
91     (mk_signature stack_env_params stack_instr_params (f_name … (f_sig … x)) bound it) (f_lab … x) t_body) :: tail)
92   (nil ?) (env … prog))
93  t_main),m_bound〉.
94
95include "Simulation.ma".
96
97definition frame_variable_pass_rel :
98∀prog : Program frame_state_params frame_state_params frame_state_params.
99∀t_prog : Program stack_env_params stack_instr_params flat_labels.
100∀bound : ℕ.
101relations flat_labels (operational_semantics frame_state_params frame_sem_state_params prog)
102(operational_semantics stack_state_params (stack_sem_state_params bound) t_prog) ≝
103λprog,t_prog,bound.
104(mk_relations flat_labels (operational_semantics frame_state_params frame_sem_state_params prog)
105 (operational_semantics stack_state_params (stack_sem_state_params bound) t_prog)
106 (λs1,s2.store … s1 = store … s2 ∧ code … s2 = (\fst (trans_var_instr (code … s1))) ∧
107         cont … s2 = foldr … (λx,tail. 〈(\fst x),(\fst (trans_var_instr (\snd x)))〉 :: tail) (nil ?) (cont … s1))
108 (λ_.λ_.True)).
109
110
111let rec expr_fv (e : expr) on e : list variable ≝
112match e with
113[ Var v ⇒ [v]
114| Const n ⇒ []
115| Plus e1 e2 ⇒ expr_fv e1 @ expr_fv e2
116| Minus e1 e2 ⇒ expr_fv e1 @ expr_fv e2
117].
118
119definition good_expr ≝  λbound : ℕ.λe : expr.All … (λv.v < bound) (expr_fv e).
120
121let rec cond_fv (c : condition) on c : list variable ≝
122match c with
123[ Eq e1 e2 ⇒ expr_fv e1 @ expr_fv e2
124| Not c1 ⇒ cond_fv c1
125].
126
127definition good_cond ≝ λbound : ℕ.λc : condition.All … (λv.v < bound) (cond_fv c).
128
129lemma good_expr_monotone : ∀n1,n2.∀e.n1 ≤ n2 → good_expr n1 e →
130good_expr n2 e.
131#n1 #n2 #e lapply n1 -n1 lapply n2 -n2 elim e //
132[ #v #n1 #n2 #H * #H1 * % /2/
133|*: #e1 #e2 #IH1 #IH2 #n1 #n2 #H #H1 cases(All_inv_append … H1) #H3 #H4 @All_append /2/
134]
135qed.
136
137lemma frame_sem_exp_cons : ∀env.∀e.∀n1,n2.
138to_shift ≤ |env| →
139good_expr (|env| - to_shift) e →
140frame_sem_expr env e = return n1 →
141frame_sem_expr (n2 :: env) e = return n1.
142#env #e lapply env -env elim e
143[ #v #env #n1 #n2 #prf1 * #Hv *
144 whd in ⊢ (??%% → ?); #H change with (nth_opt ???) in ⊢ (??%?);
145 change with ([?]@?) in match ([?]@?); >nth_second
146 [ >length_append whd in ⊢ (???%); <H @eq_f2 // normalize <minus_n_O <minus_n_O
147   >(eq_minus_S_pred ? O) <minus_n_O >(eq_minus_S_pred ? O) <minus_n_O
148   >(eq_minus_S_pred ? O) <minus_n_O @eq_f <(eq_minus_S_pred ? (S v)) %
149 | normalize <minus_n_O >minus_minus_comm @le_plus_to_minus_r
150   @(transitive_le … (monotonic_le_plus_r 1 ?? Hv)) normalize <(minus_Sn_m 2) //
151 ]
152| #n #env #n1 #n2 #prf1 * normalize //
153|*: #e1 #e2 #IH1 #IH2 #env #n1 #n2 #prf1 #H cases(All_inv_append … H) -H #H1 #H2
154  change with (m_bind ?????) in ⊢ (??%? → ?); #H cases(bind_inversion ????? H) -H
155  #n3 * #EQn3 #H cases(bind_inversion ????? H) -H #n4 * #EQn4 whd in ⊢ (??%% → ?);
156  #EQ destruct change with (m_bind ?????) in ⊢ (??%?); >(IH1 … EQn3) //
157  >(IH2 … EQn4) //
158]
159qed.
160
161
162lemma eval_exp_ok : ∀st.∀env:activation_frame.∀e: expr.∀n.
163to_shift ≤ |env| → good_expr (|env| - to_shift) e →
164frame_current_env … st = return env →
165frame_sem_expr env e = return n →
166∃st'.m_fold Option … eval_combinators (trans_expr e) st = return st' ∧
167pop st' = return 〈n,st〉.
168** [| #curr_env #rem] #sp_fp #env #e #n #H1 #H2 normalize in ⊢ (% → ?); #EQ destruct
169lapply n -n lapply H1 -H1 lapply H2 -H2 lapply env -env elim e
170[ #v #env #_ #_ #n whd in ⊢ (??%% → ?); #H %{(〈(n::env)::rem,sp_fp〉)} % try %
171  whd in ⊢ (??%?); whd in match eval_combinators; normalize nodelta
172  whd in match frame_current_env; normalize nodelta whd in match option_hd; normalize nodelta
173  >m_return_bind whd in match read_frame; normalize nodelta >H %
174| #n #env #_ #_ #m whd in ⊢ (??%% → ?); #EQ destruct %{(〈(m::env)::rem,sp_fp〉)} %%
175|*: #e1 #e2 #IH1 #IH2 #env #H1 #H2 #n change with (m_bind ?????) in ⊢ (??%? → ?);
176  #H cases(bind_inversion ????? H) -H #n1 * #EQn1
177  #H cases(bind_inversion ????? H) -H #n2 * #EQn2
178  whd in ⊢ (??%% → ?); #EQ destruct
179  [ %{(〈((n1 + n2)::env)::rem,sp_fp〉)} | %{(〈((n1 - n2)::env)::rem,sp_fp〉)} ]
180  >m_fold_append cases(IH1 … EQn1) // [2,4: cases(All_inv_append … H1) //] #st1 * #EQst1 #EQpop1 >EQst1
181  >m_return_bind >m_fold_append cases(IH2 … (n1 :: env))
182  [2,7: cases(All_inv_append … H1) #H1 #H2 @(good_expr_monotone … H2) normalize /2/
183  |3,8: normalize /2/
184  |5,10: @(frame_sem_exp_cons … EQn2) // cases(All_inv_append … H1) //
185  |4,9:
186  ]
187  #st2 * #EQst2 #EQpop2 cases st1 in EQst1 EQpop1; * [2,4: #env' #rem'] #fp_sp' #EQst1
188  [3,4: whd in ⊢ (??%% → ?); #EQ destruct ] cases env' in EQst1; [2,4: #val1 #env''] #EQst1
189  whd in ⊢ (??%% → ?); #EQ destruct >EQst2 >m_return_bind % [2,4: %] whd in ⊢ (??%?);
190  whd in match eval_combinators; normalize nodelta >EQpop2 %
191]
192qed.
193
194lemma eval_cond_ok :  ∀st.∀env:activation_frame.∀c: condition.∀b.
195to_shift ≤ |env| → good_cond (|env| - to_shift) c →
196frame_current_env … st = return env →
197frame_sem_condition env c = return b →
198∃st'.m_fold Option … eval_combinators (trans_cond c) st = return st' ∧
199pop st' = Some ? 〈if b then 1 else O,st〉.
200** [| #curr_env #rem] #sp_fp #env #c #b #H1 #H2 normalize in ⊢ (% → ?); #EQ destruct
201lapply b -b lapply H1 -H1 lapply H2 -H2 lapply env -env elim c
202[ #e1 #e2 #env #H #H1 #b change with (m_bind ?????) in ⊢ (??%? → ?); #H cases(bind_inversion ????? H) -H
203  #n * #EQn #H cases(bind_inversion ????? H) -H #m * #EQm whd in ⊢ (??%% → ?); #EQ destruct
204  %{(〈((if (eqb n m )then 1 else O) :: env)::rem,sp_fp〉)} >m_fold_append
205  cases(eval_exp_ok … EQn) //
206  [2: @(〈env::rem,sp_fp〉) |3: cases(All_inv_append … H) // |4: %]
207  #st1 * #EQst1 #EQpop1 >EQst1 >m_return_bind % // >m_fold_append
208  cases st1 in EQst1 EQpop1; * [| * [| #m' #env'] #rem'] #sp_fp' #EQst1 whd in ⊢ (??%% → ?);
209  #EQ destruct cases(eval_exp_ok … (frame_sem_exp_cons … EQm))
210  [2: @(〈(n::env)::rem,sp_fp〉) |5: % |3: @(transitive_le … H1) //
211  |4: @(good_expr_monotone … (|env| - to_shift)) [ /2/ | cases(All_inv_append … H) //]
212  |6: cases(All_inv_append … H) // |7: // |8:]
213  #st2 * #EQst2 #EQpop2 >EQst2 >m_return_bind change with (m_bind ?????) in ⊢ (??%?);
214  whd in match eval_combinators; normalize nodelta >EQpop2 %
215| #c1 #IH #env #H1 #H2 #b change with (m_bind ?????) in ⊢ (??%? → ?);
216  #H cases(bind_inversion ????? H) -H #b1 * #EQb1 whd in ⊢ (??%% → ?); #EQ destruct
217  %{(〈((if (if b1 then false else true) then 1 else O) :: env)::rem,sp_fp〉)} % //
218  >m_fold_append cases(IH … EQb1) // #st1 * #EQst1 #EQpop1 >EQst1 >m_return_bind
219  change with (m_bind ?????) in ⊢ (??%?); whd in match eval_combinators; normalize nodelta
220  >EQpop1 >m_return_bind whd in ⊢ (??%?); @eq_f @eq_f2 // cases b1 //
221]
222qed.
223
224(*
225let rec create_silent_trace_from_eval_combinators (st : (l : list guard_combinators)
226(prf: m_fold Option … eval_combinators l st = return st')
227*)
228
229definition simulation_imp_frame :
230∀prog : Program frame_state_params frame_state_params frame_state_params.
231∀t_prog,bound.〈t_prog,bound〉 = trans_var_prog prog →
232simulation_conditions … (frame_variable_pass_rel prog t_prog bound) ≝
233λprog,t_prog,bound,good_trans.
234mk_simulation_conditions ….
235cases daemon
236qed.
237 
Note: See TracBrowser for help on using the repository browser.