Elm Exactly

Rule 108d-pat-con

PatternsE,v⊢pat⇒VE/FAIL\jdgv{E}{v}{\pat}{\VE/\FAIL}
E(longcon)=conv=con v1⋯vnE,v1⊢atpat1⇒VE1/FAIL  ⋯  E,vn⊢atpatn⇒VEn/FAILE,v⊢longcon atpat1⋯atpatn⇒VE1/FAIL+⋯+VEn/FAIL\frac{\begin{array}{c}E(\longcon)=\con\premsep v=\con\ v_1\cdots v_n\\ \jdgv{E}{v_1}{\atpat_1}{\VE_1/\FAIL}\ \ \cdots\ \ \jdgv{E}{v_n}{\atpat_n}{\VE_n/\FAIL}\end{array}}{\begin{array}{c}\jdgv{E}{v}{\longcon\ \atpat_1\cdots\atpat_n} {\VE_1/\FAIL+\cdots+\VE_n/\FAIL}\end{array}}
(108)
Read it in its chapter·Permanent address: /standard/rules/d-pat-con

Comments

Here n≥1n\geq 1, and by elaboration n=arity(con)n=\arity(\con). All the argument patterns are matched, and the result is FAIL when any of them fails. Since matching has no effect and always has a result, the order in which the arguments are matched is immaterial.

TeX source
\defnrule[r:d-pat-con]
  {E(\longcon)=\con\premsep v=\con\ v_1\cdots v_n\\
   \jdgv{E}{v_1}{\atpat_1}{\VE_1/\FAIL}\ \ \cdots\ \
   \jdgv{E}{v_n}{\atpat_n}{\VE_n/\FAIL}}
  {\jdgv{E}{v}{\longcon\ \atpat_1\cdots\atpat_n}
        {\VE_1/\FAIL+\cdots+\VE_n/\FAIL}}

Tests (69)

  • elm / CaseCtorArgPatternTest
    A single-constructor custom type can be destructured by a pattern in a function parameter, as in `getId (Person id _)`.
  • elm / CaseCustomTypeTest
    A case on a custom type whose constructors carry different numbers of arguments binds those arguments in each branch.
  • elm / CaseDeeplyNestedTest
    Case expressions on `Maybe` nested three levels deep reach the right innermost branch.
  • 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 / CaseMaybePairTest
    Checks that a `case` on a tuple of two `Maybe Int` values, with one branch for each of the four `Just`/`Nothing` combinations, picks the right branch and binds the right values in each.
  • elm / CaseMaybeTest
    Checks that a `case` on a `Maybe Int` returns the wrapped value for `Just` and a default of -1 for `Nothing`.
  • 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 / CaseMultiFieldExtractTest
    Checks that a `case` on a custom type binds every argument of a two-argument and a four-argument constructor to the right variable.
  • elm / CaseNestedCtorTest
    Checks that constructor patterns can nest: a constructor pattern inside another (`Container (Wrap n)`), and a tuple pattern inside a constructor (`Box ( a, x )`).
  • elm / CaseNestedRecordAccessTest
    Checks a function that destructures a constructor in its argument and then cases on a looked-up value, calling another function that also destructures a constructor argument and cases on a record field.
  • elm / CaseNestedTest
    Checks that a `case` on one `Maybe` with a further `case` on a second `Maybe` inside each branch reaches all four combinations correctly.
  • elm / CaseResultTest
    Checks that a `case` on a `Result` returns the `Ok` value, and -1 for any `Err`.
  • elm / CaseSharedBranchTest
    Checks a `case` on a custom type whose first branch holds a nested `case` on the constructor's `String` argument.
  • elm / CaseSingleCtorBoolMultiTypeTest
    A case expression matches `True` and `False` literal patterns inside a single-constructor custom type wrapping a `Bool`, while another single-constructor type, wrapping an `Int`, is defined in the same module.
  • elm / CaseSingleCtorBoolTest
    A case expression matches `True` and `False` literal patterns inside a single-constructor custom type wrapping a `Bool`.
  • elm / CaseTreeTest
    A case expression on a recursive custom type, a binary tree, drives a recursive function that counts the tree's nodes.
  • elm / CharAstralCustomFieldTest
    An astral `Char` (code point above 0xFFFF) stored in a custom-type constructor's field keeps its code point when read back by pattern matching.
  • elm / CharLiteralPatternMatchMinimalTest
    A Char literal pattern for the double quote `'"'` matches at increasing depths of nesting: bare, inside a tuple, inside a `Maybe`, and inside a `Maybe ( Char, String )`.
  • elm / ClosureCapture01Test
    A lambda returned from a function captures a single-constructor custom-type argument and unwraps it with `case` when called.
  • elm / ClosureCapture02Test
    A lambda returned from a function captures a `Maybe String` argument and matches on it with `case` when called.
  • elm / ClosureCapture05Test
    A lambda returned from a function captures a custom-type value holding two `Maybe String`s, and when called matches on it and then on each `Maybe` in turn.
  • elm / ClosureCapture06Test
    A two-argument function matches on a custom-type value holding two `Maybe String`s and then on each `Maybe` in turn.
  • elm / CovLambdaSetFunctionsInRecordsTuplesAndMaybeTest
    Functions stored in record fields, tuples and a `Maybe`, taken out again and applied.
  • elm / CustomTypeFuncFieldTest
    A custom type whose single constructor wraps a function can be unwrapped by an argument pattern and the function called.
  • elm / CustomTypePatternTest
    Functions that take apart a custom-type value with a constructor pattern in their argument extract the right field.
  • elm / EitherTypeTest
    A custom type with two type parameters and one constructor for each can be built, mapped and pattern-matched.
  • elm / EmbeddedNilInCustomTypeTest
    An empty list stored in a custom type constructor and taken back out by a `case` is still empty, with length 0.
  • elm / EmbeddedNothingCallbackTest
    A function that pattern-matches a `Maybe` returns `Nothing` for `Nothing` and applies its function argument to a `Just` payload.
  • elm / EmbeddedNothingInCustomTypeTest
    A `Nothing` stored in a custom type constructor and taken back out by a `case` still matches the `Nothing` pattern.
  • elm / EmbeddedNothingInTupleTest
    A tuple pattern in a `let` takes `Nothing` and `Just 42` back out of a pair, and each matches the right pattern.
  • elm / EmbeddedNothingMapTest
    A local `map` with a function that matches `Maybe` maps each `Nothing` to -1 and each `Just` to its payload.
  • elm / EmbeddedReturnNothingTest
    A function that returns `Nothing` for one input and `Just` for another is consumed by a `case` in the caller.
  • elm / EscapeCustomCaseScrutineeTest
    A value of a user-defined custom type, built in the call and taken apart by a `case` in another function, yields its payload.
  • elm / HoleyAliasTwoParamsTest
    A type alias with two type parameters, used as a constructor field, keeps each parameter bound to its own argument.
  • elm / LargeDispatchCaseTest
    Checks a `case` on a list of custom-type values whose branches contain a nested `case` on a String.
  • elm / LetDestructNestedSingleConstructorNumberTest
    A `let` destructuring pattern through two nested single-constructor types reads a number literal that is then used as a Float.
  • elm / LetNumberMaybeCaseDirectTest
    An integer literal inside `Just`, taken out by a `case` pattern and multiplied by `1.5`, is used as a `Float`: `round (n * 1.5)` with `n` bound from `Just 30` logs 45.
  • elm / LetNumberNestedCustomTest
    An integer literal nested two custom-type constructors deep is used as a `Float`: `Box (NumBox 30)`, matched by the pattern `Box (NumBox n)` and multiplied by `1.5`, logs 45.
  • elm / LetNumberUnionBranchTest
    Integer literals carried by a constructor of a two-constructor custom type with a `number` type parameter are used as `Float`s: `Pair 30 40`, matched by `Pair a b` and computing `round (a * 1.5 + b * 2.5)`, logs 145.
  • elm / LssGapCtorScale
    Constructors of arity one to four build values that pattern matches in function arguments take apart again.
  • elm / LssGapCustomTypeFn
    A function stored in a custom type constructor can be pattern matched out and applied: `useBox (Box double) 7` prints 14.
  • elm / MaybePatternMatchTest
    A `case` on a `Maybe` takes the `Just` branch to extract the value and the `Nothing` branch otherwise.
  • elm / MinimalConstructedPatternTest
    A constructor pattern with an argument matches and binds the argument.
  • elm / NestedCaseReturnTest
    A `case` on a custom type whose branches return the result of another `case` on a String, either inline or through a helper, returns the right string.
  • elm / PapEtaCtorProbe
    Tests point-free top-level functions defined by partially applying a two-argument function, stored in a constructor, pattern-matched back out and applied.
  • elm / PolyChainDictLikeInsertChained5TimesWithTupleKeysConcreteCallerTest
    Tests a polymorphic `insert` on a list-like dictionary type, called five times in a chain from a function with concrete `( String, Int )` keys and `Int` values.
  • elm / RecursiveMutualTypeTest
    A pair of mutually recursive custom types, `Forest` and `RoseTree`, can be built and traversed.
  • elm / RecursiveTreeTest
    A recursive binary tree type can be summed and measured with recursive functions.
  • elm / SingleCtorPairBoolCharTest
    Pattern matching works on two single-constructor types in one module, one wrapping a `Bool` and one wrapping a `Char`.
  • elm / SingleCtorPairBoolFloatTest
    Pattern matching works on two single-constructor types in one module, one wrapping a `Bool` and one wrapping a `Float`.
  • elm / SingleCtorPairBoolIntBidiTest
    Pattern matching and value extraction both work on two single-constructor types in one module, one wrapping a `Bool` and one wrapping an `Int`.
  • elm / SingleCtorPairCharFloatTest
    Values are extracted correctly from two single-constructor types in one module, one wrapping a `Char` and one wrapping a `Float`.
  • elm / SingleCtorPairCharIntTest
    Values are extracted correctly from two single-constructor types in one module, one wrapping a `Char` and one wrapping an `Int`.
  • elm / SingleCtorPairFloatBoolTest
    Pattern matching works on two single-constructor types in one module, one wrapping a `Float` and one wrapping a `Bool`.
  • elm / SingleCtorPairIntFloatTest
    Values are extracted correctly from two single-constructor types in one module, one wrapping an `Int` and one wrapping a `Float`.
  • elm / SingleCtorPairStringBoolTest
    Pattern matching works on two single-constructor types in one module, one wrapping a `String` and one wrapping a `Bool`.
  • elm / SingleCtorPairStringIntTest
    Values are extracted correctly from two single-constructor types in one module, one wrapping a `String` and one wrapping an `Int`.
  • elm / SingleCtorPairUnitIntTest
    Pattern matching works on two single-constructor types in one module, one wrapping the unit value `()` and one wrapping an `Int`.
  • 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 / TailRecCustomCaseTest
    A self tail-recursive function that matches on a user-defined list type and destructures its `Cons` constructor gives the right result: summing `Cons 1 (Cons 2 (Cons 3 (Cons 4 Nil)))` is 10.
  • elm / TailRecDeciderSearchTest
    A local function defined in a `let` that calls itself both in a tail position and in a non-tail position, as the scrutinee of a `case`, gives the right result.
  • elm / TailRecDecoderLoopTest
    A recursive function whose outer `case` is on a custom type and whose inner `case` is on a `String` returns the right result for each branch.
  • elm / TailRecManyBranchStringTest
    A self-recursive function over a three-constructor custom type, with a tail call in one branch and a `case` in another that matches a `String` against literal patterns, returns the right string.
  • 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 / TailRecNestedStringCaseTest
    A self-recursive function whose non-recursive branch matches a `String` against literal patterns returns the matched value after unwrapping several layers.
  • elm / TailRecTypeTraversalTest
    A function over a small type-expression tree, tail-recursive through its alias constructor and matching primitive type names as string literals, resolves an alias chain to the right name.
  • elm / WideCtorField24Test
    A custom type constructor with 27 fields, 25 `Int`s followed by a `Float` and a `Char`, keeps every field, and pattern matching reads back the later ones.
  • elm / WideCtorMixedTest
    A custom type constructor with 60 fields, cycling through `Int`, `Float`, `Char`, `String` and `Bool`, supports pattern matching, equality and printing.
  • elm / WrapperUnionFieldTest
    A single-constructor custom type can wrap a record whose field is itself a custom type, and a nested `case` reads the payload of that field.

Discussions

Discuss this rule·Search the discussions on GitHub

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