CoreIssue988NanContainerSelfEqualityTest
Checks, in A Standard Definition of Elm?:basis Basics.eq
Translated into Lean for the mechanisation of the book: see its Lean translation.
Issue: elm/core#988
In Elm 0.19.1 any container holding NaN equals itself, because `(==)` first checks whether the two sides are the same object. <https://github.com/elm/core/issues/988> NaN is not equal to anything, itself included, so no value holding NaN can be equal to anything. Elm 0.19.1's `_Utils_eq` returns True as soon as `x === y`, before it looks inside, so a record, custom type or `Maybe` compared with itself is True. The shortcut applies at every level: two separately built tuples, or lists, holding the same inner NaN tuple are equal too. (CompilerIssue2362NanTupleEqualityTest covers a tuple, CoreIssue988NanEqualityReferenceTest a list.) The containers that only library functions can build, `Array`, `Dict` and `Set`, are checked by NanLibraryContainerSelfEqualityTest.
Results
elm-0.19.1
failran in 69 msMissing pattern: record: False --- output --- Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.1/optimize for better performance and smaller assets. record: True custom type: True maybe: True shared inner tuple: True shared inner list: True
elm-0.19.2
failran in 60 msMissing pattern: record: False --- output --- Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.2/optimize for better performance and smaller assets. record: True custom type: True maybe: True shared inner tuple: True shared inner list: True
elm-0.19.3
failran in 49 msMissing pattern: record: False --- output --- Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.3/optimize for better performance and smaller assets. record: True custom type: True maybe: True shared inner tuple: True shared inner list: True
lamdera-1.5.0
failran in 56 msMissing pattern: record: False --- output --- record: True custom type: True maybe: True shared inner tuple: True shared inner list: True
Discussions
Discuss this test·Search the discussions on GitHub
Discussions are on GitHub; one about this test has [t:elm/CoreIssue988NanContainerSelfEqualityTest] in its title.
Source
The -- CHECK: lines give the expected output.
module CoreIssue988NanContainerSelfEqualityTest exposing (main)
{-| In Elm 0.19.1 any container holding NaN equals itself, because `(==)` first checks whether
the two sides are the same object.
<https://github.com/elm/core/issues/988>
NaN is not equal to anything, itself included, so no value holding NaN can be equal to anything.
Elm 0.19.1's `_Utils_eq` returns True as soon as `x === y`, before it looks inside, so a record,
custom type or `Maybe` compared with itself is True. The shortcut applies at every level: two
separately built tuples, or lists, holding the same inner NaN tuple are equal too.
(CompilerIssue2362NanTupleEqualityTest covers a tuple, CoreIssue988NanEqualityReferenceTest a
list.) The containers that only library functions can build, `Array`, `Dict` and `Set`, are
checked by NanLibraryContainerSelfEqualityTest.
-}
-- TAGS: elm19-bug
-- TAGS: elm-spec, b:Basics.eq
-- CHECK: record: False
-- CHECK: custom type: False
-- CHECK: maybe: False
-- CHECK: shared inner tuple: False
-- CHECK: shared inner list: False
import Html exposing (text)
type Box
= Box Float
main =
let
nan =
0 / 0
record =
{ x = nan }
box =
Box nan
maybe =
Just nan
inner =
( nan, 1 )
_ =
Debug.log "record" (record == record)
_ =
Debug.log "custom type" (box == box)
_ =
Debug.log "maybe" (maybe == maybe)
_ =
Debug.log "shared inner tuple" (( inner, 2 ) == ( inner, 2 ))
_ =
Debug.log "shared inner list" ([ inner ] == [ inner ])
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_CoreIssue988NanContainerSelfEqualityTest, in StandardElmMech/Generated/Elm_CompilerIssue22.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 |
|---|---|---|---|
record | False | False | t_elm_CoreIssue988NanContainerSelfEqualityTest |
custom type | False | False | t_elm_CoreIssue988NanContainerSelfEqualityTest |
maybe | False | False | t_elm_CoreIssue988NanContainerSelfEqualityTest |
shared inner tuple | False | False | t_elm_CoreIssue988NanContainerSelfEqualityTest |
shared inner list | False | False | t_elm_CoreIssue988NanContainerSelfEqualityTest |
/-- tests/elm/src/CoreIssue988NanContainerSelfEqualityTest.elm -/
def g_elm_CoreIssue988NanContainerSelfEqualityTest : Program := {
name := "CoreIssue988NanContainerSelfEqualityTest", source := "tests/elm/src",
exposing := (.items [.var "main"]), imports := [⟨"Html", "Html", some (.items [.var "text"])⟩],
types := [("Box", [("Box", 1)])],
aliases := [],
defs := [
.fn "main" [] (letDefs [.fn "nan" [] (bin "/" (.atexp (.scon (.intF 0))) (.atexp (.scon (.intF 0)))), .fn "record" [] (recE [("x", v "nan")]), .fn "box" [] (ap (c "Box") [v "nan"]), .fn "maybe" [] (ap (c "Just") [v "nan"]), .fn "inner" [] (tup2 (v "nan") (.atexp (.scon (.intF 1)))), .pat pw (ap (qv "Debug" "log") [str "record", .atexp (.paren (bin "==" (v "record") (v "record")))]), .pat pw (ap (qv "Debug" "log") [str "custom type", .atexp (.paren (bin "==" (v "box") (v "box")))]), .pat pw (ap (qv "Debug" "log") [str "maybe", .atexp (.paren (bin "==" (v "maybe") (v "maybe")))]), .pat pw (ap (qv "Debug" "log") [str "shared inner tuple", .atexp (.paren (bin "==" (tup2 (v "inner") (.atexp (.scon (.intF 2)))) (tup2 (v "inner") (.atexp (.scon (.intF 2))))))]), .pat pw (ap (qv "Debug" "log") [str "shared inner list", .atexp (.paren (bin "==" (listE [v "inner"]) (listE [v "inner"])))])] (ap (v "text") [str "done"])) ],
checks := [
chk .check "record: False" .data,
chk .check "custom type: False" .data,
chk .check "maybe: False" .data,
chk .check "shared inner tuple: False" .data,
chk .check "shared inner list: 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_CoreIssue988NanContainerSelfEqualityTest 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_CoreIssue988NanContainerSelfEqualityTest 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_CoreIssue988NanContainerSelfEqualityTest states that the program has a meaning by r:p-program, with both premises, and ident_elm_CoreIssue988NanContainerSelfEqualityTest that its specialisation is unique and is the reduced program above, both with no hypothesis about principality.