[m-users.] Why do state variables need explicit quantification?
Zoltan Somogyi
zoltan.somogyi at runbox.com
Wed Jul 31 06:56:29 AEST 2019
On Tue, 30 Jul 2019 14:47:07 -0500, Julian Fondren <jfondren at minimaltype.com> wrote:
> 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.)
Yes, this is a deliberate design choice.
One of the principles we followed when designing Mercury is that
program readability is more important than program writeability.
Each line of code in a program is typically read many more times
than it is written, or modified. Therefore if we can make readers' jobs
easier at the cost of requiring a few extra keystrokes from writers,
we consider that a good tradeoff.
There are some cases, such as your example, where requiring
the writer to explicitly delineate the scope of a state variable
does not help much. But in other, longer, more complex cases,
the readability gain can be substantial. And having unambiguous
scopes allows the compiler to generate more specific, and thus
more helpful, error messages for code that has bugs involving
state variables.
> 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.
The head of a clause can introduce state variables that represent
*anything*, not just the I/O state. See for example list.foldl in the
standard library. And it is enough for the clause head to contain
either the initial or the final version of a state variable; it does not
need both (though having both is the common case).
Zoltan.
More information about the users
mailing list