Library Coqtail.Monad.Option

Definition Return : (A : Type), Aoption A := Some.

Definition Bind : (A B : Type), option A → (Aoption B) → option B :=
fun A B Oa fmatch Oa with
  | NoneNone
  | Some af a
end.

Definition Map : (A B : Type) (f : AB), option Aoption B :=
fun A B f Oamatch Oa with
  | NoneNone
  | Some aSome (f a)
end.

Definition Join : (A : Type), option (option A) → option A :=
fun A OOamatch OOa with
  | NoneNone
  | Some NoneNone
  | Some (Some a) ⇒ Some a
end.

Implicit Arguments Return [A].
Implicit Arguments Bind [A B].
Implicit Arguments Map [A B].
Implicit Arguments Join [A].

Section Monad_properties.

Variables A B C : Type.
Variable a : A.
Variable Oa : option A.
Variable OOa : option (option A).
Variable f : Aoption B.
Variable g : Boption C.
Variable h : AB.

Definition Return_Bind : Bind (Return a) f = f a.
Proof.
reflexivity.
Qed.

Definition Bind_Return : Bind Oa (@Return A) = Oa.
Proof.
destruct Oa ; reflexivity.
Qed.

Definition Bind_Bind : Bind (Bind Oa f) g = Bind Oa (fun rBind (f r) g).
Proof.
destruct Oa ; reflexivity.
Qed.

Definition Map_is_Bind : Map h Oa = Bind Oa (fun xReturn (h x)).
Proof.
reflexivity.
Qed.

Definition Join_is_Bind : Join OOa = Bind OOa (fun xx).
Proof.
destruct OOa as [Ob |] ; [destruct Ob |] ; reflexivity.
Qed.

End Monad_properties.