Elm Exactly
Results / elm-core /

ProcessYieldThrashingTest

Tags:elm-spec

Checks, in A Standard Definition of Elm?:rule p-proc-spawnrule p-proc-sleeprule p-proc-step

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
Two processes that each repeatedly yield with `Process.sleep 0` both run to
completion.

Processes `A` and `B` are spawned one after the other. Each runs five
iterations, and every iteration sleeps 0 ms and then logs its label and
iteration number. The program logs `done` after a 200 ms sleep, by which time
both processes have finished.

Results

elm-0.19.1

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

elm-0.19.2

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

elm-0.19.3

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

lamdera-1.5.0

passran in 234 ms
Output
yield: "A 1"
yield: "B 1"
yield: "A 2"
yield: "B 2"
yield: "A 3"
yield: "B 3"
yield: "A 4"
yield: "B 4"
yield: "A 5"
yield: "B 5"
yield: "done"

Discussions

Discuss this test·Search the discussions on GitHub

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

Source

The -- CHECK: lines give the expected output.

module ProcessYieldThrashingTest exposing (main)
{-| Two processes that each repeatedly yield with `Process.sleep 0` both run to
completion.
Processes `A` and `B` are spawned one after the other. Each runs five
iterations, and every iteration sleeps 0 ms and then logs its label and
iteration number. The program logs `done` after a 200 ms sleep, by which time
both processes have finished.
-}
-- TAGS: elm-spec, p-proc-spawn, p-proc-sleep, p-proc-step
-- CHECK: yield: "A 1"
-- CHECK: yield: "A 2"
-- CHECK: yield: "A 3"
-- CHECK: yield: "A 4"
-- CHECK: yield: "A 5"
-- CHECK: yield: "B 1"
-- CHECK: yield: "B 2"
-- CHECK: yield: "B 3"
-- CHECK: yield: "B 4"
-- CHECK: yield: "B 5"
-- CHECK: yield: "done"
import Platform
import Process
import Task
type Msg
= Started
| AllDone
type alias Model =
{}
iters : Int
iters =
5
yielder : String -> Int -> Task.Task Never ()
yielder label k =
if k > iters then
Task.succeed ()
else
Process.sleep 0
|> Task.andThen
(\_ ->
let
_ =
Debug.log "yield" (label ++ " " ++ Debug.toString k)
in
yielder label (k + 1)
)
init : () -> ( Model, Cmd Msg )
init _ =
( {}
, Process.spawn (yielder "A" 1)
|> Task.andThen (\_ -> Process.spawn (yielder "B" 1))
|> Task.perform (\_ -> Started)
)
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
Started ->
( model
, Process.sleep 200
|> Task.perform (\_ -> AllDone)
)
AllDone ->
let
_ =
Debug.log "yield" "done"
in
( model, Cmd.none )
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_ProcessYieldThrashingTest, 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
yield"A 1" (the test now says "done")"A 1" or "A 2" or "B 1" or "A 3" or "B 2" or "A 4" or "B 3" or "A 5" or "B 4" or "B 5" or "done"t_elm_core_ProcessYieldThrashingTest
yield"A 2" (the test now says "done")"A 1" or "A 2" or "B 1" or "A 3" or "B 2" or "A 4" or "B 3" or "A 5" or "B 4" or "B 5" or "done"t_elm_core_ProcessYieldThrashingTest
yield"A 3" (the test now says "done")"A 1" or "A 2" or "B 1" or "A 3" or "B 2" or "A 4" or "B 3" or "A 5" or "B 4" or "B 5" or "done"t_elm_core_ProcessYieldThrashingTest
yield"A 4" (the test now says "done")"A 1" or "A 2" or "B 1" or "A 3" or "B 2" or "A 4" or "B 3" or "A 5" or "B 4" or "B 5" or "done"t_elm_core_ProcessYieldThrashingTest
yield"A 5" (the test now says "done")"A 1" or "A 2" or "B 1" or "A 3" or "B 2" or "A 4" or "B 3" or "A 5" or "B 4" or "B 5" or "done"t_elm_core_ProcessYieldThrashingTest
yield"B 1" (the test now says "done")"A 1" or "A 2" or "B 1" or "A 3" or "B 2" or "A 4" or "B 3" or "A 5" or "B 4" or "B 5" or "done"t_elm_core_ProcessYieldThrashingTest
yield"B 2" (the test now says "done")"A 1" or "A 2" or "B 1" or "A 3" or "B 2" or "A 4" or "B 3" or "A 5" or "B 4" or "B 5" or "done"t_elm_core_ProcessYieldThrashingTest
yield"B 3" (the test now says "done")"A 1" or "A 2" or "B 1" or "A 3" or "B 2" or "A 4" or "B 3" or "A 5" or "B 4" or "B 5" or "done"t_elm_core_ProcessYieldThrashingTest
yield"B 4" (the test now says "done")"A 1" or "A 2" or "B 1" or "A 3" or "B 2" or "A 4" or "B 3" or "A 5" or "B 4" or "B 5" or "done"t_elm_core_ProcessYieldThrashingTest
yield"B 5" (the test now says "done")"A 1" or "A 2" or "B 1" or "A 3" or "B 2" or "A 4" or "B 3" or "A 5" or "B 4" or "B 5" or "done"t_elm_core_ProcessYieldThrashingTest
yield"done""A 1" or "A 2" or "B 1" or "A 3" or "B 2" or "A 4" or "B 3" or "A 5" or "B 4" or "B 5" or "done"t_elm_core_ProcessYieldThrashingTest
/-- tests/elm-core/src/ProcessYieldThrashingTest.elm -/
def g_elm_core_ProcessYieldThrashingTest : Program := {
  name := "ProcessYieldThrashingTest", source := "tests/elm-core/src", native := true,
  exposing := (.items [.var "main"]), imports := [⟨"Platform", "Platform", none⟩, ⟨"Process", "Process", none⟩, ⟨"Task", "Task", none⟩],
  types := [("Msg", [("Started", 0), ("AllDone", 0)])],
  aliases := [("Model", some [])],
  defs := [
    .fn "iters" [] (i 5),
    .fn "yielder" [pv "label", pv "k"] (ifte (bin ">" (v "k") (v "iters")) (ap (qv "Task" "succeed") [.atexp .unit]) (bin "|>" (ap (qv "Process" "sleep") [.atexp (.scon (.intF 0))]) (ap (qv "Task" "andThen") [.atexp (.paren (lams [pw] (letDefs [.pat pw (ap (qv "Debug" "log") [str "yield", .atexp (.paren (bin "++" (v "label") (bin "++" (str " ") (ap (qv "Debug" "toString") [v "k"]))))])] (ap (v "yielder") [v "label", .atexp (.paren (bin "+" (v "k") (i 1)))]))))]))),
    .fn "init" [pw] (tup2 (recE []) (bin "|>" (bin "|>" (ap (qv "Process" "spawn") [.atexp (.paren (ap (v "yielder") [str "A", i 1]))]) (ap (qv "Task" "andThen") [.atexp (.paren (lams [pw] (ap (qv "Process" "spawn") [.atexp (.paren (ap (v "yielder") [str "B", i 1]))])))])) (ap (qv "Task" "perform") [.atexp (.paren (lams [pw] (c "Started")))]))),
    .fn "update" [pv "msg", pv "model"] (caseOf (v "msg") [(P (pcA "Started"), tup2 (v "model") (bin "|>" (ap (qv "Process" "sleep") [.atexp (.scon (.intF 200))]) (ap (qv "Task" "perform") [.atexp (.paren (lams [pw] (c "AllDone")))]))), (P (pcA "AllDone"), letDefs [.pat pw (ap (qv "Debug" "log") [str "yield", str "done"])] (tup2 (v "model") (qv "Cmd" "none")))]),
    .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 "yield: \"A 1\"" .string,
    chk .check "yield: \"A 2\"" .string,
    chk .check "yield: \"A 3\"" .string,
    chk .check "yield: \"A 4\"" .string,
    chk .check "yield: \"A 5\"" .string,
    chk .check "yield: \"B 1\"" .string,
    chk .check "yield: \"B 2\"" .string,
    chk .check "yield: \"B 3\"" .string,
    chk .check "yield: \"B 4\"" .string,
    chk .check "yield: \"B 5\"" .string,
    chk .check "yield: \"done\"" .string ] }

/-- The worker test of ProcessYieldThrashingTest. -/
def w_elm_core_ProcessYieldThrashingTest : Prog.WorkerTest := {
  prog := g_elm_core_ProcessYieldThrashingTest,
  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_ProcessYieldThrashingTest 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_ProcessYieldThrashingTest 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_ProcessYieldThrashingTest states that the program has a meaning by r:p-program, with both premises, and ident_elm_core_ProcessYieldThrashingTest 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]).