Elm Exactly
Results / elm-core /

TaskPerformValueTest

Tags:elm-spec

Checks, in A Standard Definition of Elm?:rule e-msg-taskrule p-disp-taskrule p-proc-message

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
`Task.perform` delivers the task's value to `update` in a `Platform.worker`, for messages that carry an `Int`, a negative `Int` or a `String`, and for a message built by a function that ignores the value.

`init` batches four `Task.perform` commands on `Task.succeed`; `update` logs the
payload of each message.

Results

elm-0.19.1

passran in 90 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.1/optimize for better performance and smaller assets.
d: "hello"
c: -7
b: "nullary ok"
a: 42

elm-0.19.2

passran in 68 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.2/optimize for better performance and smaller assets.
d: "hello"
c: -7
b: "nullary ok"
a: 42

elm-0.19.3

passran in 72 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.3/optimize for better performance and smaller assets.
d: "hello"
c: -7
b: "nullary ok"
a: 42

lamdera-1.5.0

passran in 44 ms
Output
d: "hello"
c: -7
b: "nullary ok"
a: 42

Discussions

Discuss this test·Search the discussions on GitHub

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

Source

The -- CHECK: lines give the expected output.

module TaskPerformValueTest exposing (main)
{-| `Task.perform` delivers the task's value to `update` in a `Platform.worker`, for messages that carry an `Int`, a negative `Int` or a `String`, and for a message built by a function that ignores the value.
`init` batches four `Task.perform` commands on `Task.succeed`; `update` logs the
payload of each message.
-}
-- TAGS: elm-spec, e-msg-task, p-disp-task, p-proc-message
-- CHECK: a: 42
-- CHECK: b: "nullary ok"
-- CHECK: c: -7
-- CHECK: d: "hello"
import Platform
import Task
type Msg
= GotInt Int
| GotNullary
| GotNeg Int
| GotStr String
init : () -> ( Int, Cmd Msg )
init _ =
( 0
, Cmd.batch
[ Task.perform GotInt (Task.succeed 42)
, Task.perform (\_ -> GotNullary) (Task.succeed 1)
, Task.perform GotNeg (Task.succeed -7)
, Task.perform GotStr (Task.succeed "hello")
]
)
update : Msg -> Int -> ( Int, Cmd Msg )
update msg model =
case msg of
GotInt v ->
let
_ =
Debug.log "a" v
in
( model + 1, Cmd.none )
GotNullary ->
let
_ =
Debug.log "b" "nullary ok"
in
( model + 1, Cmd.none )
GotNeg v ->
let
_ =
Debug.log "c" v
in
( model + 1, Cmd.none )
GotStr s ->
let
_ =
Debug.log "d" s
in
( model + 1, Cmd.none )
main : Program () Int Msg
main =
Platform.worker
{ init = init
, update = update
, subscriptions = \_ -> Sub.none
}

Lean translation

This worker program is translated into Lean by the translator of the Lean mechanisation of A Standard Definition of Elm? (g_elm_core_TaskPerformValueTest, in StandardElmMech/Generated/Worker01.lean). Lean evaluates its modules by the rules of Chapter 7, starts it by r:p-init with no flags (null, A72), and runs it by the transitions of Chapter 9 under a host that sends nothing. Its checks become one generated theorem: some execution the book allows, under that host, has a log (the modules', then the start's, then each step's) that meets every check. The theorem trusts the parser and the translator's elaboration ([parse] [elab]), the test basis ([harness]) and the parameters of the basis ([params]), and is proved by the kernel.

CheckExpected (CHECK)Lean evaluatorProved by
a4242t_elm_core_TaskPerformValueTest
b"nullary ok""nullary ok"t_elm_core_TaskPerformValueTest
c-7-7t_elm_core_TaskPerformValueTest
d"hello""hello"t_elm_core_TaskPerformValueTest
/-- tests/elm-core/src/TaskPerformValueTest.elm -/
def g_elm_core_TaskPerformValueTest : Program := {
  name := "TaskPerformValueTest", source := "tests/elm-core/src",
  exposing := (.items [.var "main"]), imports := [⟨"Platform", "Platform", none⟩, ⟨"Task", "Task", none⟩],
  types := [("Msg", [("GotInt", 1), ("GotNullary", 0), ("GotNeg", 1), ("GotStr", 1)])],
  aliases := [],
  defs := [
    .fn "init" [pw] (tup2 (i 0) (ap (qv "Cmd" "batch") [listE [ap (qv "Task" "perform") [c "GotInt", .atexp (.paren (ap (qv "Task" "succeed") [i 42]))], ap (qv "Task" "perform") [.atexp (.paren (lams [pw] (c "GotNullary"))), .atexp (.paren (ap (qv "Task" "succeed") [.atexp (.scon (.intF 1))]))], ap (qv "Task" "perform") [c "GotNeg", .atexp (.paren (ap (qv "Task" "succeed") [.atexp (neg (A (i 7)))]))], ap (qv "Task" "perform") [c "GotStr", .atexp (.paren (ap (qv "Task" "succeed") [str "hello"]))]]])),
    .fn "update" [pv "msg", pv "model"] (caseOf (v "msg") [(pcon "GotInt" [pv "v"], letDefs [.pat pw (ap (qv "Debug" "log") [str "a", v "v"])] (tup2 (bin "+" (v "model") (i 1)) (qv "Cmd" "none"))), (P (pcA "GotNullary"), letDefs [.pat pw (ap (qv "Debug" "log") [str "b", str "nullary ok"])] (tup2 (bin "+" (v "model") (i 1)) (qv "Cmd" "none"))), (pcon "GotNeg" [pv "v"], letDefs [.pat pw (ap (qv "Debug" "log") [str "c", v "v"])] (tup2 (bin "+" (v "model") (i 1)) (qv "Cmd" "none"))), (pcon "GotStr" [pv "s"], letDefs [.pat pw (ap (qv "Debug" "log") [str "d", v "s"])] (tup2 (bin "+" (v "model") (i 1)) (qv "Cmd" "none")))]),
    .fn "main" [] (ap (qv "Platform" "worker") [recE [("init", v "init"), ("update", v "update"), ("subscriptions", lams [pw] (qv "Sub" "none"))]]) ],
  checks := [
    chk .check "a: 42" .int,
    chk .check "b: \"nullary ok\"" .string,
    chk .check "c: -7" .int,
    chk .check "d: \"hello\"" .string ] }

/-- The worker test of TaskPerformValueTest. -/
def w_elm_core_TaskPerformValueTest : Prog.WorkerTest := {
  prog := g_elm_core_TaskPerformValueTest,
  flagsType := .unit,
  flags := .null,
  host := .none,
  checks := [] }

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_core_TaskPerformValueTest 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_core_TaskPerformValueTest 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_core_TaskPerformValueTest states that the program has a meaning by r:p-program, with both premises, and ident_elm_core_TaskPerformValueTest that its specialisation is unique and is the reduced program above, both with no hypothesis about principality.