dec_agree.v 1.99 KB
Newer Older
1 2 3 4 5 6
From algebra Require Export cmra.
Local Arguments validN _ _ _ !_ /.
Local Arguments valid _ _  !_ /.
Local Arguments op _ _ _ !_ /.
Local Arguments unit _ _ !_ /.

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 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38

Section dec_agree.
Context {A : Type} `{ x y : A, Decision (x = y)}.

Instance dec_agree_valid : Valid (dec_agree A) := λ x,
  if x is DecAgree _ then True else False.
Instance dec_agree_equiv : Equiv (dec_agree A) := equivL.
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.
Instance dec_agree_unit : Unit (dec_agree A) := id.
Instance dec_agree_minus : Minus (dec_agree A) := λ x y, x.

Definition dec_agree_ra : RA (dec_agree A).
Proof.
  split.
  - apply _.
  - apply _.
  - apply _.
  - apply _.
Robbert Krebbers's avatar
Robbert Krebbers committed
39 40 41 42 43 44 45 46
  - intros [?|] [?|] [?|]; by repeat (simplify_eq/= || case_match).
  - intros [?|] [?|]; by repeat (simplify_eq/= || case_match).
  - intros [?|]; by repeat (simplify_eq/= || case_match).
  - intros [?|]; by repeat (simplify_eq/= || case_match).
  - by intros [?|] [?|] ?.
  - by intros [?|] [?|] ?.
  - intros [?|] [?|] [[?|]]; fold_leibniz;
      intros; by repeat (simplify_eq/= || case_match).
47 48 49
Qed.

Canonical Structure dec_agreeRA : cmraT := discreteRA dec_agree_ra.
Ralf Jung's avatar
...  
Ralf Jung committed
50

Ralf Jung's avatar
Ralf Jung committed
51 52
(* Some properties of this CMRA *)
Lemma dec_agree_idemp (x : dec_agree A) : x  x  x.
Robbert Krebbers's avatar
Robbert Krebbers committed
53
Proof. destruct x; by repeat (simplify_eq/= || case_match). Qed.
Ralf Jung's avatar
Ralf Jung committed
54 55

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

Arguments dec_agreeRA _ {_}.