If I understand the ST monad in Haskell correctly, runST
uses rank-2 types in a clever way to ensure that a computation does not reference any other thread when escaping the monad.
I have a toy language with a Hindley-Milner type system, and my question is the following: is it possible to extend the HM type system with an ad-hoc rule for typing runST
applications so that the ST monad is safely escapable, without introducing rank-2 types?
More precisely, runST
would have type forall s a. ST s a -> a
(i.e. rank-1) and the typing rule would first try to generalize the computation type in the same way HM generalizes types in let-expressions, but raise a type error if the s
type variable is found to be bound.
The above only restricts accepted programs compared to vanilla HM, so it seems sound, but I am unsure. Would this work?
Rust uses Hindley-Milner type inference. OCaml uses Hindley-Milner. Swift apparently uses a variant of the system with more features.
Rust performs type inference in fairly advanced situations.
Type inference is the ability to automatically deduce, either partially or fully, the type of an expression at compile time. The compiler is often able to infer the type of a variable or the type signature of a function, without explicit type annotations having been given.
Just in case the comments to the question are not entirely clear, the judgement you would need is
This is of course in conjunction with the other usual typing judgments that come with Hindley-Milner. Interestingly enough, we don't end up needing to make special rules for anything introducing an ST
type, since none of these require type signatures of rank 2:
newSTRef :: a -> ST s (STRef s a)
readSTRef :: STRef s a -> ST s a
writeSTRef :: STRef s a -> a -> ST s ()
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With