Skip to content

Commit b9a5793

Browse files
authored
define MonadThrow and MonadCatch instances for Stream
These are equivalent to the code in the existing MonadError instance.
1 parent f6ce074 commit b9a5793

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/Streaming/Internal.hs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ module Streaming.Internal (
8686
import Control.Applicative
8787
import Control.Concurrent (threadDelay)
8888
import Control.Monad
89+
import Control.Monad.Catch (MonadThrow (..), MonadCatch (..))
8990
import Control.Monad.Error.Class
9091
import Control.Monad.Fail as Fail
9192
import Control.Monad.Morph
@@ -381,6 +382,19 @@ instance (Functor f, MonadState s m) => MonadState s (Stream f m) where
381382
{-# INLINE state #-}
382383
#endif
383384

385+
instance (Functor f, MonadThrow m) => MonadThrow (Stream f m) where
386+
throwM = lift . throwM
387+
{-# INLINE throwM #-}
388+
389+
instance (Functor f, MonadCatch m) => MonadCatch (Stream f m) where
390+
catch str f = loop str
391+
where
392+
loop x = case x of
393+
Return r -> Return r
394+
Effect m -> Effect $ fmap loop m `catch` (return . f)
395+
Step g -> Step (fmap loop g)
396+
{-# INLINABLE catch #-}
397+
384398
instance (Functor f, MonadError e m) => MonadError e (Stream f m) where
385399
throwError = lift . throwError
386400
{-# INLINE throwError #-}

0 commit comments

Comments
 (0)