Elm Exactly
Results / elm /

PortEchoJsonValueTest

Tags:elm-spec

Checks, in A Standard Definition of Elm?:rule e-enc-valuerule e-dec-value

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
JSON `Value`s received as flags are sent out through a port as they are, and come back
through an incoming port decoded at a concrete type.

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.

The flags are a JSON object whose four fields are decoded at `Json.Encode.Value`: an object
holding an array and a string, a JSON string, a JSON number (2.5), and `null`. Every JSON value
decodes at `Value`, unchanged. `init` sends the four out together, as a triple whose last
component is a pair, and a `Value` is sent as the JSON value it is, so the host receives
`[{"a":[1,2,null],"b":"x"},"plain",[2.5,null]]`. That comes back on `echoIn`, decoded at a
type that reads every part: a record, a string, and a pair of a `Float` and a `Maybe Int`. The
program logs it and drops its subscription.

A Standard Definition of Elm? gives `Value` no functions within the definition, and does not fix
what `Debug.log` prints for one, so the Values are observed through the type they decode at.

Results

elm-0.19.1

passran in 66 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.1/optimize for better performance and smaller assets.
PortEchoJsonValueTest got: ({ a = [Just 1,Just 2,Nothing], b = "x" },"plain",(2.5,Nothing))

elm-0.19.2

passran in 58 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.2/optimize for better performance and smaller assets.
PortEchoJsonValueTest got: ({ a = [Just 1,Just 2,Nothing], b = "x" },"plain",(2.5,Nothing))

elm-0.19.3

passran in 53 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.3/optimize for better performance and smaller assets.
PortEchoJsonValueTest got: ({ a = [Just 1,Just 2,Nothing], b = "x" },"plain",(2.5,Nothing))

lamdera-1.5.0

passran in 44 ms
Output
PortEchoJsonValueTest got: ({ a = [Just 1,Just 2,Nothing], b = "x" },"plain",(2.5,Nothing))

Discussions

Discuss this test·Search the discussions on GitHub

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

Source

The -- CHECK: lines give the expected output.

port module PortEchoJsonValueTest exposing (main)
{-| JSON `Value`s received as flags are sent out through a port as they are, and come back
through an incoming port decoded at a concrete type.
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.
The flags are a JSON object whose four fields are decoded at `Json.Encode.Value`: an object
holding an array and a string, a JSON string, a JSON number (2.5), and `null`. Every JSON value
decodes at `Value`, unchanged. `init` sends the four out together, as a triple whose last
component is a pair, and a `Value` is sent as the JSON value it is, so the host receives
`[{"a":[1,2,null],"b":"x"},"plain",[2.5,null]]`. That comes back on `echoIn`, decoded at a
type that reads every part: a record, a string, and a pair of a `Float` and a `Maybe Int`. The
program logs it and drops its subscription.
A Standard Definition of Elm? gives `Value` no functions within the definition, and does not fix
what `Debug.log` prints for one, so the Values are observed through the type they decode at.
-}
-- TAGS: elm-spec, e-enc-value, e-dec-value
-- FLAGS: {"obj":{"a":[1,2,null],"b":"x"},"str":"plain","num":2.5,"nul":null}
-- CHECK: PortEchoJsonValueTest got: ({ a = [Just 1,Just 2,Nothing], b = "x" },"plain",(2.5,Nothing))
import Json.Encode exposing (Value)
import Platform
port echoOut : ( Value, Value, ( Value, Value ) ) -> Cmd msg
port echoIn : (( { a : List (Maybe Int), b : String }, String, ( Float, Maybe Int ) ) -> msg) -> Sub msg
type alias Flags =
{ obj : Value, str : Value, num : Value, nul : Value }
type Msg
= Got ( { a : List (Maybe Int), b : String }, String, ( Float, Maybe Int ) )
init : Flags -> ( Bool, Cmd Msg )
init flags =
( False, echoOut ( flags.obj, flags.str, ( flags.num, flags.nul ) ) )
update : Msg -> Bool -> ( Bool, Cmd Msg )
update (Got v) _ =
let
_ =
Debug.log "PortEchoJsonValueTest got" v
in
( True, Cmd.none )
subscriptions : Bool -> Sub Msg
subscriptions done =
if done then
Sub.none
else
echoIn Got
main : Program Flags 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_PortEchoJsonValueTest, in StandardElmMech/Generated/Worker01.lean). Lean evaluates its modules by the rules of Chapter 7, starts it by r:p-init with the flags {"obj":{"a":[1,2,null],"b":"x"},"str":"plain","num":2.5,"nul":null}, 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
PortEchoJsonValueTest got({ a = [Just 1,Just 2,Nothing], b = "x" },"plain",(2.5,Nothing))({ a = [Just 1,Just 2,Nothing], b = "x" },"plain",(2.5,Nothing))t_elm_PortEchoJsonValueTest
/-- tests/elm/src/PortEchoJsonValueTest.elm -/
def g_elm_PortEchoJsonValueTest : Program := {
  name := "PortEchoJsonValueTest", source := "tests/elm/src",
  exposing := (.items [.var "main"]), imports := [⟨"Json.Encode", "Json.Encode", some (.items [.tycon "Value"])⟩, ⟨"Platform", "Platform", none⟩],
  types := [("Msg", [("Got", 1)])],
  aliases := [("Flags", some ["obj", "str", "num", "nul"])],
  ports := [("echoOut", .out, (.tuple3 .value .value (.tuple2 .value .value))), ("echoIn", .inn, (.tuple3 (.record (.cons "a" (.list (.maybe .int)) (.cons "b" .string .nil))) .string (.tuple2 .float (.maybe .int))))],
  defs := [
    .fn "init" [pv "flags"] (tup2 (c "False") (ap (v "echoOut") [tup3 (.atexp (access (A (v "flags")) "obj")) (.atexp (access (A (v "flags")) "str")) (tup2 (.atexp (access (A (v "flags")) "num")) (.atexp (access (A (v "flags")) "nul")))])),
    .fn "update" [.paren (pcon "Got" [pv "v"]), pw] (letDefs [.pat pw (ap (qv "Debug" "log") [str "PortEchoJsonValueTest got", v "v"])] (tup2 (c "True") (qv "Cmd" "none"))),
    .fn "subscriptions" [pv "done"] (ifte (v "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 "PortEchoJsonValueTest got: ({ a = [Just 1,Just 2,Nothing], b = \"x\" },\"plain\",(2.5,Nothing))" .data ] }

/-- The worker test of PortEchoJsonValueTest. -/
def w_elm_PortEchoJsonValueTest : Prog.WorkerTest := {
  prog := g_elm_PortEchoJsonValueTest,
  flagsType := (.record (.cons "nul" .value (.cons "num" .value (.cons "obj" .value (.cons "str" .value .nil))))),
  flags := (Eff.BVal.mkObj [("obj", (Eff.BVal.mkObj [("a", (Eff.BVal.array [(.num (Eff.jnum (1) (0))), (.num (Eff.jnum (2) (0))), .null])), ("b", (.str "x"))])), ("str", (.str "plain")), ("num", (.num (Eff.jnum (25) (-1)))), ("nul", .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_PortEchoJsonValueTest 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_PortEchoJsonValueTest 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_PortEchoJsonValueTest states that the program has a meaning by r:p-program, with both premises, and ident_elm_PortEchoJsonValueTest that its specialisation is unique and is the reduced program above, both with no hypothesis about principality.