auth.v 6.54 KB
Newer Older
1
Require Export modures.excl.
Robbert Krebbers's avatar
Robbert Krebbers committed
2
Local Arguments validN _ _ _ !_ /.
Robbert Krebbers's avatar
Robbert Krebbers committed
3

Robbert Krebbers's avatar
Robbert Krebbers committed
4
Record auth (A : Type) : Type := Auth { authoritative : excl A ; own : A }.
5
Add Printing Constructor auth.
Robbert Krebbers's avatar
Robbert Krebbers committed
6
Arguments Auth {_} _ _.
Robbert Krebbers's avatar
Robbert Krebbers committed
7
Arguments authoritative {_} _.
Robbert Krebbers's avatar
Robbert Krebbers committed
8
Arguments own {_} _.
9
10
Notation "◯ x" := (Auth ExclUnit x) (at level 20).
Notation "● x" := (Auth (Excl x) ) (at level 20).
Robbert Krebbers's avatar
Robbert Krebbers committed
11

Robbert Krebbers's avatar
Robbert Krebbers committed
12
(* COFE *)
13
14
15
16
Section cofe.
Context {A : cofeT}.

Instance auth_equiv : Equiv (auth A) := λ x y,
Robbert Krebbers's avatar
Robbert Krebbers committed
17
  authoritative x  authoritative y  own x  own y.
18
Instance auth_dist : Dist (auth A) := λ n x y,
Robbert Krebbers's avatar
Robbert Krebbers committed
19
20
  authoritative x ={n}= authoritative y  own x ={n}= own y.

21
Global Instance Auth_ne : Proper (dist n ==> dist n ==> dist n) (@Auth A).
Robbert Krebbers's avatar
Robbert Krebbers committed
22
Proof. by split. Qed.
23
Global Instance authoritative_ne: Proper (dist n ==> dist n) (@authoritative A).
Robbert Krebbers's avatar
Robbert Krebbers committed
24
Proof. by destruct 1. Qed.
25
Global Instance own_ne : Proper (dist n ==> dist n) (@own A).
Robbert Krebbers's avatar
Robbert Krebbers committed
26
27
Proof. by destruct 1. Qed.

28
Instance auth_compl : Compl (auth A) := λ c,
Robbert Krebbers's avatar
Robbert Krebbers committed
29
  Auth (compl (chain_map authoritative c)) (compl (chain_map own c)).
30
Definition auth_cofe_mixin : CofeMixin (auth A).
Robbert Krebbers's avatar
Robbert Krebbers committed
31
32
33
34
35
36
37
38
39
40
41
42
43
Proof.
  split.
  * intros x y; unfold dist, auth_dist, equiv, auth_equiv.
    rewrite !equiv_dist; naive_solver.
  * intros n; split.
    + by intros ?; split.
    + by intros ?? [??]; split; symmetry.
    + intros ??? [??] [??]; split; etransitivity; eauto.
  * by intros n [x1 y1] [x2 y2] [??]; split; apply dist_S.
  * by split.
  * intros c n; split. apply (conv_compl (chain_map authoritative c) n).
    apply (conv_compl (chain_map own c) n).
Qed.
44
45
Canonical Structure authC := CofeT auth_cofe_mixin.
Instance Auth_timeless (x : excl A) (y : A) :
Robbert Krebbers's avatar
Robbert Krebbers committed
46
  Timeless x  Timeless y  Timeless (Auth x y).
47
Proof. by intros ?? [??] [??]; split; simpl in *; apply (timeless _). Qed.
48
49
Global Instance auth_leibniz : LeibnizEquiv A  LeibnizEquiv (auth A).
Proof. by intros ? [??] [??] [??]; f_equal'; apply leibniz_equiv. Qed.
50
51
52
End cofe.

Arguments authC : clear implicits.
Robbert Krebbers's avatar
Robbert Krebbers committed
53
54

(* CMRA *)
55
56
57
58
59
Section cmra.
Context {A : cmraT}.

Global Instance auth_empty `{Empty A} : Empty (auth A) := Auth  .
Instance auth_validN : ValidN (auth A) := λ n x,
Robbert Krebbers's avatar
Robbert Krebbers committed
60
  match authoritative x with
Robbert Krebbers's avatar
Robbert Krebbers committed
61
62
  | Excl a => own x {n} a  {n} a
  | ExclUnit => {n} (own x)
Robbert Krebbers's avatar
Robbert Krebbers committed
63
64
  | ExclBot => n = 0
  end.
65
66
Global Arguments auth_validN _ !_ /.
Instance auth_unit : Unit (auth A) := λ x,
Robbert Krebbers's avatar
Robbert Krebbers committed
67
  Auth (unit (authoritative x)) (unit (own x)).
68
Instance auth_op : Op (auth A) := λ x y,
Robbert Krebbers's avatar
Robbert Krebbers committed
69
  Auth (authoritative x  authoritative y) (own x  own y).
70
Instance auth_minus : Minus (auth A) := λ x y,
Robbert Krebbers's avatar
Robbert Krebbers committed
71
  Auth (authoritative x  authoritative y) (own x  own y).
72
Lemma auth_included (x y : auth A) :
Robbert Krebbers's avatar
Robbert Krebbers committed
73
74
75
76
77
  x  y  authoritative x  authoritative y  own x  own y.
Proof.
  split; [intros [[z1 z2] Hz]; split; [exists z1|exists z2]; apply Hz|].
  intros [[z1 Hz1] [z2 Hz2]]; exists (Auth z1 z2); split; auto.
Qed.
78
Lemma auth_includedN n (x y : auth A) :
Robbert Krebbers's avatar
Robbert Krebbers committed
79
80
81
82
83
  x {n} y  authoritative x {n} authoritative y  own x {n} own y.
Proof.
  split; [intros [[z1 z2] Hz]; split; [exists z1|exists z2]; apply Hz|].
  intros [[z1 Hz1] [z2 Hz2]]; exists (Auth z1 z2); split; auto.
Qed.
84
Lemma authoritative_validN n (x : auth A) : {n} x  {n} (authoritative x).
Robbert Krebbers's avatar
Robbert Krebbers committed
85
Proof. by destruct x as [[]]. Qed.
86
Lemma own_validN n (x : auth A) : {n} x  {n} (own x).
87
Proof. destruct x as [[]]; naive_solver eauto using cmra_validN_includedN. Qed.
88
89

Definition auth_cmra_mixin : CMRAMixin (auth A).
Robbert Krebbers's avatar
Robbert Krebbers committed
90
91
Proof.
  split.
Robbert Krebbers's avatar
Robbert Krebbers committed
92
93
  * by intros n x y1 y2 [Hy Hy']; split; simpl; rewrite ?Hy ?Hy'.
  * by intros n y1 y2 [Hy Hy']; split; simpl; rewrite ?Hy ?Hy'.
Robbert Krebbers's avatar
Robbert Krebbers committed
94
95
96
  * intros n [x a] [y b] [Hx Ha]; simpl in *;
      destruct Hx as [[][]| | |]; intros ?; cofe_subst; auto.
  * by intros n x1 x2 [Hx Hx'] y1 y2 [Hy Hy'];
Robbert Krebbers's avatar
Robbert Krebbers committed
97
      split; simpl; rewrite ?Hy ?Hy' ?Hx ?Hx'.
Robbert Krebbers's avatar
Robbert Krebbers committed
98
  * by intros [[] ?]; simpl.
99
100
101
102
103
  * intros n [[] ?] ?; naive_solver eauto using cmra_includedN_S, cmra_validN_S.
  * by split; simpl; rewrite associative.
  * by split; simpl; rewrite commutative.
  * by split; simpl; rewrite ?cmra_unit_l.
  * by split; simpl; rewrite ?cmra_unit_idempotent.
Robbert Krebbers's avatar
Robbert Krebbers committed
104
  * intros n ??; rewrite! auth_includedN; intros [??].
105
    by split; simpl; apply cmra_unit_preservingN.
106
  * assert ( n (a b1 b2 : A), b1  b2 {n} a  b1 {n} a).
107
    { intros n a b1 b2 <-; apply cmra_includedN_l. }
Robbert Krebbers's avatar
Robbert Krebbers committed
108
   intros n [[a1| |] b1] [[a2| |] b2];
109
     naive_solver eauto using cmra_validN_op_l, cmra_validN_includedN.
Robbert Krebbers's avatar
Robbert Krebbers committed
110
111
112
  * by intros n ??; rewrite auth_includedN;
      intros [??]; split; simpl; apply cmra_op_minus.
Qed.
113
Definition auth_cmra_extend_mixin : CMRAExtendMixin (auth A).
Robbert Krebbers's avatar
Robbert Krebbers committed
114
115
116
117
118
119
120
121
Proof.
  intros n x y1 y2 ? [??]; simpl in *.
  destruct (cmra_extend_op n (authoritative x) (authoritative y1)
    (authoritative y2)) as (z1&?&?&?); auto using authoritative_validN.
  destruct (cmra_extend_op n (own x) (own y1) (own y2))
    as (z2&?&?&?); auto using own_validN.
  by exists (Auth (z1.1) (z2.1), Auth (z1.2) (z2.2)).
Qed.
122
123
Canonical Structure authRA : cmraT :=
  CMRAT auth_cofe_mixin auth_cmra_mixin auth_cmra_extend_mixin.
124
Instance auth_cmra_identity `{Empty A} : CMRAIdentity A  CMRAIdentity authRA.
Robbert Krebbers's avatar
Robbert Krebbers committed
125
Proof.
126
127
128
129
  split; simpl.
  * by apply (@cmra_empty_valid A _).
  * by intros x; constructor; rewrite /= left_id.
  * apply Auth_timeless; apply _.
Robbert Krebbers's avatar
Robbert Krebbers committed
130
Qed.
131
Lemma auth_frag_op (a b : A) :  (a  b)   a   b.
Robbert Krebbers's avatar
Robbert Krebbers committed
132
Proof. done. Qed.
133
134
135
End cmra.

Arguments authRA : clear implicits.
Robbert Krebbers's avatar
Robbert Krebbers committed
136
137
138
139

(* Functor *)
Instance auth_fmap : FMap auth := λ A B f x,
  Auth (f <$> authoritative x) (f (own x)).
140
141
142
143
144
145
Arguments auth_fmap _ _ _ !_ /.
Lemma auth_fmap_id {A} (x : auth A) : id <$> x = x.
Proof. by destruct x; rewrite /= excl_fmap_id. Qed.
Lemma excl_fmap_compose {A B C} (f : A  B) (g : B  C) (x : auth A) :
  g  f <$> x = g <$> f <$> x.
Proof. by destruct x; rewrite /= excl_fmap_compose. Qed.
146
Instance auth_fmap_cmra_ne {A B : cmraT} n :
Robbert Krebbers's avatar
Robbert Krebbers committed
147
148
149
150
  Proper ((dist n ==> dist n) ==> dist n ==> dist n) (@fmap auth _ A B).
Proof.
  intros f g Hf [??] [??] [??]; split; [by apply excl_fmap_cmra_ne|by apply Hf].
Qed.
151
Instance auth_fmap_cmra_monotone {A B : cmraT} (f : A  B) :
Robbert Krebbers's avatar
Robbert Krebbers committed
152
153
154
155
  ( n, Proper (dist n ==> dist n) f)  CMRAMonotone f 
  CMRAMonotone (fmap f : auth A  auth B).
Proof.
  split.
156
157
158
  * by intros n [x a] [y b]; rewrite !auth_includedN /=;
      intros [??]; split; simpl; apply: includedN_preserving.
  * intros n [[a| |] b]; rewrite /= /cmra_validN;
Robbert Krebbers's avatar
Robbert Krebbers committed
159
160
161
162
163
164
      naive_solver eauto using @includedN_preserving, @validN_preserving.
Qed.
Definition authRA_map {A B : cmraT} (f : A -n> B) : authRA A -n> authRA B :=
  CofeMor (fmap f : authRA A  authRA B).
Lemma authRA_map_ne A B n : Proper (dist n ==> dist n) (@authRA_map A B).
Proof. intros f f' Hf [[a| |] b]; repeat constructor; apply Hf. Qed.