Safe Haskell | None |
---|---|
Language | Haskell2010 |
- class PolyFunctor t where
- type family PolyFunctorConstraint (c :: * -> Constraint) t :: Constraint
Documentation
class PolyFunctor t where Source #
A very generic class for a map function on heterogeneous data structures (i.e. those with differing types).
This allows you to do things like:
>>>
hmap triple (3 :: Int, 4.5 :: Float)
(9 :: Int, 13.5 :: Float)
hmap
takes as it's function a Poly
, as of course you'd want a polymorphic function.
The return type defined in the class is very vague, indeed it's just t
to be detailed in the instances,
because unlike a normal map
function, how hmap
changes the type depends a lot on the type it's applied to,
there's no simple f a -> f b
.
Currently only instances are defined are for 2 and 3 tuples, nag me if you want larger ones.
It's worth noting how the instances are defined, for example, for the 2 tuples, there are 3 instances defined.
This is primarily to help type inference. We don't know too much about the types hmap
will produce, but we do know,
if we feed hmap
a pair, we should get a pair back. Likewise, if the result of hmap
is a pair, then the input
should be a pair.
So we provide both instances where the input is a pair, and when the output is a pair. In both of these instances, we then in the constraints section (which happens after instance selection) ensure the other argument is also a pair.
The "know both are pairs already" case just needs to be added as a specific overlapping instance so the compiler has a most specific match when it already knows both input and output are pairs.
hmap :: forall c. PolyFunctorConstraint c t => Poly c -> t Source #
PolyFunctor ((a0, a1) -> (b0, b1)) Source # | |
IsTuple 2 b => PolyFunctor ((a0, a1) -> b) Source # | |
PolyFunctor ((a0, a1, a2) -> (b0, b1, b2)) Source # | |
IsTuple 3 b => PolyFunctor ((a0, a1, a2) -> b) Source # | |
IsTuple 3 a => PolyFunctor (a -> (b0, b1, b2)) Source # | |
IsTuple 2 a => PolyFunctor (a -> (b0, b1)) Source # | |
type family PolyFunctorConstraint (c :: * -> Constraint) t :: Constraint Source #
type PolyFunctorConstraint c ((a0, a1, a2) -> (b0, b1, b2)) Source # | |
type PolyFunctorConstraint c ((a0, a1) -> (b0, b1)) Source # | |