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