source: LTS/Vm.ma @ 3486

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