Haskell 无法访问的代码错误?

标签 haskell compiler-errors gadt

假设我有以下(错误的)代码。

data A a b where
  APure ::  (A a b)
  AApply :: A (A b c) c

test :: (A a b) -> a -> b
test (APure) a = a
test AApply a = undefined

然后 GHC 会给我这个错误:
Couldn't match type `b' with `A b1 b'
  `b' is a rigid type variable bound by
      the type signature for test :: A a b -> a -> b
Inaccessible code in
  a pattern with constructor
    AApply :: forall c b. A (A b c) c,
  in an equation for `test'
In the pattern: AApply
In an equation for `test': test AApply a = undefined

这个错误信息不是完全错误的吗?
该错误与 AApply 无关。

最佳答案

Isn't this error message completely wrong? The error has nothing to do with AApply.



不完全的。可以说,您收到该错误消息是一个错误,但它并非完全偏离基础。

看片断后一起看整体。
test (APure) a = a

说我们有一个功能
test :: A a b -> r -> r

把它和签名放在一起
test :: (A a b) -> a -> b

并统一,忽略第一个方程的类型错误,类型被细化为
test :: A r r -> r -> r

然后看方程
test AApply a = undefined

看看在提炼的类型下是如何无法访问的,因为
AApply :: A (A b c) c

将需要
c ~ A b c

如果 AApply是一个有效的第一个论点。

关于Haskell 无法访问的代码错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16134836/

相关文章:

c++ - 错误 C++ 2679(二进制 '>>' : no operator found which takes a right-hand operand of type 'const std::string' (or there is no acceptable conversion))

haskell - GADT 的这种使用是否完全等同于存在类型?

haskell - 因子分解器的简单定义

haskell - 如何为名称中包含非字母字符的库定义 MIN_VERSION_𝑙𝑖𝑏 宏?

haskell - 对于线程化Haskell调试,我最好的工具是什么?

haskell - 如何在 Haskell 中转储循环融合的结果?

c - 使用Makefile编译时出现错误 "no such file or directory"

terminal - 我在终端中遇到 tcpdump 问题

haskell - 差异 : GADT, 数据族,数据族即 GADT

haskell - 如何在镜头样式单板库中为更高种类的类型实现孔和上下文?