Skip to content
This repository was archived by the owner on Jun 21, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions texts/.gitkeep

This file was deleted.

116 changes: 116 additions & 0 deletions texts/0000-lambdacase.rst
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
Copy link

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.

Copy link

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.

#########

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)