LambdaLetBoundaryTest
Tags:elm-spec
Checks, in A Standard Definition of Elm?:rule d-atexp-letrule d-exp-lambdarule d-app-closure
Translated into Lean for the mechanisation of the book: see its Lean translation.
Checks functions that bind values in a `let` and then return a lambda for their remaining arguments.
- `basic` and `partial` call `letSeparated`, once with both arguments and
once by binding a partial application and then applying it.
- `capturing` calls `letCapturing`, whose lambda uses both the `let`
binding and the function's own parameter.
- `multi_let` calls `multiLet`, which has two `let` bindings, the second
using the first.
- `nested` calls `nestedLet`, which returns a lambda containing another
`let` and another lambda, so three arguments pass through two such
stages.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. basic: 15 capturing: 19 multi_let: 20 nested: 42 partial: 25
elm-0.19.2
passran in 53 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.2/optimize for better performance and smaller assets. basic: 15 capturing: 19 multi_let: 20 nested: 42 partial: 25
elm-0.19.3
passran in 42 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.3/optimize for better performance and smaller assets. basic: 15 capturing: 19 multi_let: 20 nested: 42 partial: 25
lamdera-1.5.0
passran in 45 msOutput
basic: 15 capturing: 19 multi_let: 20 nested: 42 partial: 25
Discussions
Discuss this test·Search the discussions on GitHub
Discussions are on GitHub; one about this test has [t:elm/LambdaLetBoundaryTest] in its title.
Source
The -- CHECK: lines give the expected output.
module LambdaLetBoundaryTest exposing (main)
{-| Checks functions that bind values in a `let` and then return a lambda for their remaining arguments.
- `basic` and `partial` call `letSeparated`, once with both arguments and
once by binding a partial application and then applying it.
- `capturing` calls `letCapturing`, whose lambda uses both the `let`
binding and the function's own parameter.
- `multi_let` calls `multiLet`, which has two `let` bindings, the second
using the first.
- `nested` calls `nestedLet`, which returns a lambda containing another
`let` and another lambda, so three arguments pass through two such
stages.
-}
-- TAGS: elm-spec, d-atexp-let, d-exp-lambda, d-app-closure
-- CHECK: basic: 15
-- CHECK: capturing: 19
-- CHECK: multi_let: 20
-- CHECK: nested: 42
-- CHECK: partial: 25
import Html exposing (text)
{-| Binds `y` to `a + 5` in a `let`, then returns a lambda adding `y` to its
argument.
-}
letSeparated : Int -> Int -> Int
letSeparated a =
let
y = a + 5
in
\z -> y + z
{-| Binds `y` to `a * 2` in a `let`, then returns a lambda that also uses `a`.
-}
letCapturing : Int -> Int -> Int
letCapturing a =
let
y = a * 2
in
\z -> y + z + a
{-| Binds two values in a `let`, the second using the first, then returns a
lambda using both.
-}
multiLet : Int -> Int -> Int
multiLet a =
let
x = a + 1
y = x * 2
in
\z -> x + y + z
{-| Returns a lambda, after a `let`, that itself has a `let` and returns
another lambda.
-}
nestedLet : Int -> Int -> Int -> Int
nestedLet a =
let
x = a + 1
in
\b ->
let
y = b + x
in
\c -> x + y + c
main =
let
_ = Debug.log "basic" (letSeparated 5 5)
_ = Debug.log "capturing" (letCapturing 3 10)
_ = Debug.log "multi_let" (multiLet 4 5)
_ = Debug.log "nested" (nestedLet 10 15 5)
partial = letSeparated 10
_ = Debug.log "partial" (partial 10)
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_LambdaLetBoundaryTest, in StandardElmMech/Generated/Elm_InfixPrecO.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 |
|---|---|---|---|
basic | 15 | 15 | t_elm_LambdaLetBoundaryTest |
capturing | 19 | 19 | t_elm_LambdaLetBoundaryTest |
multi_let | 20 | 20 | t_elm_LambdaLetBoundaryTest |
nested | 42 | 42 | t_elm_LambdaLetBoundaryTest |
partial | 25 | 25 | t_elm_LambdaLetBoundaryTest |
/-- tests/elm/src/LambdaLetBoundaryTest.elm -/
def g_elm_LambdaLetBoundaryTest : Program := {
name := "LambdaLetBoundaryTest", source := "tests/elm/src",
exposing := (.items [.var "main"]), imports := [⟨"Html", "Html", some (.items [.var "text"])⟩],
types := [],
aliases := [],
defs := [
.fn "letSeparated" [pv "a"] (letDefs [.fn "y" [] (bin "+" (v "a") (i 5))] (lams [pv "z"] (bin "+" (v "y") (v "z")))),
.fn "letCapturing" [pv "a"] (letDefs [.fn "y" [] (bin "*" (v "a") (i 2))] (lams [pv "z"] (bin "+" (bin "+" (v "y") (v "z")) (v "a")))),
.fn "multiLet" [pv "a"] (letDefs [.fn "x" [] (bin "+" (v "a") (i 1)), .fn "y" [] (bin "*" (v "x") (i 2))] (lams [pv "z"] (bin "+" (bin "+" (v "x") (v "y")) (v "z")))),
.fn "nestedLet" [pv "a"] (letDefs [.fn "x" [] (bin "+" (v "a") (i 1))] (lams [pv "b"] (letDefs [.fn "y" [] (bin "+" (v "b") (v "x"))] (lams [pv "c"] (bin "+" (bin "+" (v "x") (v "y")) (v "c")))))),
.fn "main" [] (letDefs [.pat pw (ap (qv "Debug" "log") [str "basic", .atexp (.paren (ap (v "letSeparated") [i 5, i 5]))]), .pat pw (ap (qv "Debug" "log") [str "capturing", .atexp (.paren (ap (v "letCapturing") [i 3, i 10]))]), .pat pw (ap (qv "Debug" "log") [str "multi_let", .atexp (.paren (ap (v "multiLet") [i 4, i 5]))]), .pat pw (ap (qv "Debug" "log") [str "nested", .atexp (.paren (ap (v "nestedLet") [i 10, i 15, i 5]))]), .fn "partial" [] (ap (v "letSeparated") [i 10]), .pat pw (ap (qv "Debug" "log") [str "partial", .atexp (.paren (ap (v "partial") [i 10]))])] (ap (v "text") [str "done"])) ],
checks := [
chk .check "basic: 15" .int,
chk .check "capturing: 19" .int,
chk .check "multi_let: 20" .int,
chk .check "nested: 42" .int,
chk .check "partial: 25" .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_LambdaLetBoundaryTest 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_LambdaLetBoundaryTest 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_LambdaLetBoundaryTest states that the program has a meaning by r:p-program, with both premises, and ident_elm_LambdaLetBoundaryTest that its specialisation is unique and is the reduced program above, both with no hypothesis about principality.