Skip to content

Commit 974a79e

Browse files
authored
Merge pull request #36 from coq-ext-lib/vzaliva-v8.6-writer
a few cosmetic changes to mapWriterT, etc.
2 parents c72be4e + c562d4f commit 974a79e

File tree

1 file changed

+54
-52
lines changed

1 file changed

+54
-52
lines changed

theories/Data/Monads/WriterMonad.v

Lines changed: 54 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,16 @@ Section WriterType.
1515
Polymorphic Universe s d c.
1616
Variable S : Type@{s}.
1717

18-
Record writerT (Monoid_S : Monoid@{s} S) (m : Type@{d} -> Type@{c}) (t : Type@{d}) : Type := mkWriterT
18+
Record writerT (Monoid_S : Monoid@{s} S) (m : Type@{d} -> Type@{c})
19+
(t : Type@{d}) : Type := mkWriterT
1920
{ runWriterT : m (pprod t S)%type }.
2021

2122
Variable Monoid_S : Monoid S.
2223
Variable m : Type@{d} -> Type@{c}.
2324
Context {M : Monad m}.
2425

26+
Arguments mkWriterT _ [_ _] _.
27+
2528
Definition execWriterT {T} (c : writerT Monoid_S m T) : m S :=
2629
bind (runWriterT c) (fun (x : pprod T S) => ret (psnd x)).
2730

@@ -31,30 +34,30 @@ Section WriterType.
3134
Local Notation "( x , y )" := (ppair x y).
3235

3336
Global Instance Monad_writerT : Monad (writerT Monoid_S m) :=
34-
{ ret := fun _ x => mkWriterT _ _ _ (@ret _ M _ (x, monoid_unit Monoid_S))
37+
{ ret := fun _ x => mkWriterT _ (@ret _ M _ (x, monoid_unit Monoid_S))
3538
; bind := fun _ _ c1 c2 =>
36-
mkWriterT _ _ _ (
39+
mkWriterT _ (
3740
@bind _ M _ _ (runWriterT c1) (fun v =>
3841
bind (runWriterT (c2 (pfst v))) (fun v' =>
3942
ret (pfst v', monoid_plus Monoid_S (psnd v) (psnd v')))))
4043
}.
4144

4245
Global Instance Writer_writerT : MonadWriter Monoid_S (writerT Monoid_S m) :=
43-
{ tell := fun x => mkWriterT _ _ _ (ret (tt, x))
44-
; listen := fun _ c => mkWriterT _ _ _ (bind (runWriterT c)
45-
(fun x => ret (pair (pfst x) (psnd x), psnd x)))
46-
; pass := fun _ c => mkWriterT _ _ _ (bind (runWriterT c)
47-
(fun x => ret (let '(ppair (pair x ss) s) := x in (x, ss s))))
46+
{ tell := fun x => mkWriterT _ (ret (tt, x))
47+
; listen := fun _ c => mkWriterT _ (bind (runWriterT c)
48+
(fun x => ret (pair (pfst x) (psnd x), psnd x)))
49+
; pass := fun _ c => mkWriterT _ (bind (runWriterT c)
50+
(fun x => ret (let '(ppair (pair x ss) s) := x in (x, ss s))))
4851
}.
4952

5053
Global Instance MonadT_writerT : MonadT (writerT Monoid_S m) m :=
51-
{ lift := fun _ c => mkWriterT _ _ _ (bind c (fun x => ret (x, monoid_unit Monoid_S)))
54+
{ lift := fun _ c => mkWriterT _ (bind c (fun x => ret (x, monoid_unit Monoid_S)))
5255
}.
5356

5457
Global Instance Reader_writerT {S'} (MR : MonadReader S' m) : MonadReader S' (writerT Monoid_S m) :=
55-
{ ask := mkWriterT _ _ _ (bind ask (fun v => @ret _ M _ (v, monoid_unit Monoid_S)))
58+
{ ask := mkWriterT _ (bind ask (fun v => @ret _ M _ (v, monoid_unit Monoid_S)))
5659
; local := fun _ f c =>
57-
mkWriterT _ _ _ (local f (runWriterT c))
60+
mkWriterT _ (local f (runWriterT c))
5861
}.
5962

6063
Global Instance State_writerT {S'} (MR : MonadState S' m) : MonadState S' (writerT Monoid_S m) :=
@@ -67,20 +70,20 @@ Section WriterType.
6770

6871
Global Instance Exception_writerT {E} (ME : MonadExc E m) : MonadExc E (writerT Monoid_S m) :=
6972
{ raise := fun _ v => lift (raise v)
70-
; catch := fun _ c h => mkWriterT _ _ _ (catch (runWriterT c) (fun x => runWriterT (h x)))
73+
; catch := fun _ c h => mkWriterT _ (catch (runWriterT c) (fun x => runWriterT (h x)))
7174
}.
7275

7376
Global Instance Writer_writerT_pass {T} {MonT : Monoid T} {M : Monad m} {MW : MonadWriter MonT m}
7477
: MonadWriter MonT (writerT Monoid_S m) :=
75-
{ tell := fun x => mkWriterT _ m _ (bind (tell x)
78+
{ tell := fun x => mkWriterT _ (bind (tell x)
7679
(fun x => ret (x, monoid_unit Monoid_S)))
77-
; listen := fun _ c => mkWriterT _ m _ (bind (m:=m) (@listen _ _ _ MW _ (runWriterT c))
78-
(fun x => let '(pair (ppair a t) s) := x in
79-
ret (m:=m) (pair a s,t)))
80-
; pass := fun _ c => mkWriterT _ m _ (@pass _ _ _ MW _
81-
(bind (m:=m) (runWriterT c)
82-
(fun x => let '(ppair (pair a t) s) := x in
83-
ret (m:=m) (pair (ppair a s) t))))
80+
; listen := fun _ c => mkWriterT _ (bind (m:=m) (@listen _ _ _ MW _ (runWriterT c))
81+
(fun x => let '(pair (ppair a t) s) := x in
82+
ret (m:=m) (pair a s,t)))
83+
; pass := fun _ c => mkWriterT _ (@pass _ _ _ MW _
84+
(bind (m:=m) (runWriterT c)
85+
(fun x => let '(ppair (pair a t) s) := x in
86+
ret (m:=m) (pair (ppair a s) t))))
8487
}.
8588

8689
End WriterType.
@@ -90,54 +93,57 @@ Arguments runWriterT {S} {Monoid_S} {m} {t} _.
9093
Arguments evalWriterT {S} {Monoid_S} {m} {M} {T} _.
9194
Arguments execWriterT {S} {Monoid_S} {m} {M} {T} _.
9295

96+
Local Open Scope program_scope.
97+
9398
Section MapWriterT.
9499
Variable W W': Type.
95100
Variable Monoid_W : Monoid W.
96101
Variable Monoid_W' : Monoid W'.
97102
Variable m n : Type -> Type.
98103
Variable A B: Type.
99104

100-
Open Scope program_scope.
101-
102105
(** Map both the return value and output of a computation using the given function.
103106
[[ 'runWriterT' ('mapWriterT' f m) = f ('runWriterT' m) ]]
104107
*)
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.
108+
Definition mapWriterT (f: m (pprod A W) -> n (pprod B W'))
109+
: writerT Monoid_W m A -> writerT Monoid_W' n B :=
110+
mkWriterT Monoid_W' ∘ f ∘ runWriterT.
109111

110112
End MapWriterT.
111113

112114
Section CastWriterT.
113115
Variable W : Type.
114116
Variable Monoid_W Monoid_W': Monoid W.
115117
Variable m : Type -> Type.
116-
Variable A: Type.
117-
118-
Open Scope program_scope.
118+
Variable A : Type.
119119

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.
120+
(* Special case of mapWriterT where mapping function is identity
121+
* Note: This function changes the `Monoid` instance.
122+
*)
123+
Definition castWriterT
124+
: writerT Monoid_W m A -> writerT Monoid_W' m A :=
125+
mkWriterT Monoid_W' ∘ runWriterT.
124126

125127
End CastWriterT.
126128

127129

128-
(** Simple wrapper around ExtLib's WriterMonadT trasformed pairing it with Identity monad to simulate classic Writer Monad *)
130+
(** Simple wrapper around `writerT` specializing the underlying monad to `Identity`
131+
** which yields the `writer` monad.
132+
**)
129133
Section WriterMonad.
130134

131135
Variable W: Type.
132136
Variable Monoid_W : Monoid W.
133137
Variable A: Type.
134138

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.
139+
Definition writer : Type -> Type :=
140+
writerT Monoid_W ident.
141+
Definition runWriter : writer A -> pprod A W :=
142+
unIdent ∘ (@runWriterT W Monoid_W ident A).
143+
Definition execWriter : writer A -> W :=
144+
psnd ∘ runWriter.
145+
Definition evalWriter : writer A -> A :=
146+
pfst ∘ runWriter.
141147

142148
End WriterMonad.
143149

@@ -147,27 +153,23 @@ Section MapWriter.
147153
Variable Monoid_W': Monoid W'.
148154
Variable A B: Type.
149155

150-
Open Scope program_scope.
151-
152156
(** Map both the return value and output of a computation using the given function.
153157
[[ 'runWriter' ('mapWriter' f m) = f ('runWriter' m) ]]
154158
*)
155-
Definition mapWriter (f: (pprod A W)%type -> (pprod B W')%type) :
156-
writer Monoid_W A -> writer Monoid_W' B
157-
:=
158-
mapWriterT Monoid_W' ident B (mkIdent ∘ f ∘ unIdent).
159+
Definition mapWriter (f: pprod A W -> pprod B W')
160+
: writer Monoid_W A -> writer Monoid_W' B :=
161+
mapWriterT Monoid_W' ident B (mkIdent ∘ f ∘ unIdent).
159162

160163
End MapWriter.
161164

162165
Section CastWriter.
163166
Variable W : Type.
164167
Variable Monoid_W Monoid_W': Monoid W.
165-
Variable A: Type.
166-
167-
Open Scope program_scope.
168+
Variable A : Type.
168169

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).
170+
(* Special case of mapWriter where mapping function is identity *)
171+
Definition castWriter
172+
: writer Monoid_W A -> writer Monoid_W' A :=
173+
castWriterT Monoid_W' (m:=ident).
172174

173175
End CastWriter.

0 commit comments

Comments
 (0)