Stability | experimental |
---|---|
Maintainer | conal@conal.net |
Standard building blocks for functors
- newtype Const a b = Const {
- getConst :: a
- data Void a
- type Unit = Const ()
- newtype Id a = Id a
- unId :: Id a -> a
- inId :: (a -> b) -> Id a -> Id b
- inId2 :: (a -> b -> c) -> Id a -> Id b -> Id c
- data (f :+: g) a
- eitherF :: (f a -> b) -> (g a -> b) -> (f :+: g) a -> b
- data (f :*: g) a = (f a) :*: (g a)
- newtype (g :. f) a = O (g (f a))
- unO :: :. g f a -> 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''
- (~>) :: (a' -> a) -> (b -> b') -> (a -> b) -> a' -> b'
Documentation
newtype Const a b
newtype Id a
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.
Id a |
Sum on unary type constructors
Product on unary type constructors
(f a) :*: (g a) |
newtype (g :. f) a
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
Cofunctor
instances.
instance ( Functor g, Functor f) => Functor (g :. f) where fmap = fmapFF instance (Cofunctor g, Cofunctor f) => Functor (g :. f) where fmap = fmapCC instance (Functor g, Cofunctor f) => Cofunctor (g :. f) where cofmap = cofmapFC instance (Cofunctor g, Functor f) => Cofunctor (g :. f) where cofmap = cofmapCF
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.
O (g (f a)) |
(Functor g, Functor f) => Functor (:. g f) | |
(Applicative g, Applicative f) => Applicative (:. g f) | |
(Foldable g, Foldable f, Functor g) => Foldable (:. g f) | |
(Traversable g, Traversable f) => Traversable (:. g f) | |
(Holey f, Holey g) => Holey (:. g f) | |
(Holey f, Holey g) => Holey (:. g f) | |
Show (g (f a)) => Show (:. g f a) | |
HasTrie (g (f a)) => HasTrie (:. g f a) |
inO :: (g (f a) -> g' (f' a')) -> :. g f a -> :. g' f' a'
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''
Apply a binary function within the O
constructor.
(~>) :: (a' -> a) -> (b -> b') -> (a -> b) -> a' -> b'
Add pre- and post processing