rank2classes包提供了一个Functor版本,对于该版本,映射函数似乎是类型构造函数之间的自然转换。
遵循这个想法,下面是Bifunctor的等级2版本
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE StandaloneKindSignatures #-}
import Data.Kind
type Rank2Bifunctor :: ((Type -> Type) -> (Type -> Type) -> Type) -> Constraint
class Rank2Bifunctor b where
rank2bimap ::
(forall x. p x -> p' x) -> (forall x. q x -> q' x) -> b p q -> b p' q'很明显,可以为此类型的Foo提供一个Rank2Bifunctor实例:
data Foo f g = Foo (f Int) (g Int)
instance Rank2Bifunctor Foo where
rank2bimap tleft tright (Foo f g) = Foo (tleft f) (tright g)但是,嵌套f和g的Bar类型又如何呢
data Bar f g = Bar (f (g Int))对于初学者来说,我们似乎需要在rank2bimap的签名中使用Functor p,才能在f中转换g。
Bar是一个有效的“秩2双函数器”吗?
发布于 2021-02-04 23:06:14
实际上,这不是您的Bifunctor的一个实例,因为缺少约束允许您为f选择一个逆变函数器,然后rank2bimap将大致相当于为f实现fmap
rank2bimap id :: (g ~> g') -> Bar f g -> Bar f g' -- covariance of f, kinda (since Bar f g = f (g Int))
type f ~> f' = (forall x. f x -> f' x)如果您添加了f和g (这里是可选的)是函数器的要求,那么您确实得到了一个双函数器:
rank2bimap :: (Functor f, Functor g) => (f ~> f') -> (g ~> g') -> Bar f g -> Bar f' g'特别地,为了证明双函子定律,你需要f ~> f'的自由定理,假设f和f'是函子,那么n :: f ~> f'对所有的phi,fmap phi . n = n . fmap phi都满足这个定理。
https://stackoverflow.com/questions/66047704
复制相似问题