我遇到了一种情况,我的代码将受益于使用Functor和Applicative类似的抽象,但对于类型的类型(* -> *) -> *.定义一个更高级的仿函数可以RankNTypes像这样完成
class HFunctor f where
hfmap :: (forall x. a x -> b x) -> f a -> f b
Run Code Online (Sandbox Code Playgroud)
但更高级的版本Applicative有点棘手.这是我能想到的最好的:
class HFunctor f => HApplicative f where
hpure :: (forall x. a x) -> f a
(<**>) :: f (a :-> b) -> f a -> f b
newtype (:->) a b x = HFunc (a x -> b x)
infixr 5 :->
Run Code Online (Sandbox Code Playgroud)
我们需要:-> …
阅读“程序员的范畴论”我试图为 Op 重新创建 Functor 类型类实例。
\n{-# LANGUAGE TypeSynonymInstances #-}\n\nmodule Type.Op where\n\nimport Data.Functor.Contravariant ( Contravariant, contramap )\n\ntype Op r a = a -> r\n-- data Op r a = (->) a r\n\ninstance Contravariant (Op r) where\n contramap f g = g . f\nRun Code Online (Sandbox Code Playgroud)\n编译产生以下错误:
\n \xe2\x80\xa2 The type synonym \xe2\x80\x98Op\xe2\x80\x99 should have 2 arguments, but has been given 1\n \xe2\x80\xa2 In the instance declaration for \xe2\x80\x98Contravariant (Op r)\xe2\x80\x99\n |\n10 | instance Contravariant (Op r) where\n | ^^^^^^^^^^^^^^^^^^^^\nRun Code Online (Sandbox Code Playgroud)\n我应该怎么办 …