Copyright | (c) Conal Elliott 2007-2013 |
---|---|
License | BSD3 |
Maintainer | [email protected] |
Stability | experimental |
Portability | see LANGUAGE pragma |
Safe Haskell | None |
Language | Haskell98 |
Control.Compose
Contents
Description
Various type constructor compositions and instances for them. Some come from "Applicative Programming with Effects" http://www.soi.city.ac.uk/~ross/papers/Applicative.html
- type Unop a = a -> a
- type Binop a = a -> a -> a
- result :: Category cat => (b `cat` b') -> (a `cat` b) -> a `cat` b'
- argument :: Category cat => (a' `cat` a) -> (a `cat` b) -> a' `cat` b
- (~>) :: Category cat => (a' `cat` a) -> (b `cat` b') -> (a `cat` b) -> a' `cat` b'
- (~>*) :: (Functor p, Functor q) => (a' -> a) -> (b -> b') -> (p a -> q b) -> p a' -> q b'
- (<~) :: Category cat => (b `cat` b') -> (a' `cat` a) -> (a `cat` b) -> a' `cat` b'
- (*<~) :: (Functor p, Functor q) => (b -> b') -> (a' -> a) -> (p a -> q b) -> p a' -> q b'
- class ContraFunctor h where
- contraFmap :: (a -> b) -> h b -> h a
- bicomap :: ContraFunctor f => (a :<->: b) -> f a :<->: f b
- newtype (g :. f) a = O (g (f a))
- type O = (:.)
- unO :: (g :. f) a -> g (f a)
- biO :: g (f a) :<->: (g :. f) a
- convO :: Functor g => (b :<->: g c) -> (c :<->: f a) -> b :<->: (g :. f) a
- coconvO :: ContraFunctor g => (b :<->: g c) -> (c :<->: f a) -> b :<->: (g :. f) a
- inO :: (g (f a) -> g' (f' a')) -> (g :. f) a -> (g' :. f') a'
- inO2 :: (g (f a) -> g' (f' a') -> g'' (f'' a'')) -> (g :. f) a -> (g' :. f') a' -> (g'' :. f'') a''
- inO3 :: (g (f a) -> g' (f' a') -> g'' (f'' a'') -> g''' (f''' a''')) -> (g :. f) a -> (g' :. f') a' -> (g'' :. f'') a'' -> (g''' :. f''') a'''
- oPure :: Applicative g => f a -> (g :. f) a
- oFmap :: Functor g' => (f a -> f' a') -> (g' :. f) a -> (g' :. f') a'
- oLiftA2 :: Applicative g'' => (f a -> f' a' -> f'' a'') -> (g'' :. f) a -> (g'' :. f') a' -> (g'' :. f'') a''
- oLiftA3 :: Applicative g''' => (f a -> f' a' -> f'' a'' -> f''' a''') -> (g''' :. f) a -> (g''' :. f') a' -> (g''' :. f'') a'' -> (g''' :. f''') a'''
- fmapFF :: (Functor g, Functor f) => (a -> b) -> (g :. f) a -> (g :. f) b
- fmapCC :: (ContraFunctor g, ContraFunctor f) => (a -> b) -> (g :. f) a -> (g :. f) b
- contraFmapFC :: (Functor g, ContraFunctor f) => (b -> a) -> (g :. f) a -> (g :. f) b
- contraFmapCF :: (ContraFunctor g, Functor f) => (b -> a) -> (g :. f) a -> (g :. f) b
- class DistribM m n where
- distribM :: n (m a) -> m (n a)
- joinDistribM :: (Monad m, Monad n, DistribM m n) => (m :. n) ((m :. n) a) -> (m :. n) a
- bindDistribM :: (Functor m, Functor n, Monad m, Monad n, DistribM m n) => (m :. n) a -> (a -> (m :. n) b) -> (m :. n) b
- returnDistribM :: (Monad m, Monad n) => a -> (m :. n) a
- joinMMT :: (Monad m, Monad n, Traversable n, Applicative m) => m (n (m (n a))) -> m (n a)
- joinComposeT :: (Monad m, Monad n, Traversable n, Applicative m) => (m :. n) ((m :. n) a) -> (m :. n) a
- newtype OO f j a b = OO {
- unOO :: f (a `j` b)
- newtype FunA h a b = FunA {
- unFunA :: h a -> h b
- inFunA :: ((h a -> h b) -> h' a' -> h' b') -> FunA h a b -> FunA h' a' b'
- inFunA2 :: ((h a -> h b) -> (h' a' -> h' b') -> h'' a'' -> h'' b'') -> FunA h a b -> FunA h' a' b' -> FunA h'' a'' b''
- class FunAble h where
- class Monoid_f m where
- newtype Flip j b a = Flip {
- unFlip :: a `j` b
- biFlip :: (a `j` b) :<->: Flip j b a
- inFlip :: ((a `j` b) -> a' `k` b') -> Flip j b a -> Flip k b' a'
- inFlip2 :: ((a `j` b) -> (a' `k` b') -> a'' `l` b'') -> Flip j b a -> Flip k b' a' -> Flip l b'' a''
- inFlip3 :: ((a `j` b) -> (a' `k` b') -> (a'' `l` b'') -> a''' `m` b''') -> Flip j b a -> Flip k b' a' -> Flip l b'' a'' -> Flip m b''' a'''
- type OI = Flip (->) (IO ())
- class ToOI sink where
- newtype f :$ a = App {
- unApp :: f a
- type App = (:$)
- biApp :: f a :<->: App f a
- inApp :: (f a -> f' a') -> App f a -> App f' a'
- inApp2 :: (f a -> f' a' -> f'' a'') -> App f a -> App f' a' -> App f'' a''
- newtype Id a = Id a
- unId :: Id a -> a
- biId :: a :<->: Id a
- inId :: (a -> b) -> Id a -> Id b
- inId2 :: (a -> b -> c) -> Id a -> Id b -> Id c
- newtype (f :*: g) a = Prod {
- unProd :: (f a, g a)
- (*:*) :: f a -> g a -> (f :*: g) a
- biProd :: (f a, g a) :<->: (f :*: g) a
- convProd :: (b :<->: f a) -> (c :<->: g a) -> (b, c) :<->: (f :*: g) a
- (***#) :: (a -> b -> c) -> (a' -> b' -> c') -> (a, a') -> (b, b') -> (c, c')
- ($*) :: (a -> b, a' -> b') -> (a, a') -> (b, b')
- inProd :: ((f a, g a) -> (f' a', g' a')) -> (f :*: g) a -> (f' :*: g') a'
- inProd2 :: ((f a, g a) -> (f' a', g' a') -> (f'' a'', g'' a'')) -> (f :*: g) a -> (f' :*: g') a' -> (f'' :*: g'') a''
- inProd3 :: ((f a, g a) -> (f' a', g' a') -> (f'' a'', g'' a'') -> (f''' a''', g''' a''')) -> (f :*: g) a -> (f' :*: g') a' -> (f'' :*: g'') a'' -> (f''' :*: g''') a'''
- newtype (f ::*:: g) a b = Prodd {
- unProdd :: (f a b, g a b)
- (*::*) :: f a b -> g a b -> (f ::*:: g) a b
- inProdd :: ((f a b, g a b) -> (f' a' b', g' a' b')) -> (f ::*:: g) a b -> (f' ::*:: g') a' b'
- inProdd2 :: ((f a b, g a b) -> (f' a' b', g' a' b') -> (f'' a'' b'', g'' a'' b'')) -> (f ::*:: g) a b -> (f' ::*:: g') a' b' -> (f'' ::*:: g'') a'' b''
- newtype Arrw j f g a = Arrw {
- unArrw :: f a `j` g a
- type (:->:) = Arrw (->)
- biFun :: (f a -> g a) :<->: (f :->: g) a
- convFun :: (b :<->: f a) -> (c :<->: g a) -> (b -> c) :<->: (f :->: g) a
- inArrw :: ((f a `j` g a) -> f' a' `j` g' a') -> Arrw j f g a -> Arrw j f' g' a'
- inArrw2 :: ((f a `j` g a) -> (f' a' `j` g' a') -> f'' a'' `j` g'' a'') -> Arrw j f g a -> Arrw j f' g' a' -> Arrw j f'' g'' a''
- inArrw3 :: ((f a `j` g a) -> (f' a' `j` g' a') -> (f'' a'' `j` g'' a'') -> f''' a''' `j` g''' a''') -> Arrw j f g a -> Arrw j f' g' a' -> Arrw j f'' g'' a'' -> Arrw j f''' g''' a'''
- biConst :: a :<->: Const a b
- inConst :: (a -> b) -> Const a u -> Const b v
- inConst2 :: (a -> b -> c) -> Const a u -> Const b v -> Const c w
- inConst3 :: (a -> b -> c -> d) -> Const a u -> Const b v -> Const c w -> Const d x
- biEndo :: (a -> a) :<->: Endo a
- inEndo :: (Unop a -> Unop a') -> Endo a -> Endo a'
Value transformers
Specialized semantic editor combinators
argument :: Category cat => (a' `cat` a) -> (a `cat` b) -> a' `cat` b Source
Add pre-processing argument :: (a' -> a) -> ((a -> b) -> (a' -> b))
(~>) :: Category cat => (a' `cat` a) -> (b `cat` b') -> (a `cat` b) -> a' `cat` b' infixr 1 Source
Add pre- and post processing
(~>*) :: (Functor p, Functor q) => (a' -> a) -> (b -> b') -> (p a -> q b) -> p a' -> q b' infixr 1 Source
Like '(~>)' but specialized to functors and functions.
(*<~) :: (Functor p, Functor q) => (b -> b') -> (a' -> a) -> (p a -> q b) -> p a' -> q b' infixl 1 Source
Contravariant functors
class ContraFunctor h where Source
Contravariant functors. often useful for acceptors (consumers, sinks) of values.
Methods
contraFmap :: (a -> b) -> h b -> h a Source
Instances
Arrow arr => ContraFunctor (Flip arr b) Source | |
(Arrow j, Functor f, ContraFunctor g) => ContraFunctor (Arrw j f g) Source |
bicomap :: ContraFunctor f => (a :<->: b) -> f a :<->: f b Source
Bijections on contravariant functors
Unary/unary composition
newtype (g :. f) a infixl 9 Source
Composition of unary type constructors
There are (at least) two useful Monoid
instances, so you'll have to
pick one and type-specialize it (filling in all or parts of g
and/or f
).
-- standard Monoid instance for Applicative applied to Monoid instance (Applicative (g :. f), Monoid a) => Monoid ((g :. f) a) where { mempty = pure mempty; mappend = liftA2 mappend } -- Especially handy when g is a Monoid_f. instance Monoid (g (f a)) => Monoid ((g :. f) a) where { mempty = O mempty; mappend = inO2 mappend }
Corresponding to the first and second definitions above,
instance (Applicative g, Monoid_f f) => Monoid_f (g :. f) where { mempty_f = O (pure mempty_f); mappend_f = inO2 (liftA2 mappend_f) } instance Monoid_f g => Monoid_f (g :. f) where { mempty_f = O mempty_f; mappend_f = inO2 mappend_f }
Similarly, there are two useful Functor
instances and two useful
ContraFunctor
instances.
instance ( Functor g, Functor f) => Functor (g :. f) where fmap = fmapFF instance (ContraFunctor g, ContraFunctor f) => Functor (g :. f) where fmap = fmapCC instance ( Functor g, ContraFunctor f) => ContraFunctor (g :. f) where contraFmap = contraFmapFC instance (ContraFunctor g, Functor f) => ContraFunctor (g :. f) where contraFmap = contraFmapCF
However, it's such a bother to define the Functor instances per composition type, I've left the fmapFF case in. If you want the fmapCC one, you're out of luck for now. I'd love to hear a good solution. Maybe someday Haskell will do Prolog-style search for instances, subgoaling the constraints, rather than just matching instance heads.
Constructors
O (g (f a)) |
Instances
Applicative f => Lambda f ((:.) (Flip (->) o) f) Source | |
Applicative f => Lambda f ((:.) f (Flip (->) o)) Source | |
(Functor g, Functor f) => Functor ((:.) g f) Source | |
(Applicative g, Applicative f) => Applicative ((:.) g f) Source | |
(Foldable g, Foldable f, Functor g) => Foldable ((:.) g f) Source | |
(Traversable g, Traversable f) => Traversable ((:.) g f) Source | |
Title_f g => Title_f ((:.) g f) Source | |
(Functor h, Copair f) => Copair ((:.) h f) Source | |
(Functor h, Cozip f) => Cozip ((:.) h f) Source | |
Eq (g (f a)) => Eq ((:.) g f a) Source | |
Show (g (f a)) => Show ((:.) g f a) Source |
convO :: Functor g => (b :<->: g c) -> (c :<->: f a) -> b :<->: (g :. f) a Source
Compose a bijection, Functor style
coconvO :: ContraFunctor g => (b :<->: g c) -> (c :<->: f a) -> b :<->: (g :. f) a Source
Compose a bijection, ContraFunctor style
inO :: (g (f a) -> g' (f' a')) -> (g :. f) a -> (g' :. f') a' Source
Apply a unary function within the O
constructor.
inO2 :: (g (f a) -> g' (f' a') -> g'' (f'' a'')) -> (g :. f) a -> (g' :. f') a' -> (g'' :. f'') a'' Source
Apply a binary function within the O
constructor.
inO3 :: (g (f a) -> g' (f' a') -> g'' (f'' a'') -> g''' (f''' a''')) -> (g :. f) a -> (g' :. f') a' -> (g'' :. f'') a'' -> (g''' :. f''') a''' Source
Apply a ternary function within the O
constructor.
oLiftA2 :: Applicative g'' => (f a -> f' a' -> f'' a'') -> (g'' :. f) a -> (g'' :. f') a' -> (g'' :. f'') a'' Source
oLiftA3 :: Applicative g''' => (f a -> f' a' -> f'' a'' -> f''' a''') -> (g''' :. f) a -> (g''' :. f') a' -> (g''' :. f'') a'' -> (g''' :. f''') a''' Source
fmapFF :: (Functor g, Functor f) => (a -> b) -> (g :. f) a -> (g :. f) b Source
Used for the Functor :. Functor
instance of Functor
fmapCC :: (ContraFunctor g, ContraFunctor f) => (a -> b) -> (g :. f) a -> (g :. f) b Source
Used for the ContraFunctor :. ContraFunctor
instance of Functor
contraFmapFC :: (Functor g, ContraFunctor f) => (b -> a) -> (g :. f) a -> (g :. f) b Source
Used for the Functor :. ContraFunctor
instance of Functor
contraFmapCF :: (ContraFunctor g, Functor f) => (b -> a) -> (g :. f) a -> (g :. f) b Source
Used for the ContraFunctor :. Functor
instance of Functor
class DistribM m n where Source
Monad distributivity.
TODO: what conditions are required so that (m :. n)
satisfies the monad laws?
joinDistribM :: (Monad m, Monad n, DistribM m n) => (m :. n) ((m :. n) a) -> (m :. n) a Source
A candidate join
for (m :. n)
bindDistribM :: (Functor m, Functor n, Monad m, Monad n, DistribM m n) => (m :. n) a -> (a -> (m :. n) b) -> (m :. n) b Source
A candidate '(>>=)' for (m :. n)
returnDistribM :: (Monad m, Monad n) => a -> (m :. n) a Source
joinMMT :: (Monad m, Monad n, Traversable n, Applicative m) => m (n (m (n a))) -> m (n a) Source
join
-like function for implicitly composed monads
joinComposeT :: (Monad m, Monad n, Traversable n, Applicative m) => (m :. n) ((m :. n) a) -> (m :. n) a Source
join
-like function for explicitly composed monads
Type composition
Unary/binary
Composition of type constructors: unary with binary. Called StaticArrow in [1].
Instances
(Applicative f, Category * cat) => Category * (OO f cat) Source | |
(Applicative f, Arrow arr) => Arrow (OO f arr) Source |
(->)/unary
Common pattern for Arrow
s.
inFunA :: ((h a -> h b) -> h' a' -> h' b') -> FunA h a b -> FunA h' a' b' Source
Apply unary function in side a FunA
representation.
inFunA2 :: ((h a -> h b) -> (h' a' -> h' b') -> h'' a'' -> h'' b'') -> FunA h a b -> FunA h' a' b' -> FunA h'' a'' b'' Source
Apply binary function in side a FunA
representation.
Methods
Arguments
:: (a -> b) | |
-> h a -> h b | for |
firstFun :: (h a -> h a') -> h (a, b) -> h (a', b) Source
secondFun :: (h b -> h b') -> h (a, b) -> h (a, b') Source
(***%) :: (h a -> h b) -> (h a' -> h b') -> h (a, a') -> h (b, b') Source
(&&&%) :: (h a -> h b) -> (h a -> h b') -> h a -> h (b, b') Source
Monoid constructors
Simulates universal constraint forall a. Monoid (f a)
.
See Simulating Quantified Class Constraints (http://flint.cs.yale.edu/trifonov/papers/sqcc.pdf) Instantiate this schema wherever necessary:
instance Monoid_f f where { mempty_f = mempty ; mappend_f = mappend }
Flip a binary constructor's type arguments
Flip type arguments
Instances
ToOI OI Source | |
Lambda IO OI Source | |
Applicative f => Lambda f ((:.) (Flip (->) o) f) Source | |
Applicative f => Lambda f ((:.) f (Flip (->) o)) Source | |
Lambda Id (Flip (->) o) Source | |
Monoid o => Monoid_f (Flip (->) o) Source | |
Arrow arr => ContraFunctor (Flip arr b) Source | |
Title o => Title_f (Flip (->) o) Source | |
Arrow j => Copair (Flip j o) Source | |
(Arrow j, Monoid_f (Flip j o)) => Pair (Flip j o) Source | |
Arrow j => Cozip (Flip j o) Source | |
(Arrow j, Monoid_f (Flip j o)) => Zip (Flip j o) Source | |
(Applicative (j a), Monoid o) => Monoid (Flip j o a) Source |
inFlip2 :: ((a `j` b) -> (a' `k` b') -> a'' `l` b'') -> Flip j b a -> Flip k b' a' -> Flip l b'' a'' Source
inFlip3 :: ((a `j` b) -> (a' `k` b') -> (a'' `l` b'') -> a''' `m` b''') -> Flip j b a -> Flip k b' a' -> Flip l b'' a'' -> Flip m b''' a''' Source
Convert to an OI
.
Type application
newtype f :$ a infixr 0 Source
Type application
We can also drop the App
constructor, but then we overlap with many
other instances, like [a]
. Here's a template for App
-free
instances.
instance (Applicative f, Monoid a) => Monoid (f a) where mempty = pure mempty mappend = liftA2 mappend
Instances
(Applicative f, Monoid m) => Monoid (App f m) Source |
Identity
Identity type constructor. Until there's a better place to find it. I'd use Control.Monad.Identity, but I don't want to introduce a dependency on mtl just for Id.
Constructors
Id a |
Constructor pairing
Unary
newtype (f :*: g) a infixl 7 Source
Pairing of unary type constructors
Instances
(Functor f, Functor g) => Functor ((:*:) f g) Source | |
(Applicative f, Applicative g) => Applicative ((:*:) f g) Source | |
(Monoid_f f, Monoid_f g) => Monoid_f ((:*:) f g) Source | |
(Copair f, Copair g) => Copair ((:*:) f g) Source | |
(Pair f, Pair g) => Pair ((:*:) f g) Source | |
(Cozip f, Cozip g) => Cozip ((:*:) f g) Source | |
(Zip f, Zip g) => Zip ((:*:) f g) Source | |
(Lambda src snk, Lambda dom' ran') => Lambda ((:*:) src dom') ((:*:) snk ran') Source | |
Eq (f a, g a) => Eq ((:*:) f g a) Source | |
Ord (f a, g a) => Ord ((:*:) f g a) Source | |
Show (f a, g a) => Show ((:*:) f g a) Source |
(***#) :: (a -> b -> c) -> (a' -> b' -> c') -> (a, a') -> (b, b') -> (c, c') infixr 3 Source
Combine two binary functions into a binary function on pairs
($*) :: (a -> b, a' -> b') -> (a, a') -> (b, b') infixl 0 Source
A handy combining form. See '(***#)' for an sample use.
inProd :: ((f a, g a) -> (f' a', g' a')) -> (f :*: g) a -> (f' :*: g') a' Source
Apply unary function inside of f :*: g
representation.
inProd2 :: ((f a, g a) -> (f' a', g' a') -> (f'' a'', g'' a'')) -> (f :*: g) a -> (f' :*: g') a' -> (f'' :*: g'') a'' Source
Apply binary function inside of f :*: g
representation.
inProd3 :: ((f a, g a) -> (f' a', g' a') -> (f'' a'', g'' a'') -> (f''' a''', g''' a''')) -> (f :*: g) a -> (f' :*: g') a' -> (f'' :*: g'') a'' -> (f''' :*: g''') a''' Source
Apply ternary function inside of f :*: g
representation.
Binary
inProdd :: ((f a b, g a b) -> (f' a' b', g' a' b')) -> (f ::*:: g) a b -> (f' ::*:: g') a' b' Source
Apply binary function inside of f :*: g
representation.
inProdd2 :: ((f a b, g a b) -> (f' a' b', g' a' b') -> (f'' a'' b'', g'' a'' b'')) -> (f ::*:: g) a b -> (f' ::*:: g') a' b' -> (f'' ::*:: g'') a'' b'' Source
Apply binary function inside of f :*: g
representation.
Arrow between two constructor applications
Arrow-like type between type constructors (doesn't enforce Arrow
(~>)
here).
Instances
Applicative f => Lambda f ((:->:) f (Const o)) Source | |
(Arrow j, ContraFunctor f, Functor g) => Functor (Arrw j f g) Source | |
(Arrow j, Functor f, ContraFunctor g) => ContraFunctor (Arrw j f g) Source | |
(Arrow j, Unpair f, Pair g) => Pair (Arrw j f g) Source | |
(Arrow j, Unzip f, Zip g) => Zip (Arrw j f g) Source | |
(Arrow j, Unlambda f f', Lambda g g') => Lambda (Arrw j f g) (Arrw j f' g') Source | |
Monoid (j (f a) (g a)) => Monoid (Arrw j f g a) Source |
inArrw :: ((f a `j` g a) -> f' a' `j` g' a') -> Arrw j f g a -> Arrw j f' g' a' Source
Apply unary function inside of Arrw
representation.
inArrw2 :: ((f a `j` g a) -> (f' a' `j` g' a') -> f'' a'' `j` g'' a'') -> Arrw j f g a -> Arrw j f' g' a' -> Arrw j f'' g'' a'' Source
Apply binary function inside of Arrw j f g
representation.
inArrw3 :: ((f a `j` g a) -> (f' a' `j` g' a') -> (f'' a'' `j` g'' a'') -> f''' a''' `j` g''' a''') -> Arrw j f g a -> Arrw j f' g' a' -> Arrw j f'' g'' a'' -> Arrw j f''' g''' a''' Source
Apply ternary function inside of Arrw j f g
representation.