Haskell 代码行未编译:“非法数据类型上下文" [英] Haskell line of code not compiling: "Illegal datatype context"

查看:15
本文介绍了Haskell 代码行未编译:“非法数据类型上下文"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在 Haskell 中编译这行代码,但它适用于我教授的系统.我使用的是 ghci 版本 7.6.2.

I am not able to get this line of code compiled in Haskell but it works on my professor's system. I use ghci version 7.6.2.

data Eq a => Shape a = Shape a

更准确地说,这是我得到的错误

More precisely, this is the error I am getting

[1 of 1] Compiling Main             ( test.hs, interpreted )

test.hs:1:6:
Illegal datatype context (use -XDatatypeContexts): Eq a =>
Failed, modules loaded: none.

这里有什么错误?

谢谢

推荐答案

您的教授可能正在使用旧版本的 GHC.您发布的行使用了最近被删除的功能.可能的解决方案是:

Your professor is probably using an older version of GHC. The line you posted uses a feature which has quite recently been removed. The possible solutions are:

  1. 去掉Eq a =>并写入data Shape a = Shape a.

正如 GHC 所说,给 -XDatatypeContexts 标志以重新启用已删除的功能.

As GHC says, give the -XDatatypeContexts flag to re-enable the removed feature.

更详细地说:类型声明的Eq a => 部分称为数据类型上下文.它唯一的功能是限制Shape构造函数的类型,以便代替Shape :: a ->Shape a 你得到 Shape :: Eq a =>->塑造一个.它不会使您不必在涉及 Shape 的类型签名中编写 Eq a,甚至确实会要求您在不需要时编写它们.当数据类型中的严格字段需要类约束时,它曾经很有用,但该功能很久以前就被删除了.

In more detail: the Eq a => part of your type declaration is called a datatype context. Its only function is to restrict the type of the Shape constructor, so that instead of Shape :: a -> Shape a you get Shape :: Eq a => a -> Shape a. It does not save you from having to write Eq a in type signatures involving Shapes, and indeed will even require you to write them when you wouldn't otherwise need to. It was once useful when strict fields in datatypes required a class constraint, but that feature was removed long ago.

简而言之,删除上下文几乎总是对您的程序的改进,因此它们已从 Haskell 2011 语言标准中删除.从 GHC 7.0.1 开始,有一个选项可以关闭它们,而从 7.2.1 开始,它一直是默认设置.

In short, just removing the context is nearly always an improvement to your program, so they were removed from the Haskell 2011 language standard. Since GHC 7.0.1 there has been an option to turn them off and since 7.2.1 it has been the default.

这篇关于Haskell 代码行未编译:“非法数据类型上下文"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆