source: LTS/Vm.ma @ 3482

Last change on this file since 3482 was 3478, checked in by piccolo, 5 years ago

fixed costlabels

File size: 38.4 KB
Line 
1include "costs.ma".
2include "basics/lists/list.ma".
3include "../src/utilities/option.ma".
4include "basics/jmeq.ma".
5
6lemma bind_inversion : ∀A,B : Type[0].∀m : option A.
7∀f : A → option B.∀y : B.
8! x ← m; f x = return y →
9∃ x.(m = return x) ∧ (f x = return y).
10#A #B * [| #a] #f #y normalize #EQ [destruct]
11%{a} %{(refl …)} //
12qed.
13
14record assembler_params : Type[1] ≝
15{ seq_instr : Type[0]
16; jump_condition : Type[0]
17; io_instr : Type[0]
18; entry_of_function : FunctionName → ℕ
19; call_label_fun : list (ℕ × CallCostLabel)
20; return_label_fun : list (ℕ × ReturnPostCostLabel)
21}.
22
23inductive AssemblerInstr (p : assembler_params) : Type[0] ≝
24| Seq : seq_instr p → option (NonFunctionalLabel) →  AssemblerInstr p
25| Ijmp: ℕ → AssemblerInstr p
26| CJump : jump_condition p → ℕ → NonFunctionalLabel → NonFunctionalLabel → AssemblerInstr p
27| Iio : NonFunctionalLabel → io_instr p → NonFunctionalLabel → AssemblerInstr p
28| Icall: FunctionName → AssemblerInstr p
29| Iret: AssemblerInstr p.
30
31record AssemblerProgram (p : assembler_params) : Type[0] ≝
32{ instructions : list (AssemblerInstr p)
33; endmain : ℕ
34; endmain_ok : endmain < |instructions|
35}.
36
37definition fetch: ∀p.AssemblerProgram p → ℕ → option (AssemblerInstr p) ≝
38 λp,l,n. nth_opt ? n (instructions … l).
39
40definition stackT: Type[0] ≝ list (nat).
41
42record sem_params (p : assembler_params) : Type[1] ≝
43{ m : monoid
44; asm_store_type : Type[0]
45; eval_asm_seq : seq_instr p → asm_store_type → option asm_store_type
46; eval_asm_cond : jump_condition p → asm_store_type → option bool
47; eval_asm_io : io_instr p → asm_store_type → option asm_store_type
48; cost_of_io : io_instr p → asm_store_type → m
49; cost_of : AssemblerInstr p → asm_store_type →  m
50}.
51
52record vm_state (p : assembler_params) (p' : sem_params p) : Type[0] ≝
53{ pc : ℕ
54; asm_stack : stackT
55; asm_store : asm_store_type … p'
56; asm_is_io : bool
57; cost : m … p'
58}.
59
60definition label_of_pc ≝ λL.λl.λpc.find …
61   (λp.let 〈x,y〉 ≝ p in if eqb x pc then Some L y else None ? ) l.
62   
63definition option_pop ≝
64  λA.λl:list A. match l with
65  [ nil ⇒ None ?
66  | cons a tl ⇒ Some ? (〈a,tl〉) ].
67
68
69inductive vmstep (p : assembler_params) (p' : sem_params p)
70   (prog : AssemblerProgram p)  :
71      ActionLabel → relation (vm_state p p') ≝
72| vm_seq : ∀st1,st2 : vm_state p p'.∀i,l.
73           fetch … prog (pc … st1) = return (Seq p i l) →
74           asm_is_io … st1 = false →
75           eval_asm_seq p p' i (asm_store … st1) = return asm_store … st2 → 
76           asm_stack … st1 = asm_stack … st2 →
77           asm_is_io … st1 = asm_is_io … st2 →
78           S (pc … st1) = pc … st2 →
79           op … (cost … st1) (cost_of p p' (Seq p i l) (asm_store … st1)) = cost … st2 →
80           vmstep … (cost_act l) st1 st2
81| vm_ijump : ∀st1,st2 : vm_state p p'.∀new_pc : ℕ.
82           fetch … prog (pc … st1) = return (Ijmp p new_pc) →
83           asm_is_io … st1 = false →
84           asm_store … st1 = asm_store … st2 →
85           asm_stack … st1 = asm_stack … st2 →
86           asm_is_io … st1 = asm_is_io … st2 →
87           new_pc = pc … st2 →
88           op … (cost … st1) (cost_of p p' (Ijmp … new_pc) (asm_store … st1)) = cost … st2 →
89           vmstep … (cost_act (None ?)) st1 st2
90| vm_cjump_true :
91           ∀st1,st2 : vm_state p p'.∀cond,new_pc,ltrue,lfalse.
92           eval_asm_cond p p' cond (asm_store … st1) = return true→
93           fetch … prog (pc … st1) = return (CJump p cond new_pc ltrue lfalse) →
94           asm_is_io … st1 = false →
95           asm_store … st1 = asm_store … st2 →
96           asm_stack … st1 = asm_stack … st2 →
97           asm_is_io … st1 = asm_is_io … st2 →
98           pc … st2 = new_pc →
99           op … (cost … st1) (cost_of p p' (CJump p cond new_pc ltrue lfalse) (asm_store … st1)) = cost … st2 →
100           vmstep … (cost_act (Some ? ltrue)) st1 st2
101| vm_cjump_false :
102           ∀st1,st2 : vm_state p p'.∀cond,new_pc,ltrue,lfalse.
103           eval_asm_cond p p' cond (asm_store … st1) = return false→
104           fetch … prog (pc … st1) = return (CJump p cond new_pc ltrue lfalse) →
105           asm_is_io … st1 = false →
106           asm_store … st1 = asm_store … st2 →
107           asm_stack … st1 = asm_stack … st2 →
108           asm_is_io … st1 = asm_is_io … st2 →
109           S (pc … st1) = pc … st2 →
110           op … (cost … st1) (cost_of p p' (CJump … cond new_pc ltrue lfalse) (asm_store … st1)) = cost … st2 →
111           vmstep … (cost_act (Some ? lfalse)) st1 st2
112| vm_io_in : 
113           ∀st1,st2 : vm_state p p'.∀lin,io,lout.
114           fetch … prog (pc … st1) = return (Iio p lin io lout) →
115           asm_is_io … st1 = false →
116           asm_store … st1 = asm_store … st2 →
117           asm_stack … st1 = asm_stack … st2 →
118           true = asm_is_io … st2 →
119           pc … st1 = pc … st2 →
120           cost … st1 = cost … st2 →
121           vmstep … (cost_act (Some ? lin)) st1 st2
122| vm_io_out :
123           ∀st1,st2 : vm_state p p'.∀lin,io,lout.
124           fetch … prog (pc … st1) = return (Iio p lin io lout) →
125           asm_is_io … st1 = true →
126           eval_asm_io … io (asm_store … st1) = return asm_store … st2 →
127           asm_stack … st1 = asm_stack … st2 →
128           false = asm_is_io … st2 →
129           S (pc … st1) = pc … st2 →
130           op … (cost … st1) (cost_of_io p p' io (asm_store … st1)) = cost … st2 →
131           vmstep … (cost_act (Some ? lout)) st1 st2
132| vm_call :
133           ∀st1,st2 : vm_state p p'.∀f,lbl.
134           fetch … prog (pc … st1) = return (Icall p f) →
135           asm_is_io … st1 = false →
136           asm_store … st1 = asm_store … st2 →
137           S (pc … st1) ::  asm_stack … st1 = asm_stack … st2 →
138           asm_is_io … st1 = asm_is_io … st2 →
139           entry_of_function p  f = pc … st2 →
140           op … (cost … st1) (cost_of p p' (Icall p f) (asm_store … st1)) = cost … st2 →
141           label_of_pc ? (call_label_fun p) (entry_of_function p f) = return lbl →
142           vmstep … (call_act f lbl) st1 st2
143| vm_ret :
144          ∀st1,st2 : vm_state p p'.∀newpc,lbl.
145           fetch … prog (pc … st1) = return (Iret p) →
146           asm_is_io … st1 = false →
147           asm_store … st1 = asm_store … st2 →
148           asm_stack … st1 = newpc ::  asm_stack … st2  →
149           asm_is_io … st1 = asm_is_io … st2 →
150           newpc = pc … st2 →
151           label_of_pc ? (return_label_fun p) newpc = return lbl →
152           op … (cost … st1) (cost_of p p' (Iret p) (asm_store … st1)) = cost … st2 →
153           vmstep … (ret_act (Some ? lbl)) st1 st2.
154
155definition eval_vmstate : ∀p : assembler_params.∀p' : sem_params p.
156AssemblerProgram p → vm_state p p' → option (ActionLabel × (vm_state p p')) ≝
157λp,p',prog,st.
158! i ← fetch … prog (pc … st);
159match i with
160[ Seq x opt_l ⇒
161   if asm_is_io … st then
162     None ?
163   else
164     ! new_store ← eval_asm_seq p p' x (asm_store … st);
165     return 〈cost_act opt_l,
166             mk_vm_state ?? (S (pc … st)) (asm_stack … st) new_store false
167                (op … (cost … st) (cost_of p p' (Seq p x opt_l) (asm_store … st)))〉
168| Ijmp newpc ⇒
169   if asm_is_io … st then
170     None ?
171   else
172     return 〈cost_act (None ?),
173             mk_vm_state ?? newpc (asm_stack … st) (asm_store … st) false
174                (op … (cost … st) (cost_of p p' (Ijmp … newpc) (asm_store … st)))〉
175| CJump cond newpc ltrue lfalse ⇒
176   if asm_is_io … st then
177     None ?
178   else
179     ! b ← eval_asm_cond p p' cond (asm_store … st);
180     if b then
181       return 〈cost_act (Some ? ltrue),
182               mk_vm_state ?? newpc (asm_stack … st) (asm_store … st) false
183                (op … (cost … st) (cost_of p p' (CJump … cond newpc ltrue lfalse) (asm_store … st)))〉
184     else
185       return 〈cost_act (Some ? lfalse),
186               mk_vm_state ?? (S (pc … st)) (asm_stack … st) (asm_store … st) false
187                (op … (cost … st) (cost_of p p' (CJump … cond newpc ltrue lfalse) (asm_store … st)))〉
188| Iio lin io lout ⇒
189              if asm_is_io … st then
190                 ! new_store ← eval_asm_io … io (asm_store … st);
191                 return 〈cost_act (Some ? lout),
192                         mk_vm_state ?? (S (pc … st)) (asm_stack … st)
193                         new_store false
194                         (op … (cost … st)
195                               (cost_of_io p p' io (asm_store … st)))〉   
196              else
197                return 〈cost_act (Some ? lin),
198                        mk_vm_state ?? (pc … st) (asm_stack … st) (asm_store … st)
199                                    true (cost … st)〉
200| Icall f ⇒
201    if asm_is_io … st then
202      None ?
203    else
204      ! lbl ← label_of_pc ? (call_label_fun p) (entry_of_function p f);
205      return 〈call_act f lbl,
206              mk_vm_state ?? (entry_of_function p f)
207                             ((S (pc … st)) :: (asm_stack … st))
208                             (asm_store … st) false
209                             (op … (cost … st) (cost_of p p' (Icall p f) (asm_store … st)))〉
210| Iret ⇒ if asm_is_io … st then
211            None ?
212         else
213            ! 〈newpc,tl〉 ← option_pop … (asm_stack … st);
214            ! lbl ← label_of_pc ? (return_label_fun p) newpc;
215            return 〈ret_act (Some ? lbl),
216                    mk_vm_state ?? newpc tl (asm_store … st) false   
217                     (op … (cost … st) (cost_of p p' (Iret p) (asm_store … st)))〉
218].
219
220lemma eval_vmstate_to_Prop : ∀p,p',prog,st1,st2,l.
221eval_vmstate p p' prog st1 = return 〈l,st2〉 → vmstep … prog l st1 st2.
222#p #p' #prog #st1 #st2 #l whd in match eval_vmstate; normalize nodelta
223#H cases(bind_inversion ????? H) -H * normalize nodelta
224[ #seq #opt_l * #EQfetch inversion(asm_is_io ???) normalize nodelta
225  [ #_ whd in ⊢ (??%% → ?); #EQ destruct] #EQio #H cases(bind_inversion ????? H)
226  #newstore * #EQnewstore whd in ⊢ (??%% → ?); #EQ destruct
227  @vm_seq //
228| #newpc * #EQfetch inversion(asm_is_io ???) normalize nodelta #EQio
229  [ whd in ⊢ (??%% → ?); #EQ destruct] whd in ⊢ (??%% → ?); #EQ destruct
230  @vm_ijump //
231| #cond #new_pc #ltrue #lfase * #EQfetch inversion(asm_is_io ???) normalize nodelta
232  [ #_ whd in ⊢ (??%% → ?); #EQ destruct] #EQio #H cases(bind_inversion ????? H) -H
233  * normalize nodelta * #EQcond whd in ⊢ (??%% → ?); #EQ destruct
234  [ @(vm_cjump_true … EQfetch) // | @(vm_cjump_false … EQfetch) //]
235| #lin #io #lout * #EQfetch inversion(asm_is_io ???) normalize nodelta #EQio
236  [ #H cases(bind_inversion ????? H) -H #newstore * #EQnewstore ]
237  whd in ⊢ (??%% → ?); #EQ destruct
238  [ @(vm_io_out … EQfetch) // | @(vm_io_in … EQfetch) // ]
239| #f * #EQfetch inversion(asm_is_io ???) #EQio normalize nodelta
240  [ whd in ⊢ (??%% → ?); #EQ destruct ] #H cases (bind_inversion ????? H) -H
241  #lbl * #EQlb whd in ⊢ (??%% → ?); #EQ destruct @(vm_call … EQfetch) //
242| * #EQfetch inversion(asm_is_io ???) normalize nodelta #EQio
243  [ whd in ⊢ (??%% → ?); #EQ destruct] #H cases(bind_inversion ????? H) -H
244  * #newpc #tl * whd in match option_pop; normalize nodelta inversion(asm_stack ???)
245  normalize nodelta [#_ whd in ⊢ (??%% → ?); #EQ destruct] #newpc1 #tl1 #_
246  #EQstack whd in ⊢ (??%% → ?); #EQ destruct #H cases(bind_inversion ????? H) #lbl
247  * #EQlbl whd in ⊢ (??%% → ?); #EQ destruct @vm_ret //
248]
249qed.
250
251 
252lemma vm_step_to_eval : ∀p,p',prog,st1,st2,l.vmstep … prog l st1 st2 →
253eval_vmstate p p' prog st1 = return 〈l,st2〉.
254#p #p' #prog * #pc1 #stack1 #store1 #io1 #cost1
255* #pc2 #stack2 #store2 #io2 #cost2 #l #H inversion H
256[ #s1 #s2 #i #opt_l #EQfetch #EQio #EQstore #EQstack #EQio1 #EQpc #EQcost
257  #EQ1 #EQ2 #EQ3 #EQ4 destruct whd in match eval_vmstate; normalize nodelta   
258  >EQfetch >m_return_bind normalize nodelta >EQio normalize nodelta >EQstore
259  >m_return_bind <EQio1 >EQio <EQpc >EQstack >EQcost %
260| #s1 #s2 #newpc #EQfetch #EQio1 #EQstore #EQstack #EQio2 #EQnewpc #EQcost
261  #EQ1 #EQ2 #EQ3 #EQ4 destruct whd in match eval_vmstate; normalize nodelta
262  >EQfetch >m_return_bind normalize nodelta >EQio1 normalize nodelta <EQio2 >EQio1
263  >EQstore >EQstack <EQcost >EQstore %
264|3,4: #s1 #s2 #cond #newoc #ltrue #lfalse #EQev_cond #EQfetch #EQio1 #EQstore
265  #EQstack #EQio2 #EQnewoc #EQcost #EQ1 #EQ2 #EQ3 #EQ4 destruct
266  whd in match eval_vmstate; normalize nodelta >EQfetch >m_return_bind
267  normalize nodelta >EQio1 normalize nodelta >EQev_cond >m_return_bind
268  normalize nodelta <EQio1 >EQio2 >EQstore >EQstack <EQcost >EQstore [%] >EQnewoc %
269|5,6: #s1 #s2 #lin #io #lout #EQfetch #EQio1 #EQstore #EQstack #EQio2 #EQpc
270   #EQcost #EQ1 #EQ2 #EQ3 #EQ4 destruct whd in match eval_vmstate;
271   normalize nodelta >EQfetch >m_return_bind normalize nodelta >EQio1
272   normalize nodelta >EQstack <EQpc >EQcost [ >EQstore <EQio2 %]
273   >EQstore >m_return_bind <EQpc <EQio2 %
274| #s1 #s2 #f #lbl #EQfetch #EQio1 #EQstore #EQstack #EQio2 #EQentry
275  #EQcost #EQlab_pc #EQ1 #EQ2 #EQ3 #EQ4 destruct whd in match eval_vmstate;
276  normalize nodelta >EQfetch >m_return_bind normalize nodelta >EQio1
277  normalize nodelta >EQlab_pc >m_return_bind >EQentry >EQcost <EQio2 >EQio1
278  <EQstack >EQstore %
279| #s1 #s2 #newpc #lbl #EQfetch #EQio1 #EQstore #EQstack #EQio2 #EQnewpc
280  #EQlab_pc #EQcosts #EQ1 #EQ2 #EQ3 #EQ4 destruct whd in match eval_vmstate;
281  normalize nodelta >EQfetch >m_return_bind normalize nodelta >EQio1 normalize nodelta
282  >EQstack whd in match option_pop; normalize nodelta >m_return_bind
283  >EQlab_pc >m_return_bind >EQcosts >EQstore  <EQio2 >EQio1 %
284]
285qed.
286
287coercion vm_step_to_eval.
288
289include "../src/utilities/hide.ma".
290
291discriminator option.
292
293definition asm_operational_semantics : ∀p.sem_params p → AssemblerProgram p →  abstract_status ≝
294λp,p',prog.mk_abstract_status
295                (vm_state p p')
296                (λl.λst1,st2 : vm_state p p'.(eqb (pc ?? st1) (endmain … prog)) = false ∧
297                               vmstep p p' prog l st1 st2)
298                (λ_.λ_.True)
299                (λs.match fetch … prog (pc … s) with
300                    [ Some i ⇒ match i with
301                               [ Seq _ _ ⇒ cl_other
302                               | Ijmp _ ⇒ cl_other
303                               | CJump _ _ _ _ ⇒ cl_jump
304                               | Iio _ _ _ ⇒ if asm_is_io … s then cl_io else cl_other
305                               | Icall _ ⇒ cl_other
306                               | Iret ⇒ cl_other
307                               ]
308                    | None ⇒ cl_other
309                    ]
310                )
311                (λ_.true)
312                (λs.eqb (pc ?? s) O)
313                (λs.eqb (pc … s) (endmain … prog))
314                ???.
315@hide_prf
316[ #s1 #s2 #l inversion(fetch ???) normalize nodelta
317  [ #_ #EQ destruct] * normalize nodelta
318  [ #seq #lbl #_
319  | #n #_
320  | #cond #newpc #ltrue #lfalse #EQfetch
321  | #lin #io #lout #_ cases (asm_is_io ??) normalize nodelta
322  | #f #_
323  | #_
324  ]
325  #EQ destruct * #_ #H lapply(vm_step_to_eval … H) whd in match eval_vmstate;
326  normalize nodelta >EQfetch >m_return_bind normalize nodelta cases(asm_is_io ??)
327  normalize nodelta [ whd in ⊢ (??%% → ?); #EQ destruct] #H cases(bind_inversion ????? H) -H
328  * * #_ normalize nodelta whd in ⊢ (??%% → ?); #EQ destruct % //
329| #s1 #s2 #l inversion(fetch ???) normalize nodelta
330  [ #_ #EQ destruct] * normalize nodelta
331  [ #seq #lbl #_
332  | #n #_
333  | #cond #newpc #ltrue #lfalse #_
334  | #lin #io #lout #EQfetch inversion (asm_is_io ??) #EQio normalize nodelta
335  | #f #_
336  | #_
337  ]
338  #EQ destruct * #_ #H lapply(vm_step_to_eval … H) whd in match eval_vmstate;
339  normalize nodelta #H cases(bind_inversion ????? H) -H *
340  [ #seq1 #lbl1
341  | #n1
342  | #cond1 #newpc1 #ltrue1 #lfalse1
343  | #lin1 #io1 #lout1
344  | #f
345  |
346  ]
347  normalize nodelta * #_ cases(asm_is_io ??) normalize nodelta
348  [1,3,5,9,11: whd in ⊢ (??%% → ?); #EQ destruct
349  |2,6,7,10,12: #H cases(bind_inversion ????? H) -H #x * #_
350    [2: cases x normalize nodelta
351    |5: #H cases(bind_inversion ????? H) -H #y * #_
352    ]
353  ]
354  whd in ⊢ (??%% → ?); #EQ destruct destruct % //
355|  #s1 #s2 #l inversion(fetch ???) normalize nodelta
356  [ #_ #EQ destruct] * normalize nodelta
357  [ #seq #lbl #_
358  | #n #_
359  | #cond #newpc #ltrue #lfalse #_
360  | #lin #io #lout #EQfetch inversion(asm_is_io ??) normalize nodelta #EQio
361  | #f #_
362  | #_
363  ]
364  #EQ destruct * #_ #H lapply(vm_step_to_eval … H) whd in match eval_vmstate;
365  normalize nodelta  >EQfetch >m_return_bind normalize nodelta >EQio
366  normalize nodelta #H cases(bind_inversion ????? H) -H #x * #_
367  whd in ⊢ (??%% → ?); #EQ destruct % //
368qed.
369
370definition asm_concrete_trans_sys ≝
371λp,p',prog.mk_concrete_transition_sys …
372             (asm_operational_semantics p p' prog) (m … p') (cost …).
373
374definition emits_labels ≝
375λp.λinstr : AssemblerInstr p.match instr with
376        [ Seq _ opt_l ⇒ match opt_l with
377                        [ None ⇒ Some ? (λpc.S pc)
378                        | Some _ ⇒ None ?
379                        ]
380        | Ijmp newpc ⇒ Some ? (λ_.newpc)
381        | _ ⇒ None ?
382        ].
383
384definition fetch_state : ∀p,p'.AssemblerProgram p → vm_state p p' → option (AssemblerInstr p) ≝
385λp,p',prog,st.fetch … prog (pc … st).
386
387definition asm_static_analisys_data ≝ λp,p',prog,abs_t,instr_m.
388mk_static_analysis_data (asm_concrete_trans_sys p p' prog) abs_t
389 … (fetch_state p p' prog) instr_m.
390
391definition non_empty_list : ∀A.list A → bool ≝
392λA,l.match l with [ nil ⇒ false | _ ⇒  true ].
393
394let rec block_cost (p : assembler_params)
395 (prog: AssemblerProgram p) (abs_t : monoid)
396 (instr_m : AssemblerInstr p → abs_t)
397 (program_counter: ℕ)
398    (program_size: ℕ)
399        on program_size: option abs_t ≝
400match program_size with
401  [ O ⇒ None ?
402  | S program_size' ⇒
403    if eqb program_counter (endmain … prog) then
404       return e … abs_t
405    else
406    ! instr ← fetch … prog program_counter;
407    ! n ← (match emits_labels … instr with
408          [ Some f ⇒ block_cost … prog abs_t instr_m (f program_counter) program_size'
409          | None ⇒ return e …
410          ]);
411    return (op … abs_t (instr_m … instr) n)
412  ].
413
414
415record cost_map_T (dom : DeqSet) (codom : Type[0]) : Type[1] ≝
416{ map_type :> Type[0]
417; empty_map : map_type
418; get_map : map_type → dom → option codom
419; set_map : map_type → dom → codom → map_type
420; get_set_hit : ∀k,v,m.get_map (set_map m k v) k = return v
421; get_set_miss : ∀k1,k2,v,m.(k1 == k2) = false → get_map (set_map m k1 v) k2 = get_map m k2
422}.
423
424definition labels_pc_of_instr : ∀p.AssemblerInstr p → ℕ → list (CostLabel × ℕ) ≝
425λp,i,program_counter.
426match i with
427  [ Seq _ opt_l ⇒ match opt_l with
428                  [ Some lbl ⇒ [〈(a_non_functional_label lbl),S program_counter〉]
429                  | None ⇒ [ ]
430                  ]
431  | Ijmp _ ⇒ [ ]
432  | CJump _ newpc ltrue lfalse ⇒ [〈(a_non_functional_label ltrue),newpc〉;
433                                  〈(a_non_functional_label lfalse),S program_counter〉]
434  | Iio lin _ lout ⇒ [〈(a_non_functional_label lin),program_counter〉;
435                      〈(a_non_functional_label lout),S program_counter〉]
436  | Icall f ⇒ [ ]
437  | Iret ⇒ [ ]
438  ].
439
440let rec labels_pc (p : assembler_params)
441(prog : list (AssemblerInstr p)) (program_counter : ℕ) on prog : list (CostLabel × ℕ) ≝
442match prog with
443[ nil ⇒ [〈i_act,O〉] @
444        map … (λx.let〈y,z〉 ≝ x in 〈(a_call z),y〉) (call_label_fun … p) @
445        map … (λx.let〈y,z〉 ≝ x in 〈(a_return_post z),y〉) (return_label_fun … p)
446| cons i is ⇒ (labels_pc_of_instr … i program_counter)@labels_pc p is (S program_counter)
447].
448
449lemma labels_pc_ok : ∀p,prog,i,lbl,pc,m.
450fetch p prog pc = return i →
451mem ? lbl (labels_pc_of_instr … i (m+pc)) →
452mem ? lbl (labels_pc p (instructions … prog) m).
453#p * #instrs #end_main #Hend_main #i #lbl #pc
454whd in match fetch; normalize nodelta lapply pc -pc
455-end_main elim instrs
456[ #pc #m whd in ⊢ (??%% → ?); #EQ destruct]
457#x #xs #IH * [|#pc'] #m whd in ⊢ (??%% → ?);
458[ #EQ destruct #lbl_addr whd in match (labels_pc ???);
459  /2 by mem_append_l1/
460| #EQ #H2 whd in match (labels_pc ???); @mem_append_l2 @(IH … EQ) //
461]
462qed.
463
464lemma labels_pc_return: ∀p,prog,x1,x2.
465 label_of_pc ReturnPostCostLabel (return_label_fun p) x1=return x2 →
466 ∀m.
467   mem … 〈(a_return_post x2),x1〉 (labels_pc p (instructions p prog) m).
468 #p * #l #endmain #_ whd in match (labels_pc ???); #x1 #x2 #H elim l
469[ #m @mem_append_l2 @mem_append_l2 whd in H:(??%?);
470  elim (return_label_fun ?) in H; [ whd in ⊢ (??%% → ?); #EQ destruct]
471  * #x #y #tl #IH whd in ⊢ (??%? → %); normalize nodelta @eqb_elim
472  normalize nodelta
473  [ #EQ whd in ⊢ (??%% → ?); #EQ2 destruct /2/
474  | #NEQ #H2 %2 @IH // ]
475| #hd #tl #IH #m @mem_append_l2 @IH ]
476qed.
477
478lemma labels_pc_call: ∀p,prog,x1,x2.
479 label_of_pc CallCostLabel (call_label_fun p) x1=return x2 →
480 ∀m.
481   mem … 〈(a_call x2),x1〉 (labels_pc p (instructions p prog) m).
482 #p * #l #endmain #_ whd in match (labels_pc ???); #x1 #x2 #H elim l
483[ #m @mem_append_l2 @mem_append_l1 whd in H:(??%?);
484  elim (call_label_fun ?) in H; [ whd in ⊢ (??%% → ?); #EQ destruct]
485  * #x #y #tl #IH whd in ⊢ (??%? → %); normalize nodelta @eqb_elim
486  normalize nodelta
487  [ #EQ whd in ⊢ (??%% → ?); #EQ2 destruct /2/
488  | #NEQ #H2 %2 @IH // ]
489| #hd #tl #IH #m @mem_append_l2 @IH ]
490qed.
491
492(*
493lemma labels_pc_bounded : ∀p.∀prog : AssemblerProgram p.∀lbl,pc.∀m.
494mem ? 〈lbl,pc〉 (labels_pc p (instructions … prog) m) →
495(m + pc) < (|(instructions … prog)|).
496#p * #instr #endmain #_  #H1 #H2 elim instr
497[ #H3 @⊥ /2/ ] #x #xs #IH #_ #lbl #pc #m whd in match (labels_pc ???);
498#H cases(mem_append ???? H) -H
499[ whd in match labels_pc_of_instr; normalize nodelta
500  cases x normalize nodelta
501  [ #seq * [|#lab]
502  | #newpc
503  | #cond #newpc #ltrue #lfalse
504  | #lin #io #lout
505  | #f
506  |
507  ]
508  normalize [1,3,6,7: *] * [2,4,6: * [2,4:*] ]
509  #EQ destruct
510*) 
511
512let rec m_foldr (M : Monad) (X,Y : Type[0]) (f : X→Y→M Y) (l : list X) (y : Y) on l : M Y ≝
513match l with
514[ nil ⇒ return y
515| cons x xs ⇒ ! z ← m_foldr M X Y f xs y; f x z
516].
517
518definition static_analisys : ∀p : assembler_params.∀abs_t : monoid.(AssemblerInstr p → abs_t) →
519∀mT : cost_map_T DEQCostLabel abs_t.AssemblerProgram p → option mT ≝
520λp,abs_t,instr_m,mT,prog.
521let prog_size ≝ S (|instructions … prog|) in
522m_foldr Option ?? (λx,m.let 〈z,w〉≝ x in ! k ← block_cost p prog abs_t instr_m w prog_size; 
523                               return set_map … m z k) (labels_pc … (instructions … prog) O)
524      (empty_map ?? mT).
525     
526
527include "basics/lists/listb.ma".
528
529(*doppione da mettere a posto*)
530let rec no_duplicates (A : DeqSet) (l : list A) on l : Prop ≝
531match l with
532[ nil ⇒ True
533| cons x xs ⇒ ¬ (bool_to_Prop (x ∈ xs)) ∧ no_duplicates … xs
534].
535
536definition asm_no_duplicates ≝
537λp,prog.no_duplicates … (map ?? \fst … (labels_pc … (instructions p prog) O)).
538
539(*falso: necessita di no_duplicates*)
540
541definition eq_deq_prod : ∀D1,D2 : DeqSet.D1 × D2 → D1 × D2 → bool ≝
542λD1,D2,x,y.\fst x == \fst y ∧ \snd x == \snd y.
543
544definition DeqProd ≝ λD1 : DeqSet.λD2 : DeqSet.
545mk_DeqSet (D1 × D2) (eq_deq_prod D1 D2) ?.
546@hide_prf
547* #x1 #x2 * #y1 #y2 whd in match eq_deq_prod; normalize nodelta
548% [2: #EQ destruct @andb_Prop >(\b (refl …)) %]
549inversion (? ==?) #EQ1 whd in match (andb ??); #EQ2 destruct
550>(\P EQ1) >(\P EQ2) %
551qed.
552(*
553unification hint  0 ≔ D1,D2 ;
554    X ≟ DeqProd D1 D2
555(* ---------------------------------------- *) ⊢
556    D1 × D2 ≡ carr X.
557
558
559unification hint  0 ≔ D1,D2,p1,p2;
560    X ≟ DeqProd D1 D2
561(* ---------------------------------------- *) ⊢
562    eq_deq_prod D1 D2 p1 p2 ≡ eqb X p1 p2.
563   
564definition deq_prod_to_prod : ∀D1,D2 : DeqSet.DeqProd D1 D2 → D1 × D2 ≝
565λD1,D2,x.x.
566
567coercion deq_prod_to_prod.
568*)
569
570lemma map_mem : ∀A,B,f,l,a.mem A a l → ∃b : B.mem B b (map A B f l)
571∧ b = f a.
572#A #B #f #l elim l [ #a *] #x #xs #IH #a *
573[ #EQ destruct %{(f x)} % // % // | #H cases(IH … H)
574  #b * #H1 #EQ destruct %{(f a)} % // %2 //
575]
576qed.
577
578lemma static_analisys_ok : ∀p,abs_t,instr_m,mT,prog,lbl,pc,map.
579asm_no_duplicates p prog →
580static_analisys p abs_t instr_m mT prog = return map →
581mem … 〈lbl,pc〉 (labels_pc … (instructions … prog) O) →
582get_map … map lbl =
583block_cost … prog abs_t instr_m pc (S (|(instructions … prog)|)) ∧
584block_cost … prog abs_t instr_m pc (S (|(instructions … prog)|)) ≠ None ?.
585#p #abs_t #instr_m #mT * #prog whd in match static_analisys; normalize nodelta
586whd in match asm_no_duplicates; normalize nodelta lapply (labels_pc ???)
587#l #endmain #Hendmain elim l [ #x #y #z #w #h * ]
588* #hd1 #hd2 #tl #IH #lbl #pc #map * #H1 #H2 #H
589cases(bind_inversion ????? H) -H #map1 * #EQmap1 normalize nodelta #H
590cases(bind_inversion ????? H) -H #elem * #EQelem whd in ⊢ (??%% → ?); #EQ
591destruct *
592[ #EQ destruct % [ >get_set_hit >EQelem % | >EQelem % whd in ⊢ (??%% → ?); #EQ destruct]
593| #H %
594  [ >get_set_miss [ @(proj1 … (IH …)) //] inversion (? == ?) [2: #_ %]
595    #ABS cases H1 -H1 #H1 @⊥ @H1 >(\P ABS) >mem_to_memb //
596    cases(map_mem … \fst … H) #z1 * #Hz1 #EQ destruct @Hz1
597  | @(proj2 … (IH …)) //
598  ]
599]
600qed.
601
602include "Simulation.ma".
603
604record terminated_trace (S : abstract_status) (R_s2 : S) : Type[0] ≝
605{  R_post_step : option (ActionLabel × S)
606 ; R_fin_ok : match R_post_step with
607               [ None ⇒ bool_to_Prop (as_final … R_s2)
608               | Some x ⇒ let 〈l,R_post_state〉≝ x in
609                          (is_costlabelled_act l ∨ is_labelled_ret_act l) ∧
610                          (is_call_act l → bool_to_Prop (is_call_post_label … R_s2)) ∧
611                          as_execute … l R_s2 R_post_state
612               ]
613 }.
614
615definition big_op: ∀M: monoid. list M → M ≝
616 λM. foldl … (op … M) (e … M).
617
618lemma big_op_associative:
619 ∀M:monoid. ∀l1,l2.
620  big_op M (l1@l2) = op M (big_op … l1) (big_op … l2).
621 #M #l1 whd in match big_op; normalize nodelta
622 generalize in match (e M) in ⊢ (? → (??%(??%?))); elim l1
623 [ #c #l2 whd in match (append ???); normalize lapply(neutral_r … c)
624   generalize in match c in ⊢ (??%? → ???%); lapply c -c lapply(e M)
625   elim l2 normalize
626   [ #c #c1 #c2 #EQ @sym_eq //
627   | #x #xs #IH #c1 #c2 #c3 #EQ <EQ <IH [% | <is_associative % |]
628   ]
629 | #x #xs #IH #c #l2 @IH
630 ]
631qed.
632
633lemma act_big_op : ∀M,B. ∀act : monoid_action M B.
634 ∀l1,l2,x.
635   act (big_op M (l1@l2)) x = act (big_op … l2) (act (big_op … l1) x).
636 #M #B #act #l1 elim l1
637 [ #l2 #x >act_neutral //
638 | #hd #tl #IH #l2 #x change with ([?]@(tl@l2)) in match ([?]@(tl@l2));
639   >big_op_associative >act_op >IH change with ([hd]@tl) in match ([hd]@tl);
640   >big_op_associative >act_op in ⊢ (???%); %
641 ]
642qed.
643
644lemma monotonicity_of_block_cost : ∀p,prog,abs_t,instr_m,pc,size,k.
645block_cost p prog abs_t instr_m pc size = return k →
646∀size'.size ≤ size' →
647block_cost p prog abs_t instr_m pc size' = return k.
648#p #prog #abs_t #instr_m #pc #size lapply pc elim size
649[ #pc #k whd in ⊢ (??%% → ?); #EQ destruct]
650#n #IH #pc #k whd in ⊢ (??%% → ?); @eqb_elim
651[ #EQ destruct normalize nodelta whd in ⊢ (??%% → ?); #EQ destruct
652  #size' * [2: #m #_] whd in ⊢ (??%%); @eqb_elim try( #_ %) * #H @⊥ @H %
653| #Hpc normalize nodelta #H cases(bind_inversion ????? H) -H #i
654  * #EQi #H cases(bind_inversion ????? H) -H #elem * #EQelem whd in ⊢ (??%% → ?);
655  #EQ destruct #size' *
656  [ whd in ⊢ (??%?); @eqb_elim
657    [ #EQ @⊥ @(absurd ?? Hpc) assumption ]
658    #_ normalize nodelta >EQi >m_return_bind >EQelem %
659  | #m #Hm whd in ⊢ (??%?); @eqb_elim
660    [ #EQ @⊥ @(absurd ?? Hpc) assumption ]
661    #_ normalize nodelta >EQi >m_return_bind
662    cases (emits_labels ??) in EQelem; normalize nodelta
663    [ whd in ⊢ (??%%→ ??%%); #EQ destruct %]
664    #f #EQelem >(IH … EQelem) [2: /2/ ] %
665  ]
666]
667qed.
668
669lemma step_emit : ∀p,p',prog,st1,st2,l,i.
670fetch p prog (pc … st1) = return i →
671eval_vmstate p p' … prog st1 = return 〈l,st2〉 → 
672emits_labels … i = None ? → ∃x.
673match l in ActionLabel return λ_:ActionLabel.(list CostLabel) with 
674[call_act f c ⇒ [a_call c]
675|ret_act x ⇒
676   match x with [None⇒[]|Some c⇒[a_return_post c]]
677|cost_act x ⇒
678   match x with [None⇒[]|Some c⇒[a_non_functional_label c]]
679] = [x] ∧
680 (mem … 〈x,pc … st2〉 (labels_pc p (instructions … prog) O)).
681#p #p' #prog #st1 #st2 #l #i #EQi whd in match eval_vmstate; normalize nodelta
682>EQi >m_return_bind normalize nodelta cases i in EQi; -i normalize nodelta
683[ #seq * [|#lab]
684| #newpc
685| #cond #newpc #ltrue #lfalse
686| #lin #io #lout
687| #f
688|
689]
690#EQi cases(asm_is_io ???) normalize nodelta
691[1,3,5,7,11,13: whd in ⊢ (??%% → ?); #EQ destruct
692|2,4,8,9,12,14: #H cases(bind_inversion ????? H) -H #x1 * #EQx1
693  [3: cases x1 in EQx1; -x1 #EQx1 normalize nodelta
694  |6: #H cases(bind_inversion ????? H) -H #x2 * #EQx2
695  ]
696]
697whd in ⊢ (??%% → ?); #EQ destruct whd in match emits_labels;
698normalize nodelta #EQ destruct % [2,4,6,8,10,12,14: % try % |*:]
699[1,2,4,5,7: @(labels_pc_ok … EQi) normalize /3 by or_introl,or_intror/ ]
700/2 by labels_pc_return, labels_pc_call/
701qed.
702
703lemma step_non_emit : ∀p,p',prog,st1,st2,l,i,f.
704fetch p prog (pc … st1) = return i →
705eval_vmstate p p' … prog st1 = return 〈l,st2〉 → 
706emits_labels … i = Some ? f →
707match l in ActionLabel return λ_:ActionLabel.(list CostLabel) with 
708[call_act f c ⇒ [a_call c]
709|ret_act x ⇒
710   match x with [None⇒[]|Some c⇒[a_return_post c]]
711|cost_act x ⇒
712   match x with [None⇒[]|Some c⇒[a_non_functional_label c]]
713] = [ ] ∧ pc … st2 = f (pc … st1).
714#p #p' #prog #st1 #st2 #l #i #f #EQi whd in match eval_vmstate; normalize nodelta
715>EQi >m_return_bind normalize nodelta cases i in EQi; -i normalize nodelta
716[ #seq * [|#lab]
717| #newpc
718| #cond #newpc #ltrue #lfalse
719| #lin #io #lout
720| #f
721|
722]
723#EQi cases(asm_is_io ???) normalize nodelta
724[1,3,5,7,11,13: whd in ⊢ (??%% → ?); #EQ destruct
725|2,4,8,9,12,14: #H cases(bind_inversion ????? H) -H #x1 * #EQx1
726  [3: cases x1 in EQx1; -x1 #EQx1 normalize nodelta
727  |6: #H cases(bind_inversion ????? H) -H #x2 * #EQx2
728  ]
729]
730whd in ⊢ (??%% → ?); #EQ destruct whd in match emits_labels;
731normalize nodelta #EQ destruct /2 by refl, conj/
732qed.
733
734lemma labels_of_trace_are_in_code :
735∀p,p',prog.∀st1,st2 :  vm_state p p'.∀t : raw_trace (asm_operational_semantics p p' prog) … st1 st2.
736∀lbl.
737mem … lbl (get_costlabels_of_trace … t) →
738mem … (lbl) (map ?? \fst … (labels_pc … (instructions p prog) O)).
739#p #p' #prog #st1 #st2 #t elim t
740[ #st #lbl *
741| #st1 #st2 #st3 #l * #H1 #H2 #tl #IH #lbl whd in match (get_costlabels_of_trace ????);
742  #H cases(mem_append ???? H) -H [2: #H @IH //]
743  lapply(vm_step_to_eval … H2) whd in match eval_vmstate;
744  normalize nodelta #H cases(bind_inversion ????? H) -H #i * #EQi #_
745  inversion(emits_labels … i)
746  [ #EQemit cases(step_emit … (vm_step_to_eval … H2)) // #x * #EQ1 #EQ2 >EQ1 *
747    [2: *] #EQ destruct cases(map_mem … \fst … EQ2) #y * #H3 #EQ destruct //
748  | #f #EQemit >(proj1 … (step_non_emit … EQi (vm_step_to_eval … H2) … EQemit))
749    *
750  ]
751
752qed.
753
754let rec dependent_map (A,B : Type[0]) (l : list A) (f : ∀a : A.mem … a l → B) on l : list B ≝
755(match l return λx.l=x → ? with
756[ nil ⇒ λ_.nil ?
757| cons x xs ⇒ λprf.(f x ?) :: dependent_map A B xs (λx,prf1.f x ?)
758])(refl …).
759[ >prf %% | >prf %2 assumption]
760qed.
761
762lemma dependent_map_append : ∀A,B,l1,l2,f.
763dependent_map A B (l1 @ l2) (λa,prf.f a prf) =
764(dependent_map A B l1 (λa,prf.f a ?)) @ (dependent_map A B l2 (λa,prf.f a ?)).
765[2: @hide_prf /2/ | 3: @hide_prf /2/]
766#A #B #l1 elim l1 normalize /2/
767qed.
768
769lemma rewrite_in_dependent_map : ∀A,B,l1,l2,f.
770        ∀EQ:l1 = l2.
771         dependent_map A B l1 (λa,prf.f a prf) =
772         dependent_map A B l2 (λa,prf.f a ?).
773[2: >EQ // | #A #B #l1 #l2 #f #EQ >EQ in f; #f % ]
774qed.
775
776
777lemma static_dynamic : ∀p,p',prog.∀no_dup : asm_no_duplicates p prog.
778∀abs_t : abstract_transition_sys (m … p').∀instr_m.
779∀good : static_galois … (asm_static_analisys_data p p' prog abs_t instr_m).∀mT,map1.
780∀EQmap : static_analisys p ? instr_m mT prog = return map1.
781∀st1,st2 : vm_state p p'.
782∀ter : terminated_trace (asm_operational_semantics p p' prog) st2.
783∀t : raw_trace (asm_operational_semantics p p' prog) … st1 st2.
784∀k.
785pre_measurable_trace … t →
786block_cost p prog … instr_m (pc … st1) (S (|(instructions … prog)|)) = return k →
787∀a1.rel_galois … good st1 a1 →
788let stop_state ≝ match R_post_step … ter with [None ⇒ st2 | Some x ⇒ \snd x ] in
789∀costs.
790costs = dependent_map CostLabel ? (get_costlabels_of_trace …  t) (λlbl,prf.(opt_safe … (get_map … map1 lbl) ?)) →
791rel_galois … good stop_state (act_abs … abs_t (big_op … costs) (act_abs … abs_t k a1)).
792[2: @hide_prf
793    cases(mem_map ????? (labels_of_trace_are_in_code … prf)) *
794    #lbl' #pc * #Hmem #EQ destruct   
795    >(proj1 … (static_analisys_ok … no_dup … EQmap … Hmem))
796    @(proj2 … (static_analisys_ok … no_dup … EQmap … Hmem))
797]
798#p #p' #prog #no_dup #abs_t #instr_m #good #mT #map1 #EQmap #st1 #st2 #ter #t
799lapply ter -ter elim t
800[ #st #ter inversion(R_post_step … ter) normalize nodelta [| * #lbl #st3 ]
801  #EQr_post #k #_ lapply(R_fin_ok … ter) >EQr_post normalize nodelta
802  [ whd in ⊢ (?% → ?); #final_st whd in ⊢ (??%? → ?); >final_st
803    normalize nodelta whd in ⊢ (??%% → ?); #EQ destruct(EQ)
804    #a1 #rel_st_a1 whd in match (map ????); #costs #EQ >EQ >act_neutral >act_neutral assumption
805  | ** #H1 #H2 * #H3 #H4 whd in ⊢ (??%% → ?); >H3 normalize nodelta
806    #H cases(bind_inversion ????? H) -H *
807    [ #seq * [|#lbl1]
808    | #newpc
809    | #cond #newpc #ltrue #lfalse
810    | #lin #io #lout
811    | #f
812    |
813    ]
814    * #EQfetch lapply(vm_step_to_eval … H4) whd in match eval_vmstate;
815    normalize nodelta >EQfetch >m_return_bind normalize nodelta
816    cases(asm_is_io ??) normalize nodelta
817    [1,3,5,7,11,13: whd in ⊢ (??%% → ?); #EQ destruct
818    |2,4,8,9,12,14: #H cases(bind_inversion ????? H) -H #x * #_
819      [3: cases x normalize nodelta
820      |6: #H cases(bind_inversion ????? H) -H #y * #_
821      ]
822    ]
823    whd in ⊢ (??%% → ?); #EQ destruct
824    [4,8: cases H1 [1,3: * |*: * #y #EQ destruct]]
825    >m_return_bind whd in ⊢ (??%% → ?); #EQ destruct
826    #a1 #good_st_a1 whd in match (dependent_map ????); #costs #EQ >EQ >neutral_r
827    >act_neutral
828    @(instr_map_ok … good … EQfetch … good_st_a1) /2/
829  ]
830| -st1 -st2 #st1 #st2 #st3 #l * #H3 #H4 #tl #IH #ter #k #H
831  #H2 #a1 #good_a1 whd in match (get_costlabels_of_trace ????);
832  whd #costs >dependent_map_append
833  (*change with (list_cost_to_list_initcost ?) in match (list_cost_to_list_initcost ?);*) 
834  #EQ destruct whd in H2 : (??%?); lapply H2 >H3 in ⊢ (% → ?); -H2
835  normalize nodelta #H cases(bind_inversion ????? H) -H #i * #EQi
836  inversion(emits_labels ??)
837  [ #EQemits whd in ⊢ (??%% → ?); #EQ destruct  >big_op_associative >act_op @IH
838  | #f #EQemits normalize nodelta #H
839    cases(bind_inversion ????? H) -H #k' * #EQk' whd in ⊢ (??%% → ?);
840    #EQ destruct(EQ) >act_op whd in match (append ???); @IH
841  ]
842  [1,5: inversion H in ⊢ ?;
843    [1,6: #st #c #EQ1 #EQ2 #EQ3 #EQ4 destruct
844    |2,7: #s1 #s2 #s3 #lbl #exe #tl1 #s1_noio * #opt_l #EQ destruct #pre_tl1 #_
845      #EQ1 #EQ2 #EQ3 #EQ4 destruct
846    |3,8: #s1 #s2 #s3 #lbl #s1_noio #exe #tl1 * #lbl1 #EQ destruct #pre_tl1 #_
847      #EQ1 #EQ2 #EQ3 #EQ4 destruct
848    |4,9: #s1 #s2 #s3 #lbl #exe #tl1 #s1_noio * #f * #lbl1 #EQ destruct
849      * #pre_tl1 #_ #EQ1 #EQ2 #EQ3 #EQ4 destruct
850    |5,10: #s1 #s2 #s3 #s4 #s5 #l1 #l2 #exe1 #t1 #t2 #exe2 #noio1 #noio2 #H *
851    ] //
852  | cases(step_emit … EQi (vm_step_to_eval … H4) … EQemits) #x * #EQx #Hx
853    >(rewrite_in_dependent_map … EQx)
854 
855    whd in match (dependent_map ????); whd in match (dependent_map ????); whd in match (big_op ??);
856    >neutral_l @opt_safe_elim #elem #EQget
857    cases (static_analisys_ok  … x … (pc … st2) … no_dup … EQmap) //
858    #EQ #_ <EQ assumption
859  | >neutral_r  @(instr_map_ok … good … EQi … good_a1) /2/
860  | %
861  | >(proj2 … (step_non_emit … EQi (vm_step_to_eval … H4) … EQemits))
862    >(monotonicity_of_block_cost … EQk') //
863  | @(instr_map_ok … good … EQi … good_a1) /2/
864  | lapply (proj1 … (step_non_emit … EQi (vm_step_to_eval … H4) … EQemits))
865    #EQ >(rewrite_in_dependent_map … EQ) %
866  ]
867]
868qed.
869
870definition actionlabel_to_costlabel : ActionLabel → list CostLabel ≝
871λa.match a with
872[ call_act f l ⇒ [a_call l]
873| ret_act opt_l ⇒ match opt_l with [None ⇒ [ ] | Some l ⇒ [a_return_post l]]
874| cost_act opt_l ⇒ match opt_l with [None ⇒ [ ] | Some l ⇒ [a_non_functional_label l]]
875].
876
877definition get_costlabels_of_measurable_trace : ∀S : abstract_status.measurable_trace S → list CostLabel ≝
878λS,t.
879 match L_label … t with
880 [ None ⇒ [i_act]
881 | Some l ⇒ actionlabel_to_costlabel l
882 ] @
883 get_costlabels_of_trace … (trace … t).
884
885(*get_costlabels_of_measurable_trace deve aggiungere la label iniziale.*)
886theorem static_dynamic_meas : ∀p,p',prog.∀no_dup : asm_no_duplicates p prog.
887∀abs_t : abstract_transition_sys (m … p').∀instr_m.
888∀good : static_galois … (asm_static_analisys_data p p' prog abs_t instr_m).∀mT,map1.
889∀EQmap : static_analisys p ? instr_m mT prog = return map1.
890∀t : measurable_trace (asm_operational_semantics p p' prog).
891∀a1.rel_galois … good (L_s1 … t) a1 →
892let stop_state ≝ match R_post_state … t with [None ⇒ R_s2 … t | Some x ⇒ x ] in
893∀abs_trace.
894abs_trace = dependent_map CostLabel ? (get_costlabels_of_measurable_trace …  t) (λlbl,prf.(opt_safe … (get_map … map1 lbl) ?)) →
895rel_galois … good stop_state (act_abs … abs_t (big_op … abs_trace) a1).
896[2: @hide_prf
897    whd in match get_costlabels_of_measurable_trace in prf; normalize nodelta in prf;
898    cases(mem_append ???? prf) -prf
899    [2: #prf cases(mem_map ????? (labels_of_trace_are_in_code … prf)) *
900        #lbl' #pc * #Hmem #EQ destruct   
901        >(proj1 … (static_analisys_ok … no_dup … EQmap … Hmem))
902        @(proj2 … (static_analisys_ok … no_dup … EQmap … Hmem))
903    | inversion(L_label … t) normalize nodelta [| #lbl'] #EQleft
904      [ * [2: *] #EQ destruct cases daemon (* i_act sta sempre nel codice *)
905      | (* per inversione su as_execute *) cases daemon
906      ]
907    ]
908]
909#p #p' #prog #no_dup #abs_t #instr_m #good #mT #map1 #EQmap1 #t #a1 #rel_galois #abs_trace #EQabs_trace
910xxxxxx
911lapply(static_dynamic … (trace …t) … abs_trace) try //
912
913
Note: See TracBrowser for help on using the repository browser.