summaryrefslogtreecommitdiff
path: root/libraries/base/Control
diff options
context:
space:
mode:
authorHerbert Valerio Riedel <hvr@gnu.org>2015-11-03 22:40:52 +0100
committerHerbert Valerio Riedel <hvr@gnu.org>2015-11-03 22:40:59 +0100
commit8c80dcc166e4a083086d8b240d84563d0c4c4c50 (patch)
treecce4c3b7e3e19976f0444f1c97d35f5c135b4663 /libraries/base/Control
parent83fd2bafe1e9eb33323290ac251da51c09a7deb1 (diff)
downloadhaskell-8c80dcc166e4a083086d8b240d84563d0c4c4c50.tar.gz
base: Add new Control.Monad.Fail module (re #10751)
This is based on David's initial patch augmented by more extensive Haddock comments. This has been broken out of D1248 to reduce its size by splitting the patch into smaller logical pieces. On its own, this new module does nothing interesting yet. Later patches will add support for a different desugaring of `do`-blocks, at which point the new `MonadFail` class will become more useful. Reviewed By: ekmett, austin Differential Revision: https://phabricator.haskell.org/D1424
Diffstat (limited to 'libraries/base/Control')
-rw-r--r--libraries/base/Control/Monad/Fail.hs77
1 files changed, 77 insertions, 0 deletions
diff --git a/libraries/base/Control/Monad/Fail.hs b/libraries/base/Control/Monad/Fail.hs
new file mode 100644
index 0000000000..0bbe65bbed
--- /dev/null
+++ b/libraries/base/Control/Monad/Fail.hs
@@ -0,0 +1,77 @@
+{-# LANGUAGE Trustworthy #-}
+{-# LANGUAGE NoImplicitPrelude #-}
+
+-- |
+-- Module : Control.Monad.Fail
+-- Copyright : (C) 2015 David Luposchainsky,
+-- (C) 2015 Herbert Valerio Riedel
+-- License : BSD-style (see the file LICENSE)
+--
+-- Maintainer : libraries@haskell.org
+-- Stability : provisional
+-- Portability : portable
+--
+-- Transitional module providing the 'MonadFail' class and primitive
+-- instances.
+--
+-- This module can be imported for defining forward compatible
+-- 'MonadFail' instances:
+--
+-- @
+-- import qualified Control.Monad.Fail as Fail
+--
+-- instance Monad Foo where
+-- (>>=) = {- ...bind impl... -}
+--
+-- -- Provide legacy 'fail' implementation for when
+-- -- new-style MonadFail desugaring is not enabled.
+-- fail = Fail.fail
+--
+-- instance Fail.MonadFail Foo where
+-- fail = {- ...fail implementation... -}
+-- @
+--
+-- See <https://wiki.haskell.org/MonadFail_Proposal> for more details.
+--
+-- @since 4.9.0.0
+--
+module Control.Monad.Fail ( MonadFail(fail) ) where
+
+import GHC.Base (String, Monad(), Maybe(Nothing), IO())
+import {-# SOURCE #-} GHC.IO (failIO)
+
+-- | When a value is bound in @do@-notation, the pattern on the left
+-- hand side of @<-@ might not match. In this case, this class
+-- provides a function to recover.
+--
+-- A 'Monad' without a 'MonadFail' instance may only be used in conjunction
+-- with pattern that always match, such as newtypes, tuples, data types with
+-- only a single data constructor, and irrefutable patterns (@~pat@).
+--
+-- Instances of 'MonadFail' should satisfy the following law: @fail s@ should
+-- be a left zero for '>>=',
+--
+-- @
+-- fail s >>= f = fail s
+-- @
+--
+-- If your 'Monad' is also 'MonadPlus', a popular definition is
+--
+-- @
+-- fail _ = mzero
+-- @
+--
+-- @since 4.9.0.0
+class Monad m => MonadFail m where
+ fail :: String -> m a
+
+
+instance MonadFail Maybe where
+ fail _ = Nothing
+
+instance MonadFail [] where
+ {-# INLINE fail #-}
+ fail _ = []
+
+instance MonadFail IO where
+ fail = failIO