类似于(似乎是)实现函数反演的逆变函数 [英] Typeclass for (what seems to be) a contravariant functor implementing function inversion
问题描述
可以说我有以下几种:
import Control.Category(Category,(。),id)
data Invertible ab = Invertible(a - > b)(b - > a)
实例类别Invertible其中
id =可转换的Prelude.id Prelude.id
(Invertible f f')。 (Invertible g g')=
Invertible(f Prelude .. g)(g'Prelude .. f')
invert(Invertible xy)= Invertible yx
请注意,以下情况属实:
invert(g。f)== invert f。转换g
这个结构看起来非常类似于逆变函数(维基百科),因为它遵循相同的公理:
F(g,f)= F(f)。 F(g)
在我的例子中, F
简单地是 invert
。
我看了Data.Functor.Contravariant.contramap ,它具有以下类型的功能:
(a - > b) - > f b - > f a
但我不知道如何在我的情况下执行这个操作。例如,我无法为 f
找出合理的选择,在我的情况下,没有函数 a - > b ,只是
invert
。
然而, invert
仍然符合反函数的数学公理,所以我认为我可以将它应用到某个现有的类中,但我无法找到哪一个以及如何去做。任何帮助或指针,将不胜感激。
一个类别有两个集合:对象和态射。 b
通常的Haskell前奏,看起来 Data.Functor.Contravariant
中的类只对一个非常窄的范畴进行操作,那就是其中类型是对象和函数是态射的类别,通常表示为 Hask 。标准的 Functor
类也非常狭窄:它们只表示 Hask 上的
以函子 我会留下你的 - 如果你有任何意见想要更多指导。 [1]忽略 Lets say I have the following Note that the following is true: This structure seems very similar to a contravariant functor (wikipedia), as it follows the same axiom: In my case, I looked at Data.Functor.Contravariant.contramap, which has a function of the type: But I didn't know how'd I'd implement that in my situation. For example, I can't work out a sensible choice for However, A category has two collections: objects and morphisms. The usual Haskell prelude, and it appears that the classes in Take for example the functor However, it's a fun exercise in building up your category theory knowhow to define a functor class which operates between I'll leave you with that -- feel free to comment if you would like more guidance. [1] Ignoring 这篇关于类似于(似乎是)实现函数反演的逆变函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! Maybe
为例。 Maybe 作用于类型的方式仅仅是它需要类型
a
到也许一个
。
也许地图
Int
到可能是Int
等等(I知道这听起来有点微不足道)。它对态射的作用是由 fmap
编码: fmap
需要 f ::(a - > b)
, Hask 中两个对象之间的态射,并将其映射到 fmap f ::(也许a - >可能b)
,functor映射到的对象之间的 Hask 中的另一种态射。在Haskell中,我们不能定义一个 Functor
,它需要 Int
到 Char
- 所有Haskell Functor
s必须是类型构造函数 - 但是在一般的分类理论中我们可以。
Control.Category
概括一点: $ Control.Category
类别 C
的对象仍然是类型[1],就像 Hask ,但它的态射是 C ab
类型的东西。所以在你的例子中,对象仍然是任意类型的,但是你的态射是 Invertible a b
类型的东西。由于你的态射不是函数,你将无法使用标准的 Functor
类。然而,在建立类别理论专有技术方面有趣的做法是定义一个在 Category
之间运作的函数类,类别而不是假设 Hask ,这将捕获您的示例。请记住,一个函子作用于对象(类型)和态射。
PolyKinds
,这使得这一点更普遍。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
invert (g . f) == invert f . invert g
F(g . f) = F(f) . F(g)
F
is simply invert
.(a -> b) -> f b -> f a
f
, and in my situation, there's no function a -> b
, just invert
.invert
nevertheless fits the mathematical axiom of a contravariant functor, so I'm thinking I can fit this into some existing class, but I just can't find which one and how to do it. Any help or pointers would be appreciated.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.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 Functor
s 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. Category
categories rather than assuming Hask, which would capture your example. Remember, a functor acts on objects (types) and morphisms.
PolyKinds
, which makes this a bit more general.