aboutsummaryrefslogtreecommitdiffstats
path: root/matching.v
blob: cb09690a6288b3adba6d6819916e289449d22061 (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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
Require Import Coq.Strings.Ascii.
Require Import Coq.Strings.String.
Require Import Coq.NArith.BinNat.
From stdpp Require Import fin_sets gmap option.
From mininix Require Import expr maptools.

Open Scope string_scope.
Open Scope N_scope.
Open Scope Z_scope.
Open Scope nat_scope.

Reserved Notation "bs '~' m '~>' brs" (at level 55).

Inductive matches_r : gmap string expr → matcher → gmap string b_rhs -> Prop :=
  | E_MatchEmpty strict :~ M_Matcher ∅ strict ~> ∅
  | E_MatchAny bs : bs ~ M_Matcher ∅ false ~> ∅
  | E_MatchMandatory bs x e m bs' strict :
      bs !! x = None →
      m !! x = None →
      delete x bs ~ M_Matcher m strict ~> bs'<[x := e]>bs ~ M_Matcher (<[x := M_Mandatory]>m) strict
        ~> <[x := B_Nonrec e]>bs'
  | E_MatchOptAvail bs x d e m bs' strict :
      bs !! x = None →
      m !! x = None →
      delete x bs ~ M_Matcher m strict ~> bs'<[x := d]>bs ~ M_Matcher (<[x := M_Optional e]>m) strict
        ~> <[x := B_Nonrec d]>bs'
  | E_MatchOptDefault bs x e m bs' strict :
      bs !! x = None →
      m !! x = None →
      bs ~ M_Matcher m strict ~> bs' →
      bs ~ M_Matcher (<[x := M_Optional e]>m) strict ~> <[x := B_Rec e]>bs'
where "bs ~ m ~> brs" := (matches_r bs m brs).

Definition map_foldM `{Countable K} `{FinMap K M} `{MBind F} `{MRet F} {A B}
  (f : K → A → B → F B) (init : B) (m : M A) : F B :=
    map_fold (λ i x acc, acc ≫= f i x) (mret init) m.

Definition matches_aux_f (x : string) (rhs : m_rhs)
  (acc : option (gmap string expr * gmap string b_rhs)) :=
    acc ← acc;
    let (bs, brs) := (acc : gmap string expr * gmap string b_rhs) in
    match rhs with
    | M_Mandatory  =>
        e ← bs !! x;
        Some (bs, <[x := B_Nonrec e]>brs)
    | M_Optional e =>
        match bs !! x with
        | Some e' => Some (bs, <[x := B_Nonrec e']>brs)
        | None => Some (bs, <[x := B_Rec e]>brs)
        end
    end.

Definition matches_aux (ms : gmap string m_rhs) (bs : gmap string expr) :
  option (gmap string expr * gmap string b_rhs) :=
    map_fold matches_aux_f (Some (bs,)) ms.

Definition matches (m : matcher) (bs : gmap string expr) :
  option (gmap string b_rhs) :=
    match m with
    | M_Matcher ms strict =>
        guard (strict → dom bs ⊆ matcher_keys m);;
        snd <$> matches_aux ms bs
    end.

Lemma matches_aux_empty bs : matches_aux ∅ bs = Some (bs,).
Proof. done. Qed.

Lemma matches_aux_f_comm (x : string) (rhs : m_rhs) (m : gmap string m_rhs)
  (y z : string) (rhs1 rhs2 : m_rhs)
  (acc : option (gmap string expr * gmap string b_rhs)) :
    m !! x = None → y ≠ z →
    <[x:=rhs]> m !! y = Some rhs1 →
    <[x:=rhs]> m !! z = Some rhs2 →
    matches_aux_f y rhs1 (matches_aux_f z rhs2 acc) =
    matches_aux_f z rhs2 (matches_aux_f y rhs1 acc).
Proof.
  intros Hxm Hyz Hym Hzm.
  unfold matches_aux_f.
  destruct acc.
  repeat (simplify_option_eq || done ||
          destruct (g !! y) eqn:Egy || destruct (g !! z) eqn:Egz ||
          case_match || rewrite insert_commute). done.
Qed.

Lemma matches_aux_insert m bs x rhs :
  m !! x = None →
  matches_aux (<[x := rhs]>m) bs = matches_aux_f x rhs (matches_aux m bs).
Proof.
  intros Hnotin. unfold matches_aux.
  rewrite map_fold_insert_L; try done.
  clear bs.
  intros y z rhs1 rhs2 acc Hyz Hym Hzm.
  by eapply matches_aux_f_comm.
Qed.

Lemma matches_aux_bs m bs bs' brs :
  matches_aux m bs = Some (bs', brs) → bs = bs'.
Proof.
  revert bs bs' brs.
  induction m using map_ind; intros bs bs' brs Hmatches.
  - rewrite matches_aux_empty in Hmatches. congruence.
  - rewrite matches_aux_insert in Hmatches; try done.
    unfold matches_aux_f in Hmatches.
    simplify_option_eq.
    destruct H0.
    case_match; try case_match;
      simplify_option_eq; by apply IHm with (brs := g0).
Qed.

Lemma matches_aux_impl m bs brs x e :
  m !! x = None →
  bs !! x = None →
  matches_aux m (<[x := e]>bs) = Some (<[x := e]>bs, brs) →
  matches_aux m bs = Some (bs, brs).
Proof.
  intros Hxm Hxbs Hmatches.
  revert bs brs Hxm Hxbs Hmatches.
  induction m using map_ind; intros bs brs Hxm Hxbs Hmatches.
  - rewrite matches_aux_empty.
    rewrite matches_aux_empty in Hmatches.
    by simplify_option_eq.
  - rewrite matches_aux_insert in Hmatches by done.
    rewrite matches_aux_insert by done.
    apply lookup_insert_None in Hxm as [Hxm1 Hxm2].
    unfold matches_aux_f in Hmatches. simplify_option_eq.
    destruct H0. case_match.
    + simplify_option_eq.
      rewrite lookup_insert_ne in Heqo0 by done.
      pose proof (IHm bs g0 Hxm1 Hxbs Heqo).
      rewrite H1. by simplify_option_eq.
    + case_match; simplify_option_eq;
        rewrite lookup_insert_ne in H1 by done;
        pose proof (IHm bs g0 Hxm1 Hxbs Heqo);
        rewrite H0; by simplify_option_eq.
Qed.

Lemma matches_aux_impl_2 m bs brs x e :
  m !! x = None →
  bs !! x = None →
  matches_aux m bs = Some (bs, brs) →
  matches_aux m (<[x := e]>bs) = Some (<[x := e]>bs, brs).
Proof.
  intros Hxm Hxbs Hmatches.
  revert bs brs Hxm Hxbs Hmatches.
  induction m using map_ind; intros bs brs Hxm Hxbs Hmatches.
  - rewrite matches_aux_empty.
    rewrite matches_aux_empty in Hmatches.
    by simplify_option_eq.
  - rewrite matches_aux_insert in Hmatches by done.
    rewrite matches_aux_insert by done.
    apply lookup_insert_None in Hxm as [Hxm1 Hxm2].
    unfold matches_aux_f in Hmatches. simplify_option_eq.
    destruct H0. case_match.
    + simplify_option_eq.
      rewrite <-lookup_insert_ne with (i := x) (x := e) in Heqo0 by done.
      pose proof (IHm bs g0 Hxm1 Hxbs Heqo).
      rewrite H1. by simplify_option_eq.
    + case_match; simplify_option_eq;
        rewrite <-lookup_insert_ne with (i := x) (x := e) in H1 by done;
        pose proof (IHm bs g0 Hxm1 Hxbs Heqo);
        rewrite H0; by simplify_option_eq.
Qed.

Lemma matches_aux_dom m bs brs :
  matches_aux m bs = Some (bs, brs) → dom m = dom brs.
Proof.
  revert bs brs.
  induction m using map_ind; intros bs brs Hmatches.
  - rewrite matches_aux_empty in Hmatches. by simplify_eq.
  - rewrite matches_aux_insert in Hmatches by done.
    unfold matches_aux_f in Hmatches. simplify_option_eq. destruct H0.
    case_match; try case_match;
      simplify_option_eq; apply IHm in Heqo; set_solver.
Qed.

Lemma matches_aux_inv m bs brs x e :
  m !! x = None → bs !! x = None → brs !! x = None →
  matches_aux (<[x := M_Mandatory]>m) (<[x := e]>bs) =
    Some (<[x := e]>bs, <[x := B_Nonrec e]>brs) →
  matches_aux m bs = Some (bs, brs).
Proof.
  intros Hxm Hxbs Hxbrs Hmatches.
  rewrite matches_aux_insert in Hmatches by done.
  unfold matches_aux_f in Hmatches. simplify_option_eq.
  destruct H. simplify_option_eq.
  rewrite lookup_insert in Heqo0. simplify_option_eq.
  apply matches_aux_impl in Heqo; try done.
  simplify_option_eq.
  apply matches_aux_dom in Heqo as Hdom.
  rewrite Heqo. do 2 f_equal.
  assert (g0 !! x = None).
    { apply not_elem_of_dom in Hxm.
      rewrite Hdom in Hxm.
      by apply not_elem_of_dom. }
  replace g0 with (delete x (<[x:=B_Nonrec H]> g0));
    try by rewrite delete_insert.
  replace brs with (delete x (<[x:=B_Nonrec H]> brs));
    try by rewrite delete_insert.
  by rewrite H1.
Qed.

Lemma disjoint_union_subseteq_l `{FinSet A C} `{!LeibnizEquiv C} (X Y Z : C) :
  X ## Y → X ## Z → X ∪ Y ⊆ X ∪ Z → Y ⊆ Z.
Proof.
  revert Y Z.
  induction X using set_ind_L; intros Y Z Hdisj1 Hdisj2 Hsubs.
  - by do 2 rewrite union_empty_l_L in Hsubs.
  - do 2 rewrite <-union_assoc_L in Hsubs.
    apply IHX; set_solver.
Qed.

Lemma singleton_notin_union_disjoint `{FinMapDom K M D} {A} (m : M A) (i : K) :
  m !! i = None →
  {[i]} ## dom m.
Proof.
  intros Hlookup. apply disjoint_singleton_l.
  by apply not_elem_of_dom in Hlookup.
Qed.

Lemma matches_step bs brs m strict x :
  matches (M_Matcher (<[x := M_Mandatory]>m) strict) bs = Some brs →
  ∃ e, bs !! x = Some e ∧ brs !! x = Some (B_Nonrec e).
Proof.
  intros Hmatches.
  destruct (decide (is_Some (bs !! x))).
  - destruct i. exists x0. split; [done|].
    unfold matches in Hmatches.
    destruct strict; simplify_option_eq.
    + replace (<[x := M_Mandatory]>m) with (<[x := M_Mandatory]>(delete x m))
      in Heqo0; try by rewrite insert_delete_insert.
      rewrite matches_aux_insert in Heqo0 by apply lookup_delete.
      unfold matches_aux_f in Heqo0. simplify_option_eq.
      destruct H3. simplify_option_eq.
      apply matches_aux_bs in Heqo as Hdom. simplify_option_eq.
      apply lookup_insert.
    + replace (<[x := M_Mandatory]>m) with (<[x := M_Mandatory]>(delete x m))
      in Heqo; try by rewrite insert_delete_insert.
      rewrite matches_aux_insert in Heqo by apply lookup_delete.
      unfold matches_aux_f in Heqo. simplify_option_eq.
      destruct H1. simplify_option_eq.
      apply matches_aux_bs in Heqo0 as Hdom. simplify_option_eq.
      apply lookup_insert.
  - unfold matches in Hmatches.
    destruct strict; simplify_option_eq.
    + replace (<[x := M_Mandatory]>m) with (<[x := M_Mandatory]>(delete x m))
      in Heqo0; try by rewrite insert_delete_insert.
      rewrite matches_aux_insert in Heqo0 by apply lookup_delete.
      unfold matches_aux_f in Heqo0. simplify_option_eq.
      destruct H2. simplify_option_eq.
      apply matches_aux_bs in Heqo as Hdom. simplify_option_eq.
      rewrite Heqo1 in n.
      exfalso. by apply n.
    + replace (<[x := M_Mandatory]>m) with (<[x := M_Mandatory]>(delete x m))
      in Heqo; try by rewrite insert_delete_insert.
      rewrite matches_aux_insert in Heqo by apply lookup_delete.
      unfold matches_aux_f in Heqo. simplify_option_eq.
      destruct H0. simplify_option_eq.
      apply matches_aux_bs in Heqo0 as Hdom. simplify_option_eq.
      rewrite Heqo1 in n.
      exfalso. by apply n.
Qed.

Lemma matches_step' bs brs m strict x :
  matches (M_Matcher (<[x := M_Mandatory]>m) strict) bs = Some brs →
  ∃ e bs' brs',
    bs' !! x = None ∧ bs = <[x := e]>bs' ∧
    brs' !! x = None ∧ brs = <[x := B_Nonrec e]>brs'.
Proof.
  intros Hmatches.
  apply matches_step in Hmatches as He.
  destruct He as [e [He1 He2]].
  exists e, (delete x bs), (delete x brs).
  split_and!; by apply lookup_delete || rewrite insert_delete.
Qed.

Lemma matches_opt_step bs brs m strict x d :
  matches (M_Matcher (<[x := M_Optional d]>m) strict) bs = Some brs →
  (∃ e, bs !! x = Some e ∧ brs !! x = Some (B_Nonrec e)) ∨
  bs !! x = None ∧ brs !! x = Some (B_Rec d).
Proof.
  intros Hmatches.
  destruct (decide (is_Some (bs !! x))).
  - destruct i. left. exists x0. split; [done|].
    unfold matches in Hmatches.
    destruct strict; simplify_option_eq.
    + replace (<[x := M_Optional d]>m) with (<[x := M_Optional d]>(delete x m))
      in Heqo0; try by rewrite insert_delete_insert.
      rewrite matches_aux_insert in Heqo0 by apply lookup_delete.
      unfold matches_aux_f in Heqo0. simplify_option_eq.
      destruct H3. simplify_option_eq.
      apply matches_aux_bs in Heqo as Hdom. simplify_option_eq.
      apply lookup_insert.
    + replace (<[x := M_Optional d]>m) with (<[x := M_Optional d]>(delete x m))
      in Heqo; try by rewrite insert_delete_insert.
      rewrite matches_aux_insert in Heqo by apply lookup_delete.
      unfold matches_aux_f in Heqo. simplify_option_eq.
      destruct H1. simplify_option_eq.
      apply matches_aux_bs in Heqo0 as Hdom. simplify_option_eq.
      apply lookup_insert.
  - unfold matches in Hmatches.
    destruct strict; simplify_option_eq.
    + right. apply eq_None_not_Some in n. split; [done|].
      destruct H0. simplify_option_eq.
      apply matches_aux_bs in Heqo0 as Hbs. subst.
      replace (<[x := M_Optional d]>m) with (<[x := M_Optional d]>(delete x m))
      in Heqo0; try by rewrite insert_delete_insert.
      rewrite matches_aux_insert in Heqo0 by apply lookup_delete.
      unfold matches_aux_f in Heqo0. simplify_option_eq.
      destruct H0. simplify_option_eq.
      apply matches_aux_bs in Heqo as Hdom. simplify_option_eq.
      apply lookup_insert.
    + right. apply eq_None_not_Some in n. split; [done|].
      destruct H. simplify_option_eq.
      apply matches_aux_bs in Heqo as Hbs. subst.
      replace (<[x := M_Optional d]>m) with (<[x := M_Optional d]>(delete x m))
      in Heqo; try by rewrite insert_delete_insert.
      rewrite matches_aux_insert in Heqo by apply lookup_delete.
      unfold matches_aux_f in Heqo. simplify_option_eq.
      destruct H. simplify_option_eq.
      apply matches_aux_bs in Heqo0 as Hdom. simplify_option_eq.
      apply lookup_insert.
Qed.

Lemma matches_opt_step' bs brs m strict x d :
  matches (M_Matcher (<[x := M_Optional d]>m) strict) bs = Some brs →
  (∃ e bs' brs',
     bs' !! x = None ∧ bs = <[x := e]>bs' ∧
     brs' !! x = None ∧ brs = <[x := B_Nonrec e]>brs')(∃ brs',
     bs !! x = None ∧ brs' !! x = None ∧
     brs = <[x := B_Rec d]>brs').
Proof.
  intros Hmatches.
  apply matches_opt_step in Hmatches as He.
  destruct He as [He|He].
  - destruct He as [e [He1 He2]]. left.
    exists e, (delete x bs), (delete x brs).
    split; try split; try split.
    + apply lookup_delete.
    + by rewrite insert_delete.
    + apply lookup_delete.
    + by rewrite insert_delete.
  - destruct He as [He1 He2]. right.
    exists (delete x brs). split; try split; try done.
    + apply lookup_delete.
    + by rewrite insert_delete.
Qed.

Lemma matches_inv m bs brs strict x e :
  m !! x = None → bs !! x = None → brs !! x = None →
  matches (M_Matcher (<[x := M_Mandatory]>m) strict) (<[x := e]>bs) =
    Some (<[x := B_Nonrec e]>brs)matches (M_Matcher m strict) bs = Some brs.
Proof.
  intros Hxm Hxbs Hxbrs Hmatch.
  destruct strict.
  - simplify_option_eq.
    + destruct H0. apply matches_aux_bs in Heqo0 as Hbs.
      simplify_option_eq. by erewrite matches_aux_inv.
    + exfalso. apply H2.
      repeat rewrite dom_insert in H1.
      assert ({[x]} ## dom bs). { by apply singleton_notin_union_disjoint. }
      assert ({[x]} ## dom m). { by apply singleton_notin_union_disjoint. }
      by apply disjoint_union_subseteq_l with (X := {[x]}).
  - simplify_option_eq.
    destruct H. apply matches_aux_bs in Heqo as Hbs.
    simplify_option_eq. by erewrite matches_aux_inv.
Qed.

Lemma matches_aux_avail_inv m bs brs x d e :
  m !! x = None → bs !! x = None → brs !! x = None →
  matches_aux (<[x := M_Optional d]>m) (<[x := e]>bs) =
    Some (<[x := e]>bs, <[x := B_Nonrec e]>brs) →
  matches_aux m bs = Some (bs, brs).
Proof.
  intros Hxm Hxbs Hxbrs Hmatches.
  rewrite matches_aux_insert in Hmatches by done.
  unfold matches_aux_f in Hmatches. simplify_option_eq.
  destruct H. simplify_option_eq.
  apply matches_aux_bs in Heqo as Hbs. subst.
  rewrite lookup_insert in Hmatches. simplify_option_eq.
  apply matches_aux_impl in Heqo; try done.
  simplify_option_eq.
  apply matches_aux_dom in Heqo as Hdom.
  rewrite Heqo. do 2 f_equal.
  assert (g0 !! x = None).
    { apply not_elem_of_dom in Hxm.
      rewrite Hdom in Hxm.
      by apply not_elem_of_dom. }
  replace g0 with (delete x (<[x:=B_Nonrec e]> g0));
    try by rewrite delete_insert.
  replace brs with (delete x (<[x:=B_Nonrec e]> brs));
    try by rewrite delete_insert.
  by rewrite Hmatches.
Qed.

Lemma matches_avail_inv m bs brs strict x d e :
  m !! x = None → bs !! x = None → brs !! x = None →
  matches (M_Matcher (<[x := M_Optional d]>m) strict) (<[x := e]>bs) =
    Some (<[x := B_Nonrec e]>brs)matches (M_Matcher m strict) bs = Some brs.
Proof.
  intros Hxm Hxbs Hxbrs Hmatch.
  destruct strict.
  - simplify_option_eq.
    + destruct H0. apply matches_aux_bs in Heqo0 as Hbs.
      simplify_option_eq. by erewrite matches_aux_avail_inv.
    + exfalso. apply H2.
      repeat rewrite dom_insert in H1.
      assert ({[x]} ## dom bs). { by apply singleton_notin_union_disjoint. }
      assert ({[x]} ## dom m). { by apply singleton_notin_union_disjoint. }
      by apply disjoint_union_subseteq_l with (X := {[x]}).
  - simplify_option_eq.
    destruct H. apply matches_aux_bs in Heqo as Hbs.
    simplify_option_eq. by erewrite matches_aux_avail_inv.
Qed.

Lemma matches_aux_default_inv m bs brs x e :
  m !! x = None → bs !! x = None → brs !! x = None →
  matches_aux (<[x := M_Optional e]>m) bs =
    Some (bs, <[x := B_Rec e]>brs) →
  matches_aux m bs = Some (bs, brs).
Proof.
  intros Hxm Hxbs Hxbrs Hmatches.
  rewrite matches_aux_insert in Hmatches by done.
  unfold matches_aux_f in Hmatches. simplify_option_eq.
  destruct H. simplify_option_eq.
  apply matches_aux_bs in Heqo as Hbs. subst.
  rewrite Hxbs in Hmatches. simplify_option_eq.
  apply matches_aux_dom in Heqo as Hdom.
  do 2 f_equal.
  assert (g0 !! x = None).
    { apply not_elem_of_dom in Hxm.
      rewrite Hdom in Hxm.
      by apply not_elem_of_dom. }
  replace g0 with (delete x (<[x:=B_Rec e]> g0));
    try by rewrite delete_insert.
  replace brs with (delete x (<[x:=B_Rec e]> brs));
    try by rewrite delete_insert.
  by rewrite Hmatches.
Qed.

Lemma matches_default_inv m bs brs strict x e :
  m !! x = None → bs !! x = None → brs !! x = None →
  matches (M_Matcher (<[x := M_Optional e]>m) strict) bs =
    Some (<[x := B_Rec e]>brs)matches (M_Matcher m strict) bs = Some brs.
Proof.
  intros Hxm Hxbs Hxbrs Hmatch.
  destruct strict.
  - simplify_option_eq.
    + destruct H0. apply matches_aux_bs in Heqo0 as Hbs.
      simplify_option_eq. by erewrite matches_aux_default_inv.
    + exfalso. apply H2.
      rewrite dom_insert in H1.
      assert ({[x]} ## dom bs). { by apply singleton_notin_union_disjoint. }
      assert ({[x]} ## dom m). { by apply singleton_notin_union_disjoint. }
      apply disjoint_union_subseteq_l with (X := {[x]}); set_solver.
  - simplify_option_eq.
    destruct H. apply matches_aux_bs in Heqo as Hbs.
    simplify_option_eq. by erewrite matches_aux_default_inv.
Qed.

Theorem matches_sound m bs brs : matches m bs = Some brs → bs ~ m ~> brs.
Proof.
  intros Hmatches.
  destruct m.
  revert strict bs brs Hmatches.
  induction ms using map_ind; intros strict bs brs Hmatches.
  - destruct strict; simplify_option_eq.
    + apply map_dom_empty_subset in H0. simplify_eq. constructor.
    + constructor.
  - destruct x.
    + apply matches_step' in Hmatches as He.
      destruct He as [e [bs' [brs' [Hbs'1 [Hbs'2 [Hbrs'1 Hbrs'2]]]]]].
      subst. constructor; try done.
      rewrite delete_notin by done.
      apply IHms.
      by apply matches_inv in Hmatches.
    + apply matches_opt_step' in Hmatches as He. destruct He as [He|He].
      * destruct He as [d [bs' [brs' [Hibs' [Hbs' [Hibrs' Hbrs']]]]]].
        subst. constructor; try done.
        rewrite delete_notin by done.
        apply IHms.
        by apply matches_avail_inv in Hmatches.
      * destruct He as [brs' [Hibs [Hibrs' Hbrs']]].
        subst. constructor; try done.
        apply IHms.
        by apply matches_default_inv in Hmatches.
Qed.

Theorem matches_complete m bs brs : bs ~ m ~> brs → matches m bs = Some brs.
Proof.
  intros Hbs.
  induction Hbs.
  - unfold matches. by simplify_option_eq.
  - unfold matches. by simplify_option_eq.
  - unfold matches in *. destruct strict.
    + simplify_option_eq.
      * simplify_option_eq. destruct H2. simplify_option_eq.
        apply fmap_Some. exists (<[x := e]>bs, <[x := B_Nonrec e]>g0).
        split; [|done].
        rewrite matches_aux_insert by done.
        apply matches_aux_bs in Heqo0 as Hbs'. simplify_option_eq.
        apply matches_aux_impl_2 with (x := x) (e := e) in Heqo0;
          [| done | apply lookup_delete].
        replace bs with (delete x bs); try by apply delete_notin.
        unfold matches_aux_f.
        simplify_option_eq. apply bind_Some. exists e.
        split; [apply lookup_insert | done].
      * exfalso. apply H4.
        replace bs with (delete x bs); try by apply delete_notin.
        apply map_dom_delete_insert_subseteq_L.
        set_solver.
    + simplify_option_eq. destruct H1. simplify_option_eq.
      apply fmap_Some. exists (<[x := e]>bs, <[x := B_Nonrec e]>g0).
      split; [|done].
      rewrite matches_aux_insert by done.
      apply matches_aux_bs in Heqo as Hbs'. simplify_option_eq.
      apply matches_aux_impl_2 with (x := x) (e := e) in Heqo;
        [| done | apply lookup_delete].
      replace bs with (delete x bs); try by apply delete_notin.
      unfold matches_aux_f.
      simplify_option_eq. apply bind_Some. exists e.
      split; [apply lookup_insert | done].
  - unfold matches in *. destruct strict.
    + simplify_option_eq.
      * simplify_option_eq. destruct H2. simplify_option_eq.
        apply fmap_Some. exists (<[x := d]>bs, <[x := B_Nonrec d]>g0).
        split; [|done].
        rewrite matches_aux_insert by done.
        apply matches_aux_bs in Heqo0 as Hbs'. simplify_option_eq.
        apply matches_aux_impl_2 with (x := x) (e := d) in Heqo0;
          [| done | apply lookup_delete].
        replace bs with (delete x bs); try by apply delete_notin.
        unfold matches_aux_f.
        simplify_option_eq. case_match.
        -- rewrite lookup_insert in H2. congruence.
        -- by rewrite lookup_insert in H2.
      * exfalso. apply H4.
        replace bs with (delete x bs); try by apply delete_notin.
        apply map_dom_delete_insert_subseteq_L.
        set_solver.
    + simplify_option_eq. destruct H1. simplify_option_eq.
      apply fmap_Some. exists (<[x := d]>bs, <[x := B_Nonrec d]>g0).
      split; [|done].
      rewrite matches_aux_insert by done.
      apply matches_aux_bs in Heqo as Hbs'. simplify_option_eq.
      apply matches_aux_impl_2 with (x := x) (e := d) in Heqo;
        [| done | apply lookup_delete].
      replace bs with (delete x bs); try by apply delete_notin.
      unfold matches_aux_f.
      simplify_option_eq. case_match.
      * rewrite lookup_insert in H1. congruence.
      * by rewrite lookup_insert in H1.
  - unfold matches in *. destruct strict.
    + simplify_option_eq.
      * simplify_option_eq. destruct H2. simplify_option_eq.
        apply fmap_Some. exists (bs, <[x := B_Rec e]>g0).
        split; [|done].
        rewrite matches_aux_insert by done.
        apply matches_aux_bs in Heqo0 as Hbs'. simplify_option_eq.
        unfold matches_aux_f.
        by simplify_option_eq.
      * exfalso. apply H4. set_solver.
    + simplify_option_eq. destruct H1. simplify_option_eq.
      apply fmap_Some. exists (bs, <[x := B_Rec e]>g0).
      split; [|done].
      rewrite matches_aux_insert by done.
      apply matches_aux_bs in Heqo as Hbs'. simplify_option_eq.
      unfold matches_aux_f.
      by simplify_option_eq.
Qed.

Theorem matches_correct m bs brs : matches m bs = Some brs ↔ bs ~ m ~> brs.
Proof. split; [apply matches_sound | apply matches_complete]. Qed.

Theorem matches_deterministic m bs brs1 brs2 :
  bs ~ m ~> brs1 → bs ~ m ~> brs2 → brs1 = brs2.
Proof. intros H1 H2. apply matches_correct in H1, H2. congruence. Qed.