Hacker News new | ask | show | jobs
by gilmi 3019 days ago
> a common problem I have in Haskell is figuring out _which_ monad a certain `do` block is using.

Does type signatures not help in that case?

1 comments

Yes, when people use them. There is no really elegant way of adding a type annotation to a `do` block.

    (do x <- pure 1
        y <- pure 5
        pure (x + y)) :: Maybe Int
It isn't uncommon to have to scan the `do` block for some statement that constrains the monad somehow.

This is a known and discussed problem, so I'll refer you to the article usually reference around this issue: https://wiki.haskell.org/Do_notation_considered_harmful

Many times do blocks are passed into functions so the monad type can be looked up from the function definition.

    flip runStateT s $ do ...
To find the type of this do block I don't have to scan it I just have to look up the definition of runStateT.

If I have to add an explicit annotation to a do block it's simple enough to separate it into another function.

    justSix :: Maybe Int
    justSix = do
        x <- pure 1
        y <- pure 5
        pure (x + y)
I read the article and couldn't find the part where they said that type inference is a problem with do notation.