Elm Exactly
Results / elm-core /

CoreIssue1154PortSubscribedWithCmdTest

Tags:elm19-bugelm-spec

Checks, in A Standard Definition of Elm?:rule p-updaterule p-outrule p-in-acceptrule e-msg-port

Translated into Lean for the mechanisation of the book: see its Lean translation.

elm-0.19.1 failelm-0.19.2 failelm-0.19.3 faillamdera-1.5.0 fail

Issue: elm/core#1154

A port subscription added by the same `update` that sends a command to an outgoing port misses
the outgoing port's synchronous reply, depending on the order of the ports in the generated code.

<https://github.com/elm/core/issues/1154>

`update` returns `echoOut "hello"` and a model whose `subscriptions` are `echoIn Got`. The host
(the test runner) answers every `echoOut` value synchronously through `echoIn`.
In Elm 0.19.1 the reply is dropped and `Got` never arrives, because the command is dispatched before the new subscription is in place
(this depends on `echoOut` also being used in `init`).
Subscriptions returned together with a command must be active when the command runs.

Results

elm-0.19.1

failran in 85 ms
Missing pattern: got: "hello"
--- output ---
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.1/optimize for better performance and smaller assets.

elm-0.19.2

failran in 67 ms
Missing pattern: got: "hello"
--- output ---
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.2/optimize for better performance and smaller assets.

elm-0.19.3

failran in 83 ms
Missing pattern: got: "hello"
--- output ---
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.3/optimize for better performance and smaller assets.

lamdera-1.5.0

failran in 85 ms
Missing pattern: got: "hello"
--- output ---

Discussions

Discuss this test·Search the discussions on GitHub

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

Source

The -- CHECK: lines give the expected output.

port module CoreIssue1154PortSubscribedWithCmdTest exposing (main)
{-| A port subscription added by the same `update` that sends a command to an outgoing port misses
the outgoing port's synchronous reply, depending on the order of the ports in the generated code.
<https://github.com/elm/core/issues/1154>
`update` returns `echoOut "hello"` and a model whose `subscriptions` are `echoIn Got`. The host
(the test runner) answers every `echoOut` value synchronously through `echoIn`.
In Elm 0.19.1 the reply is dropped and `Got` never arrives, because the command is dispatched before the new subscription is in place
(this depends on `echoOut` also being used in `init`).
Subscriptions returned together with a command must be active when the command runs.
-}
-- TAGS: elm19-bug
-- TAGS: elm-spec, p-update, p-out, p-in-accept, e-msg-port
-- CHECK: got: "hello"
import Process
import Task
port echoOut : String -> Cmd msg
port echoIn : (String -> msg) -> Sub msg
type Msg
= Start
| Got String
init : () -> ( Bool, Cmd Msg )
init _ =
( False
, Cmd.batch
[ echoOut "init"
, Task.perform (always Start) (Process.sleep 10)
]
)
update : Msg -> Bool -> ( Bool, Cmd Msg )
update msg listening =
case msg of
Start ->
( True, echoOut "hello" )
Got s ->
let
_ =
Debug.log "got" s
in
( listening, Cmd.none )
subscriptions : Bool -> Sub Msg
subscriptions listening =
if listening then
echoIn Got
else
Sub.none
main : Program () Bool 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_CoreIssue1154PortSubscribedWithCmdTest, 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 the test runner's echo host, which sends every value delivered on echoOut back on echoIn, in order. 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
got"hello""hello"t_elm_core_CoreIssue1154PortSubscribedWithCmdTest
/-- tests/elm-core/src/CoreIssue1154PortSubscribedWithCmdTest.elm -/
def g_elm_core_CoreIssue1154PortSubscribedWithCmdTest : Program := {
  name := "CoreIssue1154PortSubscribedWithCmdTest", source := "tests/elm-core/src",
  exposing := (.items [.var "main"]), imports := [⟨"Process", "Process", none⟩, ⟨"Task", "Task", none⟩],
  types := [("Msg", [("Start", 0), ("Got", 1)])],
  aliases := [],
  ports := [("echoOut", .out, .string), ("echoIn", .inn, .string)],
  defs := [
    .fn "init" [pw] (tup2 (c "False") (ap (qv "Cmd" "batch") [listE [ap (v "echoOut") [str "init"], ap (qv "Task" "perform") [.atexp (.paren (ap (v "always") [c "Start"])), .atexp (.paren (ap (qv "Process" "sleep") [.atexp (.scon (.intF 10))]))]]])),
    .fn "update" [pv "msg", pv "listening"] (caseOf (v "msg") [(P (pcA "Start"), tup2 (c "True") (ap (v "echoOut") [str "hello"])), (pcon "Got" [pv "s"], letDefs [.pat pw (ap (qv "Debug" "log") [str "got", v "s"])] (tup2 (v "listening") (qv "Cmd" "none")))]),
    .fn "subscriptions" [pv "listening"] (ifte (v "listening") (ap (v "echoIn") [c "Got"]) (qv "Sub" "none")),
    .fn "main" [] (ap (qv "Platform" "worker") [recE [("init", v "init"), ("update", v "update"), ("subscriptions", v "subscriptions")]]) ],
  checks := [
    chk .check "got: \"hello\"" .string ] }

/-- The worker test of CoreIssue1154PortSubscribedWithCmdTest. -/
def w_elm_core_CoreIssue1154PortSubscribedWithCmdTest : Prog.WorkerTest := {
  prog := g_elm_core_CoreIssue1154PortSubscribedWithCmdTest,
  flagsType := .unit,
  flags := .null,
  host := .echo "echoOut" "echoIn",
  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_CoreIssue1154PortSubscribedWithCmdTest 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_CoreIssue1154PortSubscribedWithCmdTest 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_CoreIssue1154PortSubscribedWithCmdTest states that the program has a meaning by r:p-program, with both premises, and ident_elm_core_CoreIssue1154PortSubscribedWithCmdTest that its specialisation is unique and is the reduced program above, both with no hypothesis about principality.