source: src/ASM/ASMCosts.ma @ 1906

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

Statements simplified in block_cost and dependencies

File size: 68.8 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
333(* XXX: todo *)
334lemma subaddressing_mode_elim':
335  ∀T: Type[2].
336  ∀n: nat.
337  ∀o: nat.
338  ∀v1: Vector addressing_mode_tag n.
339  ∀v2: Vector addressing_mode_tag o.
340  ∀Q: addressing_mode → T → Prop.
341  ∀fixed_v: Vector addressing_mode_tag (n + o).
342  ∀P1,P2,P3,P4,P5,P6,P7,P8,P9,P10,P11,P12,P13,P14,P15,P16,P17,P18,P19.
343  ∀fixed_v_proof: fixed_v = v1 @@ v2.
344  ∀subaddressing_mode_proof.
345    subaddressing_mode_elim_type T (n + o) fixed_v Q P1 P2 P3 P4 P5 P6 P7
346      P8 P9 P10 P11 P12 P13 P14 P15 P16 P17 P18 P19 (n + o) (v1 @@ v2) subaddressing_mode_proof.
347  #T #n #o #v1 #v2
348  elim v1 cases v2
349  [1:
350    #Q #fixed_v #P1 #P2 #P3 #P4 #P5 #P6 #P7 #P8 #P9 #P10
351    #P11 #P12 #P13 #P14 #P15 #P16 #P17 #P18 #P19 #fixed_v_proof
352    #subaddressing_mode_proof destruct normalize
353    #addr #absurd cases absurd
354  |2:
355    #n' #hd #tl #Q #fixed_v #P1 #P2 #P3 #P4 #P5 #P6 #P7 #P8 #P9 #P10
356    #P11 #P12 #P13 #P14 #P15 #P16 #P17 #P18 #P19 #fixed_v_proof
357    destruct normalize in match ([[]]@@hd:::tl);
358  ]
359  cases daemon
360qed.
361
362(* XXX: todo *)
363axiom subaddressing_mode_elim:
364  ∀T: Type[2].
365  ∀m: nat.
366  ∀n: nat.
367  ∀Q: addressing_mode → T → Prop.
368  ∀fixed_v: Vector addressing_mode_tag m.
369  ∀P1,P2,P3,P4,P5,P6,P7,P8,P9,P10,P11,P12,P13,P14,P15,P16,P17,P18,P19.
370  ∀v: Vector addressing_mode_tag n.
371  ∀proof.
372    subaddressing_mode_elim_type T m fixed_v Q P1 P2 P3 P4 P5 P6 P7
373      P8 P9 P10 P11 P12 P13 P14 P15 P16 P17 P18 P19 n v proof.
374
375definition current_instruction_is_labelled ≝
376  λcode_memory.
377  λcost_labels: BitVectorTrie costlabel 16.
378  λs: Status code_memory.
379  let pc ≝ program_counter … code_memory s in
380    match lookup_opt … pc cost_labels with
381    [ None ⇒ False
382    | _    ⇒ True
383    ].
384
385definition next_instruction_properly_relates_program_counters ≝
386  λcode_memory.
387  λbefore: Status code_memory.
388  λafter : Status code_memory.
389    let program_counter_before ≝ program_counter ? code_memory before in
390    let 〈instruction, program_counter_after, ticks〉 ≝ fetch code_memory program_counter_before in
391      program_counter ? code_memory after = program_counter_after.
392(* XXX: why defined like this?
393  let size ≝ current_instruction_cost code_memory before in
394  let pc_before ≝ program_counter … code_memory before in
395  let pc_after ≝ program_counter … code_memory after in
396  let sum ≝ \snd (half_add … pc_before (bitvector_of_nat … size)) in
397    sum = pc_after.
398*)
399
400definition ASM_abstract_status: ∀code_memory. BitVectorTrie costlabel 16 → abstract_status ≝
401  λcode_memory.
402  λcost_labels.
403    mk_abstract_status
404      (Status code_memory)
405      (λs,s'. (execute_1 … s) = s')
406      (λs,class. ASM_classify … s = class)
407      (current_instruction_is_labelled … cost_labels)
408      (next_instruction_properly_relates_program_counters code_memory).
409
410let rec trace_any_label_length
411  (code_memory: BitVectorTrie Byte 16) (cost_labels: BitVectorTrie costlabel 16)
412    (trace_ends_flag: trace_ends_with_ret) (start_status: Status code_memory)
413      (final_status: Status code_memory)
414      (the_trace: trace_any_label (ASM_abstract_status code_memory cost_labels) trace_ends_flag start_status final_status)
415        on the_trace: nat ≝
416  match the_trace with
417  [ tal_base_not_return the_status _ _ _ _ ⇒ 0
418  | tal_base_call pre_fun_call start_fun_call final _ _ _ _ call_trace ⇒ 0
419  | tal_base_return the_status _ _ _ ⇒ 0
420  | tal_step_call end_flag pre_fun_call start_fun_call after_fun_call final _ _ _ call_trace _ final_trace ⇒
421      let tail_length ≝ trace_any_label_length … final_trace in
422      let pc_difference ≝ nat_of_bitvector … (program_counter … after_fun_call) - nat_of_bitvector … (program_counter … pre_fun_call) in       
423        pc_difference + tail_length
424  | tal_step_default end_flag status_pre status_init status_end _ tail_trace _ _ ⇒
425      let tail_length ≝ trace_any_label_length … tail_trace in
426      let pc_difference ≝ nat_of_bitvector … (program_counter … status_init) - nat_of_bitvector … (program_counter … status_pre) in
427        pc_difference + tail_length       
428  ].
429
430let rec compute_paid_trace_any_label
431  (code_memory: BitVectorTrie Byte 16) (cost_labels: BitVectorTrie costlabel 16)
432    (trace_ends_flag: trace_ends_with_ret) (start_status: Status code_memory)
433      (final_status: Status code_memory)
434        (the_trace: trace_any_label (ASM_abstract_status code_memory cost_labels) trace_ends_flag start_status final_status)
435       on the_trace: nat ≝
436  match the_trace with
437  [ tal_base_not_return the_status _ _ _ _ ⇒ current_instruction_cost … the_status
438  | tal_base_return the_status _ _ _ ⇒ current_instruction_cost … the_status
439  | tal_base_call pre_fun_call start_fun_call final _ _ _ _ call_trace ⇒ current_instruction_cost … pre_fun_call
440  | tal_step_call end_flag pre_fun_call start_fun_call after_fun_call final
441     _ _ _ call_trace _ final_trace ⇒
442      let current_instruction_cost ≝ current_instruction_cost … pre_fun_call in
443      let final_trace_cost ≝ compute_paid_trace_any_label … cost_labels end_flag … final_trace in
444        current_instruction_cost + final_trace_cost
445  | tal_step_default end_flag status_pre status_init status_end _ tail_trace _ _ ⇒
446      let current_instruction_cost ≝ current_instruction_cost … status_pre in
447      let tail_trace_cost ≝
448       compute_paid_trace_any_label … cost_labels end_flag
449        status_init status_end tail_trace
450      in
451        current_instruction_cost + tail_trace_cost
452  ].
453
454definition compute_paid_trace_label_label ≝
455  λcode_memory: BitVectorTrie Byte 16.
456  λcost_labels: BitVectorTrie costlabel 16.
457  λtrace_ends_flag: trace_ends_with_ret.
458  λstart_status: Status code_memory.
459  λfinal_status: Status code_memory.
460  λthe_trace: trace_label_label (ASM_abstract_status … cost_labels) trace_ends_flag start_status final_status.
461  match the_trace with
462  [ tll_base ends_flag initial final given_trace labelled_proof ⇒
463      compute_paid_trace_any_label … given_trace
464  ].
465
466include alias "arithmetics/nat.ma".
467include alias "basics/logic.ma".
468
469lemma plus_right_monotone:
470  ∀m, n, o: nat.
471    m = n → m + o = n + o.
472  #m #n #o #refl >refl %
473qed.
474
475lemma plus_left_monotone:
476  ∀m, n, o: nat.
477    m = n → o + m = o + n.
478  #m #n #o #refl destruct %
479qed.
480
481lemma minus_plus_cancel:
482  ∀m, n : nat.
483  ∀proof: n ≤ m.
484    (m - n) + n = m.
485  #m #n #proof /2 by plus_minus/
486qed.
487
488(* XXX: indexing bug *)
489lemma fetch_twice_fetch_execute_1:
490  ∀code_memory: BitVectorTrie Byte 16.
491  ∀start_status: Status code_memory.
492    ASM_classify code_memory start_status = cl_other →
493    \snd (\fst (fetch code_memory (program_counter … start_status))) =
494      program_counter … (execute_1 … start_status).
495  #code_memory #start_status #classify_assm
496  whd in match execute_1; normalize nodelta
497  cases (execute_1' code_memory start_status) #the_status
498  * #_ #classify_assm' @classify_assm' assumption
499qed-.
500
501lemma reachable_program_counter_to_0_lt_total_program_size:
502  ∀code_memory: BitVectorTrie Byte 16.
503  ∀program_counter: Word.
504  ∀total_program_size: nat.
505    reachable_program_counter code_memory total_program_size program_counter →
506      0 < total_program_size.
507  #code_memory #program_counter #total_program_size
508  whd in match reachable_program_counter; normalize nodelta * * #some_n
509  #_ cases (nat_of_bitvector 16 program_counter)
510  [1:
511    #assm assumption
512  |2:
513    #new_pc @ltn_to_ltO
514  ]
515qed.
516
517lemma trace_compute_paid_trace_cl_other:
518  ∀code_memory' : (BitVectorTrie Byte 16).
519  ∀program_counter' : Word.
520  ∀total_program_size : ℕ.
521  ∀cost_labels : (BitVectorTrie costlabel 16).
522  ∀reachable_program_counter_witness : (reachable_program_counter code_memory' total_program_size program_counter').
523  ∀good_program_witness : (good_program code_memory' total_program_size).
524  ∀program_size' : ℕ.
525  ∀recursive_case : (total_program_size≤S program_size'+nat_of_bitvector 16 program_counter').
526  ∀ticks : ℕ.
527  ∀instruction : instruction.
528  ∀program_counter'' : Word.
529  ∀FETCH : (〈instruction,program_counter'',ticks〉=fetch code_memory' program_counter').
530  ∀start_status : (Status code_memory').
531  ∀final_status : (Status code_memory').
532  ∀trace_ends_flag : trace_ends_with_ret.
533  ∀the_trace : (trace_any_label (ASM_abstract_status code_memory' cost_labels) trace_ends_flag start_status final_status).
534  ∀program_counter_refl : (program_counter' = program_counter (BitVectorTrie Byte 16) code_memory' start_status).
535  ∀classify_assm: ASM_classify0 instruction = cl_other.
536  ∀pi1 : ℕ.
537   (if match lookup_opt costlabel 16 program_counter'' cost_labels with 
538         [None ⇒ true
539         |Some _ ⇒ false
540         ] 
541    then
542      ∀start_status0:Status code_memory'.
543      ∀final_status0:Status code_memory'.
544      ∀trace_ends_flag0:trace_ends_with_ret.
545      ∀the_trace0:trace_any_label (ASM_abstract_status code_memory' cost_labels) trace_ends_flag0 start_status0 final_status0.
546        program_counter'' = program_counter (BitVectorTrie Byte 16) code_memory' start_status0 →
547                  pi1
548                    =compute_paid_trace_any_label code_memory' cost_labels
549                     trace_ends_flag0 start_status0 final_status0 the_trace0
550    else (pi1=O) )
551   → ticks+pi1
552     =compute_paid_trace_any_label code_memory' cost_labels trace_ends_flag
553      start_status final_status the_trace.
554  #code_memory' #program_counter' #total_program_size #cost_labels
555  #reachable_program_counter_witness #good_program_witness
556  #program_size' #recursive_case #ticks #instruction #program_counter'' #FETCH
557  #start_status #final_status
558  #trace_ends_flag #the_trace #program_counter_refl #classify_assm #recursive_block_cost
559  #recursive_assm
560  @(trace_any_label_inv_ind … the_trace)
561    [5:
562      #end_flag #status_pre #status_init #status_end #execute_assm #trace_any_label
563      #classifier_assm #costed_assm #trace_ends_refl #start_status_refl #final_status_refl
564      #the_trace_refl
565      destruct
566      whd in match (trace_any_label_length … (tal_step_default …));
567      whd in match (compute_paid_trace_any_label … (tal_step_default …));
568      whd in costed_assm:(?%);
569      generalize in match costed_assm;
570      generalize in match (refl … (lookup_opt … (program_counter … (execute_1 … status_pre)) cost_labels));
571      generalize in match (lookup_opt … (program_counter … (execute_1 … status_pre)) cost_labels)
572        in ⊢ (??%? → ?(match % with [ _ ⇒ ? | _ ⇒ ? ]) → ?);
573      #lookup_assm cases lookup_assm
574      [1:
575        #None_lookup_opt_assm normalize nodelta #ignore
576        generalize in match recursive_assm;
577        cut(program_counter'' = (program_counter (BitVectorTrie Byte 16) code_memory' (execute_1 code_memory' status_pre)))
578        [1:
579          <fetch_twice_fetch_execute_1
580          [1:
581            <FETCH %
582          |2:
583            >classifier_assm %
584          ]
585        |2:
586          #program_counter_assm >program_counter_assm <None_lookup_opt_assm
587          normalize nodelta #new_recursive_assm
588          cases(new_recursive_assm (execute_1 code_memory' status_pre) status_end
589            end_flag trace_any_label ?) try %
590          whd in match (current_instruction_cost … status_pre);
591          cut(ticks = \snd (fetch code_memory'
592             (program_counter (BitVectorTrie Byte 16) code_memory' status_pre)))
593          [1:
594            <FETCH %
595          |2:
596            #ticks_refl_assm >ticks_refl_assm %
597          ]
598        ]
599      |2:
600        #costlabel #Some_lookup_opt_assm <Some_lookup_opt_assm normalize nodelta
601        #absurd cases absurd #absurd cases(absurd I)
602      ]
603    |1:
604      #status_start #status_final #execute_assm #classifier_assm #costed_assm
605      #trace_ends_flag_refl #start_status_refl #final_status_refl #the_trace_refl
606      destruct
607      whd in match (trace_any_label_length … (tal_base_not_return …));
608      whd in match (compute_paid_trace_any_label … (tal_base_not_return …));
609      whd in costed_assm;
610      generalize in match costed_assm;
611      generalize in match (refl … (lookup_opt … (program_counter … (execute_1 … status_start)) cost_labels));
612      generalize in match (lookup_opt … (program_counter … (execute_1 code_memory' status_start)) cost_labels)
613        in ⊢ (??%? → (match % with [ _ ⇒ ? | _ ⇒ ? ]) → ?);
614      #lookup_assm cases lookup_assm
615      [1:
616        #None_lookup_opt_assm normalize nodelta >None_lookup_opt_assm
617        #absurd cases absurd
618      |2:
619        #costlabel #Some_lookup_opt_assm normalize nodelta #ignore
620        generalize in match recursive_assm;
621        cut(program_counter'' = (program_counter (BitVectorTrie Byte 16) code_memory' (execute_1 … status_start)))
622        [1:
623          <fetch_twice_fetch_execute_1
624          [1:
625            <FETCH %
626          |2:
627            cases classifier_assm
628            [1:
629              whd in ⊢ (% → ?);
630              whd in ⊢ (??%? → ?);
631              whd in match (current_instruction code_memory' status_start);
632              <FETCH generalize in match classify_assm;
633              cases instruction
634              [8:
635                #preinstruction normalize nodelta
636                whd in match ASM_classify0; normalize nodelta
637                #contradiction >contradiction #absurd destruct(absurd)
638              ]
639              try(#addr1 #addr2 normalize nodelta #ignore #absurd destruct(absurd))
640              try(#addr normalize nodelta #ignore #absurd destruct(absurd))
641              normalize in ignore; destruct(ignore)
642            |2:
643              #classifier_assm' >classifier_assm' %
644            ]
645          ]
646        |2:
647          #program_counter_assm >program_counter_assm <Some_lookup_opt_assm
648          normalize nodelta #new_recursive_assm >new_recursive_assm
649          cut(ticks = \snd (fetch code_memory'
650             (program_counter (BitVectorTrie Byte 16) code_memory' status_start)))
651          [1:
652            <FETCH %
653          |2:
654            #ticks_refl_assm >ticks_refl_assm
655            <plus_n_O %
656          ]
657        ]
658      ]
659    |2:
660      #start_status' #final_status' #execute_assm #classifier_assm #trace_ends_assm
661      #start_status_refl #final_status_refl #the_trace_assm destruct @⊥
662    |3:
663      #status_pre_fun_call #status_start_fun_call #status_final #execute_assm
664      #classifier_assm #after_return_assm #trace_label_return #costed_assm
665      #ends_flag_refl #start_status_refl #final_status_refl #the_trace_refl
666      destruct @⊥
667    |4:
668      #end_flag #status_pre_fun_call #status_start_fun_call #status_after_fun_call
669      #status_final #execute_assm #classifier_assm #after_return_assm #trace_label_return
670      #costed_assm #trace_any_label #trace_ends_flag_refl #start_status_refl
671      #final_status_refl #the_trace_refl destruct @⊥
672    ]
673  change with (ASM_classify0 ? = ?) in classifier_assm;
674  whd in match current_instruction in classifier_assm; normalize nodelta in classifier_assm;
675  whd in match current_instruction0 in classifier_assm; normalize nodelta in classifier_assm;
676  <FETCH in classifier_assm; >classify_assm #absurd destruct(absurd)
677qed.
678
679lemma trace_compute_paid_trace_cl_jump:
680  ∀code_memory': BitVectorTrie Byte 16.
681  ∀program_counter': Word.
682  ∀total_program_size: ℕ.
683  ∀cost_labels: BitVectorTrie costlabel 16.
684  ∀reachable_program_counter_witness: reachable_program_counter code_memory' total_program_size program_counter'.
685  ∀good_program_witness: good_program code_memory' total_program_size.
686  ∀first_time_around: bool.
687  ∀program_size': ℕ.
688  ∀recursive_case: total_program_size ≤ S program_size'+nat_of_bitvector 16 program_counter'.
689  ∀ticks: ℕ.
690  ∀instruction: instruction.
691  ∀program_counter'': Word.
692  ∀FETCH: 〈instruction,program_counter'',ticks〉 = fetch code_memory' program_counter'.
693  ∀start_status: (Status code_memory').
694  ∀final_status: (Status code_memory').
695  ∀trace_ends_flag: trace_ends_with_ret.
696  ∀the_trace: (trace_any_label (ASM_abstract_status code_memory' cost_labels) trace_ends_flag start_status final_status).
697  ∀program_counter_refl: (program_counter' = program_counter (BitVectorTrie Byte 16) code_memory' start_status).
698  ∀classify_assm: ASM_classify0 instruction = cl_jump.
699    ticks
700     =compute_paid_trace_any_label code_memory' cost_labels trace_ends_flag
701      start_status final_status the_trace.
702  #code_memory' #program_counter' #total_program_size #cost_labels
703  #reachable_program_counter_witness #good_program_witness #first_time_around
704  #program_size' #recursive_case #ticks #instruction #program_counter'' #FETCH
705  #start_status #final_status
706  #trace_ends_flag #the_trace #program_counter_refl #classify_assm
707  @(trace_any_label_inv_ind … the_trace)
708  [5:
709    #end_flag #status_pre #status_init #status_end #execute_assm #trace_any_label
710    #classifier_assm #costed_assm #trace_ends_refl #start_status_refl #final_status_refl
711    #the_trace_refl destruct @⊥
712  |1:
713    #status_start #status_final #execute_assm #classifier_assm #costed_assm
714    #trace_ends_flag_refl #start_status_refl #final_status_refl #the_trace_refl
715    destruct
716    whd in match (trace_any_label_length … (tal_base_not_return …));
717    whd in match (compute_paid_trace_any_label … (tal_base_not_return …));
718    <FETCH %
719  |2:
720    #start_status' #final_status' #execute_assm #classifier_assm #trace_ends_assm
721    #start_status_refl #final_status_refl #the_trace_assm destruct @⊥
722  |3:
723    #status_pre_fun_call #status_start_fun_call #status_final #execute_assm
724    #classifier_assm #after_return_assm #trace_label_return #costed_assm
725    #ends_flag_refl #start_status_refl #final_status_refl #the_trace_refl
726    destruct @⊥
727  |4:
728    #end_flag #status_pre_fun_call #status_start_fun_call #status_after_fun_call
729    #status_final #execute_assm #classifier_assm #after_return_assm #trace_label_return
730    #costed_assm #trace_any_label #trace_ends_flag_refl #start_status_refl
731    #final_status_refl #the_trace_refl destruct @⊥
732  ]
733  change with (ASM_classify0 ? = ?) in classifier_assm;
734  whd in match current_instruction in classifier_assm; normalize nodelta in classifier_assm;
735  whd in match current_instruction0 in classifier_assm; normalize nodelta in classifier_assm;
736  <FETCH in classifier_assm; >classify_assm #absurd destruct(absurd)
737qed.
738
739lemma trace_compute_paid_trace_cl_call:
740  ∀code_memory' : (BitVectorTrie Byte 16).
741  ∀program_counter' : Word.
742  ∀total_program_size : ℕ.
743  ∀cost_labels : (BitVectorTrie costlabel 16).
744  ∀reachable_program_counter_witness : (reachable_program_counter code_memory' total_program_size program_counter').
745  ∀good_program_witness : (good_program code_memory' total_program_size).
746  ∀program_size' : ℕ.
747  ∀recursive_case : (total_program_size≤S program_size'+nat_of_bitvector 16 program_counter').
748  ∀ticks : ℕ.
749  ∀instruction : instruction.
750  ∀program_counter'' : Word.
751  ∀FETCH : (〈instruction,program_counter'',ticks〉=fetch code_memory' program_counter').
752  ∀start_status : (Status code_memory').
753  ∀final_status : (Status code_memory').
754  ∀trace_ends_flag : trace_ends_with_ret.
755  ∀the_trace : (trace_any_label (ASM_abstract_status code_memory' cost_labels) trace_ends_flag start_status final_status).
756  ∀program_counter_refl : (program_counter' = program_counter (BitVectorTrie Byte 16) code_memory' start_status).
757  ∀classify_assm: ASM_classify0 instruction = cl_call.
758  (∀pi1:ℕ
759  .if match lookup_opt costlabel 16 program_counter'' cost_labels with 
760      [None ⇒ true | Some _ ⇒ false] 
761   then (∀start_status0:Status code_memory'
762             .∀final_status0:Status code_memory'
763              .∀trace_ends_flag0:trace_ends_with_ret
764               .∀the_trace0:trace_any_label
765                                        (ASM_abstract_status code_memory' cost_labels)
766                                        trace_ends_flag0 start_status0 final_status0
767                .program_counter''
768                 =program_counter (BitVectorTrie Byte 16) code_memory' start_status0
769                 → pi1
770                   =compute_paid_trace_any_label code_memory' cost_labels
771                    trace_ends_flag0 start_status0 final_status0 the_trace0) 
772   else (pi1=O) 
773   → ticks+pi1
774     =compute_paid_trace_any_label code_memory' cost_labels trace_ends_flag
775      start_status final_status the_trace).
776  #code_memory' #program_counter' #total_program_size #cost_labels
777  #reachable_program_counter_witness #good_program_witness #program_size'
778  #recursive_case #ticks #instruction #program_counter'' #FETCH
779  #start_status #final_status #trace_ends_flag
780  #the_trace #program_counter_refl #classify_assm #recursive_block_cost #recursive_assm
781  @(trace_any_label_inv_ind … the_trace)
782  [5:
783    #end_flag #status_pre #status_init #status_end #execute_assm #trace_any_label
784    #classifier_assm #costed_assm #trace_ends_refl #start_status_refl #final_status_refl
785    #the_trace_refl destruct @⊥
786  |1:
787    #status_start #status_final #execute_assm #classifier_assm #costed_assm
788    #trace_ends_flag_refl #start_status_refl #final_status_refl #the_trace_refl
789    destruct @⊥
790  |2:
791    #start_status' #final_status' #execute_assm #classifier_assm #trace_ends_assm
792    #start_status_refl #final_status_refl #the_trace_assm destruct @⊥
793  |3:
794    #status_pre_fun_call #status_start_fun_call #status_final #execute_assm
795    #classifier_assm #after_return_assm #trace_label_return #costed_assm
796    #ends_flag_refl #start_status_refl #final_status_refl #the_trace_refl
797    destruct
798    whd in match (trace_any_label_length … (tal_base_call …));
799    whd in match (compute_paid_trace_any_label … (tal_base_call …));
800    whd in costed_assm;
801    generalize in match costed_assm;
802    generalize in match (refl … (lookup_opt … (program_counter … status_final) cost_labels));
803    generalize in match (lookup_opt … (program_counter … status_final) cost_labels)
804      in ⊢ (??%? → (match % with [ _ ⇒ ? | _ ⇒ ? ]) → ?);
805    #lookup_assm cases lookup_assm
806    [1:
807      #None_lookup_opt normalize nodelta #absurd cases absurd
808    |2:
809      #costlabel #Some_lookup_opt normalize nodelta #ignore
810      generalize in match recursive_assm;
811      cut(program_counter'' = (program_counter (BitVectorTrie Byte 16) code_memory' status_final))
812      [1:
813        generalize in match after_return_assm;
814        whd in ⊢ (% → ?); <FETCH normalize nodelta #relevant <relevant %
815      |2:
816        #program_counter_assm >program_counter_assm <Some_lookup_opt
817        normalize nodelta #new_recursive_assm >new_recursive_assm
818        cut(ticks = \snd (fetch code_memory' (program_counter (BitVectorTrie Byte 16) code_memory' status_pre_fun_call)))
819        [1:
820          <FETCH %
821        |2:
822          #ticks_refl_assm >ticks_refl_assm
823          <plus_n_O %
824        ]
825      ]
826    ]
827  |4:
828    #end_flag #status_pre_fun_call #status_start_fun_call #status_after_fun_call
829    #status_final #execute_assm #classifier_assm #after_return_assm #trace_label_return
830    #costed_assm #trace_any_label #trace_ends_flag_refl #start_status_refl
831    #final_status_refl #the_trace_refl
832    generalize in match execute_assm; destruct #execute_assm
833    whd in match (trace_any_label_length … (tal_step_call …));
834    whd in match (compute_paid_trace_any_label … (tal_step_call …));
835    whd in costed_assm:(?%);
836    generalize in match costed_assm;
837    generalize in match (refl … (lookup_opt … (program_counter … status_after_fun_call) cost_labels));
838    generalize in match (lookup_opt … (program_counter … status_after_fun_call) cost_labels)
839      in ⊢ (??%? → ?(match % with [ _ ⇒ ? | _ ⇒ ? ]) → ?);
840    #lookup_assm cases lookup_assm
841    [1:
842      #None_lookup_opt_assm normalize nodelta #ignore
843      generalize in match recursive_assm;
844      cut(program_counter'' = program_counter … status_after_fun_call)
845      [1:
846        generalize in match after_return_assm;
847        whd in ⊢ (% → ?); <FETCH normalize nodelta #relevant >relevant %
848      |2:
849        #program_counter_refl >program_counter_refl <None_lookup_opt_assm
850        normalize nodelta #new_recursive_assm
851        cases (new_recursive_assm … trace_any_label ?)
852        [1:
853          @plus_right_monotone whd in ⊢ (???%); <FETCH %
854        |2:
855          %
856        ]
857      ]
858    |2:
859      #cost_label #Some_lookup_opt_assm normalize nodelta #absurd
860      cases absurd #absurd cases (absurd I)
861    ]
862  ]
863  try (change with (ASM_classify0 ? = ? ∨ ASM_classify0 ? = ?) in classifier_assm;)
864  try (change with (ASM_classify0 ? = ?) in classifier_assm;)
865  whd in match current_instruction in classifier_assm; normalize nodelta in classifier_assm;
866  whd in match current_instruction0 in classifier_assm; normalize nodelta in classifier_assm;
867  <FETCH in classifier_assm; >classify_assm #absurd destruct(absurd) cases absurd
868  #absurd destruct(absurd)
869qed.
870
871lemma trace_compute_paid_trace_cl_return:
872  ∀code_memory' : (BitVectorTrie Byte 16).
873  ∀program_counter' : Word.
874  ∀total_program_size : ℕ.
875  ∀cost_labels : (BitVectorTrie costlabel 16).
876  ∀reachable_program_counter_witness : (reachable_program_counter code_memory' total_program_size program_counter').
877  ∀good_program_witness : (good_program code_memory' total_program_size).
878  ∀program_size' : ℕ.
879  ∀recursive_case : (total_program_size≤S program_size'+nat_of_bitvector 16 program_counter').
880  ∀ticks : ℕ.
881  ∀instruction : instruction.
882  ∀program_counter'' : Word.
883  ∀FETCH : (〈instruction,program_counter'',ticks〉=fetch code_memory' program_counter').
884  ∀start_status : (Status code_memory').
885  ∀final_status : (Status code_memory').
886  ∀trace_ends_flag : trace_ends_with_ret.
887  ∀the_trace : (trace_any_label (ASM_abstract_status code_memory' cost_labels) trace_ends_flag start_status final_status).
888  ∀program_counter_refl : (program_counter' = program_counter (BitVectorTrie Byte 16) code_memory' start_status).
889  ∀classify_assm: ASM_classify0 instruction = cl_return.
890    ticks
891     =compute_paid_trace_any_label code_memory' cost_labels trace_ends_flag
892      start_status final_status the_trace.
893  #code_memory' #program_counter' #total_program_size #cost_labels
894  #reachable_program_counter_witness #good_program_witness #program_size'
895  #recursive_case #ticks #instruction #program_counter'' #FETCH
896  #start_status #final_status #trace_ends_flag
897  #the_trace #program_counter_refl #classify_assm
898  @(trace_any_label_inv_ind … the_trace)
899  [1:
900    #start_status' #final_status' #execute_assm #classifier_assm #costed_assm
901    #trace_ends_flag_refl #start_status_refl #final_status_refl #the_trace_refl
902    destruct @⊥
903  |2:
904    #start_status' #final_status' #execute_assm #classifier_assm #trace_ends_flag_refl
905    #start_status_refl #final_status_refl #the_trace_refl destruct
906    whd in match (trace_any_label_length … (tal_base_return …));
907    whd in match (compute_paid_trace_any_label … (tal_base_return …));
908    <FETCH %
909  |3:
910    #status_pre_fun_call #status_start_fun_call #status_final #execute_assm
911    #classifier_assm #after_return_assm #trace_label_return #costed_assm
912    #trace_ends_flag_refl #start_status_refl #final_status_refl #the_trace_refl
913    destruct @⊥
914  |4:
915    #end_flag #status_pre_fun_call #status_start_fun_call #status_after_fun_call
916    #status_final #execute_assm #classifier_assm #after_return_assm #trace_label_return
917    #costed_assm #trace_any_label #trace_ends_flag_refl #start_status_refl
918    #final_status_refl #the_trace_refl
919    destruct @⊥
920  |5:
921    #end_flag #status_pre #status_init #status_end #execute_assm #trace_any_label
922    #classifier_assm #costed_assm #trace_ends_flag_refl #start_status_refl
923    #final_status_refl #the_trace_refl destruct @⊥
924  ]
925  try (change with (ASM_classify0 ? = ? ∨ ASM_classify0 ? = ?) in classifier_assm;)
926  try (change with (ASM_classify0 ? = ?) in classifier_assm;)
927  whd in match current_instruction in classifier_assm; normalize nodelta in classifier_assm;
928  whd in match current_instruction0 in classifier_assm; normalize nodelta in classifier_assm;
929  <FETCH in classifier_assm; >classify_assm
930  #absurd try (destruct(absurd))
931  cases absurd
932  #absurd destruct(absurd)
933qed.
934     
935let rec block_cost'
936  (code_memory': BitVectorTrie Byte 16) (program_counter': Word)
937    (program_size: nat) (total_program_size: nat) (cost_labels: BitVectorTrie costlabel 16)
938      (reachable_program_counter_witness: reachable_program_counter code_memory' total_program_size program_counter')
939        (good_program_witness: good_program code_memory' total_program_size) (first_time_around: bool)
940          on program_size:
941          total_program_size ≤ program_size + nat_of_bitvector … program_counter' →
942          Σcost_of_block: nat.
943          if (match lookup_opt … program_counter' cost_labels with [ None ⇒ true | _ ⇒ first_time_around ]) then
944            ∀start_status: Status code_memory'.
945            ∀final_status: Status code_memory'.
946            ∀trace_ends_flag.
947            ∀the_trace: trace_any_label (ASM_abstract_status … cost_labels) trace_ends_flag start_status final_status.
948              program_counter' = program_counter … start_status →
949                cost_of_block = compute_paid_trace_any_label code_memory' cost_labels trace_ends_flag start_status final_status the_trace
950          else
951            (cost_of_block = 0) ≝
952  match program_size return λprogram_size: nat. total_program_size ≤ program_size + nat_of_bitvector … program_counter' → ? with
953  [ O ⇒ λbase_case. ⊥
954  | S program_size' ⇒ λrecursive_case.
955    let 〈instruction, program_counter'', ticks〉 as FETCH ≝ fetch code_memory' program_counter' in
956    let to_continue ≝
957      match lookup_opt … program_counter' cost_labels with
958      [ None ⇒ true
959      | Some _ ⇒ first_time_around
960      ]
961    in
962      ((if to_continue then
963       pi1 … (match instruction return λx. x = instruction → ? with
964        [ RealInstruction real_instruction ⇒ λreal_instruction_refl.
965          match real_instruction return λx. x = real_instruction →
966          Σcost_of_block: nat.
967            ∀start_status: Status code_memory'.
968            ∀final_status: Status code_memory'.
969            ∀trace_ends_flag.
970            ∀the_trace: trace_any_label (ASM_abstract_status code_memory' cost_labels) trace_ends_flag start_status final_status.
971              program_counter' = program_counter … start_status →
972                cost_of_block = compute_paid_trace_any_label code_memory' cost_labels trace_ends_flag start_status final_status the_trace with
973          [ RET                    ⇒ λinstr. ticks
974          | RETI                   ⇒ λinstr. ticks
975          | JC   relative          ⇒ λinstr. ticks
976          | JNC  relative          ⇒ λinstr. ticks
977          | JB   bit_addr relative ⇒ λinstr. ticks
978          | JNB  bit_addr relative ⇒ λinstr. ticks
979          | JBC  bit_addr relative ⇒ λinstr. ticks
980          | JZ   relative          ⇒ λinstr. ticks
981          | JNZ  relative          ⇒ λinstr. ticks
982          | CJNE src_trgt relative ⇒ λinstr. ticks
983          | DJNZ src_trgt relative ⇒ λinstr. ticks
984          | _                      ⇒ λinstr.
985         
986              ticks + block_cost' code_memory' program_counter'' program_size' total_program_size cost_labels ? good_program_witness false ?
987          ] (refl …)
988        | ACALL addr     ⇒ λinstr.
989            ticks + block_cost' code_memory' program_counter'' program_size' total_program_size cost_labels ? good_program_witness false ?
990        | AJMP  addr     ⇒ λinstr. ticks
991        | LCALL addr     ⇒ λinstr.
992            ticks + block_cost' code_memory' program_counter'' program_size' total_program_size cost_labels ? good_program_witness false ?
993        | LJMP  addr     ⇒ λinstr. ticks
994        | SJMP  addr     ⇒ λinstr. ticks
995        | JMP   addr     ⇒ λinstr. (* XXX: actually a call due to use with fptrs *)
996            ticks + block_cost' code_memory' program_counter'' program_size' total_program_size cost_labels ? good_program_witness false ?
997        | MOVC  src trgt ⇒ λinstr.
998            ticks + block_cost' code_memory' program_counter'' program_size' total_program_size cost_labels ? good_program_witness false ?
999        ] (refl …))
1000      else
1001        0)
1002      : Σcost_of_block: nat.
1003          match (match lookup_opt … program_counter' cost_labels with [ None ⇒ true | _ ⇒ first_time_around ]) with
1004          [ true ⇒
1005            ∀start_status: Status code_memory'.
1006            ∀final_status: Status code_memory'.
1007            ∀trace_ends_flag.
1008            ∀the_trace: trace_any_label (ASM_abstract_status … cost_labels) trace_ends_flag start_status final_status.
1009              program_counter' = program_counter … start_status →
1010                cost_of_block = compute_paid_trace_any_label code_memory' cost_labels trace_ends_flag start_status final_status the_trace
1011          | false ⇒
1012            (cost_of_block = 0)
1013          ])
1014  ].
1015  [1:
1016    cases reachable_program_counter_witness #_ #hyp
1017    @(absurd (total_program_size < total_program_size) … (not_le_Sn_n …))
1018    @(le_to_lt_to_lt … base_case hyp)
1019  |2:
1020    change with (if to_continue then ? else (? = 0))
1021    >p in ⊢ (match % return ? with [ _ ⇒ ? | _ ⇒ ? ]); normalize nodelta
1022    @pi2
1023  |3:
1024    change with (if to_continue then ? else (0 = 0))
1025    >p normalize nodelta %
1026  |7,8:
1027    #start_status #final_status #trace_ends_flag #the_trace #program_counter_refl
1028    @(trace_compute_paid_trace_cl_return … reachable_program_counter_witness good_program_witness … recursive_case … FETCH … the_trace program_counter_refl)
1029    destruct %
1030  |96,102,105:
1031    #start_status #final_status #trace_ends_flag #the_trace #program_counter_refl
1032    cases(block_cost' ?????????) -block_cost'
1033    @(trace_compute_paid_trace_cl_call … reachable_program_counter_witness good_program_witness … recursive_case … FETCH … program_counter_refl)
1034    destruct %
1035  |4,9,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57,69,72,75,78,81,84,87,
1036   90,93:
1037    #start_status #final_status #trace_ends_flag #the_trace #program_counter_refl
1038    cases(block_cost' ?????????) -block_cost'
1039    @(trace_compute_paid_trace_cl_other … reachable_program_counter_witness good_program_witness … recursive_case … FETCH … the_trace program_counter_refl)
1040    destruct %
1041  |60,61,62,63,64,65,66,67,68,69,99,101,100,102:
1042    #start_status #final_status #trace_ends_flag #the_trace #program_counter_refl
1043    @(trace_compute_paid_trace_cl_jump … reachable_program_counter_witness good_program_witness … recursive_case … FETCH … the_trace program_counter_refl)
1044    destruct %
1045  |103:
1046    cases reachable_program_counter_witness * #n #fetch_n_hyp #lt_hyp
1047    lapply(good_program_witness program_counter' reachable_program_counter_witness)
1048    <FETCH normalize nodelta <instr normalize nodelta
1049    @(subaddressing_mode_elim … [[addr16]] … [[addr16]]) [1: // ] #new_addr
1050    * * * * #n'
1051    #_ #_ #program_counter_lt' #program_counter_lt_tps'
1052    %
1053    [1:
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:
1058      assumption
1059    ]
1060  |104:
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    @(transitive_le
1068      total_program_size
1069      ((S program_size') + nat_of_bitvector … program_counter')
1070      (program_size' + nat_of_bitvector … program_counter'') recursive_case)
1071    normalize in match (S program_size' + nat_of_bitvector … program_counter');
1072    >plus_n_Sm
1073    @monotonic_le_plus_r
1074    change with (
1075      nat_of_bitvector … program_counter' <
1076        nat_of_bitvector … program_counter'')
1077    assumption
1078  |106:
1079    cases reachable_program_counter_witness * #n #fetch_n_hyp #lt_hyp
1080    lapply(good_program_witness program_counter' reachable_program_counter_witness)
1081    <FETCH normalize nodelta <instr normalize nodelta
1082    @(subaddressing_mode_elim … [[addr11]] … [[addr11]]) [1: // ] #new_addr
1083    cases (split … 8 8 program_counter'') #pc_bu #pc_bl normalize nodelta
1084    cases (split … 3 8 new_addr) #thr #eig normalize nodelta
1085    cases (split … 5 3 pc_bu) #fiv #thr' normalize nodelta * * * * #n'
1086    #_ #_ #program_counter_lt' #program_counter_lt_tps'
1087    %
1088    [1:
1089      %{(S n)} whd in ⊢ (???%); <fetch_n_hyp normalize nodelta
1090      <FETCH normalize nodelta whd in match ltb; normalize nodelta
1091      >(le_to_leb_true … program_counter_lt') %
1092    |2:
1093      assumption
1094    ]
1095  |107:
1096    cases reachable_program_counter_witness * #n #fetch_n_hyp #lt_hyp
1097    lapply(good_program_witness program_counter' reachable_program_counter_witness)
1098    <FETCH normalize nodelta <instr normalize nodelta
1099    @(subaddressing_mode_elim … [[addr11]] … [[addr11]]) [1: // ] #new_addr
1100    cases (split … 8 8 program_counter'') #pc_bu #pc_bl normalize nodelta
1101    cases (split … 3 8 new_addr) #thr #eig normalize nodelta
1102    cases (split … 5 3 pc_bu) #fiv #thr' normalize nodelta * * * * #n'
1103    #_ #_ #program_counter_lt' #program_counter_lt_tps'
1104    @(transitive_le
1105      total_program_size
1106      ((S program_size') + nat_of_bitvector … program_counter')
1107      (program_size' + nat_of_bitvector … program_counter'') recursive_case)
1108    normalize in match (S program_size' + nat_of_bitvector … program_counter');
1109    >plus_n_Sm
1110    @monotonic_le_plus_r
1111    change with (
1112      nat_of_bitvector … program_counter' <
1113        nat_of_bitvector … program_counter'')
1114    assumption
1115  |94,97:
1116    cases reachable_program_counter_witness * #n #fetch_n_hyp #lt_hyp
1117    lapply(good_program_witness program_counter' reachable_program_counter_witness)
1118    <FETCH normalize nodelta <instr normalize nodelta *
1119    #program_counter_lt' #program_counter_lt_tps' %
1120    [1,3:
1121      %{(S n)} whd in ⊢ (???%); <fetch_n_hyp normalize nodelta
1122      <FETCH normalize nodelta whd in match ltb; normalize nodelta
1123      >(le_to_leb_true … program_counter_lt') %
1124    |2,4:
1125      assumption
1126    ]
1127  |5,10,12,13,16,18,19,22,25,28,31,34,37,40,43,46,49,52,55,58,70,73,
1128   76,79,82,85,88,91:
1129    cases reachable_program_counter_witness * #n #fetch_n_hyp #lt_hyp
1130    lapply(good_program_witness program_counter' reachable_program_counter_witness)
1131    <FETCH normalize nodelta <real_instruction_refl <instr normalize nodelta *
1132    #program_counter_lt' #program_counter_lt_tps' %
1133    try assumption
1134    %{(S n)} whd in ⊢ (???%); <fetch_n_hyp normalize nodelta
1135    <FETCH normalize nodelta whd in match ltb; normalize nodelta
1136    >(le_to_leb_true … program_counter_lt') %
1137  |6,11,14,17,20,23,26,29,32,35,38,41,44,47,50,53,56,59,71,74,77,80,83,86,89,
1138   92:
1139    cases reachable_program_counter_witness * #n #fetch_n_hyp #lt_hyp
1140    lapply(good_program_witness program_counter' reachable_program_counter_witness)
1141    <FETCH normalize nodelta
1142    <real_instruction_refl <instr normalize nodelta *
1143    #pc_pc_lt_hyp' #pc_tps_lt_hyp'
1144    @(transitive_le
1145      total_program_size
1146      ((S program_size') + nat_of_bitvector … program_counter')
1147      (program_size' + nat_of_bitvector … program_counter'') recursive_case)
1148    normalize in match (S program_size' + nat_of_bitvector … program_counter');
1149    >plus_n_Sm
1150    @monotonic_le_plus_r
1151    change with (
1152      nat_of_bitvector … program_counter' <
1153        nat_of_bitvector … program_counter'')
1154    assumption
1155  |95,98:
1156    cases reachable_program_counter_witness * #n #fetch_n_hyp #lt_hyp
1157    lapply(good_program_witness program_counter' reachable_program_counter_witness)
1158    <FETCH normalize nodelta <instr normalize nodelta
1159    try(<real_instruction_refl <instr normalize nodelta) *
1160    #pc_pc_lt_hyp' #pc_tps_lt_hyp'
1161    @(transitive_le
1162      total_program_size
1163      ((S program_size') + nat_of_bitvector … program_counter')
1164      (program_size' + nat_of_bitvector … program_counter'') recursive_case)
1165    normalize in match (S program_size' + nat_of_bitvector … program_counter');
1166    >plus_n_Sm
1167    @monotonic_le_plus_r
1168    change with (
1169      nat_of_bitvector … program_counter' <
1170        nat_of_bitvector … program_counter'')
1171    assumption
1172  ]
1173qed.
1174
1175definition block_cost:
1176    ∀code_memory': BitVectorTrie Byte 16.
1177    ∀program_counter': Word.
1178    ∀total_program_size: nat.
1179    ∀cost_labels: BitVectorTrie costlabel 16.
1180    ∀reachable_program_counter_witness: reachable_program_counter code_memory' total_program_size program_counter'.
1181    ∀good_program_witness: good_program code_memory' total_program_size.
1182      Σcost_of_block: nat.
1183        ∀start_status: Status code_memory'.
1184        ∀final_status: Status code_memory'.
1185        ∀trace_ends_flag.
1186        ∀the_trace: trace_any_label (ASM_abstract_status … cost_labels) trace_ends_flag start_status final_status.
1187          program_counter' = program_counter … start_status →
1188            cost_of_block = compute_paid_trace_any_label code_memory' cost_labels trace_ends_flag start_status final_status the_trace ≝
1189  λcode_memory: BitVectorTrie Byte 16.
1190  λprogram_counter: Word.
1191  λtotal_program_size: nat.
1192  λcost_labels: BitVectorTrie costlabel 16.
1193  λreachable_program_counter_witness: reachable_program_counter code_memory total_program_size program_counter.
1194  λgood_program_witness: good_program code_memory total_program_size. ?.
1195  cases(block_cost' code_memory program_counter total_program_size total_program_size cost_labels
1196    reachable_program_counter_witness good_program_witness true ?)
1197  [1:
1198    #cost_of_block #block_cost_hyp
1199    %{cost_of_block}
1200    cases(lookup_opt … cost_labels) in block_cost_hyp;
1201    [2: #cost_label] normalize nodelta
1202    #hyp assumption
1203  |2:
1204    @le_plus_n_r
1205  ]
1206qed.
1207
1208lemma fetch_program_counter_n_Sn:
1209  ∀instruction: instruction.
1210  ∀program_counter, program_counter': Word.
1211  ∀ticks, n: nat.
1212  ∀code_memory: BitVectorTrie Byte 16.
1213    Some … program_counter = fetch_program_counter_n n code_memory (zero 16) →
1214      〈instruction,program_counter',ticks〉 = fetch code_memory program_counter →
1215        nat_of_bitvector … program_counter < nat_of_bitvector … program_counter' →
1216          Some … program_counter' = fetch_program_counter_n (S n) code_memory (zero …).
1217  #instruction #program_counter #program_counter' #ticks #n #code_memory
1218  #fetch_program_counter_n_hyp #fetch_hyp #lt_hyp
1219  whd in match (fetch_program_counter_n (S n) code_memory (zero …));
1220  <fetch_program_counter_n_hyp normalize nodelta
1221  <fetch_hyp normalize nodelta
1222  change with (
1223    leb (S (nat_of_bitvector … program_counter)) (nat_of_bitvector … program_counter')
1224  ) in match (ltb (nat_of_bitvector … program_counter) (nat_of_bitvector … program_counter'));
1225  >(le_to_leb_true … lt_hyp) %
1226qed.
1227
1228(* XXX: to be moved into common/Identifiers.ma *)
1229lemma lookup_present_add_hit:
1230  ∀tag, A, map, k, v, k_pres.
1231    lookup_present tag A (add … map k v) k k_pres = v.
1232  #tag #a #map #k #v #k_pres
1233  lapply (lookup_lookup_present … (add … map k v) … k_pres)
1234  >lookup_add_hit #Some_assm destruct(Some_assm)
1235  <e0 %
1236qed.
1237
1238lemma lookup_present_add_miss:
1239  ∀tag, A, map, k, k', v, k_pres', k_pres''.
1240    k' ≠ k →
1241      lookup_present tag A (add … map k v) k' k_pres' = lookup_present tag A map k' k_pres''.
1242  #tag #A #map #k #k' #v #k_pres' #k_pres'' #neq_assm
1243  lapply (lookup_lookup_present … (add … map k v) ? k_pres')
1244  >lookup_add_miss try assumption
1245  #Some_assm
1246  lapply (lookup_lookup_present … map k') >Some_assm #Some_assm'
1247  lapply (Some_assm' k_pres'') #Some_assm'' destruct assumption
1248qed.
1249
1250(* XXX: to be moved into basics/types.ma *)
1251lemma not_None_to_Some:
1252  ∀A: Type[0].
1253  ∀o: option A.
1254    o ≠ None A → ∃v: A. o = Some A v.
1255  #A #o cases o
1256  [1:
1257    #absurd cases absurd #absurd' cases (absurd' (refl …))
1258  |2:
1259    #v' #ignore /2/
1260  ]
1261qed.
1262
1263lemma present_add_present:
1264  ∀tag, a, map, k, k', v.
1265    k' ≠ k →
1266      present tag a (add tag a map k v) k' →
1267        present tag a map k'.
1268  #tag #a #map #k #k' #v #neq_hyp #present_hyp
1269  whd in match present; normalize nodelta
1270  whd in match present in present_hyp; normalize nodelta in present_hyp;
1271  cases (not_None_to_Some a … present_hyp) #v' #Some_eq_hyp
1272  lapply (lookup_add_cases tag ?????? Some_eq_hyp) *
1273  [1:
1274    * #k_eq_hyp @⊥ /2/
1275  |2:
1276    #Some_eq_hyp' /2/
1277  ]
1278qed.
1279
1280lemma present_add_hit:
1281  ∀tag, a, map, k, v.
1282    present tag a (add tag a map k v) k.
1283  #tag #a #map #k #v
1284  whd >lookup_add_hit
1285  % #absurd destruct
1286qed.
1287
1288lemma present_add_miss:
1289  ∀tag, a, map, k, k', v.
1290    k' ≠ k → present tag a map k' → present tag a (add tag a map k v) k'.
1291  #tag #a #map #k #k' #v #neq_assm #present_assm
1292  whd >lookup_add_miss assumption
1293qed.
1294
1295lemma lt_to_le_to_le:
1296  ∀n, m, p: nat.
1297    n < m → m ≤ p → n ≤ p.
1298  #n #m #p #H #H1
1299  elim H
1300  [1:
1301    @(transitive_le n m p) /2/
1302  |2:
1303    /2/
1304  ]
1305qed.
1306
1307lemma eqb_decidable:
1308  ∀l, r: nat.
1309    (eqb l r = true) ∨ (eqb l r = false).
1310  #l #r //
1311qed.
1312
1313lemma r_Sr_and_l_r_to_Sl_r:
1314  ∀r, l: nat.
1315    (∃r': nat. r = S r' ∧ l = r') → S l = r.
1316  #r #l #exists_hyp
1317  cases exists_hyp #r'
1318  #and_hyp cases and_hyp
1319  #left_hyp #right_hyp
1320  destruct %
1321qed.
1322
1323lemma eqb_Sn_to_exists_n':
1324  ∀m, n: nat.
1325    eqb (S m) n = true → ∃n': nat. n = S n'.
1326  #m #n
1327  cases n
1328  [1:
1329    normalize #absurd
1330    destruct(absurd)
1331  |2:
1332    #n' #_ %{n'} %
1333  ]
1334qed.
1335
1336lemma eqb_true_to_eqb_S_S_true:
1337  ∀m, n: nat.
1338    eqb m n = true → eqb (S m) (S n) = true.
1339  #m #n normalize #assm assumption
1340qed.
1341
1342lemma eqb_S_S_true_to_eqb_true:
1343  ∀m, n: nat.
1344    eqb (S m) (S n) = true → eqb m n = true.
1345  #m #n normalize #assm assumption
1346qed.
1347
1348lemma eqb_true_to_refl:
1349  ∀l, r: nat.
1350    eqb l r = true → l = r.
1351  #l
1352  elim l
1353  [1:
1354    #r cases r
1355    [1:
1356      #_ %
1357    |2:
1358      #l' normalize
1359      #absurd destruct(absurd)
1360    ]
1361  |2:
1362    #l' #inductive_hypothesis #r
1363    #eqb_refl @r_Sr_and_l_r_to_Sl_r
1364    %{(pred r)} @conj
1365    [1:
1366      cases (eqb_Sn_to_exists_n' … eqb_refl)
1367      #r' #S_assm >S_assm %
1368    |2:
1369      cases (eqb_Sn_to_exists_n' … eqb_refl)
1370      #r' #refl_assm destruct normalize
1371      @inductive_hypothesis
1372      normalize in eqb_refl; assumption
1373    ]
1374  ]
1375qed.
1376
1377lemma r_O_or_exists_r_r_Sr_and_l_neq_r_to_Sl_neq_r:
1378  ∀r, l: nat.
1379    r = O ∨ (∃r': nat. r = S r' ∧ l ≠ r') → S l ≠ r.
1380  #r #l #disj_hyp
1381  cases disj_hyp
1382  [1:
1383    #r_O_refl destruct @nmk
1384    #absurd destruct(absurd)
1385  |2:
1386    #exists_hyp cases exists_hyp #r'
1387    #conj_hyp cases conj_hyp #left_conj #right_conj
1388    destruct @nmk #S_S_refl_hyp
1389    elim right_conj #hyp @hyp //
1390  ]
1391qed.
1392
1393lemma neq_l_r_to_neq_Sl_Sr:
1394  ∀l, r: nat.
1395    l ≠ r → S l ≠ S r.
1396  #l #r #l_neq_r_assm
1397  @nmk #Sl_Sr_assm cases l_neq_r_assm
1398  #assm @assm //
1399qed.
1400
1401lemma eqb_false_to_not_refl:
1402  ∀l, r: nat.
1403    eqb l r = false → l ≠ r.
1404  #l
1405  elim l
1406  [1:
1407    #r cases r
1408    [1:
1409      normalize #absurd destruct(absurd)
1410    |2:
1411      #r' #_ @nmk
1412      #absurd destruct(absurd)
1413    ]
1414  |2:
1415    #l' #inductive_hypothesis #r
1416    cases r
1417    [1:
1418      #eqb_false_assm
1419      @r_O_or_exists_r_r_Sr_and_l_neq_r_to_Sl_neq_r
1420      @or_introl %
1421    |2:
1422      #r' #eqb_false_assm
1423      @neq_l_r_to_neq_Sl_Sr
1424      @inductive_hypothesis
1425      assumption
1426    ]
1427  ]
1428qed.
1429
1430lemma le_to_lt_or_eq:
1431  ∀m, n: nat.
1432    m ≤ n → m = n ∨ m < n.
1433  #m #n #le_hyp
1434  cases le_hyp
1435  [1:
1436    @or_introl %
1437  |2:
1438    #m' #le_hyp'
1439    @or_intror
1440    normalize
1441    @le_S_S assumption
1442  ]
1443qed.
1444
1445lemma le_neq_to_lt:
1446  ∀m, n: nat.
1447    m ≤ n → m ≠ n → m < n.
1448  #m #n #le_hyp #neq_hyp
1449  cases neq_hyp
1450  #eq_absurd_hyp
1451  generalize in match (le_to_lt_or_eq m n le_hyp);
1452  #disj_assm cases disj_assm
1453  [1:
1454    #absurd cases (eq_absurd_hyp absurd)
1455  |2:
1456    #assm assumption
1457  ]
1458qed.
1459
1460inverter nat_jmdiscr for nat.
1461
1462(* XXX: this is false in the general case.  For instance, if n = 0 then the
1463        base case requires us prove 1 = 0, as it is the carry bit that holds
1464        the result of the addition. *)
1465axiom succ_nat_of_bitvector_half_add_1:
1466  ∀n: nat.
1467  ∀bv: BitVector n.
1468  ∀power_proof: nat_of_bitvector … bv < 2^n - 1.
1469    S (nat_of_bitvector … bv) = nat_of_bitvector …
1470      (\snd (half_add n (bitvector_of_nat … 1) bv)).
1471
1472lemma plus_lt_to_lt:
1473  ∀m, n, o: nat.
1474    m + n < o → m < o.
1475  #m #n #o
1476  elim n
1477  [1:
1478    <(plus_n_O m) in ⊢ (% → ?);
1479    #assumption assumption
1480  |2:
1481    #n' #inductive_hypothesis
1482    <(plus_n_Sm m n') in ⊢ (% → ?);
1483    #assm @inductive_hypothesis
1484    normalize in assm; normalize
1485    /2 by lt_S_to_lt/
1486  ]
1487qed.
1488
1489include "arithmetics/div_and_mod.ma".
1490
1491lemma n_plus_1_n_to_False:
1492  ∀n: nat.
1493    n + 1 = n → False.
1494  #n elim n
1495  [1:
1496    normalize #absurd destruct(absurd)
1497  |2:
1498    #n' #inductive_hypothesis normalize
1499    #absurd @inductive_hypothesis /2/
1500  ]
1501qed.
1502
1503lemma one_two_times_n_to_False:
1504  ∀n: nat.
1505    1=2*n→False.
1506  #n cases n
1507  [1:
1508    normalize #absurd destruct(absurd)
1509  |2:
1510    #n' normalize #absurd
1511    lapply (injective_S … absurd) -absurd #absurd
1512    /2/
1513  ]
1514qed.
1515
1516lemma generalized_nat_cases:
1517  ∀n: nat.
1518    n = 0 ∨ n = 1 ∨ ∃m: nat. n = S (S m).
1519  #n
1520  cases n
1521  [1:
1522    @or_introl @or_introl %
1523  |2:
1524    #n' cases n'
1525    [1:
1526      @or_introl @or_intror %
1527    |2:
1528      #n'' @or_intror %{n''} %
1529    ]
1530  ]
1531qed.
1532
1533let rec odd_p
1534  (n: nat)
1535    on n ≝
1536  match n with
1537  [ O ⇒ False
1538  | S n' ⇒ even_p n'
1539  ]
1540and even_p
1541  (n: nat)
1542    on n ≝
1543  match n with
1544  [ O ⇒ True
1545  | S n' ⇒ odd_p n'
1546  ].
1547
1548let rec n_even_p_to_n_plus_2_even_p
1549  (n: nat)
1550    on n: even_p n → even_p (n + 2) ≝
1551  match n with
1552  [ O ⇒ ?
1553  | S n' ⇒ ?
1554  ]
1555and n_odd_p_to_n_plus_2_odd_p
1556  (n: nat)
1557    on n: odd_p n → odd_p (n + 2) ≝
1558  match n with
1559  [ O ⇒ ?
1560  | S n' ⇒ ?
1561  ].
1562  [1,3:
1563    normalize #assm assumption
1564  |2:
1565    normalize @n_odd_p_to_n_plus_2_odd_p
1566  |4:
1567    normalize @n_even_p_to_n_plus_2_even_p
1568  ]
1569qed.
1570
1571let rec two_times_n_even_p
1572  (n: nat)
1573    on n: even_p (2 * n) ≝
1574  match n with
1575  [ O ⇒ ?
1576  | S n' ⇒ ?
1577  ]
1578and two_times_n_plus_one_odd_p
1579  (n: nat)
1580    on n: odd_p ((2 * n) + 1) ≝
1581  match n with
1582  [ O ⇒ ?
1583  | S n' ⇒ ?
1584  ].
1585  [1,3:
1586    normalize @I
1587  |2:
1588    normalize
1589    >plus_n_Sm
1590    <(associative_plus n' n' 1)
1591    >(plus_n_O (n' + n'))
1592    cut(n' + n' + 0 + 1 = 2 * n' + 1)
1593    [1:
1594      //
1595    |2:
1596      #refl_assm >refl_assm
1597      @two_times_n_plus_one_odd_p     
1598    ]
1599  |4:
1600    normalize
1601    >plus_n_Sm
1602    cut(n' + (n' + 1) + 1 = (2 * n') + 2)
1603    [1:
1604      normalize /2/
1605    |2:
1606      #refl_assm >refl_assm
1607      @n_even_p_to_n_plus_2_even_p
1608      @two_times_n_even_p
1609    ]
1610  ]
1611qed.
1612
1613let rec even_p_to_not_odd_p
1614  (n: nat)
1615    on n: even_p n → ¬ odd_p n ≝
1616  match n with
1617  [ O ⇒ ?
1618  | S n' ⇒ ?
1619  ]
1620and odd_p_to_not_even_p
1621  (n: nat)
1622    on n: odd_p n → ¬ even_p n ≝
1623  match n with
1624  [ O ⇒ ?
1625  | S n' ⇒ ?
1626  ].
1627  [1:
1628    normalize #_
1629    @nmk #assm assumption
1630  |3:
1631    normalize #absurd
1632    cases absurd
1633  |2:
1634    normalize
1635    @odd_p_to_not_even_p
1636  |4:
1637    normalize
1638    @even_p_to_not_odd_p
1639  ]
1640qed.
1641
1642lemma even_p_odd_p_cases:
1643  ∀n: nat.
1644    even_p n ∨ odd_p n.
1645  #n elim n
1646  [1:
1647    normalize @or_introl @I
1648  |2:
1649    #n' #inductive_hypothesis
1650    normalize
1651    cases inductive_hypothesis
1652    #assm
1653    try (@or_introl assumption)
1654    try (@or_intror assumption)
1655  ]
1656qed.
1657
1658lemma two_times_n_plus_one_refl_two_times_n_to_False:
1659  ∀m, n: nat.
1660    2 * m + 1 = 2 * n → False.
1661  #m #n
1662  #assm
1663  cut (even_p (2 * n) ∧ even_p ((2 * m) + 1))
1664  [1:
1665    >assm
1666    @conj
1667    @two_times_n_even_p
1668  |2:
1669    * #_ #absurd
1670    cases (even_p_to_not_odd_p … absurd)
1671    #assm @assm
1672    @two_times_n_plus_one_odd_p
1673  ]
1674qed.
1675
1676lemma nat_of_bitvector_aux_injective:
1677  ∀n: nat.
1678  ∀l, r: BitVector n.
1679  ∀acc_l, acc_r: nat.
1680    nat_of_bitvector_aux n acc_l l = nat_of_bitvector_aux n acc_r r →
1681      acc_l = acc_r ∧ l ≃ r.
1682  #n #l
1683  elim l #r
1684  [1:
1685    #acc_l #acc_r normalize
1686    >(BitVector_O r) normalize /2/
1687  |2:
1688    #hd #tl #inductive_hypothesis #r #acc_l #acc_r
1689    normalize normalize in inductive_hypothesis;
1690    cases (BitVector_Sn … r)
1691    #r_hd * #r_tl #r_refl destruct normalize
1692    cases hd cases r_hd normalize
1693    [1:
1694      #relevant
1695      cases (inductive_hypothesis … relevant)
1696      #acc_assm #tl_assm destruct % //
1697      lapply (injective_plus_l ? ? ? acc_assm)
1698      -acc_assm #acc_assm
1699      change with (2 * acc_l = 2 * acc_r) in acc_assm;
1700      lapply (injective_times_r ? ? ? ? acc_assm) /2/
1701    |4:
1702      #relevant
1703      cases (inductive_hypothesis … relevant)
1704      #acc_assm #tl_assm destruct % //
1705      change with (2 * acc_l = 2 * acc_r) in acc_assm;
1706      lapply(injective_times_r ? ? ? ? acc_assm) /2/
1707    |2:
1708      #relevant 
1709      change with ((nat_of_bitvector_aux r (2 * acc_l + 1) tl) =
1710        (nat_of_bitvector_aux r (2 * acc_r) r_tl)) in relevant;
1711      cases (eqb_decidable … (2 * acc_l + 1) (2 * acc_r))
1712      [1:
1713        #eqb_true_assm
1714        lapply (eqb_true_to_refl … eqb_true_assm)
1715        #refl_assm
1716        cases (two_times_n_plus_one_refl_two_times_n_to_False … refl_assm)
1717      |2:
1718        #eqb_false_assm
1719        lapply (eqb_false_to_not_refl … eqb_false_assm)
1720        #not_refl_assm cases not_refl_assm #absurd_assm
1721        cases (inductive_hypothesis … relevant) #absurd
1722        cases (absurd_assm absurd)
1723      ]
1724    |3:
1725      #relevant 
1726      change with ((nat_of_bitvector_aux r (2 * acc_l) tl) =
1727        (nat_of_bitvector_aux r (2 * acc_r + 1) r_tl)) in relevant;
1728      cases (eqb_decidable … (2 * acc_l) (2 * acc_r + 1))
1729      [1:
1730        #eqb_true_assm
1731        lapply (eqb_true_to_refl … eqb_true_assm)
1732        #refl_assm
1733        lapply (sym_eq ? (2 * acc_l) (2 * acc_r + 1) refl_assm)
1734        -refl_assm #refl_assm
1735        cases (two_times_n_plus_one_refl_two_times_n_to_False … refl_assm)
1736      |2:
1737        #eqb_false_assm
1738        lapply (eqb_false_to_not_refl … eqb_false_assm)
1739        #not_refl_assm cases not_refl_assm #absurd_assm
1740        cases (inductive_hypothesis … relevant) #absurd
1741        cases (absurd_assm absurd)
1742      ]
1743    ]
1744  ]
1745qed.
1746
1747lemma nat_of_bitvector_destruct:
1748  ∀n: nat.
1749  ∀l_hd, r_hd: bool.
1750  ∀l_tl, r_tl: BitVector n.
1751    nat_of_bitvector (S n) (l_hd:::l_tl) = nat_of_bitvector (S n) (r_hd:::r_tl) →
1752      l_hd = r_hd ∧ nat_of_bitvector n l_tl = nat_of_bitvector n r_tl.
1753  #n #l_hd #r_hd #l_tl #r_tl
1754  normalize
1755  cases l_hd cases r_hd
1756  normalize
1757  [4:
1758    /2/
1759  |1:
1760    #relevant
1761    cases (nat_of_bitvector_aux_injective … relevant)
1762    #_ #l_r_tl_refl destruct /2/
1763  |2,3:
1764    #relevant
1765    cases (nat_of_bitvector_aux_injective … relevant)
1766    #absurd destruct(absurd)
1767  ]
1768qed.
1769
1770lemma BitVector_cons_injective:
1771  ∀n: nat.
1772  ∀l_hd, r_hd: bool.
1773  ∀l_tl, r_tl: BitVector n.
1774    l_hd = r_hd → l_tl = r_tl → l_hd:::l_tl = r_hd:::r_tl.
1775  #l #l_hd #r_hd #l_tl #r_tl
1776  #l_refl #r_refl destruct %
1777qed.
1778
1779lemma refl_nat_of_bitvector_to_refl:
1780  ∀n: nat.
1781  ∀l, r: BitVector n.
1782    nat_of_bitvector n l = nat_of_bitvector n r → l = r.
1783  #n
1784  elim n
1785  [1:
1786    #l #r
1787    >(BitVector_O l)
1788    >(BitVector_O r)
1789    #_ %
1790  |2:
1791    #n' #inductive_hypothesis #l #r
1792    lapply (BitVector_Sn ? l) #l_hypothesis
1793    lapply (BitVector_Sn ? r) #r_hypothesis
1794    cases l_hypothesis #l_hd #l_tail_hypothesis
1795    cases r_hypothesis #r_hd #r_tail_hypothesis
1796    cases l_tail_hypothesis #l_tl #l_hd_tl_refl
1797    cases r_tail_hypothesis #r_tl #r_hd_tl_refl
1798    destruct #cons_refl
1799    cases (nat_of_bitvector_destruct n' l_hd r_hd l_tl r_tl cons_refl)
1800    #hd_refl #tl_refl
1801    @BitVector_cons_injective try assumption
1802    @inductive_hypothesis assumption
1803  ]
1804qed.
Note: See TracBrowser for help on using the repository browser.