source: src/ASM/ASMCosts.ma @ 2645

Last change on this file since 2645 was 2593, checked in by mckinna, 8 years ago

Finally chased down wicked failure to close case 1.1: of trace_compute_paid_trace_cl_other, arising from change to type of as_classify in common/StructuredTraces.ma.

Pointless case analysis on instruction in 1.1 thereby also avoided.

File size: 36.3 KB
RevLine 
[1486]1include "ASM/ASM.ma".
2include "ASM/Arithmetic.ma".
3include "ASM/Fetch.ma".
[2573]4include "ASM/Interpret.ma". (* includes ASM/AbstractStatus.ma *)
[1648]5
[2573]6(*include "common/StructuredTraces.ma". (* already included by ASM/AbstractStatus.ma *) *)
[2498]7
[2575]8(* could this definition be moved to ASM/AbstractStatus.ma ???
9   no: requires ASM/Interpret/execute_1 *)
[2573]10
[2516]11definition ASM_abstract_status: ∀code_memory. BitVectorTrie costlabel 16 → abstract_status ≝
[1938]12  λcode_memory.
13  λcost_labels.
14    mk_abstract_status
15      (Status code_memory)
16      (λs,s'. (execute_1 … s) = s')
17      word_deqset
18      (program_counter …)
[2573]19      (λs. Some ? (ASM_classify … s))
[2575]20      (λpc.lookup_opt … pc cost_labels)
[1938]21      (next_instruction_properly_relates_program_counters code_memory)
[2504]22      ? (* (λs.False) *)
[2573]23      ?
[1938]24      ?.
[2573]25  cases daemon (* XXX: is_final predicate, (tail)call_ident function *)
[1938]26qed.
27
[2573]28lemma Some_inv : ∀A.∀a,b. Some A a = Some A b → a = b.
29  #A#a #b #eq destruct //
30  qed.
31
[1929]32definition trace_any_label_length ≝
[1919]33  λcode_memory: BitVectorTrie Byte 16.
[2516]34  λcost_labels: BitVectorTrie costlabel 16.
[1919]35  λtrace_ends_flag: trace_ends_with_ret.
36  λstart_status: Status code_memory.
37  λfinal_status: Status code_memory.
[2575]38  λthe_trace: trace_any_label (ASM_abstract_status … cost_labels) trace_ends_flag start_status final_status.
[1919]39    as_trace_any_label_length' … the_trace.
[1911]40
[1921]41let rec all_program_counter_list
42  (n: nat)
43    on n ≝
44  match n with
45  [ O ⇒ [ ]
46  | S n' ⇒ (bitvector_of_nat 16 n')::(all_program_counter_list n')
47  ].
48
49lemma all_program_counter_list_bound_eq:
50  ∀bound: nat.
51    |all_program_counter_list bound| = bound.
52  #bound elim bound try %
53  #bound' #inductive_hypothesis normalize
54  >inductive_hypothesis %
55qed.
56
57lemma mem_all_program_counter_list_lt_bound:
58  ∀bound: nat.
59  ∀bound_proof: bound ≤ 2^16.
60  ∀e: Word.
61    memb word_deqset e (all_program_counter_list bound) = true →
62      nat_of_bitvector … e < bound.
63  #bound elim bound
64  [1:
65    #bound_proof #e
66    normalize #absurd destruct(absurd)
67  |2:
68    #bound' #inductive_hypothesis
69    #bound_proof #e
70    change with (if eq_bv ??? then ? else ? = ? → ?)
71    @eq_bv_elim
72    [1:
73      #eq_assm
74      normalize nodelta destruct #_
75      >nat_of_bitvector_bitvector_of_nat_inverse try assumption
76      normalize %
77    |2:
78      #neq_assm normalize nodelta
79      #memb_assm %2 @inductive_hypothesis
80      try assumption
81      @(transitive_le bound' (S bound'))
82      try assumption %2 %
83    ]
84  ]
85qed.
86
87lemma lt_bound_mem_all_program_counter_list:
88  ∀bound: nat.
89  ∀bound_proof: bound ≤ 2^16.
90  ∀e: Word.
91    nat_of_bitvector … e < bound →
92      memb word_deqset e (all_program_counter_list bound) = true.
93  #bound elim bound
94  [1:
95    #bound_proof #e normalize
96    #absurd cases (lt_to_not_zero … absurd)
97  |2:
98    #bound' #inductive_hypothesis
99    #bound_proof #e
100    change with (? → if eq_bv ??? then ? else ? = ?)
101    #assm
102    cases (le_to_or_lt_eq … (nat_of_bitvector 16 e) bound' ?)
103    [1:
104      #e_lt_assm
105      @eq_bv_elim
106      [1:
107        #eq_assm normalize nodelta %
108      |2:
109        #neq_assm normalize nodelta
110        @inductive_hypothesis try assumption
111        @(transitive_le bound' (S bound')) try assumption
112        %2 %
113      ]
114    |2:
115      #relevant >eq_eq_bv normalize nodelta try %
116      destruct >bitvector_of_nat_inverse_nat_of_bitvector %
117    |3:
118      normalize in assm;
119      @le_S_S_to_le assumption
120    ]
121  ]
122qed.
123
124include alias "arithmetics/nat.ma".
125
[2573]126lemma execute_1_and_program_counter_after_other_in_lockstep00:
[1921]127  ∀code_memory: BitVectorTrie Byte 16.
128  ∀start_status: Status code_memory.
[2573]129      let 〈instruction, pc, ticks〉 ≝ fetch code_memory (program_counter … start_status) in
130    ASM_classify0 instruction = cl_other →
131        program_counter ? ? (execute_1 … start_status) =
132          program_counter_after_other pc instruction.
133  #code_memory #start_status
134  whd in match execute_1; normalize nodelta 
135  cases (execute_1' code_memory start_status) #the_status; * #_
136  cases (fetch ? ?); * #instruction #pc #ticks
137  #classify_assm #classify_assm0 @classify_assm assumption
138qed.
139
140lemma execute_1_and_program_counter_after_other_in_lockstep0:
141  ∀code_memory: BitVectorTrie Byte 16.
142  ∀start_status: Status code_memory.
[2575]143    ASM_classify … start_status = cl_other →
[1921]144      let 〈instruction, pc, ticks〉 ≝ fetch code_memory (program_counter … start_status) in
145        program_counter ? ? (execute_1 … start_status) =
146          program_counter_after_other pc instruction.
147  #code_memory #start_status #classify_assm
148  whd in match execute_1; normalize nodelta
149  cases (execute_1' code_memory start_status) #the_status
150  * #_ whd in classify_assm:(??%?); whd in match (current_instruction ??) in classify_assm;
151  cases (fetch ? ?) in classify_assm; * #instruction #pc #ticks
152  #classify_assm #classify_assm' @classify_assm' assumption
[1962]153qed.
[1921]154
[2573]155lemma execute_1_and_program_counter_after_other_in_lockstep:
156  ∀code_memory: BitVectorTrie Byte 16.
157  ∀cost_labels: BitVectorTrie costlabel 16.
158  ∀start_status: Status code_memory.
159    as_classifier (ASM_abstract_status code_memory cost_labels) start_status cl_other →
160      let 〈instruction, pc, ticks〉 ≝ fetch code_memory (program_counter … start_status) in
161        program_counter ? ? (execute_1 … start_status) =
162          program_counter_after_other pc instruction.
163  #code_memory #cost_labels #start_status #classify_assm
164  change with (Some ? (ASM_classify0 ?) = ?) in classify_assm;
165  destruct (classify_assm)
166  @execute_1_and_program_counter_after_other_in_lockstep0 assumption.
167qed. 
168
[1929]169lemma nat_of_bitvector_lt_bound:
170  ∀n: nat.
171  ∀b: BitVector n.
172    nat_of_bitvector n b < 2^n.
173  #n #b cases daemon
174qed.
[1921]175
176lemma sublist_tal_pc_list_all_program_counter_list:
177  ∀code_memory: BitVectorTrie Byte 16.
[2516]178  ∀cost_labels: BitVectorTrie costlabel 16.
[1921]179  ∀start, final: Status code_memory.
180  ∀trace_ends_flag.
[2575]181  ∀the_trace: trace_any_label (ASM_abstract_status … cost_labels) trace_ends_flag start final.
[1929]182    sublist ? (tal_pc_list … the_trace) (all_program_counter_list (2^16)).
183  #code_memory #cost_labels #start #final #trace_ends_flag #the_trace
[1921]184  whd in match (sublist ???); #pc #memb_assm
[1929]185  @lt_bound_mem_all_program_counter_list try %
186  @nat_of_bitvector_lt_bound
[1921]187qed.
188
189corollary tal_pc_list_length_leq_total_program_size:
190  ∀code_memory: BitVectorTrie Byte 16.
[2516]191  ∀cost_labels: BitVectorTrie costlabel 16.
[1921]192  ∀start, final: Status code_memory.
193  ∀trace_ends_flag.
[2575]194  ∀the_trace: trace_any_label (ASM_abstract_status … cost_labels) trace_ends_flag start final.
195  ∀unrepeating_witness: tal_unrepeating … the_trace.
[1929]196    |tal_pc_list … the_trace| ≤ 2^16.
197  #code_memory #cost_labels #start #final #trace_ends_flag #the_trace #unrepeating_witness
198  <(all_program_counter_list_bound_eq (2^16))
[1921]199  @sublist_length
200  [1:
201    @tal_unrepeating_uniqueb
202    assumption
203  |2:
204    @sublist_tal_pc_list_all_program_counter_list
205    assumption
206  ]
207qed.
208
[1658]209let rec compute_paid_trace_any_label
[2516]210  (code_memory: BitVectorTrie Byte 16) (cost_labels: BitVectorTrie costlabel 16)
[1669]211    (trace_ends_flag: trace_ends_with_ret) (start_status: Status code_memory)
212      (final_status: Status code_memory)
[2575]213        (the_trace: trace_any_label (ASM_abstract_status … cost_labels) trace_ends_flag start_status final_status)
[1658]214       on the_trace: nat ≝
215  match the_trace with
[1669]216  [ tal_base_not_return the_status _ _ _ _ ⇒ current_instruction_cost … the_status
217  | tal_base_return the_status _ _ _ ⇒ current_instruction_cost … the_status
[1902]218  | tal_base_call pre_fun_call start_fun_call final _ _ _ _ call_trace ⇒ current_instruction_cost … pre_fun_call
[1658]219  | tal_step_call end_flag pre_fun_call start_fun_call after_fun_call final
[1902]220     _ _ _ call_trace _ final_trace ⇒
[1669]221      let current_instruction_cost ≝ current_instruction_cost … pre_fun_call in
222      let final_trace_cost ≝ compute_paid_trace_any_label … cost_labels end_flag … final_trace in
[1658]223        current_instruction_cost + final_trace_cost
224  | tal_step_default end_flag status_pre status_init status_end _ tail_trace _ _ ⇒
[1669]225      let current_instruction_cost ≝ current_instruction_cost … status_pre in
[1658]226      let tail_trace_cost ≝
[1669]227       compute_paid_trace_any_label … cost_labels end_flag
[1658]228        status_init status_end tail_trace
229      in
230        current_instruction_cost + tail_trace_cost
[2593]231  | _ ⇒ ? (* XXX Paolo ??? *)
[2573]232  ].
233  cases daemon
234  qed.
[1658]235
236definition compute_paid_trace_label_label ≝
[1669]237  λcode_memory: BitVectorTrie Byte 16.
[2516]238  λcost_labels: BitVectorTrie costlabel 16.
[1658]239  λtrace_ends_flag: trace_ends_with_ret.
[1669]240  λstart_status: Status code_memory.
241  λfinal_status: Status code_memory.
[2575]242  λthe_trace: (trace_label_label (ASM_abstract_status … cost_labels) trace_ends_flag start_status final_status).
[1658]243  match the_trace with
244  [ tll_base ends_flag initial final given_trace labelled_proof ⇒
245      compute_paid_trace_any_label … given_trace
246  ].
247
[1684]248lemma trace_compute_paid_trace_cl_other:
[1669]249  ∀code_memory' : (BitVectorTrie Byte 16).
250  ∀program_counter' : Word.
[2516]251  ∀cost_labels : BitVectorTrie costlabel 16.
[1669]252  ∀program_size' : ℕ.
253  ∀ticks : ℕ.
254  ∀instruction : instruction.
255  ∀program_counter'' : Word.
256  ∀FETCH : (〈instruction,program_counter'',ticks〉=fetch code_memory' program_counter').
[1910]257  let program_counter''' ≝ program_counter_after_other program_counter'' instruction in
[1669]258  ∀start_status : (Status code_memory').
259  ∀final_status : (Status code_memory').
260  ∀trace_ends_flag : trace_ends_with_ret.
[2575]261  ∀the_trace : (trace_any_label (ASM_abstract_status … cost_labels) trace_ends_flag start_status final_status).
[2573]262  ∀program_counter_refl : (program_counter' = program_counter … start_status).
[2575]263  ∀size_invariant : trace_any_label_length … the_trace ≤S program_size'.
[1669]264  ∀classify_assm: ASM_classify0 instruction = cl_other.
265  ∀pi1 : ℕ.
[1910]266   (if match lookup_opt costlabel 16 program_counter''' cost_labels with 
[1669]267         [None ⇒ true
268         |Some _ ⇒ false
269         ] 
270    then
271      ∀start_status0:Status code_memory'.
272      ∀final_status0:Status code_memory'.
273      ∀trace_ends_flag0:trace_ends_with_ret.
[2575]274      ∀the_trace0:trace_any_label (ASM_abstract_status … cost_labels) trace_ends_flag0 start_status0 final_status0.
275        trace_any_label_length … the_trace0 ≤ program_size' →
[2573]276        program_counter''' = program_counter … start_status0 →
[1901]277                  pi1
[2575]278                    =compute_paid_trace_any_label … the_trace0
[1669]279    else (pi1=O) )
[1906]280   → ticks+pi1
[2575]281     =compute_paid_trace_any_label … the_trace.
[1929]282  #code_memory' #program_counter' #cost_labels #program_size' #ticks #instruction
283  #program_counter'' #FETCH #start_status #final_status #trace_ends_flag #the_trace
284  #program_counter_refl #size_invariant #classify_assm #recursive_block_cost
[1692]285  #recursive_assm
286  @(trace_any_label_inv_ind … the_trace)
[2575]287    [2:
288      #start_status' #final_status' #execute_assm #classifier_assm #trace_ends_assm
289      #start_status_refl #final_status_refl #the_trace_assm destruct @⊥
290    |3:
291      #status_pre_fun_call #status_start_fun_call #status_final #execute_assm
292      #classifier_assm #after_return_assm #trace_label_return #costed_assm
293      #ends_flag_refl #start_status_refl #final_status_refl #the_trace_refl
294      destruct @⊥
295    |4: (* XXX *)
296      cases daemon
297    |5:
298      #end_flag #status_pre_fun_call #status_start_fun_call #status_after_fun_call
299      #status_final #execute_assm #classifier_assm #after_return_assm #trace_label_return
300      #costed_assm #trace_any_label #trace_ends_flag_refl #start_status_refl
301      #final_status_refl #the_trace_refl destruct @⊥
302    |6:
[1669]303      #end_flag #status_pre #status_init #status_end #execute_assm #trace_any_label
304      #classifier_assm #costed_assm #trace_ends_refl #start_status_refl #final_status_refl
305      #the_trace_refl
306      destruct
307      whd in match (trace_any_label_length … (tal_step_default …));
308      whd in match (compute_paid_trace_any_label … (tal_step_default …));
309      whd in costed_assm:(?%);
310      generalize in match costed_assm;
311      generalize in match (refl … (lookup_opt … (program_counter … (execute_1 … status_pre)) cost_labels));
[1927]312      whd in match (as_label ??);
[1669]313      generalize in match (lookup_opt … (program_counter … (execute_1 … status_pre)) cost_labels)
[1927]314        in ⊢ (??%? → % → ?);
[1669]315      #lookup_assm cases lookup_assm
316      [1:
[1910]317        #None_lookup_opt_assm normalize nodelta #_
[1669]318        generalize in match recursive_assm;
[2573]319        (*lapply (execute_1_and_program_counter_after_other_in_lockstep0 code_memory' ? (Some_inv … classifier_assm))*)
320        lapply (execute_1_and_program_counter_after_other_in_lockstep … classifier_assm)
[1910]321        <FETCH normalize nodelta #rewrite_assm >rewrite_assm in None_lookup_opt_assm;
322        #None_lookup_opt_assm <None_lookup_opt_assm
323        normalize nodelta #new_recursive_assm
324        cases(new_recursive_assm (execute_1 code_memory' status_pre) status_end
[1929]325          end_flag trace_any_label ? ?) try (>rewrite_assm %)
[1910]326        whd in match (current_instruction_cost … status_pre);
327        cut(ticks = \snd (fetch code_memory'
[2573]328           (program_counter … status_pre)))
[1929]329        [1,3:
[1910]330          <FETCH %
[1669]331        |2:
[1912]332          #ticks_refl_assm
333          >ticks_refl_assm %
[1910]334        |4:
335          #ticks_refl_assm
[1912]336          change with (S ? ≤ S ?) in size_invariant;
337          lapply (le_S_S_to_le … size_invariant) #assm
338          assumption
[1669]339        ]
340      |2:
[1927]341        #costlabel #Some_lookup_opt_assm <Some_lookup_opt_assm
342        #absurd
343        cases (not_Some_neq_None_to_False ?? absurd)
[1669]344      ]
345    |1:
346      #status_start #status_final #execute_assm #classifier_assm #costed_assm
347      #trace_ends_flag_refl #start_status_refl #final_status_refl #the_trace_refl
348      destruct
349      whd in match (trace_any_label_length … (tal_base_not_return …));
350      whd in match (compute_paid_trace_any_label … (tal_base_not_return …));
351      whd in costed_assm;
352      generalize in match costed_assm;
353      generalize in match (refl … (lookup_opt … (program_counter … (execute_1 … status_start)) cost_labels));
[1927]354      whd in match (as_label ??);
[2575]355      generalize in match (lookup_opt … (program_counter … (execute_1 … status_start)) cost_labels)
[1927]356        in ⊢ (??%? → % → ?);
[1669]357      #lookup_assm cases lookup_assm
358      [1:
[1927]359        #None_lookup_opt_assm
360        #absurd @⊥ cases absurd -absurd #absurd @absurd %
[1669]361      |2:
362        #costlabel #Some_lookup_opt_assm normalize nodelta #ignore
363        generalize in match recursive_assm;
[2593]364        cases classifier_assm -classifier_assm #classifier_assm
[2575]365        [2:
366          lapply (execute_1_and_program_counter_after_other_in_lockstep … classifier_assm)
367          <FETCH normalize nodelta #rewrite_assm >rewrite_assm in Some_lookup_opt_assm;
368          #Some_lookup_opt_assm <Some_lookup_opt_assm
369          normalize nodelta #new_recursive_assm >new_recursive_assm
370          cut(ticks = \snd (fetch code_memory'
371             (program_counter … status_start)))
372          [1:
373            <FETCH %
374          |2:
375            #ticks_refl_assm >ticks_refl_assm
376            <plus_n_O %
377          ]
[2593]378        |1:
379                (* JHM: wicked failure because Some prevent previous unfolding of ASM_classify here *)
380                @⊥
381                (* *** (* previously: unnecessary case analysis on instruction *)
382          change with ((Some ? (ASM_classify0 ?) = ?) → ?);
[1910]383          whd in match (current_instruction code_memory' status_start);
384          <FETCH generalize in match classify_assm;
385          cases instruction
386          [8:
387            #preinstruction normalize nodelta
388            whd in match ASM_classify0; normalize nodelta
389            #contradiction >contradiction #absurd destruct(absurd)
[1710]390          ]
[1910]391          try(#addr1 #addr2 normalize nodelta #ignore #absurd destruct(absurd))
392          try(#addr normalize nodelta #ignore #absurd destruct(absurd))
393          normalize in ignore; destruct(ignore)
[2593]394                *** *)
[2575]395        ]
396      ]
397    ]
398  change with (Some ? (ASM_classify0 ?) = ?) in classifier_assm;
399  whd in match current_instruction in classifier_assm; normalize nodelta in classifier_assm;
400  whd in match current_instruction0 in classifier_assm; normalize nodelta in classifier_assm;
401  <FETCH in classifier_assm; >classify_assm #absurd destruct(absurd)
[1910]402qed.
[1669]403
[1684]404lemma trace_compute_paid_trace_cl_jump:
405  ∀code_memory': BitVectorTrie Byte 16.
406  ∀program_counter': Word.
[2516]407  ∀cost_labels: BitVectorTrie costlabel 16.
[1684]408  ∀first_time_around: bool.
409  ∀program_size': ℕ.
410  ∀ticks: ℕ.
411  ∀instruction: instruction.
412  ∀program_counter'': Word.
413  ∀FETCH: 〈instruction,program_counter'',ticks〉 = fetch code_memory' program_counter'.
414  ∀start_status: (Status code_memory').
415  ∀final_status: (Status code_memory').
416  ∀trace_ends_flag: trace_ends_with_ret.
[2575]417  ∀the_trace: (trace_any_label (ASM_abstract_status … cost_labels) trace_ends_flag start_status final_status).
[2573]418  ∀program_counter_refl: (program_counter' = program_counter … start_status).
[1684]419  ∀classify_assm: ASM_classify0 instruction = cl_jump.
[1906]420    ticks
[2575]421     =compute_paid_trace_any_label … the_trace.
[1929]422  #code_memory' #program_counter' #cost_labels #first_time_around
[1912]423  #program_size' #ticks #instruction #program_counter'' #FETCH
[1691]424  #start_status #final_status
[1684]425  #trace_ends_flag #the_trace #program_counter_refl #classify_assm
426  @(trace_any_label_inv_ind … the_trace)
[2573]427  [6:
[1692]428    #end_flag #status_pre #status_init #status_end #execute_assm #trace_any_label
429    #classifier_assm #costed_assm #trace_ends_refl #start_status_refl #final_status_refl
430    #the_trace_refl destruct @⊥
431  |1:
432    #status_start #status_final #execute_assm #classifier_assm #costed_assm
[1684]433    #trace_ends_flag_refl #start_status_refl #final_status_refl #the_trace_refl
434    destruct
435    whd in match (trace_any_label_length … (tal_base_not_return …));
[1906]436    whd in match (compute_paid_trace_any_label … (tal_base_not_return …));
437    <FETCH %
[1684]438  |2:
[1692]439    #start_status' #final_status' #execute_assm #classifier_assm #trace_ends_assm
440    #start_status_refl #final_status_refl #the_trace_assm destruct @⊥
[1684]441  |3:
442    #status_pre_fun_call #status_start_fun_call #status_final #execute_assm
443    #classifier_assm #after_return_assm #trace_label_return #costed_assm
[1692]444    #ends_flag_refl #start_status_refl #final_status_refl #the_trace_refl
[1684]445    destruct @⊥
[2573]446  |4: (* XXX *)
447    cases daemon
448  |5:
[1684]449    #end_flag #status_pre_fun_call #status_start_fun_call #status_after_fun_call
450    #status_final #execute_assm #classifier_assm #after_return_assm #trace_label_return
[1692]451    #costed_assm #trace_any_label #trace_ends_flag_refl #start_status_refl
[1684]452    #final_status_refl #the_trace_refl destruct @⊥
453  ]
[2573]454  change with (Some ? (ASM_classify0 ?) = ?) in classifier_assm;
[1684]455  whd in match current_instruction in classifier_assm; normalize nodelta in classifier_assm;
456  whd in match current_instruction0 in classifier_assm; normalize nodelta in classifier_assm;
457  <FETCH in classifier_assm; >classify_assm #absurd destruct(absurd)
458qed.
459
460lemma trace_compute_paid_trace_cl_call:
461  ∀code_memory' : (BitVectorTrie Byte 16).
462  ∀program_counter' : Word.
[2516]463  ∀cost_labels : BitVectorTrie costlabel 16.
[1684]464  ∀program_size' : ℕ.
465  ∀ticks : ℕ.
466  ∀instruction : instruction.
467  ∀program_counter'' : Word.
468  ∀FETCH : (〈instruction,program_counter'',ticks〉=fetch code_memory' program_counter').
469  ∀start_status : (Status code_memory').
470  ∀final_status : (Status code_memory').
471  ∀trace_ends_flag : trace_ends_with_ret.
[2575]472  ∀the_trace : trace_any_label (ASM_abstract_status … cost_labels) trace_ends_flag start_status final_status.
[2573]473  ∀program_counter_refl : (program_counter' = program_counter … start_status).
[2575]474  ∀size_invariant : trace_any_label_length … the_trace ≤S program_size'.
[1684]475  ∀classify_assm: ASM_classify0 instruction = cl_call.
[1692]476  (∀pi1:ℕ
477  .if match lookup_opt costlabel 16 program_counter'' cost_labels with 
478      [None ⇒ true | Some _ ⇒ false] 
[1684]479   then (∀start_status0:Status code_memory'
480             .∀final_status0:Status code_memory'
481              .∀trace_ends_flag0:trace_ends_with_ret
482               .∀the_trace0:trace_any_label
[2575]483                                        (ASM_abstract_status … cost_labels)
[1912]484                                        trace_ends_flag0 start_status0 final_status0.
[2575]485                trace_any_label_length … the_trace0
[1912]486                   ≤ program_size' →
487                program_counter''
[2573]488                 =program_counter … start_status0
[1906]489                 → pi1
[2575]490                   =compute_paid_trace_any_label … the_trace0) 
[1684]491   else (pi1=O) 
[1906]492   → ticks+pi1
[2575]493     =compute_paid_trace_any_label … the_trace).
[1929]494  #code_memory' #program_counter' #cost_labels #program_size'
[1912]495  #ticks #instruction #program_counter'' #FETCH
[1691]496  #start_status #final_status #trace_ends_flag
[1912]497  #the_trace #program_counter_refl #size_invariant #classify_assm
498  #recursive_block_cost #recursive_assm
[1684]499  @(trace_any_label_inv_ind … the_trace)
[2573]500  [6:
[1692]501    #end_flag #status_pre #status_init #status_end #execute_assm #trace_any_label
502    #classifier_assm #costed_assm #trace_ends_refl #start_status_refl #final_status_refl
503    #the_trace_refl destruct @⊥
504  |1:
505    #status_start #status_final #execute_assm #classifier_assm #costed_assm
506    #trace_ends_flag_refl #start_status_refl #final_status_refl #the_trace_refl
507    destruct @⊥
508  |2:
509    #start_status' #final_status' #execute_assm #classifier_assm #trace_ends_assm
510    #start_status_refl #final_status_refl #the_trace_assm destruct @⊥
511  |3:
512    #status_pre_fun_call #status_start_fun_call #status_final #execute_assm
513    #classifier_assm #after_return_assm #trace_label_return #costed_assm
[1902]514    #ends_flag_refl #start_status_refl #final_status_refl #the_trace_refl
[1692]515    destruct
516    whd in match (trace_any_label_length … (tal_base_call …));
517    whd in match (compute_paid_trace_any_label … (tal_base_call …));
518    whd in costed_assm;
519    generalize in match costed_assm;
520    generalize in match (refl … (lookup_opt … (program_counter … status_final) cost_labels));
[1927]521    whd in match (as_label ??);
[1692]522    generalize in match (lookup_opt … (program_counter … status_final) cost_labels)
[1927]523      in ⊢ (??%? → % → ?);
[1692]524    #lookup_assm cases lookup_assm
525    [1:
526      #None_lookup_opt normalize nodelta #absurd cases absurd
[1927]527      -absurd #absurd @⊥ @absurd %
[1692]528    |2:
529      #costlabel #Some_lookup_opt normalize nodelta #ignore
530      generalize in match recursive_assm;
[2573]531      cut(program_counter'' = (program_counter … status_final))
[1692]532      [1:
[1902]533        generalize in match after_return_assm;
[1901]534        whd in ⊢ (% → ?); <FETCH normalize nodelta #relevant <relevant %
[1692]535      |2:
536        #program_counter_assm >program_counter_assm <Some_lookup_opt
[1906]537        normalize nodelta #new_recursive_assm >new_recursive_assm
[2573]538        cut(ticks = \snd (fetch code_memory' (program_counter … status_pre_fun_call)))
[1906]539        [1:
540          <FETCH %
[1692]541        |2:
[1906]542          #ticks_refl_assm >ticks_refl_assm
543          <plus_n_O %
[1692]544        ]
545      ]
546    ]
[2573]547  |4: (* XXX *)
548    cases daemon
549  |5:
[1692]550    #end_flag #status_pre_fun_call #status_start_fun_call #status_after_fun_call
551    #status_final #execute_assm #classifier_assm #after_return_assm #trace_label_return
[1902]552    #costed_assm #trace_any_label #trace_ends_flag_refl #start_status_refl
553    #final_status_refl #the_trace_refl
[1901]554    generalize in match execute_assm; destruct #execute_assm
[1692]555    whd in match (trace_any_label_length … (tal_step_call …));
556    whd in match (compute_paid_trace_any_label … (tal_step_call …));
557    whd in costed_assm:(?%);
558    generalize in match costed_assm;
559    generalize in match (refl … (lookup_opt … (program_counter … status_after_fun_call) cost_labels));
[1927]560    whd in match (as_label ??);
[1692]561    generalize in match (lookup_opt … (program_counter … status_after_fun_call) cost_labels)
[1927]562      in ⊢ (??%? → % → ?);
[1692]563    #lookup_assm cases lookup_assm
564    [1:
565      #None_lookup_opt_assm normalize nodelta #ignore
566      generalize in match recursive_assm;
567      cut(program_counter'' = program_counter … status_after_fun_call)
568      [1:
[1902]569        generalize in match after_return_assm;
[1901]570        whd in ⊢ (% → ?); <FETCH normalize nodelta #relevant >relevant %
[1692]571      |2:
572        #program_counter_refl >program_counter_refl <None_lookup_opt_assm
[1906]573        normalize nodelta #new_recursive_assm
[1912]574        cases (new_recursive_assm … trace_any_label ? ?)
[1906]575        [1:
576          @plus_right_monotone whd in ⊢ (???%); <FETCH %
577        |2:
[1919]578          @le_S_S_to_le @size_invariant
[1912]579        |3:
[1692]580          %
581        ]
582      ]
583    |2:
[1927]584      #cost_label #Some_lookup_opt_assm #absurd
585      cases (not_Some_neq_None_to_False ?? absurd)
[1692]586    ]
587  ]
[2573]588  try (change with (Some ? (ASM_classify0 ?) = ? ∨ Some ? (ASM_classify0 ?) = ?) in classifier_assm;)
589  try (change with (Some ? (ASM_classify0 ?) = ?) in classifier_assm;)
[1692]590  whd in match current_instruction in classifier_assm; normalize nodelta in classifier_assm;
591  whd in match current_instruction0 in classifier_assm; normalize nodelta in classifier_assm;
592  <FETCH in classifier_assm; >classify_assm #absurd destruct(absurd) cases absurd
593  #absurd destruct(absurd)
[1684]594qed.
595
[1710]596lemma trace_compute_paid_trace_cl_return:
597  ∀code_memory' : (BitVectorTrie Byte 16).
598  ∀program_counter' : Word.
[2516]599  ∀cost_labels : BitVectorTrie costlabel 16.
[1710]600  ∀program_size' : ℕ.
601  ∀ticks : ℕ.
602  ∀instruction : instruction.
603  ∀program_counter'' : Word.
604  ∀FETCH : (〈instruction,program_counter'',ticks〉=fetch code_memory' program_counter').
605  ∀start_status : (Status code_memory').
606  ∀final_status : (Status code_memory').
607  ∀trace_ends_flag : trace_ends_with_ret.
[2575]608  ∀the_trace : trace_any_label (ASM_abstract_status … cost_labels) trace_ends_flag start_status final_status.
[2573]609  ∀program_counter_refl : (program_counter' = program_counter … start_status).
[1710]610  ∀classify_assm: ASM_classify0 instruction = cl_return.
[1906]611    ticks
[2575]612     =compute_paid_trace_any_label … the_trace.
[1929]613  #code_memory' #program_counter' #cost_labels #program_size'
[1912]614  #ticks #instruction #program_counter'' #FETCH
[1710]615  #start_status #final_status #trace_ends_flag
616  #the_trace #program_counter_refl #classify_assm
617  @(trace_any_label_inv_ind … the_trace)
618  [1:
619    #start_status' #final_status' #execute_assm #classifier_assm #costed_assm
620    #trace_ends_flag_refl #start_status_refl #final_status_refl #the_trace_refl
621    destruct @⊥
622  |2:
623    #start_status' #final_status' #execute_assm #classifier_assm #trace_ends_flag_refl
624    #start_status_refl #final_status_refl #the_trace_refl destruct
625    whd in match (trace_any_label_length … (tal_base_return …));
[1906]626    whd in match (compute_paid_trace_any_label … (tal_base_return …));
627    <FETCH %
[1710]628  |3:
629    #status_pre_fun_call #status_start_fun_call #status_final #execute_assm
630    #classifier_assm #after_return_assm #trace_label_return #costed_assm
631    #trace_ends_flag_refl #start_status_refl #final_status_refl #the_trace_refl
632    destruct @⊥
[2573]633  |4: (* XXX *)
634    cases daemon
635  |5:
[1710]636    #end_flag #status_pre_fun_call #status_start_fun_call #status_after_fun_call
637    #status_final #execute_assm #classifier_assm #after_return_assm #trace_label_return
638    #costed_assm #trace_any_label #trace_ends_flag_refl #start_status_refl
639    #final_status_refl #the_trace_refl
640    destruct @⊥
[2573]641  |6:
[1710]642    #end_flag #status_pre #status_init #status_end #execute_assm #trace_any_label
643    #classifier_assm #costed_assm #trace_ends_flag_refl #start_status_refl
644    #final_status_refl #the_trace_refl destruct @⊥
645  ]
[2573]646  try (change with (Some ? (ASM_classify0 ?) = ? ∨ Some ? (ASM_classify0 ?) = ?) in classifier_assm;)
647  try (change with (Some ? (ASM_classify0 ?) = ?) in classifier_assm;)
648  (*
[1710]649  try (change with (ASM_classify0 ? = ? ∨ ASM_classify0 ? = ?) in classifier_assm;)
650  try (change with (ASM_classify0 ? = ?) in classifier_assm;)
[2573]651  *)
[1710]652  whd in match current_instruction in classifier_assm; normalize nodelta in classifier_assm;
653  whd in match current_instruction0 in classifier_assm; normalize nodelta in classifier_assm;
654  <FETCH in classifier_assm; >classify_assm
655  #absurd try (destruct(absurd))
656  cases absurd
657  #absurd destruct(absurd)
658qed.
[1912]659
660lemma trace_any_label_length_leq_0_to_False:
661  ∀code_memory: BitVectorTrie Byte 16.
[2516]662  ∀cost_labels: BitVectorTrie costlabel 16.
[1912]663  ∀trace_ends_flag: trace_ends_with_ret.
664  ∀start_status: Status code_memory.
665  ∀final_status: Status code_memory.
[2575]666  ∀the_trace: trace_any_label (ASM_abstract_status … cost_labels) trace_ends_flag start_status final_status.
667   trace_any_label_length … the_trace ≤ 0 → False.
[1912]668  #code_memory #cost_labels #trace_ends_flag #start_status #final_status
669  #the_trace
670  cases the_trace /2/
671qed.
[1710]672     
[1650]673let rec block_cost'
[1911]674  (code_memory': BitVectorTrie Byte 16) (program_counter': Word)
[2516]675    (program_size: nat) (cost_labels: BitVectorTrie costlabel 16)
[1929]676      (first_time_around: bool)
677        on program_size:
[1658]678          Σcost_of_block: nat.
[1669]679          if (match lookup_opt … program_counter' cost_labels with [ None ⇒ true | _ ⇒ first_time_around ]) then
680            ∀start_status: Status code_memory'.
681            ∀final_status: Status code_memory'.
[1658]682            ∀trace_ends_flag.
[1669]683            ∀the_trace: trace_any_label (ASM_abstract_status … cost_labels) trace_ends_flag start_status final_status.
[1929]684              trace_any_label_length … the_trace ≤ program_size →
[1669]685              program_counter' = program_counter … start_status →
[2575]686                cost_of_block = compute_paid_trace_any_label … the_trace
[1669]687          else
688            (cost_of_block = 0) ≝
[1911]689  match program_size return λx. x = program_size → ? with
690  [ O ⇒ λbase_case. 0 (* XXX: dummy to be inserted here *)
691  | S program_size' ⇒ λstep_case.
[1663]692    let 〈instruction, program_counter'', ticks〉 as FETCH ≝ fetch code_memory' program_counter' in
[1650]693    let to_continue ≝
[1669]694      match lookup_opt … program_counter' cost_labels with
[1650]695      [ None ⇒ true
696      | Some _ ⇒ first_time_around
697      ]
698    in
[1669]699      ((if to_continue then
700       pi1 … (match instruction return λx. x = instruction → ? with
[1663]701        [ RealInstruction real_instruction ⇒ λreal_instruction_refl.
702          match real_instruction return λx. x = real_instruction →
703          Σcost_of_block: nat.
[1669]704            ∀start_status: Status code_memory'.
705            ∀final_status: Status code_memory'.
[1663]706            ∀trace_ends_flag.
[1669]707            ∀the_trace: trace_any_label (ASM_abstract_status code_memory' cost_labels) trace_ends_flag start_status final_status.
[1929]708              trace_any_label_length … the_trace ≤ S program_size' →
[1669]709              program_counter' = program_counter … start_status →
[2575]710                cost_of_block = compute_paid_trace_any_label … the_trace with
[1663]711          [ RET                    ⇒ λinstr. ticks
[1710]712          | RETI                   ⇒ λinstr. ticks
[1663]713          | JC   relative          ⇒ λinstr. ticks
714          | JNC  relative          ⇒ λinstr. ticks
715          | JB   bit_addr relative ⇒ λinstr. ticks
716          | JNB  bit_addr relative ⇒ λinstr. ticks
717          | JBC  bit_addr relative ⇒ λinstr. ticks
718          | JZ   relative          ⇒ λinstr. ticks
719          | JNZ  relative          ⇒ λinstr. ticks
720          | CJNE src_trgt relative ⇒ λinstr. ticks
721          | DJNZ src_trgt relative ⇒ λinstr. ticks
722          | _                      ⇒ λinstr.
[1929]723              ticks + block_cost' code_memory' program_counter'' program_size' cost_labels false
[1663]724          ] (refl …)
725        | ACALL addr     ⇒ λinstr.
[1929]726            ticks + block_cost' code_memory' program_counter'' program_size' cost_labels false
[1909]727        | AJMP  addr     ⇒ λinstr.
[1910]728          let jump_target ≝ compute_target_of_unconditional_jump program_counter'' instruction in
[1929]729            ticks + block_cost' code_memory' jump_target program_size' cost_labels false
[1663]730        | LCALL addr     ⇒ λinstr.
[1929]731            ticks + block_cost' code_memory' program_counter'' program_size' cost_labels false
[1910]732        | LJMP  addr     ⇒ λinstr.
733          let jump_target ≝ compute_target_of_unconditional_jump program_counter'' instruction in
[1929]734            ticks + block_cost' code_memory' jump_target program_size' cost_labels false
[1910]735        | SJMP  addr     ⇒ λinstr.
736          let jump_target ≝ compute_target_of_unconditional_jump program_counter'' instruction in
[1929]737            ticks + block_cost' code_memory' jump_target program_size' cost_labels false
[1663]738        | JMP   addr     ⇒ λinstr. (* XXX: actually a call due to use with fptrs *)
[1929]739            ticks + block_cost' code_memory' program_counter'' program_size' cost_labels false
[1663]740        | MOVC  src trgt ⇒ λinstr.
[1929]741            ticks + block_cost' code_memory' program_counter'' program_size' cost_labels false
[1669]742        ] (refl …))
[1650]743      else
[1669]744        0)
745      : Σcost_of_block: nat.
746          match (match lookup_opt … program_counter' cost_labels with [ None ⇒ true | _ ⇒ first_time_around ]) with
747          [ true ⇒
748            ∀start_status: Status code_memory'.
749            ∀final_status: Status code_memory'.
750            ∀trace_ends_flag.
751            ∀the_trace: trace_any_label (ASM_abstract_status … cost_labels) trace_ends_flag start_status final_status.
[1929]752              trace_any_label_length … the_trace ≤ S program_size' →
[1669]753              program_counter' = program_counter … start_status →
[2575]754                cost_of_block = compute_paid_trace_any_label … the_trace
[1669]755          | false ⇒
756            (cost_of_block = 0)
757          ])
[1911]758  ] (refl …).
[1910]759  [2:
760    change with (if to_continue then ? else (? = 0))
761    >p in ⊢ (match % return ? with [ _ ⇒ ? | _ ⇒ ? ]); normalize nodelta
762    @pi2
763  |1:
[1911]764    destruct
[1912]765    cases (lookup_opt ????) normalize nodelta
[1911]766    [1:
[1912]767      #start_status #final_status #trace_ends_flag #the_trace
768      #absurd
769      cases (trace_any_label_length_leq_0_to_False … absurd)
[1911]770    |2:
[1912]771      #cost cases first_time_around normalize nodelta try %
772      #start_status #final_status #trace_ends_flag #the_trace #absurd
773      cases (trace_any_label_length_leq_0_to_False … absurd)
[1911]774    ]
[1669]775  |3:
[1684]776    change with (if to_continue then ? else (0 = 0))
777    >p normalize nodelta %
[1929]778  |5,6:
[1912]779    #start_status #final_status #trace_ends_flag #the_trace #size_invariant #program_counter_refl
[1929]780    @(trace_compute_paid_trace_cl_return … program_size' … FETCH … the_trace program_counter_refl)
[1710]781    destruct %
[1929]782  |42,46,47:
[1912]783    #start_status #final_status #trace_ends_flag #the_trace #size_invariant #program_counter_refl
[1929]784    cases(block_cost' ?????) -block_cost'
785    @(trace_compute_paid_trace_cl_call … program_size' … FETCH … the_trace program_counter_refl size_invariant)
[1691]786    destruct %
[1929]787  |43,44,45: (* XXX: unconditional jumps *)
[1912]788    #start_status #final_status #trace_ends_flag #the_trace #size_invariant #program_counter_refl
[1929]789    cases (block_cost' ?????) -block_cost'
790    lapply (trace_compute_paid_trace_cl_other … program_size' … FETCH … the_trace program_counter_refl size_invariant)
791    whd in match (program_counter_after_other ??); normalize nodelta destruct
792    whd in match (is_unconditional_jump ?); normalize nodelta #assm @assm %
793  |24,25,26,27,28,29,30,31,32:
[1912]794    #start_status #final_status #trace_ends_flag #the_trace #size_invariant #program_counter_refl
[1929]795    @(trace_compute_paid_trace_cl_jump … first_time_around program_size' … FETCH … the_trace program_counter_refl)
[1691]796    destruct %
[1929]797  |*:
[1912]798    #start_status #final_status #trace_ends_flag #the_trace #size_invariant #program_counter_refl
[1929]799    cases(block_cost' ?????) -block_cost'
800    lapply (trace_compute_paid_trace_cl_other … program_size' … FETCH … the_trace program_counter_refl size_invariant)     
801    destruct #assm @assm %
[1910]802  ]
[1642]803qed.
804
[1693]805definition block_cost:
806    ∀code_memory': BitVectorTrie Byte 16.
807    ∀program_counter': Word.
[2516]808    ∀cost_labels: BitVectorTrie costlabel 16.
[1693]809      Σcost_of_block: nat.
810        ∀start_status: Status code_memory'.
811        ∀final_status: Status code_memory'.
812        ∀trace_ends_flag.
813        ∀the_trace: trace_any_label (ASM_abstract_status … cost_labels) trace_ends_flag start_status final_status.
[2573]814        ∀unrepeating_witness: tal_unrepeating … the_trace.
[1693]815          program_counter' = program_counter … start_status →
[2575]816            cost_of_block = compute_paid_trace_any_label … the_trace ≝
[1650]817  λcode_memory: BitVectorTrie Byte 16.
818  λprogram_counter: Word.
[2516]819  λcost_labels: BitVectorTrie costlabel 16. ?.
[1929]820  cases(block_cost' code_memory program_counter (2^16) cost_labels true)
[1913]821  #cost_of_block #block_cost_hyp
822  %{cost_of_block}
823  cases(lookup_opt … cost_labels) in block_cost_hyp;
824  [2: #cost_label] normalize nodelta
[1929]825  #hyp #start_status #final_status #trace_ends_flag #the_trace #unrepeating_witness
[1921]826  @hyp @tal_pc_list_length_leq_total_program_size try assumption
[2531]827qed.
828
Note: See TracBrowser for help on using the repository browser.