agree.v 7.79 KB
Newer Older
1
From algebra Require Export cmra.
2
From algebra Require Import functor 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 (S n) n; agree_is_valid n := agree_is_valid (c (S n)) n |}.
Robbert Krebbers's avatar
Robbert Krebbers committed
33
Next Obligation.
34
  intros c n ?. apply (chain_cauchy c n (S (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.
Robbert Krebbers's avatar
Robbert Krebbers committed
56
Lemma agree_cauchy n (x : agree A) i : {n} x  i  n  x i {i} x n.
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.
63
64
Instance agree_unit : Unit (agree A) := id.
Instance agree_minus : Minus (agree A) := λ x y, x.
65

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

Robbert Krebbers's avatar
Robbert Krebbers committed
95
96
97
98
99
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.
Robbert Krebbers's avatar
Robbert Krebbers committed
100
Lemma agree_includedN n (x y : agree A) : x {n} y  y {n} x  y.
Robbert Krebbers's avatar
Robbert Krebbers committed
101
102
Proof.
  split; [|by intros ?; exists y].
103
  by intros [z Hz]; rewrite Hz assoc agree_idemp.
Robbert Krebbers's avatar
Robbert Krebbers committed
104
Qed.
105
106
107
108
109
110
111
112
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.

113
Definition agree_cmra_mixin : CMRAMixin (agree A).
Robbert Krebbers's avatar
Robbert Krebbers committed
114
115
Proof.
  split; try (apply _ || done).
116
117
  - by intros n x1 x2 Hx y1 y2 Hy.
  - intros n x [? Hx]; split; [by apply agree_valid_S|intros n' ?].
Robbert Krebbers's avatar
Robbert Krebbers committed
118
119
    rewrite (Hx n'); last auto.
    symmetry; apply dist_le with n; try apply Hx; auto.
120
  - intros x; apply agree_idemp.
Robbert Krebbers's avatar
Robbert Krebbers committed
121
  - by intros n x y [(?&?&?) ?].
Robbert Krebbers's avatar
Robbert Krebbers committed
122
  - by intros x y; rewrite agree_included.
123
124
125
  - 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
126
Qed.
127
Canonical Structure agreeRA : cmraT := CMRAT agree_cofe_mixin agree_cmra_mixin.
128

Robbert Krebbers's avatar
Robbert Krebbers committed
129
130
131
132
133
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.
134
Global Instance to_agree_proper : Proper (() ==> ()) to_agree := ne_proper _.
135
Global Instance to_agree_inj n : Inj (dist n) (dist n) (to_agree).
136
Proof. by intros x y [_ Hxy]; apply Hxy. Qed.
137
Lemma to_agree_car n (x : agree A) : {n} x  to_agree (x n) {n} x.
Robbert Krebbers's avatar
Robbert Krebbers committed
138
Proof. intros [??]; split; naive_solver eauto using agree_valid_le. Qed.
139
140

(** Internalized properties *)
141
Lemma agree_equivI {M} a b : (to_agree a  to_agree b)%I  (a  b : uPred M)%I.
142
143
144
Proof.
  uPred.unseal. do 2 split. by intros [? Hv]; apply (Hv n). apply: to_agree_ne.
Qed.
145
Lemma agree_validI {M} x y :  (x  y)  (x  y : uPred M).
146
Proof. uPred.unseal; split=> r n _ ?; by apply: agree_op_inv. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
147
148
End agree.

149
150
151
Arguments agreeC : clear implicits.
Arguments agreeRA : clear implicits.

152
Program Definition agree_map {A B} (f : A  B) (x : agree A) : agree B :=
153
  {| agree_car n := f (x n); agree_is_valid := agree_is_valid x |}.
154
Solve Obligations with auto using agree_valid_S.
155
156
Lemma agree_map_id {A} (x : agree A) : agree_map id x = x.
Proof. by destruct x. Qed.
157
158
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).
159
Proof. done. Qed.
160

Robbert Krebbers's avatar
Robbert Krebbers committed
161
Section agree_map.
162
  Context {A B : cofeT} (f : A  B) `{Hf:  n, Proper (dist n ==> dist n) f}.
163
  Global Instance agree_map_ne n : Proper (dist n ==> dist n) (agree_map f).
Robbert Krebbers's avatar
Robbert Krebbers committed
164
  Proof. by intros x1 x2 Hx; split; simpl; intros; [apply Hx|apply Hf, Hx]. Qed.
165
166
167
168
169
170
  Global Instance agree_map_proper :
    Proper (() ==> ()) (agree_map f) := ne_proper _.
  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
171
  Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
172
    split; [|by intros n x [? Hx]; split; simpl; [|by intros n' ?; rewrite Hx]].
Robbert Krebbers's avatar
Robbert Krebbers committed
173
    intros n x y; rewrite !agree_includedN; intros Hy; rewrite Hy.
Robbert Krebbers's avatar
Robbert Krebbers committed
174
    split; last done; split; simpl; last tauto.
Robbert Krebbers's avatar
Robbert Krebbers committed
175
176
    by intros (?&?&Hxy); repeat split; intros;
       try apply Hxy; try apply Hf; eauto using @agree_valid_le.
Robbert Krebbers's avatar
Robbert Krebbers committed
177
178
  Qed.
End agree_map.
Robbert Krebbers's avatar
Robbert Krebbers committed
179

180
181
182
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
183
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
184
  intros f g Hfg x; split; simpl; intros; first done.
Robbert Krebbers's avatar
Robbert Krebbers committed
185
186
  by apply dist_le with n; try apply Hfg.
Qed.
Ralf Jung's avatar
Ralf Jung committed
187
188
189
190

Program Definition agreeF : iFunctor :=
  {| ifunctor_car := agreeRA; ifunctor_map := @agreeC_map |}.
Solve Obligations with done.