将“runST”添加到“ST s a”类型的值会使其停止类型检查

时间:2014-12-24 18:01:46

标签: haskell types state monads

我有以下功能:

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"的类型?如何解决这个问题?

0 个答案:

没有答案