source: src/ASM/CostsProof.ma @ 1577

Last change on this file since 1577 was 1577, checked in by mulligan, 8 years ago

A lot more cases added to the proof at the bottom of execute_1_preinstruction, thought these make use of tacticals, with one in particular taking 1000+ seconds to complete. Changed all the Russell specifications in Interpret.ma to use the new, simplified specification.

File size: 37.5 KB
Line 
1
2include "ASM/ASMCosts.ma".
3include "ASM/WellLabeled.ma".
4include "ASM/Status.ma".
5include "common/StructuredTraces.ma".
6
7(*
8definition next_instruction_is_labelled ≝
9  λcost_labels: BitVectorTrie Byte 16.
10  λs: Status.
11  let pc ≝ program_counter … (execute_1 s) in
12    match lookup_opt … pc cost_labels with
13    [ None ⇒ False
14    | _    ⇒ True
15    ].
16
17definition current_instruction_cost ≝
18  λs: Status. \snd (fetch (code_memory … s) (program_counter … s)).
19
20definition is_call_p ≝
21  λs.
22  match s with
23  [ ACALL _ ⇒ True
24  | LCALL _ ⇒ True
25  | JMP ⇒ True (* XXX: is function pointer call *)
26  | _ ⇒ False
27  ].
28
29definition next_instruction ≝
30  λs: Status.
31  let 〈instruction, ignore, ignore〉 ≝ fetch (code_memory … s) (program_counter … s) in
32    instruction.
33
34inductive trace_ends_with_ret: Type[0] ≝
35  | ends_with_ret: trace_ends_with_ret
36  | doesnt_end_with_ret: trace_ends_with_ret.
37
38definition next_instruction_is_labelled ≝
39  λcost_labels: BitVectorTrie Byte 16.
40  λs: Status.
41  let pc ≝ program_counter … (execute 1 s) in
42    match lookup_opt … pc cost_labels with
43    [ None ⇒ False
44    | _    ⇒ True
45    ].
46
47definition next_instruction_properly_relates_program_counters ≝
48  λbefore: Status.
49  λafter : Status.
50  let size ≝ current_instruction_cost before in
51  let pc_before ≝ program_counter … before in
52  let pc_after ≝ program_counter … after in
53  let sum ≝ \snd (half_add … pc_before (bitvector_of_nat … size)) in
54    sum = pc_after.
55
56definition current_instruction ≝
57  λs: Status.
58  let pc ≝ program_counter … s in
59  \fst (\fst (fetch … (code_memory … s) pc)).
60
61definition current_instruction_is_labelled ≝
62  λcost_labels: BitVectorTrie Byte 16.
63  λs: Status.
64  let pc ≝ program_counter … s in
65    match lookup_opt … pc cost_labels with
66    [ None ⇒ False
67    | _    ⇒ True
68    ].
69
70inductive trace_label_return (cost_labels: BitVectorTrie Byte 16): Status → Status → Type[0] ≝
71  | tlr_base:
72      ∀status_before: Status.
73      ∀status_after: Status.
74        trace_label_label cost_labels ends_with_ret status_before status_after →
75        trace_label_return cost_labels status_before status_after
76  | tlr_step:
77      ∀status_initial: Status.
78      ∀status_labelled: Status.
79      ∀status_final: Status.
80          trace_label_label cost_labels doesnt_end_with_ret status_initial status_labelled →
81            trace_label_return cost_labels status_labelled status_final →
82              trace_label_return cost_labels status_initial status_final
83with trace_label_label: trace_ends_with_ret → Status → Status → Type[0] ≝
84  | tll_base:
85      ∀ends_flag: trace_ends_with_ret.
86      ∀start_status: Status.
87      ∀end_status: Status.
88        trace_label_label_pre cost_labels ends_flag start_status end_status →
89        current_instruction_is_labelled cost_labels start_status →
90        trace_label_label cost_labels ends_flag start_status end_status
91with trace_label_label_pre: trace_ends_with_ret → Status → Status → Type[0] ≝
92  | tllp_base_not_return:
93      ∀start_status: Status.
94        let final_status ≝ execute 1 start_status in
95        current_instruction start_status ≠ (RealInstruction … (RET [[relative]])) →
96        ¬ (is_jump_p (current_instruction start_status)) →
97        current_instruction_is_labelled cost_labels final_status →
98          trace_label_label_pre cost_labels doesnt_end_with_ret start_status final_status
99  | tllp_base_return:
100      ∀start_status: Status.
101        let final_status ≝ execute 1 start_status in
102          current_instruction start_status = (RealInstruction … (RET [[relative]])) →
103            trace_label_label_pre cost_labels ends_with_ret start_status final_status
104  | tllp_base_jump:
105      ∀start_status: Status.
106        let final_status ≝ execute 1 start_status in
107          is_jump_p (current_instruction start_status) →
108            current_instruction_is_labelled cost_labels final_status →
109              trace_label_label_pre cost_labels doesnt_end_with_ret start_status final_status
110  | tllp_step_call:
111      ∀end_flag: trace_ends_with_ret.
112      ∀status_pre_fun_call: Status.
113      ∀status_after_fun_call: Status.
114      ∀status_final: Status.
115        let status_start_fun_call ≝ execute 1 status_pre_fun_call in
116          is_call_p (current_instruction status_pre_fun_call) →
117          next_instruction_properly_relates_program_counters status_pre_fun_call status_after_fun_call →
118          trace_label_return cost_labels status_start_fun_call status_after_fun_call →
119          trace_label_label_pre cost_labels end_flag status_after_fun_call status_final →
120            trace_label_label_pre cost_labels end_flag status_pre_fun_call status_final
121  | tllp_step_default:
122      ∀end_flag: trace_ends_with_ret.
123      ∀status_pre: Status.
124      ∀status_end: Status.
125        let status_init ≝ execute 1 status_pre in
126          trace_label_label_pre cost_labels end_flag status_init status_end →
127          ¬ (is_call_p (current_instruction status_pre)) →
128          ¬ (is_jump_p (current_instruction status_pre)) →
129          (current_instruction status_pre) ≠ (RealInstruction … (RET [[relative]])) →
130          ¬ (current_instruction_is_labelled cost_labels status_init) →
131            trace_label_label_pre cost_labels end_flag status_pre status_end.
132*)
133
134(* XXX: not us
135definition compute_simple_path0 (s:Status) (is_labelled s) (well_balanced s) (labelled_p p): trace_lab_ret ≝ ….
136
137lemma simple_path_ok:
138 ∀st: Status.∀H.
139 let p ≝ compute_simple_path0 st H in
140   ∀n.
141    nth_path n p = execute n st ∧
142     (execute' (path_length p) st = 〈st',τ〉 →
143      τ = cost_trace p)
144  ].
145*)
146
147let rec compute_max_trace_label_label_cost
148  (cost_labels: BitVectorTrie costlabel 16)
149   (trace_ends_flag: trace_ends_with_ret)
150    (start_status: Status) (final_status: Status)
151      (the_trace: trace_label_label (ASM_abstract_status cost_labels) trace_ends_flag
152        start_status final_status) on the_trace: nat ≝
153  match the_trace with
154  [ tll_base ends_flag initial final given_trace labelled_proof ⇒
155      compute_max_trace_any_label_cost … given_trace
156  ]
157and compute_max_trace_any_label_cost
158  (cost_labels: BitVectorTrie costlabel 16)
159  (trace_ends_flag: trace_ends_with_ret)
160   (start_status: Status) (final_status: Status)
161     (the_trace: trace_any_label (ASM_abstract_status cost_labels) trace_ends_flag start_status final_status)
162       on the_trace: nat ≝
163  match the_trace with
164  [ tal_base_not_return the_status _ _ _ _ ⇒ current_instruction_cost the_status
165  | tal_base_return the_status _ _ _ ⇒ current_instruction_cost the_status
166  | tal_step_call end_flag pre_fun_call start_fun_call after_fun_call final
167     _ _ _ call_trace final_trace ⇒
168      let current_instruction_cost ≝ current_instruction_cost pre_fun_call in
169      let call_trace_cost ≝ compute_max_trace_label_return_cost … call_trace in
170      let final_trace_cost ≝ compute_max_trace_any_label_cost cost_labels end_flag … final_trace in
171        call_trace_cost + current_instruction_cost + final_trace_cost
172  | tal_step_default end_flag status_pre status_init status_end _ tail_trace _ _ ⇒
173      let current_instruction_cost ≝ current_instruction_cost status_pre in
174      let tail_trace_cost ≝
175       compute_max_trace_any_label_cost cost_labels end_flag
176        status_init status_end tail_trace
177      in
178        current_instruction_cost + tail_trace_cost
179  ]
180and compute_max_trace_label_return_cost
181  (cost_labels: BitVectorTrie costlabel 16)
182  (start_status: Status) (final_status: Status)
183    (the_trace: trace_label_return (ASM_abstract_status cost_labels) start_status final_status)
184      on the_trace: nat ≝
185  match the_trace with
186  [ tlr_base before after trace_to_lift ⇒ compute_max_trace_label_label_cost … trace_to_lift
187  | tlr_step initial labelled final labelled_trace ret_trace ⇒
188      let labelled_cost ≝ compute_max_trace_label_label_cost … labelled_trace in
189      let return_cost ≝ compute_max_trace_label_return_cost … ret_trace in
190        labelled_cost + return_cost
191  ].
192
193(*Useless now?
194(* To be moved *)
195lemma pred_minus_1:
196  ∀m, n: nat.
197  ∀proof: n < m.
198    pred (m - n) = m - n - 1.
199  #m #n
200  cases m
201  [ #proof
202    cases(lt_to_not_zero … proof)
203  | #m' #proof
204    normalize in ⊢ (???%);
205    cases n
206    [ normalize //
207    | #n' normalize
208      cases(m' - n')
209      [ %
210      | #Sm_n'
211        normalize //
212      ]
213    ]
214  ]
215qed.
216
217lemma succ_m_plus_one:
218  ∀m: nat.
219    S m = m + 1.
220 //
221qed.*)
222
223include alias "arithmetics/nat.ma".
224
225(*
226lemma minus_m_n_minus_minus_plus_1:
227  ∀m, n: nat.
228  ∀proof: n < m.
229    m - n = (m - n - 1) + 1.
230 /3 by lt_plus_to_minus_r, plus_minus/
231qed.
232*)
233
234lemma plus_minus_rearrangement_1:
235  ∀m, n, o: nat.
236  ∀proof_n_m: n ≤ m.
237  ∀proof_m_o: m ≤ o.
238    (m - n) + (o - m) = o - n.
239  #m #n #o #H1 #H2
240  lapply (minus_to_plus … H1 (refl …)) #K1 >K1
241  lapply (minus_to_plus … H2 (refl …)) #K2 >K2
242  /2 by plus_minus/
243qed.
244
245lemma plus_minus_rearrangement_2:
246  ∀m, n, o: nat. n ≤ m → o ≤ n →
247    (m - n) + (n - o) = m - o.
248 /2 by plus_minus_rearrangement_1/
249qed.
250
251axiom clock_execute_1:
252  ∀s: Status.
253    clock … (execute_1 s) = current_instruction_cost s + clock … s.
254
255axiom plus_le_le:
256  ∀m, n, o: nat.
257    m + n ≤ o → n ≤ o.
258   
259let rec compute_max_trace_label_label_cost_is_ok
260  (cost_labels: BitVectorTrie costlabel 16)
261   (trace_ends_flag: trace_ends_with_ret)
262    (start_status: Status) (final_status: Status)
263      (the_trace: trace_label_label (ASM_abstract_status cost_labels) trace_ends_flag
264        start_status final_status) on the_trace:
265          And (compute_max_trace_label_label_cost cost_labels trace_ends_flag start_status final_status the_trace =
266            (clock … final_status) - (clock … start_status)) (clock … start_status ≤ clock … final_status) ≝ ?
267and compute_max_trace_any_label_cost_is_ok
268  (cost_labels: BitVectorTrie costlabel 16)
269  (trace_ends_flag: trace_ends_with_ret)
270   (start_status: Status) (final_status: Status)
271     (the_trace: trace_any_label (ASM_abstract_status cost_labels) trace_ends_flag start_status final_status)
272       on the_trace:
273         (compute_max_trace_any_label_cost cost_labels trace_ends_flag start_status final_status the_trace =
274           (clock … final_status) - (clock … start_status)) ∧ (clock … start_status ≤ clock … final_status) ≝ ?
275and compute_max_trace_label_return_cost_is_ok
276  (cost_labels: BitVectorTrie costlabel 16)
277  (start_status: Status) (final_status: Status)
278    (the_trace: trace_label_return (ASM_abstract_status cost_labels) start_status final_status)
279      on the_trace:
280        (compute_max_trace_label_return_cost cost_labels start_status final_status the_trace =
281          (clock … final_status) - (clock … start_status)) ∧ (clock … start_status ≤ clock … final_status) ≝ ?.
282  [1:
283    cases the_trace
284    #ends_flag #start_status #end_status #any_label_trace #is_costed
285    normalize @compute_max_trace_any_label_cost_is_ok
286  |2:
287    cases the_trace
288    [1:
289      #start_status #final_status #is_next #is_not_return #is_costed
290      change with (current_instruction_cost start_status) in ⊢ (?(??%?)?);
291      cases(is_next)
292      @conj
293      [1:
294        cases(execute_1 start_status)
295        #the_status #assm
296        whd in match eject; normalize nodelta
297        cases(assm)
298        #eq_hyp #le_hyp
299        >eq_hyp %
300      |2:
301        cases(execute_1 start_status)
302        #the_status #assm
303        whd in match eject; normalize nodelta
304        cases(assm)
305        #eq_hyp #le_hyp
306        assumption
307      ]
308    |2:
309      #start_status #final_status #is_next #is_return
310      change with (current_instruction_cost start_status) in ⊢ (?(??%?)?);
311      cases(is_next)
312      @conj
313      [1:
314        cases(execute_1 start_status)
315        #the_status #assm
316        whd in match eject; normalize nodelta
317        cases(assm)
318        #eq_hyp #le_hyp
319        >eq_hyp %
320      |2:
321        cases(execute_1 start_status)
322        #the_status #assm
323        whd in match eject; normalize nodelta
324        cases(assm)
325        #eq_hyp #le_hyp
326        assumption
327      ]
328    |3:
329      #end_flag #status_pre_fun_call #status_start_fun_call #status_after_fun_call
330      #status_final #is_next #is_call #is_after_return #call_trace #final_trace
331      change with (
332      let current_instruction_cost ≝ current_instruction_cost status_pre_fun_call in
333      let call_trace_cost ≝ compute_max_trace_label_return_cost … call_trace in
334      let final_trace_cost ≝ compute_max_trace_any_label_cost cost_labels end_flag … final_trace in
335        call_trace_cost + current_instruction_cost + final_trace_cost) in ⊢ (?(??%?)?);
336      normalize nodelta;
337      @conj
338      [1:
339        cases(compute_max_trace_label_return_cost_is_ok cost_labels status_start_fun_call
340          status_after_fun_call call_trace)
341        #trace_label_return_eq #trace_label_return_le
342        cases(compute_max_trace_any_label_cost_is_ok cost_labels end_flag status_after_fun_call
343          status_final final_trace)
344        #trace_any_label_eq #trace_any_label_le
345        >trace_label_return_eq >trace_any_label_eq
346        cases(is_next)
347        >(clock_execute_1 status_pre_fun_call) in match (clock … (execute_1 status_pre_fun_call));
348        >commutative_plus in match (
349          (current_instruction_cost status_pre_fun_call
350            + clock (BitVectorTrie Byte 16) status_pre_fun_call));
351        <minus_plus
352        <(plus_minus_m_m (clock (BitVectorTrie Byte 16) status_after_fun_call
353  -clock (BitVectorTrie Byte 16) status_pre_fun_call) (current_instruction_cost status_pre_fun_call))
354        [1:
355          >plus_minus_rearrangement_1
356          [1:
357            %
358          |2:
359            assumption
360          |3:
361            generalize in match trace_label_return_le;
362            cases(is_next)
363            >(clock_execute_1 status_pre_fun_call)
364            @plus_le_le
365          ]
366        |2:
367          cases daemon (* XXX: todo *)
368        ]
369      |2:
370        cases(compute_max_trace_label_return_cost_is_ok cost_labels status_start_fun_call
371          status_after_fun_call call_trace)
372        #trace_label_return_eq cases(is_next)
373        >clock_execute_1
374        #trace_label_return_le
375        lapply (plus_le_le … trace_label_return_le)
376        #trace_label_return_le'
377        cases(compute_max_trace_any_label_cost_is_ok cost_labels end_flag status_after_fun_call
378          status_final final_trace)
379        #trace_any_label_eq #trace_any_label_le
380        @(transitive_le (clock … status_pre_fun_call) (clock … status_after_fun_call) (clock … status_final))
381        assumption
382      ]
383    |4:
384      #end_flag #status_pre #status_init #status_end #is_next
385      #trace_any_label #is_other #is_not_costed
386      change with (
387      let current_instruction_cost ≝ current_instruction_cost status_pre in
388      let tail_trace_cost ≝
389       compute_max_trace_any_label_cost cost_labels end_flag
390        status_init status_end trace_any_label
391      in
392        current_instruction_cost + tail_trace_cost) in ⊢ (?(??%?)?);
393      normalize nodelta;
394      @conj
395      [1:
396        cases(compute_max_trace_any_label_cost_is_ok cost_labels end_flag
397        status_init status_end trace_any_label)
398        #trace_any_label_eq #trace_any_label_le
399        >trace_any_label_eq
400        cases(is_next)
401        >commutative_plus
402        >clock_execute_1
403        >commutative_plus in match ((current_instruction_cost status_pre+clock (BitVectorTrie Byte 16) status_pre));
404        <minus_plus
405        <plus_minus_m_m
406        [1: %
407        |2: cases daemon (* XXX: todo *)
408        ]
409      |2:
410        cases(compute_max_trace_any_label_cost_is_ok cost_labels end_flag
411        status_init status_end trace_any_label)
412        #trace_any_label_eq
413        cases(is_next)
414        >clock_execute_1
415        @plus_le_le
416      ]
417    ]
418  |3:
419    cases the_trace
420    [1:
421      #status_before #status_after #trace_to_lift
422      normalize @compute_max_trace_label_label_cost_is_ok
423    |2:
424      #status_initial #status_labelled #status_final #labelled_trace #ret_trace
425      normalize
426      cases(compute_max_trace_label_label_cost_is_ok cost_labels doesnt_end_with_ret status_initial status_labelled labelled_trace);
427      #label_label_trace_equality #label_label_trace_leq
428      cases(compute_max_trace_label_return_cost_is_ok cost_labels status_labelled status_final ret_trace);
429      #label_return_trace_equality #label_return_trace_leq
430      >label_label_trace_equality >label_return_trace_equality
431      @conj
432      [2: @(transitive_le (clock (BitVectorTrie (Vector bool 8) 16) status_initial)
433            (clock (BitVectorTrie Byte 16) status_labelled)
434            (clock (BitVectorTrie (Vector bool 8) 16) status_final))
435      |1: @plus_minus_rearrangement_1
436      ]
437      assumption
438    ]
439  ]
440qed.
441
442(* XXX: work below here: *)
443
444let rec compute_paid_trace_any_label
445  (cost_labels: BitVectorTrie costlabel 16)
446  (trace_ends_flag: trace_ends_with_ret)
447   (start_status: Status) (final_status: Status)
448     (the_trace: trace_any_label (ASM_abstract_status cost_labels) trace_ends_flag start_status final_status)
449       on the_trace: nat ≝
450  match the_trace with
451  [ tal_base_not_return the_status _ _ _ _ ⇒ current_instruction_cost the_status
452  | tal_base_return the_status _ _ _ ⇒ current_instruction_cost the_status
453  | tal_step_call end_flag pre_fun_call start_fun_call after_fun_call final
454     _ _ _ call_trace final_trace ⇒
455      let current_instruction_cost ≝ current_instruction_cost pre_fun_call in
456      let final_trace_cost ≝ compute_paid_trace_any_label cost_labels end_flag … final_trace in
457        current_instruction_cost + final_trace_cost
458  | tal_step_default end_flag status_pre status_init status_end _ tail_trace _ _ ⇒
459      let current_instruction_cost ≝ current_instruction_cost status_pre in
460      let tail_trace_cost ≝
461       compute_paid_trace_any_label cost_labels end_flag
462        status_init status_end tail_trace
463      in
464        current_instruction_cost + tail_trace_cost
465  ].
466
467definition compute_paid_trace_label_label ≝
468 λcost_labels: BitVectorTrie costlabel 16.
469  λtrace_ends_flag: trace_ends_with_ret.
470   λstart_status: Status.
471    λfinal_status: Status.
472     λthe_trace: trace_label_label (ASM_abstract_status cost_labels) trace_ends_flag
473      start_status final_status.
474  match the_trace with
475  [ tll_base ends_flag initial final given_trace labelled_proof ⇒
476      compute_paid_trace_any_label … given_trace
477  ].
478
479let rec compute_trace_label_label_cost_using_paid
480  (cost_labels: BitVectorTrie costlabel 16)
481   (trace_ends_flag: trace_ends_with_ret)
482    (start_status: Status) (final_status: Status)
483      (the_trace: trace_label_label (ASM_abstract_status cost_labels) trace_ends_flag
484        start_status final_status) on the_trace: nat ≝
485  match the_trace with
486  [ tll_base ends_flag initial final given_trace labelled_proof ⇒
487      compute_paid_trace_label_label cost_labels … the_trace +
488      compute_trace_any_label_cost_using_paid … given_trace
489  ]
490and compute_trace_any_label_cost_using_paid
491  (cost_labels: BitVectorTrie costlabel 16)
492  (trace_ends_flag: trace_ends_with_ret)
493   (start_status: Status) (final_status: Status)
494     (the_trace: trace_any_label (ASM_abstract_status cost_labels) trace_ends_flag start_status final_status)
495       on the_trace: nat ≝
496  match the_trace with
497  [ tal_base_not_return the_status _ _ _ _ ⇒ 0
498  | tal_base_return the_status _ _ _ ⇒ 0
499  | tal_step_call end_flag pre_fun_call start_fun_call after_fun_call final
500     _ _ _ call_trace final_trace ⇒
501      let call_trace_cost ≝ compute_trace_label_return_cost_using_paid … call_trace in
502      let final_trace_cost ≝ compute_trace_any_label_cost_using_paid cost_labels end_flag … final_trace in
503        call_trace_cost + final_trace_cost
504  | tal_step_default end_flag status_pre status_init status_end _ tail_trace _ _ ⇒
505     compute_trace_any_label_cost_using_paid cost_labels end_flag
506      status_init status_end tail_trace
507  ]
508and compute_trace_label_return_cost_using_paid
509  (cost_labels: BitVectorTrie costlabel 16)
510  (start_status: Status) (final_status: Status)
511    (the_trace: trace_label_return (ASM_abstract_status cost_labels) start_status final_status)
512      on the_trace: nat ≝
513  match the_trace with
514  [ tlr_base before after trace_to_lift ⇒ compute_trace_label_label_cost_using_paid … trace_to_lift
515  | tlr_step initial labelled final labelled_trace ret_trace ⇒
516      let labelled_cost ≝ compute_trace_label_label_cost_using_paid … labelled_trace in
517      let return_cost ≝ compute_trace_label_return_cost_using_paid … ret_trace in
518        labelled_cost + return_cost
519  ].
520
521let rec compute_trace_label_label_cost_using_paid_ok
522  (cost_labels: BitVectorTrie costlabel 16)
523   (trace_ends_flag: trace_ends_with_ret)
524    (start_status: Status) (final_status: Status)
525      (the_trace: trace_label_label (ASM_abstract_status cost_labels) trace_ends_flag
526        start_status final_status) on the_trace:
527 compute_trace_label_label_cost_using_paid cost_labels … the_trace =
528 compute_max_trace_label_label_cost … the_trace ≝ ?
529and compute_trace_any_label_cost_using_paid_ok
530  (cost_labels: BitVectorTrie costlabel 16)
531  (trace_ends_flag: trace_ends_with_ret)
532   (start_status: Status) (final_status: Status)
533     (the_trace: trace_any_label (ASM_abstract_status cost_labels)
534      trace_ends_flag start_status final_status) on the_trace:     
535  compute_paid_trace_any_label cost_labels trace_ends_flag … the_trace
536  +compute_trace_any_label_cost_using_paid cost_labels trace_ends_flag … the_trace
537  =compute_max_trace_any_label_cost cost_labels trace_ends_flag … the_trace ≝ ?
538and compute_trace_label_return_cost_using_paid_ok
539  (cost_labels: BitVectorTrie costlabel 16)
540  (start_status: Status) (final_status: Status)
541    (the_trace: trace_label_return (ASM_abstract_status cost_labels)
542     start_status final_status) on the_trace:
543 compute_trace_label_return_cost_using_paid cost_labels … the_trace =
544 compute_max_trace_label_return_cost cost_labels … the_trace ≝ ?.
545[ cases the_trace #endsf #ss #es #tr #H normalize
546  @compute_trace_any_label_cost_using_paid_ok
547| cases the_trace
548  [ #ss #fs #H1 #H2 #H3 whd in ⊢ (??(?%%)%); <plus_n_O %
549  | #ss #fs #H1 #H2 whd in ⊢ (??(?%%)%); <plus_n_O %
550  | #ef #spfc #ssfc #safc #sf #H1 #H2 #H3 #tr1 #tr2 whd in ⊢ (??(?%%)%);
551    <compute_trace_any_label_cost_using_paid_ok
552    <compute_trace_label_return_cost_using_paid_ok
553    -compute_trace_label_label_cost_using_paid_ok
554    -compute_trace_label_return_cost_using_paid_ok
555    -compute_trace_any_label_cost_using_paid_ok
556    >commutative_plus in ⊢ (???(?%?));
557    >commutative_plus in ⊢ (??(??%)?);
558    >associative_plus >associative_plus in ⊢ (???%); @eq_f2 try %
559    <associative_plus <commutative_plus %
560  | #ef #sp #si #se #H1 #tr #H2 #H3 whd in ⊢ (??(?%%)%); >associative_plus @eq_f2
561    [ % | @compute_trace_any_label_cost_using_paid_ok ]
562  ]
563| cases the_trace
564  [ #sb #sa #tr normalize @compute_trace_label_label_cost_using_paid_ok
565  | #si #sl #sf #tr1 #tr2 normalize @eq_f2
566    [ @compute_trace_label_label_cost_using_paid_ok
567    | @compute_trace_label_return_cost_using_paid_ok ]]]
568qed.
569
570(*
571let rec compute_paid_trace_label_return
572  (cost_labels: BitVectorTrie costlabel 16)
573  (start_status: Status) (final_status: Status)
574    (the_trace: trace_label_return (ASM_abstract_status cost_labels) start_status final_status)
575      on the_trace: nat ≝
576 match the_trace with
577  [ tlr_base before after trace_to_lift ⇒ compute_paid_trace_label_label … trace_to_lift
578  | tlr_step initial labelled final labelled_trace ret_trace ⇒
579      let labelled_cost ≝ compute_paid_trace_label_label … labelled_trace in
580      let return_cost ≝ compute_paid_trace_label_return … ret_trace in
581        labelled_cost + return_cost
582  ].
583*)
584
585let rec compute_cost_trace_label_label
586  (cost_labels: BitVectorTrie costlabel 16)
587   (trace_ends_flag: trace_ends_with_ret)
588    (start_status: Status) (final_status: Status)
589      (the_trace: trace_label_label (ASM_abstract_status cost_labels) trace_ends_flag
590        start_status final_status) on the_trace:
591         list (Σk:costlabel. ∃pc. lookup_opt … pc cost_labels = Some … k) ≝
592  match the_trace with
593  [ tll_base ends_flag initial final given_trace labelled_proof ⇒
594     let pc ≝ program_counter … initial in
595     let label ≝
596      match lookup_opt … pc cost_labels return λx. match x with [None ⇒ False | Some _ ⇒ True] → costlabel with
597      [ None ⇒ λabs. ⊥
598      | Some l ⇒ λ_. l ] labelled_proof in
599     (dp … label ?)::compute_cost_trace_any_label … given_trace
600  ]
601and compute_cost_trace_any_label
602  (cost_labels: BitVectorTrie costlabel 16)
603  (trace_ends_flag: trace_ends_with_ret)
604   (start_status: Status) (final_status: Status)
605     (the_trace: trace_any_label (ASM_abstract_status cost_labels) trace_ends_flag start_status final_status)
606       on the_trace: list (Σk:costlabel. ∃pc. lookup_opt … pc cost_labels = Some … k) ≝
607  match the_trace with
608  [ tal_base_not_return the_status _ _ _ _ ⇒ []
609  | tal_base_return the_status _ _ _ ⇒ []
610  | tal_step_call end_flag pre_fun_call start_fun_call after_fun_call final
611     _ _ _ call_trace final_trace ⇒
612      let call_cost_trace ≝ compute_cost_trace_label_return … call_trace in
613      let final_cost_trace ≝ compute_cost_trace_any_label cost_labels end_flag … final_trace in
614        call_cost_trace @ final_cost_trace
615  | tal_step_default end_flag status_pre status_init status_end _ tail_trace _ _ ⇒
616       compute_cost_trace_any_label cost_labels end_flag
617        status_init status_end tail_trace
618  ]
619and compute_cost_trace_label_return
620  (cost_labels: BitVectorTrie costlabel 16)
621  (start_status: Status) (final_status: Status)
622    (the_trace: trace_label_return (ASM_abstract_status cost_labels) start_status final_status)
623      on the_trace: list (Σk:costlabel. ∃pc. lookup_opt … pc cost_labels = Some … k) ≝
624  match the_trace with
625  [ tlr_base before after trace_to_lift ⇒ compute_cost_trace_label_label … trace_to_lift
626  | tlr_step initial labelled final labelled_trace ret_trace ⇒
627      let labelled_cost ≝ compute_cost_trace_label_label … labelled_trace in
628      let return_cost ≝ compute_cost_trace_label_return … ret_trace in
629        labelled_cost @ return_cost
630  ].
631 [ %{pc} whd in match label;  generalize in match labelled_proof; whd in ⊢ (% → ?);
632  cases (lookup_opt costlabel … pc cost_labels) normalize
633  [ #abs cases abs | // ]
634 | // ]
635qed.
636
637(* ??????????????????????? *)
638axiom block_cost_static_dynamic_ok:
639 ∀cost_labels: BitVectorTrie costlabel 16.
640 ∀trace_ends_flag.
641 ∀start_status: Status.
642 ∀final_status: Status.
643 ∀the_trace:
644  trace_label_label (ASM_abstract_status cost_labels) trace_ends_flag
645   start_status final_status.
646 let mem ≝ code_memory … start_status in
647 let pc ≝ program_counter … start_status in
648 let program_size ≝ 2^16 in
649  block_cost mem cost_labels pc program_size =
650  compute_paid_trace_label_label cost_labels trace_ends_flag
651   start_status final_status the_trace.
652
653(*
654(* To be moved elsewhere*)
655lemma le_S_n_m_to_le_n_m: ∀n,m. S n ≤ m → n ≤ m.
656 #n #m #H change with (pred (S n) ≤ m) @(transitive_le ? (S n)) //
657qed.
658*)
659
660include "arithmetics/bigops.ma".
661
662(* This shoudl go in bigops! *)
663theorem bigop_sum_rev: ∀k1,k2,p,B,nil.∀op:Aop B nil.∀f:nat→B.
664 \big[op,nil]_{i<k1+k2|p i} (f i) =
665 op \big[op,nil]_{i<k2|p (i+k1)} (f (i+k1)) \big[op,nil]_{i<k1|p i} (f i).
666 #k1 #k2 #p #B #nil #op #f >bigop_sum >commutative_plus @same_bigop #i @leb_elim normalize
667 [2,4: //
668 | #H1 #H2 <plus_minus_m_m //
669 | #H1 #H2 #H3 <plus_minus_m_m //]
670qed.
671
672(* This is taken by sigma_pi.ma that does not compile now *)
673definition natAop ≝ mk_Aop nat 0 plus (λa.refl ? a) (λn.sym_eq ??? (plus_n_O n))
674   (λa,b,c.sym_eq ??? (associative_plus a b c)).
675
676definition natACop ≝ mk_ACop nat 0 natAop commutative_plus.
677
678definition natDop ≝ mk_Dop nat 0 natACop times (λn.(sym_eq ??? (times_n_O n)))
679   distributive_times_plus.
680
681unification hint  0 ≔ ;
682   S ≟ mk_Aop nat 0 plus (λa.refl ? a) (λn.sym_eq ??? (plus_n_O n))
683   (λa,b,c.sym_eq ??? (associative_plus a b c))
684(* ---------------------------------------- *) ⊢
685   plus ≡ op ? ? S.
686
687unification hint  0 ≔ ;
688   S ≟ mk_ACop nat 0 (mk_Aop nat 0 plus (λa.refl ? a) (λn.sym_eq ??? (plus_n_O n))
689   (λa,b,c.sym_eq ??? (associative_plus a b c))) commutative_plus
690(* ---------------------------------------- *) ⊢
691   plus ≡ op ? ? S.
692   
693unification hint  0 ≔ ;
694   S ≟ natDop
695(* ---------------------------------------- *) ⊢
696   plus ≡ sum ? ? S.
697
698unification hint  0 ≔ ;
699   S ≟ natDop
700(* ---------------------------------------- *) ⊢
701   times ≡ prod ? ? S.
702
703notation "Σ_{ ident i < n } f"
704  with precedence 20
705for @{'bigop $n plus 0 (λ${ident i}.true) (λ${ident i}. $f)}.
706
707axiom code_memory_ro_label_label:
708 ∀cost_labels. ∀ends_flag.
709 ∀initial,final. trace_label_label (ASM_abstract_status cost_labels) ends_flag initial final →
710  code_memory … initial = code_memory … final.
711
712(*
713axiom code_memory_ro_label_return:
714 ∀cost_labels.
715 ∀initial,final. trace_label_return (ASM_abstract_status cost_labels) initial final →
716  code_memory … initial = code_memory … final.
717*)
718
719definition tech_cost_of_label0:
720 ∀cost_labels: BitVectorTrie costlabel 16.
721 ∀cost_map: identifier_map CostTag nat.
722 ∀codom_dom: (∀pc,k. lookup_opt … pc cost_labels = Some … k → present … cost_map k).
723 ∀ctrace:list (Σk:costlabel.∃pc. lookup_opt costlabel 16 pc cost_labels = Some ? k).
724 ∀i,p. present … cost_map (nth_safe ? i ctrace p).
725 #cost_labels #cost_map #codom_dom #ctrace #i #p
726 cases (nth_safe … i ctrace ?) normalize #id * #id_pc #K
727 lapply (codom_dom … K) #k_pres >(lookup_lookup_present … k_pres)
728 % #abs destruct (abs)
729qed.
730
731lemma ltb_rect:
732 ∀P:Type[0].∀n,m. (n < m → P) → (¬ n < m → P) → P.
733 #P #n #m lapply (refl … (ltb n m)) cases (ltb n m) in ⊢ (???% → %); #E #H1 #H2
734 [ @H1 @leb_true_to_le @E | @H2 @leb_false_to_not_le @E ]
735qed.
736
737lemma same_ltb_rect:
738 ∀P,n,m,H1,H2,n',m',H1',H2'.
739  ltb n m = ltb n' m' → (∀x,y. H1 x = H1' y) → (∀x,y. H2 x = H2' y) →
740   ltb_rect P n m H1 H2 =
741   ltb_rect P n' m' H1' H2'.
742 #P #n #m #H1 #H2 #n' #m' #H1' #H2' #E #K1 #K2 whd in ⊢ (??%?);
743 cut (∀xxx,yyy,xxx',yyy'.
744   match ltb n m
745   return λx:bool.
746           eq bool (ltb n m) x
747            → (lt n m → P) → (Not (lt n m) → P) → P
748    with
749    [ true ⇒
750        λE0:eq bool (ltb n m) true.
751         λH10:lt n m → P.
752          λH20:Not (lt n m) → P. H10 (xxx E0)
753    | false ⇒
754        λE0:eq bool (ltb n m) false.
755         λH10:lt n m → P.
756          λH20:Not (lt n m) → P. H20 (yyy E0)]
757    (refl … (ltb n m)) H1 H2 =
758   match ltb n' m'
759   return λx:bool.
760           eq bool (ltb n' m') x
761            → (lt n' m' → P) → (Not (lt n' m') → P) → P
762    with
763    [ true ⇒
764        λE0:eq bool (ltb n' m') true.
765         λH10:lt n' m' → P.
766          λH20:Not (lt n' m') → P. H10 (xxx' E0)
767    | false ⇒
768        λE0:eq bool (ltb n' m') false.
769         λH10:lt n' m' → P.
770          λH20:Not (lt n' m') → P. H20 (yyy' E0)]
771    (refl … (ltb n' m')) H1' H2'
772  ) [2: #X @X]
773 >E cases (ltb n' m') #xxx #yyy #xxx' #yyy' normalize
774 [ @K1 | @K2 ]
775qed.
776
777
778definition tech_cost_of_label:
779 ∀cost_labels: BitVectorTrie costlabel 16.
780 ∀cost_map: identifier_map CostTag nat.
781 ∀codom_dom: (∀pc,k. lookup_opt … pc cost_labels = Some … k → present … cost_map k).
782 list (Σk:costlabel.∃pc. lookup_opt costlabel 16 pc cost_labels = Some ? k) →
783 nat → nat
784≝ λcost_labels,cost_map,codom_dom,ctrace,i.
785 ltb_rect ? i (|ctrace|)
786 (λH. lookup_present ?? cost_map (nth_safe ? i ctrace H) ?)
787 (λ_.0).
788 @tech_cost_of_label0 @codom_dom
789qed.
790
791lemma shift_nth_safe:
792 ∀T,i,l2,l1,K1,K2.
793  nth_safe T i l1 K1 = nth_safe T (i+|l2|) (l2@l1) K2.
794 #T #i #l2 elim l2 normalize
795  [ #l1 #K1 <plus_n_O #K2 %
796  | #hd #tl #IH #l1 #K1 <plus_n_Sm #K2 change with (? < ?) in K1; change with (? < ?) in K2;
797    whd in ⊢ (???%); @IH ]
798qed.
799
800lemma tech_cost_of_label_shift:
801 ∀cost_labels,cost_map,codom_dom,l1,l2,i.
802  i < |l1| →
803   tech_cost_of_label cost_labels cost_map codom_dom l2 i =
804   tech_cost_of_label cost_labels cost_map codom_dom (l1@l2) (i+|l1|).
805 #cost_labels #cost_Map #codom_dom #l1 #l2 #i #H
806 cut (ltb i (|l2|) = ltb (i+|l1|) (|l1@l2|))
807 [ change with (leb ?? = leb ??); @leb_elim
808   [ #H1 change with (i < ?) in H1; >le_to_leb_true [//] change with (? < ?)
809     >append_length >commutative_plus @monotonic_lt_plus_r //
810   | #H1 change with (¬ i < ?) in H1; >not_le_to_leb_false [//]
811     >append_length >commutative_plus % #H2
812     change with (? < ?) in H2; @(absurd ?? H1) @(lt_plus_to_lt_r … H2) ]
813 | #E whd in match tech_cost_of_label; normalize nodelta
814 @same_ltb_rect [@E]
815 [ #K1 #K2 lapply (shift_nth_safe ? i l1 l2 K1 K2) #H check eq_f
816   lapply (eq_f ?? (lookup_present CostTag nat cost_Map) ?? H)
817
818   
819 
820  cut (decide_bool (ltb i (|l2|)) = decide_bool (ltb (i+(|l1|)) (|l1@l2|)))
821  whd in match tech_cost_of_label; normalize nodelta
822   
823
824let rec compute_max_trace_label_return_cost_ok_with_trace
825 (cost_labels: BitVectorTrie costlabel 16)
826 (cost_map: identifier_map CostTag nat)
827 (initial: Status) (final: Status)
828 (trace: trace_label_return (ASM_abstract_status cost_labels) initial final)
829 (codom_dom: (∀pc,k. lookup_opt … pc cost_labels = Some … k → present … cost_map k))
830 (dom_codom:(∀k. ∀k_pres:present … cost_map k. ∃pc. lookup_opt … pc cost_labels = Some … k →
831   block_cost (code_memory … initial) cost_labels pc 2^16 = lookup_present … k_pres))
832 on trace:
833  let ctrace ≝ compute_cost_trace_label_return … trace in
834  compute_max_trace_label_return_cost … trace =
835   Σ_{i < |ctrace|} (tech_cost_of_label cost_labels cost_map codom_dom ctrace i)
836    ≝ ?.
837lapply (refl … initial) cases trace in ⊢ (??%? → %);
838[ #sb #sa #tr #_ whd in ⊢ (let ctrace ≝ % in ??%?);
839| #si #sl #sf #tr1 #tr2 #E whd in ⊢ (let ctrace ≝ % in ??%?);
840  change with (let ctrace ≝ ? in ? = bigop (|? @ ?|) ?????)
841  >append_length in ⊢ (let ctrace ≝ ? in ???(?%?????));
842  change with (?=?) >bigop_sum_rev >commutative_plus @eq_f2
843  [ >compute_max_trace_label_return_cost_ok_with_trace
844    -compute_max_trace_label_return_cost_ok_with_trace     
845    [3: @cost_map
846    |2: @codom_dom
847    |4:lapply (code_memory_ro_label_label … tr1) >E #E2 <E2 @dom_codom]
848    @same_bigop [//] #i #H #_ -dom_codom
849    generalize in match (compute_cost_trace_label_return cost_labels sl sf tr2); #l1
850    generalize in match (compute_cost_trace_label_label cost_labels doesnt_end_with_ret si sl tr1);
851
852i < |l1| →
853tech_cost_of_label cost_labels cost_map codom_dom l1 i =
854tech_cost_of_label cost_labels cost_map codom_dom (l2@l1) (i+|l1|)
855
856
857     whd in ⊢ (??%%);
858  |
859  ]
860]
861
862lemma
863 compute_max_trace_any_label_cost cost_label trace_ends_flag start_status
864  final_status the_trace =
865 
866
867let rec compute_paid_trace_label_label_cost
868  (cost_labels: BitVectorTrie Byte 16)
869   (trace_ends_flag: trace_ends_with_ret)
870    (start_status: Status) (final_status: Status)
871      (the_trace: trace_label_label (ASM_abstract_status cost_labels) trace_ends_flag
872        start_status final_status) on the_trace: nat ≝
873  match the_trace with
874  [ tll_base _ _ _ given_trace _ ⇒
875      compute_paid_trace_any_label_cost … given_trace
876  ]
877and compute_paid_trace_any_label_cost
878  (cost_labels: BitVectorTrie Byte 16)
879  (trace_ends_flag: trace_ends_with_ret)
880   (start_status: Status) (final_status: Status)
881     (the_trace: trace_any_label (ASM_abstract_status cost_labels) trace_ends_flag start_status final_status)
882       on the_trace: nat ≝
883  match the_trace with
884  [ tal_base_not_return the_status _ _ _ _ ⇒ current_instruction_cost the_status
885  | tal_base_return the_status _ _ _ ⇒ current_instruction_cost the_status
886  | tal_step_call end_flag pre_fun_call start_fun_call after_fun_call final
887     _ _ _ call_trace final_trace ⇒
888      let current_instruction_cost ≝ current_instruction_cost pre_fun_call in
889      let final_trace_cost ≝ compute_paid_trace_any_label_cost cost_labels end_flag … final_trace in
890        current_instruction_cost + final_trace_cost
891  | tal_step_default end_flag status_pre status_init status_end _ tail_trace _ _ ⇒
892      let current_instruction_cost ≝ current_instruction_cost status_pre in
893      let tail_trace_cost ≝
894       compute_paid_trace_any_label_cost cost_labels end_flag
895        status_init status_end tail_trace
896      in
897        current_instruction_cost + tail_trace_cost
898  ]
899and compute_paid_trace_label_return_cost
900  (cost_labels: BitVectorTrie Byte 16)
901  (start_status: Status) (final_status: Status)
902    (the_trace: trace_label_return (ASM_abstract_status cost_labels) start_status final_status)
903      on the_trace: nat ≝
904  match the_trace with
905  [ tlr_base before after trace_to_lift ⇒ compute_paid_trace_label_label_cost … trace_to_lift
906  | tlr_step initial labelled final labelled_trace ret_trace ⇒
907      let labelled_cost ≝ compute_paid_trace_label_label_cost … labelled_trace in
908      let return_cost ≝ compute_paid_trace_label_return_cost … ret_trace in
909        labelled_cost + return_cost
910  ].
911
912let rec trace_lab_rec_cost' (p: trace_lab_ret) : nat.
913 | (call b bf tr af tl) as self ⇒
914    trace_lab_lab_cost_nocall self + trace_lab_ret_cost' tr +
915    trace_lab_rec_cost' tl
916
917theorem main_lemma:
918 ∀p. trace_lab_rec_cost p = trace_lab_rec_cost' p.
919
920axiom lemma1:
921 ∀p: simple_path.
922  traverse_cost_internal (pc (hd p)) … = trace_lab_lab_cost_nocall p.
923
924axiom lemma2:
925 ∀s,l,cost_map.
926  is_labelled l s →
927   traverse_cost_internal s = cost_map l.
928
929axiom main_statement:
930 ∀s.
931 ∀cost_map.
932 let p ≝ compute_simple_path0 s in
933 ∑ (cost_trace p) cost_map = trace_lab_rec_cost' p.
934
935axiom main_statement:
936 ∀s.
937 ∀cost_map.
938 let p ≝ compute_simple_path0 s in
939  execute' (path_length p) s = 〈s',τ〉 →
940   Timer s' - Timer s = ∑ τ cost_map.
Note: See TracBrowser for help on using the repository browser.