StandardA49ClampTest
Checks, in A Standard Definition of Elm?:basis Basics.clamp
Translated into Lean for the mechanisation of the book: see its Lean translation.
`clamp lo hi x`: at Float it is `min (max x lo) hi`, with the IEEE 754 `maximum` and `minimum` that propagate NaN and order `-0` below `+0`; at Int it is the text of elm/core. A Standard Definition of Elm?, ruling A49. These are the book's examples: at Float, `clamp 10 0 5` is `+0` (the bounds the wrong way round give `hi`), `clamp nan 1 0.5` is NaN, `clamp 0 1 -0` is `+0` and `clamp -1 -0 0` is `-0`; at Int, `clamp 10 0 5` is `10`, as elm/core's text gives. <https://elm-test-suite.netlify.app/standard/basis/Basics/clamp> The float results are tested without printing a float: a value is NaN when it is not equal to itself, and a zero's sign is the sign of `1` divided by it. The Int examples go through `iclamp`, annotated at `Int`: an integer constant that nothing fixes is `Float` (ruling A27). Elm 0.19.1 computes `clamp` at Float by elm/core's text too, so it gives `10`, `0.5`, `-0` and `0` for the four float examples.
Results
elm-0.19.1
failran in 51 msMissing pattern: clamp 10 0 5 at Float is +0: True --- output --- Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.1/optimize for better performance and smaller assets. clamp 10 0 5 at Float is +0: False clamp nan 1 0.5 is NaN: False clamp 0 1 -0 is +0: False clamp -1 -0 0 is -0: False clamp 10 0 5 at Int: 10 clamp 10 0 20 at Int: 0 clamp 0 10 -5 at Int: 0 clamp 0 10 15 at Int: 10 clamp 0 10 7 at Int: 7
elm-0.19.2
failran in 50 msMissing pattern: clamp 10 0 5 at Float is +0: True --- output --- Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.2/optimize for better performance and smaller assets. clamp 10 0 5 at Float is +0: False clamp nan 1 0.5 is NaN: False clamp 0 1 -0 is +0: False clamp -1 -0 0 is -0: False clamp 10 0 5 at Int: 10 clamp 10 0 20 at Int: 0 clamp 0 10 -5 at Int: 0 clamp 0 10 15 at Int: 10 clamp 0 10 7 at Int: 7
elm-0.19.3
failran in 44 msMissing pattern: clamp 10 0 5 at Float is +0: True --- output --- Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.3/optimize for better performance and smaller assets. clamp 10 0 5 at Float is +0: False clamp nan 1 0.5 is NaN: False clamp 0 1 -0 is +0: False clamp -1 -0 0 is -0: False clamp 10 0 5 at Int: 10 clamp 10 0 20 at Int: 0 clamp 0 10 -5 at Int: 0 clamp 0 10 15 at Int: 10 clamp 0 10 7 at Int: 7
lamdera-1.5.0
failran in 46 msMissing pattern: clamp 10 0 5 at Float is +0: True --- output --- clamp 10 0 5 at Float is +0: False clamp nan 1 0.5 is NaN: False clamp 0 1 -0 is +0: False clamp -1 -0 0 is -0: False clamp 10 0 5 at Int: 10 clamp 10 0 20 at Int: 0 clamp 0 10 -5 at Int: 0 clamp 0 10 15 at Int: 10 clamp 0 10 7 at Int: 7
Discussions
Discuss this test·Search the discussions on GitHub
Discussions are on GitHub; one about this test has [t:elm-core/StandardA49ClampTest] in its title.
Source
The -- CHECK: lines give the expected output.
module StandardA49ClampTest exposing (main)
{-| `clamp lo hi x`: at Float it is `min (max x lo) hi`, with the IEEE 754 `maximum` and `minimum`
that propagate NaN and order `-0` below `+0`; at Int it is the text of elm/core.
A Standard Definition of Elm?, ruling A49. These are the book's examples: at Float, `clamp 10 0 5`
is `+0` (the bounds the wrong way round give `hi`), `clamp nan 1 0.5` is NaN, `clamp 0 1 -0` is
`+0` and `clamp -1 -0 0` is `-0`; at Int, `clamp 10 0 5` is `10`, as elm/core's text gives.
<https://elm-test-suite.netlify.app/standard/basis/Basics/clamp>
The float results are tested without printing a float: a value is NaN when it is not equal to
itself, and a zero's sign is the sign of `1` divided by it. The Int examples go through `iclamp`,
annotated at `Int`: an integer constant that nothing fixes is `Float` (ruling A27).
Elm 0.19.1 computes `clamp` at Float by elm/core's text too, so it gives `10`, `0.5`, `-0` and `0`
for the four float examples.
-}
-- TAGS: elm19-bug
-- TAGS: elm-spec, b:Basics.clamp
-- CHECK: clamp 10 0 5 at Float is +0: True
-- CHECK: clamp nan 1 0.5 is NaN: True
-- CHECK: clamp 0 1 -0 is +0: True
-- CHECK: clamp -1 -0 0 is -0: True
-- CHECK: clamp 10 0 5 at Int: 10
-- CHECK: clamp 10 0 20 at Int: 0
-- CHECK: clamp 0 10 -5 at Int: 0
-- CHECK: clamp 0 10 15 at Int: 10
-- CHECK: clamp 0 10 7 at Int: 7
import Html
nan : Float
nan =
0 / 0
negZero : Float
negZero =
-0
isNotSelf : Float -> Bool
isNotSelf x =
x /= x
isPositiveZero : Float -> Bool
isPositiveZero x =
x == 0 && 1 / x > 0
iclamp : Int -> Int -> Int -> Int
iclamp lo hi x =
clamp lo hi x
isNegativeZero : Float -> Bool
isNegativeZero x =
x == 0 && 1 / x < 0
main =
let
_ =
Debug.log "clamp 10 0 5 at Float is +0" (isPositiveZero (clamp 10 0 5))
_ =
Debug.log "clamp nan 1 0.5 is NaN" (isNotSelf (clamp nan 1 0.5))
_ =
Debug.log "clamp 0 1 -0 is +0" (isPositiveZero (clamp 0 1 negZero))
_ =
Debug.log "clamp -1 -0 0 is -0" (isNegativeZero (clamp -1 negZero 0))
_ =
Debug.log "clamp 10 0 5 at Int" (iclamp 10 0 5)
_ =
Debug.log "clamp 10 0 20 at Int" (iclamp 10 0 20)
_ =
Debug.log "clamp 0 10 -5 at Int" (iclamp 0 10 -5)
_ =
Debug.log "clamp 0 10 15 at Int" (iclamp 0 10 15)
_ =
Debug.log "clamp 0 10 7 at Int" (iclamp 0 10 7)
in
Html.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_StandardA49ClampTest, in StandardElmMech/Generated/ElmCore_StandardA42.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 |
|---|---|---|---|
clamp 10 0 5 at Float is +0 | True | True | t_elm_core_StandardA49ClampTest |
clamp nan 1 0.5 is NaN | True | True | t_elm_core_StandardA49ClampTest |
clamp 0 1 -0 is +0 | True | True | t_elm_core_StandardA49ClampTest |
clamp -1 -0 0 is -0 | True | True | t_elm_core_StandardA49ClampTest |
clamp 10 0 5 at Int | 10 | 10 | t_elm_core_StandardA49ClampTest |
clamp 10 0 20 at Int | 0 | 0 | t_elm_core_StandardA49ClampTest |
clamp 0 10 -5 at Int | 0 | 0 | t_elm_core_StandardA49ClampTest |
clamp 0 10 15 at Int | 10 | 10 | t_elm_core_StandardA49ClampTest |
clamp 0 10 7 at Int | 7 | 7 | t_elm_core_StandardA49ClampTest |
/-- tests/elm-core/src/StandardA49ClampTest.elm -/
def g_elm_core_StandardA49ClampTest : Program := {
name := "StandardA49ClampTest", source := "tests/elm-core/src",
exposing := (.items [.var "main"]), imports := [⟨"Html", "Html", none⟩],
types := [],
aliases := [],
defs := [
.fn "nan" [] (bin "/" (.atexp (.scon (.intF 0))) (.atexp (.scon (.intF 0)))),
.fn "negZero" [] (.atexp (neg (.scon (.intF 0)))),
.fn "isNotSelf" [pv "x"] (bin "/=" (v "x") (v "x")),
.fn "isPositiveZero" [pv "x"] (andalso (bin "==" (v "x") (.atexp (.scon (.intF 0)))) (bin ">" (bin "/" (.atexp (.scon (.intF 1))) (v "x")) (.atexp (.scon (.intF 0))))),
.fn "iclamp" [pv "lo", pv "hi", pv "x"] (ap (v "clamp") [v "lo", v "hi", v "x"]),
.fn "isNegativeZero" [pv "x"] (andalso (bin "==" (v "x") (.atexp (.scon (.intF 0)))) (bin "<" (bin "/" (.atexp (.scon (.intF 1))) (v "x")) (.atexp (.scon (.intF 0))))),
.fn "main" [] (letDefs [.pat pw (ap (qv "Debug" "log") [str "clamp 10 0 5 at Float is +0", .atexp (.paren (ap (v "isPositiveZero") [.atexp (.paren (ap (v "clamp") [.atexp (.scon (.intF 10)), .atexp (.scon (.intF 0)), .atexp (.scon (.intF 5))]))]))]), .pat pw (ap (qv "Debug" "log") [str "clamp nan 1 0.5 is NaN", .atexp (.paren (ap (v "isNotSelf") [.atexp (.paren (ap (v "clamp") [v "nan", .atexp (.scon (.intF 1)), .atexp (.scon (.float 5 (-1)))]))]))]), .pat pw (ap (qv "Debug" "log") [str "clamp 0 1 -0 is +0", .atexp (.paren (ap (v "isPositiveZero") [.atexp (.paren (ap (v "clamp") [.atexp (.scon (.intF 0)), .atexp (.scon (.intF 1)), v "negZero"]))]))]), .pat pw (ap (qv "Debug" "log") [str "clamp -1 -0 0 is -0", .atexp (.paren (ap (v "isNegativeZero") [.atexp (.paren (ap (v "clamp") [.atexp (neg (.scon (.intF 1))), v "negZero", .atexp (.scon (.intF 0))]))]))]), .pat pw (ap (qv "Debug" "log") [str "clamp 10 0 5 at Int", .atexp (.paren (ap (v "iclamp") [i 10, i 0, i 5]))]), .pat pw (ap (qv "Debug" "log") [str "clamp 10 0 20 at Int", .atexp (.paren (ap (v "iclamp") [i 10, i 0, i 20]))]), .pat pw (ap (qv "Debug" "log") [str "clamp 0 10 -5 at Int", .atexp (.paren (ap (v "iclamp") [i 0, i 10, .atexp (neg (A (i 5)))]))]), .pat pw (ap (qv "Debug" "log") [str "clamp 0 10 15 at Int", .atexp (.paren (ap (v "iclamp") [i 0, i 10, i 15]))]), .pat pw (ap (qv "Debug" "log") [str "clamp 0 10 7 at Int", .atexp (.paren (ap (v "iclamp") [i 0, i 10, i 7]))])] (ap (qv "Html" "text") [str "done"])) ],
checks := [
chk .check "clamp 10 0 5 at Float is +0: True" .data,
chk .check "clamp nan 1 0.5 is NaN: True" .data,
chk .check "clamp 0 1 -0 is +0: True" .data,
chk .check "clamp -1 -0 0 is -0: True" .data,
chk .check "clamp 10 0 5 at Int: 10" .int,
chk .check "clamp 10 0 20 at Int: 0" .int,
chk .check "clamp 0 10 -5 at Int: 0" .int,
chk .check "clamp 0 10 15 at Int: 10" .int,
chk .check "clamp 0 10 7 at Int: 7" .int ] }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_StandardA49ClampTest 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_StandardA49ClampTest 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_StandardA49ClampTest states that the program has a meaning by r:p-program, with both premises, and ident_elm_core_StandardA49ClampTest that its specialisation is unique and is the reduced program above, both with no hypothesis about principality.