module Control.Error.Safe (
assertMay,
rightMay,
tailErr,
initErr,
headErr,
lastErr,
minimumErr,
maximumErr,
foldr1Err,
foldl1Err,
foldl1Err',
atErr,
readErr,
assertErr,
justErr,
tryTail,
tryInit,
tryHead,
tryLast,
tryMinimum,
tryMaximum,
tryFoldr1,
tryFoldl1,
tryFoldl1',
tryAt,
tryRead,
tryAssert,
tryJust,
tryRight,
tailZ,
initZ,
headZ,
lastZ,
minimumZ,
maximumZ,
foldr1Z,
foldl1Z,
foldl1Z',
atZ,
readZ,
assertZ,
justZ,
rightZ
) where
import Control.Error.Util (note, hoistEither)
import Control.Monad (MonadPlus(mzero))
import Control.Monad.Trans.Except (ExceptT)
import qualified Safe as S
assertMay :: Bool -> Maybe ()
assertMay = assertZ
rightMay :: Either e a -> Maybe a
rightMay = rightZ
tailErr :: e -> [a] -> Either e [a]
tailErr e = note e . S.tailMay
initErr :: e -> [a] -> Either e [a]
initErr e = note e . S.initMay
headErr :: e -> [a] -> Either e a
headErr e = note e . S.headMay
lastErr :: e -> [a] -> Either e a
lastErr e = note e . S.lastMay
minimumErr :: (Ord a) => e -> [a] -> Either e a
minimumErr e = note e . S.minimumMay
maximumErr :: (Ord a) => e -> [a] -> Either e a
maximumErr e = note e . S.maximumMay
foldr1Err :: e -> (a -> a -> a) -> [a] -> Either e a
foldr1Err e step xs = note e $ S.foldr1May step xs
foldl1Err :: e -> (a -> a -> a) -> [a] -> Either e a
foldl1Err e step xs = note e $ S.foldl1May step xs
foldl1Err' :: e -> (a -> a -> a) -> [a] -> Either e a
foldl1Err' e step xs = note e $ S.foldl1May' step xs
atErr :: e -> [a] -> Int -> Either e a
atErr e xs n = note e $ S.atMay xs n
readErr :: (Read a) => e -> String -> Either e a
readErr e = note e . S.readMay
assertErr :: e -> Bool -> Either e ()
assertErr e p = if p then Right () else Left e
justErr :: e -> Maybe a -> Either e a
justErr e = maybe (Left e) Right
tryTail :: (Monad m) => e -> [a] -> ExceptT e m [a]
tryTail e xs = hoistEither $ tailErr e xs
tryInit :: (Monad m) => e -> [a] -> ExceptT e m [a]
tryInit e xs = hoistEither $ initErr e xs
tryHead :: (Monad m) => e -> [a] -> ExceptT e m a
tryHead e xs = hoistEither $ headErr e xs
tryLast :: (Monad m) => e -> [a] -> ExceptT e m a
tryLast e xs = hoistEither $ lastErr e xs
tryMinimum :: (Monad m, Ord a) => e -> [a] -> ExceptT e m a
tryMinimum e xs = hoistEither $ maximumErr e xs
tryMaximum :: (Monad m, Ord a) => e -> [a] -> ExceptT e m a
tryMaximum e xs = hoistEither $ maximumErr e xs
tryFoldr1 :: (Monad m) => e -> (a -> a -> a) -> [a] -> ExceptT e m a
tryFoldr1 e step xs = hoistEither $ foldr1Err e step xs
tryFoldl1 :: (Monad m) => e -> (a -> a -> a) -> [a] -> ExceptT e m a
tryFoldl1 e step xs = hoistEither $ foldl1Err e step xs
tryFoldl1' :: (Monad m) => e -> (a -> a -> a) -> [a] -> ExceptT e m a
tryFoldl1' e step xs = hoistEither $ foldl1Err' e step xs
tryAt :: (Monad m) => e -> [a] -> Int -> ExceptT e m a
tryAt e xs n = hoistEither $ atErr e xs n
tryRead :: (Monad m, Read a) => e -> String -> ExceptT e m a
tryRead e str = hoistEither $ readErr e str
tryAssert :: (Monad m) => e -> Bool -> ExceptT e m ()
tryAssert e p = hoistEither $ assertErr e p
tryJust :: (Monad m) => e -> Maybe a -> ExceptT e m a
tryJust e m = hoistEither $ justErr e m
tryRight :: (Monad m) => Either e a -> ExceptT e m a
tryRight = hoistEither
tailZ :: (MonadPlus m) => [a] -> m [a]
tailZ = maybe mzero return . S.tailMay
initZ :: (MonadPlus m) => [a] -> m [a]
initZ = maybe mzero return . S.initMay
headZ :: (MonadPlus m) => [a] -> m a
headZ = maybe mzero return . S.headMay
lastZ :: (MonadPlus m) => [a] -> m a
lastZ = maybe mzero return . S.lastMay
minimumZ :: (MonadPlus m) => (Ord a) => [a] -> m a
minimumZ = maybe mzero return . S.minimumMay
maximumZ :: (MonadPlus m) => (Ord a) => [a] -> m a
maximumZ = maybe mzero return . S.maximumMay
foldr1Z :: (MonadPlus m) => (a -> a -> a) -> [a] -> m a
foldr1Z step xs = maybe mzero return $ S.foldr1May step xs
foldl1Z :: (MonadPlus m) => (a -> a -> a) -> [a] -> m a
foldl1Z step xs = maybe mzero return $ S.foldl1May step xs
foldl1Z' :: (MonadPlus m) => (a -> a -> a) -> [a] -> m a
foldl1Z' step xs = maybe mzero return $ S.foldl1May' step xs
atZ :: (MonadPlus m) => [a] -> Int -> m a
atZ xs n = maybe mzero return $ S.atMay xs n
readZ :: (MonadPlus m) => (Read a) => String -> m a
readZ = maybe mzero return . S.readMay
assertZ :: (MonadPlus m) => Bool -> m ()
assertZ p = if p then return () else mzero
justZ :: (MonadPlus m) => Maybe a -> m a
justZ = maybe mzero return
rightZ :: (MonadPlus m) => Either e a -> m a
rightZ = either (const mzero) return