FloatEdgeTest
Tags:elm-spec
Checks, in A Standard Definition of Elm?:basis Basics.roundbasis Basics.eqbasis Basics.neqbasis Basics.minbasis Basics.isNaNbasis Basics.comparebasis Basics.truncatebasis Basics.pow
Translated into Lean for the mechanisation of the book: see its Lean translation.
Float edge cases: `round` on halves, NaN in equality, `min` and `compare`, `truncate` of NaN,
and an Int power beyond 2^32.
Most rows log `Debug.toString` of the result.
- `round` of plus and minus 0.5, 1.5 and 2.5 rounds halves up, towards positive infinity, also
inside a list (`map round`).
- NaN is not equal to itself (`==`, `/=`, also inside `Just` and a list) or to 1.
- `min 1 nan`, and `min` folded over `[ 1 ]` from NaN by a local `foldl`, give NaN (A48): each
is checked with `isNaN`, which the book defines, and then by its printed form, which it does
not (A62), though every implementation prints `NaN`.
- `compare nan 1` is `GT`, also through a `case` on `compare` (`orderName`) and when the NaN is
the first element of a tuple or list; `orderName` also gives `LT` and `EQ` for ordinary
numbers.
- `truncate nan` is 0.
- `3 ^ 33` is 5559060566555523, exact as an Int.
That `String.toFloat "1.5"` equals the literal `1.5` is checked by StringToFloatLiteralTest.Results
elm-0.19.1
passran in 66 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.1/optimize for better performance and smaller assets. round 2.5: "3" round -2.5: "-2" round 0.5: "1" round -0.5: "0" round 1.5: "2" round -1.5: "-1" nan == nan: "False" nan /= nan: "True" 1 /= nan: "True" min 1 nan is NaN: "True" min 1 nan: "NaN" compare nan 1: "GT" foldl min is NaN: "True" foldl min: "NaN" map round: "[-2,0,3]" case compare nan 1: "GT" case compare 1 2: "LT" case compare 2 2: "EQ" compare tuple nan: "GT" compare list nan: "GT" just nan == just nan: "False" list nan /= list nan: "True" truncate nan: "0" pow 3 33: "5559060566555523"
elm-0.19.2
passran in 49 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.2/optimize for better performance and smaller assets. round 2.5: "3" round -2.5: "-2" round 0.5: "1" round -0.5: "0" round 1.5: "2" round -1.5: "-1" nan == nan: "False" nan /= nan: "True" 1 /= nan: "True" min 1 nan is NaN: "True" min 1 nan: "NaN" compare nan 1: "GT" foldl min is NaN: "True" foldl min: "NaN" map round: "[-2,0,3]" case compare nan 1: "GT" case compare 1 2: "LT" case compare 2 2: "EQ" compare tuple nan: "GT" compare list nan: "GT" just nan == just nan: "False" list nan /= list nan: "True" truncate nan: "0" pow 3 33: "5559060566555523"
elm-0.19.3
passran in 41 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.3/optimize for better performance and smaller assets. round 2.5: "3" round -2.5: "-2" round 0.5: "1" round -0.5: "0" round 1.5: "2" round -1.5: "-1" nan == nan: "False" nan /= nan: "True" 1 /= nan: "True" min 1 nan is NaN: "True" min 1 nan: "NaN" compare nan 1: "GT" foldl min is NaN: "True" foldl min: "NaN" map round: "[-2,0,3]" case compare nan 1: "GT" case compare 1 2: "LT" case compare 2 2: "EQ" compare tuple nan: "GT" compare list nan: "GT" just nan == just nan: "False" list nan /= list nan: "True" truncate nan: "0" pow 3 33: "5559060566555523"
lamdera-1.5.0
passran in 56 msOutput
round 2.5: "3" round -2.5: "-2" round 0.5: "1" round -0.5: "0" round 1.5: "2" round -1.5: "-1" nan == nan: "False" nan /= nan: "True" 1 /= nan: "True" min 1 nan is NaN: "True" min 1 nan: "NaN" compare nan 1: "GT" foldl min is NaN: "True" foldl min: "NaN" map round: "[-2,0,3]" case compare nan 1: "GT" case compare 1 2: "LT" case compare 2 2: "EQ" compare tuple nan: "GT" compare list nan: "GT" just nan == just nan: "False" list nan /= list nan: "True" truncate nan: "0" pow 3 33: "5559060566555523"
Discussions
Discuss this test·Search the discussions on GitHub
Discussions are on GitHub; one about this test has [t:elm/FloatEdgeTest] in its title.
Source
The -- CHECK: lines give the expected output.
module FloatEdgeTest exposing (main)
{-| Float edge cases: `round` on halves, NaN in equality, `min` and `compare`, `truncate` of NaN,
and an Int power beyond 2^32.
Most rows log `Debug.toString` of the result.
- `round` of plus and minus 0.5, 1.5 and 2.5 rounds halves up, towards positive infinity, also
inside a list (`map round`).
- NaN is not equal to itself (`==`, `/=`, also inside `Just` and a list) or to 1.
- `min 1 nan`, and `min` folded over `[ 1 ]` from NaN by a local `foldl`, give NaN (A48): each
is checked with `isNaN`, which the book defines, and then by its printed form, which it does
not (A62), though every implementation prints `NaN`.
- `compare nan 1` is `GT`, also through a `case` on `compare` (`orderName`) and when the NaN is
the first element of a tuple or list; `orderName` also gives `LT` and `EQ` for ordinary
numbers.
- `truncate nan` is 0.
- `3 ^ 33` is 5559060566555523, exact as an Int.
That `String.toFloat "1.5"` equals the literal `1.5` is checked by StringToFloatLiteralTest.
-}
-- TAGS: elm-spec, b:Basics.round, b:Basics.eq, b:Basics.neq, b:Basics.min, b:Basics.isNaN, b:Basics.compare, b:Basics.truncate, b:Basics.pow
-- MODE-GUARD: a Char equality must give the same answer as under --optimize
-- NO-SKIP: an edge case every compiler must run
-- CHECK: round 2.5: "3"
-- CHECK: round -2.5: "-2"
-- CHECK: round 0.5: "1"
-- CHECK: round -0.5: "0"
-- CHECK: round 1.5: "2"
-- CHECK: round -1.5: "-1"
-- CHECK: nan == nan: "False"
-- CHECK: nan /= nan: "True"
-- CHECK: 1 /= nan: "True"
-- CHECK: min 1 nan is NaN: "True"
-- CHECK: min 1 nan: "NaN"
-- CHECK: compare nan 1: "GT"
-- CHECK: foldl min is NaN: "True"
-- CHECK: foldl min: "NaN"
-- CHECK: map round: "[-2,0,3]"
-- CHECK: case compare nan 1: "GT"
-- CHECK: case compare 1 2: "LT"
-- CHECK: case compare 2 2: "EQ"
-- CHECK: compare tuple nan: "GT"
-- CHECK: compare list nan: "GT"
-- CHECK: just nan == just nan: "False"
-- CHECK: list nan /= list nan: "True"
-- CHECK: truncate nan: "0"
-- CHECK: pow 3 33: "5559060566555523"
import Html exposing (text)
nan : Float
nan =
0 / 0
inf : Float
inf =
1 / 0
show : a -> String
show =
Debug.toString
{-| Folds `f` over a list from the left, as `List.foldl` does.
-}
foldl : (a -> b -> b) -> b -> List a -> b
foldl f acc xs =
case xs of
[] ->
acc
x :: rest ->
foldl f (f x acc) rest
{-| Returns the name of `compare a b`, found by a `case` on the `Order`.
-}
orderName : Float -> Float -> String
orderName a b =
case compare a b of
LT ->
"LT"
EQ ->
"EQ"
GT ->
"GT"
main =
let
_ =
Debug.log "round 2.5" (show (round 2.5))
_ =
Debug.log "round -2.5" (show (round -2.5))
_ =
Debug.log "round 0.5" (show (round 0.5))
_ =
Debug.log "round -0.5" (show (round -0.5))
_ =
Debug.log "round 1.5" (show (round 1.5))
_ =
Debug.log "round -1.5" (show (round -1.5))
_ =
Debug.log "nan == nan" (show (nan == nan))
_ =
Debug.log "nan /= nan" (show (nan /= nan))
_ =
Debug.log "1 /= nan" (show (1 /= nan))
_ =
Debug.log "min 1 nan is NaN" (show (isNaN (min 1 nan)))
_ =
Debug.log "min 1 nan" (show (min 1 nan))
_ =
Debug.log "compare nan 1" (show (compare nan 1))
_ =
Debug.log "foldl min is NaN" (show (isNaN (foldl min nan [ 1 ])))
_ =
Debug.log "foldl min" (show (foldl min nan [ 1 ]))
_ =
Debug.log "map round" (show [ round -2.5, round -0.5, round 2.5 ])
_ =
Debug.log "case compare nan 1" (orderName nan 1)
_ =
Debug.log "case compare 1 2" (orderName 1 2)
_ =
Debug.log "case compare 2 2" (orderName 2 2)
_ =
Debug.log "compare tuple nan" (show (compare ( nan, 1 ) ( 1, 1 )))
_ =
Debug.log "compare list nan" (show (compare [ nan ] [ 1 ]))
_ =
Debug.log "just nan == just nan" (show (Just nan == Just nan))
_ =
Debug.log "list nan /= list nan" (show ([ nan ] /= [ nan ]))
_ =
Debug.log "truncate nan" (show (truncate nan))
_ =
Debug.log "pow 3 33" (show (3 ^ 33))
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_FloatEdgeTest, in StandardElmMech/Generated/Elm_Fli.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 |
|---|---|---|---|
round 2.5 | "3" | "3" | t_elm_FloatEdgeTest |
round -2.5 | "-2" | "-2" | t_elm_FloatEdgeTest |
round 0.5 | "1" | "1" | t_elm_FloatEdgeTest |
round -0.5 | "0" | "0" | t_elm_FloatEdgeTest |
round 1.5 | "2" | "2" | t_elm_FloatEdgeTest |
round -1.5 | "-1" | "-1" | t_elm_FloatEdgeTest |
nan == nan | "False" | "False" | t_elm_FloatEdgeTest |
nan /= nan | "True" | "True" | t_elm_FloatEdgeTest |
1 /= nan | "True" | "True" | t_elm_FloatEdgeTest |
min 1 nan is NaN | "True" | "True" | t_elm_FloatEdgeTest |
min 1 nan | "NaN" | "NaN" | t_elm_FloatEdgeTest |
compare nan 1 | "GT" | "GT" | t_elm_FloatEdgeTest |
foldl min is NaN | "True" | "True" | t_elm_FloatEdgeTest |
foldl min | "NaN" | "NaN" | t_elm_FloatEdgeTest |
map round | "[-2,0,3]" | "[-2,0,3]" | t_elm_FloatEdgeTest |
case compare nan 1 | "GT" | "GT" | t_elm_FloatEdgeTest |
case compare 1 2 | "LT" | "LT" | t_elm_FloatEdgeTest |
case compare 2 2 | "EQ" | "EQ" | t_elm_FloatEdgeTest |
compare tuple nan | "GT" | "GT" | t_elm_FloatEdgeTest |
compare list nan | "GT" | "GT" | t_elm_FloatEdgeTest |
just nan == just nan | "False" | "False" | t_elm_FloatEdgeTest |
list nan /= list nan | "True" | "True" | t_elm_FloatEdgeTest |
truncate nan | "0" | "0" | t_elm_FloatEdgeTest |
pow 3 33 | "5559060566555523" | "5559060566555523" | t_elm_FloatEdgeTest |
/-- tests/elm/src/FloatEdgeTest.elm -/
def g_elm_FloatEdgeTest : Program := {
name := "FloatEdgeTest", source := "tests/elm/src", native := true,
exposing := (.items [.var "main"]), imports := [⟨"Html", "Html", some (.items [.var "text"])⟩],
types := [],
aliases := [],
defs := [
.fn "nan" [] (bin "/" (.atexp (.scon (.intF 0))) (.atexp (.scon (.intF 0)))),
.fn "inf" [] (bin "/" (.atexp (.scon (.intF 1))) (.atexp (.scon (.intF 0)))),
.fn "show" [] (qv "Debug" "toString"),
.fn "foldl" [pv "f", pv "acc", pv "xs"] (caseOf (v "xs") [(P (listP []), v "acc"), (.cons (P (pv "x")) (P (pv "rest")), ap (v "foldl") [v "f", .atexp (.paren (ap (v "f") [v "x", v "acc"])), v "rest"])]),
.fn "orderName" [pv "a", pv "b"] (caseOf (ap (v "compare") [v "a", v "b"]) [(P (pcA "LT"), str "LT"), (P (pcA "EQ"), str "EQ"), (P (pcA "GT"), str "GT")]),
.fn "main" [] (letDefs [.pat pw (ap (qv "Debug" "log") [str "round 2.5", .atexp (.paren (ap (v "show") [.atexp (.paren (ap (v "round") [.atexp (.scon (.float 25 (-1)))]))]))]), .pat pw (ap (qv "Debug" "log") [str "round -2.5", .atexp (.paren (ap (v "show") [.atexp (.paren (ap (v "round") [.atexp (neg (.scon (.float 25 (-1))))]))]))]), .pat pw (ap (qv "Debug" "log") [str "round 0.5", .atexp (.paren (ap (v "show") [.atexp (.paren (ap (v "round") [.atexp (.scon (.float 5 (-1)))]))]))]), .pat pw (ap (qv "Debug" "log") [str "round -0.5", .atexp (.paren (ap (v "show") [.atexp (.paren (ap (v "round") [.atexp (neg (.scon (.float 5 (-1))))]))]))]), .pat pw (ap (qv "Debug" "log") [str "round 1.5", .atexp (.paren (ap (v "show") [.atexp (.paren (ap (v "round") [.atexp (.scon (.float 15 (-1)))]))]))]), .pat pw (ap (qv "Debug" "log") [str "round -1.5", .atexp (.paren (ap (v "show") [.atexp (.paren (ap (v "round") [.atexp (neg (.scon (.float 15 (-1))))]))]))]), .pat pw (ap (qv "Debug" "log") [str "nan == nan", .atexp (.paren (ap (v "show") [.atexp (.paren (bin "==" (v "nan") (v "nan")))]))]), .pat pw (ap (qv "Debug" "log") [str "nan /= nan", .atexp (.paren (ap (v "show") [.atexp (.paren (bin "/=" (v "nan") (v "nan")))]))]), .pat pw (ap (qv "Debug" "log") [str "1 /= nan", .atexp (.paren (ap (v "show") [.atexp (.paren (bin "/=" (.atexp (.scon (.intF 1))) (v "nan")))]))]), .pat pw (ap (qv "Debug" "log") [str "min 1 nan is NaN", .atexp (.paren (ap (v "show") [.atexp (.paren (ap (v "isNaN") [.atexp (.paren (ap (v "min") [.atexp (.scon (.intF 1)), v "nan"]))]))]))]), .pat pw (ap (qv "Debug" "log") [str "min 1 nan", .atexp (.paren (ap (v "show") [.atexp (.paren (ap (v "min") [.atexp (.scon (.intF 1)), v "nan"]))]))]), .pat pw (ap (qv "Debug" "log") [str "compare nan 1", .atexp (.paren (ap (v "show") [.atexp (.paren (ap (v "compare") [v "nan", .atexp (.scon (.intF 1))]))]))]), .pat pw (ap (qv "Debug" "log") [str "foldl min is NaN", .atexp (.paren (ap (v "show") [.atexp (.paren (ap (v "isNaN") [.atexp (.paren (ap (v "foldl") [v "min", v "nan", listE [.atexp (.scon (.intF 1))]]))]))]))]), .pat pw (ap (qv "Debug" "log") [str "foldl min", .atexp (.paren (ap (v "show") [.atexp (.paren (ap (v "foldl") [v "min", v "nan", listE [.atexp (.scon (.intF 1))]]))]))]), .pat pw (ap (qv "Debug" "log") [str "map round", .atexp (.paren (ap (v "show") [listE [ap (v "round") [.atexp (neg (.scon (.float 25 (-1))))], ap (v "round") [.atexp (neg (.scon (.float 5 (-1))))], ap (v "round") [.atexp (.scon (.float 25 (-1)))]]]))]), .pat pw (ap (qv "Debug" "log") [str "case compare nan 1", .atexp (.paren (ap (v "orderName") [v "nan", .atexp (.scon (.intF 1))]))]), .pat pw (ap (qv "Debug" "log") [str "case compare 1 2", .atexp (.paren (ap (v "orderName") [.atexp (.scon (.intF 1)), .atexp (.scon (.intF 2))]))]), .pat pw (ap (qv "Debug" "log") [str "case compare 2 2", .atexp (.paren (ap (v "orderName") [.atexp (.scon (.intF 2)), .atexp (.scon (.intF 2))]))]), .pat pw (ap (qv "Debug" "log") [str "compare tuple nan", .atexp (.paren (ap (v "show") [.atexp (.paren (ap (v "compare") [tup2 (v "nan") (.atexp (.scon (.intF 1))), tup2 (.atexp (.scon (.intF 1))) (.atexp (.scon (.intF 1)))]))]))]), .pat pw (ap (qv "Debug" "log") [str "compare list nan", .atexp (.paren (ap (v "show") [.atexp (.paren (ap (v "compare") [listE [v "nan"], listE [.atexp (.scon (.intF 1))]]))]))]), .pat pw (ap (qv "Debug" "log") [str "just nan == just nan", .atexp (.paren (ap (v "show") [.atexp (.paren (bin "==" (ap (c "Just") [v "nan"]) (ap (c "Just") [v "nan"])))]))]), .pat pw (ap (qv "Debug" "log") [str "list nan /= list nan", .atexp (.paren (ap (v "show") [.atexp (.paren (bin "/=" (listE [v "nan"]) (listE [v "nan"])))]))]), .pat pw (ap (qv "Debug" "log") [str "truncate nan", .atexp (.paren (ap (v "show") [.atexp (.paren (ap (v "truncate") [v "nan"]))]))]), .pat pw (ap (qv "Debug" "log") [str "pow 3 33", .atexp (.paren (ap (v "show") [.atexp (.paren (bin "^" (.atexp (.scon (.intF 3))) (.atexp (.scon (.intF 33)))))]))])] (ap (v "text") [str "done"])) ],
checks := [
chk .check "round 2.5: \"3\"" .string,
chk .check "round -2.5: \"-2\"" .string,
chk .check "round 0.5: \"1\"" .string,
chk .check "round -0.5: \"0\"" .string,
chk .check "round 1.5: \"2\"" .string,
chk .check "round -1.5: \"-1\"" .string,
chk .check "nan == nan: \"False\"" .string,
chk .check "nan /= nan: \"True\"" .string,
chk .check "1 /= nan: \"True\"" .string,
chk .check "min 1 nan is NaN: \"True\"" .string,
chk .check "min 1 nan: \"NaN\"" .string,
chk .check "compare nan 1: \"GT\"" .string,
chk .check "foldl min is NaN: \"True\"" .string,
chk .check "foldl min: \"NaN\"" .string,
chk .check "map round: \"[-2,0,3]\"" .string,
chk .check "case compare nan 1: \"GT\"" .string,
chk .check "case compare 1 2: \"LT\"" .string,
chk .check "case compare 2 2: \"EQ\"" .string,
chk .check "compare tuple nan: \"GT\"" .string,
chk .check "compare list nan: \"GT\"" .string,
chk .check "just nan == just nan: \"False\"" .string,
chk .check "list nan /= list nan: \"True\"" .string,
chk .check "truncate nan: \"0\"" .string,
chk .check "pow 3 33: \"5559060566555523\"" .string ] }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_FloatEdgeTest 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_FloatEdgeTest 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_FloatEdgeTest states that the program has a meaning by r:p-program, with both premises, and ident_elm_FloatEdgeTest that its specialisation is unique and is the reduced program above, both with no hypothesis about principality. One of these theorems is proved by native_decide ([native]).