Elm Exactly

Rule 85d-match-first

MatchesE,v⊢match⇒v′/FAIL\jdgv{E}{v}{\match}{v'/\FAIL}
E,v⊢mrule⇒v′E,v⊢mrule ⟨; match⟩⇒v′\frac{\begin{array}{c}\jdgv{E}{v}{\mrule}{v'}\end{array}}{\begin{array}{c}\jdgv{E}{v}{\mrule\ \opt{\kw{;}\ \match}}{v'}\end{array}}
(85)
Read it in its chapter·Permanent address: /standard/rules/d-match-first

Comments

The value being matched stands to the left of the turnstile. The match rules are tried in the order written, and the first whose pattern matches gives the result; the match rules after it are not evaluated, even when their patterns would also match. This order is part of the meaning, and is unaffected by the freedom of evaluation order (Section 6.7), since a rule for a later match rule requires every earlier one to give FAIL.

TeX source
\defnrule[r:d-match-first]
  {\jdgv{E}{v}{\mrule}{v'}}
  {\jdgv{E}{v}{\mrule\ \opt{\kw{;}\ \match}}{v'}}

Tests (10)

  • elm / BoolAndTest
    The `&&` operator gives `True` only when both operands are `True`, checked over all four combinations.
  • elm / BoolOrTest
    The `||` operator gives `False` only when both operands are `False`, checked over all four combinations.
  • elm / CaseListThreeWayTest
    Checks that a `case` on a `List` with `[]`, `[x]` and `x :: xs` patterns, in that order, sends a one-element list to `[x]` rather than to the cons pattern.
  • elm / CaseTripleTest
    A case expression on a 3-tuple of `Int`s matches literal and wildcard patterns in order, taking the first branch that fits.
  • elm / CaseTupleLiteralTest
    A case expression on a pair of `Int`s matches literal and wildcard patterns in order, so a pair that fits several patterns takes the first.
  • elm / CaseTupleTest
    A case expression on a pair of `Int`s matches literal and wildcard patterns in order, taking the first branch that fits.
  • elm / MinimalCaseTest
    A case expression with one rule whose pattern is a variable.
  • elm / TailRecNestedCaseTest
    Self-recursive list functions whose tail call sits in an `if` branch or behind a `case` with several list patterns return the right results.
  • elm-core / EqualityBoolIfTest
    The result of `==` and `/=` on `Bool` operands drives an `if` expression correctly.
  • elm-core / NotEqualBoolIfTest
    The result of `/=` on two `Bool` literals is used as the condition of an `if`, for all four combinations of operands; each logs `"yes"` exactly when the operands differ.

Discussions

Discuss this rule·Search the discussions on GitHub

Discussions are on GitHub; one about this rule has [r:d-match-first] in its title.

A Standard Definition of Elm? (Draft version 0.4) © Rupert Smith. The text is licensed under CC BY 4.0: share and adapt it freely, with credit and a link to the licence.