TailRecBoolCarryTest
Tags:elm-spec
Checks, in A Standard Definition of Elm?:rule d-pat-consrule d-atpat-nilrule d-match-next
Translated into Lean for the mechanisation of the book: see its Lean translation.
A self tail-recursive function that carries a `Bool` argument from one call to the next, and sets it from a branch inside a `case`, returns the right result. `searchList found acc list` adds every element greater than 5 to `acc` and sets `found` to `True` when it meets one. At the end of the list it returns `acc` if `found`, and 0 otherwise. So `[1, 2, 3, 10]` gives 10, `[1, 2, 3]` gives 0, and `[1, 42]` gives 42.
Results
elm-0.19.1
passran in 61 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.1/optimize for better performance and smaller assets. result1: 10 result2: 0 result3: 42
elm-0.19.2
passran in 44 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.2/optimize for better performance and smaller assets. result1: 10 result2: 0 result3: 42
elm-0.19.3
passran in 77 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.3/optimize for better performance and smaller assets. result1: 10 result2: 0 result3: 42
lamdera-1.5.0
passran in 60 msOutput
result1: 10 result2: 0 result3: 42
Discussions
Discuss this test·Search the discussions on GitHub
Discussions are on GitHub; one about this test has [t:elm/TailRecBoolCarryTest] in its title.
Source
The -- CHECK: lines give the expected output.
module TailRecBoolCarryTest exposing (main)
{-| A self tail-recursive function that carries a `Bool` argument from one call to the
next, and sets it from a branch inside a `case`, returns the right result.
`searchList found acc list` adds every element greater than 5 to `acc` and sets `found` to
`True` when it meets one. At the end of the list it returns `acc` if `found`, and 0
otherwise. So `[1, 2, 3, 10]` gives 10, `[1, 2, 3]` gives 0, and `[1, 42]` gives 42.
-}
-- TAGS: elm-spec, d-pat-cons, d-atpat-nil, d-match-next
-- CHECK: result1: 10
-- CHECK: result2: 0
-- CHECK: result3: 42
import Html exposing (text)
{-| Returns the sum of the elements of `list` greater than 5, added to `acc`, or 0
if neither `found` was already `True` nor any such element exists.
-}
searchList : Bool -> Int -> List Int -> Int
searchList found acc list =
case list of
[] ->
if found then
acc
else
0
x :: xs ->
if x > 5 then
searchList True (acc + x) xs
else
searchList found acc xs
main =
let
_ = Debug.log "result1" (searchList False 0 [1, 2, 3, 10])
_ = Debug.log "result2" (searchList False 0 [1, 2, 3])
_ = Debug.log "result3" (searchList False 0 [1, 42])
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_TailRecBoolCarryTest, 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 |
|---|---|---|---|
result1 | 10 | 10 | t_elm_TailRecBoolCarryTest |
result2 | 0 | 0 | t_elm_TailRecBoolCarryTest |
result3 | 42 | 42 | t_elm_TailRecBoolCarryTest |
/-- tests/elm/src/TailRecBoolCarryTest.elm -/
def g_elm_TailRecBoolCarryTest : Program := {
name := "TailRecBoolCarryTest", source := "tests/elm/src",
exposing := (.items [.var "main"]), imports := [⟨"Html", "Html", some (.items [.var "text"])⟩],
types := [],
aliases := [],
defs := [
.fn "searchList" [pv "found", pv "acc", pv "list"] (caseOf (v "list") [(P (listP []), ifte (v "found") (v "acc") (i 0)), (.cons (P (pv "x")) (P (pv "xs")), ifte (bin ">" (v "x") (i 5)) (ap (v "searchList") [c "True", .atexp (.paren (bin "+" (v "acc") (v "x"))), v "xs"]) (ap (v "searchList") [v "found", v "acc", v "xs"]))]),
.fn "main" [] (letDefs [.pat pw (ap (qv "Debug" "log") [str "result1", .atexp (.paren (ap (v "searchList") [c "False", i 0, listE [i 1, i 2, i 3, i 10]]))]), .pat pw (ap (qv "Debug" "log") [str "result2", .atexp (.paren (ap (v "searchList") [c "False", i 0, listE [i 1, i 2, i 3]]))]), .pat pw (ap (qv "Debug" "log") [str "result3", .atexp (.paren (ap (v "searchList") [c "False", i 0, listE [i 1, i 42]]))])] (ap (v "text") [str "done"])) ],
checks := [
chk .check "result1: 10" .int,
chk .check "result2: 0" .int,
chk .check "result3: 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_TailRecBoolCarryTest 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_TailRecBoolCarryTest 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_TailRecBoolCarryTest states that the program has a meaning by r:p-program, with both premises, and ident_elm_TailRecBoolCarryTest that its specialisation is unique and is the reduced program above, both with no hypothesis about principality.