Elm Exactly
Results / elm /

FlagsRecordTest

Tags:elm-spec

Checks, in A Standard Definition of Elm?:rule p-initrule p-stat-main-programrule e-dec-recordrule e-dec-true

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
A `Platform.worker` whose flags are a record of an `Int`, a `String` and a `Bool` receives the decoded record in `init`.

The `FLAGS` directive supplies the JSON `{"count":42,"label":"native-flags","enabled":true}`. `init` logs `label`, `count + 1` and `enabled`.

Results

elm-0.19.1

passran in 51 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.1/optimize for better performance and smaller assets.
FlagsRecordTest label: "native-flags"
FlagsRecordTest count: 43
FlagsRecordTest enabled: True

elm-0.19.2

passran in 50 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.2/optimize for better performance and smaller assets.
FlagsRecordTest label: "native-flags"
FlagsRecordTest count: 43
FlagsRecordTest enabled: True

elm-0.19.3

passran in 75 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.3/optimize for better performance and smaller assets.
FlagsRecordTest label: "native-flags"
FlagsRecordTest count: 43
FlagsRecordTest enabled: True

lamdera-1.5.0

passran in 63 ms
Output
FlagsRecordTest label: "native-flags"
FlagsRecordTest count: 43
FlagsRecordTest enabled: True

Discussions

Discuss this test·Search the discussions on GitHub

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

Source

The -- CHECK: lines give the expected output.

module FlagsRecordTest exposing (main)
{-| A `Platform.worker` whose flags are a record of an `Int`, a `String` and a `Bool` receives the decoded record in `init`.
The `FLAGS` directive supplies the JSON `{"count":42,"label":"native-flags","enabled":true}`. `init` logs `label`, `count + 1` and `enabled`.
-}
-- TAGS: elm-spec, p-init, p-stat-main-program, e-dec-record, e-dec-true
-- SKIP-AOT: needs `-- FLAGS:` JSON, which only the JIT harness can inject (eco_entry.cpp reads no flags)
-- FLAGS: {"count":42,"label":"native-flags","enabled":true}
-- CHECK: FlagsRecordTest label: "native-flags"
-- CHECK: FlagsRecordTest count: 43
-- CHECK: FlagsRecordTest enabled: True
import Platform
type alias Flags =
{ count : Int
, label : String
, enabled : Bool
}
init : Flags -> ( (), Cmd msg )
init flags =
let
_ =
Debug.log "FlagsRecordTest label" flags.label
_ =
Debug.log "FlagsRecordTest count" (flags.count + 1)
_ =
Debug.log "FlagsRecordTest enabled" flags.enabled
in
( (), Cmd.none )
main : Program Flags () msg
main =
Platform.worker
{ init = init
, update = \_ model -> ( model, Cmd.none )
, subscriptions = \_ -> Sub.none
}

Lean translation

This worker program is translated into Lean by the translator of the Lean mechanisation of A Standard Definition of Elm? (g_elm_FlagsRecordTest, in StandardElmMech/Generated/Worker01.lean). Lean evaluates its modules by the rules of Chapter 7, starts it by r:p-init with the flags {"count":42,"label":"native-flags","enabled":true}, 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 the kernel.

CheckExpected (CHECK)Lean evaluatorProved by
FlagsRecordTest label"native-flags""native-flags"t_elm_FlagsRecordTest
FlagsRecordTest count4343t_elm_FlagsRecordTest
FlagsRecordTest enabledTrueTruet_elm_FlagsRecordTest
/-- tests/elm/src/FlagsRecordTest.elm -/
def g_elm_FlagsRecordTest : Program := {
  name := "FlagsRecordTest", source := "tests/elm/src",
  exposing := (.items [.var "main"]), imports := [⟨"Platform", "Platform", none⟩],
  types := [],
  aliases := [("Flags", some ["count", "label", "enabled"])],
  defs := [
    .fn "init" [pv "flags"] (letDefs [.pat pw (ap (qv "Debug" "log") [str "FlagsRecordTest label", .atexp (access (A (v "flags")) "label")]), .pat pw (ap (qv "Debug" "log") [str "FlagsRecordTest count", .atexp (.paren (bin "+" (.atexp (access (A (v "flags")) "count")) (i 1)))]), .pat pw (ap (qv "Debug" "log") [str "FlagsRecordTest enabled", .atexp (access (A (v "flags")) "enabled")])] (tup2 (.atexp .unit) (qv "Cmd" "none"))),
    .fn "main" [] (ap (qv "Platform" "worker") [recE [("init", v "init"), ("update", lams [pw, pv "model"] (tup2 (v "model") (qv "Cmd" "none"))), ("subscriptions", lams [pw] (qv "Sub" "none"))]]) ],
  checks := [
    chk .check "FlagsRecordTest label: \"native-flags\"" .string,
    chk .check "FlagsRecordTest count: 43" .int,
    chk .check "FlagsRecordTest enabled: True" .data ] }

/-- The worker test of FlagsRecordTest. -/
def w_elm_FlagsRecordTest : Prog.WorkerTest := {
  prog := g_elm_FlagsRecordTest,
  flagsType := (.record (.cons "count" .int (.cons "enabled" .bool (.cons "label" .string .nil)))),
  flags := (Eff.BVal.mkObj [("count", (.num (Eff.jnum (42) (0)))), ("label", (.str "native-flags")), ("enabled", .tru)]),
  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_FlagsRecordTest 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_FlagsRecordTest 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_FlagsRecordTest states that the program has a meaning by r:p-program, with both premises, and ident_elm_FlagsRecordTest that its specialisation is unique and is the reduced program above, both with no hypothesis about principality.