category-abstractions

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

一个人想着一个人 提交于 2019-12-22 05:15:09
问题 Lets say I have the following import Control.Category (Category, (.), id) data Invertible a b = Invertible (a -> b) (b -> a) instance Category Invertible where 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 that the following is true: invert (g . f) == invert f . invert g This structure seems very similar to a contravariant functor (wikipedia), as it follows the same axiom

Generalising ($) like Control.Category generalises (.)

大兔子大兔子 提交于 2019-12-08 21:22:20
问题 I had a thought to generalise ($) like Control.Category generalises (.) , and I've done so with the code at the end of this post (also ideone). In this code I've created a class called FunctionObject . This class has a function ($) with the following signature: ($) :: f a b -> a -> b Naturally I make (->) an instance of this class so $ continues to work with ordinary functions. But this allows you to make special functions that, for example, know their own inverse, as the example below shows.

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

你说的曾经没有我的故事 提交于 2019-12-05 05:58:24
Lets say I have the following import Control.Category (Category, (.), id) data Invertible a b = Invertible (a -> b) (b -> a) instance Category Invertible where 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 that the following is true: invert (g . f) == invert f . invert g This structure seems very similar to a contravariant functor (wikipedia) , as it follows the same axiom: F(g . f) = F(f) . F(g) In my case, F is simply invert . I looked at Data.Functor.Contravariant