11Require Import ExtLib.Structures.Monads.
22Require Import ExtLib.Structures.Monoid.
3+ Require Import ExtLib.Data.PPair.
34
45Set Implicit Arguments .
56Set Maximal Implicit Insertion.
7+ Set Universe Polymorphism.
8+
9+ Set Printing Universes .
610
711Section WriterType.
8- Variable S : Type.
12+ Polymorphic Universe s d c.
13+ Variable S : Type@{s}.
914
10- Record writerT (Monoid_S : Monoid S) (m : Type -> Type ) (t : Type ) : Type := mkWriterT
11- { runWriterT : m (t * S)%type }.
15+ Record writerT (Monoid_S : Monoid@{s} S) (m : Type @{d} -> Type @{c} ) (t : Type @{d} ) : Type := mkWriterT
16+ { runWriterT : m (pprod t S)%type }.
1217
1318 Variable Monoid_S : Monoid S.
14- Variable m : Type -> Type.
19+ Variable m : Type@{d} -> Type@{c} .
1520 Context {M : Monad m}.
1621
17- Definition execWriterT {T} (c : writerT Monoid_S m T) : m S :=
18- bind (runWriterT c) (fun (x : T * S) => ret (snd x)).
22+ Definition execWriterT {T} (c : writerT Monoid_S m T) : m S :=
23+ bind (runWriterT c) (fun (x : pprod T S) => ret (psnd x)).
24+
25+ Definition evalWriterT {T} (c : writerT Monoid_S m T) : m T :=
26+ bind (runWriterT c) (fun (x : pprod T S) => ret (pfst x)).
1927
20- Definition evalWriterT {T} (c : writerT Monoid_S m T) : m T :=
21- bind (runWriterT c) (fun (x : T * S) => ret (fst x)).
28+ Local Notation "( x , y )" := (ppair x y).
2229
2330 Global Instance Monad_writerT : Monad (writerT Monoid_S m) :=
2431 { ret := fun _ x => mkWriterT _ _ _ (@ret _ M _ (x, monoid_unit Monoid_S))
2532 ; bind := fun _ _ c1 c2 =>
2633 mkWriterT _ _ _ (
2734 @bind _ M _ _ (runWriterT c1) (fun v =>
28- bind (runWriterT (c2 (fst v))) (fun v' =>
29- ret (fst v', monoid_plus Monoid_S (snd v) (snd v')))))
35+ bind (runWriterT (c2 (pfst v))) (fun v' =>
36+ ret (pfst v', monoid_plus Monoid_S (psnd v) (psnd v')))))
3037 }.
3138
3239 Global Instance Writer_writerT : MonadWriter Monoid_S (writerT Monoid_S m) :=
3340 { tell := fun x => mkWriterT _ _ _ (ret (tt, x))
34- ; listen := fun _ c => mkWriterT _ _ _ (bind (runWriterT c) (fun x => ret (fst x, snd x, snd x)))
35- ; pass := fun _ c => mkWriterT _ _ _ (bind (runWriterT c) (fun x => ret (let '(x,ss,s) := x in (x, ss s))))
41+ ; listen := fun _ c => mkWriterT _ _ _ (bind (runWriterT c)
42+ (fun x => ret (pair (pfst x) (psnd x), psnd x)))
43+ ; pass := fun _ c => mkWriterT _ _ _ (bind (runWriterT c)
44+ (fun x => ret (let '(ppair (pair x ss) s) := x in (x, ss s))))
3645 }.
3746
3847 Global Instance MonadT_writerT : MonadT (writerT Monoid_S m) m :=
@@ -58,14 +67,22 @@ Section WriterType.
5867 ; catch := fun _ c h => mkWriterT _ _ _ (catch (runWriterT c) (fun x => runWriterT (h x)))
5968 }.
6069
61- Global Instance Writer_writerT_pass {T} {MonT : Monoid T} {_ : Monad m} {_ : MonadWriter MonT m} : MonadWriter MonT (writerT Monoid_S m) :=
62- { tell := fun x => mkWriterT _ m _ (bind (tell x) (fun x => ret (x, monoid_unit Monoid_S)))
63- ; listen := fun _ c => mkWriterT _ m _ (bind (listen (runWriterT c)) (fun x => let '(a,t,s) := x in ret (a,s,t)))
64- ; pass := fun _ c => mkWriterT _ m _ (pass (bind (runWriterT c) (fun x => let '(a,t,s) := x in ret (a,s,t))))
70+ Global Instance Writer_writerT_pass {T} {MonT : Monoid T} {M : Monad m} {MW : MonadWriter MonT m}
71+ : MonadWriter MonT (writerT Monoid_S m) :=
72+ { tell := fun x => mkWriterT _ m _ (bind (tell x)
73+ (fun x => ret (x, monoid_unit Monoid_S)))
74+ ; listen := fun _ c => mkWriterT _ m _ (bind (m:=m) (@listen _ _ _ MW _ (runWriterT c))
75+ (fun x => let '(pair (ppair a t) s) := x in
76+ ret (m:=m) (pair a s,t)))
77+ ; pass := fun _ c => mkWriterT _ m _ (@pass _ _ _ MW _
78+ (bind (m:=m) (runWriterT c)
79+ (fun x => let '(ppair (pair a t) s) := x in
80+ ret (m:=m) (pair (ppair a s) t))))
6581 }.
6682
6783End WriterType.
6884
85+ Arguments mkWriterT {_} _ {_ _} _.
6986Arguments runWriterT {S} {Monoid_S} {m} {t} _.
7087Arguments evalWriterT {S} {Monoid_S} {m} {M} {T} _.
7188Arguments execWriterT {S} {Monoid_S} {m} {M} {T} _.
0 commit comments