我正在通过本教程.如教程中所述,我复制了一些代码,用于表示functor composition和identity functor:
{-# LANGUAGE FlexibleContexts #-}
module Test where
newtype FComp f g a = C { unC :: f (g a) }
instance (Show (f (g a))) => Show (FComp f g a) where
show (C x) = "FComp " ++ show x
instance (Functor f, Functor g) => Functor (FComp f g) where
fmap h (C x) = C (fmap (fmap h) x)
newtype Id a = Identity { unId :: a } deriving Show
instance Functor Id where
fmap f x = Identity (f (unId x))
Run Code Online (Sandbox Code Playgroud)
现在,这是教程所述的内容identity functor:
Composition with the identity functor in the same category is as expected.
F?IdB = F
IdA?F = F
Run Code Online (Sandbox Code Playgroud)
我所坚持的是试图用FComp上面的代码所代表的仿函数组合来考虑它.以下示例:
$ let a = C (Identity (Just (5::Int)))
$ :t a
a :: FComp Id Maybe Int
$ let b = C (Just (Identity (5::Int)))
$ :t b
b :: FComp Maybe Id Int
Run Code Online (Sandbox Code Playgroud)
我不能想办法断言类型的a和b上述的例子中表示为是相同的.我将不胜感激怎么想的指针identity functor来讲functor composition.
就像在Haskell,应用范畴论许多公式,˚F ∘标识乙 ≡标识一个 ∘ ˚F ≡ ˚F应该真的只是被解读为等价.与类型检查器FComp Id Maybe Int非常不一样FComp Maybe Id Int; 但是你可以很容易地写
idFunctorIso :: Functor f => FComp f Id a -> f a
idFunctorIso (C fIdca) = fmap unId fIdca
idFunctorIso' :: Functor f => f a -> FComp f Id a
idFunctorIso' fa = C $ fmap Identity fIdc
Run Code Online (Sandbox Code Playgroud)
这意味着两种类型都包含相同的信息1.这就是我们所说的他们是同构的意思.
1无信息丢失在任一方向,因为idFunctorIso' . idFunctorIso ≡ id(来自算符法如下fmap id ≡ id,与事实一起unC并unId是NEWTYPE构造的简单倒数).
| 归档时间: |
|
| 查看次数: |
829 次 |
| 最近记录: |