[英]“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.