From 7a0e274b20347bc51e7c43680ba4b5c3e5cefe0f Mon Sep 17 00:00:00 2001 From: Stephen Diehl Date: Mon, 10 Oct 2016 11:13:26 +0100 Subject: [PATCH] note and hush for #22 --- protolude.cabal | 1 + src/Exceptions.hs | 28 ++++++++++++++++++++++++++++ src/Protolude.hs | 1 + 3 files changed, 30 insertions(+) create mode 100644 src/Exceptions.hs diff --git a/protolude.cabal b/protolude.cabal index 6af9af06f..a0c5fcc05 100644 --- a/protolude.cabal +++ b/protolude.cabal @@ -46,6 +46,7 @@ library Functor Semiring Bifunctor + Exceptions Panic other-modules: diff --git a/src/Exceptions.hs b/src/Exceptions.hs new file mode 100644 index 000000000..ec77b1f43 --- /dev/null +++ b/src/Exceptions.hs @@ -0,0 +1,28 @@ +{-# LANGUAGE Trustworthy #-} +{-# LANGUAGE NoImplicitPrelude #-} + +module Exceptions ( + hush, + note, + tryIO, +) where + +import Base (IO) +import Data.Function ((.)) +import Control.Monad.Trans (liftIO) +import Control.Monad.IO.Class (MonadIO) +import Control.Monad.Except (ExceptT(..), MonadError, throwError) +import Control.Exception as Exception +import Control.Applicative +import Data.Maybe (Maybe, maybe) +import Data.Either (Either(..)) + +hush :: Alternative m => Either e a -> m a +hush (Left _) = empty +hush (Right x) = pure x + +note :: (MonadError e m) => e -> Maybe a -> m a +note err = maybe (throwError err) pure + +tryIO :: MonadIO m => IO a -> ExceptT IOException m a +tryIO = ExceptT . liftIO . Exception.try diff --git a/src/Protolude.hs b/src/Protolude.hs index f2bd9d2e3..32b8d786f 100644 --- a/src/Protolude.hs +++ b/src/Protolude.hs @@ -35,6 +35,7 @@ import Either as X import Applicative as X import Conv as X import Panic as X +import Exceptions as X import Base as Base hiding ( putStr -- Overriden by Show.putStr