[Haskell-beginners] How works this `do` example?
Francesco Ariis
fa-ml at ariis.it
Thu Jul 13 09:06:13 UTC 2017
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?
More information about the Beginners
mailing list