【发布时间】: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
范畴理论意义?因为它显然(?)不是自然的转变”。
【问题讨论】: