Description
Subcategories induced by class constraints.
Description
Please see the README on GitHub at https://github.com/konn/subcategories#readme
README.md
The subcategories
package
This package provides variants of functor-like structures, with domain types are constrained. In particular, this package provides an abstraction for functorial containers, which can be expressed as a functor from a full-subcategory of Hask to Hask itself [^1].
For example:
- We can treat
Set
as if it is aFuctor
,Foldable
,Applicative
, with their domain restricted to full-subcategory Ord ofOrd
instances of Hask. - For
MonoFoldable
orMonoTraversable
types (frommono-traversable
package), we provideWrapMono
wrapper with zero-cost coercion. Suchmono
s can be regarded as a functorial structure from the full subcategory consisting of just a single object, sayElement mono
.
[^1]: Strictly speaking, CFoldable
, a constrained counterpart of Foldable
, doesn't require a functoriality as with the original Foldable
.
Optimisation
This library is designed to keep the abstraction runtime overhead as minimum as possible.
Some notes:
- If a constrained term such as
cmap
orczipWith
has concrete type, it must have exactly the same representation as the corresponding operation modulo (zero-cost) coercion.- The same still holds if the set of required constraints coincides.
- Although the constructor of
WrapMono mono a
is hidden, its just anewtype
-wrapper aroundmono
; hence, constrained operators must have the same representations as the corresponding combinators inmono-traversable
package.
- OTOH, for a polymorphic term, like
cmap :: (Ord a, Ord b) => (a -> b) Set a -> Set b
andSet.map
, they can have different representations; indeed,Set.map
doesn't requirea
to beOrd
-instance and therefore the implementation ofcmap
discards the dictionary forOrd a
to callSet.map
.