source: src/ASM/AssemblyProof.ma @ 865

Last change on this file since 865 was 865, checked in by sacerdot, 9 years ago

Renaming.

File size: 30.7 KB
Line 
1include "ASM/Assembly.ma".
2include "ASM/Interpret.ma".
3
4(* RUSSEL **)
5
6include "basics/jmeq.ma".
7
8notation > "hvbox(a break ≃ b)"
9  non associative with precedence 45
10for @{ 'jmeq ? $a ? $b }.
11
12notation < "hvbox(term 46 a break maction (≃) (≃\sub(t,u)) term 46 b)"
13  non associative with precedence 45
14for @{ 'jmeq $t $a $u $b }.
15
16interpretation "john major's equality" 'jmeq t x u y = (jmeq t x u y).
17
18lemma eq_to_jmeq:
19  ∀A: Type[0].
20  ∀x, y: A.
21    x = y → x ≃ y.
22  //
23qed.
24
25definition inject : ∀A.∀P:A → Prop.∀a.∀p:P a.Σx:A.P x ≝ λA,P,a,p. dp … a p.
26definition eject : ∀A.∀P: A → Prop.(Σx:A.P x) → A ≝ λA,P,c.match c with [ dp w p ⇒ w].
27
28coercion inject nocomposites: ∀A.∀P:A → Prop.∀a.∀p:P a.Σx:A.P x ≝ inject on a:? to Σx:?.?.
29coercion eject nocomposites: ∀A.∀P:A → Prop.∀c:Σx:A.P x.A ≝ eject on _c:Σx:?.? to ?.
30
31axiom VOID: Type[0].
32axiom assert_false: VOID.
33definition bigbang: ∀A:Type[0].False → VOID → A.
34 #A #abs cases abs
35qed.
36
37coercion bigbang nocomposites: ∀A:Type[0].False → ∀v:VOID.A ≝ bigbang on _v:VOID to ?.
38
39lemma sig2: ∀A.∀P:A → Prop. ∀p:Σx:A.P x. P (eject … p).
40 #A #P #p cases p #w #q @q
41qed.
42
43lemma jmeq_to_eq: ∀A:Type[0]. ∀x,y:A. x≃y → x=y.
44 #A #x #y #JMEQ @(jmeq_elim ? x … JMEQ) %
45qed.
46
47coercion jmeq_to_eq: ∀A:Type[0]. ∀x,y:A. ∀p:x≃y.x=y ≝ jmeq_to_eq on _p:?≃? to ?=?.
48
49(* END RUSSELL **)
50
51let rec foldl_strong_internal
52  (A: Type[0]) (P: list A → Type[0]) (l: list A)
53  (H: ∀prefix. ∀hd. ∀tl. l = prefix @ [hd] @ tl → P prefix → P (prefix @ [hd]))
54  (prefix: list A) (suffix: list A) (acc: P prefix) on suffix:
55    l = prefix @ suffix → P(prefix @ suffix) ≝
56  match suffix return λl'. l = prefix @ l' → P (prefix @ l') with
57  [ nil ⇒ λprf. ?
58  | cons hd tl ⇒ λprf. ?
59  ].
60  [ > (append_nil ?)
61    @ acc
62  | applyS (foldl_strong_internal A P l H (prefix @ [hd]) tl ? ?)
63    [ @ (H prefix hd tl prf acc)
64    | applyS prf
65    ]
66  ]
67qed.
68
69definition foldl_strong ≝
70  λA: Type[0].
71  λP: list A → Type[0].
72  λl: list A.
73  λH: ∀prefix. ∀hd. ∀tl. l = prefix @ [hd] @ tl → P prefix → P (prefix @ [hd]).
74  λacc: P [ ].
75    foldl_strong_internal A P l H [ ] l acc (refl …).
76
77definition bit_elim: ∀P: bool → bool. bool ≝
78  λP.
79    P true ∧ P false.
80
81let rec bitvector_elim_internal
82  (n: nat) (P: BitVector n → bool) (m: nat) on m: m ≤ n → BitVector (n - m) → bool ≝
83  match m return λm. m ≤ n → BitVector (n - m) → bool with
84  [ O    ⇒ λprf1. λprefix. P ?
85  | S n' ⇒ λprf2. λprefix. bit_elim (λbit. bitvector_elim_internal n P n' ? ?)
86  ].
87  [ applyS prefix
88  | letin res ≝ (bit ::: prefix)
89    < (minus_S_S ? ?)
90    > (minus_Sn_m ? ?)
91    [ @ res
92    | @ prf2
93    ]
94  | /2/
95  ].
96qed.
97
98definition bitvector_elim ≝
99  λn: nat.
100  λP: BitVector n → bool.
101    bitvector_elim_internal n P n ? ?.
102  [ @ (le_n ?)
103  | < (minus_n_n ?)
104    @ [[ ]]
105  ]
106qed.
107
108axiom vector_associativity_of_append:
109  ∀A: Type[0].
110  ∀n, m, o:  nat.
111  ∀v: Vector A n.
112  ∀q: Vector A m.
113  ∀r: Vector A o.
114    ((v @@ q) @@ r)
115    ≃
116    (v @@ (q @@ r)).
117       
118axiom vector_cons_append:
119  ∀A: Type[0].
120  ∀n: nat.
121  ∀a: A.
122  ∀v: Vector A n.
123    a ::: v = [[ a ]] @@ v.
124
125lemma super_rewrite2:
126 ∀A:Type[0].∀n,m.∀v1: Vector A n.∀v2: Vector A m.
127  ∀P: ∀m. Vector A m → Prop.
128   n=m → v1 ≃ v2 → P n v1 → P m v2.
129 #A #n #m #v1 #v2 #P #EQ <EQ in v2; #V #JMEQ >JMEQ //
130qed.
131
132lemma mem_middle_vector:
133  ∀A: Type[0].
134  ∀m, o: nat.
135  ∀eq: A → A → bool.
136  ∀reflex: ∀a. eq a a = true.
137  ∀p: Vector A m.
138  ∀a: A.
139  ∀r: Vector A o.
140    mem A eq ? (p@@(a:::r)) a = true.
141  # A # M # O # EQ # REFLEX # P # A
142  elim P
143  [ normalize
144    > (REFLEX A)
145    normalize
146    # H
147    %
148  | # NN # AA # PP # IH
149    normalize
150    cases (EQ A AA) //
151     @ IH
152  ]
153qed.
154
155lemma mem_monotonic_wrt_append:
156  ∀A: Type[0].
157  ∀m, o: nat.
158  ∀eq: A → A → bool.
159  ∀reflex: ∀a. eq a a = true.
160  ∀p: Vector A m.
161  ∀a: A.
162  ∀r: Vector A o.
163    mem A eq ? r a = true → mem A eq ? (p @@ r) a = true.
164  # A # M # O # EQ # REFLEX # P # A
165  elim P
166  [ #R #H @H
167  | #NN #AA # PP # IH #R #H
168    normalize
169    cases (EQ A AA)
170    [ normalize %
171    | @ IH @ H
172    ]
173  ]
174qed.
175
176lemma subvector_multiple_append:
177  ∀A: Type[0].
178  ∀o, n: nat.
179  ∀eq: A → A → bool.
180  ∀refl: ∀a. eq a a = true.
181  ∀h: Vector A o.
182  ∀v: Vector A n.
183  ∀m: nat.
184  ∀q: Vector A m.
185    bool_to_Prop (subvector_with A ? ? eq v (h @@ q @@ v)).
186  # A # O # N # EQ # REFLEX # H # V
187  elim V
188  [ normalize
189    # M # V %
190  | # NN # AA # VV # IH # MM # QQ
191    change with (bool_to_Prop (andb ??))
192    cut ((mem A EQ (O + (MM + S NN)) (H@@QQ@@AA:::VV) AA) = true)
193    [
194    | # HH > HH
195      > (vector_cons_append ? ? AA VV)
196      change with (bool_to_Prop (subvector_with ??????))
197      @(super_rewrite2 A ((MM + 1)+ NN) (MM+S NN) ??
198        (λSS.λVS.bool_to_Prop (subvector_with ?? (O+SS) ?? (H@@VS)))
199        ?
200        (vector_associativity_of_append A ? ? ? QQ [[AA]] VV))
201      [ >associative_plus //
202      | @IH ]
203    ]
204    @(mem_monotonic_wrt_append)
205    [ @ REFLEX
206    | @(mem_monotonic_wrt_append)
207      [ @ REFLEX
208      | normalize
209        > REFLEX
210        normalize
211        %
212      ]
213    ]
214qed.
215
216lemma subvector_hd_tl:
217  ∀A: Type[0].
218  ∀o: nat.
219  ∀eq: A → A → bool.
220  ∀refl: ∀a. eq a a = true.
221  ∀h: A.
222  ∀v: Vector A o.
223    bool_to_Prop (subvector_with A ? ? eq v (h ::: v)).
224
225axiom eq_a_reflexive:
226  ∀a. eq_a a a = true.
227 
228(*
229let rec list_addressing_mode_tags_elim
230  (n: nat) (l: Vector addressing_mode_tag (S n)) on l: (l → bool) → bool ≝
231  match l return λx.match x with [O ⇒ λl: Vector … O. bool | S x' ⇒ λl: Vector addressing_mode_tag (S x').
232   (l → bool) → bool ] with
233  [ VEmpty      ⇒  true 
234  | VCons len hd tl ⇒ λP.
235    let process_hd ≝
236      match hd return λhd. ∀P: hd:::tl → bool. bool with
237      [ direct ⇒ λP.bitvector_elim 8 (λx. P (DIRECT x))
238      | indirect ⇒ λP.bit_elim (λx. P (INDIRECT x))
239      | ext_indirect ⇒ λP.bit_elim (λx. P (EXT_INDIRECT x))
240      | registr ⇒ λP.bitvector_elim 3 (λx. P (REGISTER x))
241      | acc_a ⇒ λP.P ACC_A
242      | acc_b ⇒ λP.P ACC_B
243      | dptr ⇒ λP.P DPTR
244      | data ⇒ λP.bitvector_elim 8 (λx. P (DATA x))
245      | data16 ⇒ λP.bitvector_elim 16 (λx. P (DATA16 x))
246      | acc_dptr ⇒ λP.P ACC_DPTR
247      | acc_pc ⇒ λP.P ACC_PC
248      | ext_indirect_dptr ⇒ λP.P EXT_INDIRECT_DPTR
249      | indirect_dptr ⇒ λP.P INDIRECT_DPTR
250      | carry ⇒ λP.P CARRY
251      | bit_addr ⇒ λP.bitvector_elim 8 (λx. P (BIT_ADDR x))
252      | n_bit_addr ⇒ λP.bitvector_elim 8 (λx. P (N_BIT_ADDR x))
253      | relative ⇒ λP.bitvector_elim 8 (λx. P (RELATIVE x))
254      | addr11 ⇒ λP.bitvector_elim 11 (λx. P (ADDR11 x))
255      | addr16 ⇒ λP.bitvector_elim 16 (λx. P (ADDR16 x))
256      ]
257    in
258      andb (process_hd P)
259       (match len return λlen. Vector addressing_mode_tag len → bool with
260         [ O ⇒ λ_.true
261         | S y ⇒ λtl.list_addressing_mode_tags_elim y tl (λaddr.P addr) ] tl)
262  ].
263  [1: @ (execute_1_technical ? ? tl)
264      [ //
265      | @ (subvector_hd_tl addressing_mode_tag (S y) (S len) eq_a eq_a_reflexive)
266      ]
267  ].
268
269definition preinstruction_elim: ∀P: preinstruction [[ relative ]] → bool. bool ≝
270 λP.
271   P (ADD … ACC_A
272   P (DA … ACC_A).lemma jmeq_to_eq: ∀A:Type[0]. ∀x,y:A. x≃y → x=y.
273 #A #x #y #JMEQ @(jmeq_elim ? x … JMEQ) %
274qed.
275 %
276qed.
277 
278
279definition instruction_elim: ∀P: instruction → bool. bool.
280 
281 
282lemma instruction_elim_correct:
283  ∀i: instruction.
284  ∀P: instruction → bool.
285    instruction_elim P = true → ∀j. P j = true.
286 
287lemma test:
288  ∀i: instruction.
289  ∃pc.
290  let assembled ≝ assembly1 i in
291  let code_memory ≝ load_code_memory assembled in
292  let fetched ≝ fetch code_memory pc in
293  let 〈instr_pc, ticks〉 ≝ fetched in
294    \fst instr_pc = i.
295  # INSTR
296  @ (ex_intro ?)
297  [ @ (zero 16)
298  | @ (instruction_elim INSTR)
299  ].
300*)
301 
302(* This establishes the correspondence between pseudo program counters and
303   program counters. It is at the heart of the proof. *)
304(*CSC: code taken from build_maps *)
305definition sigma0: pseudo_assembly_program → option (nat × (nat × (BitVectorTrie Word 16))) ≝
306 λinstr_list.
307  foldl ??
308    (λt. λi.
309       match t with
310       [ None ⇒ None ?
311       | Some ppc_pc_map ⇒
312         let 〈ppc,pc_map〉 ≝ ppc_pc_map in
313         let 〈program_counter, sigma_map〉 ≝ pc_map in
314         let 〈label, i〉 ≝ i in
315          match construct_costs instr_list program_counter (λx. zero ?) (λx. zero ?) (Stub …) i with
316           [ None ⇒ None ?
317           | Some pc_ignore ⇒
318              let 〈pc,ignore〉 ≝ pc_ignore in
319              Some … 〈S ppc,〈pc, insert ? ? (bitvector_of_nat ? ppc) (bitvector_of_nat ? pc) sigma_map〉〉 ]
320       ]) (Some ? 〈0, 〈0, (Stub ? ?)〉〉) (\snd instr_list).
321       
322definition tech_pc_sigma0: pseudo_assembly_program → option nat ≝
323 λinstr_list.
324  match sigma0 instr_list with
325   [ None ⇒ None …
326   | Some result ⇒
327      let 〈ppc,pc_sigma_map〉 ≝ result in
328      let 〈pc, sigma_map〉 ≝ pc_sigma_map in
329       Some … pc ].
330
331definition sigma_safe: pseudo_assembly_program → option (Word → Word) ≝       
332 λinstr_list.
333  match sigma0 instr_list with
334  [ None ⇒ None ?
335  | Some result ⇒
336    let 〈ppc,pc_sigma_map〉 ≝ result in
337    let 〈pc, sigma_map〉 ≝ pc_sigma_map in
338      if gtb pc (2^16) then
339        None ?
340      else
341        Some ? (λx.lookup ?? x sigma_map (zero …)) ].
342
343axiom policy_ok: ∀p. sigma_safe p ≠ None ….
344
345definition sigma: pseudo_assembly_program → Word → Word ≝
346 λp.
347  match sigma_safe p return λr:option (Word → Word). r ≠ None … → Word → Word with
348   [ None ⇒ λabs. ⊥
349   | Some r ⇒ λ_.r] (policy_ok p).
350 cases abs //
351qed.
352
353lemma length_append:
354 ∀A.∀l1,l2:list A.
355  |l1 @ l2| = |l1| + |l2|.
356 #A #l1 elim l1
357  [ //
358  | #hd #tl #IH #l2 normalize <IH //]
359qed.
360
361let rec does_not_occur (id:Identifier) (l:list labelled_instruction) on l: bool ≝
362 match l with
363  [ nil ⇒ true
364  | cons hd tl ⇒ notb (instruction_matches_identifier id hd) ∧ does_not_occur id tl].
365
366lemma does_not_occur_None:
367 ∀id,i,list_instr.
368  does_not_occur id (list_instr@[〈None …,i〉]) =
369  does_not_occur id list_instr.
370 #id #i #list_instr elim list_instr
371  [ % | #hd #tl #IH whd in ⊢ (??%%) >IH %]
372qed.
373
374let rec occurs_exactly_once (id:Identifier) (l:list labelled_instruction) on l : bool ≝
375 match l with
376  [ nil ⇒ false
377  | cons hd tl ⇒
378     if instruction_matches_identifier id hd then
379      does_not_occur id tl
380     else
381      occurs_exactly_once id tl ].
382
383lemma occurs_exactly_once_None:
384 ∀id,i,list_instr.
385  occurs_exactly_once id (list_instr@[〈None …,i〉]) =
386  occurs_exactly_once id list_instr.
387 #id #i #list_instr elim list_instr
388  [ % | #hd #tl #IH whd in ⊢ (??%%) >IH >does_not_occur_None %]
389qed.
390
391coercion bool_to_Prop: ∀b:bool. Prop ≝ bool_to_Prop on _b:bool to Type[0].
392
393lemma index_of_internal_None: ∀i,id,instr_list,n.
394 occurs_exactly_once id (instr_list@[〈None …,i〉]) →
395  index_of_internal ? (instruction_matches_identifier id) instr_list n =
396   index_of_internal ? (instruction_matches_identifier id) (instr_list@[〈None …,i〉]) n.
397 #i #id #instr_list elim instr_list
398  [ #n #abs whd in abs; cases abs
399  | #hd #tl #IH #n whd in ⊢ (% → ??%%); whd in ⊢ (match % with [_ ⇒ ? | _ ⇒ ?] → ?)
400    cases (instruction_matches_identifier id hd) whd in ⊢ (match % with [_ ⇒ ? | _ ⇒ ?] → ??%%)
401    [ #H %
402    | #H @IH whd in H; cases (occurs_exactly_once ??) in H ⊢ %
403      [ #_ % | #abs cases abs ]]]
404qed.
405
406lemma address_of_word_labels_code_mem_None: ∀i,id,instr_list.
407 occurs_exactly_once id (instr_list@[〈None …,i〉]) →
408  address_of_word_labels_code_mem instr_list id =
409  address_of_word_labels_code_mem (instr_list@[〈None …,i〉]) id.
410 #i #id #instr_list #H whd in ⊢ (??%%) whd in ⊢ (??(??%?)(??%?))
411 >(index_of_internal_None … H) %
412qed.
413
414definition build_maps' ≝
415  λpseudo_program.
416  let 〈preamble,instr_list〉 ≝ pseudo_program in
417  let result ≝
418   foldl_strong
419    (option Identifier × pseudo_instruction)
420    (λpre. Σres:((BitVectorTrie Word 16) × (nat × (BitVectorTrie Word 16))).
421      let pre' ≝ 〈preamble,pre〉 in
422      let 〈labels,pc_costs〉 ≝ res in
423      let 〈program_counter,costs〉 ≝ pc_costs in
424       tech_pc_sigma0 pre' = Some … program_counter ∧
425       ∀id. occurs_exactly_once id pre →
426        lookup ?? id labels (zero …) = sigma pre' (address_of_word_labels_code_mem pre id))
427    instr_list
428    (λprefix,i,tl,prf,t.
429      let 〈labels, pc_costs〉 ≝ t in
430      let 〈program_counter, costs〉 ≝ pc_costs in
431       let 〈label, i'〉 ≝ i in
432       let labels ≝
433         match label with
434         [ None ⇒ labels
435         | Some label ⇒
436           let program_counter_bv ≝ bitvector_of_nat ? program_counter in
437             insert ? ? label program_counter_bv labels
438         ]
439       in
440         match construct_costs pseudo_program program_counter (λx. zero ?) (λx. zero ?) costs i' with
441         [ None ⇒
442            let dummy ≝ 〈labels,pc_costs〉 in
443             dummy
444         | Some construct ⇒ 〈labels, construct〉
445         ]
446    ) 〈(Stub ? ?), 〈0, (Stub ? ?)〉〉
447  in
448   let 〈labels, pc_costs〉 ≝ result in
449   let 〈pc, costs〉 ≝ pc_costs in
450    〈labels, costs〉.
451 [ whd cases construct in p3 #PC #CODE #JMEQ whd %
452    [
453    | #id #Hid
454      generalize in match (sig2 … t) whd in ⊢ (% → ?)
455      >p whd in ⊢ (% → ?) >p1 whd in ⊢ (% → ?) * #IH0 #IH1
456      whd in ⊢ (??(????%?)?) -labels1;
457      cases label in Hid
458       [ #Hid whd in ⊢ (??(????%?)?) >IH1 -IH1
459          [ >(address_of_word_labels_code_mem_None … Hid)
460            (* MANCA LEMMA: INDIRIZZO TROVATO NEL PROGRAMMA! *)
461          | whd in Hid >occurs_exactly_once_None in Hid // ]
462       | -label #label #Hid whd in ⊢ (??(????%?)?)
463     
464       ]]
465 | (* dummy case *)
466 | whd % // #pc normalize in ⊢ (% → ?) #abs @⊥ // ]
467qed.
468
469(*
470(*
471notation < "hvbox('let' 〈ident x,ident y〉 ≝ t 'in' s)"
472 with precedence 10
473for @{ match $t with [ pair ${ident x} ${ident y} ⇒ $s ] }.
474*)
475
476lemma build_maps_ok:
477 ∀p:pseudo_assembly_program.
478  let 〈labels,costs〉 ≝ build_maps' p in
479   ∀pc.
480    (nat_of_bitvector … pc) < length … (\snd p) →
481     lookup ?? pc labels (zero …) = sigma p (\snd (fetch_pseudo_instruction (\snd p) pc)).
482 #p cases p #preamble #instr_list
483  elim instr_list
484   [ whd #pc #abs normalize in abs; cases (not_le_Sn_O ?) [#H cases (H abs) ]
485   | #hd #tl #IH
486    whd in ⊢ (match % with [ _ ⇒ ?])
487   ]
488qed.
489*)
490
491(*
492lemma list_elim_rev:
493 ∀A:Type[0].∀P:list A → Prop.
494  P [ ] → (∀n,l. length l = n → P l → 
495  P [ ] → (∀l,a. P l → P (l@[a])) →
496   ∀l. P l.
497 #A #P
498qed.*)
499
500lemma rev_preserves_length:
501 ∀A.∀l. length … (rev A l) = length … l.
502  #A #l elim l
503   [ %
504   | #hd #tl #IH normalize >length_append normalize /2/ ]
505qed.
506
507lemma rev_append:
508 ∀A.∀l1,l2.
509  rev A (l1@l2) = rev A l2 @ rev A l1.
510 #A #l1 elim l1 normalize //
511qed.
512 
513lemma rev_rev: ∀A.∀l. rev … (rev A l) = l.
514 #A #l elim l
515  [ //
516  | #hd #tl #IH normalize >rev_append normalize // ]
517qed.
518
519lemma split_len_Sn:
520 ∀A:Type[0].∀l:list A.∀len.
521  length … l = S len →
522   Σl'.Σa. l = l'@[a] ∧ length … l' = len.
523 #A #l elim l
524  [ normalize #len #abs destruct
525  | #hd #tl #IH #len
526    generalize in match (rev_rev … tl)
527    cases (rev A tl) in ⊢ (??%? → ?)
528     [ #H <H normalize #EQ % [@[ ]] % [@hd] normalize /2/ 
529     | #a #l' #H <H normalize #EQ
530      %[@(hd::rev … l')] %[@a] % //
531      >length_append in EQ #EQ normalize in EQ; normalize;
532      generalize in match (injective_S … EQ) #EQ2 /2/ ]]
533qed.
534
535lemma list_elim_rev:
536 ∀A:Type[0].∀P:list A → Type[0].
537  P [ ] → (∀l,a. P l → P (l@[a])) →
538   ∀l. P l.
539 #A #P #H1 #H2 #l
540 generalize in match (refl … (length … l))
541 generalize in ⊢ (???% → ?) #n generalize in match l
542 elim n
543  [ #L cases L [ // | #x #w #abs (normalize in abs) @⊥ // ]
544  | #m #IH #L #EQ
545    cases (split_len_Sn … EQ) #l' * #a * /3/ ]
546qed.
547
548axiom is_prefix: ∀A:Type[0]. list A → list A → Prop.
549axiom prefix_of_append:
550 ∀A:Type[0].∀l,l1,l2:list A.
551  is_prefix … l l1 → is_prefix … l (l1@l2).
552axiom prefix_reflexive: ∀A,l. is_prefix A l l.
553axiom nil_prefix: ∀A,l. is_prefix A [ ] l.
554
555record Propify (A:Type[0]) : Type[0] (*Prop*) ≝ { in_propify: A }.
556
557definition Propify_elim: ∀A. ∀P:Prop. (A → P) → (Propify A → P) ≝
558 λA,P,H,x. match x with [ mk_Propify p ⇒ H p ].
559
560definition app ≝
561 λA:Type[0].λl1:Propify (list A).λl2:list A.
562  match l1 with
563   [ mk_Propify l1 ⇒ mk_Propify … (l1@l2) ].
564
565lemma app_nil: ∀A,l1. app A l1 [ ] = l1.
566 #A * /3/
567qed.
568
569lemma app_assoc: ∀A,l1,l2,l3. app A (app A l1 l2) l3 = app A l1 (l2@l3).
570 #A * #l1 normalize //
571qed.
572
573let rec foldli (A: Type[0]) (B: Propify (list A) → Type[0])
574 (f: ∀prefix. B prefix → ∀x.B (app … prefix [x]))
575 (prefix: Propify (list A)) (b: B prefix) (l: list A) on l :
576 B (app … prefix l) ≝
577  match l with
578  [ nil ⇒ ? (* b *)
579  | cons hd tl ⇒ ? (*foldli A B f (prefix@[hd]) (f prefix b hd) tl*)
580  ].
581 [ applyS b
582 | <(app_assoc ?? [hd]) @(foldli A B f (app … prefix [hd]) (f prefix b hd) tl) ]
583qed.
584
585(*
586let rec foldli (A: Type[0]) (B: list A → Type[0]) (f: ∀prefix. B prefix → ∀x. B (prefix@[x]))
587 (prefix: list A) (b: B prefix) (l: list A) on l : B (prefix@l) ≝
588  match l with
589  [ nil ⇒ ? (* b *)
590  | cons hd tl ⇒
591     ? (*foldli A B f (prefix@[hd]) (f prefix b hd) tl*)
592  ].
593 [ applyS b
594 | applyS (foldli A B f (prefix@[hd]) (f prefix b hd) tl) ]
595qed.
596*)
597
598definition foldll:
599 ∀A:Type[0].∀B: Propify (list A) → Type[0].
600  (∀prefix. B prefix → ∀x. B (app … prefix [x])) →
601   B (mk_Propify … []) → ∀l: list A. B (mk_Propify … l)
602 ≝ λA,B,f. foldli A B f (mk_Propify … [ ]).
603
604axiom is_pprefix: ∀A:Type[0]. Propify (list A) → list A → Prop.
605axiom pprefix_of_append:
606 ∀A:Type[0].∀l,l1,l2.
607  is_pprefix A l l1 → is_pprefix A l (l1@l2).
608axiom pprefix_reflexive: ∀A,l. is_pprefix A (mk_Propify … l) l.
609axiom nil_pprefix: ∀A,l. is_pprefix A (mk_Propify … [ ]) l.
610
611
612axiom foldll':
613 ∀A:Type[0].∀l: list A.
614  ∀B: ∀prefix:Propify (list A). is_pprefix ? prefix l → Type[0].
615  (∀prefix,proof. B prefix proof → ∀x,proof'. B (app … prefix [x]) proof') →
616   B (mk_Propify … [ ]) (nil_pprefix …) → B (mk_Propify … l) (pprefix_reflexive … l).
617 #A #l #B
618 generalize in match (foldll A (λprefix. is_pprefix ? prefix l)) #HH
619 
620 
621  #H #acc
622 @foldll
623  [
624  |
625  ]
626 
627 ≝ λA,B,f. foldli A B f (mk_Propify … [ ]).
628
629
630(*
631record subset (A:Type[0]) (P: A → Prop): Type[0] ≝
632 { subset_wit:> A;
633   subset_proof: P subset_wit
634 }.
635*)
636
637definition build_maps' ≝
638  λpseudo_program.
639  let 〈preamble,instr_list〉 ≝ pseudo_program in
640  let result ≝
641   foldll
642    (option Identifier × pseudo_instruction)
643    (λprefix.
644      Σt:((BitVectorTrie Word 16) × (nat × (BitVectorTrie Word 16))).
645       match prefix return λ_.Prop with [mk_Propify prefix ⇒ tech_pc_sigma0 〈preamble,prefix〉 ≠ None ?])
646    (λprefix,t,i.
647      let 〈labels, pc_costs〉 ≝ t in
648      let 〈program_counter, costs〉 ≝ pc_costs in
649       let 〈label, i'〉 ≝ i in
650       let labels ≝
651         match label with
652         [ None ⇒ labels
653         | Some label ⇒
654           let program_counter_bv ≝ bitvector_of_nat ? program_counter in
655             insert ? ? label program_counter_bv labels
656         ]
657       in
658         match construct_costs pseudo_program program_counter (λx. zero ?) (λx. zero ?) costs i' with
659         [ None ⇒
660            let dummy ≝ 〈labels,pc_costs〉 in
661              dummy
662         | Some construct ⇒ 〈labels, construct〉
663         ]
664    ) 〈(Stub ? ?), 〈0, (Stub ? ?)〉〉 instr_list
665  in
666   let 〈labels, pc_costs〉 ≝ result in
667   let 〈pc, costs〉 ≝ pc_costs in
668    〈labels, costs〉.
669 [
670 | @⊥
671 | normalize % //
672 ]
673qed.
674
675definition build_maps' ≝
676  λpseudo_program.
677  let 〈preamble,instr_list〉 ≝ pseudo_program in
678  let result ≝
679   foldl
680    (Σt:((BitVectorTrie Word 16) × (nat × (BitVectorTrie Word 16))).
681          ∃instr_list_prefix. is_prefix ? instr_list_prefix instr_list ∧
682           tech_pc_sigma0 〈preamble,instr_list_prefix〉 = Some ? (\fst (\snd t)))
683    (Σi:option Identifier × pseudo_instruction. ∀instr_list_prefix.
684          let instr_list_prefix' ≝ instr_list_prefix @ [i] in
685           is_prefix ? instr_list_prefix' instr_list →
686           tech_pc_sigma0 〈preamble,instr_list_prefix'〉 ≠ None ?)
687    (λt: Σt:((BitVectorTrie Word 16) × (nat × (BitVectorTrie Word 16))).
688          ∃instr_list_prefix. is_prefix ? instr_list_prefix instr_list ∧
689           tech_pc_sigma0 〈preamble,instr_list_prefix〉 = Some ? (\fst (\snd t)).
690     λi: Σi:option Identifier × pseudo_instruction. ∀instr_list_prefix.
691          let instr_list_prefix' ≝ instr_list_prefix @ [i] in
692           is_prefix ? instr_list_prefix' instr_list →
693           tech_pc_sigma0 〈preamble,instr_list_prefix'〉 ≠ None ? .
694      let 〈labels, pc_costs〉 ≝ t in
695      let 〈program_counter, costs〉 ≝ pc_costs in
696       let 〈label, i'〉 ≝ i in
697       let labels ≝
698         match label with
699         [ None ⇒ labels
700         | Some label ⇒
701           let program_counter_bv ≝ bitvector_of_nat ? program_counter in
702             insert ? ? label program_counter_bv labels
703         ]
704       in
705         match construct_costs pseudo_program program_counter (λx. zero ?) (λx. zero ?) costs i' with
706         [ None ⇒
707            let dummy ≝ 〈labels,pc_costs〉 in
708              dummy
709         | Some construct ⇒ 〈labels, construct〉
710         ]
711    ) 〈(Stub ? ?), 〈0, (Stub ? ?)〉〉 ?(*instr_list*)
712  in
713   let 〈labels, pc_costs〉 ≝ result in
714   let 〈pc, costs〉 ≝ pc_costs in
715    〈labels, costs〉.
716 [4: @(list_elim_rev ?
717       (λinstr_list. list (
718        (Σi:option Identifier × pseudo_instruction. ∀instr_list_prefix.
719          let instr_list_prefix' ≝ instr_list_prefix @ [i] in
720           is_prefix ? instr_list_prefix' instr_list →
721           tech_pc_sigma0 〈preamble,instr_list_prefix'〉 ≠ None ?)))
722       ?? instr_list) (* CSC: BAD ORDER FOR CODE EXTRACTION *)
723      [ @[ ]
724      | #l' #a #limage %2
725        [ %[@a] #PREFIX #PREFIX_OK
726        | (* CSC: EVEN WORST CODE FOR EXTRACTION: WE SHOULD STRENGTHEN
727             THE INDUCTION HYPOTHESIS INSTEAD *)
728          elim limage
729           [ %1
730           | #HD #TL #IH @(?::IH) cases HD #ELEM #K1 %[@ELEM] #K2 #K3
731             @K1 @(prefix_of_append ???? K3)
732           ] 
733        ]
734       
735       
736     
737 
738  cases t in c2 ⊢ % #t' * #LIST_PREFIX * #H1t' #H2t' #HJMt'
739     % [@ (LIST_PREFIX @ [i])] %
740      [ cases (sig2 … i LIST_PREFIX) #K1 #K2 @K1
741      | (* DOABLE IN PRINCIPLE *)
742      ]
743 | (* assert false case *)
744 |3: % [@ ([ ])] % [2: % | (* DOABLE *)]
745 |   
746
747let rec encoding_check (code_memory: BitVectorTrie Byte 16) (pc: Word) (final_pc: Word)
748                       (encoding: list Byte) on encoding: Prop ≝
749  match encoding with
750  [ nil ⇒ final_pc = pc
751  | cons hd tl ⇒
752    let 〈new_pc, byte〉 ≝ next code_memory pc in
753      hd = byte ∧ encoding_check code_memory new_pc final_pc tl
754  ].
755
756definition assembly_specification:
757  ∀assembly_program: pseudo_assembly_program.
758  ∀code_mem: BitVectorTrie Byte 16. Prop ≝
759  λpseudo_assembly_program.
760  λcode_mem.
761    ∀pc: Word.
762      let 〈preamble, instr_list〉 ≝ pseudo_assembly_program in
763      let 〈pre_instr, pre_new_pc〉 ≝ fetch_pseudo_instruction instr_list pc in
764      let labels ≝ λx. sigma' pseudo_assembly_program (address_of_word_labels_code_mem instr_list x) in
765      let datalabels ≝ λx. sigma' pseudo_assembly_program (lookup ? ? x (construct_datalabels preamble) (zero ?)) in
766      let pre_assembled ≝ assembly_1_pseudoinstruction pseudo_assembly_program
767       (sigma' pseudo_assembly_program pc) labels datalabels pre_instr in
768      match pre_assembled with
769       [ None ⇒ True
770       | Some pc_code ⇒
771          let 〈new_pc,code〉 ≝ pc_code in
772           encoding_check code_mem pc (sigma' pseudo_assembly_program pre_new_pc) code ].
773
774axiom assembly_meets_specification:
775  ∀pseudo_assembly_program.
776    match assembly pseudo_assembly_program with
777    [ None ⇒ True
778    | Some code_mem_cost ⇒
779      let 〈code_mem, cost〉 ≝ code_mem_cost in
780        assembly_specification pseudo_assembly_program (load_code_memory code_mem)
781    ].
782(*
783  # PROGRAM
784  [ cases PROGRAM
785    # PREAMBLE
786    # INSTR_LIST
787    elim INSTR_LIST
788    [ whd
789      whd in ⊢ (∀_. %)
790      # PC
791      whd
792    | # INSTR
793      # INSTR_LIST_TL
794      # H
795      whd
796      whd in ⊢ (match % with [ _ ⇒ ? | _ ⇒ ?])
797    ]
798  | cases not_implemented
799  ] *)
800
801definition status_of_pseudo_status: PseudoStatus → option Status ≝
802 λps.
803  let pap ≝ code_memory … ps in
804   match assembly pap with
805    [ None ⇒ None …
806    | Some p ⇒
807       let cm ≝ load_code_memory (\fst p) in
808       let pc ≝ sigma' pap (program_counter ? ps) in
809        Some …
810         (mk_PreStatus (BitVectorTrie Byte 16)
811           cm
812           (low_internal_ram … ps)
813           (high_internal_ram … ps)
814           (external_ram … ps)
815           pc
816           (special_function_registers_8051 … ps)
817           (special_function_registers_8052 … ps)
818           (p1_latch … ps)
819           (p3_latch … ps)
820           (clock … ps)) ].
821
822definition write_at_stack_pointer':
823 ∀M. ∀ps: PreStatus M. Byte → Σps':PreStatus M.(code_memory … ps = code_memory … ps') ≝
824  λM: Type[0].
825  λs: PreStatus M.
826  λv: Byte.
827    let 〈 nu, nl 〉 ≝ split … 4 4 (get_8051_sfr ? s SFR_SP) in
828    let bit_zero ≝ get_index_v… nu O ? in
829    let bit_1 ≝ get_index_v… nu 1 ? in
830    let bit_2 ≝ get_index_v… nu 2 ? in
831    let bit_3 ≝ get_index_v… nu 3 ? in
832      if bit_zero then
833        let memory ≝ insert … ([[ bit_1 ; bit_2 ; bit_3 ]] @@ nl)
834                              v (low_internal_ram ? s) in
835          set_low_internal_ram ? s memory
836      else
837        let memory ≝ insert … ([[ bit_1 ; bit_2 ; bit_3 ]] @@ nl)
838                              v (high_internal_ram ? s) in
839          set_high_internal_ram ? s memory.
840  [ cases l0 %
841  |2,3,4,5: normalize repeat (@ le_S_S) @ le_O_n ]
842qed.
843
844definition execute_1_pseudo_instruction': (Word → nat) → ∀ps:PseudoStatus.
845 Σps':PseudoStatus.(code_memory … ps = code_memory … ps')
846
847  λticks_of.
848  λs.
849  let 〈instr, pc〉 ≝ fetch_pseudo_instruction (\snd (code_memory ? s)) (program_counter ? s) in
850  let ticks ≝ ticks_of (program_counter ? s) in
851  let s ≝ set_clock ? s (clock ? s + ticks) in
852  let s ≝ set_program_counter ? s pc in
853    match instr with
854    [ Instruction instr ⇒
855       execute_1_preinstruction … (λx, y. address_of_word_labels y x) instr s
856    | Comment cmt ⇒ s
857    | Cost cst ⇒ s
858    | Jmp jmp ⇒ set_program_counter ? s (address_of_word_labels s jmp)
859    | Call call ⇒
860      let a ≝ address_of_word_labels s call in
861      let 〈carry, new_sp〉 ≝ half_add ? (get_8051_sfr ? s SFR_SP) (bitvector_of_nat 8 1) in
862      let s ≝ set_8051_sfr ? s SFR_SP new_sp in
863      let 〈pc_bu, pc_bl〉 ≝ split ? 8 8 (program_counter ? s) in
864      let s ≝ write_at_stack_pointer' ? s pc_bl in
865      let 〈carry, new_sp〉 ≝ half_add ? (get_8051_sfr ? s SFR_SP) (bitvector_of_nat 8 1) in
866      let s ≝ set_8051_sfr ? s SFR_SP new_sp in
867      let s ≝ write_at_stack_pointer' ? s pc_bu in
868        set_program_counter ? s a
869    | Mov dptr ident ⇒
870       set_arg_16 ? s (get_arg_16 ? s (DATA16 (address_of_word_labels s ident))) dptr
871    ].
872 [
873 |2,3,4: %
874 | <(sig2 … l7) whd in ⊢ (??? (??%)) <(sig2 … l5) %
875 |
876 | %
877 ]
878 cases not_implemented
879qed.
880
881(*
882lemma execute_code_memory_unchanged:
883 ∀ticks_of,ps. code_memory ? ps = code_memory ? (execute_1_pseudo_instruction ticks_of ps).
884 #ticks #ps whd in ⊢ (??? (??%))
885 cases (fetch_pseudo_instruction (\snd (code_memory pseudo_assembly_program ps))
886  (program_counter pseudo_assembly_program ps)) #instr #pc
887 whd in ⊢ (??? (??%)) cases instr
888  [ #pre cases pre
889     [ #a1 #a2 whd in ⊢ (??? (??%)) cases (add_8_with_carry ???) #y1 #y2 whd in ⊢ (??? (??%))
890       cases (split ????) #z1 #z2 %
891     | #a1 #a2 whd in ⊢ (??? (??%)) cases (add_8_with_carry ???) #y1 #y2 whd in ⊢ (??? (??%))
892       cases (split ????) #z1 #z2 %
893     | #a1 #a2 whd in ⊢ (??? (??%)) cases (sub_8_with_carry ???) #y1 #y2 whd in ⊢ (??? (??%))
894       cases (split ????) #z1 #z2 %
895     | #a1 whd in ⊢ (??? (??%)) cases a1 #x #H whd in ⊢ (??? (??%)) cases x
896       [ #x1 whd in ⊢ (??? (??%))
897     | *: cases not_implemented
898     ]
899  | #comment %
900  | #cost %
901  | #label %
902  | #label whd in ⊢ (??? (??%)) cases (half_add ???) #x1 #x2 whd in ⊢ (??? (??%))
903    cases (split ????) #y1 #y2 whd in ⊢ (??? (??%)) cases (half_add ???) #z1 #z2
904    whd in ⊢ (??? (??%)) whd in ⊢ (??? (??%)) cases (split ????) #w1 #w2
905    whd in ⊢ (??? (??%)) cases (get_index_v bool ????) whd in ⊢ (??? (??%))
906    (* CSC: ??? *)
907  | #dptr #label (* CSC: ??? *)
908  ]
909  cases not_implemented
910qed.
911*)
912
913lemma status_of_pseudo_status_failure_depends_only_on_code_memory:
914 ∀ps,ps': PseudoStatus.
915  code_memory … ps = code_memory … ps' →
916   match status_of_pseudo_status ps with
917    [ None ⇒ status_of_pseudo_status ps' = None …
918    | Some _ ⇒ ∃w. status_of_pseudo_status ps' = Some … w
919    ].
920 #ps #ps' #H whd in ⊢ (mat
921 ch % with [ _ ⇒ ? | _ ⇒ ? ])
922 generalize in match (refl … (assembly (code_memory … ps)))
923 cases (assembly ?) in ⊢ (???% → %)
924  [ #K whd whd in ⊢ (??%?) <H >K %
925  | #x #K whd whd in ⊢ (?? (λ_.??%?)) <H >K % [2: % ] ]
926qed.*)
927
928let rec encoding_check' (code_memory: BitVectorTrie Byte 16) (pc: Word) (encoding: list Byte) on encoding: Prop ≝
929  match encoding with
930  [ nil ⇒ True
931  | cons hd tl ⇒
932    let 〈new_pc, byte〉 ≝ next code_memory pc in
933      hd = byte ∧ encoding_check' code_memory new_pc tl
934  ].
935
936(* prove later *)
937axiom test:
938  ∀pc: Word.
939  ∀code_memory: BitVectorTrie Byte 16.
940  ∀i: instruction.
941    let assembled ≝ assembly1 i in
942      encoding_check' code_memory pc assembled →
943        let 〈instr_pc, ignore〉 ≝ fetch code_memory pc in
944        let 〈instr, pc〉 ≝ instr_pc in
945          instr = i.
946 
947lemma main_thm:
948 ∀ticks_of.
949 ∀ps: PseudoStatus.
950  match status_of_pseudo_status ps with [ None ⇒ True | Some s ⇒
951  let ps' ≝ execute_1_pseudo_instruction ticks_of ps in
952  match status_of_pseudo_status ps' with [ None ⇒ True | Some s'' ⇒
953  let s' ≝ execute_1 s in
954   s = s'']].
955 #ticks_of #ps
956 whd in ⊢ (match % with [ _ ⇒ ? | _ ⇒ ? ])
957 cases (assembly (code_memory pseudo_assembly_program ps)) [%] * #cm #costs whd
958 whd in ⊢ (match % with [ _ ⇒ ? | _ ⇒ ? ])
959 generalize in match (sig2 … (execute_1_pseudo_instruction' ticks_of ps))
960 
961 cases (status_of_pseudo_status (execute_1_pseudo_instruction ticks_of ps)) [%] #s'' whd
Note: See TracBrowser for help on using the repository browser.