1 = 0?

51 views
Skip to first unread message

Alexey Muranov

unread,
Jun 17, 2014, 6:25:10 AM6/17/14
to haskel...@googlegroups.com
Is this the expected behavior that

    1 = 0

does not raise any error?  What does this mean?

Thanks.

Alexey.

Jan Stolarek

unread,
Jun 17, 2014, 6:50:06 AM6/17/14
to haskel...@haskell.org, Alexey Muranov, haskel...@googlegroups.com
Interesting. I just told GHC "let 5 = 3" and got no complaint. I'm curious to hear the rationale
for this.

Janek


_______________________________________________
Haskell-Cafe mailing list
Haskel...@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Roman Cheplyaka

unread,
Jun 17, 2014, 6:51:44 AM6/17/14
to Haskell Cafe
* Alexey Muranov <alexey....@gmail.com> [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

Roman

J. Waldmann

unread,
Jun 17, 2014, 6:54:06 AM6/17/14
to haskel...@haskell.org

> Interesting. I just told GHC "let 5 = 3" and got no complaint.

lazy pattern matching semantics, see:

ghci -XBangPatterns

Prelude> let { 0 = 1 } in 0
0

Prelude> let { ! 0 = 1 } in 0
*** Exception: <interactive>:3:7-13: Non-exhaustive patterns in pattern binding

Tom Ellis

unread,
Jun 17, 2014, 6:54:39 AM6/17/14
to haskel...@haskell.org
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

Vo Minh Thu

unread,
Jun 17, 2014, 6:55:19 AM6/17/14
to Roman Cheplyaka, Haskell Cafe
2014-06-17 12:51 GMT+02:00 Roman Cheplyaka <ro...@ro-che.info>:
> * Alexey Muranov <alexey....@gmail.com> [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

Erik Hesselink

unread,
Jun 17, 2014, 7:05:41 AM6/17/14
to Roman Cheplyaka, Haskell Cafe
On Tue, Jun 17, 2014 at 12:51 PM, Roman Cheplyaka <ro...@ro-che.info> wrote:
> * Alexey Muranov <alexey....@gmail.com> [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

Brent Yorgey

unread,
Jun 17, 2014, 7:08:45 AM6/17/14
to haskel...@haskell.org
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

Alexey Muranov

unread,
Jun 17, 2014, 7:14:53 AM6/17/14
to haskel...@googlegroups.com, haskel...@haskell.org

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.
Reply all
Reply to author
Forward
0 new messages