TailRecCaseMultiBranchTypesTest
Tags:elm-spec
Checks, in A Standard Definition of Elm?:rule d-pat-consrule d-pat-conrule d-pat-con-failrule d-atpat-scon
Translated into Lean for the mechanisation of the book: see its Lean translation.
A self tail-recursive search whose outer `case` has one branch per constructor of a custom type at the head of a list, with a nested `case` on a `String` inside one branch, finds the right element. `findEndianness` walks a list of `Expr`, counting positions from `idx`, and stops at the first `Var "LE"` or `Var "BE"`; every other element is skipped by a tail call. In `[ Lit 1, Call "foo" [], Var "LE", Var "other" ]` that is position 2, returned as `FoundLE 2`.
Results
elm-0.19.1
passran in 39 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.1/optimize for better performance and smaller assets. result: FoundLE 2
elm-0.19.2
passran in 67 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.2/optimize for better performance and smaller assets. result: FoundLE 2
elm-0.19.3
passran in 44 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.3/optimize for better performance and smaller assets. result: FoundLE 2
lamdera-1.5.0
passran in 46 msOutput
result: FoundLE 2
Discussions
Discuss this test·Search the discussions on GitHub
Discussions are on GitHub; one about this test has [t:elm/TailRecCaseMultiBranchTypesTest] in its title.
Source
The -- CHECK: lines give the expected output.
module TailRecCaseMultiBranchTypesTest exposing (main)
{-| A self tail-recursive search whose outer `case` has one branch per constructor of a
custom type at the head of a list, with a nested `case` on a `String` inside one branch,
finds the right element.
`findEndianness` walks a list of `Expr`, counting positions from `idx`, and stops at the
first `Var "LE"` or `Var "BE"`; every other element is skipped by a tail call. In
`[ Lit 1, Call "foo" [], Var "LE", Var "other" ]` that is position 2, returned as
`FoundLE 2`.
-}
-- TAGS: elm-spec, d-pat-cons, d-pat-con, d-pat-con-fail, d-atpat-scon
-- CHECK: result: FoundLE 2
import Html exposing (text)
type Expr
= Var String
| Call String (List Expr)
| Lit Int
type Found
= FoundLE Int
| FoundBE Int
| NotFound
findEndianness : List Expr -> Int -> Found
findEndianness exprs idx =
case exprs of
[] ->
NotFound
(Var name) :: rest ->
case name of
"LE" ->
FoundLE idx
"BE" ->
FoundBE idx
_ ->
findEndianness rest (idx + 1)
(Call _ _) :: rest ->
findEndianness rest (idx + 1)
(Lit _) :: rest ->
findEndianness rest (idx + 1)
main =
let
exprs =
[ Lit 1, Call "foo" [], Var "LE", Var "other" ]
_ = Debug.log "result" (findEndianness exprs 0)
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_TailRecCaseMultiBranchTypesTest, in StandardElmMech/Generated/Elm_StandardL.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 |
|---|---|---|---|
result | FoundLE 2 | FoundLE 2 | t_elm_TailRecCaseMultiBranchTypesTest |
/-- tests/elm/src/TailRecCaseMultiBranchTypesTest.elm -/
def g_elm_TailRecCaseMultiBranchTypesTest : Program := {
name := "TailRecCaseMultiBranchTypesTest", source := "tests/elm/src",
exposing := (.items [.var "main"]), imports := [⟨"Html", "Html", some (.items [.var "text"])⟩],
types := [("Expr", [("Var", 1), ("Call", 2), ("Lit", 1)]), ("Found", [("FoundLE", 1), ("FoundBE", 1), ("NotFound", 0)])],
aliases := [],
defs := [
.fn "findEndianness" [pv "exprs", pv "idx"] (caseOf (v "exprs") [(P (listP []), c "NotFound"), (.cons (P (.paren (pcon "Var" [pv "name"]))) (P (pv "rest")), caseOf (v "name") [(P (.scon (.str "LE")), ap (c "FoundLE") [v "idx"]), (P (.scon (.str "BE")), ap (c "FoundBE") [v "idx"]), (P pw, ap (v "findEndianness") [v "rest", .atexp (.paren (bin "+" (v "idx") (i 1)))])]), (.cons (P (.paren (pcon "Call" [pw, pw]))) (P (pv "rest")), ap (v "findEndianness") [v "rest", .atexp (.paren (bin "+" (v "idx") (i 1)))]), (.cons (P (.paren (pcon "Lit" [pw]))) (P (pv "rest")), ap (v "findEndianness") [v "rest", .atexp (.paren (bin "+" (v "idx") (i 1)))])]),
.fn "main" [] (letDefs [.fn "exprs" [] (listE [ap (c "Lit") [i 1], ap (c "Call") [str "foo", listE []], ap (c "Var") [str "LE"], ap (c "Var") [str "other"]]), .pat pw (ap (qv "Debug" "log") [str "result", .atexp (.paren (ap (v "findEndianness") [v "exprs", i 0]))])] (ap (v "text") [str "done"])) ],
checks := [
chk .check "result: FoundLE 2" .data ] }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_TailRecCaseMultiBranchTypesTest 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_TailRecCaseMultiBranchTypesTest 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_TailRecCaseMultiBranchTypesTest states that the program has a meaning by r:p-program, with both premises, and ident_elm_TailRecCaseMultiBranchTypesTest that its specialisation is unique and is the reduced program above, both with no hypothesis about principality.