Elm Exactly
Results / elm /

LetRecClosureTest

Tags:elm-spec

Checks, in A Standard Definition of Elm?:rule s-dec-grouprule d-dec-recrule d-atexp-let

Translated into Lean for the mechanisation of the book: see its Lean translation.

elm-0.19.1 passelm-0.19.2 passelm-0.19.3 passlamdera-1.5.0 pass
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`.

`processItems` binds `collected = takeMore rest` before `takeMore` is defined;
`takeMore` reads the enclosing `threshold` and collects the leading `Num` values
above it, stopping at the first value that is not or at a `Blank`. On `[ Num 1, Num 2, Num 3, Blank, Num 99 ]` with
threshold 0 the result is `[1,2,3]`.

Results

elm-0.19.1

passran in 91 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.1/optimize for better performance and smaller assets.
LetRecClosureTest: [1,2,3]

elm-0.19.2

passran in 50 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.2/optimize for better performance and smaller assets.
LetRecClosureTest: [1,2,3]

elm-0.19.3

passran in 69 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.3/optimize for better performance and smaller assets.
LetRecClosureTest: [1,2,3]

lamdera-1.5.0

passran in 61 ms
Output
LetRecClosureTest: [1,2,3]

Discussions

Discuss this test·Search the discussions on GitHub

Discussions are on GitHub; one about this test has [t:elm/LetRecClosureTest] in its title.

Source

The -- CHECK: lines give the expected output.

module LetRecClosureTest exposing (main)
{-| 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`.
`processItems` binds `collected = takeMore rest` before `takeMore` is defined;
`takeMore` reads the enclosing `threshold` and collects the leading `Num` values
above it, stopping at the first value that is not or at a `Blank`. On `[ Num 1, Num 2, Num 3, Blank, Num 99 ]` with
threshold 0 the result is `[1,2,3]`.
-}
-- TAGS: elm-spec, s-dec-group, d-dec-rec, d-atexp-let
-- CHECK: LetRecClosureTest: [1,2,3]
import Html exposing (text)
type Item
= Num Int
| Blank
main =
let
items =
[ Num 1, Num 2, Num 3, Blank, Num 99 ]
result =
processItems 0 items
_ =
Debug.log "LetRecClosureTest" result
in
text "hello"
processItems : Int -> List Item -> List Int
processItems threshold items =
case items of
[] ->
[]
(Num n) :: rest ->
let
collected =
takeMore rest
takeMore : List Item -> List Int
takeMore xs =
case xs of
(Num m) :: ys ->
if m > threshold then
m :: takeMore ys
else
[]
_ ->
[]
in
n :: collected
Blank :: rest ->
processItems threshold rest

Lean translation

This program is translated into Lean by the translator of the Lean mechanisation of A Standard Definition of Elm?: the program it is, its modules in the reduced syntax of Chapter 7 with every integer constant at Int or Float by number specialisation, and its CHECK lines as written (g_elm_LetRecClosureTest, in StandardElmMech/Generated/Elm_LetNumberLo.lean). Lean's evaluator runs the program by the rules of Chapters 7 and 9, and its checks become one generated theorem: some derivation of the program has a log that meets every check, or, for a test that stops at Debug.todo, the program has no meaning.

CheckExpected (CHECK)Lean evaluatorProved by
LetRecClosureTest[1,2,3][1,2,3]t_elm_LetRecClosureTest
/-- tests/elm/src/LetRecClosureTest.elm -/
def g_elm_LetRecClosureTest : Program := {
  name := "LetRecClosureTest", source := "tests/elm/src",
  exposing := (.items [.var "main"]), imports := [⟨"Html", "Html", some (.items [.var "text"])⟩],
  types := [("Item", [("Num", 1), ("Blank", 0)])],
  aliases := [],
  defs := [
    .fn "processItems" [pv "threshold", pv "items"] (caseOf (v "items") [(P (listP []), listE []), (.cons (P (.paren (pcon "Num" [pv "n"]))) (P (pv "rest")), letDefs [.fn "takeMore" [pv "xs"] (caseOf (v "xs") [(.cons (P (.paren (pcon "Num" [pv "m"]))) (P (pv "ys")), ifte (bin ">" (v "m") (v "threshold")) (bin "::" (v "m") (ap (v "takeMore") [v "ys"])) (listE [])), (P pw, listE [])]), .fn "collected" [] (ap (v "takeMore") [v "rest"])] (bin "::" (v "n") (v "collected"))), (.cons (P (pcA "Blank")) (P (pv "rest")), ap (v "processItems") [v "threshold", v "rest"])]),
    .fn "main" [] (letDefs [.fn "items" [] (listE [ap (c "Num") [i 1], ap (c "Num") [i 2], ap (c "Num") [i 3], c "Blank", ap (c "Num") [i 99]]), .fn "result" [] (ap (v "processItems") [i 0, v "items"]), .pat pw (ap (qv "Debug" "log") [str "LetRecClosureTest", v "result"])] (ap (v "text") [str "hello"])) ],
  checks := [
    chk .check "LetRecClosureTest: [1,2,3]" .data ] }

Reading the translation: v "x" is the variable x, c "K" the constructor K, i 42 the integer 42 at Int, .scon (.intF 2) the integer 2 at Float, and str "s" a string; ap f [a, b] applies f to a and then b, and bin "+" a b is a + b. lams, caseOf, ifte, listE, tup2 and recE build lambdas, case, if, lists, pairs and records by the book's derived forms; pv, pw and pcon are variable, wildcard and constructor patterns. defs are the program's top-level definitions and locals the other declarations of main's let, placed in a let around each check; ckk label value kind exp is one check. A name with $ (incF$Float) is a copy number specialisation made.

Static semantics (Chapters 4, 5 and 9): legal_elm_LetRecClosureTest states that the program as written is legal, with no hypothesis: the proved inference W gives each module a principal derivation, which meets the syntactic restrictions and the further restrictions, with a type of main Chapter 9 allows. End to end: spec_elm_LetRecClosureTest states that Lean computes the reduced program above from the source (derived forms, inference, the proved checker, number specialisation, type erasure), so the theorems about it do not trust the translator's elaboration ([elab]); mean_elm_LetRecClosureTest states that the program has a meaning by r:p-program, with both premises, and ident_elm_LetRecClosureTest that its specialisation is unique and is the reduced program above, both with no hypothesis about principality.