recursion-schemes 0.1 → 0.1.1
raw patch · 2 files changed
+26/−17 lines, 2 filesdep −semigroups
Dependencies removed: semigroups
Files
- Data/Functor/Foldable.hs +25/−15
- recursion-schemes.cabal +1/−2
Data/Functor/Foldable.hs view
@@ -51,6 +51,9 @@ , fold, gfold , unfold, gunfold , refold, grefold+ -- * Mendler-style+ , mcata+ , mhisto ) where import Control.Applicative@@ -79,17 +82,19 @@ cata f = c where c = f . fmap c . project para :: Unfoldable t => (Base t (t, a) -> a) -> t -> a- para = zygo embed+ para t = zygo embed t gpara :: (Unfoldable t, Comonad w) => (forall b. Base t (w b) -> w (Base t b)) -> (Base t (EnvT t w a) -> a) -> t -> a- gpara = gzygo embed+ gpara t = gzygo embed t +mcata :: Foldable t => (forall y. (y -> c) -> Base t y -> c) -> t -> c+mcata psi = psi (mcata psi) . project distPara :: Unfoldable t => Base t (t, a) -> (t, Base t a) distPara = distZygo embed distParaT :: (Unfoldable t, Comonad w) => (forall b. Base t (w b) -> w (Base t b)) -> Base t (EnvT t w a) -> EnvT t w (Base t a)-distParaT = distZygoT embed+distParaT t = distZygoT embed t class Functor (Base t) => Unfoldable t where embed :: Base t t -> t@@ -155,7 +160,7 @@ -> a gcata k g = g . extract . c where c = k . fmap (duplicate . fmap g . c) . project-gfold = gcata+gfold k g t = gcata k g t distCata :: Functor f => f (Identity a) -> Identity (f a) distCata = Identity . fmap runIdentity@@ -169,7 +174,7 @@ -> t gana k f = a . return . f where a = embed . fmap (a . liftM f . join) . k-gunfold = gana+gunfold k f t = gana k f t distAna :: Functor f => Identity (f a) -> f (Identity a) distAna = fmap Identity . runIdentity@@ -185,7 +190,7 @@ -> b ghylo w m f g = extract . h . return where h = fmap f . w . fmap (duplicate . h . join) . m . liftM g-grefold = ghylo+grefold w m f g a = ghylo w m f g a newtype Fix f = Fix (f (Fix f)) deriving instance Eq (f (Fix f)) => Eq (Fix f)@@ -210,17 +215,17 @@ newtype Mu f = Mu (forall a. (f a -> a) -> a) -instance (Functor f, Eq (Fix f)) => Eq (Mu f) where+instance (Functor f, Eq (f (Fix f)), Eq (Fix f)) => Eq (Mu f) where (==) = (==) `on` toFix -instance (Functor f, Ord (Fix f)) => Ord (Mu f) where+instance (Functor f, Ord (f (Fix f)), Ord (Fix f)) => Ord (Mu f) where compare = compare `on` toFix -instance (Functor f, Show (Fix f)) => Show (Mu f) where+instance (Functor f, Show (f (Fix f)), Show (Fix f)) => Show (Mu f) where showsPrec d f = showParen (d > 10) $ showString "fromFix " . showsPrec 11 (toFix f) -instance (Functor f, Read (Fix f)) => Read (Mu f) where+instance (Functor f, Read (f (Fix f)), Read (Fix f)) => Read (Mu f) where readPrec = parens $ prec 10 $ do Ident "fromFix" <- lexP fromFix <$> step readPrec@@ -234,17 +239,17 @@ data Nu f where Nu :: (a -> f a) -> a -> Nu f -instance (Functor f, Eq (Fix f)) => Eq (Nu f) where+instance (Functor f, Eq (f (Fix f)), Eq (Fix f)) => Eq (Nu f) where (==) = (==) `on` toFix -instance (Functor f, Ord (Fix f)) => Ord (Nu f) where+instance (Functor f, Ord (f (Fix f)), Ord (Fix f)) => Ord (Nu f) where compare = compare `on` toFix -instance (Functor f, Show (Fix f)) => Show (Nu f) where+instance (Functor f, Show (f (Fix f)), Show (Fix f)) => Show (Nu f) where showsPrec d f = showParen (d > 10) $ showString "fromFix " . showsPrec 11 (toFix f) -instance (Functor f, Read (Fix f)) => Read (Nu f) where+instance (Functor f, Read (f (Fix f)), Read (Fix f)) => Read (Nu f) where readPrec = parens $ prec 10 $ do Ident "fromFix" <- lexP fromFix <$> step readPrec@@ -282,7 +287,7 @@ -> f (EnvT b w a) -> EnvT b w (f a) -- A new distributive law that adds semi-mutual recursion distZygoT g k fe = EnvT (g (getEnv <$> fe)) (k (lower <$> fe)) where getEnv (EnvT e _) = e - + gapo :: Unfoldable t => (b -> Base t b) -> (a -> Base t (Either b a)) -> a -> t gapo g = gunfold (distGApo g) @@ -292,11 +297,16 @@ distGApo :: Functor f => (b -> f b) -> Either b (f a) -> f (Either b a) distGApo f = either (fmap Left . f) (fmap Right) +-- | Course-of-value iteration histo :: Foldable t => (Base t (Stream (Base t) a) -> a) -> t -> a histo = gfold (distHisto id) ghisto :: (Foldable t, Functor h) => (forall b. Base t (h b) -> h (Base t b)) -> (Base t (Stream h a) -> a) -> t -> a ghisto g = gfold (distHisto g)++-- | Mendler-style course-of-value iteration+mhisto :: Foldable t => (forall y. (y -> c) -> (y -> Base t y) -> Base t y -> c) -> t -> c+mhisto psi = psi (mhisto psi) project . project distHisto :: (Functor f, Functor h) => (forall b. f (h b) -> h (f b)) -> f (Stream h a) -> Stream h (f a) distHisto k = Stream.unfold (\as -> (Stream.head <$> as, k (Stream.tail <$> as)))
recursion-schemes.cabal view
@@ -1,6 +1,6 @@ name: recursion-schemes category: Control, Recursion-version: 0.1+version: 0.1.1 license: BSD3 cabal-version: >= 1.6 license-file: LICENSE@@ -21,7 +21,6 @@ build-depends: base >= 4 && < 4.4, transformers >= 0.2.0 && < 0.3,- semigroups >= 0.3.4 && < 0.5, comonad >= 0.9.0.1 && < 0.10, comonad-transformers >= 0.10.1.1 && < 0.11, streams >= 0.5.0 && < 0.6