Library QedkConcrete.contractmonad

The contract harness uses the existing flattened result/state monad. These equations compare its primitives with the previous result transformer over state. Errors retain the resulting state in both; transaction rollback is performed by the caller, not by this monad.
Require Import MonadVM.Monad.

Module M := MonadVM.Monad.

Section Equivalence.
Context (State Error : Set).

Definition stateArgs : @M.State.FArgs State :=
  M.State.Build_FArgs (@M.State_T_signature.Build_signature State).
Definition resultArgs : @M.Result.FArgs Error :=
  M.Result.Build_FArgs (@M.State_T_signature.Build_signature Error).
Definition stateMonad : M.SIG_MONAD (t := @M.State.t State stateArgs) :=
  {| M.SIG._return := @M.State._return State stateArgs;
     M.SIG.op_gtgteq := @M.State.op_gtgteq State stateArgs |}.
Definition oldArgs := @M.Result.Trans.Build_FArgs Error resultArgs
  (@M.State.t State stateArgs) stateMonad.
Definition flatArgs : @M.Result_state.FArgs State Error :=
  M.Result_state.Build_FArgs
    (@M.Result_state_T_signature.Build_signature State Error).

Lemma return_equivalent (A : Set) (value : A) (state : State) :
  M.Result.Trans._return (_fargs := oldArgs) value state =
  M.Result_state._return (_fargs := flatArgs) value state.
Proof. reflexivity. Qed.

Lemma bind_equivalent (A B : Set)
    (action : State → (A + Error) × State)
    (next : A → State → (B + Error) × State) (state : State) :
  M.Result.Trans.op_gtgteq (_fargs := oldArgs) action next state =
  M.Result_state.op_gtgteq (_fargs := flatArgs) action next state.
Proof. cbn. destruct (action state) as [[value|error] post]; reflexivity. Qed.

Lemma fail_equivalent (A : Set) (error : Error) (state : State) :
  M.Result.Trans.fail (_fargs := oldArgs) (a := A) error state =
  M.Result_state.fail (_fargs := flatArgs) (a := A) error state.
Proof. reflexivity. Qed.

Lemma lift_equivalent (A : Set) (action : State → A × State) (state : State) :
  M.Result.Trans.lift (_fargs := oldArgs) action state =
  let '(value, post) := action state in (inl value : A + Error, post).
Proof. reflexivity. Qed.

Lemma get_equivalent (state : State) :
  M.Result.Trans.lift (_fargs := oldArgs)
    (M.State.get (_fargs := stateArgs)) state =
  M.Result_state.get (_fargs := flatArgs) state.
Proof. reflexivity. Qed.

Lemma put_equivalent (post state : State) :
  M.Result.Trans.lift (_fargs := oldArgs)
    (M.State.put (_fargs := stateArgs) post) state =
  M.Result_state.put (_fargs := flatArgs) post state.
Proof. reflexivity. Qed.

Lemma read_equivalent (A : Set) (lens : Lens.t State A) (state : State) :
  M.Result.Trans.lift (_fargs := oldArgs)
    (M.State.op_exclamation (_fargs := stateArgs) lens) state =
  M.Result_state.op_exclamation (_fargs := flatArgs) lens state.
Proof. reflexivity. Qed.

Lemma write_equivalent (A : Set) (lens : Lens.t State A)
    (value : A) (state : State) :
  M.Result.Trans.lift (_fargs := oldArgs)
    (M.State.op_coloneq (_fargs := stateArgs) lens value) state =
  M.Result_state.op_coloneq (_fargs := flatArgs) lens value state.
Proof. reflexivity. Qed.

End Equivalence.

Print Assumptions return_equivalent.
Print Assumptions bind_equivalent.
Print Assumptions fail_equivalent.
Print Assumptions lift_equivalent.
Print Assumptions get_equivalent.
Print Assumptions put_equivalent.
Print Assumptions read_equivalent.
Print Assumptions write_equivalent.