source: src/common/StructuredTraces.ma @ 2305

Last change on this file since 2305 was 2299, checked in by campbell, 8 years ago

Soundly labelled RTLabs structured traces are "unrepeating".

File size: 33.5 KB
Line 
1include "basics/types.ma".
2include "basics/bool.ma".
3include "basics/jmeq.ma".
4include "common/CostLabel.ma".
5include "utilities/option.ma".
6include "basics/lists/listb.ma".
7include "ASM/Util.ma".
8
9inductive status_class: Type[0] ≝
10  | cl_return: status_class
11  | cl_jump: status_class
12  | cl_call: status_class
13  | cl_other: status_class.
14
15record abstract_status : Type[1] ≝
16{
17    as_status :> Type[0]
18  ; as_execute : as_status → as_status → Prop
19  ; as_pc : DeqSet
20  ; as_pc_of : as_status → as_pc
21  ; as_classifier : as_status → status_class → Prop
22  ; as_label_of_pc : as_pc → option costlabel
23  ; as_after_return : (Σs:as_status. as_classifier s cl_call) → as_status → Prop
24  ; as_final: as_status → Prop
25}.
26
27definition as_label ≝ λS : abstract_status. λs : S. as_label_of_pc ? (as_pc_of ? s).
28
29(* temporary alias for backward compatibility *)
30definition final_abstract_status ≝ abstract_status.
31
32definition as_costed : ∀a_s : abstract_status.a_s → Prop ≝
33  λa_s,st.as_label ? st ≠ None ?.
34
35lemma as_costed_exc : ∀S:abstract_status. ∀s:S. (as_costed S s) ∨ (¬as_costed S s).
36#S #s whd in match (as_costed S s);
37cases (as_label S s) [ %2 % * /2/ | #c %1 % #E destruct ]
38qed.
39
40definition as_label_safe : ∀a_s : abstract_status.
41  (Σs : a_s.as_costed ? s) → costlabel ≝
42  λa_s,st_sig.opt_safe … (pi2 … st_sig).
43
44inductive trace_ends_with_ret: Type[0] ≝
45  | ends_with_ret: trace_ends_with_ret
46  | doesnt_end_with_ret: trace_ends_with_ret.
47
48inductive trace_label_return (S:abstract_status) : S → S → Type[0] ≝
49  | tlr_base:
50      ∀status_before: S.
51      ∀status_after: S.
52        trace_label_label S ends_with_ret status_before status_after →
53        trace_label_return S status_before status_after
54  | tlr_step:
55      ∀status_initial: S.
56      ∀status_labelled: S.
57      ∀status_final: S.
58        trace_label_label S doesnt_end_with_ret status_initial status_labelled →
59        trace_label_return S status_labelled status_final →
60          trace_label_return S status_initial status_final
61with trace_label_label: trace_ends_with_ret → S → S → Type[0] ≝
62  | tll_base:
63      ∀ends_flag: trace_ends_with_ret.
64      ∀start_status: S.
65      ∀end_status: S.
66        trace_any_label S ends_flag start_status end_status →
67        as_costed S start_status →
68          trace_label_label S ends_flag start_status end_status
69with trace_any_label: trace_ends_with_ret → S → S → Type[0] ≝
70  (* Single steps within a function which reach a label.
71     Note that this is the only case applicable for a jump. *)
72  | tal_base_not_return:
73      ∀start_status: S.
74      ∀final_status: S.
75        as_execute S start_status final_status →
76        (as_classifier S start_status cl_jump ∨
77         as_classifier S start_status cl_other) →
78        as_costed S final_status →
79          trace_any_label S doesnt_end_with_ret start_status final_status
80  | tal_base_return:
81      ∀start_status: S.
82      ∀final_status: S.
83        as_execute S start_status final_status →
84        as_classifier S start_status cl_return →
85          trace_any_label S ends_with_ret start_status final_status
86  (* A call followed by a label on return. *)
87  | tal_base_call:
88      ∀status_pre_fun_call: S.
89      ∀status_start_fun_call: S.
90      ∀status_final: S.
91        as_execute S status_pre_fun_call status_start_fun_call →
92        ∀H:as_classifier S status_pre_fun_call cl_call.
93          as_after_return S (mk_Sig ?? status_pre_fun_call H) status_final →
94          trace_label_return S status_start_fun_call status_final →
95          as_costed S status_final →
96            trace_any_label S doesnt_end_with_ret status_pre_fun_call status_final
97  (* A call followed by a non-empty trace. *)
98  | tal_step_call:
99      ∀end_flag: trace_ends_with_ret.
100      ∀status_pre_fun_call: S.
101      ∀status_start_fun_call: S.
102      ∀status_after_fun_call: S.
103      ∀status_final: S.
104        as_execute S status_pre_fun_call status_start_fun_call →
105        ∀H:as_classifier S status_pre_fun_call cl_call.
106          as_after_return S (mk_Sig ?? status_pre_fun_call H) status_after_fun_call →
107          trace_label_return S status_start_fun_call status_after_fun_call →
108          ¬ as_costed S status_after_fun_call →
109          trace_any_label S end_flag status_after_fun_call status_final →
110            trace_any_label S end_flag status_pre_fun_call status_final
111  | tal_step_default:
112      ∀end_flag: trace_ends_with_ret.
113      ∀status_pre: S.
114      ∀status_init: S.
115      ∀status_end: S.
116        as_execute S status_pre status_init →
117        trace_any_label S end_flag status_init status_end →
118        as_classifier S status_pre cl_other →
119        ¬ (as_costed S status_init) →
120          trace_any_label S end_flag status_pre status_end.
121
122let rec tal_pc_list (S : abstract_status) fl st1 st2 (tal : trace_any_label S fl st1 st2)
123  on tal : list (as_pc S) ≝
124 match tal with
125 [ tal_step_call fl' pre _ st1' st2' _ _ _ _ _ tl ⇒ as_pc_of … pre :: tal_pc_list … tl
126 | tal_step_default fl' pre st1' st2' _ tl _ _ ⇒  as_pc_of … pre :: tal_pc_list … tl
127 | tal_base_not_return pre _ _ _ _ ⇒ [as_pc_of … pre]
128 | tal_base_return pre _ _ _ ⇒ [as_pc_of … pre]
129 | tal_base_call pre _ _ _ _ _ _ _ ⇒ [as_pc_of … pre]
130 ].
131
132definition as_trace_any_label_length':
133    ∀S: abstract_status.
134    ∀trace_ends_flag: trace_ends_with_ret.
135    ∀start_status: S.
136    ∀final_status: S.
137    ∀the_trace: trace_any_label S trace_ends_flag start_status final_status.
138      nat ≝
139  λS: abstract_status.
140  λtrace_ends_flag: trace_ends_with_ret.
141  λstart_status: S.
142  λfinal_status: S.
143  λthe_trace: trace_any_label S trace_ends_flag start_status final_status.
144    |tal_pc_list … the_trace|.
145
146let rec tlr_unrepeating S st1 st2 (tlr : trace_label_return S st1 st2) on tlr : Prop ≝
147  match tlr with
148  [ tlr_base st1 st2 tll ⇒ tll_unrepeating … tll
149  | tlr_step st1 st2 st3 tll tl ⇒ tll_unrepeating … tll ∧ tlr_unrepeating … tl
150  ]
151and tll_unrepeating S fl st1 st2 (tll : trace_label_label S fl st1 st2) on tll : Prop ≝
152  match tll with
153  [ tll_base fl st1 st2 tal _ ⇒ tal_unrepeating … tal
154  ]
155and tal_unrepeating S fl st1 st2 (tal : trace_any_label S fl st1 st2) on tal : Prop ≝
156  match tal with
157  [ tal_step_call fl st1 st2 st3 st4 _ _ _ tlr _ tl ⇒
158    bool_to_Prop (notb (memb ? (as_pc_of … st1) (tal_pc_list … tl))) ∧
159    tal_unrepeating … tl ∧
160    tlr_unrepeating … tlr
161  | tal_step_default fl st1 st2 st3 _ tl _ _ ⇒
162    bool_to_Prop (notb (memb ? (as_pc_of … st1) (tal_pc_list … tl))) ∧
163    tal_unrepeating … tl
164  | tal_base_call pre _ _ _ _ _ trace _ ⇒ tlr_unrepeating … trace
165  | _ ⇒ True
166  ].
167
168definition tll_hd_label : ∀S : abstract_status.∀fl,st1,st2.
169  trace_label_label S fl st1 st2 → costlabel ≝
170  λS,fl,st1,st2,tr.match tr with
171  [ tll_base _ st1' _ _ prf ⇒ as_label_safe … «st1', prf» ].
172
173definition tlr_hd_label : ∀S : abstract_status.∀st1,st2.
174  trace_label_return S st1 st2 → costlabel ≝
175  λS,st1,st2,tr.match tr with
176  [ tlr_base st1' st2' tll ⇒ tll_hd_label … tll
177  | tlr_step st1' st2' _ tll _ ⇒ tll_hd_label … tll
178  ].
179
180let rec tal_unrepeating_uniqueb S fl st1 st2 tal on tal :
181  tal_unrepeating S fl st1 st2 tal → bool_to_Prop (uniqueb … (tal_pc_list … tal)) ≝ ?.
182cases tal //
183#fl' #st1' [#st_fun] #st2' #st3' #H
184[ #H0 #H1 #tlr #G #tal | #tal #H0 #G ] whd in ⊢ (% → ?%); [*]*
185#A #B [#_] >A normalize nodelta @tal_unrepeating_uniqueb assumption
186qed.
187
188lemma not_costed_no_label : ∀S,st.
189  ¬as_costed S st →
190  as_label_of_pc S (as_pc_of S st) = None ?.
191#S #st * normalize cases (as_label_of_pc S ?)
192[ // | #l #H cases (H ?) % #E destruct ]
193qed.
194
195lemma tal_pc_list_start : ∀S,fl,s1,s2. ∀tal: trace_any_label S fl s1 s2.
196  ∃tl. tal_pc_list … tal = (as_pc_of S s1)::tl.
197#S #fl0 #s10 #s20 *
198[ #s1 #s2 #EX #CL #CS
199| #s1 #s2 #EX #CL
200| #s1 #s2 #s3 #EX #CL #AF #tlr #CS
201| #fl #s1 #s2 #s3 #s4 #EX #CL #AF #tlr #CS #tal
202| #fl #s1 #s2 #s3 #EX #tal #CL #CS
203] whd in ⊢ (??(λ_.??%?)); % [ 2,4,6,8,10: % | *: skip ]
204qed.
205
206let rec tal_tail_not_costed S fl st1 st2 tal
207  (H:Not (as_costed S st1)) on tal :
208  All ? (λl. as_label_of_pc S l = None ?) (tal_pc_list S fl st1 st2 tal) ≝ ?.
209cases tal in H ⊢ %;
210[ #start #final #EX #CL #CS #CS' % // @(not_costed_no_label ?? CS')
211| #start #final #EX #CL #CS % // @(not_costed_no_label ?? CS)
212| #pre #start #final #EX #CL #AF #tlr #CS #CS' % // @(not_costed_no_label ?? CS')
213| #fl' #pre #start #after #final #EX #CL #AF #tlr #CS #tal' #CS'
214  cases (tal_pc_list_start … tal')
215  #hd #E >E
216  % [ @(not_costed_no_label ?? CS') | @tal_tail_not_costed assumption ]
217| #fl' #pre #init #end #EX #tal' #CL #CS #CS'
218  cases (tal_pc_list_start … tal')
219  #hd #E >E
220  % [ @(not_costed_no_label ?? CS') | @tal_tail_not_costed assumption ]
221] qed.
222
223
224inductive trace_any_call (S:abstract_status) : S → S → Type[0] ≝
225  | tac_base:
226      ∀status: S.
227        as_classifier S status cl_call →
228          trace_any_call S status status
229  | tac_step_call:
230      ∀status_pre_fun_call: S.
231      ∀status_after_fun_call: S.
232      ∀status_final: S.
233      ∀status_start_fun_call: S.
234        as_execute S status_pre_fun_call status_start_fun_call →
235        ∀H:as_classifier S status_pre_fun_call cl_call.
236          as_after_return S (mk_Sig ?? status_pre_fun_call H) status_after_fun_call →
237          trace_label_return S status_start_fun_call status_after_fun_call →
238          ¬ as_costed S status_after_fun_call →
239          trace_any_call S status_after_fun_call status_final →
240            trace_any_call S status_pre_fun_call status_final
241  | tac_step_default:
242      ∀status_pre: S.
243      ∀status_end: S.
244      ∀status_init: S.
245        as_execute S status_pre status_init →
246        trace_any_call S status_init status_end →
247        as_classifier S status_pre cl_other →
248        ¬ (as_costed S status_init) →
249          trace_any_call S status_pre status_end.
250
251             
252inductive trace_label_call (S:abstract_status) : S → S → Type[0] ≝
253  | tlc_base:
254      ∀start_status: S.
255      ∀end_status: S.
256        trace_any_call S start_status end_status →
257        as_costed S start_status →
258        trace_label_call S start_status end_status
259.
260
261definition tlc_hd_label : ∀S : abstract_status.∀st1,st2.
262  trace_label_call S st1 st2 → costlabel ≝
263  λS,st1,st2,tr.match tr with
264  [ tlc_base st1' _ _ prf ⇒ as_label_safe … «st1', prf»
265  ].
266   
267coinductive trace_label_diverges (S:abstract_status) : S → Type[0] ≝
268  | tld_step:
269      ∀status_initial: S.
270      ∀status_labelled: S.
271          trace_label_label S doesnt_end_with_ret status_initial status_labelled →
272          trace_label_diverges S status_labelled →
273            trace_label_diverges S status_initial
274  | tld_base:
275      ∀status_initial: S.
276      ∀status_pre_fun_call: S.
277      ∀status_start_fun_call: S.
278        trace_label_call S status_initial status_pre_fun_call →
279        as_execute S status_pre_fun_call status_start_fun_call →
280        ∀H:as_classifier S status_pre_fun_call cl_call.
281          trace_label_diverges S status_start_fun_call →
282            trace_label_diverges S status_initial.
283
284definition tld_hd_label : ∀S : abstract_status.∀st.
285  trace_label_diverges S st → costlabel ≝
286  λS,st,tr.match tr with
287  [ tld_step st' st'' tll _ ⇒ tll_hd_label … tll
288  | tld_base st' st'' _ tlc _ _ _ ⇒ tlc_hd_label … tlc
289  ].       
290
291(* Version in Prop for showing existence. *)
292coinductive trace_label_diverges_exists (S:abstract_status) : S → Prop ≝
293  | tld_step':
294      ∀status_initial: S.
295      ∀status_labelled: S.
296          trace_label_label S doesnt_end_with_ret status_initial status_labelled →
297          trace_label_diverges_exists S status_labelled →
298            trace_label_diverges_exists S status_initial
299  | tld_base':
300      ∀status_initial: S.
301      ∀status_pre_fun_call: S.
302      ∀status_start_fun_call: S.
303        trace_label_call S status_initial status_pre_fun_call →
304        as_execute S status_pre_fun_call status_start_fun_call →
305        ∀H:as_classifier S status_pre_fun_call cl_call.
306          trace_label_diverges_exists S status_start_fun_call →
307            trace_label_diverges_exists S status_initial.
308
309inductive trace_whole_program (S: abstract_status) : S → Type[0] ≝
310  | twp_terminating:
311      ∀status_initial: S.
312      ∀status_start_fun: S.
313      ∀status_final: S.
314        as_classifier S status_initial cl_call →
315        as_execute S status_initial status_start_fun →
316        trace_label_return S status_start_fun status_final →
317        as_final S status_final →
318        trace_whole_program S status_initial
319  | twp_diverges:
320      ∀status_initial: S.
321      ∀status_start_fun: S.
322        as_classifier S status_initial cl_call →
323        as_execute S status_initial status_start_fun →
324        trace_label_diverges S status_start_fun →
325        trace_whole_program S status_initial.
326
327(* Again, an identical version in Prop for existence proofs. *)
328inductive trace_whole_program_exists (S: abstract_status) : S → Prop ≝
329  | twp_terminating:
330      ∀status_initial: S.
331      ∀status_start_fun: S.
332      ∀status_final: S.
333        as_classifier S status_initial cl_call →
334        as_execute S status_initial status_start_fun →
335        trace_label_return S status_start_fun status_final →
336        as_final S status_final →
337        trace_whole_program_exists S status_initial
338  | twp_diverges:
339      ∀status_initial: S.
340      ∀status_start_fun: S.
341        as_classifier S status_initial cl_call →
342        as_execute S status_initial status_start_fun →
343        trace_label_diverges_exists S status_start_fun →
344        trace_whole_program_exists S status_initial.
345
346
347let rec trace_any_label_label S s s' f
348  (tr:trace_any_label S f s s') on tr : match f with [ doesnt_end_with_ret ⇒ as_costed S s' | ends_with_ret ⇒ True ] ≝
349match tr return λf,s,s',tr. match f with [ doesnt_end_with_ret ⇒ as_costed S s' | ends_with_ret ⇒ True ] with
350[ tal_base_not_return start final _ _ C ⇒ C
351| tal_base_return _ _  _ _ ⇒ I
352| tal_base_call _ _ _ _ _ _ _ C ⇒ C
353| tal_step_call f pre start after final X C RET LR C' tr' ⇒ trace_any_label_label … tr'
354| tal_step_default f pre init end X tr' C C' ⇒ trace_any_label_label … tr'
355].
356
357definition tal_tl_label : ∀S : abstract_status.∀st1,st2.
358  trace_any_label S doesnt_end_with_ret st1 st2 → costlabel ≝
359  λS,st1,st2,tr.as_label_safe … «st2, trace_any_label_label … tr».
360
361lemma trace_label_label_label : ∀S,s,s',f.
362  ∀tr:trace_label_label S f s s'. match f with [ doesnt_end_with_ret ⇒ as_costed S s' | ends_with_ret ⇒ True ].
363#S #s #s' #f #tr
364cases tr
365#f #start #end #tr' #C @(trace_any_label_label … tr')
366qed.
367
368definition tll_tl_label : ∀S : abstract_status.∀st1,st2.
369  trace_label_label S doesnt_end_with_ret st1 st2 → costlabel ≝
370  λS,st1,st2,tr.as_label_safe … «st2, trace_label_label_label … tr».
371
372lemma trace_any_call_call : ∀S,s,s'.
373  trace_any_call S s s' → as_classifier S s' cl_call.
374#S #s #s' #T elim T //
375qed.
376
377(*
378(* an trace of unlabeled and cl_other states, possibly empty *)
379inductive trace_no_label_any (S:abstract_status) : S → S → Type[0] ≝
380  | tna_base :
381      ∀start_status: S.
382      ¬as_costed … start_status →
383      trace_no_label_any S start_status start_status
384  | tna_step :
385      ∀status_pre: S.
386      ∀status_init: S.
387      ∀status_end: S.
388        as_execute S status_pre status_init →
389        as_classifier S status_pre cl_other →
390        ¬as_costed … status_pre →
391        trace_no_label_any S status_init status_end →
392          trace_no_label_any S status_pre status_end.
393
394let rec tna_append_tna S st1 st2 st3 (taa1 : trace_no_label_any S st1 st2)
395  on taa1 :
396    trace_no_label_any S st2 st3 →
397    trace_no_label_any S st1 st3 ≝
398  match taa1 with
399  [ tna_base st1' H ⇒ λtaa2.taa2
400  | tna_step st1' st2' st3' H G K tl ⇒
401    λtaa2.tna_step ???? H G K (tna_append_tna … tl taa2)
402  ].
403
404definition tna_non_empty ≝ λS,st1,st2.λtna : trace_no_label_any S st1 st2.
405  match tna with
406  [ tna_base _ _ ⇒ false
407  | tna_step _ _ _ _ _ _ _ ⇒ true
408  ].
409
410coercion tna_to_bool : ∀S,st1,st2.∀tna:trace_no_label_any S st1 st2.bool ≝
411 tna_non_empty on _tna : trace_no_label_any ??? to bool.
412
413lemma tna_unlabelled : ∀S,st1,st2.trace_no_label_any S st1 st2 → ¬as_costed … st1.
414#S #st1 #st2 * [#st #H @H | #st #st' #st'' #_ #_ #H #_ @H] qed.
415
416let rec tna_append_tal S st1 fl st2 st3 (tna : trace_no_label_any S st1 st2)
417  on tna :
418  trace_any_label S fl st2 st3 →
419  if tna then Not (as_costed … st2) else True →
420  trace_any_label S fl st1 st3 ≝
421  match tna return λst1,st2.λx : trace_no_label_any S st1 st2.
422    ∀fl,st3.
423    trace_any_label S fl st2 st3 →
424    if x then Not (as_costed … st2) else True →
425    trace_any_label S fl st1 st3
426  with
427  [ tna_base st1' H ⇒ λfl,st3,taa2,prf.taa2
428  | tna_step st1' st2' st3' H G K tl ⇒ λfl,st3,taa2,prf.
429    tal_step_default ????? H (tna_append_tal ????? tl taa2 ?) G (tna_unlabelled … tl)
430  ] fl st3.
431  cases (tna_non_empty … tl) [@prf|%]
432  qed.
433*)
434
435inductive trace_any_any (S : abstract_status) : S → S → Type[0] ≝
436  | taa_base : ∀st.trace_any_any S st st
437  | taa_step : ∀st1,st2,st3.
438    as_execute S st1 st2 →
439    as_classifier S st1 cl_other →
440    ¬as_costed S st2 →
441    trace_any_any S st2 st3 →
442    trace_any_any S st1 st3.
443
444definition taa_non_empty ≝ λS,st1,st2.λtaa : trace_any_any S st1 st2.
445  match taa with
446  [ taa_base _ ⇒ false
447  | taa_step _ _ _ _ _ _ _ ⇒ true
448  ].
449
450coercion taa_to_bool : ∀S,st1,st2.∀taa:trace_any_any S st1 st2.bool ≝
451 taa_non_empty on _taa : trace_any_any ??? to bool.
452
453let rec taa_append_tal S st1 fl st2 st3
454  (taa : trace_any_any S st1 st2) on taa :
455  trace_any_label S fl st2 st3 →
456  trace_any_label S fl st1 st3 ≝
457  match taa return λst1,st2.λx : trace_any_any S st1 st2.
458    ∀fl,st3.
459    trace_any_label S fl st2 st3 →
460    trace_any_label S fl st1 st3
461  with
462  [ taa_base st1' ⇒ λfl,st3,tal2.tal2
463  | taa_step st1' st2' st3' H G K tl ⇒ λfl,st3,tal2.
464    tal_step_default ????? H (taa_append_tal ????? tl tal2) G K
465  ] fl st3.
466
467interpretation "trace any any label append" 'append taa tal = (taa_append_tal ????? taa tal).
468
469let rec tlr_rel S1 st1 st1' S2 st2 st2'
470  (tlr1 : trace_label_return S1 st1 st1')
471  (tlr2 : trace_label_return S2 st2 st2') on tlr1 : Prop ≝
472match tlr1 with
473  [ tlr_base st1 st1' tll1 ⇒
474    match tlr2 with
475    [ tlr_base st2 st2' tll2 ⇒ tll_rel … tll1 tll2
476    | _ ⇒ False
477    ]
478  | tlr_step st1 st1' st1'' tll1 tl1 ⇒
479    match tlr2 with
480    [ tlr_step st2 st2' st2'' tll2 tl2 ⇒
481      tll_rel … tll1 tll2 ∧ tlr_rel … tl1 tl2
482    | _ ⇒ False
483    ]
484  ]
485and tll_rel S1 fl1 st1 st1' S2 fl2 st2 st2'
486 (tll1 : trace_label_label S1 fl1 st1 st1')
487 (tll2 : trace_label_label S2 fl2 st2 st2') on tll1 : Prop ≝
488  match tll1 with
489  [ tll_base fl1 st1 st1' tal1 H ⇒
490    match tll2 with
491    [ tll_base fl2 st2 st2 tal2 G ⇒
492      as_label_safe … («?, H») = as_label_safe … («?, G») ∧
493      tal_rel … tal1 tal2
494    ]
495  ]
496and tal_rel S1 fl1 st1 st1' S2 fl2 st2 st2'
497 (tal1 : trace_any_label S1 fl1 st1 st1')
498 (tal2 : trace_any_label S2 fl2 st2 st2')
499   on tal1 : Prop ≝
500  match tal1 with
501  [ tal_base_not_return st1 st1' _ _ _ ⇒
502    fl2 = doesnt_end_with_ret ∧
503    ∃st2mid,taa,H,G,K.
504    tal2 ≃ taa_append_tal ? st2 ??? taa
505      (tal_base_not_return ? st2mid st2' H G K)
506  | tal_base_return st1 st1' _ _ ⇒
507    fl2 = ends_with_ret ∧
508    ∃st2mid,taa,H,G.
509    tal2 ≃ taa_append_tal ? st2 ? st2mid st2' taa
510      (tal_base_return ? st2mid st2' H G)
511  | tal_base_call st1 st1' st1'' _ _ _ tlr1 _ ⇒
512    fl2 = doesnt_end_with_ret ∧
513    ∃st2mid,taa,st2mid',H,G,K,tlr2,L.
514    tal2 ≃ taa_append_tal ? st2 ? st2mid st2' taa
515      (tal_base_call ? st2mid st2mid' st2' H G K tlr2 L) ∧
516      tlr_rel … tlr1 tlr2
517  | tal_step_call fl1 st1 st1' st1'' st1''' _ _ _ tlr1 _ tl1 ⇒
518    ∃st2mid,taa,st2_fun,st2_after_fun,H,G,K,tlr2,L,tl2.
519    tal2 ≃ taa_append_tal ? st2 ? st2mid st2' taa
520      (tal_step_call ?? st2mid st2_fun st2_after_fun st2' H G K tlr2 L tl2) ∧
521      tlr_rel … tlr1 tlr2 ∧ tal_rel … tl1 tl2
522  | tal_step_default fl1 st1 st1' st1'' _ tl1 _ _ ⇒
523    tal_rel … tl1 tal2 (* <- this makes it many to many *)
524  ].
525
526interpretation "trace any label rel" 'napart t1 t2 = (tal_rel ???????? t1 t2).
527interpretation "trace label label rel" 'napart t1 t2 = (tll_rel ???????? t1 t2).
528interpretation "trace label return rel" 'napart t1 t2 = (tlr_rel ?????? t1 t2).
529
530let rec taa_rel_inv S1 fl1 st1 st1mid st1' S2 fl2 st2 st2'
531  (taa1 : trace_any_any S1 st1 st1mid) on taa1 :
532  ∀tal1 : trace_any_label S1 fl1 st1mid st1'.
533  ∀tal2 : trace_any_label S2 fl2 st2 st2'.
534  tal_rel … (taa1 @ tal1) tal2 →
535  tal_rel … tal1 tal2 ≝ ?.
536cases taa1 -taa1
537[ -taa_rel_inv //
538| #st #st' #st'' #H #G #K #tl #tal1 #tal2 whd in ⊢ (%→?);
539  @(taa_rel_inv … tl)
540]
541qed.
542
543let rec tlr_rel_transitive S1 st1 st1' S2 st2 st2' S3 st3 st3'
544  (tlr1 : trace_label_return S1 st1 st1')
545  (tlr2 : trace_label_return S2 st2 st2')
546  (tlr3 : trace_label_return S3 st3 st3') on tlr1 :
547  tlr_rel … tlr1 tlr2 → tlr_rel … tlr2 tlr3 → tlr_rel … tlr1 tlr3 ≝
548match tlr1 with
549  [ tlr_base st1' st1'' tll1 ⇒ ?
550  | tlr_step st1' st1'' st1''' tll1 tl1 ⇒ ?
551  ]
552and tll_rel_transitive S1 fl1 st1 st1' S2 fl2 st2 st2' S3 fl3 st3 st3'
553  (tll1 : trace_label_label S1 fl1 st1 st1')
554  (tll2 : trace_label_label S2 fl2 st2 st2')
555  (tll3 : trace_label_label S3 fl3 st3 st3') on tll1 :
556  tll1 ≈ tll2 → tll2 ≈ tll3 → tll1 ≈ tll3 ≝
557  match tll1 with
558  [ tll_base fl1' st1' st1'' tal1 H ⇒ ?
559  ]
560and tal_rel_transitive S1 fl1 st1 st1' S2 fl2 st2 st2' S3 fl3 st3 st3'
561  (tal1 : trace_any_label S1 fl1 st1 st1')
562  (tal2 : trace_any_label S2 fl2 st2 st2')
563  (tal3 : trace_any_label S3 fl3 st3 st3') on tal1 :
564  tal1 ≈ tal2 → tal2 ≈ tal3 → tal1 ≈ tal3 ≝
565  match tal1 with
566  [ tal_base_not_return st1' st1'' H G K ⇒ ?
567  | tal_base_return st1' st1'' H G ⇒ ?
568  | tal_base_call st1' st1'' st1''' H G K tlr1 L ⇒ ?
569  | tal_step_call fl1' st1' st1'' st1''' st1'''' H G L tlr1 K tl1 ⇒ ?
570  | tal_step_default fl1' st1' st1'' st1''' H tl1 G K ⇒ ?
571  ].
572[1,2: cases tlr2 #st2' #st2'' [1,3: #tllhd2 |2,4: #st2''' #tllhd2 #tlrtl2]
573  [2,3: *] normalize in ⊢ (%→?); [ #Rhd12 | * #Rhd12 #Rtl12 ]
574  cases tlr3 #st3' #st3'' [1,3: #tllhd3 |2,4: #st3''' #tllhd3 #tlrtl3]
575  [2,3: *] normalize in ⊢ (%→?); [ #Rhd23 | * #Rhd23 #Rtl23 ] whd
576  [ @(tll_rel_transitive … Rhd12 Rhd23) |
577    %{(tll_rel_transitive … Rhd12 Rhd23)}
578    @(tlr_rel_transitive … Rtl12 Rtl23)
579  ]
580|3:
581  cases tll2 #fl2' #st2' #st2'' #tal2 #H2 * #G2 #R12
582  cases tll3 #fl3' #st3' #st3'' #tal3 #H3 * #G3 #R23
583  %{(tal_rel_transitive … R12 R23)} //
584|*:
585  [1,2,3: * #EQ]
586  [5: whd in ⊢ (%→?→%); @tal_rel_transitive]
587  *#st2mid *#taa2
588  [ *#H2 *#G2 *#K2 #R12
589  | *#H2 *#G2 #R12
590  | *#st2' *#H2 *#G2 *#K2 *#tlr2 *#L2 *#R12 #R12'
591  | *#st2' *#st2'' *#H2 *#G2 *#K2 *#tlr2 *#L2 *#tl2 ** #R12 #R12' #R12''
592  ] destruct #R23 lapply (taa_rel_inv … R23) [1,2: // |3: *#EQ destruct]
593  *#st3mid *#taa3
594  [ *#st3' *#H3 *#G3 *#K3 *#tlr3 *#L3 *#R23 #R23'
595    %{(refl …)} %{st3mid} %{taa3} %{st3'} %{H3} %{G3} %{K3} %{tlr3} %{L3}
596    %{R23} @(tlr_rel_transitive … R12' R23')
597  | *#st3' *#st3'' *#H3 *#G3 *#K3 *#tlr3 *#L3 *#tl3 ** #R23 #R23' #R23''
598    %{st3mid} %{taa3} %{st3'} %{st3''} %{H3} %{G3} %{K3} %{tlr3} %{L3} %{tl3}
599    %{(tal_rel_transitive … R12'' R23'')}
600    %{R23} @(tlr_rel_transitive … R12' R23')
601  ]
602]
603qed.
604
605let rec flatten_trace_label_label
606  (S: abstract_status) (trace_ends_flag: trace_ends_with_ret)
607    (start_status: S) (final_status: S)
608      (the_trace: trace_label_label S trace_ends_flag start_status final_status)
609        on the_trace: list (Σl: costlabel. ∃pc. as_label_of_pc S pc = Some … l) ≝
610  match the_trace with
611  [ tll_base ends_flag initial final given_trace labelled_proof ⇒
612      let label ≝
613        match as_label … initial return λx: option costlabel. x ≠ None costlabel → ? with
614        [ None ⇒ λabs. ⊥
615        | Some l ⇒ λ_. l
616        ] labelled_proof
617      in
618        (mk_Sig … label ?)::flatten_trace_any_label S ends_flag initial final given_trace
619  ]
620and flatten_trace_any_label
621  (S: abstract_status) (trace_ends_flag: trace_ends_with_ret)
622    (start_status: S) (final_status: S)
623      (the_trace: trace_any_label S trace_ends_flag start_status final_status)
624        on the_trace: list (Σl: costlabel.  ∃pc. as_label_of_pc S pc = Some … l) ≝
625  match the_trace with
626  [ tal_base_not_return the_status _ _ _ _ ⇒ [ ]
627  | tal_base_call pre_fun_call start_fun_call final _ _ _ call_trace _ ⇒
628      flatten_trace_label_return … call_trace
629  | tal_base_return the_status _ _ _ ⇒ [ ]
630  | tal_step_call end_flag pre_fun_call start_fun_call after_fun_call final
631    _ _ _ call_trace _ final_trace ⇒
632    let call_cost_trace ≝ flatten_trace_label_return … call_trace in
633    let final_cost_trace ≝ flatten_trace_any_label … end_flag … final_trace in
634        call_cost_trace @ final_cost_trace
635  | tal_step_default end_flag status_pre status_init status_end _ tail_trace _ _ ⇒
636      flatten_trace_any_label … tail_trace
637  ]
638and flatten_trace_label_return
639  (S: abstract_status)
640    (start_status: S) (final_status: S)
641      (the_trace: trace_label_return S start_status final_status)
642        on the_trace: list (Σl: costlabel.  ∃pc. as_label_of_pc S pc = Some … l) ≝
643  match the_trace with
644  [ tlr_base before after trace_to_lift ⇒
645      flatten_trace_label_label … trace_to_lift
646  | tlr_step initial labelled final labelled_trace ret_trace ⇒
647    let labelled_cost ≝ flatten_trace_label_label … doesnt_end_with_ret … labelled_trace in
648    let return_cost ≝ flatten_trace_label_return … ret_trace in
649        labelled_cost @ return_cost
650  ].
651  [2:
652    cases abs -abs #abs @abs %
653  |1:
654    %{(as_pc_of … initial)} whd in match label;
655    change with (as_label ?? = ?)
656    generalize in match labelled_proof; whd in ⊢ (% → ?);
657    cases (as_label S initial)
658    [1:
659      #absurd @⊥ cases absurd -absurd #absurd @absurd %
660    |2:
661      #costlabel normalize nodelta #_ %
662    ]
663  ]
664qed.
665
666let rec taa_append_tal_same_flatten
667  S st1 fl st2 st3 (taa : trace_any_any S st1 st2) on taa :
668  ∀tal : trace_any_label S fl st2 st3.
669    flatten_trace_any_label … (taa @ tal) =
670      flatten_trace_any_label … tal ≝ ?.
671cases taa -st1 -st2
672[ //
673| #st_pre #st_init #st2 #H #G #K #taa' #tal
674  whd in match (? @ ?);
675  whd in ⊢ (??%?); //
676]
677qed.
678
679let rec tll_rel_to_traces_same_flatten
680  (S: abstract_status) (S': abstract_status)
681    (trace_ends_flag_l: trace_ends_with_ret) (trace_ends_flag_r: trace_ends_with_ret)
682    (start_status_l: S) (final_status_l: S) (start_status_r: S') (final_status_r: S')
683      (the_trace_l: trace_label_label S trace_ends_flag_l start_status_l final_status_l)
684        (the_trace_r: trace_label_label S' trace_ends_flag_r start_status_r final_status_r)
685          on the_trace_l:
686            tll_rel … the_trace_l the_trace_r →
687              map … (pi1 …) (flatten_trace_label_label … the_trace_l) =
688                map … (pi1 …) (flatten_trace_label_label … the_trace_r) ≝
689  match the_trace_l with
690  [ tll_base fl1 st1 st1' tal1 H ⇒
691    match the_trace_r with
692    [ tll_base fl2 st2 st2 tal2 G ⇒ ?
693    ]
694  ]
695and tal_rel_to_traces_same_flatten
696  (S: abstract_status) (S': abstract_status) (trace_ends_flag_l: trace_ends_with_ret)
697    (trace_ends_flag_r: trace_ends_with_ret)
698      (start_status_l: S) (final_status_l: S) (start_status_r: S') (final_status_r: S')
699        (the_trace_l: trace_any_label S trace_ends_flag_l start_status_l final_status_l)
700          (the_trace_r: trace_any_label S' trace_ends_flag_r start_status_r final_status_r)
701          on the_trace_l:
702            tal_rel … the_trace_l the_trace_r →
703              map … (pi1 …) (flatten_trace_any_label … the_trace_l) =
704                map … (pi1 …) (flatten_trace_any_label … the_trace_r) ≝
705  match the_trace_l with
706  [ tal_base_not_return st1 st1' H G K ⇒ ?
707  | tal_base_return st1 st1' H G ⇒ ?
708  | tal_base_call st1 st1' st1'' H G K tlr1 L ⇒ ?
709  | tal_step_call fl1 st1 st1' st1'' st1''' H G K tlr1 L tl1 ⇒ ?
710  | tal_step_default fl1 st1 st1' st1'' H tl1 G K ⇒ ?
711  ]
712and tlr_rel_to_traces_same_flatten
713  (S: abstract_status) (S': abstract_status) (start_status_l: S) (final_status_l: S)
714    (start_status_r: S') (final_status_r: S')
715      (the_trace_l: trace_label_return S start_status_l final_status_l)
716        (the_trace_r: trace_label_return S' start_status_r final_status_r)
717        on the_trace_l:
718          tlr_rel … the_trace_l the_trace_r →
719            map … (pi1 …) (flatten_trace_label_return … the_trace_l) =
720              map … (pi1 …) (flatten_trace_label_return … the_trace_r) ≝
721  match the_trace_l with
722  [ tlr_base before after tll_l ⇒ ?
723  | tlr_step initial labelled final tll_l tlr_l ⇒ ?
724  ]. 
725[ * whd in match as_label_safe; normalize nodelta
726  @opt_safe_elim #l1 #EQ1
727  @opt_safe_elim #l2 #EQ2
728  #EQ destruct(EQ) #H_tal
729  change with (? :: ? = ? :: ?) lapply H -H lapply G -G
730  whd in match as_costed; normalize nodelta
731  >EQ1 >EQ2 normalize nodelta #_ #_
732  >(tal_rel_to_traces_same_flatten … H_tal) @refl
733|2,3,4,5,6:
734  [1,2,3: * #EQ destruct(EQ)]
735  [1,2,3,4: * #st_mid * #taa
736    [ *#H' *#G' *#K' #EQ
737    | *#H' *#G' #EQ
738    | *#st_mid' *#H' *#G' *#K' *#tlr2 *#L' * #EQ #H_tlr
739    | *#st_fun *#st_after *#H' *#G' *#K' *#tlr2 *#L' *#tl2 ** #EQ #H_tlr #H_tal
740    ] >EQ >taa_append_tal_same_flatten
741  | whd in ⊢ (%→??(????%)?);
742    @tal_rel_to_traces_same_flatten
743  ]
744  [1,2: %
745  | @(tlr_rel_to_traces_same_flatten … H_tlr)
746  | <map_append
747    >(tlr_rel_to_traces_same_flatten … H_tlr)
748    >(tal_rel_to_traces_same_flatten … H_tal)
749    @map_append
750  ]
751|*: cases the_trace_r
752  [1,3: #st_before_r #st_after_r #tll_r
753    [ @tll_rel_to_traces_same_flatten | * ]
754  |*: #st_init_r #st_labld_r #st_fin_r #tll_r #tlr_r *
755    #H_tll #H_tlr
756    <map_append
757    >(tll_rel_to_traces_same_flatten … H_tll)
758    >(tlr_rel_to_traces_same_flatten … H_tlr)
759    @map_append
760  ]
761]
762qed.
763
764definition as_cost_map ≝
765  λS : abstract_status. (Σl.∃pc.as_label_of_pc S pc = Some ? l) → ℕ.
766
767definition lift_sigma_map_id :
768  ∀A,B : Type[0].∀P_in,P_out : A → Prop.B →
769    (∀a.P_out a + ¬ P_out a) →
770  ((Σa.P_out a) → B) → (Σa.P_in a) → B ≝ λA,B,P_in,P_out,dflt,dec,m,a_sig.
771   match dec a_sig with
772   [ inl prf ⇒ m «a_sig, prf»
773   | inr _ ⇒ dflt (* labels not present in out code get 0 *)
774   ].
775
776lemma lift_sigma_map_id_eq :
777  ∀A,B,P_in,P_out,dflt,dec,m,a_in,a_out.
778  pi1 ?? a_in = pi1 ?? a_out →
779  lift_sigma_map_id A B P_in P_out dflt dec m a_in = m a_out.
780#A#B#P_in#P_out#dflt#dec#m#a_in#a_out#EQ
781whd in match lift_sigma_map_id; normalize nodelta
782cases (dec a_in) normalize nodelta >EQ cases a_out
783#a #H #G [ % | @⊥ /2 by absurd/ ]
784qed.
785
786notation > "Σ_{ ident i ∈ l } f"
787  with precedence 20
788  for @{'fold plus 0 (λ${ident i}.true) (λ${ident i}. $f) $l}.
789notation < "Σ_{ ident i ∈ l } f"
790  with precedence 20
791for @{'fold plus 0 (λ${ident i}:$X.true) (λ${ident i}:$Y. $f) $l}.
792
793definition lift_cost_map_id :
794  ∀S_in,S_out : abstract_status.? →
795  as_cost_map S_out → as_cost_map S_in
796  ≝
797 λS_in,S_out : abstract_status.
798  lift_sigma_map_id costlabel ℕ
799    (λl.∃pc.as_label_of_pc S_in pc = Some ? l)
800    (λl.∃pc.as_label_of_pc S_out pc = Some ? l)
801    0.
802
803lemma lift_cost_map_same_cost :
804  ∀S_in, S_out, dec, m_out, trace_in, trace_out.
805  map … (pi1 ??) trace_in = map … (pi1 ??) trace_out →
806  (Σ_{ l_sig ∈ trace_in } (lift_cost_map_id S_in S_out dec m_out l_sig)) =
807  (Σ_{ l_sig ∈ trace_out } (m_out l_sig)).
808#S_in #S_out #dec #m_out #trace_in elim trace_in
809[2: #hd_in #tl_in #IH] * [2,4: #hd_out #tl_out]
810normalize in ⊢ (%→?);
811[2,3: #ABS destruct(ABS)
812|4: #_ %
813|1:
814  #EQ destruct
815  whd in ⊢(??%%);
816  whd in match lift_cost_map_id; normalize nodelta
817  >(lift_sigma_map_id_eq ????????? e0)
818  >e0 in e1; normalize in ⊢(%→?); #EQ
819  >(IH … EQ) %
820]
821qed.
822
823lemma lift_cost_map_same_cost_tal :
824  ∀S_in, S_out, dec, m_out, f_in, f_out, start_in, start_out, end_in, end_out.
825  ∀the_trace_in : trace_any_label S_in f_in start_in end_in.
826  ∀the_trace_out : trace_any_label S_out f_out start_out end_out.
827  tal_rel … the_trace_in the_trace_out →
828  (Σ_{l ∈ flatten_trace_any_label … the_trace_in}
829    (lift_cost_map_id … dec m_out l)) =
830  (Σ_{l ∈ flatten_trace_any_label … the_trace_out} (m_out l)).
831#S_in #S_out #dec #m_out #f_in #f_out #start_in #start_out #end_in #end_out
832#tal_in #tal_out #H_tal
833@(lift_cost_map_same_cost … (tal_rel_to_traces_same_flatten … H_tal))
834qed.
835
836lemma lift_cost_map_same_cost_tll :
837  ∀S_in, S_out, dec, m_out, f_in, f_out, start_in, start_out, end_in, end_out.
838  ∀the_trace_in : trace_label_label S_in f_in start_in end_in.
839  ∀the_trace_out : trace_label_label S_out f_out start_out end_out.
840  tll_rel … the_trace_in the_trace_out →
841  (Σ_{l ∈ flatten_trace_label_label … the_trace_in}
842    (lift_cost_map_id … dec m_out l)) =
843  (Σ_{l ∈ flatten_trace_label_label … the_trace_out} (m_out l)).
844#S_in #S_out #dec #m_out #f_in #f_out #start_in #start_out #end_in #end_out
845#tll_in #tll_out #H_tll
846@(lift_cost_map_same_cost … (tll_rel_to_traces_same_flatten … H_tll))
847qed.
848
849lemma lift_cost_map_same_cost_tlr :
850  ∀S_in, S_out, dec, m_out, start_in, start_out, end_in, end_out.
851  ∀the_trace_in : trace_label_return S_in start_in end_in.
852  ∀the_trace_out : trace_label_return S_out start_out end_out.
853  tlr_rel … the_trace_in the_trace_out →
854  (Σ_{l ∈ flatten_trace_label_return … the_trace_in}
855    (lift_cost_map_id … dec m_out l)) =
856  (Σ_{l ∈ flatten_trace_label_return … the_trace_out} (m_out l)).
857#S_in #S_out #dec #m_out #start_in #start_out #end_in #end_out
858#tlr_in #tlr_out #H_tlr
859@(lift_cost_map_same_cost … (tlr_rel_to_traces_same_flatten … H_tlr))
860qed.
Note: See TracBrowser for help on using the repository browser.