source: src/ASM/CostsProof.ma @ 2754

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