Elm Exactly
Results / elm-core /

OrderingMixedCaptureTest

Tags:elm-spec

Checks, in A Standard Definition of Elm?:rule s-atexp-oprule 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
Tests `(<)` and `(>=)` passed as arguments to one polymorphic counting
helper, used at `Int`, `Float` and `Char` in the same program.

`countWith` counts the list elements `x` for which `pred x target` holds. Each
type uses a five-element list (`1` to `5`, `1.0` to `5.0`, `'a'` to `'e'`) and
a target fourth in that list, so `(<)` counts 3 and `(>=)` counts 2.

Results

elm-0.19.1

passran in 83 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.1/optimize for better performance and smaller assets.
int_lt_count: 3
int_ge_count: 2
float_lt_count: 3
float_ge_count: 2
char_lt_count: 3
char_ge_count: 2

elm-0.19.2

passran in 61 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.2/optimize for better performance and smaller assets.
int_lt_count: 3
int_ge_count: 2
float_lt_count: 3
float_ge_count: 2
char_lt_count: 3
char_ge_count: 2

elm-0.19.3

passran in 68 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.3/optimize for better performance and smaller assets.
int_lt_count: 3
int_ge_count: 2
float_lt_count: 3
float_ge_count: 2
char_lt_count: 3
char_ge_count: 2

lamdera-1.5.0

passran in 66 ms
Output
int_lt_count: 3
int_ge_count: 2
float_lt_count: 3
float_ge_count: 2
char_lt_count: 3
char_ge_count: 2

Discussions

Discuss this test·Search the discussions on GitHub

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

Source

The -- CHECK: lines give the expected output.

module OrderingMixedCaptureTest exposing (main)
{-| Tests `(<)` and `(>=)` passed as arguments to one polymorphic counting
helper, used at `Int`, `Float` and `Char` in the same program.
`countWith` counts the list elements `x` for which `pred x target` holds. Each
type uses a five-element list (`1` to `5`, `1.0` to `5.0`, `'a'` to `'e'`) and
a target fourth in that list, so `(<)` counts 3 and `(>=)` counts 2.
-}
-- TAGS: elm-spec, s-atexp-op, d-atexp-op, d-app-basic-partial, d-app-basic-apply
-- CHECK: int_lt_count: 3
-- CHECK: int_ge_count: 2
-- CHECK: float_lt_count: 3
-- CHECK: float_ge_count: 2
-- CHECK: char_lt_count: 3
-- CHECK: char_ge_count: 2
import Html exposing (text)
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
main =
let
ints : List Int
ints =
[ 1, 2, 3, 4, 5 ]
floats : List Float
floats =
[ 1.0, 2.0, 3.0, 4.0, 5.0 ]
chars : List Char
chars =
[ 'a', 'b', 'c', 'd', 'e' ]
_ =
Debug.log "int_lt_count" (countWith (<) 4 ints)
_ =
Debug.log "int_ge_count" (countWith (>=) 4 ints)
_ =
Debug.log "float_lt_count" (countWith (<) 4.0 floats)
_ =
Debug.log "float_ge_count" (countWith (>=) 4.0 floats)
_ =
Debug.log "char_lt_count" (countWith (<) 'd' chars)
_ =
Debug.log "char_ge_count" (countWith (>=) 'd' chars)
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_OrderingMixedCaptureTest, in StandardElmMech/Generated/ElmCore_NotEqualBoolP.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.

/-- tests/elm-core/src/OrderingMixedCaptureTest.elm -/
def g_elm_core_OrderingMixedCaptureTest : Program := {
  name := "OrderingMixedCaptureTest", 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 "main" [] (letDefs [.fn "ints" [] (listE [i 1, i 2, i 3, i 4, i 5]), .fn "floats" [] (listE [.atexp (.scon (.float 10 (-1))), .atexp (.scon (.float 20 (-1))), .atexp (.scon (.float 30 (-1))), .atexp (.scon (.float 40 (-1))), .atexp (.scon (.float 50 (-1)))]), .fn "chars" [] (listE [.atexp (chA 'a'), .atexp (chA 'b'), .atexp (chA 'c'), .atexp (chA 'd'), .atexp (chA 'e')]), .pat pw (ap (qv "Debug" "log") [str "int_lt_count", .atexp (.paren (ap (v "countWith") [.atexp (.op "<"), i 4, v "ints"]))]), .pat pw (ap (qv "Debug" "log") [str "int_ge_count", .atexp (.paren (ap (v "countWith") [.atexp (.op ">="), i 4, v "ints"]))]), .pat pw (ap (qv "Debug" "log") [str "float_lt_count", .atexp (.paren (ap (v "countWith") [.atexp (.op "<"), .atexp (.scon (.float 40 (-1))), v "floats"]))]), .pat pw (ap (qv "Debug" "log") [str "float_ge_count", .atexp (.paren (ap (v "countWith") [.atexp (.op ">="), .atexp (.scon (.float 40 (-1))), v "floats"]))]), .pat pw (ap (qv "Debug" "log") [str "char_lt_count", .atexp (.paren (ap (v "countWith") [.atexp (.op "<"), .atexp (chA 'd'), v "chars"]))]), .pat pw (ap (qv "Debug" "log") [str "char_ge_count", .atexp (.paren (ap (v "countWith") [.atexp (.op ">="), .atexp (chA 'd'), v "chars"]))])] (ap (v "text") [str "done"])) ],
  checks := [
    chk .check "int_lt_count: 3" .int,
    chk .check "int_ge_count: 2" .int,
    chk .check "float_lt_count: 3" .int,
    chk .check "float_ge_count: 2" .int,
    chk .check "char_lt_count: 3" .int,
    chk .check "char_ge_count: 2" .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_OrderingMixedCaptureTest 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_OrderingMixedCaptureTest 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_OrderingMixedCaptureTest states that the program has a meaning by r:p-program, with both premises, and ident_elm_core_OrderingMixedCaptureTest that its specialisation is unique and is the reduced program above, both with no hypothesis about principality.