Skip to content

Recommend MonadFailDesugaring? #85

@snoyberg

Description

@snoyberg

I haven't used this extension myself previously, so my understanding may not be perfect. Consider this script:

#!/usr/bin/env stack
-- stack --resolver nightly-2018-04-15 script
{-# LANGUAGE NoImplicitPrelude #-}
-- {-# LANGUAGE MonadFailDesugaring #-}
{-# OPTIONS_GHC -Wall -Werror #-}
import RIO

main :: IO ()
main = runRIO () $ do
  Just x <- return Nothing
  return x

As is, it will crash at runtime with:

Main.hs: user error (Pattern match failure in do expression at /Users/michael/Desktop/Main.hs:10:3-8)

Note that even with -Wall -Werror turned on, the compiler does nothing to prevent us from the partial pattern match Just x <- return Nothing. By contrast, if we uncomment the {-# LANGUAGE MonadFailDesugaring #-} line, we get a compiler time error due to a missing MonadFail instance instead:

/Users/michael/Desktop/Main.hs:10:3: error:
    • No instance for (Control.Monad.Fail.MonadFail (RIO ()))
        arising from a do statement
        with the failable pattern ‘Just x’
    • In a stmt of a 'do' block: Just x <- return Nothing
      In the second argument of ‘($)’, namely
        ‘do Just x <- return Nothing
            return x’
      In the expression:
        runRIO ()
          $ do Just x <- return Nothing
               return x
   |
10 |   Just x <- return Nothing

It seems preferable to me to totally block partial pattern matches via this language extension and lack of MonadFail instance for RIO.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions