CharCaptureEscapedTailDefTest
Tags:elm-spec
Checks, in A Standard Definition of Elm?:rule d-dec-recrule d-exp-lambdarule d-app-closure
Translated into Lean for the mechanisation of the book: see its Lean translation.
A tail-recursive local function that refers to a `Char` argument of its enclosing function still works when it is returned inside a list and called later. `pickers c` defines `go`, which reports whether a `List Char` contains `c`, and returns `[ go ]`. The test applies the first function from that list to a list containing `'x'`, the same function built for `'q'` to the same list, and every function built for `'z'` to `[ 'z' ]`, using a local `runAll` that is `True` when every function in a list gives `True`.
Results
elm-0.19.1
passran in 66 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.1/optimize for better performance and smaller assets. hit: True miss: False both: True
elm-0.19.2
passran in 48 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.2/optimize for better performance and smaller assets. hit: True miss: False both: True
elm-0.19.3
passran in 81 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.3/optimize for better performance and smaller assets. hit: True miss: False both: True
lamdera-1.5.0
passran in 42 msOutput
hit: True miss: False both: True
Discussions
Discuss this test·Search the discussions on GitHub
Discussions are on GitHub; one about this test has [t:elm/CharCaptureEscapedTailDefTest] in its title.
Source
The -- CHECK: lines give the expected output.
module CharCaptureEscapedTailDefTest exposing (main)
{-| A tail-recursive local function that refers to a `Char` argument of its
enclosing function still works when it is returned inside a list and called
later.
`pickers c` defines `go`, which reports whether a `List Char` contains `c`, and
returns `[ go ]`. The test applies the first function from that list to a list
containing `'x'`, the same function built for `'q'` to the same list, and every
function built for `'z'` to `[ 'z' ]`, using a local `runAll` that is `True` when
every function in a list gives `True`.
-}
import Html exposing (text)
-- TAGS: elm-spec, d-dec-rec, d-exp-lambda, d-app-closure
-- CHECK: hit: True
-- CHECK: miss: False
-- CHECK: both: True
pickers : Char -> List (List Char -> Bool)
pickers c =
let
go xs =
case xs of
[] ->
False
y :: rest ->
if y == c then
True
else
go rest
in
[ go ]
runFirst : List (List Char -> Bool) -> List Char -> Bool
runFirst fs xs =
case fs of
f :: _ ->
f xs
[] ->
False
runAll : List (List Char -> Bool) -> List Char -> Bool
runAll fs xs =
case fs of
[] ->
True
f :: rest ->
f xs && runAll rest xs
main =
let
_ =
Debug.log "hit" (runFirst (pickers 'x') [ 'a', 'b', 'x' ])
_ =
Debug.log "miss" (runFirst (pickers 'q') [ 'a', 'b', 'x' ])
_ =
Debug.log "both" (runAll (pickers 'z') [ 'z' ])
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_CharCaptureEscapedTailDefTest, 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.
| Check | Expected (CHECK) | Lean evaluator | Proved by |
|---|---|---|---|
hit | True | True | t_elm_CharCaptureEscapedTailDefTest |
miss | False | False | t_elm_CharCaptureEscapedTailDefTest |
both | True | True | t_elm_CharCaptureEscapedTailDefTest |
/-- tests/elm/src/CharCaptureEscapedTailDefTest.elm -/
def g_elm_CharCaptureEscapedTailDefTest : Program := {
name := "CharCaptureEscapedTailDefTest", source := "tests/elm/src",
exposing := (.items [.var "main"]), imports := [⟨"Html", "Html", some (.items [.var "text"])⟩],
types := [],
aliases := [],
defs := [
.fn "pickers" [pv "c"] (letDefs [.fn "go" [pv "xs"] (caseOf (v "xs") [(P (listP []), c "False"), (.cons (P (pv "y")) (P (pv "rest")), ifte (bin "==" (v "y") (v "c")) (c "True") (ap (v "go") [v "rest"]))])] (listE [v "go"])),
.fn "runFirst" [pv "fs", pv "xs"] (caseOf (v "fs") [(.cons (P (pv "f")) (P pw), ap (v "f") [v "xs"]), (P (listP []), c "False")]),
.fn "runAll" [pv "fs", pv "xs"] (caseOf (v "fs") [(P (listP []), c "True"), (.cons (P (pv "f")) (P (pv "rest")), andalso (ap (v "f") [v "xs"]) (ap (v "runAll") [v "rest", v "xs"]))]),
.fn "main" [] (letDefs [.pat pw (ap (qv "Debug" "log") [str "hit", .atexp (.paren (ap (v "runFirst") [.atexp (.paren (ap (v "pickers") [.atexp (chA 'x')])), listE [.atexp (chA 'a'), .atexp (chA 'b'), .atexp (chA 'x')]]))]), .pat pw (ap (qv "Debug" "log") [str "miss", .atexp (.paren (ap (v "runFirst") [.atexp (.paren (ap (v "pickers") [.atexp (chA 'q')])), listE [.atexp (chA 'a'), .atexp (chA 'b'), .atexp (chA 'x')]]))]), .pat pw (ap (qv "Debug" "log") [str "both", .atexp (.paren (ap (v "runAll") [.atexp (.paren (ap (v "pickers") [.atexp (chA 'z')])), listE [.atexp (chA 'z')]]))])] (ap (v "text") [str "done"])) ],
checks := [
chk .check "hit: True" .data,
chk .check "miss: False" .data,
chk .check "both: True" .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_CharCaptureEscapedTailDefTest 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_CharCaptureEscapedTailDefTest 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_CharCaptureEscapedTailDefTest states that the program has a meaning by r:p-program, with both premises, and ident_elm_CharCaptureEscapedTailDefTest that its specialisation is unique and is the reduced program above, both with no hypothesis about principality.