没有使用'下一个'的实例

Xze*_*non 1 haskell functional-programming

我注意到我写的一个函数现在无法工作,尽管在其他场合成功使用它.

我的测试文件(仅用于测试此问题)看起来像这样:

import System.Random
generator = next . snd
Run Code Online (Sandbox Code Playgroud)

这导致错误

No instance for (RandomGen g0) arising from a use of ‘next’
The type variable ‘g0’ is ambiguous
Relevant bindings include
  generator :: (a, g0) -> (Int, g0) (bound at Test.hs:2:1)
Note: there is a potential instance available:
  instance RandomGen StdGen -- Defined in ‘System.Random’
In the first argument of ‘(.)’, namely ‘next’
In the expression: next . snd
In an equation for ‘generator’: generator = next . snd
Run Code Online (Sandbox Code Playgroud)

奇怪的是,如果我打开ghci并输入:

import System.Random
let generator = next . snd
Run Code Online (Sandbox Code Playgroud)

一切正常.我到底错过了什么?

编辑:也试过这个,它的工作正常:

generator something = next (snd something)
Run Code Online (Sandbox Code Playgroud)

Car*_*ten 6

这是因为单态限制

这是一个技术问题(如果您对细节感兴趣,请浏览链接),通常您永远不会看到,因为您在模块中添加签名或写下参数(不是无样式) - 而在GHCi中它被禁用 -你这里有点不走运

对于较新版本的GHC,默认情况下已为已编译模块启用,但对GHCi禁用(因此它将使用默认值,因为epsilonhalbe告诉您)

要获得相同的行为,您可以运行

:set -XMonomorphismRestriction
Run Code Online (Sandbox Code Playgroud)

在GHCi