Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1.  (->) reader/environment (Martin Vlk)
   2. Re:  (->) reader/environment (Grzegorz Milka)
   3. Re:  (->) reader/environment (Martin Vlk)


----------------------------------------------------------------------

Message: 1
Date: Thu, 23 Jul 2015 08:07:04 +0000
From: Martin Vlk <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: [Haskell-beginners] (->) reader/environment
Message-ID: <[email protected]>
Content-Type: text/plain; charset=utf-8

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


------------------------------

Message: 2
Date: Thu, 23 Jul 2015 10:58:19 +0200
From: Grzegorz Milka <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] (->) reader/environment
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"

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
> [email protected]
> 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-0001.sig>

------------------------------

Message: 3
Date: Thu, 23 Jul 2015 09:03:29 +0000
From: Martin Vlk <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] (->) reader/environment
Message-ID: <[email protected]>
Content-Type: text/plain; charset=utf-8

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
> 


------------------------------

Subject: Digest Footer

_______________________________________________
Beginners mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


------------------------------

End of Beginners Digest, Vol 85, Issue 15
*****************************************

Reply via email to