Elm Exactly
Results / elm /

TailRecDeciderSearchTest

Tags:elm-spec

Checks, in A Standard Definition of Elm?:rule d-dec-recrule d-exp-caserule d-pat-con

Translated into Lean for the mechanisation of the book: see its Lean translation.

elm-0.19.1 passelm-0.19.2 passelm-0.19.3 passlamdera-1.5.0 pass
A local function defined in a `let` that calls itself both in a tail position and in a
non-tail position, as the scrutinee of a `case`, gives the right result.

`firstInlineExpr` searches a binary tree of `Decider` values for the leftmost
`Leaf (Inline n)`. On a `Chain yes no` it first searches `yes` (the non-tail call) and,
when that finds nothing, searches `no` (the tail call). In the test tree the only `Inline`
leaf is the rightmost one, so both calls are taken and the result is 42.

The module defines its own `Maybe_` type instead of using `Maybe`.

Results

elm-0.19.1

passran in 53 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.1/optimize for better performance and smaller assets.
found: 42

elm-0.19.2

passran in 63 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.2/optimize for better performance and smaller assets.
found: 42

elm-0.19.3

passran in 45 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.3/optimize for better performance and smaller assets.
found: 42

lamdera-1.5.0

passran in 79 ms
Output
found: 42

Discussions

Discuss this test·Search the discussions on GitHub

Discussions are on GitHub; one about this test has [t:elm/TailRecDeciderSearchTest] in its title.

Source

The -- CHECK: lines give the expected output.

module TailRecDeciderSearchTest exposing (main)
{-| A local function defined in a `let` that calls itself both in a tail position and in a
non-tail position, as the scrutinee of a `case`, gives the right result.
`firstInlineExpr` searches a binary tree of `Decider` values for the leftmost
`Leaf (Inline n)`. On a `Chain yes no` it first searches `yes` (the non-tail call) and,
when that finds nothing, searches `no` (the tail call). In the test tree the only `Inline`
leaf is the rightmost one, so both calls are taken and the result is 42.
The module defines its own `Maybe_` type instead of using `Maybe`.
-}
-- TAGS: elm-spec, d-dec-rec, d-exp-case, d-pat-con
-- CHECK: found: 42
import Html exposing (text)
type Choice
= Inline Int
| Jump Int
type Decider
= Leaf Choice
| Chain Decider Decider
type Maybe_ a
= Just_ a
| Nothing_
search : Decider -> Maybe_ Int
search tree =
let
firstInlineExpr decider =
case decider of
Leaf choice ->
case choice of
Inline val ->
Just_ val
Jump _ ->
Nothing_
Chain yes no ->
case firstInlineExpr yes of
Just_ e ->
Just_ e
Nothing_ ->
firstInlineExpr no
in
firstInlineExpr tree
unwrap : Maybe_ Int -> Int
unwrap m =
case m of
Just_ x -> x
Nothing_ -> -1
main =
let
tree = Chain (Leaf (Jump 0)) (Chain (Leaf (Jump 1)) (Leaf (Inline 42)))
result = unwrap (search tree)
_ = Debug.log "found" result
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_TailRecDeciderSearchTest, 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.

CheckExpected (CHECK)Lean evaluatorProved by
found4242t_elm_TailRecDeciderSearchTest
/-- tests/elm/src/TailRecDeciderSearchTest.elm -/
def g_elm_TailRecDeciderSearchTest : Program := {
  name := "TailRecDeciderSearchTest", source := "tests/elm/src",
  exposing := (.items [.var "main"]), imports := [⟨"Html", "Html", some (.items [.var "text"])⟩],
  types := [("Choice", [("Inline", 1), ("Jump", 1)]), ("Decider", [("Leaf", 1), ("Chain", 2)]), ("Maybe_", [("Just_", 1), ("Nothing_", 0)])],
  aliases := [],
  defs := [
    .fn "search" [pv "tree"] (letDefs [.fn "firstInlineExpr" [pv "decider"] (caseOf (v "decider") [(pcon "Leaf" [pv "choice"], caseOf (v "choice") [(pcon "Inline" [pv "val"], ap (c "Just_") [v "val"]), (pcon "Jump" [pw], c "Nothing_")]), (pcon "Chain" [pv "yes", pv "no"], caseOf (ap (v "firstInlineExpr") [v "yes"]) [(pcon "Just_" [pv "e"], ap (c "Just_") [v "e"]), (P (pcA "Nothing_"), ap (v "firstInlineExpr") [v "no"])])])] (ap (v "firstInlineExpr") [v "tree"])),
    .fn "unwrap" [pv "m"] (caseOf (v "m") [(pcon "Just_" [pv "x"], v "x"), (P (pcA "Nothing_"), .atexp (neg (A (i 1))))]),
    .fn "main" [] (letDefs [.fn "tree" [] (ap (c "Chain") [.atexp (.paren (ap (c "Leaf") [.atexp (.paren (ap (c "Jump") [i 0]))])), .atexp (.paren (ap (c "Chain") [.atexp (.paren (ap (c "Leaf") [.atexp (.paren (ap (c "Jump") [i 1]))])), .atexp (.paren (ap (c "Leaf") [.atexp (.paren (ap (c "Inline") [i 42]))]))]))]), .fn "result" [] (ap (v "unwrap") [.atexp (.paren (ap (v "search") [v "tree"]))]), .pat pw (ap (qv "Debug" "log") [str "found", v "result"])] (ap (v "text") [str "done"])) ],
  checks := [
    chk .check "found: 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_TailRecDeciderSearchTest 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_TailRecDeciderSearchTest 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_TailRecDeciderSearchTest states that the program has a meaning by r:p-program, with both premises, and ident_elm_TailRecDeciderSearchTest that its specialisation is unique and is the reduced program above, both with no hypothesis about principality.