NestedCaseReturnTest
Tags:elm-spec
Checks, in A Standard Definition of Elm?:rule d-exp-caserule d-pat-conrule d-atpat-sconrule d-match-next
Translated into Lean for the mechanisation of the book: see its Lean translation.
A `case` on a custom type whose branches return the result of another `case` on a String, either inline or through a helper, returns the right string. `Primitive` names go through `describePrimitive`, whose fallback is `"other"`; `Compound` names are matched inline, with the fallback `"custom " ++ name`; `Unit` gives `"empty"`. The test checks two known primitive names, the inline `"List"` branch, an unknown primitive (`"Bytes"`) and `Unit`.
Results
elm-0.19.1
passran in 73 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.1/optimize for better performance and smaller assets. r1: "integer" r2: "text" r3: "custom List" r4: "other" r5: "empty"
elm-0.19.2
passran in 55 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.2/optimize for better performance and smaller assets. r1: "integer" r2: "text" r3: "custom List" r4: "other" r5: "empty"
elm-0.19.3
passran in 47 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.3/optimize for better performance and smaller assets. r1: "integer" r2: "text" r3: "custom List" r4: "other" r5: "empty"
lamdera-1.5.0
passran in 97 msOutput
r1: "integer" r2: "text" r3: "custom List" r4: "other" r5: "empty"
Discussions
Discuss this test·Search the discussions on GitHub
Discussions are on GitHub; one about this test has [t:elm/NestedCaseReturnTest] in its title.
Source
The -- CHECK: lines give the expected output.
module NestedCaseReturnTest exposing (main)
{-| A `case` on a custom type whose branches return the result of another `case` on a String, either inline or through a helper, returns the right string.
`Primitive` names go through `describePrimitive`, whose fallback is `"other"`;
`Compound` names are matched inline, with the fallback `"custom " ++ name`;
`Unit` gives `"empty"`. The test checks two known primitive names, the inline
`"List"` branch, an unknown primitive (`"Bytes"`) and `Unit`.
-}
-- TAGS: elm-spec, d-exp-case, d-pat-con, d-atpat-scon, d-match-next
-- CHECK: r1: "integer"
-- CHECK: r2: "text"
-- CHECK: r3: "custom List"
-- CHECK: r4: "other"
-- CHECK: r5: "empty"
import Html exposing (text)
type TypeRepr
= Primitive String
| Compound String (List TypeRepr)
| Unit
describePrimitive : String -> String
describePrimitive name =
case name of
"Int" ->
"integer"
"Float" ->
"decimal"
"Bool" ->
"flag"
"String" ->
"text"
_ ->
"other"
describeType : TypeRepr -> String
describeType tipe =
case tipe of
Primitive name ->
describePrimitive name
Compound name _ ->
case name of
"List" ->
"custom List"
"Dict" ->
"custom Dict"
"Set" ->
"custom Set"
_ ->
"custom " ++ name
Unit ->
"empty"
main =
let
_ = Debug.log "r1" (describeType (Primitive "Int"))
_ = Debug.log "r2" (describeType (Primitive "String"))
_ = Debug.log "r3" (describeType (Compound "List" []))
_ = Debug.log "r4" (describeType (Primitive "Bytes"))
_ = Debug.log "r5" (describeType Unit)
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_NestedCaseReturnTest, in StandardElmMech/Generated/Elm_M.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 | "integer" | "integer" | t_elm_NestedCaseReturnTest |
r2 | "text" | "text" | t_elm_NestedCaseReturnTest |
r3 | "custom List" | "custom List" | t_elm_NestedCaseReturnTest |
r4 | "other" | "other" | t_elm_NestedCaseReturnTest |
r5 | "empty" | "empty" | t_elm_NestedCaseReturnTest |
/-- tests/elm/src/NestedCaseReturnTest.elm -/
def g_elm_NestedCaseReturnTest : Program := {
name := "NestedCaseReturnTest", source := "tests/elm/src",
exposing := (.items [.var "main"]), imports := [⟨"Html", "Html", some (.items [.var "text"])⟩],
types := [("TypeRepr", [("Primitive", 1), ("Compound", 2), ("Unit", 0)])],
aliases := [],
defs := [
.fn "describePrimitive" [pv "name"] (caseOf (v "name") [(P (.scon (.str "Int")), str "integer"), (P (.scon (.str "Float")), str "decimal"), (P (.scon (.str "Bool")), str "flag"), (P (.scon (.str "String")), str "text"), (P pw, str "other")]),
.fn "describeType" [pv "tipe"] (caseOf (v "tipe") [(pcon "Primitive" [pv "name"], ap (v "describePrimitive") [v "name"]), (pcon "Compound" [pv "name", pw], caseOf (v "name") [(P (.scon (.str "List")), str "custom List"), (P (.scon (.str "Dict")), str "custom Dict"), (P (.scon (.str "Set")), str "custom Set"), (P pw, bin "++" (str "custom ") (v "name"))]), (P (pcA "Unit"), str "empty")]),
.fn "main" [] (letDefs [.pat pw (ap (qv "Debug" "log") [str "r1", .atexp (.paren (ap (v "describeType") [.atexp (.paren (ap (c "Primitive") [str "Int"]))]))]), .pat pw (ap (qv "Debug" "log") [str "r2", .atexp (.paren (ap (v "describeType") [.atexp (.paren (ap (c "Primitive") [str "String"]))]))]), .pat pw (ap (qv "Debug" "log") [str "r3", .atexp (.paren (ap (v "describeType") [.atexp (.paren (ap (c "Compound") [str "List", listE []]))]))]), .pat pw (ap (qv "Debug" "log") [str "r4", .atexp (.paren (ap (v "describeType") [.atexp (.paren (ap (c "Primitive") [str "Bytes"]))]))]), .pat pw (ap (qv "Debug" "log") [str "r5", .atexp (.paren (ap (v "describeType") [c "Unit"]))])] (ap (v "text") [str "done"])) ],
checks := [
chk .check "r1: \"integer\"" .string,
chk .check "r2: \"text\"" .string,
chk .check "r3: \"custom List\"" .string,
chk .check "r4: \"other\"" .string,
chk .check "r5: \"empty\"" .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_NestedCaseReturnTest 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_NestedCaseReturnTest 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_NestedCaseReturnTest states that the program has a meaning by r:p-program, with both premises, and ident_elm_NestedCaseReturnTest that its specialisation is unique and is the reduced program above, both with no hypothesis about principality.