Rule 20s-valbind-var
Comments
When the second option is present, by the syntactic restrictions. With an annotation, holds the variables of not already explicit in (Section 4.7). They are in while and elaborate, so an annotation nested in that writes one of their names means the same variable. Since is fixed by , must have exactly that type, with the variables of uninstantiated: an annotation may be less general than the type could have, but never more general. Thus f : a -> a with f x = x + 1 does not elaborate, nor does g : number -> number with g x = x // 2.
The condition prevents an explicit variable from escaping its annotation. In f x = let g : a -> a ; g y = x in g x, the body of g has type a -> a only if x has the explicit type a, which is then free in the context of g; so the expression does not elaborate.
Referred to from
- The comment on rule
s-valbind-pat(Chapter 4) - The comment on rule
s-ty-tyvar(Chapter 4) - Section 6.2 Number Specialisation
TeX source
\defnrule[r:s-valbind-var]
{\opt{U' \text{ is new for } \ty \text{ in } C \premsep \jdg{C + U'}{\ty}{\tau} \premsep U' \cap \tyvars C = \emptyset} \\ \jdg{C\opt{{}+U'}}{\expm}{\tau} \premsep \optt{\jdg{C}{\valbind}{\VE}}}
{\jdg{C}{\var\ \opt{\kw{:}\ \ty}\ \kw{=}\ \expm\ \optt{\kw{and}\ \valbind}}{\{\var \mapsto (\opt{\forall U'.}\tau, \isv)\}\ \optt{{}+\VE}}}Tests (101)
- elm / AliasForwardTestTop-level values defined as another function, `dup = twice` and `plus = (+)`, behave like the function they name.
- elm / AnnotatedLetRecFnResultTestA recursive let-bound function whose type annotation has a function as its result works.
- elm / CompilerIssue1639LetAnnotationTypeVarCrashTestA let annotation with a type variable, inside an unannotated function, crashes the compiler.
- elm / CompilerIssue1691ContradictoryExtensibleRecordHangTestA point-free definition whose extensible-record annotations contradict each other is rejected with a type error.
- elm / CompilerIssue1789RankCrashMissingJustTestA missing `Just` in a case branch crashes the type checker with `[rank = 1]`.
- elm / CompilerIssue1839RigidLetAnnotationCrashTestA let annotation with a free type variable, in an unannotated function, crashes the compiler.
- elm / CompilerIssue1858AnnotatedPolymorphicRecursionTestA recursive call at a different type argument is rejected despite the annotation.
- elm / CompilerIssue1866PhantomExtensibleRecordHangTestComposing two functions on phantom types over different extensible records, taken from one record argument, is rejected with a type error.
- elm / CompilerIssue1964AnnotatedPolymorphicRecursionWrapperTestA recursive call at a wrapped type argument is rejected despite the annotation.
- elm / CompilerIssue1974RankCrashWrongAnnotationTestA wrong argument annotation on a recursive function called from another crashes the type checker.
- elm / CompilerIssue2001RankCrashParametricLetAnnotationTestA let annotation `Dict a b` inside an unannotated function crashes the compiler.
- elm / CompilerIssue2013RankCrashLambdaLetAnnotationTestA let annotation `b : h` in a piped lambda crashes the compiler.
- elm / CompilerIssue2014RankCrashIdentityLetAnnotationTestA let annotation `b : h` in a lambda defining an annotated identity function crashes the compiler.
- elm / CompilerIssue2057CallSiteChangesInferredTypeTestTwo annotated callers of a function with unannotated mutually recursive let functions are wrongly rejected.
- elm / CompilerIssue2066RankCrashNumberFloatTestAnnotating `number` arguments compared with a `Float` crashes the type checker.
- elm / CompilerIssue2077RankCrashExtensibleLetAnnotationTestA `let` annotation that gives an unannotated argument a more general extensible-record type must be reported as a type error, not crash the compiler.
- elm / CompilerIssue2100LetAnnotationRank2CrashTestA wrong annotation on a let-bound function that uses an outer argument must be reported as a TYPE MISMATCH, not crash the compiler.
- elm / CompilerIssue2105ExtensibleFoldlCrashTestFolding with a function that extends a record type parameter must produce a complete type error report, not crash the compiler.
- elm / CompilerIssue2146RoseLambdaPatternCrashTestA type error involving a lambda pattern on a custom type must be reported normally, not crash the compiler.
- elm / CompilerIssue2153RecursiveCallWrongArgCrashTestA recursive call with an argument of the wrong type must be reported as a TYPE MISMATCH, not crash the compiler.
- elm / CompilerIssue2184LetRecursionTooManyArgsCrashTestTwo let-bound recursive functions that call themselves with too many arguments must be reported as a type error, not crash the compiler.
- elm / CompilerIssue2274MutualRecursionTransmuteTestThe compiler rejects `eraseType : a -> b` because the unannotated mutually recursive `f` and `g` only have type `a -> b -> b`.
- elm / CompilerIssue2295LambdaLetAnnotationCrashTestThe compiler reports a TYPE MISMATCH for a let annotation `b : b` whose value is a lambda argument.
- elm / CompilerIssue2296LetAnnotationFreeVarCrashTestThe compiler reports a type error for a let annotation that uses a type variable belonging to an unannotated enclosing function.
- elm / CompilerIssue2298IdentityAliasResultTestA type alias `Msg a = a` in an annotation's result is interchangeable with `a`, so `make = identity` type-checks against `a -> Msg a`.
- elm / CompilerIssue2301InnerSkolemOuterVarCrashTestThe compiler reports a type error when a let annotation's type variable must equal the type of an enclosing function's argument.
- elm / CompilerIssue2338LambdaLetAnnotationTupleCrashTestThe compiler reports a TYPE MISMATCH for a let annotation `( Int, x )` inside a lambda passed to a higher-order function.
- elm / ErasedListSpecializationTestA 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 / EtaExpandStateFunctionSequenceTestA state-passing `sequence` written with `andThen` and `pure` over a function type alias, `St a = Int -> a`, whose definitions take more parameters than their annotations show.
- elm / EtaExpandStateTestA state monad over the function alias `St a = Int -> ( Int, a )`, with `sequence` written recursively using `andThen` and `pure`, threads its state correctly.
- elm / FlipFunctionTestA locally defined, annotated `flip` swaps the two arguments of the function it is given.
- elm / FunctionCompappendWithStringTestA function whose arguments have the `compappend` constraint (comparable and appendable) works when called with `String`s.
- elm / LetSeparatedStagingTestA function defined with fewer parameters than its annotated type returns, from different `case` branches, lambdas that take the remaining arguments in different groupings.
- elm / LocalTailRecSimpleTestAn annotated let-bound tail-recursive function with an accumulator computes the sum 1 + 2 + ... + 10 = 55.
- elm / LssGapBodyTieAnnoABA top-level function that returns a lambda gives the same result whether or not it has a type annotation.
- elm / PhantomTypeVarTestTests a custom type with a type parameter that one of its constructors does not use, rebuilt at a new type by generic functions.
- elm / PolyChainCallTestTests a polymorphic function that calls another polymorphic function twice, building a binary tree of two nodes that is then counted.
- 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 / PolyChainFoldlLikeCalled8TimesOverTupleKeyedStructureTestTests 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 / PolySpecializeRecursiveTestAnnotated polymorphic recursive functions, `myLength` and `myMap`, give the right results when called at both `Int` and `String` element types.
- elm / PolyStepLoopFloatResultTestA polymorphic state-threading loop, driven by a `Step state a` custom type, returns a `Float` result and the threaded state correctly.
- elm / PolyStepLoopFoldMIntTestA monadic fold, `foldM`, built on a polymorphic `Step`-driven state loop, sums a list of `Int`s and threads its state correctly.
- elm / RecordNarrow03FullAnnotationTestA let-bound helper annotated with concrete record aliases, folded over two nested records with `List.foldl`, reads the right nested field.
- elm / RecordNarrow06RowPolyAnnotationTestA let-bound helper annotated with a nested extensible record type, folded with `List.foldl`, reads the right nested field.
- elm / TypeClassConstraintTestChecks functions whose types use the constrained type variables `number`, `comparable` and `appendable`.
- elm / TypeMismatchAccessorUnderIntTestA field accessor function annotated as `Int` is rejected with a TYPE MISMATCH.
- elm / TypeMismatchAliasWhereARecordTestA value of a record alias used where a record with a differently typed field is expected is rejected with a TYPE MISMATCH.
- elm / TypeMismatchAliasedRecordFieldOfTheWrongTypeTestA record whose field has the wrong type for its record alias annotation is rejected with a TYPE MISMATCH.
- elm / TypeMismatchCharUnderIntTestA value annotated `Int` but defined as the character `'c'` is rejected with a TYPE MISMATCH.
- elm / TypeMismatchClosedRecordWhereExtensibleTestPassing a record that lacks a field the result must have is rejected with a TYPE MISMATCH.
- elm / TypeMismatchCompappendAgainstARigidVarTestA value used with both `++` and `<` cannot share a type with an annotation's type variable, and the program is rejected with a TYPE MISMATCH.
- elm / TypeMismatchExtensibleRecordGivenAClosedOneTestA function annotated to return the extensible record `{ r | name : Int }` but returning the record literal `{ name = 1 }` is rejected with a TYPE MISMATCH.
- elm / TypeMismatchFieldAccessUnderIntTestA value annotated `Int` but defined as a `String` field of a record, `r.name`, is rejected with a TYPE MISMATCH.
- elm / TypeMismatchFloatUnderIntTestThe compiler rejects the `Float` literal `1.5` as the body of a definition annotated `Int`, with a TYPE MISMATCH error.
- elm / TypeMismatchFunctionInATypeArgumentTestThe compiler rejects a list holding `Just not` as the body of a definition annotated `Maybe (Int -> Int)`, with a TYPE MISMATCH error.
- elm / TypeMismatchIfUnderIntTestThe compiler rejects an `if` producing strings as the body of a definition annotated `Int`, with a TYPE MISMATCH error.
- elm / TypeMismatchIntWhereFloatTestReturning an `Int` from a definition annotated `Float` is rejected with a TYPE MISMATCH.
- elm / TypeMismatchLocalTypeNamedLikeAnImportedOneTestA local type named `Maybe` is a different type from the core `Maybe`, so mixing them is rejected with a TYPE MISMATCH.
- elm / TypeMismatchMaybeOfANumberAgainstARigidVarTestReturning `Just 1` from a function annotated `a -> a` is rejected with a TYPE MISMATCH.
- elm / TypeMismatchMisspeltFieldOfExtensibleRecordTestA function annotated to take an extensible record with fields `alpha` and `beta` that reads the misspelt field `r.alpah` is rejected with a TYPE MISMATCH error.
- elm / TypeMismatchOneArgumentFunctionWhereTwoTestDefining `f : Int -> Int -> Int` as a function of type `Int -> Int` (`f = g`) is rejected with a TYPE MISMATCH error.
- elm / TypeMismatchParameterisedAliasTestGiving a `Box Int` where a `Box String` is annotated is rejected with a TYPE MISMATCH error.
- elm / TypeMismatchRecordPatternTestA record pattern on an argument annotated as `Int` is rejected with a TYPE MISMATCH.
- elm / TypeMismatchRecordWhereEmptyClosedRecordTestA record with a field, given where the annotation says the empty record `{}`, is rejected with a TYPE MISMATCH.
- elm / TypeMismatchRigidAppendableAddedTestAdding two values of an annotated `appendable` type with `+` is rejected with a TYPE MISMATCH.
- elm / TypeMismatchRigidCompappendAsIntTestReturning an annotated `compappend` value where the annotation says `Int` is rejected with a TYPE MISMATCH.
- elm / TypeMismatchRigidComparableAsANumberTestReturning a number literal where the annotation promises a `comparable` is rejected with a TYPE MISMATCH.
- elm / TypeMismatchRigidComparableAsNumberTestReturning an annotated `comparable` value where the annotation promises a `number` is rejected with a TYPE MISMATCH.
- elm / TypeMismatchRigidExtensionWhereClosedTestReturning an extensible record argument where the annotation says a closed record is rejected with a TYPE MISMATCH.
- elm / TypeMismatchRigidNumberAsAliasTestReturning a record-alias value where the annotation promises a `number` is rejected with a TYPE MISMATCH.
- elm / TypeMismatchRigidNumberAsFunctionTestReturning a lambda where the annotation promises a `number` is rejected with a TYPE MISMATCH.
- elm / TypeMismatchRigidNumberAsMaybeTestReturning a `Maybe` where the annotation promises a `number` is rejected with a TYPE MISMATCH.
- elm / TypeMismatchRigidNumberAsRecordTestReturning a record where the annotation promises a `number` is rejected with a TYPE MISMATCH.
- elm / TypeMismatchRigidNumberAsRigidVarTestReturning a value of an annotated type variable `a` where the annotation promises a `number` is rejected with a TYPE MISMATCH.
- elm / TypeMismatchRigidNumberAsTupleTestReturning a tuple where the annotation promises a `number` is rejected with a TYPE MISMATCH.
- elm / TypeMismatchRigidNumberAsUnitTestReturning `()` where the annotation promises a `number` is rejected with a TYPE MISMATCH.
- elm / TypeMismatchRigidVarAgainstItsListTestReturning an argument of type `a` where the annotation says `List a` is rejected with a TYPE MISMATCH.
- elm / TypeMismatchRigidVarAgainstItsMaybeTestReturning an argument of type `a` where the annotation says `Maybe a` is rejected with a TYPE MISMATCH.
- elm / TypeMismatchRigidVarAppendedTestAppending two values of an unconstrained annotated type variable with `++` is rejected with a TYPE MISMATCH.
- elm / TypeMismatchRigidVarAsAliasTestReturning a record-alias value where the annotation promises a type variable `a` is rejected with a TYPE MISMATCH.
- elm / TypeMismatchRigidVarAsFunctionTestReturning a lambda where the annotation promises a type variable `a` is rejected with a TYPE MISMATCH.
- elm / TypeMismatchRigidVarAsMaybeTestA function annotated `a -> a` that returns a `Maybe` is rejected with a TYPE MISMATCH.
- elm / TypeMismatchRigidVarAsRecordTestA function annotated `a -> a` that returns a record is rejected with a TYPE MISMATCH.
- elm / TypeMismatchRigidVarAsRigidNumberTestA function that returns its `number` argument where its annotation promises a different type variable is rejected with a TYPE MISMATCH.
- elm / TypeMismatchRigidVarAsTupleTestA function annotated `a -> a` that returns a tuple is rejected with a TYPE MISMATCH.
- elm / TypeMismatchRigidVarAsUnitTestA function annotated `a -> a` that returns unit is rejected with a TYPE MISMATCH.
- elm / TypeMismatchRigidVarComparedTestComparing values of an unconstrained annotated type variable with `<` is rejected with a TYPE MISMATCH.
- elm / TypeMismatchSameRigidExtensionTestAn extensible record returned with a field of a different type than its annotation promises is rejected with a TYPE MISMATCH.
- elm / TypeMismatchSelfAppliedArgumentAgainstARigidNumberTestA local function argument that is applied to itself and also listed beside a function involving an annotated `number` is rejected with a TYPE MISMATCH.
- elm / TypeMismatchSelfAppliedArgumentAgainstARigidVarTestA local function argument that is applied to itself and also listed beside a function involving an annotated type variable is rejected with a TYPE MISMATCH.
- elm / TypeMismatchTriplesDifferTestA triple whose third component does not match its annotation is rejected with a TYPE MISMATCH.
- elm / TypeMismatchTupleSizesDifferTestA triple given where the annotation promises a pair is rejected with a TYPE MISMATCH.
- elm / TypeMismatchTwoAliasedRecordsTestA value of one record alias annotated as a different record alias is rejected with a TYPE MISMATCH.
- elm / TypeMismatchTwoArgumentLambdaWhereOneTestA two-argument lambda annotated as `Int -> Int` is rejected with a TYPE MISMATCH.
- elm / TypeMismatchTwoExtensibleRecordsTestAn extensible record returned where the annotation promises a record with a different extension variable is rejected with a TYPE MISMATCH.
- elm / TypeMismatchTwoRigidVarsTestA function annotated `a -> b` that returns its argument is rejected with a TYPE MISMATCH.
- elm / TypeMismatchTypeUnderAnExposingListTestA value annotated `Int` but defined as `Just 1` is rejected with a TYPE MISMATCH error.
- elm / TypeMismatchTypeWhereAnAliasTestA value annotated with a type alias of `Int` but defined as a `String` is rejected with a TYPE MISMATCH error.
- elm / TypeMismatchUnitPatternTestA function annotated `Int -> Int` whose parameter is the unit pattern `()` is rejected with a TYPE MISMATCH error.
- elm / TypeMismatchUnknownTypeAgainstARigidNumberTestA function annotated `number -> ( Bool, number )` whose case branches return `( Just 1, h )` and `( True, x )` is rejected with a TYPE MISMATCH error.
- elm / TypeMismatchUnknownTypeAgainstARigidVarTestA function annotated `a -> ( Bool, a )` whose case branches return `( Just 1, h )` and `( True, x )` is rejected with a TYPE MISMATCH error.
Discussions
Discuss this rule·Search the discussions on GitHub
Discussions are on GitHub; one about this rule has [r:s-valbind-var] 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.