[Haskell-cafe] Language support for imperative code. Was: Re:
brianh at metamilk.com
Wed Aug 8 13:39:10 EDT 2007
Martin Percossi wrote:
> Brian Hulley wrote:
>> hidden away in the definition of their API function to create a
>> label, is a call to (ref 0) !!!! ;-) The equivalent implementation in
>> Haskell would completely destroy all hope of using this in a pure
>> context and force all use of the API into the IO monad.
> Really? I would have thought this is a job for the ST monad, in which
> case the API can be wrapped up in a runST or similar; no need for
> leaking IO monads.
> Or am I missing something?
Well I agree you're right on this particular use of a Ref, since their
program is only dealing with a mapping from input to output so once
they're finished using the data structure there is no longer any need
for the ref and so the result can be returned to the rest of the program.
However there are 2 problems with this approach in general afaics:
1) All code that uses this data structure ie that participates in the
implementation of the transformations by using the API functions will
have to be in a monad (ST or IO, it really doesn't matter in terms of
all the perceived burdensomeness of do notation relative to normal
2) The example doesn't transfer to an interactive situation, where we
would need to keep the data structure around and use it forever -
because we would be forever trapped inside the ST monad otherwise we'd
lose that particular STRef which we were hoping to use to efficiently
update the output in the face of a delta in the input.
I found this page helpful to get an understanding of the value
restriction in ML:
The restriction was proposed by Andrew Wright in 1995:
"Simple Imperative Polymorphism" by Wright
Some other related papers are:
"The Type and effect discipline" by Talpin and Jouvelot 1992
"Standard ML-NJ weak polymorphism and imperative constructs" by Hoang,
Mitchell, and Viswanathan
"Weak polymorphism can be sound" by Greiner 1993
and more recently (2003)
"Relaxing the value restriction" by Garrigue
(Note that even now there is still no real solution to it.)
Best regards, Brian.
More information about the Haskell-Cafe