乱数モナドの利用に関して対話実行では動作するのにインタプリタとしてはエラーが出る.

Haskellは先週程度にインストールした初心者です.
http://www.nct9.ne.jp/m_hiroi/func/haskell26.html 等のサイトを参考に,乱数モナドを用いて何かしようと考え,一旦次のコードを実行しようとしました.

Haskell

import System.Randomr = getStdRandom(randomR(1,4))main = r >>= print

しかしながら,次のエラーが出力されました.

txt

bj2.hs:2:18: error: • Ambiguous type variable 'a0' arising from a use of 'randomR' prevents the constraint '(Random a0)' from being solved. Relevant bindings include r :: IO a0 (bound at bj2.hs:2:1) Probable fix: use a type annotation to specify what 'a0' should be. These potential instances exist: instance Random Integer -- Defined in 'System.Random' instance (Random a, Random b) => Random (a, b) -- Defined in 'System.Random' instance (Random a, Random b, Random c) => Random (a, b, c) -- Defined in 'System.Random' ...plus 10 others ...plus 30 instances involving out-of-scope types (use -fprint-potential-instances to see them all) • In the first argument of 'getStdRandom', namely '(randomR (1, 4))' In the expression: getStdRandom (randomR (1, 4)) In an equation for 'r': r = getStdRandom (randomR (1, 4)) | 2 | r = getStdRandom(randomR(1,4)) | ^^^^^^^^^^^^ bj2.hs:2:26: error: • Ambiguous type variable 'a0' arising from the literal '1' prevents the constraint '(Num a0)' from being solved. Relevant bindings include r :: IO a0 (bound at bj2.hs:2:1) Probable fix: use a type annotation to specify what 'a0' should be. These potential instances exist: instance Num Integer -- Defined in 'GHC.Num' instance Num Double -- Defined in 'GHC.Float' instance Num Float -- Defined in 'GHC.Float' ...plus two others ...plus 35 instances involving out-of-scope types (use -fprint-potential-instances to see them all) • In the expression: 1 In the first argument of 'randomR', namely '(1, 4)' In the first argument of 'getStdRandom', namely '(randomR (1, 4))' | 2 | r = getStdRandom(randomR(1,4)) | ^ bj2.hs:3:14: error: • Ambiguous type variable 'a0' arising from a use of 'print' prevents the constraint '(Show a0)' from being solved. Probable fix: use a type annotation to specify what 'a0' should be. These potential instances exist: instance Show Ordering -- Defined in 'GHC.Show' instance Show StdGen -- Defined in 'System.Random.Internal' instance Show a => Show (Maybe a) -- Defined in 'GHC.Show' ...plus 24 others ...plus 48 instances involving out-of-scope types (use -fprint-potential-instances to see them all) • In the second argument of '(>>=)', namely 'print' In the expression: r >>= print In an equation for 'main': main = r >>= print | 3 | main = r >>= print |

尚,ghciでは次の結果が得られています.

Haskell

ghci> import System.Randomghci> r = getStdRandom(randomR(1,4))ghci> main = r >>= printghci> main4

このコードでインタプリタとして実行した時と,対話で実行した時に差が発生する理屈が分かりません.
エラーコードを翻訳した限り型注釈が必要と書いてあるように見えますが,どこに型注釈を入れるべきなのか,そしてモナドの型注釈はどのように記述するべきなのかが分かりません.
また,少々話が逸れますが,主にこちらのサイト(https://minoki.github.io/ks-material/haskell/ )を参考に入門を試みていますが,データ型とパターンマッチ以降の題材に関して良く理解出来ていないと考えているので,参考にしやすいサイト等々ありましたらお教えいただけますと幸いです.
実行環境表示
$stack --version
Version 2.7.5, Git revision ba147e6f59b2da75b1beb98b1888cce97f7032b1 x86_64 hpack-0.34.4
$cat /etc/debian_version
11.2
上部実行コマンド $stack runghc bj2,hs 2> error.txt
下部実行コマンド $stack ghci
但し上部のファイル名をbj2.hsとし,エラー出力として示したものはerror.txtの中身であるものとする.
追伸:今最終的に目標とするのはブラックジャックですが,成否問わず学習を目的に作成しようと試みています.

コメントを投稿

0 コメント