packages feed

hoist-error (empty) → 0.1.0.0

raw patch · 4 files changed

+126/−0 lines, 4 filesdep +basedep +mtlsetup-changed

Dependencies added: base, mtl

Files

+ LICENSE view
@@ -0,0 +1,20 @@+Copyright (c) 2014 AlephCloud, Inc++Permission is hereby granted, free of charge, to any person obtaining+a copy of this software and associated documentation files (the+"Software"), to deal in the Software without restriction, including+without limitation the rights to use, copy, modify, merge, publish,+distribute, sublicense, and/or sell copies of the Software, and to+permit persons to whom the Software is furnished to do so, subject to+the following conditions:++The above copyright notice and this permission notice shall be included+in all copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ hoist-error.cabal view
@@ -0,0 +1,18 @@+name:                hoist-error+version:             0.1.0.0+synopsis:            Some convenience facilities for hoisting errors into a monad+license:             MIT+license-file:        LICENSE+author:              Jon Sterling+maintainer:          jon@jonmsterling.com+copyright:           Copyright (c) 2014 AlephCloud, Inc+category:            Control+build-type:          Simple+cabal-version:       >=1.10++library+  exposed-modules:     Control.Monad.Error.Hoist+  build-depends:       base >=4.7 && <4.8+                     , mtl >=2.2.1+  hs-source-dirs:      src+  default-language:    Haskell2010
+ src/Control/Monad/Error/Hoist.hs view
@@ -0,0 +1,86 @@+{-# LANGUAGE FlexibleInstances #-}+{-# LANGUAGE FunctionalDependencies #-}+{-# LANGUAGE MultiParamTypeClasses #-}+{-# LANGUAGE UnicodeSyntax #-}++module Control.Monad.Error.Hoist+( HoistError(..)+, (<%?>)+, (<%!?>)+, (<?>)+, (<!?>)+) where++import Control.Monad.Error.Class++-- | A tricky class for easily hoisting errors out of partiality types (e.g.+-- 'Maybe', @'Either' e@) into a monad. The parameter @e@ represents the error+-- information carried by the partiality type @t@, and @e'@ represents the type+-- of error expected in the monad @m@.+--+class Monad m ⇒ HoistError m t e e' | t → e where++  -- | Given a conversion from the error in @t α@ to @e'@, we can hoist the+  -- computation into @m@.+  --+  hoistError+    ∷ (e → e')+    → t α+    → m α++instance MonadError e m ⇒ HoistError m Maybe () e where+  hoistError f = maybe (throwError $ f ()) return++instance MonadError e' m ⇒ HoistError m (Either e) e e' where+  hoistError f = either (throwError . f) return++-- | A flipped synonym for 'hoistError'.+(<%?>)+  ∷ HoistError m t e e'+  ⇒ t α+  → (e → e')+  → m α+(<%?>) = flip hoistError++infixr 9 <%?>+{-# INLINE (<%?>) #-}++-- | A version of '<%?>' that operates on values already in the monad.+--+(<%!?>)+  ∷ HoistError m t e e'+  ⇒ m (t α)+  → (e → e')+  → m α+m <%!?> e = do+  x ← m+  x <%?> e++infixr 9 <%!?>+{-# INLINE (<%!?>) #-}++-- | A version of @hoistError@ that ignores the error in @t α@ and replaces it+-- with a new one in @e'@.+--+(<?>)+  ∷ HoistError m t e e'+  ⇒ t α+  → e'+  → m α+m <?> e = m <%?> const e++infixr 9 <?>+{-# INLINE (<?>) #-}++-- | A version of @<?>@ that operates on values already in the monad.+(<!?>)+  ∷ HoistError m t e e'+  ⇒ m (t α)+  → e'+  → m α+m <!?> e = do+  x ← m+  x <?> e++infixr 9 <!?>+{-# INLINE (<!?>) #-}