AccessorVariableTest
Tags:elm-spec
Checks, in A Standard Definition of Elm?:rule s-atpat-recordrule d-patrow-field
Translated into Lean for the mechanisation of the book: see its Lean translation.
A record accessor works when bound to a variable, and `.name` works on elements of lists of two different record types. The elements are taken out of the lists by pattern matching, and `.value` is applied to each element of a list of three records.
Results
elm-0.19.1
passran in 43 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.1/optimize for better performance and smaller assets. via_var: 42 mapped: [10,20,30] person_name: "Alice" company_name: "ACME"
elm-0.19.2
passran in 39 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.2/optimize for better performance and smaller assets. via_var: 42 mapped: [10,20,30] person_name: "Alice" company_name: "ACME"
elm-0.19.3
passran in 74 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.3/optimize for better performance and smaller assets. via_var: 42 mapped: [10,20,30] person_name: "Alice" company_name: "ACME"
lamdera-1.5.0
passran in 68 msOutput
via_var: 42 mapped: [10,20,30] person_name: "Alice" company_name: "ACME"
Discussions
Discuss this test·Search the discussions on GitHub
Discussions are on GitHub; one about this test has [t:elm/AccessorVariableTest] in its title.
Source
The -- CHECK: lines give the expected output.
module AccessorVariableTest exposing (main)
{-| A record accessor works when bound to a variable, and `.name` works on elements of lists of two different record types.
The elements are taken out of the lists by pattern matching, and `.value` is applied to each
element of a list of three records.
-}
-- TAGS: elm-spec, s-atpat-record, d-patrow-field
-- CHECK: via_var: 42
-- CHECK: mapped: [10,20,30]
-- CHECK: person_name: "Alice"
-- CHECK: company_name: "ACME"
import Html exposing (text)
main =
let
accessor = .value
item = { value = 42, label = "test" }
_ = Debug.log "via_var" (accessor item)
items = [ { value = 10 }, { value = 20 }, { value = 30 } ]
mapped =
case items of
[ i1, i2, i3 ] ->
[ .value i1, .value i2, .value i3 ]
_ ->
[]
_ = Debug.log "mapped" mapped
persons = [ { name = "Alice", age = 30 } ]
companies = [ { name = "ACME", employees = 100 } ]
personName =
case persons of
p :: _ ->
.name p
[] ->
"none"
companyName =
case companies of
c :: _ ->
.name c
[] ->
"none"
_ = Debug.log "person_name" personName
_ = Debug.log "company_name" companyName
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_AccessorVariableTest, 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.
| Check | Expected (CHECK) | Lean evaluator | Proved by |
|---|---|---|---|
via_var | 42 | 42 | t_elm_AccessorVariableTest |
mapped | [10,20,30] | [10,20,30] | t_elm_AccessorVariableTest |
person_name | "Alice" | "Alice" | t_elm_AccessorVariableTest |
company_name | "ACME" | "ACME" | t_elm_AccessorVariableTest |
/-- tests/elm/src/AccessorVariableTest.elm -/
def g_elm_AccessorVariableTest : Program := {
name := "AccessorVariableTest", source := "tests/elm/src",
exposing := (.items [.var "main"]), imports := [⟨"Html", "Html", some (.items [.var "text"])⟩],
types := [],
aliases := [],
defs := [
.fn "main" [] (letDefs [.fn "accessor" [] (.atexp (accessor "value")), .fn "item" [] (recE [("value", .atexp (.scon (.intF 42))), ("label", str "test")]), .pat pw (ap (qv "Debug" "log") [str "via_var", .atexp (.paren (ap (v "accessor") [v "item"]))]), .fn "items" [] (listE [recE [("value", .atexp (.scon (.intF 10)))], recE [("value", .atexp (.scon (.intF 20)))], recE [("value", .atexp (.scon (.intF 30)))]]), .fn "mapped" [] (caseOf (v "items") [(P (listP [P (pv "i1"), P (pv "i2"), P (pv "i3")]), listE [ap (.atexp (accessor "value")) [v "i1"], ap (.atexp (accessor "value")) [v "i2"], ap (.atexp (accessor "value")) [v "i3"]]), (P pw, listE [])]), .pat pw (ap (qv "Debug" "log") [str "mapped", v "mapped"]), .fn "persons" [] (listE [recE [("name", str "Alice"), ("age", .atexp (.scon (.intF 30)))]]), .fn "companies" [] (listE [recE [("name", str "ACME"), ("employees", .atexp (.scon (.intF 100)))]]), .fn "personName" [] (caseOf (v "persons") [(.cons (P (pv "p")) (P pw), ap (.atexp (accessor "name")) [v "p"]), (P (listP []), str "none")]), .fn "companyName" [] (caseOf (v "companies") [(.cons (P (pv "c")) (P pw), ap (.atexp (accessor "name")) [v "c"]), (P (listP []), str "none")]), .pat pw (ap (qv "Debug" "log") [str "person_name", v "personName"]), .pat pw (ap (qv "Debug" "log") [str "company_name", v "companyName"])] (ap (v "text") [str "done"])) ],
checks := [
chk .check "via_var: 42" .float,
chk .check "mapped: [10,20,30]" .data,
chk .check "person_name: \"Alice\"" .string,
chk .check "company_name: \"ACME\"" .string ] }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_AccessorVariableTest 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_AccessorVariableTest 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_AccessorVariableTest states that the program has a meaning by r:p-program, with both premises, and ident_elm_AccessorVariableTest that its specialisation is unique and is the reduced program above, both with no hypothesis about principality.