没有使用'next'的例子 [英] No instance for ariving from use of 'next'
问题描述
我的测试文件(只是为了测试这个问题而已)我发现我写的函数现在不起作用,看起来像这样:
import System.Random
generator = next。 snd
这会导致错误
没有使用'next'引起的(RandomGen g0)实例
类型变量'g0'不明确
相关绑定包括
generator :: (a,g0) - > (Int,g0)(绑定在Test.hs:2:1)
注意:有一个潜在的实例可用:
instance RandomGen StdGen - 在'System.Random'中定义
In '(。)'的第一个参数,即'next'
在表达式:next中。 snd
在'generator'的等式中:generator = next。 snd
奇怪的是,如果我打开ghci并键入:
import System.Random
让generator = next。 snd
一切正常。我错过了什么?
编辑:也试过这个,它工作得很好:
generator something = next(snd something)
这是因为单形性限制
这是一项技术问题(如果你对细节感兴趣,请探索链接),通常你永远不会看到,因为你在你的模块中添加了签名或者写下了参数(而不是 point-free 样式) GHCi已禁用 - 这里有点不吉利。
对于GHC的新版本,默认情况下已启用编译模块,但对GHCi禁用(因此它将使用默认值作为epsilonhalbe告诉你)
要获得相同的行为,您可以运行
:set -XMonomorphismRestriction
在GHCi中$ / b>
I noticed that a function I wrote is now not working, despite using it successfully on other occasions.
My test file (made one just to test this issue) looks like this:
import System.Random
generator = next . snd
This results in the error
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
The weird thing is that if I open ghci and type:
import System.Random
let generator = next . snd
Everything works just fine. What exactly am I missing?
Edit: Also tried this and it works just fine:
generator something = next (snd something)
this is because of the Monomorphism Restriction
It's a technical issue (please explore the link if you are interested in the details) and normally you'll never see because you add signatures or write down the arguments (not point-free style) in your modules - and in GHCi it's disabled - you got a bit unlucky here.
for newer versions of GHC this is enabled by default for compiled modules but disabled for GHCi (so it will use defaults as epsilonhalbe told you)
To get the same behavior you can run
:set -XMonomorphismRestriction
in GHCi
这篇关于没有使用'next'的例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!