What does "1 = 2" mean in Haskell?

Kids have this amazing ability to break any toy in minutes. I gave my seven year old daughter ghci to play with and in a little while she said it is broken:
let 1 = 2
1
1
Earlier, I had explained to her about symbols and assigning values to symbols, and I said numbers are not symbols. But when she came up with this I could not explain what's going on. How can "1 = 2" be a valid equation? Am I missing something fundamental here, or it is just broken? -harendra

On Thu, Feb 23, 2017 at 9:05 PM, Harendra Kumar
Kids have this amazing ability to break any toy in minutes. I gave my seven year old daughter ghci to play with and in a little while she said it is broken:
let 1 = 2
1
1
Earlier, I had explained to her about symbols and assigning values to symbols, and I said numbers are not symbols. But when she came up with this I could not explain what's going on. How can "1 = 2" be a valid equation? Am I missing something fundamental here, or it is just broken?
It's a pattern match. The match fails, but as it produced no bindings it cannot be observed and its success or failure is irrelevant. -- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net

My first guess was a pattern match, but it sounded a bit odd because there
is no explicit constructor in case of numbers. If there were an explicit
constructor it would have been easier to imagine this as a pattern match.
This seems to be a weird side effect of the special handling of numbers.
-harendra
On 24 February 2017 at 07:37, Brandon Allbery
On Thu, Feb 23, 2017 at 9:05 PM, Harendra Kumar
wrote: Kids have this amazing ability to break any toy in minutes. I gave my seven year old daughter ghci to play with and in a little while she said it is broken:
let 1 = 2
1
1
Earlier, I had explained to her about symbols and assigning values to symbols, and I said numbers are not symbols. But when she came up with this I could not explain what's going on. How can "1 = 2" be a valid equation? Am I missing something fundamental here, or it is just broken?
It's a pattern match. The match fails, but as it produced no bindings it cannot be observed and its success or failure is irrelevant.
-- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net

It is, yes. (Literal numbers in patterns occasionally have unexpected type
ramifications as a result; and occasionally others, since the compiler
rewrites the pattern match into a guard. It's one of those things that Just
Works 99% of the time and then makes you tear your hair out.)
On Thu, Feb 23, 2017 at 9:56 PM, Harendra Kumar
My first guess was a pattern match, but it sounded a bit odd because there is no explicit constructor in case of numbers. If there were an explicit constructor it would have been easier to imagine this as a pattern match. This seems to be a weird side effect of the special handling of numbers.
-harendra
On 24 February 2017 at 07:37, Brandon Allbery
wrote: On Thu, Feb 23, 2017 at 9:05 PM, Harendra Kumar
wrote:
Kids have this amazing ability to break any toy in minutes. I gave my seven year old daughter ghci to play with and in a little while she said it is broken:
let 1 = 2
1
1
Earlier, I had explained to her about symbols and assigning values to symbols, and I said numbers are not symbols. But when she came up with this I could not explain what's going on. How can "1 = 2" be a valid equation? Am I missing something fundamental here, or it is just broken?
It's a pattern match. The match fails, but as it produced no bindings it cannot be observed and its success or failure is irrelevant.
-- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net
-- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net

Are pattern matches which produce no bindings useful in any case? Will it
be possible or a good idea for the compiler to produce warnings in such
cases? This seems to be just a no-op.
-harendra
On 24 February 2017 at 08:30, Brandon Allbery
It is, yes. (Literal numbers in patterns occasionally have unexpected type ramifications as a result; and occasionally others, since the compiler rewrites the pattern match into a guard. It's one of those things that Just Works 99% of the time and then makes you tear your hair out.)
On Thu, Feb 23, 2017 at 9:56 PM, Harendra Kumar
wrote: My first guess was a pattern match, but it sounded a bit odd because there is no explicit constructor in case of numbers. If there were an explicit constructor it would have been easier to imagine this as a pattern match. This seems to be a weird side effect of the special handling of numbers.
-harendra
On 24 February 2017 at 07:37, Brandon Allbery
wrote: On Thu, Feb 23, 2017 at 9:05 PM, Harendra Kumar < harendra.kumar@gmail.com> wrote:
Kids have this amazing ability to break any toy in minutes. I gave my seven year old daughter ghci to play with and in a little while she said it is broken:
let 1 = 2
1
1
Earlier, I had explained to her about symbols and assigning values to symbols, and I said numbers are not symbols. But when she came up with this I could not explain what's going on. How can "1 = 2" be a valid equation? Am I missing something fundamental here, or it is just broken?
It's a pattern match. The match fails, but as it produced no bindings it cannot be observed and its success or failure is irrelevant.
-- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net
-- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net

Literally the only use I've seen for this was a CCC puzzle. However, it is
the trivial case of something that is more useful: pattern matching the
result of an expression (say, a Data.Map.lookup when you know the key
exists).
pyanfar Z$ ghc -c -Wall Mu.hs
Mu.hs:3:1: Warning:
Defaulting the following constraint(s) to type ‘Integer’
(Eq a0) arising from the literal ‘1’ at Mu.hs:3:1
(Num a0) arising from the literal ‘1’ at Mu.hs:3:1
In the pattern: 1
In a pattern binding: 1 = 2
Mu.hs:3:1: Warning: This pattern-binding binds no variables: 1 = 2
On Thu, Feb 23, 2017 at 10:11 PM, Harendra Kumar
Are pattern matches which produce no bindings useful in any case? Will it be possible or a good idea for the compiler to produce warnings in such cases? This seems to be just a no-op.
-harendra
On 24 February 2017 at 08:30, Brandon Allbery
wrote: It is, yes. (Literal numbers in patterns occasionally have unexpected type ramifications as a result; and occasionally others, since the compiler rewrites the pattern match into a guard. It's one of those things that Just Works 99% of the time and then makes you tear your hair out.)
On Thu, Feb 23, 2017 at 9:56 PM, Harendra Kumar
wrote:
My first guess was a pattern match, but it sounded a bit odd because there is no explicit constructor in case of numbers. If there were an explicit constructor it would have been easier to imagine this as a pattern match. This seems to be a weird side effect of the special handling of numbers.
-harendra
On 24 February 2017 at 07:37, Brandon Allbery
wrote: On Thu, Feb 23, 2017 at 9:05 PM, Harendra Kumar < harendra.kumar@gmail.com> wrote:
Kids have this amazing ability to break any toy in minutes. I gave my seven year old daughter ghci to play with and in a little while she said it is broken:
> let 1 = 2
> 1
1
>
Earlier, I had explained to her about symbols and assigning values to symbols, and I said numbers are not symbols. But when she came up with this I could not explain what's going on. How can "1 = 2" be a valid equation? Am I missing something fundamental here, or it is just broken?
It's a pattern match. The match fails, but as it produced no bindings it cannot be observed and its success or failure is irrelevant.
-- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net
-- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net
-- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net

On 24 February 2017 at 08:45, Brandon Allbery
Literally the only use I've seen for this was a CCC puzzle. However, it is the trivial case of something that is more useful: pattern matching the result of an expression (say, a Data.Map.lookup when you know the key exists).
Can you explain how that will be useful (without a binding)? Will the pattern match be ever actually tried when there is no binding? -harendra

Without a binding it is useless at top level, but if you strictify the
pattern it can be useful in `let` (possibly as a sanity check where you
want the program to abort if it fails). I don't recall offhand if it
desugars usefully in list comprehensions, but if so it would work as a
filter. There may also be other specialized use cases; general syntax tends
to get reused a lot in Haskell, so making this case a syntax error could
make it difficult to support actually useful cases. :)
(Also I'm sure someone overly clever could figure out some way to abuse it.
:)
On Thu, Feb 23, 2017 at 10:41 PM, Harendra Kumar
On 24 February 2017 at 08:45, Brandon Allbery
wrote: Literally the only use I've seen for this was a CCC puzzle. However, it is the trivial case of something that is more useful: pattern matching the result of an expression (say, a Data.Map.lookup when you know the key exists).
Can you explain how that will be useful (without a binding)? Will the pattern match be ever actually tried when there is no binding?
-harendra
-- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net

Since nobody has provided an example use case, I will. It's not particularly useful, but it's a minimal case that does something interesting. func :: (Int, a) -> Maybe a func (0, x) = Just x func _ = Nothing Excerpts from Brandon Allbery's message of February 23, 2017 10:51 pm:
Without a binding it is useless at top level, but if you strictify the pattern it can be useful in `let` (possibly as a sanity check where you want the program to abort if it fails). I don't recall offhand if it desugars usefully in list comprehensions, but if so it would work as a filter. There may also be other specialized use cases; general syntax tends to get reused a lot in Haskell, so making this case a syntax error could make it difficult to support actually useful cases. :)
(Also I'm sure someone overly clever could figure out some way to abuse it. :)
On Thu, Feb 23, 2017 at 10:41 PM, Harendra Kumar
wrote: On 24 February 2017 at 08:45, Brandon Allbery
wrote: Literally the only use I've seen for this was a CCC puzzle. However, it is the trivial case of something that is more useful: pattern matching the result of an expression (say, a Data.Map.lookup when you know the key exists).
Can you explain how that will be useful (without a binding)? Will the pattern match be ever actually tried when there is no binding?
-harendra
-- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net _______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post.
--Taeer

On Fri, Feb 24, 2017 at 12:00 AM, Taeer Bar-Yam
Since nobody has provided an example use case, I will. It's not particularly useful, but it's a minimal case that does something interesting.
I spent about 10 minutes trying to construct one that made any sense. :/ -- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net

Pattern match in a function definition is different. Here the pattern match
is actually being used as part of the function definition. We were trying
to figure out the usefulness of non-function pattern matches which do not
result in a binding. The assert case pointed out by Brandon is one example.
Though not so useful.
-harendra
On 24 February 2017 at 10:30, Taeer Bar-Yam
Since nobody has provided an example use case, I will. It's not particularly useful, but it's a minimal case that does something interesting.
func :: (Int, a) -> Maybe a func (0, x) = Just x func _ = Nothing
Excerpts from Brandon Allbery's message of February 23, 2017 10:51 pm:
Without a binding it is useless at top level, but if you strictify the pattern it can be useful in `let` (possibly as a sanity check where you want the program to abort if it fails). I don't recall offhand if it desugars usefully in list comprehensions, but if so it would work as a filter. There may also be other specialized use cases; general syntax tends to get reused a lot in Haskell, so making this case a syntax error could make it difficult to support actually useful cases. :)
(Also I'm sure someone overly clever could figure out some way to abuse it. :)
On Thu, Feb 23, 2017 at 10:41 PM, Harendra Kumar < harendra.kumar@gmail.com> wrote:
On 24 February 2017 at 08:45, Brandon Allbery
wrote:
Literally the only use I've seen for this was a CCC puzzle. However, it
is the trivial case of something that is more useful: pattern matching the result of an expression (say, a Data.Map.lookup when you know the key exists).
Can you explain how that will be useful (without a binding)? Will the pattern match be ever actually tried when there is no binding?
-harendra
-- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net _______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post.
--Taeer

Yes, if you use a bang pattern in a let clause it can act as an assert in a
strict context (e.g. in IO Monad). But, it can't even be caught as an
exception (or can it?). For example, this fails:
x = fromList [("x", 1)] :: Map String Int
main = print $ let !Nothing = Data.Map.lookup "x" x in 10
x: x.hs:22:20-51: Irrefutable pattern failed for pattern Nothing
-harendra
On 24 February 2017 at 09:21, Brandon Allbery
Without a binding it is useless at top level, but if you strictify the
pattern it can be useful in `let` (possibly as a sanity check where you want the program to abort if it fails). I don't recall offhand if it desugars usefully in list comprehensions, but if so it would work as a filter. There may also be other specialized use cases; general syntax tends to get reused a lot in Haskell, so making this case a syntax error could make it difficult to support actually useful cases. :)
(Also I'm sure someone overly clever could figure out some way to abuse
it. :)
On Thu, Feb 23, 2017 at 10:41 PM, Harendra Kumar
On 24 February 2017 at 08:45, Brandon Allbery
wrote:
Literally the only use I've seen for this was a CCC puzzle. However, it
is the trivial case of something that is more useful: pattern matching the result of an expression (say, a Data.Map.lookup when you know the key exists).
Can you explain how that will be useful (without a binding)? Will the
wrote: pattern match be ever actually tried when there is no binding?
-harendra
-- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net

It seems weird however that Haskell allows let 1 = 0 but does not allow let (f x, g y z) = (x*x, y*z) Alexey. On Friday, February 24, 2017 at 4:53:59 AM UTC+1, Brandon Allbery wrote:
Without a binding it is useless at top level, but if you strictify the pattern it can be useful in `let` (possibly as a sanity check where you want the program to abort if it fails). I don't recall offhand if it desugars usefully in list comprehensions, but if so it would work as a filter. There may also be other specialized use cases; general syntax tends to get reused a lot in Haskell, so making this case a syntax error could make it difficult to support actually useful cases. :)
(Also I'm sure someone overly clever could figure out some way to abuse it. :)
On Thu, Feb 23, 2017 at 10:41 PM, Harendra Kumar
javascript:> wrote: On 24 February 2017 at 08:45, Brandon Allbery
javascript:> wrote: Literally the only use I've seen for this was a CCC puzzle. However, it is the trivial case of something that is more useful: pattern matching the result of an expression (say, a Data.Map.lookup when you know the key exists).
Can you explain how that will be useful (without a binding)? Will the pattern match be ever actually tried when there is no binding?
-harendra
-- brandon s allbery kf8nh sine nomine associates allb...@gmail.com javascript: ball...@sinenomine.net javascript: unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net

This works too: Nothing = Just "hello" so you get the same effect even without any literal number specialness. Even this: Just x = Nothing also "works" until you force evaluation of x, as an irrefutable (lazy) pattern match. So in a way, you could view the first case as a lazy pattern match in which there is nothing you could possibly force, so there's no way to manifest the pattern match failure. Just another way of looking at it. JEff
On Feb 23, 2017, at 7:00 PM, Brandon Allbery
wrote: It is, yes. (Literal numbers in patterns occasionally have unexpected type ramifications as a result; and occasionally others, since the compiler rewrites the pattern match into a guard. It's one of those things that Just Works 99% of the time and then makes you tear your hair out.)
On Thu, Feb 23, 2017 at 9:56 PM, Harendra Kumar
wrote: My first guess was a pattern match, but it sounded a bit odd because there is no explicit constructor in case of numbers. If there were an explicit constructor it would have been easier to imagine this as a pattern match. This seems to be a weird side effect of the special handling of numbers. -harendra
On 24 February 2017 at 07:37, Brandon Allbery
wrote: On Thu, Feb 23, 2017 at 9:05 PM, Harendra Kumar
wrote: Kids have this amazing ability to break any toy in minutes. I gave my seven year old daughter ghci to play with and in a little while she said it is broken: let 1 = 2
1
1
Earlier, I had explained to her about symbols and assigning values to symbols, and I said numbers are not symbols. But when she came up with this I could not explain what's going on. How can "1 = 2" be a valid equation? Am I missing something fundamental here, or it is just broken?
It's a pattern match. The match fails, but as it produced no bindings it cannot be observed and its success or failure is irrelevant.
-- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net
-- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net _______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post.

In these examples, we can identify the constructor (capitalized first
letter) on the LHS and so we are trained to know that it is a pattern
match. The original point related to number specialness was that "1 = 2" is
not easily identifiable as a pattern match because there are no explicit
constructors. The literal "1" here is neither an "explicit constructor" nor
a binding symbol.
-harendra
On 24 February 2017 at 10:48, Jeff Clites
This works too:
Nothing = Just "hello"
so you get the same effect even without any literal number specialness.
Even this:
Just x = Nothing
also "works" until you force evaluation of x, as an irrefutable (lazy) pattern match. So in a way, you could view the first case as a lazy pattern match in which there is nothing you could possibly force, so there's no way to manifest the pattern match failure.
Just another way of looking at it.
JEff
On Feb 23, 2017, at 7:00 PM, Brandon Allbery
wrote: It is, yes. (Literal numbers in patterns occasionally have unexpected type ramifications as a result; and occasionally others, since the compiler rewrites the pattern match into a guard. It's one of those things that Just Works 99% of the time and then makes you tear your hair out.)
On Thu, Feb 23, 2017 at 9:56 PM, Harendra Kumar
wrote: My first guess was a pattern match, but it sounded a bit odd because there is no explicit constructor in case of numbers. If there were an explicit constructor it would have been easier to imagine this as a pattern match. This seems to be a weird side effect of the special handling of numbers.
-harendra
On 24 February 2017 at 07:37, Brandon Allbery
wrote: On Thu, Feb 23, 2017 at 9:05 PM, Harendra Kumar < harendra.kumar@gmail.com> wrote:
Kids have this amazing ability to break any toy in minutes. I gave my seven year old daughter ghci to play with and in a little while she said it is broken:
let 1 = 2
1
1
Earlier, I had explained to her about symbols and assigning values to symbols, and I said numbers are not symbols. But when she came up with this I could not explain what's going on. How can "1 = 2" be a valid equation? Am I missing something fundamental here, or it is just broken?
It's a pattern match. The match fails, but as it produced no bindings it cannot be observed and its success or failure is irrelevant.
-- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net
-- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net
_______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post.
_______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post.

On Fri, Feb 24, 2017 at 12:38 AM, Harendra Kumar
In these examples, we can identify the constructor (capitalized first letter) on the LHS and so we are trained to know that it is a pattern match. The original point related to number specialness was that "1 = 2" is not easily identifiable as a pattern match because there are no explicit constructors. The literal "1" here is neither an "explicit constructor" nor a binding symbol.
Yes, at this point you just have to know that the Report specifies a bunch of special handling for numeric literals. -- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net

On Feb 23, 2017, at 9:49 PM, Brandon Allbery
wrote: On Fri, Feb 24, 2017 at 12:38 AM, Harendra Kumar
wrote: In these examples, we can identify the constructor (capitalized first letter) on the LHS and so we are trained to know that it is a pattern match. The original point related to number specialness was that "1 = 2" is not easily identifiable as a pattern match because there are no explicit constructors. The literal "1" here is neither an "explicit constructor" nor a binding symbol. Yes, at this point you just have to know that the Report specifies a bunch of special handling for numeric literals.
Also: "day" = "night" Isn't every "=" a pattern match? JEff

On 24 February 2017 at 11:25, Jeff Clites
On Feb 23, 2017, at 9:49 PM, Brandon Allbery
wrote: On Fri, Feb 24, 2017 at 12:38 AM, Harendra Kumar
wrote:
In these examples, we can identify the constructor (capitalized first letter) on the LHS and so we are trained to know that it is a pattern match. The original point related to number specialness was that "1 = 2" is not easily identifiable as a pattern match because there are no explicit constructors. The literal "1" here is neither an "explicit constructor" nor a binding symbol.
Yes, at this point you just have to know that the Report specifies a bunch of special handling for numeric literals.
Also: "day" = "night"
Isn't every "=" a pattern match?
Every "=" with a constructor on the LHS. String is also a special case similar to numeric literals. The above example should be equivalent to: 'd' : 'a' : 'y' : [] = "night" -harendra

Just to link this to a previous discussion of this topic: https://groups.google.com/d/msg/haskell-cafe/2zpIvI0IBSc/MuHmTEUiswUJ Alexey. On Friday, February 24, 2017 at 3:06:16 AM UTC+1, Harendra Kumar wrote:
Kids have this amazing ability to break any toy in minutes. I gave my seven year old daughter ghci to play with and in a little while she said it is broken:
let 1 = 2
1
1
Earlier, I had explained to her about symbols and assigning values to symbols, and I said numbers are not symbols. But when she came up with this I could not explain what's going on. How can "1 = 2" be a valid equation? Am I missing something fundamental here, or it is just broken?
-harendra
participants (5)
-
Alexey Muranov
-
Brandon Allbery
-
Harendra Kumar
-
Jeff Clites
-
Taeer Bar-Yam