Elm Exactly
Results / elm /

StringCreationMatrixTest

Tags:elm-spec

Checks, in A Standard Definition of Elm?:rule d-atpat-scon

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
The same string produced in different ways is `==`, and matches the same string pattern in a
`case`.

The string `"abc123"` is produced four ways: as a literal, as `"abc" ++ "123"`, as
`"ab" ++ "c1" ++ "23"`, and as `"" ++ "abc123" ++ ""`. The test logs two results:

  - `eq`: all four are equal;
  - `case`: the three concatenated ones each match the `"abc123"` pattern.

The same string made by library functions, and its use as a `Dict` key, are checked by
StringCreationLibraryMatrixTest.

Results

elm-0.19.1

passran in 63 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.1/optimize for better performance and smaller assets.
Creation.eq: True
Creation.case: True

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.
Creation.eq: True
Creation.case: True

elm-0.19.3

passran in 78 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.3/optimize for better performance and smaller assets.
Creation.case: True
Creation.eq: True

lamdera-1.5.0

passran in 47 ms
Output
Creation.case: True
Creation.eq: True

Discussions

Discuss this test·Search the discussions on GitHub

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

Source

The -- CHECK: lines give the expected output.

module StringCreationMatrixTest exposing (main)
{-| The same string produced in different ways is `==`, and matches the same string pattern in a
`case`.
The string `"abc123"` is produced four ways: as a literal, as `"abc" ++ "123"`, as
`"ab" ++ "c1" ++ "23"`, and as `"" ++ "abc123" ++ ""`. The test logs two results:
- `eq`: all four are equal;
- `case`: the three concatenated ones each match the `"abc123"` pattern.
The same string made by library functions, and its use as a `Dict` key, are checked by
StringCreationLibraryMatrixTest.
-}
-- TAGS: elm-spec, d-atpat-scon
-- CHECK: Creation.eq: True
-- CHECK: Creation.case: True
import Html exposing (text)
classify : String -> String
classify s =
case s of
"abc123" ->
"matched"
_ ->
"no"
main : Html.Html msg
main =
let
viaLiteral =
"abc123"
viaConcat =
"abc" ++ "123"
viaThree =
"ab" ++ "c1" ++ "23"
viaEmpty =
"" ++ "abc123" ++ ""
eq =
(viaLiteral == viaConcat)
&& (viaConcat == viaThree)
&& (viaThree == viaEmpty)
caseOk =
(classify viaConcat == "matched")
&& (classify viaThree == "matched")
&& (classify viaEmpty == "matched")
_ =
Debug.log "Creation.eq" eq
_ =
Debug.log "Creation.case" caseOk
in
text ""

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_StringCreationMatrixTest, in StandardElmMech/Generated/Elm_StandardL.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
Creation.eqTrueTruet_elm_StringCreationMatrixTest
Creation.caseTrueTruet_elm_StringCreationMatrixTest
/-- tests/elm/src/StringCreationMatrixTest.elm -/
def g_elm_StringCreationMatrixTest : Program := {
  name := "StringCreationMatrixTest", source := "tests/elm/src",
  exposing := (.items [.var "main"]), imports := [⟨"Html", "Html", some (.items [.var "text"])⟩],
  types := [],
  aliases := [],
  defs := [
    .fn "classify" [pv "s"] (caseOf (v "s") [(P (.scon (.str "abc123")), str "matched"), (P pw, str "no")]),
    .fn "main" [] (letDefs [.fn "viaLiteral" [] (str "abc123"), .fn "viaConcat" [] (bin "++" (str "abc") (str "123")), .fn "viaThree" [] (bin "++" (str "ab") (bin "++" (str "c1") (str "23"))), .fn "viaEmpty" [] (bin "++" (str "") (bin "++" (str "abc123") (str ""))), .fn "eq" [] (andalso (.atexp (.paren (bin "==" (v "viaLiteral") (v "viaConcat")))) (andalso (.atexp (.paren (bin "==" (v "viaConcat") (v "viaThree")))) (.atexp (.paren (bin "==" (v "viaThree") (v "viaEmpty")))))), .fn "caseOk" [] (andalso (.atexp (.paren (bin "==" (ap (v "classify") [v "viaConcat"]) (str "matched")))) (andalso (.atexp (.paren (bin "==" (ap (v "classify") [v "viaThree"]) (str "matched")))) (.atexp (.paren (bin "==" (ap (v "classify") [v "viaEmpty"]) (str "matched")))))), .pat pw (ap (qv "Debug" "log") [str "Creation.eq", v "eq"]), .pat pw (ap (qv "Debug" "log") [str "Creation.case", v "caseOk"])] (ap (v "text") [str ""])) ],
  checks := [
    chk .check "Creation.eq: True" .data,
    chk .check "Creation.case: True" .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_StringCreationMatrixTest 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_StringCreationMatrixTest 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_StringCreationMatrixTest states that the program has a meaning by r:p-program, with both premises, and ident_elm_StringCreationMatrixTest that its specialisation is unique and is the reduced program above, both with no hypothesis about principality.