相关疑难解决方法(0)

类型的函数和应用程序(* - >*) - >*

我遇到了一种情况,我的代码将受益于使用FunctorApplicative类似的抽象,但对于类型的类型(* -> *) -> *.定义一个更高级的仿函数可以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)

我们需要:-> …

haskell functor higher-kinded-types applicative

21
推荐指数
1
解决办法
694
查看次数

类型同义词实例

阅读“程序员的范畴论”我试图为 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\n
Run 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   |          ^^^^^^^^^^^^^^^^^^^^\n
Run Code Online (Sandbox Code Playgroud)\n

我应该怎么办 …

haskell

1
推荐指数
1
解决办法
398
查看次数