source: src/joint/blocks.ma @ 2595

Last change on this file since 2595 was 2595, checked in by tranquil, 7 years ago
  • dropped locals and exit from definition of joint_if_function
  • new definition of blocks to accomodate ERTLptr pass
  • stated properties of translation as axioms
File size: 28.2 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 head_ne : ∀X.(Σl.not_empty X l) → X ≝
68λX,l.
69match l return λl.not_empty ? l → ? with
70[ nil ⇒ Ⓧ
71| cons hd _ ⇒ λ_.hd
72] (pi2 … l).
73
74
75let rec split_on_last_ne_aux X l on l : not_empty X l → (list X) × X ≝
76match l return λx.not_empty ? x → ? with
77[ nil ⇒ Ⓧ
78| cons hd tl ⇒ λ_.
79  match tl return λtl.(not_empty X tl → ?) → ? with
80  [ nil ⇒ λ_.〈[ ], hd〉
81  | cons hd' tl' ⇒ λrec_call.let 〈pre, last〉 ≝ rec_call I in 〈hd :: pre, last〉
82  ] (split_on_last_ne_aux ? tl)
83].
84
85definition split_on_last_ne : ∀X.(Σl.not_empty X l) → (list X)×X ≝
86λX,l.split_on_last_ne_aux … (pi2 … l).
87
88definition last_ne : ∀X.(Σl.not_empty X l) → X ≝ λX,l.\snd (split_on_last_ne … l).
89definition chop_last_ne : ∀X.(Σl.not_empty X l) → list X ≝ λX,l.\fst (split_on_last_ne … l).
90
91lemma split_on_last_ne_def : ∀X,pre,last,prf.split_on_last_ne X «pre@[last], prf» = 〈pre, last〉.
92whd in match split_on_last_ne; normalize nodelta
93#X #pre elim pre -pre [ #last #prf % ]
94#hd * [2: #hd' #tl'] #IH #last *
95[ whd in ⊢ (??%?); >IH % | % ]
96qed.
97
98lemma split_on_last_ne_inv : ∀X,l.
99pi1 ?? l = (let 〈pre, last〉 ≝ split_on_last_ne X l in pre @ [last]).
100whd in match split_on_last_ne; normalize nodelta
101#X * #l elim l -l [ * ]
102#hd * [2: #hd' #tl'] #IH * [2: % ]
103whd in match (split_on_last_ne_aux ???);
104lapply (IH I) @pair_elim #pre #last #_ #EQ >EQ %
105qed.
106
107(* the label input is to accomodate ERTptr pass *)
108definition step_block ≝ λp,globals.Σl : list (code_point p → joint_step p globals).not_empty ? l.
109unification hint 0 ≔ p : params, g;
110S ≟ code_point p → joint_step p g,
111L ≟ list S,
112P ≟ not_empty S
113(*---------------------------------------*)⊢
114step_block p g ≡ Sig L P.
115
116definition fin_block ≝ λp,globals.(list (joint_step p globals))×(joint_fin_step p).
117
118(*definition seq_to_skip_block :
119  ∀p,g,A.seq_block p g A → skip_block p g A
120 ≝ λp,g,A,b.〈\fst b, \snd b〉.
121
122coercion skip_from_seq_block :
123  ∀p,g,A.∀b : seq_block p g A.skip_block p g A ≝
124  seq_to_skip_block on _b : seq_block ??? to skip_block ???.*)
125
126definition bind_step_block ≝ λp.λglobals.
127  bind_new register (step_block p globals).
128
129unification hint 0 ≔ p, g;
130P ≟ step_block p g
131(*---------------------------------------*)⊢
132bind_step_block p g ≡ bind_new register P.
133
134definition bind_fin_block ≝ λp : stmt_params.λglobals.
135  bind_new register (fin_block p globals).
136
137unification hint 0 ≔ p : stmt_params, g;
138P ≟ fin_block p g
139(*---------------------------------------*)⊢
140bind_fin_block p g ≡ bind_new register P.
141
142definition bind_step_list ≝ λp : stmt_params.λglobals.
143  bind_new register (list (joint_step p globals)).
144unification hint 0 ≔ p : stmt_params, g;
145S ≟ joint_step p g,
146L ≟ list S
147(*---------------------------------------*)⊢
148bind_step_list p g ≡ bind_new register L.
149
150definition add_dummy_variance : ∀X,Y : Type[0].list Y → list (X → Y) ≝ λX,Y.map … (λx.λ_.x).
151
152definition ensure_step_block : ∀p : params.∀globals.
153list (joint_step p globals) → step_block p globals ≝
154λp,g,l.match add_dummy_variance … l return λ_.Σl.not_empty ? l with
155[ nil ⇒ «[λ_.NOOP ??], I»
156| cons hd tl ⇒ «hd :: tl, I»
157].
158
159definition ensure_step_list : ∀p,globals.
160list (joint_seq p globals) → list (joint_step p globals) ≝
161λp,g.map … (step_seq …).
162
163definition ensure_step_block_from_seq : ∀p : params.∀globals.
164list (joint_seq p globals) → step_block p globals ≝
165λp,g.ensure_step_block ?? ∘ ensure_step_list ??.
166
167coercion step_block_from_list nocomposites : ∀p : params.∀g.∀l : list (joint_step p g).step_block p g ≝
168ensure_step_block on _l : list (joint_step ??) to step_block ??.
169
170coercion step_list_from_seq nocomposites : ∀p,g.∀l : list (joint_seq p g).list (joint_step p g) ≝
171ensure_step_list on _l : list (joint_seq ??) to list (joint_step ??).
172
173coercion step_block_from_seq nocomposites : ∀p : params.∀g.∀l : list (joint_seq p g).step_block p g ≝
174ensure_step_block_from_seq on _l : list (joint_seq ??) to step_block ??.
175
176
177(*definition bind_skip_block ≝ λp : stmt_params.λglobals,A.
178  bind_new (localsT p) (skip_block p globals A).
179unification hint 0 ≔ p : stmt_params, g, A;
180B ≟ skip_block p g A, R ≟ localsT p
181(*---------------------------------------*)⊢
182bind_skip_block p g A ≡ bind_new R B.
183
184definition bind_seq_to_skip_block :
185  ∀p,g,A.bind_seq_block p g A → bind_skip_block p g A ≝
186  λp,g,A.m_map ? (seq_block p g A) (skip_block p g A)
187    (λx.x).
188
189coercion bind_skip_from_seq_block :
190  ∀p,g,A.∀b:bind_seq_block p g A.bind_skip_block p g A ≝
191  bind_seq_to_skip_block on _b : bind_seq_block ??? to bind_skip_block ???.*)
192(*
193definition block_classifier ≝
194  λp,g.λb : other_block p g.
195  seq_or_fin_step_classifier ?? (\snd b).
196*)
197(*definition seq_block_from_seq_list :
198∀p : stmt_params.∀g.list (joint_seq p g) → seq_block p g (joint_step p g) ≝
199λp,g,l.let 〈pre,post〉 ≝ split_on_last … (NOOP ??) l in 〈pre, (post : joint_step ??)〉.
200
201definition bind_seq_block_from_bind_seq_list :
202  ∀p : stmt_params.∀g.bind_new (localsT p) (list (joint_seq p g)) →
203    bind_seq_block p g (joint_step p g) ≝ λp.λg.m_map … (seq_block_from_seq_list …).
204
205definition bind_seq_block_step :
206  ∀p,g.bind_seq_block p g (joint_step p g) →
207    bind_seq_block p g (joint_step p g) + bind_seq_block p g (joint_fin_step p) ≝
208  λp,g.inl ….
209coercion bind_seq_block_from_step :
210  ∀p,g.∀b:bind_seq_block p g (joint_step p g).
211    bind_seq_block p g (joint_step p g) + bind_seq_block p g (joint_fin_step p) ≝
212  bind_seq_block_step on _b : bind_seq_block ?? (joint_step ??) to
213    bind_seq_block ?? (joint_step ??) + bind_seq_block ?? (joint_fin_step ?).
214
215definition bind_seq_block_fin_step :
216  ∀p,g.bind_seq_block p g (joint_fin_step p) →
217    bind_seq_block p g (joint_step p g) + bind_seq_block p g (joint_fin_step p) ≝
218  λp,g.inr ….
219coercion bind_seq_block_from_fin_step :
220  ∀p,g.∀b:bind_seq_block p g (joint_fin_step p).
221    bind_seq_block p g (joint_step p g) + bind_seq_block p g (joint_fin_step p) ≝
222  bind_seq_block_fin_step on _b : bind_seq_block ?? (joint_fin_step ?) to
223    bind_seq_block ?? (joint_step ??) + bind_seq_block ?? (joint_fin_step ?).
224
225definition seq_block_bind_seq_block :
226  ∀p : stmt_params.∀g,A.seq_block p g A → bind_seq_block p g A ≝ λp,g,A.bret ….
227coercion seq_block_to_bind_seq_block :
228  ∀p : stmt_params.∀g,A.∀b:seq_block p g A.bind_seq_block p g A ≝
229  seq_block_bind_seq_block
230  on _b : seq_block ??? to bind_seq_block ???.
231
232definition joint_step_seq_block : ∀p : stmt_params.∀g.joint_step p g → seq_block p g (joint_step p g) ≝
233  λp,g,x.〈[ ], x〉.
234coercion joint_step_to_seq_block : ∀p : stmt_params.∀g.∀b : joint_step p g.seq_block p g (joint_step p g) ≝
235  joint_step_seq_block on _b : joint_step ?? to seq_block ?? (joint_step ??).
236
237definition joint_fin_step_seq_block : ∀p : stmt_params.∀g.joint_fin_step p → seq_block p g (joint_fin_step p) ≝
238  λp,g,x.〈[ ], x〉.
239coercion joint_fin_step_to_seq_block : ∀p : stmt_params.∀g.∀b : joint_fin_step p.seq_block p g (joint_fin_step p) ≝
240  joint_fin_step_seq_block on _b : joint_fin_step ? to seq_block ?? (joint_fin_step ?).
241
242definition seq_list_seq_block :
243  ∀p:stmt_params.∀g.list (joint_seq p g) → seq_block p g (joint_step p g) ≝
244  λp,g,l.let pr ≝ split_on_last … (NOOP ??) l in 〈\fst pr, \snd pr〉.
245coercion seq_list_to_seq_block :
246  ∀p:stmt_params.∀g.∀l:list (joint_seq p g).seq_block p g (joint_step p g) ≝
247  seq_list_seq_block on _l : list (joint_seq ??) to seq_block ?? (joint_step ??).
248
249definition bind_seq_list_bind_seq_block :
250  ∀p:stmt_params.∀g.bind_new (localsT p) (list (joint_seq p g)) → bind_seq_block p g (joint_step p g) ≝
251  λp,g.m_map ??? (λx : list (joint_seq ??).(x : seq_block ???)).
252
253coercion bind_seq_list_to_bind_seq_block :
254  ∀p:stmt_params.∀g.∀l:bind_new (localsT p) (list (joint_seq p g)).bind_seq_block p g (joint_step p g) ≝
255  bind_seq_list_bind_seq_block on _l : bind_new ? (list (joint_seq ??)) to bind_seq_block ?? (joint_step ??).
256*)
257
258notation > "x ~❨ B , l ❩~> y 'in' c" with precedence 56 for
259  @{'block_in_code $c $x $B $l $y}.
260 
261notation < "hvbox(x ~❨ B , l ❩~> y \nbsp 'in' \nbsp break c)" with precedence 56 for
262  @{'block_in_code $c $x $B $l $y}.
263
264notation > "x ~❨ B , l, r ❩~> y 'in' c" with precedence 56 for
265  @{'bind_block_in_code $c $x $B $l $r $y}.
266 
267notation < "hvbox(x ~❨ B , l, r ❩~> y \nbsp 'in' \nbsp break c)" with precedence 56 for
268  @{'bind_block_in_code $c $x $B $l $r $y}.
269
270definition step_in_code ≝
271  λp,globals.λc : codeT p globals.λsrc : code_point p.λs : joint_step p globals.
272  λdst : code_point p.
273  ∃nxt.stmt_at … c src = Some ? (sequential … s nxt) ∧
274       point_of_succ … src nxt = dst.
275
276definition fin_step_in_code ≝
277  λp,globals.λc : codeT p globals.λsrc : code_point p.λs : joint_fin_step p.
278  stmt_at … c src = Some ? (final … s).
279
280let rec step_list_in_code p globals (c : codeT p globals)
281  (src : code_point p) (B : list (joint_step p globals))
282  (l : list (code_point p)) (dst : code_point p) on B : Prop ≝
283  match B with
284  [ nil ⇒
285    match l with
286    [ nil ⇒ src = dst
287    | _ ⇒ False
288    ]
289  | cons hd tl ⇒
290    match l with
291    [ nil ⇒ False
292    | cons mid rest ⇒
293      step_in_code …  c src hd mid ∧ step_list_in_code … c mid tl rest dst
294    ]
295  ].
296
297interpretation "step list in code" 'block_in_code c x B l y = (step_list_in_code ?? c x B l y).
298
299lemma step_list_in_code_append :
300  ∀p,globals.∀c : codeT p globals.∀src,B1,l1,mid,B2,l2,dst.
301  src ~❨B1,l1❩~> mid in c → mid ~❨B2,l2❩~> dst in c →
302  src ~❨B1@B2,l1@l2❩~> dst in c.
303#p #globals #c #src #B1 lapply src -src elim B1
304[ #src * [2: #mid' #rest] #mid #B2 #l2 #dst [*] #EQ normalize in EQ; destruct(EQ)
305  #H @H
306| #hd #tl #IH #src * [2: #mid' #rest] #mid #B2 #l2 #dst * #H1 #H2
307  #H3 %{H1 (IH … H2 H3)}
308]
309qed.
310
311lemma step_list_in_code_append_inv :
312  ∀p,globals.∀c : codeT p globals.∀src,B1,B2,l,dst.
313  src ~❨B1@B2,l❩~> dst in c →
314  ∃l1,mid,l2.l = l1 @ l2 ∧ src ~❨B1,l1❩~> mid in c ∧ mid ~❨B2,l2❩~> dst in c.
315#p #globals #c #src #B1 lapply src -src elim B1 -B1
316[ #src #B2 #l #dst #H %{[ ]} %{src} %{l} %{H} % %
317| #hd #tl #IH #src #B2 * [2: #mid #rest] #dst * #H1 #H2
318  elim (IH … H2) #l1 * #mid' * #l2 ** #G1 #G2 #G3
319  %{(mid::l1)} %{mid'} %{l2} %{G3} >G1 %{(refl …)}
320  %{H1 G2}
321]
322qed.
323
324lemma append_not_empty_r : ∀X,l1,l2.not_empty X l2 → not_empty ? (l1 @ l2).
325#X #l1 cases l1 -l1 [ #l2 #K @K | #hd #tl #l2 #_ % ] qed.
326
327definition map_eval : ∀X,Y : Type[0].list (X → Y) → X → list Y ≝ λX,Y,l,x.map … (λf.f x) l.
328
329definition step_block_in_code ≝
330  λp,g.λc : codeT p g.λsrc.λb : step_block p g.λl,dst.
331  let 〈pref, last〉 ≝ split_on_last_ne … b in
332  ∃mid.src ~❨map_eval … pref mid, l❩~> mid in c ∧ step_in_code ?? c mid (last mid) dst.
333
334lemma map_compose : ∀X,Y,Z,f,g.∀l : list X.map Y Z f (map X Y g l) = map … (f∘g) l.
335#X #Y #Z #f #g #l elim l -l normalize // qed.
336
337lemma 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.
338#X #Y #f #g #l #H elim l -l normalize // qed.
339
340lemma map_id : ∀X.∀l : list X.map X X (λx.x) l = l.
341#X #l elim l -l normalize // qed.
342
343lemma coerced_step_list_in_code :
344∀p : params.∀g,c,src.∀b : list (joint_step p g).∀l,dst.
345step_block_in_code p g c src b l dst →
346match b with
347[ nil ⇒ step_in_code … c src (NOOP …) dst
348| _ ⇒ step_list_in_code … c src b (l@[dst]) dst
349].
350#p #g #c #src @list_elim_left [2: #last #pref #_ ] #l #dst
351[2: * #src' * cases l [2: #hd' #tl' *] whd in ⊢ (%→?); #EQ destruct #K @K ]
352cases pref -pref [2: #hd #tl ]
353whd in ⊢ (%→?);
354[2: * #src' * cases l [2: #hd' #tl' *] whd in ⊢ (%→?); #EQ destruct #K %{K} % ]
355whd in match (ensure_step_block ???);
356<map_append
357change with ((? :: ?) @ ?) in match (? :: ? @ ?);
358>split_on_last_ne_def normalize nodelta * #mid *
359whd in match (map_eval ????);
360>map_compose >map_id #H1 #H2
361@(step_list_in_code_append … H1) %{H2} %
362qed.
363
364definition fin_block_in_code ≝
365  λp,g.λc:codeT p g.λsrc.λB : fin_block p g.λl.λdst : unit.
366  ∃hd,tl.l = hd @ [tl] ∧
367  src ~❨\fst B, l❩~> tl in c ∧ fin_step_in_code … c tl (\snd B).
368
369interpretation "step block in code" 'block_in_code c x B l y = (step_block_in_code ?? c x B l y).
370interpretation "fin block in code" 'block_in_code c x B l y = (fin_block_in_code ?? c x B l y).
371
372let rec bind_new_instantiates B X
373  (x : X) (b : bind_new B X) (l : list B) on b : Prop ≝
374  match b with
375  [ bret B ⇒
376    match l with
377    [ nil ⇒ x = B
378    | _ ⇒ False
379    ]
380  | bnew f ⇒
381    match l with
382    [ nil ⇒ False
383    | cons r l' ⇒
384      bind_new_instantiates B X x (f r) l'
385    ]
386  ].
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.