Rule 108d-pat-con
Patterns
(108)
Comments
Here , and by elaboration . 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 / CaseCtorArgPatternTestA single-constructor custom type can be destructured by a pattern in a function parameter, as in `getId (Person id _)`.
- elm / CaseCustomTypeTestA case on a custom type whose constructors carry different numbers of arguments binds those arguments in each branch.
- elm / CaseDeeplyNestedTestCase expressions on `Maybe` nested three levels deep reach the right innermost branch.
- elm / CaseFanOutShadowTestA 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 / CaseMaybePairTestChecks 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 / CaseMaybeTestChecks that a `case` on a `Maybe Int` returns the wrapped value for `Just` and a default of -1 for `Nothing`.
- elm / CaseMixedPatternTestChecks 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 / CaseMultiFieldExtractTestChecks that a `case` on a custom type binds every argument of a two-argument and a four-argument constructor to the right variable.
- elm / CaseNestedCtorTestChecks that constructor patterns can nest: a constructor pattern inside another (`Container (Wrap n)`), and a tuple pattern inside a constructor (`Box ( a, x )`).
- elm / CaseNestedRecordAccessTestChecks 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 / CaseNestedTestChecks that a `case` on one `Maybe` with a further `case` on a second `Maybe` inside each branch reaches all four combinations correctly.
- elm / CaseResultTestChecks that a `case` on a `Result` returns the `Ok` value, and -1 for any `Err`.
- elm / CaseSharedBranchTestChecks a `case` on a custom type whose first branch holds a nested `case` on the constructor's `String` argument.
- elm / CaseSingleCtorBoolMultiTypeTestA 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 / CaseSingleCtorBoolTestA case expression matches `True` and `False` literal patterns inside a single-constructor custom type wrapping a `Bool`.
- elm / CaseTreeTestA case expression on a recursive custom type, a binary tree, drives a recursive function that counts the tree's nodes.
- elm / CharAstralCustomFieldTestAn 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 / CharLiteralPatternMatchMinimalTestA 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 / ClosureCapture01TestA lambda returned from a function captures a single-constructor custom-type argument and unwraps it with `case` when called.
- elm / ClosureCapture02TestA lambda returned from a function captures a `Maybe String` argument and matches on it with `case` when called.
- elm / ClosureCapture05TestA 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 / ClosureCapture06TestA two-argument function matches on a custom-type value holding two `Maybe String`s and then on each `Maybe` in turn.
- elm / CovLambdaSetFunctionsInRecordsTuplesAndMaybeTestFunctions stored in record fields, tuples and a `Maybe`, taken out again and applied.
- elm / CustomTypeFuncFieldTestA custom type whose single constructor wraps a function can be unwrapped by an argument pattern and the function called.
- elm / CustomTypePatternTestFunctions that take apart a custom-type value with a constructor pattern in their argument extract the right field.
- elm / EitherTypeTestA custom type with two type parameters and one constructor for each can be built, mapped and pattern-matched.
- elm / EmbeddedNilInCustomTypeTestAn empty list stored in a custom type constructor and taken back out by a `case` is still empty, with length 0.
- elm / EmbeddedNothingCallbackTestA function that pattern-matches a `Maybe` returns `Nothing` for `Nothing` and applies its function argument to a `Just` payload.
- elm / EmbeddedNothingInCustomTypeTestA `Nothing` stored in a custom type constructor and taken back out by a `case` still matches the `Nothing` pattern.
- elm / EmbeddedNothingInTupleTestA tuple pattern in a `let` takes `Nothing` and `Just 42` back out of a pair, and each matches the right pattern.
- elm / EmbeddedNothingMapTestA local `map` with a function that matches `Maybe` maps each `Nothing` to -1 and each `Just` to its payload.
- elm / EmbeddedReturnNothingTestA function that returns `Nothing` for one input and `Just` for another is consumed by a `case` in the caller.
- elm / EscapeCustomCaseScrutineeTestA value of a user-defined custom type, built in the call and taken apart by a `case` in another function, yields its payload.
- elm / HoleyAliasTwoParamsTestA type alias with two type parameters, used as a constructor field, keeps each parameter bound to its own argument.
- elm / LargeDispatchCaseTestChecks a `case` on a list of custom-type values whose branches contain a nested `case` on a String.
- elm / LetDestructNestedSingleConstructorNumberTestA `let` destructuring pattern through two nested single-constructor types reads a number literal that is then used as a Float.
- elm / LetNumberMaybeCaseDirectTestAn 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 / LetNumberNestedCustomTestAn 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 / LetNumberUnionBranchTestInteger 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 / LssGapCtorScaleConstructors of arity one to four build values that pattern matches in function arguments take apart again.
- elm / LssGapCustomTypeFnA function stored in a custom type constructor can be pattern matched out and applied: `useBox (Box double) 7` prints 14.
- elm / MaybePatternMatchTestA `case` on a `Maybe` takes the `Just` branch to extract the value and the `Nothing` branch otherwise.
- elm / MinimalConstructedPatternTestA constructor pattern with an argument matches and binds the argument.
- elm / NestedCaseReturnTestA `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 / PapEtaCtorProbeTests point-free top-level functions defined by partially applying a two-argument function, stored in a constructor, pattern-matched back out and applied.
- elm / PolyChainDictLikeInsertChained5TimesWithTupleKeysConcreteCallerTestTests 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 / RecursiveMutualTypeTestA pair of mutually recursive custom types, `Forest` and `RoseTree`, can be built and traversed.
- elm / RecursiveTreeTestA recursive binary tree type can be summed and measured with recursive functions.
- elm / SingleCtorPairBoolCharTestPattern matching works on two single-constructor types in one module, one wrapping a `Bool` and one wrapping a `Char`.
- elm / SingleCtorPairBoolFloatTestPattern matching works on two single-constructor types in one module, one wrapping a `Bool` and one wrapping a `Float`.
- elm / SingleCtorPairBoolIntBidiTestPattern matching and value extraction both work on two single-constructor types in one module, one wrapping a `Bool` and one wrapping an `Int`.
- elm / SingleCtorPairCharFloatTestValues are extracted correctly from two single-constructor types in one module, one wrapping a `Char` and one wrapping a `Float`.
- elm / SingleCtorPairCharIntTestValues are extracted correctly from two single-constructor types in one module, one wrapping a `Char` and one wrapping an `Int`.
- elm / SingleCtorPairFloatBoolTestPattern matching works on two single-constructor types in one module, one wrapping a `Float` and one wrapping a `Bool`.
- elm / SingleCtorPairIntFloatTestValues are extracted correctly from two single-constructor types in one module, one wrapping an `Int` and one wrapping a `Float`.
- elm / SingleCtorPairStringBoolTestPattern matching works on two single-constructor types in one module, one wrapping a `String` and one wrapping a `Bool`.
- elm / SingleCtorPairStringIntTestValues are extracted correctly from two single-constructor types in one module, one wrapping a `String` and one wrapping an `Int`.
- elm / SingleCtorPairUnitIntTestPattern matching works on two single-constructor types in one module, one wrapping the unit value `()` and one wrapping an `Int`.
- elm / TailRecCaseMultiBranchTypesTestA 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 / TailRecCustomCaseTestA 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 / TailRecDeciderSearchTestA 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 / TailRecDecoderLoopTestA 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 / TailRecManyBranchStringTestA 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 / TailRecMultiCaseWhileTestA 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 / TailRecNestedStringCaseTestA self-recursive function whose non-recursive branch matches a `String` against literal patterns returns the matched value after unwrapping several layers.
- elm / TailRecTypeTraversalTestA 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 / WideCtorField24TestA 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 / WideCtorMixedTestA custom type constructor with 60 fields, cycling through `Int`, `Float`, `Char`, `String` and `Bool`, supports pattern matching, equality and printing.
- elm / WrapperUnionFieldTestA 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.