catchE Moduleerrors-2.3.0Haskell98
Control.Error
Import this module in your code to access the entire library's functionality:
import Control.ErrorThis module exports the entire library as well as useful exports from other standard error-handling libraries:
Control.Error.Safe: Generalizes the
safelibrary, including Either,EitherT, andMonadPlusvariations on total functionsControl.Error.Script: Support for simple scripts that catch all errors and transform them to
TextControl.Error.Util: Utility functions and conversions between common error-handling types
Control.Monad.Trans.Except: The ExceptT monad transformerControl.Monad.Trans.Maybe: The MaybeT monad transformerData.Either: Either utility functionsData.EitherR: throw and catch functions, and their corresponding "success" monads
Data.Maybe: Maybe utility functionsSafe: Total versions of partial Prelude functions
This module does not re-export partial functions from other libraries.
- 2 types
- 47 values
- Packageerrors-2.3.0
- Exports49
- LanguageHaskell98
- LicenceBSD-3-Clause
- SourceError.hs
Re-exports
49 declarationsmodule Control.Error.Safe
module Control.Error.Script
module Control.Error.Util
Map the unwrapped computation using the given function.
runExceptT (mapExceptT f m) = f (runExceptT m)
The inverse of ExceptT.
Transform any exceptions thrown by the computation using the given function.
A monad transformer that adds exceptions to other monads.
ExceptT constructs a monad parameterized over two things:
e - An arbitrary exception type.
m - The inner monad.
The monadic computations are a plain values. They are unrelated to the Control.Exception mechanism, which is tied to the IO monad.
The return function yields a computation that produces the given
value, while >>= sequences two subcomputations, exiting on the
first exception.
Instances36MonadRWS, Generic1, MonadAccum, MonadError, MonadReader, MonadState, …
MonadRWS r w s m => MonadRWS r w s (ExceptT e m)Defined in mtl-2.3.1 · Control.Monad.RWS.ClassFunctor m => Generic1 (ExceptT e m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.ExceptMonadAccum w m => MonadAccum w (ExceptT e m)Defined in mtl-2.3.1 · Control.Monad.AccumThe accumulated value 'survives' an exception: even if the computation fails to deliver a result, we still have an accumulated value.
Monad m => MonadError e (ExceptT e m)Defined in mtl-2.3.1 · Control.Monad.Error.ClassMonadReader r m => MonadReader r (ExceptT e m)Defined in mtl-2.3.1 · Control.Monad.Reader.ClassMonadState s m => MonadState s (ExceptT e m)Defined in mtl-2.3.1 · Control.Monad.State.ClassMonadWriter w m => MonadWriter w (ExceptT e m)Defined in mtl-2.3.1 · Control.Monad.Writer.ClassMonadSelect r m => MonadSelect r (ExceptT e m)Defined in mtl-2.3.1 · Control.Monad.SelectMonadTrans (ExceptT e)Defined in transformers-0.6.1.2 · Control.Monad.Trans.ExceptMonad m => Monad (ExceptT e m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.ExceptFunctor m => Functor (ExceptT e m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.ExceptMonadFix m => MonadFix (ExceptT e m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.ExceptMonadFail m => MonadFail (ExceptT e m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.Except(Functor m, Monad m) => Applicative (ExceptT e m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.ExceptFoldable f => Foldable (ExceptT e f)Defined in transformers-0.6.1.2 · Control.Monad.Trans.ExceptTraversable f => Traversable (ExceptT e f)Defined in transformers-0.6.1.2 · Control.Monad.Trans.Except(Functor m, Monad m, Monoid e) => Alternative (ExceptT e m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.Except(Monad m, Monoid e) => MonadPlus (ExceptT e m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.ExceptMonadIO m => MonadIO (ExceptT e m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.ExceptMonadZip m => MonadZip (ExceptT e m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.Except(Eq e, Eq1 m) => Eq1 (ExceptT e m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.Except(Ord e, Ord1 m) => Ord1 (ExceptT e m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.Except(Read e, Read1 m) => Read1 (ExceptT e m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.Except(Show e, Show1 m) => Show1 (ExceptT e m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.ExceptContravariant m => Contravariant (ExceptT e m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.ExceptMonadCont m => MonadCont (ExceptT e m)Defined in mtl-2.3.1 · Control.Monad.Cont.ClassMonadCatch m => MonadCatch (ExceptT e m)Defined in exceptions-0.10.11 · Control.Monad.CatchCatches exceptions from the base monad.
MonadMask m => MonadMask (ExceptT e m)Defined in exceptions-0.10.11 · Control.Monad.CatchMonadThrow m => MonadThrow (ExceptT e m)Defined in exceptions-0.10.11 · Control.Monad.CatchThrows exceptions into the base monad.
(Eq e, Eq1 m, Eq a) => Eq (ExceptT e m a)Defined in transformers-0.6.1.2 · Control.Monad.Trans.Except(Ord e, Ord1 m, Ord a) => Ord (ExceptT e m a)Defined in transformers-0.6.1.2 · Control.Monad.Trans.Except(Read e, Read1 m, Read a) => Read (ExceptT e m a)Defined in transformers-0.6.1.2 · Control.Monad.Trans.Except(Show e, Show1 m, Show a) => Show (ExceptT e m a)Defined in transformers-0.6.1.2 · Control.Monad.Trans.ExceptGeneric (ExceptT e m a)Defined in transformers-0.6.1.2 · Control.Monad.Trans.Excepttype Rep (ExceptT e m a) = D1 ('MetaDataDefined in transformers-0.6.1.2 · Control.Monad.Trans.Except"ExceptT"
"Control.Monad.Trans.Except"
"transformers-0.6.1.2-8201"
'True) (C1 ('MetaCons"ExceptT"
'PrefixI 'False) (S1 ('MetaSel 'Nothing 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (m (Either e a)))))type Rep1 (ExceptT e m) = D1 ('MetaDataDefined in transformers-0.6.1.2 · Control.Monad.Trans.Except"ExceptT"
"Control.Monad.Trans.Except"
"transformers-0.6.1.2-8201"
'True) (C1 ('MetaCons"ExceptT"
'PrefixI 'False) (S1 ('MetaSel 'Nothing 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (m :.: Rec1 (Either e))))
Lift a callCC operation to the new monad.
Lift a catchE operation to the new monad.
Lift a listen operation to the new monad.
Lift a pass operation to the new monad.
Instances36MonadTrans, MonadRWS, Generic1, MonadAccum, MonadError, MonadReader, …
MonadTrans MaybeTDefined in transformers-0.6.1.2 · Control.Monad.Trans.MaybeMonadRWS r w s m => MonadRWS r w s (MaybeT m)Defined in mtl-2.3.1 · Control.Monad.RWS.ClassFunctor m => Generic1 (MaybeT m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.MaybeMonadAccum w m => MonadAccum w (MaybeT m)Defined in mtl-2.3.1 · Control.Monad.AccumThe accumulated value 'survives' an error: even if the computation fails to deliver a result, we still have an accumulated value.
MonadError e m => MonadError e (MaybeT m)Defined in mtl-2.3.1 · Control.Monad.Error.ClassMonadReader r m => MonadReader r (MaybeT m)Defined in mtl-2.3.1 · Control.Monad.Reader.ClassMonadState s m => MonadState s (MaybeT m)Defined in mtl-2.3.1 · Control.Monad.State.ClassMonadWriter w m => MonadWriter w (MaybeT m)Defined in mtl-2.3.1 · Control.Monad.Writer.ClassMonadSelect r m => MonadSelect r (MaybeT m)Defined in mtl-2.3.1 · Control.Monad.SelectMonad m => Monad (MaybeT m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.MaybeFunctor m => Functor (MaybeT m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.MaybeMonadFix m => MonadFix (MaybeT m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.MaybeMonad m => MonadFail (MaybeT m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.Maybe(Functor m, Monad m) => Applicative (MaybeT m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.MaybeFoldable f => Foldable (MaybeT f)Defined in transformers-0.6.1.2 · Control.Monad.Trans.MaybeTraversable f => Traversable (MaybeT f)Defined in transformers-0.6.1.2 · Control.Monad.Trans.Maybe(Functor m, Monad m) => Alternative (MaybeT m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.MaybeMonad m => MonadPlus (MaybeT m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.MaybeMonadIO m => MonadIO (MaybeT m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.MaybeMonadZip m => MonadZip (MaybeT m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.MaybeEq1 m => Eq1 (MaybeT m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.MaybeOrd1 m => Ord1 (MaybeT m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.MaybeRead1 m => Read1 (MaybeT m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.MaybeShow1 m => Show1 (MaybeT m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.MaybeContravariant m => Contravariant (MaybeT m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.MaybeMonadCont m => MonadCont (MaybeT m)Defined in mtl-2.3.1 · Control.Monad.Cont.ClassMonadCatch m => MonadCatch (MaybeT m)Defined in exceptions-0.10.11 · Control.Monad.CatchCatches exceptions from the base monad.
MonadMask m => MonadMask (MaybeT m)Defined in exceptions-0.10.11 · Control.Monad.CatchMonadThrow m => MonadThrow (MaybeT m)Defined in exceptions-0.10.11 · Control.Monad.CatchThrows exceptions into the base monad.
(Eq1 m, Eq a) => Eq (MaybeT m a)Defined in transformers-0.6.1.2 · Control.Monad.Trans.Maybe(Ord1 m, Ord a) => Ord (MaybeT m a)Defined in transformers-0.6.1.2 · Control.Monad.Trans.Maybe(Read1 m, Read a) => Read (MaybeT m a)Defined in transformers-0.6.1.2 · Control.Monad.Trans.Maybe(Show1 m, Show a) => Show (MaybeT m a)Defined in transformers-0.6.1.2 · Control.Monad.Trans.MaybeGeneric (MaybeT m a)Defined in transformers-0.6.1.2 · Control.Monad.Trans.Maybetype Rep (MaybeT m a) = D1 ('MetaDataDefined in transformers-0.6.1.2 · Control.Monad.Trans.Maybe"MaybeT"
"Control.Monad.Trans.Maybe"
"transformers-0.6.1.2-8201"
'True) (C1 ('MetaCons"MaybeT"
'PrefixI 'True) (S1 ('MetaSel ('Just"runMaybeT"
) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (m (Maybe a)))))type Rep1 (MaybeT m) = D1 ('MetaDataDefined in transformers-0.6.1.2 · Control.Monad.Trans.Maybe"MaybeT"
"Control.Monad.Trans.Maybe"
"transformers-0.6.1.2-8201"
'True) (C1 ('MetaCons"MaybeT"
'PrefixI 'True) (S1 ('MetaSel ('Just"runMaybeT"
) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (m :.: Rec1 Maybe)))
Case analysis for the Either type.
If the value is Left a, apply the first function to a;
if it is Right b, apply the second function to b.
Examples
We create two values of type Either String Int, one using the
Left constructor and another using the Right constructor. Then
we apply "either" the Prelude.length function (if we have a String)
or the "times-two" function (if we have an Int):
let s = Left "foo" :: Either String Intlet n = Right 3 :: Either String Inteither length (*2) s3either length (*2) n6
Partitions a list of Either into two lists. All the Left elements are extracted, in order, to the first component of the output. Similarly the Right elements are extracted to the second component of the output.
Examples
Basic usage:
let list = [ Left "foo", Right 3, Left "bar", Right 7, Left "baz" ]partitionEithers list(["foo","bar","baz"],[3,7])
The pair returned by partitionEithers x should be the same
pair as (lefts x, rights x):
let list = [ Left "foo", Right 3, Left "bar", Right 7, Left "baz" ]partitionEithers list == (lefts list, rights list)True
module Data.EitherR
The catMaybes function takes a list of Maybes and returns a list of all the Just values.
Examples
Basic usage:
catMaybes [Just 1, Nothing, Just 3][1,3]
When constructing a list of Maybe values, catMaybes can be used to return all of the "success" results (if the list is the result of a map, then mapMaybe would be more appropriate):
import GHC.Internal.Text.Read ( readMaybe )[readMaybe x :: Maybe Int | x <- ["1", "Foo", "3"] ][Just 1,Nothing,Just 3]catMaybes $ [readMaybe x :: Maybe Int | x <- ["1", "Foo", "3"] ][1,3]
The fromMaybe function takes a default value and a Maybe value. If the Maybe is Nothing, it returns the default value; otherwise, it returns the value contained in the Maybe.
Examples
Basic usage:
fromMaybe "" (Just "Hello, World!")"Hello, World!"
fromMaybe "" Nothing""
Read an integer from a string using readMaybe. If we fail to
parse an integer, we want to return 0 by default:
import GHC.Internal.Text.Read ( readMaybe )fromMaybe 0 (readMaybe "5")5fromMaybe 0 (readMaybe "")0
isJust x returns True when x is of the form Just _ and False
otherwise.
Examples
Basic usage:
isJust (Just 3)True
isJust (Just ())True
isJust NothingFalse
Only the outer constructor is taken into consideration:
isJust (Just Nothing)True
isNothing x returns True when x is Nothing and False otherwise.
Examples
Basic usage:
isNothing (Just 3)False
isNothing (Just ())False
isNothing NothingTrue
Only the outer constructor is taken into consideration:
isNothing (Just Nothing)False
The listToMaybe function returns Nothing on an empty list
or Just a where a is the first element of the list.
Examples
Basic usage:
listToMaybe []Nothing
listToMaybe [9]Just 9
listToMaybe [1,2,3]Just 1
Composing maybeToList with listToMaybe should be the identity on singleton/empty lists:
maybeToList $ listToMaybe [5][5]maybeToList $ listToMaybe [][]
But not on lists with more than one element:
maybeToList $ listToMaybe [1,2,3][1]
The mapMaybe function is a version of map which can throw
out elements. In particular, the functional argument returns
something of type Maybe b. If this is Nothing, no element
is added on to the result list. If it is Just b, then b is
included in the result list.
Examples
Using mapMaybe f x is a shortcut for catMaybes $ map f x
in most cases:
import GHC.Internal.Text.Read ( readMaybe )let readMaybeInt = readMaybe :: String -> Maybe IntmapMaybe readMaybeInt ["1", "Foo", "3"][1,3]catMaybes $ map readMaybeInt ["1", "Foo", "3"][1,3]
If we map the Just constructor, the entire list should be returned:
mapMaybe Just [1,2,3][1,2,3]
The maybe function takes a default value, a function, and a Maybe value. If the Maybe value is Nothing, the function returns the default value. Otherwise, it applies the function to the value inside the Just and returns the result.
Examples
Basic usage:
maybe False odd (Just 3)True
maybe False odd NothingFalse
Read an integer from a string using readMaybe. If we succeed,
return twice the integer; that is, apply (*2) to it. If instead
we fail to parse an integer, return 0 by default:
import GHC.Internal.Text.Read ( readMaybe )maybe 0 (*2) (readMaybe "5")10maybe 0 (*2) (readMaybe "")0
Apply show to a Maybe Int. If we have Just n, we want to show
the underlying Int n. But if we have Nothing, we return the
empty string instead of (for example) "Nothing":
maybe "" show (Just 5)"5"maybe "" show Nothing""
The maybeToList function returns an empty list when given Nothing or a singleton list when given Just.
Examples
Basic usage:
maybeToList (Just 7)[7]
maybeToList Nothing[]
One can use maybeToList to avoid pattern matching when combined with a function that (safely) works on lists:
import GHC.Internal.Text.Read ( readMaybe )sum $ maybeToList (readMaybe "3")3sum $ maybeToList (readMaybe "")0
Deprecated. Use foldl1May' instead.
Deprecated. Use foldr1May instead.
New users are recommended to use minimumBound or maximumBound instead.
New users are recommended to use minimumBound or maximumBound instead.
tailDef [12] [] = [12]
tailDef [12] [1,3,4] = [3,4]tailMay [] = Nothing
tailMay [1,3,4] = Just [3,4]tailSafe [] = []
tailSafe [1,3,4] = [3,4]