Elm Exactly

Rule 19s-dec-group

DeclarationsC⊢dec⇒VE\jdg{C}{\dec}{\VE}
C+VE⊢valbind⇒VEVE′=ClosCVEC⊢valbind⇒VE′\frac{\begin{array}{c}\jdg{C + \VE}{\valbind}{\VE} \premsep \VE' = \Clos_C \VE\end{array}}{\begin{array}{c}\jdg{C}{\valbind}{\VE'}\end{array}}
(19)
Read it in its chapter·Permanent address: /standard/rules/s-dec-group

Comments

A declaration is one group of mutually dependent bindings; a let with several definitions is a derived form that divides them into groups (Appendix A). Modifying CC by VE\VE on the left puts every member of the group in scope in every member’s expression. Since VE\VE is the same on both sides, an unannotated member has one type throughout the group: it is monomorphic within the group, and is generalised only by the closure, for the body of the let. An annotated member enters VE\VE with the type scheme of its annotation, so it may be used polymorphically within the group, in its own body included.

The closure applies to every member whatever its expression, so there is no value restriction: in let n = 5 in ( n // 2, n / 2 ) the variable n is used at Int\tInt and at Float\tFloat. The closure leaves the scheme of an annotated member unchanged, since its free variables are explicit variables of CC.

Referred to from

TeX source
\defnrule[r:s-dec-group]
  {\jdg{C + \VE}{\valbind}{\VE} \premsep \VE' = \Clos_C \VE}
  {\jdg{C}{\valbind}{\VE'}}

Tests (77)

  • elm / AnnotatedLetRecFnResultTest
    A recursive let-bound function whose type annotation has a function as its result works.
  • elm / CombinatorStdlibTCombinatorPipeListIntoComposedOpsTest
    The T combinator, built from S and K inside a `let`, passing `[ 1, 2, 3 ]` to the composition of a sum and a doubling map: the result is 12.
  • elm / CompilerIssue1766LetMutualRecursionMismatchTest
    Mutually recursive let functions give a type a false "mismatch" with itself.
  • elm / CompilerIssue1793ComparableTupleLetCrashTest
    Comparing tuples containing a `comparable` in a let binding crashes the compiler.
  • elm / CompilerIssue2057CallSiteChangesInferredTypeTest
    Two annotated callers of a function with unannotated mutually recursive let functions are wrongly rejected.
  • elm / LetMutualRecursionBetweenLocalsTest
    Two functions defined in the same `let` call each other.
  • elm / LetNumberAndThenTest
    An unannotated `let` binding `Just 30`, whose literal has the constrained type `number`, is used at `Float` inside `Maybe.andThen`: `round (30 * 1.5)` gives 45.
  • elm / LetNumberApplyToTest
    An unannotated `let`-bound number literal reaches a Float multiplication only through a generic higher-order function: `round (applyTo (\x -> x * 1.5) n)` with `n = 30` gives 45.
  • elm / LetNumberBoxedSilentMiscompileTest
    Number literals placed inside a container by an unannotated `let` binding, then taken out and used as Floats, give the correct Float results.
  • elm / LetNumberCaseLetTest
    An unannotated number literal bound by a `let` inside a `case` branch, then used as a Float: `round (30 * 1.5)` gives 45.
  • elm / LetNumberClosureInRecordTest
    An anonymous function that uses an unannotated `let`-bound number is stored in a record field and then called with a Float: `rec.f 1.5` computes `1.5 * 30`, logged rounded as 45.
  • elm / LetNumberContainerDualTest
    One unannotated `let`-bound tuple of number literals is used both as Ints and as Floats.
  • elm / LetNumberCustomTriple3Test
    A single-constructor type whose three fields share one type variable, built from number literals in an unannotated `let` and matched by `case` with every field used as a Float: `round 45 + round 60 + round 75` gives 180.
  • elm / LetNumberCustomTypeTest
    A let-bound custom type value whose type parameter is `number`, built from an integer literal, has its payload extracted by `case` and multiplied by a `Float`.
  • elm / LetNumberDeepNestRecordTest
    A `number` literal in a record that is itself a field of a let-bound record is read through two field accesses and used as a `Float`.
  • elm / LetNumberDualUseTest
    An unannotated let-bound `number` value can be used as an `Int` and as a `Float` in the same expression, because Elm generalizes the type of a `let` binding and each use picks its own type.
  • elm / LetNumberFloatArithCrashTest
    An unannotated let-bound `number` literal passed to `sqrt`, or combined with a `Float` by `(+)`, `(/)` or `(^)`, is a `Float`.
  • elm / LetNumberFoldrTest
    A let-bound list of `number` literals is folded by `List.foldr` with a `Float` accumulator, which makes its elements `Float`s.
  • elm / LetNumberFracRecordTest
    The two `number` fields of a let-bound record literal are typed independently, one used as an `Int` and the other as a `Float`.
  • elm / LetNumberIfBranchTest
    A let-bound `number` passed through an `if` expression whose result is multiplied by a `Float` is a `Float`.
  • elm / LetNumberIndirectCaptureTest
    A let-bound `number` captured by an anonymous function and multiplied by that function's `Float` argument is a `Float`.
  • elm / LetNumberIndirectDualTest
    A let-bound `number` used once in a `Float` multiplication and once in an unconstrained addition takes a type at each use independently.
  • elm / LetNumberIndirectFoldlTest
    A let-bound `number` used as the initial accumulator of `List.foldl (+)` over a list of `Float`s is a `Float`.
  • elm / LetNumberIndirectIdentityTest
    A let-bound `number` passed through `identity` before a `Float` multiplication is a `Float`.
  • elm / LetNumberIndirectTopFnTest
    A let-bound `number` passed to a top-level function annotated `Float -> Float` is a `Float`.
  • elm / LetNumberLetInLambdaTest
    A `number` bound in a `let` inside a top-level function's body and multiplied by a `Float` is a `Float`.
  • elm / LetNumberListCaseTest
    A `number` computed by a `case` over a let-bound list can be used both as an `Int` and as a `Float`, because Elm generalizes the type of a `let` binding.
  • elm / LetNumberListOfClosuresTest
    A let-bound `number` captured by two anonymous functions stored in a list, each applied to a `Float`, is a `Float`.
  • elm / LetNumberListOfMaybeTest
    `number` literals inside `Just` values in a let-bound list, mapped with a `Float` multiplication, are `Float`s.
  • elm / LetNumberListOfRecordsTest
    `number` fields of records in a let-bound list, mapped with a `Float` multiplication, are `Float`s.
  • elm / LetNumberLocalMap2Test
    A list of integer literals combined with a list of `Float`s by a two-argument mapping function is used at `Float`: `[ 30, 40 ]` scaled by 1.5 and rounded logs `[45,60]`.
  • elm / LetNumberLogBaseTest
    An unannotated let-bound integer literal, passed to `logBase`, is used as a `Float`: `round (logBase 10 n)` with `n = 100` logs 2.
  • elm / LetNumberMaybeChainTest
    An integer literal inside `Just`, run through two `Maybe.map` calls, is used as a `Float`: `Just 30` multiplied by `1.5`, divided by 3 and rounded logs 15.
  • elm / LetNumberMulFnTest
    An unannotated let-bound integer literal passed to a user function annotated `Float -> Float -> Float` is used as a `Float`: `round (mulF 1.5 n)` with `n = 30` logs 45.
  • elm / LetNumberNegateTest
    An unannotated let-bound integer literal passed through `negate` twice and then to `sqrt` is used as a `Float`: `round (sqrt (negate (negate n)))` with `n = 144` logs 12.
  • 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 / LetNumberNestedLetTest
    An integer literal bound in one `let` and re-bound in a nested `let` is used as a `Float`: `n = 30`, then `m = n`, then `round (m * 1.5)` logs 45.
  • elm / LetNumberRecordMultiFieldTest
    Two integer-literal fields of one let-bound record, read as `r.lo` and `r.hi`, are each used as a `Float`: `round (r.lo * 1.5) + round (r.hi * 1.5)` with `r = { lo = 30, hi = 40 }` logs 105.
  • elm / LetNumberRecordToFnTest
    An unannotated let-bound integer literal, placed in a record passed to a function that takes `{ v : Float }`, is used as a `Float`: `round (useFloat { v = base })` with `base = 30` logs 45.
  • elm / LetNumberResultMapTest
    An integer literal inside `Ok`, mapped by `Result.map`, is used as a `Float`: `Result.map (\x -> round (x * 1.5)) (Ok 30)`, unwrapped by a `case`, logs 45.
  • elm / LetNumberScaleFnTest
    A let-bound record of integer literals passed to a function that takes a record of `Float`s is used at `Float`: `round (scale 1.5 p)` with `p = { lo = 30, hi = 40 }` logs 105.
  • elm / LetNumberSumMapTest
    A let-bound list of integer literals, mapped to `Float`s and summed, is used at `Float`: `round (List.sum (List.map (\x -> x * 1.5) [ 30, 40 ]))` logs 105.
  • elm / LetNumberTupleBothTest
    Both integer-literal elements of a let-bound pair are used as `Float`s: `round (Tuple.first p * 1.5) + round (Tuple.second p * 1.5)` with `p = ( 30, 40 )` logs 105.
  • elm / LetNumberTupleSecondTest
    The second integer-literal element of a let-bound pair, read with `Tuple.second`, is used as a `Float` while the first is never used: `round (Tuple.second p * 1.5)` with `p = ( 99, 30 )` logs 45.
  • elm / LetNumberTupleToFnTest
    A let-bound pair of integer literals passed to a function that takes `( Float, Float )` is used at `Float`: `round (useTuple p)` with `p = ( 30, 40 )` logs 105.
  • 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 / LetRecClosureTest
    A recursive function defined in a `let` block captures a parameter of the enclosing function and is used by an earlier binding of the same `let`.
  • elm / LetRecMultiArgTest
    A recursive two-argument function defined in a `let` block calls itself with both arguments changed; `f 1 1` logs `result: 2`.
  • elm / LocalMultiNonTailRecTest
    A let-bound recursive function that is not tail recursive, with an unannotated polymorphic type, is used at both Int and Float in the same body.
  • elm / LocalTailRecSimpleTest
    An annotated let-bound tail-recursive function with an accumulator computes the sum 1 + 2 + ... + 10 = 55.
  • elm / MultiLocalTailRecTest
    Two independent self-recursive functions defined in the same `let` block, each calling itself in tail position, both run correctly.
  • elm / MutualLetRecClosuresTest
    Two `let`-bound functions that call each other (`evenP` and `oddP`) are mutually recursive, and each returns a record holding a Bool result and a step count.
  • elm / MutualLetRecManyCapturesTest
    Two mutually recursive `let`-bound functions inside a top-level function, both using all four of the enclosing function's parameters, compute the right result.
  • elm / NumberDestructFieldTest
    Number literals stored in a constructor's tuple, list, record or `Maybe` field, then destructured in a `let` with a constructor pattern, take the type `Float` from how they are later used.
  • elm / NumberGenericAppCustom
    A number literal passed through a generic function, here `unbox : Box a -> a` applied to `Box 30`, keeps the type `number`, so the `let`-bound result can be used in `Float` arithmetic: `round (1.5 * n)` logs 45.
  • elm / NumberGenericAppList
    A number literal passed through a generic function, here `List.head` followed by `Maybe.withDefault 0`, keeps the type `number`, so the `let`-bound result can be used in `Float` arithmetic: `round (1.5 * n)` logs 45.
  • elm / NumberGenericAppMaybe
    A number literal passed through a generic function, here `Maybe.withDefault 0` applied to `Just 30`, keeps the type `number`, so the `let`-bound result can be used in `Float` arithmetic: `round (1.5 * n)` logs 45.
  • elm / NumberGenericAppTuple
    A number literal passed through a generic function, here `Tuple.first` applied to `( 30, "x" )`, keeps the type `number`, so the `let`-bound result can be used in `Float` arithmetic: `round (1.5 * n)` logs 45.
  • elm / PolyChainFoldlLikeCalled8TimesOverTupleKeyedStructureTest
    Tests a polymorphic fold-like function called eight times in a chain, each with a step function over `( List String, Global )` elements and the previous call's result as the initial value.
  • elm / PolyEscapePolymorphicFunctionExtractedFromTupleTest
    A function stored in a tuple and taken out again by a let-bound pattern-matching helper can be applied: `first ( id, 0 ) 42` evaluates to 42.
  • elm / PolyEscapeRecordTest
    A record field holding a polymorphic identity function can be called, and a record update that replaces it with an increment function gives a record whose field calls the new function.
  • elm / PolyLetConstTest
    A let-bound polymorphic `const` is used at two different type combinations in the same scope, and each use returns its first argument.
  • elm / PolyLetTailRecTwoTypesTest
    A let-bound, polymorphic, tail-recursive local `foldl` used at two different types in the same expression gives the right result for each.
  • elm / RecordUpdatePolyFieldTest
    Updating a function-typed field of a let-bound record whose function is polymorphic gives a record whose new function works.
  • elm / StandardA93MutualNumberDefaultTest
    A `number` variable of a recursive group that one binding's type does not mention is Float at a use of that binding.
  • elm / StandardA99RecordPatternAsTest
    A record pattern under `as` in a `let` binding.
  • elm / StandardA99RecordPatternBothTest
    A variable bound by a record pattern in a `let`, used at Int and at Float.
  • elm / StandardA99RecordPatternDependentTest
    A `let` definition that uses a variable bound by a record pattern.
  • elm / StandardA99RecordPatternFloatTest
    A variable bound by a record pattern in a `let`, used at Float.
  • elm / StandardA99RecordPatternFunctionTest
    A function bound by a record pattern in a `let`, applied at Int and at Float.
  • elm / StandardA99RecordPatternInTopCopyTest
    A record pattern in a `let` inside a top-level value that is itself copied.
  • elm / StandardA99RecordPatternInTupleTest
    A record pattern inside the tuple pattern of a `let` binding.
  • elm / StandardA99RecordPatternIntTest
    A variable bound by a record pattern in a `let`, used at Int.
  • elm / StandardA99RecordPatternLambdaTest
    A record pattern in a lambda inside a `let` binding that is copied.
  • elm / StandardA99RecordPatternSharedVariableTest
    Two variables of one record pattern that share one `number` type.
  • elm / StandardA99RecordPatternTwoVariablesTest
    Two variables of one record pattern, with independent `number` types, used at opposite types.
  • elm / TailLoopMutualRecMutualRecursionInsideATailLoopTest
    A self tail-recursive function whose loop body defines a pair of mutually recursive local functions, and calls one of them to compute the next accumulator, gives the right result.

Discussions

Discuss this rule·Search the discussions on GitHub

Discussions are on GitHub; one about this rule has [r:s-dec-group] 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.