source: src/ASM/ASMCosts.ma @ 1697

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

important bug found

File size: 70.4 KB
Line 
1include "ASM/ASM.ma".
2include "ASM/Arithmetic.ma".
3include "ASM/Fetch.ma".
4include "ASM/Interpret.ma".
5include "common/StructuredTraces.ma".
6
7let rec fetch_program_counter_n
8  (n: nat) (code_memory: BitVectorTrie Byte 16) (program_counter: Word)
9    on n: option Word ≝
10  match n with
11  [ O ⇒ Some … program_counter
12  | S n ⇒
13    match fetch_program_counter_n n code_memory program_counter with
14    [ None ⇒ None …
15    | Some tail_pc ⇒
16      let 〈instr, program_counter, ticks〉 ≝ fetch code_memory tail_pc in
17        if ltb (nat_of_bitvector … tail_pc) (nat_of_bitvector … program_counter) then
18          Some … program_counter
19        else
20          None Word (* XXX: overflow! *)
21    ]
22  ].
23   
24definition reachable_program_counter: BitVectorTrie Byte 16 → nat → Word → Prop ≝
25  λcode_memory: BitVectorTrie Byte 16.
26  λprogram_size: nat.
27  λprogram_counter: Word.
28    (∃n: nat. Some … program_counter = fetch_program_counter_n n code_memory (zero 16)) ∧
29        nat_of_bitvector 16 program_counter < program_size.
30   
31definition well_labelling: BitVectorTrie costlabel 16 → Prop ≝
32  λcost_labels.
33    injective … (λx. lookup_opt … x cost_labels).
34   
35definition good_program: ∀code_memory: BitVectorTrie Byte 16. ∀total_program_size: nat. Prop ≝
36  λcode_memory: BitVectorTrie Byte 16.
37  λtotal_program_size: nat.
38  ∀program_counter: Word.
39  ∀good_program_counter_witness: reachable_program_counter code_memory total_program_size program_counter.
40  let 〈instruction, program_counter', ticks〉 ≝ fetch code_memory program_counter in
41    match instruction with
42    [ RealInstruction instr ⇒
43      match instr with
44      [ RET                    ⇒ True
45      | JC   relative          ⇒ True (* XXX: see below *)
46      | JNC  relative          ⇒ True (* XXX: see below *)
47      | JB   bit_addr relative ⇒ True
48      | JNB  bit_addr relative ⇒ True
49      | JBC  bit_addr relative ⇒ True
50      | JZ   relative          ⇒ True
51      | JNZ  relative          ⇒ True
52      | CJNE src_trgt relative ⇒ True
53      | DJNZ src_trgt relative ⇒ True
54      | _                      ⇒
55        nat_of_bitvector … program_counter < nat_of_bitvector … program_counter' ∧
56          nat_of_bitvector … program_counter' < total_program_size
57      ]
58    | LCALL addr         ⇒
59      match addr return λx. bool_to_Prop (is_in … [[ addr16 ]] x) → Prop with
60      [ ADDR16 addr ⇒ λaddr16: True.
61          reachable_program_counter code_memory total_program_size addr ∧
62            nat_of_bitvector … program_counter < nat_of_bitvector … program_counter' ∧
63              nat_of_bitvector … program_counter' < total_program_size
64      | _ ⇒ λother: False. ⊥
65      ] (subaddressing_modein … addr)
66    | ACALL addr         ⇒
67      match addr return λx. bool_to_Prop (is_in … [[ addr11 ]] x) → Prop with
68      [ ADDR11 addr ⇒ λaddr11: True.
69        let 〈pc_bu, pc_bl〉 ≝ split … 8 8 program_counter' in
70        let 〈thr, eig〉 ≝ split … 3 8 addr in
71        let 〈fiv, thr'〉 ≝ split … 5 3 pc_bu in
72        let new_program_counter ≝ (fiv @@ thr) @@ pc_bl in
73          reachable_program_counter code_memory total_program_size new_program_counter ∧
74            nat_of_bitvector … program_counter < nat_of_bitvector … program_counter' ∧
75              nat_of_bitvector … program_counter' < total_program_size
76      | _ ⇒ λother: False. ⊥
77      ] (subaddressing_modein … addr)
78    | AJMP  addr         ⇒
79      match addr return λx. bool_to_Prop (is_in … [[ addr11 ]] x) → Prop with
80      [ ADDR11 addr ⇒ λaddr11: True.
81        let 〈pc_bu, pc_bl〉 ≝ split … 8 8 program_counter' in
82        let 〈nu, nl〉 ≝ split … 4 4 pc_bu in
83        let bit ≝ get_index' … O ? nl in
84        let 〈relevant1, relevant2〉 ≝ split … 3 8 addr in
85        let new_addr ≝ (nu @@ (bit ::: relevant1)) @@ relevant2 in
86        let 〈carry, new_program_counter〉 ≝ half_add 16 program_counter new_addr in
87          reachable_program_counter code_memory total_program_size new_program_counter
88      | _ ⇒ λother: False. ⊥
89      ] (subaddressing_modein … addr)
90    | LJMP  addr         ⇒
91      match addr return λx. bool_to_Prop (is_in … [[ addr16 ]] x) → Prop with
92      [ ADDR16 addr ⇒ λaddr16: True.
93          reachable_program_counter code_memory total_program_size addr
94      | _ ⇒ λother: False. ⊥
95      ] (subaddressing_modein … addr)
96    | SJMP  addr     ⇒
97      match addr return λx. bool_to_Prop (is_in … [[ relative ]] x) → Prop with
98      [ RELATIVE addr ⇒ λrelative: True.
99        let 〈carry, new_program_counter〉 ≝ half_add … program_counter' (sign_extension addr) in
100          reachable_program_counter code_memory total_program_size new_program_counter
101      | _ ⇒ λother: False. ⊥
102      ] (subaddressing_modein … addr)
103    | JMP   addr     ⇒ (* XXX: JMP is used for fptrs and unconstrained *)
104      nat_of_bitvector … program_counter < nat_of_bitvector … program_counter' ∧
105        nat_of_bitvector … program_counter' < total_program_size
106    | MOVC  src trgt ⇒
107        nat_of_bitvector … program_counter < nat_of_bitvector … program_counter' ∧
108          nat_of_bitvector … program_counter' < total_program_size
109    ].
110  cases other
111qed.
112       
113lemma is_a_decidable:
114  ∀hd.
115  ∀element.
116    is_a hd element = true ∨ is_a hd element = false.
117  #hd #element //
118qed.
119
120lemma mem_decidable:
121  ∀n: nat.
122  ∀v: Vector addressing_mode_tag n.
123  ∀element: addressing_mode_tag.
124    mem … eq_a n v element = true ∨
125      mem … eq_a … v element = false.
126  #n #v #element //
127qed.
128
129lemma eq_a_elim:
130  ∀tag.
131  ∀hd.
132  ∀P: bool → Prop.
133    (tag = hd → P (true)) →
134      (tag ≠ hd → P (false)) →
135        P (eq_a tag hd).
136  #tag #hd #P
137  cases tag
138  cases hd
139  #true_hyp #false_hyp
140  try @false_hyp
141  try @true_hyp
142  try %
143  #absurd destruct(absurd)
144qed.
145 
146lemma is_a_true_to_is_in:
147  ∀n: nat.
148  ∀x: addressing_mode.
149  ∀tag: addressing_mode_tag.
150  ∀supervector: Vector addressing_mode_tag n.
151  mem addressing_mode_tag eq_a n supervector tag →
152    is_a tag x = true →
153      is_in … supervector x.
154  #n #x #tag #supervector
155  elim supervector
156  [1:
157    #absurd cases absurd
158  |2:
159    #n' #hd #tl #inductive_hypothesis
160    whd in match (mem … eq_a (S n') (hd:::tl) tag);
161    @eq_a_elim normalize nodelta
162    [1:
163      #tag_hd_eq #irrelevant
164      whd in match (is_in (S n') (hd:::tl) x);
165      <tag_hd_eq #is_a_hyp >is_a_hyp normalize nodelta
166      @I
167    |2:
168      #tag_hd_neq
169      whd in match (is_in (S n') (hd:::tl) x);
170      change with (
171        mem … eq_a n' tl tag)
172          in match (fold_right … n' ? false tl);
173      #mem_hyp #is_a_hyp
174      cases(is_a hd x)
175      [1:
176        normalize nodelta //
177      |2:
178        normalize nodelta
179        @inductive_hypothesis assumption
180      ]
181    ]
182  ]
183qed.
184
185lemma is_in_subvector_is_in_supervector:
186  ∀m, n: nat.
187  ∀subvector: Vector addressing_mode_tag m.
188  ∀supervector: Vector addressing_mode_tag n.
189  ∀element: addressing_mode.
190    subvector_with … eq_a subvector supervector →
191      is_in m subvector element → is_in n supervector element.
192  #m #n #subvector #supervector #element
193  elim subvector
194  [1:
195    #subvector_with_proof #is_in_proof
196    cases is_in_proof
197  |2:
198    #n' #hd' #tl' #inductive_hypothesis #subvector_with_proof
199    whd in match (is_in … (hd':::tl') element);
200    cases (is_a_decidable hd' element)
201    [1:
202      #is_a_true >is_a_true
203      #irrelevant
204      whd in match (subvector_with … eq_a (hd':::tl') supervector) in subvector_with_proof;
205      @(is_a_true_to_is_in … is_a_true)
206      lapply(subvector_with_proof)
207      cases(mem … eq_a n supervector hd') //
208    |2:
209      #is_a_false >is_a_false normalize nodelta
210      #assm
211      @inductive_hypothesis
212      [1:
213        generalize in match subvector_with_proof;
214        whd in match (subvector_with … eq_a (hd':::tl') supervector);
215        cases(mem_decidable n supervector hd')
216        [1:
217          #mem_true >mem_true normalize nodelta
218          #assm assumption
219        |2:
220          #mem_false >mem_false #absurd
221          cases absurd
222        ]
223      |2:
224        assumption
225      ]
226    ]
227  ]
228qed.
229
230let rec member_addressing_mode_tag
231  (n: nat) (v: Vector addressing_mode_tag n) (a: addressing_mode_tag)
232    on v: Prop ≝
233  match v with
234  [ VEmpty ⇒ False
235  | VCons n' hd tl ⇒
236      bool_to_Prop (eq_a hd a) ∨ member_addressing_mode_tag n' tl a
237  ].
238 
239let rec subaddressing_mode_elim_type
240  (T: Type[2]) (m: nat) (fixed_v: Vector addressing_mode_tag m)
241    (Q: addressing_mode → T → Prop)
242      (p_addr11:            ∀w: Word11.      is_in m fixed_v (ADDR11 w)        → T)
243      (p_addr16:            ∀w: Word.        is_in m fixed_v (ADDR16 w)        → T)
244      (p_direct:            ∀w: Byte.        is_in m fixed_v (DIRECT w)        → T)
245      (p_indirect:          ∀w: Bit.         is_in m fixed_v (INDIRECT w)      → T)
246      (p_ext_indirect:      ∀w: Bit.         is_in m fixed_v (EXT_INDIRECT w)  → T)
247      (p_acc_a:                              is_in m fixed_v ACC_A             → T)
248      (p_register:          ∀w: BitVector 3. is_in m fixed_v (REGISTER w)      → T)
249      (p_acc_b:                              is_in m fixed_v ACC_B             → T)
250      (p_dptr:                               is_in m fixed_v DPTR              → T)
251      (p_data:              ∀w: Byte.        is_in m fixed_v (DATA w)          → T)
252      (p_data16:            ∀w: Word.        is_in m fixed_v (DATA16 w)        → T)
253      (p_acc_dptr:                           is_in m fixed_v ACC_DPTR          → T)
254      (p_acc_pc:                             is_in m fixed_v ACC_PC            → T)
255      (p_ext_indirect_dptr:                  is_in m fixed_v EXT_INDIRECT_DPTR → T)
256      (p_indirect_dptr:                      is_in m fixed_v INDIRECT_DPTR     → T)
257      (p_carry:                              is_in m fixed_v CARRY             → T)
258      (p_bit_addr:          ∀w: Byte.        is_in m fixed_v (BIT_ADDR w)      → T)
259      (p_n_bit_addr:        ∀w: Byte.        is_in m fixed_v (N_BIT_ADDR w)    → T)
260      (p_relative:          ∀w: Byte.        is_in m fixed_v (RELATIVE w)      → T)
261        (n: nat) (v: Vector addressing_mode_tag n) (proof: subvector_with … eq_a v fixed_v)
262      on v: Prop ≝
263  match v return λo: nat. λv': Vector addressing_mode_tag o. o = n → v ≃ v' → ? with
264  [ VEmpty         ⇒ λm_refl. λv_refl.
265      ∀addr: addressing_mode. ∀p: is_in m fixed_v addr.
266        Q addr (
267        match addr return λx: addressing_mode. is_in … fixed_v x → T with 
268        [ ADDR11 x          ⇒ p_addr11 x
269        | ADDR16 x          ⇒ p_addr16 x
270        | DIRECT x          ⇒ p_direct x
271        | INDIRECT x        ⇒ p_indirect x
272        | EXT_INDIRECT x    ⇒ p_ext_indirect x
273        | ACC_A             ⇒ p_acc_a
274        | REGISTER x        ⇒ p_register x
275        | ACC_B             ⇒ p_acc_b
276        | DPTR              ⇒ p_dptr
277        | DATA x            ⇒ p_data x
278        | DATA16 x          ⇒ p_data16 x
279        | ACC_DPTR          ⇒ p_acc_dptr
280        | ACC_PC            ⇒ p_acc_pc
281        | EXT_INDIRECT_DPTR ⇒ p_ext_indirect_dptr
282        | INDIRECT_DPTR     ⇒ p_indirect_dptr
283        | CARRY             ⇒ p_carry
284        | BIT_ADDR x        ⇒ p_bit_addr x
285        | N_BIT_ADDR x      ⇒ p_n_bit_addr x
286        | RELATIVE x        ⇒ p_relative x
287        ] p)
288  | VCons n' hd tl ⇒ λm_refl. λv_refl.
289    let tail_call ≝ subaddressing_mode_elim_type T m fixed_v Q p_addr11
290      p_addr16 p_direct p_indirect p_ext_indirect p_acc_a
291        p_register p_acc_b p_dptr p_data p_data16 p_acc_dptr
292          p_acc_pc p_ext_indirect_dptr p_indirect_dptr p_carry
293            p_bit_addr p_n_bit_addr p_relative n' tl ?
294    in
295    match hd return λa: addressing_mode_tag. a = hd → ? with
296    [ addr11            ⇒ λhd_refl. (∀w. Q (ADDR11 w) (p_addr11 w ?)) → tail_call
297    | addr16            ⇒ λhd_refl. (∀w. Q (ADDR16 w) (p_addr16 w ?)) → tail_call
298    | direct            ⇒ λhd_refl. (∀w. Q (DIRECT w) (p_direct w ?)) → tail_call
299    | indirect          ⇒ λhd_refl. (∀w. Q (INDIRECT w) (p_indirect w ?)) → tail_call
300    | ext_indirect      ⇒ λhd_refl. (∀w. Q (EXT_INDIRECT w) (p_ext_indirect w ?)) → tail_call
301    | acc_a             ⇒ λhd_refl. (Q ACC_A (p_acc_a ?)) → tail_call
302    | registr           ⇒ λhd_refl. (∀w. Q (REGISTER w) (p_register w ?)) → tail_call
303    | acc_b             ⇒ λhd_refl. (Q ACC_A (p_acc_b ?)) → tail_call
304    | dptr              ⇒ λhd_refl. (Q DPTR (p_dptr ?)) → tail_call
305    | data              ⇒ λhd_refl. (∀w. Q (DATA w) (p_data w ?)) → tail_call
306    | data16            ⇒ λhd_refl. (∀w. Q (DATA16 w) (p_data16 w ?)) → tail_call
307    | acc_dptr          ⇒ λhd_refl. (Q ACC_DPTR (p_acc_dptr ?)) → tail_call
308    | acc_pc            ⇒ λhd_refl. (Q ACC_PC (p_acc_pc ?)) → tail_call
309    | ext_indirect_dptr ⇒ λhd_refl. (Q EXT_INDIRECT_DPTR (p_ext_indirect_dptr ?)) → tail_call
310    | indirect_dptr     ⇒ λhd_refl. (Q INDIRECT_DPTR (p_indirect_dptr ?)) → tail_call
311    | carry             ⇒ λhd_refl. (Q CARRY (p_carry ?)) → tail_call
312    | bit_addr          ⇒ λhd_refl. (∀w. Q (BIT_ADDR w) (p_bit_addr w ?)) → tail_call
313    | n_bit_addr        ⇒ λhd_refl. (∀w. Q (N_BIT_ADDR w) (p_n_bit_addr w ?)) → tail_call
314    | relative          ⇒ λhd_refl. (∀w. Q (RELATIVE w) (p_relative w ?)) → tail_call
315    ] (refl … hd)
316  ] (refl … n) (refl_jmeq … v).
317  [20:
318    generalize in match proof; destruct
319    whd in match (subvector_with … eq_a (hd:::tl) fixed_v);
320    cases (mem … eq_a m fixed_v hd) normalize nodelta
321    [1:
322      whd in match (subvector_with … eq_a tl fixed_v);
323      #assm assumption
324    |2:
325      normalize in ⊢ (% → ?);
326      #absurd cases absurd
327    ]
328  ]
329  @(is_in_subvector_is_in_supervector … proof)
330  destruct @I
331qed.
332
333lemma subaddressing_mode_elim':
334  ∀T: Type[2].
335  ∀n: nat.
336  ∀o: nat.
337  ∀Q: addressing_mode → T → Prop.
338  ∀fixed_v: Vector addressing_mode_tag (n + o).
339  ∀P1,P2,P3,P4,P5,P6,P7,P8,P9,P10,P11,P12,P13,P14,P15,P16,P17,P18,P19.
340  ∀v1: Vector addressing_mode_tag n.
341  ∀v2: Vector addressing_mode_tag o.
342  ∀fixed_v_proof: fixed_v = v1 @@ v2.
343  ∀subaddressing_mode_proof.
344    subaddressing_mode_elim_type T (n + o) fixed_v Q P1 P2 P3 P4 P5 P6 P7
345      P8 P9 P10 P11 P12 P13 P14 P15 P16 P17 P18 P19 (n + o) (v1 @@ v2) subaddressing_mode_proof.
346  #T #n #o #Q #fixed_v #P1 #P2 #P3 #P4 #P5 #P6 #P7 #P8 #P9 #P10
347  #P11 #P12 #P13 #P14 #P15 #P16 #P17 #P18 #P19 #v1 #v2 #fixed_v_proof
348  cases daemon
349qed.
350
351axiom subaddressing_mode_elim:
352  ∀T: Type[2].
353  ∀m: nat.
354  ∀n: nat.
355  ∀Q: addressing_mode → T → Prop.
356  ∀fixed_v: Vector addressing_mode_tag m.
357  ∀P1,P2,P3,P4,P5,P6,P7,P8,P9,P10,P11,P12,P13,P14,P15,P16,P17,P18,P19.
358  ∀v: Vector addressing_mode_tag n.
359  ∀proof.
360    subaddressing_mode_elim_type T m fixed_v Q P1 P2 P3 P4 P5 P6 P7
361      P8 P9 P10 P11 P12 P13 P14 P15 P16 P17 P18 P19 n v proof.
362
363definition current_instruction0 ≝
364  λcode_memory: BitVectorTrie Byte 16.
365  λprogram_counter: Word.
366    \fst (\fst (fetch … code_memory program_counter)).
367
368definition current_instruction ≝
369  λcode_memory.
370  λs: Status code_memory.
371    current_instruction0 code_memory (program_counter … s).
372
373definition ASM_classify0: instruction → status_class ≝
374  λi: instruction.
375  match i with
376   [ RealInstruction pre ⇒
377     match pre with
378      [ RET ⇒ cl_return
379      | JZ _ ⇒ cl_jump
380      | JNZ _ ⇒ cl_jump
381      | JC _ ⇒ cl_jump
382      | JNC _ ⇒ cl_jump
383      | JB _ _ ⇒ cl_jump
384      | JNB _ _ ⇒ cl_jump
385      | JBC _ _ ⇒ cl_jump
386      | CJNE _ _ ⇒ cl_jump
387      | DJNZ _ _ ⇒ cl_jump
388      | _ ⇒ cl_other
389      ]
390   | ACALL _ ⇒ cl_call
391   | LCALL _ ⇒ cl_call
392   | JMP _ ⇒ cl_call
393   | AJMP _ ⇒ cl_jump
394   | LJMP _ ⇒ cl_jump
395   | SJMP _ ⇒ cl_jump
396   | _ ⇒ cl_other
397   ].
398
399definition ASM_classify: ∀code_memory. Status code_memory → status_class ≝
400  λcode_memory.
401  λs: Status code_memory.
402    ASM_classify0 (current_instruction … s).
403
404definition current_instruction_is_labelled ≝
405  λcode_memory.
406  λcost_labels: BitVectorTrie costlabel 16.
407  λs: Status code_memory.
408  let pc ≝ program_counter … code_memory s in
409    match lookup_opt … pc cost_labels with
410    [ None ⇒ False
411    | _    ⇒ True
412    ].
413
414definition next_instruction_properly_relates_program_counters ≝
415  λcode_memory.
416  λbefore: Status code_memory.
417  λafter : Status code_memory.
418  let size ≝ current_instruction_cost code_memory before in
419  let pc_before ≝ program_counter … code_memory before in
420  let pc_after ≝ program_counter … code_memory after in
421  let sum ≝ \snd (half_add … pc_before (bitvector_of_nat … size)) in
422    sum = pc_after.
423
424definition ASM_abstract_status: ∀code_memory. BitVectorTrie costlabel 16 → abstract_status ≝
425  λcode_memory.
426  λcost_labels.
427    mk_abstract_status
428      (Status code_memory)
429      (λs,s'. (execute_1 … s) = s')
430      (λs,class. ASM_classify … s = class)
431      (current_instruction_is_labelled … cost_labels)
432      (next_instruction_properly_relates_program_counters code_memory).
433
434let rec trace_any_label_length
435  (code_memory: BitVectorTrie Byte 16) (cost_labels: BitVectorTrie costlabel 16)
436    (trace_ends_flag: trace_ends_with_ret) (start_status: Status code_memory)
437      (final_status: Status code_memory)
438      (the_trace: trace_any_label (ASM_abstract_status code_memory cost_labels) trace_ends_flag start_status final_status)
439        on the_trace: nat ≝
440  match the_trace with
441  [ tal_base_not_return the_status _ _ _ _ ⇒ 0
442  | tal_base_call pre_fun_call start_fun_call final _ _ _ call_trace _ ⇒ 0
443  | tal_base_return the_status _ _ _ ⇒ 0
444  | tal_step_call end_flag pre_fun_call start_fun_call after_fun_call final _ _ _ call_trace _ final_trace ⇒
445      let tail_length ≝ trace_any_label_length … final_trace in
446      let pc_difference ≝ nat_of_bitvector … (program_counter … after_fun_call) - nat_of_bitvector … (program_counter … pre_fun_call) in       
447        pc_difference + tail_length
448  | tal_step_default end_flag status_pre status_init status_end _ tail_trace _ _ ⇒
449      let tail_length ≝ trace_any_label_length … tail_trace in
450      let pc_difference ≝ nat_of_bitvector … (program_counter … status_init) - nat_of_bitvector … (program_counter … status_pre) in
451        pc_difference + tail_length       
452  ].
453
454let rec compute_paid_trace_any_label
455  (code_memory: BitVectorTrie Byte 16) (cost_labels: BitVectorTrie costlabel 16)
456    (trace_ends_flag: trace_ends_with_ret) (start_status: Status code_memory)
457      (final_status: Status code_memory)
458        (the_trace: trace_any_label (ASM_abstract_status code_memory cost_labels) trace_ends_flag start_status final_status)
459       on the_trace: nat ≝
460  match the_trace with
461  [ tal_base_not_return the_status _ _ _ _ ⇒ current_instruction_cost … the_status
462  | tal_base_return the_status _ _ _ ⇒ current_instruction_cost … the_status
463  | tal_base_call pre_fun_call start_fun_call final _ _ _ call_trace _ ⇒ current_instruction_cost … pre_fun_call
464  | tal_step_call end_flag pre_fun_call start_fun_call after_fun_call final
465     _ _ _ call_trace _ final_trace ⇒
466      let current_instruction_cost ≝ current_instruction_cost … pre_fun_call in
467      let final_trace_cost ≝ compute_paid_trace_any_label … cost_labels end_flag … final_trace in
468        current_instruction_cost + final_trace_cost
469  | tal_step_default end_flag status_pre status_init status_end _ tail_trace _ _ ⇒
470      let current_instruction_cost ≝ current_instruction_cost … status_pre in
471      let tail_trace_cost ≝
472       compute_paid_trace_any_label … cost_labels end_flag
473        status_init status_end tail_trace
474      in
475        current_instruction_cost + tail_trace_cost
476  ].
477
478definition compute_paid_trace_label_label ≝
479  λcode_memory: BitVectorTrie Byte 16.
480  λcost_labels: BitVectorTrie costlabel 16.
481  λtrace_ends_flag: trace_ends_with_ret.
482  λstart_status: Status code_memory.
483  λfinal_status: Status code_memory.
484  λthe_trace: trace_label_label (ASM_abstract_status … cost_labels) trace_ends_flag start_status final_status.
485  match the_trace with
486  [ tll_base ends_flag initial final given_trace labelled_proof ⇒
487      compute_paid_trace_any_label … given_trace
488  ].
489
490include alias "arithmetics/nat.ma".
491include alias "basics/logic.ma".
492
493lemma plus_right_monotone:
494  ∀m, n, o: nat.
495    m = n → m + o = n + o.
496  #m #n #o #refl >refl %
497qed.
498
499lemma minus_plus_cancel:
500  ∀m, n : nat.
501  ∀proof: n ≤ m.
502    (m - n) + n = m.
503  #m #n #proof /2 by plus_minus/
504qed.
505
506(* XXX: indexing bug *)
507example fetch_twice_fetch_execute_1:
508  ∀code_memory: BitVectorTrie Byte 16.
509  ∀start_status: Status code_memory.
510    \snd (\fst (fetch code_memory (program_counter … start_status))) =
511      program_counter … (execute_1 … start_status).
512  cases daemon
513qed.
514
515lemma reachable_program_counter_to_0_lt_total_program_size:
516  ∀code_memory: BitVectorTrie Byte 16.
517  ∀program_counter: Word.
518  ∀total_program_size: nat.
519    reachable_program_counter code_memory total_program_size program_counter →
520      0 < total_program_size.
521  #code_memory #program_counter #total_program_size
522  whd in match reachable_program_counter; normalize nodelta * * #some_n
523  #_ cases (nat_of_bitvector 16 program_counter)
524  [1:
525    #assm assumption
526  |2:
527    #new_pc @ltn_to_ltO
528  ]
529qed.
530
531lemma trace_compute_paid_trace_cl_other:
532  ∀code_memory' : (BitVectorTrie Byte 16).
533  ∀program_counter' : Word.
534  ∀total_program_size : ℕ.
535  ∀cost_labels : (BitVectorTrie costlabel 16).
536  ∀reachable_program_counter_witness : (reachable_program_counter code_memory' total_program_size program_counter').
537  ∀good_program_witness : (good_program code_memory' total_program_size).
538  ∀program_size' : ℕ.
539  ∀recursive_case : (total_program_size≤S program_size'+nat_of_bitvector 16 program_counter').
540  ∀ticks : ℕ.
541  ∀instruction : instruction.
542  ∀program_counter'' : Word.
543  ∀FETCH : (〈instruction,program_counter'',ticks〉=fetch code_memory' program_counter').
544  ∀start_status : (Status code_memory').
545  ∀final_status : (Status code_memory').
546  ∀trace_ends_flag : trace_ends_with_ret.
547  ∀the_trace : (trace_any_label (ASM_abstract_status code_memory' cost_labels) trace_ends_flag start_status final_status).
548  ∀program_counter_refl : (program_counter' = program_counter (BitVectorTrie Byte 16) code_memory' start_status).
549  ∀classify_assm: ASM_classify0 instruction = cl_other.
550  ∀pi1 : ℕ.
551   (if match lookup_opt costlabel 16 program_counter'' cost_labels with 
552         [None ⇒ true
553         |Some _ ⇒ false
554         ] 
555    then
556      ∀start_status0:Status code_memory'.
557      ∀final_status0:Status code_memory'.
558      ∀trace_ends_flag0:trace_ends_with_ret.
559      ∀the_trace0:trace_any_label (ASM_abstract_status code_memory' cost_labels) trace_ends_flag0 start_status0 final_status0.
560        program_counter'' = program_counter (BitVectorTrie Byte 16) code_memory' start_status0 →
561        (∃n:ℕ
562                    .trace_any_label_length code_memory' cost_labels
563                     trace_ends_flag0 start_status0 final_status0 the_trace0
564                     +S n
565                     =total_program_size)
566                   ∧pi1
567                    =compute_paid_trace_any_label code_memory' cost_labels
568                     trace_ends_flag0 start_status0 final_status0 the_trace0
569    else (pi1=O) )
570   →(∃n:ℕ
571     .trace_any_label_length code_memory' cost_labels trace_ends_flag
572      start_status final_status the_trace
573      +S n
574      =total_program_size)
575    ∧ticks+pi1
576     =compute_paid_trace_any_label code_memory' cost_labels trace_ends_flag
577      start_status final_status the_trace.
578  #code_memory' #program_counter' #total_program_size #cost_labels
579  #reachable_program_counter_witness #good_program_witness
580  #program_size' #recursive_case #ticks #instruction #program_counter'' #FETCH
581  #start_status #final_status
582  #trace_ends_flag #the_trace #program_counter_refl #classify_assm #recursive_block_cost
583  #recursive_assm
584  @(trace_any_label_inv_ind … the_trace)
585    [5:
586      #end_flag #status_pre #status_init #status_end #execute_assm #trace_any_label
587      #classifier_assm #costed_assm #trace_ends_refl #start_status_refl #final_status_refl
588      #the_trace_refl
589      destruct
590      whd in match (trace_any_label_length … (tal_step_default …));
591      whd in match (compute_paid_trace_any_label … (tal_step_default …));
592      whd in costed_assm:(?%);
593      generalize in match costed_assm;
594      generalize in match (refl … (lookup_opt … (program_counter … (execute_1 … status_pre)) cost_labels));
595      generalize in match (lookup_opt … (program_counter … (execute_1 … status_pre)) cost_labels)
596        in ⊢ (??%? → ?(match % with [ _ ⇒ ? | _ ⇒ ? ]) → ?);
597      #lookup_assm cases lookup_assm
598      [1:
599        #None_lookup_opt_assm normalize nodelta #ignore
600        generalize in match recursive_assm;
601        cut(program_counter'' = (program_counter (BitVectorTrie Byte 16) code_memory' (execute_1 code_memory' status_pre)))
602        [1:
603          <fetch_twice_fetch_execute_1 <FETCH %
604        |2:
605          #program_counter_assm >program_counter_assm <None_lookup_opt_assm
606          normalize nodelta #new_recursive_assm
607          cases(new_recursive_assm (execute_1 code_memory' status_pre) status_end
608            end_flag trace_any_label ?) [2: % ]
609          #exists_assm #recursive_block_cost_assm %
610          [1:
611            cases exists_assm #exists_n #total_program_size_refl
612            <total_program_size_refl
613            %{(exists_n - current_instruction_cost … status_pre)}
614            (* XXX: Christ knows what's going on with the rewrite tactic here? *)
615            cases daemon
616          |2:
617            >recursive_block_cost_assm
618            whd in match (current_instruction_cost … status_pre);
619            cut(ticks = \snd (fetch code_memory'
620               (program_counter (BitVectorTrie Byte 16) code_memory' status_pre)))
621            [1:
622              <FETCH %
623            |2:
624              #ticks_refl_assm >ticks_refl_assm %
625            ]
626          ]
627        ]
628      |2:
629        #costlabel #Some_lookup_opt_assm <Some_lookup_opt_assm normalize nodelta
630        #absurd cases absurd #absurd cases(absurd I)
631      ]
632    |1:
633      #status_start #status_final #execute_assm #classifier_assm #costed_assm
634      #trace_ends_flag_refl #start_status_refl #final_status_refl #the_trace_refl
635      destruct
636      whd in match (trace_any_label_length … (tal_base_not_return …));
637      whd in match (compute_paid_trace_any_label … (tal_base_not_return …));
638      whd in costed_assm;
639      generalize in match costed_assm;
640      generalize in match (refl … (lookup_opt … (program_counter … (execute_1 … status_start)) cost_labels));
641      generalize in match (lookup_opt … (program_counter … (execute_1 code_memory' status_start)) cost_labels)
642        in ⊢ (??%? → (match % with [ _ ⇒ ? | _ ⇒ ? ]) → ?);
643      #lookup_assm cases lookup_assm
644      [1:
645        #None_lookup_opt_assm normalize nodelta >None_lookup_opt_assm
646        #absurd cases absurd
647      |2:
648        #costlabel #Some_lookup_opt_assm normalize nodelta #ignore
649        generalize in match recursive_assm;
650        cut(program_counter'' = (program_counter (BitVectorTrie Byte 16) code_memory' (execute_1 … status_start)))
651        [1:
652          <fetch_twice_fetch_execute_1 <FETCH %
653        |2:
654          #program_counter_assm >program_counter_assm <Some_lookup_opt_assm
655          normalize nodelta #new_recursive_assm >new_recursive_assm %
656          [1:
657            %{(pred total_program_size)} whd in ⊢ (??%?);
658            @S_pred
659            @(reachable_program_counter_to_0_lt_total_program_size … reachable_program_counter_witness)
660          |2:
661            cut(ticks = \snd (fetch code_memory'
662               (program_counter (BitVectorTrie Byte 16) code_memory' status_start)))
663            [1:
664              <FETCH %
665            |2:
666              #ticks_refl_assm >ticks_refl_assm
667              <plus_n_O %
668            ]
669          ]
670        ]
671      ]
672    |2:
673      #start_status' #final_status' #execute_assm #classifier_assm #trace_ends_assm
674      #start_status_refl #final_status_refl #the_trace_assm destruct @⊥
675    |3:
676      #status_pre_fun_call #status_start_fun_call #status_final #execute_assm
677      #classifier_assm #after_return_assm #trace_label_return #costed_assm
678      #ends_flag_refl #start_status_refl #final_status_refl #the_trace_refl
679      destruct @⊥
680    |4:
681      #end_flag #status_pre_fun_call #status_start_fun_call #status_after_fun_call
682      #status_final #execute_assm #classifier_assm #after_return_assm #trace_label_return
683      #costed_assm #trace_any_label #trace_ends_flag_refl #start_status_refl
684      #final_status_refl #the_trace_refl destruct @⊥
685    ]
686  change with (ASM_classify0 ? = ?) in classifier_assm;
687  whd in match current_instruction in classifier_assm; normalize nodelta in classifier_assm;
688  whd in match current_instruction0 in classifier_assm; normalize nodelta in classifier_assm;
689  <FETCH in classifier_assm; >classify_assm #absurd destruct(absurd)
690qed.
691
692lemma trace_compute_paid_trace_cl_jump:
693  ∀code_memory': BitVectorTrie Byte 16.
694  ∀program_counter': Word.
695  ∀total_program_size: ℕ.
696  ∀cost_labels: BitVectorTrie costlabel 16.
697  ∀reachable_program_counter_witness: reachable_program_counter code_memory' total_program_size program_counter'.
698  ∀good_program_witness: good_program code_memory' total_program_size.
699  ∀first_time_around: bool.
700  ∀program_size': ℕ.
701  ∀recursive_case: total_program_size ≤ S program_size'+nat_of_bitvector 16 program_counter'.
702  ∀ticks: ℕ.
703  ∀instruction: instruction.
704  ∀program_counter'': Word.
705  ∀FETCH: 〈instruction,program_counter'',ticks〉 = fetch code_memory' program_counter'.
706  ∀start_status: (Status code_memory').
707  ∀final_status: (Status code_memory').
708  ∀trace_ends_flag: trace_ends_with_ret.
709  ∀the_trace: (trace_any_label (ASM_abstract_status code_memory' cost_labels) trace_ends_flag start_status final_status).
710  ∀program_counter_refl: (program_counter' = program_counter (BitVectorTrie Byte 16) code_memory' start_status).
711  ∀classify_assm: ASM_classify0 instruction = cl_jump.
712   (∃n:ℕ
713     .trace_any_label_length code_memory' cost_labels trace_ends_flag
714      start_status final_status the_trace
715      +S n
716      =total_program_size)
717    ∧ticks
718     =compute_paid_trace_any_label code_memory' cost_labels trace_ends_flag
719      start_status final_status the_trace.
720  #code_memory' #program_counter' #total_program_size #cost_labels
721  #reachable_program_counter_witness #good_program_witness #first_time_around
722  #program_size' #recursive_case #ticks #instruction #program_counter'' #FETCH
723  #start_status #final_status
724  #trace_ends_flag #the_trace #program_counter_refl #classify_assm
725  @(trace_any_label_inv_ind … the_trace)
726  [5:
727    #end_flag #status_pre #status_init #status_end #execute_assm #trace_any_label
728    #classifier_assm #costed_assm #trace_ends_refl #start_status_refl #final_status_refl
729    #the_trace_refl destruct @⊥
730  |1:
731    #status_start #status_final #execute_assm #classifier_assm #costed_assm
732    #trace_ends_flag_refl #start_status_refl #final_status_refl #the_trace_refl
733    destruct
734    whd in match (trace_any_label_length … (tal_base_not_return …));
735    whd in match (compute_paid_trace_any_label … (tal_base_not_return …)); %
736    [1:
737      %{(pred total_program_size)} whd in ⊢ (??%?);
738      @S_pred
739      @(reachable_program_counter_to_0_lt_total_program_size … reachable_program_counter_witness)
740    |2:
741      <FETCH %
742    ]
743  |2:
744    #start_status' #final_status' #execute_assm #classifier_assm #trace_ends_assm
745    #start_status_refl #final_status_refl #the_trace_assm destruct @⊥
746  |3:
747    #status_pre_fun_call #status_start_fun_call #status_final #execute_assm
748    #classifier_assm #after_return_assm #trace_label_return #costed_assm
749    #ends_flag_refl #start_status_refl #final_status_refl #the_trace_refl
750    destruct @⊥
751  |4:
752    #end_flag #status_pre_fun_call #status_start_fun_call #status_after_fun_call
753    #status_final #execute_assm #classifier_assm #after_return_assm #trace_label_return
754    #costed_assm #trace_any_label #trace_ends_flag_refl #start_status_refl
755    #final_status_refl #the_trace_refl destruct @⊥
756  ]
757  change with (ASM_classify0 ? = ?) in classifier_assm;
758  whd in match current_instruction in classifier_assm; normalize nodelta in classifier_assm;
759  whd in match current_instruction0 in classifier_assm; normalize nodelta in classifier_assm;
760  <FETCH in classifier_assm; >classify_assm #absurd destruct(absurd)
761qed.
762
763lemma trace_compute_paid_trace_cl_call:
764  ∀code_memory' : (BitVectorTrie Byte 16).
765  ∀program_counter' : Word.
766  ∀total_program_size : ℕ.
767  ∀cost_labels : (BitVectorTrie costlabel 16).
768  ∀reachable_program_counter_witness : (reachable_program_counter code_memory' total_program_size program_counter').
769  ∀good_program_witness : (good_program code_memory' total_program_size).
770  ∀program_size' : ℕ.
771  ∀recursive_case : (total_program_size≤S program_size'+nat_of_bitvector 16 program_counter').
772  ∀ticks : ℕ.
773  ∀instruction : instruction.
774  ∀program_counter'' : Word.
775  ∀FETCH : (〈instruction,program_counter'',ticks〉=fetch code_memory' program_counter').
776  ∀start_status : (Status code_memory').
777  ∀final_status : (Status code_memory').
778  ∀trace_ends_flag : trace_ends_with_ret.
779  ∀the_trace : (trace_any_label (ASM_abstract_status code_memory' cost_labels) trace_ends_flag start_status final_status).
780  ∀program_counter_refl : (program_counter' = program_counter (BitVectorTrie Byte 16) code_memory' start_status).
781  ∀classify_assm: ASM_classify0 instruction = cl_call.
782  (∀pi1:ℕ
783  .if match lookup_opt costlabel 16 program_counter'' cost_labels with 
784      [None ⇒ true | Some _ ⇒ false] 
785   then (∀start_status0:Status code_memory'
786             .∀final_status0:Status code_memory'
787              .∀trace_ends_flag0:trace_ends_with_ret
788               .∀the_trace0:trace_any_label
789                                        (ASM_abstract_status code_memory' cost_labels)
790                                        trace_ends_flag0 start_status0 final_status0
791                .program_counter''
792                 =program_counter (BitVectorTrie Byte 16) code_memory' start_status0
793                 →(∃n:ℕ
794                   .trace_any_label_length code_memory' cost_labels trace_ends_flag0
795                    start_status0 final_status0 the_trace0
796                    +S n
797                    =total_program_size)
798                  ∧pi1
799                   =compute_paid_trace_any_label code_memory' cost_labels
800                    trace_ends_flag0 start_status0 final_status0 the_trace0) 
801   else (pi1=O) 
802   →(∃n:ℕ
803     .trace_any_label_length code_memory' cost_labels trace_ends_flag
804      start_status final_status the_trace
805      +S n
806      =total_program_size)
807    ∧ticks+pi1
808     =compute_paid_trace_any_label code_memory' cost_labels trace_ends_flag
809      start_status final_status the_trace).
810  #code_memory' #program_counter' #total_program_size #cost_labels
811  #reachable_program_counter_witness #good_program_witness #program_size'
812  #recursive_case #ticks #instruction #program_counter'' #FETCH
813  #start_status #final_status #trace_ends_flag
814  #the_trace #program_counter_refl #classify_assm #recursive_block_cost #recursive_assm
815  @(trace_any_label_inv_ind … the_trace)
816  [5:
817    #end_flag #status_pre #status_init #status_end #execute_assm #trace_any_label
818    #classifier_assm #costed_assm #trace_ends_refl #start_status_refl #final_status_refl
819    #the_trace_refl destruct @⊥
820  |1:
821    #status_start #status_final #execute_assm #classifier_assm #costed_assm
822    #trace_ends_flag_refl #start_status_refl #final_status_refl #the_trace_refl
823    destruct @⊥
824  |2:
825    #start_status' #final_status' #execute_assm #classifier_assm #trace_ends_assm
826    #start_status_refl #final_status_refl #the_trace_assm destruct @⊥
827  |3:
828    #status_pre_fun_call #status_start_fun_call #status_final #execute_assm
829    #classifier_assm #after_return_assm #trace_label_return #costed_assm
830    #ends_flag_refl #start_status_refl #final_status_refl #the_trace_refl
831    destruct
832    whd in match (trace_any_label_length … (tal_base_call …));
833    whd in match (compute_paid_trace_any_label … (tal_base_call …));
834    whd in costed_assm;
835    generalize in match costed_assm;
836    generalize in match (refl … (lookup_opt … (program_counter … status_final) cost_labels));
837    generalize in match (lookup_opt … (program_counter … status_final) cost_labels)
838      in ⊢ (??%? → (match % with [ _ ⇒ ? | _ ⇒ ? ]) → ?);
839    #lookup_assm cases lookup_assm
840    [1:
841      #None_lookup_opt normalize nodelta #absurd cases absurd
842    |2:
843      #costlabel #Some_lookup_opt normalize nodelta #ignore
844      generalize in match recursive_assm;
845      cut(program_counter'' = (program_counter (BitVectorTrie Byte 16) code_memory' status_final))
846      [1:
847        whd in after_return_assm; <after_return_assm; (* XXX: here *)
848        cases daemon
849      |2:
850        #program_counter_assm >program_counter_assm <Some_lookup_opt
851        normalize nodelta #new_recursive_assm >new_recursive_assm %
852        [1:
853          %{(pred total_program_size)} whd in ⊢ (??%?);
854          @S_pred
855          @(reachable_program_counter_to_0_lt_total_program_size … reachable_program_counter_witness)
856        |2:
857          cut(ticks = \snd (fetch code_memory' (program_counter (BitVectorTrie Byte 16) code_memory' status_pre_fun_call)))
858          [1:
859            <FETCH %
860          |2:
861            #ticks_refl_assm >ticks_refl_assm
862            <plus_n_O %
863          ]
864        ]
865      ]
866    ]
867  |4:
868    #end_flag #status_pre_fun_call #status_start_fun_call #status_after_fun_call
869    #status_final #execute_assm #classifier_assm #after_return_assm #trace_label_return
870    #costed_assm #trace_any_label #trace_ends_flag_refl #start_status_refl
871    #final_status_refl #the_trace_refl destruct
872    whd in match (trace_any_label_length … (tal_step_call …));
873    whd in match (compute_paid_trace_any_label … (tal_step_call …));
874    whd in costed_assm:(?%);
875    generalize in match costed_assm;
876    generalize in match (refl … (lookup_opt … (program_counter … status_after_fun_call) cost_labels));
877    generalize in match (lookup_opt … (program_counter … status_after_fun_call) cost_labels)
878      in ⊢ (??%? → ?(match % with [ _ ⇒ ? | _ ⇒ ? ]) → ?);
879    #lookup_assm cases lookup_assm
880    [1:
881      #None_lookup_opt_assm normalize nodelta #ignore
882      generalize in match recursive_assm;
883      cut(program_counter'' = program_counter … status_after_fun_call)
884      [1:
885        cases daemon
886      |2:
887        #program_counter_refl >program_counter_refl <None_lookup_opt_assm
888        normalize nodelta #new_recursive_assm %
889        cases (new_recursive_assm … trace_any_label ?)
890        [1,3:
891          #exists_assm #recursive_block_cost_assm
892          [2:
893            >recursive_block_cost_assm
894            @plus_right_monotone
895            whd in ⊢ (???%); <FETCH %
896          |1:
897            (*
898            cases exists_assm #recursive_n #new_exists_assm
899            <new_exists_assm
900           
901            %
902            [1:
903              @(recursive_n - current_instruction_cost … status_pre_fun_call)
904            |2:
905             
906              cut(current_instruction_cost … status_pre_fun_call =
907                (nat_of_bitvector 16
908  (program_counter (BitVectorTrie Byte 16) code_memory' status_after_fun_call)
909  -nat_of_bitvector 16
910   (program_counter (BitVectorTrie Byte 16) code_memory' status_pre_fun_call)))
911              [1:
912              |2:
913                #eq_assm <eq_assm -eq_assm -new_exists_assm -recursive_block_cost_assm
914                <plus_n_Sm <plus_n_Sm @eq_f
915                >associative_plus in ⊢ (??%?); >commutative_plus in ⊢ (??%?);
916                >associative_plus @eq_f
917                <plus_minus_m_m [1: % ]
918              ]
919            ] *)
920            cases daemon
921          ]
922        |2,4:
923          %
924        ]
925      ]
926    |2:
927      #cost_label #Some_lookup_opt_assm normalize nodelta #absurd
928      cases absurd #absurd cases (absurd I)
929    ]
930  ]
931  try (change with (ASM_classify0 ? = ? ∨ ASM_classify0 ? = ?) in classifier_assm;)
932  try (change with (ASM_classify0 ? = ?) in classifier_assm;)
933  whd in match current_instruction in classifier_assm; normalize nodelta in classifier_assm;
934  whd in match current_instruction0 in classifier_assm; normalize nodelta in classifier_assm;
935  <FETCH in classifier_assm; >classify_assm #absurd destruct(absurd) cases absurd
936  #absurd destruct(absurd)
937qed.
938
939let rec block_cost'
940  (code_memory': BitVectorTrie Byte 16) (program_counter': Word)
941    (program_size: nat) (total_program_size: nat) (cost_labels: BitVectorTrie costlabel 16)
942      (reachable_program_counter_witness: reachable_program_counter code_memory' total_program_size program_counter')
943        (good_program_witness: good_program code_memory' total_program_size) (first_time_around: bool)
944          on program_size:
945          total_program_size ≤ program_size + nat_of_bitvector … program_counter' →
946          Σcost_of_block: nat.
947          if (match lookup_opt … program_counter' cost_labels with [ None ⇒ true | _ ⇒ first_time_around ]) then
948            ∀start_status: Status code_memory'.
949            ∀final_status: Status code_memory'.
950            ∀trace_ends_flag.
951            ∀the_trace: trace_any_label (ASM_abstract_status … cost_labels) trace_ends_flag start_status final_status.
952              program_counter' = program_counter … start_status →
953                (∃n: nat. trace_any_label_length … the_trace + (S n) = total_program_size) ∧
954                  cost_of_block = compute_paid_trace_any_label code_memory' cost_labels trace_ends_flag start_status final_status the_trace
955          else
956            (cost_of_block = 0) ≝
957  match program_size return λprogram_size: nat. total_program_size ≤ program_size + nat_of_bitvector … program_counter' → ? with
958  [ O ⇒ λbase_case. ⊥
959  | S program_size' ⇒ λrecursive_case.
960    let 〈instruction, program_counter'', ticks〉 as FETCH ≝ fetch code_memory' program_counter' in
961    let to_continue ≝
962      match lookup_opt … program_counter' cost_labels with
963      [ None ⇒ true
964      | Some _ ⇒ first_time_around
965      ]
966    in
967      ((if to_continue then
968       pi1 … (match instruction return λx. x = instruction → ? with
969        [ RealInstruction real_instruction ⇒ λreal_instruction_refl.
970          match real_instruction return λx. x = real_instruction →
971          Σcost_of_block: nat.
972            ∀start_status: Status code_memory'.
973            ∀final_status: Status code_memory'.
974            ∀trace_ends_flag.
975            ∀the_trace: trace_any_label (ASM_abstract_status code_memory' cost_labels) trace_ends_flag start_status final_status.
976              program_counter' = program_counter … start_status →
977                (∃n: nat. trace_any_label_length … the_trace + (S n) = total_program_size) ∧
978                  cost_of_block = compute_paid_trace_any_label code_memory' cost_labels trace_ends_flag start_status final_status the_trace with
979          [ RET                    ⇒ λinstr. ticks
980          | JC   relative          ⇒ λinstr. ticks
981          | JNC  relative          ⇒ λinstr. ticks
982          | JB   bit_addr relative ⇒ λinstr. ticks
983          | JNB  bit_addr relative ⇒ λinstr. ticks
984          | JBC  bit_addr relative ⇒ λinstr. ticks
985          | JZ   relative          ⇒ λinstr. ticks
986          | JNZ  relative          ⇒ λinstr. ticks
987          | CJNE src_trgt relative ⇒ λinstr. ticks
988          | DJNZ src_trgt relative ⇒ λinstr. ticks
989          | _                      ⇒ λinstr.
990         
991              ticks + block_cost' code_memory' program_counter'' program_size' total_program_size cost_labels ? good_program_witness false ?
992          ] (refl …)
993        | ACALL addr     ⇒ λinstr.
994            ticks + block_cost' code_memory' program_counter'' program_size' total_program_size cost_labels ? good_program_witness false ?
995        | AJMP  addr     ⇒ λinstr. ticks
996        | LCALL addr     ⇒ λinstr.
997            ticks + block_cost' code_memory' program_counter'' program_size' total_program_size cost_labels ? good_program_witness false ?
998        | LJMP  addr     ⇒ λinstr. ticks
999        | SJMP  addr     ⇒ λinstr. ticks
1000        | JMP   addr     ⇒ λinstr. (* XXX: actually a call due to use with fptrs *)
1001            ticks + block_cost' code_memory' program_counter'' program_size' total_program_size cost_labels ? good_program_witness false ?
1002        | MOVC  src trgt ⇒ λinstr.
1003            ticks + block_cost' code_memory' program_counter'' program_size' total_program_size cost_labels ? good_program_witness false ?
1004        ] (refl …))
1005      else
1006        0)
1007      : Σcost_of_block: nat.
1008          match (match lookup_opt … program_counter' cost_labels with [ None ⇒ true | _ ⇒ first_time_around ]) with
1009          [ true ⇒
1010            ∀start_status: Status code_memory'.
1011            ∀final_status: Status code_memory'.
1012            ∀trace_ends_flag.
1013            ∀the_trace: trace_any_label (ASM_abstract_status … cost_labels) trace_ends_flag start_status final_status.
1014              program_counter' = program_counter … start_status →
1015                (∃n: nat. trace_any_label_length … the_trace + (S n) = total_program_size) ∧
1016                  cost_of_block = compute_paid_trace_any_label code_memory' cost_labels trace_ends_flag start_status final_status the_trace
1017          | false ⇒
1018            (cost_of_block = 0)
1019          ])
1020  ].
1021  cases daemon (*
1022  [1:
1023    cases reachable_program_counter_witness #_ #hyp
1024    @(absurd (total_program_size < total_program_size) … (not_le_Sn_n …))
1025    @(le_to_lt_to_lt … base_case hyp)
1026  |2:
1027    change with (if to_continue then ? else (? = 0))
1028    >p in ⊢ (match % return ? with [ _ ⇒ ? | _ ⇒ ? ]); normalize nodelta
1029    @pi2
1030  |3:
1031    change with (if to_continue then ? else (0 = 0))
1032    >p normalize nodelta %
1033  |98,104,107:
1034    #start_status #final_status #trace_ends_flag #the_trace #program_counter_refl
1035    cases(block_cost' ?????????) -block_cost'
1036    @(trace_compute_paid_trace_cl_call … reachable_program_counter_witness good_program_witness … recursive_case … FETCH … program_counter_refl)
1037    destruct %
1038  |4,7,11,14,17,20,23,26,29,32,35,38,41,44,47,50,53,56,59,71,74,77,80,83,86,89,
1039   92,95:
1040    #start_status #final_status #trace_ends_flag #the_trace #program_counter_refl
1041    cases(block_cost' ?????????) -block_cost'
1042    @(trace_compute_paid_trace_cl_other … reachable_program_counter_witness good_program_witness … recursive_case … FETCH … the_trace program_counter_refl)
1043    destruct %
1044  |62,63,64,65,66,67,68,69,101,102,103:
1045    #start_status #final_status #trace_ends_flag #the_trace #program_counter_refl
1046    @(trace_compute_paid_trace_cl_jump … reachable_program_counter_witness good_program_witness … recursive_case … FETCH … the_trace program_counter_refl)
1047    destruct %
1048  |96,99:
1049    cases reachable_program_counter_witness * #n #fetch_n_hyp #lt_hyp
1050    lapply(good_program_witness program_counter' reachable_program_counter_witness)
1051    <FETCH normalize nodelta <instr normalize nodelta *
1052    #program_counter_lt' #program_counter_lt_tps' %
1053    [1,3:
1054      %{(S n)} whd in ⊢ (???%); <fetch_n_hyp normalize nodelta
1055      <FETCH normalize nodelta whd in match ltb; normalize nodelta
1056      >(le_to_leb_true … program_counter_lt') %
1057    |2,4:
1058      assumption
1059    ]
1060  |105:
1061    cases reachable_program_counter_witness * #n #fetch_n_hyp #lt_hyp
1062    lapply(good_program_witness program_counter' reachable_program_counter_witness)
1063    <FETCH normalize nodelta <instr normalize nodelta
1064    @(subaddressing_mode_elim … [[addr16]] … [[addr16]]) [1: // ] #new_addr
1065    * * * * #n'
1066    #_ #_ #program_counter_lt' #program_counter_lt_tps'
1067    %
1068    [1:
1069      %{(S n)} whd in ⊢ (???%); <fetch_n_hyp normalize nodelta
1070      <FETCH normalize nodelta whd in match ltb; normalize nodelta
1071      >(le_to_leb_true … program_counter_lt') %
1072    |2:
1073      assumption
1074    ]
1075  |106:
1076    cases reachable_program_counter_witness * #n #fetch_n_hyp #lt_hyp
1077    lapply(good_program_witness program_counter' reachable_program_counter_witness)
1078    <FETCH normalize nodelta <instr normalize nodelta
1079    @(subaddressing_mode_elim … [[addr16]] … [[addr16]]) [1: // ] #new_addr
1080    * * * * #n'
1081    #_ #_ #program_counter_lt' #program_counter_lt_tps'
1082    @(transitive_le
1083      total_program_size
1084      ((S program_size') + nat_of_bitvector … program_counter')
1085      (program_size' + nat_of_bitvector … program_counter'') recursive_case)
1086    normalize in match (S program_size' + nat_of_bitvector … program_counter');
1087    >plus_n_Sm
1088    @monotonic_le_plus_r
1089    change with (
1090      nat_of_bitvector … program_counter' <
1091        nat_of_bitvector … program_counter'')
1092    assumption
1093  |108:
1094    cases reachable_program_counter_witness * #n #fetch_n_hyp #lt_hyp
1095    lapply(good_program_witness program_counter' reachable_program_counter_witness)
1096    <FETCH normalize nodelta <instr normalize nodelta
1097    @(subaddressing_mode_elim … [[addr11]] … [[addr11]]) [1: // ] #new_addr
1098    cases (split … 8 8 program_counter'') #pc_bu #pc_bl normalize nodelta
1099    cases (split … 3 8 new_addr) #thr #eig normalize nodelta
1100    cases (split … 5 3 pc_bu) #fiv #thr' normalize nodelta * * * * #n'
1101    #_ #_ #program_counter_lt' #program_counter_lt_tps'
1102    %
1103    [1:
1104      %{(S n)} whd in ⊢ (???%); <fetch_n_hyp normalize nodelta
1105      <FETCH normalize nodelta whd in match ltb; normalize nodelta
1106      >(le_to_leb_true … program_counter_lt') %
1107    |2:
1108      assumption
1109    ]
1110  |109:
1111    cases reachable_program_counter_witness * #n #fetch_n_hyp #lt_hyp
1112    lapply(good_program_witness program_counter' reachable_program_counter_witness)
1113    <FETCH normalize nodelta <instr normalize nodelta
1114    @(subaddressing_mode_elim … [[addr11]] … [[addr11]]) [1: // ] #new_addr
1115    cases (split … 8 8 program_counter'') #pc_bu #pc_bl normalize nodelta
1116    cases (split … 3 8 new_addr) #thr #eig normalize nodelta
1117    cases (split … 5 3 pc_bu) #fiv #thr' normalize nodelta * * * * #n'
1118    #_ #_ #program_counter_lt' #program_counter_lt_tps'
1119    @(transitive_le
1120      total_program_size
1121      ((S program_size') + nat_of_bitvector … program_counter')
1122      (program_size' + nat_of_bitvector … program_counter'') recursive_case)
1123    normalize in match (S program_size' + nat_of_bitvector … program_counter');
1124    >plus_n_Sm
1125    @monotonic_le_plus_r
1126    change with (
1127      nat_of_bitvector … program_counter' <
1128        nat_of_bitvector … program_counter'')
1129    assumption
1130  |5,8,12,14,15,18,20,21,24,27,30,33,36,39,42,45,48,51,54,57,60,72,75,
1131   78,81,84,87,90,93:
1132    cases reachable_program_counter_witness * #n #fetch_n_hyp #lt_hyp
1133    lapply(good_program_witness program_counter' reachable_program_counter_witness)
1134    <FETCH normalize nodelta <real_instruction_refl <instr normalize nodelta *
1135    #program_counter_lt' #program_counter_lt_tps' %
1136    try assumption
1137    %{(S n)} whd in ⊢ (???%); <fetch_n_hyp normalize nodelta
1138    <FETCH normalize nodelta whd in match ltb; normalize nodelta
1139    >(le_to_leb_true … program_counter_lt') %
1140  |6,9,13,16,19,22,25,28,31,34,37,40,43,46,49,52,55,58,61,73,76,79,82,85,88,91,
1141   94:
1142    cases reachable_program_counter_witness * #n #fetch_n_hyp #lt_hyp
1143    lapply(good_program_witness program_counter' reachable_program_counter_witness)
1144    <FETCH normalize nodelta
1145    <real_instruction_refl <instr normalize nodelta *
1146    #pc_pc_lt_hyp' #pc_tps_lt_hyp'
1147    @(transitive_le
1148      total_program_size
1149      ((S program_size') + nat_of_bitvector … program_counter')
1150      (program_size' + nat_of_bitvector … program_counter'') recursive_case)
1151    normalize in match (S program_size' + nat_of_bitvector … program_counter');
1152    >plus_n_Sm
1153    @monotonic_le_plus_r
1154    change with (
1155      nat_of_bitvector … program_counter' <
1156        nat_of_bitvector … program_counter'')
1157    assumption
1158  |10:
1159    #start_status #final_status #trace_ends_flag #the_trace #program_counter_refl
1160    cases(block_cost' code_memory' program_counter' program_size total_program_size cost_labels
1161      reachable_program_counter_witness good_program_witness first_time_around ?) -block_cost'
1162    [1:
1163      #recursive_block_cost #recursive_assm
1164      @(trace_any_label_inv_ind … the_trace)
1165      [1:
1166        #start_status' #final_status' #execute_assm #classifier_assm #costed_assm
1167        #trace_ends_flag_refl #start_status_refl #final_status_refl #the_trace_refl
1168        destruct @⊥
1169      |2:
1170        #start_status' #final_status' #execute_assm #classifier_assm #trace_ends_flag_refl
1171        #start_status_refl #final_status_refl #the_trace_refl destruct
1172        whd in match (trace_any_label_length … (tal_base_return …));
1173        whd in match (compute_paid_trace_any_label … (tal_base_return …)); %
1174        [1:
1175          %{(pred total_program_size)} whd in ⊢ (??%?);
1176          @S_pred
1177          @(reachable_program_counter_to_0_lt_total_program_size … reachable_program_counter_witness)
1178        |2:
1179          <FETCH %
1180        ]
1181      |3:
1182        #status_pre_fun_call #status_start_fun_call #status_final #execute_assm
1183        #classifier_assm #after_return_assm #trace_label_return #costed_assm
1184        #trace_ends_flag_refl #start_status_refl #final_status_refl #the_trace_refl
1185        destruct @⊥
1186      |4:
1187        #end_flag #status_pre_fun_call #status_start_fun_call #status_after_fun_call
1188        #status_final #execute_assm #classifier_assm #after_return_assm #trace_label_return
1189        #costed_assm #trace_any_label #trace_ends_flag_refl #start_status_refl
1190        #final_status_refl #the_trace_refl
1191        destruct @⊥
1192      |5:
1193        #end_flag #status_pre #status_init #status_end #execute_assm #trace_any_label
1194        #classifier_assm #costed_assm #trace_ends_flag_refl #start_status_refl
1195        #final_status_refl #the_trace_refl destruct @⊥
1196      ]
1197      try (change with (ASM_classify0 ? = ? ∨ ASM_classify0 ? = ?) in classifier_assm;)
1198      try (change with (ASM_classify0 ? = ?) in classifier_assm;)
1199      whd in match current_instruction in classifier_assm; normalize nodelta in classifier_assm;
1200      whd in match current_instruction0 in classifier_assm; normalize nodelta in classifier_assm;
1201      <FETCH in classifier_assm;
1202      try(whd in ⊢ ((??%?) → ?); #absurd destruct(absurd))
1203      whd in ⊢ (?(??%?)(??%?) → ?); #absurd
1204      cases absurd #absurd destruct(absurd)
1205    |2:
1206      cases daemon (* XXX: ??? *)
1207    ]
1208  |62,63,64,65,66,67,68,69,70,101,102,103:
1209    #start_status #final_status #trace_ends_flag #the_trace #program_counter_refl
1210    cases(block_cost' code_memory' program_counter' program_size total_program_size cost_labels
1211      reachable_program_counter_witness good_program_witness first_time_around ?) -block_cost'
1212    try (@(trace_compute_paid_trace_cl_jump … reachable_program_counter_witness good_program_witness … recursive_case … FETCH … the_trace program_counter_refl …) destruct %)
1213    cases daemon (* XXX: ???, same as above case *)
1214  |96,99:
1215    cases reachable_program_counter_witness * #n #fetch_n_hyp #lt_hyp
1216    lapply(good_program_witness program_counter' reachable_program_counter_witness)
1217    <FETCH normalize nodelta <instr normalize nodelta *
1218    #program_counter_lt' #program_counter_lt_tps' %
1219    [1,3:
1220      %{(S n)} whd in ⊢ (???%); <fetch_n_hyp normalize nodelta
1221      <FETCH normalize nodelta whd in match ltb; normalize nodelta
1222      >(le_to_leb_true … program_counter_lt') %
1223    |2,4:
1224      assumption
1225    ]
1226  |98,104,107:
1227    #start_status #final_status #trace_ends_flag #the_trace #program_counter_refl
1228    cases(block_cost' ?????????) -block_cost'
1229    @(trace_compute_paid_trace_cl_call … reachable_program_counter_witness good_program_witness first_time_around … recursive_case … FETCH … the_trace program_counter_refl …)
1230    destruct %
1231  |105:
1232    cases reachable_program_counter_witness * #n #fetch_n_hyp #lt_hyp
1233    lapply(good_program_witness program_counter' reachable_program_counter_witness)
1234    <FETCH normalize nodelta <instr normalize nodelta
1235    @(subaddressing_mode_elim … [[addr16]] … [[addr16]]) [1: // ] #new_addr
1236    * * * * #n'
1237    #_ #_ #program_counter_lt' #program_counter_lt_tps'
1238    %
1239    [1:
1240      %{(S n)} whd in ⊢ (???%); <fetch_n_hyp normalize nodelta
1241      <FETCH normalize nodelta whd in match ltb; normalize nodelta
1242      >(le_to_leb_true … program_counter_lt') %
1243    |2:
1244      assumption
1245    ]
1246  |106:
1247    cases reachable_program_counter_witness * #n #fetch_n_hyp #lt_hyp
1248    lapply(good_program_witness program_counter' reachable_program_counter_witness)
1249    <FETCH normalize nodelta <instr normalize nodelta
1250    @(subaddressing_mode_elim … [[addr16]] … [[addr16]]) [1: // ] #new_addr
1251    * * * * #n'
1252    #_ #_ #program_counter_lt' #program_counter_lt_tps'
1253    @(transitive_le
1254      total_program_size
1255      ((S program_size') + nat_of_bitvector … program_counter')
1256      (program_size' + nat_of_bitvector … program_counter'') recursive_case)
1257    normalize in match (S program_size' + nat_of_bitvector … program_counter');
1258    >plus_n_Sm
1259    @monotonic_le_plus_r
1260    change with (
1261      nat_of_bitvector … program_counter' <
1262        nat_of_bitvector … program_counter'')
1263    assumption
1264  |108:
1265    cases reachable_program_counter_witness * #n #fetch_n_hyp #lt_hyp
1266    lapply(good_program_witness program_counter' reachable_program_counter_witness)
1267    <FETCH normalize nodelta <instr normalize nodelta
1268    @(subaddressing_mode_elim … [[addr11]] … [[addr11]]) [1: // ] #new_addr
1269    cases (split … 8 8 program_counter'') #pc_bu #pc_bl normalize nodelta
1270    cases (split … 3 8 new_addr) #thr #eig normalize nodelta
1271    cases (split … 5 3 pc_bu) #fiv #thr' normalize nodelta * * * * #n'
1272    #_ #_ #program_counter_lt' #program_counter_lt_tps'
1273    %
1274    [1:
1275      %{(S n)} whd in ⊢ (???%); <fetch_n_hyp normalize nodelta
1276      <FETCH normalize nodelta whd in match ltb; normalize nodelta
1277      >(le_to_leb_true … program_counter_lt') %
1278    |2:
1279      assumption
1280    ]
1281  |109:
1282    cases reachable_program_counter_witness * #n #fetch_n_hyp #lt_hyp
1283    lapply(good_program_witness program_counter' reachable_program_counter_witness)
1284    <FETCH normalize nodelta <instr normalize nodelta
1285    @(subaddressing_mode_elim … [[addr11]] … [[addr11]]) [1: // ] #new_addr
1286    cases (split … 8 8 program_counter'') #pc_bu #pc_bl normalize nodelta
1287    cases (split … 3 8 new_addr) #thr #eig normalize nodelta
1288    cases (split … 5 3 pc_bu) #fiv #thr' normalize nodelta * * * * #n'
1289    #_ #_ #program_counter_lt' #program_counter_lt_tps'
1290    @(transitive_le
1291      total_program_size
1292      ((S program_size') + nat_of_bitvector … program_counter')
1293      (program_size' + nat_of_bitvector … program_counter'') recursive_case)
1294    normalize in match (S program_size' + nat_of_bitvector … program_counter');
1295    >plus_n_Sm
1296    @monotonic_le_plus_r
1297    change with (
1298      nat_of_bitvector … program_counter' <
1299        nat_of_bitvector … program_counter'')
1300    assumption
1301  |97,100:
1302    cases reachable_program_counter_witness * #n #fetch_n_hyp #lt_hyp
1303    lapply(good_program_witness program_counter' reachable_program_counter_witness)
1304    <FETCH normalize nodelta <instr normalize nodelta
1305    try(<real_instruction_refl <instr normalize nodelta) *
1306    #pc_pc_lt_hyp' #pc_tps_lt_hyp'
1307    @(transitive_le
1308      total_program_size
1309      ((S program_size') + nat_of_bitvector … program_counter')
1310      (program_size' + nat_of_bitvector … program_counter'') recursive_case)
1311    normalize in match (S program_size' + nat_of_bitvector … program_counter');
1312    >plus_n_Sm
1313    @monotonic_le_plus_r
1314    change with (
1315      nat_of_bitvector … program_counter' <
1316        nat_of_bitvector … program_counter'')
1317    assumption
1318  ] *)
1319qed.
1320
1321(*
1322  (code_memory': BitVectorTrie Byte 16) (program_counter': Word)
1323    (program_size: nat) (total_program_size: nat) (cost_labels: BitVectorTrie costlabel 16)
1324      (reachable_program_counter_witness: reachable_program_counter code_memory' total_program_size program_counter')
1325        (good_program_witness: good_program code_memory' total_program_size) (first_time_around: bool)
1326          on program_size:
1327          total_program_size ≤ program_size + nat_of_bitvector … program_counter' →
1328          Σcost_of_block: nat.
1329          if (match lookup_opt … program_counter' cost_labels with [ None ⇒ true | _ ⇒ first_time_around ]) then
1330            ∀start_status: Status code_memory'.
1331            ∀final_status: Status code_memory'.
1332            ∀trace_ends_flag.
1333            ∀the_trace: trace_any_label (ASM_abstract_status … cost_labels) trace_ends_flag start_status final_status.
1334              program_counter' = program_counter … start_status →
1335                (∃n: nat. trace_any_label_length … the_trace + (S n) = total_program_size) ∧
1336                  cost_of_block = compute_paid_trace_any_label code_memory' cost_labels trace_ends_flag start_status final_status the_trace
1337          else
1338            (cost_of_block = 0)
1339*)
1340
1341definition block_cost:
1342    ∀code_memory': BitVectorTrie Byte 16.
1343    ∀program_counter': Word.
1344    ∀total_program_size: nat.
1345    ∀cost_labels: BitVectorTrie costlabel 16.
1346    ∀reachable_program_counter_witness: reachable_program_counter code_memory' total_program_size program_counter'.
1347    ∀good_program_witness: good_program code_memory' total_program_size.
1348      Σcost_of_block: nat.
1349        ∀start_status: Status code_memory'.
1350        ∀final_status: Status code_memory'.
1351        ∀trace_ends_flag.
1352        ∀the_trace: trace_any_label (ASM_abstract_status … cost_labels) trace_ends_flag start_status final_status.
1353          program_counter' = program_counter … start_status →
1354            (∃n: nat. trace_any_label_length … the_trace + (S n) = total_program_size) ∧
1355              cost_of_block = compute_paid_trace_any_label code_memory' cost_labels trace_ends_flag start_status final_status the_trace ≝
1356  λcode_memory: BitVectorTrie Byte 16.
1357  λprogram_counter: Word.
1358  λtotal_program_size: nat.
1359  λcost_labels: BitVectorTrie costlabel 16.
1360  λreachable_program_counter_witness: reachable_program_counter code_memory total_program_size program_counter.
1361  λgood_program_witness: good_program code_memory total_program_size. ?.
1362  cases(block_cost' code_memory program_counter total_program_size total_program_size cost_labels
1363    reachable_program_counter_witness good_program_witness true ?)
1364  [1:
1365    #cost_of_block #block_cost_hyp
1366    %{cost_of_block}
1367    cases(lookup_opt … cost_labels) in block_cost_hyp;
1368    [2: #cost_label] normalize nodelta
1369    #hyp assumption
1370  |2:
1371    @le_plus_n_r
1372  ]
1373qed.
1374
1375lemma fetch_program_counter_n_Sn:
1376  ∀instruction: instruction.
1377  ∀program_counter, program_counter': Word.
1378  ∀ticks, n: nat.
1379  ∀code_memory: BitVectorTrie Byte 16.
1380    Some … program_counter = fetch_program_counter_n n code_memory (zero 16) →
1381      〈instruction,program_counter',ticks〉 = fetch code_memory program_counter →
1382        nat_of_bitvector … program_counter < nat_of_bitvector … program_counter' →
1383          Some … program_counter' = fetch_program_counter_n (S n) code_memory (zero …).
1384  #instruction #program_counter #program_counter' #ticks #n #code_memory
1385  #fetch_program_counter_n_hyp #fetch_hyp #lt_hyp
1386  whd in match (fetch_program_counter_n (S n) code_memory (zero …));
1387  <fetch_program_counter_n_hyp normalize nodelta
1388  <fetch_hyp normalize nodelta
1389  change with (
1390    leb (S (nat_of_bitvector … program_counter)) (nat_of_bitvector … program_counter')
1391  ) in match (ltb (nat_of_bitvector … program_counter) (nat_of_bitvector … program_counter'));
1392  >(le_to_leb_true … lt_hyp) %
1393qed.
1394
1395(* XXX: to be moved into common/Identifiers.ma *)
1396lemma lookup_present_add_hit:
1397  ∀tag, A, map, k, v, k_pres.
1398    lookup_present tag A (add … map k v) k k_pres = v.
1399  #tag #a #map #k #v #k_pres
1400  lapply (lookup_lookup_present … (add … map k v) … k_pres)
1401  >lookup_add_hit #Some_assm destruct(Some_assm)
1402  <e0 %
1403qed.
1404
1405lemma lookup_present_add_miss:
1406  ∀tag, A, map, k, k', v, k_pres', k_pres''.
1407    k' ≠ k →
1408      lookup_present tag A (add … map k v) k' k_pres' = lookup_present tag A map k' k_pres''.
1409  #tag #A #map #k #k' #v #k_pres' #k_pres'' #neq_assm
1410  lapply (lookup_lookup_present … (add … map k v) ? k_pres')
1411  >lookup_add_miss try assumption
1412  #Some_assm
1413  lapply (lookup_lookup_present … map k') >Some_assm #Some_assm'
1414  lapply (Some_assm' k_pres'') #Some_assm'' destruct assumption
1415qed.
1416
1417(* XXX: to be moved into basics/types.ma *)
1418lemma not_None_to_Some:
1419  ∀A: Type[0].
1420  ∀o: option A.
1421    o ≠ None A → ∃v: A. o = Some A v.
1422  #A #o cases o
1423  [1:
1424    #absurd cases absurd #absurd' cases (absurd' (refl …))
1425  |2:
1426    #v' #ignore /2/
1427  ]
1428qed.
1429
1430lemma present_add_present:
1431  ∀tag, a, map, k, k', v.
1432    k' ≠ k →
1433      present tag a (add tag a map k v) k' →
1434        present tag a map k'.
1435  #tag #a #map #k #k' #v #neq_hyp #present_hyp
1436  whd in match present; normalize nodelta
1437  whd in match present in present_hyp; normalize nodelta in present_hyp;
1438  cases (not_None_to_Some a … present_hyp) #v' #Some_eq_hyp
1439  lapply (lookup_add_cases tag ?????? Some_eq_hyp) *
1440  [1:
1441    * #k_eq_hyp @⊥ /2/
1442  |2:
1443    #Some_eq_hyp' /2/
1444  ]
1445qed.
1446
1447lemma present_add_hit:
1448  ∀tag, a, map, k, v.
1449    present tag a (add tag a map k v) k.
1450  #tag #a #map #k #v
1451  whd >lookup_add_hit
1452  % #absurd destruct
1453qed.
1454
1455lemma present_add_miss:
1456  ∀tag, a, map, k, k', v.
1457    k' ≠ k → present tag a map k' → present tag a (add tag a map k v) k'.
1458  #tag #a #map #k #k' #v #neq_assm #present_assm
1459  whd >lookup_add_miss assumption
1460qed.
1461
1462let rec traverse_code_internal
1463  (code_memory: BitVectorTrie Byte 16) (cost_labels: BitVectorTrie costlabel 16)
1464    (program_counter: Word) (fixed_program_size: nat) (program_size: nat)
1465      (reachable_program_counter_witness:
1466          ∀lbl: costlabel.
1467          ∀program_counter: Word. Some … lbl = lookup_opt … program_counter cost_labels →
1468            reachable_program_counter code_memory fixed_program_size program_counter)
1469        (good_program_witness: good_program code_memory fixed_program_size)
1470        on program_size:
1471          Σcost_map: identifier_map CostTag nat.
1472            (∀pc,k. nat_of_bitvector … program_counter < nat_of_bitvector 16 pc → nat_of_bitvector … pc < program_size + nat_of_bitvector … program_counter → lookup_opt … pc cost_labels = Some … k → present … cost_map k) ∧
1473            (∀k. ∀k_pres:present … cost_map k. ∃pc. lookup_opt … pc cost_labels = Some … k →
1474              ∃reachable_witness: reachable_program_counter code_memory fixed_program_size pc.
1475                pi1 … (block_cost code_memory pc fixed_program_size cost_labels reachable_witness good_program_witness) = lookup_present … k_pres) ≝
1476  match program_size with
1477  [ O ⇒ empty_map …
1478  | S program_size' ⇒ pi1 …
1479 
1480    (let 〈instruction, new_program_counter, ticks〉 ≝ fetch … code_memory program_counter in
1481    let cost_mapping ≝ traverse_code_internal code_memory cost_labels new_program_counter fixed_program_size program_size' ? good_program_witness in
1482    match lookup_opt … program_counter cost_labels return λx. x = lookup_opt … program_counter cost_labels → Σcost_map: ?. ? with
1483    [ None ⇒ λlookup_refl. pi1 … cost_mapping
1484    | Some lbl ⇒ λlookup_refl.
1485      let cost ≝ block_cost code_memory program_counter fixed_program_size cost_labels ? good_program_witness in
1486        add … cost_mapping lbl cost
1487    ] (refl … (lookup_opt … program_counter cost_labels)))
1488  ].
1489  [2:
1490    @pi2
1491  |4:
1492    @(reachable_program_counter_witness lbl)
1493    assumption
1494  |3:
1495    assumption
1496  |1:
1497    %
1498    [1:
1499      #pc #k #absurd1 #absurd2
1500      @⊥ lapply(lt_to_not_le … absurd1) #absurd
1501      cases absurd #absurd @absurd
1502      @(transitive_le … absurd2) @le_S @le_n
1503    |2:
1504      #k #k_pres
1505      @⊥ normalize in k_pres; /2/
1506    ]
1507  |5:
1508    %
1509    [2:
1510      #k #k_pres
1511      @(eq_identifier_elim … k lbl)
1512      [1:
1513        #eq_assm %{program_counter} #lookup_opt_assm
1514        %{(reachable_program_counter_witness …)} try assumption
1515        >eq_assm in k_pres ⊢ (???%); #k_pres >lookup_present_add_hit %
1516      |2:
1517        #neq_assm
1518        cases cost_mapping in k_pres; #cost_mapping' #ind_hyp #present_assm
1519        cases ind_hyp #_ #relevant cases (relevant k ?)
1520        [2:
1521          @(present_add_present … present_assm) assumption
1522        |1:
1523          #program_counter' #ind_hyp' %{program_counter'}
1524          #relevant cases (ind_hyp' relevant) #reachable_witness'
1525          #ind_hyp'' %{reachable_witness'} >ind_hyp''
1526          @sym_eq @lookup_present_add_miss assumption
1527        ]
1528      ]
1529    |1:
1530      #pc #k #H1 #H2 #lookup_opt_assm @(eq_identifier_elim … k lbl)
1531      [1:
1532        #eq_assm >eq_assm
1533        cases cost_mapping #cost_mapping' * #ind_hyp #_
1534        @present_add_hit
1535      |2:
1536        #neq_assm @present_add_miss try assumption
1537        cases cost_mapping #cost_mapping' * #ind_hyp #_
1538        cases daemon (* XXX: !!! *)
1539      ]
1540    ]
1541  |6:
1542  ]
1543qed.
1544
1545definition traverse_code:
1546  ∀code_memory: BitVectorTrie Byte 16.
1547  ∀cost_labels: BitVectorTrie costlabel 16.
1548  ∀program_size: nat.
1549  ∀reachable_program_counter_witness:
1550    ∀lbl: costlabel.
1551    ∀program_counter: Word. Some … lbl = lookup_opt … program_counter cost_labels →
1552      reachable_program_counter code_memory program_size program_counter.
1553  ∀good_program_witness: good_program code_memory program_size.
1554    Σcost_map: identifier_map CostTag nat.
1555      (∀k. ∀k_pres:present … cost_map k. ∃pc. lookup_opt … pc cost_labels = Some … k →
1556        ∃reachable_witness: reachable_program_counter code_memory program_size pc.
1557          pi1 … (block_cost code_memory pc program_size cost_labels reachable_witness good_program_witness) = lookup_present … k_pres) ≝
1558  λcode_memory: BitVectorTrie Byte 16.
1559  λcost_labels: BitVectorTrie costlabel 16.
1560  λprogram_size: nat.
1561  λreachable_program_counter_witness:
1562          ∀lbl: costlabel.
1563          ∀program_counter: Word. Some … lbl = lookup_opt … program_counter cost_labels →
1564            reachable_program_counter code_memory program_size program_counter.
1565  λgood_program_witness: good_program code_memory program_size.
1566    traverse_code_internal code_memory cost_labels (zero …) program_size program_size reachable_program_counter_witness good_program_witness.
1567 
1568definition compute_costs ≝
1569  λprogram: list Byte.
1570  λcost_labels: BitVectorTrie costlabel 16.
1571  λreachable_program_witness.
1572  λgood_program_witness: good_program (load_code_memory program) (|program| + 1).
1573    let program_size ≝ |program| + 1 in
1574    let code_memory ≝ load_code_memory program in
1575      traverse_code code_memory cost_labels program_size reachable_program_witness ?.
1576  @good_program_witness
1577qed.
Note: See TracBrowser for help on using the repository browser.