MutualLetRecClosuresTest
Tags:elm-spec
Checks, in A Standard Definition of Elm?:rule s-dec-grouprule d-dec-recrestriction core-let-recursion
Translated into Lean for the mechanisation of the book: see its Lean translation.
Two `let`-bound functions that call each other (`evenP` and `oddP`) are mutually recursive, and each returns a record holding a Bool result and a step count.
Each step decrements `k` and increments `counter`; `evenP` returns `True` and
`oddP` returns `False` when `k` reaches `0`.
- `classify 4` starts at `evenP 4`, ends in `evenP 0` after 4 steps, and
reports `isEven = True`, `calls = 4`.
- `classifyOdd 3` starts at `oddP 3`, ends in `evenP 0` after 3 steps, so the
probe result is `True` and `isOdd`, its negation, is `False`.Results
elm-0.19.1
passran in 53 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.1/optimize for better performance and smaller assets. evenResult: True oddResult: False callsEven: 4 callsOdd: 3
elm-0.19.2
passran in 71 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.2/optimize for better performance and smaller assets. evenResult: True oddResult: False callsEven: 4 callsOdd: 3
elm-0.19.3
passran in 57 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.3/optimize for better performance and smaller assets. evenResult: True oddResult: False callsEven: 4 callsOdd: 3
lamdera-1.5.0
passran in 48 msOutput
evenResult: True oddResult: False callsEven: 4 callsOdd: 3
Discussions
Discuss this test·Search the discussions on GitHub
Discussions are on GitHub; one about this test has [t:elm/MutualLetRecClosuresTest] in its title.
Source
The -- CHECK: lines give the expected output.
module MutualLetRecClosuresTest exposing (main)
{-| Two `let`-bound functions that call each other (`evenP` and `oddP`) are mutually recursive, and each returns a record holding a Bool result and a step count.
Each step decrements `k` and increments `counter`; `evenP` returns `True` and
`oddP` returns `False` when `k` reaches `0`.
- `classify 4` starts at `evenP 4`, ends in `evenP 0` after 4 steps, and
reports `isEven = True`, `calls = 4`.
- `classifyOdd 3` starts at `oddP 3`, ends in `evenP 0` after 3 steps, so the
probe result is `True` and `isOdd`, its negation, is `False`.
-}
-- TAGS: elm-spec, s-dec-group, d-dec-rec, core-let-recursion
-- CHECK: evenResult: True
-- CHECK: oddResult: False
-- CHECK: callsEven: 4
-- CHECK: callsOdd: 3
import Html exposing (text)
classify : Int -> { isEven : Bool, calls : Int }
classify n =
let
evenP : Int -> Int -> { result : Bool, steps : Int }
evenP k counter =
if k == 0 then
{ result = True, steps = counter }
else
oddP (k - 1) (counter + 1)
oddP : Int -> Int -> { result : Bool, steps : Int }
oddP k counter =
if k == 0 then
{ result = False, steps = counter }
else
evenP (k - 1) (counter + 1)
probe =
evenP n 0
in
{ isEven = probe.result, calls = probe.steps }
classifyOdd : Int -> { isOdd : Bool, calls : Int }
classifyOdd n =
let
evenP : Int -> Int -> { result : Bool, steps : Int }
evenP k counter =
if k == 0 then
{ result = True, steps = counter }
else
oddP (k - 1) (counter + 1)
oddP : Int -> Int -> { result : Bool, steps : Int }
oddP k counter =
if k == 0 then
{ result = False, steps = counter }
else
evenP (k - 1) (counter + 1)
probe =
oddP n 0
in
{ isOdd = not probe.result, calls = probe.steps }
main =
let
evenInfo =
classify 4
oddInfo =
classifyOdd 3
_ =
Debug.log "evenResult" evenInfo.isEven
_ =
Debug.log "oddResult" oddInfo.isOdd
_ =
Debug.log "callsEven" evenInfo.calls
_ =
Debug.log "callsOdd" oddInfo.calls
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_MutualLetRecClosuresTest, 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 |
|---|---|---|---|
evenResult | True | True | t_elm_MutualLetRecClosuresTest |
oddResult | False | False | t_elm_MutualLetRecClosuresTest |
callsEven | 4 | 4 | t_elm_MutualLetRecClosuresTest |
callsOdd | 3 | 3 | t_elm_MutualLetRecClosuresTest |
/-- tests/elm/src/MutualLetRecClosuresTest.elm -/
def g_elm_MutualLetRecClosuresTest : Program := {
name := "MutualLetRecClosuresTest", source := "tests/elm/src",
exposing := (.items [.var "main"]), imports := [⟨"Html", "Html", some (.items [.var "text"])⟩],
types := [],
aliases := [],
defs := [
.fn "classify" [pv "n"] (letDefs [.fn "evenP" [pv "k", pv "counter"] (ifte (bin "==" (v "k") (i 0)) (recE [("result", c "True"), ("steps", v "counter")]) (ap (v "oddP") [.atexp (.paren (bin "-" (v "k") (i 1))), .atexp (.paren (bin "+" (v "counter") (i 1)))])), .fn "oddP" [pv "k", pv "counter"] (ifte (bin "==" (v "k") (i 0)) (recE [("result", c "False"), ("steps", v "counter")]) (ap (v "evenP") [.atexp (.paren (bin "-" (v "k") (i 1))), .atexp (.paren (bin "+" (v "counter") (i 1)))])), .fn "probe" [] (ap (v "evenP") [v "n", i 0])] (recE [("isEven", .atexp (access (A (v "probe")) "result")), ("calls", .atexp (access (A (v "probe")) "steps"))])),
.fn "classifyOdd" [pv "n"] (letDefs [.fn "evenP" [pv "k", pv "counter"] (ifte (bin "==" (v "k") (i 0)) (recE [("result", c "True"), ("steps", v "counter")]) (ap (v "oddP") [.atexp (.paren (bin "-" (v "k") (i 1))), .atexp (.paren (bin "+" (v "counter") (i 1)))])), .fn "oddP" [pv "k", pv "counter"] (ifte (bin "==" (v "k") (i 0)) (recE [("result", c "False"), ("steps", v "counter")]) (ap (v "evenP") [.atexp (.paren (bin "-" (v "k") (i 1))), .atexp (.paren (bin "+" (v "counter") (i 1)))])), .fn "probe" [] (ap (v "oddP") [v "n", i 0])] (recE [("isOdd", ap (v "not") [.atexp (access (A (v "probe")) "result")]), ("calls", .atexp (access (A (v "probe")) "steps"))])),
.fn "main" [] (letDefs [.fn "evenInfo" [] (ap (v "classify") [i 4]), .fn "oddInfo" [] (ap (v "classifyOdd") [i 3]), .pat pw (ap (qv "Debug" "log") [str "evenResult", .atexp (access (A (v "evenInfo")) "isEven")]), .pat pw (ap (qv "Debug" "log") [str "oddResult", .atexp (access (A (v "oddInfo")) "isOdd")]), .pat pw (ap (qv "Debug" "log") [str "callsEven", .atexp (access (A (v "evenInfo")) "calls")]), .pat pw (ap (qv "Debug" "log") [str "callsOdd", .atexp (access (A (v "oddInfo")) "calls")])] (ap (v "text") [str "done"])) ],
checks := [
chk .check "evenResult: True" .data,
chk .check "oddResult: False" .data,
chk .check "callsEven: 4" .int,
chk .check "callsOdd: 3" .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_MutualLetRecClosuresTest 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_MutualLetRecClosuresTest 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_MutualLetRecClosuresTest states that the program has a meaning by r:p-program, with both premises, and ident_elm_MutualLetRecClosuresTest that its specialisation is unique and is the reduced program above, both with no hypothesis about principality.