Commit b62605e5 authored by Herbert Valerio Riedel's avatar Herbert Valerio Riedel 🕺 Committed by Austin Seipp

Add `MonadPlus IO` and `Alternative IO` instances

This requires adding a new primitive `mplusIO` to `GHC.IO`

Update transformers submodule to accomodate extant orphan instances.

Reviewed By: austin, bgamari

Differential Revision: https://phabricator.haskell.org/D1148

GHC Trac Issues: #10755
parent 8c80dcc1
......@@ -111,7 +111,7 @@ import GHC.CString
import GHC.Magic
import GHC.Prim
import GHC.Err
import {-# SOURCE #-} GHC.IO (failIO)
import {-# SOURCE #-} GHC.IO (failIO,mplusIO)
import GHC.Tuple () -- Note [Depend on GHC.Tuple]
import GHC.Integer () -- Note [Depend on GHC.Integer]
......@@ -1079,6 +1079,12 @@ instance Monad IO where
(>>=) = bindIO
fail s = failIO s
instance Alternative IO where
empty = failIO "mzero"
(<|>) = mplusIO
instance MonadPlus IO
returnIO :: a -> IO a
returnIO x = IO $ \ s -> (# s, x #)
......
......@@ -23,7 +23,7 @@
-----------------------------------------------------------------------------
module GHC.IO (
IO(..), unIO, failIO, liftIO,
IO(..), unIO, failIO, liftIO, mplusIO,
unsafePerformIO, unsafeInterleaveIO,
unsafeDupablePerformIO, unsafeDupableInterleaveIO,
noDuplicate,
......@@ -45,7 +45,7 @@ import GHC.ST
import GHC.Exception
import GHC.Show
import {-# SOURCE #-} GHC.IO.Exception ( userError )
import {-# SOURCE #-} GHC.IO.Exception ( userError, IOError )
-- ---------------------------------------------------------------------------
-- The IO Monad
......@@ -292,6 +292,12 @@ catchAny :: IO a -> (forall e . Exception e => e -> IO a) -> IO a
catchAny (IO io) handler = IO $ catch# io handler'
where handler' (SomeException e) = unIO (handler e)
mplusIO :: IO a -> IO a -> IO a
mplusIO m n = m `catchIOError` \ _ -> n
where catchIOError :: IO a -> (IOError -> IO a) -> IO a
catchIOError = catchException
-- | A variant of 'throw' that can only be used within the 'IO' monad.
--
-- Although 'throwIO' has a type that is an instance of the type of 'throw', the
......
......@@ -6,4 +6,4 @@ module GHC.IO where
import GHC.Types
failIO :: [Char] -> IO a
mplusIO :: IO a -> IO a -> IO a
......@@ -85,6 +85,9 @@
* The `MINIMAL` definition of `ArrowChoice` is now `left OR (+++)`.
* Add `MonadPlus IO` and `Alternative IO` instances
(previously orphans in `transformers`) (#10755)
## 4.8.2.0 *Oct 2015*
* Bundled with GHC 7.10.3
......
Subproject commit 0c7207e9702afb5344cc33892eb6da9126a85cf3
Subproject commit 5ccb747e67d579e3f212fd3526469c35282e532e
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment