Monoid to Monad

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
{-# LANGUAGE RankNTypes, TypeOperators, NoImplicitPrelude, ScopedTypeVariables #-}
module MonoidMonad where
import Prelude (Functor(..), Eq(..), id, (.), concat, Integer(..), Int(..), Bool(..), fromInteger, (*))

-- Natural transformations of functors
type f :-> g = (forall x. f x -> g x)

data    (a :* b)   = P a b
newtype (f :. g) x = O (f (g x))
o = O
unO (O x) = x
instance (Functor f, Functor g) => Functor (f :. g) where
    fmap f = o . fmap (fmap f) . unO

data One  = One
data Id x = Id x

class Monoid m where
   one    :: One       -> m
   mult   :: (m :* m)  -> m

class Functor m => 
      Monad m where
   one'   :: Id       :-> m
   mult'  :: (m :. m) :-> m

left  :: a  -> (One :* a)
left' :: a :-> ( Id :. a)

left  a = P One a
left'   = o . Id

right  ::              a  -> (a :* One)
right' :: Functor a => a :-> (a :.  Id)

right  a = P a One
right'   = o . fmap Id

assoc  ::              ((a :* b) :* c)  -> (a :* (b :* c))
assoc' :: Functor a => ((a :. b) :. c) :-> (a :. (b :. c))

assoc  (P (P a b) c) = P a (P b c)
assoc' = o . fmap o . unO . unO

unassoc  ::              (a :* (b :* c))  -> ((a :* b) :* c)
unassoc' :: Functor a => (a :. (b :. c)) :-> ((a :. b) :. c)

unassoc (P a (P b c)) = P (P a b) c
unassoc' = o . o . fmap unO . unO

first  :: (a  -> b) -> ((a :* c)  -> (b :* c))
first' :: (a :-> b) -> ((a :. c) :-> (b :. c))

first f (P a c) = P (f a) c
first' f = o . f . unO

second  ::              (a  -> b) -> ((c :* a)  -> (c :* b))
second' :: Functor c => (a :-> b) -> ((c :. a) :-> (c :. b))

second f (P c a) = P c (f a)
second' f = o . fmap f . unO

(<&.>) :: (Functor a, Functor b, Functor c, Functor d) => 
          (a :-> b) -> (c :-> d) -> ((a :. c) :-> (b :. d))
(<&*>) :: (a  -> b) -> (c  -> d) -> ((a :* c)  -> (b :* d))

f <&*> g = first  f . second  g
f <&.> g = first' f . second' g

-- Laws
--
-- law1 : identity
--    (one * x) = x = (x * one)
--
-- law2 : associativity
--   ((x * y) * z) = (x * (y * z)) 

law1_left,  law1_right  :: Monoid m => m  -> m
law1_left', law1_right' ::  Monad m => m :-> m

--               m
-- left       -> (One :* m)
-- first one  -> (m   :* m)
-- mult       -> m
law1_left    = mult  . first  one  . left
law1_left'   = mult' . first' one' . left'

--               m
-- right      -> (m :* One)
-- second one -> (m :* m)
-- mult       -> m
law1_right   = mult  . second  one  . right
law1_right'  = mult' . second' one' . right'

law2_left,  law2_right  :: Monoid m => ((m :* m) :* m)  -> m
law2_left', law2_right' ::  Monad m => ((m :. m) :. m) :-> m

--               (m :* m) :* m   
-- first mult -> m        :* m
-- mult       -> m
law2_left   = mult  . first  mult
law2_left'  = mult' . first' mult'

--                (m :* m) :* m
-- assoc       -> m :* (m :* m)
-- second mult -> m :* m
-- mult        -> m
law2_right  = mult  . second  mult  . assoc
law2_right' = mult' . second' mult' . assoc'

instance Functor Id where fmap f (Id x) = Id (f x)
unId (Id x) = x

-- QuickCheck test properties
instance Monoid Integer where
    one One      = 1
    mult (P x y) = x * y

singleton x = [x]
instance Monad [] where
    one'  = singleton . unId
    mult' = concat . unO

prop_law1_left,  prop_law1_right  :: Integer -> Bool
prop_law1_left', prop_law1_right' :: [Integer] -> Bool

prop_law1_left  x = law1_left  x == id x
prop_law1_left' x = law1_left' x == id x

prop_law1_right  x = law1_right  x == id x
prop_law1_right' x = law1_right' x == id x

prop_law2  :: (Integer,Integer,Integer) -> Bool
prop_law2' :: [] ([] ([] Int)) -> Bool

prop_law2  (x,y,z) = law2_left  v == law2_right  v where v = P (P x y) z
prop_law2' xsss    = law2_left' v == law2_right' v where v = o (o xsss)

-- bonus: Haskell monad laws proved in these terms
return :: Monad m => a -> m a
return = one' . Id

(>>=) :: Monad m => m a -> (a -> m b) -> m b
m >>= k = (mult' . o . fmap k) m

--
-- Monad laws:
--   1) return x >>= f    = f x
--   2) m >>= return      = m
--   3) (m >>= f) >>= g   = m >>= (\x -> f x >>= g)
--
-- (1)  return x >>= f    =     f x
--
--   \x -> return x >>= f
--   [expand return]
--   \x -> (one' . Id) x >>= f
--   [expand >>=]
--   \x -> (mult' . o . fmap f) ((one' . Id) x)
--   [collapse .]
--   \x -> (mult' . o . fmap f . one' . Id) x
--   [eta reduce]
--   mult' . o . fmap f . one' . Id
--   [one' is a natural transformation]
--   mult' . o . one' . fmap f . Id
--   [expand fmap[Id]]
--   mult' . o . one' . Id . f
--   [o/unO are inverses]
--   mult' . o . one' . unO . o . Id . f
--   [collapse first']
--   mult' . first' one' . o . Id . f
--   [collapse left']
--   mult' . first' one' . left' . f
--   [monoid left identity law]
--   id . f
--   [expand .]
--   \x -> id (f x)
--   [expand id & beta reduce]
--   \x -> f x
-- QED
--
-- (2)  m >>= return     =     m
--
--    \m -> m >>= return
--    [expand >>=]
--    \m -> (mult' . o . fmap return) m
--    [eta reduce]
--    mult' . o . fmap return
--    [expand return]
--    mult' . o . fmap (one' . Id)
--    [functor composition law]
--    mult' . o . fmap one' . fmap Id
--    [add unO . o (inverses)]
--    mult' . o . fmap one' . unO . o . fmap Id
--    [collapse second']
--    mult' . second one' . o . fmap Id
--    [collapse right']
--    mult' . second one' . right'
--    [monoid right identity law]
--    id
--    [expand id & alpha convert]
--    \m -> m
--  QED
--
-- (3) (m >>= f) >>= g    =    m >>= (\x -> f x >>= g)
--
--    \f g m -> (m >>= f) >>= g
--    [expand >>=]
--    \f g m -> ((mult' . o . fmap f) m) >>= g
--    [expand >>=]
--    \f g m -> (mult' . o . fmap g) ((mult' . o . fmap f) m)
--    [collapse .]
--    \f g m -> ((mult' . o . fmap g) . (mult' . o . fmap f)) m
--    [eta reduce]
--    \f g   -> (mult' . o . fmap g) . (mult' . o . fmap f)
--    [. is associative]
--    \f g   -> mult' . o . fmap g . mult' . o . fmap f
--    [mult' is a natural transformation]
--    \f g   -> mult' . o . mult' . fmap g . o . fmap f
--    [expand fmap[:.]]
--    \f g   -> mult' . o . mult' . o . fmap (fmap g) . unO . o . fmap f
--    [remove unO . o (inverses)]
--    \f g   -> mult' . o . mult' . o . fmap (fmap g) . fmap f
--    [insert unO . o (inverses)]
--    \f g   -> mult' . o . mult' . unO . o . o . fmap (fmap g) . fmap f
--    [collapse first']
--    \f g   -> mult' . first' mult' . o . o . fmap (fmap g) . fmap f
--    [monoid associativity law]
--    \f g   -> mult' . second mult' . assoc' . o . o . fmap (fmap g) . fmap f
--    [expand second' and assoc']
--    \f g   -> mult' . o . fmap mult' . unO . o . fmap o . unO . unO . o . o . fmap (fmap g) . fmap f
--    [remove unO . o (inverses) three times]
--    \f g   -> mult' . o . fmap mult' . fmap o . fmap (fmap g) . fmap f
--    [functor composition law]
--    \f g   -> mult' . o . fmap (mult' . o . fmap g . f)
--    [. is associative]
--    \f g   -> mult' . o . fmap ((mult' . o . fmap g) . f)
--    [expand .]
--    \f g   -> mult' . o . fmap (\x -> (mult' . o . fmap g) (f x))
--    [collapse >>=]
--    \f g   -> mult' . o . fmap (\x -> f x >>= g)
--    [eta expand]
--    \f g m -> (mult' . o . fmap (\x -> f x >>= g)) m
--    [collapse >>=]
--    \f g m -> m >>= (\x -> f x >>= g)
-- QED