7

私は次の関数を書きました:

(.>=.) :: Num a => STRef s a -> a -> Bool
r .>=. x = runST $ do
 v <- readSTRef r
 return $ v >= x

しかし、コンパイルしようとすると、次のエラーが発生しました。

Could not deduce (s ~ s1)
from the context (Num a)
  bound by the type signature for
             .>=. :: Num a => STRef s a -> a -> Bool
  at test.hs:(27,1)-(29,16)
  `s' is a rigid type variable bound by
      the type signature for .>=. :: Num a => STRef s a -> a -> Bool
      at test.hs:27:1
  `s1' is a rigid type variable bound by
       a type expected by the context: ST s1 Bool at test.hs:27:12
Expected type: STRef s1 a
  Actual type: STRef s a
In the first argument of `readSTRef', namely `r'
In a stmt of a 'do' expression: v <- readSTRef r

誰か助けてもらえますか?

4

2 に答える 2

12

これは意図したとおりです。は、のSTRef1回の実行でのみ有効ですrunSTSTRefそして、外部をの新しい実行に入れようとしますrunST。それは無効です。これにより、純粋なコードで任意の副作用が発生する可能性があります。

ですから、あなたが試みることは達成することが不可能です。意図的に!

于 2011-11-04T09:23:25.143 に答える