在GHCI或多或少挑剔的类型中展示
如果我启动GHCI并输入一个简单的算术表达式,我通常不会在没有大惊小怪的情况下获得预期的结果
GHCi λ: 6 * 7
42
,但是我在教程中遵循的示例是Cabal Project的一部分。如果我在项目中启动GHCI,例如 cabal reploce示例
,请尝试相同的表达式,我得到以下内容:
GHCi λ: 6 * 7
<interactive>:4:1: error: [-Wtype-defaults, -Werror=type-defaults]
• Defaulting the following constraints to type ‘Integer’
(Show a0) arising from a use of ‘print’ at <interactive>:4:1-5
(Num a0) arising from a use of ‘it’ at <interactive>:4:1-5
• In a stmt of an interactive GHCi command: print it
我被迫指定表达式的类型:
GHCi λ: 6 * 7 :: Int
42
我想了解可能制作的“显示” 的内容教程的项目中更挑剔,如果有某种方法可以回到“明智”默认值的便利。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题与Haskell如何处理数字文字有关:有人可能会认为
6
具有类型int
(或integer
);但是,如果您向GHCI询问6
您会得到的类型:这意味着
6
(或任何数字文字)实际上是通用类型<代码> p 提供p
具有num
实例。通常,由于类型注释,编译器可以自动能够理解您实际想要的数字类型:当编译器无法理解您对您的类型的字体类型时,它将默认为
Integer
,for 示例如果您写:此处
print(2 :: int)
和print(2 :: Integer)
是正确的,因此编译器默认为2 :::::::整数
。这种默认行为可能并非总是可以的(尤其是因为
Integer
s的效率不如int
s),因此每次发生这种情况时都会警告您。您的项目的
.cabal
必须已启用-wtype-defaults
和 -werror 您仍然会看到有关此默认行为的警告,但至少您的代码将执行;您还可以删除-wtype-defaults
以禁用警告。如果您在
.cabal
文件中没有看到-wtype-default
,则可以通过指定此标志之一来启用它: ,-weverithing
。tl; dr 您应该从项目的
.cabal
文件中删除这些汇编标志之一:-wtype-defaults
(或-wall)
/-weverithing
)-werror
Your issue is related to how Haskell deals with number literals: one might think that a number like
6
has typeInt
(orInteger
); however, if you ask ghci the type of6
you'll get:What this means is that
6
(or any number literal) is actually a value of a generic typep
provided thatp
has aNum
instance. Usually, thanks to type annotations, the compiler is automatically able to understand which numeric type you actually want:When the compiler can not understand what specific type you need for your type literal it will default to
Integer
, for example if you write:Here both
print (2 :: Int)
, andprint (2 :: Integer)
are correct so the compiler defaults to2 :: Integer
.This defaulting behaviour might not always be ok (especially since
Integer
s are less efficient thanInt
s), so there is a GHC compilation flag that warns you every time this happens.Your project's
.cabal
must have enabled the-Wtype-defaults
and the-Werror
flags. If you remove-Werror
you'll still see the warning about this defaulting behaviour but at least your code will execute; you can also remove the-Wtype-defaults
to disable the warning.If you do not see the
-Wtype-default
in your.cabal
file it is because it can also be enabled by specifying one of this flags:-Wall
,-Weverithing
.TL;DR you should remove one of these compilation flags from the project's
.cabal
file:-Wtype-defaults
(or-Wall
/-Weverithing
)-Werror