PhantomTypeVarTest
Tags:elm-spec
Checks, in A Standard Definition of Elm?:rule sm-datbindrule sm-conbindrule s-valbind-var
Translated into Lean for the mechanisation of the book: see its Lean translation.
Tests a custom type with a type parameter that one of its constructors does not use, rebuilt at a new type by generic functions. In `type RStep e a = ROk a | RErr (List e)`, the `RErr` constructor has no `a` field, so `mapStep` and `applyR` can rebuild an `RErr` at any result type. `main` applies an identity function wrapped in `ROk` three times to `ROk 42` through `applyR`, and logs the value inside.
Results
elm-0.19.1
passran in 62 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.1/optimize for better performance and smaller assets. result: 42
elm-0.19.2
passran in 47 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.2/optimize for better performance and smaller assets. result: 42
elm-0.19.3
passran in 70 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.3/optimize for better performance and smaller assets. result: 42
lamdera-1.5.0
passran in 59 msOutput
result: 42
Discussions
Discuss this test·Search the discussions on GitHub
Discussions are on GitHub; one about this test has [t:elm/PhantomTypeVarTest] in its title.
Source
The -- CHECK: lines give the expected output.
module PhantomTypeVarTest exposing (main)
{-| Tests a custom type with a type parameter that one of its constructors
does not use, rebuilt at a new type by generic functions.
In `type RStep e a = ROk a | RErr (List e)`, the `RErr` constructor has no
`a` field, so `mapStep` and `applyR` can rebuild an `RErr` at any result
type. `main` applies an identity function wrapped in `ROk` three times to
`ROk 42` through `applyR`, and logs the value inside.
-}
-- TAGS: elm-spec, sm-datbind, sm-conbind, s-valbind-var
-- CHECK: result: 42
import Html exposing (text)
type RStep e a
= ROk a
| RErr (List e)
mapStep : (a -> b) -> RStep e a -> RStep e b
mapStep f step =
case step of
ROk val ->
ROk (f val)
RErr errs ->
RErr errs
applyR : RStep e (a -> b) -> RStep e a -> RStep e b
applyR funcStep argStep =
case funcStep of
RErr errs ->
RErr errs
ROk func ->
mapStep func argStep
main =
let
base =
ROk 42
idFunc =
ROk (\x -> x)
step1 =
applyR idFunc base
step2 =
applyR idFunc step1
step3 =
applyR idFunc step2
result =
case step3 of
ROk v ->
v
RErr _ ->
-1
_ =
Debug.log "result" 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_PhantomTypeVarTest, in StandardElmMech/Generated/Elm_PartialAppCh.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 |
|---|---|---|---|
result | 42 | 42 | t_elm_PhantomTypeVarTest |
/-- tests/elm/src/PhantomTypeVarTest.elm -/
def g_elm_PhantomTypeVarTest : Program := {
name := "PhantomTypeVarTest", source := "tests/elm/src",
exposing := (.items [.var "main"]), imports := [⟨"Html", "Html", some (.items [.var "text"])⟩],
types := [("RStep", [("ROk", 1), ("RErr", 1)])],
aliases := [],
defs := [
.fn "mapStep" [pv "f", pv "step"] (caseOf (v "step") [(pcon "ROk" [pv "val"], ap (c "ROk") [.atexp (.paren (ap (v "f") [v "val"]))]), (pcon "RErr" [pv "errs"], ap (c "RErr") [v "errs"])]),
.fn "applyR" [pv "funcStep", pv "argStep"] (caseOf (v "funcStep") [(pcon "RErr" [pv "errs"], ap (c "RErr") [v "errs"]), (pcon "ROk" [pv "func"], ap (v "mapStep") [v "func", v "argStep"])]),
.fn "main" [] (letDefs [.fn "base" [] (ap (c "ROk") [.atexp (.scon (.intF 42))]), .fn "idFunc" [] (ap (c "ROk") [.atexp (.paren (lams [pv "x"] (v "x")))]), .fn "step1" [] (ap (v "applyR") [v "idFunc", v "base"]), .fn "step2" [] (ap (v "applyR") [v "idFunc", v "step1"]), .fn "step3" [] (ap (v "applyR") [v "idFunc", v "step2"]), .fn "result" [] (caseOf (v "step3") [(pcon "ROk" [pv "v"], v "v"), (pcon "RErr" [pw], .atexp (neg (.scon (.intF 1))))]), .pat pw (ap (qv "Debug" "log") [str "result", v "result"])] (ap (v "text") [str "done"])) ],
checks := [
chk .check "result: 42" .float ] }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_PhantomTypeVarTest 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_PhantomTypeVarTest 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_PhantomTypeVarTest states that the program has a meaning by r:p-program, with both premises, and ident_elm_PhantomTypeVarTest that its specialisation is unique and is the reduced program above, both with no hypothesis about principality.