[英]“Inferred type is ambiguous” error using Data.Vector
以下代碼無法編譯,並顯示以下錯誤消息。 f
應該只是一個狀態monad,在運行時它會創建一個長度為1且具有單個int“ 42”的向量。 我懷疑在run
和unstream
之間會發生一些模棱兩可的情況,就像show . read
一樣show . read
show . read
,但我不知道如何解決它:
{-# LANGUAGE NoMonomorphismRestriction #-}
import Data.Vector.Generic.New (run, unstream)
import Data.Vector.Fusion.Stream (singleton)
f = run . unstream . singleton $ (42 :: Int)
main = return ()
錯誤:
main.hs:6:1:
Could not deduce (Data.Vector.Generic.Base.Vector v0 Int)
arising from the ambiguity check for `f'
from the context (Data.Vector.Generic.Base.Vector v Int)
bound by the inferred type for `f':
Data.Vector.Generic.Base.Vector v Int =>
GHC.ST.ST s (Data.Vector.Generic.Base.Mutable v s Int)
at sort.hs:6:1-44
Possible fix:
add an instance declaration for
(Data.Vector.Generic.Base.Vector v0 Int)
When checking that `f'
has the inferred type `forall (v :: * -> *) s.
Data.Vector.Generic.Base.Vector v Int =>
GHC.ST.ST s (Data.Vector.Generic.Base.Mutable v s Int)'
Probable cause: the inferred type is ambiguous
main.hs:6:1:
Could not deduce (Data.Vector.Generic.Base.Mutable v0
~ Data.Vector.Generic.Base.Mutable v)
from the context (Data.Vector.Generic.Base.Vector v Int)
bound by the inferred type for `f':
Data.Vector.Generic.Base.Vector v Int =>
GHC.ST.ST s (Data.Vector.Generic.Base.Mutable v s Int)
at sort.hs:6:1-44
NB: `Data.Vector.Generic.Base.Mutable' is a type function, and may not be injective
Expected type: Data.Vector.Generic.Base.Mutable v s Int
Actual type: Data.Vector.Generic.Base.Mutable v0 s Int
Expected type: GHC.ST.ST
s (Data.Vector.Generic.Base.Mutable v s Int)
Actual type: GHC.ST.ST
s (Data.Vector.Generic.Base.Mutable v0 s Int)
When checking that `f'
has the inferred type `forall (v1 :: * -> *) s1.
Data.Vector.Generic.Base.Vector v1 Int =>
GHC.ST.ST s1 (Data.Vector.Generic.Base.Mutable v1 s1 Int)'
Probable cause: the inferred type is ambiguous
通常,您可以通過附加類型注釋來解決此類歧義。 在這種情況下,問題是:“當unstream創建New va
時,我應該使用哪個Vector va
實例?”。 可以通過在run
或unstream
上添加注釋來解決此問題,但是看起來unstream
上的注釋的unstream
會更少(手指)。 像這樣:
f = run . (unstream :: Stream Int -> New {- put something concrete here -} Int) . singleton $ 42
這是show . read
show . read
問題,但有一個轉折。
我們在這里組成的兩個函數是
unstream :: forall s v a. (Vector v) => Stream a -> New v a
run :: forall s v' a'. New v' a' -> ST s (Mutable v' s a')
組成它們會產生New va ~ New v' a'
,並且由於New
是數據類型,因此它是單射的; 因此我們有v ~ v'
和a ~ a'
為:
run . unstream :: forall s v a. (Vector v) => Stream a -> ST s (Mutable v s a)
但是, v
的選擇未由Stream a -> ST s (Mutable vsa)
類型指定,因為Mutable
是類型族,因此不是單射的。 這就是show . read :: forall a. (Show a, Read a) => String -> String
show . read :: forall a. (Show a, Read a) => String -> String
show . read :: forall a. (Show a, Read a) => String -> String
; 很難看到,因為v
似乎出現在類型中。
想一想當您將其用於更具體的類型時會發生什么情況,例如
run . unstream :: forall s. Stream Int -> ST s (MVector s a)
無法通過Mutable v ~ MVector
知道v
應該是什么。
所有這些都暗示了一種鍵入run . unstream . singleton
run . unstream . singleton
run . unstream . singleton
態多態,無需預先承諾對v
的選擇,只需要求將其傳遞給呼叫站點即可:
{- LANGUAGE ScopedTypeVariables #-}
f :: forall s v a. (Vector v a) => Proxy v -> a -> ST s (Mutable v s a)
f _ = run . (unstream :: Stream a -> New v a) . singleton
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.