Is ST referential transparent?

Why is it STintended to refuse the following code as shown in wikibook ? The article seems to suggest that the effects STwill flow in another STif it was allowed, but I really don't understand why.

It seems that I cannot run the specific one ST s (STRef s a). Are the STeffects contained and referenced transparent, but is such use still void?

Intuition tells me that there is a difference between semantics IOand STthat related to the fact that there is one IOand many independent ones ST, but I'm not sure.

> runST (newSTRef True)

<interactive>:5:8: error:
     Couldn't match type ‘a’ with ‘STRef s Bool’
        because type variable ‘s’ would escape its scope
      This (rigid, skolem) type variable is bound by
        a type expected by the context:
          forall s. ST s a
        at <interactive>:5:1-21
      Expected type: ST s a
        Actual type: ST s (STRef s Bool)
     In the first argument of ‘runST’, namely ‘(newSTRef True)’
      In the expression: runST (newSTRef True)
      In an equation for ‘it’: it = runST (newSTRef True)
     Relevant bindings include it :: a (bound at <interactive>:5:1)

> :t newSTRef
newSTRef :: a -> ST s (STRef s a)

> :t runST
runST :: (forall s. ST s a) -> a
+4
source share
2 answers

, , :

, ST .

, :

: runST
, , -,
POPL 2018
http://iris-project.org/ PDF

+7

, , , :

ST runST (newSTRef True)?

, :

main = do
    let r = runST (newSTRef True)
    evaluate (runST (writeSTRef r False))
    print (runST (readSTRef r))

False.

main = do
    evaluate (runST (writeSTRef (runST (newSTRef True)) False))
    print (runST (readSTRef (runST (newSTRef True))))

( r ) True, runST (newSTRef True) STRef.

runST , , ST s, .

IO ST s runIO. IO, runIO ( ). IO - main ( runIO, main), IO , - .

, , . , runST ST s .

+6

Source: https://habr.com/ru/post/1689538/


All Articles