Skip to content

Commit

Permalink
Removed the extras from Monad. They were annoying and had no benefit.
Browse files Browse the repository at this point in the history
I also proved that State is a monad.
  • Loading branch information
heades committed Mar 28, 2013
1 parent 17af208 commit 7c5d982
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 17 deletions.
6 changes: 1 addition & 5 deletions coq/monads/maybe.v
Expand Up @@ -11,11 +11,7 @@ Instance Maybe_Monad : Monad Maybe := {
| None => None b
end;

ret a := Just a;

bind_ign a b i j := j;

fail_m a s := None a
ret a := Just a
}.

(* Proofs of the monad laws. *)
Expand Down
13 changes: 1 addition & 12 deletions coq/monads/monad.v
Expand Up @@ -17,17 +17,8 @@ Class Monad (m : Type -> Type) := {
where "m '>>=' f" := (bind m f);

(* return - The injector of our monad. *)
ret : forall {a}, a -> m a;

(* Extras. *)
ret : forall {a}, a -> m a;

(* bind_ign - Like bind, but ignores the argument. *)
bind_ign : forall {a} {b}, m a -> m b -> m b
where "m '>>' f" := (bind m f);

(* fail_m - Used to break a chain. *)
fail_m : forall {a}, string -> m a;

(* Monads Laws. *)

(* Left identity. *)
Expand Down Expand Up @@ -75,5 +66,3 @@ Proof.
rewrite -> left_id.
reflexivity.
Defined.


47 changes: 47 additions & 0 deletions coq/monads/state.v
@@ -0,0 +1,47 @@
Require Import monad.

Inductive State (S A : Type) :=
| state : (S -> (A * S)) -> State S A.

Definition run {S A : Type} (m : State S A) : (S -> (A * S)) :=
match m with
| state a => a
end.

(* State is a monad. *)
Parameter S : Type.

Instance State_Monad : Monad (State S) := {
bind a b i f :=
state S b (fun s =>
let x := run i s in
match x with
| (x', s') => run (f x') s'
end);

ret A a := state S A (fun s => (a, s))
}.

Require Import FunctionalExtensionality.
Proof.
intros a b c f.
simpl; destruct (f c); simpl; reflexivity.

intros a m'.
destruct m'.
simpl.
rewrite -> (functional_extensionality (fun s : S => let (x', s') := p s in (x', s')) p).
reflexivity.
intros x.
destruct (p x).
reflexivity.

intros a b c i f g.
simpl.
f_equal.
destruct i.
extensionality s.
simpl.
destruct (p s).
reflexivity.
Defined.

0 comments on commit 7c5d982

Please sign in to comment.