MutualLetRecManyCapturesTest
Tags:elm-spec
Checks, in A Standard Definition of Elm?:rule s-dec-grouprule d-dec-recrule d-exp-lambda
Translated into Lean for the mechanisation of the book: see its Lean translation.
Two mutually recursive `let`-bound functions inside a top-level function, both using all four of the enclosing function's parameters, compute the right result. `classify 10 11 12 9 n` alternates between `innerSmall` and `innerBig` until `k` reaches `0`, and either base case returns `10 + 11 + 12 + 9 = 42`. The extra term in `innerBig` is multiplied by `0`, so every `n` from `3` down to `0` logs `42`.
Results
elm-0.19.1
passran in 74 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.1/optimize for better performance and smaller assets. go 3: 42 go 2: 42 go 1: 42 go 0: 42
elm-0.19.2
passran in 54 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.2/optimize for better performance and smaller assets. go 3: 42 go 2: 42 go 1: 42 go 0: 42
elm-0.19.3
passran in 51 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.3/optimize for better performance and smaller assets. go 0: 42 go 1: 42 go 2: 42 go 3: 42
lamdera-1.5.0
passran in 53 msOutput
go 0: 42 go 1: 42 go 2: 42 go 3: 42
Discussions
Discuss this test·Search the discussions on GitHub
Discussions are on GitHub; one about this test has [t:elm/MutualLetRecManyCapturesTest] in its title.
Source
The -- CHECK: lines give the expected output.
module MutualLetRecManyCapturesTest exposing (main)
{-| Two mutually recursive `let`-bound functions inside a top-level function, both using all four of the enclosing function's parameters, compute the right result.
`classify 10 11 12 9 n` alternates between `innerSmall` and `innerBig` until
`k` reaches `0`, and either base case returns `10 + 11 + 12 + 9 = 42`. The
extra term in `innerBig` is multiplied by `0`, so every `n` from `3` down to
`0` logs `42`.
-}
-- TAGS: elm-spec, s-dec-group, d-dec-rec, d-exp-lambda
-- CHECK: go 3: 42
-- CHECK: go 2: 42
-- CHECK: go 1: 42
-- CHECK: go 0: 42
import Html exposing (text)
{-| Returns `extA + extB + extC + extD` for any `n`, reached by alternating between two
mutually recursive local functions.
-}
classify : Int -> Int -> Int -> Int -> Int -> Int
classify extA extB extC extD n =
let
innerSmall k =
if k <= 0 then
extA + extB + extC + extD
else
innerBig (k - 1)
innerBig k =
if k <= 0 then
extA + extB + extC + extD
else
innerSmall (k - 1) + 0 * (extA - extB + extC - extD)
in
innerSmall n
main =
let
a =
10
b =
11
c =
12
d =
9
_ =
Debug.log "go 3" (classify a b c d 3)
_ =
Debug.log "go 2" (classify a b c d 2)
_ =
Debug.log "go 1" (classify a b c d 1)
_ =
Debug.log "go 0" (classify a b c d 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_MutualLetRecManyCapturesTest, 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 |
|---|---|---|---|
go 3 | 42 | 42 | t_elm_MutualLetRecManyCapturesTest |
go 2 | 42 | 42 | t_elm_MutualLetRecManyCapturesTest |
go 1 | 42 | 42 | t_elm_MutualLetRecManyCapturesTest |
go 0 | 42 | 42 | t_elm_MutualLetRecManyCapturesTest |
/-- tests/elm/src/MutualLetRecManyCapturesTest.elm -/
def g_elm_MutualLetRecManyCapturesTest : Program := {
name := "MutualLetRecManyCapturesTest", source := "tests/elm/src",
exposing := (.items [.var "main"]), imports := [⟨"Html", "Html", some (.items [.var "text"])⟩],
types := [],
aliases := [],
defs := [
.fn "classify" [pv "extA", pv "extB", pv "extC", pv "extD", pv "n"] (letDefs [.fn "innerSmall" [pv "k"] (ifte (bin "<=" (v "k") (.atexp (.scon (.intF 0)))) (bin "+" (bin "+" (bin "+" (v "extA") (v "extB")) (v "extC")) (v "extD")) (ap (v "innerBig") [.atexp (.paren (bin "-" (v "k") (.atexp (.scon (.intF 1)))))])), .fn "innerBig" [pv "k"] (ifte (bin "<=" (v "k") (.atexp (.scon (.intF 0)))) (bin "+" (bin "+" (bin "+" (v "extA") (v "extB")) (v "extC")) (v "extD")) (bin "+" (ap (v "innerSmall") [.atexp (.paren (bin "-" (v "k") (.atexp (.scon (.intF 1)))))]) (bin "*" (i 0) (.atexp (.paren (bin "-" (bin "+" (bin "-" (v "extA") (v "extB")) (v "extC")) (v "extD"))))))), .fn "innerSmall$Int" [pv "k"] (ifte (bin "<=" (v "k") (i 0)) (bin "+" (bin "+" (bin "+" (v "extA") (v "extB")) (v "extC")) (v "extD")) (ap (v "innerBig$Int") [.atexp (.paren (bin "-" (v "k") (i 1)))])), .fn "innerBig$Int" [pv "k"] (ifte (bin "<=" (v "k") (i 0)) (bin "+" (bin "+" (bin "+" (v "extA") (v "extB")) (v "extC")) (v "extD")) (bin "+" (ap (v "innerSmall$Int") [.atexp (.paren (bin "-" (v "k") (i 1)))]) (bin "*" (i 0) (.atexp (.paren (bin "-" (bin "+" (bin "-" (v "extA") (v "extB")) (v "extC")) (v "extD")))))))] (ap (v "innerSmall$Int") [v "n"])),
.fn "main" [] (letDefs [.fn "a" [] (.atexp (.scon (.intF 10))), .fn "a$Int" [] (i 10), .fn "b" [] (.atexp (.scon (.intF 11))), .fn "b$Int" [] (i 11), .fn "c" [] (.atexp (.scon (.intF 12))), .fn "c$Int" [] (i 12), .fn "d" [] (.atexp (.scon (.intF 9))), .fn "d$Int" [] (i 9), .pat pw (ap (qv "Debug" "log") [str "go 3", .atexp (.paren (ap (v "classify") [v "a$Int", v "b$Int", v "c$Int", v "d$Int", i 3]))]), .pat pw (ap (qv "Debug" "log") [str "go 2", .atexp (.paren (ap (v "classify") [v "a$Int", v "b$Int", v "c$Int", v "d$Int", i 2]))]), .pat pw (ap (qv "Debug" "log") [str "go 1", .atexp (.paren (ap (v "classify") [v "a$Int", v "b$Int", v "c$Int", v "d$Int", i 1]))]), .pat pw (ap (qv "Debug" "log") [str "go 0", .atexp (.paren (ap (v "classify") [v "a$Int", v "b$Int", v "c$Int", v "d$Int", i 0]))])] (ap (v "text") [str "done"])) ],
checks := [
chk .check "go 3: 42" .int,
chk .check "go 2: 42" .int,
chk .check "go 1: 42" .int,
chk .check "go 0: 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_MutualLetRecManyCapturesTest 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_MutualLetRecManyCapturesTest 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_MutualLetRecManyCapturesTest states that the program has a meaning by r:p-program, with both premises, and ident_elm_MutualLetRecManyCapturesTest that its specialisation is unique and is the reduced program above, both with no hypothesis about principality.