[m-users.] Why do state variables need explicit quantification?
Julian Fondren
jfondren at minimaltype.com
Wed Jul 31 05:47:07 AEST 2019
Howdy,
I looked through mailing list archives and didn't find an answer to
this question, so I'll ask it. When a state variable is introduced
in the body of a rule, instead of in its head, Mercury requires
explicit quantification:
main(!IO) :-
some [!RS] (
random.init(seed, !:RS),
random.random(0, 100, Roll, !.RS, _)
),
io.format("You rolled %d on the d100 die.\n", [i(Roll)], !IO).
Why is this the case? Why can't this just be written as follows?
main(!IO) :-
random.init(seed, !:RS),
random.random(0, 100, Roll, !.RS, _)
io.format("You rolled %d on the d100 die.\n", [i(Roll)], !IO).
(Putting aside that a single variable RS would work just fine in
this example.)
My guesses are
1. the Mercury compiler just has difficulty with implicit
quantification of state variables, for some reason. (i.e., a future
version of the language might relax this restriction.), or
2. in some cases it's difficult for the programmer to tell if that
first !:RS is introducing a new state variable rather than making
use of an existing one, especially in otherwise erroneous code
(i.e., this restriction is a deliberate language-design choice.)
3. this restriction is implied somehow by the transformation that
are applied to code containing state variables, to turn it into
'real' Mercury code. (i.e., the restriction might be lifted by
baking state variables deeper into the language, but nobody wants
to do this.)
To be clear, I don't think this is a big problem with the language.
I just'd like to be able to provide an answer, since you run into
the restriction immediately if you try using state variables other
than !IO, such as for arrays and hash tables.
More information about the users
mailing list