source: src/joint/blocks.ma

Last change on this file was 2879, checked in by tranquil, 7 years ago

changed coercion from list of joint_seq to blocks to a more efficient one

File size: 28.1 KB
Line 
1include "joint/Joint.ma".
2include "utilities/bindLists.ma".
3
4(* inductive block_step (p : stmt_params) (globals : list ident) : Type[0] ≝
5  | block_seq : joint_seq p globals → block_step p globals
6  | block_skip : label → block_step p globals.
7
8definition if_seq : ∀p,globals.∀A:Type[2].block_step p globals → A → A → A ≝
9λp,g,A,s.match s with
10[ block_seq _ ⇒ λx,y.x
11| _ ⇒ λx,y.y
12].
13
14definition stmt_of_block_step : ∀p : stmt_params.∀globals.
15  ∀s : block_step p globals.if_seq … s (succ p) unit → joint_statement p globals ≝
16  λp,g,s.match s return λx.if_seq ??? x ?? → joint_statement ?? with
17  [ block_seq s' ⇒ λnxt.sequential … s' nxt
18  | block_skip l ⇒ λ_.GOTO … l
19  ].
20
21definition seq_to_block_step_list : ∀p : stmt_params.∀globals.
22  list (joint_seq p globals) →
23  list (block_step p globals) ≝ λp,globals.map ?? (block_seq ??).
24
25coercion block_step_from_seq_list : ∀p : stmt_params.∀globals.
26  ∀l:list (joint_seq p globals).
27  list (block_step p globals) ≝
28  seq_to_block_step_list
29  on _l:list (joint_seq ??)
30  to list (block_step ??).
31
32definition is_inr : ∀A,B.A + B → bool ≝ λA,B,x.match x with
33  [ inl _ ⇒ true
34  | inr _ ⇒ false
35  ].
36definition is_inl : ∀A,B.A + B → bool ≝ λA,B,x.match x with
37  [ inl _ ⇒ true
38  | inr _ ⇒ false
39  ].
40
41definition skip_block ≝ λp,globals,A.
42  (list (block_step p globals)) × A.*)
43
44(* move *)
45let rec bind_new_P R X (P : X → Prop) (b : bind_new R X) on b : Prop ≝
46match b with
47[ bret x ⇒ P x
48| bnew f ⇒ ∀x.bind_new_P … P (f x)
49].
50
51definition BindNewP : ∀R.MonadPred (BindNew R) ≝
52λR.mk_MonadPred (BindNew R) (bind_new_P R) ???.
53[ #X #P #x #K @K
54| #X #Y #Pin #Pout #m elim m -m
55  [ #x #H #f #G @G @H
56  | #g #IH #H #f #G #x @IH [ @H | @G ]
57  ]
58| #X #P #Q #H #m elim m -m
59  [ #x #Px @H @Px
60  | #g #IH #Pg #x @IH @Pg
61  ]
62]
63qed.
64
65definition not_empty : ∀X.list X → Prop ≝ λX,l.match l with [ nil ⇒ False | _ ⇒ True ].
66
67definition split_on_last : ∀X.list X → option ((list X) × X) ≝
68λX.foldr …
69  (λel,acc.match acc with
70    [ None ⇒ Some ? 〈[ ], el〉
71    | Some pr ⇒ Some ? 〈el :: \fst pr, \snd pr〉
72    ]) (None ?).
73
74lemma split_on_last_inv : ∀X,l.
75match split_on_last X l with
76[ None ⇒ l = [ ]
77| Some pr ⇒ l = \fst pr @ [ \snd pr]
78].
79#X #l elim l -l normalize nodelta [ // ]
80#hd #tl
81change with (match split_on_last ? tl in option  with [ _ ⇒ ?]) in
82  match (split_on_last ? (hd :: tl)); cases (split_on_last ??)
83normalize nodelta [2: * #pref #last ] #EQ >EQ normalize % qed.
84
85lemma split_on_last_hit : ∀X,pref,last.
86split_on_last X (pref @ [last]) = Some ? 〈pref, last〉.
87#X #pref elim pref -pref normalize [//]
88#hd #tl #IH #last >IH % qed.
89
90(* the label input is to accomodate ERTLptr pass
91   the postfix is for the case CALL ↦ instrs* ; CALL; instrs* *)
92definition step_block ≝
93  λp,globals.list (code_point p → joint_seq p globals) ×
94             (code_point p → joint_step p globals) ×
95             (list (joint_seq p globals)).
96
97definition fin_block ≝ λp,globals.(list (joint_seq p globals))×(joint_fin_step p).
98
99(*definition seq_to_skip_block :
100  ∀p,g,A.seq_block p g A → skip_block p g A
101 ≝ λp,g,A,b.〈\fst b, \snd b〉.
102
103coercion skip_from_seq_block :
104  ∀p,g,A.∀b : seq_block p g A.skip_block p g A ≝
105  seq_to_skip_block on _b : seq_block ??? to skip_block ???.*)
106
107definition bind_step_block ≝ λp.λglobals.
108  bind_new register (step_block p globals).
109
110unification hint 0 ≔ p, g;
111P ≟ step_block p g
112(*---------------------------------------*)⊢
113bind_step_block p g ≡ bind_new register P.
114
115definition bind_fin_block ≝ λp : stmt_params.λglobals.
116  bind_new register (fin_block p globals).
117
118unification hint 0 ≔ p : stmt_params, g;
119P ≟ fin_block p g
120(*---------------------------------------*)⊢
121bind_fin_block p g ≡ bind_new register P.
122
123definition bind_seq_list ≝ λp : stmt_params.λglobals.
124  bind_new register (list (joint_seq p globals)).
125unification hint 0 ≔ p : stmt_params, g;
126S ≟ joint_seq p g,
127L ≟ list S
128(*---------------------------------------*)⊢
129bind_seq_list p g ≡ bind_new register L.
130
131definition add_dummy_variance : ∀X,Y : Type[0].list Y → list (X → Y) ≝ λX,Y.map … (λx.λ_.x).
132
133definition ensure_step_block : ∀p : params.∀globals.
134list (joint_seq p globals) → step_block p globals ≝
135λp,g,l.
136match l return λ_.step_block ?? with
137[ nil ⇒ 〈[ ], λ_.NOOP ??, [ ]〉
138| cons hd tl ⇒ 〈[ ], λ_.hd, tl〉
139].
140
141coercion step_block_from_seq_list nocomposites :
142∀p : params.∀g.∀l : list (joint_seq p g).step_block p g ≝
143ensure_step_block on _l : list (joint_seq ??) to step_block ??.
144
145(*definition bind_skip_block ≝ λp : stmt_params.λglobals,A.
146  bind_new (localsT p) (skip_block p globals A).
147unification hint 0 ≔ p : stmt_params, g, A;
148B ≟ skip_block p g A, R ≟ localsT p
149(*---------------------------------------*)⊢
150bind_skip_block p g A ≡ bind_new R B.
151
152definition bind_seq_to_skip_block :
153  ∀p,g,A.bind_seq_block p g A → bind_skip_block p g A ≝
154  λp,g,A.m_map ? (seq_block p g A) (skip_block p g A)
155    (λx.x).
156
157coercion bind_skip_from_seq_block :
158  ∀p,g,A.∀b:bind_seq_block p g A.bind_skip_block p g A ≝
159  bind_seq_to_skip_block on _b : bind_seq_block ??? to bind_skip_block ???.*)
160(*
161definition block_classifier ≝
162  λp,g.λb : other_block p g.
163  seq_or_fin_step_classifier ?? (\snd b).
164*)
165(*definition seq_block_from_seq_list :
166∀p : stmt_params.∀g.list (joint_seq p g) → seq_block p g (joint_step p g) ≝
167λp,g,l.let 〈pre,post〉 ≝ split_on_last … (NOOP ??) l in 〈pre, (post : joint_step ??)〉.
168
169definition bind_seq_block_from_bind_seq_list :
170  ∀p : stmt_params.∀g.bind_new (localsT p) (list (joint_seq p g)) →
171    bind_seq_block p g (joint_step p g) ≝ λp.λg.m_map … (seq_block_from_seq_list …).
172
173definition bind_seq_block_step :
174  ∀p,g.bind_seq_block p g (joint_step p g) →
175    bind_seq_block p g (joint_step p g) + bind_seq_block p g (joint_fin_step p) ≝
176  λp,g.inl ….
177coercion bind_seq_block_from_step :
178  ∀p,g.∀b:bind_seq_block p g (joint_step p g).
179    bind_seq_block p g (joint_step p g) + bind_seq_block p g (joint_fin_step p) ≝
180  bind_seq_block_step on _b : bind_seq_block ?? (joint_step ??) to
181    bind_seq_block ?? (joint_step ??) + bind_seq_block ?? (joint_fin_step ?).
182
183definition bind_seq_block_fin_step :
184  ∀p,g.bind_seq_block p g (joint_fin_step p) →
185    bind_seq_block p g (joint_step p g) + bind_seq_block p g (joint_fin_step p) ≝
186  λp,g.inr ….
187coercion bind_seq_block_from_fin_step :
188  ∀p,g.∀b:bind_seq_block p g (joint_fin_step p).
189    bind_seq_block p g (joint_step p g) + bind_seq_block p g (joint_fin_step p) ≝
190  bind_seq_block_fin_step on _b : bind_seq_block ?? (joint_fin_step ?) to
191    bind_seq_block ?? (joint_step ??) + bind_seq_block ?? (joint_fin_step ?).
192
193definition seq_block_bind_seq_block :
194  ∀p : stmt_params.∀g,A.seq_block p g A → bind_seq_block p g A ≝ λp,g,A.bret ….
195coercion seq_block_to_bind_seq_block :
196  ∀p : stmt_params.∀g,A.∀b:seq_block p g A.bind_seq_block p g A ≝
197  seq_block_bind_seq_block
198  on _b : seq_block ??? to bind_seq_block ???.
199
200definition joint_step_seq_block : ∀p : stmt_params.∀g.joint_step p g → seq_block p g (joint_step p g) ≝
201  λp,g,x.〈[ ], x〉.
202coercion joint_step_to_seq_block : ∀p : stmt_params.∀g.∀b : joint_step p g.seq_block p g (joint_step p g) ≝
203  joint_step_seq_block on _b : joint_step ?? to seq_block ?? (joint_step ??).
204
205definition joint_fin_step_seq_block : ∀p : stmt_params.∀g.joint_fin_step p → seq_block p g (joint_fin_step p) ≝
206  λp,g,x.〈[ ], x〉.
207coercion joint_fin_step_to_seq_block : ∀p : stmt_params.∀g.∀b : joint_fin_step p.seq_block p g (joint_fin_step p) ≝
208  joint_fin_step_seq_block on _b : joint_fin_step ? to seq_block ?? (joint_fin_step ?).
209
210definition seq_list_seq_block :
211  ∀p:stmt_params.∀g.list (joint_seq p g) → seq_block p g (joint_step p g) ≝
212  λp,g,l.let pr ≝ split_on_last … (NOOP ??) l in 〈\fst pr, \snd pr〉.
213coercion seq_list_to_seq_block :
214  ∀p:stmt_params.∀g.∀l:list (joint_seq p g).seq_block p g (joint_step p g) ≝
215  seq_list_seq_block on _l : list (joint_seq ??) to seq_block ?? (joint_step ??).
216
217definition bind_seq_list_bind_seq_block :
218  ∀p:stmt_params.∀g.bind_new (localsT p) (list (joint_seq p g)) → bind_seq_block p g (joint_step p g) ≝
219  λp,g.m_map ??? (λx : list (joint_seq ??).(x : seq_block ???)).
220
221coercion bind_seq_list_to_bind_seq_block :
222  ∀p:stmt_params.∀g.∀l:bind_new (localsT p) (list (joint_seq p g)).bind_seq_block p g (joint_step p g) ≝
223  bind_seq_list_bind_seq_block on _l : bind_new ? (list (joint_seq ??)) to bind_seq_block ?? (joint_step ??).
224*)
225
226notation > "x ~❨ B , l ❩~> y 'in' c" with precedence 56 for
227  @{'block_in_code $c $x $B $l $y}.
228 
229notation < "hvbox(x ~❨ B , break l ❩~> y \nbsp 'in' \nbsp break c)" with precedence 56 for
230  @{'block_in_code $c $x $B $l $y}.
231
232notation > "x ~❨ B , l , r ❩~> y 'in' c" with precedence 56 for
233  @{'bind_block_in_code $c $x $B $l $r $y}.
234 
235notation < "hvbox(x ~❨ B , break l , break r ❩~> y \nbsp 'in' \nbsp break c)" with precedence 56 for
236  @{'bind_block_in_code $c $x $B $l $r $y}.
237
238definition step_in_code ≝
239  λp,globals.λc : codeT p globals.λsrc : code_point p.λs : joint_step p globals.
240  λdst : code_point p.
241  ∃nxt.stmt_at … c src = Some ? (sequential … s nxt) ∧
242       point_of_succ … src nxt = dst.
243
244definition fin_step_in_code ≝
245  λp,globals.λc : codeT p globals.λsrc : code_point p.λs : joint_fin_step p.
246  stmt_at … c src = Some ? (final … s).
247
248let rec seq_list_in_code p globals (c : codeT p globals)
249  (src : code_point p) (B : list (joint_seq p globals))
250  (l : list (code_point p)) (dst : code_point p) on B : Prop ≝
251  match B with
252  [ nil ⇒ l = [ ] ∧ src = dst
253  | cons hd tl ⇒
254    ∃mid,rest.l = src :: rest ∧
255    step_in_code …  c src hd mid ∧
256    seq_list_in_code … c mid tl rest dst
257  ].
258
259(* leaving it out because it can be misleading
260interpretation "step list in code" 'block_in_code c x B l y = (step_list_in_code ?? c x B l y).
261*)
262
263lemma seq_list_in_code_ne : ∀p,globals.∀c : codeT p globals.∀src,B,l,dst.
264  not_empty ? B →
265  seq_list_in_code … c src B l dst →
266  ∃post.l = src :: post.
267#p #globals #c #src * [ #l #dst * ] #hd #tl #l #dst #_ * #mid * #post ** #EQl #_ #_ %{EQl} qed.
268(* #b lapply src -src elim b -b [ #src #l #dst * ]
269#hd * [2: #hd' #tl ] #IH #src * [1,3: #dst #_ * ]
270#mid * [ #dst #_ ** #EQ destruct #_ * |4: #mid' #tl #dst #_ ** #_ #_ * ]
271[2: #dst #_ ** #EQ destruct normalize nodelta #_ #_ %{[ ]} % ]
272#mid' #rest #dst #_ ** #EQ destruct #H1 #H2
273cases (IH … I H2) #post normalize nodelta #EQ destruct %{(mid' :: post)} %
274qed.
275*)
276
277lemma seq_list_in_code_append :
278  ∀p,globals.∀c : codeT p globals.∀src,B1,l1,mid,B2,l2,dst.
279  seq_list_in_code … c src B1 l1 mid →
280  seq_list_in_code … c mid B2 l2 dst →
281  seq_list_in_code … c src (B1@B2) (l1@l2) dst.
282#p #globals #c #src #B1 lapply src -src elim B1 -B1
283[ #src #l1 #mid #B2 #l2 #dst * #EQ1 #EQ2 destruct #H @H
284| #hd #tl #IH #src #l1 #mid #B2 #l2 #dst * #mid' * #rest **
285  #EQ destruct #H1 #H2 #H3 %{mid'} %{(rest@l2)} %{(IH … H2 H3)} %{H1} %
286]
287qed.
288
289lemma seq_list_in_code_append_inv :
290  ∀p,globals.∀c : codeT p globals.∀src,B1,B2,l,dst.
291  seq_list_in_code … c src (B1@B2) l dst →
292  ∃l1,mid,l2.l = l1 @ l2 ∧ seq_list_in_code … c src B1 l1 mid ∧
293  seq_list_in_code … c mid B2 l2 dst.
294#p #globals #c #src #B1 lapply src -src elim B1 -B1
295[ #src #B2 #l #dst #H %{[ ]} %{src} %{l} %{H} % % %
296| #hd #tl #IH #src #B2 #l2 #dst * #mid * #rest ** #EQ destruct
297  #H1 #H2 elim (IH … H2) #l1 * #mid' * #l2 ** #EQ destruct #G1 #G2
298  %{(src::l1)} %{mid'} %{l2} %{G2} %{(refl …)} % [| %[| %{G1} %{H1} % ]]
299]
300qed.
301
302definition map_eval : ∀X,Y : Type[0].list (X → Y) → X → list Y ≝ λX,Y,l,x.map … (λf.f x) l.
303
304definition step_block_in_code ≝
305  λp,g.λc : codeT p g.λsrc.λb : step_block p g.λl,dst.
306  let pref ≝ \fst (\fst b) in
307  let stp ≝ \snd (\fst b) in
308  let post ≝ \snd b in
309  ∃l1,mid1,mid2,l2.
310  l = l1 @ mid1 :: l2 ∧
311  seq_list_in_code ?? c src (map_eval … pref mid1) l1 mid1 ∧
312  step_in_code ?? c mid1 (stp mid1) mid2 ∧
313  seq_list_in_code ?? c mid2 post l2 dst.
314
315lemma map_compose : ∀X,Y,Z,f,g.∀l : list X.map Y Z f (map X Y g l) = map … (f∘g) l.
316#X #Y #Z #f #g #l elim l -l normalize // qed.
317
318lemma map_ext_eq : ∀X,Y,f,g.∀l : list X.(∀x.f x = g x) → map X Y f l = map X Y g l.
319#X #Y #f #g #l #H elim l -l normalize // qed.
320
321lemma map_id : ∀X.∀l : list X.map X X (λx.x) l = l.
322#X #l elim l -l normalize // qed.
323
324definition fin_block_in_code ≝
325  λp,g.λc:codeT p g.λsrc.λB : fin_block p g.λl.λdst : unit.
326  ∃pref,mid.l = pref @ [mid] ∧
327  seq_list_in_code … c src (\fst B) pref mid ∧ fin_step_in_code … c mid (\snd B).
328
329interpretation "step block in code" 'block_in_code c x B l y = (step_block_in_code ?? c x B l y).
330interpretation "fin block in code" 'block_in_code c x B l y = (fin_block_in_code ?? c x B l y).
331
332lemma coerced_step_list_in_code :
333∀p : params.∀g,c,src.∀b : list (joint_seq p g).∀l,dst.
334src ~❨b, l❩~> dst in c →
335match b with
336[ nil ⇒ step_in_code … c src (NOOP …) dst
337| _ ⇒ seq_list_in_code … c src b l dst
338].
339#p #g #c #src * [2: #hd #tl]
340#l #dst * #pref * #mid1 * #mid2 * #post *** #EQ * #EQ' #EQ''
341#stp_in_code [ #tl_in | * #EQ''' #EQ''''] whd
342destruct
343[ % [| % [| %{tl_in} %{stp_in_code} % ]]
344| @stp_in_code
345]
346qed.
347
348let rec bind_new_instantiates B X
349  (x : X) (b : bind_new B X) (l : list B) on b : Prop ≝
350  match b with
351  [ bret B ⇒
352    match l with
353    [ nil ⇒ x = B
354    | _ ⇒ False
355    ]
356  | bnew f ⇒
357    match l with
358    [ nil ⇒ False
359    | cons r l' ⇒
360      bind_new_instantiates B X x (f r) l'
361    ]
362  ].
363
364lemma bind_new_instantiates_bind_inv : ∀B,X,Y,y.
365  ∀b : bind_new B X.∀f : X → bind_new B Y.∀l.
366bind_new_instantiates B Y y (!y ← b; f y) l →
367∃x,l1,l2.l = l1@l2 ∧ bind_new_instantiates B X x b l1 ∧
368  bind_new_instantiates B Y y (f x) l2.
369#B #X #Y #y #b elim b -b
370[ #x #f #l >m_return_bind #H %{x} %{[ ]} %{l} %{H} %%
371| #g #IH #f * [ * ] #hd #tl whd in ⊢ (%→?); #H
372  cases (IH … H) #x * #l1 * #l2 ** #EQ #H1 #H2 %{x} %{(hd::l1)} %{l2}
373  %{H2} %{H1} >EQ %
374]
375qed.
376
377lemma bind_new_instantiates_bind : ∀B,X,Y,x,y.
378  ∀b : bind_new B X.∀f : X → bind_new B Y.∀l1,l2.
379bind_new_instantiates B X x b l1 →
380bind_new_instantiates B Y y (f x) l2 →
381bind_new_instantiates B Y y (!y ← b; f y) (l1@l2).
382#B #X #Y #x #y #b elim b -b
383[ #x' #f * [2: #hd #tl ] #l2 * #H @H
384| #g #IH #f * [ #l2 * ] #hd #tl #l2 #H1 #H2 whd @IH assumption
385]
386qed.
387
388definition bind_step_block_in_code ≝
389  λp,g.λc:codeT p g.λsrc.λB : bind_step_block p g.λlbls,regs.λdst.
390  ∃b.bind_new_instantiates … b B regs ∧ src ~❨b, lbls❩~> dst in c.
391
392definition bind_fin_block_in_code ≝
393  λp,g.λc:codeT p g.λsrc.λB : bind_fin_block p g.λlbls,regs.λdst.
394  ∃b.bind_new_instantiates … b B regs ∧ src ~❨b, lbls❩~> dst in c.
395
396interpretation "bound step block in code" 'bind_block_in_code c x B l r y = (bind_step_block_in_code ?? c x B l r y).
397interpretation "bound fin block in code" 'bind_block_in_code c x B l r y = (bind_fin_block_in_code ?? c x B l r y).
398
399(* generates ambiguity even if it shouldn't
400interpretation "seq block step in code" 'block_in_code c x B l y = (seq_block_step_in_code ?? c x B l y).
401interpretation "seq block fin step in code" 'block_in_code c x B l y = (seq_block_fin_step_in_code ?? c x B l y).
402*)
403
404(*
405
406definition seq_block_append :
407  ∀p,g.
408  ∀b1 : Σb.is_safe_block p g b.
409  ∀b2 : seq_block p g.
410  seq_block p g ≝ λp,g,b1,b2.
411  〈match b1 with
412  [ mk_Sig instr prf ⇒
413    match \snd instr return λx.bool_to_Prop (is_inl … x) ∧ seq_or_fin_step_classifier … x = ? → ? with
414    [ inl i ⇒ λprf.\fst b1 @ i :: \fst b2
415    | inr _ ⇒ λprf.⊥
416    ] prf
417  ],\snd b2〉.
418  cases prf #H1 #H2 assumption
419  qed.
420
421definition other_block_append :
422  ∀p,g.
423  (Σb.block_classifier ?? b = cl_other) →
424  other_block p g →
425  other_block p g ≝ λp,g,b1,b2.
426  〈\fst b1 @ «\snd b1, pi2 … b1» :: \fst b2, \snd b2〉.
427
428definition seq_block_cons : ∀p : stmt_params.∀g.(Σs.step_classifier p g s = cl_other) →
429  seq_block p g → seq_block p g ≝
430  λp,g,x,b.〈x :: \fst b,\snd b〉.
431definition other_block_cons : ∀p,g.
432  (Σs.seq_or_fin_step_classifier p g s = cl_other) → other_block p g →
433  other_block p g ≝
434  λp,g,x,b.〈x :: \fst b,\snd b〉.
435
436interpretation "seq block cons" 'cons x b = (seq_block_cons ?? x b).
437interpretation "other block cons" 'vcons x b = (other_block_cons ?? x b).
438interpretation "seq block append" 'append b1 b2 = (seq_block_append ?? b1 b2).
439interpretation "other block append" 'vappend b1 b2 = (other_block_append ?? b1 b2).
440
441definition step_to_block : ∀p,g.
442  seq_or_fin_step p g → seq_block p g ≝ λp,g,s.〈[ ], s〉.
443
444coercion block_from_step : ∀p,g.∀s : seq_or_fin_step p g.
445  seq_block p g ≝ step_to_block on _s : seq_or_fin_step ?? to seq_block ??.
446
447definition bind_seq_block_cons :
448  ∀p : stmt_params.∀g,is_seq.
449  (Σs.step_classifier p g s = cl_other) → bind_seq_block p g is_seq →
450  bind_seq_block p g is_seq ≝
451  λp,g,is_seq,x.m_map ??? (λb.〈x::\fst b,\snd b〉).
452
453definition bind_other_block_cons :
454  ∀p,g.(Σs.seq_or_fin_step_classifier p g s = cl_other) → bind_other_block p g → bind_other_block p g ≝
455  λp,g,x.m_map … (other_block_cons … x).
456
457let rec bind_pred_aux B X (P : X → Prop) (c : bind_new B X) on c : Prop ≝
458  match c with
459  [ bret x ⇒ P x
460  | bnew f ⇒ ∀b.bind_pred_aux B X P (f b)
461  ].
462
463let rec bind_pred_inj_aux B X (P : X → Prop) (c : bind_new B X) on c :
464  bind_pred_aux B X P c → bind_new B (Σx.P x) ≝
465  match c return λx.bind_pred_aux B X P x → bind_new B (Σx.P x) with
466  [ bret x ⇒ λprf.return «x, prf»
467  | bnew f ⇒ λprf.bnew … (λx.bind_pred_inj_aux B X P (f x) (prf x))
468  ].
469
470definition bind_pred ≝ λB.
471  mk_InjMonadPred (BindNew B)
472    (mk_MonadPred ?
473      (bind_pred_aux B)
474      ???)
475    (λX,P,c_sig.bind_pred_inj_aux B X P c_sig (pi2 … c_sig))
476    ?.
477[ #X #P #Q #H #y elim y [ #x @H | #f #IH #G #b @IH @G]
478| #X #Y #Pin #Pout #m elim m [#x | #f #IH] #H #g #G [ @G @H | #b @(IH … G) @H]
479| #X #P #x #Px @Px
480| #X #P * #m elim m [#x | #f #IH] #H [ % | @bnew_proper #b @IH]
481]
482qed.
483
484definition bind_seq_block_append :
485  ∀p,g,is_seq.(Σb : bind_seq_block p g true.bind_pred ? (λb.step_classifier p g (\snd b) = cl_other) b) →
486  bind_seq_block p g is_seq → bind_seq_block p g is_seq ≝
487  λp,g,is_seq,b1,b2.
488    !«p, prf» ← mp_inject … b1;
489    !〈post, last〉 ← b2;
490    return 〈\fst p @ «\snd p, prf» :: post, last〉.
491
492definition bind_other_block_append :
493  ∀p,g.(Σb : bind_other_block p g.bind_pred ?
494    (λx.block_classifier ?? x = cl_other) b) →
495  bind_other_block p g → bind_other_block p g ≝
496  λp,g,b1.m_bin_op … (other_block_append ??) (mp_inject … b1).
497
498interpretation "bind seq block cons" 'cons x b = (bind_seq_block_cons ??? x b).
499interpretation "bind other block cons" 'vcons x b = (bind_other_block_cons ?? x b).
500interpretation "bind seq block append" 'append b1 b2 = (bind_seq_block_append ??? b1 b2).
501interpretation "bind other block append" 'vappend b1 b2 = (bind_other_block_append ?? b1 b2).
502
503let rec instantiates_to B X
504  (b : bind_new B X) (l : list B) (x : X) on b : Prop ≝
505  match b with
506  [ bret B ⇒
507    match l with
508    [ nil ⇒ x = B
509    | _ ⇒ False
510    ]
511  | bnew f ⇒
512    match l with
513    [ nil ⇒ False
514    | cons r l' ⇒
515      instantiates_to B X (f r) l' x
516    ]
517  ].
518
519lemma instantiates_to_bind_pred :
520  ∀B,X,P,b,l,x.instantiates_to B X b l x → bind_pred B P b → P x.
521#B #X #P #b elim b
522[ #x * [ #y #EQ >EQ normalize // | #hd #tl #y *]
523| #f #IH * [ #y * | #hd #tl normalize #b #H #G @(IH … H) @G ]
524]
525qed.
526
527lemma seq_block_append_proof_irr :
528  ∀p,g,b1,b1',b2.pi1 ?? b1 = pi1 ?? b1' →
529    seq_block_append p g b1 b2 = seq_block_append p g b1' b2.
530#p #g * #b1 #b1prf * #b1' #b1prf' #b2 #EQ destruct(EQ) %
531qed.
532
533lemma other_block_append_proof_irr :
534  ∀p,g,b1,b1',b2.pi1 ?? b1 = pi1 ?? b1' →
535  other_block_append p g b1 b2 = other_block_append p g b1' b2.
536#p #g * #b1 #b1prf * #b1' #b1prf' #b2 #EQ destruct(EQ) %
537qed.
538
539(*
540lemma is_seq_block_instance_append : ∀p,g,is_seq.
541  ∀B1.
542  ∀B2 : bind_seq_block p g is_seq.
543  ∀l1,l2.
544  ∀b1 : Σb.is_safe_block p g b.
545  ∀b2 : seq_block p g.
546  instantiates_to ? (seq_block p g) B1 l1 (pi1 … b1) →
547  instantiates_to ? (seq_block p g) B2 l2 b2 →
548  instantiates_to ? (seq_block p g) (B1 @ B2) (l1 @ l2) (b1 @ b2).
549#p #g * #B1 elim B1 -B1
550[ #B1 | #f1 #IH1 ]
551#B1prf whd in B1prf;
552#B2 * [2,4: #r1 #l1' ] #l2 #b1 #b2 [1,4: *]
553whd in ⊢ (%→?);
554[ @IH1
555| #EQ destruct(EQ) lapply b2 -b2 lapply l2 -l2 elim B2 -B2
556  [ #B2 | #f2 #IH2] * [2,4: #r2 #l2'] #b2 [1,4: *]
557  whd in ⊢ (%→?);
558  [ @IH2
559  | #EQ' whd destruct @seq_block_append_proof_irr %
560  ]
561]
562qed.
563
564lemma is_other_block_instance_append : ∀p,g.
565  ∀B1 : Σb.bind_pred ? (λx.block_classifier p g x = cl_other) b.
566  ∀B2 : bind_other_block p g.
567  ∀l1,l2.
568  ∀b1 : Σb.block_classifier p g b = cl_other.
569  ∀b2 : other_block p g.
570  instantiates_to ? (other_block p g) B1 l1 (pi1 … b1) →
571  instantiates_to ? (other_block p g) B2 l2 b2 →
572  instantiates_to ? (other_block p g) (B1 @@ B2) (l1 @ l2) (b1 @@ b2).
573#p #g * #B1 elim B1 -B1
574[ #B1 | #f1 #IH1 ]
575#B1prf whd in B1prf;
576#B2 * [2,4: #r1 #l1' ] #l2 #b1 #b2 [1,4: *]
577whd in ⊢ (%→?);
578[ @IH1
579| #EQ destruct(EQ) lapply b2 -b2 lapply l2 -l2 elim B2 -B2
580  [ #B2 | #f2 #IH2] * [2,4: #r2 #l2'] #b2 [1,4: *]
581  whd in ⊢ (%→?);
582  [ @IH2
583  | #EQ' whd destruct @other_block_append_proof_irr %
584  ]
585]
586qed.
587
588lemma other_fin_step_has_one_label :
589  ∀p,g.∀s:(Σs.fin_step_classifier p g s = cl_other).
590  match fin_step_labels ?? s with
591  [ nil ⇒ False
592  | cons _ tl ⇒
593    match tl with
594    [ nil ⇒ True
595    | _ ⇒ False
596    ]
597  ].
598#p #g ** [#lbl || #ext]
599normalize
600[3: cases (ext_fin_step_flows p ext)
601  [* [2: #lbl' * [2: #lbl'' #tl']]] normalize nodelta ]
602#EQ destruct %
603qed.
604
605definition label_of_other_fin_step : ∀p,g.
606  (Σs.fin_step_classifier p g s = cl_other) → label ≝
607λp,g,s.
608match fin_step_labels p ? s return λx.match x with [ nil ⇒ ? | cons _ tl ⇒ ?] → ? with
609[ nil ⇒ Ⓧ
610| cons lbl tl ⇒ λ_.lbl
611] (other_fin_step_has_one_label p g s).
612
613(*
614definition point_seq_transition : ∀p,g.codeT p g →
615  code_point p → code_point p → Prop ≝
616  λp,g,c,src,dst.
617  match stmt_at … c src with
618  [ Some stmt ⇒ match stmt with
619    [ sequential sq nxt ⇒
620      point_of_succ … src nxt = dst
621    | final fn ⇒
622      match fin_step_labels … fn with
623      [ nil ⇒ False
624      | cons lbl tl ⇒
625        match tl with
626        [ nil ⇒ point_of_label … c lbl = Some ? dst
627        | _ ⇒ False
628        ]
629      ]
630    ]
631  | None ⇒ False
632  ].
633
634lemma point_seq_transition_label_of_other_fin_step :
635  ∀p,c,src.∀s : (Σs.fin_step_classifier p s = cl_other).∀dst.
636  stmt_at ?? c src = Some ? s →
637  point_seq_transition p c src dst →
638  point_of_label … c (label_of_other_fin_step p s) = Some ? dst.
639#p #c #src ** [#lbl || #ext] #EQ1
640#dst #EQ2
641whd in match point_seq_transition; normalize nodelta
642>EQ2 normalize nodelta whd in ⊢ (?→??(????%)?);
643[#H @H | * ]
644lapply (other_fin_step_has_one_label ? «ext,?»)
645cases (fin_step_labels p ? ext) normalize nodelta [*]
646#hd * normalize nodelta [2: #_ #_ *] *
647#H @H
648qed.
649
650lemma point_seq_transition_succ :
651  ∀p,c,src.∀s,nxt.∀dst.
652  stmt_at ?? c src = Some ? (sequential ?? s nxt) →
653  point_seq_transition p c src dst →
654  point_of_succ … src nxt = dst.
655#p #c #src #s #nxt #dst #EQ
656whd in match point_seq_transition; normalize nodelta
657>EQ normalize nodelta #H @H
658qed.
659*)
660
661definition if_other : ∀p,g.∀A : Type[2].seq_or_fin_step p g → A → A → A ≝
662  λp,g,A,c.match seq_or_fin_step_classifier p g c with
663  [ cl_other ⇒ λx,y.x
664  | _ ⇒ λx,y.y
665  ].
666
667definition other_step_in_code ≝
668  λp,g.
669  λc : codeT p g.
670  λsrc : code_point p.
671  λs : seq_or_fin_step p g.
672  match s return λx.if_other p g ? x (code_point p) unit → Prop with
673  [ inl s'' ⇒ λdst.∃n.stmt_at … c src = Some ? (sequential … s'' n) ∧ ?
674  | inr s'' ⇒ λdst.stmt_at … c src = Some ? (final … s'') ∧ ?
675  ].
676  [ whd in dst; cases (seq_or_fin_step_classifier ???) in dst;
677    normalize nodelta [1,2,3: #_ @True |*: #dst
678      @(point_of_succ … src n = dst)]
679  | whd in dst;
680    lapply dst -dst
681    lapply (refl … (seq_or_fin_step_classifier ?? (inr … s'')))
682    cases (seq_or_fin_step_classifier ?? (inr … s'')) in ⊢ (???%→%);
683    normalize nodelta
684    [1,2,3: #_ #_ @True
685    |*: #EQ #dst
686      @(point_of_label … c (label_of_other_fin_step p g «s'', EQ») = Some ? dst)
687    ]
688  ]
689qed.
690
691definition if_other_sig :
692  ∀p,g.∀B,C : Type[0].∀s : Σs.seq_or_fin_step_classifier p g s = cl_other.
693  if_other p g ? s B C → B ≝
694  λp,g,B,C.?.
695  ** #s whd in match (if_other ??????);
696  cases (seq_or_fin_step_classifier ???) normalize nodelta #EQ destruct(EQ)
697  #x @x
698qed.
699
700definition if_other_block_sig :
701  ∀p,g.∀B,C : Type[0].∀b : Σb.block_classifier p g b = cl_other.
702  if_other p g ? (\snd b) B C → B ≝
703  λp,g,B,C.?.
704  ** #l #s
705  #prf #x @(if_other_sig ???? «s, prf» x)
706qed.
707
708coercion other_sig_to_if nocomposites:
709  ∀p,g.∀B,C : Type[0].∀s : Σs.seq_or_fin_step_classifier p g s = cl_other.
710  ∀x : if_other p g ? s B C.B ≝ if_other_sig
711  on _x : if_other ?? Type[0] ??? to ?.
712
713coercion other_block_sig_to_if nocomposites:
714  ∀p,g.∀B,C : Type[0].∀s : Σs.block_classifier p g s = cl_other.
715  ∀x : if_other p g ? (\snd s) B C.B ≝ if_other_block_sig
716  on _x : if_other ?? Type[0] (\snd ?) ?? to ?.
717
718let rec other_list_in_code p g (c : codeT p g)
719  src
720  (b : list (Σs.seq_or_fin_step_classifier p g s = cl_other))
721  dst on b : Prop ≝
722  match b with
723  [ nil ⇒ src = dst
724  | cons hd tl ⇒ ∃mid.
725    other_step_in_code p g c src hd mid ∧ other_list_in_code p g c mid tl dst
726  ].
727
728notation > "x ~❨ B ❩~> y 'in' c" with precedence 56 for
729  @{'block_in_code $c $x $B $y}.
730 
731notation < "hvbox(x ~❨ B ❩~> y \nbsp 'in' \nbsp break c)" with precedence 56 for
732  @{'block_in_code $c $x $B $y}.
733
734interpretation "list in code" 'block_in_code c x B y =
735  (other_list_in_code ?? c x B y).
736
737definition other_block_in_code : ∀p,g.codeT p g →
738  code_point p → ∀b : other_block p g.
739    if_other … (\snd b) (code_point p) unit → Prop ≝
740  λp,g,c,src,b,dst.
741  ∃mid.src ~❨\fst b❩~> mid in c ∧
742  other_step_in_code p g c mid (\snd b) dst.
743
744interpretation "block in code" 'block_in_code c x B y =
745  (other_block_in_code ?? c x B y).
746
747lemma other_list_in_code_append : ∀p,g.∀c : codeT p g.
748  ∀x.∀b1 : list ?.
749  ∀y.∀b2 : list ?.∀z.
750  x ~❨b1❩~> y in c→ y ~❨b2❩~> z in c → x ~❨b1@b2❩~> z in c.
751#p#g#c#x#b1 lapply x -x
752elim b1 [2: ** #hd #hd_prf #tl #IH] #x #y #b2 #z
753[3: #EQ normalize in EQ; destruct #H @H]
754* #mid * normalize nodelta [ *#n ] #H1 #H2 #H3
755whd normalize nodelta %{mid}
756%{(IH … H2 H3)}
757[ %{n} ] @H1
758qed.
759
760lemma other_block_in_code_append : ∀p,g.∀c : codeT p g.∀x.
761  ∀B1 : Σb.block_classifier p g b = cl_other.
762  ∀y.
763  ∀B2 : other_block p g.
764  ∀z.
765  x ~❨B1❩~> y in c → y ~❨B2❩~> z in c →
766  x ~❨B1@@B2❩~> z in c.
767#p#g#c #x ** #hd1 *#tl1 #tl1prf
768#y * #hd2 #tl2 #z
769* #mid1 * #H1 #H2
770* #mid2 * #G1 #G2
771%{mid2} %{G2}
772whd in match (\fst ?);
773@(other_list_in_code_append … H1)
774%{y} %{H2 G1}
775qed.
776
777(*
778definition instr_block_in_function :
779  ∀p : evaluation_params.∀fn : joint_internal_function (globals p) p.
780    code_point p →
781    ∀b : bind_other_block p.
782    ? → Prop ≝
783 λp,fn,src,B,dst.
784 ∃vars,B'.
785  All ? (In ? (joint_if_locals … fn)) vars ∧
786  instantiates_to … B vars B' ∧
787  src ~❨B'❩~> dst in joint_if_code … fn.
788
789interpretation "bind block in function" 'block_in_code fn x B y =
790  (instr_block_in_function ? fn x B y).
791
792lemma instr_block_in_function_trans :
793  ∀p,fn,src.
794  ∀B1 : ΣB.bind_pred ? (λb.block_classifier p b = cl_other) B.
795  ∀mid.
796  ∀B2 : bind_other_block p.
797  ∀dst.
798  src ~❨B1❩~> Some ? mid in fn →
799  mid ~❨B2❩~> dst in fn →
800  src ~❨B1@@B2❩~> dst in fn.
801#p#fn#src*#B1#B1prf#mid#B2#dst
802* #vars1 * #b1 ** #vars1_ok #b1B1 #b1_in
803* #vars2 * #b2 ** #vars2_ok #b2B2 #b2_in
804%{(vars1@vars2)} %{(«b1,instantiates_to_bind_pred … b1B1 B1prf» @@ b2)}
805/4 by All_append, conj, is_other_block_instance_append, other_block_in_code_append/
806qed.
807*)
808*)
809*)
Note: See TracBrowser for help on using the repository browser.