Rule 56sm-topdec-value
Top-level Declarations
(56)
Comments
The rule is that for declarations in the Core (rule (19)): a group of top-level values is recursive, an unannotated member is monomorphic within its group, and the closure makes every top-level value polymorphic in its other uses. At the top level the restriction on recursive values is syntactic and weaker than in a let (Section 3.7), so f = \n -> f n elaborates here.
Referred to from
TeX source
\defnrule[r:sm-topdec-value]
{\jdg{C + \VE}{\valbind}{\VE} \premsep \VE' = \Clos_C \VE}
{\jdg{C}{\valbind}{\VE'\ \text{in}\ \Env,\ \emptymap}}Tests (40)
- elm / AliasForwardTestTop-level values defined as another function, `dup = twice` and `plus = (+)`, behave like the function they name.
- elm / CombinatorBComposeTestThe B combinator, function composition, built only from S and K: `b square inc 4` is `square (inc 4)`, which is 25.
- elm / CombinatorBSumMapTestThe B combinator, built from S and K, composing `sum` with a partially applied `map`: the sum of `[ 1, 2, 3 ]` each plus one is 9.
- elm / CombinatorCConsTestThe C combinator, which swaps two arguments, built from S and K and applied to the cons operator: `c (::) [ 2, 3 ] 1` is `1 :: [ 2, 3 ]`.
- elm / CombinatorCFlipTestThe C combinator, which swaps two arguments, built from S and K: `c sub 10 3` is `sub 3 10`, which is -7.
- elm / CombinatorIIdentityTestThe I combinator, identity, written as `s k k`: `i 42` is 42.
- elm / CombinatorListStringTestSKI-style combinators applied to lists, strings and functions over them, each printing its result.
- elm / CombinatorRefIdentityBugTestA B combinator defined with all its arguments, `b f g y = s (k s) k f g y`, used in the same program as a separate application of `s` to a different function: `b square inc 4` must still be `square (inc 4)`, which is 25.
- elm / CombinatorTPipeTestThe T combinator, built from S and K, passing `[ 1, 2, 3 ]` to the composition of `sum` and `map ((*) 2)`: the result is 12.
- elm / CombinatorTThrushTestThe T combinator, which applies its second argument to its first, built from S and K: `t 7 (\x -> x * 3)` is 21.
- elm / CombinatorTestSKI-style combinators, most of them built from S and K, each applied to `Int` arguments and printing its result.
- elm / CompilerIssue1858AnnotatedPolymorphicRecursionTestA recursive call at a different type argument is rejected despite the annotation.
- elm / CompilerIssue1890RecordUpdateMutualRecursionCrashTestMutually recursive unannotated functions with a record update crash the compiler.
- elm / CompilerIssue1964AnnotatedPolymorphicRecursionWrapperTestA recursive call at a wrapped type argument is rejected despite the annotation.
- elm / CompilerIssue2057CallSiteChangesInferredTypeTestTwo annotated callers of a function with unannotated mutually recursive let functions are wrongly rejected.
- elm / CompilerIssue2119MutualRecursionUnannotatedTestAn exposed, unannotated, mutually recursive function used from another module compiles and runs.
- elm / CompilerIssue2207MutualRecursionSecondFunctionTestThe second of two exposed, unannotated, mutually recursive functions can be used from another module.
- elm / CompilerIssue2274MutualRecursionTransmuteTestThe compiler rejects `eraseType : a -> b` because the unannotated mutually recursive `f` and `g` only have type `a -> b -> b`.
- elm / CovLambdaBoundaryCaseBoundaryInARecursiveValueTestA top-level function defined as a lambda whose `case` returns a different lambda per branch, one of which calls the function recursively.
- elm / CovTailRecPairParameterPassedUnchangedLetBoundAndRebuiltTestSelf tail-recursive functions with a pair parameter that is destructured in a `let`, then passed on unchanged, rebuilt with its components swapped, or replaced by a `let`-bound pair.
- elm / CovTailRecPairParametersSwappedAndALoopReturningALambdaTestSelf tail-recursive functions over pair parameters: one that swaps a pair's components or exchanges its two pair arguments, and one that returns a lambda in its base case.
- elm / CovTailRecSplitATripleParameterTestA self tail-recursive function that destructures a triple of `Int`, `Int` and `Float` in its parameter pattern and rebuilds it on each call.
- elm / DebugNumberAtFloatTestUnannotated functions over the `number` type that use `Debug.toString` and `Debug.log` give the right results when called with a `Float` and with an `Int` in the same program.
- elm / FunctionUnannotatedCompappendTestA module containing an unannotated function that both compares and appends its arguments compiles, with the type `compappend` inferred.
- elm / LssGapNoDeclsA program with no type declarations calls one annotated top-level function.
- elm / MutualRecursionTestTwo top-level functions that call each other, `isEven` and `isOdd`, decide parity by counting down to zero.
- elm / MutualRecursionThreeWayTestThree top-level functions that call each other in a cycle (`funcA` calls `funcB`, `funcB` calls `funcC`, `funcC` calls `funcA`) recurse correctly.
- elm / NonZeroArityRecursionTestA self-recursive top-level function, `countDown : Int -> Int`, calls itself in tail position from 100 down to 0 and returns 0; the program logs `countDown: 0`.
- elm / PolyApplyLambdaTestTests a polymorphic `apply f x = f x` called with an `Int` function, a `String` function and an anonymous lambda.
- elm / PolySpecializeRecursiveTestAnnotated polymorphic recursive functions, `myLength` and `myMap`, give the right results when called at both `Int` and `String` element types.
- elm / PolyTailRecReverseTestAn unannotated, polymorphic, tail-recursive list reverse gives the right result at both `Int` and `String` element types.
- elm / PureMutualRecursiveValueTestTwo top-level values that refer to each other through lambdas can be defined and used.
- elm / PureRecursiveValueThunkTestA top-level value that refers to itself inside a lambda can be defined and used.
- elm / RecordAccessorFunctionCaseTestA record accessor function `.x` stored in a top-level value can be applied to a record.
- elm / RecursiveFactorialTestA self-recursive top-level `factorial` returns 1 for 0 and 120 and 3628800 for 5 and 10.
- elm / RecursiveFibonacciTestA top-level `fib` that calls itself twice per step returns 0, 1 and 55 for 0, 1 and 10.
- elm / RecursiveListLengthTestA self-recursive function that pattern matches on `[]` and `_ :: rest` counts the elements of lists of length 0, 3 and 5.
- elm / RecursiveTopLevelLambdaTestA top-level value defined as a lambda that refers to itself can be called recursively.
- elm / RecursiveTreeTestA recursive binary tree type can be summed and measured with recursive functions.
- elm / TailRecursiveSumTestA tail-recursive accumulator function sums the integers from 1 to n correctly for n = 5, 10 and 100.
Discussions
Discuss this rule·Search the discussions on GitHub
Discussions are on GitHub; one about this rule has [r:sm-topdec-value] 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.