Why can I bind values to values at Haskell?

Viewed 81

If I will start ghci and enter something like 4 = 5 or 1.2 = 3.4 it will accept it but nothing is printed. However, after this 4 is still 4 and not 5. Why didn't GHC throw any error like "cannot assign to literal"?

1 Answers

Consider

> let 4=5 in 4
4

> let x@4=5 in x
*** Exception: <interactive>:1423:5-9: Irrefutable pattern failed for pattern x@4

The returned 4 in the first snippet does not refer to the 4 in LHS. The returned 4 is just another literal.

The returned x in the second does, and so the pattern 4 (not a literal 4) is matched with the value of the literal 5. And that matching fails.

Anyway this does not of course affect any subsequent interactions because the binding is confined to the let expression. And if you use let without the in as in older versions of GHCi, to indeed affect the following interactions, there's nothing to force that binding at all, so it stays idle and void.

Related