source: LTS/frame_variable_pass.ma @ 3577

Last change on this file since 3577 was 3577, checked in by sacerdot, 4 years ago

Number of local variables computed during the first pass and then propagated.

File size: 11.3 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 "Simulation.ma".
16include "variable.ma".
17include alias "arithmetics/nat.ma".
18
19definition update_fun : ∀A : DeqSet.∀B : Type[0].(A → B) → A → B → A → B ≝
20λA,B,f,a,b,x.if x == a then b else f x.
21
22let rec get_expr_bound (e : expr) on e : ℕ ≝
23match e with
24[ Var v ⇒ S v
25| Const n ⇒ O
26| Plus e1 e2 ⇒ max (get_expr_bound e1) (get_expr_bound e2)
27| Minus e1 e2 ⇒ max (get_expr_bound e1) (get_expr_bound e2)
28].
29
30let rec get_cond_bound (c : condition) on c : ℕ ≝
31match c with
32[Eq e1 e2 ⇒ max (get_expr_bound e1) (get_expr_bound e2)
33| Not c ⇒ get_cond_bound c
34].
35
36let rec get_instr_bound (i : frame_Instructions) ≝
37match i with
38[ EMPTY ⇒ O
39| RETURN exp ⇒ get_expr_bound exp
40| SEQ seq opt_l instr ⇒
41  let c_bound ≝ get_instr_bound instr in
42  match seq with
43  [ Seq_i seq' ⇒
44     match seq' with [ sAss v e ⇒ max (get_expr_bound e) c_bound ]
45  | PopReg v ⇒ max (S v) c_bound
46  ]
47| COND cond ltrue i_true lfalse i_false instr ⇒
48  let i_true_bound ≝ get_instr_bound i_true in
49  let i_false_bound ≝ get_instr_bound i_false in
50  let c_bound ≝ get_instr_bound instr in 
51   max (get_cond_bound cond) (max i_true_bound (max i_false_bound c_bound))
52| LOOP cond ltrue i_true lfalse instr ⇒
53  let i_true_bound ≝ get_instr_bound i_true in
54  let c_bound ≝ get_instr_bound instr in
55   max (get_cond_bound cond) (max i_true_bound c_bound)
56| CALL f act_p opt_l instr ⇒
57  let c_bound ≝ get_instr_bound instr in
58   max (get_expr_bound act_p) c_bound
59| IO lin io lout instr ⇒ ?
60].
61cases io
62qed.
63
64let rec map_exp (e : expr) (map : variable → variable) on e : expr ≝
65match e with
66[ Var v ⇒ Var (map v)
67| Const n ⇒ Const n
68| Plus e1 e2 ⇒ Plus (map_exp e1 map) (map_exp e2 map)
69| Minus e1 e2 ⇒ Minus (map_exp e1 map) (map_exp e2 map)
70].
71
72let rec map_cond (c : condition) (map : variable → variable) on c : condition ≝
73match c with
74[Eq e1 e2 ⇒ Eq (map_exp e1 map) (map_exp e2 map)
75| Not c1 ⇒ Not (map_cond c1 map)
76].
77
78definition map_seq ≝
79 λi,map.
80  match i with
81  [ sAss v e ⇒ Seq_i (sAss (map v) (map_exp e map))
82  ].
83
84let rec trans_imp_instr (i : imp_Instructions) (map : variable → variable) on i : frame_Instructions  ≝
85match i with
86[ EMPTY ⇒ EMPTY …
87| RETURN rt ⇒ RETURN … rt
88| SEQ seq opt_l i ⇒ SEQ … (map_seq seq map) opt_l (trans_imp_instr i map)
89| COND cond ltrue i_true lfalse i_false instr ⇒
90    COND frame_state_params flat_labels (map_cond cond map) ltrue (trans_imp_instr i_true map) lfalse (trans_imp_instr i_false map)
91           (trans_imp_instr instr map)
92| LOOP cond ltrue i_true lfalse instr ⇒
93    LOOP frame_state_params flat_labels (map_cond cond map) ltrue (trans_imp_instr i_true map) lfalse 
94           (trans_imp_instr instr map)
95| CALL f act_p opt_l instr ⇒
96    (CALL frame_state_params flat_labels f (map_exp (\snd act_p) map) opt_l
97      (SEQ … (PopReg (\fst act_p)) (None …) (trans_imp_instr instr map)))
98| IO lin io lout instr ⇒ ?
99].
100cases io qed.
101
102
103let rec All2 (A,B : Type[0]) (f : A → B → Prop) (l1 : list A) (l2 : list B) on l1 : Prop ≝
104match l1 with
105[ nil ⇒ match l2 with [nil ⇒ True | cons _ _ ⇒ False]
106| cons x xs ⇒ match l2 with [nil ⇒ False | cons y ys ⇒ f x y ∧ All2 … f xs ys]
107].
108
109definition environment_rel : (variable → variable) → environment → activation_frame → Prop ≝
110λmap,st1,st2.
111 All … (λx.let 〈v,val〉 ≝ x in read_frame … st2 (to_shift + (map v)) O = return val)
112  st1.
113
114definition store_rel : (variable → variable) → store_t → frame_store_t → Prop ≝
115λmap,st1,st2.
116All2 ?? (λel1,el2.environment_rel map (\fst el1) el2)
117 st1 (\fst st2) ∧ \snd st2 = 〈O,O〉.
118
119lemma store_rel_inv:
120 ∀m,env,var,l,st.
121  store_rel m (〈env,var〉::l) st →
122   ∃hd,tl,fpsp.
123    st = 〈hd::tl,fpsp〉 ∧
124     store_rel m l 〈tl,fpsp〉 ∧
125      environment_rel m env hd.
126 #m #env #var #l * #xx #fpsp * whd in ⊢ (% → ?); cases xx [*] normalize nodelta
127 #frame #frames * #Rel1 #ALL #EQ destruct (skip fpsp)
128 %{frame} %{frames} %{fpsp} % // % // % //
129qed.
130
131definition trans_imp_prog : (variable → variable) → Program imp_env_params imp_instr_params flat_labels →
132Program frame_env_params frame_instr_params flat_labels ≝ λmap,prog.
133mk_Program ???
134(foldr …
135   (λx,tail.(mk_env_item …
136               (mk_signature frame_env_params … (f_name … (f_sig … x)) 14 ?(*(f_ret …(f_sig … x))*))
137               (f_lab … x) (trans_imp_instr (f_body ? imp_instr_params ? x) map)) :: tail) (nil ?) (env … prog))
138(trans_imp_instr (main … prog) map).
139@(f_ret … (f_sig … x))
140qed.
141
142definition trans_imp_cont ≝
143 λmap,cont.
144  foldr … (λx:(ActionLabel flat_labels)×?.λrem.〈\fst x,(trans_imp_instr (\snd x) map)〉 :: rem) (nil ?) cont.
145
146definition imp_variable_pass_rel ≝ 
147λmap : variable → variable.
148λprog : Program imp_state_params imp_state_params imp_state_params.
149let t_prog ≝ trans_imp_prog map prog in
150mk_relations flat_labels (operational_semantics imp_state_params imp_sem_state_params prog)
151 (operational_semantics frame_state_params frame_sem_state_params t_prog)
152(λs1,s2.store_rel map (store … s1) (store … s2) ∧ code … s2 = trans_imp_instr (code … s1) map ∧ 
153cont … s2 = trans_imp_cont map (cont … s1))
154(λ_.λ_.True).
155
156lemma map_exp_ok:
157 ∀m: variable → variable.
158  ∀e: expr.
159  let e' ≝ map_exp e m in
160   ∀env: environment.
161    ∀env': activation_frame.
162     environment_rel m env env' →
163      ∀val.
164       sem_expr env e = Some … val →
165        frame_sem_expr env' e' = Some … val.
166 #m #e elim e
167 [ #v #env elim env
168   [ #env' #_ #val #abs normalize in abs; destruct
169   | * #var #val #tl #IH #env' * #RF #Htl #val'
170     whd in ⊢ (??%? → ?); inversion (?==?) #Hv_var normalize nodelta
171     [ #Hval_val' destruct >(\P Hv_var) assumption
172     | #Hread_tl @IH // assumption ]]
173 | normalize //
174 |*: #e1 #e2 #IH1 #IH2 #env #env' #Rel #v
175   change with (m_bind ?????) in match (sem_expr ??);
176   #H cases (bind_inversion ????? H) -H #v1 * #He1
177   #H cases (bind_inversion ????? H) -H #v2 * #He2
178   #H normalize in H; destruct
179   change with (m_bind ?????) in ⊢ (??%?);
180   >(IH1 … He1) // >(IH2 … He2) // ]
181qed.
182
183lemma map_cond_ok:
184 ∀m: variable → variable.
185  ∀e: condition.
186  let e' ≝ map_cond e m in
187   ∀env: environment.
188    ∀env': activation_frame.
189     environment_rel m env env' →
190      ∀val.
191       sem_condition env e = Some … val →
192        frame_sem_condition env' e' = Some … val.
193 #m #e elim e
194 [ #e1 #e2 #env #env' #Rel #v
195   change with (m_bind ?????) in ⊢ (??%? → ?);
196   #H cases (bind_inversion ????? H) -H #v1 * #He1
197   #H cases (bind_inversion ????? H) -H #v2 * #He2
198   #H normalize in H; destruct
199   change with (m_bind ?????) in ⊢ (??%?);
200   >(map_exp_ok … He1) // >(map_exp_ok … He2) //
201 | #c #IH #env #env' #Rel #v
202   change with (m_bind ?????) in ⊢ (??%? → ?);
203   #H cases (bind_inversion ????? H) -H #v' * #Hv'
204   #H normalize in H; destruct
205   change with (m_bind ?????) in ⊢ (??%?);
206   >(IH … Hv') // ]
207qed.
208
209lemma map_assign_ok:
210 ∀m,env,frame,v,val,env'.
211  environment_rel m env frame →
212   assign_frame frame (to_shift + m v) 0 val = return env' →
213   environment_rel m (assign env v val) env'.
214 #m #env #frame #v #val
215 elim env
216 [ #env' #_ #H % // @(read_frame_assign_frame_hit … H)
217 | * #v' #val' #frames #IH #env' * #Hread #Hall
218   change with (environment_rel ???) in Hall;
219   whd in match (assign ???);
220   inversion (v==v') normalize nodelta
221   [2: #Ineq #H %
222      [ <Hread @(read_frame_assign_frame_miss … H)
223        [ cases daemon
224        | cases daemon
225        | @(not_to_not ??? (\Pf Ineq)) -Ineq #H
226          cases daemon (* MANCA INIETTIVITA' DELLA M *)
227        ]
228      | @IH // ]
229   | #EQ #H %
230     [ @(read_frame_assign_frame_hit … H)
231     |change with (environment_rel ???)
232      cases daemon (* NOT IMPLIED BY Hall BECAUSE OF DUPLICATES :-( *)]]]
233qed.
234
235lemma map_assign_var_ok:
236 ∀m,env,var,tl,v,val,frame,frames,st2,fpsp.
237 store_rel m tl 〈frames,fpsp〉 →
238 environment_rel m env frame →
239  assign_var (〈env,var〉::tl) v val = Some … st2 →
240   ∃st2'.
241    frame_assign_var 〈frame::frames,fpsp〉 (m v) val = Some … st2' ∧
242     store_rel m st2 st2'.
243     cases daemon (* si è rotto!!!
244 #m #env #var #tl #v #val #frame #frames #st2 #fpsp #Rel1 #Rel2
245 whd in ⊢ (??%? → ?); #EQ destruct
246 whd in match frame_assign_var; normalize nodelta % [2: % [%] | skip]
247 cases Rel1 -Rel1 #Rel3 #Rel4 destruct % // whd /6/ *)
248qed.
249
250lemma map_seq_ok:
251 ∀m: variable → variable.
252  ∀e:seq_i.
253  let e' ≝ map_seq e m in
254   ∀st1.
255    ∀st1'.
256     store_rel m st1 st1' →
257      ∀st2.
258       imp_eval_seq e st1 = Some … st2 →
259        ∃st2'.
260         frame_eval_seq e' st1' = Some … st2' ∧
261          store_rel m st2 st2'.
262 #m * [2: #v #expr] #st1 * #st1' * #fp #sp #Rel #st2
263 whd in match imp_eval_seq; normalize nodelta
264 [2: #EQ destruct /3/
265 | cases st1 in Rel; normalize nodelta [ #_ #abs destruct ]
266   * #env #var #tl #Rel
267   cases (store_rel_inv … Rel) -Rel #frame * #frames * #fpsp ** #EQ destruct
268   #Rel1 #Rel2 normalize nodelta #H cases (bind_inversion ????? H) -H #val *
269   #Hval #H whd in match (frame_eval_seq ??); >(map_exp_ok … Rel2 … Hval)
270   normalize nodelta cases (map_assign_var_ok … Rel1 Rel2 … H) #st2' *
271   #H1 #H2 %{st2'} % // assumption ]
272qed.
273
274definition simulation_imp_frame ≝
275λprog : Program imp_state_params imp_state_params imp_state_params.
276λmap : variable → variable.
277λprf : All … (λx.map (f_pars imp_state_params imp_state_params (f_sig … x)) = O) (env … prog).
278mk_simulation_conditions … (imp_variable_pass_rel map prog) ???????????.
279[ #s1 #s2 ** #H1 #H2 #H3 whd in match (as_initial ???); (*store iniziali in relazione*) cases daemon
280| cases daemon
281| cases daemon
282| #s1 #s2 #s1' #H inversion H
283  [ #s11 #s22 * #act #instr #cont #EQcode_s11 #EQcont_s11 #EQ #EQ1 destruct #EQstore #Hio1 #Hio2
284    #act_no_ret #EQ1 #EQ2 #EQ3 destruct #_ ** #Hstore #EQcode #EQcont #_ #_
285    %{(mk_state … (trans_imp_instr (code … s22) map)
286      (trans_imp_cont map (cont … s22)) (store … s2) (io_info … s22))}
287    %{(t_ind … (t_base …))}
288    [2: @(cost_act … (None ?))
289    | @hide_prf whd applyS(empty)
290      [9: >EQcode >EQcode_s11 %
291      |2: normalize % *
292      |3: %
293      |4: cases daemon (*da mettere a posto io *)
294    |5: % |6: % |7: % |8: >EQcont >EQcont_s11 % |*:]
295    | % [ % // % // ] %1 %2 [cases daemon] %1 cases daemon
296    ]
297  |*: cases daemon 
298  ]
299|*: cases daemon
300]
301qed.
Note: See TracBrowser for help on using the repository browser.