Library CompOptCert.Event

Require Import List.
Require Import PeanoNat.
Require Import Orders.
Require Import MSetList.

Require Import sflib.

Require Import DataStructure.
Require Import Basic.
Require Import Loc.

Set Implicit Arguments.

Require Import Integers.
Module Const := Int.

Lemma const_neq_sym:
   (x y:Const.t),
    x y y x.
Proof.
  intros.
  split.
  - intro. intro. rewrite H0 in H. contradiction.
  - intro. intro. rewrite H0 in H. contradiction.
Qed.

Lemma loc_neq_sym:
   (x y:Loc.t),
    x y y x.
Proof.
  intros.
  split.
  - intro. intro. rewrite H0 in H. contradiction.
  - intro. intro. rewrite H0 in H. contradiction.
Qed.

Module Ordering.
  Inductive t :=
  | plain
  | relaxed
  | strong_relaxed
  | acqrel
  | seqcst
  .

  Inductive atomicity :=
  | atomic
  | nonatomic
  .

  Definition mem_ord_match (ord: t) (o: atomicity): Prop :=
    match o with
    | nonatomicord = plain
    | atomic(ord = acqrel) (ord = relaxed) (ord = strong_relaxed)
    end
  .

LocOrdMap: records predefined atomicity for each location. atomic: allow relaxed or stronger access. nonatomic: allow plain access.
  Definition LocOrdMap := Loc.t atomicity.

  Definition le (lhs rhs:t): bool :=
    match lhs, rhs with
    | plain, _true
    | _, plainfalse

    | relaxed, _true
    | _, relaxedfalse

    | strong_relaxed, _true
    | _, strong_relaxedfalse

    | acqrel, _true
    | _, acqrelfalse

    | seqcst, seqcsttrue
    end.
  Global Opaque le.

  Global Program Instance le_PreOrder: PreOrder le.
  Next Obligation.
    ii. destruct x; auto.
  Qed.
  Next Obligation.
    ii. destruct x, y, z; auto.
  Qed.
  Hint Resolve le_PreOrder_obligation_2.

  Definition join (lhs rhs:t): t :=
    match lhs, rhs with
    | plain, _rhs
    | _, plainlhs

    | relaxed, _rhs
    | _, relaxedlhs

    | strong_relaxed, _rhs
    | _, strong_relaxedlhs

    | acqrel, _rhs
    | _, acqrellhs

    | seqcst, _rhs
    end.

  Lemma join_comm lhs rhs: join lhs rhs = join rhs lhs.
  Proof. destruct lhs, rhs; ss. Qed.

  Lemma join_assoc a b c: join (join a b) c = join a (join b c).
  Proof. destruct a, b, c; ss. Qed.

  Lemma join_l lhs rhs:
    le lhs (join lhs rhs).
  Proof. destruct lhs, rhs; ss. Qed.

  Lemma join_r lhs rhs:
    le rhs (join lhs rhs).
  Proof. destruct lhs, rhs; ss. Qed.

  Lemma join_spec lhs rhs o
        (LHS: le lhs o)
        (RHS: le rhs o):
    le (join lhs rhs) o.
  Proof. destruct lhs, rhs; ss. Qed.

  Lemma join_cases lhs rhs:
    join lhs rhs = lhs join lhs rhs = rhs.
  Proof. destruct lhs, rhs; auto. Qed.
End Ordering.

Module Event.
  Structure t := mk {
    output: Const.t;
    
  }.
End Event.

Observable event

Observable event includes:
  • output: for system call;
  • abort: for program abort;
  • done: for program done.
Module VisibleEvent.
  Inductive t :=
  | out (e: Event.t)
  | abort
  | done.
End VisibleEvent.

Module AuxEvent.
  Inductive t :=
  | na
  | prc
  | atm
  | out (e: Event.t)
  | sw
  | tterm
  .
End AuxEvent.

Program transition event (or machine evet)

Program transition event includes:
Module MachineEvent.
  Inductive t :=
  | silent
  | switch
  | syscall (e: Event.t)
  .
End MachineEvent.

Module ProgramEvent.
  Inductive t :=
  | silent
  | read (loc:Loc.t) (val:Const.t) (ord:Ordering.t)
  | write (loc:Loc.t) (val:Const.t) (ord:Ordering.t)
  | update (loc:Loc.t) (valr valw:Const.t) (ordr ordw:Ordering.t)
  | fence (ordr ordw:Ordering.t)
  | syscall (e:Event.t)
  .

  Definition is_reading (e:t): option (Loc.t × Const.t × Ordering.t) :=
    match e with
    | read loc val ordSome (loc, val, ord)
    | update loc valr _ ordr _Some (loc, valr, ordr)
    | _None
    end.

  Definition is_writing (e:t): option (Loc.t × Const.t × Ordering.t) :=
    match e with
    | write loc val ordSome (loc, val, ord)
    | update loc _ valw _ ordwSome (loc, valw, ordw)
    | _None
    end.

  Definition is_updating (e:t): option (Loc.t × Const.t × Ordering.t) :=
    match e with
    | update loc valr _ ordr _Some (loc, valr, ordr)
    | _None
    end.

End ProgramEvent.