Hask的类别适合哪些值? [英] Where do values fit in Category of Hask?

查看:116
本文介绍了Hask的类别适合哪些值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


$ b

  • 类型是类别的对象

  • 函数是类中从对象到态射的态射。


    类似于 Functor $ b


    • 一个类型构造函数,用于将对象从一个类别映射到另一个类别
    • fmap 用于将态射从一个类别映射到另一个类别。



    现在,当我们编写程序时,我们基本上转换了值(而不是类型),看起来Hask的类别根本不谈论值。我尝试在整个方程中拟合数值,并得出以下观察结果:


    • 每个类型本身就是一个类别。例如:Int是所有整数的类别。

    • 从一个值到另一个相同类型值的函数是类别的态射。例如: Int - > Int

    • 函数从一个值到另一个值不同类型是用于将一个类型的值映射到另一个类型的函子。 >


    现在我的问题是:在哈斯克范畴(或一般范畴理论)中,值是否有意义?如果是,那么任何引用来阅读它或如果没有,那么任何理由。



    我希望这个问题是有道理的:)

    解决方案

    (除非我将标记为代码

    一个类别一次



    类别理论的一个重要思想是将大型复杂事物视为一个点,所以,当你想到Hask类时,真正构成所有整数的set / group / ring / class / category被认为是单一点。类似地,你可以在整数上有一个非常复杂的函数,但它只是一个集合(集合/类)态射的单个元素(点/箭头)。

    你在分类理论中首先要做的是忽略细节。所以Hask这个类别并不在乎Int可以被认为是一个类别 - 这是一个不同的级别。 Int只是Hask中的一个点(对象)。



    一级关闭



    每个monoid都是一个类一个对象。让我们使用它。



    整数是一个类别吗?



    有多个答案整数是加法下的幺半群和乘法下的幺半群)。让我们做加法:

    您可以将整数视为一个单个对象的范畴,而态射是诸如(+1),(+2),(减去4)。

    你必须坚持认为我将整数7视为数字7,但使用表示(+7)使其看起来像一个类别。分类理论的定律故意不要说你的态射必须是函数,但如果它具有包含身份和在合成下关闭的一组函数的结构,那么它就更清楚了。



    任何monoid都以与我们刚刚完成的整数相同的方式创建单个对象类别。



    < h2>来自整数的函数?

    函数 f 来自整数作为< + ,对于其他类型的操作£形成一个类别只能是一个仿函数,如果您有 f(x + y)= f(x)£f(y)。 (这被称为幺半同态)。大多数函数不是态射。

    示例态度



    字符串 s是一个monoid在 ++ 下,所以它们是一个类别。

      len :: String  - > Int 
    len =长度

    len String 到 Int 的单态射线,因为 len(xs ++ ys) = len xs + len ys ,所以如果你正在考虑( String ++ )和( Int + )作为类别, len 是一个仿函数。
    $ b

    示例非态射



    Bool || )是一个monoid,以 False 作为标识,所以它是一个单一对象类别。函数

      quiteLong :: String  - > Bool 
    quiteLong xs =长度xs> 10

    不是态射,因为 quiteLongHello False quiteLongthere!也是 False ,但 quiteLong(Hello++there!) True False || False 不是 True 。



    因为 quiteLong 不是一个态射,它也不是一个函子。



    你有什么意见,安德鲁?



    我的观点是一些 Haskell类型可以被视为类别,但并不是它们之间的所有函数都是morhpisms。



    我们不会同时考虑不同级别的类别(除非您将这两个类别用于某种奇怪的目的),并且故意没有理论上的相互作用之间的水平,因为故意没有对象和态射的细节。这部分是因为类别理论在数学中起了一定的作用,它提供了一种语言来描述伽罗瓦理论在有限群/子群和场/场扩展之间的可爱交互作用,其中两个显然 >完全不同的结构,结果是密切相关的。后来,同源/同伦理论在拓扑空间和组之间产生了函数,这两个函数变得既迷人又有用,但重点是允许对象和态射在两类函数中彼此非常不同。



    (通常,类别理论以Hask的形式出现在Haskell中,所以在函数式编程的实践中,这两个类别是相同的。)



    那么...原始问题的答案是什么?





    • 每个类型本身就是一个类别。例如:Int是一个包含所有整数的类别。




    • 从一个值到另一个相同类型值的函数是该类别的态射。例如: Int - > Int


    我认为你混淆了两个层次。函数可以是Hask中的态射,但不是所有函数 Int - > Int 是加法结构下的Functors,例如 fx = 2 * x + 10 不是Int和Int之间的函子,所以它不是( Int + )到(>)的类别态射(另一种说法是函子的方式) Int + ),但它是一个态射体 Int - > Int 在类别Hask中。



    • 函数从一个值到另一个值不同的类型是用于将一个类型的值映射到另一个类型的函子。

    不,所有函数都不是仿函数,例如 quiteLong 不是。


    值是否合理Hask的范畴(或者一般的范畴论)?

    类别在类别理论中没有价值,它们只有对象和态射,其被视为顶点和有向边缘。对象不一定有价值观,价值观不属于类别理论。


    So we have Category of Hask, where:

    • Types are the objects of the category
    • Functions are the morphisms from object to object in the category.

    Similarly for Functor we have:

    • a Type constructor as the mapping of objects from one category to another
    • fmap for the mapping of morphisms from one category to another.

    Now, when we write program we basically transform values (not types) and it seems that the Category of Hask doesn't talk about values at all. I tried to fit values in the whole equation and came up with the following observation:

    • Each Type is a category itself. Ex: Int is a category of all integers.
    • Functions from a value to another value of same type are morphism of the category. Eg: Int -> Int
    • Functions from one value to another value of different type are functor for mapping values of one type to another.

    Now my question is - Do values even make sense in the Category of Hask (or in general category theory)? If yes then any reference to read about it OR if not, then any reason for that.

    I hope the question makes sense :)

    解决方案

    (I'll use words with their meaning from maths/category theory rather than programming, unless I mark it as code.)

    One category at a time

    One of the big ideas of category theory is to treat large complex things as a point, so, true to form the set/group/ring/class/category of all integers is considered a single point when you're thinking of the category Hask.

    Similarly, you could have a very complicated function on integers, but it's just considered a single element (point/arrow) of a collection (set/class) of morphisms.

    The first thing you do in category theory is ignore the detail. So the category Hask doesn't care that Int can be considered a category - that's at a different level. Int is just a point (object) in Hask.

    One level down

    Every monoid is a category with one object. Let's use that.

    How are the integers a category?

    There's more than one answer to this (since the integers are a monoid under addition and a monoid under multiplication). Let's do addition:

    You can consider the integers as a category with a single object, and the morphisms are functions such as (+1), (+2), (subtract 4).

    You have to hold in your head that I'm considering the integer 7 as the number 7 but using the representation (+7) to make it appear to be a category. The laws of category theory deliberately don't say your morphisms have to be functions, but it's clearer that something's a category if it has the structure of a set of functions containing identity and closed under composition.

    Any monoid makes a single-object category in the same way as we've just done with the integers.

    Functors from the integers?

    A function f from the integers as a category under the operation +, to some other type with an operation £ that forms a category could only be a functor if you had f(x+y) = f(x) £ f(y). (This is called a monoid homomorphism). Most functions aren't morphisms.

    Example morphism

    Strings are a monoid under ++, so they're a category.

    len :: String -> Int
    len = length
    

    len is a monoid morphism from String to Int, because len (xs ++ ys) = len xs + len ys, so if you're considering (String,++) and (Int,+) as category, len is a functor.

    Example non-morphism

    (Bool,||) is a monoid, with False as the identity, so it's a one-object category. The function

    quiteLong :: String -> Bool
    quiteLong xs = length xs > 10
    

    isn't a morphism because quiteLong "Hello " is False and quiteLong "there!" is also False, but quiteLong ("Hello " ++ "there!") is True, and False || False is not True.

    Because quiteLong isn't a morphism, it's not a functor either.

    What's your point, Andrew?

    My point is that some Haskell types can be considered categories, but not all functions between them are morhpisms.

    We don't think of the categories at different levels at the same time (unless you're using both categories for some weird purpose), and there's deliberately no theoretical interaction between the levels, because there's deliberately no detail on the objects and morphisms.

    This is partly because category theory took off in maths to provide a language to describe Galois theory's lovely interaction between finite groups/subgroups and fields/field extensions, two apparently completely different structures that turn out to be closely related. Later, homology/homotopy theory made functors between topological spaces and groups that turn out to be both fascinating and useful, but the main point is that the objects and the morphisms are allowed to be very different to each other in the two categories of a functor.

    (Normally category theory comes into Haskell in the form of a functor from Hask to Hask, so in practice in functional programming the two categories are the same.)

    So... what exactly is the answer to the original question?

    • Each Type is a category itself. Ex: Int is a category of all integers.

    If you think of them in particular ways. See PhilipJF's answer for details.

    • Functions from a value to another value of same type are morphism of the category. Eg: Int -> Int

    I think you mixed up the two levels. Functions can be morphisms in Hask, but not all functions Int -> Int are Functors under the addition structure, for example f x = 2 * x + 10 isn't a functor between Int and Int, so it's not a category morphism (another way of saying functor) from (Int,+) to (Int,+) but it is a morphism Int -> Int in the category Hask.

    • Functions from one value to another value of different type are functor for mapping values of one type to another.

    No, not all functions are functors, for example quiteLong isn't.

    Do values even make sense in the Category of Hask (or in general category theory)?

    Categories don't have values in category theory, they just have objects and morphisms, which are treated like vertices and directed edges. The objects don't have to have values, and values aren't part of category theory.

    这篇关于Hask的类别适合哪些值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆