source: src/ASM/CostsProof.ma @ 1575

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

Changes to specifications on execute functions

File size: 36.4 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 current_instruction_cost_non_zero:
252  ∀s: Status.
253    current_instruction_cost s > 0.
254
255lemma le_monotonic_plus:
256  ∀m, n, o: nat.
257    m ≤ n → m + o ≤ n + o.
258  #m #n #o #hyp /2/
259qed.
260
261lemma le_plus_to_minus_l:
262  ∀m, n, o: nat.
263    n ≤ o → m ≤ o - n → m + n ≤ o.
264  #m #n #o #le_hyp #hyp
265  generalize in match (le_monotonic_plus m (o - n) n hyp);
266  generalize in match (plus_minus_m_m o n le_hyp);
267  #assm <assm in ⊢ (% → ?); #finished assumption
268qed.
269   
270axiom minus_m_n_gt_o_O_le_n_m:
271  ∀m, n, o.
272    m - n = o → o > 0 → n ≤ m.
273(*  #m #n #o #eq_hyp <eq_hyp in ⊢ (% → ?);
274  normalize in ⊢ (% → ?);
275  >le_monot *)
276
277let rec compute_max_trace_label_label_cost_is_ok
278  (cost_labels: BitVectorTrie costlabel 16)
279   (trace_ends_flag: trace_ends_with_ret)
280    (start_status: Status) (final_status: Status)
281      (the_trace: trace_label_label (ASM_abstract_status cost_labels) trace_ends_flag
282        start_status final_status) on the_trace:
283          And (compute_max_trace_label_label_cost cost_labels trace_ends_flag start_status final_status the_trace =
284            (clock … final_status) - (clock … start_status)) (clock … start_status ≤ clock … final_status) ≝ ?
285and compute_max_trace_any_label_cost_is_ok
286  (cost_labels: BitVectorTrie costlabel 16)
287  (trace_ends_flag: trace_ends_with_ret)
288   (start_status: Status) (final_status: Status)
289     (the_trace: trace_any_label (ASM_abstract_status cost_labels) trace_ends_flag start_status final_status)
290       on the_trace:
291         (compute_max_trace_any_label_cost cost_labels trace_ends_flag start_status final_status the_trace =
292           (clock … final_status) - (clock … start_status)) ∧ (clock … start_status ≤ clock … final_status) ≝ ?
293and compute_max_trace_label_return_cost_is_ok
294  (cost_labels: BitVectorTrie costlabel 16)
295  (start_status: Status) (final_status: Status)
296    (the_trace: trace_label_return (ASM_abstract_status cost_labels) start_status final_status)
297      on the_trace:
298        (compute_max_trace_label_return_cost cost_labels start_status final_status the_trace =
299          (clock … final_status) - (clock … start_status)) ∧ (clock … start_status ≤ clock … final_status) ≝ ?.
300  [1:
301    cases the_trace
302    #ends_flag #start_status #end_status #any_label_trace #is_costed
303    normalize @compute_max_trace_any_label_cost_is_ok
304  |2:
305    cases the_trace
306    [1:
307      #start_status #final_status #is_next #is_not_return #is_costed
308      change with (current_instruction_cost start_status) in ⊢ (?(??%?)?);
309      cases(is_next)
310      @conj
311      [1:
312        cases(execute_1 start_status)
313        #the_status #assm
314        whd in match eject; normalize nodelta
315        >assm %
316      |2:
317        cases(execute_1 start_status)
318        #the_status #assm
319        whd in match eject; normalize nodelta
320        @(minus_m_n_gt_o_O_le_n_m ? ? (current_instruction_cost start_status))
321        [1: assumption
322        |2: @(current_instruction_cost_non_zero start_status)
323        ]
324      ]
325    |2:
326      #start_status #final_status #is_next #is_return
327      change with (current_instruction_cost start_status) in ⊢ (?(??%?)?);
328      cases(is_next)
329      @conj
330      [1:
331        cases(execute_1 start_status)
332        #the_status #assm
333        whd in match eject; normalize nodelta
334        >assm %
335      |2:
336        cases(execute_1 start_status)
337        #the_status #assm
338        whd in match eject; normalize nodelta
339        @(minus_m_n_gt_o_O_le_n_m ? ? (current_instruction_cost start_status))
340        [1: assumption
341        |2: @current_instruction_cost_non_zero
342        ]
343      ]
344    |3:
345      #end_flag #status_pre_fun_call #status_start_fun_call #status_after_fun_call
346      #status_final #is_next #is_call #is_after_return #call_trace #final_trace
347      change with (
348      let current_instruction_cost ≝ current_instruction_cost status_pre_fun_call in
349      let call_trace_cost ≝ compute_max_trace_label_return_cost … call_trace in
350      let final_trace_cost ≝ compute_max_trace_any_label_cost cost_labels end_flag … final_trace in
351        call_trace_cost + current_instruction_cost + final_trace_cost) in ⊢ (?(??%?)?);
352      normalize nodelta;
353      @conj
354      [1:
355        cases(compute_max_trace_label_return_cost_is_ok cost_labels status_start_fun_call
356          status_after_fun_call call_trace)
357        #trace_label_return_eq #trace_label_return_le
358        cases(compute_max_trace_any_label_cost_is_ok cost_labels end_flag status_after_fun_call
359          status_final final_trace)
360        #trace_any_label_eq #trace_any_label_le
361        >trace_label_return_eq >trace_any_label_eq
362        >commutative_plus
363       
364        cut ((clock (BitVectorTrie Byte 16) status_after_fun_call
365  -clock (BitVectorTrie Byte 16) status_start_fun_call
366  +current_instruction_cost status_pre_fun_call
367  +(clock (BitVectorTrie Byte 16) status_final
368    -clock (BitVectorTrie Byte 16) status_after_fun_call) =
369          ((clock (BitVectorTrie Byte 16) status_after_fun_call
370            - clock (BitVectorTrie Byte 16) status_start_fun_call)
371          + (clock (BitVectorTrie Byte 16) status_final
372          - clock (BitVectorTrie Byte 16) status_after_fun_call))
373          + current_instruction_cost status_pre_fun_call))
374        [ 1:
375          cases daemon (* XXX: complete me *)
376        | 2:
377          #eq_hyp >eq_hyp
378          >plus_minus_rearrangement_1
379          [1:
380          |2, 3:
381            assumption
382          ]
383        ]
384      |2:
385        cases daemon (* XXX: complete me *)
386      ]
387    |4:
388      #end_flag #status_pre #status_init #status_end #is_next
389      #trace_any_label #is_other #is_not_costed
390      cases daemon (* XXX: complete me *)
391    ]
392  |3:
393    cases the_trace
394    [1:
395      #status_before #status_after #trace_to_lift
396      normalize @compute_max_trace_label_label_cost_is_ok
397    |2:
398      #status_initial #status_labelled #status_final #labelled_trace #ret_trace
399      normalize
400      cases(compute_max_trace_label_label_cost_is_ok cost_labels doesnt_end_with_ret status_initial status_labelled labelled_trace);
401      #label_label_trace_equality #label_label_trace_leq
402      cases(compute_max_trace_label_return_cost_is_ok cost_labels status_labelled status_final ret_trace);
403      #label_return_trace_equality #label_return_trace_leq
404      >label_label_trace_equality >label_return_trace_equality
405      @conj
406      [2: @(transitive_le (clock (BitVectorTrie (Vector bool 8) 16) status_initial)
407            (clock (BitVectorTrie Byte 16) status_labelled)
408            (clock (BitVectorTrie (Vector bool 8) 16) status_final))
409      |1: @plus_minus_rearrangement_1
410      ]
411      assumption
412    ]
413  ]
414qed.
415
416(* XXX: work below here: *)
417
418let rec compute_paid_trace_any_label
419  (cost_labels: BitVectorTrie costlabel 16)
420  (trace_ends_flag: trace_ends_with_ret)
421   (start_status: Status) (final_status: Status)
422     (the_trace: trace_any_label (ASM_abstract_status cost_labels) trace_ends_flag start_status final_status)
423       on the_trace: nat ≝
424  match the_trace with
425  [ tal_base_not_return the_status _ _ _ _ ⇒ current_instruction_cost the_status
426  | tal_base_return the_status _ _ _ ⇒ current_instruction_cost the_status
427  | tal_step_call end_flag pre_fun_call start_fun_call after_fun_call final
428     _ _ _ call_trace final_trace ⇒
429      let current_instruction_cost ≝ current_instruction_cost pre_fun_call in
430      let final_trace_cost ≝ compute_paid_trace_any_label cost_labels end_flag … final_trace in
431        current_instruction_cost + final_trace_cost
432  | tal_step_default end_flag status_pre status_init status_end _ tail_trace _ _ ⇒
433      let current_instruction_cost ≝ current_instruction_cost status_pre in
434      let tail_trace_cost ≝
435       compute_paid_trace_any_label cost_labels end_flag
436        status_init status_end tail_trace
437      in
438        current_instruction_cost + tail_trace_cost
439  ].
440
441definition compute_paid_trace_label_label ≝
442 λcost_labels: BitVectorTrie costlabel 16.
443  λtrace_ends_flag: trace_ends_with_ret.
444   λstart_status: Status.
445    λfinal_status: Status.
446     λthe_trace: trace_label_label (ASM_abstract_status cost_labels) trace_ends_flag
447      start_status final_status.
448  match the_trace with
449  [ tll_base ends_flag initial final given_trace labelled_proof ⇒
450      compute_paid_trace_any_label … given_trace
451  ].
452
453let rec compute_trace_label_label_cost_using_paid
454  (cost_labels: BitVectorTrie costlabel 16)
455   (trace_ends_flag: trace_ends_with_ret)
456    (start_status: Status) (final_status: Status)
457      (the_trace: trace_label_label (ASM_abstract_status cost_labels) trace_ends_flag
458        start_status final_status) on the_trace: nat ≝
459  match the_trace with
460  [ tll_base ends_flag initial final given_trace labelled_proof ⇒
461      compute_paid_trace_label_label cost_labels … the_trace +
462      compute_trace_any_label_cost_using_paid … given_trace
463  ]
464and compute_trace_any_label_cost_using_paid
465  (cost_labels: BitVectorTrie costlabel 16)
466  (trace_ends_flag: trace_ends_with_ret)
467   (start_status: Status) (final_status: Status)
468     (the_trace: trace_any_label (ASM_abstract_status cost_labels) trace_ends_flag start_status final_status)
469       on the_trace: nat ≝
470  match the_trace with
471  [ tal_base_not_return the_status _ _ _ _ ⇒ 0
472  | tal_base_return the_status _ _ _ ⇒ 0
473  | tal_step_call end_flag pre_fun_call start_fun_call after_fun_call final
474     _ _ _ call_trace final_trace ⇒
475      let call_trace_cost ≝ compute_trace_label_return_cost_using_paid … call_trace in
476      let final_trace_cost ≝ compute_trace_any_label_cost_using_paid cost_labels end_flag … final_trace in
477        call_trace_cost + final_trace_cost
478  | tal_step_default end_flag status_pre status_init status_end _ tail_trace _ _ ⇒
479     compute_trace_any_label_cost_using_paid cost_labels end_flag
480      status_init status_end tail_trace
481  ]
482and compute_trace_label_return_cost_using_paid
483  (cost_labels: BitVectorTrie costlabel 16)
484  (start_status: Status) (final_status: Status)
485    (the_trace: trace_label_return (ASM_abstract_status cost_labels) start_status final_status)
486      on the_trace: nat ≝
487  match the_trace with
488  [ tlr_base before after trace_to_lift ⇒ compute_trace_label_label_cost_using_paid … trace_to_lift
489  | tlr_step initial labelled final labelled_trace ret_trace ⇒
490      let labelled_cost ≝ compute_trace_label_label_cost_using_paid … labelled_trace in
491      let return_cost ≝ compute_trace_label_return_cost_using_paid … ret_trace in
492        labelled_cost + return_cost
493  ].
494
495let rec compute_trace_label_label_cost_using_paid_ok
496  (cost_labels: BitVectorTrie costlabel 16)
497   (trace_ends_flag: trace_ends_with_ret)
498    (start_status: Status) (final_status: Status)
499      (the_trace: trace_label_label (ASM_abstract_status cost_labels) trace_ends_flag
500        start_status final_status) on the_trace:
501 compute_trace_label_label_cost_using_paid cost_labels … the_trace =
502 compute_max_trace_label_label_cost … the_trace ≝ ?
503and compute_trace_any_label_cost_using_paid_ok
504  (cost_labels: BitVectorTrie costlabel 16)
505  (trace_ends_flag: trace_ends_with_ret)
506   (start_status: Status) (final_status: Status)
507     (the_trace: trace_any_label (ASM_abstract_status cost_labels)
508      trace_ends_flag start_status final_status) on the_trace:     
509  compute_paid_trace_any_label cost_labels trace_ends_flag … the_trace
510  +compute_trace_any_label_cost_using_paid cost_labels trace_ends_flag … the_trace
511  =compute_max_trace_any_label_cost cost_labels trace_ends_flag … the_trace ≝ ?
512and compute_trace_label_return_cost_using_paid_ok
513  (cost_labels: BitVectorTrie costlabel 16)
514  (start_status: Status) (final_status: Status)
515    (the_trace: trace_label_return (ASM_abstract_status cost_labels)
516     start_status final_status) on the_trace:
517 compute_trace_label_return_cost_using_paid cost_labels … the_trace =
518 compute_max_trace_label_return_cost cost_labels … the_trace ≝ ?.
519[ cases the_trace #endsf #ss #es #tr #H normalize
520  @compute_trace_any_label_cost_using_paid_ok
521| cases the_trace
522  [ #ss #fs #H1 #H2 #H3 whd in ⊢ (??(?%%)%); <plus_n_O %
523  | #ss #fs #H1 #H2 whd in ⊢ (??(?%%)%); <plus_n_O %
524  | #ef #spfc #ssfc #safc #sf #H1 #H2 #H3 #tr1 #tr2 whd in ⊢ (??(?%%)%);
525    <compute_trace_any_label_cost_using_paid_ok
526    <compute_trace_label_return_cost_using_paid_ok
527    -compute_trace_label_label_cost_using_paid_ok
528    -compute_trace_label_return_cost_using_paid_ok
529    -compute_trace_any_label_cost_using_paid_ok
530    >commutative_plus in ⊢ (???(?%?));
531    >commutative_plus in ⊢ (??(??%)?);
532    >associative_plus >associative_plus in ⊢ (???%); @eq_f2 try %
533    <associative_plus <commutative_plus %
534  | #ef #sp #si #se #H1 #tr #H2 #H3 whd in ⊢ (??(?%%)%); >associative_plus @eq_f2
535    [ % | @compute_trace_any_label_cost_using_paid_ok ]
536  ]
537| cases the_trace
538  [ #sb #sa #tr normalize @compute_trace_label_label_cost_using_paid_ok
539  | #si #sl #sf #tr1 #tr2 normalize @eq_f2
540    [ @compute_trace_label_label_cost_using_paid_ok
541    | @compute_trace_label_return_cost_using_paid_ok ]]]
542qed.
543
544(*
545let rec compute_paid_trace_label_return
546  (cost_labels: BitVectorTrie costlabel 16)
547  (start_status: Status) (final_status: Status)
548    (the_trace: trace_label_return (ASM_abstract_status cost_labels) start_status final_status)
549      on the_trace: nat ≝
550 match the_trace with
551  [ tlr_base before after trace_to_lift ⇒ compute_paid_trace_label_label … trace_to_lift
552  | tlr_step initial labelled final labelled_trace ret_trace ⇒
553      let labelled_cost ≝ compute_paid_trace_label_label … labelled_trace in
554      let return_cost ≝ compute_paid_trace_label_return … ret_trace in
555        labelled_cost + return_cost
556  ].
557*)
558
559let rec compute_cost_trace_label_label
560  (cost_labels: BitVectorTrie costlabel 16)
561   (trace_ends_flag: trace_ends_with_ret)
562    (start_status: Status) (final_status: Status)
563      (the_trace: trace_label_label (ASM_abstract_status cost_labels) trace_ends_flag
564        start_status final_status) on the_trace:
565         list (Σk:costlabel. ∃pc. lookup_opt … pc cost_labels = Some … k) ≝
566  match the_trace with
567  [ tll_base ends_flag initial final given_trace labelled_proof ⇒
568     let pc ≝ program_counter … initial in
569     let label ≝
570      match lookup_opt … pc cost_labels return λx. match x with [None ⇒ False | Some _ ⇒ True] → costlabel with
571      [ None ⇒ λabs. ⊥
572      | Some l ⇒ λ_. l ] labelled_proof in
573     (dp … label ?)::compute_cost_trace_any_label … given_trace
574  ]
575and compute_cost_trace_any_label
576  (cost_labels: BitVectorTrie costlabel 16)
577  (trace_ends_flag: trace_ends_with_ret)
578   (start_status: Status) (final_status: Status)
579     (the_trace: trace_any_label (ASM_abstract_status cost_labels) trace_ends_flag start_status final_status)
580       on the_trace: list (Σk:costlabel. ∃pc. lookup_opt … pc cost_labels = Some … k) ≝
581  match the_trace with
582  [ tal_base_not_return the_status _ _ _ _ ⇒ []
583  | tal_base_return the_status _ _ _ ⇒ []
584  | tal_step_call end_flag pre_fun_call start_fun_call after_fun_call final
585     _ _ _ call_trace final_trace ⇒
586      let call_cost_trace ≝ compute_cost_trace_label_return … call_trace in
587      let final_cost_trace ≝ compute_cost_trace_any_label cost_labels end_flag … final_trace in
588        call_cost_trace @ final_cost_trace
589  | tal_step_default end_flag status_pre status_init status_end _ tail_trace _ _ ⇒
590       compute_cost_trace_any_label cost_labels end_flag
591        status_init status_end tail_trace
592  ]
593and compute_cost_trace_label_return
594  (cost_labels: BitVectorTrie costlabel 16)
595  (start_status: Status) (final_status: Status)
596    (the_trace: trace_label_return (ASM_abstract_status cost_labels) start_status final_status)
597      on the_trace: list (Σk:costlabel. ∃pc. lookup_opt … pc cost_labels = Some … k) ≝
598  match the_trace with
599  [ tlr_base before after trace_to_lift ⇒ compute_cost_trace_label_label … trace_to_lift
600  | tlr_step initial labelled final labelled_trace ret_trace ⇒
601      let labelled_cost ≝ compute_cost_trace_label_label … labelled_trace in
602      let return_cost ≝ compute_cost_trace_label_return … ret_trace in
603        labelled_cost @ return_cost
604  ].
605 [ %{pc} whd in match label;  generalize in match labelled_proof; whd in ⊢ (% → ?);
606  cases (lookup_opt costlabel … pc cost_labels) normalize
607  [ #abs cases abs | // ]
608 | // ]
609qed.
610
611(* ??????????????????????? *)
612axiom block_cost_static_dynamic_ok:
613 ∀cost_labels: BitVectorTrie costlabel 16.
614 ∀trace_ends_flag.
615 ∀start_status: Status.
616 ∀final_status: Status.
617 ∀the_trace:
618  trace_label_label (ASM_abstract_status cost_labels) trace_ends_flag
619   start_status final_status.
620 let mem ≝ code_memory … start_status in
621 let pc ≝ program_counter … start_status in
622 let program_size ≝ 2^16 in
623  block_cost mem cost_labels pc program_size =
624  compute_paid_trace_label_label cost_labels trace_ends_flag
625   start_status final_status the_trace.
626
627(*
628(* To be moved elsewhere*)
629lemma le_S_n_m_to_le_n_m: ∀n,m. S n ≤ m → n ≤ m.
630 #n #m #H change with (pred (S n) ≤ m) @(transitive_le ? (S n)) //
631qed.
632*)
633
634include "arithmetics/bigops.ma".
635
636(* This shoudl go in bigops! *)
637theorem bigop_sum_rev: ∀k1,k2,p,B,nil.∀op:Aop B nil.∀f:nat→B.
638 \big[op,nil]_{i<k1+k2|p i} (f i) =
639 op \big[op,nil]_{i<k2|p (i+k1)} (f (i+k1)) \big[op,nil]_{i<k1|p i} (f i).
640 #k1 #k2 #p #B #nil #op #f >bigop_sum >commutative_plus @same_bigop #i @leb_elim normalize
641 [2,4: //
642 | #H1 #H2 <plus_minus_m_m //
643 | #H1 #H2 #H3 <plus_minus_m_m //]
644qed.
645
646(* This is taken by sigma_pi.ma that does not compile now *)
647definition natAop ≝ mk_Aop nat 0 plus (λa.refl ? a) (λn.sym_eq ??? (plus_n_O n))
648   (λa,b,c.sym_eq ??? (associative_plus a b c)).
649
650definition natACop ≝ mk_ACop nat 0 natAop commutative_plus.
651
652definition natDop ≝ mk_Dop nat 0 natACop times (λn.(sym_eq ??? (times_n_O n)))
653   distributive_times_plus.
654
655unification hint  0 ≔ ;
656   S ≟ mk_Aop nat 0 plus (λa.refl ? a) (λn.sym_eq ??? (plus_n_O n))
657   (λa,b,c.sym_eq ??? (associative_plus a b c))
658(* ---------------------------------------- *) ⊢
659   plus ≡ op ? ? S.
660
661unification hint  0 ≔ ;
662   S ≟ mk_ACop nat 0 (mk_Aop nat 0 plus (λa.refl ? a) (λn.sym_eq ??? (plus_n_O n))
663   (λa,b,c.sym_eq ??? (associative_plus a b c))) commutative_plus
664(* ---------------------------------------- *) ⊢
665   plus ≡ op ? ? S.
666   
667unification hint  0 ≔ ;
668   S ≟ natDop
669(* ---------------------------------------- *) ⊢
670   plus ≡ sum ? ? S.
671
672unification hint  0 ≔ ;
673   S ≟ natDop
674(* ---------------------------------------- *) ⊢
675   times ≡ prod ? ? S.
676
677notation "Σ_{ ident i < n } f"
678  with precedence 20
679for @{'bigop $n plus 0 (λ${ident i}.true) (λ${ident i}. $f)}.
680
681axiom code_memory_ro_label_label:
682 ∀cost_labels. ∀ends_flag.
683 ∀initial,final. trace_label_label (ASM_abstract_status cost_labels) ends_flag initial final →
684  code_memory … initial = code_memory … final.
685
686(*
687axiom code_memory_ro_label_return:
688 ∀cost_labels.
689 ∀initial,final. trace_label_return (ASM_abstract_status cost_labels) initial final →
690  code_memory … initial = code_memory … final.
691*)
692
693definition tech_cost_of_label0:
694 ∀cost_labels: BitVectorTrie costlabel 16.
695 ∀cost_map: identifier_map CostTag nat.
696 ∀codom_dom: (∀pc,k. lookup_opt … pc cost_labels = Some … k → present … cost_map k).
697 ∀ctrace:list (Σk:costlabel.∃pc. lookup_opt costlabel 16 pc cost_labels = Some ? k).
698 ∀i,p. present … cost_map (nth_safe ? i ctrace p).
699 #cost_labels #cost_map #codom_dom #ctrace #i #p
700 cases (nth_safe … i ctrace ?) normalize #id * #id_pc #K
701 lapply (codom_dom … K) #k_pres >(lookup_lookup_present … k_pres)
702 % #abs destruct (abs)
703qed.
704
705lemma ltb_rect:
706 ∀P:Type[0].∀n,m. (n < m → P) → (¬ n < m → P) → P.
707 #P #n #m lapply (refl … (ltb n m)) cases (ltb n m) in ⊢ (???% → %); #E #H1 #H2
708 [ @H1 @leb_true_to_le @E | @H2 @leb_false_to_not_le @E ]
709qed.
710
711lemma same_ltb_rect:
712 ∀P,n,m,H1,H2,n',m',H1',H2'.
713  ltb n m = ltb n' m' → (∀x,y. H1 x = H1' y) → (∀x,y. H2 x = H2' y) →
714   ltb_rect P n m H1 H2 =
715   ltb_rect P n' m' H1' H2'.
716 #P #n #m #H1 #H2 #n' #m' #H1' #H2' #E #K1 #K2 whd in ⊢ (??%?);
717 cut (∀xxx,yyy,xxx',yyy'.
718   match ltb n m
719   return λx:bool.
720           eq bool (ltb n m) x
721            → (lt n m → P) → (Not (lt n m) → P) → P
722    with
723    [ true ⇒
724        λE0:eq bool (ltb n m) true.
725         λH10:lt n m → P.
726          λH20:Not (lt n m) → P. H10 (xxx E0)
727    | false ⇒
728        λE0:eq bool (ltb n m) false.
729         λH10:lt n m → P.
730          λH20:Not (lt n m) → P. H20 (yyy E0)]
731    (refl … (ltb n m)) H1 H2 =
732   match ltb n' m'
733   return λx:bool.
734           eq bool (ltb n' m') x
735            → (lt n' m' → P) → (Not (lt n' m') → P) → P
736    with
737    [ true ⇒
738        λE0:eq bool (ltb n' m') true.
739         λH10:lt n' m' → P.
740          λH20:Not (lt n' m') → P. H10 (xxx' E0)
741    | false ⇒
742        λE0:eq bool (ltb n' m') false.
743         λH10:lt n' m' → P.
744          λH20:Not (lt n' m') → P. H20 (yyy' E0)]
745    (refl … (ltb n' m')) H1' H2'
746  ) [2: #X @X]
747 >E cases (ltb n' m') #xxx #yyy #xxx' #yyy' normalize
748 [ @K1 | @K2 ]
749qed.
750
751
752definition tech_cost_of_label:
753 ∀cost_labels: BitVectorTrie costlabel 16.
754 ∀cost_map: identifier_map CostTag nat.
755 ∀codom_dom: (∀pc,k. lookup_opt … pc cost_labels = Some … k → present … cost_map k).
756 list (Σk:costlabel.∃pc. lookup_opt costlabel 16 pc cost_labels = Some ? k) →
757 nat → nat
758≝ λcost_labels,cost_map,codom_dom,ctrace,i.
759 ltb_rect ? i (|ctrace|)
760 (λH. lookup_present ?? cost_map (nth_safe ? i ctrace H) ?)
761 (λ_.0).
762 @tech_cost_of_label0 @codom_dom
763qed.
764
765lemma shift_nth_safe:
766 ∀T,i,l2,l1,K1,K2.
767  nth_safe T i l1 K1 = nth_safe T (i+|l2|) (l2@l1) K2.
768 #T #i #l2 elim l2 normalize
769  [ #l1 #K1 <plus_n_O #K2 %
770  | #hd #tl #IH #l1 #K1 <plus_n_Sm #K2 change with (? < ?) in K1; change with (? < ?) in K2;
771    whd in ⊢ (???%); @IH ]
772qed.
773
774lemma tech_cost_of_label_shift:
775 ∀cost_labels,cost_map,codom_dom,l1,l2,i.
776  i < |l1| →
777   tech_cost_of_label cost_labels cost_map codom_dom l2 i =
778   tech_cost_of_label cost_labels cost_map codom_dom (l1@l2) (i+|l1|).
779 #cost_labels #cost_Map #codom_dom #l1 #l2 #i #H
780 cut (ltb i (|l2|) = ltb (i+|l1|) (|l1@l2|))
781 [ change with (leb ?? = leb ??); @leb_elim
782   [ #H1 change with (i < ?) in H1; >le_to_leb_true [//] change with (? < ?)
783     >append_length >commutative_plus @monotonic_lt_plus_r //
784   | #H1 change with (¬ i < ?) in H1; >not_le_to_leb_false [//]
785     >append_length >commutative_plus % #H2
786     change with (? < ?) in H2; @(absurd ?? H1) @(lt_plus_to_lt_r … H2) ]
787 | #E whd in match tech_cost_of_label; normalize nodelta
788 @same_ltb_rect [@E]
789 [ #K1 #K2 lapply (shift_nth_safe ? i l1 l2 K1 K2) #H check eq_f
790   lapply (eq_f ?? (lookup_present CostTag nat cost_Map) ?? H)
791
792   
793 
794  cut (decide_bool (ltb i (|l2|)) = decide_bool (ltb (i+(|l1|)) (|l1@l2|)))
795  whd in match tech_cost_of_label; normalize nodelta
796   
797
798let rec compute_max_trace_label_return_cost_ok_with_trace
799 (cost_labels: BitVectorTrie costlabel 16)
800 (cost_map: identifier_map CostTag nat)
801 (initial: Status) (final: Status)
802 (trace: trace_label_return (ASM_abstract_status cost_labels) initial final)
803 (codom_dom: (∀pc,k. lookup_opt … pc cost_labels = Some … k → present … cost_map k))
804 (dom_codom:(∀k. ∀k_pres:present … cost_map k. ∃pc. lookup_opt … pc cost_labels = Some … k →
805   block_cost (code_memory … initial) cost_labels pc 2^16 = lookup_present … k_pres))
806 on trace:
807  let ctrace ≝ compute_cost_trace_label_return … trace in
808  compute_max_trace_label_return_cost … trace =
809   Σ_{i < |ctrace|} (tech_cost_of_label cost_labels cost_map codom_dom ctrace i)
810    ≝ ?.
811lapply (refl … initial) cases trace in ⊢ (??%? → %);
812[ #sb #sa #tr #_ whd in ⊢ (let ctrace ≝ % in ??%?);
813| #si #sl #sf #tr1 #tr2 #E whd in ⊢ (let ctrace ≝ % in ??%?);
814  change with (let ctrace ≝ ? in ? = bigop (|? @ ?|) ?????)
815  >append_length in ⊢ (let ctrace ≝ ? in ???(?%?????));
816  change with (?=?) >bigop_sum_rev >commutative_plus @eq_f2
817  [ >compute_max_trace_label_return_cost_ok_with_trace
818    -compute_max_trace_label_return_cost_ok_with_trace     
819    [3: @cost_map
820    |2: @codom_dom
821    |4:lapply (code_memory_ro_label_label … tr1) >E #E2 <E2 @dom_codom]
822    @same_bigop [//] #i #H #_ -dom_codom
823    generalize in match (compute_cost_trace_label_return cost_labels sl sf tr2); #l1
824    generalize in match (compute_cost_trace_label_label cost_labels doesnt_end_with_ret si sl tr1);
825
826i < |l1| →
827tech_cost_of_label cost_labels cost_map codom_dom l1 i =
828tech_cost_of_label cost_labels cost_map codom_dom (l2@l1) (i+|l1|)
829
830
831     whd in ⊢ (??%%);
832  |
833  ]
834]
835
836lemma
837 compute_max_trace_any_label_cost cost_label trace_ends_flag start_status
838  final_status the_trace =
839 
840
841let rec compute_paid_trace_label_label_cost
842  (cost_labels: BitVectorTrie Byte 16)
843   (trace_ends_flag: trace_ends_with_ret)
844    (start_status: Status) (final_status: Status)
845      (the_trace: trace_label_label (ASM_abstract_status cost_labels) trace_ends_flag
846        start_status final_status) on the_trace: nat ≝
847  match the_trace with
848  [ tll_base _ _ _ given_trace _ ⇒
849      compute_paid_trace_any_label_cost … given_trace
850  ]
851and compute_paid_trace_any_label_cost
852  (cost_labels: BitVectorTrie Byte 16)
853  (trace_ends_flag: trace_ends_with_ret)
854   (start_status: Status) (final_status: Status)
855     (the_trace: trace_any_label (ASM_abstract_status cost_labels) trace_ends_flag start_status final_status)
856       on the_trace: nat ≝
857  match the_trace with
858  [ tal_base_not_return the_status _ _ _ _ ⇒ current_instruction_cost the_status
859  | tal_base_return the_status _ _ _ ⇒ current_instruction_cost the_status
860  | tal_step_call end_flag pre_fun_call start_fun_call after_fun_call final
861     _ _ _ call_trace final_trace ⇒
862      let current_instruction_cost ≝ current_instruction_cost pre_fun_call in
863      let final_trace_cost ≝ compute_paid_trace_any_label_cost cost_labels end_flag … final_trace in
864        current_instruction_cost + final_trace_cost
865  | tal_step_default end_flag status_pre status_init status_end _ tail_trace _ _ ⇒
866      let current_instruction_cost ≝ current_instruction_cost status_pre in
867      let tail_trace_cost ≝
868       compute_paid_trace_any_label_cost cost_labels end_flag
869        status_init status_end tail_trace
870      in
871        current_instruction_cost + tail_trace_cost
872  ]
873and compute_paid_trace_label_return_cost
874  (cost_labels: BitVectorTrie Byte 16)
875  (start_status: Status) (final_status: Status)
876    (the_trace: trace_label_return (ASM_abstract_status cost_labels) start_status final_status)
877      on the_trace: nat ≝
878  match the_trace with
879  [ tlr_base before after trace_to_lift ⇒ compute_paid_trace_label_label_cost … trace_to_lift
880  | tlr_step initial labelled final labelled_trace ret_trace ⇒
881      let labelled_cost ≝ compute_paid_trace_label_label_cost … labelled_trace in
882      let return_cost ≝ compute_paid_trace_label_return_cost … ret_trace in
883        labelled_cost + return_cost
884  ].
885
886let rec trace_lab_rec_cost' (p: trace_lab_ret) : nat.
887 | (call b bf tr af tl) as self ⇒
888    trace_lab_lab_cost_nocall self + trace_lab_ret_cost' tr +
889    trace_lab_rec_cost' tl
890
891theorem main_lemma:
892 ∀p. trace_lab_rec_cost p = trace_lab_rec_cost' p.
893
894axiom lemma1:
895 ∀p: simple_path.
896  traverse_cost_internal (pc (hd p)) … = trace_lab_lab_cost_nocall p.
897
898axiom lemma2:
899 ∀s,l,cost_map.
900  is_labelled l s →
901   traverse_cost_internal s = cost_map l.
902
903axiom main_statement:
904 ∀s.
905 ∀cost_map.
906 let p ≝ compute_simple_path0 s in
907 ∑ (cost_trace p) cost_map = trace_lab_rec_cost' p.
908
909axiom main_statement:
910 ∀s.
911 ∀cost_map.
912 let p ≝ compute_simple_path0 s in
913  execute' (path_length p) s = 〈s',τ〉 →
914   Timer s' - Timer s = ∑ τ cost_map.
Note: See TracBrowser for help on using the repository browser.