Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does an unused let binding have any effect in Haskell?

Tags:

syntax

haskell

I just realised that it is actually legal to write this:

let _ = sum [1..100]
in  "Hello"

The let-binding appears to do absolutely nothing.

But now I'm wondering about the exact semantics here. It is possible to write a program which contains a _ binding, and yet deleting that binding would visibly alter the meaning of said program?

Basically, I'm wondering whether it's safe to automatically delete such bindings. As far as I can tell, the value of this binding cannot possibly affect anything. However, it seems hypothetically possible that it's type might affect something else. Can anybody construct an example?

like image 311
MathematicalOrchid Avatar asked Jul 27 '14 19:07

MathematicalOrchid


1 Answers

Here is one example. With the _ binding, the output is 8.0, but without it, it is 8. (Admittedly, this isn't a very big difference, but I'm sure this could be used as a basis for something more substantial.)

main :: IO ()
main = let x = 5
           _ = asTypeOf x 6.0
       in print $ x + 3
like image 161
jwodder Avatar answered Oct 05 '22 13:10

jwodder