Q&A
UArrayの配列にSystem.Random.MWCの乱数で更新したい
UArrayの配列にSystem.Random.MWCの乱数で更新したいと思いますが、以下のエラーとなります。
/home/apo/doc/project/make/haskell/20221209_2/my-project/app/Main.hs:11:13: error: ~ Couldn't match type ‘IO’ with ‘UArray Int’ Expected: UArray Int (Gen ghc-prim-0.8.0:GHC.Prim.RealWorld) Actual: IO GenIO ~ In a stmt of a 'do' block: gen2 <- createSystemRandom In the expression: do gen2 <- createSystemRandom pos2 <- uniformR (0, 7) gen2 :: IO Int let data4 = x // ... data4 In an equation for ‘func1’: func1 x = do gen2 <- createSystemRandom pos2 <- uniformR (0, 7) gen2 :: IO Int let data4 = ... .... | 11 | gen2 <- createSystemRandom | ^^^^^^^^^^^^^^^^^^ /home/apo/doc/project/make/haskell/20221209_2/my-project/app/Main.hs:12:13: error: ~ Couldn't match type ‘IO’ with ‘UArray Int’ Expected: UArray Int Int Actual: IO Int ~ In a stmt of a 'do' block: pos2 <- uniformR (0, 7) gen2 :: IO Int In the expression: do gen2 <- createSystemRandom pos2 <- uniformR (0, 7) gen2 :: IO Int let data4 = x // ... data4 In an equation for ‘func1’: func1 x = do gen2 <- createSystemRandom pos2 <- uniformR (0, 7) gen2 :: IO Int let data4 = ... .... | 12 | pos2 <- uniformR(0,7) gen2 :: IO Int | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Couldn't match type ‘IO’ with ‘UArray Int’
エラーの中にCouldn't match type ‘IO’ with ‘UArray Int’とありますので、型があっていないようなのですが、修正方法がわかりません。
コンパイルエラー
コンパイルエラーの全文は下記のとおりです。
[apo@arch my-project]$ stack build my-project> build (lib + exe) Preprocessing library for my-project-0.1.0.0.. Building library for my-project-0.1.0.0.. Preprocessing executable 'my-project-exe' for my-project-0.1.0.0.. Building executable 'my-project-exe' for my-project-0.1.0.0.. [2 of 2] Compiling Main /home/apo/doc/project/make/haskell/20221209_2/my-project/app/Main.hs:11:13: error: ~ Couldn't match type ‘IO’ with ‘UArray Int’ Expected: UArray Int (Gen ghc-prim-0.8.0:GHC.Prim.RealWorld) Actual: IO GenIO ~ In a stmt of a 'do' block: gen2 <- createSystemRandom In the expression: do gen2 <- createSystemRandom pos2 <- uniformR (0, 7) gen2 :: IO Int let data4 = x // ... data4 In an equation for ‘func1’: func1 x = do gen2 <- createSystemRandom pos2 <- uniformR (0, 7) gen2 :: IO Int let data4 = ... .... | 11 | gen2 <- createSystemRandom | ^^^^^^^^^^^^^^^^^^ /home/apo/doc/project/make/haskell/20221209_2/my-project/app/Main.hs:12:13: error: ~ Couldn't match type ‘IO’ with ‘UArray Int’ Expected: UArray Int Int Actual: IO Int ~ In a stmt of a 'do' block: pos2 <- uniformR (0, 7) gen2 :: IO Int In the expression: do gen2 <- createSystemRandom pos2 <- uniformR (0, 7) gen2 :: IO Int let data4 = x // ... data4 In an equation for ‘func1’: func1 x = do gen2 <- createSystemRandom pos2 <- uniformR (0, 7) gen2 :: IO Int let data4 = ... .... | 12 | pos2 <- uniformR(0,7) gen2 :: IO Int | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -- While building package my-project-0.1.0.0 (scroll up to its section to see the error) using: /home/apo/.stack/setup-exe-cache/x86_64-linux-tinfo6/Cabal-simple_mPHDZzAJ_3.6.3.0_ghc-9.2.5 --verbose=1 --builddir=.stack-work/dist/x86_64-linux-tinfo6/Cabal-3.6.3.0 build lib:my-project exe:my-project-exe --ghc-options " -fdiagnostics-color=always" Process exited with code: ExitFailure 1
ソースコード
package.yaml
dependencies: - base >= 4.7 && < 5 - array - mwc-random
app/Main.hs
Haskell
1module Main (main) where 2 3import Data.Array.Unboxed 4import System.Random.MWC 5 6data1 :: UArray Int Int 7data1 = array (0,9)[] 8 9func1 :: UArray Int Int -> UArray Int Int 10func1 x = do 11 gen2 <- createSystemRandom 12 pos2 <- uniformR(0,7) gen2 :: IO Int 13 let data4 = x // [(0,pos2)] 14 data4 15 16 17main :: IO () 18main = do 19 20 gen <- createSystemRandom 21 num <- uniformR(0,10) gen :: IO Int 22 let data2 = data1 // [(0,num)] 23 print data2 24 25 let data3 = func1 data1 26 print data3 27 28 return ()
試したこと
・main関数の中ではエラーになりませんでした。
・UArrayではなく、リストではエラーになりませんでした。
・createSystemRandomの前にSystem.Random.MWCを付けてみましたがエラーに変わりはありませんでした。
gen <- System.Random.MWC.createSystemRandom num <- System.Random.MWC.uniformR(0,10) gen :: IO Int
・createSystemRandomの後ろに型を指定してみましたが、エラーに変わりはありませんでした。
gen <- createSystemRandom :: IO GenIO
gen <- System.Random.MWC.createSystemRandom :: IO GenIO
・func1の前の型(func1 :: UArray Int Int -> UArray Int Int)を消してみましたが、エラーは解消されませんでした。
補足情報(FW/ツールのバージョンなど)
stack --version
Version 2.9.1, Git revision 409d56031b4240221d656db09b2ba476fe6bb5b1 x86_64 hpack-0.35.0
よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。