LargeDispatchCaseTest
Tags:elm-spec
Checks, in A Standard Definition of Elm?:rule d-pat-consrule d-pat-conrule d-atpat-sconrule d-match-next
Translated into Lean for the mechanisation of the book: see its Lean translation.
Checks a `case` on a list of custom-type values whose branches contain a nested `case` on a String. `dispatch` matches a one-element and a two-element list of `VInt`, then chooses an operation by name. The checks take the `"add"`, `"sub"` and `"negate"` branches and the outer default branch, reached with an empty list. The other names, the inner default branches and the `VStr` and `VNone` constructors are not used.
Results
elm-0.19.1
passran in 74 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.1/optimize for better performance and smaller assets. r1: 3 r2: -1 r3: -5 r4: 42
elm-0.19.2
passran in 45 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.2/optimize for better performance and smaller assets. r1: 3 r2: -1 r3: -5 r4: 42
elm-0.19.3
passran in 75 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.3/optimize for better performance and smaller assets. r1: 3 r2: -1 r3: -5 r4: 42
lamdera-1.5.0
passran in 60 msOutput
r1: 3 r2: -1 r3: -5 r4: 42
Discussions
Discuss this test·Search the discussions on GitHub
Discussions are on GitHub; one about this test has [t:elm/LargeDispatchCaseTest] in its title.
Source
The -- CHECK: lines give the expected output.
module LargeDispatchCaseTest exposing (main)
{-| Checks a `case` on a list of custom-type values whose branches contain a nested `case` on a String.
`dispatch` matches a one-element and a two-element list of `VInt`, then
chooses an operation by name. The checks take the `"add"`, `"sub"` and
`"negate"` branches and the outer default branch, reached with an empty list.
The other names, the inner default branches and the `VStr` and `VNone`
constructors are not used.
-}
-- TAGS: elm-spec, d-pat-cons, d-pat-con, d-atpat-scon, d-match-next
-- CHECK: r1: 3
-- CHECK: r2: -1
-- CHECK: r3: -5
-- CHECK: r4: 42
import Html exposing (text)
type Value
= VInt Int
| VStr String
| VNone
dispatch : List Value -> String -> Int
dispatch args name =
case args of
[ VInt a ] ->
case name of
"negate" ->
negate a
"abs" ->
abs a
_ ->
0
[ VInt a, VInt b ] ->
case name of
"add" ->
a + b
"sub" ->
a - b
"mul" ->
a * b
"max" ->
max a b
_ ->
0
_ ->
42
main =
let
_ = Debug.log "r1" (dispatch [ VInt 1, VInt 2 ] "add")
_ = Debug.log "r2" (dispatch [ VInt 1, VInt 2 ] "sub")
_ = Debug.log "r3" (dispatch [ VInt 5 ] "negate")
_ = Debug.log "r4" (dispatch [] "whatever")
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_LargeDispatchCaseTest, in StandardElmMech/Generated/Elm_InfixPrecO.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 |
|---|---|---|---|
r1 | 3 | 3 | t_elm_LargeDispatchCaseTest |
r2 | -1 | -1 | t_elm_LargeDispatchCaseTest |
r3 | -5 | -5 | t_elm_LargeDispatchCaseTest |
r4 | 42 | 42 | t_elm_LargeDispatchCaseTest |
/-- tests/elm/src/LargeDispatchCaseTest.elm -/
def g_elm_LargeDispatchCaseTest : Program := {
name := "LargeDispatchCaseTest", source := "tests/elm/src",
exposing := (.items [.var "main"]), imports := [⟨"Html", "Html", some (.items [.var "text"])⟩],
types := [("Value", [("VInt", 1), ("VStr", 1), ("VNone", 0)])],
aliases := [],
defs := [
.fn "dispatch" [pv "args", pv "name"] (caseOf (v "args") [(P (listP [pcon "VInt" [pv "a"]]), caseOf (v "name") [(P (.scon (.str "negate")), ap (v "negate") [v "a"]), (P (.scon (.str "abs")), ap (v "abs$Int") [v "a"]), (P pw, i 0)]), (P (listP [pcon "VInt" [pv "a"], pcon "VInt" [pv "b"]]), caseOf (v "name") [(P (.scon (.str "add")), bin "+" (v "a") (v "b")), (P (.scon (.str "sub")), bin "-" (v "a") (v "b")), (P (.scon (.str "mul")), bin "*" (v "a") (v "b")), (P (.scon (.str "max")), ap (v "max") [v "a", v "b"]), (P pw, i 0)]), (P pw, i 42)]),
.fn "main" [] (letDefs [.pat pw (ap (qv "Debug" "log") [str "r1", .atexp (.paren (ap (v "dispatch") [listE [ap (c "VInt") [i 1], ap (c "VInt") [i 2]], str "add"]))]), .pat pw (ap (qv "Debug" "log") [str "r2", .atexp (.paren (ap (v "dispatch") [listE [ap (c "VInt") [i 1], ap (c "VInt") [i 2]], str "sub"]))]), .pat pw (ap (qv "Debug" "log") [str "r3", .atexp (.paren (ap (v "dispatch") [listE [ap (c "VInt") [i 5]], str "negate"]))]), .pat pw (ap (qv "Debug" "log") [str "r4", .atexp (.paren (ap (v "dispatch") [listE [], str "whatever"]))])] (ap (v "text") [str "done"])) ],
checks := [
chk .check "r1: 3" .int,
chk .check "r2: -1" .int,
chk .check "r3: -5" .int,
chk .check "r4: 42" .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_LargeDispatchCaseTest 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_LargeDispatchCaseTest 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_LargeDispatchCaseTest states that the program has a meaning by r:p-program, with both premises, and ident_elm_LargeDispatchCaseTest that its specialisation is unique and is the reduced program above, both with no hypothesis about principality.