Elm Exactly
Results / elm /

RecordRowsTest

Tags:elm-spec

Checks, in A Standard Definition of Elm?:rule s-exprowrule s-tyrowrule s-patrowrule s-ty-unitrule dm-topdec-aliasrule sm-typbind-record

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
Record expressions, record types and record patterns are rows of fields.

A Standard Definition of Elm?, rules s-exprow (a record expression's fields, each typed by its
expression), s-tyrow (a record type's fields), s-patrow (a record pattern's field binds the
variable spelt as its label) and s-ty-unit (the type `()`). The record alias `Point` declares a
constructor taking the fields in the order written (rule dm-topdec-alias). <https://elm-test-suite.netlify.app/standard/rules/s-patrow>

Results

elm-0.19.1

passran in 46 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.1/optimize for better performance and smaller assets.
sum of fields: 3
label: "p"
constructor order: (1,2)
unit: 5

elm-0.19.2

passran in 67 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.2/optimize for better performance and smaller assets.
sum of fields: 3
label: "p"
constructor order: (1,2)
unit: 5

elm-0.19.3

passran in 65 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.3/optimize for better performance and smaller assets.
constructor order: (1,2)
unit: 5
label: "p"
sum of fields: 3

lamdera-1.5.0

passran in 58 ms
Output
constructor order: (1,2)
unit: 5
label: "p"
sum of fields: 3

Discussions

Discuss this test·Search the discussions on GitHub

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

Source

The -- CHECK: lines give the expected output.

module RecordRowsTest exposing (main)
{-| Record expressions, record types and record patterns are rows of fields.
A Standard Definition of Elm?, rules s-exprow (a record expression's fields, each typed by its
expression), s-tyrow (a record type's fields), s-patrow (a record pattern's field binds the
variable spelt as its label) and s-ty-unit (the type `()`). The record alias `Point` declares a
constructor taking the fields in the order written (rule dm-topdec-alias). <https://elm-test-suite.netlify.app/standard/rules/s-patrow>
-}
-- TAGS: elm-spec, s-exprow, s-tyrow, s-patrow, s-ty-unit, dm-topdec-alias, sm-typbind-record
-- CHECK: sum of fields: 3
-- CHECK: label: "p"
-- CHECK: constructor order: (1,2)
-- CHECK: unit: 5
import Html
type alias Point =
{ y : Int, x : Int }
sumFields : { x : Int, y : Int, label : String } -> Int
sumFields { x, y } =
x + y
labelOf : { x : Int, y : Int, label : String } -> String
labelOf { label } =
label
five : () -> Int
five () =
5
main =
let
r =
{ x = 1, y = 2, label = "p" }
_ =
Debug.log "sum of fields" (sumFields r)
_ =
Debug.log "label" (labelOf r)
_ =
Debug.log "constructor order" (let p = Point 1 2 in ( p.y, p.x ))
_ =
Debug.log "unit" (five ())
in
Html.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_RecordRowsTest, in StandardElmMech/Generated/Elm_RecordAccessorFunctionT.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
sum of fields33t_elm_RecordRowsTest
label"p""p"t_elm_RecordRowsTest
constructor order(1,2)(1,2)t_elm_RecordRowsTest
unit55t_elm_RecordRowsTest
/-- tests/elm/src/RecordRowsTest.elm -/
def g_elm_RecordRowsTest : Program := {
  name := "RecordRowsTest", source := "tests/elm/src",
  exposing := (.items [.var "main"]), imports := [⟨"Html", "Html", none⟩],
  types := [],
  aliases := [("Point", some ["y", "x"])],
  defs := [
    .fn "sumFields" [precord [("x", "x"), ("y", "y")]] (bin "+" (v "x") (v "y")),
    .fn "labelOf" [precord [("label", "label")]] (v "label"),
    .fn "five" [.unit] (i 5),
    .fn "main" [] (letDefs [.fn "r" [] (recE [("x", .atexp (.scon (.intF 1))), ("y", .atexp (.scon (.intF 2))), ("label", str "p")]), .fn "r$Int$Int" [] (recE [("x", i 1), ("y", i 2), ("label", str "p")]), .pat pw (ap (qv "Debug" "log") [str "sum of fields", .atexp (.paren (ap (v "sumFields") [v "r$Int$Int"]))]), .pat pw (ap (qv "Debug" "log") [str "label", .atexp (.paren (ap (v "labelOf") [v "r$Int$Int"]))]), .pat pw (ap (qv "Debug" "log") [str "constructor order", .atexp (.paren (letDefs [.fn "p" [] (ap (c "Point") [i 1, i 2])] (tup2 (.atexp (access (A (v "p")) "y")) (.atexp (access (A (v "p")) "x")))))]), .pat pw (ap (qv "Debug" "log") [str "unit", .atexp (.paren (ap (v "five") [.atexp .unit]))])] (ap (qv "Html" "text") [str "done"])) ],
  checks := [
    chk .check "sum of fields: 3" .int,
    chk .check "label: \"p\"" .string,
    chk .check "constructor order: (1,2)" .data,
    chk .check "unit: 5" .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_RecordRowsTest 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_RecordRowsTest 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_RecordRowsTest states that the program has a meaning by r:p-program, with both premises, and ident_elm_RecordRowsTest that its specialisation is unique and is the reduced program above, both with no hypothesis about principality.