UtilsOrderingCaptureIntTest
Tags:elm-spec
Checks, in A Standard Definition of Elm?:rule d-atexp-oprule d-app-basic-partialrule d-app-basic-apply
Translated into Lean for the mechanisation of the book: see its Lean translation.
`(<)`, `(<=)`, `(>)` and `(>=)` on `Int` give the right answers when passed as function values or applied in prefix form. On the list `[1, 2, 3, 4, 5]` with pivot `3`, each operator is applied in prefix form inside a lambda passed to the local helper `filter`, and `(<)` and `(>=)` are also passed to the generic helper `countWith`, counting 2 and 3 elements.
Results
elm-0.19.1
passran in 48 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.1/optimize for better performance and smaller assets. lt_filter: [1,2] le_filter: [1,2,3] gt_filter: [4,5] ge_filter: [3,4,5] count_lt: 2 count_ge: 3
elm-0.19.2
passran in 37 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.2/optimize for better performance and smaller assets. lt_filter: [1,2] le_filter: [1,2,3] gt_filter: [4,5] ge_filter: [3,4,5] count_lt: 2 count_ge: 3
elm-0.19.3
passran in 40 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.3/optimize for better performance and smaller assets. count_ge: 3 count_lt: 2 ge_filter: [3,4,5] gt_filter: [4,5] le_filter: [1,2,3] lt_filter: [1,2]
lamdera-1.5.0
passran in 52 msOutput
count_ge: 3 count_lt: 2 ge_filter: [3,4,5] gt_filter: [4,5] le_filter: [1,2,3] lt_filter: [1,2]
Discussions
Discuss this test·Search the discussions on GitHub
Discussions are on GitHub; one about this test has [t:elm-core/UtilsOrderingCaptureIntTest] in its title.
Source
The -- CHECK: lines give the expected output.
module UtilsOrderingCaptureIntTest exposing (main)
{-| `(<)`, `(<=)`, `(>)` and `(>=)` on `Int` give the right answers when
passed as function values or applied in prefix form.
On the list `[1, 2, 3, 4, 5]` with pivot `3`, each operator is applied in prefix
form inside a lambda passed to the local helper `filter`, and `(<)` and `(>=)`
are also passed to the generic helper `countWith`, counting 2 and 3 elements.
-}
-- TAGS: elm-spec, d-atexp-op, d-app-basic-partial, d-app-basic-apply
-- CHECK: lt_filter: [1,2]
-- CHECK: le_filter: [1,2,3]
-- CHECK: gt_filter: [4,5]
-- CHECK: ge_filter: [3,4,5]
-- CHECK: count_lt: 2
-- CHECK: count_ge: 3
import Html exposing (text)
{-| Counts the elements `x` of a list for which `pred x target` holds.
-}
countWith : (a -> a -> Bool) -> a -> List a -> Int
countWith pred target xs =
case xs of
[] ->
0
x :: rest ->
if pred x target then
1 + countWith pred target rest
else
countWith pred target rest
{-| Keeps the elements of a list for which `keep` holds, in order.
-}
filter : (a -> Bool) -> List a -> List a
filter keep xs =
case xs of
[] ->
[]
x :: rest ->
if keep x then
x :: filter keep rest
else
filter keep rest
main =
let
xs : List Int
xs =
[ 1, 2, 3, 4, 5 ]
_ =
Debug.log "lt_filter" (filter (\x -> (<) x 3) xs)
_ =
Debug.log "le_filter" (filter (\x -> (<=) x 3) xs)
_ =
Debug.log "gt_filter" (filter (\x -> (>) x 3) xs)
_ =
Debug.log "ge_filter" (filter (\x -> (>=) x 3) xs)
_ =
Debug.log "count_lt" (countWith (<) 3 xs)
_ =
Debug.log "count_ge" (countWith (>=) 3 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_UtilsOrderingCaptureIntTest, in StandardElmMech/Generated/ElmCore_TaskOnErrorS.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 |
|---|---|---|---|
lt_filter | [1,2] | [1,2] | t_elm_core_UtilsOrderingCaptureIntTest |
le_filter | [1,2,3] | [1,2,3] | t_elm_core_UtilsOrderingCaptureIntTest |
gt_filter | [4,5] | [4,5] | t_elm_core_UtilsOrderingCaptureIntTest |
ge_filter | [3,4,5] | [3,4,5] | t_elm_core_UtilsOrderingCaptureIntTest |
count_lt | 2 | 2 | t_elm_core_UtilsOrderingCaptureIntTest |
count_ge | 3 | 3 | t_elm_core_UtilsOrderingCaptureIntTest |
/-- tests/elm-core/src/UtilsOrderingCaptureIntTest.elm -/
def g_elm_core_UtilsOrderingCaptureIntTest : Program := {
name := "UtilsOrderingCaptureIntTest", source := "tests/elm-core/src",
exposing := (.items [.var "main"]), imports := [⟨"Html", "Html", some (.items [.var "text"])⟩],
types := [],
aliases := [],
defs := [
.fn "countWith" [pv "pred", pv "target", pv "xs"] (caseOf (v "xs") [(P (listP []), i 0), (.cons (P (pv "x")) (P (pv "rest")), ifte (ap (v "pred") [v "x", v "target"]) (bin "+" (i 1) (ap (v "countWith") [v "pred", v "target", v "rest"])) (ap (v "countWith") [v "pred", v "target", v "rest"]))]),
.fn "filter" [pv "keep", pv "xs"] (caseOf (v "xs") [(P (listP []), listE []), (.cons (P (pv "x")) (P (pv "rest")), ifte (ap (v "keep") [v "x"]) (bin "::" (v "x") (ap (v "filter") [v "keep", v "rest"])) (ap (v "filter") [v "keep", v "rest"]))]),
.fn "main" [] (letDefs [.fn "xs" [] (listE [i 1, i 2, i 3, i 4, i 5]), .pat pw (ap (qv "Debug" "log") [str "lt_filter", .atexp (.paren (ap (v "filter") [.atexp (.paren (lams [pv "x"] (ap (.atexp (.op "<")) [v "x", i 3]))), v "xs"]))]), .pat pw (ap (qv "Debug" "log") [str "le_filter", .atexp (.paren (ap (v "filter") [.atexp (.paren (lams [pv "x"] (ap (.atexp (.op "<=")) [v "x", i 3]))), v "xs"]))]), .pat pw (ap (qv "Debug" "log") [str "gt_filter", .atexp (.paren (ap (v "filter") [.atexp (.paren (lams [pv "x"] (ap (.atexp (.op ">")) [v "x", i 3]))), v "xs"]))]), .pat pw (ap (qv "Debug" "log") [str "ge_filter", .atexp (.paren (ap (v "filter") [.atexp (.paren (lams [pv "x"] (ap (.atexp (.op ">=")) [v "x", i 3]))), v "xs"]))]), .pat pw (ap (qv "Debug" "log") [str "count_lt", .atexp (.paren (ap (v "countWith") [.atexp (.op "<"), i 3, v "xs"]))]), .pat pw (ap (qv "Debug" "log") [str "count_ge", .atexp (.paren (ap (v "countWith") [.atexp (.op ">="), i 3, v "xs"]))])] (ap (v "text") [str "done"])) ],
checks := [
chk .check "lt_filter: [1,2]" .data,
chk .check "le_filter: [1,2,3]" .data,
chk .check "gt_filter: [4,5]" .data,
chk .check "ge_filter: [3,4,5]" .data,
chk .check "count_lt: 2" .int,
chk .check "count_ge: 3" .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_UtilsOrderingCaptureIntTest 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_UtilsOrderingCaptureIntTest 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_UtilsOrderingCaptureIntTest states that the program has a meaning by r:p-program, with both premises, and ident_elm_core_UtilsOrderingCaptureIntTest that its specialisation is unique and is the reduced program above, both with no hypothesis about principality.