简体   繁体   English

Haskell类型错误消息

[英]Haskell type error messages

I am interested in the way Haskell generates type error messages. 我对Haskell生成类型错误消息的方式感兴趣。 In particular, I am interested in the way it suggests program expressions the user might want to fix in order for the error to go away. 特别是,我对它建议用户可能要修复的程序表达式以使错误消失的方式感兴趣。 Examples should make this more clear. 实例应该使这一点更加清楚。 Consider the first example. 考虑第一个示例。

main = zprint 2
zprint x = putStrLn x

The corresponding error message is: 相应的错误消息是:

No instance for (Num String)
  arising from the literal `2'
Possible fix: add an instance declaration for (Num String)
In the first argument of `zprint', namely `2'
In the expression: zprint 2
In an equation for `main': main = zprint 2

The way I am interpreting this is that the compiler is telling me that I should fix the parameter being passed 2 , or the whole function call zprint 2 , or even the whole main = zprint 2 , but it does report putStrLn saying I am using it in a wrong way and I should use some other function. 我的解释方式是编译器告诉我应该修复传递的参数2 ,或者整个函数调用zprint 2 ,甚至整个main = zprint 2 ,但是它确实报告putStrLn表示我正在使用它错误的方式,我应该使用其他功能。 Likewise, we have the following example: 同样,我们有以下示例:

main = zprint 2
idn x = x
zprint x = putStrLn (idn x)

The message is the same: 消息是一样的:

No instance for (Num String)
  arising from the literal `2'
Possible fix: add an instance declaration for (Num String)
In the first argument of `zprint', namely `2'
In the expression: zprint 2
In an equation for `main': main = zprint 2

But, we could also change the second line to idn x = show x and the program is now well typed. 但是,我们也可以将第二行更改为idn x = show x ,并且程序现在已正确键入。 Hence, I can see the pattern in the way Haskell lists those expressions, but I would like to see the actual algorithm if it is written down somewhere, but not the compiler source code. 因此,我可以看到Haskell列出这些表达式的方式中的模式,但是如果它写在某个地方,而不是编译器源代码,我希望看到实际的算法。 Note that I am not an expert in Haskell, more of an OCaml person. 请注意,我不是Haskell的专家,而是OCaml的专家。 GJC version I am using is 7.4.1. 我正在使用的GJC版本是7.4.1。

Hence, I can see the pattern in the way Haskell lists those expressions, but I would like to see the actual algorithm if it is written down somewhere, but not the compiler source code. 因此,我可以看到Haskell列出这些表达式的方式中的模式,但是如果它写在某个地方,而不是编译器源代码,我希望看到实际的算法。

There isn't a formal algorithm for error reporting, it's really an engineering detail of the compiler implementation and different Haskell compilers approach it differently. 没有用于错误报告的正式算法,它实际上是编译器实现的工程细节,不同的Haskell编译器采用不同的方法。 So I'm afraid if you want to understand the gritty details of GHC the only way to do so is read the literate Haskell in the compiler or SPJ's notes on the Trac wiki. 因此,恐怕如果您想了解GHC的具体细节,唯一的方法是阅读编译器中有识字的Haskell或Trac Wiki上的SPJ注释

Like BenjaminKovach said, the algorithm and the error reporting is not significantly different than what you'd do in vanilla Hindley Milner. 就像本杰明·科瓦奇(BenjaminKovach)所说的那样,该算法和错误报告与您在香草欣德利·米尔纳(Hindley Milner)中所做的没有太大不同。 The typechecker tracks position information and when the unifier fails to find a solution it reports the provenance of the terms which generated constraints that failed. 类型检查器会跟踪位置信息,并且当统一者无法找到解决方案时,它将报告生成约束失败的术语的出处。 One minor detail is that it also tries to apply available partial solutions on the free variables in the types of the error message. 一个较小的细节是它还尝试将错误消息类型中的可用局部解决方案应用于自由变量。

The type system itself is an evolution of HM(X) ( see Chapter 10 of Advanced Topics in Types and Programming Languages ). 类型系统本身是HM(X)的演进(请参见“类型和编程语言高级主题”的第10章)。 The latest paper outlining the typechecker is OutsideIn(X) which is more or less how it works in latest GHC sans some of bleeding egde work. 概述类型检查器的最新论文是OutsideIn(X) ,它或多或少地在最新的GHC中是如何工作的,但有一些出血的工作。

Haskell uses a modified form of a Hindley-Milner type system , and GHC uses a modified version of Damas-Hindley-Milner type inference in order to type-check and generate error messages. Haskell使用Hindley-Milner类型系统的修改形式,而GHC使用Damas-Hindley-Milner类型推断的修改版本,以便进行类型检查并生成错误消息。

There are tutorial implementations of Algorithm W out there, as well as a long-winded discussion about how Haskell in particular is type checked, located here . 算法W的教程实现在那里,以及如何哈斯克尔尤其是类型检查,长篇大论的讨论设在这里

I can't guarantee that these resources will be super accessible, but that's because type inference isn't easy, type systems in general require a little bit of study to fully grasp, and GHC uses Haskell's complex type system to generate the error messages you're seeing. 我不能保证这些资源是超级可访问的,但是那是因为类型推断并不容易,类型系统通常需要一点点研究才能完全掌握,GHC使用Haskell的复杂类型系统来生成错误消息。在看。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM