Elm Exactly
Results / elm-core /

ContainerEqualityRecordFloatTest

Tags:elm-spec

Checks, in A Standard Definition of Elm?:basis 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
Checks `==` on records whose fields are `Float`, including NaN, infinities
and negative zero.

Record fields are compared with `Float` equality: `NaN` is unequal even to
itself, `-0.0` equals `0.0`, and `Infinity` equals itself but not `-Infinity`.
Records are not `comparable`, so only `==` is used.

  - Two-field records: equal, differing in each field, `0.0` against `-0.0`
    (True), NaN (False), the same infinity (True) and opposite infinities
    (False).
  - Three-field records: equal, differing in the middle, zeros of mixed sign
    (True), NaN in the middle (False), and infinities with mixed-sign zeros
    (True).

Results

elm-0.19.1

passran in 65 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.1/optimize for better performance and smaller assets.
recEqual: True
recXDiff: False
recYDiff: False
recNegZero: True
recNaN: False
recPosInf: True
recInfSign: False
rec3Equal: True
rec3MidDiff: False
rec3AllNegZero: True
rec3NaNMid: False
rec3MixedInf: True

elm-0.19.2

passran in 78 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.2/optimize for better performance and smaller assets.
recEqual: True
recXDiff: False
recYDiff: False
recNegZero: True
recNaN: False
recPosInf: True
recInfSign: False
rec3Equal: True
rec3MidDiff: False
rec3AllNegZero: True
rec3NaNMid: False
rec3MixedInf: True

elm-0.19.3

passran in 72 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.3/optimize for better performance and smaller assets.
recEqual: True
recXDiff: False
recYDiff: False
recNegZero: True
recNaN: False
recPosInf: True
recInfSign: False
rec3Equal: True
rec3MidDiff: False
rec3AllNegZero: True
rec3NaNMid: False
rec3MixedInf: True

lamdera-1.5.0

passran in 57 ms
Output
recEqual: True
recXDiff: False
recYDiff: False
recNegZero: True
recNaN: False
recPosInf: True
recInfSign: False
rec3Equal: True
rec3MidDiff: False
rec3AllNegZero: True
rec3NaNMid: False
rec3MixedInf: True

Discussions

Discuss this test·Search the discussions on GitHub

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

Source

The -- CHECK: lines give the expected output.

module ContainerEqualityRecordFloatTest exposing (main)
{-| Checks `==` on records whose fields are `Float`, including NaN, infinities
and negative zero.
Record fields are compared with `Float` equality: `NaN` is unequal even to
itself, `-0.0` equals `0.0`, and `Infinity` equals itself but not `-Infinity`.
Records are not `comparable`, so only `==` is used.
- Two-field records: equal, differing in each field, `0.0` against `-0.0`
(True), NaN (False), the same infinity (True) and opposite infinities
(False).
- Three-field records: equal, differing in the middle, zeros of mixed sign
(True), NaN in the middle (False), and infinities with mixed-sign zeros
(True).
-}
-- TAGS: elm-spec, b:Basics.eq
-- CHECK: recEqual: True
-- CHECK: recXDiff: False
-- CHECK: recYDiff: False
-- CHECK: recNegZero: True
-- CHECK: recNaN: False
-- CHECK: recPosInf: True
-- CHECK: recInfSign: False
-- CHECK: rec3Equal: True
-- CHECK: rec3MidDiff: False
-- CHECK: rec3AllNegZero: True
-- CHECK: rec3NaNMid: False
-- CHECK: rec3MixedInf: True
import Html exposing (text)
main =
let
nan = 0.0 / 0.0
posInf = 1.0 / 0.0
negInf = -1.0 / 0.0
-- Two-field record ----------------------------------------------
_ = Debug.log "recEqual" ({ x = 1.5, y = 2.5 } == { x = 1.5, y = 2.5 })
_ = Debug.log "recXDiff" ({ x = 1.5, y = 2.5 } == { x = 2.5, y = 2.5 })
_ = Debug.log "recYDiff" ({ x = 1.5, y = 2.5 } == { x = 1.5, y = 3.5 })
_ = Debug.log "recNegZero" ({ x = 0.0, y = 1.0 } == { x = -0.0, y = 1.0 })
_ = Debug.log "recNaN" ({ x = nan, y = 1.0 } == { x = nan, y = 1.0 })
_ = Debug.log "recPosInf" ({ x = posInf, y = 1.0 } == { x = posInf, y = 1.0 })
_ = Debug.log "recInfSign" ({ x = posInf, y = 1.0 } == { x = negInf, y = 1.0 })
-- Three-field record --------------------------------------------
_ = Debug.log "rec3Equal" ({ a = 1.0, b = 2.0, c = 3.0 } == { a = 1.0, b = 2.0, c = 3.0 })
_ = Debug.log "rec3MidDiff" ({ a = 1.0, b = 2.0, c = 3.0 } == { a = 1.0, b = 9.0, c = 3.0 })
_ = Debug.log "rec3AllNegZero" ({ a = 0.0, b = -0.0, c = 0.0 } == { a = -0.0, b = 0.0, c = -0.0 })
_ = Debug.log "rec3NaNMid" ({ a = 1.0, b = nan, c = 3.0 } == { a = 1.0, b = nan, c = 3.0 })
_ = Debug.log "rec3MixedInf" ({ a = posInf, b = negInf, c = 0.0 } == { a = posInf, b = negInf, c = -0.0 })
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_ContainerEqualityRecordFloatTest, 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/ContainerEqualityRecordFloatTest.elm -/
def g_elm_core_ContainerEqualityRecordFloatTest : Program := {
  name := "ContainerEqualityRecordFloatTest", source := "tests/elm-core/src",
  exposing := (.items [.var "main"]), imports := [⟨"Html", "Html", some (.items [.var "text"])⟩],
  types := [],
  aliases := [],
  defs := [
    .fn "main" [] (letDefs [.fn "nan" [] (bin "/" (.atexp (.scon (.float 0 (-1)))) (.atexp (.scon (.float 0 (-1))))), .fn "posInf" [] (bin "/" (.atexp (.scon (.float 10 (-1)))) (.atexp (.scon (.float 0 (-1))))), .fn "negInf" [] (bin "/" (.atexp (neg (.scon (.float 10 (-1))))) (.atexp (.scon (.float 0 (-1))))), .pat pw (ap (qv "Debug" "log") [str "recEqual", .atexp (.paren (bin "==" (recE [("x", .atexp (.scon (.float 15 (-1)))), ("y", .atexp (.scon (.float 25 (-1))))]) (recE [("x", .atexp (.scon (.float 15 (-1)))), ("y", .atexp (.scon (.float 25 (-1))))])))]), .pat pw (ap (qv "Debug" "log") [str "recXDiff", .atexp (.paren (bin "==" (recE [("x", .atexp (.scon (.float 15 (-1)))), ("y", .atexp (.scon (.float 25 (-1))))]) (recE [("x", .atexp (.scon (.float 25 (-1)))), ("y", .atexp (.scon (.float 25 (-1))))])))]), .pat pw (ap (qv "Debug" "log") [str "recYDiff", .atexp (.paren (bin "==" (recE [("x", .atexp (.scon (.float 15 (-1)))), ("y", .atexp (.scon (.float 25 (-1))))]) (recE [("x", .atexp (.scon (.float 15 (-1)))), ("y", .atexp (.scon (.float 35 (-1))))])))]), .pat pw (ap (qv "Debug" "log") [str "recNegZero", .atexp (.paren (bin "==" (recE [("x", .atexp (.scon (.float 0 (-1)))), ("y", .atexp (.scon (.float 10 (-1))))]) (recE [("x", .atexp (neg (.scon (.float 0 (-1))))), ("y", .atexp (.scon (.float 10 (-1))))])))]), .pat pw (ap (qv "Debug" "log") [str "recNaN", .atexp (.paren (bin "==" (recE [("x", v "nan"), ("y", .atexp (.scon (.float 10 (-1))))]) (recE [("x", v "nan"), ("y", .atexp (.scon (.float 10 (-1))))])))]), .pat pw (ap (qv "Debug" "log") [str "recPosInf", .atexp (.paren (bin "==" (recE [("x", v "posInf"), ("y", .atexp (.scon (.float 10 (-1))))]) (recE [("x", v "posInf"), ("y", .atexp (.scon (.float 10 (-1))))])))]), .pat pw (ap (qv "Debug" "log") [str "recInfSign", .atexp (.paren (bin "==" (recE [("x", v "posInf"), ("y", .atexp (.scon (.float 10 (-1))))]) (recE [("x", v "negInf"), ("y", .atexp (.scon (.float 10 (-1))))])))]), .pat pw (ap (qv "Debug" "log") [str "rec3Equal", .atexp (.paren (bin "==" (recE [("a", .atexp (.scon (.float 10 (-1)))), ("b", .atexp (.scon (.float 20 (-1)))), ("c", .atexp (.scon (.float 30 (-1))))]) (recE [("a", .atexp (.scon (.float 10 (-1)))), ("b", .atexp (.scon (.float 20 (-1)))), ("c", .atexp (.scon (.float 30 (-1))))])))]), .pat pw (ap (qv "Debug" "log") [str "rec3MidDiff", .atexp (.paren (bin "==" (recE [("a", .atexp (.scon (.float 10 (-1)))), ("b", .atexp (.scon (.float 20 (-1)))), ("c", .atexp (.scon (.float 30 (-1))))]) (recE [("a", .atexp (.scon (.float 10 (-1)))), ("b", .atexp (.scon (.float 90 (-1)))), ("c", .atexp (.scon (.float 30 (-1))))])))]), .pat pw (ap (qv "Debug" "log") [str "rec3AllNegZero", .atexp (.paren (bin "==" (recE [("a", .atexp (.scon (.float 0 (-1)))), ("b", .atexp (neg (.scon (.float 0 (-1))))), ("c", .atexp (.scon (.float 0 (-1))))]) (recE [("a", .atexp (neg (.scon (.float 0 (-1))))), ("b", .atexp (.scon (.float 0 (-1)))), ("c", .atexp (neg (.scon (.float 0 (-1)))))])))]), .pat pw (ap (qv "Debug" "log") [str "rec3NaNMid", .atexp (.paren (bin "==" (recE [("a", .atexp (.scon (.float 10 (-1)))), ("b", v "nan"), ("c", .atexp (.scon (.float 30 (-1))))]) (recE [("a", .atexp (.scon (.float 10 (-1)))), ("b", v "nan"), ("c", .atexp (.scon (.float 30 (-1))))])))]), .pat pw (ap (qv "Debug" "log") [str "rec3MixedInf", .atexp (.paren (bin "==" (recE [("a", v "posInf"), ("b", v "negInf"), ("c", .atexp (.scon (.float 0 (-1))))]) (recE [("a", v "posInf"), ("b", v "negInf"), ("c", .atexp (neg (.scon (.float 0 (-1)))))])))])] (ap (v "text") [str "done"])) ],
  checks := [
    chk .check "recEqual: True" .data,
    chk .check "recXDiff: False" .data,
    chk .check "recYDiff: False" .data,
    chk .check "recNegZero: True" .data,
    chk .check "recNaN: False" .data,
    chk .check "recPosInf: True" .data,
    chk .check "recInfSign: False" .data,
    chk .check "rec3Equal: True" .data,
    chk .check "rec3MidDiff: False" .data,
    chk .check "rec3AllNegZero: True" .data,
    chk .check "rec3NaNMid: False" .data,
    chk .check "rec3MixedInf: 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_ContainerEqualityRecordFloatTest 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_ContainerEqualityRecordFloatTest 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_ContainerEqualityRecordFloatTest states that the program has a meaning by r:p-program, with both premises, and ident_elm_core_ContainerEqualityRecordFloatTest that its specialisation is unique and is the reduced program above, both with no hypothesis about principality.