This repository was archived by the owner on Jun 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Add LambdaCase proposal #3
Open
quchen
wants to merge
2
commits into
haskell:master
Choose a base branch
from
quchen:lambdacase
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| - Feature Name: LambdaCase | ||
| - Start Date: 2016-07-23 | ||
| - RFC PR: | ||
| - Haskell Report Issue: | ||
|
|
||
|
|
||
|
|
||
| ####### | ||
| Summary | ||
| ####### | ||
|
|
||
| For convenience, add the shorthand | ||
|
|
||
| .. code-block:: haskell | ||
|
|
||
| \case >>> \x -> case x of | ||
| Foo a -> … >>> Foo a -> … | ||
| … >>> … | ||
|
|
||
| where ``x`` is a fresh variable. | ||
|
|
||
| This is currently implemented via the ``LambdaCase`` GHC extension. | ||
|
|
||
|
|
||
|
|
||
| ########## | ||
| Motivation | ||
| ########## | ||
|
|
||
|
|
||
| ``LambdaCase`` is a syntactic convenience feature whose sole purpose is making | ||
| code a bit cleaner. | ||
|
|
||
|
|
||
| Example 1: Multiple pattern matches | ||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
|
||
| The following example repeats the function name multiple times: | ||
|
|
||
| .. code-block:: haskell | ||
|
|
||
| myFunction (Foo (Just a)) = … | ||
| myFunction (Bar b) = … | ||
| myFunction (Qux (a,b)) = … | ||
|
|
||
| Using lambda case, this could be refactored to | ||
|
|
||
| .. code-block:: haskell | ||
|
|
||
| myFunction = \case | ||
| Foo (Just a) -> … | ||
| Bar b -> … | ||
| Qux (a,b) -> … | ||
|
|
||
| This lacks one level of parentheses, indents the patterns to make the top-level | ||
| definition stand out more, and avoids repeating the function name multiple | ||
| times. | ||
|
|
||
|
|
||
| Example 2: Monadic binding | ||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
|
||
| To pass a value through a monadic chain of binds, pattern matching on an | ||
| intermediate result requires creating a new variable whose only purpose is to be | ||
| scrutinized by a ``case`` again in the next step: | ||
|
|
||
| .. code-block:: haskell | ||
|
|
||
| main = foo >>= \x -> case x of Just bar -> … | ||
| Nothing -> … | ||
|
|
||
| Lambda case would clean this up by getting rid of the intermediate variable, | ||
|
|
||
| .. code-block:: haskell | ||
|
|
||
| main = foo >>= \case Just bar -> … | ||
| Nothing -> … | ||
|
|
||
|
|
||
|
|
||
| ############### | ||
| Detailed design | ||
| ############### | ||
|
|
||
| The Summary_ already describes this to sufficient detail. | ||
|
|
||
|
|
||
|
|
||
| ######### | ||
| Drawbacks | ||
| ######### | ||
|
|
||
| There are no technical drawbacks. Since ``case`` is a reserved keyword in | ||
| Haskell, it cannot be used as a variable name. Therefore, in standard Haskell, | ||
| ``\case`` is invalid. Thus, this proposal gives meaning to a previously | ||
| non-existing construct, resulting in no breakage. | ||
|
|
||
| Feature-wise, it is worth noting that lambda case does not support lambdas with | ||
| multiple arguments well. In this case, explicitly naming the `case` scrutinee, | ||
| as is current (standard Haskell) practice, explicitly. | ||
|
|
||
|
|
||
|
|
||
| ############ | ||
| Alternatives | ||
| ############ | ||
|
|
||
| No alternatives tackling the same issue are known (to me). | ||
|
|
||
|
|
||
|
|
||
| #################### | ||
| Unresolved questions | ||
| #################### | ||
|
|
||
| (none) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A drawback which comes to mind, actually, is the inability to match multiple arguments with this syntax. I think we ought to note this in the RFC.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And now as i re-read this thread i see @goldfirere already brought this up, derp.
Anyhow, thanks for noting in RFC.