source: src/common/StructuredTraces.ma @ 2548

Last change on this file since 2548 was 2531, checked in by mckinna, 7 years ago

Trivial tweaks.

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