[Haskell-beginners] How works this `do` example?

Baa aquagnu at gmail.com
Thu Jul 13 09:41:49 UTC 2017


I suspected that it was in Read monad, but I don't see where is it this
"Read" monad here :)  Francesco, thank you very much!!

Absolutely clear :)


В Thu, 13 Jul 2017 11:06:13 +0200
Francesco Ariis <fa-ml at ariis.it> wrote:

> On Thu, Jul 13, 2017 at 11:29:56AM +0300, Baa wrote:
> >     main :: IO ()
> >     main = f0  
> >       >>= do f1  
> >              f2  
> >       >> print "end"  
> > 
> > and I get output:
> > 
> >     "f2!"
> >     10
> >     "end"  
> 
> Hello Paul, your `main` desugars to
> 
>     main = f0 >>= (f1 >> f2) >> print "end"
> 
> Now, the quizzical part is
> 
>     λ> :t (f1 >> f2)  
>     (f1 >> f2) :: Int -> IO Int
> 
> Why does this even type checks? Because:
> 
>     λ> :i (->)  
>     [..]
>     instance Monad ((->) r) -- Defined in ‘GHC.Base’
>     [..]
> 
> ((->) r) is an instance of Monad! The instance is:
> 
>     instance Monad ((->) r) where
>         f >>= k = \r -> k (f r) r
> 
> you already know that `m >> k` is defined as `m >>= \_ -> k`, so
> 
>         f >> k = \r -> (\_ -> k) (f r) r
>                = \r -> k r
> 
> Is it clear enough?
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners



More information about the Beginners mailing list