source: src/joint/Traces.ma @ 2547

Last change on this file since 2547 was 2538, checked in by tranquil, 7 years ago

fixed Traces.ma after changes in joint/semantics.ma

File size: 10.2 KB
Line 
1include "joint/semantics.ma".
2include "common/StructuredTraces.ma".
3
4record evaluation_params : Type[1] ≝
5 { globals: list ident
6 ; sparams:> sem_params
7 ; exit: program_counter
8 ; ev_genv: genv sparams globals
9(* ; io_env : state sparams → ∀o:io_out.res (io_in o) *)
10 }.
11 
12
13
14record prog_params : Type[1] ≝
15 { prog_spars : sem_params
16 ; prog : joint_program prog_spars
17 ; stack_sizes : ident → option ℕ
18(* ; prog_io : state prog_spars → ∀o.res (io_in o) *)
19 }.
20
21lemma map_Exists : ∀A,B,f,P,l.Exists B P (map A B f l) → Exists ? (λx.P (f x)) l.
22#A #B #f #P #l elim l -l [*]
23#hd #tl #IH *
24[ #Phd %1{Phd}
25| #Ptl %2{(IH Ptl)}
26]
27qed.
28
29lemma Exists_In : ∀A,P,l.Exists A P l → ∃x.In A l x ∧ P x.
30#A #P #l elim l -l [*] #hd #tl #IH *
31[ #Phd %{hd} %{Phd} %1 %
32| #Ptl elim (IH Ptl) #x * #H1 #H2 %{x} %{H2} %2{H1}
33]
34qed.
35
36lemma In_Exists : ∀A,P,l,x.In A l x → P x → Exists A P l.
37#A #P #l elim l -l [ #x *] #hd #tl #IH #x *
38[ #EQ >EQ #H %1{H}
39| #Intl #Px %2{(IH … Intl Px)}
40]
41qed.
42
43lemma Exists_mp : ∀A,P,Q,l.(∀x.P x → Q x) → Exists A P l → Exists A Q l.
44#A #P #Q #l #H elim l -l [*] #hd #tl #IH * #G [ %1 | %2 ] /2/ qed.
45
46definition make_global : prog_params → evaluation_params
47
48λpars.
49(* Invariant: a -1 block is unused in common/Globalenvs *)
50let b ≝ mk_block Code (-1) in
51let ptr ≝ mk_program_counter «b, refl …» one in
52let p ≝ prog pars in
53mk_evaluation_params
54  (prog_var_names … p)
55  (prog_spars pars)
56  ptr
57  (mk_genv_gen ?? (globalenv_noinit ? p) ? (stack_sizes pars))
58 (* (prog_io pars) *).
59#s #H
60elim (find_symbol_exists … p s ?)
61[ #bl #EQ >EQ % #ABS destruct(ABS)|*:]
62@Exists_append_r
63@(Exists_mp … H) //
64qed.
65
66coercion prog_params_to_ev_params : ∀p : prog_params.evaluation_params
67≝ make_global on _p : prog_params to evaluation_params.
68
69axiom BadMain : String.
70
71definition make_initial_state :
72 ∀pars: prog_params.res (state_pc pars) ≝
73λpars.let p ≝ prog pars in
74  let sem_globals : evaluation_params ≝ pars in
75  let ge ≝ ev_genv sem_globals in
76  let m ≝ alloc_mem … p in
77  let 〈m,spb〉 ≝ alloc … m 0 external_ram_size XData in
78  let 〈m,ispb〉 ≝ alloc … m 0 internal_ram_size XData in
79  let dummy_pc ≝ mk_program_counter «mk_block Code (-1), refl …» one in
80  let spp : xpointer ≝
81    «mk_pointer spb (mk_offset (bitvector_of_Z ? external_ram_size)),
82     pi2 … spb» in
83(*  let ispp : xpointer ≝ mk_pointer ispb (mk_offset (bitvector_of_nat ? 47)) in *)
84  let main ≝ prog_main … p in
85  let st0 ≝ mk_state pars (empty_framesT …) empty_is (BBbit false) (empty_regsT … spp) m in
86  (* use exit sem_globals as ra and call_dest_for_main as dest *)
87  let st0' ≝ mk_state_pc … (set_sp … spp st0) (exit sem_globals) in
88  ! st0'' ← save_frame ?? sem_globals true (call_dest_for_main … pars) st0' ;
89  let st_pc0 ≝ mk_state_pc ? st0'' dummy_pc in
90  ! bl ← block_of_call … ge (inl … main) st_pc0;
91  ! 〈i, fn〉 ← fetch_function … ge bl ;
92  match fn with
93  [ Internal ifn ⇒
94    ! st' ← eval_internal_call … ge i ifn (call_args_for_main … pars) st_pc0 ;
95    let pc' ≝ pc_of_point … bl (joint_if_entry … ifn) in
96    return mk_state_pc … st' pc'
97  | External _ ⇒ Error … [MSG BadMain; CTX ? main] (* External main not supported: why? *)
98  ].
99
100definition joint_classify_seq :
101  ∀p : evaluation_params.state p → joint_seq p (globals … p) → status_class ≝
102  λp,st,stmt.
103  match stmt with
104  [ CALL f args dest ⇒
105    match (! bl ← block_of_call … (ev_genv p) f st ;
106            fetch_function … (ev_genv p) bl) with
107    [ OK id_fn ⇒
108      match \snd id_fn with
109      [ Internal _ ⇒ cl_call
110      | External _ ⇒ cl_other
111      ]
112    | Error _ ⇒ cl_other
113    ]
114  | _ ⇒ cl_other
115  ].
116
117definition joint_classify_step :
118  ∀p : evaluation_params.state p → joint_step p (globals … p) → status_class ≝
119  λp,st,stmt.
120  match stmt with
121  [ step_seq s ⇒ joint_classify_seq p st s
122  | COND _ _ ⇒ cl_jump
123  ].
124
125definition joint_classify_final :
126  ∀p : evaluation_params.joint_fin_step p → status_class ≝
127  λp,stmt.
128  match stmt with
129  [ GOTO _ ⇒ cl_other
130  | RETURN ⇒ cl_return
131  | TAILCALL _ _ _ ⇒ cl_other (* this needs tailcalls implemented in structured traces *)
132  ].
133
134definition joint_classify :
135  ∀p : evaluation_params.state_pc p → status_class ≝
136  λp,st.
137  match fetch_statement … (ev_genv p) (pc … st) with
138  [ OK i_f_s ⇒
139    match \snd i_f_s with
140    [ sequential s _ ⇒ joint_classify_step p st s
141    | final s ⇒ joint_classify_final p s
142    | FCOND _ _ _ _ ⇒ cl_jump
143    ]
144  | Error _ ⇒ cl_other
145  ].
146
147definition no_call : ∀p,g.joint_seq p g → Prop ≝
148  λp,g,s.match s with
149  [ CALL _ _ _ ⇒ False
150  | _ ⇒ True
151  ].
152
153definition no_cost_label : ∀p,g.joint_seq p g → Prop ≝
154  λp,g,s.match s with
155  [ COST_LABEL _ ⇒ False
156  | _ ⇒ True
157  ].
158
159lemma no_call_advance : ∀p : evaluation_params.
160∀s,nxt.∀st : state_pc p.
161no_call p (globals p) s →
162eval_seq_advance p (globals p) (ev_genv p) s nxt st = return next … nxt st.
163#p * // #f #args #dest #nxt #st *
164qed.
165
166lemma no_call_other : ∀p : evaluation_params.∀st,s.
167no_call p (globals p) s →
168joint_classify_seq … st s = cl_other.
169#p #st * // #f #args #dest *
170qed.
171
172lemma cond_call_other :
173  ∀p,globals.∀P : joint_step p globals → Prop.
174  (∀a,lbltrue.P (COND … a lbltrue)) →
175  (∀f,args,dest.P (CALL … f args dest)) →
176  (∀s.no_call ?? s → P s) →
177  ∀s.P s.
178#p #globals #P #H1 #H2 #H3 * // * /2 by / qed.
179
180lemma joint_classify_call : ∀p : evaluation_params.∀st.
181  joint_classify p st = cl_call →
182  ∃i_f,f',args,dest,next,bl,i',fd'.
183  fetch_statement … (ev_genv p) (pc … st) =
184    OK ? 〈i_f, sequential … (CALL … f' args dest) next〉 ∧
185  block_of_call … (ev_genv p) f' st = OK ? bl ∧
186  fetch_internal_function … (ev_genv p) bl = OK ? 〈i', fd'〉.
187#p #st
188whd in match joint_classify; normalize nodelta
189inversion (fetch_statement … (ev_genv p) (pc … st)) normalize nodelta
190[2: #e #_ #ABS destruct(ABS) ]
191* #i_f * [2,3: [ * [ #lbl | | #fl #f #args ] | #fl #a #ltrue #lfalse ] #_
192  normalize in ⊢ (%→?); #ABS destruct(ABS) ]
193@cond_call_other
194[ #a #lbl #nxt #_ normalize in ⊢ (%→?); #ABS destruct(ABS)
195|3: #s #no_call #nxt #_ whd in match joint_classify_step;
196  normalize nodelta >(no_call_other … no_call) #ABS destruct(ABS)
197]
198#f' #args #dest #nxt #fetch
199whd in match joint_classify_step; whd in match joint_classify_seq;
200normalize nodelta
201inversion (block_of_call ?????)
202[ #bl #block_of_c  >m_return_bind
203  inversion (fetch_function ???)
204  [ * #i' *
205    [ #fd' #fetch' #_
206      %{i_f} %{f'} %{args} %{dest} %{nxt} %{bl} %{i'} %{fd'}
207      % [ %{block_of_c} % ]
208      whd in match fetch_internal_function; normalize nodelta
209      >fetch' %
210    ]
211  ]
212]
213#x #_ normalize in ⊢ (%→?); #ABS destruct(ABS)
214qed.
215
216definition joint_after_ret : ∀p:evaluation_params.
217  (Σs : state_pc p.joint_classify p s = cl_call) → state_pc p → Prop ≝
218λp,s1,s2.
219match fetch_statement … (ev_genv p) (pc … s1) with
220[ OK x ⇒
221  match \snd x with
222  [ sequential s next ⇒
223    pc … s2 = succ_pc p (pc … s1) next
224  | _ ⇒ False (* never happens *)
225  ]
226| _ ⇒ False (* never happens *)
227].
228
229definition joint_call_ident : ∀p:evaluation_params.
230  (Σs : state_pc p.joint_classify p s = cl_call) → ident ≝
231(* this is a definition without a dummy ident :
232λp,st.
233match ?
234return λx.
235  !〈f, s〉 ← fetch_statement ? p … (ev_genv p) (pc … st) ;
236  match s with
237  [ sequential s next ⇒
238    match s with
239    [ step_seq s ⇒
240      match s with
241      [ CALL f' args dest ⇒
242        function_of_call … (ev_genv p) st f'
243      | _ ⇒ Error … [ ]
244      ]
245    | _ ⇒ Error … [ ]
246    ]
247  | _ ⇒ Error … [ ]
248  ] = x → ? with
249[ OK v ⇒ λ_.v
250| Error e ⇒ λABS.⊥
251] (refl …).
252@hide_prf
253elim (joint_classify_call … (pi2 … st))
254#f *#f' *#args *#dest *#next *#fn *#fd ** #EQ1 #EQ2 #EQ3
255lapply ABS -ABS
256>EQ1 >m_return_bind normalize nodelta >EQ2 #ABS destruct(ABS)
257qed. *)
258(* with a dummy ident (which is never used as seen above in the commented script)
259   I think handling of the function is easier *)
260λp,st.
261let dummy : ident ≝ an_identifier ? one in (* used where it cannot ever happen *)
262match fetch_statement … (ev_genv p) (pc … st) with
263[ OK x ⇒
264  match \snd x with
265  [ sequential s next ⇒
266    match s with
267    [ step_seq s ⇒
268      match s with
269      [ CALL f' args dest ⇒
270        match
271          (! bl ← block_of_call … (ev_genv p) f' st;
272           fetch_internal_function … (ev_genv p) bl) with
273        [ OK i_f ⇒ \fst i_f
274        | _ ⇒ dummy
275        ]
276      | _ ⇒ dummy
277      ]
278    | _ ⇒ dummy
279    ]
280  | _ ⇒ dummy
281  ]
282| _ ⇒ dummy
283].
284
285definition pcDeq ≝ mk_DeqSet program_counter eq_program_counter ?.
286*#p1 #EQ1 * #p2 #EQ2 @eq_program_counter_elim
287[ #EQ destruct % #H %
288| * #NEQ % #ABS destruct elim (NEQ ?) %
289]
290qed.
291
292(*
293let rec io_evaluate O I X (env : ∀o.res (I o)) (c : IO O I X) on c : res X ≝
294  match c with
295  [ Value x ⇒ OK … x
296  | Interact o f ⇒
297    ! i ← env o ;
298    io_evaluate O I X env (f i)
299  | Wrong e ⇒ Error … e
300  ].
301*)
302
303definition cost_label_of_stmt :
304  ∀p : evaluation_params.joint_statement p (globals p) → option costlabel ≝
305  λp,s.match s with
306  [ sequential s _ ⇒
307    match s with
308    [ step_seq s ⇒
309      match s with
310      [ COST_LABEL lbl ⇒ Some ? lbl
311      | _ ⇒ None ?
312      ]
313    | _ ⇒ None ?
314    ]
315  | _ ⇒ None ?
316  ].
317
318
319lemma no_label_uncosted : ∀p : evaluation_params.∀s,nxt.
320no_cost_label p (globals p) s →
321cost_label_of_stmt … (sequential … s nxt) = None ?.
322#p * // #lbl #next *
323qed.
324
325definition joint_abstract_status :
326 ∀p : evaluation_params.
327 abstract_status ≝
328 λp.
329 mk_abstract_status
330   (* as_status ≝ *) (state_pc p)
331   (* as_execute ≝ *)
332    (λs1,s2.(* io_evaluate … (io_env p s1) *) (eval_state … (ev_genv p) s1) = return s2)
333   (* as_pc ≝ *) pcDeq
334   (* as_pc_of ≝ *) (pc …)
335   (* as_classify ≝ *) (joint_classify p)
336   (* as_label_of_pc ≝ *)
337    (λpc.
338      match fetch_statement … (ev_genv p) pc with
339      [ OK fn_stmt ⇒ cost_label_of_stmt … (\snd fn_stmt)
340      | _ ⇒ None ?
341      ])
342   (* as_after_return ≝ *) (joint_after_ret p)
343   (* as_final ≝ *) (λs.is_final p  (globals ?) (ev_genv p) (exit p) s ≠ None ?)
344   (* as_call_ident ≝ *) (joint_call_ident p).
Note: See TracBrowser for help on using the repository browser.