Elm Exactly

Rule 110d-pat-cons

PatternsE,v⊢pat⇒VE/FAIL\jdgv{E}{v}{\pat}{\VE/\FAIL}
v=v1::v2E,v1⊢pat1⇒VE1/FAILE,v2⊢pat2⇒VE2/FAILE,v⊢pat1 :: pat2⇒VE1/FAIL+VE2/FAIL\frac{\begin{array}{c}v=v_1\mathbin{\kw{::}}v_2\\ \jdgv{E}{v_1}{\pat_1}{\VE_1/\FAIL}\premsep \jdgv{E}{v_2}{\pat_2}{\VE_2/\FAIL}\end{array}}{\begin{array}{c}\jdgv{E}{v}{\pat_1\ \kw{::}\ \pat_2}{\VE_1/\FAIL+\VE_2/\FAIL}\end{array}}
(110)
Read it in its chapter·Permanent address: /standard/rules/d-pat-cons
TeX source
\defnrule[r:d-pat-cons]
  {v=v_1\mathbin{\kw{::}}v_2\\
   \jdgv{E}{v_1}{\pat_1}{\VE_1/\FAIL}\premsep
   \jdgv{E}{v_2}{\pat_2}{\VE_2/\FAIL}}
  {\jdgv{E}{v}{\pat_1\ \kw{::}\ \pat_2}{\VE_1/\FAIL+\VE_2/\FAIL}}

Tests (25)

  • elm / CaseAsPatternTest
    An as-pattern in a case branch, `((x :: _) as whole)`, binds both the list head and the whole list.
  • elm / CaseFanOutShadowTest
    A function that destructures a custom type in its parameter and then cases on a `Result` combines the bound field with the `Ok` value, or returns -1 for `Err`.
  • elm / CaseListConsTest
    Checks that a recursive function matching a list against `[]` and `x :: xs` sums the empty list, a one-element list and a three-element list correctly.
  • elm / CaseListTest
    Checks that a `case` on a `List` distinguishes the empty list, a list of exactly one element (`[_]`) and any longer list (`_`).
  • 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 / CaseMixedPatternTest
    Checks that a single `case` can match a custom type constructor whose argument is itself matched as a list, with `x :: _` and `[]` patterns nested inside the constructor pattern.
  • elm / DeepNestStressTest
    Deeply nested lists, tuples, records and `let` expressions can be built and taken apart.
  • elm / EmbeddedNilRecursiveTest
    A recursive function that returns `[]` for an empty list builds the doubled positive elements of its input, skipping the rest.
  • elm / EmbeddedNothingRecursiveTest
    A recursive list search returns `Just` the first matching element, or `Nothing` once it reaches the end of the list.
  • elm / EmbeddedReturnNilTest
    A recursive filter returns the empty list when no element passes, and the caller receives it like any other list.
  • elm / ErasedListSpecializationTest
    A polymorphic self-recursive `count : List a -> Int` gives the right length when called both on `List Int` values and on `[]`, whose element type nothing constrains.
  • elm / FunctionAccumulatorFunctionLookupTest
    Looking up a function in a list of key and function pairs, and applying the function found, gives the right result.
  • elm / HigherOrderFilterLikeFunctionTest
    A let-bound function that takes a predicate and a list, and keeps the list's head when the predicate holds, gives `[1]` for a predicate that always returns `True`.
  • elm / LargeDispatchCaseTest
    Checks a `case` on a list of custom-type values whose branches contain a nested `case` on a String.
  • elm / LetNumberListConsHeadTest
    The head of a list of `number` literals, matched by a `::` pattern in a `case` and multiplied by a `Float`, is a `Float`.
  • elm / LetNumberListFixedTest
    Both elements of a list of `number` literals, matched by the fixed-length list pattern `[ a, b ]` and multiplied by a `Float`, are `Float`s.
  • elm / MinimalConsPatternTest
    A list construction pattern matches a non-empty list.
  • elm / RecursiveListLengthTest
    A self-recursive function that pattern matches on `[]` and `_ :: rest` counts the elements of lists of length 0, 3 and 5.
  • elm / StandardCoreEvaluationExamplesTest
    Small programs for rules of the dynamic semantics of the Core that other tests reach only in passing, and examples from the Comments of A Standard Definition of Elm?, Chapter 6.
  • elm / TailRecBoolCarryTest
    A self tail-recursive function that carries a `Bool` argument from one call to the next, and sets it from a branch inside a `case`, returns the right result.
  • elm / TailRecCaseDestructTest
    Self tail-recursive functions whose tail calls sit in a `case` branch that destructures a list with `x :: xs` give the right results.
  • elm / TailRecCaseMultiBranchTypesTest
    A self tail-recursive search whose outer `case` has one branch per constructor of a custom type at the head of a list, with a nested `case` on a `String` inside one branch, finds the right element.
  • elm / TailRecFoldlCaseTest
    A hand-written left fold, defined as a self tail-recursive local function in `main`'s `let` and matching on the list with `case`, sums `[ 1, 2, 3 ]` to 6.
  • elm / TailRecMultiCaseWhileTest
    A tail-recursive fold over a list of custom-type values, with a `case` on string literals nested inside one list pattern and tail calls in every branch, accumulates the right pair of totals.
  • 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.

Discussions

Discuss this rule·Search the discussions on GitHub

Discussions are on GitHub; one about this rule has [r:d-pat-cons] 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.