GHC compiles it without a hitch, but it fails miserably at runtime:
many_a x =
let
a = 2
in
let
a = 2*a
in
x*a
Intuitively, this shouldn’t work. But yet GHC accepted it.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Yes, that’s valid Haskell code. The thing is that the
ain the secondlet...inexpression is actually a newa; it’s perfectly permissible to shadow existing variables by defining a new variable with the same name. However, it doesn’t affect the value of the outera. It’s generally considered bad style, however, due to the potential for confusion and mistakes; if you pass-Wallto GHC, it’ll issue a warning if you do this.It “fails” at runtime because you’ve defined
ato be2*a, which results in an infinite loop at runtime. This is due to laziness; basically,(*)evaluates both its arguments before multiplying them. Evaluating2works fine, of course, but evaluatingacauses it to go through the same process all over again. The same thing that allows infinite lists (likeones = 1:ones) makes this code be an infinite loop.