これをコンパイルしようとすると:
module Main where
import qualified Data.Vector.Unboxed.Mutable as MV
import Control.Monad.ST
myRead mv = runST $ MV.read mv 0
次のエラー メッセージが表示されます。
Could not deduce (t ~ U.MVector s a)
from the context (U.Unbox a)
bound by the inferred type of myRead :: U.Unbox a => t -> a
at src/Main.hs:53:1-32
`t' is a rigid type variable bound by
the inferred type of myRead :: U.Unbox a => t -> a
at src/Main.hs:53:1
Expected type: U.MVector (PrimState (ST s)) a
Actual type: t
In the first argument of `MV.read', namely `mv'
In the second argument of `($)', namely `MV.read mv 0'
In the expression: runST $ MV.read mv 0
runST を使用して可変ベクトルからの読み取りをピュアにすることはできますか? もしそうなら、どのように?の型署名が必要だとmyRead
思いますが、私が試したすべてのことは、ますます理解できないエラーメッセージにつながりました。
編集:以下のコメントに入れただけのコンテキストを強調しています:ここでのコンテキストは、変更可能なベクトルを受け取り、変更可能なベクトルを一時的なスクラッチスペースとして使用していくつかの計算を行い、浮動小数点値を返す必要があるということです。可変ベクトルへの変更は気にしないので、その「状態の変化」を無視して、その中から値の 1 つを単に返す方法があるかどうか疑問に思っていました。