Skip to content

Commit c315bb0

Browse files
jldoddsliyishuai
andauthored
quick ixmonad and examples (#55)
Co-authored-by: Yishuai Li <liyishuai.lys@alibaba-inc.com>
1 parent 00d3f4e commit c315bb0

File tree

4 files changed

+97
-0
lines changed

4 files changed

+97
-0
lines changed

_CoqProject

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ theories/Structures/EqDep.v
1818
theories/Structures/Foldable.v
1919
theories/Structures/FunctorLaws.v
2020
theories/Structures/Functor.v
21+
theories/Structures/IXMonad.v
2122
theories/Structures/Maps.v
2223
theories/Structures/MonadCont.v
2324
theories/Structures/MonadExc.v
@@ -119,6 +120,7 @@ theories/Data/Monads/FuelMonadLaws.v
119120
theories/Data/Monads/FuelMonad.v
120121
theories/Data/Monads/IdentityMonadLaws.v
121122
theories/Data/Monads/IdentityMonad.v
123+
theories/Data/Monads/IStateMonad.v
122124
theories/Data/Monads/OptionMonadLaws.v
123125
theories/Data/Monads/OptionMonad.v
124126
theories/Data/Monads/ReaderMonadLaws.v

examples/indexedstate.v

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Require Import ExtLib.Data.Monads.IStateMonad.
2+
Require Import ExtLib.Structures.IXMonad.
3+
4+
Section example.
5+
6+
Import IxMonadNotation.
7+
Local Open Scope ixmonad_scope.
8+
9+
Variables A B C : Type.
10+
11+
Variable function1 : A -> B.
12+
Variable function2 : B -> C.
13+
14+
Definition update1 : istate A B unit :=
15+
modify_ function1.
16+
17+
Definition update2 : istate B C unit :=
18+
modify_ function2.
19+
20+
Definition compose : istate A C unit :=
21+
update1 ;;
22+
update2.
23+
24+
End example.

theories/Data/Monads/IStateMonad.v

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
Require Import ExtLib.Structures.IXMonad.
2+
3+
Set Implicit Arguments.
4+
Set Strict Implicit.
5+
6+
Section IStateType.
7+
8+
Record istate (i s t: Type) : Type := mkIState
9+
{ runIState : i -> t * s }.
10+
11+
Definition evalState {i s t} (c : istate i s t) (s : i) : t :=
12+
fst (runIState c s).
13+
14+
Definition execState {i s t} (c : istate i s t) (st : i) : s :=
15+
snd (runIState c st).
16+
17+
18+
Global Instance IMonad_Ixstate : IxMonad istate :=
19+
{
20+
ret := fun _ _ v => mkIState (fun s => (v, s))
21+
; bind := fun _ _ _ _ _ c1 c2 =>
22+
mkIState (fun s =>
23+
let (v,s) := runIState c1 s in
24+
runIState (c2 v) s)
25+
}.
26+
27+
Definition get {i : Type} := @mkIState i i i (fun s => (s,s)).
28+
Definition put {i o : Type} := (fun v => @mkIState i o unit (fun _ => (tt, v))).
29+
Definition put_ {i o : Type} (s : o) : istate i o unit := (bind (put s) (fun _ => ret tt)).
30+
Definition modify {i o : Type} (f : i -> o) : istate i o i :=
31+
bind get (fun x : i => bind (put (f x)) (fun _ : unit => ret x)).
32+
Definition modify_ {i o : Type} (f : i -> o) : istate i o unit :=
33+
bind (modify f) (fun _ => ret tt).
34+
35+
End IStateType.

theories/Structures/IXMonad.v

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
Require Import ExtLib.Structures.Monad.
2+
Import Applicative.
3+
4+
Set Implicit Arguments.
5+
Set Strict Implicit.
6+
7+
Polymorphic Class IxMonad@{d c} (m : Type@{d} -> Type@{d} -> Type@{c} -> Type@{c}) : Type :=
8+
{ ret : forall {i : Type@{d}} {a : Type@{d}}, a -> m i i a
9+
; bind : forall {i j k a b: Type@{d}}, m i j a -> (a -> m j k b) -> m i k b
10+
}.
11+
12+
Module IxMonadNotation.
13+
14+
Delimit Scope ixmonad_scope with ixmonad.
15+
16+
Notation "c >>= f" := (@bind _ _ _ _ _ _ _ c f) (at level 50, left associativity) : ixmonad_scope.
17+
Notation "f =<< c" := (@bind _ _ _ _ _ _ _ c f) (at level 51, right associativity) : ixmonad_scope.
18+
19+
Notation "x <- c1 ;; c2" := (@bind _ _ _ _ _ _ _ c1 (fun x => c2))
20+
(at level 100, c1 at next level, right associativity) : ixmonad_scope.
21+
22+
Notation "e1 ;; e2" := (_ <- e1%ixmonad ;; e2%ixmonad)%ixmonad
23+
(at level 100, right associativity) : ixmonad_scope.
24+
25+
Notation "' pat <- c1 ;; c2" :=
26+
(@bind _ _ _ _ _ _ _ c1 (fun x => match x with pat => c2 end))
27+
(at level 100, pat pattern, c1 at next level, right associativity) : monad_scope.
28+
29+
End IxMonadNotation.
30+
31+
Global
32+
Polymorphic Instance Applicative_Monad {m i} {M:IxMonad m}
33+
: Applicative (m i i) :=
34+
{| Applicative.pure := fun (A : Type) (X : A) => ret X
35+
; Applicative.ap := fun (A B : Type) (X : m i i (A -> B)) (X0 : m i i A) => bind X (fun X1 : A -> B => bind X0 (fun X2 : A => ret (X1 X2)))
36+
|}.

0 commit comments

Comments
 (0)