packages feed

data-fix 0.0.3 → 0.0.4

raw patch · 2 files changed

+34/−26 lines, 2 filesPVP: major bump suggested

API removals or changes: PVP suggests a major version bump

API changes (from Hackage documentation)

- Data.Fix: instance GHC.Generics.Constructor Data.Fix.C1_0Fix
- Data.Fix: instance GHC.Generics.Datatype Data.Fix.D1Fix
- Data.Fix: instance GHC.Generics.Selector Data.Fix.S1_0_0Fix
+ Data.Fix: instance GHC.Read.Read (f (Data.Fix.Fix f)) => GHC.Read.Read (Data.Fix.Fix f)

Files

data-fix.cabal view
@@ -1,5 +1,5 @@ Name:            data-fix-Version:         0.0.3+Version:         0.0.4 Cabal-Version:   >= 1.6 License:         BSD3 License-file:    LICENSE@@ -11,6 +11,8 @@ Description:        Fixpoint types and recursion schemes. If you define your AST as   fixpoint type, you get fold and unfold operations for free.+  .+  Thanks for contribution to: Matej Kollar  Stability:       Experimental 
src/Data/Fix.hs view
@@ -1,5 +1,5 @@-{-# Language -        FlexibleContexts, +{-# Language+        FlexibleContexts,         UndecidableInstances,         TypeSynonymInstances,         DeriveGeneric,@@ -12,8 +12,8 @@ -- Type @f@ should be a 'Functor' if you want to use -- simple recursion schemes or 'Traversable' if you want to -- use monadic recursion schemes. This style allows you to express--- recursive functions in non-recursive manner. --- You can imagine that a non-recursive function +-- recursive functions in non-recursive manner.+-- You can imagine that a non-recursive function -- holds values of the previous iteration. -- -- Little example:@@ -26,7 +26,7 @@ -- >    fmap f x = case x of -- >        Nil      -> Nil -- >        Cons a b -> Cons a (f b)--- > +-- > -- > length :: List a -> Int -- > length = cata $ \x -> case x of -- >    Nil      -> 0@@ -40,8 +40,8 @@ module Data.Fix (     Fix(..)     -- * Simple recursion-    -- | Type @f@ should be a 'Functor'. They transform -    -- non-recursive functions to recursive ones. +    -- | Type @f@ should be a 'Functor'. They transform+    -- non-recursive functions to recursive ones.     , cata     , ana     , hylo@@ -51,62 +51,68 @@     , cataM     , anaM     , hyloM-    ) +    ) where  import GHC.Generics import Control.Applicative import Data.Data+import Data.Function (on) import Data.Traversable --- | A fix-point type. +-- | A fix-point type. newtype Fix f = Fix { unFix :: f (Fix f) } deriving (Generic, Typeable) deriving instance (Typeable f, Data (f (Fix f))) => Data (Fix f) --- standard instances +-- standard instances  instance Show (f (Fix f)) => Show (Fix f) where-    show x = "(" ++ show (unFix x) ++ ")"-  +    showsPrec n x = showParen (n > 10) $ \s ->+        "Fix " ++ showsPrec 11 (unFix x) s++instance Read (f (Fix f)) => Read (Fix f) where+    readsPrec d = readParen (d > 10) $ \r ->+        [(Fix m, t) | ("Fix", s) <- lex r, (m, t) <- readsPrec 11 s]+ instance Eq (f (Fix f)) => Eq (Fix f) where-    a == b = unFix a == unFix b-  +    (==) = (==) `on` unFix+ instance Ord (f (Fix f)) => Ord (Fix f) where-    a `compare` b = unFix a `compare` unFix b+    compare = compare `on` unFix   -- recursion --- | Catamorphism or generic function fold. +-- | Catamorphism or generic function fold. cata :: Functor f => (f a -> a) -> (Fix f -> a) cata f = f . fmap (cata f) . unFix --- | Anamorphism or generic function unfold. +-- | Anamorphism or generic function unfold. ana :: Functor f => (a -> f a) -> (a -> Fix f) ana f = Fix . fmap (ana f) . f  -- | Hylomorphism is anamorphism followed by catamorphism.-hylo :: Functor f => (f b -> b) -> (a -> f a) -> (a -> b) +hylo :: Functor f => (f b -> b) -> (a -> f a) -> (a -> b) hylo phi psi = cata phi . ana psi  -- | Infix version of @hylo@.-(~>) :: Functor f => (a -> f a) -> (f b -> b) -> (a -> b) -psi ~> phi = phi . (fmap $ hylo phi psi) . psi+(~>) :: Functor f => (a -> f a) -> (f b -> b) -> (a -> b)+psi ~> phi = phi . fmap (hylo phi psi) . psi  -- monadic recursion  -- | Monadic catamorphism.-cataM :: (Applicative m, Monad m, Traversable t) +cataM :: (Applicative m, Monad m, Traversable t)     => (t a -> m a) -> Fix t -> m a-cataM f = (f =<< ) . traverse (cataM f) . unFix+cataM f = (f =<<) . traverse (cataM f) . unFix  -- | Monadic anamorphism. anaM :: (Applicative m, Monad m, Traversable t)     => (a -> m (t a)) -> (a -> m (Fix t))-anaM f = fmap Fix . (traverse (anaM f) =<<) . f -   +anaM f = fmap Fix . (traverse (anaM f) =<<) . f+ -- | Monadic hylomorphism. hyloM :: (Applicative m, Monad m, Traversable t)     => (t b -> m b) -> (a -> m (t a)) -> (a -> m b)-hyloM phi psi = (cataM phi =<< ) . anaM psi+hyloM phi psi = (cataM phi =<<) . anaM psi