为什么 Int maxBound 不起作用?

发布于 2024-11-03 02:23:47 字数 223 浏览 2 评论 0 原文

当我在 ghci 中尝试时

> Int maxBound

,我得到

Not in scope: data constructor 'Int'

Even if I import Data.Int, still the Problem persists.这是怎么回事?

When I try

> Int maxBound

in ghci, I get

Not in scope: data constructor 'Int'

Even if I import Data.Int, still the problem persists. What is going on here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

少女的英雄梦 2024-11-10 02:23:47

编辑:该函数的官方文档位于 http://www.haskell.org/ghc/docs/7.0.3/html/libraries/base-4.3.1.0/Prelude.html#v:maxBound

首先, 您应该这样做

Prelude> maxBound :: Int
9223372036854775807
Prelude> 

如果您查看 maxBound 的类型签名,

Prelude> :t maxBound
maxBound :: (Bounded a) => a

:那么 maxBound 是一个返回 a 类型的函数,其中 a有界。但是,它不接受任何参数。 Int maxBound 表示您正在尝试使用数据构造函数 Int 和参数 maxBound 创建某些内容。

对于您的特定错误消息,您尝试使用 Int - 这是一种类型 - 作为值,从而导致您收到错误。导入 Data.Int 没有帮助。

EDIT: The official docs for the function are at http://www.haskell.org/ghc/docs/7.0.3/html/libraries/base-4.3.1.0/Prelude.html#v:maxBound

To begin with, you should be doing

Prelude> maxBound :: Int
9223372036854775807
Prelude> 

If you look at the type signature of maxBound:

Prelude> :t maxBound
maxBound :: (Bounded a) => a

Then maxBound is a function that returns something of type a, where a is Bounded. However, it does not accept any parameters. Int maxBound means that you are trying to create something with data constructor Int and parameter maxBound.

For your specific error message, you are trying to use Int - which is a type - as a value, resulting in the error you are getting. Importing Data.Int won't help.

深海少女心 2024-11-10 02:23:47

那不是有效的 Haskell。

maxBound< /a> 是一个常量,定义 Bounded 类< /a>:

Prelude> :t maxBound
maxBound :: Bounded a => a

要获取任何特定类型的绑定,您需要将其专门化为特定类型。 类型注释< /a> 由表达式的 :: 语法给出,如下所示:

Prelude> maxBound :: Int
9223372036854775807

That's not valid Haskell.

maxBound is a constant that defines the maximum element of types that are in the Bounded class:

Prelude> :t maxBound
maxBound :: Bounded a => a

To get the bound for any particular type, you need to specialize it to a particular type. Type annotations are given by :: syntax on expressions, like so:

Prelude> maxBound :: Int
9223372036854775807
放血 2024-11-10 02:23:47

即使这种情况 (ma​​xBound) 不是 TypeApplications GHC 编译器选项最有效的用法,我发现它与类型注释选项 :: (至少对于我这个刚踏上 Haskell 船的人来说)。

我承认它需要编译选项/编译指示,但它是我在 ~/.ghci 文件中全局激活的众多选项之一。而且申请起来很容易!

因此,事不宜迟,让我们在实践中看看它。以下是对我来说启用 TypeApplications 编译器选项的最常见选项。

  1. 每个 GHCI 会话启用 TypeApplications
Prelude> :set -XTypeApplications
Prelude> maxBound @Int
9223372036854775807
Prelude> maxBound @Bool
True
  1. 通过在文件 .ghci 中添加以下内容,在目录级别或操作系统用户级别启用 TypeApplications。
:set -XTypeApplications

然后在 GHCI 提示符下,

Prelude> maxBound @Int
9223372036854775807
  1. 通过将以下内容放在文件开头,在 Haskell 文件级别启用 TypeApplications。
{-# LANGUAGE TypeApplications #-}

注释:

  • 要在 GHCI 提示符中禁用此选项,
Prelude> :unset -XTypeApplications
  • 这篇文章有点过于冗长,但它强调了此编译选项在很多情况下派上用场的情况。

Even if this case (maxBound) is not the most elocvent usage of the TypeApplications GHC compiler option, I found it a bit more suggestive compared with the type annotation options :: (at least for me as a newly embarked on Haskell boat).

I admit it requires a compilation option/pragma, but it is one of many I have activated by globally in ~/.ghci file. And it's easy to apply!

So, without further ado let's see it in practice. The followings are for me the most common options to enable the TypeApplications compiler option.

  1. Enable TypeApplications per GHCI session
Prelude> :set -XTypeApplications
Prelude> maxBound @Int
9223372036854775807
Prelude> maxBound @Bool
True
  1. Enable TypeApplications at directory level or OS user level, by adding in the file .ghci the following.
:set -XTypeApplications

and then in GHCI prompt

Prelude> maxBound @Int
9223372036854775807
  1. Enable TypeApplications at the Haskell file level by placing the following at the beginning of the file.
{-# LANGUAGE TypeApplications #-}

Notes:

  • To disable this option in the GHCI prompt just
Prelude> :unset -XTypeApplications
  • This post is a bit too verbose but it emphasises just on of the many cases this compilation option comes in handy.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文