UtilsEqualityCaptureIntTest
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 same answers when passed as function values as when applied directly. On the list `[1, 2, 3, 5, 5]` with target `5`, the operators are passed to the generic helper `countWith`, giving counts of 2 and 3, and applied in prefix form inside lambdas passed to the local helper `filter`, keeping the two matches and the other three elements.
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. eq_count: 2 ne_count: 3 eq_filter: [5,5] ne_filter: [1,2,3]
elm-0.19.2
passran in 70 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.2/optimize for better performance and smaller assets. eq_count: 2 ne_count: 3 eq_filter: [5,5] ne_filter: [1,2,3]
elm-0.19.3
passran in 51 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.3/optimize for better performance and smaller assets. ne_filter: [1,2,3] eq_filter: [5,5] ne_count: 3 eq_count: 2
lamdera-1.5.0
passran in 48 msOutput
ne_filter: [1,2,3] eq_filter: [5,5] ne_count: 3 eq_count: 2
Discussions
Discuss this test·Search the discussions on GitHub
Discussions are on GitHub; one about this test has [t:elm-core/UtilsEqualityCaptureIntTest] in its title.
Source
The -- CHECK: lines give the expected output.
module UtilsEqualityCaptureIntTest exposing (main)
{-| `(==)` and `(/=)` on `Int` give the same answers when passed as function
values as when applied directly.
On the list `[1, 2, 3, 5, 5]` with target `5`, the operators are passed to the
generic helper `countWith`, giving counts of 2 and 3, and applied in prefix
form inside lambdas passed to the local helper `filter`, keeping the two
matches and the other three elements.
-}
-- TAGS: elm-spec, d-atexp-op, d-app-basic-partial, d-app-basic-apply
-- CHECK: eq_count: 2
-- CHECK: ne_count: 3
-- CHECK: eq_filter: [5,5]
-- CHECK: ne_filter: [1,2,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, 5, 5 ]
_ =
Debug.log "eq_count" (countWith (==) 5 xs)
_ =
Debug.log "ne_count" (countWith (/=) 5 xs)
_ =
Debug.log "eq_filter" (filter (\x -> (==) x 5) xs)
_ =
Debug.log "ne_filter" (filter (\x -> (/=) x 5) 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_UtilsEqualityCaptureIntTest, 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 |
|---|---|---|---|
eq_count | 2 | 2 | t_elm_core_UtilsEqualityCaptureIntTest |
ne_count | 3 | 3 | t_elm_core_UtilsEqualityCaptureIntTest |
eq_filter | [5,5] | [5,5] | t_elm_core_UtilsEqualityCaptureIntTest |
ne_filter | [1,2,3] | [1,2,3] | t_elm_core_UtilsEqualityCaptureIntTest |
/-- tests/elm-core/src/UtilsEqualityCaptureIntTest.elm -/
def g_elm_core_UtilsEqualityCaptureIntTest : Program := {
name := "UtilsEqualityCaptureIntTest", 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 5, i 5]), .pat pw (ap (qv "Debug" "log") [str "eq_count", .atexp (.paren (ap (v "countWith") [.atexp (.op "=="), i 5, v "xs"]))]), .pat pw (ap (qv "Debug" "log") [str "ne_count", .atexp (.paren (ap (v "countWith") [.atexp (.op "/="), i 5, v "xs"]))]), .pat pw (ap (qv "Debug" "log") [str "eq_filter", .atexp (.paren (ap (v "filter") [.atexp (.paren (lams [pv "x"] (ap (.atexp (.op "==")) [v "x", i 5]))), v "xs"]))]), .pat pw (ap (qv "Debug" "log") [str "ne_filter", .atexp (.paren (ap (v "filter") [.atexp (.paren (lams [pv "x"] (ap (.atexp (.op "/=")) [v "x", i 5]))), v "xs"]))])] (ap (v "text") [str "done"])) ],
checks := [
chk .check "eq_count: 2" .int,
chk .check "ne_count: 3" .int,
chk .check "eq_filter: [5,5]" .data,
chk .check "ne_filter: [1,2,3]" .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_UtilsEqualityCaptureIntTest 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_UtilsEqualityCaptureIntTest 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_UtilsEqualityCaptureIntTest states that the program has a meaning by r:p-program, with both premises, and ident_elm_core_UtilsEqualityCaptureIntTest that its specialisation is unique and is the reduced program above, both with no hypothesis about principality.