【问题标题】:Are the "natural transformations" we apply on Coyoneda to get a Functor actually "natural transformations"?我们在 Coyoneda 上应用以获得 Functor 的“自然变换”实际上是“自然变换”吗?
【发布时间】:2015-08-01 06:50:39
【问题描述】:

我有一个关于在 很多解释 Coyoneda 引理的例子。它们通常被称为 作为“自然变换”,据我所知,函子之间的映射。 令我困惑的是,在这些示例中,它们有时会从 Set 映射到某个函子 F。所以它看起来并不是函子之间的映射,而是更轻松一些。

这里是有问题的代码:

{-# LANGUAGE GADTs #-}
{-# LANGUAGE RankNTypes #-}
module Coyo where

import           Data.Set (Set)
import qualified Data.Set as Set

data Coyoneda f a where
  Coyoneda :: (b -> a) -> f b -> Coyoneda f a

instance Functor (Coyoneda f) where
  fmap f (Coyoneda c fa) =  Coyoneda (f . c) fa

set :: Set Int
set = Set.fromList [1,2,3,4]

lift :: f a -> Coyoneda f a
lift fa = Coyoneda id fa

lower :: Functor f => Coyoneda f a -> f a
lower (Coyoneda f fa) = fmap f fa

type NatT f g = forall a. f a -> g a

coyoset :: Coyoneda Set Int
coyoset = fmap (+1) (lift set)

applyNatT :: NatT f g -> Coyoneda f a -> Coyoneda g a
applyNatT n (Coyoneda f fa) = Coyoneda f (n fa)

-- Set.toList is used as a "natural transformation" here
-- while it conforms to the type signature of NatT, it
-- is not a mapping between functors `f` and `g` since
-- `Set` is not a functor.
run :: [Int]
run = lower (applyNatT Set.toList coyoset)

我在这里误会了什么?

编辑:在 freenode 中讨论了#haskell 之后,我想我需要稍微澄清一下我的问题。基本上是:“什么是Set.toList 范畴理论意义?因为它显然(?)不是自然的转变”。

【问题讨论】:

    标签: haskell category-theory


    【解决方案1】:

    n 要成为 Haskell 中的自然转换,它需要遵守(对于所有 f

    (fmap f) . n == n . (fmap f)
    

    Set.toList 不是这种情况。

    fmap (const 0) . Set.toList        $ Set.fromList [1, 2, 3] = [0, 0, 0]
    Set.toList     . Set.map (const 0) $ Set.fromList [1, 2, 3] = [0]
    

    相反,它遵循一套不同的法律。还有另一个转换 n' 以另一种方式返回,使得以下内容成立

    n' . (fmap f) . n == fmap f
    

    如果我们选择f = id 并应用函子定律fmap id == id,我们可以看到这意味着n' . n == id,因此我们有一个类似的公式:

    (fmap f) . n' . n == n' . (fmap f) . n == n' . n . (fmap f)
    

    n = Set.toListn' = Set.fromList 遵守这条法律。

    Set.map (const 0) . Set.fromList   . Set.toList        $ Set.fromList [1, 2, 3] = fromList [0]
    Set.fromList      . fmap (const 0) . Set.toList        $ Set.fromList [1, 2, 3] = fromList [0]
    Set.fromList      . Set.toList     . Set.map (const 0) $ Set.fromList [1, 2, 3] = fromList [0]
    

    除了观察到Set 是列表的等价类外,我不知道我们可以称之为什么。 Set.toList 查找等价类的代表成员,Set.fromList 是商。

    可能值得注意的是Set.fromList 是一种自然转换。至少它在 Hask 的合理子类别中,其中a == b 暗示f a == f b(这里== 是来自Eq 的平等)。这也是 Hask 的子类别,其中Set 是函子;它不包括degenerate things like this

    leftaroundabout 还指出,Set.toListHask 子类别的自然变换,其中态射仅限于 injective functions where f a == f b implies a == b

    【讨论】:

    • 在单射子范畴上,即使toList 也是一种自然变换。
    • 感谢您的回答!那么n'n 在这里形成一个部分/撤回对?
    • @raichoo 是的,nn' 是整个系列的部分/撤回对,因为 n' . n == id。并非所有部分/撤回对都会遵守更严格的法律n' . fmap f . n == fmap f
    猜你喜欢
    • 1970-01-01
    • 2021-02-13
    • 1970-01-01
    • 2018-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-09
    • 1970-01-01
    相关资源
    最近更新 更多