cps-except (empty) → 0.0.1.0
raw patch · 3 files changed
+106/−0 lines, 3 filesdep +basedep +mtldep +mtl-compat
Dependencies added: base, mtl, mtl-compat
Files
- LICENSE +27/−0
- cps-except.cabal +26/−0
- src/Control/Monad/CPSExcept.hs +53/−0
+ LICENSE view
@@ -0,0 +1,27 @@+Copyright (c) 2014, Kristof Bastiaensen+All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++* Redistributions of source code must retain the above copyright notice, this+ list of conditions and the following disclaimer.++* Redistributions in binary form must reproduce the above copyright notice,+ this list of conditions and the following disclaimer in the documentation+ and/or other materials provided with the distribution.++* Neither the name of the {organization} nor the names of its+ contributors may be used to endorse or promote products derived from+ this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE+DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR+SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER+CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,+OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ cps-except.cabal view
@@ -0,0 +1,26 @@+cabal-version: >=1.10+name: cps-except+version: 0.0.1.0+synopsis: ExceptT replacement in CPS style+license: BSD3+license-file: LICENSE+author: Kristof Bastiaensen+maintainer: kristof@resonata.be+copyright: Kristof Bastiaensen+category: mtl, transformers+build-type: Simple+Description: ExceptT replacement in CPS style, that can faster in some circumstances.++source-repository head+ type: git+ location: https://github.com/kuribas/mfsolve++library+ hs-source-dirs: src+ Ghc-options: -Wall+ default-language: Haskell2010+ Build-depends: base >= 3 && < 5,+ mtl >= 2.1.3,+ mtl-compat >= 0.2.1+ Exposed-Modules:+ Control.Monad.CPSExcept
+ src/Control/Monad/CPSExcept.hs view
@@ -0,0 +1,53 @@+{-# LANGUAGE RankNTypes #-}+{-# LANGUAGE MultiParamTypeClasses #-}+{-# LANGUAGE FlexibleInstances #-}+{-# LANGUAGE UndecidableInstances #-}+module Control.Monad.CPSExcept where+import Control.Monad.Except+import Control.Monad.State+import Control.Monad.Reader()+import Control.Monad.Writer()+import Control.Monad.RWS()++newtype CPSExceptT e m a =+ CPSExceptT { getCPSExceptT :: forall r. ((e -> m r) -> (a -> m r) -> m r) }++runCPSExceptT :: Applicative m => CPSExceptT e m a -> m (Either e a)+runCPSExceptT (CPSExceptT f) = f (pure . Left) (pure . Right)+{-# INLINE runCPSExceptT #-}++instance Functor (CPSExceptT e m) where+ fmap f (CPSExceptT g) = CPSExceptT $ \failC successC ->+ g failC (successC . f)+ {-# INLINE fmap #-}++instance Monad m => Applicative (CPSExceptT e m) where+ pure x = CPSExceptT $ \_failC successC -> successC x+ {-# INLINE pure #-}+ (<*>) = ap+ {-# INLINE (<*>) #-}++instance Monad m => Monad (CPSExceptT e m) where+ CPSExceptT f >>= g = CPSExceptT $ \failC successC ->+ f failC (\a -> getCPSExceptT (g a) failC successC)+ {-# INLINE (>>=) #-}++instance Monad m => MonadError e (CPSExceptT e m) where+ throwError e = CPSExceptT $ \failC _successC -> failC e+ {-# INLINE throwError #-}+ catchError (CPSExceptT f) handler =+ CPSExceptT $ \failC successC ->+ f (\e -> getCPSExceptT (handler e) failC successC)+ successC+ {-# INLINE catchError #-}++instance MonadTrans (CPSExceptT e) where+ lift m = CPSExceptT $ \_failC successC -> m >>= successC+ {-# INLINE lift #-}++instance MonadState s m => MonadState s (CPSExceptT e m) where+ state f = lift $ state f+ {-# INLINE state #-}++ +