Typeclass for (what seems to be) a contravariant functor implementing function inversion

你说的曾经没有我的故事 提交于 2019-12-05 05:58:24

A category has two collections: objects and morphisms.

The usual Haskell prelude, and it appears that the classes in Data.Functor.Contravariant, only operate on a very narrow category, that is the category where types are objects and functions are morphisms, usually denoted Hask. The standard Functor class is also very narrow: they only represent endofunctors on Hask: they must take types to types and functions to functions.

Take for example the functor Maybe. The way Maybe acts on types is just that it takes types a to Maybe a. Maybe maps Int to Maybe Int and so on (I know this sounds a bit trivial). What it does to morphisms is encoded by fmap: fmap takes f :: (a -> b), a morphism between two objects in Hask, and maps it to fmap f :: (Maybe a -> Maybe b), another morphism in Hask between the objects that the functor maps to. In Haskell we could not define a Functor which takes e.g. Int to Char -- all Haskell Functors have to be type constructors -- but in general category theory we could.

Control.Category generalizes a little bit: the objects of a Control.Category category C are still types[1] just like in Hask, but its morphisms are things of type C a b. So in your example, the objects are still arbitrary types, but your morphisms are things of type Invertible a b. Since your morphisms are not functions, you will not be able to use the standard Functor classes.

However, it's a fun exercise in building up your category theory knowhow to define a functor class which operates between Category categories rather than assuming Hask, which would capture your example. Remember, a functor acts on objects (types) and morphisms.

I'll leave you with that -- feel free to comment if you would like more guidance.


[1] Ignoring PolyKinds, which makes this a bit more general.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!