packages feed

control-monad-exception-monadstf (empty) → 0.8.0

raw patch · 3 files changed

+181/−0 lines, 3 filesdep +basedep +control-monad-exceptiondep +monads-tfsetup-changed

Dependencies added: base, control-monad-exception, monads-tf, transformers

Files

+ Setup.lhs view
@@ -0,0 +1,3 @@+#!/usr/bin/env runhaskell+> import Distribution.Simple+> main = defaultMain
+ control-monad-exception-monadstf.cabal view
@@ -0,0 +1,96 @@+name: control-monad-exception-monadstf+version: 0.8.0+Cabal-Version:  >= 1.6+build-type: Simple+license: PublicDomain+author: Pepe Iborra+maintainer: pepeiborra@gmail.com+homepage: http://pepeiborra.github.com/control-monad-exception+description: +  This package provides explicitly typed, checked exceptions as a library.+  .+  Computations throwing different types of exception can be combined seamlessly.+  .+  /Example/+  .+  > data Expr = Add Expr Expr | Div Expr Expr | Val Double+  > eval (Val x)     = return x+  > eval (Add a1 a2) = do+  >    v1 <- eval a1+  >    v2 <- eval a2+  >    let sum = v1 + v2+  >    if sum < v1 || sum < v2 then throw SumOverflow else return sum+  > eval (Div a1 a2) = do+  >    v1 <- eval a1+  >    v2 <- eval a2+  >    if v2 == 0 then throw DivideByZero else return (v1 / v2)+  .+  > data DivideByZero = DivideByZero deriving (Show, Typeable)+  > data SumOverflow  = SumOverflow  deriving (Show, Typeable)+  .+  > instance Exception DivideByZero+  > instance Exception SumOverflow+  .+  GHCi infers the following types+  .+  > eval :: (Throws DivideByZero l, Throws SumOverflow l) => Expr -> EM l Double+  > eval `catch` \ (e::DivideByZero) -> return (-1)  :: Throws SumOverflow l => Expr -> EM l Double+  > runEM(eval `catch` \ (e::SomeException) -> return (-1))  :: Expr -> Double+  .+  .+  In addition to explicitly typed exceptions his package provides:+  .+    * Support for explicitly documented, unchecked exceptions (via 'Control.Monad.Exception.tryEMT').+  .+    * Support for selective unchecked exceptions (via 'Control.Monad.Exception.UncaughtException').+  .+    * Support for exception call traces via 'Control.Monad.Loc.MonadLoc'. /Example:/+  .+  >+  > f () = do throw MyException+  > g a  = do f a+  >+  > main = runEMT $ do g () `catchWithSrcLoc`+  >                        \loc (e::MyException) -> lift(putStrLn$ showExceptionWithTrace loc e)+  >+  > -- Running main produces the output:+  >+  > *Main> main+  >  MyException+  >    in f, Main(example.hs): (1,6)+  >       g, Main(example.hs): (2,6)+  >       main, Main(example.hs): (5,9)+  >       main, Main(example.hs): (4,16)++synopsis: Explicitly typed, checked exceptions with stack traces+category: Control, Monads+stability: experimental+tested-with: GHC ==6.10.3++Library+  buildable: True +  build-depends: base > 4 && < 5+               , control-monad-exception == 0.8.0+               , transformers >= 0.1.0+               , monads-tf    >= 0.0.0.1++  extensions:  ScopedTypeVariables, +               PackageImports,+               MultiParamTypeClasses,+               TypeFamilies,+               FlexibleContexts,+               FlexibleInstances,+               UndecidableInstances++  exposed-modules:+     Control.Monad.Exception.MonadsTF++  hs-source-dirs: src-monadstf++  ghc-options: -Wall -fno-warn-name-shadowing -fno-warn-orphans++++source-repository head+  type:     git+  location: git://github.com/pepeiborra/control-monad-exception.git
+ src-monadstf/Control/Monad/Exception/MonadsTF.hs view
@@ -0,0 +1,82 @@+{-# LANGUAGE ScopedTypeVariables #-}+{-# LANGUAGE PackageImports #-}+{-# LANGUAGE MultiParamTypeClasses #-}+{-# LANGUAGE TypeFamilies #-}+{-# LANGUAGE FlexibleInstances #-}+{-# LANGUAGE UndecidableInstances #-}++-- | 'EMT' liftings for the classes in the monads-fd package+module Control.Monad.Exception.MonadsTF (module Control.Monad.Exception, Control.Monad.Exception.catch+                                        ) where++import qualified Control.Exception as CE++import qualified Control.Monad.Exception+import Control.Monad.Exception hiding (catch, Error)+import Control.Monad.Exception.Catch+import Control.Monad.Exception.Throws+import "monads-tf" Control.Monad.Cont.Class+import "monads-tf" Control.Monad.RWS.Class++import Control.Monad+import "transformers" Control.Monad.Trans++import Control.Monad.Trans.Error+import Control.Monad.Trans.List+import Control.Monad.Trans.Reader (ReaderT(..))+import Control.Monad.Trans.State (StateT(..))+import Control.Monad.Trans.Writer (WriterT(..))+import Control.Monad.Trans.RWS (RWST(..))++import Data.Monoid+import Prelude hiding (catch)++instance MonadTrans (EMT l) where lift = EMT . liftM Right++instance (Throws SomeException l, MonadIO m) => MonadIO (EMT l m) where+  liftIO m = EMT (liftIO m') where+      m' = liftM Right m+            `CE.catch`+           \(e::SomeException) -> return (Left ([], CheckedException e))++instance MonadCont m => MonadCont (EMT l m) where+  callCC f = EMT $ callCC $ \c -> unEMT (f (\a -> EMT $ c (Right a)))++instance MonadReader m => MonadReader (EMT l m) where+  type EnvType (EMT l m) = EnvType m+  ask = lift ask+  local f m = EMT (local f (unEMT m))++instance MonadState m => MonadState (EMT l m) where+  type StateType (EMT l m) = StateType m+  get = lift get+  put = lift . put++instance (MonadWriter m) => MonadWriter (EMT l m) where+  type WriterType (EMT l m) = WriterType m+  tell   = lift . tell+  listen m = EMT $ do+               (res, w) <- listen (unEMT m)+               return (fmap (\x -> (x,w)) res)+  pass m   = EMT $ pass $ do+               a <- unEMT m+               case a of+                 Left  l     -> return (Left l, id)+                 Right (r,f) -> return (Right r, f)++instance (MonadRWS m) => MonadRWS (EMT l m)+++-- MonadCatch Instances+-- -------------------------------------------------------------------------+instance (Error e) => MonadCatch e (Either e) (Either e) where catch m h = either h Right m+instance (Error e, Monad m) => MonadCatch e (ErrorT e m) (ErrorT e m) where catch = catchError++instance MonadCatch e m m' => MonadCatch e (ListT m) (ListT m') where catch (ListT m) h = ListT (catch m (runListT . h))+instance MonadCatch e m m' => MonadCatch e (ReaderT r m) (ReaderT r m') where catch (ReaderT m) h = ReaderT (\s -> catch (m s) ((`runReaderT` s) . h))++instance (Monoid w, MonadCatch e m m') => MonadCatch e (WriterT w m) (WriterT w m') where catch (WriterT m) h = WriterT (catch m (runWriterT . h))++instance MonadCatch e m m' => MonadCatch e (StateT s m) (StateT s m') where catch (StateT m) h = StateT (\s -> catch (m s) ((`runStateT` s) . h))++instance (Monoid w, MonadCatch e m m') => MonadCatch e (RWST r w s m) (RWST r w s m') where catch (RWST m) h = RWST (\r s -> catch (m r s) ((\m -> runRWST m r s) . h))