source: src/common/StructuredTraces.ma @ 2417

Last change on this file since 2417 was 2417, checked in by boender, 7 years ago
  • reverted changes to StructuredTraces? (shouldn't have been committed yet)
  • updated _jaap files to remove function identifier from cl_call and add an as_call predicate
File size: 35.0 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 «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 «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 [1,3: //]
375#H73 #H74 #H75 #H76 #H77 #H78 #H79 #H80 #H81 #H82 #H83 //
376qed.
377
378(*
379(* an trace of unlabeled and cl_other states, possibly empty *)
380inductive trace_no_label_any (S:abstract_status) : S → S → Type[0] ≝
381  | tna_base :
382      ∀start_status: S.
383      ¬as_costed … start_status →
384      trace_no_label_any S start_status start_status
385  | tna_step :
386      ∀status_pre: S.
387      ∀status_init: S.
388      ∀status_end: S.
389        as_execute S status_pre status_init →
390        as_classifier S status_pre cl_other →
391        ¬as_costed … status_pre →
392        trace_no_label_any S status_init status_end →
393          trace_no_label_any S status_pre status_end.
394
395let rec tna_append_tna S st1 st2 st3 (taa1 : trace_no_label_any S st1 st2)
396  on taa1 :
397    trace_no_label_any S st2 st3 →
398    trace_no_label_any S st1 st3 ≝
399  match taa1 with
400  [ tna_base st1' H ⇒ λtaa2.taa2
401  | tna_step st1' st2' st3' H G K tl ⇒
402    λtaa2.tna_step ???? H G K (tna_append_tna … tl taa2)
403  ].
404
405definition tna_non_empty ≝ λS,st1,st2.λtna : trace_no_label_any S st1 st2.
406  match tna with
407  [ tna_base _ _ ⇒ false
408  | tna_step _ _ _ _ _ _ _ ⇒ true
409  ].
410
411coercion tna_to_bool : ∀S,st1,st2.∀tna:trace_no_label_any S st1 st2.bool ≝
412 tna_non_empty on _tna : trace_no_label_any ??? to bool.
413
414lemma tna_unlabelled : ∀S,st1,st2.trace_no_label_any S st1 st2 → ¬as_costed … st1.
415#S #st1 #st2 * [#st #H @H | #st #st' #st'' #_ #_ #H #_ @H] qed.
416
417let rec tna_append_tal S st1 fl st2 st3 (tna : trace_no_label_any S st1 st2)
418  on tna :
419  trace_any_label S fl st2 st3 →
420  if tna then Not (as_costed … st2) else True →
421  trace_any_label S fl st1 st3 ≝
422  match tna return λst1,st2.λx : trace_no_label_any S st1 st2.
423    ∀fl,st3.
424    trace_any_label S fl st2 st3 →
425    if x then Not (as_costed … st2) else True →
426    trace_any_label S fl st1 st3
427  with
428  [ tna_base st1' H ⇒ λfl,st3,taa2,prf.taa2
429  | tna_step st1' st2' st3' H G K tl ⇒ λfl,st3,taa2,prf.
430    tal_step_default ????? H (tna_append_tal ????? tl taa2 ?) G (tna_unlabelled … tl)
431  ] fl st3.
432  cases (tna_non_empty … tl) [@prf|%]
433  qed.
434*)
435
436inductive trace_any_any (S : abstract_status) : S → S → Type[0] ≝
437  | taa_base : ∀st.trace_any_any S st st
438  | taa_step : ∀st1,st2,st3.
439    as_execute S st1 st2 →
440    as_classifier S st1 cl_other →
441    ¬as_costed S st2 →
442    trace_any_any S st2 st3 →
443    trace_any_any S st1 st3.
444
445definition taa_non_empty ≝ λS,st1,st2.λtaa : trace_any_any S st1 st2.
446  match taa with
447  [ taa_base _ ⇒ false
448  | taa_step _ _ _ _ _ _ _ ⇒ true
449  ].
450
451coercion taa_to_bool : ∀S,st1,st2.∀taa:trace_any_any S st1 st2.bool ≝
452 taa_non_empty on _taa : trace_any_any ??? to bool.
453
454let rec taa_append_tal S st1 fl st2 st3
455  (taa : trace_any_any S st1 st2) on taa :
456  trace_any_label S fl st2 st3 →
457  trace_any_label S fl st1 st3 ≝
458  match taa return λst1,st2.λx : trace_any_any S st1 st2.
459    ∀fl,st3.
460    trace_any_label S fl st2 st3 →
461    trace_any_label S fl st1 st3
462  with
463  [ taa_base st1' ⇒ λfl,st3,tal2.tal2
464  | taa_step st1' st2' st3' H G K tl ⇒ λfl,st3,tal2.
465    tal_step_default ????? H (taa_append_tal ????? tl tal2) G K
466  ] fl st3.
467
468interpretation "trace any any label append" 'append taa tal = (taa_append_tal ????? taa tal).
469
470let rec tal_collapsable S fl s1 s2 (tal : trace_any_label S fl s1 s2) on tal : Prop ≝
471match tal with
472[ tal_base_not_return _ _ _ _ _ ⇒ True
473| tal_step_default fl1 _ st1' st1'' _ tl1 _ _ ⇒ tal_collapsable … tl1
474| _ ⇒ False
475].
476
477let rec tlr_rel S1 st1 st1' S2 st2 st2'
478  (tlr1 : trace_label_return S1 st1 st1')
479  (tlr2 : trace_label_return S2 st2 st2') on tlr1 : Prop ≝
480match tlr1 with
481  [ tlr_base st1 st1' tll1 ⇒
482    match tlr2 with
483    [ tlr_base st2 st2' tll2 ⇒ tll_rel … tll1 tll2
484    | _ ⇒ False
485    ]
486  | tlr_step st1 st1' st1'' tll1 tl1 ⇒
487    match tlr2 with
488    [ tlr_step st2 st2' st2'' tll2 tl2 ⇒
489      tll_rel … tll1 tll2 ∧ tlr_rel … tl1 tl2
490    | _ ⇒ False
491    ]
492  ]
493and tll_rel S1 fl1 st1 st1' S2 fl2 st2 st2'
494 (tll1 : trace_label_label S1 fl1 st1 st1')
495 (tll2 : trace_label_label S2 fl2 st2 st2') on tll1 : Prop ≝
496  match tll1 with
497  [ tll_base fl1 st1 st1' tal1 H ⇒
498    match tll2 with
499    [ tll_base fl2 st2 st2 tal2 G ⇒
500      as_label_safe … («?, H») = as_label_safe … («?, G») ∧
501      tal_rel … tal1 tal2
502    ]
503  ]
504and tal_rel S1 fl1 st1 st1' S2 fl2 st2 st2'
505 (tal1 : trace_any_label S1 fl1 st1 st1')
506 (tal2 : trace_any_label S2 fl2 st2 st2')
507   on tal1 : Prop ≝
508  match tal1 with
509  [ tal_base_not_return st1 st1' _ _ _ ⇒
510    fl2 = doesnt_end_with_ret ∧
511    ∃st2mid,taa,H,G,K.
512    tal2 ≃ taa_append_tal ? st2 ??? taa
513      (tal_base_not_return ? st2mid st2' H G K)
514  | tal_base_return st1 st1' _ _ ⇒
515    fl2 = ends_with_ret ∧
516    ∃st2mid,taa,H,G.
517    tal2 ≃ taa_append_tal ? st2 ? st2mid st2' taa
518      (tal_base_return ? st2mid st2' H G)
519  | tal_base_call st1 st1' st1'' _ _ _ tlr1 _ ⇒
520    fl2 = doesnt_end_with_ret ∧
521    ∃st2mid.∃taa : trace_any_any ? st2 st2mid.∃st2mid',H.
522    (* we must allow a tal_base_call to be similar to a call followed
523      by a collapsable trace (trace_any_any followed by a base_not_return;
524      we cannot use trace_any_any as it disallows labels in the end as soon
525      as it is non-empty) *)
526    (∃G,K.∃tlr2 : trace_label_return ? st2mid' st2'.∃L.
527      tal2 ≃ taa @ (tal_base_call … H G K tlr2 L) ∧ tlr_rel … tlr1 tlr2) ∨
528    ∃st2mid'',G,K.∃tlr2 : trace_label_return ? st2mid' st2mid''.∃L.
529    ∃tl2 : trace_any_label … doesnt_end_with_ret st2mid'' st2'.
530      tal2 ≃ taa @ (tal_step_call … H G K tlr2 L tl2) ∧
531      tlr_rel … tlr1 tlr2 ∧ tal_collapsable … tl2
532  | tal_step_call fl1 st1 st1' st1'' st1''' _ _ _ tlr1 _ tl1 ⇒
533    ∃st2mid.∃taa : trace_any_any ? st2 st2mid.∃st2mid',H.
534    (fl2 = doesnt_end_with_ret ∧ ∃G,K.∃tlr2 : trace_label_return ? st2mid' st2'.∃L.
535      tal2 ≃ taa @ tal_base_call … H G K tlr2 L ∧
536      tal_collapsable … tl1 ∧ tlr_rel … tlr1 tlr2) ∨
537    ∃st2mid'',G,K.∃tlr2 : trace_label_return ? st2mid' st2mid''.∃L.
538    ∃tl2 : trace_any_label ? fl2 st2mid'' st2'.
539      tal2 ≃ taa @ (tal_step_call … H G K tlr2 L tl2) ∧
540      tal_rel … tl1 tl2 ∧ tlr_rel … tlr1 tlr2
541  | tal_step_default fl1 st1 st1' st1'' _ tl1 _ _ ⇒
542    tal_rel … tl1 tal2 (* <- this makes it many to many *)
543  ].
544
545interpretation "trace any label rel" 'napart t1 t2 = (tal_rel ???????? t1 t2).
546interpretation "trace label label rel" 'napart t1 t2 = (tll_rel ???????? t1 t2).
547interpretation "trace label return rel" 'napart t1 t2 = (tlr_rel ?????? t1 t2).
548
549let rec tal_collapsable_eq_fl S1 fl1 s1 s1'
550  (tal1 : trace_any_label S1 fl1 s1 s1') on tal1 :
551  tal_collapsable … tal1 → fl1 = doesnt_end_with_ret ≝ ?.
552cases tal1 -fl1 -s1 -s1' //
553[ #s1 #s1' #H #I *
554| #s1 #s1' #s1'' #s1''' #s1'''' #H #I #J #tlr #K #tl *
555| #fl1 #s1 #s1' #s1'' #H #tl #I #J @(tal_collapsable_eq_fl … tl)
556]
557qed.
558
559let rec tal_rel_eq_fl S1 fl1 s1 s1' S2 fl2 s2 s2'
560  (tal1 : trace_any_label S1 fl1 s1 s1') on tal1 :
561  ∀tal2 : trace_any_label S2 fl2 s2 s2'.tal_rel … tal1 tal2 → fl1 = fl2 ≝
562  match tal1 return λfl1,s1,s1',tal1.? with
563  [ tal_base_not_return st1 st1' _ _ _ ⇒ let BASE_NR ≝ 0 in ?
564  | tal_base_return st1 st1' _ _ ⇒ let BASE_R ≝ 0 in ?
565  | tal_base_call st1 st1' st1'' _ _ _ tlr1 _ ⇒ let BASE_C ≝ 0 in ?
566  | tal_step_call flg1 st1 st1' st1'' st1''' _ _ _ tlr1 _ tl1 ⇒ let STEP_C ≝ 0 in ?
567  | tal_step_default flg1 st1 st1' st1'' _ tl1 _ _ ⇒ let STEP ≝ 0 in ?
568  ].
569-fl1 -s1 -s1'
570[1,2,3: -tal_rel_eq_fl #tal2 * //
571| #tal2 * #s2_mid * #taa2 * #s2' *#H2 *
572  [ * #EQ1 *#G2 *#K2 *#tlr2 *#L2 ** #_ #coll #_ >(tal_collapsable_eq_fl … coll) //
573  | * #s2_mid' *#G2 *#K2 *#tlr2 *#L2 *#tl2 ** #_ #step #_
574     @(tal_rel_eq_fl … step)
575  ]
576| #tal2 whd in ⊢ (%→?); #step @(tal_rel_eq_fl … step)
577]
578qed.
579
580let rec taa_rel_inv S1 fl1 st1 st1mid st1' S2 fl2 st2 st2'
581  (taa1 : trace_any_any S1 st1 st1mid) on taa1 :
582  ∀tal1 : trace_any_label S1 fl1 st1mid st1'.
583  ∀tal2 : trace_any_label S2 fl2 st2 st2'.
584  tal_rel … (taa1 @ tal1) tal2 →
585  tal_rel … tal1 tal2 ≝ ?.
586cases taa1 -taa1
587[ -taa_rel_inv //
588| #st #st' #st'' #H #G #K #tl #tal1 #tal2 whd in ⊢ (%→?);
589  @(taa_rel_inv … tl)
590]
591qed.
592
593lemma taa_append_collapsable : ∀S,s1,fl,s2,s3.
594  ∀taa,tal.tal_collapsable S fl s2 s3 tal → tal_collapsable S fl s1 s3 (taa@tal).
595  #S #s1 #fl #s2 #s3 #taa elim taa -s1 -s2 /2/
596qed.
597
598let rec tal_rel_collapsable S1 fl1 s1 s1' S2 fl2 s2 s2'
599  (tal1 : trace_any_label S1 fl1 s1 s1') on tal1 :
600  ∀tal2 : trace_any_label S2 fl2 s2 s2'.tal_collapsable … tal1 → tal_rel … tal1 tal2 →
601  tal_collapsable … tal2 ≝
602  match tal1 return λfl1,s1,s1',tal1.? with
603  [ tal_base_not_return st1 st1' _ _ _ ⇒ let BASE_NR ≝ 0 in ?
604  | tal_base_return st1 st1' _ _ ⇒ let BASE_R ≝ 0 in ?
605  | tal_base_call st1 st1' st1'' _ _ _ tlr1 _ ⇒ let BASE_C ≝ 0 in ?
606  | tal_step_call flg1 st1 st1' st1'' st1''' _ _ _ tlr1 _ tl1 ⇒ let STEP_C ≝ 0 in ?
607  | tal_step_default flg1 st1 st1' st1'' _ tl1 _ _ ⇒ let STEP ≝ 0 in ?
608  ].
609-fl1 -s1 -s1'
610[1,2,3: -tal_rel_collapsable #tal2 * *
611  #EQ * #s2 * #taa2 *#H *#G *#K #EQ' destruct @taa_append_collapsable %
612| #tal2 *
613| #tal2 #tal2 whd in ⊢ (%→?); #step @(tal_rel_collapsable … step) assumption
614]
615qed.
616
617let rec flatten_trace_label_label
618  (S: abstract_status) (trace_ends_flag: trace_ends_with_ret)
619    (start_status: S) (final_status: S)
620      (the_trace: trace_label_label S trace_ends_flag start_status final_status)
621        on the_trace: list (Σl: costlabel. ∃pc. as_label_of_pc S pc = Some … l) ≝
622  match the_trace with
623  [ tll_base ends_flag initial final given_trace labelled_proof ⇒
624      let label ≝
625        match as_label … initial return λx: option costlabel. x ≠ None costlabel → ? with
626        [ None ⇒ λabs. ⊥
627        | Some l ⇒ λ_. l
628        ] labelled_proof
629      in
630        (mk_Sig … label ?)::flatten_trace_any_label S ends_flag initial final given_trace
631  ]
632and flatten_trace_any_label
633  (S: abstract_status) (trace_ends_flag: trace_ends_with_ret)
634    (start_status: S) (final_status: S)
635      (the_trace: trace_any_label S trace_ends_flag start_status final_status)
636        on the_trace: list (Σl: costlabel.  ∃pc. as_label_of_pc S pc = Some … l) ≝
637  match the_trace with
638  [ tal_base_not_return the_status _ _ _ _ ⇒ [ ]
639  | tal_base_call pre_fun_call start_fun_call final _ _ _ call_trace _ ⇒
640      flatten_trace_label_return … call_trace
641  | tal_base_return the_status _ _ _ ⇒ [ ]
642  | tal_step_call end_flag pre_fun_call start_fun_call after_fun_call final
643    _ _ _ call_trace _ final_trace ⇒
644    let call_cost_trace ≝ flatten_trace_label_return … call_trace in
645    let final_cost_trace ≝ flatten_trace_any_label … end_flag … final_trace in
646        call_cost_trace @ final_cost_trace
647  | tal_step_default end_flag status_pre status_init status_end _ tail_trace _ _ ⇒
648      flatten_trace_any_label … tail_trace
649  ]
650and flatten_trace_label_return
651  (S: abstract_status)
652    (start_status: S) (final_status: S)
653      (the_trace: trace_label_return S start_status final_status)
654        on the_trace: list (Σl: costlabel.  ∃pc. as_label_of_pc S pc = Some … l) ≝
655  match the_trace with
656  [ tlr_base before after trace_to_lift ⇒
657      flatten_trace_label_label … trace_to_lift
658  | tlr_step initial labelled final labelled_trace ret_trace ⇒
659    let labelled_cost ≝ flatten_trace_label_label … doesnt_end_with_ret … labelled_trace in
660    let return_cost ≝ flatten_trace_label_return … ret_trace in
661        labelled_cost @ return_cost
662  ].
663  [2:
664    cases abs -abs #abs @abs %
665  |1:
666    %{(as_pc_of … initial)} whd in match label;
667    change with (as_label ?? = ?)
668    generalize in match labelled_proof; whd in ⊢ (% → ?);
669    cases (as_label S initial)
670    [1:
671      #absurd @⊥ cases absurd -absurd #absurd @absurd %
672    |2:
673      #costlabel normalize nodelta #_ %
674    ]
675  ]
676qed.
677
678let rec taa_append_tal_same_flatten
679  S st1 fl st2 st3 (taa : trace_any_any S st1 st2) on taa :
680  ∀tal : trace_any_label S fl st2 st3.
681    flatten_trace_any_label … (taa @ tal) =
682      flatten_trace_any_label … tal ≝ ?.
683cases taa -st1 -st2
684[ //
685| #st_pre #st_init #st2 #H #G #K #taa' #tal
686  whd in match (? @ ?);
687  whd in ⊢ (??%?); //
688]
689qed.
690
691let rec tal_collapsable_flatten S fl st1 st2 tal
692  on tal :
693  tal_collapsable S fl st1 st2 tal → flatten_trace_any_label … tal = [ ] ≝
694match tal
695return λfl,st1,st2,tal.tal_collapsable S fl st1 st2 tal → flatten_trace_any_label … tal = [ ]
696with
697[ tal_base_not_return the_status _ _ _ _ ⇒ λ_.refl ??
698| tal_step_default end_flag status_pre status_init status_end _ tail_trace _ _ ⇒
699    tal_collapsable_flatten ???? tail_trace
700| _ ⇒ Ⓧ
701].
702
703let rec tll_rel_to_traces_same_flatten
704  (S: abstract_status) (S': abstract_status)
705    (trace_ends_flag_l: trace_ends_with_ret) (trace_ends_flag_r: trace_ends_with_ret)
706    (start_status_l: S) (final_status_l: S) (start_status_r: S') (final_status_r: S')
707      (the_trace_l: trace_label_label S trace_ends_flag_l start_status_l final_status_l)
708        (the_trace_r: trace_label_label S' trace_ends_flag_r start_status_r final_status_r)
709          on the_trace_l:
710            tll_rel … the_trace_l the_trace_r →
711              map … (pi1 …) (flatten_trace_label_label … the_trace_l) =
712                map … (pi1 …) (flatten_trace_label_label … the_trace_r) ≝
713  match the_trace_l with
714  [ tll_base fl1 st1 st1' tal1 H ⇒
715    match the_trace_r with
716    [ tll_base fl2 st2 st2 tal2 G ⇒ ?
717    ]
718  ]
719and tal_rel_to_traces_same_flatten
720  (S: abstract_status) (S': abstract_status) (trace_ends_flag_l: trace_ends_with_ret)
721    (trace_ends_flag_r: trace_ends_with_ret)
722      (start_status_l: S) (final_status_l: S) (start_status_r: S') (final_status_r: S')
723        (the_trace_l: trace_any_label S trace_ends_flag_l start_status_l final_status_l)
724          (the_trace_r: trace_any_label S' trace_ends_flag_r start_status_r final_status_r)
725          on the_trace_l:
726            tal_rel … the_trace_l the_trace_r →
727              map … (pi1 …) (flatten_trace_any_label … the_trace_l) =
728                map … (pi1 …) (flatten_trace_any_label … the_trace_r) ≝
729  match the_trace_l with
730  [ tal_base_not_return st1 st1' H G K ⇒ ?
731  | tal_base_return st1 st1' H G ⇒ ?
732  | tal_base_call st1 st1' st1'' H G K tlr1 L ⇒ ?
733  | tal_step_call fl1 st1 st1' st1'' st1''' H G K tlr1 L tl1 ⇒ ?
734  | tal_step_default fl1 st1 st1' st1'' H tl1 G K ⇒ ?
735  ]
736and tlr_rel_to_traces_same_flatten
737  (S: abstract_status) (S': abstract_status) (start_status_l: S) (final_status_l: S)
738    (start_status_r: S') (final_status_r: S')
739      (the_trace_l: trace_label_return S start_status_l final_status_l)
740        (the_trace_r: trace_label_return S' start_status_r final_status_r)
741        on the_trace_l:
742          tlr_rel … the_trace_l the_trace_r →
743            map … (pi1 …) (flatten_trace_label_return … the_trace_l) =
744              map … (pi1 …) (flatten_trace_label_return … the_trace_r) ≝
745  match the_trace_l with
746  [ tlr_base before after tll_l ⇒ ?
747  | tlr_step initial labelled final tll_l tlr_l ⇒ ?
748  ]. 
749[ * whd in match as_label_safe; normalize nodelta
750  @opt_safe_elim #l1 #EQ1
751  @opt_safe_elim #l2 #EQ2
752  #EQ destruct(EQ) #H_tal
753  change with (? :: ? = ? :: ?) lapply H -H lapply G -G
754  whd in match as_costed; normalize nodelta
755  >EQ1 >EQ2 normalize nodelta #_ #_
756  >(tal_rel_to_traces_same_flatten … H_tal) @refl
757|2,3,4,5,6:
758  [1,2,3: * #EQ destruct(EQ)]
759  [1,2,3,4: * #st_mid * #taa
760    [ *#H' *#G' *#K' #EQ
761    | *#H' *#G' #EQ
762    | *#st_mid' *#H' * [|*#st2_mid''] *#G' *#K' *#tlr2 *#L'
763      [|*#tl2 *] * #EQ #H_tlr [| #H_tl]
764    | *#st_fun *#H' *
765      [*#fl_EQ destruct(fl_EQ) |* #st2_mid ] *#G' *#K' *#tlr2 *#L'
766      [| *#tl2] ** #EQ #H_tl #H_tlr
767    ] >EQ >taa_append_tal_same_flatten
768  | whd in ⊢ (%→??(????%)?);
769    @tal_rel_to_traces_same_flatten
770  ]
771  [1,2: %
772  |3: @(tlr_rel_to_traces_same_flatten … H_tlr)
773  |4,5: <map_append
774    >(tal_collapsable_flatten … H_tl) >append_nil
775    >(tlr_rel_to_traces_same_flatten … H_tlr) %
776  |6: <map_append
777    >(tlr_rel_to_traces_same_flatten … H_tlr)
778    >(tal_rel_to_traces_same_flatten … H_tl)
779    @map_append
780  ]
781|*: cases the_trace_r
782  [1,3: #st_before_r #st_after_r #tll_r
783    [ @tll_rel_to_traces_same_flatten | * ]
784  |*: #st_init_r #st_labld_r #st_fin_r #tll_r #tlr_r *
785    #H_tll #H_tlr
786    <map_append
787    >(tll_rel_to_traces_same_flatten … H_tll)
788    >(tlr_rel_to_traces_same_flatten … H_tlr)
789    @map_append
790  ]
791]
792qed.
793
794definition as_cost_map ≝
795  λS : abstract_status. (Σl.∃pc.as_label_of_pc S pc = Some ? l) → ℕ.
796
797definition lift_sigma_map_id :
798  ∀A,B : Type[0].∀P_in,P_out : A → Prop.B →
799    (∀a.P_out a + ¬ P_out a) →
800  ((Σa.P_out a) → B) → (Σa.P_in a) → B ≝ λA,B,P_in,P_out,dflt,dec,m,a_sig.
801   match dec a_sig with
802   [ inl prf ⇒ m «a_sig, prf»
803   | inr _ ⇒ dflt (* labels not present in out code get 0 *)
804   ].
805
806lemma lift_sigma_map_id_eq :
807  ∀A,B,P_in,P_out,dflt,dec,m,a_in,a_out.
808  pi1 ?? a_in = pi1 ?? a_out →
809  lift_sigma_map_id A B P_in P_out dflt dec m a_in = m a_out.
810#A#B#P_in#P_out#dflt#dec#m#a_in#a_out#EQ
811whd in match lift_sigma_map_id; normalize nodelta
812cases (dec a_in) normalize nodelta >EQ cases a_out
813#a #H #G [ % | @⊥ /2 by absurd/ ]
814qed.
815
816notation > "Σ_{ ident i ∈ l } f"
817  with precedence 20
818  for @{'fold plus 0 (λ${ident i}.true) (λ${ident i}. $f) $l}.
819notation < "Σ_{ ident i ∈ l } f"
820  with precedence 20
821for @{'fold plus 0 (λ${ident i}:$X.true) (λ${ident i}:$Y. $f) $l}.
822
823definition lift_cost_map_id :
824  ∀S_in,S_out : abstract_status.? →
825  as_cost_map S_out → as_cost_map S_in
826  ≝
827 λS_in,S_out : abstract_status.
828  lift_sigma_map_id costlabel ℕ
829    (λl.∃pc.as_label_of_pc S_in pc = Some ? l)
830    (λl.∃pc.as_label_of_pc S_out pc = Some ? l)
831    0.
832
833lemma lift_cost_map_same_cost :
834  ∀S_in, S_out, dec, m_out, trace_in, trace_out.
835  map … (pi1 ??) trace_in = map … (pi1 ??) trace_out →
836  (Σ_{ l_sig ∈ trace_in } (lift_cost_map_id S_in S_out dec m_out l_sig)) =
837  (Σ_{ l_sig ∈ trace_out } (m_out l_sig)).
838#S_in #S_out #dec #m_out #trace_in elim trace_in
839[2: #hd_in #tl_in #IH] * [2,4: #hd_out #tl_out]
840normalize in ⊢ (%→?);
841[2,3: #ABS destruct(ABS)
842|4: #_ %
843|1:
844  #EQ destruct
845  whd in ⊢(??%%);
846  whd in match lift_cost_map_id; normalize nodelta
847  >(lift_sigma_map_id_eq ????????? e0)
848  >e0 in e1; normalize in ⊢(%→?); #EQ
849  >(IH … EQ) %
850]
851qed.
852
853lemma lift_cost_map_same_cost_tal :
854  ∀S_in, S_out, dec, m_out, f_in, f_out, start_in, start_out, end_in, end_out.
855  ∀the_trace_in : trace_any_label S_in f_in start_in end_in.
856  ∀the_trace_out : trace_any_label S_out f_out start_out end_out.
857  tal_rel … the_trace_in the_trace_out →
858  (Σ_{l ∈ flatten_trace_any_label … the_trace_in}
859    (lift_cost_map_id … dec m_out l)) =
860  (Σ_{l ∈ flatten_trace_any_label … the_trace_out} (m_out l)).
861#S_in #S_out #dec #m_out #f_in #f_out #start_in #start_out #end_in #end_out
862#tal_in #tal_out #H_tal
863@(lift_cost_map_same_cost … (tal_rel_to_traces_same_flatten … H_tal))
864qed.
865
866lemma lift_cost_map_same_cost_tll :
867  ∀S_in, S_out, dec, m_out, f_in, f_out, start_in, start_out, end_in, end_out.
868  ∀the_trace_in : trace_label_label S_in f_in start_in end_in.
869  ∀the_trace_out : trace_label_label S_out f_out start_out end_out.
870  tll_rel … the_trace_in the_trace_out →
871  (Σ_{l ∈ flatten_trace_label_label … the_trace_in}
872    (lift_cost_map_id … dec m_out l)) =
873  (Σ_{l ∈ flatten_trace_label_label … the_trace_out} (m_out l)).
874#S_in #S_out #dec #m_out #f_in #f_out #start_in #start_out #end_in #end_out
875#tll_in #tll_out #H_tll
876@(lift_cost_map_same_cost … (tll_rel_to_traces_same_flatten … H_tll))
877qed.
878
879lemma lift_cost_map_same_cost_tlr :
880  ∀S_in, S_out, dec, m_out, start_in, start_out, end_in, end_out.
881  ∀the_trace_in : trace_label_return S_in start_in end_in.
882  ∀the_trace_out : trace_label_return S_out start_out end_out.
883  tlr_rel … the_trace_in the_trace_out →
884  (Σ_{l ∈ flatten_trace_label_return … the_trace_in}
885    (lift_cost_map_id … dec m_out l)) =
886  (Σ_{l ∈ flatten_trace_label_return … the_trace_out} (m_out l)).
887#S_in #S_out #dec #m_out #start_in #start_out #end_in #end_out
888#tlr_in #tlr_out #H_tlr
889@(lift_cost_map_same_cost … (tlr_rel_to_traces_same_flatten … H_tlr))
890qed.
Note: See TracBrowser for help on using the repository browser.