ClosureCaptureTupleTest
Tags:elm-spec
Checks, in A Standard Definition of Elm?:rule s-valbind-patrule d-valbind-patrule d-exp-lambda
Translated into Lean for the mechanisation of the book: see its Lean translation.
A lambda returned from a function captures a tuple and destructures it with a `let` pattern when called. `addWithTuple ( 1, 2 ) 3` gives 6. `nestedTuple` destructures a tuple nested inside a tuple, and `nestedTuple ( ( 1, 2 ), 3 ) 4` gives 10. `mixedCapture` captures an `Int` alongside the tuple, and `mixedCapture 1 ( 2, 3 ) 3` gives 9.
Results
elm-0.19.1
passran in 66 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.1/optimize for better performance and smaller assets. add: 6 nested: 10 mixed: 9
elm-0.19.2
passran in 61 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.2/optimize for better performance and smaller assets. add: 6 nested: 10 mixed: 9
elm-0.19.3
passran in 66 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.3/optimize for better performance and smaller assets. add: 6 nested: 10 mixed: 9
lamdera-1.5.0
passran in 49 msOutput
add: 6 nested: 10 mixed: 9
Discussions
Discuss this test·Search the discussions on GitHub
Discussions are on GitHub; one about this test has [t:elm/ClosureCaptureTupleTest] in its title.
Source
The -- CHECK: lines give the expected output.
module ClosureCaptureTupleTest exposing (main)
{-| A lambda returned from a function captures a tuple and destructures it with a
`let` pattern when called.
`addWithTuple ( 1, 2 ) 3` gives 6. `nestedTuple` destructures a tuple nested
inside a tuple, and `nestedTuple ( ( 1, 2 ), 3 ) 4` gives 10. `mixedCapture`
captures an `Int` alongside the tuple, and `mixedCapture 1 ( 2, 3 ) 3` gives 9.
-}
-- TAGS: elm-spec, s-valbind-pat, d-valbind-pat, d-exp-lambda
-- CHECK: add: 6
-- CHECK: nested: 10
-- CHECK: mixed: 9
import Html exposing (text)
addWithTuple : ( Int, Int ) -> Int -> Int
addWithTuple pair =
\n ->
let
( a, b ) =
pair
in
a + b + n
nestedTuple : ( ( Int, Int ), Int ) -> Int -> Int
nestedTuple outer =
\n ->
let
( inner, c ) =
outer
( a, b ) =
inner
in
a + b + c + n
mixedCapture : Int -> ( Int, Int ) -> Int -> Int
mixedCapture x pair =
\n ->
let
( a, b ) =
pair
in
x + a + b + n
main =
let
_ =
Debug.log "add" (addWithTuple ( 1, 2 ) 3)
_ =
Debug.log "nested" (nestedTuple ( ( 1, 2 ), 3 ) 4)
_ =
Debug.log "mixed" (mixedCapture 1 ( 2, 3 ) 3)
in
text "done"
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_ClosureCaptureTupleTest, in StandardElmMech/Generated/Elm_CharU.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.
| Check | Expected (CHECK) | Lean evaluator | Proved by |
|---|---|---|---|
add | 6 | 6 | t_elm_ClosureCaptureTupleTest |
nested | 10 | 10 | t_elm_ClosureCaptureTupleTest |
mixed | 9 | 9 | t_elm_ClosureCaptureTupleTest |
/-- tests/elm/src/ClosureCaptureTupleTest.elm -/
def g_elm_ClosureCaptureTupleTest : Program := {
name := "ClosureCaptureTupleTest", source := "tests/elm/src",
exposing := (.items [.var "main"]), imports := [⟨"Html", "Html", some (.items [.var "text"])⟩],
types := [],
aliases := [],
defs := [
.fn "addWithTuple" [pv "pair"] (lams [pv "n"] (letDefs [.pat (ptup2 (P (pv "a")) (P (pv "b"))) (v "pair")] (bin "+" (bin "+" (v "a") (v "b")) (v "n")))),
.fn "nestedTuple" [pv "outer"] (lams [pv "n"] (letDefs [.pat (ptup2 (P (pv "inner")) (P (pv "c"))) (v "outer"), .pat (ptup2 (P (pv "a")) (P (pv "b"))) (v "inner")] (bin "+" (bin "+" (bin "+" (v "a") (v "b")) (v "c")) (v "n")))),
.fn "mixedCapture" [pv "x", pv "pair"] (lams [pv "n"] (letDefs [.pat (ptup2 (P (pv "a")) (P (pv "b"))) (v "pair")] (bin "+" (bin "+" (bin "+" (v "x") (v "a")) (v "b")) (v "n")))),
.fn "main" [] (letDefs [.pat pw (ap (qv "Debug" "log") [str "add", .atexp (.paren (ap (v "addWithTuple") [tup2 (i 1) (i 2), i 3]))]), .pat pw (ap (qv "Debug" "log") [str "nested", .atexp (.paren (ap (v "nestedTuple") [tup2 (tup2 (i 1) (i 2)) (i 3), i 4]))]), .pat pw (ap (qv "Debug" "log") [str "mixed", .atexp (.paren (ap (v "mixedCapture") [i 1, tup2 (i 2) (i 3), i 3]))])] (ap (v "text") [str "done"])) ],
checks := [
chk .check "add: 6" .int,
chk .check "nested: 10" .int,
chk .check "mixed: 9" .int ] }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_ClosureCaptureTupleTest 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_ClosureCaptureTupleTest 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_ClosureCaptureTupleTest states that the program has a meaning by r:p-program, with both premises, and ident_elm_ClosureCaptureTupleTest that its specialisation is unique and is the reduced program above, both with no hypothesis about principality.