cofe_solver.v 10.1 KB
Newer Older
1
From iris.algebra Require Export ofe.
2 3
(* FIXME: This file needs a 'Proof Using' hint, but the default we use
   everywhere makes for lots of extra ssumptions. *)
Robbert Krebbers's avatar
Robbert Krebbers committed
4

5
Record solution (F : cFunctor) := Solution {
6 7
  solution_car :> ofeT;
  solution_cofe : Cofe solution_car;
8 9
  solution_unfold : solution_car -n> F solution_car;
  solution_fold : F solution_car -n> solution_car;
10 11 12 13 14
  solution_fold_unfold X : solution_fold (solution_unfold X)  X;
  solution_unfold_fold X : solution_unfold (solution_fold X)  X
}.
Arguments solution_unfold {_} _.
Arguments solution_fold {_} _.
15
Existing Instance solution_cofe.
16 17

Module solver. Section solver.
18
Context (F : cFunctor) `{Fcontr : cFunctorContractive F}
19
        `{Fcofe :  T : ofeT, Cofe T  Cofe (F T)} `{Finh : Inhabited (F unitC)}.
20
Notation map := (cFunctor_map F).
Robbert Krebbers's avatar
Robbert Krebbers committed
21

22
Fixpoint A (k : nat) : ofeT :=
23
  match k with 0 => unitC | S k => F (A k) end.
24 25
Local Instance:  k, Cofe (A k).
Proof. induction 0; apply _. Defined.
26 27 28 29 30 31 32 33 34 35 36
Fixpoint f (k : nat) : A k -n> A (S k) :=
  match k with 0 => CofeMor (λ _, inhabitant) | S k => map (g k,f k) end
with g (k : nat) : A (S k) -n> A k :=
  match k with 0 => CofeMor (λ _, ()) | S k => map (f k,g k) end.
Definition f_S k (x : A (S k)) : f (S k) x = map (g k,f k) x := eq_refl.
Definition g_S k (x : A (S (S k))) : g (S k) x = map (f k,g k) x := eq_refl.
Arguments A : simpl never.
Arguments f : simpl never.
Arguments g : simpl never.

Lemma gf {k} (x : A k) : g k (f k x)  x.
Robbert Krebbers's avatar
Robbert Krebbers committed
37 38
Proof.
  induction k as [|k IH]; simpl in *; [by destruct x|].
39
  rewrite -cFunctor_compose -{2}[x]cFunctor_id. by apply (contractive_proper map).
Robbert Krebbers's avatar
Robbert Krebbers committed
40
Qed.
41
Lemma fg {k} (x : A (S (S k))) : f (S k) (g (S k) x) {k} x.
Robbert Krebbers's avatar
Robbert Krebbers committed
42
Proof.
43
  induction k as [|k IH]; simpl.
44 45 46 47
  - rewrite f_S g_S -{2}[x]cFunctor_id -cFunctor_compose.
    apply (contractive_0 map).
  - rewrite f_S g_S -{2}[x]cFunctor_id -cFunctor_compose.
    by apply (contractive_S map).
Robbert Krebbers's avatar
Robbert Krebbers committed
48 49 50 51
Qed.

Record tower := {
  tower_car k :> A k;
52
  g_tower k : g k (tower_car (S k))  tower_car k
Robbert Krebbers's avatar
Robbert Krebbers committed
53 54
}.
Instance tower_equiv : Equiv tower := λ X Y,  k, X k  Y k.
55
Instance tower_dist : Dist tower := λ n X Y,  k, X k {n} Y k.
56
Definition tower_ofe_mixin : OfeMixin tower.
Robbert Krebbers's avatar
Robbert Krebbers committed
57 58
Proof.
  split.
59
  - intros X Y; split; [by intros HXY n k; apply equiv_dist|].
Robbert Krebbers's avatar
Robbert Krebbers committed
60
    intros HXY k; apply equiv_dist; intros n; apply HXY.
61
  - intros k; split.
Robbert Krebbers's avatar
Robbert Krebbers committed
62 63
    + by intros X n.
    + by intros X Y ? n.
64
    + by intros X Y Z ?? n; trans (Y n).
65
  - intros k X Y HXY n; apply dist_S.
Robbert Krebbers's avatar
Robbert Krebbers committed
66
    by rewrite -(g_tower X) (HXY (S n)) g_tower.
Robbert Krebbers's avatar
Robbert Krebbers committed
67
Qed.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
Definition T : ofeT := OfeT tower tower_ofe_mixin.

Program Definition tower_chain (c : chain T) (k : nat) : chain (A k) :=
  {| chain_car i := c i k |}.
Next Obligation. intros c k n i ?; apply (chain_cauchy c n); lia. Qed.
Program Definition tower_compl : Compl T := λ c,
  {| tower_car n := compl (tower_chain c n) |}.
Next Obligation.
  intros c k; apply equiv_dist=> n.
  by rewrite (conv_compl n (tower_chain c k))
    (conv_compl n (tower_chain c (S k))) /= (g_tower (c _) k).
Qed.
Global Program Instance tower_cofe : Cofe T := { compl := tower_compl }.
Next Obligation.
  intros n c k; rewrite /= (conv_compl n (tower_chain c k)).
  apply (chain_cauchy c); lia.
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
85 86

Fixpoint ff {k} (i : nat) : A k -n> A (i + k) :=
87
  match i with 0 => cid | S i => f (i + k)  ff i end.
Robbert Krebbers's avatar
Robbert Krebbers committed
88
Fixpoint gg {k} (i : nat) : A (i + k) -n> A k :=
89
  match i with 0 => cid | S i => gg i  g (i + k) end.
Robbert Krebbers's avatar
Robbert Krebbers committed
90
Lemma ggff {k i} (x : A k) : gg i (ff i x)  x.
Robbert Krebbers's avatar
Robbert Krebbers committed
91
Proof. induction i as [|i IH]; simpl; [done|by rewrite (gf (ff i x)) IH]. Qed.
92 93 94
Lemma f_tower k (X : tower) : f (S k) (X (S k)) {k} X (S (S k)).
Proof. intros. by rewrite -(fg (X (S (S k)))) -(g_tower X). Qed.
Lemma ff_tower k i (X : tower) : ff i (X (S k)) {k} X (i + S k).
Robbert Krebbers's avatar
Robbert Krebbers committed
95 96
Proof.
  intros; induction i as [|i IH]; simpl; [done|].
97
  by rewrite IH Nat.add_succ_r (dist_le _ _ _ _ (f_tower _ X)); last omega.
Robbert Krebbers's avatar
Robbert Krebbers committed
98 99
Qed.
Lemma gg_tower k i (X : tower) : gg i (X (i + k))  X k.
Robbert Krebbers's avatar
Robbert Krebbers committed
100
Proof. by induction i as [|i IH]; simpl; [done|rewrite g_tower IH]. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
101 102 103 104 105 106 107 108 109 110 111 112 113

Instance tower_car_ne n k : Proper (dist n ==> dist n) (λ X, tower_car X k).
Proof. by intros X Y HX. Qed.
Definition project (k : nat) : T -n> A k := CofeMor (λ X : T, tower_car X k).

Definition coerce {i j} (H : i = j) : A i -n> A j :=
  eq_rect _ (λ i', A i -n> A i') cid _ H.
Lemma coerce_id {i} (H : i = i) (x : A i) : coerce H x = x.
Proof. unfold coerce. by rewrite (proof_irrel H (eq_refl i)). Qed.
Lemma coerce_proper {i j} (x y : A i) (H1 H2 : i = j) :
  x = y  coerce H1 x = coerce H2 y.
Proof. by destruct H1; rewrite !coerce_id. Qed.
Lemma g_coerce {k j} (H : S k = S j) (x : A (S k)) :
114
  g j (coerce H x) = coerce (Nat.succ_inj _ _ H) (g k x).
Robbert Krebbers's avatar
Robbert Krebbers committed
115 116
Proof. by assert (k = j) by lia; subst; rewrite !coerce_id. Qed.
Lemma coerce_f {k j} (H : S k = S j) (x : A k) :
117
  coerce H (f k x) = f j (coerce (Nat.succ_inj _ _ H) x).
Robbert Krebbers's avatar
Robbert Krebbers committed
118
Proof. by assert (k = j) by lia; subst; rewrite !coerce_id. Qed.
119
Lemma gg_gg {k i i1 i2 j} :  (H1: k = i + j) (H2: k = i2 + (i1 + j)) (x: A k),
Robbert Krebbers's avatar
Robbert Krebbers committed
120 121
  gg i (coerce H1 x) = gg i1 (gg i2 (coerce H2 x)).
Proof.
122
  intros ? -> x. assert (i = i2 + i1) as -> by lia. revert j x H1.
123
  induction i2 as [|i2 IH]; intros j X H1; simplify_eq/=;
Robbert Krebbers's avatar
Robbert Krebbers committed
124
    [by rewrite coerce_id|by rewrite g_coerce IH].
Robbert Krebbers's avatar
Robbert Krebbers committed
125
Qed.
126
Lemma ff_ff {k i i1 i2 j} :  (H1: i + k = j) (H2: i1 + (i2 + k) = j) (x: A k),
Robbert Krebbers's avatar
Robbert Krebbers committed
127 128
  coerce H1 (ff i x) = coerce H2 (ff i1 (ff i2 x)).
Proof.
129
  intros ? <- x. assert (i = i1 + i2) as -> by lia.
130
  induction i1 as [|i1 IH]; simplify_eq/=;
Robbert Krebbers's avatar
Robbert Krebbers committed
131
    [by rewrite coerce_id|by rewrite coerce_f IH].
Robbert Krebbers's avatar
Robbert Krebbers committed
132 133
Qed.

134
Definition embed_coerce {k} (i : nat) : A k -n> A i :=
Robbert Krebbers's avatar
Robbert Krebbers committed
135 136 137 138
  match le_lt_dec i k with
  | left H => gg (k-i)  coerce (eq_sym (Nat.sub_add _ _ H))
  | right H => coerce (Nat.sub_add k i (Nat.lt_le_incl _ _ H))  ff (i-k)
  end.
139 140
Lemma g_embed_coerce {k i} (x : A k) :
  g i (embed_coerce (S i) x)  embed_coerce i x.
Robbert Krebbers's avatar
Robbert Krebbers committed
141
Proof.
142
  unfold embed_coerce; destruct (le_lt_dec (S i) k), (le_lt_dec i k); simpl.
143 144 145
  - symmetry; by erewrite (@gg_gg _ _ 1 (k - S i)); simpl.
  - exfalso; lia.
  - assert (i = k) by lia; subst.
Robbert Krebbers's avatar
Robbert Krebbers committed
146
    rewrite (ff_ff _ (eq_refl (1 + (0 + k)))) /= gf.
Robbert Krebbers's avatar
Robbert Krebbers committed
147
    by rewrite (gg_gg _ (eq_refl (0 + (0 + k)))).
148
  - assert (H : 1 + ((i - k) + k) = S i) by lia.
Robbert Krebbers's avatar
Robbert Krebbers committed
149
    rewrite (ff_ff _ H) /= -{2}(gf (ff (i - k) x)) g_coerce.
Robbert Krebbers's avatar
Robbert Krebbers committed
150 151
    by erewrite coerce_proper by done.
Qed.
152 153 154 155 156
Program Definition embed (k : nat) (x : A k) : T :=
  {| tower_car n := embed_coerce n x |}.
Next Obligation. intros k x i. apply g_embed_coerce. Qed.
Instance: Params (@embed) 1.
Instance embed_ne k n : Proper (dist n ==> dist n) (embed k).
157
Proof. by intros x y Hxy i; rewrite /= Hxy. Qed.
158 159
Definition embed' (k : nat) : A k -n> T := CofeMor (embed k).
Lemma embed_f k (x : A k) : embed (S k) (f k x)  embed k x.
Robbert Krebbers's avatar
Robbert Krebbers committed
160
Proof.
161
  rewrite equiv_dist=> n i; rewrite /embed /= /embed_coerce.
Robbert Krebbers's avatar
Robbert Krebbers committed
162
  destruct (le_lt_dec i (S k)), (le_lt_dec i k); simpl.
163
  - assert (H : S k = S (k - i) + (0 + i)) by lia; rewrite (gg_gg _ H) /=.
Robbert Krebbers's avatar
Robbert Krebbers committed
164
    by erewrite g_coerce, gf, coerce_proper by done.
165
  - assert (S k = 0 + (0 + i)) as H by lia.
166
    rewrite (gg_gg _ H); simplify_eq/=.
Robbert Krebbers's avatar
Robbert Krebbers committed
167
    by rewrite (ff_ff _ (eq_refl (1 + (0 + k)))).
168 169
  - exfalso; lia.
  - assert (H : (i - S k) + (1 + k) = i) by lia; rewrite (ff_ff _ H) /=.
Robbert Krebbers's avatar
Robbert Krebbers committed
170 171
    by erewrite coerce_proper by done.
Qed.
172
Lemma embed_tower k (X : T) : embed (S k) (X (S k)) {k} X.
Robbert Krebbers's avatar
Robbert Krebbers committed
173
Proof.
174 175
  intros i; rewrite /= /embed_coerce.
  destruct (le_lt_dec i (S k)) as [H|H]; simpl.
176
  - rewrite -(gg_tower i (S k - i) X).
Robbert Krebbers's avatar
Robbert Krebbers committed
177
    apply (_ : Proper (_ ==> _) (gg _)); by destruct (eq_sym _).
178
  - rewrite (ff_tower k (i - S k) X). by destruct (Nat.sub_add _ _ _).
Robbert Krebbers's avatar
Robbert Krebbers committed
179 180
Qed.

181
Program Definition unfold_chain (X : T) : chain (F T) :=
182
  {| chain_car n := map (project n,embed' n) (X (S n)) |}.
Robbert Krebbers's avatar
Robbert Krebbers committed
183 184 185
Next Obligation.
  intros X n i Hi.
  assert ( k, i = k + n) as [k ?] by (exists (i - n); lia); subst; clear Hi.
186
  induction k as [|k IH]; simpl; first done.
187
  rewrite -IH -(dist_le _ _ _ _ (f_tower (k + n) _)); last lia.
188
  rewrite f_S -cFunctor_compose.
189
  by apply (contractive_ne map); split=> Y /=; rewrite ?g_tower ?embed_f.
Robbert Krebbers's avatar
Robbert Krebbers committed
190
Qed.
191
Definition unfold (X : T) : F T := compl (unfold_chain X).
192
Instance unfold_ne : Proper (dist n ==> dist n) unfold.
193
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
194
  intros n X Y HXY. by rewrite /unfold (conv_compl n (unfold_chain X))
195
    (conv_compl n (unfold_chain Y)) /= (HXY (S n)).
196
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
197

198
Program Definition fold (X : F T) : T :=
199
  {| tower_car n := g n (map (embed' n,project n) X) |}.
Robbert Krebbers's avatar
Robbert Krebbers committed
200
Next Obligation.
201
  intros X k. apply (_ : Proper (() ==> ()) (g k)).
202
  rewrite g_S -cFunctor_compose.
203
  apply (contractive_proper map); split=> Y; [apply embed_f|apply g_tower].
Robbert Krebbers's avatar
Robbert Krebbers committed
204
Qed.
205 206
Instance fold_ne : Proper (dist n ==> dist n) fold.
Proof. by intros n X Y HXY k; rewrite /fold /= HXY. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
207

208
Theorem result : solution F.
Robbert Krebbers's avatar
Robbert Krebbers committed
209
Proof.
210
  apply (Solution F T _ (CofeMor unfold) (CofeMor fold)).
211
  - move=> X /=. rewrite equiv_dist=> n k; rewrite /unfold /fold /=.
212
    rewrite -g_tower -(gg_tower _ n); apply (_ : Proper (_ ==> _) (g _)).
213
    trans (map (ff n, gg n) (X (S (n + k)))).
Robbert Krebbers's avatar
Robbert Krebbers committed
214
    { rewrite /unfold (conv_compl n (unfold_chain X)).
215 216
      rewrite -(chain_cauchy (unfold_chain X) n (S (n + k))) /=; last lia.
      rewrite -(dist_le _ _ _ _ (f_tower (n + k) _)); last lia.
217
      rewrite f_S -!cFunctor_compose; apply (contractive_ne map); split=> Y.
218 219 220 221 222 223 224 225 226
      + rewrite /embed' /= /embed_coerce.
        destruct (le_lt_dec _ _); simpl; [exfalso; lia|].
        by rewrite (ff_ff _ (eq_refl (S n + (0 + k)))) /= gf.
      + rewrite /embed' /= /embed_coerce.
        destruct (le_lt_dec _ _); simpl; [|exfalso; lia].
        by rewrite (gg_gg _ (eq_refl (0 + (S n + k)))) /= gf. }
    assert ( i k (x : A (S i + k)) (H : S i + k = i + S k),
      map (ff i, gg i) x  gg i (coerce H x)) as map_ff_gg.
    { intros i; induction i as [|i IH]; intros k' x H; simpl.
227 228
      { by rewrite coerce_id cFunctor_id. }
      rewrite cFunctor_compose g_coerce; apply IH. }
229 230
    assert (H: S n + k = n + S k) by lia.
    rewrite (map_ff_gg _ _ _ H).
231
    apply (_ : Proper (_ ==> _) (gg _)); by destruct H.
232
  - intros X; rewrite equiv_dist=> n /=.
233
    rewrite /unfold /= (conv_compl' n (unfold_chain (fold X))) /=.
234
    rewrite g_S -!cFunctor_compose -{2}[X]cFunctor_id.
235
    apply (contractive_ne map); split => Y /=.
236
    + rewrite f_tower. apply dist_S. by rewrite embed_tower.
237
    + etrans; [apply embed_ne, equiv_dist, g_tower|apply embed_tower].
Robbert Krebbers's avatar
Robbert Krebbers committed
238
Qed.
239
End solver. End solver.