【问题标题】:Function argument which transforms in ST Monad在 ST Monad 中转换的函数参数
【发布时间】:2016-10-27 02:09:36
【问题描述】:

我如何编写以下函数tt,当前有类型错误:

t :: Int
t = runST $ do
  ref <- newSTRef 10
  readSTRef ref

tt :: (STRef s a -> ST s a) -> Int
tt f = runST $ do
  ref <- newSTRef 10
  f ref

ttTest = tt readSTRef

我认为在tt 中的runST 内部,状态变量s 可以线程化到函数f,但是下面的编译器错误告诉我我错了:

transform.hs:50:3: Couldn't match type `s' with `s1' …
  `s' is a rigid type variable bound by
      the type signature for tt :: (STRef s a -> ST s a) -> Int
      at transform.hs:47:7
  `s1' is a rigid type variable bound by
       a type expected by the context: ST s1 Int
       at transform.hs:48:8
Expected type: ST s1 Int
  Actual type: ST s a
Relevant bindings include
  ref :: STRef s1 a
    (bound at transform.hs:49:3)
  f :: STRef s a -> ST s a
    (bound at transform.hs:48:4)
  tt :: (STRef s a -> ST s a) -> Int
    (bound at transform.hs:48:1)
In a stmt of a 'do' block: f ref
In the second argument of `($)', namely
  `do { ref <- newSTRef 10;
        f ref }'
transform.hs:50:3: Couldn't match type `a' with `Int' …
  `a' is a rigid type variable bound by
      the type signature for tt :: (STRef s a -> ST s a) -> Int
      at transform.hs:47:7
Expected type: ST s1 Int
  Actual type: ST s a
Relevant bindings include
  ref :: STRef s1 a
    (bound at transform.hs:49:3)
  f :: STRef s a -> ST s a
    (bound at transform.hs:48:4)
  tt :: (STRef s a -> ST s a) -> Int
    (bound at transform.hs:48:1)
In a stmt of a 'do' block: f ref
In the second argument of `($)', namely
  `do { ref <- newSTRef 10;
        f ref }'

任何cmets都将不胜感激。

【问题讨论】:

  • tt :: (forall s . STRef s Int -&gt; ST s Int) -&gt; Int
  • 啊哈,是Rank-2类型。谢谢。

标签: haskell st-monad


【解决方案1】:

答案是匿名发布的:

tt :: (forall s . STRef s Int -&gt; ST s Int) -&gt; Int.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-17
    • 2019-01-23
    • 2022-01-22
    相关资源
    最近更新 更多