Re: Multiple functions applied to a single value

A little while ago, I sent a long rambling message [1] containing a key question in response to an earlier helpful response. I guess my ramblings were quite reasonably passed over as too much waffle, and the question was lost. Here, I isolate the question. From the earlier response, I understand that: ((->) r) is an instance of Monad, but I haven't found a definition of the Monad instance functions. I think these do the job, and they satisfy the monad laws [2]: [[ instance Monad ((->) r) where return a = const a g1 >>= f = \e -> f (g1 e) e ]] Can anyone confirm, or point me at an actual definition? #g -- [1] http://haskell.org/pipermail/haskell-cafe/2003-November/005566.html [2] Checking the Monad laws for the above definitions (cf. Haskell report p90): (A) return a >>= k = [g1/const a][f/k] \e -> f (g1 e) e = \e -> k (const a e) e = \e -> k a e = k a -- as required. (B) m >>= return = [g1/m][f/const] \e -> f (g1 e) e = \e -> const (m e) e = \e -> (m e) = m -- as required. (C) m >>= (\x -> k x >>= h) = [g1/m][f/(\x -> k x >>= h)] \e -> f (g1 e) e = \e -> (\x -> k x >>= h) (m e) e = \e -> (k (m e) >>= h) e = \e -> ([g1/k (m e)][f/h] \e1 -> f (g1 e1) e1) e = \e -> (\e1 -> h (k (m e) e1) e1) e = \e -> h (k (m e) e) e (m >>= k) >>= h = ([g1/m][f/k] \e -> f (g1 e) e) >>= h = (\e -> k (m e) e) >>= h = [g1/(\e -> k (m e) e)][f/h] \e1 -> f (g1 e1) e1 = \e1 -> h ((\e -> k (m e) e) e1) e1 = \e1 -> h (k (m e1) e1) e1 = \e -> h (k (m e) e) e So both expressions are equivalent, as required. ------------ Graham Klyne For email: http://www.ninebynine.org/#Contact

On Mon, 08 Dec 2003 16:33:53 +0000
Graham Klyne
A little while ago, I sent a long rambling message [1] containing a key question in response to an earlier helpful response. I guess my ramblings were quite reasonably passed over as too much waffle, and the question was lost. Here, I isolate the question.
From the earlier response, I understand that:
((->) r)
is an instance of Monad, but I haven't found a definition of the Monad instance functions. I think these do the job, and they satisfy the monad laws [2]:
[[ instance Monad ((->) r) where return a = const a g1 >>= f = \e -> f (g1 e) e ]]
Can anyone confirm, or point me at an actual definition?
http://www.haskell.org/hawiki/MonadReader There's also the source of Control.Monad.Reader (and the new library) as well as various papers containing implementations. And, as long as you didn't make a mistake below, you've proven it. Well, you've proven it's a monad in this way.
[2] Checking the Monad laws for the above definitions (cf. Haskell report p90):
(A) return a >>= k = [g1/const a][f/k] \e -> f (g1 e) e = \e -> k (const a e) e = \e -> k a e = k a -- as required.
(B) m >>= return = [g1/m][f/const] \e -> f (g1 e) e = \e -> const (m e) e = \e -> (m e) = m -- as required.
(C) m >>= (\x -> k x >>= h) = [g1/m][f/(\x -> k x >>= h)] \e -> f (g1 e) e = \e -> (\x -> k x >>= h) (m e) e = \e -> (k (m e) >>= h) e = \e -> ([g1/k (m e)][f/h] \e1 -> f (g1 e1) e1) e = \e -> (\e1 -> h (k (m e) e1) e1) e = \e -> h (k (m e) e) e
(m >>= k) >>= h = ([g1/m][f/k] \e -> f (g1 e) e) >>= h = (\e -> k (m e) e) >>= h = [g1/(\e -> k (m e) e)][f/h] \e1 -> f (g1 e1) e1 = \e1 -> h ((\e -> k (m e) e) e1) e1 = \e1 -> h (k (m e1) e1) e1 = \e -> h (k (m e) e) e
So both expressions are equivalent, as required.

Control.Monad.Reader defines instances of Monad and MonadReader for ((->) r). Strangely enough, the documentation claims the Monad instance comes from Control.Monad, which is untrue. Here's the relevant chunk of the file. It looks like you came up with exactly the same code (modulo names). -- ---------------------------------------------------------------------------- -- The partially applied function type is a simple reader monad instance Functor ((->) r) where fmap = (.) instance Monad ((->) r) where return = const m >>= k = \r -> k (m r) r instance MonadFix ((->) r) where mfix f = \r -> let a = f a r in a instance MonadReader r ((->) r) where ask = id local f m = m . f Brandon
participants (3)
-
Brandon Michael Moore
-
Derek Elkins
-
Graham Klyne