我有以下功能:
foo :: (GMV.MVector v a) => Matrix v (PrimState (ST s)) a -> (Int, Int) -> ST s a
foo mat (x, y) = get (Vec x y 0) mat
它会返回ST s a
,但我希望它只返回a
。请注意runST
的类型为:
runST :: (forall s. ST s a) -> a
所以我将我的函数定义替换为:
foo :: (GMV.MVector v a) => Matrix v (PrimState (ST s)) a -> (Int, Int) -> a
foo mat (x, y) = runST (get (Vec x y 0) mat)
预计它会起作用,但我收到以下错误:
Main.hs:16:41:
Could not deduce (s1 ~ s)
from the context (GMV.MVector v a)
bound by the type signature for
foo :: GMV.MVector v a =>
Matrix v (PrimState (ST s)) a -> (Int, Int) -> a
at Main.hs:15:8-76
‘s1’ is a rigid type variable bound by
a type expected by the context: ST s1 a at Main.hs:16:18
‘s’ is a rigid type variable bound by
the type signature for
foo :: GMV.MVector v a =>
Matrix v (PrimState (ST s)) a -> (Int, Int) -> a
at Main.hs:15:8
Expected type: Matrix v (PrimState (ST s1)) a
Actual type: Matrix v (PrimState (ST s)) a
Relevant bindings include
mat :: Matrix v (PrimState (ST s)) a (bound at Main.hs:16:5)
foo :: Matrix v (PrimState (ST s)) a -> (Int, Int) -> a
(bound at Main.hs:16:1)
In the second argument of ‘get’, namely ‘mat’
In the first argument of ‘runST’, namely ‘(get (Vec x y 0) mat)’
如果我理解它,那就是试图等同于" s"来自runST的类型,使用" s"来自" foo"的类型?如何解决这个问题?