PortEchoStringTest
Tags:elm-spec
Checks, in A Standard Definition of Elm?:rule e-enc-stringrule e-dec-string
Translated into Lean for the mechanisation of the book: see its Lean translation.
A `String` sent out through a port comes back unchanged through an incoming port, for each of six sample values. The program assumes a host that passes every value sent out on the `echoOut` port straight back in on the `echoIn` port; without one, nothing after `init` is logged. It sends the values in `values` one at a time, sending the next only after the previous one has come back, logs each value received as the pair of whether it equals the value sent (kept in the model as `sent`) and the value itself, logs `"done"` after the last one, and then drops its subscription. The values are the empty string, plain ASCII, a string with a double quote and a backslash, a string with a newline and a tab, a string with a Latin-1 character and a character outside the Basic Multilingual Plane (U+1F600), and a string containing a NUL character. The equality shows that each comes back unchanged, the non-BMP character included.
Results
elm-0.19.1
passran in 69 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.1/optimize for better performance and smaller assets. PortEchoStringTest got: (True,"") PortEchoStringTest got: (True,"hello") PortEchoStringTest got: (True,"quote \" back\\slash") PortEchoStringTest got: (True,"line\nbreak\ttab") PortEchoStringTest got: (True,"café 😀") PortEchoStringTest got: (True,"nul\0inside") PortEchoStringTest: "done"
elm-0.19.2
passran in 53 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.2/optimize for better performance and smaller assets. PortEchoStringTest got: (True,"") PortEchoStringTest got: (True,"hello") PortEchoStringTest got: (True,"quote \" back\\slash") PortEchoStringTest got: (True,"line\nbreak\ttab") PortEchoStringTest got: (True,"café 😀") PortEchoStringTest got: (True,"nul\0inside") PortEchoStringTest: "done"
elm-0.19.3
passran in 52 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.3/optimize for better performance and smaller assets. PortEchoStringTest got: (True,"") PortEchoStringTest got: (True,"hello") PortEchoStringTest got: (True,"quote \" back\\slash") PortEchoStringTest got: (True,"line\nbreak\ttab") PortEchoStringTest got: (True,"café 😀") PortEchoStringTest got: (True,"nul\0inside") PortEchoStringTest: "done"
lamdera-1.5.0
passran in 57 msOutput
PortEchoStringTest got: (True,"") PortEchoStringTest got: (True,"hello") PortEchoStringTest got: (True,"quote \" back\\slash") PortEchoStringTest got: (True,"line\nbreak\ttab") PortEchoStringTest got: (True,"café 😀") PortEchoStringTest got: (True,"nul\0inside") PortEchoStringTest: "done"
Discussions
Discuss this test·Search the discussions on GitHub
Discussions are on GitHub; one about this test has [t:elm/PortEchoStringTest] in its title.
Source
The -- CHECK: lines give the expected output.
port module PortEchoStringTest exposing (main)
{-| A `String` sent out through a port comes back unchanged through an incoming
port, for each of six sample values.
The program assumes a host that passes every value sent out on the `echoOut`
port straight back in on the `echoIn` port; without one, nothing after `init`
is logged. It sends the values in `values` one at a time, sending the next only
after the previous one has come back, logs each value received as the pair of
whether it equals the value sent (kept in the model as `sent`) and the value
itself, logs `"done"` after the last one, and then drops its subscription.
The values are the empty string, plain ASCII, a string with a double quote and
a backslash, a string with a newline and a tab, a string with a Latin-1
character and a character outside the Basic Multilingual Plane (U+1F600), and
a string containing a NUL character. The equality shows that each comes back
unchanged, the non-BMP character included.
-}
-- TAGS: elm-spec, e-enc-string, e-dec-string
-- CHECK: PortEchoStringTest got: (True,"")
-- CHECK: PortEchoStringTest got: (True,"hello")
-- CHECK: PortEchoStringTest got: (True,"quote \" back\\slash")
-- CHECK: PortEchoStringTest got: (True,"line\nbreak\ttab")
-- CHECK: PortEchoStringTest got: (True,"café 😀")
-- CHECK: PortEchoStringTest got: (True,"nul\0inside")
-- CHECK: PortEchoStringTest: "done"
import Platform
port echoOut : String -> Cmd msg
port echoIn : (String -> msg) -> Sub msg
values : List (String)
values =
[ ""
, "hello"
, "quote \" back\\slash"
, "line\nbreak\ttab"
, "caf\u{00E9} \u{1F600}"
, "nul\u{0000}inside"
]
type Msg
= Got (String)
type alias Model =
{ sent : String, remaining : List (String), done : Bool }
init : () -> ( Model, Cmd Msg )
init _ =
case values of
first :: rest ->
( { sent = first, remaining = rest, done = False }, echoOut first )
[] ->
( { sent = "", remaining = [], done = True }, Cmd.none )
update : Msg -> Model -> ( Model, Cmd Msg )
update (Got v) model =
let
_ =
Debug.log "PortEchoStringTest got" ( v == model.sent, v )
in
case model.remaining of
next :: rest ->
( { model | sent = next, remaining = rest }, echoOut next )
[] ->
let
_ =
Debug.log "PortEchoStringTest" "done"
in
( { model | done = True }, Cmd.none )
subscriptions : Model -> Sub Msg
subscriptions model =
if model.done then
Sub.none
else
echoIn Got
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_PortEchoStringTest, 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.
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 |
|---|---|---|---|
PortEchoStringTest got | (True,"") (the test now says (True,"nul\0inside")) | (True,"") or (True,"hello") or (True,"quote \" back\\slash") or (True,"line\nbreak\ttab") or (True,"café 😀") or (True,"nul\0inside") | t_elm_PortEchoStringTest |
PortEchoStringTest got | (True,"hello") (the test now says (True,"nul\0inside")) | (True,"") or (True,"hello") or (True,"quote \" back\\slash") or (True,"line\nbreak\ttab") or (True,"café 😀") or (True,"nul\0inside") | t_elm_PortEchoStringTest |
PortEchoStringTest got | (True,"quote \" back\\slash") (the test now says (True,"nul\0inside")) | (True,"") or (True,"hello") or (True,"quote \" back\\slash") or (True,"line\nbreak\ttab") or (True,"café 😀") or (True,"nul\0inside") | t_elm_PortEchoStringTest |
PortEchoStringTest got | (True,"line\nbreak\ttab") (the test now says (True,"nul\0inside")) | (True,"") or (True,"hello") or (True,"quote \" back\\slash") or (True,"line\nbreak\ttab") or (True,"café 😀") or (True,"nul\0inside") | t_elm_PortEchoStringTest |
PortEchoStringTest got | (True,"café 😀") (the test now says (True,"nul\0inside")) | (True,"") or (True,"hello") or (True,"quote \" back\\slash") or (True,"line\nbreak\ttab") or (True,"café 😀") or (True,"nul\0inside") | t_elm_PortEchoStringTest |
PortEchoStringTest got | (True,"nul\0inside") | (True,"") or (True,"hello") or (True,"quote \" back\\slash") or (True,"line\nbreak\ttab") or (True,"café 😀") or (True,"nul\0inside") | t_elm_PortEchoStringTest |
PortEchoStringTest | "done" | "done" | t_elm_PortEchoStringTest |
/-- tests/elm/src/PortEchoStringTest.elm -/
def g_elm_PortEchoStringTest : Program := {
name := "PortEchoStringTest", source := "tests/elm/src",
exposing := (.items [.var "main"]), imports := [⟨"Platform", "Platform", none⟩],
types := [("Msg", [("Got", 1)])],
aliases := [("Model", some ["sent", "remaining", "done"])],
ports := [("echoOut", .out, .string), ("echoIn", .inn, .string)],
defs := [
.fn "values" [] (listE [str "", str "hello", str "quote \" back\\slash", str "line\nbreak\ttab", str "café 😀", str "nul\x00inside"]),
.fn "init" [pw] (caseOf (v "values") [(.cons (P (pv "first")) (P (pv "rest")), tup2 (recE [("sent", v "first"), ("remaining", v "rest"), ("done", c "False")]) (ap (v "echoOut") [v "first"])), (P (listP []), tup2 (recE [("sent", str ""), ("remaining", listE []), ("done", c "True")]) (qv "Cmd" "none"))]),
.fn "update" [.paren (pcon "Got" [pv "v"]), pv "model"] (letDefs [.pat pw (ap (qv "Debug" "log") [str "PortEchoStringTest got", tup2 (bin "==" (v "v") (.atexp (access (A (v "model")) "sent"))) (v "v")])] (caseOf (.atexp (access (A (v "model")) "remaining")) [(.cons (P (pv "next")) (P (pv "rest")), tup2 (updE "model" [("sent", v "next"), ("remaining", v "rest")]) (ap (v "echoOut") [v "next"])), (P (listP []), letDefs [.pat pw (ap (qv "Debug" "log") [str "PortEchoStringTest", str "done"])] (tup2 (updE "model" [("done", c "True")]) (qv "Cmd" "none")))])),
.fn "subscriptions" [pv "model"] (ifte (.atexp (access (A (v "model")) "done")) (qv "Sub" "none") (ap (v "echoIn") [c "Got"])),
.fn "main" [] (ap (qv "Platform" "worker") [recE [("init", v "init"), ("update", v "update"), ("subscriptions", v "subscriptions")]]) ],
checks := [
chk .check "PortEchoStringTest got: (True,\"\")" .data,
chk .check "PortEchoStringTest got: (True,\"hello\")" .data,
chk .check "PortEchoStringTest got: (True,\"quote \\\" back\\\\slash\")" .data,
chk .check "PortEchoStringTest got: (True,\"line\\nbreak\\ttab\")" .data,
chk .check "PortEchoStringTest got: (True,\"café 😀\")" .data,
chk .check "PortEchoStringTest got: (True,\"nul\\0inside\")" .data,
chk .check "PortEchoStringTest: \"done\"" .string ] }
/-- The worker test of PortEchoStringTest. -/
def w_elm_PortEchoStringTest : Prog.WorkerTest := {
prog := g_elm_PortEchoStringTest,
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_PortEchoStringTest 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_PortEchoStringTest 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_PortEchoStringTest states that the program has a meaning by r:p-program, with both premises, and ident_elm_PortEchoStringTest that its specialisation is unique and is the reduced program above, both with no hypothesis about principality.