<div dir="ltr">On Saturday, April 16, 2016 at 5:49:24 PM UTC+2, Roman Cheplyaka wrote:<blockquote class="gmail_quote" style="margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;">On 04/16/2016 02:53 PM, Alexey Muranov wrote:
<br>> Sorry for the question that probably is already answered somewhere, but i do not get it from the basic documentation i have looked at so far.
<br>> 
<br>> Is monomorphism restriction really necessary, or is it just a convenience?
<br>> 
<br>> In the example from A History of Haskell
<br>> 
<br>>     genericLength :: Num a => [b] -> a
<br>> 
<br>>     f xs = (len, len)
<br>>         where
<br>>             len = genericLength xs
<br>> 
<br>> can't the monomorphism restriction be replaced with just adding the signature
<br>> 
<br>>     f :: Num a => [b] -> (a, a)
<br>> 
<br>> ?
<br>
<br>No; the let binding 'len' would still generalize. It would generalize
<br>even if you assigned 'f' a monotype such as [b] -> (Int, Int).
<br>
<br>Now, the compiler *could* later realize that the polymorphic value 'len'
<br>is evaluated twice at the same type and perform a CSE, but I guess the
<br>standard authors were not comfortable with *requiring* that kind of
<br>optimization from all compliant compilers.
<br>
<br>Of course, nowadays we have scoped type variables, so MR is no longer
<br>strictly necessary. Sometimes it is desirable in that it gives the
<br>behavior that you would intuitively expect; sometimes, the opposite.
<br>
<br>Roman
</blockquote><div>  </div><div>Thank you, i will think about it, though i don't understand yet.</div><div><br></div><div>Could you show me, please, how to use scoped type variables to avoid MR in this example?</div><div><br></div><div>Alexey.</div></div>