source: src/ASM/CostsProof.ma @ 1573

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

more complicated than it appears :(

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