CompareFloatCaptureTest
Tags:elm-spec
Checks, in A Standard Definition of Elm?:rule d-app-basic-partialrule d-app-basic-apply
Translated into Lean for the mechanisation of the book: see its Lean translation.
`compare` on `Float` passed as a function value to a local insertion sort, `sortWith`. The list is sorted once with `compare` itself and once with a lambda that swaps its arguments, which sorts it in descending order.
Results
elm-0.19.1
passran in 56 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.1/optimize for better performance and smaller assets. sorted: [1.5,2,2.75,3,4.25] reverse_sorted: [4.25,3,2.75,2,1.5]
elm-0.19.2
passran in 66 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.2/optimize for better performance and smaller assets. sorted: [1.5,2,2.75,3,4.25] reverse_sorted: [4.25,3,2.75,2,1.5]
elm-0.19.3
passran in 59 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.3/optimize for better performance and smaller assets. reverse_sorted: [4.25,3,2.75,2,1.5] sorted: [1.5,2,2.75,3,4.25]
lamdera-1.5.0
passran in 77 msOutput
reverse_sorted: [4.25,3,2.75,2,1.5] sorted: [1.5,2,2.75,3,4.25]
Discussions
Discuss this test·Search the discussions on GitHub
Discussions are on GitHub; one about this test has [t:elm-core/CompareFloatCaptureTest] in its title.
Source
The -- CHECK: lines give the expected output.
module CompareFloatCaptureTest exposing (main)
{-| `compare` on `Float` passed as a function value to a local insertion sort,
`sortWith`.
The list is sorted once with `compare` itself and once with a lambda that
swaps its arguments, which sorts it in descending order.
-}
-- TAGS: elm-spec, d-app-basic-partial, d-app-basic-apply
-- CHECK: sorted: [1.5,2,2.75,3,4.25]
-- CHECK: reverse_sorted: [4.25,3,2.75,2,1.5]
import Html exposing (text)
{-| Sorts a list by insertion, ordering its elements with `cmp`.
-}
sortWith : (a -> a -> Order) -> List a -> List a
sortWith cmp xs =
case xs of
[] ->
[]
x :: rest ->
insertWith cmp x (sortWith cmp rest)
insertWith : (a -> a -> Order) -> a -> List a -> List a
insertWith cmp x ys =
case ys of
[] ->
[ x ]
y :: rest ->
case cmp x y of
GT ->
y :: insertWith cmp x rest
_ ->
x :: ys
main =
let
xs : List Float
xs =
[ 3.0, 1.5, 4.25, 2.75, 2.0 ]
_ =
Debug.log "sorted" (sortWith compare xs)
_ =
Debug.log "reverse_sorted" (sortWith (\a b -> compare b a) xs)
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_CompareFloatCaptureTest, in StandardElmMech/Generated/ElmCore.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 |
|---|---|---|---|
sorted | [1.5,2,2.75,3,4.25] | [1.5,2,2.75,3,4.25] | t_elm_core_CompareFloatCaptureTest |
reverse_sorted | [4.25,3,2.75,2,1.5] | [4.25,3,2.75,2,1.5] | t_elm_core_CompareFloatCaptureTest |
/-- tests/elm-core/src/CompareFloatCaptureTest.elm -/
def g_elm_core_CompareFloatCaptureTest : Program := {
name := "CompareFloatCaptureTest", source := "tests/elm-core/src",
exposing := (.items [.var "main"]), imports := [⟨"Html", "Html", some (.items [.var "text"])⟩],
types := [],
aliases := [],
defs := [
.fn "insertWith" [pv "cmp", pv "x", pv "ys"] (caseOf (v "ys") [(P (listP []), listE [v "x"]), (.cons (P (pv "y")) (P (pv "rest")), caseOf (ap (v "cmp") [v "x", v "y"]) [(P (pcA "GT"), bin "::" (v "y") (ap (v "insertWith") [v "cmp", v "x", v "rest"])), (P pw, bin "::" (v "x") (v "ys"))])]),
.fn "sortWith" [pv "cmp", pv "xs"] (caseOf (v "xs") [(P (listP []), listE []), (.cons (P (pv "x")) (P (pv "rest")), ap (v "insertWith") [v "cmp", v "x", .atexp (.paren (ap (v "sortWith") [v "cmp", v "rest"]))])]),
.fn "main" [] (letDefs [.fn "xs" [] (listE [.atexp (.scon (.float 30 (-1))), .atexp (.scon (.float 15 (-1))), .atexp (.scon (.float 425 (-2))), .atexp (.scon (.float 275 (-2))), .atexp (.scon (.float 20 (-1)))]), .pat pw (ap (qv "Debug" "log") [str "sorted", .atexp (.paren (ap (v "sortWith") [v "compare", v "xs"]))]), .pat pw (ap (qv "Debug" "log") [str "reverse_sorted", .atexp (.paren (ap (v "sortWith") [.atexp (.paren (lams [pv "a", pv "b"] (ap (v "compare") [v "b", v "a"]))), v "xs"]))])] (ap (v "text") [str "done"])) ],
checks := [
chk .check "sorted: [1.5,2,2.75,3,4.25]" .data,
chk .check "reverse_sorted: [4.25,3,2.75,2,1.5]" .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_CompareFloatCaptureTest 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_CompareFloatCaptureTest 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_CompareFloatCaptureTest states that the program has a meaning by r:p-program, with both premises, and ident_elm_core_CompareFloatCaptureTest that its specialisation is unique and is the reduced program above, both with no hypothesis about principality.