TypeCompose-0.9.9: Type composition classes & instances

Portabilitysee LANGUAGE pragma
Stabilityexperimental
Maintainerconal@conal.net
Safe HaskellNone

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

Synopsis

Value transformers

type Unop a = a -> aSource

Unary functions

type Binop a = a -> a -> aSource

Binary functions

Specialized semantic editor combinators

result :: Category cat => (b `cat` b') -> (a `cat` b) -> a `cat` b'Source

Add post-processing

argument :: Category cat => (a' `cat` a) -> (a `cat` b) -> a' `cat` bSource

Add pre-processing argument :: (a' -> a) -> ((a -> b) -> (a' -> b))

(~>) :: Category cat => (a' `cat` a) -> (b `cat` b') -> (a `cat` b) -> a' `cat` b'Source

Add pre- and post processing

(~>*) :: (Functor p, Functor q) => (a' -> a) -> (b -> b') -> (p a -> q b) -> p a' -> q b'Source

Like '(~>)' but specialized to functors and functions.

(<~) :: Category cat => (b `cat` b') -> (a' `cat` a) -> (a `cat` b) -> a' `cat` b'Source

(*<~) :: (Functor p, Functor q) => (b -> b') -> (a' -> a) -> (p a -> q b) -> p a' -> q b'Source

Contravariant functors

class ContraFunctor h whereSource

Contravariant functors. often useful for acceptors (consumers, sinks) of values.

Methods

contraFmap :: (a -> b) -> h b -> h aSource

Instances

Arrow arr => ContraFunctor (Flip arr b) 
(Arrow j, Functor f, ContraFunctor g) => ContraFunctor (Arrw j f g) 

bicomap :: ContraFunctor f => (a :<->: b) -> f a :<->: f bSource

Bijections on contravariant functors

Unary/unary composition

newtype (g :. f) a 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) 
Applicative f => Lambda f (:. f (Flip (->) o)) 
(Functor g, Functor f) => Functor (:. g f) 
(Functor (:. g f), Applicative g, Applicative f) => Applicative (:. g f) 
(Foldable g, Foldable f, Functor g) => Foldable (:. g f) 
(Functor (:. g f), Foldable (:. g f), Traversable g, Traversable f) => Traversable (:. g f) 
Title_f g => Title_f (:. g f) 
(Functor h, Copair f) => Copair (:. h f) 
(Functor h, Cozip f) => Cozip (:. h f) 
Eq (g (f a)) => Eq (:. g f a) 
Show (g (f a)) => Show (:. g f a) 

type O = :.Source

Compatibility synonym

unO :: (g :. f) a -> g (f a)Source

Unwrap a '(:.)'.

biO :: g (f a) :<->: (g :. f) aSource

newtype bijection

convO :: Functor g => (b :<->: g c) -> (c :<->: f a) -> b :<->: (g :. f) aSource

Compose a bijection, Functor style

coconvO :: ContraFunctor g => (b :<->: g c) -> (c :<->: f a) -> b :<->: (g :. f) aSource

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.

oPure :: Applicative g => f a -> (g :. f) aSource

Handy combination of O and pure.

oFmap :: Functor g' => (f a -> f' a') -> (g' :. f) a -> (g' :. f') a'Source

Handy combination of inO and fmap.

oLiftA2 :: Applicative g'' => (f a -> f' a' -> f'' a'') -> (g'' :. f) a -> (g'' :. f') a' -> (g'' :. f'') a''Source

Handy combination of inO2 and liftA2.

oLiftA3 :: Applicative g''' => (f a -> f' a' -> f'' a'' -> f''' a''') -> (g''' :. f) a -> (g''' :. f') a' -> (g''' :. f'') a'' -> (g''' :. f''') a'''Source

Handy combination of inO3 and liftA3.

fmapFF :: (Functor g, Functor f) => (a -> b) -> (g :. f) a -> (g :. f) bSource

Used for the Functor :. Functor instance of Functor

fmapCC :: (ContraFunctor g, ContraFunctor f) => (a -> b) -> (g :. f) a -> (g :. f) bSource

Used for the ContraFunctor :. ContraFunctor instance of Functor

contraFmapFC :: (Functor g, ContraFunctor f) => (b -> a) -> (g :. f) a -> (g :. f) bSource

Used for the Functor :. ContraFunctor instance of Functor

contraFmapCF :: (ContraFunctor g, Functor f) => (b -> a) -> (g :. f) a -> (g :. f) bSource

Used for the ContraFunctor :. Functor instance of Functor

class DistribM m n whereSource

Monad distributivity.

TODO: what conditions are required so that (m :. n) satisfies the monad laws?

Methods

distribM :: n (m a) -> m (n a)Source

joinDistribM :: (Monad m, Monad n, DistribM m n) => (m :. n) ((m :. n) a) -> (m :. n) aSource

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) bSource

A candidate '(>>=)' for (m :. n)

returnDistribM :: (Monad m, Monad n) => a -> (m :. n) aSource

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) aSource

join-like function for explicitly composed monads

Type composition

Unary/binary

newtype OO f j a b Source

Composition of type constructors: unary with binary. Called StaticArrow in [1].

Constructors

OO 

Fields

unOO :: f (a `j` b)
 

Instances

(Category (OO f arr), Applicative f, Arrow arr) => Arrow (OO f arr) 
(Applicative f, Category cat) => Category (OO f cat) 

(->)/unary

newtype FunA h a b Source

Common pattern for Arrows.

Constructors

FunA 

Fields

unFunA :: h a -> h b
 

Instances

(Category (FunA h), FunAble h) => Arrow (FunA h) 
FunAble h => Category (FunA h) 

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.

class FunAble h whereSource

Support needed for a FunA to be an Arrow.

Methods

arrFunSource

Arguments

:: (a -> b) 
-> h a -> h b

for arr

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

Instances

Monoid constructors

class Monoid_f m whereSource

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 }

Methods

mempty_f :: forall a. m aSource

mappend_f :: forall a. m a -> m a -> m aSource

Instances

Monoid_f [] 
Monoid_f Endo 
(Monoid_f f, Monoid_f g) => Monoid_f (:*: f g) 
Monoid o => Monoid_f (Flip (->) o) 

Flip a binary constructor's type arguments

newtype Flip j b a Source

Flip type arguments

Constructors

Flip 

Fields

unFlip :: a `j` b
 

Instances

ToOI OI 
Lambda IO OI 
Applicative f => Lambda f (:. (Flip (->) o) f) 
Applicative f => Lambda f (:. f (Flip (->) o)) 
Lambda Id (Flip (->) o) 
Monoid o => Monoid_f (Flip (->) o) 
Arrow arr => ContraFunctor (Flip arr b) 
Title o => Title_f (Flip (->) o) 
Arrow j => Copair (Flip j o) 
(Arrow j, Monoid_f (Flip j o)) => Pair (Flip j o) 
Arrow j => Cozip (Flip j o) 
(Arrow j, Monoid_f (Flip j o)) => Zip (Flip j o) 
(Applicative (j a), Monoid o) => Monoid (Flip j o a) 

biFlip :: (a `j` b) :<->: Flip j b aSource

newtype bijection

inFlip :: ((a `j` b) -> a' `k` b') -> Flip j b a -> Flip k b' 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

type OI = Flip (->) (IO ())Source

(-> IO ()) as a Flip. A ContraFunctor.

class ToOI sink whereSource

Convert to an OI.

Methods

toOI :: sink b -> OI bSource

Instances

Type application

newtype f :$ a 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

Constructors

App 

Fields

unApp :: f a
 

Instances

(Applicative f, Monoid m) => Monoid (App f m) 

type App = :$Source

Compatibility synonym for (:$).

biApp :: f a :<->: App f aSource

newtype bijection

inApp :: (f a -> f' a') -> App f a -> App f' a'Source

inApp2 :: (f a -> f' a' -> f'' a'') -> App f a -> App f' a' -> App f'' a''Source

Identity

newtype Id a Source

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 

unId :: Id a -> aSource

biId :: a :<->: Id aSource

newtype bijection

inId :: (a -> b) -> Id a -> Id bSource

inId2 :: (a -> b -> c) -> Id a -> Id b -> Id cSource

Constructor pairing

Unary

newtype (f :*: g) a Source

Pairing of unary type constructors

Constructors

Prod 

Fields

unProd :: (f a, g a)
 

Instances

(Functor f, Functor g) => Functor (:*: f g) 
(Functor (:*: f g), Applicative f, Applicative g) => Applicative (:*: f g) 
(Monoid_f f, Monoid_f g) => Monoid_f (:*: f g) 
(Copair f, Copair g) => Copair (:*: f g) 
(Pair f, Pair g) => Pair (:*: f g) 
(Cozip f, Cozip g) => Cozip (:*: f g) 
(Zip f, Zip g) => Zip (:*: f g) 
(Lambda src snk, Lambda dom' ran') => Lambda (:*: src dom') (:*: snk ran') 
Eq (f a, g a) => Eq (:*: f g a) 
(Eq (:*: f g a), Ord (f a, g a)) => Ord (:*: f g a) 
Show (f a, g a) => Show (:*: f g a) 

(*:*) :: f a -> g a -> (f :*: g) aSource

Handy infix & curried Prod

biProd :: (f a, g a) :<->: (f :*: g) aSource

newtype bijection

convProd :: (b :<->: f a) -> (c :<->: g a) -> (b, c) :<->: (f :*: g) aSource

Compose a bijection

(***#) :: (a -> b -> c) -> (a' -> b' -> c') -> (a, a') -> (b, b') -> (c, c')Source

Combine two binary functions into a binary function on pairs

($*) :: (a -> b, a' -> b') -> (a, a') -> (b, b')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

newtype (f ::*:: g) a b Source

Pairing of binary type constructors

Constructors

Prodd 

Fields

unProdd :: (f a b, g a b)
 

Instances

(Category (::*:: f f'), Arrow f, Arrow f') => Arrow (::*:: f f') 
(Category f, Category f') => Category (::*:: f f') 
(Eq (f a b), Eq (g a b)) => Eq (::*:: f g a b) 
(Eq (::*:: f g a b), Ord (f a b), Ord (g a b)) => Ord (::*:: f g a b) 
(Show (f a b), Show (g a b)) => Show (::*:: f g a b) 

(*::*) :: f a b -> g a b -> (f ::*:: g) a bSource

Handy infix & curried Prodd

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

newtype Arrw j f g a Source

Arrow-like type between type constructors (doesn't enforce Arrow (~>) here).

Constructors

Arrw 

Fields

unArrw :: f a `j` g a
 

Instances

Applicative f => Lambda f (:->: f (Const o)) 
(Arrow j, ContraFunctor f, Functor g) => Functor (Arrw j f g) 
(Arrow j, Functor f, ContraFunctor g) => ContraFunctor (Arrw j f g) 
(Arrow j, Unpair f, Pair g) => Pair (Arrw j f g) 
(Arrow j, Unzip f, Zip g) => Zip (Arrw j f g) 
(Arrow j, Unlambda f f', Lambda g g') => Lambda (Arrw j f g) (Arrw j f' g') 
Monoid (j (f a) (g a)) => Monoid (Arrw j f g a) 

type :->: = Arrw (->)Source

biFun :: (f a -> g a) :<->: (f :->: g) aSource

newtype bijection

convFun :: (b :<->: f a) -> (c :<->: g a) -> (b -> c) :<->: (f :->: g) aSource

Compose a bijection

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.

Augment other modules

biConst :: a :<->: Const a bSource

newtype bijection

inConst :: (a -> b) -> Const a u -> Const b vSource

inConst2 :: (a -> b -> c) -> Const a u -> Const b v -> Const c wSource

inConst3 :: (a -> b -> c -> d) -> Const a u -> Const b v -> Const c w -> Const d xSource

biEndo :: (a -> a) :<->: Endo aSource

newtype bijection

inEndo :: (Unop a -> Unop a') -> Endo a -> Endo a'Source

Convenience for partial-manipulating functions