source: src/ASM/ASMCosts.ma @ 1907

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

Fixes to get file to compile

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