【发布时间】:2014-02-19 03:10:09
【问题描述】:
我正在尝试使用System.MWC 包生成随机数。我写了一个小测试代码如下:
module Main where
import Data.Word(Word32)
import Control.Monad.ST as ST
import System.Random.MWC
import Data.Vector.Generic.Base
import qualified Data.Vector.Unboxed as U
test :: Word32 -> Int
test x = runST $ do
gen <- initialize (U.fromList [x] :: U.Vector Word32)
v <- uniformR (1,100) gen
return v
问题是我在尝试使用initialize 函数时遇到实例错误。这是我得到的实例错误:
No instance for (vector-0.9.1:Data.Vector.Generic.Base.Vector
U.Vector Word32)
arising from a use of `initialize'
Possible fix:
add an instance declaration for
(vector-0.9.1:Data.Vector.Generic.Base.Vector U.Vector Word32)
In a stmt of a 'do' block:
gen <- initialize (U.fromList [x] :: U.Vector Word32)
In the second argument of `($)', namely
`do { gen <- initialize (U.fromList [x] :: U.Vector Word32);
v <- uniformR (1, 100) gen;
return v }'
In the expression:
runST
$ do { gen <- initialize (U.fromList [x] :: U.Vector Word32);
v <- uniformR (1, 100) gen;
return v }
Failed, modules loaded: none.
我在ghci 中运行了info,以确认没有为Data.Vector.Generic.Base.Vector 定义任何实例。然后我检查了hackage 的文档,但该包不存在页面。
所以,我的问题是缺少的实例在哪里定义。一旦我知道它的定义位置,我就可以加载它。导入 Data.Vector.Generic.Base.Vector 不起作用。此外,mwc-random-0.13.1.1 包依赖于vector-0.9.1,如您在上面的错误消息中所见。
【问题讨论】:
-
它对我有用,但我做了
gen <- initialize (U.fromList [x]),因为额外的类型签名是多余的。我正在使用 mwc-random-0.13.1.0,它实际上取决于矢量 >= 0.7,所以矢量 = 0.9.1 是您在系统上安装的。不过它应该仍然可以工作。 -
@bheklilr,由于错误,我添加了类型签名。不过,我得到了错误,但你没有,这很令人惊讶。
-
我正在使用向量 0.10.0.1 和 mwc-random 0.13.1.0,也许您可以尝试更新您的向量版本以匹配?
-
@bheklilr,我也安装了
vector 0.10.9.1(与ghc-pkg确认),但mwc-random似乎取而代之的是vector 0.9.1。删除vector 0.9.1并重新安装mwc-random将依赖关系更新为vector 0.10.9.1,并修复了该问题。你能发表你的建议(请提及阴谋集团重建)作为答案吗? -
很高兴这是一个简单的修复。我已将此作为答案发布。