failure 0.1.1 → 0.1.2
raw patch · 3 files changed
+132/−128 lines, 3 filesdep ~basesetup-changedPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
Dependency ranges changed: base
API changes (from Hackage documentation)
- Control.Failure: instance (MonadTrans t, Failure e m, Monad (t m)) => Failure e (t m)
- Control.Failure: class Try f where { type family Error f; }
+ Control.Failure: class Try f where type family Error f
Files
- Control/Failure.hs +95/−96
- Setup.lhs +7/−7
- failure.cabal +30/−25
Control/Failure.hs view
@@ -1,96 +1,95 @@-{-# LANGUAGE Rank2Types #-}-{-# LANGUAGE TypeFamilies #-}-{-# LANGUAGE MultiParamTypeClasses #-}-{-# LANGUAGE FlexibleInstances #-}-{-# LANGUAGE UndecidableInstances #-}-{-# LANGUAGE DeriveDataTypeable #-}-{-# LANGUAGE FlexibleContexts #-}--- | Type classes for returning failures.-module Control.Failure- ( -- * Type class- Failure (..)- -- * Wrapping failures- , WrapFailure (..)- -- * Convenience 'String' failure- , StringException (..)- , failureString- -- * Convert 'Failure's into concrete types- , Try (..)- , NothingException (..)- , NullException (..)- ) where--import Prelude hiding (catch)-import Control.Exception (throwIO, catch, Exception, SomeException (..))-import Data.Typeable (Typeable)-import Control.Monad.Trans.Error ()-import Control.Monad.Trans.Class (MonadTrans (lift))--class Monad f => Failure e f where- failure :: e -> f v--class Failure e f => WrapFailure e f where- -- | Wrap the failure value, if any, with the given function. This is- -- useful in particular when you want all the exceptions returned from a- -- certain library to be of a certain type, even if they were generated by- -- a different library.- wrapFailure :: (forall eIn. Exception eIn => eIn -> e) -> f a -> f a-instance Exception e => WrapFailure e IO where- wrapFailure f m =- m `catch` \e@SomeException{} -> throwIO (f e)--class Try f where- type Error f- -- Turn a concrete failure into an abstract failure- try :: Failure (Error f) f' => f a -> f' a---- | Call 'failure' with a 'String'.-failureString :: Failure StringException m => String -> m a-failureString = failure . StringException--newtype StringException = StringException String- deriving Typeable-instance Show StringException where- show (StringException s) = "StringException: " ++ s-instance Exception StringException---- ----------------- base instances--- ----------------instance Failure e Maybe where failure _ = Nothing-instance Failure e [] where failure _ = []-instance Failure e (Either e) where failure = Left--instance Exception e => Failure e IO where- failure = throwIO---- | Instance for all monad transformers, simply lift the @failure@ into the--- base monad.-instance (MonadTrans t, Failure e m, Monad (t m)) => Failure e (t m) where- failure = lift . failure---- not a monad or applicative instance Failure e (Either e) where failure = Left--data NothingException = NothingException- deriving (Show, Typeable)-instance Exception NothingException--instance Try Maybe where- type Error Maybe = NothingException- try Nothing = failure NothingException- try (Just x) = return x--instance Try (Either e) where- type Error (Either e) = e- try (Left e) = failure e- try (Right x) = return x--data NullException = NullException- deriving (Show, Typeable)-instance Exception NullException--instance Try [] where- type Error [] = NullException- try [] = failure NullException- try (x:_) = return x+{-# LANGUAGE Rank2Types #-} +{-# LANGUAGE TypeFamilies #-} +{-# LANGUAGE MultiParamTypeClasses #-} +{-# LANGUAGE FlexibleInstances #-} +{-# LANGUAGE UndecidableInstances #-} +{-# LANGUAGE DeriveDataTypeable #-} +{-# LANGUAGE FlexibleContexts #-} +{-# LANGUAGE CPP #-} +-- | Type classes for returning failures. +module Control.Failure + ( -- * Type class + Failure (..) + -- * Wrapping failures + , WrapFailure (..) + -- * Convenience 'String' failure + , StringException (..) + , failureString + -- * Convert 'Failure's into concrete types + , Try (..) + , NothingException (..) + , NullException (..) + ) where + +import Prelude hiding (catch) +import Control.Exception (throw, catch, Exception, SomeException (..)) +import Data.Typeable (Typeable) +#if !(MIN_VERSION_base(4,3,0)) +import Control.Monad.Trans.Error () +#else +import Control.Monad.Instances () +#endif + +class Monad f => Failure e f where + failure :: e -> f v + +class Failure e f => WrapFailure e f where + -- | Wrap the failure value, if any, with the given function. This is + -- useful in particular when you want all the exceptions returned from a + -- certain library to be of a certain type, even if they were generated by + -- a different library. + wrapFailure :: (forall eIn. Exception eIn => eIn -> e) -> f a -> f a +instance Exception e => WrapFailure e IO where + wrapFailure f m = + m `catch` \e@SomeException{} -> throw (f e) + +class Try f where + type Error f + -- Turn a concrete failure into an abstract failure + try :: Failure (Error f) f' => f a -> f' a + +-- | Call 'failure' with a 'String'. +failureString :: Failure StringException m => String -> m a +failureString = failure . StringException + +newtype StringException = StringException String + deriving Typeable +instance Show StringException where + show (StringException s) = "StringException: " ++ s +instance Exception StringException + +-- -------------- +-- base instances +-- -------------- + +instance Failure e Maybe where failure _ = Nothing +instance Failure e [] where failure _ = [] +instance Failure e (Either e) where failure = Left + +instance Exception e => Failure e IO where + failure = Control.Exception.throw + +-- not a monad or applicative instance Failure e (Either e) where failure = Left + +data NothingException = NothingException + deriving (Show, Typeable) +instance Exception NothingException + +instance Try Maybe where + type Error Maybe = NothingException + try Nothing = failure NothingException + try (Just x) = return x + +instance Try (Either e) where + type Error (Either e) = e + try (Left e) = failure e + try (Right x) = return x + +data NullException = NullException + deriving (Show, Typeable) +instance Exception NullException + +instance Try [] where + type Error [] = NullException + try [] = failure NullException + try (x:_) = return x
Setup.lhs view
@@ -1,7 +1,7 @@-#!/usr/bin/env runhaskell--> module Main where-> import Distribution.Simple--> main :: IO ()-> main = defaultMain+#!/usr/bin/env runhaskell + +> module Main where +> import Distribution.Simple + +> main :: IO () +> main = defaultMain
failure.cabal view
@@ -1,25 +1,30 @@-name: failure-version: 0.1.1-Cabal-Version: >= 1.6-build-type: Simple-license: BSD3-author: Pepe Iborra, Michael Snoyman, Nicolas Pouillard-maintainer: pepeiborra@gmail.com-homepage: http://www.haskell.org/haskellwiki/Failure-description: A simple type class for success/failure computations.-synopsis: A simple type class for success/failure computations.-category: Control, Monads, Failure-stability: stable--Library- buildable: True- build-depends: base >= 4 && < 5- , transformers >= 0.2 && < 0.3- ghc-options: -Wall-- exposed-modules:- Control.Failure--source-repository head- type: git- location: git://github.com/snoyberg/failure.git+name: failure +version: 0.1.2 +Cabal-Version: >= 1.6 +build-type: Simple +license: BSD3 +author: Pepe Iborra, Michael Snoyman, Nicolas Pouillard +maintainer: pepeiborra@gmail.com +homepage: http://www.haskell.org/haskellwiki/Failure +description: A simple type class for success/failure computations. +synopsis: A simple type class for success/failure computations. +category: Control, Monads, Failure +stability: stable + +flag base43 + +Library + buildable: True + if flag(base43) + build-depends: base >= 4.3 && < 5 + else + build-depends: base >= 4 && < 4.3 + , transformers >= 0.2 && < 0.3 + ghc-options: -Wall + + exposed-modules: + Control.Failure + +source-repository head + type: git + location: git://github.com/snoyberg/failure.git