ProcessSpawnRecursiveTest
Tags:elm-spec
Checks, in A Standard Definition of Elm?:rule p-proc-spawnrule p-proc-sleep
Translated into Lean for the mechanisation of the book: see its Lean translation.
A process spawned with `Process.spawn` can itself spawn a process, nested five deep, and every level runs. Level `k` sleeps 10 ms, logs `spawned k`, and then spawns level `k + 1`; level 5 logs `bottom` instead of spawning. `init` spawns level 1, and the program logs `done` after a 300 ms sleep, by which time every level has run.
Results
elm-0.19.1
passran in 348 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.1/optimize for better performance and smaller assets. recurse: "spawned 1" recurse: "spawned 2" recurse: "spawned 3" recurse: "spawned 4" recurse: "spawned 5" recurse: "bottom" recurse: "done"
elm-0.19.2
passran in 343 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.2/optimize for better performance and smaller assets. recurse: "spawned 1" recurse: "spawned 2" recurse: "spawned 3" recurse: "spawned 4" recurse: "spawned 5" recurse: "bottom" recurse: "done"
elm-0.19.3
passran in 343 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.3/optimize for better performance and smaller assets. recurse: "spawned 1" recurse: "spawned 2" recurse: "spawned 3" recurse: "spawned 4" recurse: "spawned 5" recurse: "bottom" recurse: "done"
lamdera-1.5.0
passran in 349 msOutput
recurse: "spawned 1" recurse: "spawned 2" recurse: "spawned 3" recurse: "spawned 4" recurse: "spawned 5" recurse: "bottom" recurse: "done"
Discussions
Discuss this test·Search the discussions on GitHub
Discussions are on GitHub; one about this test has [t:elm-core/ProcessSpawnRecursiveTest] in its title.
Source
The -- CHECK: lines give the expected output.
module ProcessSpawnRecursiveTest exposing (main)
{-| A process spawned with `Process.spawn` can itself spawn a process, nested
five deep, and every level runs.
Level `k` sleeps 10 ms, logs `spawned k`, and then spawns level `k + 1`; level
5 logs `bottom` instead of spawning. `init` spawns level 1, and the program
logs `done` after a 300 ms sleep, by which time every level has run.
-}
-- TAGS: elm-spec, p-proc-spawn, p-proc-sleep
-- CHECK: recurse: "spawned 1"
-- CHECK: recurse: "spawned 2"
-- CHECK: recurse: "spawned 3"
-- CHECK: recurse: "spawned 4"
-- CHECK: recurse: "spawned 5"
-- CHECK: recurse: "bottom"
-- CHECK: recurse: "done"
import Platform
import Process
import Task
type Msg
= Started
| AllDone
type alias Model =
{}
depth : Int
depth =
5
recur : Int -> Task.Task Never ()
recur k =
Process.sleep 10
|> Task.andThen
(\_ ->
let
_ =
Debug.log "recurse" ("spawned " ++ Debug.toString k)
in
if k >= depth then
let
_ =
Debug.log "recurse" "bottom"
in
Task.succeed ()
else
Process.spawn (recur (k + 1))
|> Task.map (\_ -> ())
)
init : () -> ( Model, Cmd Msg )
init _ =
( {}
, Process.spawn (recur 1)
|> Task.perform (\_ -> Started)
)
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
Started ->
( model
, Process.sleep 300
|> Task.perform (\_ -> AllDone)
)
AllDone ->
let
_ =
Debug.log "recurse" "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_ProcessSpawnRecursiveTest, 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.
| Check | Expected (CHECK) | Lean evaluator | Proved by |
|---|---|---|---|
recurse | "spawned 1" (the test now says "done") | "spawned 1" or "spawned 2" or "spawned 3" or "spawned 4" or "spawned 5" or "bottom" or "done" | t_elm_core_ProcessSpawnRecursiveTest |
recurse | "spawned 2" (the test now says "done") | "spawned 1" or "spawned 2" or "spawned 3" or "spawned 4" or "spawned 5" or "bottom" or "done" | t_elm_core_ProcessSpawnRecursiveTest |
recurse | "spawned 3" (the test now says "done") | "spawned 1" or "spawned 2" or "spawned 3" or "spawned 4" or "spawned 5" or "bottom" or "done" | t_elm_core_ProcessSpawnRecursiveTest |
recurse | "spawned 4" (the test now says "done") | "spawned 1" or "spawned 2" or "spawned 3" or "spawned 4" or "spawned 5" or "bottom" or "done" | t_elm_core_ProcessSpawnRecursiveTest |
recurse | "spawned 5" (the test now says "done") | "spawned 1" or "spawned 2" or "spawned 3" or "spawned 4" or "spawned 5" or "bottom" or "done" | t_elm_core_ProcessSpawnRecursiveTest |
recurse | "bottom" (the test now says "done") | "spawned 1" or "spawned 2" or "spawned 3" or "spawned 4" or "spawned 5" or "bottom" or "done" | t_elm_core_ProcessSpawnRecursiveTest |
recurse | "done" | "spawned 1" or "spawned 2" or "spawned 3" or "spawned 4" or "spawned 5" or "bottom" or "done" | t_elm_core_ProcessSpawnRecursiveTest |
/-- tests/elm-core/src/ProcessSpawnRecursiveTest.elm -/
def g_elm_core_ProcessSpawnRecursiveTest : Program := {
name := "ProcessSpawnRecursiveTest", 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 "depth" [] (i 5),
.fn "recur" [pv "k"] (bin "|>" (ap (qv "Process" "sleep") [.atexp (.scon (.intF 10))]) (ap (qv "Task" "andThen") [.atexp (.paren (lams [pw] (letDefs [.pat pw (ap (qv "Debug" "log") [str "recurse", .atexp (.paren (bin "++" (str "spawned ") (ap (qv "Debug" "toString") [v "k"])))])] (ifte (bin ">=" (v "k") (v "depth")) (letDefs [.pat pw (ap (qv "Debug" "log") [str "recurse", str "bottom"])] (ap (qv "Task" "succeed") [.atexp .unit])) (bin "|>" (ap (qv "Process" "spawn") [.atexp (.paren (ap (v "recur") [.atexp (.paren (bin "+" (v "k") (i 1)))]))]) (ap (qv "Task" "map") [.atexp (.paren (lams [pw] (.atexp .unit)))]))))))])),
.fn "init" [pw] (tup2 (recE []) (bin "|>" (ap (qv "Process" "spawn") [.atexp (.paren (ap (v "recur") [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 300))]) (ap (qv "Task" "perform") [.atexp (.paren (lams [pw] (c "AllDone")))]))), (P (pcA "AllDone"), letDefs [.pat pw (ap (qv "Debug" "log") [str "recurse", 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 "recurse: \"spawned 1\"" .string,
chk .check "recurse: \"spawned 2\"" .string,
chk .check "recurse: \"spawned 3\"" .string,
chk .check "recurse: \"spawned 4\"" .string,
chk .check "recurse: \"spawned 5\"" .string,
chk .check "recurse: \"bottom\"" .string,
chk .check "recurse: \"done\"" .string ] }
/-- The worker test of ProcessSpawnRecursiveTest. -/
def w_elm_core_ProcessSpawnRecursiveTest : Prog.WorkerTest := {
prog := g_elm_core_ProcessSpawnRecursiveTest,
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_ProcessSpawnRecursiveTest 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_ProcessSpawnRecursiveTest 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_ProcessSpawnRecursiveTest states that the program has a meaning by r:p-program, with both premises, and ident_elm_core_ProcessSpawnRecursiveTest 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]).