agree.v 7.19 KB
Newer Older
1
Require Export modures.cmra.
Robbert Krebbers's avatar
Robbert Krebbers committed
2 3
Local Hint Extern 10 (_  _) => omega.

4
Record agree (A : Type) : Type := Agree {
Robbert Krebbers's avatar
Robbert Krebbers committed
5 6 7
  agree_car :> nat  A;
  agree_is_valid : nat  Prop;
  agree_valid_0 : agree_is_valid 0;
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 11 12
Arguments Agree {_} _ _ _ _.
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,
Robbert Krebbers's avatar
Robbert Krebbers committed
18 19 20 21
  agree_is_valid x n   n', n'  n  x n' ={n'}= x n.
Lemma agree_valid_le (x : agree A) n n' :
  agree_is_valid x n  n'  n  agree_is_valid x n'.
Proof. induction 2; eauto using agree_valid_S. Qed.
22 23
Instance agree_valid : Valid (agree A) := λ x,  n, {n} x.
Instance agree_equiv : Equiv (agree A) := λ x y,
Robbert Krebbers's avatar
Robbert Krebbers committed
24 25
  ( n, agree_is_valid x n  agree_is_valid y n) 
  ( n, agree_is_valid x n  x n ={n}= y n).
26
Instance agree_dist : Dist (agree A) := λ n x y,
Robbert Krebbers's avatar
Robbert Krebbers committed
27 28
  ( n', n'  n  agree_is_valid x n'  agree_is_valid y n') 
  ( n', n'  n  agree_is_valid x n'  x n' ={n'}= y n').
29
Program Instance agree_compl : Compl (agree A) := λ c,
Robbert Krebbers's avatar
Robbert Krebbers committed
30
  {| agree_car n := c n n; agree_is_valid n := agree_is_valid (c n) n |}.
Robbert Krebbers's avatar
Robbert Krebbers committed
31 32 33 34
Next Obligation. intros; apply agree_valid_0. Qed.
Next Obligation.
  intros c n ?; apply (chain_cauchy c n (S n)), agree_valid_S; auto.
Qed.
35
Definition agree_cofe_mixin : CofeMixin (agree A).
Robbert Krebbers's avatar
Robbert Krebbers committed
36 37 38 39 40 41 42 43 44
Proof.
  split.
  * intros x y; split.
    + by intros Hxy n; split; intros; apply Hxy.
    + by intros Hxy; split; intros; apply Hxy with n.
  * split.
    + 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.
Robbert Krebbers's avatar
Robbert Krebbers committed
45
      - transitivity (agree_is_valid y n'). by apply Hxy. by apply Hyz.
Robbert Krebbers's avatar
Robbert Krebbers committed
46 47 48 49 50 51
      - transitivity (y n'). by apply Hxy. by apply Hyz, Hxy.
  * intros n x y Hxy; split; intros; apply Hxy; auto.
  * intros x y; split; intros n'; rewrite Nat.le_0_r; intros ->; [|done].
    by split; intros; apply agree_valid_0.
  * by intros c n; split; intros; apply (chain_cauchy c).
Qed.
52
Canonical Structure agreeC := CofeT agree_cofe_mixin.
Robbert Krebbers's avatar
Robbert Krebbers committed
53

54
Program Instance agree_op : Op (agree A) := λ x y,
Robbert Krebbers's avatar
Robbert Krebbers committed
55 56
  {| agree_car := x;
     agree_is_valid n := agree_is_valid x n  agree_is_valid y n  x ={n}= y |}.
Robbert Krebbers's avatar
Robbert Krebbers committed
57
Next Obligation. by intros; simpl; split_ands; try apply agree_valid_0. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
58
Next Obligation. naive_solver eauto using agree_valid_S, dist_S. Qed.
59 60
Instance agree_unit : Unit (agree A) := id.
Instance agree_minus : Minus (agree A) := λ x y, x.
Robbert Krebbers's avatar
Robbert Krebbers committed
61
Instance: Commutative () (@op (agree A) _).
62
Proof. intros x y; split; [naive_solver|by intros n (?&?&Hxy); apply Hxy]. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
63
Definition agree_idempotent (x : agree A) : x  x  x.
64
Proof. split; naive_solver. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
65 66 67 68 69 70 71 72 73
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.
  * etransitivity; [apply Hxy|apply Hy]; eauto using agree_valid_le.
  * etransitivity; [apply Hxy|symmetry; apply Hy, Hy'];
      eauto using agree_valid_le.
Qed.
74
Instance: Proper (dist n ==> dist n ==> dist n) (@op (agree A) _).
Robbert Krebbers's avatar
Robbert Krebbers committed
75
Proof. by intros n x1 x2 Hx y1 y2 Hy; rewrite Hy !(commutative _ _ y2) Hx. Qed.
76
Instance: Proper (() ==> () ==> ()) op := ne_proper_2 _.
77 78 79 80 81 82
Instance: Associative () (@op (agree A) _).
Proof.
  intros x y z; split; simpl; intuition;
    repeat match goal with H : agree_is_valid _ _ |- _ => clear H end;
    by cofe_subst; rewrite !agree_idempotent.
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
83 84 85
Lemma agree_includedN (x y : agree A) n : x {n} y  y ={n}= x  y.
Proof.
  split; [|by intros ?; exists y].
Robbert Krebbers's avatar
Robbert Krebbers committed
86
  by intros [z Hz]; rewrite Hz (associative _) agree_idempotent.
Robbert Krebbers's avatar
Robbert Krebbers committed
87
Qed.
88
Definition agree_cmra_mixin : CMRAMixin (agree A).
Robbert Krebbers's avatar
Robbert Krebbers committed
89 90
Proof.
  split; try (apply _ || done).
Robbert Krebbers's avatar
Robbert Krebbers committed
91
  * intros n x y Hxy [? Hx]; split; [by apply Hxy|intros n' ?].
Robbert Krebbers's avatar
Robbert Krebbers committed
92
    rewrite -(proj2 Hxy n') 1?(Hx n'); eauto using agree_valid_le.
Robbert Krebbers's avatar
Robbert Krebbers committed
93
    by apply dist_le with n; try apply Hxy.
Robbert Krebbers's avatar
Robbert Krebbers committed
94
  * by intros n x1 x2 Hx y1 y2 Hy.
Robbert Krebbers's avatar
Robbert Krebbers committed
95 96 97
  * intros x; split; [apply agree_valid_0|].
    by intros n'; rewrite Nat.le_0_r; intros ->.
  * intros n x [? Hx]; split; [by apply agree_valid_S|intros n' ?].
Robbert Krebbers's avatar
Robbert Krebbers committed
98 99
    rewrite (Hx n'); last auto.
    symmetry; apply dist_le with n; try apply Hx; auto.
Robbert Krebbers's avatar
Robbert Krebbers committed
100
  * intros x; apply agree_idempotent.
Robbert Krebbers's avatar
Robbert Krebbers committed
101
  * by intros x y n [(?&?&?) ?].
Robbert Krebbers's avatar
Robbert Krebbers committed
102
  * by intros x y n; rewrite agree_includedN.
Robbert Krebbers's avatar
Robbert Krebbers committed
103 104
Qed.
Lemma agree_op_inv (x y1 y2 : agree A) n :
Robbert Krebbers's avatar
Robbert Krebbers committed
105
  {n} x  x ={n}= y1  y2  y1 ={n}= y2.
Robbert Krebbers's avatar
Robbert Krebbers committed
106
Proof. by intros [??] Hxy; apply Hxy. Qed.
107
Definition agree_cmra_extend_mixin : CMRAExtendMixin (agree A).
Robbert Krebbers's avatar
Robbert Krebbers committed
108
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
109
  intros n x y1 y2 ? Hx; exists (x,x); simpl; split.
Robbert Krebbers's avatar
Robbert Krebbers committed
110
  * by rewrite agree_idempotent.
Robbert Krebbers's avatar
Robbert Krebbers committed
111
  * by rewrite Hx (agree_op_inv x y1 y2) // agree_idempotent.
Robbert Krebbers's avatar
Robbert Krebbers committed
112
Qed.
113 114 115
Canonical Structure agreeRA : cmraT :=
  CMRAT agree_cofe_mixin agree_cmra_mixin agree_cmra_extend_mixin.

Robbert Krebbers's avatar
Robbert Krebbers committed
116 117 118 119 120
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.
Robbert Krebbers's avatar
Robbert Krebbers committed
121
Lemma agree_car_ne (x y : agree A) n : {n} x  x ={n}= y  x n ={n}= y n.
Robbert Krebbers's avatar
Robbert Krebbers committed
122
Proof. by intros [??] Hxy; apply Hxy. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
123
Lemma agree_cauchy (x : agree A) n i : n  i  {i} x  x n ={n}= x i.
Robbert Krebbers's avatar
Robbert Krebbers committed
124 125
Proof. by intros ? [? Hx]; apply Hx. Qed.
Lemma agree_to_agree_inj (x y : agree A) a n :
Robbert Krebbers's avatar
Robbert Krebbers committed
126
  {n} x  x ={n}= to_agree a  y  x n ={n}= a.
Robbert Krebbers's avatar
Robbert Krebbers committed
127
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
128
  by intros; transitivity ((to_agree a  y) n); first apply agree_car_ne.
Robbert Krebbers's avatar
Robbert Krebbers committed
129
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
130 131
End agree.

132 133 134
Arguments agreeC : clear implicits.
Arguments agreeRA : clear implicits.

135
Program Definition agree_map {A B} (f : A  B) (x : agree A) : agree B :=
136 137
  {| agree_car n := f (x n); agree_is_valid := agree_is_valid x |}.
Solve Obligations with auto using agree_valid_0, agree_valid_S.
138 139 140 141 142
Lemma agree_map_id {A} (x : agree A) : agree_map id x = x.
Proof. by destruct x. Qed.
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).
Proof. done. Qed.
143

Robbert Krebbers's avatar
Robbert Krebbers committed
144
Section agree_map.
145
  Context {A B : cofeT} (f : A  B) `{Hf:  n, Proper (dist n ==> dist n) f}.
146
  Global Instance agree_map_ne n : Proper (dist n ==> dist n) (agree_map f).
Robbert Krebbers's avatar
Robbert Krebbers committed
147
  Proof. by intros x1 x2 Hx; split; simpl; intros; [apply Hx|apply Hf, Hx]. Qed.
148 149 150 151 152 153
  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
154
  Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
155
    split; [|by intros n x [? Hx]; split; simpl; [|by intros n' ?; rewrite Hx]].
Robbert Krebbers's avatar
Robbert Krebbers committed
156
    intros x y n; rewrite !agree_includedN; intros Hy; rewrite Hy.
Robbert Krebbers's avatar
Robbert Krebbers committed
157
    split; last done; split; simpl; last tauto.
Robbert Krebbers's avatar
Robbert Krebbers committed
158 159
    by intros (?&?&Hxy); repeat split; intros;
       try apply Hxy; try apply Hf; eauto using @agree_valid_le.
Robbert Krebbers's avatar
Robbert Krebbers committed
160 161
  Qed.
End agree_map.
Robbert Krebbers's avatar
Robbert Krebbers committed
162 163 164

Definition agreeRA_map {A B} (f : A -n> B) : agreeRA A -n> agreeRA B :=
  CofeMor (agree_map f : agreeRA A  agreeRA B).
Robbert Krebbers's avatar
Robbert Krebbers committed
165 166
Instance agreeRA_map_ne A B n : Proper (dist n ==> dist n) (@agreeRA_map A B).
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
167
  intros f g Hfg x; split; simpl; intros; first done.
Robbert Krebbers's avatar
Robbert Krebbers committed
168 169
  by apply dist_le with n; try apply Hfg.
Qed.