source: src/ASM/Policy.ma @ 2270

Last change on this file since 2270 was 2264, checked in by sacerdot, 7 years ago

1) Major change: we now always use the efficient way of resolving labels

for the interpretation of pseudo assembly code. The inefficient way is
only used locally in ASM/Interpret.ma to prove some properties more easily.

2) AssemblyProofSplitSplit?.ma partially repaired. In particular, the main

lemma is now called correctly thanks to the previous change.

File size: 34.3 KB
Line 
1include "ASM/PolicyStep.ma".
2
3include alias "basics/lists/list.ma".
4include alias "arithmetics/nat.ma".
5include alias "basics/logic.ma".
6
7let rec jump_expansion_internal (program: Σl:list labelled_instruction.
8  lt (S (|l|)) 2^16 ∧ is_well_labelled_p l) (n: ℕ)
9  on n:(Σx:bool × (option ppc_pc_map).
10    let 〈no_ch,pol〉 ≝ x in
11    match pol with
12    [ None ⇒ True
13    | Some x ⇒
14      And (And (And (And
15        (not_jump_default program x)
16        (\fst (bvt_lookup … (bitvector_of_nat ? 0) (\snd x) 〈0,short_jump〉) = 0))
17        (\fst x = \fst (bvt_lookup … (bitvector_of_nat ? (|program|)) (\snd x) 〈0,short_jump〉)))
18        (sigma_compact_unsafe program (create_label_map program) x))
19        (\fst x ≤ 2^16)
20    ]) ≝
21 let labels ≝ create_label_map program in
22  match n return λx.n = x → Σa:bool × (option ppc_pc_map).? with
23  [ O   ⇒ λp.〈false,pi1 ?? (jump_expansion_start program labels)〉
24  | S m ⇒ λp.let 〈no_ch,z〉 as p1 ≝ (pi1 ?? (jump_expansion_internal program m)) in
25          match z return λx. z=x → Σa:bool × (option ppc_pc_map).? with
26          [ None    ⇒ λp2.〈false,None ?〉
27          | Some op ⇒ λp2.if no_ch
28            then pi1 ?? (jump_expansion_internal program m)
29            else pi1 ?? (jump_expansion_step program (pi1 ?? labels) «op,?»)
30          ] (refl … z)
31  ] (refl … n).
32[5: #l #_ %
33| normalize nodelta cases (jump_expansion_start program (create_label_map program))
34  #x cases x -x
35  [ #H %
36  | #sigma normalize nodelta #H @conj [ @conj
37    [ @(proj1 ?? (proj1 ?? (proj1 ?? H)))
38    | @(proj2 ?? (proj1 ?? (proj1 ?? H)))
39    ]
40    | @(proj2 ?? H)
41    ]
42  ]
43| %
44| cases no_ch in p1; #p1
45  [ @(pi2 ?? (jump_expansion_internal program m))
46  | cases (jump_expansion_step ???)
47    #x cases x -x #ch2 #z2 cases z2 normalize nodelta
48    [ #_ %
49    | #j2 #H2 @conj [ @conj
50      [ @(proj1 ?? (proj1 ?? (proj1 ?? (proj1 ?? (proj1 ?? H2)))))
51      | @(proj2 ?? (proj1 ?? (proj1 ?? (proj1 ?? H2))))
52      ]
53      | @(proj2 ?? H2)
54      ]
55    ]
56  ]
57| cases (jump_expansion_internal program m) in p1;
58  #p cases p -p #p #r cases r normalize nodelta
59  [ #_ >p2 #ABS destruct (ABS)
60  | #map >p2 normalize nodelta
61    #H #eq destruct (eq) @H
62  ]
63]
64qed.
65
66 
67lemma pe_int_refl: ∀program.reflexive ? (sigma_jump_equal program).
68#program whd #x whd #n #Hn
69cases (bvt_lookup … (bitvector_of_nat 16 n) (\snd x) 〈0,short_jump〉)
70#y #z normalize nodelta @refl
71qed.
72
73lemma pe_int_sym: ∀program.symmetric ? (sigma_jump_equal program).
74#program whd #x #y #Hxy whd #n #Hn
75lapply (Hxy n Hn) cases (bvt_lookup … (bitvector_of_nat ? n) (\snd x) 〈0,short_jump〉)
76#x1 #x2
77cases (bvt_lookup … (bitvector_of_nat ? n) (\snd y) 〈0,short_jump〉)
78#y1 #y2 normalize nodelta //
79qed.
80 
81lemma pe_int_trans: ∀program.transitive ? (sigma_jump_equal program).
82#program whd #x #y #z whd in match (sigma_jump_equal ???); whd in match (sigma_jump_equal ?y ?);
83whd in match (sigma_jump_equal ? x z); #Hxy #Hyz #n #Hn lapply (Hxy n Hn) -Hxy
84lapply (Hyz n Hn) -Hyz cases (bvt_lookup … (bitvector_of_nat ? n) (\snd x) 〈0,short_jump〉)
85#x1 #x2
86cases (bvt_lookup … (bitvector_of_nat ? n) (\snd y) 〈0,short_jump〉) #y1 #y2
87cases (bvt_lookup … (bitvector_of_nat ? n) (\snd z) 〈0,short_jump〉) #z1 #z2
88normalize nodelta //
89qed.
90
91definition policy_equal_opt ≝
92  λprogram:list labelled_instruction.λp1,p2:option ppc_pc_map.
93  match p1 with
94  [ Some x1 ⇒ match p2 with
95              [ Some x2 ⇒ sigma_jump_equal program x1 x2
96              | _       ⇒ False
97              ]
98  | None    ⇒ p2 = None ?
99  ].
100
101lemma pe_refl: ∀program.reflexive ? (policy_equal_opt program).
102#program whd #x whd cases x try % #y @pe_int_refl
103qed.
104
105lemma pe_sym: ∀program.symmetric ? (policy_equal_opt program).
106#program whd #x #y #Hxy whd cases y in Hxy;
107[ cases x
108  [ #_ %
109  | #x' #H @⊥ @(absurd ? H) /2 by nmk/
110  ]
111| #y' cases x
112  [ #H @⊥ @(absurd ? H) whd in match (policy_equal_opt ???); @nmk #H destruct (H)
113  | #x' #H @pe_int_sym @H
114  ]
115]
116qed.
117
118lemma pe_trans: ∀program.transitive ? (policy_equal_opt program).
119#program whd #x #y #z cases x
120[ #Hxy #Hyz >Hxy in Hyz; //
121| #x' cases y
122  [ #H @⊥ @(absurd ? H) /2 by nmk/
123  | #y' cases z
124    [ #_ #H @⊥ @(absurd ? H) /2 by nmk/
125    | #z' @pe_int_trans
126    ]
127  ]
128]
129qed.
130
131definition step_none: ∀program.∀n.
132  (\snd (pi1 ?? (jump_expansion_internal program n))) = None ? →
133  ∀k.(\snd (pi1 ?? (jump_expansion_internal program (n+k)))) = None ?.
134#program #n lapply (refl ? (jump_expansion_internal program n))
135 cases (jump_expansion_internal program n) in ⊢ (???% → %);
136 #x1 cases x1 #p1 #j1 -x1; #H1 #Heqj #Hj #k elim k
137[ <plus_n_O >Heqj @Hj
138| #k' -k <plus_n_Sm
139  lapply (refl ? (jump_expansion_internal program (n+k')))
140  cases (jump_expansion_internal program (n+k')) in ⊢ (???% → %);
141  #x2 cases x2 -x2 #c2 #p2 normalize nodelta #H #Heqj2
142  cases p2 in H Heqj2;
143  [ #H #Heqj2 #_ whd in match (jump_expansion_internal ??);
144    >Heqj2 normalize nodelta @refl
145  | #x #H #Heqj2 #abs destruct (abs)
146  ]
147]
148qed.
149
150lemma jump_pc_equal: ∀program.∀n.
151  match \snd (jump_expansion_internal program n) with
152  [ None   ⇒ True
153  | Some p1 ⇒ match \snd (jump_expansion_internal program (S n)) with
154    [ None ⇒ True
155    | Some p2 ⇒ sigma_jump_equal program p1 p2 → sigma_pc_equal program p1 p2
156    ]
157  ].
158 #program #n lapply (refl ? (jump_expansion_internal program n))
159 cases (jump_expansion_internal program n) in ⊢ (???% → %); #x cases x -x
160 #Nno_ch #No cases No
161 [ normalize nodelta #HN #NEQ @I
162 | #Npol normalize nodelta #HN #NEQ lapply (refl ? (jump_expansion_internal program (S n)))
163   cases (jump_expansion_internal program (S n)) in ⊢ (???% → %); #x cases x -x
164   #Sno_ch #So cases So
165   [ normalize nodelta #HS #SEQ @I
166   | #Spol normalize nodelta #HS #SEQ #Hj
167     whd in match (jump_expansion_internal program (S n)) in SEQ; (*80s*)
168     >NEQ in SEQ; normalize nodelta cases Nno_ch in HN;
169     [ #HN normalize nodelta #SEQ >(Some_eq ??? (proj2 ?? (pair_destruct ?????? (pi1_eq ???? SEQ))))
170       / by /
171     | #HN normalize nodelta cases (jump_expansion_step ???)     
172       #x cases x -x #Stno_ch #Stno_o normalize nodelta cases Stno_o
173       [ normalize nodelta #_ #H destruct (H)
174       | #Stno_p normalize nodelta #HSt #STeq
175         <(Some_eq ??? (proj2 ?? (pair_destruct ?????? (pi1_eq ???? STeq)))) in Hj; #Hj
176         @(proj2 ?? (proj1 ?? HSt)) @(proj2 ?? (proj1 ?? (proj1 ?? HSt))) @Hj
177       ]
178     ]
179   ]
180 ]
181qed.     
182
183lemma pe_step: ∀program:(Σl:list labelled_instruction.S (|l|) < 2^16 ∧ is_well_labelled_p l).
184  ∀n.policy_equal_opt program (\snd (pi1 ?? (jump_expansion_internal program n)))
185   (\snd (pi1 ?? (jump_expansion_internal program (S n)))) →
186  policy_equal_opt program (\snd (pi1 ?? (jump_expansion_internal program (S n))))
187    (\snd (pi1 ?? (jump_expansion_internal program (S (S n))))).
188#program #n #Heq cases daemon (* XXX *)
189qed.
190
191lemma equal_remains_equal: ∀program:(Σl:list labelled_instruction.
192  S (|l|) < 2^16 ∧ is_well_labelled_p l).∀n:ℕ.
193  policy_equal_opt program (\snd (pi1 … (jump_expansion_internal program n)))
194   (\snd (pi1 … (jump_expansion_internal program (S n)))) →
195  ∀k.k ≥ n → policy_equal_opt program (\snd (pi1 … (jump_expansion_internal program n)))
196   (\snd (pi1 … (jump_expansion_internal program k))).
197#program #n #Heq #k #Hk elim (le_plus_k … Hk); #z #H >H -H -Hk -k;
198lapply Heq -Heq; lapply n -n; elim z -z;
199[ #n #Heq <plus_n_O @pe_refl
200| #z #Hind #n #Heq <plus_Sn_m1 whd in match (plus (S n) z);
201  @(pe_trans … (\snd (pi1 … (jump_expansion_internal program (S n)))))
202  [ @Heq
203  | @Hind @pe_step @Heq
204  ]
205]
206qed.
207
208(* this number monotonically increases over iterations, maximum 2*|program| *)
209let rec measure_int (program: list labelled_instruction) (policy: ppc_pc_map) (acc: ℕ)
210 on program: ℕ ≝
211 match program with
212 [ nil      ⇒ acc
213 | cons h t ⇒ match (\snd (bvt_lookup ?? (bitvector_of_nat ? (|t|)) (\snd policy) 〈0,short_jump〉)) with
214   [ long_jump   ⇒ measure_int t policy (acc + 2)
215   | absolute_jump ⇒ measure_int t policy (acc + 1)
216   | _           ⇒ measure_int t policy acc
217   ]
218 ].
219
220lemma measure_plus: ∀program.∀policy.∀x,d:ℕ.
221 measure_int program policy (x+d) = measure_int program policy x + d.
222#program #policy #x #d generalize in match x; -x elim d
223[ //
224| -d; #d #Hind elim program
225  [ / by refl/
226  | #h #t #Hd #x whd in match (measure_int ???); whd in match (measure_int ?? x);
227    cases (\snd (bvt_lookup … (bitvector_of_nat ? (|t|)) (\snd policy) 〈0,short_jump〉))
228    [ normalize nodelta @Hd
229    |2,3: normalize nodelta >associative_plus >(commutative_plus (S d) ?) <associative_plus
230      @Hd
231    ]
232  ]
233]
234qed.
235
236lemma measure_le: ∀program.∀policy.
237  measure_int program policy 0 ≤ 2*|program|.
238#program #policy elim program
239[ normalize @le_n
240| #h #t #Hind whd in match (measure_int ???);
241  cases (\snd (lookup ?? (bitvector_of_nat ? (|t|)) (\snd policy) 〈0,short_jump〉))
242  [ normalize nodelta @(transitive_le ??? Hind) /2 by monotonic_le_times_r/
243  |2,3: normalize nodelta >measure_plus <times_n_Sm >(commutative_plus 2 ?)
244    @le_plus [1,3: @Hind |2,4: / by le_n/ ]
245  ]
246]
247qed.
248
249(* uses the second part of policy_increase *)
250lemma measure_incr_or_equal: ∀program:(Σl:list labelled_instruction.
251  S (|l|) <2^16 ∧ is_well_labelled_p l).
252  ∀policy:Σp:ppc_pc_map.
253    (*out_of_program_none program p ∧*)
254    not_jump_default program p ∧
255    \fst (bvt_lookup … (bitvector_of_nat ? 0) (\snd p) 〈0,short_jump〉) = 0 ∧
256    \fst p = \fst (bvt_lookup … (bitvector_of_nat ? (|program|)) (\snd p) 〈0,short_jump〉) ∧
257    sigma_compact_unsafe program (pi1 … (create_label_map program)) p ∧
258    \fst p ≤ 2^16.
259  ∀l.|l| ≤ |program| → ∀acc:ℕ.
260  match \snd (pi1 ?? (jump_expansion_step program (pi1 … (create_label_map program)) policy)) with
261  [ None   ⇒ True
262  | Some p ⇒ measure_int l policy acc ≤ measure_int l p acc
263  ].
264[2: #l #_ %]
265#program #policy #l elim l -l;
266[ #Hp #acc cases (jump_expansion_step ???) #pi1 cases pi1 #p #q -pi1; cases q [ // | #x #_ @le_n ]
267| #h #t #Hind #Hp #acc
268  inversion (jump_expansion_step ???) #pi1 cases pi1 -pi1 #c #r cases r
269  [ / by I/
270  | #x normalize nodelta #Hx #Hjeq
271    lapply (proj2 ?? (proj1 ?? (proj1 ?? (proj1 ?? (proj1 ?? Hx)))) (|t|) (le_S_to_le … Hp))
272    whd in match (measure_int ???); whd in match (measure_int ? x ?);
273    cases (bvt_lookup ?? (bitvector_of_nat ? (|t|)) (\snd (pi1 ?? policy)) 〈0,short_jump〉)
274    #x1 #x2 cases (bvt_lookup ?? (bitvector_of_nat ? (|t|)) (\snd x) 〈0,short_jump〉)
275    #y1 #y2 normalize nodelta #Hblerp cases Hblerp cases x2 cases y2
276    [1,4,5,7,8,9: #H cases H
277    |2,3,6: #_ normalize nodelta
278      [1,2: @(transitive_le ? (measure_int t x acc))
279      |3: @(transitive_le ? (measure_int t x (acc+1)))
280      ]
281      [2,4,5,6: >measure_plus [1,2: @le_plus_n_r] >measure_plus @le_plus / by le_n/]
282      >Hjeq in Hind; #Hind @Hind @(transitive_le … Hp) @le_n_Sn
283    |11,12,13,15,16,17: #H destruct (H)
284    |10,14,18: normalize nodelta #_ >Hjeq in Hind; #Hind @Hind @(transitive_le … Hp) @le_n_Sn
285    ]
286  ]
287]
288qed.
289
290lemma measure_full: ∀program.∀policy.
291  measure_int program policy 0 = 2*|program| → ∀i.i<|program| →
292  is_jump (\snd (nth i ? program 〈None ?,Comment []〉)) →
293  (\snd (bvt_lookup ?? (bitvector_of_nat ? i) (\snd policy) 〈0,short_jump〉)) = long_jump.
294#program #policy elim program in ⊢ (% → ∀i.% → ? → %);
295[ #Hm #i #Hi @⊥ @(absurd … Hi) @not_le_Sn_O
296| #h #t #Hind #Hm #i #Hi #Hj
297  cases (le_to_or_lt_eq … Hi) -Hi
298  [ #Hi @Hind
299    [ whd in match (measure_int ???) in Hm;
300      cases (\snd (bvt_lookup … (bitvector_of_nat ? (|t|)) (\snd policy) 〈0,short_jump〉)) in Hm;
301      normalize nodelta
302      [ #H @⊥ @(absurd ? (measure_le t policy)) >H @lt_to_not_le /2 by lt_plus, le_n/
303      | >measure_plus >commutative_plus #H @⊥ @(absurd ? (measure_le t policy))
304        <(plus_to_minus … (sym_eq … H)) @lt_to_not_le normalize /2 by le_n/
305      | >measure_plus <times_n_Sm >commutative_plus /2 by injective_plus_r/
306      ]
307    | @(le_S_S_to_le … Hi)
308    | @Hj
309    ]
310  | #Hi >(injective_S … Hi) whd in match (measure_int ???) in Hm;
311    cases (\snd (bvt_lookup … (bitvector_of_nat ? (|t|)) (\snd policy) 〈0,short_jump〉)) in Hm;
312    normalize nodelta
313    [ #Hm @⊥ @(absurd ? (measure_le t policy)) >Hm @lt_to_not_le /2 by lt_plus, le_n/
314    | >measure_plus >commutative_plus #H @⊥ @(absurd ? (measure_le t policy))
315      <(plus_to_minus … (sym_eq … H)) @lt_to_not_le normalize /2 by le_n/
316    | >measure_plus <times_n_Sm >commutative_plus /2 by injective_plus_r/
317    ]
318  ]
319]
320qed.
321
322(* uses second part of policy_increase *)
323lemma measure_special: ∀program:(Σl:list labelled_instruction.
324    (S (|l|)) < 2^16 ∧ is_well_labelled_p l).
325  ∀policy:Σp:ppc_pc_map.
326    not_jump_default program p ∧
327    \fst (bvt_lookup … (bitvector_of_nat ? 0) (\snd p) 〈0,short_jump〉) = 0 ∧
328    \fst p = \fst (bvt_lookup … (bitvector_of_nat ? (|program|)) (\snd p) 〈0,short_jump〉) ∧
329    sigma_compact_unsafe program (pi1 … (create_label_map program)) p ∧
330    \fst p ≤ 2^16.
331  match (\snd (pi1 ?? (jump_expansion_step program (pi1 … (create_label_map program)) policy))) with
332  [ None ⇒ True
333  | Some p ⇒ measure_int program policy 0 = measure_int program p 0 → sigma_jump_equal program policy p ].
334[2: #l #_ %]
335#program #policy inversion (jump_expansion_step ???)
336#p cases p -p #ch #pol normalize nodelta cases pol
337[ / by I/
338| #p normalize nodelta #Hpol #eqpol lapply (le_n (|program|))
339  @(list_ind ?  (λx.|x| ≤ |pi1 ?? program| →
340      measure_int x policy 0 = measure_int x p 0 →
341      sigma_jump_equal x policy p) ?? (pi1 ?? program))
342 [ #_ #_ #i #Hi @⊥ @(absurd ? Hi) @le_to_not_lt @le_O_n
343 | #h #t #Hind #Hp #Hm #i #Hi cases (le_to_or_lt_eq … (le_S_S_to_le …  Hi)) -Hi #Hi
344   [ @Hind
345     [ @(transitive_le … Hp) / by /
346     | whd in match (measure_int ???) in Hm; whd in match (measure_int ? p ?) in Hm;
347       lapply (proj2 ?? (proj1 ?? (proj1 ?? (proj1 ?? (proj1 ?? Hpol)))) (|t|) (le_S_to_le … Hp))
348       #Hinc cases (bvt_lookup ?? (bitvector_of_nat ? (|t|)) ? 〈0,short_jump〉) in Hm Hinc;
349       #x1 #x2 cases (bvt_lookup ?? (bitvector_of_nat ? (|t|)) ? 〈0,short_jump〉);
350       #y1 #y2 #Hm #Hinc lapply Hm -Hm; lapply Hinc -Hinc; normalize nodelta
351       cases x2 cases y2 normalize nodelta
352       [1: / by /
353       |2,3: >measure_plus #_ #H @⊥ @(absurd ? (eq_plus_S_to_lt … H)) @le_to_not_lt
354         lapply (measure_incr_or_equal program policy t ? 0)
355         [1,3: @(transitive_le … Hp) @le_n_Sn ] >eqpol / by /
356       |4,7,8: #H elim H #H2 [1,3,5: cases H2 |2,4,6: destruct (H2) ]
357       |5: >measure_plus >measure_plus >commutative_plus >(commutative_plus ? 1)
358         #_ #H @(injective_plus_r … H)
359       |6: >measure_plus >measure_plus
360         change with (1+1) in match (2); >assoc_plus1 >(commutative_plus 1 (measure_int ???))
361         #_ #H @⊥ @(absurd ? (eq_plus_S_to_lt … H)) @le_to_not_lt @monotonic_le_plus_l
362         lapply (measure_incr_or_equal program policy t ? 0)
363         [ @(transitive_le … Hp) @le_n_Sn ] >eqpol / by /
364       |9: >measure_plus >measure_plus >commutative_plus >(commutative_plus ? 2)
365         #_ #H @(injective_plus_r … H)
366       ]
367     | @Hi
368     ]
369   | >Hi whd in match (measure_int ???) in Hm; whd in match (measure_int ? p ?) in Hm;
370     lapply (proj2 ?? (proj1 ?? (proj1 ?? (proj1 ?? (proj1 ?? Hpol)))) (|t|) (le_S_to_le … Hp))
371     cases (bvt_lookup ?? (bitvector_of_nat ? (|t|)) ? 〈0,short_jump〉) in Hm;
372     #x1 #x2
373     cases (bvt_lookup ?? (bitvector_of_nat ? (|t|)) ? 〈0,short_jump〉); #y1 #y2
374     normalize nodelta cases x2 cases y2 normalize nodelta
375     [1,5,9: #_ #_ @refl
376     |4,7,8: #_ #H elim H #H2 [1,3,5: cases H2 |2,4,6: destruct (H2) ]
377     |2,3: >measure_plus #H #_ @⊥ @(absurd ? (eq_plus_S_to_lt … H)) @le_to_not_lt
378       lapply (measure_incr_or_equal program policy t ? 0)
379       [1,3: @(transitive_le … Hp) @le_n_Sn ] >eqpol / by /
380     |6: >measure_plus >measure_plus
381        change with (1+1) in match (2); >assoc_plus1 >(commutative_plus 1 (measure_int ???))
382        #H #_ @⊥ @(absurd ? (eq_plus_S_to_lt … H)) @le_to_not_lt @monotonic_le_plus_l
383        lapply (measure_incr_or_equal program policy t ? 0)
384        [ @(transitive_le … Hp) @le_n_Sn ] >eqpol / by /
385     ]
386   ]
387 ]
388qed.
389
390lemma measure_zero: ∀l.∀program:Σl:list labelled_instruction.
391  S (|l|) < 2^16 ∧ is_well_labelled_p l.
392  match jump_expansion_start program (create_label_map program) with
393  [ None ⇒ True
394  | Some p ⇒ |l| ≤ |program| → measure_int l p 0 = 0
395  ].
396 #l #program lapply (refl ? (jump_expansion_start program (create_label_map program)))
397 cases (jump_expansion_start program (create_label_map program)) in ⊢ (???% → %); #p #Hp #EQ
398 cases p in Hp EQ;
399 [ / by I/
400 | #pl normalize nodelta #Hpl #EQ elim l
401   [ / by refl/
402   | #h #t #Hind #Hp whd in match (measure_int ???);
403     elim (proj2 ?? (proj1 ?? Hpl) (|t|) (le_S_to_le … Hp))
404     #pc #Hpc >(lookup_opt_lookup_hit … Hpc 〈0,short_jump〉) normalize nodelta @Hind
405     @(transitive_le … Hp) @le_n_Sn
406   ]
407 ]
408qed.
409
410(* the actual computation of the fixpoint *)
411definition je_fixpoint: ∀program:(Σl:list labelled_instruction.
412  S (|l|) < 2^16 ∧ is_well_labelled_p l).
413  Σp:option ppc_pc_map.
414    match p with
415      [ None ⇒ True
416      | Some pol ⇒ And (And (And
417          (\fst (bvt_lookup … (bitvector_of_nat ? 0) (\snd pol) 〈0,short_jump〉) = 0)
418          (\fst pol = \fst (bvt_lookup … (bitvector_of_nat ? (|program|)) (\snd pol) 〈0,short_jump〉)))
419          (sigma_compact program (pi1 … (create_label_map program)) pol))
420          (\fst pol ≤ 2^16)
421      ].
422#program @(\snd (jump_expansion_internal program (S (2*|program|))))
423cases (dec_bounded_exists (λk.policy_equal_opt (pi1 ?? program)
424   (\snd (pi1 ?? (jump_expansion_internal program k)))
425   (\snd (pi1 ?? (jump_expansion_internal program (S k))))) ? (2*|program|))
426[ #Hex cases Hex -Hex #k #Hk
427  inversion (jump_expansion_internal ??)
428  #x cases x -x #Gno_ch #Go cases Go normalize nodelta
429  [ #H #Heq / by I/
430  | -Go #Gp #HGp #Geq
431    cut (policy_equal_opt program (\snd (jump_expansion_internal program (2*|program|)))
432      (\snd (jump_expansion_internal program (S (2*|program|)))))
433    [ @(pe_trans … (equal_remains_equal program k (proj2 ?? Hk) (S (2*|program|)) (le_S … (le_S_to_le … (proj1 ?? Hk)))))
434      @pe_sym @equal_remains_equal [ @(proj2 ?? Hk) | @(le_S_to_le … (proj1 ?? Hk)) ]
435    | >Geq lapply (refl ? (jump_expansion_internal program (2*|program|)))
436      cases (jump_expansion_internal program (2*|program|)) in ⊢ (???% → %);
437      #x cases x -x #Fno_ch #Fo cases Fo normalize nodelta
438      [ #H #Feq whd in match policy_equal_opt; normalize nodelta #ABS destruct (ABS)
439      | -Fo #Fp #HFp #Feq whd in match policy_equal_opt; normalize nodelta #Heq
440        @conj [ @conj [ @conj
441        [ @(proj2 ?? (proj1 ?? (proj1 ?? (proj1 ?? HGp))))
442        | @(proj2 ?? (proj1 ?? (proj1 ?? HGp)))
443        ]
444        | @(equal_compact_unsafe_compact ? Fp)
445          [ lapply (jump_pc_equal program (2*|program|))
446            >Feq >Geq normalize nodelta #H @H @Heq
447          | cases daemon (* true, but have to add this to properties *)
448          | @(proj2 ?? (proj1 ?? HGp))
449          ]
450        ]
451        | @(proj2 ?? HGp)
452        ]
453      ]
454    ]
455  ]
456| #Hnex lapply (not_exists_forall … Hnex) -Hnex #Hfa
457  lapply (refl ? (jump_expansion_internal program (2*|program|)))
458  cases (jump_expansion_internal program (2*|program|)) in ⊢ (???% → %);
459  #x cases x -x #Fno_ch #Fo cases Fo normalize nodelta
460  [ (* None *)
461     #HF #Feq lapply (step_none program (2*|program|) ? 1) >Feq / by /
462    <plus_n_Sm <plus_n_O #H >H -H normalize nodelta / by I/
463  | -Fo #Fp #HFp #Feq lapply (measure_full program Fp ?)
464    [ @le_to_le_to_eq
465      [ @measure_le
466      | cut (∀x:ℕ.x ≤ 2*|program| →
467         ∃p.(\snd (pi1 ?? (jump_expansion_internal program x)) = Some ? p ∧
468          x ≤ measure_int program p 0))
469        [ #x elim x
470          [ #Hx lapply (refl ? (jump_expansion_start program (create_label_map program)))
471            cases (jump_expansion_start program (create_label_map program)) in ⊢ (???% → %);
472             #z cases z -z normalize nodelta
473             [ #H #Heqn @⊥ elim (le_to_eq_plus ?? Hx) #n #Hn
474               @(absurd … (step_none program 0 ? n))
475               [ whd in match (jump_expansion_internal ??); >Heqn @refl
476               | <Hn >Feq @nmk #H destruct (H)
477               ]
478             | #Zp #HZp #Zeq @(ex_intro ?? Zp) @conj
479               [ whd in match (jump_expansion_internal ??); >Zeq @refl
480               | @le_O_n
481               ]
482             ]
483          | -x #x #Hind #Hx
484            lapply (refl ? (jump_expansion_internal program (S x)))
485            cases (jump_expansion_internal program (S x)) in ⊢ (???% → %);
486            #z cases z -z #Sno_ch #So cases So -So
487            [ #HSp #Seq normalize nodelta @⊥ elim (le_to_eq_plus ?? Hx) #k #Hk
488              @(absurd … (step_none program (S x) ? k))
489              [ >Seq @refl
490              | <Hk >Feq @nmk #H destruct (H)
491              ]
492            | #Sp #HSp #Seq @(ex_intro ?? Sp) @conj
493              [ @refl
494              | elim (Hind (transitive_le … (le_n_Sn x) Hx))
495                #pol #Hpol @(le_to_lt_to_lt … (proj2 ?? Hpol))
496                lapply (proj1 ?? Hpol) -Hpol
497                lapply (refl ? (jump_expansion_internal program x))
498                cases (jump_expansion_internal program x) in ⊢ (???% → %);
499                #z cases z -z #Xno_ch #Xo cases Xo
500                [ #HXp #Xeq #abs destruct (abs)
501                | normalize nodelta #Xp #HXp #Xeq #H <(Some_eq ??? H) -H -pol
502                  lapply (Hfa x Hx) >Xeq >Seq whd in match policy_equal_opt;
503                  normalize nodelta #Hpe
504                  lapply (measure_incr_or_equal program Xp program (le_n (|program|)) 0)
505                  [ @HXp
506                  | lapply (Hfa x Hx) >Xeq >Seq
507                    lapply (measure_special program «Xp,?»)
508                    [ @HXp
509                    | lapply Seq whd in match (jump_expansion_internal program (S x)); (*340s*)
510                      >Xeq normalize nodelta cases Xno_ch in HXp Xeq; #HXp #Xeq
511                      [ normalize nodelta #EQ
512                        >(proj2 ?? (pair_destruct ?????? (pi1_eq ???? EQ)))
513                        #_ #abs @⊥ @(absurd ?? abs) / by /
514                      | normalize nodelta cases (jump_expansion_step ???);
515                        #z cases z -z #stch #sto cases sto   
516                        [ normalize nodelta #_ #ABS destruct (ABS)
517                        | -sto #stp normalize nodelta #Hstp #steq
518                          >(Some_eq ??? (proj2 ?? (pair_destruct ?????? (pi1_eq ???? steq))))
519                          #Hms #Hneq #glerp elim (le_to_or_lt_eq … glerp)
520                          [ / by /
521                          | #glorp @⊥ @(absurd ?? Hneq) @Hms @glorp
522                          ]
523                        ]
524                      ]
525                    ]
526                  ]
527                ]
528              ]
529            ]
530          ]
531        | #H elim (H (2*|program|) (le_n ?)) #plp >Feq #Hplp
532          >(Some_eq ??? (proj1 ?? Hplp)) @(proj2 ?? Hplp)
533        ]
534      ]
535    | #Hfull lapply (refl ? (jump_expansion_internal program (S (2*|program|))))
536      cases (jump_expansion_internal program (S (2*|program|))) in ⊢ (???% → %);
537      #z cases z -z #Gch #Go cases Go normalize nodelta
538      [ #HGp #Geq @I
539      | -Go #Gp normalize nodelta #HGp #Geq @conj [ @conj [ @conj
540        [ @(proj2 ?? (proj1 ?? (proj1 ?? (proj1 ?? HGp))))
541        | @(proj2 ?? (proj1 ?? (proj1 ?? HGp)))
542        ]
543        | @(equal_compact_unsafe_compact ? Fp)
544          [ lapply (jump_pc_equal program (2*(|program|))) >Feq >Geq normalize nodelta
545            #H @H #i #Hi
546            inversion (is_jump (\snd (nth i ? program 〈None ?, Comment []〉)))
547            [ #Hj whd in match (jump_expansion_internal program (S (2*|program|))) in Geq; (*85s*)
548              >Feq in Geq; normalize nodelta cases Fno_ch
549              [ normalize nodelta #Heq
550                >(Some_eq ??? (proj2 ?? (pair_destruct ?????? (pi1_eq ???? Heq)))) %
551              | normalize nodelta cases (jump_expansion_step ???)
552                #x cases x -x #stch #sto normalize nodelta cases sto
553                [ normalize nodelta #_ #X destruct (X)
554                | -sto #stp normalize nodelta #Hst #Heq
555                   <(Some_eq ??? (proj2 ?? (pair_destruct ?????? (pi1_eq ???? Heq))))
556                   lapply (proj2 ?? (proj1 ?? (proj1 ?? (proj1 ?? (proj1 ?? Hst)))) i (le_S_to_le … Hi))
557                   lapply (Hfull i Hi ?) [>Hj %]
558                   cases (bvt_lookup … (bitvector_of_nat ? i) (\snd Fp) 〈0,short_jump〉)
559                   #fp #fj #Hfj >Hfj normalize nodelta
560                   cases (bvt_lookup … (bitvector_of_nat ? i) (\snd stp) 〈0,short_jump〉)
561                   #stp #stj cases stj normalize nodelta
562                   [1,2: #H cases H #H2 cases H2 destruct (H2)
563                   |3: #_ @refl
564                   ]
565                 ]
566               ]
567             | #Hj >(proj1 ?? (proj1 ?? (proj1 ?? (proj1 ?? HGp))) i Hi ?) [2:>Hj %]
568                >(proj1 ?? (proj1 ?? (proj1 ?? (proj1 ?? HFp))) i Hi ?) [2:>Hj] %
569              ]
570            | cases daemon (* true, but have to add to properties in some way *)
571            | @(proj2 ?? (proj1 ?? HGp))
572            ]
573          ]
574          | @(proj2 ?? HGp)
575          ]
576        ]
577      ]
578    ]
579  | #n cases (jump_expansion_internal program n) cases (jump_expansion_internal program (S n))
580    #x cases x -x #nch #npol normalize nodelta #Hnpol
581    #x cases x -x #Sch #Spol normalize nodelta #HSpol
582    cases npol in Hnpol; cases Spol in HSpol;
583    [ #Hnpol #HSpol %1 //
584    |2,3: #x #Hnpol #HSpol %2 @nmk whd in match (policy_equal_opt ???); //
585      #H destruct (H)
586    |4: #np #Hnp #Sp #HSp whd in match (policy_equal_opt ???); @dec_bounded_forall #m
587        cases (bvt_lookup ?? (bitvector_of_nat 16 m) ? 〈0,short_jump〉)
588        #x1 #x2
589        cases (bvt_lookup ?? (bitvector_of_nat ? m) ? 〈0,short_jump〉)
590        #y1 #y2 normalize nodelta
591        @dec_eq_jump_length
592    ]
593  ]
594qed.
595
596include alias "arithmetics/nat.ma".
597include alias "basics/logic.ma".
598
599lemma pc_increases: ∀i,j:ℕ.∀program.∀pol:Σp:ppc_pc_map.
600  And (And (And
601    (\fst (bvt_lookup … (bitvector_of_nat ? 0) (\snd p) 〈0,short_jump〉) = 0)
602    (\fst p = \fst (bvt_lookup … (bitvector_of_nat ? (|program|)) (\snd p) 〈0,short_jump〉)))
603    (sigma_compact program (create_label_map program) p))
604    (\fst p ≤ 2^16).i ≤ j → j ≤ |program| →
605  \fst (bvt_lookup (ℕ×jump_length) 16 (bitvector_of_nat 16 i) (\snd pol) 〈0,short_jump〉) ≤
606  \fst (bvt_lookup (ℕ×jump_length) 16 (bitvector_of_nat 16 j) (\snd pol) 〈0,short_jump〉).
607 #i #j #program #pol #H elim (le_to_eq_plus … H) #n #Hn >Hn -Hn -j elim n
608 [ <plus_n_O #_ @le_n
609 | -n #n <plus_n_Sm #Hind #H @(transitive_le ??? (Hind (le_S_to_le … H)))
610   lapply (proj2 ?? (proj1 ?? (pi2 ?? pol)) (i+n) H)
611   lapply (refl ? (lookup_opt … (bitvector_of_nat ? (i+n)) (\snd pol)))
612   cases (lookup_opt … (bitvector_of_nat ? (i+n)) (\snd pol)) in ⊢ (???% → %);
613   [ normalize nodelta #_ #abs cases abs
614   | #x cases x -x #pc #jl #EQ normalize nodelta
615     lapply (refl ? (lookup_opt … (bitvector_of_nat ? (S (i+n))) (\snd pol)))
616     cases (lookup_opt … (bitvector_of_nat ? (S (i+n))) (\snd pol)) in ⊢ (???% → %);
617     [ normalize nodelta #_ #abs cases abs
618     | #x cases x -x #Spc #Sjl #SEQ normalize nodelta #Hcomp
619       >(lookup_opt_lookup_hit … EQ 〈0,short_jump〉)
620       >(lookup_opt_lookup_hit … SEQ 〈0,short_jump〉) >Hcomp @le_plus_n_r
621     ]
622   ]
623 ]
624qed.
625 
626(* The glue between Policy and Assembly. *)
627definition jump_expansion':
628∀program:preamble × (Σl:list labelled_instruction.S (|l|) < 2^16 ∧ is_well_labelled_p l).
629 option (Σsigma_policy:(Word → Word) × (Word → bool).
630   let 〈sigma,policy〉≝ sigma_policy in
631   sigma_policy_specification 〈\fst program,\snd program〉 sigma policy)
632   ≝
633 λprogram.
634  let f: option ppc_pc_map ≝ je_fixpoint (\snd program) in
635  match f return λx.f = x → ? with
636  [ None ⇒ λp.None ?
637  | Some x ⇒ λp.Some ?
638      «〈(λppc.let pc ≝ \fst (bvt_lookup ?? ppc (\snd x) 〈0,short_jump〉) in
639          bitvector_of_nat 16 pc),
640         (λppc.let jl ≝ \snd (bvt_lookup ?? ppc (\snd x) 〈0,short_jump〉) in
641          jmpeqb jl long_jump)〉,?»
642  ] (refl ? f).
643normalize nodelta in p; whd in match sigma_policy_specification; normalize nodelta
644lapply (pi2 ?? (je_fixpoint (\snd program))) >p normalize nodelta cases x
645#fpc #fpol #Hfpol cases Hfpol ** #Hfpol1 #Hfpol2 #Hfpol3 #Hfpol4
646@conj
647[ >Hfpol1 %
648| #ppc #ppc_ok normalize nodelta
649  >(?:\fst (fetch_pseudo_instruction (pi1 … (\snd program)) ppc ppc_ok) =
650       \snd (nth (nat_of_bitvector … ppc) ? (\snd program) 〈None ?, Comment []〉))
651  [2: whd in match fetch_pseudo_instruction; normalize nodelta
652   >(nth_safe_nth … 〈None ?, Comment []〉)
653   cases (nth (nat_of_bitvector ? ppc) ? (\snd program) 〈None ?, Comment []〉)
654   #lbl #ins % ]
655  lapply (Hfpol3 (nat_of_bitvector ? ppc) ppc_ok) >bitvector_of_nat_inverse_nat_of_bitvector
656  inversion (lookup_opt ????) normalize nodelta [ #Hl #abs cases abs ]
657  * #pc #jl #Hl normalize nodelta
658  inversion (lookup_opt ????) normalize nodelta [ #Hl #abs cases abs ]
659  * #Spc #Sjl #SHL lapply SHL
660  <add_bitvector_of_nat_Sm >bitvector_of_nat_inverse_nat_of_bitvector >add_commutative
661  #SHl normalize nodelta #Hcompact
662  @conj
663  [ >(lookup_opt_lookup_hit … SHl 〈0,short_jump〉)
664    >(lookup_opt_lookup_hit … Hl 〈0,short_jump〉)
665    >add_bitvector_of_nat_plus >Hcompact %
666  | (* Basic proof scheme:
667       - ppc < |snd program|, hence our instruction is in the program
668       - either we are the last non-zero-size instruction, in which case we are
669         either smaller than 2^16 (because the entire program is), or we are exactly
670         2^16 and something weird happens
671       - or we are not, in which case we are definitely smaller than 2^16 (by transitivity
672         through the next non-zero instruction)
673    *)
674    elim (le_to_or_lt_eq … Hfpol4) #Hfpc
675    [ %1 @(le_to_lt_to_lt … Hfpc) >Hfpol2
676      >(lookup_opt_lookup_hit … Hl 〈0,short_jump〉)
677      >nat_of_bitvector_bitvector_of_nat_inverse
678      [2: lapply (pc_increases (nat_of_bitvector 16 ppc) (|\snd program|) (\snd program) «〈fpc,fpol〉,Hfpol» (le_S_to_le … ppc_ok) (le_n ?))
679          >bitvector_of_nat_inverse_nat_of_bitvector >(lookup_opt_lookup_hit … Hl 〈0,short_jump〉)
680          #H @(le_to_lt_to_lt … Hfpc) >Hfpol2 @H ]
681      lapply (pc_increases (S (nat_of_bitvector 16 ppc)) (|\snd program|) (\snd program) «〈fpc,fpol〉,Hfpol» ppc_ok (le_n ?))
682      >(lookup_opt_lookup_hit … SHL 〈0,short_jump〉) >Hcompact #X @X
683    | (* the program is of length 2^16 and ppc is followed by only zero-size instructions
684       * until the end of the program *)
685      elim (le_to_or_lt_eq … (pc_increases (nat_of_bitvector ? ppc) (|\snd program|) (\snd program) «〈fpc,fpol〉,Hfpol» (le_S_to_le … ppc_ok) (le_n ?)))
686      [ >bitvector_of_nat_inverse_nat_of_bitvector
687        >(lookup_opt_lookup_hit … Hl 〈0,short_jump〉) #Hpc normalize nodelta
688        >nat_of_bitvector_bitvector_of_nat_inverse
689        [2: <Hfpc >Hfpol2 @Hpc ]
690        elim (le_to_or_lt_eq … (pc_increases (S (nat_of_bitvector ? ppc)) (|\snd program|) (\snd program) «〈fpc,fpol〉,Hfpol» ppc_ok (le_n ?)))
691        <Hfpol2 >Hfpc >(lookup_opt_lookup_hit … SHL 〈0,short_jump〉) #HSpc
692        [ %1 >Hcompact in HSpc; #X @X
693        | %2 @conj
694          [2: >Hcompact in HSpc; #X @X
695          | #ppc' #ppc_ok' #Hppc'
696                (* S ppc < ppc' < |\snd program| *)
697                (* lookup S ppc = 2^16 *)
698                (* lookup |\snd program| = 2^16 *)
699                (* lookup ppc' = 2^16 → instruction size = 0 *)
700                lapply (Hfpol3 (nat_of_bitvector ? ppc') ppc_ok')
701                >bitvector_of_nat_inverse_nat_of_bitvector
702                inversion (lookup_opt ????) normalize nodelta
703                [ #_ #abs cases abs
704                | * #xpc #xjl #XEQ normalize nodelta
705                  inversion (lookup_opt ????) normalize nodelta
706                  [ #_ #abs cases abs
707                  | * #Sxpc #Sxjl #SXEQ normalize nodelta
708                    #Hpcompact
709                    lapply (pc_increases (S (nat_of_bitvector ? ppc)) (nat_of_bitvector ? ppc') (\snd program) «〈fpc,fpol〉,Hfpol» Hppc' (le_S_to_le … ppc_ok'))
710                    >(lookup_opt_lookup_hit … SHL 〈0,short_jump〉) >HSpc #Hle1
711                    lapply (pc_increases (nat_of_bitvector ? ppc') (|\snd program|) (\snd program) «〈fpc,fpol〉,Hfpol» (le_S_to_le … ppc_ok') (le_n ?))
712                    <Hfpol2 >Hfpc #Hle2
713                    lapply (le_to_le_to_eq ?? Hle2 Hle1) -Hle2 -Hle1
714                    >bitvector_of_nat_inverse_nat_of_bitvector
715                    >(lookup_opt_lookup_hit … XEQ 〈0,short_jump〉) #Hxpc
716                    lapply (pc_increases (S (nat_of_bitvector ? ppc)) (S (nat_of_bitvector ? ppc')) (\snd program) «〈fpc,fpol〉,Hfpol» (le_S_to_le … (le_S_S … Hppc')) ppc_ok')
717                    >(lookup_opt_lookup_hit … SHL 〈0,short_jump〉) >HSpc #Hle1
718                    lapply (pc_increases (S (nat_of_bitvector ? ppc')) (|\snd program|) (\snd program) «〈fpc,fpol〉,Hfpol» ppc_ok' (le_n ?))
719                    <Hfpol2 >Hfpc #Hle2
720                    lapply (le_to_le_to_eq ?? Hle2 Hle1) -Hle1 -Hle2
721                    >(lookup_opt_lookup_hit … SXEQ 〈0,short_jump〉) #HSxpc
722                    >Hxpc in Hpcompact; >HSxpc whd in match create_label_map; #H
723                    @(plus_equals_zero (2^16)) whd in match fetch_pseudo_instruction;
724                    normalize nodelta >(nth_safe_nth … 〈None ?, Comment []〉)
725                    cases (nth (nat_of_bitvector ? ppc') ? (\snd program) 〈None ?, Comment []〉) in H;
726                    #lbl #ins normalize nodelta #X @sym_eq @X
727                  ]
728                ]
729          ]
730        ]
731      | >bitvector_of_nat_inverse_nat_of_bitvector
732        <Hfpol2 >Hfpc >(lookup_opt_lookup_hit … Hl 〈0,short_jump〉) #Hpc
733        %1 >Hpc
734        >bitvector_of_nat_exp_zero whd in match (nat_of_bitvector ? (zero ?));
735        <plus_O_n whd in match instruction_size; normalize nodelta
736        inversion (assembly_1_pseudoinstruction ??? ppc (λx0.zero 16) ?)
737        #len #ins #Hass lapply (fst_snd_assembly_1_pseudoinstruction … Hass)
738        #Hli >Hli
739        lapply (assembly1_pseudoinstruction_lt_2_to_16 ??? ppc (λx0.zero 16) ?)
740        [5: >Hass / by / ]
741      ]
742    ]
743  ]
744]
745qed.
Note: See TracBrowser for help on using the repository browser.