category-extras 0.52.3 → 0.53.0
raw patch · 13 files changed
+196/−35 lines, 13 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
- Control.Comonad.Fix: class (Comonad w) => ComonadFix w
- Control.Comonad.Fix: instance ComonadFix ((,) e)
- Control.Comonad.Fix: instance ComonadFix Identity
+ Control.Functor.Composition: postTransform :: (Functor k, Composition o) => (f :~> g) -> (k o f) :~> (k o g)
+ Control.Functor.Composition: preTransform :: (Composition o) => (f :~> g) -> (f o k) :~> (g o k)
+ Control.Morphism.Apo: g_postpro_apo :: (Functor f) => Coalgebra f b -> GCoalgebra f (GApo b) a -> (f :~> f) -> a -> FixF f
+ Control.Morphism.Apo: postpro_apo :: (Functor f) => GCoalgebra f (Apo f) a -> (f :~> f) -> a -> FixF f
+ Control.Morphism.Futu: g_postpro_futu :: (Functor f, RunMonadFree h m) => Dist h f -> GCoalgebra f m a -> (f :~> f) -> a -> FixF f
+ Control.Morphism.Futu: postpro_futu :: (RunMonadFree f m) => GCoalgebra f m a -> (f :~> f) -> a -> FixF f
+ Control.Morphism.Histo: g_prepro_histo :: (RunComonadCofree h w, Functor f) => Dist f h -> GAlgebra f w a -> (f :~> f) -> FixF f -> a
+ Control.Morphism.Histo: prepro_histo :: (RunComonadCofree f w) => GAlgebra f w a -> (f :~> f) -> FixF f -> a
+ Control.Morphism.Para: g_prepro_para :: (Functor f, Comonad w) => Dist f w -> GAlgebra f (ParaT w f) a -> (f :~> f) -> FixF f -> a
+ Control.Morphism.Para: prepro_para :: (Functor f) => GAlgebra f (Para f) a -> (f :~> f) -> FixF f -> a
+ Control.Morphism.Postpro: g_postpro :: (Functor f, Monad m) => Dist m f -> GCoalgebra f m a -> (f :~> f) -> a -> FixF f
+ Control.Morphism.Postpro: postpro :: (Functor f) => (c -> f c) -> (f :~> f) -> c -> FixF f
+ Control.Morphism.Prepro: cascade :: (Bifunctor s Hask Hask Hask) => (a -> a) -> Fix s a -> Fix s a
+ Control.Morphism.Prepro: g_prepro :: (Functor f, Comonad w) => Dist f w -> GAlgebra f w a -> (f :~> f) -> FixF f -> a
+ Control.Morphism.Prepro: prepro :: (Functor f) => (f c -> c) -> (f :~> f) -> FixF f -> c
+ Control.Morphism.Zygo: g_prepro_zygo :: (Functor f, Comonad w) => GAlgebra f w b -> Dist f w -> GAlgebra f (ZygoT w b) a -> (f :~> f) -> FixF f -> a
+ Control.Morphism.Zygo: prepro_zygo :: (Functor f) => Algebra f b -> GAlgebra f (Zygo b) a -> (f :~> f) -> FixF f -> a
- Control.Comonad.Fix: cofix :: (ComonadFix w) => w (w a -> a) -> a
+ Control.Comonad.Fix: cofix :: (Comonad w) => w (w a -> a) -> a
- Control.Functor.Composition: associateComposition :: (Functor f, Composition c) => c (c f g) h a -> c f (c g h) a
+ Control.Functor.Composition: associateComposition :: (Functor f, Composition o) => ((f o g) o h) :~> (f o (g o h))
- Control.Functor.Composition: coassociateComposition :: (Functor f, Composition c) => c f (c g h) a -> c (c f g) h a
+ Control.Functor.Composition: coassociateComposition :: (Functor f, Composition o) => (f o (g o h)) :~> ((f o g) o h)
Files
- category-extras.cabal +3/−1
- src/Control/Comonad/Fix.hs +13/−8
- src/Control/Functor/Composition.hs +11/−2
- src/Control/Functor/Fix.hs +1/−0
- src/Control/Functor/Strong.hs +2/−2
- src/Control/Morphism/Apo.hs +11/−3
- src/Control/Morphism/Build.hs +1/−0
- src/Control/Morphism/Futu.hs +18/−4
- src/Control/Morphism/Histo.hs +16/−3
- src/Control/Morphism/Para.hs +22/−5
- src/Control/Morphism/Postpro.hs +32/−0
- src/Control/Morphism/Prepro.hs +44/−0
- src/Control/Morphism/Zygo.hs +22/−7
category-extras.cabal view
@@ -1,6 +1,6 @@ name: category-extras category: Control, Monads, Comonads-version: 0.52.3+version: 0.53.0 license: BSD3 cabal-version: >= 1.2 license-file: LICENSE@@ -135,6 +135,8 @@ Control.Morphism.Hylo, Control.Morphism.Meta, Control.Morphism.Para,+ Control.Morphism.Postpro,+ Control.Morphism.Prepro, Control.Morphism.Span, Control.Morphism.Universal, Control.Morphism.Zygo,
src/Control/Comonad/Fix.hs view
@@ -10,17 +10,22 @@ -- ------------------------------------------------------------------------------------------- module Control.Comonad.Fix - ( ComonadFix(..)+ ( cofix ) where import Control.Comonad-import Control.Monad.Identity+-- import Control.Monad.Identity -class Comonad w => ComonadFix w where- cofix :: w (w a -> a) -> a+--class Comonad w => ComonadFix w where+-- cofix :: w (w a -> a) -> a -instance ComonadFix Identity where- cofix (Identity f) = fix (f . Identity)+--instance ComonadFix Identity where+-- cofix (Identity f) = fix (f . Identity) -instance ComonadFix ((,)e) where- cofix ~(e,f) = let x = f (e,x) in x+--instance ComonadFix ((,)e) where+-- cofix ~(e,f) = let x = f (e,x) in x+++cofix :: Comonad w => w (w a -> a) -> a+cofix w = extract w (extend cofix w)+
src/Control/Functor/Composition.hs view
@@ -21,6 +21,8 @@ , associateComposition , coassociateComposition , (:.:)+ , preTransform+ , postTransform , Comp , (:++:) , (:**:)@@ -28,6 +30,7 @@ ) where import Control.Functor+import Control.Functor.Extras import Control.Functor.Exponential import Control.Functor.Full import Control.Functor.HigherOrder@@ -62,11 +65,17 @@ instance (Full f, Full g) => Full (CompF f g) where premap f = premap . premap $ decompose . f . compose +preTransform :: Composition o => (f :~> g) -> (f `o` k) :~> (g `o` k) +preTransform f x = compose (f (decompose x))++postTransform :: (Functor k, Composition o) => (f :~> g) -> (k `o` f) :~> (k `o` g) +postTransform f x = compose (fmap f (decompose x))+ -- | The only reason the compositions are all the same is for type inference. This can be liberalized.-associateComposition :: (Functor f, Composition c) => c (c f g) h a -> c f (c g h) a+associateComposition :: (Functor f, Composition o) => ((f `o` g) `o` h) :~> (f `o` (g `o` h)) associateComposition = compose . fmap compose . decompose . decompose -coassociateComposition :: (Functor f, Composition c) => c f (c g h) a -> c (c f g) h a+coassociateComposition :: (Functor f, Composition o) => (f `o` (g `o` h)) :~> ((f `o` g) `o` h) coassociateComposition = compose . compose . fmap decompose . decompose
src/Control/Functor/Fix.hs view
@@ -62,3 +62,4 @@ pcoaugment :: PComonad f => ((Fix f a -> f b (Fix f a)) -> Fix f b) -> (Fix f a -> b) -> Fix f b pcoaugment g k = g (pextend (k . InB) . outB)+
src/Control/Functor/Strong.hs view
@@ -18,7 +18,7 @@ import Control.Monad.Either (Either(..)) strength :: Functor f => a -> f b -> f (a,b)-strength a fb = fmap ((,)a) fb+strength = fmap . (,) costrength :: Traversable f => f (Either a b) -> Either a (f b)-costrength = sequence+costrength = Data.Traversable.sequence
src/Control/Morphism/Apo.hs view
@@ -12,10 +12,10 @@ -- Traditional operators, shown here to show how to roll your own ---------------------------------------------------------------------------- module Control.Morphism.Apo - ( apo+ ( apo, g_apo+ , postpro_apo, g_postpro_apo , Apo, ApoT , distApoT- , g_apo , GApo, GApoT , distGApo, distGApoT ) where@@ -26,6 +26,7 @@ import Control.Monad import Control.Monad.Either import Control.Morphism.Ana+import Control.Morphism.Postpro import Control.Arrow ((|||)) -- * Unfold Sugar@@ -36,13 +37,20 @@ g_apo :: Functor f => Coalgebra f b -> GCoalgebra f (GApo b) a -> a -> FixF f g_apo g = g_ana (distGApo g) +postpro_apo :: Functor f => GCoalgebra f (Apo f) a -> (f :~> f) -> a -> FixF f+postpro_apo = g_postpro_apo outF++g_postpro_apo :: Functor f => Coalgebra f b -> GCoalgebra f (GApo b) a -> (f :~> f) -> a -> FixF f+g_postpro_apo g = g_postpro (distGApo g)+ type Apo f a = Either (FixF f) a type ApoT f m a = EitherT (FixF f) m a type GApo b a = Either b a type GApoT b m a = EitherT b m a --- * Distributive Law Combinators+-- * Distributive Law Combinators for apomorphisms+-- NB: we don't actually have simple recursion combinators for all of these distGApo :: Functor f => Coalgebra f b -> Dist (Either b) f distGApo f = fmap Left . f ||| fmap Right
src/Control/Morphism/Build.hs view
@@ -17,6 +17,7 @@ import Control.Functor.KanExtension -- import Control.Functor.KanExtension.Interpreter -- import Control.Morphism.Cata+-- prepro/preprobuild fusion? -- | @forall h g. hcata h . hbuild g = g h@ cannot be realized as a RULE because -- h and g are not monotypes.
src/Control/Morphism/Futu.hs view
@@ -11,22 +11,36 @@ -- -- Traditional operators, shown here to show how to roll your own -----------------------------------------------------------------------------module Control.Morphism.Futu where+module Control.Morphism.Futu + ( futu, g_futu+ , postpro_futu, g_postpro_futu+ , distFutu+ ) where import Control.Functor.Algebra import Control.Functor.Extras import Control.Functor.Fix-import Control.Comonad () import Control.Monad.Free import Control.Morphism.Ana+import Control.Morphism.Postpro --- futu :: Functor f => GCoalgebra f (Free f) a -> a -> FixF f+-- | Generalized from @futu :: Functor f => GCoalgebra f (Free f) a -> a -> FixF f@ futu :: (RunMonadFree f m) => GCoalgebra f m a -> a -> FixF f futu = g_ana (distFutu id) g_futu :: (Functor f, RunMonadFree h m) => Dist h f -> GCoalgebra f m a -> a -> FixF f g_futu k = g_ana (distFutu k) +-- | A futumorphic postpromorphism+postpro_futu :: (RunMonadFree f m) => GCoalgebra f m a -> (f :~> f) -> a -> FixF f+postpro_futu = g_postpro (distFutu id)++-- | A generalized-futumorphic postpromorphism+g_postpro_futu :: (Functor f, RunMonadFree h m) => Dist h f -> GCoalgebra f m a -> (f :~> f) -> a -> FixF f+g_postpro_futu k = g_postpro (distFutu k)++-- | Turn a distributive law for a functor into a distributive law for the free monad of that functor.+-- This has been generalized to support generating distributive laws for a number of related free-monad-like+-- constructions such as the Codensity monad of the free monad of a functor. distFutu :: (Functor f, RunMonadFree h m) => Dist h f -> Dist m f distFutu k = cataFree (fmap return) (fmap inFree . k)-
src/Control/Morphism/Histo.hs view
@@ -11,7 +11,11 @@ -- -- Traditional operators, shown here to show how to roll your own -----------------------------------------------------------------------------module Control.Morphism.Histo where+module Control.Morphism.Histo + ( distHisto+ , histo, g_histo+ , prepro_histo, g_prepro_histo+ ) where import Control.Functor.Algebra import Control.Functor.Extras@@ -19,12 +23,21 @@ import Control.Comonad import Control.Comonad.Cofree import Control.Morphism.Cata+import Control.Morphism.Prepro +distHisto :: (RunComonadCofree h w, Functor f) => Dist f h -> Dist f w+distHisto k = anaCofree (fmap extract) (k . fmap outCofree)+ histo :: (RunComonadCofree f w) => GAlgebra f w a -> FixF f -> a histo = g_cata (distHisto id) g_histo :: (RunComonadCofree h w, Functor f) => Dist f h -> GAlgebra f w a -> FixF f -> a g_histo k = g_cata (distHisto k) -distHisto :: (RunComonadCofree h w, Functor f) => Dist f h -> Dist f w-distHisto k = anaCofree (fmap extract) (k . fmap outCofree)+-- A histomorphic prepromorphism+prepro_histo :: (RunComonadCofree f w) => GAlgebra f w a -> (f :~> f) -> FixF f -> a+prepro_histo = g_prepro (distHisto id)++-- A generalized histomorphic prepromorphism+g_prepro_histo :: (RunComonadCofree h w, Functor f) => Dist f h -> GAlgebra f w a -> (f :~> f) -> FixF f -> a+g_prepro_histo k = g_prepro (distHisto k)
src/Control/Morphism/Para.hs view
@@ -10,7 +10,13 @@ -- Portability : non-portable (rank-2 polymorphism) -- -----------------------------------------------------------------------------module Control.Morphism.Para where+module Control.Morphism.Para + ( Para+ , ParaT + , distParaT + , para, g_para+ , prepro_para, g_prepro_para+ ) where import Control.Comonad import Control.Comonad.Reader@@ -19,17 +25,28 @@ import Control.Functor.Fix import Control.Morphism.Cata import Control.Morphism.Zygo---- * Refold Sugar+import Control.Morphism.Prepro +-- * Paramorphisms use Reader Comonads type Para f = (,) (FixF f) type ParaT w f = CoreaderT w (FixF f) +-- * Distributive Laws+distParaT :: (Functor f, Comonad w) => Dist f w -> Dist f (ParaT w f)+distParaT = distZygoT (liftAlgebra InF)++-- * Paramorphism para :: Functor f => GAlgebra f (Para f) a -> FixF f -> a para = zygo InF +-- | Generalized paramorphisms using a comonad reader transformer to carry the primitive recursive state g_para :: (Functor f, Comonad w) => Dist f w -> GAlgebra f (ParaT w f) a -> FixF f -> a g_para f = g_cata (distParaT f) -distParaT :: (Functor f, Comonad w) => Dist f w -> Dist f (ParaT w f)-distParaT = distZygoT (liftAlgebra InF)+-- | A paramorphic prepromorphism+prepro_para :: Functor f => GAlgebra f (Para f) a -> (f :~> f) -> FixF f -> a+prepro_para = prepro_zygo InF++-- | A generalized paramorphic prepromorphism+g_prepro_para :: (Functor f, Comonad w) => Dist f w -> GAlgebra f (ParaT w f) a -> (f :~> f) -> FixF f -> a+g_prepro_para f = g_prepro (distParaT f)
+ src/Control/Morphism/Postpro.hs view
@@ -0,0 +1,32 @@+{-# OPTIONS_GHC -fglasgow-exts #-}+-----------------------------------------------------------------------------+-- |+-- Module : Control.Morphism.Postpro+-- Copyright : (C) 2008 Edward Kmett+-- License : BSD-style (see the file LICENSE)+--+-- Maintainer : Edward Kmett <ekmett@gmail.com>+-- Stability : experimental+-- Portability : non-portable (rank-2 polymorphism)+-- +-- See Maarten Fokkinga''s PhD Dissertation for postpro. g_postpro is +-- an obvious generalization.+----------------------------------------------------------------------------+module Control.Morphism.Postpro + ( postpro+ , g_postpro+ ) where++import Control.Monad+import Control.Functor.Algebra +import Control.Functor.Extras+import Control.Functor.Fix+import Control.Morphism.Ana++-- prepro f e = x where x = f . fmap (x . cata (InF . e)) . outF+postpro :: Functor f => (c -> f c) -> (f :~> f) -> c -> FixF f+postpro g e = x where x = InF . fmap (ana (e . outF) . x) . g++-- | Generalized postpromorphisms+g_postpro :: (Functor f, Monad m) => Dist m f -> GCoalgebra f m a -> (f :~> f) -> a -> FixF f+g_postpro k g e = a . return where a = InF . fmap (ana (e . outF) . a . join) . k . liftM g
+ src/Control/Morphism/Prepro.hs view
@@ -0,0 +1,44 @@+{-# OPTIONS_GHC -fglasgow-exts #-}+-----------------------------------------------------------------------------+-- |+-- Module : Control.Morphism.Prepro+-- Copyright : (C) 2008 Edward Kmett+-- License : BSD-style (see the file LICENSE)+--+-- Maintainer : Edward Kmett <ekmett@gmail.com>+-- Stability : experimental+-- Portability : non-portable (rank-2 polymorphism)+-- +-- See Maarten Fokkinga''s PhD Dissertation for cascade and prepro.+-- g_prepro is an obvious generalization.+----------------------------------------------------------------------------+module Control.Morphism.Prepro + ( prepro, g_prepro, cascade+ ) where++import Control.Comonad+import Control.Category.Hask+import Control.Functor+import Control.Functor.Pointed+import Control.Functor.Algebra +import Control.Functor.Extras+import Control.Functor.Fix+-- import Control.Functor.HigherOrder+import Control.Monad.Identity+import Control.Morphism.Cata++-- | @cascade f . map f = map f . cascade f@+cascade :: Bifunctor s Hask Hask Hask => (a -> a) -> Fix s a -> Fix s a +cascade f = InB . bimap id (cascade f . fmap f) . outB +-- equivalently:+-- cascade f = InB . bimap id (fmap f . cascade f) . outB ++prepro :: Functor f => (f c -> c) -> (f :~> f) -> FixF f -> c+prepro f e = x where x = f . fmap (x . cata (InF . e)) . outF++-- | Generalized prepromorphisms+g_prepro :: (Functor f, Comonad w) => Dist f w -> GAlgebra f w a -> (f :~> f) -> FixF f -> a+g_prepro k g e = extract . c where c = liftW g . k . fmap (duplicate . c . cata (InF . e)) . outF++--repro :: Functor f => (f b -> b) -> (f :~> f) -> (f :~> f) -> (a -> f a) -> a -> b+--repro f fe ge g = x where x = f . fmap (ana (fe . outF) . x . cata (InF . ge)) . g
src/Control/Morphism/Zygo.hs view
@@ -10,7 +10,14 @@ -- Portability : non-portable (rank-2 polymorphism) -- -----------------------------------------------------------------------------module Control.Morphism.Zygo where+module Control.Morphism.Zygo + ( Zygo, ZygoT+ , distZygo, distZygoT+ , zygo+ , g_zygo+ , prepro_zygo+ , g_prepro_zygo + ) where import Control.Arrow ((&&&)) import Control.Comonad@@ -19,16 +26,11 @@ import Control.Functor.Extras import Control.Functor.Fix import Control.Morphism.Cata+import Control.Morphism.Prepro type Zygo = (,) type ZygoT = CoreaderT -zygo :: Functor f => Algebra f b -> GAlgebra f (Zygo b) a -> FixF f -> a-zygo f = g_cata (distZygo f)--g_zygo :: (Functor f, Comonad w) => GAlgebra f w b -> Dist f w -> GAlgebra f (ZygoT w b) a -> FixF f -> a-g_zygo f w = g_cata (distZygoT f w)- -- * Distributive Law Combinators distZygo :: Functor f => Algebra f b -> Dist f (Zygo b)@@ -37,3 +39,16 @@ distZygoT :: (Functor f, Comonad w) => GAlgebra f w b -> Dist f w -> Dist f (ZygoT w b) distZygoT g k = CoreaderT . liftW (g . fmap (liftW fst) &&& fmap (snd . extract)) . k . fmap (duplicate . runCoreaderT) +zygo :: Functor f => Algebra f b -> GAlgebra f (Zygo b) a -> FixF f -> a+zygo f = g_cata (distZygo f)++g_zygo :: (Functor f, Comonad w) => GAlgebra f w b -> Dist f w -> GAlgebra f (ZygoT w b) a -> FixF f -> a+g_zygo f w = g_cata (distZygoT f w)++-- | a zygomorphic prepromorphism+prepro_zygo :: Functor f => Algebra f b -> GAlgebra f (Zygo b) a -> (f :~> f) -> FixF f -> a+prepro_zygo f = g_prepro (distZygo f)++-- | a generalized zygomorphic prepromorphism +g_prepro_zygo :: (Functor f, Comonad w) => GAlgebra f w b -> Dist f w -> GAlgebra f (ZygoT w b) a -> (f :~> f) -> FixF f -> a+g_prepro_zygo f w = g_prepro (distZygoT f w)