Multiple functions applied to a single value

Graham Klyne GK at ninebynine.org
Mon Dec 8 16:33:53 EST 2003


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



More information about the Haskell-Cafe mailing list