>> 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
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).
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:
> _______________________________________________
> 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-cafe
Only members subscribed via the mailman list are allowed to post.
x: x.hs:22:20-51: Irrefutable pattern failed for pattern Nothing
-harendraIn 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
<object>: <source>: Irrefutable pattern failed for pattern [x].
I wish the compiler would report an error something like
"<location>: possibly failing match deleted
because it binds no live variables"
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 wish the compiler would report an error something like
"<location>: possibly failing match deleted
because it binds no live variables"
_______________________________________________
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.
On Fri, Feb 24, 2017 at 12:38 AM, Harendra Kumar <harendr...@gmail.com> 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.
Furthermore, you would not want
```
main = let 1 = 2 in print "foo"
```
to error, since the pattern match is unused, and haskell is a lazy language.
Really, though, we probably shouldn't be putting incomplete pattern matches in
our code :P
--Taeer
Are pattern matches which produce no bindings useful in any case?
I've used that though in combination of guards where I call the
(lazily evaluated) result only in cases where it's irrefutable.
--
Ivan Lazar Miljenovic
Ivan.Mi...@gmail.com
http://IvanMiljenovic.wordpress.com
where
~(a, b) = ....
or whatever. I think a bang is good for a strict non-pattern binding, like
where
!a = ....
because that's the unusual case, but I think it's bad for a strict
pattern binding, which is the *usual* case.
That's not at all clear. In fact, I believe this thread exists
because the OP's daughter expected that it WOULD raise an error.
For what it's worth,
Start = let 1 = 2 in "hello"
is rejected by the Clean compiler, even though Clean is much
like Haskell, and 1 is otherwise allowed as a pattern.
If nothing else, it would be nice to have a "dead code" warning
from the compiler about code that is certain not to be evaluated.
"2" counts as dead code in this example.
If nothing else, it would be nice to have a "dead code" warning
from the compiler about code that is certain not to be evaluated.
"2" counts as dead code in this example.
>
> Furthermore, you would not want
> ```
> main = let 1 = 2 in print "foo"
> ```
> to error, since the pattern match is unused, and haskell is a lazy
> language.That's not at all clear. In fact, I believe this thread exists
because the OP's daughter expected that it WOULD raise an error.