dec_agree.v 2.24 KB
Newer Older
1
From iris.algebra Require Export cmra.
2 3 4
Local Arguments validN _ _ _ !_ /.
Local Arguments valid _ _  !_ /.
Local Arguments op _ _ _ !_ /.
Robbert Krebbers's avatar
Robbert Krebbers committed
5
Local Arguments pcore _ _ !_ /.
6

Robbert Krebbers's avatar
Robbert Krebbers committed
7
(* This is isomorphic to option, but has a very different RA structure. *)
8 9 10 11 12
Inductive dec_agree (A : Type) : Type := 
  | DecAgree : A  dec_agree A
  | DecAgreeBot : dec_agree A.
Arguments DecAgree {_} _.
Arguments DecAgreeBot {_}.
Robbert Krebbers's avatar
Robbert Krebbers committed
13 14
Instance maybe_DecAgree {A} : Maybe (@DecAgree A) := λ x,
  match x with DecAgree a => Some a | _ => None end.
15 16

Section dec_agree.
17
Context `{EqDecision A}.
18 19
Implicit Types a b : A.
Implicit Types x y : dec_agree A.
20 21 22 23 24 25 26 27 28 29

Instance dec_agree_valid : Valid (dec_agree A) := λ x,
  if x is DecAgree _ then True else False.
Canonical Structure dec_agreeC : cofeT := leibnizC (dec_agree A).

Instance dec_agree_op : Op (dec_agree A) := λ x y,
  match x, y with
  | DecAgree a, DecAgree b => if decide (a = b) then DecAgree a else DecAgreeBot
  | _, _ => DecAgreeBot
  end.
Robbert Krebbers's avatar
Robbert Krebbers committed
30
Instance dec_agree_pcore : PCore (dec_agree A) := Some.
31

32
Definition dec_agree_ra_mixin : RAMixin (dec_agree A).
33
Proof.
34
  apply ra_total_mixin; apply _ || eauto.
Robbert Krebbers's avatar
Robbert Krebbers committed
35 36 37 38
  - intros [?|] [?|] [?|]; by repeat (simplify_eq/= || case_match).
  - intros [?|] [?|]; by repeat (simplify_eq/= || case_match).
  - intros [?|]; by repeat (simplify_eq/= || case_match).
  - by intros [?|] [?|] ?.
39 40
Qed.

41 42
Canonical Structure dec_agreeR : cmraT :=
  discreteR (dec_agree A) dec_agree_ra_mixin.
Ralf Jung's avatar
...  
Ralf Jung committed
43

44 45 46
Global Instance dec_agree_total : CMRATotal dec_agreeR.
Proof. intros x. by exists x. Qed.

Ralf Jung's avatar
Ralf Jung committed
47
(* Some properties of this CMRA *)
48
Global Instance dec_agree_persistent (x : dec_agreeR) : Persistent x.
Robbert Krebbers's avatar
Robbert Krebbers committed
49
Proof. by constructor. Qed.
50

51 52
Lemma dec_agree_ne a b : a  b  DecAgree a  DecAgree b = DecAgreeBot.
Proof. intros. by rewrite /= decide_False. Qed.
Ralf Jung's avatar
Ralf Jung committed
53

54 55 56 57
Lemma dec_agree_idemp (x : dec_agree A) : x  x = x.
Proof. destruct x; by rewrite /= ?decide_True. Qed.

Lemma dec_agree_op_inv (x1 x2 : dec_agree A) :  (x1  x2)  x1 = x2.
Robbert Krebbers's avatar
Robbert Krebbers committed
58
Proof. destruct x1, x2; by repeat (simplify_eq/= || case_match). Qed.
59

60 61 62 63
Lemma DecAgree_included a b : DecAgree a  DecAgree b  a = b.
Proof.
  split. intros [[c|] [=]%leibniz_equiv]. by simplify_option_eq. by intros ->.
Qed.
Ralf Jung's avatar
Ralf Jung committed
64
End dec_agree.
65

66
Arguments dec_agreeC : clear implicits.
67
Arguments dec_agreeR _ {_}.