Elm Exactly
Results / elm /

EtaExpandStateTest

Tags:elm-spec

Checks, in A Standard Definition of Elm?:rule sm-typbindrule s-ty-conrule s-valbind-varrule d-app-closurerule d-valbind-pat

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 state monad over the function alias `St a = Int -> ( Int, a )`, with `sequence` written recursively using `andThen` and `pure`, threads its state correctly.

`andThen`, `pure`, `tick` and `tick2` are annotated against `St` and take the state as an extra parameter in their definitions. `sequence` is written without the state parameter: each branch returns a `St` value built by partial application.

`run n` sequences `[ tick, tick2, pure 5, tick ]` from state `n`, which yields `n`, `10 * (n + 1)`, `5` and `n + 3`, and sums them to `12 * n + 18` with a local `sum`. For `n` equal to 1, 2 and 3 that is `[30,42,54]`; a mistake in how the state is threaded changes these numbers.

Results

elm-0.19.1

passran in 65 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.1/optimize for better performance and smaller assets.
r: [30,42,54]

elm-0.19.2

passran in 69 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.2/optimize for better performance and smaller assets.
r: [30,42,54]

elm-0.19.3

passran in 45 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.3/optimize for better performance and smaller assets.
r: [30,42,54]

lamdera-1.5.0

passran in 47 ms
Output
r: [30,42,54]

Discussions

Discuss this test·Search the discussions on GitHub

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

Source

The -- CHECK: lines give the expected output.

module EtaExpandStateTest exposing (main)
{-| A state monad over the function alias `St a = Int -> ( Int, a )`, with `sequence` written recursively using `andThen` and `pure`, threads its state correctly.
`andThen`, `pure`, `tick` and `tick2` are annotated against `St` and take the state as an extra parameter in their definitions. `sequence` is written without the state parameter: each branch returns a `St` value built by partial application.
`run n` sequences `[ tick, tick2, pure 5, tick ]` from state `n`, which yields `n`, `10 * (n + 1)`, `5` and `n + 3`, and sums them to `12 * n + 18` with a local `sum`. For `n` equal to 1, 2 and 3 that is `[30,42,54]`; a mistake in how the state is threaded changes these numbers.
-}
import Html exposing (text)
-- TAGS: elm-spec, sm-typbind, s-ty-con, s-valbind-var, d-app-closure, d-valbind-pat
-- CHECK: r: [30,42,54]
type alias St a =
Int -> ( Int, a )
andThen : (a -> St b) -> St a -> St b
andThen f ma s0 =
let
( s1, a ) =
ma s0
in
f a s1
pure : a -> St a
pure x s =
( s, x )
tick : St Int
tick s =
( s + 1, s )
tick2 : St Int
tick2 s =
( s + 2, s * 10 )
sequence : List (St a) -> St (List a)
sequence actions =
case actions of
[] ->
pure []
m :: rest ->
m |> andThen (\x -> sequence rest |> andThen (\xs -> pure (x :: xs)))
sum : List Int -> Int
sum xs =
case xs of
[] ->
0
x :: rest ->
x + sum rest
run : Int -> Int
run n =
let
( _, xs ) =
sequence [ tick, tick2, pure 5, tick ] n
in
sum xs
main =
let
_ =
Debug.log "r" [ run 1, run 2, run 3 ]
in
text "done"

Lean translation

This program is translated into Lean by the translator of the Lean mechanisation of A Standard Definition of Elm?: the program it is, its modules in the reduced syntax of Chapter 7 with every integer constant at Int or Float by number specialisation, and its CHECK lines as written (g_elm_EtaExpandStateTest, in StandardElmMech/Generated/Elm_EmbeddedNo.lean). Lean's evaluator runs the program by the rules of Chapters 7 and 9, and its checks become one generated theorem: some derivation of the program has a log that meets every check, or, for a test that stops at Debug.todo, the program has no meaning.

CheckExpected (CHECK)Lean evaluatorProved by
r[30,42,54][30,42,54]t_elm_EtaExpandStateTest
/-- tests/elm/src/EtaExpandStateTest.elm -/
def g_elm_EtaExpandStateTest : Program := {
  name := "EtaExpandStateTest", source := "tests/elm/src",
  exposing := (.items [.var "main"]), imports := [⟨"Html", "Html", some (.items [.var "text"])⟩],
  types := [],
  aliases := [("St", none)],
  defs := [
    .fn "andThen" [pv "f", pv "ma", pv "s0"] (letDefs [.pat (ptup2 (P (pv "s1")) (P (pv "a"))) (ap (v "ma") [v "s0"])] (ap (v "f") [v "a", v "s1"])),
    .fn "pure" [pv "x", pv "s"] (tup2 (v "s") (v "x")),
    .fn "tick" [pv "s"] (tup2 (bin "+" (v "s") (i 1)) (v "s")),
    .fn "tick2" [pv "s"] (tup2 (bin "+" (v "s") (i 2)) (bin "*" (v "s") (i 10))),
    .fn "sequence" [pv "actions"] (caseOf (v "actions") [(P (listP []), ap (v "pure") [listE []]), (.cons (P (pv "m")) (P (pv "rest")), bin "|>" (v "m") (ap (v "andThen") [.atexp (.paren (lams [pv "x"] (bin "|>" (ap (v "sequence") [v "rest"]) (ap (v "andThen") [.atexp (.paren (lams [pv "xs"] (ap (v "pure") [.atexp (.paren (bin "::" (v "x") (v "xs")))])))]))))]))]),
    .fn "sum" [pv "xs"] (caseOf (v "xs") [(P (listP []), i 0), (.cons (P (pv "x")) (P (pv "rest")), bin "+" (v "x") (ap (v "sum") [v "rest"]))]),
    .fn "run" [pv "n"] (letDefs [.pat (ptup2 (P pw) (P (pv "xs"))) (ap (v "sequence") [listE [v "tick", v "tick2", ap (v "pure") [i 5], v "tick"], v "n"])] (ap (v "sum") [v "xs"])),
    .fn "main" [] (letDefs [.pat pw (ap (qv "Debug" "log") [str "r", listE [ap (v "run") [i 1], ap (v "run") [i 2], ap (v "run") [i 3]]])] (ap (v "text") [str "done"])) ],
  checks := [
    chk .check "r: [30,42,54]" .data ] }

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_EtaExpandStateTest 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_EtaExpandStateTest 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_EtaExpandStateTest states that the program has a meaning by r:p-program, with both premises, and ident_elm_EtaExpandStateTest that its specialisation is unique and is the reduced program above, both with no hypothesis about principality.