質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Haskell

Haskellは高い機能性をもった関数型プログラミング言語で、他の手続き型プログラミング言語では難しいとされている関数でも容易に行うことができます。強い静的型付け、遅延評価などに対応しています。

Q&A

解決済

1回答

538閲覧

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

apo

総合スコア349

Haskell

Haskellは高い機能性をもった関数型プログラミング言語で、他の手続き型プログラミング言語では難しいとされている関数でも容易に行うことができます。強い静的型付け、遅延評価などに対応しています。

0グッド

0クリップ

投稿2022/12/09 07:51

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

よろしくお願いします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

型宣言を下記のように修正したら直りました。

func1 :: UArray Int Int -> IO (UArray Int Int)

お騒がせしました。

投稿2022/12/09 08:08

apo

総合スコア349

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問