safe-failure 0.2 → 0.4.0
raw patch · 4 files changed
+41/−73 lines, 4 filesdep +failuredep −control-monad-exceptiondep −control-monad-failuredep −transformerssetup-changednew-uploader
Dependencies added: failure
Dependencies removed: control-monad-exception, control-monad-failure, transformers
Files
- Safe/Failure.hs +28/−59
- Setup.hs +0/−2
- Setup.lhs +7/−0
- safe-failure.cabal +6/−12
Safe/Failure.hs view
@@ -2,7 +2,6 @@ {-# LANGUAGE ExistentialQuantification #-} {-# LANGUAGE FlexibleContexts, FlexibleInstances #-} {-# LANGUAGE DeriveDataTypeable #-}-{-# LANGUAGE CPP #-} {- | Module : Safe.Fail Copyright : (c) Neil Mitchell 2007-2008, Jose Iborra 2009@@ -14,7 +13,7 @@ A library for safe functions, based on standard functions that may crash. -This module reexports versions which produce exceptions in an arbitrary 'MonadFailure' monad.+This module reexports versions which produce exceptions in an arbitrary 'Failure'. -} @@ -33,8 +32,6 @@ def, note, -- * Assertions Safe.Failure.assert,--- * IO functions-Safe.Failure.readFile, -- * Exceptions SafeException(..), HeadFailure(..), TailFailure(..), InitFailure(..), LastFailure(..),@@ -46,14 +43,10 @@ ) where import Control.Exception-import Control.Monad.Failure+import Control.Failure import Data.Maybe import Data.Typeable-import Control.Monad.Trans-#ifdef CME-import Control.Monad.Exception.Throws-#endif-import Control.Monad (liftM)+import Control.Applicative (pure) {-| @def@, use it to return a default value in the event of an error. @@ -84,8 +77,13 @@ safeExceptionFromException :: Exception e => SomeException -> Maybe e safeExceptionFromException e = do { SafeException e' <- fromException e; cast e'} -liftFailure :: MonadFailure e m => (a -> Bool) -> e -> (a -> b) -> a -> m b-liftFailure test e f val = if test val then failure e else return (f val)+liftFailure :: ApplicativeFailure e m+ => (a -> Bool)+ -> e+ -> (a -> b)+ -> a+ -> m b+liftFailure test e f val = if test val then failure e else pure (f val) data TailFailure = TailFailure deriving (Show,Typeable)@@ -93,7 +91,7 @@ fromException = safeExceptionFromException toException = safeExceptionToException -tail :: MonadFailure TailFailure m => [a] -> m [a]+tail :: ApplicativeFailure TailFailure m => [a] -> m [a] tail = liftFailure null TailFailure Prelude.tail @@ -102,7 +100,7 @@ fromException = safeExceptionFromException toException = safeExceptionToException -init :: MonadFailure InitFailure m => [a] -> m [a]+init :: ApplicativeFailure InitFailure m => [a] -> m [a] init = liftFailure null InitFailure Prelude.init @@ -111,7 +109,7 @@ fromException = safeExceptionFromException toException = safeExceptionToException -head :: MonadFailure HeadFailure m => [a] -> m a+head :: ApplicativeFailure HeadFailure m => [a] -> m a head = liftFailure null HeadFailure Prelude.head @@ -120,7 +118,7 @@ fromException = safeExceptionFromException toException = safeExceptionToException -last :: MonadFailure LastFailure m => [a] -> m a+last :: ApplicativeFailure LastFailure m => [a] -> m a last = liftFailure null LastFailure Prelude.last @@ -129,7 +127,7 @@ fromException = safeExceptionFromException toException = safeExceptionToException -minimum :: (Ord a, MonadFailure MinimumFailure m) => [a] -> m a+minimum :: (Ord a, ApplicativeFailure MinimumFailure m) => [a] -> m a minimum = liftFailure null MinimumFailure Prelude.minimum @@ -138,7 +136,7 @@ fromException = safeExceptionFromException toException = safeExceptionToException -maximum :: (Ord a, MonadFailure MaximumFailure m) => [a] -> m a+maximum :: (Ord a, ApplicativeFailure MaximumFailure m) => [a] -> m a maximum = liftFailure null MaximumFailure Prelude.maximum @@ -147,7 +145,7 @@ fromException = safeExceptionFromException toException = safeExceptionToException -foldr1 :: MonadFailure Foldr1Failure m => (a -> a -> a) -> [a] -> m a+foldr1 :: ApplicativeFailure Foldr1Failure m => (a -> a -> a) -> [a] -> m a foldr1 f = liftFailure null Foldr1Failure (Prelude.foldr1 f) @@ -156,7 +154,7 @@ fromException = safeExceptionFromException toException = safeExceptionToException -foldl1 :: MonadFailure Foldl1Failure m => (a -> a -> a) -> [a] -> m a+foldl1 :: ApplicativeFailure Foldl1Failure m => (a -> a -> a) -> [a] -> m a foldl1 f = liftFailure null Foldl1Failure (Prelude.foldl1 f) @@ -165,7 +163,7 @@ fromException = safeExceptionFromException toException = safeExceptionToException -fromJust :: MonadFailure FromJustFailure m => Maybe a -> m a+fromJust :: ApplicativeFailure FromJustFailure m => Maybe a -> m a fromJust = liftFailure isNothing FromJustFailure Data.Maybe.fromJust @@ -174,13 +172,13 @@ fromException = safeExceptionFromException toException = safeExceptionToException -at :: MonadFailure IndexFailure m => [a] -> Int -> m a+at :: ApplicativeFailure IndexFailure m => [a] -> Int -> m a at xs n | n < 0 = failure (IndexFailure n) | otherwise = go xs n where go [] _ = failure (IndexFailure n)- go (x:_) 0 = return x+ go (x:_) 0 = pure x go (_:xx) i = go xx (i-1) @@ -189,9 +187,9 @@ fromException = safeExceptionFromException toException = safeExceptionToException -read :: (Read a, MonadFailure ReadFailure m) => String -> m a+read :: (Read a, ApplicativeFailure ReadFailure m) => String -> m a read s = case [x | (x,t) <- reads s, ("","") <- lex t] of- [x] -> return x+ [x] -> pure x _ -> failure (ReadFailure s) data LookupFailure a = LookupFailure a deriving (Show,Typeable)@@ -201,44 +199,15 @@ -- | -- > lookupJust key = fromJust . lookup key-lookup :: (Eq a, MonadFailure (LookupFailure a) m) => a -> [(a,b)] -> m b-lookup key = maybe (failure (LookupFailure key)) return . Prelude.lookup key+lookup :: (Eq a, ApplicativeFailure (LookupFailure a) m)+ => a -> [(a,b)] -> m b+lookup key = maybe (failure (LookupFailure key)) pure . Prelude.lookup key -- | Assert a value to be true. If true, returns the first value as a succss. -- Otherwise, returns the second value as a failure.-assert :: (MonadFailure e m, Exception e)+assert :: (ApplicativeFailure e m, Exception e) => Bool -> v -> e -> m v-assert b v e = if b then return v else failure e---- | The standard readFile function with any 'IOException's returned as a--- failure instead of a runtime exception.-readFile :: (MonadFailure IOException m, MonadIO m) => FilePath -> m String-readFile fp = do- contents <- liftIO $ handle- (\e -> return $ Left (e :: IOException))- (liftM Right $ Prelude.readFile fp)- case contents of- Left e -> failure e- Right v -> return v--#ifdef CME---- Encoding the exception hierarchy in Throws--instance Throws TailFailure (Caught SafeException l)-instance Throws HeadFailure (Caught SafeException l)-instance Throws InitFailure (Caught SafeException l)-instance Throws LastFailure (Caught SafeException l)-instance Throws MinimumFailure (Caught SafeException l)-instance Throws MaximumFailure (Caught SafeException l)-instance Throws Foldr1Failure (Caught SafeException l)-instance Throws Foldl1Failure (Caught SafeException l)-instance Throws FromJustFailure (Caught SafeException l)-instance Throws IndexFailure (Caught SafeException l)-instance Throws ReadFailure (Caught SafeException l)-instance (Typeable a, Show a) => Throws (LookupFailure a) (Caught SafeException l)--#endif+assert b v e = if b then pure v else failure e
− Setup.hs
@@ -1,2 +0,0 @@-import Distribution.Simple-main = defaultMain
+ Setup.lhs view
@@ -0,0 +1,7 @@+#!/usr/bin/env runhaskell++> module Main where+> import Distribution.Simple++> main :: IO ()+> main = defaultMain
safe-failure.cabal view
@@ -1,32 +1,26 @@ Name: safe-failure Build-Type: Simple Cabal-Version: >= 1.2-Version: 0.2+Version: 0.4.0 License: BSD3 License-File: LICENSE Copyright: 2007-8, Neil Mitchell Maintainer: pepeiborra@gmail.com Author: Neil Mitchell, Jose Iborra (2009), Michael Snoyman (2009) Homepage: http://www-users.cs.york.ac.uk/~ndm/safe/-Category: Unclassified-Synopsis: Partial functions from the prelude with a MonadFailure interface+Category: Failure+Synopsis: Library for safe functions Description: Partial functions from the base library, such as @head@ and @!!@, modified- to fail in a @MonadFailure@ monad.+ to fail in a @Failure@. These functions can be used to reduce the number of unsafe pattern matches in your code. -Flag cme- description: build with special support for control-monad-exception- default: True- Library buildable: True- build-depends: base >= 4 && < 5, control-monad-failure, transformers+ build-depends: base >= 4 && < 5,+ failure >= 0.0.0 && < 0.1 exposed-modules: Safe.Failure ghc-options: -Wall- if flag(cme)- build-depends: control-monad-exception- cpp-options: -DCME