haskell - Typeclass for (what seems to be) a contravariant functor implementing function inversion -
lets have following
import control.category (category, (.), id) data invertible b = invertible (a -> b) (b -> a) instance category invertible id = invertible prelude.id prelude.id (invertible f f') . (invertible g g') = invertible (f prelude.. g) (g' prelude.. f') invert (invertible x y) = invertible y x
note following true:
invert (g . f) == invert f . invert g
this structure seems similar contravariant functor (wikipedia), follows same axiom:
f(g . f) = f(f) . f(g)
in case, f
invert
.
i looked @ data.functor.contravariant.contramap, has function of type:
(a -> b) -> f b -> f
but didn't know how'd i'd implement in situation. example, can't work out sensible choice f
, , in situation, there's no function a -> b
, invert
.
however, invert
nevertheless fits mathematical axiom of contravariant functor, i'm thinking can fit existing class, can't find 1 , how it. or pointers appreciated.
a category has 2 collections: objects , morphisms.
the usual haskell prelude, , appears classes in data.functor.contravariant
, operate on narrow category, category types objects , functions morphisms, denoted hask. standard functor
class narrow: represent endofunctors on hask: must take types types , functions functions.
take example functor maybe
. way maybe
acts on types takes types a
maybe a
. maybe
maps int
maybe int
, on (i know sounds bit trivial). morphisms encoded fmap
: fmap
takes f :: (a -> b)
, morphism between 2 objects in hask, , maps fmap f :: (maybe -> maybe b)
, morphism in hask between objects functor maps to. in haskell not define functor
takes e.g. int
char
-- haskell functor
s have type constructors -- in general category theory could.
control.category
generalizes little bit: objects of control.category
category c
still types[1] in hask, morphisms things of type c b
. in example, objects still arbitrary types, morphisms things of type invertible b
. since morphisms not functions, not able use standard functor
classes.
however, it's fun exercise in building category theory knowhow define functor class operates between category
categories rather assuming hask, capture example. remember, functor acts on objects (types) and morphisms.
i'll leave -- feel free comment if more guidance.
[1] ignoring polykinds
, makes bit more general.
Comments
Post a Comment