agree.v 8.07 KB
Newer Older
1
2
From iris.algebra Require Export cmra.
From iris.algebra Require Import upred.
Robbert Krebbers's avatar
Robbert Krebbers committed
3
4
Local Hint Extern 10 (_  _) => omega.

5
Record agree (A : Type) : Type := Agree {
Robbert Krebbers's avatar
Robbert Krebbers committed
6
7
  agree_car :> nat  A;
  agree_is_valid : nat  Prop;
Robbert Krebbers's avatar
Robbert Krebbers committed
8
  agree_valid_S n : agree_is_valid (S n)  agree_is_valid n
Robbert Krebbers's avatar
Robbert Krebbers committed
9
}.
10
Arguments Agree {_} _ _ _.
11
12
Arguments agree_car {_} _ _.
Arguments agree_is_valid {_} _ _.
Robbert Krebbers's avatar
Robbert Krebbers committed
13
14

Section agree.
15
Context {A : cofeT}.
Robbert Krebbers's avatar
Robbert Krebbers committed
16

17
Instance agree_validN : ValidN (agree A) := λ n x,
18
  agree_is_valid x n   n', n'  n  x n {n'} x n'.
19
20
Instance agree_valid : Valid (agree A) := λ x,  n, {n} x.

Robbert Krebbers's avatar
Robbert Krebbers committed
21
Lemma agree_valid_le n n' (x : agree A) :
Robbert Krebbers's avatar
Robbert Krebbers committed
22
23
  agree_is_valid x n  n'  n  agree_is_valid x n'.
Proof. induction 2; eauto using agree_valid_S. Qed.
24

25
Instance agree_equiv : Equiv (agree A) := λ x y,
Robbert Krebbers's avatar
Robbert Krebbers committed
26
  ( n, agree_is_valid x n  agree_is_valid y n) 
27
  ( n, agree_is_valid x n  x n {n} y n).
28
Instance agree_dist : Dist (agree A) := λ n x y,
Robbert Krebbers's avatar
Robbert Krebbers committed
29
  ( n', n'  n  agree_is_valid x n'  agree_is_valid y n') 
30
  ( n', n'  n  agree_is_valid x n'  x n' {n'} y n').
31
Program Instance agree_compl : Compl (agree A) := λ c,
32
  {| agree_car n := c n n; agree_is_valid n := agree_is_valid (c n) n |}.
Robbert Krebbers's avatar
Robbert Krebbers committed
33
Next Obligation.
34
  intros c n ?. apply (chain_cauchy c n (S n)), agree_valid_S; auto.
Robbert Krebbers's avatar
Robbert Krebbers committed
35
Qed.
36
Definition agree_cofe_mixin : CofeMixin (agree A).
Robbert Krebbers's avatar
Robbert Krebbers committed
37
38
Proof.
  split.
39
  - intros x y; split.
Robbert Krebbers's avatar
Robbert Krebbers committed
40
41
    + by intros Hxy n; split; intros; apply Hxy.
    + by intros Hxy; split; intros; apply Hxy with n.
42
  - split.
Robbert Krebbers's avatar
Robbert Krebbers committed
43
44
45
    + by split.
    + by intros x y Hxy; split; intros; symmetry; apply Hxy; auto; apply Hxy.
    + intros x y z Hxy Hyz; split; intros n'; intros.
46
47
      * trans (agree_is_valid y n'). by apply Hxy. by apply Hyz.
      * trans (y n'). by apply Hxy. by apply Hyz, Hxy.
48
  - intros n x y Hxy; split; intros; apply Hxy; auto.
Robbert Krebbers's avatar
Robbert Krebbers committed
49
  - intros n c; apply and_wlog_r; intros;
50
      symmetry; apply (chain_cauchy c); naive_solver.
Robbert Krebbers's avatar
Robbert Krebbers committed
51
Qed.
52
Canonical Structure agreeC := CofeT agree_cofe_mixin.
Robbert Krebbers's avatar
Robbert Krebbers committed
53

Robbert Krebbers's avatar
Robbert Krebbers committed
54
Lemma agree_car_ne n (x y : agree A) : {n} x  x {n} y  x n {n} y n.
55
Proof. by intros [??] Hxy; apply Hxy. Qed.
56
Lemma agree_cauchy n (x : agree A) i : {n} x  i  n  x n {i} x i.
57
58
Proof. by intros [? Hx]; apply Hx. Qed.

59
Program Instance agree_op : Op (agree A) := λ x y,
Robbert Krebbers's avatar
Robbert Krebbers committed
60
  {| agree_car := x;
61
     agree_is_valid n := agree_is_valid x n  agree_is_valid y n  x {n} y |}.
Robbert Krebbers's avatar
Robbert Krebbers committed
62
Next Obligation. naive_solver eauto using agree_valid_S, dist_S. Qed.
Ralf Jung's avatar
Ralf Jung committed
63
Instance agree_core : Core (agree A) := id.
64

65
Instance: Comm () (@op (agree A) _).
66
Proof. intros x y; split; [naive_solver|by intros n (?&?&Hxy); apply Hxy]. Qed.
Ralf Jung's avatar
...    
Ralf Jung committed
67
Lemma agree_idemp (x : agree A) : x  x  x.
68
Proof. split; naive_solver. Qed.
69
70
71
Instance:  n : nat, Proper (dist n ==> impl) (@validN (agree A) _ n).
Proof.
  intros n x y Hxy [? Hx]; split; [by apply Hxy|intros n' ?].
72
73
  rewrite -(proj2 Hxy n') -1?(Hx n'); eauto using agree_valid_le.
  symmetry. by apply dist_le with n; try apply Hxy.
74
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
75
76
77
78
79
Instance:  x : agree A, Proper (dist n ==> dist n) (op x).
Proof.
  intros n x y1 y2 [Hy' Hy]; split; [|done].
  split; intros (?&?&Hxy); repeat (intro || split);
    try apply Hy'; eauto using agree_valid_le.
80
81
  - etrans; [apply Hxy|apply Hy]; eauto using agree_valid_le.
  - etrans; [apply Hxy|symmetry; apply Hy, Hy'];
Robbert Krebbers's avatar
Robbert Krebbers committed
82
83
      eauto using agree_valid_le.
Qed.
84
Instance: Proper (dist n ==> dist n ==> dist n) (@op (agree A) _).
85
Proof. by intros n x1 x2 Hx y1 y2 Hy; rewrite Hy !(comm _ _ y2) Hx. Qed.
86
Instance: Proper (() ==> () ==> ()) op := ne_proper_2 _.
87
Instance: Assoc () (@op (agree A) _).
88
89
90
Proof.
  intros x y z; split; simpl; intuition;
    repeat match goal with H : agree_is_valid _ _ |- _ => clear H end;
91
    by cofe_subst; rewrite !agree_idemp.
92
Qed.
93

Robbert Krebbers's avatar
Robbert Krebbers committed
94
95
96
97
98
Lemma agree_included (x y : agree A) : x  y  y  x  y.
Proof.
  split; [|by intros ?; exists y].
  by intros [z Hz]; rewrite Hz assoc agree_idemp.
Qed.
99
100
101
102
103
104
105
106
Lemma agree_op_inv n (x1 x2 : agree A) : {n} (x1  x2)  x1 {n} x2.
Proof. intros Hxy; apply Hxy. Qed.
Lemma agree_valid_includedN n (x y : agree A) : {n} y  x {n} y  x {n} y.
Proof.
  move=> Hval [z Hy]; move: Hval; rewrite Hy.
  by move=> /agree_op_inv->; rewrite agree_idemp.
Qed.

107
Definition agree_cmra_mixin : CMRAMixin (agree A).
Robbert Krebbers's avatar
Robbert Krebbers committed
108
109
Proof.
  split; try (apply _ || done).
110
  - intros n x [? Hx]; split; [by apply agree_valid_S|intros n' ?].
111
    rewrite -(Hx n'); last auto.
Robbert Krebbers's avatar
Robbert Krebbers committed
112
    symmetry; apply dist_le with n; try apply Hx; auto.
113
  - intros x; apply agree_idemp.
Robbert Krebbers's avatar
Robbert Krebbers committed
114
  - by intros n x y [(?&?&?) ?].
115
116
117
  - intros n x y1 y2 Hval Hx; exists (x,x); simpl; split.
    + by rewrite agree_idemp.
    + by move: Hval; rewrite Hx; move=> /agree_op_inv->; rewrite agree_idemp.
Robbert Krebbers's avatar
Robbert Krebbers committed
118
Qed.
119
Canonical Structure agreeR : cmraT := CMRAT agree_cofe_mixin agree_cmra_mixin.
120

Robbert Krebbers's avatar
Robbert Krebbers committed
121
122
123
124
125
Program Definition to_agree (x : A) : agree A :=
  {| agree_car n := x; agree_is_valid n := True |}.
Solve Obligations with done.
Global Instance to_agree_ne n : Proper (dist n ==> dist n) to_agree.
Proof. intros x1 x2 Hx; split; naive_solver eauto using @dist_le. Qed.
126
Global Instance to_agree_proper : Proper (() ==> ()) to_agree := ne_proper _.
127
Global Instance to_agree_inj n : Inj (dist n) (dist n) (to_agree).
128
Proof. by intros x y [_ Hxy]; apply Hxy. Qed.
129
Lemma to_agree_car n (x : agree A) : {n} x  to_agree (x n) {n} x.
Robbert Krebbers's avatar
Robbert Krebbers committed
130
Proof. intros [??]; split; naive_solver eauto using agree_valid_le. Qed.
131
132

(** Internalized properties *)
133
Lemma agree_equivI {M} a b : (to_agree a  to_agree b) ⊣⊢ (a  b : uPred M).
134
135
136
Proof.
  uPred.unseal. do 2 split. by intros [? Hv]; apply (Hv n). apply: to_agree_ne.
Qed.
137
Lemma agree_validI {M} x y :  (x  y)  (x  y : uPred M).
138
Proof. uPred.unseal; split=> r n _ ?; by apply: agree_op_inv. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
139
140
End agree.

141
Arguments agreeC : clear implicits.
142
Arguments agreeR : clear implicits.
143

144
Program Definition agree_map {A B} (f : A  B) (x : agree A) : agree B :=
145
  {| agree_car n := f (x n); agree_is_valid := agree_is_valid x |}.
146
Solve Obligations with auto using agree_valid_S.
147
148
Lemma agree_map_id {A} (x : agree A) : agree_map id x = x.
Proof. by destruct x. Qed.
149
150
Lemma agree_map_compose {A B C} (f : A  B) (g : B  C) (x : agree A) :
  agree_map (g  f) x = agree_map g (agree_map f x).
151
Proof. done. Qed.
152

Robbert Krebbers's avatar
Robbert Krebbers committed
153
Section agree_map.
154
  Context {A B : cofeT} (f : A  B) `{Hf:  n, Proper (dist n ==> dist n) f}.
Robbert Krebbers's avatar
Robbert Krebbers committed
155
  Instance agree_map_ne n : Proper (dist n ==> dist n) (agree_map f).
Robbert Krebbers's avatar
Robbert Krebbers committed
156
  Proof. by intros x1 x2 Hx; split; simpl; intros; [apply Hx|apply Hf, Hx]. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
157
  Instance agree_map_proper : Proper (() ==> ()) (agree_map f) := ne_proper _.
158
159
160
161
  Lemma agree_map_ext (g : A  B) x :
    ( x, f x  g x)  agree_map f x  agree_map g x.
  Proof. by intros Hfg; split; simpl; intros; rewrite ?Hfg. Qed.
  Global Instance agree_map_monotone : CMRAMonotone (agree_map f).
Robbert Krebbers's avatar
Robbert Krebbers committed
162
  Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
163
164
165
166
167
168
    split; first apply _.
    - by intros n x [? Hx]; split; simpl; [|by intros n' ?; rewrite Hx].
    - intros x y; rewrite !agree_included=> ->.
      split; last done; split; simpl; last tauto.
      by intros (?&?&Hxy); repeat split; intros;
        try apply Hxy; try apply Hf; eauto using @agree_valid_le.
Robbert Krebbers's avatar
Robbert Krebbers committed
169
170
  Qed.
End agree_map.
Robbert Krebbers's avatar
Robbert Krebbers committed
171

172
173
174
Definition agreeC_map {A B} (f : A -n> B) : agreeC A -n> agreeC B :=
  CofeMor (agree_map f : agreeC A  agreeC B).
Instance agreeC_map_ne A B n : Proper (dist n ==> dist n) (@agreeC_map A B).
Robbert Krebbers's avatar
Robbert Krebbers committed
175
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
176
  intros f g Hfg x; split; simpl; intros; first done.
Robbert Krebbers's avatar
Robbert Krebbers committed
177
178
  by apply dist_le with n; try apply Hfg.
Qed.
Ralf Jung's avatar
Ralf Jung committed
179

180
181
182
183
Program Definition agreeRF (F : cFunctor) : rFunctor := {|
  rFunctor_car A B := agreeR (cFunctor_car F A B);
  rFunctor_map A1 A2 B1 B2 fg := agreeC_map (cFunctor_map F fg)
|}.
184
185
186
Next Obligation.
  intros ? A1 A2 B1 B2 n ???; simpl. by apply agreeC_map_ne, cFunctor_ne.
Qed.
187
188
189
190
191
192
193
194
Next Obligation.
  intros F A B x; simpl. rewrite -{2}(agree_map_id x).
  apply agree_map_ext=>y. by rewrite cFunctor_id.
Qed.
Next Obligation.
  intros F A1 A2 A3 B1 B2 B3 f g f' g' x; simpl. rewrite -agree_map_compose.
  apply agree_map_ext=>y; apply cFunctor_compose.
Qed.
195
196
197
198
199
200
201

Instance agreeRF_contractive F :
  cFunctorContractive F  rFunctorContractive (agreeRF F).
Proof.
  intros ? A1 A2 B1 B2 n ???; simpl.
  by apply agreeC_map_ne, cFunctor_contractive.
Qed.