packages feed

monad-supply (empty) → 0.1

raw patch · 4 files changed

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

Dependencies added: base, mtl

Files

+ LICENSE view
@@ -0,0 +1,18 @@+This library is based on code found on the HaskellWiki, and subject to the+license found there:++  http://www.haskell.org/haskellwiki/HaskellWiki:Copyrights++Permission is hereby granted, free of charge, to any person obtaining this+work (the "Work"), to deal in the Work without restriction, including+without limitation the rights to use, copy, modify, merge, publish,+distribute, sublicense, and/or sell copies of the Work, and to permit+persons to whom the Work is furnished to do so.++THE WORK 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 WORK OR THE USE OR OTHER DEALINGS IN+THE WORK.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ monad-supply.cabal view
@@ -0,0 +1,23 @@+Name:          monad-supply+Build-type:    Simple+Version:       0.1+Cabal-Version: >= 1.6+Synopsis:      Stateful supply monad.+Description:   Support for computations which consume values from a (possibly infinite) supply.+License:       OtherLicense+License-file:  LICENSE+Category:      Control, Data, Monads+Author:        Geoff Hulette and unknown HaskellWiki contributor(s).+Maintainer:    Geoff Hulette <ghulette@gmail.com>+Stability:     experimental+Tested-With:   GHC == 7.0.3++Library+  Exposed-modules: Control.Monad.Supply+  Hs-Source-Dirs:  src+  Build-Depends:   base >= 4 && < 5,mtl >= 2+  GHC-Options:     -Wall++Source-Repository head+  type:     git+  location: git://github.com/ghulette/monad-supply.git
+ src/Control/Monad/Supply.hs view
@@ -0,0 +1,64 @@+{-# LANGUAGE FlexibleInstances #-}+{-# LANGUAGE FunctionalDependencies #-}+{-# LANGUAGE GeneralizedNewtypeDeriving #-}+{-# LANGUAGE MultiParamTypeClasses #-}++-- | Support for computations which consume values from a (possibly infinite)+-- supply. See http://www.haskell.org/haskellwiki/New_monads/MonadSupply for+-- details.+module Control.Monad.Supply+( MonadSupply+, SupplyT+, Supply+, evalSupplyT+, evalSupply+, runSupplyT+, runSupply+, supplies+) where++import Control.Monad.Identity+import Control.Monad.State+import Data.Monoid++class Monad m => MonadSupply s m | m -> s where+  supply :: m s++-- | Supply monad transformer.+newtype SupplyT s m a = SupplyT (StateT [s] m a)+  deriving (Functor, Monad, MonadTrans, MonadIO)++-- | Supply monad. +newtype Supply s a = Supply (SupplyT s Identity a)+  deriving (Functor, Monad, MonadSupply s)++instance Monad m => MonadSupply s (SupplyT s m) where+  supply = SupplyT $ do (x:xs) <- get+                        put xs+                        return x++-- | Monoid instance for the supply monad. Actually any monad/monoid pair gives+-- rise to this monoid instance, but we can't write it like that because it+-- would conflict with existing instances provided by Data.Monoid.+instance (Monoid a) => Monoid (Supply s a) where+  mempty = return mempty+  m1 `mappend` m2 = do+    x1 <- m1+    x2 <- m2+    return (x1 `mappend` x2)++-- | Get n supplies.+supplies :: MonadSupply s m => Int -> m [s]+supplies n = replicateM n supply++evalSupplyT :: Monad m => SupplyT s m a -> [s] -> m a+evalSupplyT (SupplyT s) = evalStateT s++evalSupply :: Supply s a -> [s] -> a+evalSupply (Supply s) = runIdentity . evalSupplyT s++runSupplyT :: Monad m => SupplyT s m a -> [s] -> m (a,[s])+runSupplyT (SupplyT s) = runStateT s++runSupply :: Supply s a -> [s] -> (a,[s])+runSupply (Supply s) = runIdentity . runSupplyT s