source: src/ASM/CostsProof.ma @ 1581

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

Dangling de Bruijn pointer when trying to propagate russell to set_arg_1

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