PortAliasEchoTest
Tags:elm-spec
Checks, in A Standard Definition of Elm?:rule sm-topdec-portoutrule s-ty-conrule e-enc-recordrule e-dec-record
Translated into Lean for the mechanisation of the book: see its Lean translation.
An outgoing port whose type is written through a type alias,
`port echoOut : Out msg` with `type alias Out msg = Payload -> Cmd msg`, sends
its record payload, and the record comes back unchanged through an incoming
port.
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 `{ n = 42, s = "hi" }` once, logs the record received and
then `"ok"`, and drops its subscription.Results
elm-0.19.1
passran in 56 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.1/optimize for better performance and smaller assets.
PortAliasEchoTest got: { n = 42, s = "hi" }
PortAliasEchoTest done: "ok"
elm-0.19.2
passran in 76 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.2/optimize for better performance and smaller assets.
PortAliasEchoTest got: { n = 42, s = "hi" }
PortAliasEchoTest done: "ok"
elm-0.19.3
passran in 57 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.3/optimize for better performance and smaller assets.
PortAliasEchoTest got: { n = 42, s = "hi" }
PortAliasEchoTest done: "ok"
lamdera-1.5.0
passran in 63 msOutput
PortAliasEchoTest got: { n = 42, s = "hi" }
PortAliasEchoTest done: "ok"
Discussions
Discuss this test·Search the discussions on GitHub
Discussions are on GitHub; one about this test has [t:elm/PortAliasEchoTest] in its title.
Source
The -- CHECK: lines give the expected output.
port module PortAliasEchoTest exposing (main)
{-| An outgoing port whose type is written through a type alias,
`port echoOut : Out msg` with `type alias Out msg = Payload -> Cmd msg`, sends
its record payload, and the record comes back unchanged through an incoming
port.
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 `{ n = 42, s = "hi" }` once, logs the record received and
then `"ok"`, and drops its subscription.
-}
-- TAGS: elm-spec, sm-topdec-portout, s-ty-con, e-enc-record, e-dec-record
-- CHECK: PortAliasEchoTest got: { n = 42, s = "hi" }
-- CHECK: PortAliasEchoTest done: "ok"
import Platform
type alias Payload =
{ n : Int, s : String }
type alias Out msg =
Payload -> Cmd msg
port echoOut : Out msg
port echoIn : (Payload -> msg) -> Sub msg
type Msg
= Got Payload
type alias Model =
{ received : Bool }
init : () -> ( Model, Cmd Msg )
init _ =
( { received = False }, echoOut { n = 42, s = "hi" } )
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
Got payload ->
let
_ =
Debug.log "PortAliasEchoTest got" payload
_ =
Debug.log "PortAliasEchoTest done" "ok"
in
( { model | received = True }, Cmd.none )
subscriptions : Model -> Sub Msg
subscriptions model =
if model.received 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_PortAliasEchoTest, 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.
| Check | Expected (CHECK) | Lean evaluator | Proved by |
|---|---|---|---|
PortAliasEchoTest got | { n = 42, s = "hi" } | { n = 42, s = "hi" } | t_elm_PortAliasEchoTest |
PortAliasEchoTest done | "ok" | "ok" | t_elm_PortAliasEchoTest |
/-- tests/elm/src/PortAliasEchoTest.elm -/
def g_elm_PortAliasEchoTest : Program := {
name := "PortAliasEchoTest", source := "tests/elm/src",
exposing := (.items [.var "main"]), imports := [⟨"Platform", "Platform", none⟩],
types := [("Msg", [("Got", 1)])],
aliases := [("Model", some ["received"]), ("Out", none), ("Payload", some ["n", "s"])],
ports := [("echoOut", .out, (.record (.cons "n" .int (.cons "s" .string .nil)))), ("echoIn", .inn, (.record (.cons "n" .int (.cons "s" .string .nil))))],
defs := [
.fn "init" [pw] (tup2 (recE [("received", c "False")]) (ap (v "echoOut") [recE [("n", i 42), ("s", str "hi")]])),
.fn "update" [pv "msg", pv "model"] (caseOf (v "msg") [(pcon "Got" [pv "payload"], letDefs [.pat pw (ap (qv "Debug" "log") [str "PortAliasEchoTest got", v "payload"]), .pat pw (ap (qv "Debug" "log") [str "PortAliasEchoTest done", str "ok"])] (tup2 (updE "model" [("received", c "True")]) (qv "Cmd" "none")))]),
.fn "subscriptions" [pv "model"] (ifte (.atexp (access (A (v "model")) "received")) (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 "PortAliasEchoTest got: { n = 42, s = \"hi\" }" .data,
chk .check "PortAliasEchoTest done: \"ok\"" .string ] }
/-- The worker test of PortAliasEchoTest. -/
def w_elm_PortAliasEchoTest : Prog.WorkerTest := {
prog := g_elm_PortAliasEchoTest,
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_PortAliasEchoTest 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_PortAliasEchoTest 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_PortAliasEchoTest states that the program has a meaning by r:p-program, with both premises, and ident_elm_PortAliasEchoTest that its specialisation is unique and is the reduced program above, both with no hypothesis about principality.