Skip to content

Commit 64cdaf8

Browse files
committed
io-sim: IOSim's Alternative and MonadPlus instances
1 parent dd1f489 commit 64cdaf8

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

io-sim/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
### Non-breaking changes
1010

11+
* `Alternative` & `MonadPlus` instances for `IOSim`.
12+
1113
## 1.3.1.0
1214

1315
### Non-breaking changes

io-sim/src/Control/Monad/IOSim/Types.hs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
{-# LANGUAGE NumericUnderscores #-}
1313
{-# LANGUAGE PatternSynonyms #-}
1414
{-# LANGUAGE RankNTypes #-}
15+
{-# LANGUAGE ScopedTypeVariables #-}
1516
{-# LANGUAGE TypeFamilies #-}
1617

1718
-- Needed for `SimEvent` type.
@@ -136,6 +137,7 @@ import Control.Monad.IOSimPOR.Types
136137

137138
import qualified System.IO.Error as IO.Error (userError)
138139
import Data.List (intercalate)
140+
import GHC.IO (mkUserError)
139141

140142
{-# ANN module "HLint: ignore Use readTVarIO" #-}
141143
newtype IOSim s a = IOSim { unIOSim :: forall r. (a -> SimA s r) -> SimA s r }
@@ -287,6 +289,11 @@ instance Fail.MonadFail (IOSim s) where
287289
instance MonadFix (IOSim s) where
288290
mfix f = IOSim $ oneShot $ \k -> Fix f k
289291

292+
instance Alternative (IOSim s) where
293+
empty = throwIO (mkUserError "mzero")
294+
!a <|> b = a `catch` \(_ :: IOError) -> b
295+
296+
instance MonadPlus (IOSim s)
290297

291298
instance Functor (STM s) where
292299
{-# INLINE fmap #-}

0 commit comments

Comments
 (0)