Elm Exactly
Results / elm-core /

TimerEffectTest

Tags:elm-spec

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

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
A `Platform.worker` that chains `Process.sleep` timers through `Task.perform` receives each timer message in turn and stops after five.

`init` starts a 100 ms sleep; each time it ends, `update` logs the count and
starts another, until the fifth, when it logs `"done"` and returns `Cmd.none`.
The message is built by a function that ignores the sleep's `()` result.

Results

elm-0.19.1

passran in 566 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.1/optimize for better performance and smaller assets.
TimerEffectTest: "fired 1"
TimerEffectTest: "fired 2"
TimerEffectTest: "fired 3"
TimerEffectTest: "fired 4"
TimerEffectTest: "fired 5"
TimerEffectTest: "done"

elm-0.19.2

passran in 569 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.2/optimize for better performance and smaller assets.
TimerEffectTest: "fired 1"
TimerEffectTest: "fired 2"
TimerEffectTest: "fired 3"
TimerEffectTest: "fired 4"
TimerEffectTest: "fired 5"
TimerEffectTest: "done"

elm-0.19.3

passran in 541 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.3/optimize for better performance and smaller assets.
TimerEffectTest: "fired 1"
TimerEffectTest: "fired 2"
TimerEffectTest: "fired 3"
TimerEffectTest: "fired 4"
TimerEffectTest: "fired 5"
TimerEffectTest: "done"

lamdera-1.5.0

passran in 569 ms
Output
TimerEffectTest: "fired 1"
TimerEffectTest: "fired 2"
TimerEffectTest: "fired 3"
TimerEffectTest: "fired 4"
TimerEffectTest: "fired 5"
TimerEffectTest: "done"

Discussions

Discuss this test·Search the discussions on GitHub

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

Source

The -- CHECK: lines give the expected output.

module TimerEffectTest exposing (main)
{-| A `Platform.worker` that chains `Process.sleep` timers through `Task.perform` receives each timer message in turn and stops after five.
`init` starts a 100 ms sleep; each time it ends, `update` logs the count and
starts another, until the fifth, when it logs `"done"` and returns `Cmd.none`.
The message is built by a function that ignores the sleep's `()` result.
-}
-- TAGS: elm-spec, p-proc-sleep, e-msg-task, p-update
-- CHECK: TimerEffectTest: "fired 1"
-- CHECK: TimerEffectTest: "fired 2"
-- CHECK: TimerEffectTest: "fired 3"
-- CHECK: TimerEffectTest: "fired 4"
-- CHECK: TimerEffectTest: "fired 5"
-- CHECK: TimerEffectTest: "done"
import Platform
import Process
import Task
type Msg
= TimerFired
type alias Model =
{ count : Int
}
sleepCmd : Cmd Msg
sleepCmd =
Process.sleep 100
|> Task.perform (\_ -> TimerFired)
init : () -> ( Model, Cmd Msg )
init _ =
( { count = 0 }
, sleepCmd
)
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
TimerFired ->
let
newCount =
model.count + 1
_ =
Debug.log "TimerEffectTest" ("fired " ++ Debug.toString newCount)
in
if newCount >= 5 then
let
_ =
Debug.log "TimerEffectTest" "done"
in
( { model | count = newCount }, Cmd.none )
else
( { model | count = newCount }, sleepCmd )
subscriptions : Model -> Sub Msg
subscriptions _ =
Sub.none
main : Program () Model Msg
main =
Platform.worker
{ init = init
, update = update
, subscriptions = subscriptions
}

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_TimerEffectTest, 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 native_decide ([native]).

Out of date. The test, or a module it imports, has changed since this translation was made; the translation, and so its theorems, describe the program as it was until the mechanisation is rebuilt.

CheckExpected (CHECK)Lean evaluatorProved by
TimerEffectTest"fired 1" (the test now says "done")"fired 1" or "fired 2" or "fired 3" or "fired 4" or "fired 5" or "done"t_elm_core_TimerEffectTest
TimerEffectTest"fired 2" (the test now says "done")"fired 1" or "fired 2" or "fired 3" or "fired 4" or "fired 5" or "done"t_elm_core_TimerEffectTest
TimerEffectTest"fired 3" (the test now says "done")"fired 1" or "fired 2" or "fired 3" or "fired 4" or "fired 5" or "done"t_elm_core_TimerEffectTest
TimerEffectTest"fired 4" (the test now says "done")"fired 1" or "fired 2" or "fired 3" or "fired 4" or "fired 5" or "done"t_elm_core_TimerEffectTest
TimerEffectTest"fired 5" (the test now says "done")"fired 1" or "fired 2" or "fired 3" or "fired 4" or "fired 5" or "done"t_elm_core_TimerEffectTest
TimerEffectTest"done""fired 1" or "fired 2" or "fired 3" or "fired 4" or "fired 5" or "done"t_elm_core_TimerEffectTest
/-- tests/elm-core/src/TimerEffectTest.elm -/
def g_elm_core_TimerEffectTest : Program := {
  name := "TimerEffectTest", source := "tests/elm-core/src", native := true,
  exposing := (.items [.var "main"]), imports := [⟨"Platform", "Platform", none⟩, ⟨"Process", "Process", none⟩, ⟨"Task", "Task", none⟩],
  types := [("Msg", [("TimerFired", 0)])],
  aliases := [("Model", some ["count"])],
  defs := [
    .fn "sleepCmd" [] (bin "|>" (ap (qv "Process" "sleep") [.atexp (.scon (.intF 100))]) (ap (qv "Task" "perform") [.atexp (.paren (lams [pw] (c "TimerFired")))])),
    .fn "init" [pw] (tup2 (recE [("count", i 0)]) (v "sleepCmd")),
    .fn "update" [pv "msg", pv "model"] (caseOf (v "msg") [(P (pcA "TimerFired"), letDefs [.fn "newCount" [] (bin "+" (.atexp (access (A (v "model")) "count")) (i 1)), .pat pw (ap (qv "Debug" "log") [str "TimerEffectTest", .atexp (.paren (bin "++" (str "fired ") (ap (qv "Debug" "toString") [v "newCount"])))])] (ifte (bin ">=" (v "newCount") (i 5)) (letDefs [.pat pw (ap (qv "Debug" "log") [str "TimerEffectTest", str "done"])] (tup2 (updE "model" [("count", v "newCount")]) (qv "Cmd" "none"))) (tup2 (updE "model" [("count", v "newCount")]) (v "sleepCmd"))))]),
    .fn "subscriptions" [pw] (qv "Sub" "none"),
    .fn "main" [] (ap (qv "Platform" "worker") [recE [("init", v "init"), ("update", v "update"), ("subscriptions", v "subscriptions")]]) ],
  checks := [
    chk .check "TimerEffectTest: \"fired 1\"" .string,
    chk .check "TimerEffectTest: \"fired 2\"" .string,
    chk .check "TimerEffectTest: \"fired 3\"" .string,
    chk .check "TimerEffectTest: \"fired 4\"" .string,
    chk .check "TimerEffectTest: \"fired 5\"" .string,
    chk .check "TimerEffectTest: \"done\"" .string ] }

/-- The worker test of TimerEffectTest. -/
def w_elm_core_TimerEffectTest : Prog.WorkerTest := {
  prog := g_elm_core_TimerEffectTest,
  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_TimerEffectTest 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_TimerEffectTest 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_TimerEffectTest states that the program has a meaning by r:p-program, with both premises, and ident_elm_core_TimerEffectTest that its specialisation is unique and is the reduced program above, both with no hypothesis about principality. One of these theorems is proved by native_decide ([native]).