Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does let y = 1 + y compile, and what does it mean?

Tags:

haskell

In GHCi let y = y + 1 compiles fine, but when I try to evaluate y I got *** Exception: <<loop>>

Why is there no compile error and what does it mean <<loop>>?

like image 584
Aler Avatar asked Dec 18 '14 23:12

Aler


1 Answers

Haskell let, where and top-level bindings are recursive by default, even if they're not for a function. So let y = y + 1 defines an infinite loop of adding 1 to a number. GHC represents loops like this with the <<loop>> exception—if it can catch them, of course!

This is usable for lazy operations, because it allows us to easily define things like infinite lists (let xs = 0:xs), which are well-defined and actually useful for normal code. However, it can't work for strict operations like + (for most numeric types) because they require evaluating the whole (infinite) thing right away.

like image 144
Tikhon Jelvis Avatar answered Nov 04 '22 23:11

Tikhon Jelvis