category-extras 0.44.1 → 0.44.2
raw patch · 50 files changed
+757/−569 lines, 50 files
Files
- category-extras.cabal +10/−11
- src/Control/Applicative/Indexed.hs +29/−0
- src/Control/Applicative/Parameterized.hs +24/−0
- src/Control/Arrow/BiKleisli.hs +3/−1
- src/Control/Arrow/CoKleisli.hs +3/−1
- src/Control/Bifunctor.hs +11/−1
- src/Control/Bifunctor/Associative.hs +19/−1
- src/Control/Bifunctor/Biff.hs +31/−0
- src/Control/Bifunctor/Braided.hs +20/−2
- src/Control/Bifunctor/Composition.hs +5/−109
- src/Control/Bifunctor/Constant.hs +75/−0
- src/Control/Bifunctor/Either.hs +0/−39
- src/Control/Bifunctor/Fix.hs +5/−1
- src/Control/Bifunctor/Functor.hs +38/−0
- src/Control/Bifunctor/HigherOrder.hs +0/−26
- src/Control/Bifunctor/Monoidal.hs +15/−3
- src/Control/Bifunctor/Pair.hs +0/−41
- src/Control/Bifunctor/Swap.hs +36/−0
- src/Control/Comonad.hs +17/−3
- src/Control/Comonad/Cofree.hs +11/−16
- src/Control/Comonad/Composition.hs +0/−40
- src/Control/Comonad/Context.hs +24/−6
- src/Control/Comonad/Context/Class.hs +0/−25
- src/Control/Comonad/HigherOrder.hs +5/−2
- src/Control/Comonad/Identity.hs +0/−21
- src/Control/Comonad/Indexed.hs +9/−9
- src/Control/Comonad/Parameterized.hs +44/−8
- src/Control/Comonad/Parameterized/Class.hs +0/−40
- src/Control/Comonad/Pointer.hs +9/−3
- src/Control/Comonad/Reader.hs +19/−15
- src/Control/Comonad/Reader/Class.hs +0/−18
- src/Control/Comonad/Supply.hs +7/−4
- src/Control/Functor/Adjunction.hs +6/−2
- src/Control/Functor/Bifunctor.hs +1/−3
- src/Control/Functor/Composition.hs +8/−3
- src/Control/Functor/Derivative.hs +0/−13
- src/Control/Functor/Pointed.hs +28/−6
- src/Control/Functor/Pointed/Composition.hs +49/−3
- src/Control/Functor/Pointed/Indexed.hs +39/−0
- src/Control/Functor/Pointed/Parameterized.hs +27/−0
- src/Control/Functor/Zip.hs +37/−3
- src/Control/Monad/Composition.hs +0/−42
- src/Control/Monad/Free.hs +12/−17
- src/Control/Monad/Indexed.hs +13/−10
- src/Control/Monad/Indexed/Cont.hs +12/−14
- src/Control/Monad/Indexed/State.hs +6/−1
- src/Control/Monad/Parameterized.hs +49/−3
- src/Control/Morphism/Cata.hs +1/−1
- src/Control/Morphism/Hylo.hs +0/−1
- src/Control/Morphism/Zygo.hs +0/−1
category-extras.cabal view
@@ -1,6 +1,6 @@ name: category-extras category: Control, Monads, Comonads-version: 0.44.1+version: 0.44.2 license: BSD3 license-file: LICENSE build-depends: base -any, mtl -any, array -any@@ -33,30 +33,29 @@ exposed-modules:+ Control.Applicative.Indexed,+ Control.Applicative.Parameterized, Control.Arrow.BiKleisli, Control.Arrow.CoKleisli, Control.Bifunctor, Control.Bifunctor.Associative,+ Control.Bifunctor.Biff, Control.Bifunctor.Braided, Control.Bifunctor.Composition,- Control.Bifunctor.Either,+ Control.Bifunctor.Constant,+ Control.Bifunctor.Functor, Control.Bifunctor.Fix,- Control.Bifunctor.HigherOrder, Control.Bifunctor.Monoidal,- Control.Bifunctor.Pair,+ Control.Bifunctor.Swap, Control.Comonad, Control.Comonad.Cofree,- Control.Comonad.Composition, Control.Comonad.Context,- Control.Comonad.Context.Class,- Control.Comonad.Identity,+-- Control.Comonad.Identity, Control.Comonad.Indexed, Control.Comonad.HigherOrder, Control.Comonad.Parameterized,- Control.Comonad.Parameterized.Class, Control.Comonad.Pointer, Control.Comonad.Reader,- Control.Comonad.Reader.Class, Control.Comonad.Supply Control.Functor.Adjunction, Control.Functor.Algebra,@@ -65,7 +64,6 @@ Control.Functor.Composition.Class Control.Functor.Contravariant, Control.Functor.Constant,- Control.Functor.Derivative, Control.Functor.Extras, Control.Functor.Exponential, Control.Functor.Fix,@@ -76,10 +74,11 @@ Control.Functor.Strong, Control.Functor.Pointed, Control.Functor.Pointed.Composition,+ Control.Functor.Pointed.Indexed,+ Control.Functor.Pointed.Parameterized, Control.Functor.Representable, Control.Functor.Zip, Control.Functor.Zap,- Control.Monad.Composition, Control.Monad.Free, Control.Monad.HigherOrder, Control.Monad.Indexed,
+ src/Control/Applicative/Indexed.hs view
@@ -0,0 +1,29 @@+-----------------------------------------------------------------------------+-- |+-- Module : Control.Applicative.Indexed+-- 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)+--+----------------------------------------------------------------------------+module Control.Applicative.Indexed + ( module Control.Applicative+ , module Control.Functor.Pointed.Indexed+ , IxApplicative(..)+ ) where++import Control.Applicative+import Control.Functor.Pointed.Indexed++class IxPointed m => IxApplicative m where+ iap :: m i j (a -> b) -> m j k a -> m i k b++instance (Pointed m, Applicative m) => IxApplicative (LiftIx m) where+ iap f m = LiftIx (lowerIx f <*> lowerIx m)++instance IxApplicative m => Applicative (LowerIx m i) where+ pure = LowerIx . ireturn+ f <*> m = LowerIx (liftIx f `iap` liftIx m)
+ src/Control/Applicative/Parameterized.hs view
@@ -0,0 +1,24 @@+{-# OPTIONS -fglasgow-exts #-}+-----------------------------------------------------------------------------+-- |+-- Module : Control.Applicative.Paramterized+-- Copyright : (C) 2008 Edward Kmett+-- License : BSD-style (see the file LICENSE)+--+-- Maintainer : Edward Kmett <ekmett@gmail.com>+-- Stability : experimental+-- Portability : portable+--+----------------------------------------------------------------------------+module Control.Applicative.Parameterized + ( PApplicative(..)+ , module Control.Functor.Pointed.Parameterized+ ) where++import Control.Arrow ((|||), (+++))+import Control.Monad+import Control.Monad.Parameterized.Class+import Control.Functor.Pointed.Parameterized ++class PPointed f => PApplicative f where+ pap :: f (a -> b) c -> f a c -> f b c
src/Control/Arrow/BiKleisli.hs view
@@ -10,7 +10,9 @@ -- ------------------------------------------------------------------------------------------- -module Control.Arrow.BiKleisli where+module Control.Arrow.BiKleisli+ ( BiKleisli(..)+ ) where #if __GLASGOW_HASKELL__ >= 609 import Prelude hiding (id,(.))
src/Control/Arrow/CoKleisli.hs view
@@ -9,7 +9,9 @@ -- Portability : portable -- --------------------------------------------------------------------------------------------module Control.Arrow.CoKleisli where+module Control.Arrow.CoKleisli + ( CoKleisli(..)+ ) where #if __GLASGOW_HASKELL__ >= 609 import Prelude hiding (id,(.))
src/Control/Bifunctor.hs view
@@ -9,11 +9,21 @@ -- Portability : non-portable (functional-dependencies) -- --------------------------------------------------------------------------------------------module Control.Bifunctor where+module Control.Bifunctor + ( Bifunctor(..) + ) where +import Control.Arrow ((+++), (***))+ class Bifunctor f where bimap :: (a -> c) -> (b -> d) -> f a b -> f c d first :: (a -> c) -> f a b -> f c b first f = bimap f id second :: (b -> d) -> f a b -> f a d second = bimap id++instance Bifunctor Either where+ bimap = (+++)++instance Bifunctor (,) where+ bimap = (***)
src/Control/Bifunctor/Associative.hs view
@@ -13,7 +13,11 @@ -- where the pentagonal condition does not hold, but for which there is an identity. -- --------------------------------------------------------------------------------------------module Control.Bifunctor.Associative where+module Control.Bifunctor.Associative + ( module Control.Bifunctor+ , Associative(..)+ , Coassociative(..)+ ) where import Control.Bifunctor @@ -36,4 +40,18 @@ "pentagonal coherence" bimap id associate . associate . bimap associate id = associate . associate #-} +instance Associative (,) where+ associate ((a,b),c) = (a,(b,c)) +instance Coassociative (,) where+ coassociate (a,(b,c)) = ((a,b),c)++instance Associative Either where+ associate (Left (Left a)) = Left a+ associate (Left (Right b)) = Right (Left b)+ associate (Right c) = Right (Right c)++instance Coassociative Either where+ coassociate (Left a) = Left (Left a)+ coassociate (Right (Left b)) = Left (Right b)+ coassociate (Right (Right c)) = Right c
+ src/Control/Bifunctor/Biff.hs view
@@ -0,0 +1,31 @@+-------------------------------------------------------------------------------------------+-- |+-- Module : Control.Bifunctor.Biff+-- Copyright : 2008 Edward Kmett+-- License : BSD3+--+-- Maintainer : Edward Kmett <ekmett@gmail.com>+-- Stability : experimental+-- Portability : portable+--+-------------------------------------------------------------------------------------------++module Control.Bifunctor.Biff + ( module Control.Bifunctor.Monoidal+ , BiffB(..)+ ) where++import Control.Bifunctor.Monoidal++newtype BiffB p f g a b = BiffB { runBiffB :: p (f a) (g b) } ++instance (Functor f, Bifunctor p, Functor g) => Bifunctor (BiffB p f g) where+ bimap f g = BiffB . bimap (fmap f) (fmap g) . runBiffB++instance (Functor f, Braided p) => Braided (BiffB p f f) where+ braid = BiffB . braid . runBiffB++instance (Functor f, Symmetric p) => Symmetric (BiffB p f f) ++instance (Functor f, Bifunctor p, Functor g) => Functor (BiffB p f g a) where+ fmap f = bimap id f
src/Control/Bifunctor/Braided.hs view
@@ -9,9 +9,13 @@ -- Portability : portable -- --------------------------------------------------------------------------------------------module Control.Bifunctor.Braided where+module Control.Bifunctor.Braided + ( module Control.Bifunctor.Associative+ , Braided(..)+ , Symmetric+ , swap+ ) where -import Control.Bifunctor import Control.Bifunctor.Associative {- | A braided (co)(monoidal or associative) category can commute the arguments of its bi-endofunctor. Obeys the laws:@@ -43,3 +47,17 @@ "braid/associate/braid" bimap id braid . associate . bimap braid id = associate . braid . associate "braid/coassociate/braid" bimap braid id . coassociate . bimap id braid = coassociate . braid . coassociate #-}+++instance Braided Either where+ braid (Left a) = Right a+ braid (Right b) = Left b++instance Symmetric Either+++instance Braided (,) where+ braid ~(a,b) = (b,a)++instance Symmetric (,)+
src/Control/Bifunctor/Composition.hs view
@@ -10,63 +10,14 @@ -- ------------------------------------------------------------------------------------------- -module Control.Bifunctor.Composition where-+module Control.Bifunctor.Composition + ( module Control.Bifunctor.Braided+ , CompB(..)+ , liftCompB+ ) where -import Control.Comonad-import Control.Bifunctor-import Control.Bifunctor.Associative import Control.Bifunctor.Braided-import Control.Bifunctor.Monoidal-import Control.Functor.Pointed-import Control.Functor.Exponential-import Control.Functor.Contravariant -newtype ArrowB f g a b = ArrowB { runArrowB :: f a b -> g a b }----newtype ConstB t a b = ConstB { runConstB :: t } --instance Bifunctor (ConstB t) where- bimap f g = ConstB . runConstB-instance Functor (ConstB t a) where- fmap f = ConstB . runConstB----newtype FstB a b = FstB { runFstB :: a } --instance Bifunctor FstB where- bimap f g = FstB . f . runFstB --instance Associative FstB where- associate = FstB . runFstB . runFstB--instance Functor (FstB a) where- fmap f (FstB a) = FstB a--instance ContravariantFunctor (FstB a) where- contramap f (FstB a) = FstB a--instance ExpFunctor (FstB a) where- xmap f g (FstB a) = FstB a---newtype SndB a b = SndB { runSndB :: b } --instance Bifunctor SndB where- bimap f g = SndB . g . runSndB ---- instance Coassociative SndB where--- coassociate = SndB . SndB . runSndB---- as a functor its a family of identity functors with a type-level parameter (a)-instance Functor (SndB a) where- fmap = bimap id---- bifunctor composition- newtype CompB p f g a b = CompB { runCompB :: p (f a b) (g a b) } instance (Bifunctor p, Bifunctor f, Bifunctor g) => Bifunctor (CompB p f g) where@@ -84,58 +35,3 @@ fmap = bimap id ----newtype SwapB p a b = SwapB { runSwapB :: p b a } --liftSwapB :: Bifunctor p => (p a b -> p c d) -> SwapB p b a -> SwapB p d c-liftSwapB f = SwapB . f . runSwapB--instance Bifunctor p => Bifunctor (SwapB p) where- bimap f g = liftSwapB (bimap g f)--instance Braided p => Braided (SwapB p) where- braid = liftSwapB braid--instance Symmetric p => Symmetric (SwapB p)--instance Bifunctor p => Functor (SwapB p a) where- fmap = bimap id------- a functor composed around a bifunctor--newtype FunctorB f p a b = FunctorB { runFunctorB :: f (p a b) } --liftFunctorB :: Functor f => (p a b -> p c d) -> FunctorB f p a b -> FunctorB f p c d-liftFunctorB f = FunctorB . fmap f . runFunctorB--instance (Functor f, Bifunctor p) => Bifunctor (FunctorB f p) where- bimap f g = liftFunctorB (bimap f g)--instance (Functor f, Braided p) => Braided (FunctorB f p) where- braid = liftFunctorB braid--instance (Functor f, Symmetric p) => Symmetric (FunctorB f p) --instance (Functor f, Bifunctor p) => Functor (FunctorB f p a) where- fmap = bimap id----- a bifunctor wrapping a pair of functors with different values--newtype BiffB p f g a b = BiffB { runBiffB :: p (f a) (g b) } --instance (Functor f, Bifunctor p, Functor g) => Bifunctor (BiffB p f g) where- bimap f g = BiffB . bimap (fmap f) (fmap g) . runBiffB--instance (Functor f, Braided p) => Braided (BiffB p f f) where- braid = BiffB . braid . runBiffB--instance (Functor f, Symmetric p) => Symmetric (BiffB p f f) --instance (Functor f, Bifunctor p, Functor g) => Functor (BiffB p f g a) where- fmap f = bimap id f
+ src/Control/Bifunctor/Constant.hs view
@@ -0,0 +1,75 @@+-------------------------------------------------------------------------------------------+-- |+-- Module : Control.Bifunctor.Constant+-- Copyright : 2008 Edward Kmett+-- License : BSD3+--+-- Maintainer : Edward Kmett <ekmett@gmail.com>+-- Stability : experimental+-- Portability : portable+--+-------------------------------------------------------------------------------------------++module Control.Bifunctor.Constant + ( module Control.Functor.Exponential+ , module Control.Functor.Contravariant+ , module Control.Bifunctor.Associative+ , ConstB(..)+ , FstB(..)+ , SndB(..)+ ) where++import Control.Bifunctor.Associative+import Control.Functor.Exponential+import Control.Functor.Contravariant++newtype ConstB t a b = ConstB { runConstB :: t } ++instance Bifunctor (ConstB t) where+ bimap f g = ConstB . runConstB++instance Functor (ConstB t a) where+ fmap f = ConstB . runConstB++instance ContravariantFunctor (ConstB t a) where+ contramap f = ConstB . runConstB++instance ExpFunctor (ConstB t a) where+ xmap f g = ConstB . runConstB+++++++newtype FstB a b = FstB { runFstB :: a } ++instance Bifunctor FstB where+ bimap f g = FstB . f . runFstB ++instance Associative FstB where+ associate = FstB . runFstB . runFstB++instance Functor (FstB a) where+ fmap f (FstB a) = FstB a++instance ContravariantFunctor (FstB a) where+ contramap f (FstB a) = FstB a++instance ExpFunctor (FstB a) where+ xmap f g (FstB a) = FstB a+++++++++newtype SndB a b = SndB { runSndB :: b } ++instance Bifunctor SndB where+ bimap f g = SndB . g . runSndB ++instance Functor (SndB a) where+ fmap = bimap id
− src/Control/Bifunctor/Either.hs
@@ -1,39 +0,0 @@-{-# OPTIONS -fglasgow-exts #-}--------------------------------------------------------------------------------- |--- Module : Control.Bifunctor.Either--- Copyright : (C) 2008 Edward Kmett--- License : BSD-style (see the file LICENSE)------ Maintainer : Edward Kmett <ekmett@gmail.com>--- Stability : experimental--- Portability : portable---------------------------------------------------------------------------------module Control.Bifunctor.Either where--import Control.Bifunctor-import Control.Bifunctor.Associative-import Control.Bifunctor.Monoidal-import Control.Bifunctor.Braided-import Data.Void-import Control.Arrow ((***), (+++))--instance Bifunctor Either where- bimap = (+++)--instance Associative Either where- associate (Left (Left a)) = Left a- associate (Left (Right b)) = Right (Left b)- associate (Right c) = Right (Right c)--instance Coassociative Either where- coassociate (Left a) = Left (Left a)- coassociate (Right (Left b)) = Left (Right b)- coassociate (Right (Right c)) = Right c--instance Braided Either where- braid (Left a) = Right a- braid (Right b) = Left b--instance Symmetric Either
src/Control/Bifunctor/Fix.hs view
@@ -9,7 +9,10 @@ -- Portability : portable -- --------------------------------------------------------------------------------------------module Control.Bifunctor.Fix where+module Control.Bifunctor.Fix + ( FixB(..)+ , module Control.Bifunctor+ ) where import Control.Bifunctor @@ -17,3 +20,4 @@ instance Bifunctor s => Functor (FixB s) where fmap f = InB . bimap f (fmap f) . outB+
+ src/Control/Bifunctor/Functor.hs view
@@ -0,0 +1,38 @@+-------------------------------------------------------------------------------------------+-- |+-- Module : Control.Bifunctor.Functor+-- Copyright : 2008 Edward Kmett+-- License : BSD3+--+-- Maintainer : Edward Kmett <ekmett@gmail.com>+-- Stability : experimental+-- Portability : portable+--+-------------------------------------------------------------------------------------------++module Control.Bifunctor.Functor + ( module Control.Bifunctor.Monoidal+ , FunctorB(..)+ , liftFunctorB+ ) where++import Control.Bifunctor.Monoidal++-- a functor composed around a bifunctor++newtype FunctorB f p a b = FunctorB { runFunctorB :: f (p a b) } ++liftFunctorB :: Functor f => (p a b -> p c d) -> FunctorB f p a b -> FunctorB f p c d+liftFunctorB f = FunctorB . fmap f . runFunctorB++instance (Functor f, Bifunctor p) => Bifunctor (FunctorB f p) where+ bimap f g = liftFunctorB (bimap f g)++instance (Functor f, Braided p) => Braided (FunctorB f p) where+ braid = liftFunctorB braid++instance (Functor f, Symmetric p) => Symmetric (FunctorB f p) ++instance (Functor f, Bifunctor p) => Functor (FunctorB f p a) where+ fmap = bimap id+
− src/Control/Bifunctor/HigherOrder.hs
@@ -1,26 +0,0 @@--------------------------------------------------------------------------------- |--- Module : Control.Bifunctor.HigherOrder--- 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)---------------------------------------------------------------------------------module Control.Bifunctor.HigherOrder where--import Control.Bifunctor-import Control.Functor.Extras--{--type BiNatural p q = forall a b. p a b -> q a b--class HBifunctor p where- fbimap :: Bifunctor q => (a -> b) -> (c -> d) -> p q a c -> p q b d- hbimap :: BiNatural g h -> BiNatural (p g) (p h)--newtype MuHB p a b = InHB { outHB :: p (MuHB p) a b }-type NuHB p a b = MuHB p a b--}
src/Control/Bifunctor/Monoidal.hs view
@@ -16,11 +16,17 @@ -- monoidal. This lets us reuse the same Bifunctor over different categories without -- painful type annotations. --------------------------------------------------------------------------------------------module Control.Bifunctor.Monoidal where -import Control.Bifunctor-import Control.Bifunctor.Associative+module Control.Bifunctor.Monoidal + ( module Control.Bifunctor.Braided+ , module Data.Void+ , HasIdentity+ , Monoidal(..)+ , Comonoidal(..)+ ) where+ import Control.Bifunctor.Braided+import Data.Void -- | Denotes that we have some reasonable notion of 'Identity' for a particular 'Bifunctor' in this 'Category'. This -- notion is currently used by both 'Monoidal' and 'Comonoidal'@@ -69,4 +75,10 @@ "braid/coidr" braid . coidr = coidl "braid/coidl" braid . coidl = coidr #-}++instance HasIdentity (,) Void++instance Monoidal (,) Void where+ idl = snd+ idr = fst
− src/Control/Bifunctor/Pair.hs
@@ -1,41 +0,0 @@-{-# OPTIONS -fglasgow-exts #-}--------------------------------------------------------------------------------- |--- Module : Control.Bifunctor.Pair--- Copyright : (C) 2008 Edward Kmett--- License : BSD-style (see the file LICENSE)------ Maintainer : Edward Kmett <ekmett@gmail.com>--- Stability : experimental--- Portability : portable---------------------------------------------------------------------------------module Control.Bifunctor.Pair where--import Control.Bifunctor-import Control.Bifunctor.Associative-import Control.Bifunctor.Monoidal-import Control.Bifunctor.Braided-import Data.Void-import Control.Arrow ((***), (+++))--instance Bifunctor (,) where- bimap = (***)--instance Associative (,) where- associate ((a,b),c) = (a,(b,c))--instance Coassociative (,) where- coassociate (a,(b,c)) = ((a,b),c)--instance HasIdentity (,) Void--instance Monoidal (,) Void where- idl = snd- idr = fst--instance Braided (,) where- braid ~(a,b) = (b,a)--instance Symmetric (,)-
+ src/Control/Bifunctor/Swap.hs view
@@ -0,0 +1,36 @@+-------------------------------------------------------------------------------------------+-- |+-- Module : Control.Bifunctor.Composition+-- Copyright : 2008 Edward Kmett+-- License : BSD3+--+-- Maintainer : Edward Kmett <ekmett@gmail.com>+-- Stability : experimental+-- Portability : portable+--+-------------------------------------------------------------------------------------------++module Control.Bifunctor.Swap + ( module Control.Bifunctor.Monoidal+ , SwapB(..)+ , liftSwapB+ ) where++import Control.Bifunctor.Monoidal++newtype SwapB p a b = SwapB { runSwapB :: p b a } ++liftSwapB :: Bifunctor p => (p a b -> p c d) -> SwapB p b a -> SwapB p d c+liftSwapB f = SwapB . f . runSwapB++instance Bifunctor p => Bifunctor (SwapB p) where+ bimap f g = liftSwapB (bimap g f)++instance Braided p => Braided (SwapB p) where+ braid = liftSwapB braid++instance Symmetric p => Symmetric (SwapB p)++instance Bifunctor p => Functor (SwapB p a) where+ fmap = bimap id+
src/Control/Comonad.hs view
@@ -12,9 +12,21 @@ -- -- This module declares the 'Comonad' class -----------------------------------------------------------------------------module Control.Comonad where+module Control.Comonad + ( module Control.Functor.Pointed+ , Comonad(..)+ , liftW+ , (=>>)+ , (.>>)+ , liftCtx+ , mapW+ , parallelW+ , unfoldW+ , sequenceW+ ) where import Control.Monad+import Control.Functor.Pointed import Control.Arrow ((|||), (&&&), (+++), (***)) infixl 1 =>>, .>>@@ -50,10 +62,9 @@ and the third is the definition of 'liftW'.) -} -class Functor w => Comonad w where+class Copointed w => Comonad w where duplicate :: w a -> w (w a) extend :: (w a -> b) -> w a -> w b- extract :: w a -> a extend f = fmap f . duplicate duplicate = extend id @@ -89,3 +100,6 @@ sequenceW [] _ = [] sequenceW (f:fs) w = f w : sequenceW fs w +instance Comonad Identity where+ extend f x = Identity (f x)+ duplicate = Identity
src/Control/Comonad/Cofree.hs view
@@ -10,31 +10,26 @@ -- Portability : rank-2 types -- -----------------------------------------------------------------------------module Control.Comonad.Cofree where+module Control.Comonad.Cofree + ( BiffB(..)+ , FixB(..)+ , PCopointed(..)+ , PComonad(..)+ , Identity(..)+ , CofreeB+ , Cofree+ , outCofree, runCofree, anaCofree, cofree+ ) where import Control.Arrow ((&&&))-import Control.Bifunctor import Control.Bifunctor.Fix-import Control.Bifunctor.Composition-import Control.Bifunctor.Pair-import Control.Comonad+import Control.Bifunctor.Biff import Control.Comonad.Parameterized-import Control.Comonad.Parameterized.Class-import Control.Functor.Extras import Control.Monad.Identity import Control.Monad.Parameterized-import Control.Monad.Parameterized.Class type CofreeB f a b = BiffB (,) Identity f a b type Cofree f a = FixB (BiffB (,) Identity f) a--instance Functor f => PComonad (BiffB (,) Identity f) where- pextract = runIdentity . fst . runBiffB- pextend f = BiffB . (Identity . f &&& snd . runBiffB)--instance FunctorPlus f => PMonad (BiffB (,) Identity f) where- preturn a = BiffB (Identity a,fzero)- pbind k (BiffB ~(Identity a,as)) = BiffB (ib, fplus as bs) where BiffB (ib,bs) = k a outCofree :: Cofree f a -> f (Cofree f a) outCofree = snd . runBiffB . outB
− src/Control/Comonad/Composition.hs
@@ -1,40 +0,0 @@----------------------------------------------------------------------------------------------- |--- Module : Control.Comonad.Composition--- Copyright : 2008 Edward Kmett--- License : BSD------ Maintainer : Edward Kmett <ekmett@gmail.com>--- Stability : experimental--- Portability : non-portable (functional-dependencies)------ composing a comonad with a copointed endofunctor yields a comonad given a distributive law----------------------------------------------------------------------------------------------module Control.Comonad.Composition where--import Control.Comonad-import Control.Functor.Composition-import Control.Functor.Composition.Class-import Control.Functor.Extras-import Control.Functor.Pointed-import Control.Functor.Pointed.Composition---- postDuplicate :: (Comonad w, PostUnfold w f) => w (f a) -> w (f (w (f a)))--- postDuplicate = fmap postUnfold . duplicate--instance (Comonad w, Copointed f, PostUnfold w f) => Comonad (PostCompF w f) where- extract = copoint . extract . decompose- duplicate = compose . liftW (fmap compose . postUnfold) . duplicate . decompose---- preDuplicate :: (Comonad w, Functor f, PreUnfold f w) => f (w a) -> f (w (f (w a)))--- preDuplicate = preUnfold . fmap duplicate--instance (Copointed f, Comonad w, PreUnfold f w) => Comonad (PreCompF f w) where- extract = extract . copoint . decompose- duplicate = compose . fmap (liftW compose) . preUnfold . fmap (duplicate) . decompose--instance (Comonad f, Comonad g, Distributes f g) => Comonad (DistCompF f g) where- extract = extract . extract . decompose- duplicate = compose . fmap (fmap compose . dist) . duplicate . fmap duplicate . decompose--- join = compose . fmap join . join . fmap dist . fmap (fmap decompose) . decompose
src/Control/Comonad/Context.hs view
@@ -11,13 +11,29 @@ -- -- The state-in-context comonad and comonad transformer -----------------------------------------------------------------------------module Control.Comonad.Context where+module Control.Comonad.Context + ( module Control.Comonad+ , ComonadContext(..)+ , putC+ , experiment+ , Context(..)+ , runContext+ , ContextT(..)+ ) where import Control.Arrow ((&&&), first) import Control.Comonad-import Control.Comonad.Context.Class +class Comonad w => ComonadContext s w | w -> s where+ getC :: w a -> s+ modifyC :: (s -> s) -> w a -> a +putC :: ComonadContext s w => s -> w a -> a+putC = modifyC . const ++experiment :: (ComonadContext s w, Functor f) => f (s -> s) -> w a -> f a+experiment ms a = fmap (flip modifyC a) ms+ data Context s a = Context (s -> a) s runContext f s = (a b, b) where@@ -30,11 +46,11 @@ instance Functor (Context s) where fmap f (Context f' s) = Context (f . f') s -instance Comonad (Context s) where+instance Copointed (Context s) where extract (Context f a) = f a- duplicate (Context f a) = Context (Context f) a -+instance Comonad (Context s) where+ duplicate (Context f a) = Context (Context f) a newtype ContextT s w a = ContextT { runContextT :: (w s -> a, w s) }@@ -46,6 +62,8 @@ instance Functor (ContextT b f) where fmap f = ContextT . first (f .) . runContextT -instance Comonad w => Comonad (ContextT b w) where+instance Copointed (ContextT b w) where extract = uncurry id . runContextT++instance Comonad w => Comonad (ContextT b w) where duplicate (ContextT (f,ws)) = ContextT (ContextT . (,) f, ws)
− src/Control/Comonad/Context/Class.hs
@@ -1,25 +0,0 @@-{-# OPTIONS -fglasgow-exts #-}--------------------------------------------------------------------------------- |--- Module : Control.Comonad.Context.Class--- Copyright : (C) 2008 Edward Kmett--- License : BSD-style (see the file LICENSE)------ Maintainer : Edward Kmett <ekmett@gmail.com>--- Stability : experimental--- Portability : portable---------------------------------------------------------------------------------module Control.Comonad.Context.Class where--import Control.Comonad--class Comonad w => ComonadContext s w | w -> s where- getC :: w a -> s- modifyC :: (s -> s) -> w a -> a --putC :: ComonadContext s w => s -> w a -> a-putC = modifyC . const --experiment :: (ComonadContext s w, Functor f) => f (s -> s) -> w a -> f a-experiment ms a = fmap (flip modifyC a) ms
src/Control/Comonad/HigherOrder.hs view
@@ -10,9 +10,12 @@ -- -- extending Neil Ghani and Patrician Johann's HFunctor to higher order comonads -----------------------------------------------------------------------------module Control.Comonad.HigherOrder where+module Control.Comonad.HigherOrder + ( module Control.Functor.HigherOrder+ , HComonad(..)+ ) where -import Control.Functor.Extras+import Control.Functor.Extras (Natural) import Control.Functor.HigherOrder class HFunctor w => HComonad w where
− src/Control/Comonad/Identity.hs
@@ -1,21 +0,0 @@-{-# OPTIONS -fglasgow-exts #-}--------------------------------------------------------------------------------- |--- Module : Control.Comonad.Identity--- Copyright : (C) 2008 Edward Kmett--- License : BSD-style (see the file LICENSE)------ Maintainer : Edward Kmett <ekmett@gmail.com>--- Stability : experimental--- Portability : portable---------------------------------------------------------------------------------module Control.Comonad.Identity where--import Control.Monad.Identity-import Control.Comonad--instance Comonad Identity where- extract = runIdentity- extend f x = Identity (f x)- duplicate = Identity
src/Control/Comonad/Indexed.hs view
@@ -9,22 +9,22 @@ -- Portability : portable -- -----------------------------------------------------------------------------module Control.Comonad.Indexed where+module Control.Comonad.Indexed + ( module Control.Comonad+ , Control.Functor.Pointed.Indexed.IxPointed(..)+ , Control.Functor.Pointed.Indexed.IxCopointed(..)+ , IxComonad(..)+ , iduplicate+ ) where import Control.Comonad-import Control.Arrow-import Control.Functor.Extras-import Control.Functor.HigherOrder-import Control.Functor.Indexed-import Control.Monad+import Control.Functor.Pointed.Indexed -class IxFunctor w => IxComonad w where- iextract :: w i i a -> a+class IxCopointed w => IxComonad w where iextend :: (w j k a -> b) -> w i k a -> w i j b iduplicate :: IxComonad w => w i k a -> w i j (w j k a) iduplicate = iextend id instance Comonad w => IxComonad (LiftIx w) where- iextract = extract . lowerIx iextend f = LiftIx . extend (f . LiftIx) . lowerIx
src/Control/Comonad/Parameterized.hs view
@@ -10,17 +10,53 @@ -- Portability : portable -- -----------------------------------------------------------------------------module Control.Comonad.Parameterized where+module Control.Comonad.Parameterized + ( module Control.Bifunctor.Biff+ , module Control.Functor.Pointed.Parameterized+ , module Control.Comonad+ , PComonad(..)+ , pcoaugment+ ) where -import Control.Bifunctor-import Control.Bifunctor.Fix+import Control.Arrow ((&&&)) import Control.Comonad-import Control.Comonad.Parameterized.Class+import Control.Bifunctor.Fix+import Control.Bifunctor.Biff+import Control.Functor.Pointed.Parameterized import Control.Morphism.Ana -copaugment :: PComonad f => ((FixB f a -> f b (FixB f a)) -> FixB f b) -> (FixB f a -> b) -> FixB f b-copaugment g k = g (pextend (k . InB) . outB)+class PCopointed f => PComonad f where+ pextend :: (f b c -> a) -> f b c -> f a c -instance PComonad f => Comonad (FixB f) where+{- Parameterized comonad laws:++> pextend pextract = id+> pextract . pextend g = g+> pextend (g . pextend j) = pextend g . pextend j+> pextract . second g = pextract +> second g . pextend (j . second g) = pextend j . second g ++-}++{-# RULES+"pextend pextract" pextend pextract = id+"pextract . pextend g" forall g. pextract . pextend g = g+"pextract . bimap id g" forall g. pextract . bimap id g = pextract+"bimap _ _ . pextract" forall j g. bimap id g . pextend (j . bimap id g) = pextend j . bimap id g+ #-}++pcoaugment :: PComonad f => ((FixB f a -> f b (FixB f a)) -> FixB f b) -> (FixB f a -> b) -> FixB f b+pcoaugment g k = g (pextend (k . InB) . outB)++instance PCopointed f => Copointed (FixB f) where extract = pextract . outB- extend k w = copaugment (flip biana w) k+ +instance PComonad f => Comonad (FixB f) where+ extend k w = pcoaugment (flip biana w) k++instance Functor f => PCopointed (BiffB (,) Identity f) where+ pextract = runIdentity . fst . runBiffB+ +instance Functor f => PComonad (BiffB (,) Identity f) where+ pextend f = BiffB . (Identity . f &&& snd . runBiffB)+
− src/Control/Comonad/Parameterized/Class.hs
@@ -1,40 +0,0 @@-{-# OPTIONS -fglasgow-exts #-}--------------------------------------------------------------------------------- |--- Module : Control.Comonad.Parameterized.Class--- Copyright : (C) 2008 Edward Kmett--- License : BSD-style (see the file LICENSE)------ Maintainer : Edward Kmett <ekmett@gmail.com>--- Stability : experimental--- Portability : portable---------------------------------------------------------------------------------module Control.Comonad.Parameterized.Class where--import Control.Arrow ((|||), (+++))-import Control.Monad-import Control.Bifunctor--class Bifunctor f => PComonad f where- pextract :: f a c -> a- pextend :: (f b c -> a) -> f b c -> f a c--{- Parameterized comonad laws:--> pextend pextract = id-> pextract . pextend g = g-> pextend (g . pextend j) = pextend g . pextend j-> pextract . second g = pextract -> second g . pextend (j . second g) = pextend j . second g ---}--#ifndef __HADDOCK__-{-# RULES-"pextend pextract" pextend pextract = id-"pextract . pextend g" forall g. pextract . pextend g = g-"pextract . bimap id g" forall g. pextract . bimap id g = pextract-"bimap _ _ . pextract" forall j g. bimap id g . pextend (j . bimap id g) = pextend j . bimap id g- #-}-#endif
src/Control/Comonad/Pointer.hs view
@@ -7,11 +7,15 @@ -- -- Maintainer : Edward Kmett <ekmett@gmail.com> -- Stability : experimental--- Portability : non-portable (MPTCs)+-- Portability : portable -- -- SIGFPE (Dan Piponi)'s Pointer Comonad -----------------------------------------------------------------------------module Control.Comonad.Pointer where+module Control.Comonad.Pointer + ( module Control.Comonad+ , Pointer(..)+ , distPointer+ ) where import Control.Functor.Extras import Control.Arrow ((&&&), first)@@ -25,8 +29,10 @@ instance Ix i => Functor (Pointer i) where fmap f (Pointer i a) = Pointer i (fmap f a) -instance Ix i => Comonad (Pointer i) where+instance Ix i => Copointed (Pointer i) where extract (Pointer i a) = a ! i++instance Ix i => Comonad (Pointer i) where extend f (Pointer i a) = Pointer i . listArray bds $ fmap (f . flip Pointer a) (range bds) where bds = bounds a
src/Control/Comonad/Reader.hs view
@@ -10,15 +10,23 @@ -- Portability : portable -- -----------------------------------------------------------------------------module Control.Comonad.Reader where+module Control.Comonad.Reader + ( module Control.Bifunctor+ , module Control.Comonad+ , ReaderC(..)+ , runReaderC+ , ReaderCT(..)+ , ComonadReader(..)+ ) where +import Control.Arrow ((&&&)) import Control.Bifunctor-import Control.Bifunctor.Pair-import Control.Monad.Instances -- for Functor ((,)e)-import Control.Comonad.Reader.Class import Control.Comonad-import Control.Arrow ((&&&))+import Control.Monad.Instances +class Comonad w => ComonadReader r w | w -> r where+ askC :: w a -> r+ data ReaderC r a = ReaderC r a runReaderC (ReaderC r a) = (r,a) @@ -28,8 +36,10 @@ instance Functor (ReaderC r) where fmap f = uncurry ReaderC . second f . runReaderC -instance Comonad (ReaderC r) where+instance Copointed (ReaderC r) where extract (ReaderC _ a) = a++instance Comonad (ReaderC r) where duplicate (ReaderC e a) = ReaderC e (ReaderC e a) instance Bifunctor ReaderC where@@ -44,8 +54,10 @@ instance Functor f => Functor (ReaderCT f b) where fmap f = ReaderCT . fmap (fmap f) . runReaderCT -instance Comonad w => Comonad (ReaderCT w b) where+instance Copointed w => Copointed (ReaderCT w b) where extract = snd . extract . runReaderCT++instance Comonad w => Comonad (ReaderCT w b) where duplicate = ReaderCT . liftW (fst . extract &&& ReaderCT) . duplicate . runReaderCT instance Functor f => Bifunctor (ReaderCT f) where@@ -53,15 +65,7 @@ instance Comonad ((,)e) where- extract = snd duplicate ~(e,a) = (e,(e,a)) instance ComonadReader e ((,)e) where askC = fst---- instance Functor ((,)e) where--- fmap f = second f ---- instance Bifunctor (,) where--- bimap f g = uncurry ReaderC . bimap f g . runReaderC-
− src/Control/Comonad/Reader/Class.hs
@@ -1,18 +0,0 @@-{-# OPTIONS -fglasgow-exts #-}--------------------------------------------------------------------------------- |--- Module : Control.Comonad.Reader.Class--- Copyright : (C) 2008 Edward Kmett--- License : BSD-style (see the file LICENSE)------ Maintainer : Edward Kmett <ekmett@gmail.com>--- Stability : experimental--- Portability : portable---------------------------------------------------------------------------------module Control.Comonad.Reader.Class where--import Control.Comonad--class Comonad w => ComonadReader r w | w -> r where- askC :: w a -> r
src/Control/Comonad/Supply.hs view
@@ -19,10 +19,11 @@ -------------------------------------------------------------------- module Control.Comonad.Supply- (+ ( module Control.Comonad + -- * Creating supplies- Supply+ , Supply , newSupply , newEnumSupply , newNumSupply@@ -94,7 +95,7 @@ modifySupply :: Supply a -> (Supply a -> b) -> Supply b modifySupply = flip extend --- (Supply, supplyValue, modifySupply) form a comonad:+-- (Supply, supplyValue, modifySupply) forms a comonad: {- law1 s = [ modifySupply s supplyValue, s ] law2 s f = [ supplyValue (modifySupply s f), f s ]@@ -136,8 +137,10 @@ split4 s = let s1 : s2 : s3 : s4 : _ = split s in (s1,s2,s3,s4) -instance Comonad Supply where+instance Copointed Supply where extract = supplyValue++instance Comonad Supply where extend f s = Node { supplyValue = f s , supplyLeft = modifySupply (supplyLeft s) f , supplyRight = modifySupply (supplyRight s) f
src/Control/Functor/Adjunction.hs view
@@ -20,6 +20,7 @@ import Control.Functor.Full import Control.Functor.Pointed import Control.Monad+import Control.Applicative -- | An 'Adjunction' formed by the 'Functor' f and 'Functor' g. @@ -50,12 +51,15 @@ point = compose . unit instance Adjunction f g => Copointed (ACompF f g) where- copoint = counit . decompose+ extract = counit . decompose +instance Adjunction f g => Applicative (ACompF g f) where+ pure = point+ (<*>) = ap+ instance Adjunction f g => Monad (ACompF g f) where return = point m >>= f = compose . fmap (rightAdjunct (decompose . f)) $ decompose m instance Adjunction f g => Comonad (ACompF f g) where- extract = copoint extend f = compose . fmap (leftAdjunct (f . compose)) . decompose
src/Control/Functor/Bifunctor.hs view
@@ -16,8 +16,6 @@ module Control.Functor.Bifunctor where import Control.Bifunctor-import Control.Bifunctor.Pair-import Control.Bifunctor.Either import Control.Functor.Contravariant import Control.Functor.Exponential import Control.Functor.Full@@ -61,5 +59,5 @@ runCoproductF = runBifunctorF instance (Copointed f, Copointed g) => Copointed (BifunctorF Either f g) where- copoint = (copoint ||| copoint) . runBifunctorF+ extract = (extract ||| extract) . runBifunctorF
src/Control/Functor/Composition.hs view
@@ -12,7 +12,12 @@ -- Generalized functor composeosition. ------------------------------------------------------------------------------------------- -module Control.Functor.Composition where+module Control.Functor.Composition+ ( CompF(..)+ , module Control.Functor.Composition.Class+ , module Control.Functor.Exponential+ , module Control.Functor.Full+ ) where import Control.Functor.Composition.Class import Control.Functor.Exponential@@ -24,9 +29,9 @@ compose = CompF decompose = runCompF -#ifndef __HADDOCK__+-- #ifndef __HADDOCK__ type (f :.: g) a = CompF f g a-#endif+-- #endif -- common functor composition traits instance (Functor f, Functor g) => Functor (CompF f g) where
− src/Control/Functor/Derivative.hs
@@ -1,13 +0,0 @@--------------------------------------------------------------------------------- |--- Module : Control.Functor.Derivative--- 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)---------------------------------------------------------------------------------module Control.Functor.Derivative where-
src/Control/Functor/Pointed.hs view
@@ -6,22 +6,44 @@ -- -- Maintainer : Edward Kmett <ekmett@gmail.com> -- Stability : experimental--- Portability : non-portable (functional-dependencies)+-- Portability : portable -- ------------------------------------------------------------------------------------------- -module Control.Functor.Pointed where+module Control.Functor.Pointed + ( module Control.Monad.Identity+ , Pointed(..)+ , Copointed(..)+ ) where +import Control.Monad.Identity+ -- return class Functor f => Pointed f where point :: a -> f a --- extract class Functor f => Copointed f where- copoint :: f a -> a+ extract :: f a -> a {-# RULES-"copoint/point" copoint . point = id-"point/copoint" point . copoint = id+"extract/point" extract . point = id+"point/extract" point . extract = id #-} +instance Pointed Identity where+ point = Identity++instance Pointed Maybe where+ point = Just++instance Pointed (Either a) where+ point = Right++instance Pointed [] where+ point a = [a]++instance Copointed Identity where+ extract = runIdentity++instance Copointed ((,)e) where+ extract = snd
src/Control/Functor/Pointed/Composition.hs view
@@ -9,13 +9,25 @@ -- Stability : experimental -- Portability : non-portable (functional-dependencies) --+-- TODO: finish the monad instances ------------------------------------------------------------------------------------------- -module Control.Functor.Pointed.Composition where+module Control.Functor.Pointed.Composition + ( Control.Functor.Pointed.Pointed(..)+ , Control.Functor.Pointed.Copointed(..)+ , module Control.Functor.Extras+ , module Control.Functor.Composition+ , PointedCompF(..)+ , PostCompF(..)+ , PreCompF(..)+ , DistCompF(..)+ ) where +import Control.Functor.Extras import Control.Functor.Pointed-import Control.Functor.Composition.Class import Control.Functor.Composition+import Control.Comonad+import Control.Monad import Control.Functor.Exponential import Control.Functor.Full @@ -25,9 +37,43 @@ point = compose . point . point instance (Copointed f, Copointed g) => Copointed (PointedCompF f g) where- copoint = copoint . copoint . decompose+ extract = extract . extract . decompose newtype PostCompF mw f a = PostCompF (PointedCompF mw f a) deriving (Functor, ExpFunctor, Full, Composition, Pointed, Copointed)++instance (Comonad w, Copointed f, PostUnfold w f) => Comonad (PostCompF w f) where+ duplicate = compose . liftW (fmap compose . postUnfold) . duplicate . decompose++{-+instance (Monad m, Pointed f, PostFold m f) => Monad (PostCompF m f) where+ return = compose . return . point+ m >>= k = undefined where+ postJoin :: (Monad m, PostFold m f) => m (f (m (f a))) -> m (f a)+ postJoin = join . liftM postFold+-}++ newtype PreCompF f mw a = PreCompF (PointedCompF f mw a) deriving (Functor, ExpFunctor, Full, Composition, Pointed, Copointed)++instance (Copointed f, Comonad w, PreUnfold f w) => Comonad (PreCompF f w) where+ duplicate = compose . fmap (liftW compose) . preUnfold . fmap (duplicate) . decompose++{-+instance (Pointed f, Monad m, PreFold f m) => Monad (PreCompF f m) where+ return = compose . point . return+ m >>= k = undefined where+ preJoin :: (Monad m, Functor f, PreFold f m) => f (m (f (m a))) -> f (m a)+ preJoin = fmap join . preFold+-}++ newtype DistCompF f g a = DistCompF (PointedCompF f g a) deriving (Functor, ExpFunctor, Full, Composition, Pointed, Copointed) +instance (Comonad f, Comonad g, Distributes f g) => Comonad (DistCompF f g) where+ duplicate = compose . fmap (fmap compose . dist) . duplicate . fmap duplicate . decompose++{-+instance (Monad m, Monad n, Distributes m n) => Monad (DistCompF m n) where+ return = compose . return . return+ m >>= k = undefined+-}
+ src/Control/Functor/Pointed/Indexed.hs view
@@ -0,0 +1,39 @@+-------------------------------------------------------------------------------------------+-- |+-- Module : Control.Functor.Pointed.Indexed+-- Copyright : 2008 Edward Kmett+-- License : BSD3+--+-- Maintainer : Edward Kmett <ekmett@gmail.com>+-- Stability : experimental+-- Portability : portable+--+-------------------------------------------------------------------------------------------++module Control.Functor.Pointed.Indexed + ( IxPointed(..)+ , IxCopointed(..)+ , module Control.Functor.Indexed+ , module Control.Functor.Pointed+ )where++import Control.Functor.Pointed+import Control.Functor.Indexed++class IxFunctor m => IxPointed m where+ ireturn :: a -> m i i a++class IxFunctor w => IxCopointed w where+ iextract :: w i i a -> a++instance Pointed m => IxPointed (LiftIx m) where+ ireturn = LiftIx . point++instance Copointed m => IxCopointed (LiftIx m) where+ iextract = extract . lowerIx++{-# RULES+"ireturn/iextract" ireturn . iextract = id+"iextract/ireturn" iextract . ireturn = id+ #-}+
+ src/Control/Functor/Pointed/Parameterized.hs view
@@ -0,0 +1,27 @@+-------------------------------------------------------------------------------------------+-- |+-- Module : Control.Functor.Pointed.Parameterized+-- Copyright : 2008 Edward Kmett+-- License : BSD+--+-- Maintainer : Edward Kmett <ekmett@gmail.com>+-- Stability : experimental+-- Portability : non-portable (functional-dependencies)+--+-------------------------------------------------------------------------------------------++module Control.Functor.Pointed.Parameterized where++import Control.Bifunctor++class Bifunctor f => PPointed f where+ preturn :: a -> f a c ++class Bifunctor f => PCopointed f where+ pextract :: f a c -> a++{-# RULES+"preturn/pextract" preturn . pextract = id+"pextract/preturn" pextract. preturn = id+ #-}+
src/Control/Functor/Zip.hs view
@@ -8,14 +8,23 @@ -- Stability : experimental -- Portability : portable --+-- Described in <http://comonad.com/reader/2008/zipping-and-unzipping-functors/> and+-- <http://comonad.com/reader/2008/cozipping/> ------------------------------------------------------------------------------------------- -module Control.Functor.Zip where+module Control.Functor.Zip + ( unfzip, unbizip+ , counzip, counbizip+ , Zip(..)+ , Bizip(..)+ , Cozip(..)+ ) where -import Control.Arrow ((&&&))+import Control.Arrow ((&&&),(|||)) import Control.Bifunctor import Control.Bifunctor.Composition-import Control.Bifunctor.Pair -- Bifunctor (,)+import Control.Bifunctor.Functor+import Control.Bifunctor.Biff import Control.Bifunctor.Fix import Control.Comonad.Cofree import Control.Monad.Free@@ -97,3 +106,28 @@ instance Zip f => Bizip (FreeB f) where bizipWith f g (FreeB as) (FreeB bs) = FreeB $ bizipWith f (fzipWith g) as bs -}++counzip :: Functor f => Either (f a) (f b) -> f (Either a b)+counzip = fmap Left ||| fmap Right+ +counbizip :: Bifunctor f => Either (f a c) (f b d) -> f (Either a b) (Either c d)+counbizip = bimap Left Left ||| bimap Right Right++class Functor f => Cozip f where+ cozip :: f (Either a b) -> Either (f a) (f b)+ +instance Cozip Identity where+ cozip = bimap Identity Identity . runIdentity++instance Cozip ((,)c) where+ cozip (c,ab) = bimap ((,)c) ((,)c) ab+ +-- ambiguous choice+instance Cozip Maybe where+ cozip = maybe (Left Nothing) (bimap Just Just)+-- cozip = maybe (Right Nothing) (bimap Just Just)+ +-- ambiguous choice+instance Cozip (Either c) where+ cozip = (Left . Left) ||| bimap Right Right+-- cozip = (Right . Left) ||| bimap Right Right
− src/Control/Monad/Composition.hs
@@ -1,42 +0,0 @@----------------------------------------------------------------------------------------------- |--- Module : Control.Monad.Composition--- Copyright : 2008 Edward Kmett--- License : BSD------ Maintainer : Edward Kmett <ekmett@gmail.com>--- Stability : experimental--- Portability : non-portable (functional-dependencies)-------------------------------------------------------------------------------------------------module Control.Monad.Composition where--import Control.Monad--- import Control.Functor.Composition-import Control.Functor.Composition.Class-import Control.Functor.Extras-import Control.Functor.Pointed-import Control.Functor.Pointed.Composition--postJoin :: (Monad m, PostFold m f) => m (f (m (f a))) -> m (f a)-postJoin = join . liftM postFold--instance (Monad m, Pointed f, PostFold m f) => Monad (PostCompF m f) where- return = compose . return . point- m >>= k = undefined -- TODO: dualize the comonad version--preJoin :: (Monad m, Functor f, PreFold f m) => f (m (f (m a))) -> f (m a)-preJoin = fmap join . preFold--instance (Pointed f, Monad m, PreFold f m) => Monad (PreCompF f m) where- return = compose . point . return- m >>= k = undefined---instance (Monad m, Monad n, Distributes m n) => Monad (DistCompF m n) where- return = compose . return . return- m >>= k = undefined---
src/Control/Monad/Free.hs view
@@ -10,26 +10,21 @@ -- Portability : portable -- -----------------------------------------------------------------------------module Control.Monad.Free where+module Control.Monad.Free + ( module Control.Monad.Parameterized+ , module Control.Monad.Identity+ , FreeB+ , Free+ , inFree+ , runFree+ , cataFree+ , free+ ) where import Control.Arrow ((|||), (+++))-import Control.Bifunctor-import Control.Bifunctor.Either-import Control.Bifunctor.Fix-import Control.Bifunctor.Composition-import Control.Functor.Extras-import Control.Functor.Exponential-import Control.Functor.Contravariant-import Control.Monad-import Control.Monad.Identity+import Control.Bifunctor (first) import Control.Monad.Parameterized-import Control.Monad.Parameterized.Class-import Control.Comonad.Parameterized-import Control.Comonad.Parameterized.Class--instance Functor f => PMonad (BiffB Either Identity f) where- preturn = BiffB . Left . Identity- pbind k = (k . runIdentity ||| BiffB . Right) . runBiffB+import Control.Monad.Identity type FreeB f a b = BiffB Either Identity f a b type Free f a = FixB (BiffB Either Identity f) a
src/Control/Monad/Indexed.hs view
@@ -9,17 +9,18 @@ -- Portability : non-portable (rank-2 polymorphism) -- -----------------------------------------------------------------------------module Control.Monad.Indexed where+module Control.Monad.Indexed + ( module Control.Applicative.Indexed+ , IxMonad(..)+ , ijoin, (>>>=)+ , iapIxMonad+ ) where -import Control.Comonad import Control.Arrow-import Control.Functor.Extras-import Control.Functor.HigherOrder-import Control.Functor.Indexed+import Control.Applicative.Indexed import Control.Monad -class IxFunctor m => IxMonad m where- ireturn :: a -> m i i a+class IxApplicative m => IxMonad m where ibind :: (a -> m j k b) -> m i j a -> m i k b ijoin :: IxMonad m => m i j (m j k a) -> m i k a @@ -30,10 +31,12 @@ (>>>=) :: IxMonad m => m i j a -> (a -> m j k b) -> m i k b m >>>= k = ibind k m -instance (Functor m, Monad m) => IxMonad (LiftIx m) where- ireturn = LiftIx . return+instance (Pointed m, Applicative m, Monad m) => IxMonad (LiftIx m) where ibind f m = LiftIx (lowerIx m >>= lowerIx . f) -instance (IxMonad m) => Monad (LowerIx m i) where+instance IxMonad m => Monad (LowerIx m i) where return = LowerIx . ireturn m >>= f = LowerIx (liftIx m >>>= liftIx . f)++iapIxMonad :: IxMonad m => m i j (a -> b) -> m j k a -> m i k b+iapIxMonad f x = f >>>= \ f' -> x >>>= \x' -> ireturn (f' x')
src/Control/Monad/Indexed/Cont.hs view
@@ -2,8 +2,7 @@ ------------------------------------------------------------------------------------------- -- | -- Module : Control.Monad.Indexed.Cont--- Copyright : 2008 Edward Kmett--- 2008 Dan Doel+-- Copyright : 2008 Edward Kmett, Dan Doel -- License : BSD -- -- Maintainer : Edward Kmett <ekmett@gmail.com>@@ -15,36 +14,35 @@ module Control.Monad.Indexed.Cont where import Control.Monad.Identity-import Control.Functor.Indexed import Control.Monad.Indexed +class IxMonad m => IxMonadCont m where+ reset :: m a o o -> m r r a+ shift :: ((a -> m i i o) -> m r j j) -> m r o a newtype IxContT m r o a = IxContT { runIxContT :: (a -> m o) -> m r } runIxContT_ :: Monad m => IxContT m r a a -> m r runIxContT_ m = runIxContT m return --- runIxContT :: Monad m => IxContT m r a a -> m r--- runIxContT m = unIxContT m return- instance IxFunctor (IxContT m) where imap f m = IxContT $ \c -> runIxContT m (c . f) --instance Monad m => IxMonad (IxContT m) where+instance IxPointed (IxContT m) where ireturn a = IxContT ($a)- ibind f c = IxContT $ \k -> runIxContT c $ \a -> runIxContT (f a) k -class IxMonad m => IxContMonad m where- reset :: m a o o -> m r r a- shift :: ((a -> m i i o) -> m r j j) -> m r o a+instance Monad m => IxApplicative (IxContT m) where+ iap = iapIxMonad +instance Monad m => IxMonad (IxContT m) where+ ibind f c = IxContT $ \k -> runIxContT c $ \a -> runIxContT (f a) k -instance Monad m => IxContMonad (IxContT m) where+instance Monad m => IxMonadCont (IxContT m) where reset e = IxContT $ \k -> runIxContT e return >>= k shift e = IxContT $ \k -> e (\a -> IxContT (\k' -> k a >>= k')) `runIxContT` return -newtype IxCont r o a = IxCont (IxContT Identity r o a) deriving (IxFunctor, IxMonad, IxContMonad)+newtype IxCont r o a = IxCont (IxContT Identity r o a) + deriving (IxFunctor, IxPointed, IxApplicative, IxMonad, IxMonadCont) runIxCont :: IxCont r o a -> (a -> o) -> r runIxCont (IxCont k) f = runIdentity $ runIxContT k (return . f)
src/Control/Monad/Indexed/State.hs view
@@ -24,6 +24,11 @@ instance IxFunctor IxState where imap f (IxState m) = IxState (first f . m) -instance IxMonad IxState where+instance IxPointed IxState where ireturn = IxState . (,)++instance IxApplicative IxState where+ iap = iapIxMonad++instance IxMonad IxState where ibind f (IxState m) = IxState $ \s1 -> let (a,s2) = m s1 in runIxState (f a) s2
src/Control/Monad/Parameterized.hs view
@@ -10,15 +10,48 @@ -- Portability : portable -- -----------------------------------------------------------------------------module Control.Monad.Parameterized where+module Control.Monad.Parameterized + ( FixB(..)+ , BiffB(..)+ , PPointed(..)+ , PMonad(..)+ , (>>*=), (=*<<), (>>*)+ , paugment+ ) where import Control.Arrow ((|||), (+++))-import Control.Monad import Control.Bifunctor import Control.Bifunctor.Fix-import Control.Monad.Parameterized.Class+import Control.Bifunctor.Biff+import Control.Monad.Identity+import Control.Functor.Extras+import Control.Functor.Pointed.Parameterized+import Control.Monad import Control.Morphism.Cata +class PPointed f => PMonad f where+ pbind :: (a -> f b c) -> f a c -> f b c+ pbind f = pjoin . bimap f id+ pjoin :: f (f a b) b -> f a b+ pjoin = pbind id++(>>*=) :: PMonad f => f a c -> (a -> f b c) -> f b c+(>>*=) = flip pbind++(=*<<) :: PMonad f => (a -> f b c) -> f a c -> f b c+(=*<<) = pbind++-- (>>*) :: PMonad f => f a c -> f b c -> f b c +m >>* n = m >>*= const n++{- Parameterized monad laws (from <http://crab.rutgers.edu/~pjohann/f14-ghani.pdf>)+> pbind preturn = id+> pbind g . preturn = g+> pbind (pbind g . j) = pbind g . pbind j+> pmap g . preturn = preturn+> pbind (pmap g . j) . pmap g = pmap g . pbind j +-}+ paugment :: PMonad f => (forall c. (f a c -> c) -> c) -> (a -> FixB f b) -> FixB f b paugment g k = g (InB . pbind (outB . k)) @@ -26,3 +59,16 @@ return = InB . preturn m >>= k = paugment (flip bicata m) k +instance Functor f => PPointed (BiffB Either Identity f) where+ preturn = BiffB . Left . Identity++-- The Free Monad+instance Functor f => PMonad (BiffB Either Identity f) where+ pbind k = (k . runIdentity ||| BiffB . Right) . runBiffB++instance FunctorPlus f => PPointed (BiffB (,) Identity f) where+ preturn a = BiffB (Identity a,fzero)++-- The 'Cofree' Monad+instance FunctorPlus f => PMonad (BiffB (,) Identity f) where+ pbind k (BiffB ~(Identity a,as)) = BiffB (ib, fplus as bs) where BiffB (ib,bs) = k a
src/Control/Morphism/Cata.hs view
@@ -15,7 +15,7 @@ import Control.Bifunctor import Control.Bifunctor.Fix import Control.Comonad-import Control.Comonad.Identity+import Control.Functor.Pointed import Control.Functor.Algebra import Control.Functor.HigherOrder import Control.Functor.Extras
src/Control/Morphism/Hylo.hs view
@@ -14,7 +14,6 @@ module Control.Morphism.Hylo where import Control.Bifunctor-import Control.Bifunctor.HigherOrder import Control.Comonad import Control.Monad import Control.Functor.Algebra
src/Control/Morphism/Zygo.hs view
@@ -13,7 +13,6 @@ module Control.Morphism.Zygo where import Control.Arrow ((&&&))-import Control.Bifunctor.Pair import Control.Comonad import Control.Comonad.Reader import Control.Functor.Algebra