perm 0.1.0.1 → 0.2.0.0
raw patch · 6 files changed
+108/−139 lines, 6 filessetup-changed
Files
- Control/Applicative/Perm.hs +18/−20
- Control/Monad/Perm.hs +18/−20
- Control/Monad/Perm/Internal.hs +32/−54
- LICENSE +30/−30
- Setup.lhs +7/−7
- perm.cabal +3/−8
Control/Applicative/Perm.hs view
@@ -1,20 +1,18 @@-{- |-Copyright: Andy Sonnenburg (c) 2012-License: BSD-style (see the file LICENSE)-Maintainer: Andy Sonnenburg <andy22286@gmail.com>-Stability: experimental-Portability: non-portable--}-module Control.Applicative.Perm- ( Perm- , runPerm- , PermT'- , liftPerm- , hoistPerm- ) where--import Control.Monad.Perm.Internal (Perm,- PermT',- hoistPerm,- liftPerm,- runPerm)+{- | +Copyright: Andy Sonnenburg (c) 2012 +License: BSD-style (see the file LICENSE) +Maintainer: Andy Sonnenburg <andy22286@gmail.com> +Stability: experimental +Portability: non-portable +-} +module Control.Applicative.Perm + ( Perm + , runPerm + , liftPerm + , hoistPerm + ) where + +import Control.Monad.Perm.Internal (Perm, + hoistPerm, + liftPerm, + runPerm)
Control/Monad/Perm.hs view
@@ -1,20 +1,18 @@-{- |-Copyright: Andy Sonnenburg (c) 2012-License: BSD-style (see the file LICENSE)-Maintainer: Andy Sonnenburg <andy22286@gmail.com>-Stability: experimental-Portability: non-portable--}-module Control.Monad.Perm- ( PermT- , runPermT- , PermT'- , liftPerm- , hoistPerm- ) where--import Control.Monad.Perm.Internal (PermT,- PermT',- liftPerm,- hoistPerm,- runPermT)+{- | +Copyright: Andy Sonnenburg (c) 2012 +License: BSD-style (see the file LICENSE) +Maintainer: Andy Sonnenburg <andy22286@gmail.com> +Stability: experimental +Portability: non-portable +-} +module Control.Monad.Perm + ( PermT + , runPermT + , liftPerm + , hoistPerm + ) where + +import Control.Monad.Perm.Internal (PermT, + liftPerm, + hoistPerm, + runPermT)
Control/Monad/Perm/Internal.hs view
@@ -1,15 +1,9 @@-{-# LANGUAGE CPP #-}-#ifdef LANGUAGE_DataKinds-{-# LANGUAGE DataKinds #-}-#else-{-# LANGUAGE EmptyDataDecls #-}-#endif {-# LANGUAGE- FlexibleInstances+ CPP+ , FlexibleInstances , GADTs , MultiParamTypeClasses , Rank2Types- , TypeSynonymInstances , UndecidableInstances #-} {- | Copyright: Andy Sonnenburg (c) 2012@@ -23,15 +17,12 @@ , runPerm , PermT , runPermT- , PermT' , liftPerm , hoistPerm ) where -import Control.Applicative hiding (Applicative)-import qualified Control.Applicative as Applicative (Applicative)-import Control.Monad hiding (Monad)-import qualified Control.Monad as Monad (Monad)+import Control.Applicative+import Control.Monad import Control.Monad.Catch.Class import Control.Monad.IO.Class import Control.Monad.Reader.Class@@ -55,60 +46,47 @@ #endif -- | The permutation applicative-type Perm = PermT' Applicative+type Perm = PermT -- | The permutation monad-type PermT = PermT' Monad--{- |-The permutation action, available as either an 'Applicative.Applicative'-or a 'Monad.Monad', determined by the combinators used.--}-data PermT' c m a = Choice (Maybe a) [Branch c m a]--data Branch c m b where- Ap :: PermT' c m (a -> b) -> m a -> Branch c m b- Bind :: (a -> PermT m b) -> m a -> Branch Monad m b+data PermT m a = Choice (Maybe a) [Branch m a] -#ifdef LANGUAGE_DataKinds-data Constraint = Applicative | Monad-#else-data Applicative-data Monad-#endif+data Branch m b where+ Ap :: PermT m (a -> b) -> m a -> Branch m b+ Bind :: Monad m => (a -> PermT m b) -> m a -> Branch m b -instance Functor (PermT' c m) where+instance Functor (PermT m) where fmap f (Choice a xs) = Choice (f <$> a) (fmap f <$> xs) -instance Functor (Branch c m) where+instance Functor (Branch m) where fmap f (Ap perm m) = Ap (fmap (f .) perm) m fmap f (Bind k m) = Bind (fmap f . k) m -instance Applicative.Applicative (PermT' c m) where+instance Applicative (PermT m) where pure a = Choice (pure a) mempty f@(Choice f' fs) <*> a@(Choice a' as) = Choice (f' <*> a') (fmap (`apB` a) fs <> fmap (f `apP`) as) (*>) = liftThen (*>) -apP :: PermT' c m (a -> b) -> Branch c m a -> Branch c m b+apP :: PermT m (a -> b) -> Branch m a -> Branch m b f `apP` Ap perm m = (f .@ perm) `Ap` m f `apP` Bind k m = Bind ((f `ap`) . k) m -(.@) :: Applicative.Applicative f => f (b -> c) -> f (a -> b) -> f (a -> c)+(.@) :: Applicative f => f (b -> c) -> f (a -> b) -> f (a -> c) (.@) = liftA2 (.) -apB :: Branch c m (a -> b) -> PermT' c m a -> Branch c m b+apB :: Branch m (a -> b) -> PermT m a -> Branch m b Ap perm m `apB` a = flipA2 perm a `Ap` m Bind k m `apB` a = Bind ((`ap` a) . k) m -flipA2 :: Applicative.Applicative f => f (a -> b -> c) -> f b -> f (a -> c)+flipA2 :: Applicative f => f (a -> b -> c) -> f b -> f (a -> c) flipA2 = liftA2 flip -instance Alternative (PermT' c m) where+instance Alternative (PermT m) where empty = liftZero empty (<|>) = plus -instance Monad.Monad (PermT m) where+instance Monad m => Monad (PermT m) where return a = Choice (return a) mempty Choice Nothing xs >>= k = Choice Nothing (map (bindP k) xs) Choice (Just a) xs >>= k = case k a of@@ -116,15 +94,15 @@ (>>) = liftThen (>>) fail _ = Choice mzero mempty -bindP :: (a -> PermT m b) -> Branch Monad m a -> Branch Monad m b+bindP :: Monad m => (a -> PermT m b) -> Branch m a -> Branch m b bindP k (Ap perm m) = Bind (\ a -> k . ($ a) =<< perm) m bindP k (Bind k' m) = Bind (k <=< k') m -instance MonadPlus (PermT m) where+instance Monad m => MonadPlus (PermT m) where mzero = liftZero mzero mplus = plus -instance MonadTrans (PermT' c) where+instance MonadTrans PermT where lift = liftPerm instance MonadIO m => MonadIO (PermT m) where@@ -134,8 +112,7 @@ ask = lift ask local f (Choice a xs) = Choice a (map (localBranch f) xs) -localBranch :: MonadReader r m =>- (r -> r) -> Branch Monad m a -> Branch Monad m a+localBranch :: MonadReader r m => (r -> r) -> Branch m a -> Branch m a localBranch f (Ap perm m) = Ap (local f perm) (local f m) localBranch f (Bind k m) = Bind (local f . k) (local f m) @@ -151,22 +128,22 @@ #endif liftThen :: (Maybe a -> Maybe b -> Maybe b) ->- PermT' c m a -> PermT' c m b -> PermT' c m b+ PermT m a -> PermT m b -> PermT m b liftThen thenMaybe m@(Choice m' ms) n@(Choice n' ns) = Choice (m' `thenMaybe` n') (map (`thenB` n) ms <> map (m `thenP`) ns) -thenP :: PermT' c m a -> Branch c m b -> Branch c m b+thenP :: PermT m a -> Branch m b -> Branch m b m `thenP` Ap perm m' = (m *> perm) `Ap` m' m `thenP` Bind k m' = Bind ((m >>) . k) m' -thenB :: Branch c m a -> PermT' c m b -> Branch c m b+thenB :: Branch m a -> PermT m b -> Branch m b Ap perm m `thenB` n = (perm *> fmap const n) `Ap` m Bind k m `thenB` n = Bind ((>> n) . k) m -liftZero :: Maybe a -> PermT' c m a+liftZero :: Maybe a -> PermT m a liftZero zeroMaybe = Choice zeroMaybe mempty -plus :: PermT' c m a -> PermT' c m a -> PermT' c m a+plus :: PermT m a -> PermT m a -> PermT m a m@(Choice (Just _) _) `plus` _ = m Choice Nothing xs `plus` Choice b ys = Choice b (xs <> ys) @@ -176,6 +153,7 @@ where lower (Choice a xs) = foldr ((<|>) . f) (maybe empty pure a) xs f (perm `Ap` m) = m <**> runPerm perm+ f (Bind k m) = m >>= runPerm . k -- | Unwrap a 'PermT', combining actions using the 'MonadPlus' for @f@. runPermT :: MonadPlus m => PermT m a -> m a@@ -186,19 +164,19 @@ f (Bind k m) = m >>= runPermT . k -- | A version of 'lift' without the @'Monad.Monad' m@ constraint-liftPerm :: m a -> PermT' c m a+liftPerm :: m a -> PermT m a liftPerm = Choice empty . pure . liftBranch -liftBranch :: m a -> Branch c m a+liftBranch :: m a -> Branch m a liftBranch = Ap (Choice (pure id) mempty) {- | Lift a natural transformation from @m@ to @n@ into a natural transformation from @'PermT'' c m@ to @'PermT'' c n@. -}-hoistPerm :: (forall a . m a -> n a) -> PermT' c m b -> PermT' c n b+hoistPerm :: Monad n => (forall a . m a -> n a) -> PermT m b -> PermT n b hoistPerm f (Choice a xs) = Choice a (hoistBranch f <$> xs) -hoistBranch :: (forall a . m a -> n a) -> Branch c m b -> Branch c n b+hoistBranch :: Monad n => (forall a . m a -> n a) -> Branch m b -> Branch n b hoistBranch f (perm `Ap` m) = hoistPerm f perm `Ap` f m hoistBranch f (Bind k m) = Bind (hoistPerm f . k) (f m)
LICENSE view
@@ -1,30 +1,30 @@-Copyright (c) 2012, Andy Sonnenburg--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 Andy Sonnenburg nor the names of other- 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-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.+Copyright (c) 2012, Andy Sonnenburg + +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 Andy Sonnenburg nor the names of other + 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 +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.
Setup.lhs view
@@ -1,7 +1,7 @@-#!/usr/bin/env runhaskell-> module Main (main) where--> import Distribution.Simple (defaultMain)--> main :: IO ()-> main = defaultMain+#!/usr/bin/env runhaskell +> module Main (main) where + +> import Distribution.Simple (defaultMain) + +> main :: IO () +> main = defaultMain
perm.cabal view
@@ -1,13 +1,13 @@ name: perm-version: 0.1.0.1+version: 0.2.0.0 cabal-version: >= 1.10 synopsis: permutation Applicative and Monad with many mtl instances description: Based on \"Parsing Permutation Phrases\", by Arthur Baars, Andres Loeh and S. Doaitse Swierstra, /Haskell Workshop 2001/. The implementation given here does not include explicit optional actions, and instead implements- 'Alternative' and 'MonadPlus'. @m '<|>' 'pure' a@ should be used where- @'addOpt' a m@ would be used.+ 'Alternative' and 'MonadPlus'. @m '<*>' ('lift' n '<|>' 'pure' a)@ should+ be used where @'addOpt' m a n@ would be used. license: BSD3 license-file: LICENSE author: Andy Sonnenburg@@ -34,10 +34,6 @@ mtl >= 2.0 && < 2.2, catch-fd >= 0.2 && < 0.4 ghc-options: -Wall- if impl(ghc >= 7.6)- cpp-options: -DLANGUAGE_DataKinds- else- other-extensions: EmptyDataDecls if impl(ghc >= 7.2) cpp-options: -DLANGUAGE_DefaultSignatures other-extensions:@@ -46,5 +42,4 @@ GADTs MultiParamTypeClasses Rank2Types- TypeSynonymInstances UndecidableInstances