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 [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners |
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 > [hidden email] > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners _______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners signature.asc (853 bytes) Download Attachment |
In reply to this post by Martin Vlk
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 > Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners |
Free forum by Nabble | Edit this page |