[Haskell-beginners] (->) reader/environment

Grzegorz Milka grzegorzmilka at gmail.com
Thu Jul 23 08:58:19 UTC 2015


Hi Martin,

You certainly know what (->) is, but you might be confused about its
prefix notation. (->) is a type constructor which represent a normal
Haskell function. That is,(->) A B represents a type of a function from
type A to type B. Since (->) is a right-associative infix operator we
almost always write it in the infix form: a -> b -> c, which means a ->
(b -> c), which means in the prefix form:(->) a ((->) b c).
Similarly + is the normal, infix addition operator, but written in
parenthesis you can write it in prefix form, that is: (+) 2 2 == 1 + 3.

When you import Control.Monad.Reader, ghci will tell you that:

Prelude Control.Monad.Reader> :info (->)
data (->) a b     -- Defined in `GHC.Prim'
instance Monad ((->) r) -- Defined in `GHC.Base'
instance Functor ((->) r) -- Defined in `GHC.Base'
instance MonadReader r ((->) r)

That is a type contructor (still not a complete type!) (->) r is of
class MonadReader r. In other words, a function which accepts type r may
act as a reader from environment of type r. It might be easier to
understand after reading its implementation.

instance MonadReader r ((->) r) where
    ask       = id
    local f m = m . f


Best,
Grzegorz Milka

On 23.07.2015 10:07, Martin Vlk wrote:
> Hi, this one is a bit of a mystery to me.
>
> I am working on solutions to cis194/NICTA Haskell exercises and among
> others I am asked to work with (->).
>
> I understand it refers to the reader/environment design patterns, but I
> am stumped as for what exactly it is. It doesn't seem to be an ordinary
> type, I know it is used as type constructor in type declarations, so
> that suggests it's a function, but somehow special. Looking in Hoogle it
> seems to be Haskell keyword - e.g. not an ordinary function.
>
> So what is it - type constructor, a keyword, how is it related to the
> reader/environment pattern?
>
> From GHCI's :info (->) I get:
> data (->) a b 	-- Defined in ‘ghc-prim-0.4.0.0:GHC.Prim’
> instance P.Monad ((->) r) -- Defined in ‘GHC.Base’
> instance P.Functor ((->) r) -- Defined in ‘GHC.Base’
> instance P.Applicative ((->) a) -- Defined in ‘GHC.Base’
> instance P.Monoid b => P.Monoid (a -> b) -- Defined in ‘GHC.Base’
>
> Cheers
> Martin
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: OpenPGP digital signature
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20150723/1cc7e7ee/attachment.sig>


More information about the Beginners mailing list