Commit 780f6b82 authored by Robbert Krebbers's avatar Robbert Krebbers

Step-indexed order on CMRAs

* Remove the order from RAs, it is now defined in terms of the ⋅ operation.
* Define ownership using the step-indexed order.
* Remove the order also from DRAs and change STS accordingly. While doing
  that, I changed STS to no longer use decidable token sets, which removes the
  requirement of decidable equality on tokens.
parent db003ba1
......@@ -57,7 +57,6 @@ Next Obligation. by intros; simpl; split_ands; try apply agree_valid_0. Qed.
Next Obligation. naive_solver eauto using agree_valid_S, dist_S. Qed.
Global Instance agree_unit : Unit (agree A) := id.
Global Instance agree_minus : Minus (agree A) := λ x y, x.
Global Instance agree_included : Included (agree A) := λ x y, y x y.
Instance: Commutative () (@op (agree A) _).
Proof. intros x y; split; [naive_solver|by intros n (?&?&Hxy); apply Hxy]. Qed.
Definition agree_idempotent (x : agree A) : x x x.
......@@ -80,6 +79,11 @@ Proof.
repeat match goal with H : agree_is_valid _ _ |- _ => clear H end;
by cofe_subst; rewrite !agree_idempotent.
Qed.
Lemma agree_includedN (x y : agree A) n : x {n} y y ={n}= x y.
Proof.
split; [|by intros ?; exists y].
by intros [z Hz]; rewrite Hz, (associative _), agree_idempotent.
Qed.
Global Instance agree_cmra : CMRA (agree A).
Proof.
split; try (apply _ || done).
......@@ -87,23 +91,20 @@ Proof.
rewrite <-(proj2 Hxy n'), (Hx n') by eauto using agree_valid_le.
by apply dist_le with n; try apply Hxy.
* by intros n x1 x2 Hx y1 y2 Hy.
* by intros x y1 y2 Hy ?; do 2 red; rewrite <-Hy.
* 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' ?].
rewrite (Hx n') by auto; symmetry; apply dist_le with n; try apply Hx; auto.
* intros x; apply agree_idempotent.
* intros x y; change (x y x (x y)).
by rewrite (associative _), agree_idempotent.
* by intros x y n [(?&?&?) ?].
* by intros x y; do 2 red; rewrite (associative _), agree_idempotent.
* by intros x y n; rewrite agree_includedN.
Qed.
Lemma agree_op_inv (x y1 y2 : agree A) n :
validN n x x ={n}= y1 y2 y1 ={n}= y2.
Proof. by intros [??] Hxy; apply Hxy. Qed.
Global Instance agree_extend : CMRAExtend (agree A).
Proof.
intros x y1 y2 n ? Hx; exists (x,x); simpl; split.
intros n x y1 y2 ? Hx; exists (x,x); simpl; split.
* by rewrite agree_idempotent.
* by rewrite Hx, (agree_op_inv x y1 y2), agree_idempotent by done.
Qed.
......@@ -131,15 +132,13 @@ Section agree_map.
Global Instance agree_map_ne n : Proper (dist n ==> dist n) agree_map.
Proof. by intros x1 x2 Hx; split; simpl; intros; [apply Hx|apply Hf, Hx]. Qed.
Global Instance agree_map_proper: Proper (()==>()) agree_map := ne_proper _.
Global Instance agree_map_preserving : CMRAPreserving agree_map.
Global Instance agree_map_monotone : CMRAMonotone agree_map.
Proof.
split; [|by intros n x [? Hx]; split; simpl; [|by intros n' ?; rewrite Hx]].
intros x y; unfold included, agree_included; intros Hy; rewrite Hy.
split; [split|done].
* by intros (?&?&Hxy); repeat (intro || split);
try apply Hxy; try apply Hf; eauto using @agree_valid_le.
* by intros (?&(?&?&Hxy)&_); repeat split;
try apply Hxy; eauto using agree_valid_le.
intros x y n; rewrite !agree_includedN; intros Hy; rewrite Hy.
split; [split; simpl; try tauto|done].
by intros (?&?&Hxy); repeat split; intros;
try apply Hxy; try apply Hf; eauto using @agree_valid_le.
Qed.
End agree_map.
Lemma agree_map_id `{Cofe A} (x : agree A) : agree_map id x = x.
......
Require Export iris.excl.
Local Arguments disjoint _ _ !_ !_ /.
Local Arguments included _ _ !_ !_ /.
Record auth (A : Type) : Type := Auth { authorative : excl A ; own : A }.
Record auth (A : Type) : Type := Auth { authoritative : excl A ; own : A }.
Arguments Auth {_} _ _.
Arguments authorative {_} _.
Arguments authoritative {_} _.
Arguments own {_} _.
Notation "∘ x" := (Auth ExclUnit x) (at level 20).
Notation "∙ x" := (Auth (Excl x) ) (at level 20).
Instance auth_empty `{Empty A} : Empty (auth A) := Auth .
Instance auth_valid `{Valid A, Included A} : Valid (auth A) := λ x,
valid (authorative x) excl_above (own x) (authorative x).
Instance auth_valid `{Equiv A, Valid A, Op A} : Valid (auth A) := λ x,
valid (authoritative x) excl_above (own x) (authoritative x).
Instance auth_equiv `{Equiv A} : Equiv (auth A) := λ x y,
authorative x authorative y own x own y.
authoritative x authoritative y own x own y.
Instance auth_unit `{Unit A} : Unit (auth A) := λ x,
Auth (unit (authorative x)) (unit (own x)).
Auth (unit (authoritative x)) (unit (own x)).
Instance auth_op `{Op A} : Op (auth A) := λ x y,
Auth (authorative x authorative y) (own x own y).
Auth (authoritative x authoritative y) (own x own y).
Instance auth_minus `{Minus A} : Minus (auth A) := λ x y,
Auth (authorative x authorative y) (own x own y).
Instance auth_included `{Equiv A, Included A} : Included (auth A) := λ x y,
authorative x authorative y own x own y.
Auth (authoritative x authoritative y) (own x own y).
Lemma auth_included `{Equiv A, Op A} (x y : auth A) :
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.
Instance auth_ra `{RA A} : RA (auth A).
Proof.
......@@ -35,19 +38,19 @@ Proof.
* by intros y1 y2 [Hy Hy'] [??]; split; simpl; rewrite <-?Hy, <-?Hy'.
* by intros x1 x2 [Hx Hx'] y1 y2 [Hy Hy'];
split; simpl; rewrite ?Hy, ?Hy', ?Hx, ?Hx'.
* by intros x y1 y2 [Hy Hy'] [??]; split; simpl; rewrite <-?Hy, <-?Hy'.
* by split; simpl; rewrite (associative _).
* by split; simpl; rewrite (commutative _).
* by split; simpl; rewrite ?(ra_unit_l _).
* by split; simpl; rewrite ?(ra_unit_idempotent _).
* split; simpl; auto using ra_unit_weaken.
* intros ?? [??]; split; [by apply ra_valid_op_l with (authorative y)|].
* intros ??; rewrite! auth_included; intros [??].
by split; simpl; apply ra_unit_preserving.
* intros ?? [??]; split; [by apply ra_valid_op_l with (authoritative y)|].
by apply excl_above_weaken with (own x own y)
(authorative x authorative y); try apply ra_included_l.
* split; simpl; apply ra_included_l.
* by intros ?? [??]; split; simpl; apply ra_op_minus.
(authoritative x authoritative y); try apply ra_included_l.
* by intros ??; rewrite auth_included;
intros [??]; split; simpl; apply ra_op_minus.
Qed.
Instance auth_ra_empty `{RA A, Empty A, !RAEmpty A} : RAEmpty (auth A).
Proof. split. done. by intros x; constructor; simpl; rewrite (left_id _ _). Qed.
Lemma auth_frag_op `{RA A} a b : (a b) a b.
Proof. done. Qed.
Proof. done. Qed.
\ No newline at end of file
......@@ -3,15 +3,18 @@ Require Export iris.ra iris.cofe.
Class ValidN (A : Type) := validN : nat A Prop.
Instance: Params (@validN) 3.
Class CMRA A `{Equiv A, Compl A,
Unit A, Op A, Valid A, ValidN A, Included A, Minus A} : Prop := {
Definition includedN `{Dist A, Op A} (n : nat) (x y : A) := z, y ={n}= x z.
Notation "x ≼{ n } y" := (includedN n x y)
(at level 70, format "x ≼{ n } y") : C_scope.
Instance: Params (@includedN) 4.
Class CMRA A `{Equiv A, Compl A, Unit A, Op A, Valid A, ValidN A, Minus A} := {
(* setoids *)
cmra_cofe :> Cofe A;
cmra_op_ne n x :> Proper (dist n ==> dist n) (op x);
cmra_unit_ne n :> Proper (dist n ==> dist n) unit;
cmra_valid_ne n :> Proper (dist n ==> impl) (validN n);
cmra_minus_ne n :> Proper (dist n ==> dist n ==> dist n) minus;
cmra_included_proper x : Proper (() ==> impl) (included x);
(* valid *)
cmra_valid_0 x : validN 0 x;
cmra_valid_S n x : validN (S n) x validN n x;
......@@ -21,18 +24,17 @@ Class CMRA A `{Equiv A, Compl A,
cmra_commutative : Commutative () ();
cmra_unit_l x : unit x x x;
cmra_unit_idempotent x : unit (unit x) unit x;
cmra_unit_weaken x y : unit x unit (x y);
cmra_unit_preserving n x y : x {n} y unit x {n} unit y;
cmra_valid_op_l n x y : validN n (x y) validN n x;
cmra_included_l x y : x x y;
cmra_op_minus x y : x y x y x y
cmra_op_minus n x y : x {n} y x y x ={n}= y
}.
Class CMRAExtend A `{Equiv A, Dist A, Op A, ValidN A} :=
cmra_extend_op x y1 y2 n :
cmra_extend_op n x y1 y2 :
validN n x x ={n}= y1 y2 { z | x z.1 z.2 z ={n}= (y1,y2) }.
Class CMRAPreserving
`{Included A, ValidN A, Included B, ValidN B} (f : A B) := {
included_preserving x y : x y f x f y;
Class CMRAMonotone
`{Dist A, Op A, ValidN A, Dist B, Op B, ValidN B} (f : A B) := {
includedN_preserving n x y : x {n} y f x {n} f y;
validN_preserving n x : validN n x validN n (f x)
}.
......@@ -46,12 +48,11 @@ Structure cmraT := CMRAT {
cmra_op : Op cmra_car;
cmra_valid : Valid cmra_car;
cmra_validN : ValidN cmra_car;
cmra_included : Included cmra_car;
cmra_minus : Minus cmra_car;
cmra_cmra : CMRA cmra_car;
cmra_extend : CMRAExtend cmra_car
}.
Arguments CMRAT _ {_ _ _ _ _ _ _ _ _ _ _}.
Arguments CMRAT _ {_ _ _ _ _ _ _ _ _ _}.
Arguments cmra_car _ : simpl never.
Arguments cmra_equiv _ _ _ : simpl never.
Arguments cmra_dist _ _ _ _ : simpl never.
......@@ -60,12 +61,11 @@ Arguments cmra_unit _ _ : simpl never.
Arguments cmra_op _ _ _ : simpl never.
Arguments cmra_valid _ _ : simpl never.
Arguments cmra_validN _ _ _ : simpl never.
Arguments cmra_included _ _ _ : simpl never.
Arguments cmra_minus _ _ _ : simpl never.
Arguments cmra_cmra _ : simpl never.
Add Printing Constructor cmraT.
Existing Instances cmra_equiv cmra_dist cmra_compl cmra_unit cmra_op
cmra_valid cmra_validN cmra_included cmra_minus cmra_cmra cmra_extend.
cmra_valid cmra_validN cmra_minus cmra_cmra cmra_extend.
Coercion cmra_cofeC (A : cmraT) : cofeT := CofeT A.
Canonical Structure cmra_cofeC.
......@@ -73,6 +73,13 @@ Section cmra.
Context `{cmra : CMRA A}.
Implicit Types x y z : A.
Lemma cmra_included_includedN x y : x y n, x {n} y.
Proof.
split; [by intros [z Hz] n; exists z; rewrite Hz|].
intros Hxy; exists (y x); apply equiv_dist; intros n.
symmetry; apply cmra_op_minus, Hxy.
Qed.
Global Instance cmra_valid_ne' : Proper (dist n ==> iff) (validN n).
Proof. by split; apply cmra_valid_ne. Qed.
Global Instance cmra_valid_proper : Proper (() ==> iff) (validN n).
......@@ -82,16 +89,15 @@ Proof.
split; try by (destruct cmra;
eauto using ne_proper, ne_proper_2 with typeclass_instances).
* by intros x1 x2 Hx; rewrite !cmra_valid_validN; intros ? n; rewrite <-Hx.
* intros x y; rewrite !cmra_included_includedN.
eauto using cmra_unit_preserving.
* intros x y; rewrite !cmra_valid_validN; intros ? n.
by apply cmra_valid_op_l with y.
* intros x y [z Hz]; apply equiv_dist; intros n.
by apply cmra_op_minus; exists z; rewrite Hz.
Qed.
Lemma cmra_valid_op_r x y n : validN n (x y) validN n y.
Proof. rewrite (commutative _ x); apply cmra_valid_op_l. Qed.
Lemma cmra_valid_included x y n : validN n y x y validN n x.
Proof.
rewrite ra_included_spec; intros Hvalid [z Hy]; rewrite Hy in Hvalid.
eauto using cmra_valid_op_l.
Qed.
Lemma cmra_valid_le x n n' : validN n x n' n validN n' x.
Proof. induction 2; eauto using cmra_valid_S. Qed.
Global Instance ra_op_ne n : Proper (dist n ==> dist n ==> dist n) ().
......@@ -101,24 +107,66 @@ Proof.
Qed.
Lemma cmra_unit_valid x n : validN n x validN n (unit x).
Proof. rewrite <-(cmra_unit_l x) at 1; apply cmra_valid_op_l. Qed.
Lemma cmra_included_dist_l x1 x2 x1' n :
x1 x2 x1' ={n}= x1 x2', x1' x2' x2' ={n}= x2.
Proof.
rewrite ra_included_spec; intros [z Hx2] Hx1; exists (x1' z); split.
apply ra_included_l. by rewrite Hx1, Hx2.
Qed.
Lemma cmra_op_timeless `{!CMRAExtend A} x1 x2 :
validN 1 (x1 x2) Timeless x1 Timeless x2 Timeless (x1 x2).
Proof.
intros ??? z Hz.
destruct (cmra_extend_op z x1 x2 1) as ([y1 y2]&Hz'&?&?); auto; simpl in *.
destruct (cmra_extend_op 1 z x1 x2) as ([y1 y2]&Hz'&?&?); auto; simpl in *.
{ by rewrite <-?Hz. }
by rewrite Hz', (timeless x1 y1), (timeless x2 y2).
Qed.
(** * Included *)
Global Instance cmra_included_ne n :
Proper (dist n ==> dist n ==> iff) (includedN n).
Proof.
intros x x' Hx y y' Hy; unfold includedN.
by setoid_rewrite Hx; setoid_rewrite Hy.
Qed.
Global Instance cmra_included_proper:Proper (() ==> () ==> iff) (includedN n).
Proof.
intros n x x' Hx y y' Hy; unfold includedN.
by setoid_rewrite Hx; setoid_rewrite Hy.
Qed.
Lemma cmra_included_0 x y : x {0} y.
Proof. by exists (unit x). Qed.
Lemma cmra_included_S x y n : x {S n} y x {n} y.
Proof. by intros [z Hz]; exists z; apply dist_S. Qed.
Lemma cmra_included_l n x y : x {n} x y.
Proof. by exists y. Qed.
Lemma cmra_included_r n x y : y {n} x y.
Proof. rewrite (commutative op); apply cmra_included_l. Qed.
Global Instance cmra_included_ord n : PreOrder (includedN n).
Proof.
split.
* by intros x; exists (unit x); rewrite ra_unit_r.
* intros x y z [z1 Hy] [z2 Hz]; exists (z1 z2).
by rewrite (associative _), <-Hy, <-Hz.
Qed.
Lemma cmra_valid_includedN x y n : validN n y x {n} y validN n x.
Proof. intros Hyv [z Hy]; rewrite Hy in Hyv; eauto using cmra_valid_op_l. Qed.
Lemma cmra_valid_included x y n : validN n y x y validN n x.
Proof. rewrite cmra_included_includedN; eauto using cmra_valid_includedN. Qed.
Lemma cmra_included_dist_l x1 x2 x1' n :
x1 x2 x1' ={n}= x1 x2', x1' x2' x2' ={n}= x2.
Proof.
intros [z Hx2] Hx1; exists (x1' z); split; auto using ra_included_l.
by rewrite Hx1, Hx2.
Qed.
End cmra.
Instance cmra_preserving_id `{CMRA A} : CMRAPreserving (@id A).
Instance cmra_monotone_id `{CMRA A} : CMRAMonotone (@id A).
Proof. by split. Qed.
Instance cmra_monotone_ra_monotone `{CMRA A, CMRA B} (f : A B) :
CMRAMonotone f RAMonotone f.
Proof.
split.
* intros x y; rewrite !cmra_included_includedN.
by intros ? n; apply includedN_preserving.
* intros x; rewrite !cmra_valid_validN.
by intros ? n; apply validN_preserving.
Qed.
(* Also via [cmra_cofe; cofe_equivalence] *)
Hint Cut [!*; ra_equivalence; cmra_ra] : typeclass_instances.
......@@ -139,15 +187,18 @@ Section discrete.
* by intros [|n]; try apply ra_minus_proper.
* by intros [|n].
* intros x; split; intros Hvalid. by intros [|n]. apply (Hvalid 1).
* by intros [|n]; try apply ra_unit_preserving.
* by intros [|n]; try apply ra_valid_op_l.
* by intros [|n] x y; try apply ra_op_minus.
Qed.
Instance discrete_extend : CMRAExtend A.
Proof.
intros x y1 y2 [|n] ??; [|by exists (y1,y2)].
intros [|n] x y1 y2 ??; [|by exists (y1,y2)].
by exists (x,unit x); simpl; rewrite ra_unit_r.
Qed.
Definition discreteC : cmraT := CMRAT A.
Definition discreteRA : cmraT := CMRAT A.
End discrete.
Arguments discreteRA _ {_ _ _ _ _ _}.
(** Product *)
Instance prod_op `{Op A, Op B} : Op (A * B) := λ x y, (x.1 y.1, x.2 y.2).
......@@ -158,11 +209,20 @@ Instance prod_valid `{Valid A, Valid B} : Valid (A * B) := λ x,
valid (x.1) valid (x.2).
Instance prod_validN `{ValidN A, ValidN B} : ValidN (A * B) := λ n x,
validN n (x.1) validN n (x.2).
Instance prod_included `{Included A, Included B} : Included (A * B) :=
prod_relation () ().
Instance prod_minus `{Minus A, Minus B} : Minus (A * B) := λ x y,
(x.1 y.1, x.2 y.2).
Lemma prod_included `{Equiv A, Equiv B, Op A, Op B} (x y : A * B) :
x y x.1 y.1 x.2 y.2.
Proof.
split; [intros [z Hz]; split; [exists (z.1)|exists (z.2)]; apply Hz|].
intros [[z1 Hz1] [z2 Hz2]]; exists (z1,z2); split; auto.
Qed.
Lemma prod_includedN `{Dist A, Dist B, Op A, Op B} (x y : A * B) n :
x {n} y x.1 {n} y.1 x.2 {n} y.2.
Proof.
split; [intros [z Hz]; split; [exists (z.1)|exists (z.2)]; apply Hz|].
intros [[z1 Hz1] [z2 Hz2]]; exists (z1,z2); split; auto.
Qed.
Instance prod_cmra `{CMRA A, CMRA B} : CMRA (A * B).
Proof.
split; try apply _.
......@@ -171,7 +231,6 @@ Proof.
* by intros n y1 y2 [Hy1 Hy2] [??]; split; simpl in *; rewrite <-?Hy1, <-?Hy2.
* by intros n x1 x2 [Hx1 Hx2] y1 y2 [Hy1 Hy2];
split; simpl in *; rewrite ?Hx1, ?Hx2, ?Hy1, ?Hy2.
* by intros x y1 y2 [Hy1 Hy2] [??]; split; simpl in *; rewrite <-?Hy1, <-?Hy2.
* split; apply cmra_valid_0.
* by intros n x [??]; split; apply cmra_valid_S.
* intros x; split; [by intros [??] n; split; apply cmra_valid_validN|].
......@@ -180,10 +239,11 @@ Proof.
* split; simpl; apply (commutative _).
* split; simpl; apply ra_unit_l.
* split; simpl; apply ra_unit_idempotent.
* split; apply ra_unit_weaken.
* intros n x y; rewrite !prod_includedN.
by intros [??]; split; apply cmra_unit_preserving.
* intros n x y [??]; split; simpl in *; eapply cmra_valid_op_l; eauto.
* split; apply cmra_included_l.
* by intros x y [??]; split; apply cmra_op_minus.
* intros x y n; rewrite prod_includedN; intros [??].
by split; apply cmra_op_minus.
Qed.
Instance prod_ra_empty `{RAEmpty A, RAEmpty B} : RAEmpty (A * B).
Proof.
......@@ -191,18 +251,19 @@ Proof.
Qed.
Instance prod_cmra_extend `{CMRAExtend A, CMRAExtend B} : CMRAExtend (A * B).
Proof.
intros x y1 y2 n [??] [??]; simpl in *.
destruct (cmra_extend_op (x.1) (y1.1) (y2.1) n) as (z1&?&?&?); auto.
destruct (cmra_extend_op (x.2) (y1.2) (y2.2) n) as (z2&?&?&?); auto.
intros n x y1 y2 [??] [??]; simpl in *.
destruct (cmra_extend_op n (x.1) (y1.1) (y2.1)) as (z1&?&?&?); auto.
destruct (cmra_extend_op n (x.2) (y1.2) (y2.2)) as (z2&?&?&?); auto.
by exists ((z1.1,z2.1),(z1.2,z2.2)).
Qed.
Canonical Structure prodRA (A B : cmraT) : cmraT := CMRAT (A * B).
Instance prod_map_cmra_preserving `{CMRA A, CMRA A', CMRA B, CMRA B'}
(f : A A') (g : B B') `{!CMRAPreserving f, !CMRAPreserving g} :
CMRAPreserving (prod_map f g).
Instance prod_map_cmra_monotone `{CMRA A, CMRA A', CMRA B, CMRA B'}
(f : A A') (g : B B') `{!CMRAMonotone f, !CMRAMonotone g} :
CMRAMonotone (prod_map f g).
Proof.
split.
* by intros x1 x2 [??]; split; simpl; apply included_preserving.
* intros n x1 x2; rewrite !prod_includedN; intros [??]; simpl.
by split; apply includedN_preserving.
* by intros n x [??]; split; simpl; apply validN_preserving.
Qed.
Definition prodRA_map {A A' B B' : cmraT}
......
......@@ -3,18 +3,28 @@ Require Import prelude.pmap prelude.nmap prelude.zmap.
Require Import prelude.stringmap prelude.natmap.
(** option *)
Instance option_valid `{Valid A} : Valid (option A) := λ x,
match x with Some x => valid x | None => True end.
Instance option_validN `{ValidN A} : ValidN (option A) := λ n x,
match x with Some x => validN n x | None => True end.
Instance option_valid `{Valid A} : Valid (option A) := λ mx,
match mx with Some x => valid x | None => True end.
Instance option_validN `{ValidN A} : ValidN (option A) := λ n mx,
match mx with Some x => validN n x | None => True end.
Instance option_unit `{Unit A} : Unit (option A) := fmap unit.
Instance option_op `{Op A} : Op (option A) := union_with (λ x y, Some (x y)).
Instance option_minus `{Minus A} : Minus (option A) :=
difference_with (λ x y, Some (x y)).
Inductive option_included `{Included A} : Included (option A) :=
| Some_included x y : x y Some x Some y
| None_included x : None x.
Existing Instance option_included.
Lemma option_includedN `{CMRA A} n mx my :
mx {n} my n = 0 mx = None x y, mx = Some x my = Some y x {n} y.
Proof.
split.
* intros [mz Hmz]; destruct n as [|n]; [by left|right].
destruct mx as [x|]; [right|by left].
destruct my as [y|]; [exists x, y|destruct mz; inversion_clear Hmz].
destruct mz as [z|]; inversion_clear Hmz; split_ands; auto.
+ by exists z.
+ by cofe_subst.
* intros [->|[->|(x&y&->&->&z&Hz)]];
try (by exists my; destruct my; constructor).
by exists (Some z); constructor.
Qed.
Instance option_cmra `{CMRA A} : CMRA (option A).
Proof.
split.
......@@ -27,8 +37,6 @@ Proof.
eapply (_ : Proper (dist _ ==> impl) (validN _)); eauto.
* by destruct 1; inversion_clear 1; constructor;
repeat apply (_ : Proper (dist _ ==> _ ==> _) _).
* intros [x|]; destruct 1; inversion_clear 1; constructor;
eapply (_ : Proper (equiv ==> impl) (included _)); eauto.
* intros [x|]; unfold validN, option_validN; auto using cmra_valid_0.
* intros n [x|]; unfold validN, option_validN; auto using cmra_valid_S.
* by intros [x|]; unfold valid, validN, option_validN, option_valid;
......@@ -37,19 +45,21 @@ Proof.
* intros [x|] [y|]; constructor; rewrite 1?(commutative _); auto.
* by intros [x|]; constructor; rewrite cmra_unit_l.
* by intros [x|]; constructor; rewrite cmra_unit_idempotent.
* intros [x|] [y|]; constructor; auto using cmra_unit_weaken.
* intros n mx my; rewrite !option_includedN;intros [|[->|(x&y&->&->&?)]];auto.
do 2 right; exists (unit x), (unit y); eauto using cmra_unit_preserving.
* intros n [x|] [y|]; unfold validN, option_validN; simpl;
eauto using cmra_valid_op_l.
* intros [x|] [y|]; constructor; auto using cmra_included_l.
* destruct 1 as [|[]]; constructor; eauto using cmra_op_minus.
* intros n mx my; rewrite option_includedN.
intros [->|[->|(x&y&->&->&?)]]; [done|by destruct my|].
by constructor; apply cmra_op_minus.
Qed.
Instance option_cmra_extend `{CMRA A, !CMRAExtend A} : CMRAExtend (option A).
Proof.
intros mx my1 my2 n; destruct (decide (n = 0)) as [->|].
intros n mx my1 my2; destruct (decide (n = 0)) as [->|].
{ by exists (mx, None); repeat constructor; destruct mx; constructor. }
destruct mx as [x|], my1 as [y1|], my2 as [y2|]; intros Hx Hx';
try (by exfalso; inversion Hx'; auto).
* destruct (cmra_extend_op x y1 y2 n) as ([z1 z2]&?&?&?); auto.
* destruct (cmra_extend_op n x y1 y2) as ([z1 z2]&?&?&?); auto.
{ by inversion_clear Hx'. }
by exists (Some z1, Some z2); repeat constructor.
* by exists (Some x,None); inversion Hx'; repeat constructor.
......@@ -57,10 +67,11 @@ Proof.
* exists (None,None); repeat constructor.
Qed.
Instance option_fmap_cmra_preserving `{CMRA A, CMRA B} (f : A B)
`{!CMRAPreserving f} : CMRAPreserving (fmap f : option A option B).
`{!CMRAMonotone f} : CMRAMonotone (fmap f : option A option B).
Proof.
split.
* by destruct 1 as [|[?|]]; constructor; apply included_preserving.
* intros n mx my; rewrite !option_includedN.
intros [->|[->|(x&y&->&->&?)]]; simpl; eauto 10 using @includedN_preserving.
* by intros n [x|] ?;
unfold validN, option_validN; simpl; try apply validN_preserving.
Qed.
......@@ -74,14 +85,28 @@ Section map.
Instance map_valid `{Valid A} : Valid (M A) := λ m, i, valid (m !! i).
Instance map_validN `{ValidN A} : ValidN (M A) := λ n m, i, validN n (m!!i).
Instance map_minus `{Minus A} : Minus (M A) := merge minus.
Instance map_included `{Included A} : Included (M A) := λ m1 m2,
i, m1 !! i m2 !! i.
Lemma lookup_op `{Op A} m1 m2 i : (m1 m2) !! i = m1 !! i m2 !! i.
Proof. by apply lookup_merge. Qed.
Lemma lookup_minus `{Minus A} m1 m2 i : (m1 m2) !! i = m1 !! i m2 !! i.
Proof. by apply lookup_merge. Qed.
Lemma lookup_unit `{Unit A} m i : unit m !! i = unit (m !! i).
Proof. by apply lookup_fmap. Qed.
Lemma map_included_spec `{CMRA A} (m1 m2 : M A) :
m1 m2 i, m1 !! i m2 !! i.
Proof.
split.
* intros [m Hm]; intros i; exists (m !! i). by rewrite <-lookup_op, Hm.
* intros Hm; exists (m2 m1); intros i.
by rewrite lookup_op, lookup_minus, ra_op_minus.
Qed.
Lemma map_includedN_spec `{CMRA A} (m1 m2 : M A) n :
m1 {n} m2 i, m1 !! i {n} m2 !! i.
Proof.
split.
* intros [m Hm]; intros i; exists (m !! i). by rewrite <-lookup_op, Hm.
* intros Hm; exists (m2 m1); intros i.
by rewrite lookup_op, lookup_minus, cmra_op_minus.
Qed.
Instance map_cmra `{CMRA A} : CMRA (M A).
Proof.
split.
......@@ -91,7 +116,6 @@ Section map.
* by intros n m1 m2 Hm ? i; rewrite <-(Hm i).
* intros n m1 m1' Hm1 m2 m2' Hm2 i.
by rewrite !lookup_minus, (Hm1 i), (Hm2 i).
* by intros m1 m2 m2' Hm2 ? i; rewrite <-(Hm2 i).