Elm Exactly
Results / elm /

AnonymousFunctionTest

Tags:elm-spec

Checks, in A Standard Definition of Elm?:rule s-exp-lambdarule d-exp-lambdarule d-app-closure

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
Anonymous functions work as the function argument of higher-order functions.

The program passes lambdas to local `map`, `foldl` and `filter` functions written by pattern
matching on lists.

Results

elm-0.19.1

passran in 43 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.1/optimize for better performance and smaller assets.
lambda1: [2,4,6]
lambda2: 15
lambda3: [2,4]

elm-0.19.2

passran in 46 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.2/optimize for better performance and smaller assets.
lambda1: [2,4,6]
lambda2: 15
lambda3: [2,4]

elm-0.19.3

passran in 55 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.3/optimize for better performance and smaller assets.
lambda1: [2,4,6]
lambda2: 15
lambda3: [2,4]

lamdera-1.5.0

passran in 54 ms
Output
lambda1: [2,4,6]
lambda2: 15
lambda3: [2,4]

Discussions

Discuss this test·Search the discussions on GitHub

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

Source

The -- CHECK: lines give the expected output.

module AnonymousFunctionTest exposing (main)
{-| Anonymous functions work as the function argument of higher-order functions.
The program passes lambdas to local `map`, `foldl` and `filter` functions written by pattern
matching on lists.
-}
-- TAGS: elm-spec, s-exp-lambda, d-exp-lambda, d-app-closure
-- CHECK: lambda1: [2,4,6]
-- CHECK: lambda2: 15
-- CHECK: lambda3: [2,4]
import Html exposing (text)
map : (a -> b) -> List a -> List b
map f xs =
case xs of
[] ->
[]
x :: rest ->
f x :: map f rest
foldl : (a -> b -> b) -> b -> List a -> b
foldl f acc xs =
case xs of
[] ->
acc
x :: rest ->
foldl f (f x acc) rest
filter : (a -> Bool) -> List a -> List a
filter keep xs =
case xs of
[] ->
[]
x :: rest ->
if keep x then
x :: filter keep rest
else
filter keep rest
main =
let
_ = Debug.log "lambda1" (map (\x -> x * 2) [1, 2, 3])
_ = Debug.log "lambda2" (foldl (\x acc -> x + acc) 0 [1, 2, 3, 4, 5])
_ = Debug.log "lambda3" (filter (\x -> modBy 2 x == 0) [1, 2, 3, 4, 5])
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_AnonymousFunctionTest, in StandardElmMech/Generated/Elm.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
lambda1[2,4,6][2,4,6]t_elm_AnonymousFunctionTest
lambda21515t_elm_AnonymousFunctionTest
lambda3[2,4][2,4]t_elm_AnonymousFunctionTest
/-- tests/elm/src/AnonymousFunctionTest.elm -/
def g_elm_AnonymousFunctionTest : Program := {
  name := "AnonymousFunctionTest", source := "tests/elm/src",
  exposing := (.items [.var "main"]), imports := [⟨"Html", "Html", some (.items [.var "text"])⟩],
  types := [],
  aliases := [],
  defs := [
    .fn "map" [pv "f", pv "xs"] (caseOf (v "xs") [(P (listP []), listE []), (.cons (P (pv "x")) (P (pv "rest")), bin "::" (ap (v "f") [v "x"]) (ap (v "map") [v "f", v "rest"]))]),
    .fn "foldl" [pv "f", pv "acc", pv "xs"] (caseOf (v "xs") [(P (listP []), v "acc"), (.cons (P (pv "x")) (P (pv "rest")), ap (v "foldl") [v "f", .atexp (.paren (ap (v "f") [v "x", v "acc"])), v "rest"])]),
    .fn "filter" [pv "keep", pv "xs"] (caseOf (v "xs") [(P (listP []), listE []), (.cons (P (pv "x")) (P (pv "rest")), ifte (ap (v "keep") [v "x"]) (bin "::" (v "x") (ap (v "filter") [v "keep", v "rest"])) (ap (v "filter") [v "keep", v "rest"]))]),
    .fn "main" [] (letDefs [.pat pw (ap (qv "Debug" "log") [str "lambda1", .atexp (.paren (ap (v "map") [.atexp (.paren (lams [pv "x"] (bin "*" (v "x") (.atexp (.scon (.intF 2)))))), listE [.atexp (.scon (.intF 1)), .atexp (.scon (.intF 2)), .atexp (.scon (.intF 3))]]))]), .pat pw (ap (qv "Debug" "log") [str "lambda2", .atexp (.paren (ap (v "foldl") [.atexp (.paren (lams [pv "x", pv "acc"] (bin "+" (v "x") (v "acc")))), .atexp (.scon (.intF 0)), listE [.atexp (.scon (.intF 1)), .atexp (.scon (.intF 2)), .atexp (.scon (.intF 3)), .atexp (.scon (.intF 4)), .atexp (.scon (.intF 5))]]))]), .pat pw (ap (qv "Debug" "log") [str "lambda3", .atexp (.paren (ap (v "filter") [.atexp (.paren (lams [pv "x"] (bin "==" (ap (v "modBy") [i 2, v "x"]) (i 0)))), listE [i 1, i 2, i 3, i 4, i 5]]))])] (ap (v "text") [str "done"])) ],
  checks := [
    chk .check "lambda1: [2,4,6]" .data,
    chk .check "lambda2: 15" .float,
    chk .check "lambda3: [2,4]" .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_AnonymousFunctionTest 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_AnonymousFunctionTest 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_AnonymousFunctionTest states that the program has a meaning by r:p-program, with both premises, and ident_elm_AnonymousFunctionTest that its specialisation is unique and is the reduced program above, both with no hypothesis about principality.