-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTransition.v
380 lines (357 loc) · 11.6 KB
/
Transition.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
(******************************************************************************)
(* Copyright (c) 2019--2022 - Paulo Torrens <paulotorrens AT gnu DOT org> *)
(******************************************************************************)
Require Import Lia.
Require Import Arith.
Require Import List.
Require Import Relations.
Require Import Equality.
Require Import Local.Prelude.
Require Import Local.AbstractRewriting.
Require Import Local.Substitution.
Require Import Local.Syntax.
Require Import Local.Metatheory.
Require Import Local.Context.
Require Import Local.Reduction.
(* TODO: We take only converges from here; might wanna move it to Syntax. *)
Require Import Local.Observational.
(* This labelled transition semantics comes from Merro's paper, "On the
Observational Theory of the CPS-calculus". We note that a label is either a
silent action, tau, or an output action in the form of k<x>M. Merro mentions
that the transition system is necessarily higher-order (carrying the M) in
order to preserve some properties, but this doesn't seem to be necessary,
actually. TODO: remove the M term from the label.
We note that we give a special purpose for the k de Bruijn index in here, as
we are reserving it to be the name of the continuation to jump to. I.e., in
an action k<x>M, we're saying a term performs an action which may interact
with the discriminating context [] { k<x> = M }. *)
Inductive label: Set :=
| label_tau
| label_jmp (k: nat) (ts: list pseudoterm) (b: pseudoterm).
Inductive transition: label -> relation pseudoterm :=
(*
x \notin FV(a<b>)
--------------------------------------- (JMP)
a<x>M
a<b> ---------> M[b/x] { a<x> = M }
*)
| transition_jmp:
forall k xs ts c,
length xs = length ts ->
transition (label_jmp k ts c) (jump 0 xs)
(bind
(apply_parameters xs 0 (lift 1 (length ts) c)) ts c)
(*
a<x>N
M ---------> M' { a<x> = N } a != b b \notin FV(N)
---------------------------------------------------------------- (CTX-JMP)
a<x>N
M { b<y> = O } ---------> M' { b<y> = O } { a<x> = N }
*)
| transition_ctx_jmp:
forall k a b ts c us d,
(* This lift was hinted by the "Proof-relevant pi-calculus" paper! *)
transition
(label_jmp k (traverse_list (lift 1) 0 ts) (lift 1 (length ts) c))
a
(bind b (traverse_list (lift 1) 0 ts) (lift 1 (length ts) c)) ->
transition
(label_jmp k ts c)
(bind (switch_bindings 0 a) us d)
(bind (bind (switch_bindings 0 b) us d) ts c)
(*
a<x>N
M ---------> M'
---------------------------- (TAU)
T
M { a<x> = N } -----> M'
*)
| transition_tau:
forall k a b ts c,
transition (label_jmp k ts c) a b ->
transition label_tau (bind a ts c) b
(*
T
M -----> M'
----------------------------------------- (CTX-TAU)
T
M { a<x> = N } -----> M' { a<x> = N }
*)
| transition_ctx_tau:
forall a b ts c,
transition label_tau a b ->
transition label_tau (bind a ts c) (bind b ts c).
Inductive transition_label_jmp_invariant (k: nat) ts c a b: Prop :=
transition_label_jmp_invariant_ctor
(h: context)
(H1: static h)
(* H2: k = #h *)
(xs: list pseudoterm)
(H3: length xs = length ts)
(H4: a = h (jump #h xs))
(H5: b = bind
(h
(apply_parameters xs 0 (lift (S #h) (length ts) c)))
ts c).
Lemma transition_jmp_preserves_invariant:
forall k ts c a b,
transition (label_jmp k ts c) a b ->
transition_label_jmp_invariant k ts c a b.
Proof.
intros.
dependent induction H.
(* Case: transition_jmp. *)
- apply transition_label_jmp_invariant_ctor with context_hole xs; simpl.
+ constructor.
+ assumption.
+ reflexivity.
+ reflexivity.
(* Case: transition_ctx_jmp. *)
- clear H.
edestruct IHtransition; eauto.
dependent destruction H5.
(* This has all that we need. *)
apply transition_label_jmp_invariant_ctor with
(context_left (context_switch_bindings 0 h) us d)
(map (switch_bindings #h) xs); simpl.
+ constructor.
apply static_context_switch_bindings; auto.
+ rewrite traverse_list_length in H3.
rewrite map_length.
assumption.
+ rewrite context_switch_bindings_is_sound.
rewrite Nat.add_0_r.
f_equal; f_equal.
rewrite switch_bindings_distributes_over_jump.
f_equal.
rewrite switch_bindings_bound_eq; auto.
rewrite context_switch_bindings_bvars.
reflexivity.
+ f_equal; f_equal.
rewrite traverse_list_length.
rewrite lift_lift_simplification; try lia.
rewrite context_switch_bindings_is_sound.
rewrite Nat.add_0_r.
replace (S #h + 1) with (S (S #h)); try lia.
f_equal; clear H1 d us.
(* TODO: I'd like to prove this one with sigma! Can we try later? *)
rewrite switch_bindings_behavior.
rewrite right_cycle_characterization.
rewrite lift_distributes_over_apply_parameters.
simpl sequence.
simpl app.
rewrite apply_parameters_cons.
rewrite apply_parameters_cons.
rewrite apply_parameters_nil.
rewrite subst_distributes_over_apply_parameters.
rewrite map_map; f_equal.
rewrite map_length.
rewrite traverse_list_length in H3.
rewrite context_switch_bindings_bvars.
rewrite lift_lift_simplification by lia.
simpl.
rewrite subst_lift_simplification by lia.
rewrite subst_distributes_over_apply_parameters.
rewrite map_map.
rewrite map_length.
rewrite subst_lift_simplification by lia.
erewrite map_ext; intros.
* reflexivity.
* rewrite switch_bindings_behavior.
rewrite right_cycle_characterization.
simpl sequence.
simpl app.
repeat rewrite apply_parameters_cons.
rewrite apply_parameters_nil.
reflexivity.
Qed.
Local Lemma transition_ctx_jmp_helper:
forall k a b ts c us d e f g h,
transition (label_jmp k e f) a (bind b e f) ->
e = (traverse_list (lift 1) 0 ts) ->
f = (lift 1 (length ts) c) ->
g = (switch_bindings 0 a) ->
h = (bind (switch_bindings 0 b) us d) ->
transition (label_jmp k ts c) (bind g us d) (bind h ts c).
Proof.
intros until 5.
generalize H; clear H.
rewrite H0, H1, H2, H3.
apply transition_ctx_jmp.
Qed.
Lemma transition_tau_longjmp:
forall h,
static h ->
forall xs ts c,
length xs = length ts ->
transition label_tau (bind (h (jump #h xs)) ts c)
(bind (h (apply_parameters xs 0 (lift (S #h) (length ts) c))) ts c).
Proof.
intros.
(* We start by applying (TAU) to fix the binding. *)
apply transition_tau with #h.
generalize xs ts c H0; clear xs ts c H0.
(* Our induction has to happen on #h, not h itself! *)
assert (exists k, k = #h) as (k, ?H); eauto.
generalize #h at 1 as n.
replace #h with k; auto.
generalize dependent h.
(* Now we can proceed to prove it. *)
induction k; intros.
(* Case: zero. *)
- (* Clearly we're at a hole! *)
destruct H; try discriminate; simpl.
(* Immediate jump! *)
apply transition_jmp.
assumption.
(* Case: succ. *)
- (* We clearly have a left context. *)
destruct H; try discriminate.
simpl in H0 |- *.
(* We will apply a (CTX-JMP) here, but there's a lot of housekeeping. *)
eapply transition_ctx_jmp_helper with
(e := traverse_list (lift 1) 0 ts)
(f := lift 1 (length ts) c).
+ apply IHk with
(h := context_switch_bindings 0 h)
(xs := map (switch_bindings #h) xs).
* apply static_context_switch_bindings; auto.
* rewrite context_switch_bindings_bvars; lia.
* rewrite map_length.
rewrite traverse_list_length.
assumption.
+ reflexivity.
+ reflexivity.
+ rewrite context_switch_bindings_is_sound; simpl.
rewrite context_switch_bindings_is_involutive.
rewrite context_switch_bindings_bvars.
rewrite Nat.add_0_r.
rewrite switch_bindings_distributes_over_jump.
rewrite switch_bindings_bound_eq; try lia.
f_equal; f_equal.
rewrite map_switch_bindings_is_involutive; auto.
+ f_equal.
rewrite context_switch_bindings_is_sound.
rewrite context_switch_bindings_is_involutive.
rewrite context_switch_bindings_bvars.
rewrite Nat.add_0_r.
rewrite traverse_list_length.
rewrite lift_lift_simplification; try lia.
replace (S k + 1) with (S (S k)); try lia.
replace k with #h; try lia.
f_equal.
(* Same as in the lemma above... TODO: move this property to its own lemma
to avoid code duplication, and, if possible, try to prove it with the
sigma tactic instead. *)
rewrite switch_bindings_behavior.
rewrite right_cycle_characterization.
rewrite lift_distributes_over_apply_parameters.
simpl sequence.
simpl app.
rewrite apply_parameters_cons.
rewrite apply_parameters_cons.
rewrite apply_parameters_nil.
rewrite subst_distributes_over_apply_parameters.
rewrite map_map; f_equal.
repeat rewrite map_length.
rewrite lift_lift_simplification by lia.
simpl.
rewrite subst_lift_simplification by lia.
rewrite subst_distributes_over_apply_parameters.
rewrite map_map.
repeat rewrite map_length.
rewrite subst_lift_simplification by lia.
erewrite map_ext; intros.
* rewrite map_switch_bindings_is_involutive.
reflexivity.
* rewrite switch_bindings_behavior.
rewrite right_cycle_characterization.
simpl sequence.
simpl app.
repeat rewrite apply_parameters_cons.
rewrite apply_parameters_nil.
reflexivity.
Qed.
Lemma transition_tau_head:
forall a b,
head a b -> transition label_tau a b.
Proof.
intros.
destruct H.
induction H0; simpl.
- apply transition_tau_longjmp; auto.
- apply transition_ctx_tau; auto.
Qed.
Lemma head_transition_tau:
forall a b,
transition label_tau a b -> head a b.
Proof.
intros.
dependent induction H.
(* Case: transition_tau. *)
- clear IHtransition.
edestruct transition_jmp_preserves_invariant; eauto.
rewrite H4.
rewrite H5.
apply (head_longjmp h context_hole); auto with cps.
(* Case: transition_tau_ctx. *)
- apply head_bind_left.
firstorder.
Qed.
Lemma converges_transition_jmp:
forall k ts c a b,
transition (label_jmp k ts c) a b ->
converges a 0.
Proof.
intros.
apply transition_jmp_preserves_invariant in H.
dependent destruction H.
dependent destruction H4.
clear H3 H5 k.
assert (exists k, k = #h + 0) as (k, ?); eauto.
replace #h with k; try lia.
generalize dependent k.
generalize O as n.
induction H1; simpl; intros.
- destruct H.
constructor.
- constructor.
apply IHstatic.
lia.
Qed.
Theorem head_and_transition_tau_are_equivalent:
(* Merro, lemma 2.4 (2). *)
same_relation head (transition label_tau).
Proof.
split; intros.
- exact transition_tau_head.
- exact head_transition_tau.
Qed.
(* -------------------------------------------------------------------------- *)
Lemma tau_eq_dec:
forall l,
{ l = label_tau } + { l <> label_tau }.
Proof.
induction l.
- left; reflexivity.
- right; discriminate.
Defined.
Definition weak (l: label): relation pseudoterm :=
weak_transition transition tau_eq_dec l.
Definition bisi: relation pseudoterm :=
bisimilarity transition tau_eq_dec.
Lemma bisi_refl:
reflexive bisi.
Proof.
apply bisimilarity_refl.
Qed.
Lemma bisi_sym:
symmetric bisi.
Proof.
apply bisimilarity_sym.
Qed.
Lemma bisi_trans:
transitive bisi.
Proof.
apply bisimilarity_trans.
Qed.