|
1 | 1 | Require Import ExtLib.Structures.Monads. |
2 | 2 | Require Import ExtLib.Structures.Monoid. |
3 | 3 | Require Import ExtLib.Data.PPair. |
| 4 | +Require Import ExtLib.Data.Monads.IdentityMonad. |
| 5 | + |
| 6 | +Require Import Coq.Program.Basics. (* for (∘) *) |
4 | 7 |
|
5 | 8 | Set Implicit Arguments. |
6 | 9 | Set Maximal Implicit Insertion. |
@@ -86,3 +89,85 @@ Arguments mkWriterT {_} _ {_ _} _. |
86 | 89 | Arguments runWriterT {S} {Monoid_S} {m} {t} _. |
87 | 90 | Arguments evalWriterT {S} {Monoid_S} {m} {M} {T} _. |
88 | 91 | 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