
Is this the expected behavior that 1 = 0 does not raise any error? What does this mean? Thanks. Alexey.

Interesting. I just told GHC "let 5 = 3" and got no complaint. I'm curious to hear the rationale for this. Janek Dnia wtorek, 17 czerwca 2014, Alexey Muranov napisał:
Is this the expected behavior that
1 = 0
does not raise any error? What does this mean?
Thanks.
Alexey.

* Alexey Muranov
Is this the expected behavior that
1 = 0
does not raise any error? What does this mean?
1 is a valid (nullary) pattern, so yes. This pattern binding obviously fails. Prelude> let x@1 = 0 Prelude> x *** Exception: <interactive>:2:5-11: Irrefutable pattern failed for pattern x@1 Roman

2014-06-17 12:51 GMT+02:00 Roman Cheplyaka
* Alexey Muranov
[2014-06-17 03:25:10-0700] Is this the expected behavior that
1 = 0
does not raise any error? What does this mean?
1 is a valid (nullary) pattern, so yes. This pattern binding obviously fails.
Prelude> let x@1 = 0 Prelude> x *** Exception: <interactive>:2:5-11: Irrefutable pattern failed for pattern x@1
If it can make things clearer, you can do something similar with any constructor: > let Just x = Just 5 in x > let Just x = Nothing in x *** Exception: <interactive>:19:5-20: Irrefutable pattern failed for pattern Data.Maybe.Just x

On Tue, Jun 17, 2014 at 12:51 PM, Roman Cheplyaka
* Alexey Muranov
[2014-06-17 03:25:10-0700] Is this the expected behavior that
1 = 0
does not raise any error? What does this mean?
1 is a valid (nullary) pattern, so yes. This pattern binding obviously fails.
Prelude> let x@1 = 0 Prelude> x *** Exception: <interactive>:2:5-11: Irrefutable pattern failed for pattern x@1
It doesn't even have to fail, since numeric literals are overloaded: instance Num () where fromInteger _ = () v :: () v = let x@1 = 0 in x
v ()
Erik

On Tue, Jun 17, 2014 at 03:25:10AM -0700, Alexey Muranov wrote:
Is this the expected behavior that
1 = 0
does not raise any error? What does this mean?
I guess it's an irrefutable pattern match that doesn't bind any variables, so you can never see it fair. Cf Prelude> let (x, 1) = (2,3) Prelude> x *** Exception: <interactive>:7:5-18: Irrefutable pattern failed for pattern *** (x, 1) Prelude> let (x, 3) = (2,3) Prelude> x 2 I'll let someone more knowledgeable comment on why it's a good idea not to prevent this kind of thing. Tom

On Tue, Jun 17, 2014 at 03:25:10AM -0700, Alexey Muranov wrote:
Is this the expected behavior that
1 = 0
does not raise any error? What does this mean?
The general syntax of assignments is pattern = expression One most often uses a pattern consisting of a single variable, but any pattern will do. (For example, try writing [x,y,z] = [1,2,3] as a top-level declaration and see what happens!) 1 is a pattern which matches the number 1. 0 is obviously an expression. Of course, the pattern 1 does not match the expression 0. However, binding is lazy: for example, writing [x,y] = [1,2,3] does not in and of itself cause an error; it will only cause an error if you try to use x or y. However, since the pattern 1 contains no variables, there is no way to ever force the pattern-matching to actually take place. So 1 = 0 just sits there, sort of like an ugly, angry dog who wants to bite people except that it is locked in a cage with no door. Occasionally people walking by look at it pityingly, but mostly no one pays it any attention. -Brent

On Tuesday, June 17, 2014 1:08:45 PM UTC+2, Brent Yorgey wrote: The general syntax of assignments is
pattern = expression
One most often uses a pattern consisting of a single variable, but any pattern will do. (For example, try writing [x,y,z] = [1,2,3] as a top-level declaration and see what happens!) 1 is a pattern which matches the number 1. 0 is obviously an expression. Of course, the pattern 1 does not match the expression 0. However, binding is lazy: for example, writing [x,y] = [1,2,3] does not in and of itself cause an error; it will only cause an error if you try to use x or y. However, since the pattern 1 contains no variables, there is no way to ever force the pattern-matching to actually take place.
So 1 = 0 just sits there, sort of like an ugly, angry dog who wants to bite people except that it is locked in a cage with no door. Occasionally people walking by look at it pityingly, but mostly no one pays it any attention.
Thanks for the detailed explanation, i think i've understood. Alexey.
participants (8)
-
Alexey Muranov
-
Brent Yorgey
-
Erik Hesselink
-
J. Waldmann
-
Jan Stolarek
-
Roman Cheplyaka
-
Tom Ellis
-
Vo Minh Thu