On 26-Apr-2000, Jan Brosius <[EMAIL PROTECTED]> wrote:
>
> runST :: forall s. (ST s a) -> a
It would be clearer to write that as
runST :: (forall s. ST s a) -> a
rather than relying on the relative precedences
of `.' and `->'.
> give runST the type signature
>
>
> runST :: exists s . ( ST s a ) -> a
>
> and technically we have also a bounded variable s. What is the reason for
> choosing
>
> forall against exists ?
`forall' means that the variable can be instantiated by the
caller, but not by the callee.
`exists' means the opposite, that the variable can be
instantiated by the callee, but not the caller.
If a function has a universally quantified type
such as `forall s . <some type involving s>',
then the function definition must be polymorphic in `s',
but the caller can call it with `s' bound to some
particular instance. For `exists', it is the other way around,
the caller must be polymorphic in `s' but the function definition
could bind it to some particular instance.
If some Haskell implementation supported `exists' in that position,
and runST were declared with `exists' rather than `forall', then the
following example would be well-typed:
data MyStore = MyStore
side_effect :: STRef MyStore Int -> Int -> ()
side_effect ref new_val = runST arg
where
arg :: exists s . ST s ()
arg = clobber_it
clobber_it :: ST MyStore ()
clobber_it = writeSTRef ref new_val
Note here how the definition `arg = clobber_it'
binds the existentially quantified variable `s' to `MyStore'.
This allows the side effect to escape the ST monad.
You could then go ahead and use this nasty side_effect:
main = print (runST nasty)
nasty :: ST MyStore Int
nasty = do x <- newSTRef 1
dummy <- newSTRef (side_effect x 42)
readSTRef x
This might print either 1 or 42, depending on whether side
effect in the construction of the unused `dummy' variable
was optimized out. Ouch!
--
Fergus Henderson <[EMAIL PROTECTED]> | "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh> | of excellence is a lethal habit"
PGP: finger [EMAIL PROTECTED] | -- the last words of T. S. Garp.