haskell - 使用通用量化约束重写规则/特化类型错误

标签 haskell ghc typeclass haskell-lens

我正在尝试实现一些特定于类型的特化 一些与 Prisms 一起使用的功能,我在使用时遇到了困难 GHC 8。(我遇到了 GHC < 8 的不同问题,但那就是 另一个问题)。

问题的(人为的)最小示例:

foo :: Prism' s a -> Prism' s a
foo = id

{-# RULES "foo/foo'" foo = foo' #-}
foo' :: Prism' Char Bool -> Prism' Char Bool
foo' = id

这会导致编译器错误:

error:
• Couldn't match type ‘p0 Bool (f0 Bool) -> p0 Char (f0 Char)’
                 with ‘forall (p :: * -> * -> *) (f :: * -> *).
                       (Choice p, Applicative f) =>
                       p Bool (f Bool) -> p Char (f Char)’
  Expected type: Prism' Char Bool
    Actual type: p0 Bool (f0 Bool) -> p0 Char (f0 Char)
• In the expression: foo'
  When checking the transformation rule "foo/foo'"

在我看来,这就像重写规则类型检查的一方面 “忘记”它的上下文。这里究竟发生了什么以及如何发生 我能让 GHC 高兴吗?

最佳答案

这本质上是一个老问题,即 N 级类型没有形成适当的层次结构来确定哪种类型更通用,哪种类型不太通用。看起来这个特定情况可以可以解决,但是肯定不可能为一般的 N 级函数编写重写规则。

幸运的是,这对于镜头和 friend 来说并不是什么大问题,因为它们始终有可用的单态版本!

foo₀ :: APrism' s a -> APrism' s a
foo₀ = id

{-# RULES "foo/foo'" foo₀ = foo' #-}
foo' :: APrism' Char Bool -> APrism' Char Bool
foo' = id

foo :: Prism' s a -> Prism' s a
foo p = clonePrism $ foo₀ (clonePrism p)

关于haskell - 使用通用量化约束重写规则/特化类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38186795/

相关文章:

Haskell SDL 绑定(bind) : How to blit sprites with transparent color?

Haskell:使用 UNPACK Pragma 的 GADT

haskell - 多态 "flip"在 7.10 中失败

scala - 类型类泛型中的类型类约束

haskell——有什么方法可以为大致元组同构数据类型生成 "deriving"实例?

haskell - 无法使用 Control.Exception.try 捕获 "Prelude.read: no parse"异常

haskell - Monadic 杂质和 Haskell 纯度。它们是如何结合的?

function - 如何用任意数量的函数组成 `not`?

haskell - 如何在 Haskell 的文件中获取任意表达式的类型?

haskell - 我怎样才能给这个子函数一个显式类型?