Elm Exactly
Results / elm-core /

CompareRecordWithEmptyListFieldTest

Tags:elm-spec

Checks, in A Standard Definition of Elm?:basis Basics.comparebasis Basics.eq

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
Ordering and equality of a record whose `List Int` field may be empty.

Records are not `comparable`, so the test orders two `Rec` values with a
hand-written `cmpRec` that compares the `xs` fields and then the `n` fields. It
checks that an empty `xs` orders before `[ 1 ]` and equal to another empty
list, and that `==` on the records themselves tells an empty `xs` apart from a
non-empty one.

Results

elm-0.19.1

passran in 75 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.1/optimize for better performance and smaller assets.
cmp_ee: EQ
cmp_eA: LT
cmp_Ae: GT
eq_ee: True
eq_eA: False
eq_Ae: False

elm-0.19.2

passran in 60 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.2/optimize for better performance and smaller assets.
cmp_ee: EQ
cmp_eA: LT
cmp_Ae: GT
eq_ee: True
eq_eA: False
eq_Ae: False

elm-0.19.3

passran in 73 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.3/optimize for better performance and smaller assets.
eq_ee: True
cmp_ee: EQ
eq_Ae: False
eq_eA: False
cmp_Ae: GT
cmp_eA: LT

lamdera-1.5.0

passran in 68 ms
Output
eq_ee: True
cmp_ee: EQ
eq_Ae: False
eq_eA: False
cmp_Ae: GT
cmp_eA: LT

Discussions

Discuss this test·Search the discussions on GitHub

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

Source

The -- CHECK: lines give the expected output.

module CompareRecordWithEmptyListFieldTest exposing (main)
{-| Ordering and equality of a record whose `List Int` field may be empty.
Records are not `comparable`, so the test orders two `Rec` values with a
hand-written `cmpRec` that compares the `xs` fields and then the `n` fields. It
checks that an empty `xs` orders before `[ 1 ]` and equal to another empty
list, and that `==` on the records themselves tells an empty `xs` apart from a
non-empty one.
-}
-- TAGS: elm-spec, b:Basics.compare, b:Basics.eq
-- CHECK: cmp_ee: EQ
-- CHECK: cmp_eA: LT
-- CHECK: cmp_Ae: GT
-- CHECK: eq_ee: True
-- CHECK: eq_eA: False
-- CHECK: eq_Ae: False
import Html exposing (text)
type alias Rec =
{ xs : List Int, n : Int }
main =
let
emptyL : List Int
emptyL = []
re : Rec
re = { xs = emptyL, n = 0 }
ra : Rec
ra = { xs = [ 1 ], n = 0 }
cmpRec : Rec -> Rec -> Order
cmpRec r1 r2 =
case compare r1.xs r2.xs of
EQ -> compare r1.n r2.n
ord -> ord
_ = Debug.log "cmp_ee" (cmpRec re re)
_ = Debug.log "cmp_eA" (cmpRec re ra)
_ = Debug.log "cmp_Ae" (cmpRec ra re)
_ = Debug.log "eq_ee" (re == re)
_ = Debug.log "eq_eA" (re == ra)
_ = Debug.log "eq_Ae" (ra == re)
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_core_CompareRecordWithEmptyListFieldTest, in StandardElmMech/Generated/ElmCore_CompareI.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.

/-- tests/elm-core/src/CompareRecordWithEmptyListFieldTest.elm -/
def g_elm_core_CompareRecordWithEmptyListFieldTest : Program := {
  name := "CompareRecordWithEmptyListFieldTest", source := "tests/elm-core/src",
  exposing := (.items [.var "main"]), imports := [⟨"Html", "Html", some (.items [.var "text"])⟩],
  types := [],
  aliases := [("Rec", some ["xs", "n"])],
  defs := [
    .fn "main" [] (letDefs [.fn "emptyL" [] (listE []), .fn "re" [] (recE [("xs", v "emptyL"), ("n", i 0)]), .fn "ra" [] (recE [("xs", listE [i 1]), ("n", i 0)]), .fn "cmpRec" [pv "r1", pv "r2"] (caseOf (ap (v "compare") [.atexp (access (A (v "r1")) "xs"), .atexp (access (A (v "r2")) "xs")]) [(P (pcA "EQ"), ap (v "compare") [.atexp (access (A (v "r1")) "n"), .atexp (access (A (v "r2")) "n")]), (P (pv "ord"), v "ord")]), .pat pw (ap (qv "Debug" "log") [str "cmp_ee", .atexp (.paren (ap (v "cmpRec") [v "re", v "re"]))]), .pat pw (ap (qv "Debug" "log") [str "cmp_eA", .atexp (.paren (ap (v "cmpRec") [v "re", v "ra"]))]), .pat pw (ap (qv "Debug" "log") [str "cmp_Ae", .atexp (.paren (ap (v "cmpRec") [v "ra", v "re"]))]), .pat pw (ap (qv "Debug" "log") [str "eq_ee", .atexp (.paren (bin "==" (v "re") (v "re")))]), .pat pw (ap (qv "Debug" "log") [str "eq_eA", .atexp (.paren (bin "==" (v "re") (v "ra")))]), .pat pw (ap (qv "Debug" "log") [str "eq_Ae", .atexp (.paren (bin "==" (v "ra") (v "re")))])] (ap (v "text") [str "done"])) ],
  checks := [
    chk .check "cmp_ee: EQ" .data,
    chk .check "cmp_eA: LT" .data,
    chk .check "cmp_Ae: GT" .data,
    chk .check "eq_ee: True" .data,
    chk .check "eq_eA: False" .data,
    chk .check "eq_Ae: False" .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_core_CompareRecordWithEmptyListFieldTest 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_core_CompareRecordWithEmptyListFieldTest 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_core_CompareRecordWithEmptyListFieldTest states that the program has a meaning by r:p-program, with both premises, and ident_elm_core_CompareRecordWithEmptyListFieldTest that its specialisation is unique and is the reduced program above, both with no hypothesis about principality.