source: LTS/variable_stack_pass.ma @ 3575

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