free 4.8 → 4.8.0.1
raw patch · 4 files changed
+29/−14 lines, 4 filesdep ~basePVP ok
version bump matches the API change (PVP)
Dependency ranges changed: base
API changes (from Hackage documentation)
Files
- CHANGELOG.markdown +4/−0
- free.cabal +1/−1
- src/Control/Monad/Free.hs +6/−1
- src/Control/Monad/Free/Church.hs +18/−12
CHANGELOG.markdown view
@@ -1,3 +1,7 @@+4.8.0.1+-------+* Allow complation with older versions of `base`. (Foldable didn't add foldl' until base 4.6)+ 4.8 ----- * Added a `MonadFree` instance for `EitherT` (frrom the `either` package).
free.cabal view
@@ -1,6 +1,6 @@ name: free category: Control, Monads-version: 4.8+version: 4.8.0.1 license: BSD3 cabal-version: >= 1.10 license-file: LICENSE
src/Control/Monad/Free.hs view
@@ -7,10 +7,13 @@ #if __GLASGOW_HASKELL__ >= 707 {-# LANGUAGE DeriveDataTypeable #-} #endif+#ifndef MIN_VERSION_base+#define MIN_VERSION_base(x,y,z) 1+#endif ----------------------------------------------------------------------------- -- | -- Module : Control.Monad.Free--- Copyright : (C) 2008-2013 Edward Kmett+-- Copyright : (C) 2008-2014 Edward Kmett -- License : BSD-style (see the file LICENSE) -- -- Maintainer : Edward Kmett <ekmett@gmail.com>@@ -219,12 +222,14 @@ Free fa -> foldr (flip go) r fa {-# INLINE foldr #-} +#if MIN_VERSION_base(4,6,0) foldl' f = go where go r free = case free of Pure a -> f r a Free fa -> foldl' go r fa {-# INLINE foldl' #-}+#endif instance Foldable1 f => Foldable1 (Free f) where foldMap1 f = go where
src/Control/Monad/Free/Church.hs view
@@ -1,7 +1,11 @@+{-# LANGUAGE CPP #-} {-# LANGUAGE Rank2Types #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE UndecidableInstances #-} {-# LANGUAGE MultiParamTypeClasses #-}+#ifndef MIN_VERSION_base+#define MIN_VERSION_base(x,y,z) 1+#endif ----------------------------------------------------------------------------- -- | -- Module : Control.Monad.Free.Church@@ -18,7 +22,7 @@ -- datatype that allows for arbitrarily deep nesting of the base functor. This is -- akin to a tree, with the leaves containing the values, and the nodes being a -- level of 'Functor' over subtrees.--- +-- -- For each time that the `fmap` or `>>=` operations is used, the old tree is -- traversed up to the leaves, a new set of nodes is allocated, and -- the old ones are garbage collected. Even if the Haskell runtime@@ -30,12 +34,12 @@ -- -- * All uses of `fmap` are collapsed into a single one, so that the values on the -- _leaves_ are transformed in one pass.--- +-- -- prop> fmap f . fmap g == fmap (f . g)--- +-- -- * All uses of `>>=` are right associated, so that every new subtree created -- is final.--- +-- -- prop> (m >>= f) >>= g == m >>= (\x -> f x >>= g) -- -- Asymptotically, the Church encoding supports the monadic operations more@@ -73,9 +77,9 @@ import Prelude hiding (foldr) -- | The Church-encoded free monad for a functor @f@.--- +-- -- It is /asymptotically/ more efficient to use ('>>=') for 'F' than it is to ('>>=') with 'Free'.--- +-- -- <http://comonad.com/reader/2011/free-monads-for-less-2/> newtype F f a = F { runF :: forall r. (a -> r) -> (f r -> r) -> r } @@ -113,9 +117,11 @@ foldr f r xs = runF xs f (foldr (.) id) r {-# INLINE foldr #-} - foldl' f r xs = runF xs (flip f) (foldr (!>>>) id) r- where (!>>>) f g = \r -> g $! f r+#if MIN_VERSION_base(4,6,0)+ foldl' f z xs = runF xs (flip f) (foldr (!>>>) id) z+ where (!>>>) h g = \r -> g $! h r {-# INLINE foldl' #-}+#endif instance MonadPlus f => MonadPlus (F f) where mzero = F (\_ kf -> kf mzero)@@ -170,13 +176,13 @@ go kp kf (Free fma) = kf (fmap (go kp kf) fma) -- | Improve the asymptotic performance of code that builds a free monad with only binds and returns by using 'F' behind the scenes.--- +-- -- This is based on the \"Free Monads for Less\" series of articles by Edward Kmett:--- +-- -- * <http://comonad.com/reader/2011/free-monads-for-less/ Free monads for less — Part 1>--- +-- -- * <http://comonad.com/reader/2011/free-monads-for-less-2/ Free monads for less — Part 2>--- +-- -- and <http://www.iai.uni-bonn.de/~jv/mpc08.pdf \"Asymptotic Improvement of Computations over Free Monads\"> by Janis Voightländer. improve :: Functor f => (forall m. MonadFree f m => m a) -> Free f a improve m = fromF m