Skip to content

Commit 0a08d4e

Browse files
committed
mapWriter, castWriter
1 parent 5d19dff commit 0a08d4e

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

theories/Data/Monads/WriterMonad.v

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
Require Import ExtLib.Structures.Monads.
22
Require Import ExtLib.Structures.Monoid.
33
Require Import ExtLib.Data.PPair.
4+
Require Import ExtLib.Data.Monads.IdentityMonad.
5+
6+
Require Import Coq.Program.Basics. (* for (∘) *)
47

58
Set Implicit Arguments.
69
Set Maximal Implicit Insertion.
@@ -86,3 +89,85 @@ Arguments mkWriterT {_} _ {_ _} _.
8689
Arguments runWriterT {S} {Monoid_S} {m} {t} _.
8790
Arguments evalWriterT {S} {Monoid_S} {m} {M} {T} _.
8891
Arguments execWriterT {S} {Monoid_S} {m} {M} {T} _.
92+
93+
Section MapWriterT.
94+
Variable A B: Type.
95+
Variable W W': Type.
96+
Variable Monoid_W : Monoid W.
97+
Variable Monoid_W' : Monoid W'.
98+
Variable m n : Type -> Type.
99+
100+
Open Scope program_scope.
101+
102+
(** Map both the return value and output of a computation using the given function.
103+
[[ 'runWriterT' ('mapWriterT' f m) = f ('runWriterT' m) ]]
104+
*)
105+
Definition mapWriterT (f: (m (pprod A W)%type) -> (n (pprod B W')%type)):
106+
(writerT Monoid_W m A) -> writerT Monoid_W' n B
107+
:=
108+
mkWriterT Monoid_W' ∘ f ∘ runWriterT.
109+
110+
End MapWriterT.
111+
112+
Section CastWriterT.
113+
Variable A: Type.
114+
Variable W : Type.
115+
Variable Monoid_W Monoid_W': Monoid W.
116+
Variable m : Type -> Type.
117+
118+
Open Scope program_scope.
119+
120+
(* Special case of mapWriterT where mapping functoin is identity *)
121+
Definition castWriterT:
122+
writerT Monoid_W m A -> writerT Monoid_W' m A
123+
:= mkWriterT Monoid_W' ∘ runWriterT.
124+
125+
End CastWriterT.
126+
127+
128+
(** Simple wrapper around ExtLib's WriterMonadT trasformed pairing it with Identity monad to simulate classic Writer Monad *)
129+
Section WriterMonad.
130+
131+
Variable W: Type.
132+
Variable A: Type.
133+
Variable Monoid_W : Monoid W.
134+
135+
Open Scope program_scope.
136+
137+
Definition writer := writerT Monoid_W ident.
138+
Definition runWriter := unIdent ∘ (@runWriterT W Monoid_W ident A).
139+
Definition execWriter:= psnd ∘ runWriter.
140+
Definition evalWriter:= pfst ∘ runWriter.
141+
142+
End WriterMonad.
143+
144+
Section MapWriter.
145+
Variable A B: Type.
146+
Variable W W' : Type.
147+
Variable Monoid_W: Monoid W.
148+
Variable Monoid_W': Monoid W'.
149+
150+
Open Scope program_scope.
151+
152+
(** Map both the return value and output of a computation using the given function.
153+
[[ 'runWriter' ('mapWriter' f m) = f ('runWriter' m) ]]
154+
*)
155+
Definition mapWriter (f: (pprod A W)%type -> (pprod B W')%type) :
156+
writer Monoid_W A -> writer Monoid_W' B
157+
:=
158+
mapWriterT B Monoid_W' ident (mkIdent ∘ f ∘ unIdent).
159+
160+
End MapWriter.
161+
162+
Section CastWriter.
163+
Variable A: Type.
164+
Variable W : Type.
165+
Variable Monoid_W Monoid_W': Monoid W.
166+
167+
Open Scope program_scope.
168+
169+
(* Special case of mapWriter where mapping functoin is identity *)
170+
Definition castWriter: writer Monoid_W A -> writer Monoid_W' A
171+
:= castWriterT Monoid_W' (m:=ident).
172+
173+
End CastWriter.

0 commit comments

Comments
 (0)