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