
Dear Cafe, a student in my class detected this inconsistency in the design of Haskell:
Why require that each "if-then" has an "else" while still allowing incomplete sets of patterns? We could define "if p then a" by translating to "case p of { True -> a }"
I think that "but then we'd have the dangling-else problem" is not a good answer because this is really about semantics, not surface syntax. For reference, Scheme has short-if, see http://www.r6rs.org/final/html/r6rs/r6rs-Z-H-14.html#node_sec_11.4.3 (value is "unspecified" if condition is false) and Common LISP does as well https://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node84.html#SECTION0011600000... (value is "nil") Was this considered at some point in Haskell's design process? There is Section 5.2 on pattern matching in http://haskell.cs.yale.edu/wp-content/uploads/2011/02/history.pdf (*) but it does not mention if-then(-else). - J.W. (*) can we please add this to https://www.haskell.org/documentation Upvote this: https://github.com/haskell-infra/hl/issues/86

On Mon, 2018-07-09 at 19:20 +0200, Johannes Waldmann wrote:
Dear Cafe,
a student in my class detected this inconsistency in the design of Haskell:
Why require that each "if-then" has an "else" while still allowing incomplete sets of patterns? We could define "if p then a" by translating to "case p of { True -> a }"
This is the same as (case p of { True -> a; _ -> throw PatternDoesNotMatchException }). Therefore (if False then a) would give an exception. Normally, every expression has a value. The value of (if p then x else y) is properly defined. What would be the value of (if p then x)? If it is (), then x should also have type (), I think. You can do something like that in a Monad, and the function is called when. And there is an unless, which is an if-else without a then.
I think that "but then we'd have the dangling-else problem" is not a good answer because this is really about semantics, not surface syntax.
For reference, Scheme has short-if, see http://www.r6rs.org/final/html/r6rs/r6rs-Z-H-14.html#node_sec_11.4.3 (value is "unspecified" if condition is false) and Common LISP does as well https://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node84.html#SECTION001 160000000000000000 (value is "nil")
Was this considered at some point in Haskell's design process? There is Section 5.2 on pattern matching in http://haskell.cs.yale.edu/wp-content/uploads/2011/02/history.pdf (*) but it does not mention if-then(-else).
- J.W.
(*) can we please add this to https://www.haskell.org/documentation Upvote this: https://github.com/haskell-infra/hl/issues/86 _______________________________________________
Hope this helps and kind regards, Arjen

If-then-else is a syntactic convenience. Missing cases are usually wrong. We don't want to go out of our way to make it convenient to write wrong code. On Mon, Jul 9, 2018, 1:20 PM Johannes Waldmann < johannes.waldmann@htwk-leipzig.de> wrote:
Dear Cafe,
a student in my class detected this inconsistency in the design of Haskell:
Why require that each "if-then" has an "else" while still allowing incomplete sets of patterns? We could define "if p then a" by translating to "case p of { True -> a }"
I think that "but then we'd have the dangling-else problem" is not a good answer because this is really about semantics, not surface syntax.
For reference, Scheme has short-if, see http://www.r6rs.org/final/html/r6rs/r6rs-Z-H-14.html#node_sec_11.4.3 (value is "unspecified" if condition is false) and Common LISP does as well
https://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node84.html#SECTION0011600000... (value is "nil")
Was this considered at some point in Haskell's design process? There is Section 5.2 on pattern matching in http://haskell.cs.yale.edu/wp-content/uploads/2011/02/history.pdf (*) but it does not mention if-then(-else).
- J.W.
(*) can we please add this to https://www.haskell.org/documentation Upvote this: https://github.com/haskell-infra/hl/issues/86 _______________________________________________ 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.

Was this considered at some point in Haskell's design process?
I don't know, but such an `if .. then` without `else` would encourage bad coding practices, IMO. On a related note: in C and friends if can do if () { .... }; but you can't do int x = E1 ? E2; So, even those pragmatic people (including those C++ people who like to include not just the kitchen sink but the surrounding farm as well) agree with Haskell's designers. Stefan

On Mon, Jul 9, 2018 at 12:20 PM Johannes Waldmann
Why require that each "if-then" has an "else" while still allowing incomplete sets of patterns? We could define "if p then a" by translating to "case p of { True -> a }"
Hi Johannes, `if p then a` can only ever have value `a`, so why not just write `a` in the first place? Of course, it could fail to evaluate... but that's not something (as a Haskell 1.0 program anyway) you could detect or recover. Incomplete case matches, in contrast, can do meaningful things---to name parts of a data structure, or perhaps reflecting a programmer-believed invariant. Sure, they let in degenerate cases, like `case p of {True -> a}`, but that's different from adding syntax to the language that only expresses degenerate cases. /g

On 2018-07-09 01:20 PM, Johannes Waldmann wrote:
a student in my class detected this inconsistency in the design of Haskell:
Why require that each "if-then" has an "else" while still allowing incomplete sets of patterns? We could define "if p then a" by translating to "case p of { True -> a }"
I think that "but then we'd have the dangling-else problem" is not a good answer because this is really about semantics, not surface syntax.
Anthony Clayden is right that if-then-else has the benefit of giving an unambiguous grammar. if-then can be made unambiguous if you throw in delimiting, e.g., if-then-fi, if-then-begin-end. Suppose you like to support if-then (and have your disambiguation story) on the ground that case-of-True is already supported, and you don't mind the partiality, then why stop there? Why is if-else getting no love from you? Afterall, case-of-False is already supported, too.
:)
participants (6)
-
Albert Y. C. Lai
-
arjenvanweelden@gmail.com
-
David Feuer
-
J. Garrett Morris
-
Johannes Waldmann
-
Stefan Monnier