Let's say you want to write some stateful function in Haskell. You have to use a monadic style like this: (using whatever state monad)
f :: x -> k -> (ST s) r
So this means that essentially the function takes some input x
and k
, might use and/or modify the world to compute a return value r
.
Assume x
is a stateful structure, that might be modified by f
. Assume k
is just a simple key type used for example to access something in x
. k
itself will be assigned a simple number type later, but we don't want to have to decide now of its type.
So essentially I know that x
is a mutable thing, and k
is immutable.
The problem is just looking at f
's signature, we cannot tell that, so if f
occurs in the body of some more complex monadic code we can't reason very well about those variables.
Example:
g :: x -> k -> (ST s) r
g a i = do
...
f a i -- I don't know if i :: k depends on state
... --- I don't know if i was changed by f
What I mean is that if I was given a variable i
of unknown type k
, I don't know whether or not it depends on s
and whether its value can be affected by a call of f
.
This problem of course does not exist when writing pure functions, since everything is immutable.
Is there a way to conveniently annotate and, more importantly, statically enforce that k
will remain unchanged in the ST monad when calling f
?