Couldn't match type ‘IO’ with ‘UArray Int’

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

module Main (main) where import Data.Array.Unboxedimport System.Random.MWC data1 :: UArray Int Intdata1 = array (0,9)[] func1 :: UArray Int Int -> UArray Int Intfunc1 x = do gen2 <- createSystemRandom pos2 <- uniformR(0,7) gen2 :: IO Int let data4 = x // [(0,pos2)] data4 main :: IO ()main = do gen <- createSystemRandom num <- uniformR(0,10) gen :: IO Int let data2 = data1 // [(0,num)] print data2 let data3 = func1 data1 print data3 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

よろしくお願いします。

コメントを投稿

0 コメント