# What does "1 = 2" mean in Haskell?

30 messages
12
Open this post in threaded view
|

## 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 _______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafeOnly members subscribed via the mailman list are allowed to post.
Open this post in threaded view
|

## Re: What does "1 = 2" mean in Haskell?

 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 associatesunix, 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-cafeOnly members subscribed via the mailman list are allowed to post.
Open this post in threaded view
|

## Re: What does "1 = 2" mean in Haskell?

 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.-harendraOn 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 associatesunix, 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-cafeOnly members subscribed via the mailman list are allowed to post.
Open this post in threaded view
|

## Re: What does "1 = 2" mean in Haskell?

 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.-harendraOn 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 associatesunix, openafs, kerberos, infrastructure, xmonad        http://sinenomine.net -- brandon s allbery kf8nh                               sine nomine associatesunix, 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-cafeOnly members subscribed via the mailman list are allowed to post.
Open this post in threaded view
|

## Re: What does "1 = 2" mean in Haskell?

 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.-harendraOn 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.-harendraOn 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 associatesunix, openafs, kerberos, infrastructure, xmonad        http://sinenomine.net -- brandon s allbery kf8nh                               sine nomine associatesunix, 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-cafeOnly members subscribed via the mailman list are allowed to post.
Open this post in threaded view
|

## Re: What does "1 = 2" mean in Haskell?

 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 = 2Mu.hs:3:1: Warning: This pattern-binding binds no variables: 1 = 2On Thu, Feb 23, 2017 at 10:11 PM, Harendra Kumar wrote: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.-harendraOn 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.-harendraOn 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 associatesunix, openafs, kerberos, infrastructure, xmonad        http://sinenomine.net -- brandon s allbery kf8nh                               sine nomine associatesunix, openafs, kerberos, infrastructure, xmonad        http://sinenomine.net -- brandon s allbery kf8nh                               sine nomine associatesunix, 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-cafeOnly members subscribed via the mailman list are allowed to post.
Open this post in threaded view
|

## Re: What does "1 = 2" mean in Haskell?

 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  _______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafeOnly members subscribed via the mailman list are allowed to post.
Open this post in threaded view
|

## Re: What does "1 = 2" mean in Haskell?

 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 associatesunix, 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-cafeOnly members subscribed via the mailman list are allowed to post.
Open this post in threaded view
|

## Re: What does "1 = 2" mean in Haskell?

 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 <[hidden email]> > wrote: > >> On 24 February 2017 at 08:45, Brandon Allbery <[hidden email]> 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 > [hidden email]                                  [hidden email] > 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 _______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafeOnly members subscribed via the mailman list are allowed to post.
Open this post in threaded view
|

## Re: What does "1 = 2" mean in Haskell?

 In reply to this post by Brandon Allbery 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 Intmain = print \$ let !Nothing = Data.Map.lookup "x" x in 10 x: x.hs:22:20-51: Irrefutable pattern failed for pattern Nothing-harendraOn 24 February 2017 at 09:21, Brandon Allbery <[hidden email]> 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 <[hidden email]> wrote:>>>> On 24 February 2017 at 08:45, Brandon Allbery <[hidden email]> 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> [hidden email]                                  [hidden email]> 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-cafeOnly members subscribed via the mailman list are allowed to post.
Open this post in threaded view
|

## Re: What does "1 = 2" mean in Haskell?

 In reply to this post by Harendra Kumar CCing the list. I guess you intended to cc but forgot.On 24 February 2017 at 09:27, wrote:In Erlang, the equivalent of a let fails. 1> 1=2. ** exception error: no match of right hand side value 2 In SML, the equivalent of a let fails. - val 1 = 1; - val 1 = 2; uncaught exception Bind [nonexhaustive binding failure]   raised at: stdIn:2.5-2.10 The problem is not that let 1 = 2 ... is *legal* but that - the compiler is  *silent* about it - the runtime is *silent* about it. Compiling the little program   main = let 1 = 2 in print "hi" I expected that the compiler would be silent but that there would be some sort of "matched failed" error at run time.  Silly me. The thing is, it is not just bindings that bind no variables that act as if they were not there.   main = let [x] = [1,2] in print "hi" also compiles silently and runs without error.  Change it to   main = let [x] = [1,2] in print ("hi" ++ show x) and you get a runtime error
Open this post in threaded view
|

## Re: What does "1 = 2" mean in Haskell?

 In reply to this post by Taeer Bar-Yam On Fri, Feb 24, 2017 at 12:00 AM, Taeer Bar-Yam wrote: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 associatesunix, 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-cafeOnly members subscribed via the mailman list are allowed to post.
Open this post in threaded view
|

## Re: What does "1 = 2" mean in Haskell?

 In reply to this post by Harendra Kumar On Fri, Feb 24, 2017 at 12:08 AM, Harendra Kumar wrote:I wish the compiler would report an error something like ": possibly failing match deleted              because it binds no live variables"Note that I showed the warning -Wall gives you earlier. -Werror also works... and, while I don't think it's in 8.0.x yet, future versions will allow selective conversions of warnings to errors.-- brandon s allbery kf8nh                               sine nomine associatesunix, 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-cafeOnly members subscribed via the mailman list are allowed to post.
Open this post in threaded view
|

## Re: What does "1 = 2" mean in Haskell?

Open this post in threaded view
|

## Re: What does "1 = 2" mean in Haskell?

 In reply to this post by Taeer Bar-Yam 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.-harendraOn 24 February 2017 at 10:30, Taeer Bar-Yam wrote: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 <[hidden email]> wrote: On 24 February 2017 at 08:45, Brandon Allbery <[hidden email]> 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 [hidden email]                                  [hidden email] 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 _______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafeOnly members subscribed via the mailman list are allowed to post.
Open this post in threaded view
|

## Re: What does "1 = 2" mean in Haskell?

Open this post in threaded view
|

## Re: What does "1 = 2" mean in Haskell?

 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.-- brandon s allbery kf8nh                               sine nomine associatesunix, 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-cafeOnly members subscribed via the mailman list are allowed to post.
Open this post in threaded view
|

## Re: What does "1 = 2" mean in Haskell?

 On Feb 23, 2017, at 9:49 PM, Brandon Allbery <[hidden email]> 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_______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafeOnly members subscribed via the mailman list are allowed to post.
Open this post in threaded view
|

## Re: What does "1 = 2" mean in Haskell?

 On 24 February 2017 at 11:25, Jeff Clites wrote:On Feb 23, 2017, at 9:49 PM, Brandon Allbery <[hidden email]> 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 _______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafeOnly members subscribed via the mailman list are allowed to post.