Elm Exactly
Results / elm /

CaseTupleTest

Tags:elm-spec

Checks, in A Standard Definition of Elm?:rule d-atpat-tuplerule d-atpat-scon-failrule d-match-first

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
A case expression on a pair of `Int`s matches literal and wildcard patterns
in order, taking the first branch that fits.

The patterns are `(0, 0)`, `(0, _)`, `(_, 0)` and a wildcard, and each of the
four inputs reaches a different one.

Results

elm-0.19.1

passran in 66 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.1/optimize for better performance and smaller assets.
pair1: "both zero"
pair2: "x is zero"
pair3: "y is zero"
pair4: "neither zero"

elm-0.19.2

passran in 67 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.2/optimize for better performance and smaller assets.
pair1: "both zero"
pair2: "x is zero"
pair3: "y is zero"
pair4: "neither zero"

elm-0.19.3

passran in 61 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.3/optimize for better performance and smaller assets.
pair1: "both zero"
pair2: "x is zero"
pair3: "y is zero"
pair4: "neither zero"

lamdera-1.5.0

passran in 93 ms
Output
pair1: "both zero"
pair2: "x is zero"
pair3: "y is zero"
pair4: "neither zero"

Discussions

Discuss this test·Search the discussions on GitHub

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

Source

The -- CHECK: lines give the expected output.

module CaseTupleTest exposing (main)
{-| A case expression on a pair of `Int`s matches literal and wildcard patterns
in order, taking the first branch that fits.
The patterns are `(0, 0)`, `(0, _)`, `(_, 0)` and a wildcard, and each of the
four inputs reaches a different one.
-}
-- TAGS: elm-spec, d-atpat-tuple, d-atpat-scon-fail, d-match-first
-- CHECK: pair1: "both zero"
-- CHECK: pair2: "x is zero"
-- CHECK: pair3: "y is zero"
-- CHECK: pair4: "neither zero"
import Html exposing (text)
describePair pair =
case pair of
(0, 0) -> "both zero"
(0, _) -> "x is zero"
(_, 0) -> "y is zero"
_ -> "neither zero"
main =
let
_ = Debug.log "pair1" (describePair (0, 0))
_ = Debug.log "pair2" (describePair (0, 5))
_ = Debug.log "pair3" (describePair (3, 0))
_ = Debug.log "pair4" (describePair (1, 2))
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_CaseTupleTest, in StandardElmMech/Generated/Elm_CaseS.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
pair1"both zero""both zero"t_elm_CaseTupleTest
pair2"x is zero""x is zero"t_elm_CaseTupleTest
pair3"y is zero""y is zero"t_elm_CaseTupleTest
pair4"neither zero""neither zero"t_elm_CaseTupleTest
/-- tests/elm/src/CaseTupleTest.elm -/
def g_elm_CaseTupleTest : Program := {
  name := "CaseTupleTest", source := "tests/elm/src",
  exposing := (.items [.var "main"]), imports := [⟨"Html", "Html", some (.items [.var "text"])⟩],
  types := [],
  aliases := [],
  defs := [
    .fn "describePair" [pv "pair"] (caseOf (v "pair") [(P (ptup2 (P (pint 0)) (P (pint 0))), str "both zero"), (P (ptup2 (P (pint 0)) (P pw)), str "x is zero"), (P (ptup2 (P pw) (P (pint 0))), str "y is zero"), (P pw, str "neither zero")]),
    .fn "main" [] (letDefs [.pat pw (ap (qv "Debug" "log") [str "pair1", .atexp (.paren (ap (v "describePair") [tup2 (i 0) (i 0)]))]), .pat pw (ap (qv "Debug" "log") [str "pair2", .atexp (.paren (ap (v "describePair") [tup2 (i 0) (i 5)]))]), .pat pw (ap (qv "Debug" "log") [str "pair3", .atexp (.paren (ap (v "describePair") [tup2 (i 3) (i 0)]))]), .pat pw (ap (qv "Debug" "log") [str "pair4", .atexp (.paren (ap (v "describePair") [tup2 (i 1) (i 2)]))])] (ap (v "text") [str "done"])) ],
  checks := [
    chk .check "pair1: \"both zero\"" .string,
    chk .check "pair2: \"x is zero\"" .string,
    chk .check "pair3: \"y is zero\"" .string,
    chk .check "pair4: \"neither zero\"" .string ] }

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_CaseTupleTest 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_CaseTupleTest 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_CaseTupleTest states that the program has a meaning by r:p-program, with both premises, and ident_elm_CaseTupleTest that its specialisation is unique and is the reduced program above, both with no hypothesis about principality.