packages feed

general-allocate 0.1.1.0 → 0.2.0.0

raw patch · 5 files changed

+71/−65 lines, 5 filesPVP ok

version bump matches the API change (PVP)

API changes (from Hackage documentation)

- Control.Monad.With: EitherException :: e -> EitherException e
- Control.Monad.With: NothingException :: NothingException
- Control.Monad.With: class MonadWith m => MonadWithExceptable m
- Control.Monad.With: data NothingException
- Control.Monad.With: instance (Control.Monad.With.MonadWithExceptable m, GHC.Base.Monoid w) => Control.Monad.With.MonadWithExceptable (Control.Monad.Trans.RWS.Lazy.RWST r w s m)
- Control.Monad.With: instance (Control.Monad.With.MonadWithExceptable m, GHC.Base.Monoid w) => Control.Monad.With.MonadWithExceptable (Control.Monad.Trans.RWS.Strict.RWST r w s m)
- Control.Monad.With: instance (Control.Monad.With.MonadWithExceptable m, GHC.Base.Monoid w) => Control.Monad.With.MonadWithExceptable (Control.Monad.Trans.Writer.Lazy.WriterT w m)
- Control.Monad.With: instance (Control.Monad.With.MonadWithExceptable m, GHC.Base.Monoid w) => Control.Monad.With.MonadWithExceptable (Control.Monad.Trans.Writer.Strict.WriterT w m)
- Control.Monad.With: instance (GHC.Show.Show e, Data.Typeable.Internal.Typeable e) => Control.Monad.With.MonadWithExceptable (Data.Either.Either e)
- Control.Monad.With: instance (GHC.Show.Show e, Data.Typeable.Internal.Typeable e) => GHC.Exception.Type.Exception (Control.Monad.With.EitherException e)
- Control.Monad.With: instance (GHC.Show.Show e, Data.Typeable.Internal.Typeable e, Control.Monad.With.MonadWithExceptable m) => Control.Monad.With.MonadWithExceptable (Control.Monad.Trans.Except.ExceptT e m)
- Control.Monad.With: instance Control.Monad.With.MonadWithExceptable (GHC.ST.ST s)
- Control.Monad.With: instance Control.Monad.With.MonadWithExceptable Data.Functor.Identity.Identity
- Control.Monad.With: instance Control.Monad.With.MonadWithExceptable GHC.Types.IO
- Control.Monad.With: instance Control.Monad.With.MonadWithExceptable m => Control.Monad.With.MonadWithExceptable (Control.Monad.Trans.Identity.IdentityT m)
- Control.Monad.With: instance Control.Monad.With.MonadWithExceptable m => Control.Monad.With.MonadWithExceptable (Control.Monad.Trans.Maybe.MaybeT m)
- Control.Monad.With: instance Control.Monad.With.MonadWithExceptable m => Control.Monad.With.MonadWithExceptable (Control.Monad.Trans.Reader.ReaderT r m)
- Control.Monad.With: instance Control.Monad.With.MonadWithExceptable m => Control.Monad.With.MonadWithExceptable (Control.Monad.Trans.Resource.Internal.ResourceT m)
- Control.Monad.With: instance Control.Monad.With.MonadWithExceptable m => Control.Monad.With.MonadWithExceptable (Control.Monad.Trans.State.Lazy.StateT s m)
- Control.Monad.With: instance Control.Monad.With.MonadWithExceptable m => Control.Monad.With.MonadWithExceptable (Control.Monad.Trans.State.Strict.StateT s m)
- Control.Monad.With: instance GHC.Base.Monad m => Control.Monad.With.MonadWithExceptable (Control.Monad.With.WithNoContinuation m)
- Control.Monad.With: instance GHC.Exception.Type.Exception Control.Monad.With.NothingException
- Control.Monad.With: instance GHC.Show.Show Control.Monad.With.NothingException
- Control.Monad.With: instance GHC.Show.Show e => GHC.Show.Show (Control.Monad.With.EitherException e)
- Control.Monad.With: newtype EitherException e
- Control.Monad.With: withExceptionToException :: MonadWithExceptable m => WithException m -> SomeException
+ Control.Monad.Allocate: type MonadAllocateExceptable m = (MonadAllocate m, Exceptable (AllocationException m))
+ Control.Monad.With: type MonadWithExceptable m = (MonadWith m, Exceptable (WithException m))
+ Data.Exceptable: EitherException :: e -> EitherException e
+ Data.Exceptable: NothingException :: NothingException
+ Data.Exceptable: class Exceptable e
+ Data.Exceptable: data NothingException
+ Data.Exceptable: instance (GHC.Show.Show e, Data.Typeable.Internal.Typeable e) => Data.Exceptable.Exceptable (Data.Exceptable.EitherException e)
+ Data.Exceptable: instance (GHC.Show.Show e, Data.Typeable.Internal.Typeable e) => GHC.Exception.Type.Exception (Data.Exceptable.EitherException e)
+ Data.Exceptable: instance (GHC.Show.Show e, Data.Typeable.Internal.Typeable e, Data.Exceptable.Exceptable e') => Data.Exceptable.Exceptable (Data.Either.Either e e')
+ Data.Exceptable: instance Data.Exceptable.Exceptable Data.Void.Void
+ Data.Exceptable: instance Data.Exceptable.Exceptable GHC.Exception.Type.SomeException
+ Data.Exceptable: instance Data.Exceptable.Exceptable e => Data.Exceptable.Exceptable (GHC.Maybe.Maybe e)
+ Data.Exceptable: instance Data.Exceptable.Exceptable e => Data.Exceptable.Exceptable (e, s)
+ Data.Exceptable: instance Data.Exceptable.Exceptable e => Data.Exceptable.Exceptable (e, s, w)
+ Data.Exceptable: instance GHC.Exception.Type.Exception Data.Exceptable.NothingException
+ Data.Exceptable: instance GHC.Show.Show Data.Exceptable.NothingException
+ Data.Exceptable: instance GHC.Show.Show e => GHC.Show.Show (Data.Exceptable.EitherException e)
+ Data.Exceptable: newtype EitherException e
+ Data.Exceptable: toSomeException :: Exceptable e => e -> SomeException

Files

CHANGELOG.md view
@@ -1,5 +1,9 @@ # Revision history for general-allocate +## 0.2.0.0 -- 2022-12-20++* Break out `Exceptable` out from `MonadWithExceptable`+ ## 0.1.1.0 -- 2022-12-20  * Add `transformers` instances for `MonadWith`.
general-allocate.cabal view
@@ -1,6 +1,6 @@ cabal-version:      3.0 name:               general-allocate-version:            0.1.1.0+version:            0.2.0.0 license:            Apache-2.0 license-file:       LICENSE author:             Shea Levy@@ -45,6 +45,7 @@     Control.Monad.NoContinuation.Resource.Internal     Control.Monad.With     Data.GeneralAllocate+    Data.Exceptable    build-depends:     , base             ^>= { 4.14, 4.16, 4.17 }
src/Control/Monad/Allocate.hs view
@@ -1,4 +1,5 @@ {-# LANGUAGE AllowAmbiguousTypes #-}+{-# LANGUAGE ConstraintKinds #-} {-# LANGUAGE DerivingVia #-} {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-}@@ -46,6 +47,7 @@ import qualified Control.Monad.Writer.Strict as Strict import Data.Acquire import Data.Acquire.Internal+import Data.Exceptable import Data.GeneralAllocate import Data.Kind @@ -95,6 +97,9 @@   generalRelease     ∷ GeneralReleaseKey m     → AllocationContext m ()++-- | A 'MonadAllocate' whose exception type can be projected into the Haskell exception hierarchy+type MonadAllocateExceptable m = (MonadAllocate m, Exceptable (AllocationException m))  {- | A helper for [DerivingVia](https://ghc.gitlab.haskell.org/ghc/doc/users_guide/exts/deriving_via.html) a 'MonadAllocate' instance for any 'MonadResource'.
src/Control/Monad/With.hs view
@@ -1,12 +1,13 @@ {-# LANGUAGE AllowAmbiguousTypes #-}+{-# LANGUAGE ConstraintKinds #-} {-# LANGUAGE DerivingVia #-}+{-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-} {-# LANGUAGE InstanceSigs #-} {-# LANGUAGE LambdaCase #-} {-# LANGUAGE RankNTypes #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE StandaloneDeriving #-}-{-# LANGUAGE TypeApplications #-} {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE UndecidableInstances #-} {-# LANGUAGE UnicodeSyntax #-}@@ -38,6 +39,7 @@ import Control.Monad.Trans.Resource.Internal import qualified Control.Monad.Writer.Lazy as L import Control.Monad.Writer.Strict+import Data.Exceptable import Data.Functor import Data.Functor.Identity import Data.GeneralAllocate@@ -140,9 +142,7 @@   rel _ = fin  -- | A 'MonadWith' whose exception type can be projected into the Haskell exception hierarchy-class MonadWith m ⇒ MonadWithExceptable m where-  -- |-  withExceptionToException ∷ WithException m → SomeException+type MonadWithExceptable m = (MonadWith m, Exceptable (WithException m))  instance MonadWith IO where   stateThreadingGeneralWith (GeneralAllocate allocA) go = mask $ \restore → do@@ -154,9 +154,6 @@     c ← releaseA $ ReleaseSuccess b     pure (b, c) -instance MonadWithExceptable IO where-  withExceptionToException = id- {- | A helper for [DerivingVia](https://ghc.gitlab.haskell.org/ghc/doc/users_guide/exts/deriving_via.html) a 'MonadWith' and 'MonadWithExceptable' instance for any 'Monad'. @@ -181,17 +178,10 @@     c ← releaseA'     pure (b, c) -instance (Monad m) ⇒ MonadWithExceptable (WithNoContinuation m) where-  withExceptionToException = absurd- deriving via WithNoContinuation (ST s) instance MonadWith (ST s) -deriving via WithNoContinuation (ST s) instance MonadWithExceptable (ST s)- deriving via WithNoContinuation Identity instance MonadWith Identity -deriving via WithNoContinuation Identity instance MonadWithExceptable Identity- instance (MonadWith m) ⇒ MonadWith (ReaderT r m) where   type WithException (ReaderT r m) = WithException m   stateThreadingGeneralWith@@ -212,9 +202,6 @@         pure $ GeneralAllocated a releaseA'     stateThreadingGeneralWith (GeneralAllocate allocA') (flip runReaderT r . go) -instance (MonadWithExceptable m) ⇒ MonadWithExceptable (ReaderT r m) where-  withExceptionToException = withExceptionToException @m- instance (MonadWith m) ⇒ MonadWith (ResourceT m) where   type WithException (ResourceT m) = WithException m   stateThreadingGeneralWith@@ -235,29 +222,18 @@         pure $ GeneralAllocated a releaseA'     stateThreadingGeneralWith (GeneralAllocate allocA') (flip unResourceT st . go) -instance (MonadWithExceptable m) ⇒ MonadWithExceptable (ResourceT m) where-  withExceptionToException = withExceptionToException @m- instance MonadWith (Either e) where-  type WithException (Either e) = e+  type WithException (Either e) = EitherException e   stateThreadingGeneralWith (GeneralAllocate allocA) go = do     GeneralAllocated a releaseA ← allocA id     b ← case go a of       Left e → do-        _ ← releaseA $ ReleaseFailure e+        _ ← releaseA . ReleaseFailure $ EitherException e         Left e       x → x     c ← releaseA $ ReleaseSuccess b     pure (b, c) --- | An 'Exception' representing a failure in the 'Either' monad.-newtype EitherException e = EitherException e deriving stock (Show)--instance (Show e, Typeable e) ⇒ Exception (EitherException e)--instance (Show e, Typeable e) ⇒ MonadWithExceptable (Either e) where-  withExceptionToException = toException . EitherException- instance MonadWith m ⇒ MonadWith (MaybeT m) where   type WithException (MaybeT m) = Maybe (WithException m)   stateThreadingGeneralWith@@ -283,15 +259,6 @@       restore' ∷ ∀ x. MaybeT m x → MaybeT m x       restore' = MaybeT . restore . runMaybeT --- | An 'Exception' representing the 'Nothing' case in a 'MaybeT' monad.-data NothingException = NothingException deriving (Show)--instance Exception NothingException--instance MonadWithExceptable m ⇒ MonadWithExceptable (MaybeT m) where-  withExceptionToException (Just e) = withExceptionToException @m e-  withExceptionToException Nothing = toException NothingException- instance MonadWith m ⇒ MonadWith (ExceptT e m) where   type WithException (ExceptT e m) = Either e (WithException m)   stateThreadingGeneralWith@@ -320,10 +287,6 @@       restore' ∷ ∀ x. ExceptT e m x → ExceptT e m x       restore' = ExceptT . restore . runExceptT -instance (Show e, Typeable e, MonadWithExceptable m) ⇒ MonadWithExceptable (ExceptT e m) where-  withExceptionToException (Right e) = withExceptionToException @m e-  withExceptionToException (Left e) = toException $ EitherException e- instance MonadWith m ⇒ MonadWith (IdentityT m) where   type WithException (IdentityT m) = WithException m   stateThreadingGeneralWith@@ -342,9 +305,6 @@       restore' ∷ ∀ x. IdentityT m x → IdentityT m x       restore' = IdentityT . restore . runIdentityT -instance MonadWithExceptable m ⇒ MonadWithExceptable (IdentityT m) where-  withExceptionToException = withExceptionToException @m- instance MonadWith m ⇒ MonadWith (L.StateT s m) where   type WithException (L.StateT s m) = (WithException m, s)   stateThreadingGeneralWith@@ -366,9 +326,6 @@       (a, s1) → L.runStateT (go a) s1     pure ((b, c), s3) -instance (MonadWithExceptable m) ⇒ MonadWithExceptable (L.StateT s m) where-  withExceptionToException (e, _) = withExceptionToException @m e- instance MonadWith m ⇒ MonadWith (StateT s m) where   type WithException (StateT s m) = (WithException m, s)   stateThreadingGeneralWith@@ -390,9 +347,6 @@       (a, s1) → runStateT (go a) s1     pure ((b, c), s3) -instance (MonadWithExceptable m) ⇒ MonadWithExceptable (StateT s m) where-  withExceptionToException (e, _) = withExceptionToException @m e- instance (MonadWith m, Monoid w) ⇒ MonadWith (L.WriterT w m) where   type WithException (L.WriterT w m) = (WithException m, w)   stateThreadingGeneralWith@@ -420,9 +374,6 @@         pure (b, w0 <> w1)     pure ((b, c), w2) -instance (MonadWithExceptable m, Monoid w) ⇒ MonadWithExceptable (L.WriterT w m) where-  withExceptionToException (e, _) = withExceptionToException @m e- instance (MonadWith m, Monoid w) ⇒ MonadWith (WriterT w m) where   type WithException (WriterT w m) = (WithException m, w)   stateThreadingGeneralWith@@ -450,9 +401,6 @@         pure (b, w0 <> w1)     pure ((b, c), w2) -instance (MonadWithExceptable m, Monoid w) ⇒ MonadWithExceptable (WriterT w m) where-  withExceptionToException (e, _) = withExceptionToException @m e- instance (MonadWith m, Monoid w) ⇒ MonadWith (L.RWST r w s m) where   type WithException (L.RWST r w s m) = (WithException m, s, w)   stateThreadingGeneralWith@@ -480,9 +428,6 @@         pure (b, s2, w0 <> w1)     pure ((b, c), s3, w2) -instance (MonadWithExceptable m, Monoid w) ⇒ MonadWithExceptable (L.RWST r w s m) where-  withExceptionToException (e, _, _) = withExceptionToException @m e- instance (MonadWith m, Monoid w) ⇒ MonadWith (RWST r w s m) where   type WithException (RWST r w s m) = (WithException m, s, w)   stateThreadingGeneralWith@@ -509,6 +454,3 @@         (b, s2, w1) ← runRWST (go a) r s1         pure (b, s2, w0 <> w1)     pure ((b, c), s3, w2)--instance (MonadWithExceptable m, Monoid w) ⇒ MonadWithExceptable (RWST r w s m) where-  withExceptionToException (e, _, _) = withExceptionToException @m e
+ src/Data/Exceptable.hs view
@@ -0,0 +1,54 @@+{-# LANGUAGE DerivingStrategies #-}+{-# LANGUAGE UnicodeSyntax #-}++{- |+Description : Projections into the Haskell exception hierarchy+Copyright   : Copyright 2022 Shea Levy.+License     : Apache-2.0+Maintainer  : shea@shealevy.com++Provides 'Exceptable' for error types which can be projected into+the Haskell exception hierarchy.+-}+module Data.Exceptable where++import Control.Exception+import Data.Typeable+import Data.Void++-- | Types which can be projected into the Haskell exception hierarchy+class Exceptable e where+  toSomeException ∷ e → SomeException++instance Exceptable SomeException where+  toSomeException = id++instance Exceptable Void where+  toSomeException = absurd++-- | An 'Exception' representing a failure in the 'Either' monad.+newtype EitherException e = EitherException e deriving stock (Show)++instance (Show e, Typeable e) ⇒ Exception (EitherException e)++instance (Show e, Typeable e) ⇒ Exceptable (EitherException e) where+  toSomeException = toException++-- | An 'Exception' representing the 'Nothing' case in a 'Maybe' monad.+data NothingException = NothingException deriving (Show)++instance Exception NothingException++instance Exceptable e ⇒ Exceptable (Maybe e) where+  toSomeException (Just e) = toSomeException e+  toSomeException Nothing = toException NothingException++instance (Show e, Typeable e, Exceptable e') ⇒ Exceptable (Either e e') where+  toSomeException (Left e) = toException $ EitherException e+  toSomeException (Right e) = toSomeException e++instance Exceptable e ⇒ Exceptable (e, s) where+  toSomeException (e, _) = toSomeException e++instance Exceptable e ⇒ Exceptable (e, s, w) where+  toSomeException (e, _, _) = toSomeException e