Elm Exactly
Results / elm-core /

ProcessSpawnKillHalfTest

Tags:elm-spec

Checks, in A Standard Definition of Elm?:rule p-proc-spawnrule p-proc-killrule p-proc-sleep

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 process killed with `Process.kill` before its sleep finishes never runs
the rest of its task, while the processes left alive do.

Ten processes are spawned, numbered 1 to 10; process `i` sleeps `i * 10` ms and
then logs `fired i`. Straight after spawning them all, the even-numbered ones
are killed. The program then sleeps 300 ms, longer than the slowest process,
and logs `done`. Only the odd-numbered processes are expected to log, followed
by `done`.

Results

elm-0.19.1

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

elm-0.19.2

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

elm-0.19.3

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

lamdera-1.5.0

passran in 334 ms
Output
spawnKill: "fired 1"
spawnKill: "fired 3"
spawnKill: "fired 5"
spawnKill: "fired 7"
spawnKill: "fired 9"
spawnKill: "done"

Discussions

Discuss this test·Search the discussions on GitHub

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

Source

The -- CHECK: lines give the expected output.

module ProcessSpawnKillHalfTest exposing (main)
{-| A process killed with `Process.kill` before its sleep finishes never runs
the rest of its task, while the processes left alive do.
Ten processes are spawned, numbered 1 to 10; process `i` sleeps `i * 10` ms and
then logs `fired i`. Straight after spawning them all, the even-numbered ones
are killed. The program then sleeps 300 ms, longer than the slowest process,
and logs `done`. Only the odd-numbered processes are expected to log, followed
by `done`.
-}
-- TAGS: elm-spec, p-proc-spawn, p-proc-kill, p-proc-sleep
-- CHECK: spawnKill: "fired 1"
-- CHECK: spawnKill: "fired 3"
-- CHECK: spawnKill: "fired 5"
-- CHECK: spawnKill: "fired 7"
-- CHECK: spawnKill: "fired 9"
-- CHECK: spawnKill: "done"
import Platform
import Process
import Task
type Msg
= Spawned (List Process.Id)
| AllDone
type alias Model =
{ ids : List Process.Id }
fiberCount : Int
fiberCount =
10
worker : Int -> Task.Task Never ()
worker i =
Process.sleep (toFloat (i * 10))
|> Task.andThen
(\_ ->
let
_ =
Debug.log "spawnKill" ("fired " ++ Debug.toString i)
in
Task.succeed ()
)
spawnAll : Task.Task Never (List Process.Id)
spawnAll =
let
go k acc =
if k > fiberCount then
Task.succeed acc
else
Process.spawn (worker k)
|> Task.andThen (\id -> go (k + 1) (acc ++ [ id ]))
in
go 1 []
killEven : List Process.Id -> Task.Task Never ()
killEven ids =
let
go k remaining =
case remaining of
[] ->
Task.succeed ()
id :: rest ->
if modBy 2 k == 0 then
Process.kill id
|> Task.andThen (\_ -> go (k + 1) rest)
else
go (k + 1) rest
in
go 1 ids
init : () -> ( Model, Cmd Msg )
init _ =
( { ids = [] }
, spawnAll
|> Task.andThen (\ids -> killEven ids |> Task.map (\_ -> ids))
|> Task.perform Spawned
)
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
Spawned ids ->
( { model | ids = ids }
, Process.sleep 300
|> Task.perform (\_ -> AllDone)
)
AllDone ->
let
_ =
Debug.log "spawnKill" "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_ProcessSpawnKillHalfTest, 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
spawnKill"fired 1" (the test now says "done")"fired 1" or "fired 3" or "fired 5" or "fired 7" or "fired 9" or "done"t_elm_core_ProcessSpawnKillHalfTest
spawnKill"fired 3" (the test now says "done")"fired 1" or "fired 3" or "fired 5" or "fired 7" or "fired 9" or "done"t_elm_core_ProcessSpawnKillHalfTest
spawnKill"fired 5" (the test now says "done")"fired 1" or "fired 3" or "fired 5" or "fired 7" or "fired 9" or "done"t_elm_core_ProcessSpawnKillHalfTest
spawnKill"fired 7" (the test now says "done")"fired 1" or "fired 3" or "fired 5" or "fired 7" or "fired 9" or "done"t_elm_core_ProcessSpawnKillHalfTest
spawnKill"fired 9" (the test now says "done")"fired 1" or "fired 3" or "fired 5" or "fired 7" or "fired 9" or "done"t_elm_core_ProcessSpawnKillHalfTest
spawnKill"done""fired 1" or "fired 3" or "fired 5" or "fired 7" or "fired 9" or "done"t_elm_core_ProcessSpawnKillHalfTest
/-- tests/elm-core/src/ProcessSpawnKillHalfTest.elm -/
def g_elm_core_ProcessSpawnKillHalfTest : Program := {
  name := "ProcessSpawnKillHalfTest", source := "tests/elm-core/src", native := true,
  exposing := (.items [.var "main"]), imports := [⟨"Platform", "Platform", none⟩, ⟨"Process", "Process", none⟩, ⟨"Task", "Task", none⟩],
  types := [("Msg", [("Spawned", 1), ("AllDone", 0)])],
  aliases := [("Model", some ["ids"])],
  defs := [
    .fn "fiberCount" [] (i 10),
    .fn "worker" [pv "i"] (bin "|>" (ap (qv "Process" "sleep") [.atexp (.paren (ap (v "toFloat") [.atexp (.paren (bin "*" (v "i") (i 10)))]))]) (ap (qv "Task" "andThen") [.atexp (.paren (lams [pw] (letDefs [.pat pw (ap (qv "Debug" "log") [str "spawnKill", .atexp (.paren (bin "++" (str "fired ") (ap (qv "Debug" "toString") [v "i"])))])] (ap (qv "Task" "succeed") [.atexp .unit]))))])),
    .fn "spawnAll" [] (letDefs [.fn "go" [pv "k", pv "acc"] (ifte (bin ">" (v "k") (v "fiberCount")) (ap (qv "Task" "succeed") [v "acc"]) (bin "|>" (ap (qv "Process" "spawn") [.atexp (.paren (ap (v "worker") [v "k"]))]) (ap (qv "Task" "andThen") [.atexp (.paren (lams [pv "id"] (ap (v "go") [.atexp (.paren (bin "+" (v "k") (i 1))), .atexp (.paren (bin "++" (v "acc") (listE [v "id"])))])))])))] (ap (v "go") [i 1, listE []])),
    .fn "killEven" [pv "ids"] (letDefs [.fn "go" [pv "k", pv "remaining"] (caseOf (v "remaining") [(P (listP []), ap (qv "Task" "succeed") [.atexp .unit]), (.cons (P (pv "id")) (P (pv "rest")), ifte (bin "==" (ap (v "modBy") [i 2, v "k"]) (i 0)) (bin "|>" (ap (qv "Process" "kill") [v "id"]) (ap (qv "Task" "andThen") [.atexp (.paren (lams [pw] (ap (v "go") [.atexp (.paren (bin "+" (v "k") (i 1))), v "rest"])))])) (ap (v "go") [.atexp (.paren (bin "+" (v "k") (i 1))), v "rest"]))])] (ap (v "go") [i 1, v "ids"])),
    .fn "init" [pw] (tup2 (recE [("ids", listE [])]) (bin "|>" (bin "|>" (v "spawnAll") (ap (qv "Task" "andThen") [.atexp (.paren (lams [pv "ids"] (bin "|>" (ap (v "killEven") [v "ids"]) (ap (qv "Task" "map") [.atexp (.paren (lams [pw] (v "ids")))]))))])) (ap (qv "Task" "perform") [c "Spawned"]))),
    .fn "update" [pv "msg", pv "model"] (caseOf (v "msg") [(pcon "Spawned" [pv "ids"], tup2 (updE "model" [("ids", v "ids")]) (bin "|>" (ap (qv "Process" "sleep") [.atexp (.scon (.intF 300))]) (ap (qv "Task" "perform") [.atexp (.paren (lams [pw] (c "AllDone")))]))), (P (pcA "AllDone"), letDefs [.pat pw (ap (qv "Debug" "log") [str "spawnKill", 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 "spawnKill: \"fired 1\"" .string,
    chk .check "spawnKill: \"fired 3\"" .string,
    chk .check "spawnKill: \"fired 5\"" .string,
    chk .check "spawnKill: \"fired 7\"" .string,
    chk .check "spawnKill: \"fired 9\"" .string,
    chk .check "spawnKill: \"done\"" .string ] }

/-- The worker test of ProcessSpawnKillHalfTest. -/
def w_elm_core_ProcessSpawnKillHalfTest : Prog.WorkerTest := {
  prog := g_elm_core_ProcessSpawnKillHalfTest,
  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_ProcessSpawnKillHalfTest 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_ProcessSpawnKillHalfTest 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_ProcessSpawnKillHalfTest states that the program has a meaning by r:p-program, with both premises, and ident_elm_core_ProcessSpawnKillHalfTest 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]).