Mon, 20 Mar 2000 14:59:26 +0100, Jan Brosius <[EMAIL PROTECTED]> pisze:

> >     let v = runst (newVar True)
> >     in
> >     runST (readVar v)
> >
> > Consider the last line ;

Consider the first line before, because it must be typed in order to
type the last line.

"newVar True" has type "ST s (MutVar s Bool)".
runST has type "(forall s. ST s a) -> a".

The type of expected argument of runST cannot be matched with the
type of the actual argument. "ST s (MutVar s Bool)" is not of the
form "forall s. ST s a" for any type a, because the second argument
of ST depends on the first, yet "forall s. ST s a" says it does not
(a is bound somewhere outside).

So the first line won't typecheck, and the question whether the last
line would typecheck if the first line would becomes irrelevant.


Now, consider the following example:

    runST $ do
        v <- newVar True
        return (runST (readVar v))

Here the cause is different. The type of "readVar v" is "ST s Bool".
Which s? The s that comes from a lambda-bound variable v, because
readVar requires s from the monad and s from the variable to be the
same. The lambda is hidden inside the do notation:

    runST $
        newVar True >>= \v ->
        return (runST (readVar v))

"ST s Bool" with s taken from the environment does not match
"forall s. ST s a", because runST requires the s to be free.


BTW. In GHC and Hugs the names are actually STRef, newSTRef, readSTRef,
writeSTRef.

-- 
 __("<    Marcin Kowalczyk * [EMAIL PROTECTED] http://qrczak.ids.net.pl/
 \__/              GCS/M d- s+:-- a22 C+++$ UL++>++++$ P+++ L++>++++$ E-
  ^^                  W++ N+++ o? K? w(---) O? M- V? PS-- PE++ Y? PGP+ t
QRCZAK                  5? X- R tv-- b+>++ DI D- G+ e>++++ h! r--%>++ y-

Reply via email to