Elm Exactly
Results / elm-core /

EqBoxedFastPathTest

Tags:elm-spec

Checks, in A Standard Definition of Elm?:basis Basics.eqbasis Basics.neq

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
Structural equality gives the correct answer for strings, lists, tuples,
records, custom types, unit, empty values, `Bool` and nested containers, through
both `==` and `/=`.

Each shape is compared with an equal value built separately (`strTwin`,
`listTwin`, `tupleTwin`, `recordTwin`, `customTwin`, `nestedTwin`) and, except
the nested one, with a different value (`strDiff`, `listDiff`, `tupleDiff`,
`recordDiff`, `customDiff`). A string and a list are also compared with
themselves (`strSame`, `listSame`). `twinA` and `twinB` are computed at run time
by the same expression, `suffixed "42"`, which appends `"x"`, so they are equal strings
that are not one literal; `upFrom 1`, where `upFrom n` builds
`[ n, n + 1, n + 2 ]` at run time, is compared with the literal `[ 1, 2, 3 ]`
for the same reason.

The remaining cases compare `()` with `()`, `""` with `""`, `[]` with `[]`, and
the `Bool` constants with each other; `strNe` and `boolNe` check `/=`.

Results

elm-0.19.1

passran in 60 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.1/optimize for better performance and smaller assets.
strSame: True
strTwin: True
strDiff: False
strNe: True
listSame: True
listTwin: True
listDiff: False
tupleTwin: True
tupleDiff: False
recordTwin: True
recordDiff: False
customTwin: True
customDiff: False
unitEq: True
emptyStrEq: True
emptyListEq: True
boolTT: True
boolTF: False
boolNe: True
nestedTwin: True

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.
strSame: True
strTwin: True
strDiff: False
strNe: True
listSame: True
listTwin: True
listDiff: False
tupleTwin: True
tupleDiff: False
recordTwin: True
recordDiff: False
customTwin: True
customDiff: False
unitEq: True
emptyStrEq: True
emptyListEq: True
boolTT: True
boolTF: False
boolNe: True
nestedTwin: True

elm-0.19.3

passran in 76 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.3/optimize for better performance and smaller assets.
strTwin: True
strDiff: False
strNe: True
listTwin: True
listDiff: False
tupleTwin: True
tupleDiff: False
recordTwin: True
recordDiff: False
customTwin: True
customDiff: False
unitEq: True
emptyStrEq: True
emptyListEq: True
boolTT: True
boolTF: False
boolNe: True
nestedTwin: True
strSame: True
listSame: True

lamdera-1.5.0

passran in 43 ms
Output
strTwin: True
strDiff: False
strNe: True
listTwin: True
listDiff: False
tupleTwin: True
tupleDiff: False
recordTwin: True
recordDiff: False
customTwin: True
customDiff: False
unitEq: True
emptyStrEq: True
emptyListEq: True
boolTT: True
boolTF: False
boolNe: True
nestedTwin: True
strSame: True
listSame: True

Discussions

Discuss this test·Search the discussions on GitHub

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

Source

The -- CHECK: lines give the expected output.

module EqBoxedFastPathTest exposing (main)
{-| Structural equality gives the correct answer for strings, lists, tuples,
records, custom types, unit, empty values, `Bool` and nested containers, through
both `==` and `/=`.
Each shape is compared with an equal value built separately (`strTwin`,
`listTwin`, `tupleTwin`, `recordTwin`, `customTwin`, `nestedTwin`) and, except
the nested one, with a different value (`strDiff`, `listDiff`, `tupleDiff`,
`recordDiff`, `customDiff`). A string and a list are also compared with
themselves (`strSame`, `listSame`). `twinA` and `twinB` are computed at run time
by the same expression, `suffixed "42"`, which appends `"x"`, so they are equal strings
that are not one literal; `upFrom 1`, where `upFrom n` builds
`[ n, n + 1, n + 2 ]` at run time, is compared with the literal `[ 1, 2, 3 ]`
for the same reason.
The remaining cases compare `()` with `()`, `""` with `""`, `[]` with `[]`, and
the `Bool` constants with each other; `strNe` and `boolNe` check `/=`.
-}
-- TAGS: elm-spec, b:Basics.eq, b:Basics.neq
-- CHECK: strSame: True
-- CHECK: strTwin: True
-- CHECK: strDiff: False
-- CHECK: strNe: True
-- CHECK: listSame: True
-- CHECK: listTwin: True
-- CHECK: listDiff: False
-- CHECK: tupleTwin: True
-- CHECK: tupleDiff: False
-- CHECK: recordTwin: True
-- CHECK: recordDiff: False
-- CHECK: customTwin: True
-- CHECK: customDiff: False
-- CHECK: unitEq: True
-- CHECK: emptyStrEq: True
-- CHECK: emptyListEq: True
-- CHECK: boolTT: True
-- CHECK: boolTF: False
-- CHECK: boolNe: True
-- CHECK: nestedTwin: True
import Html exposing (text)
type Shape
= Circle Int
| Rect Int Int
{-| `s` with `"x"` appended, at run time.
-}
suffixed : String -> String
suffixed s =
s ++ "x"
twinA : String
twinA =
suffixed "42"
twinB : String
twinB =
suffixed "42"
upFrom : Int -> List Int
upFrom n =
[ n, n + 1, n + 2 ]
main : Html.Html msg
main =
let
s =
"hello"
l =
[ 1, 2, 3 ]
_ =
Debug.log "strSame" (s == s)
-- Equal strings computed separately, not the same literal.
_ =
Debug.log "strTwin" (twinA == twinB)
_ =
Debug.log "strDiff" (twinA == "nope")
_ =
Debug.log "strNe" (twinA /= "nope")
_ =
Debug.log "listSame" (l == l)
_ =
Debug.log "listTwin" (upFrom 1 == [ 1, 2, 3 ])
_ =
Debug.log "listDiff" (upFrom 1 == [ 1, 2 ])
_ =
Debug.log "tupleTwin" (( 1, "a" ) == ( 1, "a" ))
_ =
Debug.log "tupleDiff" (( 1, "a" ) == ( 1, "b" ))
_ =
Debug.log "recordTwin" ({ x = 1, y = "a" } == { x = 1, y = "a" })
_ =
Debug.log "recordDiff" ({ x = 1, y = "a" } == { x = 2, y = "a" })
_ =
Debug.log "customTwin" (Rect 2 3 == Rect 2 3)
_ =
Debug.log "customDiff" (Rect 2 3 == Circle 2)
_ =
Debug.log "unitEq" (() == ())
_ =
Debug.log "emptyStrEq" ("" == "")
_ =
Debug.log "emptyListEq" ([] == [])
_ =
Debug.log "boolTT" (True == True)
_ =
Debug.log "boolTF" (True == False)
_ =
Debug.log "boolNe" (True /= False)
_ =
Debug.log "nestedTwin" (Just [ ( 1, "a" ) ] == Just [ ( 1, "a" ) ])
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_EqBoxedFastPathTest, in StandardElmMech/Generated/ElmCore_CoreIssue115.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/EqBoxedFastPathTest.elm -/
def g_elm_core_EqBoxedFastPathTest : Program := {
  name := "EqBoxedFastPathTest", source := "tests/elm-core/src",
  exposing := (.items [.var "main"]), imports := [⟨"Html", "Html", some (.items [.var "text"])⟩],
  types := [("Shape", [("Circle", 1), ("Rect", 2)])],
  aliases := [],
  defs := [
    .fn "suffixed" [pv "s"] (bin "++" (v "s") (str "x")),
    .fn "twinA" [] (ap (v "suffixed") [str "42"]),
    .fn "twinB" [] (ap (v "suffixed") [str "42"]),
    .fn "upFrom" [pv "n"] (listE [v "n", bin "+" (v "n") (i 1), bin "+" (v "n") (i 2)]),
    .fn "main" [] (letDefs [.fn "s" [] (str "hello"), .fn "l" [] (listE [.atexp (.scon (.intF 1)), .atexp (.scon (.intF 2)), .atexp (.scon (.intF 3))]), .pat pw (ap (qv "Debug" "log") [str "strSame", .atexp (.paren (bin "==" (v "s") (v "s")))]), .pat pw (ap (qv "Debug" "log") [str "strTwin", .atexp (.paren (bin "==" (v "twinA") (v "twinB")))]), .pat pw (ap (qv "Debug" "log") [str "strDiff", .atexp (.paren (bin "==" (v "twinA") (str "nope")))]), .pat pw (ap (qv "Debug" "log") [str "strNe", .atexp (.paren (bin "/=" (v "twinA") (str "nope")))]), .pat pw (ap (qv "Debug" "log") [str "listSame", .atexp (.paren (bin "==" (v "l") (v "l")))]), .pat pw (ap (qv "Debug" "log") [str "listTwin", .atexp (.paren (bin "==" (ap (v "upFrom") [i 1]) (listE [i 1, i 2, i 3])))]), .pat pw (ap (qv "Debug" "log") [str "listDiff", .atexp (.paren (bin "==" (ap (v "upFrom") [i 1]) (listE [i 1, i 2])))]), .pat pw (ap (qv "Debug" "log") [str "tupleTwin", .atexp (.paren (bin "==" (tup2 (.atexp (.scon (.intF 1))) (str "a")) (tup2 (.atexp (.scon (.intF 1))) (str "a"))))]), .pat pw (ap (qv "Debug" "log") [str "tupleDiff", .atexp (.paren (bin "==" (tup2 (.atexp (.scon (.intF 1))) (str "a")) (tup2 (.atexp (.scon (.intF 1))) (str "b"))))]), .pat pw (ap (qv "Debug" "log") [str "recordTwin", .atexp (.paren (bin "==" (recE [("x", .atexp (.scon (.intF 1))), ("y", str "a")]) (recE [("x", .atexp (.scon (.intF 1))), ("y", str "a")])))]), .pat pw (ap (qv "Debug" "log") [str "recordDiff", .atexp (.paren (bin "==" (recE [("x", .atexp (.scon (.intF 1))), ("y", str "a")]) (recE [("x", .atexp (.scon (.intF 2))), ("y", str "a")])))]), .pat pw (ap (qv "Debug" "log") [str "customTwin", .atexp (.paren (bin "==" (ap (c "Rect") [i 2, i 3]) (ap (c "Rect") [i 2, i 3])))]), .pat pw (ap (qv "Debug" "log") [str "customDiff", .atexp (.paren (bin "==" (ap (c "Rect") [i 2, i 3]) (ap (c "Circle") [i 2])))]), .pat pw (ap (qv "Debug" "log") [str "unitEq", .atexp (.paren (bin "==" (.atexp .unit) (.atexp .unit)))]), .pat pw (ap (qv "Debug" "log") [str "emptyStrEq", .atexp (.paren (bin "==" (str "") (str "")))]), .pat pw (ap (qv "Debug" "log") [str "emptyListEq", .atexp (.paren (bin "==" (listE []) (listE [])))]), .pat pw (ap (qv "Debug" "log") [str "boolTT", .atexp (.paren (bin "==" (c "True") (c "True")))]), .pat pw (ap (qv "Debug" "log") [str "boolTF", .atexp (.paren (bin "==" (c "True") (c "False")))]), .pat pw (ap (qv "Debug" "log") [str "boolNe", .atexp (.paren (bin "/=" (c "True") (c "False")))]), .pat pw (ap (qv "Debug" "log") [str "nestedTwin", .atexp (.paren (bin "==" (ap (c "Just") [listE [tup2 (.atexp (.scon (.intF 1))) (str "a")]]) (ap (c "Just") [listE [tup2 (.atexp (.scon (.intF 1))) (str "a")]])))])] (ap (v "text") [str "done"])) ],
  checks := [
    chk .check "strSame: True" .data,
    chk .check "strTwin: True" .data,
    chk .check "strDiff: False" .data,
    chk .check "strNe: True" .data,
    chk .check "listSame: True" .data,
    chk .check "listTwin: True" .data,
    chk .check "listDiff: False" .data,
    chk .check "tupleTwin: True" .data,
    chk .check "tupleDiff: False" .data,
    chk .check "recordTwin: True" .data,
    chk .check "recordDiff: False" .data,
    chk .check "customTwin: True" .data,
    chk .check "customDiff: False" .data,
    chk .check "unitEq: True" .data,
    chk .check "emptyStrEq: True" .data,
    chk .check "emptyListEq: True" .data,
    chk .check "boolTT: True" .data,
    chk .check "boolTF: False" .data,
    chk .check "boolNe: True" .data,
    chk .check "nestedTwin: True" .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_EqBoxedFastPathTest 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_EqBoxedFastPathTest 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_EqBoxedFastPathTest states that the program has a meaning by r:p-program, with both premises, and ident_elm_core_EqBoxedFastPathTest that its specialisation is unique and is the reduced program above, both with no hypothesis about principality.