source: src/ASM/CostsProof.ma @ 1695

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

Progress on CostsProof?.ma file.

File size: 30.9 KB
Line 
1include "ASM/ASMCosts.ma".
2include "ASM/WellLabeled.ma".
3include "ASM/Status.ma".
4include "common/StructuredTraces.ma".
5include "arithmetics/bigops.ma".
6include alias "arithmetics/nat.ma".
7include alias "basics/logic.ma".
8
9let rec compute_max_trace_label_label_cost
10  (cm: ?)
11  (cost_labels: BitVectorTrie costlabel 16)
12   (trace_ends_flag: trace_ends_with_ret)
13    (start_status: Status cm) (final_status: Status cm)
14      (the_trace: trace_label_label (ASM_abstract_status cm cost_labels) trace_ends_flag
15        start_status final_status) on the_trace: nat ≝
16  match the_trace with
17  [ tll_base ends_flag initial final given_trace labelled_proof ⇒
18      compute_max_trace_any_label_cost … given_trace
19  ]
20and compute_max_trace_any_label_cost
21  (cm: ?)
22  (cost_labels: BitVectorTrie costlabel 16)
23  (trace_ends_flag: trace_ends_with_ret)
24   (start_status: Status cm) (final_status: Status cm)
25     (the_trace: trace_any_label (ASM_abstract_status cm cost_labels) trace_ends_flag start_status final_status)
26       on the_trace: nat ≝
27  match the_trace with
28  [ tal_base_not_return the_status _ _ _ _ ⇒ current_instruction_cost cm the_status
29  | tal_base_return the_status _ _ _ ⇒ current_instruction_cost cm the_status
30  | tal_base_call pre_fun_call start_fun_call final _ _ _ call_trace _ ⇒
31      let current_instruction_cost ≝ current_instruction_cost cm pre_fun_call in
32      let call_trace_cost ≝ compute_max_trace_label_return_cost … call_trace in
33        call_trace_cost + current_instruction_cost
34  | tal_step_call end_flag pre_fun_call start_fun_call after_fun_call final
35     _ _ _ call_trace _ final_trace ⇒
36      let current_instruction_cost ≝ current_instruction_cost cm pre_fun_call in
37      let call_trace_cost ≝ compute_max_trace_label_return_cost … call_trace in
38      let final_trace_cost ≝ compute_max_trace_any_label_cost cm cost_labels end_flag … final_trace in
39        call_trace_cost + current_instruction_cost + final_trace_cost
40  | tal_step_default end_flag status_pre status_init status_end _ tail_trace _ _ ⇒
41      let current_instruction_cost ≝ current_instruction_cost cm status_pre in
42      let tail_trace_cost ≝
43       compute_max_trace_any_label_cost cm cost_labels end_flag
44        status_init status_end tail_trace
45      in
46        current_instruction_cost + tail_trace_cost
47  ]
48and compute_max_trace_label_return_cost
49  (cm: ?)
50  (cost_labels: BitVectorTrie costlabel 16)
51  (start_status: Status cm) (final_status: Status cm)
52    (the_trace: trace_label_return (ASM_abstract_status cm cost_labels) start_status final_status)
53      on the_trace: nat ≝
54  match the_trace with
55  [ tlr_base before after trace_to_lift ⇒ compute_max_trace_label_label_cost … trace_to_lift
56  | tlr_step initial labelled final labelled_trace ret_trace ⇒
57      let labelled_cost ≝ compute_max_trace_label_label_cost … labelled_trace in
58      let return_cost ≝ compute_max_trace_label_return_cost … ret_trace in
59        labelled_cost + return_cost
60  ].
61
62include alias "arithmetics/nat.ma".
63
64let rec compute_max_trace_label_label_cost_is_ok
65  (cm: ?)
66  (cost_labels: BitVectorTrie costlabel 16)
67   (trace_ends_flag: trace_ends_with_ret)
68    (start_status: Status cm) (final_status: Status cm)
69      (the_trace: trace_label_label (ASM_abstract_status cm cost_labels) trace_ends_flag
70        start_status final_status) on the_trace:
71          clock … cm … final_status = (compute_max_trace_label_label_cost cm cost_labels trace_ends_flag start_status final_status the_trace) + (clock … cm … start_status) ≝ ?
72and compute_max_trace_any_label_cost_is_ok
73  (cm: ?)
74  (cost_labels: BitVectorTrie costlabel 16)
75  (trace_ends_flag: trace_ends_with_ret)
76   (start_status: Status cm) (final_status: Status cm)
77     (the_trace: trace_any_label (ASM_abstract_status cm cost_labels) trace_ends_flag start_status final_status)
78       on the_trace:
79         clock … cm … final_status = (compute_max_trace_any_label_cost cm cost_labels trace_ends_flag start_status final_status the_trace) + (clock … cm … start_status) ≝ ?
80and compute_max_trace_label_return_cost_is_ok
81  (cm: ?)
82  (cost_labels: BitVectorTrie costlabel 16)
83  (start_status: Status cm) (final_status: Status cm)
84    (the_trace: trace_label_return (ASM_abstract_status cm cost_labels) start_status final_status)
85      on the_trace:
86        clock … cm … final_status = (compute_max_trace_label_return_cost cm cost_labels start_status final_status the_trace) + clock … cm … start_status ≝ ?.
87  [1:
88    cases the_trace
89    #ends_flag #start_status #end_status #any_label_trace #is_costed
90    normalize @compute_max_trace_any_label_cost_is_ok
91  |2:
92    cases the_trace
93    [1,2:
94      #start_status #final_status #is_next #is_not_return try (#is_costed)
95      change with (current_instruction_cost cm start_status) in ⊢ (???(?%?));
96      cases(is_next) @execute_1_ok
97    |3:
98      #status_pre_fun_call #status_start_fun_call #status_final #is_next
99      #classifier_assm #after_return_assm #call_trace #costed_assm
100      whd in match (compute_max_trace_any_label_cost … (tal_base_call …));
101      >(compute_max_trace_label_return_cost_is_ok … call_trace)
102      >associative_plus @eq_f cases(is_next)
103      >execute_1_ok %
104    |4:
105      #end_flag #status_pre_fun_call #status_start_fun_call #status_after_fun_call
106      #status_final #is_next #is_call #is_after_return #call_trace #not_costed #final_trace
107      change with (
108      let current_instruction_cost ≝ current_instruction_cost cm status_pre_fun_call in
109      let call_trace_cost ≝ compute_max_trace_label_return_cost cm … call_trace in
110      let final_trace_cost ≝ compute_max_trace_any_label_cost cm cost_labels end_flag … final_trace in
111        call_trace_cost + current_instruction_cost + final_trace_cost) in ⊢ (???(?%?));
112      normalize nodelta;
113      >(compute_max_trace_any_label_cost_is_ok … cost_labels end_flag status_after_fun_call
114          status_final final_trace)
115      >(compute_max_trace_label_return_cost_is_ok … cost_labels status_start_fun_call
116        status_after_fun_call call_trace)
117      cases(is_next) in match (clock … cm status_start_fun_call);
118      >(execute_1_ok … status_pre_fun_call)
119      <associative_plus in ⊢ (??%?);
120      <commutative_plus in match (
121        compute_max_trace_any_label_cost cm cost_labels end_flag status_after_fun_call status_final final_trace
122          + compute_max_trace_label_return_cost cm cost_labels status_start_fun_call status_after_fun_call call_trace);
123      >associative_plus in ⊢ (??%?); >associative_plus in ⊢ (???%); >associative_plus in ⊢ (???%);
124      @eq_f >commutative_plus in ⊢ (??%?); >associative_plus in ⊢ (??%?);
125      @eq_f @commutative_plus
126    |5:
127      #end_flag #status_pre #status_init #status_end #is_next
128      #trace_any_label #is_other #is_not_costed
129      change with (
130      let current_instruction_cost ≝ current_instruction_cost cm status_pre in
131      let tail_trace_cost ≝
132       compute_max_trace_any_label_cost cm cost_labels end_flag
133        status_init status_end trace_any_label
134      in
135        current_instruction_cost + tail_trace_cost) in ⊢ (???(?%?));
136      normalize nodelta;
137      >(compute_max_trace_any_label_cost_is_ok cm cost_labels end_flag
138         status_init status_end trace_any_label)
139      cases(is_next) in match (clock … cm status_init);
140      >(execute_1_ok cm status_pre)
141      >commutative_plus >associative_plus >associative_plus @eq_f
142      @commutative_plus
143    ]
144  |3:
145    cases the_trace
146    [1:
147      #status_before #status_after #trace_to_lift
148      normalize @compute_max_trace_label_label_cost_is_ok
149    |2:
150      #status_initial #status_labelled #status_final #labelled_trace #ret_trace
151      normalize
152      >(compute_max_trace_label_return_cost_is_ok cm cost_labels status_labelled status_final ret_trace);
153      >(compute_max_trace_label_label_cost_is_ok cm cost_labels doesnt_end_with_ret status_initial status_labelled labelled_trace);
154      <associative_plus in ⊢ (??%?);
155      >commutative_plus in match (
156        compute_max_trace_label_return_cost cm cost_labels status_labelled status_final ret_trace
157          + compute_max_trace_label_label_cost cm cost_labels doesnt_end_with_ret status_initial status_labelled labelled_trace);
158      %
159    ]
160  ]
161qed.
162
163let rec compute_trace_label_label_cost_using_paid
164  (cm: ?)
165  (cost_labels: BitVectorTrie costlabel 16)
166   (trace_ends_flag: trace_ends_with_ret)
167    (start_status: Status cm) (final_status: Status cm)
168      (the_trace: trace_label_label (ASM_abstract_status cm cost_labels) trace_ends_flag
169        start_status final_status) on the_trace: nat ≝
170  match the_trace with
171  [ tll_base ends_flag initial final given_trace labelled_proof ⇒
172      compute_paid_trace_label_label cm cost_labels … the_trace +
173      compute_trace_any_label_cost_using_paid … given_trace
174  ]
175and compute_trace_any_label_cost_using_paid
176  (cm: ?)
177  (cost_labels: BitVectorTrie costlabel 16)
178  (trace_ends_flag: trace_ends_with_ret)
179   (start_status: Status cm) (final_status: Status cm)
180     (the_trace: trace_any_label (ASM_abstract_status cm cost_labels) trace_ends_flag start_status final_status)
181       on the_trace: nat ≝
182  match the_trace with
183  [ tal_base_not_return the_status _ _ _ _ ⇒ 0
184  | tal_base_return the_status _ _ _ ⇒ 0
185  | tal_base_call pre_fun_call start_fun_call final _ _ _ call_trace _ ⇒
186      compute_trace_label_return_cost_using_paid … call_trace
187  | tal_step_call end_flag pre_fun_call start_fun_call after_fun_call final
188     _ _ _ call_trace _ final_trace ⇒
189      let call_trace_cost ≝ compute_trace_label_return_cost_using_paid … call_trace in
190      let final_trace_cost ≝ compute_trace_any_label_cost_using_paid cm cost_labels end_flag … final_trace in
191        call_trace_cost + final_trace_cost
192  | tal_step_default end_flag status_pre status_init status_end _ tail_trace _ _ ⇒
193     compute_trace_any_label_cost_using_paid cm cost_labels end_flag
194      status_init status_end tail_trace
195  ]
196and compute_trace_label_return_cost_using_paid
197  (cm: ?)
198  (cost_labels: BitVectorTrie costlabel 16)
199  (start_status: Status cm) (final_status: Status cm)
200    (the_trace: trace_label_return (ASM_abstract_status cm cost_labels) start_status final_status)
201      on the_trace: nat ≝
202  match the_trace with
203  [ tlr_base before after trace_to_lift ⇒ compute_trace_label_label_cost_using_paid … trace_to_lift
204  | tlr_step initial labelled final labelled_trace ret_trace ⇒
205      let labelled_cost ≝ compute_trace_label_label_cost_using_paid … labelled_trace in
206      let return_cost ≝ compute_trace_label_return_cost_using_paid … ret_trace in
207        labelled_cost + return_cost
208  ].
209
210let rec compute_trace_label_label_cost_using_paid_ok
211  (cm: ?)
212  (cost_labels: BitVectorTrie costlabel 16)
213   (trace_ends_flag: trace_ends_with_ret)
214    (start_status: Status cm) (final_status: Status cm)
215      (the_trace: trace_label_label (ASM_abstract_status cm cost_labels) trace_ends_flag
216        start_status final_status) on the_trace:
217 compute_trace_label_label_cost_using_paid cm cost_labels … the_trace =
218 compute_max_trace_label_label_cost … the_trace ≝ ?
219and compute_trace_any_label_cost_using_paid_ok
220  (cm: ?)
221  (cost_labels: BitVectorTrie costlabel 16)
222  (trace_ends_flag: trace_ends_with_ret)
223   (start_status: Status cm) (final_status: Status cm)
224     (the_trace: trace_any_label (ASM_abstract_status cm cost_labels)
225      trace_ends_flag start_status final_status) on the_trace:     
226  compute_paid_trace_any_label cm cost_labels trace_ends_flag … the_trace
227  +compute_trace_any_label_cost_using_paid cm cost_labels trace_ends_flag … the_trace
228  =compute_max_trace_any_label_cost cm cost_labels trace_ends_flag … the_trace ≝ ?
229and compute_trace_label_return_cost_using_paid_ok
230  (cm: ?)
231  (cost_labels: BitVectorTrie costlabel 16)
232  (start_status: Status cm) (final_status: Status cm)
233    (the_trace: trace_label_return (ASM_abstract_status cm cost_labels)
234     start_status final_status) on the_trace:
235 compute_trace_label_return_cost_using_paid cm cost_labels … the_trace =
236 compute_max_trace_label_return_cost cm cost_labels … the_trace ≝ ?.
237[ cases the_trace #endsf #ss #es #tr #H normalize
238  @compute_trace_any_label_cost_using_paid_ok
239| cases the_trace
240  [ #ss #fs #H1 #H2 #H3 whd in ⊢ (??(?%%)%); <plus_n_O %
241  | #ss #fs #H1 #H2 whd in ⊢ (??(?%%)%); <plus_n_O %
242  |
243    #sp #ss #sf #H1 #H2 #tr1 #tr2 #H3
244    whd in ⊢ (???%); whd in ⊢ (??(??%)?); whd in ⊢ (??(?%?)?);
245    >compute_trace_label_return_cost_using_paid_ok in ⊢ (??%?);
246    >commutative_plus in ⊢ (??%?); @eq_f %
247  | #ef #spfc #ssfc #safc #sf #H1 #H2 #H3 #tr1 #H4 #tr2 whd in ⊢ (??(?%%)%);
248    <compute_trace_any_label_cost_using_paid_ok
249    <compute_trace_label_return_cost_using_paid_ok
250    -compute_trace_label_label_cost_using_paid_ok
251    -compute_trace_label_return_cost_using_paid_ok
252    -compute_trace_any_label_cost_using_paid_ok
253    >commutative_plus in ⊢ (???(?%?));
254    >commutative_plus in ⊢ (??(??%)?);
255    >associative_plus >associative_plus in ⊢ (???%); @eq_f2 try %
256    <associative_plus <commutative_plus %
257  | #ef #sp #si #se #H1 #tr #H2 #H3 whd in ⊢ (??(?%%)%); >associative_plus @eq_f2
258    [ % | @compute_trace_any_label_cost_using_paid_ok ]
259  ]
260| cases the_trace
261  [ #sb #sa #tr normalize @compute_trace_label_label_cost_using_paid_ok
262  | #si #sl #sf #tr1 #tr2 normalize @eq_f2
263    [ @compute_trace_label_label_cost_using_paid_ok
264    | @compute_trace_label_return_cost_using_paid_ok ]]]
265qed.
266
267include alias "ASM/BitVectorTrie.ma".
268
269let rec compute_cost_trace_label_label
270  (cm: BitVectorTrie Byte 16)
271  (cost_labels: BitVectorTrie costlabel 16)
272   (trace_ends_flag: trace_ends_with_ret)
273    (start_status: Status cm) (final_status: Status cm)
274      (the_trace: trace_label_label (ASM_abstract_status cm cost_labels) trace_ends_flag
275        start_status final_status) on the_trace:
276         list (Σk:costlabel. ∃pc: Word. lookup_opt … pc cost_labels = Some … k) ≝
277  match the_trace with
278  [ tll_base ends_flag initial final given_trace labelled_proof ⇒
279     let pc ≝ program_counter ? cm initial in
280     let label ≝
281      match lookup_opt … pc cost_labels return λx: option ?. match x with [None ⇒ False | Some _ ⇒ True] → costlabel with
282      [ None ⇒ λabs. ⊥
283      | Some l ⇒ λ_. l ] labelled_proof in
284      (mk_Sig ?? label ?)::compute_cost_trace_any_label cm cost_labels ends_flag initial final … given_trace
285  ]
286and compute_cost_trace_any_label
287  (cm: BitVectorTrie Byte 16)
288  (cost_labels: BitVectorTrie costlabel 16)
289  (trace_ends_flag: trace_ends_with_ret)
290   (start_status: Status cm) (final_status: Status cm)
291     (the_trace: trace_any_label (ASM_abstract_status cm cost_labels) trace_ends_flag start_status final_status)
292       on the_trace: list (Σk:costlabel. ∃pc. lookup_opt … pc cost_labels = Some … k) ≝
293  match the_trace with
294  [ tal_base_not_return the_status _ _ _ _ ⇒ [ ]
295  | tal_base_call pre_fun_call start_fun_call final _ _ _ call_trace _ ⇒
296      compute_cost_trace_label_return … call_trace
297  | tal_base_return the_status _ _ _ ⇒ [ ]
298  | tal_step_call end_flag pre_fun_call start_fun_call after_fun_call final
299     _ _ _ call_trace _ final_trace ⇒
300      let call_cost_trace ≝ compute_cost_trace_label_return … call_trace in
301      let final_cost_trace ≝ compute_cost_trace_any_label cm cost_labels end_flag … final_trace in
302        call_cost_trace @ final_cost_trace
303  | tal_step_default end_flag status_pre status_init status_end _ tail_trace _ _ ⇒
304       compute_cost_trace_any_label cm cost_labels end_flag
305        status_init status_end tail_trace
306  ]
307and compute_cost_trace_label_return
308  (cm: BitVectorTrie Byte 16)
309  (cost_labels: BitVectorTrie costlabel 16)
310  (start_status: Status cm) (final_status: Status cm)
311    (the_trace: trace_label_return (ASM_abstract_status cm cost_labels) start_status final_status)
312      on the_trace: list (Σk:costlabel. ∃pc. lookup_opt … pc cost_labels = Some … k) ≝
313  match the_trace with
314  [ tlr_base before after trace_to_lift ⇒ compute_cost_trace_label_label cm … trace_to_lift
315  | tlr_step initial labelled final labelled_trace ret_trace ⇒
316      let labelled_cost ≝ compute_cost_trace_label_label cm … labelled_trace in
317      let return_cost ≝ compute_cost_trace_label_return cm … ret_trace in
318        labelled_cost @ return_cost
319  ].
320 [ %{pc} whd in match label;  generalize in match labelled_proof; whd in ⊢ (% → ?);
321  cases (lookup_opt costlabel … pc cost_labels) normalize
322  [ #abs cases abs | // ]
323 | // ]
324qed.
325
326include alias "arithmetics/nat.ma".
327include alias "basics/logic.ma".
328
329lemma and_intro_right:
330  ∀a, b: Prop.
331    a → (a → b) → a ∧ b.
332  #a #b /3/
333qed.
334
335lemma lt_m_n_to_exists_o_plus_m_n:
336  ∀m, n: nat.
337    m < n → ∃o: nat. m + o = n.
338  #m #n
339  cases m
340  [1:
341    #irrelevant
342    %{n} %
343  |2:
344    #m' #lt_hyp
345    %{(n - S m')}
346    >commutative_plus in ⊢ (??%?);
347    <plus_minus_m_m
348    [1:
349      %
350    |2:
351      @lt_S_to_lt
352      assumption
353    ]
354  ]
355qed.
356
357lemma lt_O_n_to_S_pred_n_n:
358  ∀n: nat.
359    0 < n → S (pred n) = n.
360  #n
361  cases n
362  [1:
363    #absurd
364    cases(lt_to_not_zero 0 0 absurd)
365  |2:
366    #n' #lt_hyp %
367  ]
368qed.
369
370lemma exists_plus_m_n_to_exists_Sn:
371  ∀m, n: nat.
372    m < n → ∃p: nat. S p = n.
373  #m #n
374  cases m
375  [1:
376    #lt_hyp %{(pred n)}
377    @(lt_O_n_to_S_pred_n_n … lt_hyp)
378  |2:
379    #m' #lt_hyp %{(pred n)}
380    @(lt_O_n_to_S_pred_n_n)
381    @(transitive_le … (S m') …)
382    [1:
383      //
384    |2:
385      @lt_S_to_lt
386      assumption
387    ]
388  ]
389qed.
390
391include alias "arithmetics/bigops.ma".
392
393(* This shoudl go in bigops! *)
394theorem bigop_sum_rev: ∀k1,k2,p,B,nil.∀op:Aop B nil.∀f:nat→B.
395 \big[op,nil]_{i<k1+k2|p i} (f i) =
396 op \big[op,nil]_{i<k2|p (i+k1)} (f (i+k1)) \big[op,nil]_{i<k1|p i} (f i).
397 #k1 #k2 #p #B #nil #op #f >bigop_sum
398 >commutative_plus @same_bigop #i @leb_elim normalize
399 [2,4: //
400 | #H1 #H2 <plus_minus_m_m //
401 | #H1 #H2 #H3 <plus_minus_m_m //]
402qed.
403
404(* This is taken by sigma_pi.ma that does not compile now *)
405definition natAop ≝ mk_Aop nat 0 plus (λa.refl ? a) (λn.sym_eq ??? (plus_n_O n))
406   (λa,b,c.sym_eq ??? (associative_plus a b c)).
407
408definition natACop ≝ mk_ACop nat 0 natAop commutative_plus.
409
410definition natDop ≝ mk_Dop nat 0 natACop times (λn.(sym_eq ??? (times_n_O n)))
411   distributive_times_plus.
412
413unification hint  0 ≔ ;
414   S ≟ mk_Aop nat 0 plus (λa.refl ? a) (λn.sym_eq ??? (plus_n_O n))
415   (λa,b,c.sym_eq ??? (associative_plus a b c))
416(* ---------------------------------------- *) ⊢
417   plus ≡ op ? ? S.
418
419unification hint  0 ≔ ;
420   S ≟ mk_ACop nat 0 (mk_Aop nat 0 plus (λa.refl ? a) (λn.sym_eq ??? (plus_n_O n))
421   (λa,b,c.sym_eq ??? (associative_plus a b c))) commutative_plus
422(* ---------------------------------------- *) ⊢
423   plus ≡ op ? ? S.
424   
425unification hint  0 ≔ ;
426   S ≟ natDop
427(* ---------------------------------------- *) ⊢
428   plus ≡ sum ? ? S.
429
430unification hint  0 ≔ ;
431   S ≟ natDop
432(* ---------------------------------------- *) ⊢
433   times ≡ prod ? ? S.
434
435notation > "Σ_{ ident i < n } f"
436  with precedence 20
437for @{'bigop $n plus 0 (λ${ident i}.true) (λ${ident i}. $f)}.
438
439notation < "Σ_{ ident i < n } f"
440  with precedence 20
441for @{'bigop $n plus 0 (λ${ident i}:$X.true) (λ${ident i}:$Y. $f)}.
442
443definition tech_cost_of_label0:
444 ∀cost_labels: BitVectorTrie costlabel 16.
445 ∀cost_map: identifier_map CostTag nat.
446 ∀codom_dom: (∀pc,k. lookup_opt … pc cost_labels = Some … k → present … cost_map k).
447 ∀ctrace:list (Σk:costlabel.∃pc. lookup_opt costlabel 16 pc cost_labels = Some ? k).
448 ∀i,p. present … cost_map (nth_safe ? i ctrace p).
449 #cost_labels #cost_map #codom_dom #ctrace #i #p
450 cases (nth_safe … i ctrace ?) normalize #id * #id_pc #K
451 lapply (codom_dom … K) #k_pres >(lookup_lookup_present … k_pres)
452 % #abs destruct (abs)
453qed.
454
455include alias "arithmetics/nat.ma".
456include alias "basics/logic.ma".
457
458lemma ltb_rect:
459 ∀P:Type[0].∀n,m. (n < m → P) → (¬ n < m → P) → P.
460 #P #n #m lapply (refl … (ltb n m)) cases (ltb n m) in ⊢ (???% → %); #E #H1 #H2
461 [ @H1 @leb_true_to_le @E | @H2 @leb_false_to_not_le @E ]
462qed.
463
464lemma same_ltb_rect:
465 ∀P,n,m,H1,H2,n',m',H1',H2'.
466  ltb n m = ltb n' m' → (∀x,y. H1 x = H1' y) → (∀x,y. H2 x = H2' y) →
467   ltb_rect P n m H1 H2 =
468   ltb_rect P n' m' H1' H2'.
469 #P #n #m #H1 #H2 #n' #m' #H1' #H2' #E #K1 #K2 whd in ⊢ (??%?);
470 cut (∀xxx,yyy,xxx',yyy'.
471   match ltb n m
472   return λx:bool.
473           eq bool (ltb n m) x
474            → (lt n m → P) → (Not (lt n m) → P) → P
475    with
476    [ true ⇒
477        λE0:eq bool (ltb n m) true.
478         λH10:lt n m → P.
479          λH20:Not (lt n m) → P. H10 (xxx E0)
480    | false ⇒
481        λE0:eq bool (ltb n m) false.
482         λH10:lt n m → P.
483          λH20:Not (lt n m) → P. H20 (yyy E0)]
484    (refl … (ltb n m)) H1 H2 =
485   match ltb n' m'
486   return λx:bool.
487           eq bool (ltb n' m') x
488            → (lt n' m' → P) → (Not (lt n' m') → P) → P
489    with
490    [ true ⇒
491        λE0:eq bool (ltb n' m') true.
492         λH10:lt n' m' → P.
493          λH20:Not (lt n' m') → P. H10 (xxx' E0)
494    | false ⇒
495        λE0:eq bool (ltb n' m') false.
496         λH10:lt n' m' → P.
497          λH20:Not (lt n' m') → P. H20 (yyy' E0)]
498    (refl … (ltb n' m')) H1' H2'
499  ) [2: #X @X]
500 >E cases (ltb n' m') #xxx #yyy #xxx' #yyy' normalize
501 [ @K1 | @K2 ]
502qed.
503
504
505definition tech_cost_of_label:
506 ∀cost_labels: BitVectorTrie costlabel 16.
507 ∀cost_map: identifier_map CostTag nat.
508 ∀codom_dom: (∀pc,k. lookup_opt … pc cost_labels = Some … k → present … cost_map k).
509 list (Σk:costlabel.∃pc. lookup_opt costlabel 16 pc cost_labels = Some ? k) →
510 nat → nat
511≝ λcost_labels,cost_map,codom_dom,ctrace,i.
512 ltb_rect ? i (|ctrace|)
513 (λH. lookup_present ?? cost_map (nth_safe ? i ctrace H) ?)
514 (λ_.0).
515 @tech_cost_of_label0 @codom_dom
516qed.
517
518lemma shift_nth_safe:
519 ∀T,i,l2,l1,K1,K2.
520  nth_safe T i l1 K1 = nth_safe T (i+|l2|) (l2@l1) K2.
521 #T #i #l2 elim l2 normalize
522  [ #l1 #K1 <plus_n_O #K2 %
523  | #hd #tl #IH #l1 #K1 <plus_n_Sm #K2 change with (? < ?) in K1; change with (? < ?) in K2;
524    whd in ⊢ (???%); @IH ]
525qed.
526
527lemma shift_nth_prefix:
528 ∀T,l1,i,l2,K1,K2.
529  nth_safe T i l1 K1 = nth_safe T i (l1@l2) K2.
530  #T #l1 elim l1 normalize
531  [
532    #i #l1 #K1 cases(lt_to_not_zero … K1)
533  |
534    #hd #tl #IH #i #l2
535    cases i
536    [
537      //
538    |
539      #i' #K1 #K2 whd in ⊢ (??%%);
540      @IH
541    ]
542  ]
543qed.
544
545lemma tech_cost_of_label_shift:
546 ∀cost_labels,cost_map,codom_dom,l1,l2,i.
547  i < |l2| →
548   tech_cost_of_label cost_labels cost_map codom_dom l2 i =
549   tech_cost_of_label cost_labels cost_map codom_dom (l1@l2) (i+|l1|).
550 #cost_labels #cost_Map #codom_dom #l1 #l2 #i #H
551 whd in match tech_cost_of_label; normalize nodelta @same_ltb_rect
552 [ @(ltb_rect ? i (|l2|)) @(ltb_rect ? (i+|l1|) (|l1@l2|)) #K1 #K2
553   whd in match ltb; normalize nodelta
554   [1: >le_to_leb_true try assumption applyS le_to_leb_true //
555   |4: >not_le_to_leb_false try assumption applyS not_le_to_leb_false
556       change with (¬ ? ≤ ?) in K1; applyS K1
557   |2: @⊥ @(absurd (i+|l1| < |l1@l2|)) // >length_append
558       applyS (monotonic_lt_plus_r … (|l1|)) //
559   |3: @⊥ @(absurd ?? K2) >length_append in K1; #K1 /2 by lt_plus_to_lt_l/ ]
560 | #H1 #H2
561   generalize in match (tech_cost_of_label0 ??? (l1@l2) ??);
562   <(shift_nth_safe … H1) #p %
563 | // ]
564qed.
565
566lemma tech_cost_of_label_prefix:
567 ∀cost_labels,cost_map,codom_dom,l1,l2,i.
568  i < |l1| →
569   tech_cost_of_label cost_labels cost_map codom_dom l1 i =
570   tech_cost_of_label cost_labels cost_map codom_dom (l1@l2) i.
571 #cost_labels #cost_map #codom_dom #l1 #l2 #i #H
572 whd in match tech_cost_of_label; normalize nodelta @same_ltb_rect
573 [1:
574   whd in match ltb; normalize nodelta
575   >(le_to_leb_true … H) applyS le_to_leb_true
576   >length_append whd in H; >commutative_plus @le_plus_a assumption
577 |2:
578   #K1 #K2
579   generalize in match (tech_cost_of_label0 ??? (l1@l2) ??);
580   <(shift_nth_prefix … l1 i l2 K1 K2) //
581 |3:
582   #_ #_ %
583 ]
584qed.
585   
586(* XXX: here *)
587let rec compute_trace_label_return_using_paid_ok_with_trace
588 (cm: ?) (total_program_size: nat) (good_program_witness: good_program cm total_program_size)
589 (cost_labels: BitVectorTrie costlabel 16)
590 (cost_map: identifier_map CostTag nat)
591 (initial: Status cm) (final: Status cm)
592 (trace: trace_label_return (ASM_abstract_status cm cost_labels) initial final)
593 (codom_dom: (∀pc,k. lookup_opt … pc cost_labels = Some … k → present … cost_map k))
594 on trace:
595 ∀dom_codom:(∀k. ∀k_pres:present … cost_map k. ∃pc. lookup_opt … pc cost_labels = Some … k →
596   ∃reachable_witness: reachable_program_counter cm total_program_size pc.
597   pi1 … (block_cost cm pc total_program_size cost_labels reachable_witness good_program_witness) = lookup_present … k_pres).
598  let ctrace ≝ compute_cost_trace_label_return cm … trace in
599  compute_trace_label_return_cost_using_paid cm … trace =
600   (Σ_{i < |ctrace|} (tech_cost_of_label cost_labels cost_map codom_dom ctrace i))
601    ≝ ?
602and compute_trace_any_label_using_paid_ok_with_trace
603 (cm: ?) (total_program_size: nat) (good_program_witness: good_program cm total_program_size)
604 (cost_labels: BitVectorTrie costlabel 16)
605 (trace_ends_flag: trace_ends_with_ret)
606 (cost_map: identifier_map CostTag nat)
607 (initial: Status cm) (final: Status cm)
608 (trace: trace_any_label (ASM_abstract_status cm cost_labels) trace_ends_flag initial final)
609 (codom_dom: (∀pc,k. lookup_opt … pc cost_labels = Some … k → present … cost_map k))
610 (dom_codom:(∀k. ∀k_pres:present … cost_map k. ∃pc. lookup_opt … pc cost_labels = Some … k →
611   ∃reachable_witness: reachable_program_counter cm total_program_size pc.
612   pi1 … (block_cost cm pc total_program_size cost_labels reachable_witness good_program_witness) = lookup_present … k_pres))
613 on trace:
614  let ctrace ≝ compute_cost_trace_any_label … trace in
615  compute_trace_any_label_cost_using_paid … trace =
616   (Σ_{i < |ctrace|} (tech_cost_of_label cost_labels cost_map codom_dom ctrace i))
617    ≝ ?
618and compute_trace_label_label_using_paid_ok_with_trace
619 (cm: ?) (total_program_size: nat) (good_program_witness: good_program cm total_program_size)
620 (cost_labels: BitVectorTrie costlabel 16)
621 (trace_ends_flag: trace_ends_with_ret)
622 (cost_map: identifier_map CostTag nat)
623 (initial: Status cm) (final: Status cm)
624 (trace: trace_label_label (ASM_abstract_status cm cost_labels) trace_ends_flag initial final)
625 (codom_dom: (∀pc,k. lookup_opt … pc cost_labels = Some … k → present … cost_map k))
626 (dom_codom:(∀k. ∀k_pres:present … cost_map k. ∃pc. lookup_opt … pc cost_labels = Some … k →
627   ∃reachable_witness: reachable_program_counter cm total_program_size pc.
628   pi1 … (block_cost cm pc total_program_size cost_labels reachable_witness good_program_witness) = lookup_present … k_pres))
629 on trace:
630  let ctrace ≝ compute_cost_trace_label_label … trace in
631  compute_trace_label_label_cost_using_paid … trace =
632   (Σ_{i < |ctrace|} (tech_cost_of_label cost_labels cost_map codom_dom ctrace i))
633    ≝ ?.
634  cases trace normalize nodelta
635  [ #sb #sa #tr #dom_codom whd in ⊢ (??%?);
636    whd in match (compute_cost_trace_label_return ?????);
637    @(compute_trace_label_label_using_paid_ok_with_trace … good_program_witness) @dom_codom
638  | #si #sl #sf #tr1 #tr2 #dom_codom
639    whd in ⊢ (??%?);
640    whd in match (compute_cost_trace_label_return ?????);
641    >append_length >bigop_sum_rev >commutative_plus @eq_f2
642    [ >(compute_trace_label_return_using_paid_ok_with_trace … good_program_witness … cost_map … codom_dom dom_codom)
643      -compute_trace_label_return_using_paid_ok_with_trace
644      @same_bigop [//] #i #H #_ -dom_codom @tech_cost_of_label_shift //
645    | >(compute_trace_label_label_using_paid_ok_with_trace … good_program_witness … cost_map … codom_dom … dom_codom)
646      -compute_trace_label_label_using_paid_ok_with_trace
647      @same_bigop [//] #i #H #_ @(tech_cost_of_label_prefix … H)
648    ]
649  |8:
650    #end_flag #start_status #end_status #trace_any_label #costed_assm
651    whd in ⊢ (??%?); whd in ⊢ (??(?%?)?);
652    >(compute_trace_any_label_using_paid_ok_with_trace … good_program_witness … cost_map … codom_dom … dom_codom)
653    >bigop_0 in ⊢ (???%); >commutative_plus @eq_f2
654    [1:
655      @same_bigop [//] #i #H #_ -dom_codom >(plus_n_O i) >plus_n_Sm
656      <(tech_cost_of_label_shift ??? [?] ? i) //
657    |2:
658      whd in ⊢ (???%); (* XXX: should be easy *)
659      cases daemon
660    ]
661  |3:
662    #start_status #final_status #execute_assm #classifier_assm #costed_assm
663    %
664  |4:
665    #start_status #final_status #execute_assm #classifier_assm %
666  |5:
667    #status_pre_fun_call #status_start_fun_call #status_final #execute_assm
668    #classifier_assm #after_return_assm #trace_label_return #costed_assm
669    whd in ⊢ (??%?);
670    @(compute_trace_label_return_using_paid_ok_with_trace … good_program_witness … cost_map … codom_dom dom_codom)
671  |6:
672    #end_flag #status_pre_fun_call #status_start_fun_call #status_after_fun_call
673    #status_final #execute_assm #classifier_assm #after_return_assm #trace_label_return
674    #costed_assm #trace_any_label
675    whd in ⊢ (??%?);
676    >(compute_trace_label_return_using_paid_ok_with_trace … good_program_witness … cost_map … codom_dom dom_codom)
677    >(compute_trace_any_label_using_paid_ok_with_trace … good_program_witness … cost_map … codom_dom … dom_codom)
678    >length_append >bigop_sum_rev >commutative_plus @eq_f2
679    [ @same_bigop [2: #i #H #_ -dom_codom @tech_cost_of_label_shift assumption
680                  |1: #i #H % ]
681    | @same_bigop [#i #H %] #i #H #_ @(tech_cost_of_label_prefix … H)
682    ]
683  |7:
684    #end_flag #status_pre_fun_call #status_start_fun_call #status_final
685    #execute_assm #trace_any_label #classifier_assm #costed_assm
686    whd in ⊢ (??%?);
687    @(compute_trace_any_label_using_paid_ok_with_trace … good_program_witness … cost_map … codom_dom … dom_codom)
688  ]
689qed.
690 
691lemma compute_max_trace_label_return_cost_ok_with_trace:
692  ∀code_memory: BitVectorTrie Byte 16.
693  ∀total_program_size: nat.
694  ∀good_program_witness: good_program code_memory total_program_size.
695  ∀cost_labels: BitVectorTrie costlabel 16.
696  ∀cost_map: identifier_map CostTag nat.
697  ∀initial: Status code_memory.
698  ∀final: Status code_memory.
699  ∀trace: trace_label_return (ASM_abstract_status code_memory cost_labels) initial final.
700  ∀codom_dom: (∀pc,k. lookup_opt … pc cost_labels = Some … k → present … cost_map k).
701  ∀dom_codom: (∀k. ∀k_pres:present … cost_map k. ∃pc. lookup_opt … pc cost_labels = Some … k →
702    ∃reachable_witness: reachable_program_counter code_memory total_program_size pc.
703      pi1 … (block_cost code_memory pc total_program_size cost_labels reachable_witness good_program_witness) = lookup_present … k_pres).
704        let ctrace ≝ compute_cost_trace_label_return code_memory … trace in
705          clock … code_memory … final =
706            clock … code_memory … initial + (Σ_{i < |ctrace|} (tech_cost_of_label cost_labels cost_map codom_dom ctrace i)).
707  #code_memory #total_program_size #good_program_witness #cost_labels #cost_map
708  #initial #final #trace #codom_dom #dom_codom normalize nodelta
709  <compute_trace_label_return_using_paid_ok_with_trace try assumption
710  >commutative_plus >compute_trace_label_return_cost_using_paid_ok //
711qed.
Note: See TracBrowser for help on using the repository browser.