PapEtaCtorProbe
Tags:elm-spec
Checks, in A Standard Definition of Elm?:rule d-app-closurerule d-app-conrule d-pat-conrule d-atexp-update
Translated into Lean for the mechanisation of the book: see its Lean translation.
Tests point-free top-level functions defined by partially applying a two-argument function, stored in a constructor, pattern-matched back out and applied. `bumpA` and `bumpB` are `bumpWith` applied to a record-update lambda; each increments `shape` and one of `a` or `b`. `resolve` returns `Stamp n` for `n > 4`, else `Decline bumpA` for even `n` and `Decline bumpB` for odd `n`. Folding `step`, with a local `foldl`, over `[ 1, 2, 3, 4, 9 ]` applies `bumpB`, `bumpA`, `bumpB`, `bumpA`, then adds 9 to `depth`.
Results
elm-0.19.1
passran in 58 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.1/optimize for better performance and smaller assets.
final: { depth = 9, stats = { a = 2, b = 2, shape = 4 } }
elm-0.19.2
passran in 55 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.2/optimize for better performance and smaller assets.
final: { depth = 9, stats = { a = 2, b = 2, shape = 4 } }
elm-0.19.3
passran in 52 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.3/optimize for better performance and smaller assets.
final: { depth = 9, stats = { a = 2, b = 2, shape = 4 } }
lamdera-1.5.0
passran in 63 msOutput
final: { depth = 9, stats = { a = 2, b = 2, shape = 4 } }
Discussions
Discuss this test·Search the discussions on GitHub
Discussions are on GitHub; one about this test has [t:elm/PapEtaCtorProbe] in its title.
Source
The -- CHECK: lines give the expected output.
module PapEtaCtorProbe exposing (main)
{-| Tests point-free top-level functions defined by partially applying a
two-argument function, stored in a constructor, pattern-matched back out
and applied.
`bumpA` and `bumpB` are `bumpWith` applied to a record-update lambda; each
increments `shape` and one of `a` or `b`. `resolve` returns `Stamp n` for
`n > 4`, else `Decline bumpA` for even `n` and `Decline bumpB` for odd `n`.
Folding `step`, with a local `foldl`, over `[ 1, 2, 3, 4, 9 ]` applies `bumpB`, `bumpA`, `bumpB`,
`bumpA`, then adds 9 to `depth`.
-}
import Html exposing (text)
-- TAGS: elm-spec, d-app-closure, d-app-con, d-pat-con, d-atexp-update
-- CHECK: final: { depth = 9, stats = { a = 2, b = 2, shape = 4 } }
type alias Stats =
{ a : Int, b : Int, shape : Int }
type alias Ctx =
{ stats : Stats, depth : Int }
type Resolution
= Stamp Int
| Decline (Ctx -> Ctx)
bumpWith : (Stats -> Stats) -> Ctx -> Ctx
bumpWith sub ctx =
let
stats =
ctx.stats
in
{ ctx | stats = sub { stats | shape = stats.shape + 1 } }
bumpA : Ctx -> Ctx
bumpA =
bumpWith (\st -> { st | a = st.a + 1 })
bumpB : Ctx -> Ctx
bumpB =
bumpWith (\st -> { st | b = st.b + 1 })
resolve : Int -> Resolution
resolve n =
if n > 4 then
Stamp n
else if modBy 2 n == 0 then
Decline bumpA
else
Decline bumpB
step : Int -> Ctx -> Ctx
step n ctx =
case resolve n of
Stamp k ->
{ ctx | depth = ctx.depth + k }
Decline bump ->
bump ctx
foldl : (a -> b -> b) -> b -> List a -> b
foldl f acc xs =
case xs of
[] ->
acc
x :: rest ->
foldl f (f x acc) rest
main =
let
init =
{ stats = { a = 0, b = 0, shape = 0 }, depth = 0 }
_ =
Debug.log "final" (foldl step init [ 1, 2, 3, 4, 9 ])
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_PapEtaCtorProbe, 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 |
|---|---|---|---|
final | { depth = 9, stats = { a = 2, b = 2, shape = 4 } } | { depth = 9, stats = { a = 2, b = 2, shape = 4 } } | t_elm_PapEtaCtorProbe |
/-- tests/elm/src/PapEtaCtorProbe.elm -/
def g_elm_PapEtaCtorProbe : Program := {
name := "PapEtaCtorProbe", source := "tests/elm/src",
exposing := (.items [.var "main"]), imports := [⟨"Html", "Html", some (.items [.var "text"])⟩],
types := [("Resolution", [("Stamp", 1), ("Decline", 1)])],
aliases := [("Ctx", some ["stats", "depth"]), ("Stats", some ["a", "b", "shape"])],
defs := [
.fn "bumpWith" [pv "sub", pv "ctx"] (letDefs [.fn "stats" [] (.atexp (access (A (v "ctx")) "stats"))] (updE "ctx" [("stats", ap (v "sub") [updE "stats" [("shape", bin "+" (.atexp (access (A (v "stats")) "shape")) (i 1))]])])),
.fn "bumpA" [] (ap (v "bumpWith") [.atexp (.paren (lams [pv "st"] (updE "st" [("a", bin "+" (.atexp (access (A (v "st")) "a")) (i 1))])))]),
.fn "bumpB" [] (ap (v "bumpWith") [.atexp (.paren (lams [pv "st"] (updE "st" [("b", bin "+" (.atexp (access (A (v "st")) "b")) (i 1))])))]),
.fn "resolve" [pv "n"] (ifChain [(bin ">" (v "n") (i 4), ap (c "Stamp") [v "n"]), (bin "==" (ap (v "modBy") [i 2, v "n"]) (i 0), ap (c "Decline") [v "bumpA"])] (ap (c "Decline") [v "bumpB"])),
.fn "step" [pv "n", pv "ctx"] (caseOf (ap (v "resolve") [v "n"]) [(pcon "Stamp" [pv "k"], updE "ctx" [("depth", bin "+" (.atexp (access (A (v "ctx")) "depth")) (v "k"))]), (pcon "Decline" [pv "bump"], ap (v "bump") [v "ctx"])]),
.fn "foldl" [pv "f", pv "acc", pv "xs"] (caseOf (v "xs") [(P (listP []), v "acc"), (.cons (P (pv "x")) (P (pv "rest")), ap (v "foldl") [v "f", .atexp (.paren (ap (v "f") [v "x", v "acc"])), v "rest"])]),
.fn "main" [] (letDefs [.fn "init" [] (recE [("stats", recE [("a", .atexp (.scon (.intF 0))), ("b", .atexp (.scon (.intF 0))), ("shape", .atexp (.scon (.intF 0)))]), ("depth", .atexp (.scon (.intF 0)))]), .fn "init$Int$Int$Int$Int" [] (recE [("stats", recE [("a", i 0), ("b", i 0), ("shape", i 0)]), ("depth", i 0)]), .pat pw (ap (qv "Debug" "log") [str "final", .atexp (.paren (ap (v "foldl") [v "step", v "init$Int$Int$Int$Int", listE [i 1, i 2, i 3, i 4, i 9]]))])] (ap (v "text") [str "done"])) ],
checks := [
chk .check "final: { depth = 9, stats = { a = 2, b = 2, shape = 4 } }" .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_PapEtaCtorProbe 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_PapEtaCtorProbe 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_PapEtaCtorProbe states that the program has a meaning by r:p-program, with both premises, and ident_elm_PapEtaCtorProbe that its specialisation is unique and is the reduced program above, both with no hypothesis about principality.