[Haskell-beginners] 'Simple' function

Brandon Allbery allbery.b at gmail.com
Wed Jun 10 17:06:07 UTC 2015


On Wed, Jun 10, 2015 at 12:35 PM, Mike Houghton <mike_k_houghton at yahoo.co.uk
> wrote:

> asString ioStr = do
>     str <- ioStr
>     return $ str
>
> and the compiler tells me its signature is
>
> asString :: forall (m :: * -> *) b. Monad m => m b -> m b
>
> which, at this stage of my Haskell progress, is just pure Voodoo.
> Why isn’t it’s signature  asString :: IO String -> String ?
>

Because the only thing it knows about ioStr is that it is a monadic action.
IO is not the only monad, nor even the only useful monad. And "do" syntax
including <- is not specific to IO.

That said, most of the type signature it showed you is only important if
you are doing advanced things like type level programming. The short
version of that type signature is

    asString :: Monad m -> m b -> m b

asString ioStr = str where
>     str <- ioStr
>
> and then compiler says
> parse error on input ‘<-’
>

<- is part of "do" syntax, it cannot be used by itself like that.

Just to give you some idea of what's really going on, let me show you that
first one without the "do" syntax:

asString ioStr = ioStr >>= (\s -> return $ s)


(Let me additionally note that the "$" does nothing whatsoever in either
case, and can and should be left out. Moreover, (x >>= \y -> return y) is
just a long-winded way of writing (x).)

-- 
brandon s allbery kf8nh                               sine nomine associates
allbery.b at gmail.com                                  ballbery at sinenomine.net
unix, openafs, kerberos, infrastructure, xmonad        http://sinenomine.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20150610/f3463781/attachment.html>


More information about the Beginners mailing list