Elm Exactly
Results / elm-core /

UtilsOrderingCaptureCharTest

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.

elm-0.19.1 passelm-0.19.2 passelm-0.19.3 passlamdera-1.5.0 pass
`(<)`, `(<=)`, `(>)` and `(>=)` on `Char` give the right answers when
passed as function values or applied in prefix form.

On the list `['a','b','c','d','e']` with pivot `'c'`, 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 55 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.1/optimize for better performance and smaller assets.
lt_filter: ['a','b']
le_filter: ['a','b','c']
gt_filter: ['d','e']
ge_filter: ['c','d','e']
count_lt: 2
count_ge: 3

elm-0.19.2

passran in 37 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.2/optimize for better performance and smaller assets.
lt_filter: ['a','b']
le_filter: ['a','b','c']
gt_filter: ['d','e']
ge_filter: ['c','d','e']
count_lt: 2
count_ge: 3

elm-0.19.3

passran in 72 ms
Output
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: ['c','d','e']
gt_filter: ['d','e']
le_filter: ['a','b','c']
lt_filter: ['a','b']

lamdera-1.5.0

passran in 49 ms
Output
count_ge: 3
count_lt: 2
ge_filter: ['c','d','e']
gt_filter: ['d','e']
le_filter: ['a','b','c']
lt_filter: ['a','b']

Discussions

Discuss this test·Search the discussions on GitHub

Discussions are on GitHub; one about this test has [t:elm-core/UtilsOrderingCaptureCharTest] in its title.

Source

The -- CHECK: lines give the expected output.

module UtilsOrderingCaptureCharTest exposing (main)
{-| `(<)`, `(<=)`, `(>)` and `(>=)` on `Char` give the right answers when
passed as function values or applied in prefix form.
On the list `['a','b','c','d','e']` with pivot `'c'`, 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: ['a','b']
-- CHECK: le_filter: ['a','b','c']
-- CHECK: gt_filter: ['d','e']
-- CHECK: ge_filter: ['c','d','e']
-- 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 Char
xs =
[ 'a', 'b', 'c', 'd', 'e' ]
_ =
Debug.log "lt_filter" (filter (\x -> (<) x 'c') xs)
_ =
Debug.log "le_filter" (filter (\x -> (<=) x 'c') xs)
_ =
Debug.log "gt_filter" (filter (\x -> (>) x 'c') xs)
_ =
Debug.log "ge_filter" (filter (\x -> (>=) x 'c') xs)
_ =
Debug.log "count_lt" (countWith (<) 'c' xs)
_ =
Debug.log "count_ge" (countWith (>=) 'c' 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_UtilsOrderingCaptureCharTest, 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.

CheckExpected (CHECK)Lean evaluatorProved by
lt_filter['a','b']['a','b']t_elm_core_UtilsOrderingCaptureCharTest
le_filter['a','b','c']['a','b','c']t_elm_core_UtilsOrderingCaptureCharTest
gt_filter['d','e']['d','e']t_elm_core_UtilsOrderingCaptureCharTest
ge_filter['c','d','e']['c','d','e']t_elm_core_UtilsOrderingCaptureCharTest
count_lt22t_elm_core_UtilsOrderingCaptureCharTest
count_ge33t_elm_core_UtilsOrderingCaptureCharTest
/-- tests/elm-core/src/UtilsOrderingCaptureCharTest.elm -/
def g_elm_core_UtilsOrderingCaptureCharTest : Program := {
  name := "UtilsOrderingCaptureCharTest", 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 [.atexp (chA 'a'), .atexp (chA 'b'), .atexp (chA 'c'), .atexp (chA 'd'), .atexp (chA 'e')]), .pat pw (ap (qv "Debug" "log") [str "lt_filter", .atexp (.paren (ap (v "filter") [.atexp (.paren (lams [pv "x"] (ap (.atexp (.op "<")) [v "x", .atexp (chA 'c')]))), 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", .atexp (chA 'c')]))), 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", .atexp (chA 'c')]))), 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", .atexp (chA 'c')]))), v "xs"]))]), .pat pw (ap (qv "Debug" "log") [str "count_lt", .atexp (.paren (ap (v "countWith") [.atexp (.op "<"), .atexp (chA 'c'), v "xs"]))]), .pat pw (ap (qv "Debug" "log") [str "count_ge", .atexp (.paren (ap (v "countWith") [.atexp (.op ">="), .atexp (chA 'c'), v "xs"]))])] (ap (v "text") [str "done"])) ],
  checks := [
    chk .check "lt_filter: ['a','b']" .data,
    chk .check "le_filter: ['a','b','c']" .data,
    chk .check "gt_filter: ['d','e']" .data,
    chk .check "ge_filter: ['c','d','e']" .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_UtilsOrderingCaptureCharTest 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_UtilsOrderingCaptureCharTest 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_UtilsOrderingCaptureCharTest states that the program has a meaning by r:p-program, with both premises, and ident_elm_core_UtilsOrderingCaptureCharTest that its specialisation is unique and is the reduced program above, both with no hypothesis about principality.