aboutsummaryrefslogtreecommitdiffstats
path: root/complete.v
blob: 9939b5085fb7f86dbde08483dee329867779567a (plain) (blame)
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
Require Import Coq.Strings.String.
From stdpp Require Import gmap relations.
From mininix Require Import binop expr interpreter maptools matching sem.

Lemma eval_le n uf e v' :
  eval n uf e = Some v' →
  ∀ m, n ≤ m → eval m uf e = Some v'.
Proof.
  revert uf e v'.
  induction n; [discriminate|].
  intros uf e v' Heval [] Hle; [lia|].
  apply le_S_n in Hle.
  rewrite eval_S in *.
  destruct e; repeat (case_match || simplify_option_eq || by eauto).
  apply bind_Some. exists H.
  split; try by reflexivity.
  apply map_mapM_Some_L in Heqo.
  apply map_mapM_Some_L.
  eapply map_Forall2_impl_L, Heqo.
  eauto.
Qed.

Lemma eval_value n (v : value) : 0 < n → eval n false v = Some v.
Proof. destruct n, v; by try lia. Qed.

Lemma eval_strong_value n (sv : strong_value) :
  0 < n →
  eval n false sv = Some (value_from_strong_value sv).
Proof. destruct n, sv; by try lia. Qed.

Lemma eval_force_strong_value v : ∃ n,
  eval n true (expr_from_strong_value v) = Some (value_from_strong_value v).
Proof.
  induction v using strong_value_ind'; try by exists 2.
  unfold expr_from_strong_value. simpl.
  fold expr_from_strong_value.
  induction bs using map_ind.
  + by exists 2.
  + apply map_Forall_insert in H as [[n Hn] H2]; try done.
    apply IHbs in H2 as [o Ho]. clear IHbs.
    exists (S (n `max` o)).
    rewrite eval_S. csimpl.
    setoid_rewrite map_mapM_Some_2_L
    with (m2 := value_from_strong_value <$> <[i := x]>m); csimpl.
    * by rewrite <-map_fmap_compose.
    * destruct o as [|o]; csimpl in *; try discriminate.
      apply map_Forall2_alt_L.
      split.
      -- set_solver.
      -- intros j u v ??. rewrite eval_S in Ho.
         apply bind_Some in Ho as [vs [Ho1 Ho2]].
         setoid_rewrite map_mapM_Some_L in Ho1. simplify_eq.
         unfold expr_from_strong_value in Ho2.
         rewrite map_fmap_compose in Ho2.
         simplify_eq.
         destruct (decide (i = j)).
         ++ simplify_eq.
            repeat rewrite lookup_fmap, lookup_insert in *.
            simplify_eq/=.
            apply eval_le with (n := n); lia || done.
         ++ repeat rewrite lookup_fmap, lookup_insert_ne in * by done.
            repeat rewrite <-lookup_fmap in *.
            apply map_Forall2_alt_L in Ho1.
            destruct Ho1 as [Ho1 Ho2].
            apply eval_le with (n := o); try lia.
            by apply Ho2 with (i := j).
Qed.

Lemma eval_force_strong_value' v :
  ∃ n, eval n false (X_Force (expr_from_strong_value v)) =
       Some (value_from_strong_value v).
Proof.
  destruct (eval_force_strong_value v) as [n Heval].
  exists (S n). by rewrite eval_S.
Qed.

Lemma rec_subst_lookup bs x e :
  bs !! x = Some (B_Nonrec e) → rec_subst bs !! x = Some e.
Proof. unfold rec_subst. rewrite lookup_fmap. by intros ->. Qed.

Lemma rec_subst_lookup_fmap bs x e :
  bs !! x = Some e → rec_subst (B_Nonrec <$> bs) !! x = Some e.
Proof. unfold rec_subst. do 2 rewrite lookup_fmap. by intros ->. Qed.

Lemma rec_subst_lookup_fmap' bs x :
  is_Some (bs !! x)is_Some (rec_subst (B_Nonrec <$> bs) !! x).
Proof.
  unfold rec_subst. split;
    do 2 rewrite lookup_fmap in *;
    intros [b H]; by simplify_option_eq.
Qed.

Lemma eval_base_step uf e e' v'' n :
  e -->base e' →
  eval n uf e' = Some v'' →
  ∃ m, eval m uf e = Some v''.
Proof.
  intros [] Heval.
  - (* E_Force *)
    destruct uf.
    + (* true *)
      exists (S n). rewrite eval_S. by csimpl.
    + (* false *)
      destruct n; try discriminate.
      rewrite eval_strong_value in Heval by lia.
      simplify_option_eq.
      apply eval_force_strong_value'.
  - (* E_Closed *)
    exists (S n). rewrite eval_S. by csimpl.
  - (* E_Placeholder *)
    exists (S n). rewrite eval_S. by csimpl.
  - (* E_MSelect *)
    destruct n; try discriminate.
    rewrite eval_S in Heval. simplify_option_eq.
    destruct ys. simplify_option_eq.
    destruct n as [|[|n]]; try discriminate.
    rewrite eval_S in Heqo. simplify_option_eq.
    rewrite eval_S in Heqo1. simplify_option_eq.
    exists (S (S (S (S n)))). rewrite eval_S. simplify_option_eq.
    rewrite eval_value by lia. simplify_option_eq.
    rewrite eval_S. simplify_option_eq.
    rewrite eval_le with (n := S n) (v' := V_Attrset H0) by done || lia.
    by simplify_option_eq.
  - (* E_Select *)
    exists (S n). rewrite eval_S. csimpl.
    apply bind_Some. exists (V_Attrset (<[x := e0]>bs)).
    destruct n; try discriminate. split; [done|].
    apply bind_Some. exists (<[x := e0]>bs). split; [done|].
    rewrite lookup_insert. by simplify_option_eq.
  - (* E_SelectOr *)
    destruct n; try discriminate.
    rewrite eval_S in Heval. simplify_option_eq.
    destruct n as [|[|n]]; try discriminate.
    rewrite eval_S in Heqo. simplify_option_eq.
    rewrite eval_S in Heqo0. simplify_option_eq.
    exists (S (S (S n))). rewrite eval_S. simplify_option_eq.
    rewrite eval_value by lia. simplify_option_eq.
    case_match.
    + rewrite bool_decide_eq_true in H. destruct H as [d Hd].
      simplify_option_eq. rewrite eval_S in Heval. simplify_option_eq.
      rewrite eval_S in Heqo. simplify_option_eq.
      apply eval_le with (n := S n); done || lia.
    + rewrite bool_decide_eq_false in H. apply eq_None_not_Some in H.
      by simplify_option_eq.
  - (* E_MSelectOr *)
    destruct n; try discriminate.
    rewrite eval_S in Heval. simplify_option_eq.
    destruct ys. simplify_option_eq.
    destruct n as [|[|n]]; try discriminate.
    rewrite eval_S in Heqo. simplify_option_eq.
    rewrite eval_S in Heqo0. simplify_option_eq.
    exists (S (S (S n))). rewrite eval_S. simplify_option_eq.
    rewrite eval_value by lia. simplify_option_eq.
    case_match.
    + rewrite bool_decide_eq_true in H. destruct H as [d Hd].
      simplify_option_eq. rewrite eval_S in Heval. simplify_option_eq.
      rewrite eval_S in Heqo. simplify_option_eq.
      destruct n; try discriminate. rewrite eval_S in Heqo0.
      simplify_option_eq. rewrite eval_S.
      simplify_option_eq.
      setoid_rewrite eval_le with (n := S n) (v' := V_Attrset H0); done || lia.
    + rewrite bool_decide_eq_false in H. apply eq_None_not_Some in H.
      by simplify_option_eq.
  - (* E_Rec *)
    exists (S n). rewrite eval_S. by csimpl.
  - (* E_Let *)
    exists (S n). rewrite eval_S. by csimpl.
  - (* E_With *)
    exists (S n). rewrite eval_S. csimpl.
    apply bind_Some. exists (V_Attrset bs).
    by destruct n; try discriminate.
  - (* E_WithNoAttrset *)
    exists (S n). rewrite eval_S. csimpl.
    apply bind_Some. exists v1.
    destruct v1; try by destruct n; try discriminate.
    exfalso. apply H. by exists bs.
  - (* E_ApplySimple *)
    exists (S n). rewrite eval_S. simpl.
    apply bind_Some. exists (V_Fn x e1).
    split; [|assumption].
    destruct n; try discriminate; reflexivity.
  - (* E_ApplyAttrset *)
    exists (S n). rewrite eval_S. csimpl.
    apply bind_Some. exists (V_AttrsetFn m e0).
    destruct n; try discriminate. split; [done|].
    apply bind_Some. exists (V_Attrset bs). split; [done|].
    apply bind_Some. exists bs. split; [done|].
    apply bind_Some. apply matches_complete in H.
    by exists bs'.
  - (* E_ApplyFunctor *)
    exists (S n). rewrite eval_S. csimpl.
    apply bind_Some. exists (V_Attrset (<["__functor" := e2]>bs)).
    destruct n; try discriminate. split; [done|].
    rewrite lookup_insert. by simplify_option_eq.
  - (* E_IfTrue *)
    exists (S n). rewrite eval_S. csimpl.
    apply bind_Some. exists true.
    by destruct n; try discriminate.
  - (* E_IfFalse *)
    exists (S n). rewrite eval_S. csimpl.
    apply bind_Some. exists false.
    by destruct n; try discriminate.
  - (* E_Op *)
    exists (S n). rewrite eval_S. simpl.
    apply bind_Some. exists v1.
    destruct n; try discriminate.
    split.
    + apply eval_value. lia.
    + apply bind_Some. exists v2. split.
      * apply eval_value. lia.
      * apply binop_eval_complete in H.
        apply bind_Some. by exists u.
  - (* E_OpHasAttrTrue *)
    exists (S n). rewrite eval_S. csimpl.
    apply bind_Some. exists (V_Attrset bs).
    destruct n; try discriminate. rewrite eval_S in Heval.
    simplify_option_eq. by rewrite bool_decide_eq_true_2.
  - (* E_OpHasAttrFalse *)
    exists (S n). rewrite eval_S. csimpl.
    apply bind_Some. exists (V_Attrset bs).
    destruct n; try discriminate. rewrite eval_S in Heval.
    simplify_option_eq. rewrite eq_None_not_Some in H.
    by rewrite bool_decide_eq_false_2.
  - (* E_OpHasAttrNoAttrset *)
    exists (S n). rewrite eval_S. csimpl.
    destruct n; try discriminate. rewrite eval_S in Heval.
    simplify_option_eq.
    apply bind_Some. exists v.
    split; [apply eval_value; lia|].
    case_match; try done.
    exfalso. apply H. by exists bs.
  - (* E_Assert *)
    exists (S n). rewrite eval_S. csimpl.
    apply bind_Some. exists true.
    split; [by destruct n | done].
Qed.

Lemma eval_step_ctx : ∀ e e' E uf_ext uf_int n v'',
  is_ctx uf_ext uf_int E →
  e -->base e' →
  eval n uf_ext (E e') = Some v'' →
  ∃ m, eval m uf_ext (E e) = Some v''.
Proof.
  intros e e' E uf_in uf_out n v'' Hctx Hbstep.
  revert v''.
  induction Hctx.
  - intros. by apply eval_base_step with (e' := e') (n := n).
  - inv H; intros.
    + destruct n as [|n]; try discriminate.
      rewrite eval_S in H. simplify_option_eq.
      destruct xs. simplify_option_eq.
      apply eval_le with (m := S n) in Heqo; try lia.
      apply IHHctx in Heqo as [m He].
      exists (S (n `max` m)).
      rewrite eval_S. simplify_option_eq.
      rewrite eval_le with (n := m) (v' := V_Attrset H1) by lia || done.
      simplify_option_eq.
      case_match; by rewrite eval_le with (n := n) (v' := v'') by lia || done.
    + intros.
      destruct n as [|n]; try discriminate.
      rewrite eval_S in H. simplify_option_eq.
      destruct xs. simplify_option_eq.
      apply eval_le with (m := S n) in Heqo; try lia.
      apply IHHctx in Heqo as [m He].
      exists (S (n `max` m)).
      rewrite eval_S. simplify_option_eq.
      setoid_rewrite eval_le with (n := m); try lia || done.
      simplify_option_eq. repeat case_match;
        apply eval_le with (n := n); lia || done.
    + intros.
      destruct n as [|n]; try discriminate.
      rewrite eval_S in H. simplify_option_eq.
      apply eval_le with (m := S n) in Heqo; try lia.
      apply IHHctx in Heqo as [m He].
      exists (S (n `max` m)).
      rewrite eval_S. simplify_option_eq.
      setoid_rewrite eval_le with (n := m); try lia || done.
      simplify_option_eq. repeat case_match;
        apply eval_le with (n := n); lia || done.
    + (* X_Apply *)
      intros.
      destruct n as [|n]; try discriminate.
      rewrite eval_S in H. simplify_option_eq.
      apply eval_le with (m := S n) in Heqo; try done || lia.
      apply IHHctx in Heqo as [m He].
      exists (S (n `max` m)).
      rewrite eval_S. simplify_option_eq.
      destruct H0; try discriminate.
      * setoid_rewrite eval_le with (n := m); try done || lia.
        simplify_option_eq.
        setoid_rewrite eval_le with (n := n); done || lia.
      * setoid_rewrite eval_le with (n := m); try done || lia.
        simplify_option_eq.
        rewrite eval_le with (n := n) at 1; try done || lia.
        simplify_option_eq.
        setoid_rewrite eval_le with (n := n); done || lia.
      * setoid_rewrite eval_le with (n := m); try done || lia.
        simplify_option_eq.
        setoid_rewrite eval_le with (n := n); done || lia.
    + intros.
      destruct n as [|n]; try discriminate.
      rewrite eval_S in H. simplify_option_eq.
      destruct n; try discriminate. rewrite eval_S in Heqo.
      simplify_option_eq.
      apply eval_le with (m := S (S n)) in Heqo; try lia.
      apply IHHctx in Heqo as [o He].
      exists (S (S (n `max` o))).
      rewrite eval_S. simplify_option_eq.
      destruct o as [|o]; try discriminate.
      setoid_rewrite eval_le with (n := S o) (v' := V_AttrsetFn m e1);
        try done || lia.
      simplify_option_eq.
      rewrite eval_le with (n := S o) (v' := V_Attrset H1);
        try by rewrite eval_S || lia.
      simplify_option_eq.
      rewrite eval_le with (n := S n) (v' := v''); done || lia.
    + intros.
      destruct n as [|n]; try discriminate.
      rewrite eval_S in H. simplify_option_eq.
      apply eval_le with (m := S n) in Heqo. 2: lia.
      apply IHHctx in Heqo as [m He].
      exists (S (n `max` m)).
      rewrite eval_S. simplify_option_eq.
      rewrite eval_le with (n := m) (v' := H1) by lia || done.
      setoid_rewrite eval_le with (n := n) (v' := v''); try lia || done.
    + intros.
      destruct n as [|n]; try discriminate.
      rewrite eval_S in H. simplify_option_eq.
      apply eval_le with (m := S n) in Heqo. 2: lia.
      apply IHHctx in Heqo as [m He].
      exists (S (n `max` m)).
      destruct H0; try discriminate.
      rewrite eval_S. simplify_option_eq.
      setoid_rewrite eval_le with (n := m) (v' := p); try lia || done.
      simplify_option_eq. destruct p; try discriminate.
      apply eval_le with (n := n); lia || done.
    + intros.
      destruct n as [|n]; try discriminate.
      rewrite eval_S in H. simplify_option_eq.
      apply eval_le with (m := S n) in Heqo. 2: lia.
      apply IHHctx in Heqo as [m He].
      exists (S (n `max` m)).
      rewrite eval_S. simplify_option_eq.
      rewrite eval_le with (n := n) (v' := H1) by lia || done.
      rewrite eval_le with (n := m) (v' := H0) by lia || done.
      simplify_option_eq.
      apply eval_le with (n := n); lia || done.
    + intros.
      destruct n as [|n]; try discriminate.
      rewrite eval_S in H. simplify_option_eq.
      apply eval_le with (m := S n) in Heqo0. 2: lia.
      apply IHHctx in Heqo0 as [m He].
      exists (S (n `max` m)).
      rewrite eval_S. simplify_option_eq.
      rewrite eval_le with (n := m) (v' := H1) by lia || done.
      rewrite eval_le with (n := n) (v' := H0) by lia || done.
      simplify_option_eq.
      apply eval_le with (n := n) (v' := v''); lia || done.
    + (* IsCtxItem_OpHasAttrL *)
      intros.
      destruct n as [|n]; try discriminate.
      rewrite eval_S in H. simplify_option_eq.
      apply eval_le with (m := S n) in Heqo. 2: lia.
      apply IHHctx in Heqo as [m He].
      exists (S (n `max` m)).
      rewrite eval_S. simplify_option_eq.
      by rewrite eval_le with (n := m) (v' := H0) by lia || done.
    + intros.
      destruct n as [|n]; try discriminate.
      rewrite eval_S in H. simplify_option_eq.
      apply eval_le with (m := S n) in H. 2: lia.
      apply IHHctx in H as [m He].
      exists (S (n `max` m)).
      rewrite eval_S; simplify_option_eq.
      apply eval_le with (n := m) (v' := v''); lia || done.
    + intros. simplify_option_eq.
      destruct n as [|n]; try discriminate.
      rewrite eval_S in H. simplify_option_eq.
      apply map_mapM_Some_L in Heqo.
      destruct (map_Forall2_destruct _ _ _ _ _ Heqo)
        as [v' [m2' [Hkm2' HeqH0]]]. simplify_option_eq.
      apply map_Forall2_insert_inv in Heqo as [Hinterp HForall2]; try done.
      apply eval_le with (m := S n) in Hinterp; try lia.
      apply IHHctx in Hinterp as [m Hinterp].
      exists (S (n `max` m)).
      rewrite eval_S. simplify_option_eq.
      apply bind_Some. exists (<[x := v']> m2'). split; try done.
      apply map_mapM_Some_L.
      apply map_Forall2_insert_weak.
      * apply eval_le with (n := m); lia || done.
      * apply map_Forall2_impl_L
        with (R1 := λ x y, eval n true x = Some y); try done.
        intros u v Hjuv. by apply eval_le with (n := n); try lia.
Qed.

Lemma eval_step e e' v'' n :
  e --> e' →
  eval n false e' = Some v'' →
  ∃ m, eval m false e = Some v''.
Proof.
 intros. inv H.
 apply (eval_step_ctx e1 e2 E false uf_int n v'' H1 H2 H0).
Qed.

Theorem eval_complete e (v' : value) :
  e -->* v' → ∃ n, eval n false e = Some v'.
Proof.
  intros [steps Hsteps] % rtc_nsteps. revert e v' Hsteps.
  induction steps; intros e e' Hsteps; inv Hsteps.
  - exists 1. apply eval_value. lia.
  - destruct (IHsteps y e') as [n Hn]; try done.
    clear IHsteps. by apply eval_step with (e := e) in Hn.
Qed.