[Haskell-beginners] (->) reader/environment
Martin Vlk
martin at vlkk.cz
Thu Jul 23 09:03:29 UTC 2015
Found some info on Typeclassopedia
(https://wiki.haskell.org/Typeclassopedia) - see below.
It sounds like this is basically a "function with one parameter already
applied". The parameter is then available for further usage..
----
((->) e) (which can be thought of as (e ->); see above), the type
of functions which take a value of type e as a parameter, is a Functor.
As a container, (e -> a) represents a (possibly infinite) set of values
of a, indexed by values of e. Alternatively, and more usefully, ((->) e)
can be thought of as a context in which a value of type e is available
to be consulted in a read-only fashion. This is also why ((->) e) is
sometimes referred to as the reader monad; more on this later.
...then:
As mentioned earlier, ((->) e) is known as the reader monad, since it
describes computations in which a value of type e is available as a
read-only environment. The Control.Monad.Reader module provides the
Reader e a type, which is just a convenient newtype wrapper around (e ->
a), along with an appropriate Monad instance and some Reader-specific
utility functions such as ask (retrieve the environment), asks (retrieve
a function of the environment), and local (run a subcomputation under a
different environment).
Martin
Martin Vlk:
> 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
>
More information about the Beginners
mailing list