simple-effects 0.6.0.2 → 0.7.0.0
raw patch · 4 files changed
+42/−39 lines, 4 files
Files
- LICENSE +5/−26
- simple-effects.cabal +1/−1
- src/Control/Effects/Early.hs +19/−11
- test/Main.hs +17/−1
LICENSE view
@@ -1,30 +1,9 @@-Copyright Author name here (c) 2016 - -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are met: +The MIT License (MIT) - * Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. +Copyright (c) 2017 Luka Horvat - * 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. +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: - * Neither the name of Author name here nor the names of other - contributors may be used to endorse or promote products derived - from this software without specific prior written permission. +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. -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 -OWNER 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.+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.
simple-effects.cabal view
@@ -1,5 +1,5 @@ name: simple-effects -version: 0.6.0.2 +version: 0.7.0.0 synopsis: A simple effect system that integrates with MTL description: Please see README.md homepage: https://gitlab.com/LukaHorvat/simple-effects
src/Control/Effects/Early.hs view
@@ -1,32 +1,30 @@ {-# LANGUAGE RankNTypes, TypeFamilies, FlexibleContexts, ScopedTypeVariables, MultiParamTypeClasses #-} +{-# LANGUAGE FlexibleInstances #-} module Control.Effects.Early ( module Control.Effects, Early - , earlyReturn, handleEarly, onlyDo, ifNothingEarlyReturn, ifNothingDo ) where + , earlyReturn, handleEarly, onlyDo, ifNothingEarlyReturn, ifNothingDo + , ifLeftEarlyReturn, ifLeftDo ) where import Interlude import Control.Monad.Trans.Except import Control.Effects -data Early a +newtype Early a = Early { getEarlyReturn :: a } type instance EffectMsg (Early a) = a type instance EffectRes (Early a) = Void -instance Monad m => MonadEffect (Early a) (ExceptT a m) where - effect _ = throwE +instance (Monad m, a ~ b) => MonadEffect (Early a) (ExceptT (Early b) m) where + effect _ = throwE . Early -- | Allows you to return early from a function. Make sure you 'handleEarly' to get the actual -- result out. earlyReturn :: forall a b m. MonadEffect (Early a) m => a -> m b -earlyReturn = fmap absurd . effect (Proxy :: Proxy (Early a)) - -collapseEither :: Either a a -> a -collapseEither (Left a) = a -collapseEither (Right a) = a +earlyReturn = fmap (getEarlyReturn . absurd) . effect (Proxy :: Proxy (Early a)) -- | Get the result from a computation. Either the early returned one, or the regular result. -handleEarly :: Monad m => ExceptT a m a -> m a -handleEarly = fmap collapseEither +handleEarly :: Monad m => ExceptT (Early a) m a -> m a +handleEarly = fmap (either getEarlyReturn identity) . runExceptT -- | Only do the given action and exit early with it's result. @@ -42,3 +40,13 @@ -- Otherwise continue with the value inside of the 'Maybe'. ifNothingDo :: MonadEffect (Early a) m => m a -> Maybe b -> m b ifNothingDo m = maybe (onlyDo m) return + +-- | If the value is a 'Left', get the value, process it and early return the result. +-- Otherwise just return the 'Right' value. +ifLeftEarlyReturn :: MonadEffect (Early c) m => (a -> c) -> Either a b -> m b +ifLeftEarlyReturn f = either (earlyReturn . f) return + +-- | If the value is a 'Left', get the value, process it and only do the resulting action. +-- Otherwise just return the 'Right' value. +ifLeftDo :: MonadEffect (Early c) m => (a -> m c) -> Either a b -> m b +ifLeftDo f = either (onlyDo . f) return
test/Main.hs view
@@ -6,6 +6,7 @@ import Control.Effects.Signal import Control.Effects.State import Control.Effects.Parallel +import Control.Effects.Early -- Should infer ex1 = signal True @@ -20,6 +21,21 @@ handleException (\(_ :: Bool) -> return ()) ex2 handleSignal (\(_ :: Bool) -> Resume 5) ex1 +-- Nested Early +testEarly1 :: Monad m => m Bool +testEarly1 = handleEarly $ do + return () + _ <- earlyReturn True + _ <- handleEarly $ do + return () + earlyReturn (123 :: Int) + _ <- testEarly2 + return True + +testEarly2 :: Monad m => m Char +testEarly2 = handleEarly $ + earlyReturn 'a' + orderTest :: (Handles Bool m, MonadEffectState Int m, MonadIO m) => m () orderTest = do setState (1 :: Int) @@ -33,7 +49,7 @@ inc :: Int -> Int inc !x = x + 1 -task :: (MonadEffectState Int m, MonadIO m) => m Int +task :: (MonadEffectState Int m) => m Int task = do replicateM_ 10000000 (modifyState inc) st <- getState