Elm Exactly
Results / elm /

CharLiteralPatternMatchMinimalTest

Tags:elm-spec

Checks, in A Standard Definition of Elm?:rule d-atpat-sconrule d-atpat-tuplerule d-pat-con

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 Char literal pattern for the double quote `'"'` matches at increasing depths of nesting:
bare, inside a tuple, inside a `Maybe`, and inside a `Maybe ( Char, String )`.

Every case should log `"y"`. The same pattern matched against a Char taken from a String by the
library function `String.uncons` is checked by elm-core's CharUnconsPatternTest.

Results

elm-0.19.1

passran in 41 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.1/optimize for better performance and smaller assets.
m1_bareChar: "y"
m2_charInTuple: "y"
m3_charInMaybe: "y"
m4_charInMaybeTuple: "y"

elm-0.19.2

passran in 70 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.2/optimize for better performance and smaller assets.
m1_bareChar: "y"
m2_charInTuple: "y"
m3_charInMaybe: "y"
m4_charInMaybeTuple: "y"

elm-0.19.3

passran in 77 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.3/optimize for better performance and smaller assets.
m4_charInMaybeTuple: "y"
m3_charInMaybe: "y"
m2_charInTuple: "y"
m1_bareChar: "y"

lamdera-1.5.0

passran in 74 ms
Output
m4_charInMaybeTuple: "y"
m3_charInMaybe: "y"
m2_charInTuple: "y"
m1_bareChar: "y"

Discussions

Discuss this test·Search the discussions on GitHub

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

Source

The -- CHECK: lines give the expected output.

module CharLiteralPatternMatchMinimalTest exposing (main)
{-| A Char literal pattern for the double quote `'"'` matches at increasing depths of nesting:
bare, inside a tuple, inside a `Maybe`, and inside a `Maybe ( Char, String )`.
Every case should log `"y"`. The same pattern matched against a Char taken from a String by the
library function `String.uncons` is checked by elm-core's CharUnconsPatternTest.
-}
-- TAGS: elm-spec, d-atpat-scon, d-atpat-tuple, d-pat-con
-- CHECK: m1_bareChar: "y"
-- CHECK: m2_charInTuple: "y"
-- CHECK: m3_charInMaybe: "y"
-- CHECK: m4_charInMaybeTuple: "y"
import Html exposing (text)
main =
let
-- m1: bare Char
m1 =
case '"' of
'"' ->
"y"
_ ->
"n"
-- m2: Char inside a tuple
m2 =
case ( '"', 0 ) of
( '"', _ ) ->
"y"
_ ->
"n"
-- m3: Char inside a Maybe
m3 =
case Just '"' of
Just '"' ->
"y"
_ ->
"n"
-- m4: Char inside a Maybe (Char, X), the shape String.uncons gives
m4 =
case Just ( '"', "x" ) of
Just ( '"', _ ) ->
"y"
_ ->
"n"
_ =
Debug.log "m1_bareChar" m1
_ =
Debug.log "m2_charInTuple" m2
_ =
Debug.log "m3_charInMaybe" m3
_ =
Debug.log "m4_charInMaybeTuple" m4
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_CharLiteralPatternMatchMinimalTest, in StandardElmMech/Generated/Elm_CaseS.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
m1_bareChar"y""y"t_elm_CharLiteralPatternMatchMinimalTest
m2_charInTuple"y""y"t_elm_CharLiteralPatternMatchMinimalTest
m3_charInMaybe"y""y"t_elm_CharLiteralPatternMatchMinimalTest
m4_charInMaybeTuple"y""y"t_elm_CharLiteralPatternMatchMinimalTest
/-- tests/elm/src/CharLiteralPatternMatchMinimalTest.elm -/
def g_elm_CharLiteralPatternMatchMinimalTest : Program := {
  name := "CharLiteralPatternMatchMinimalTest", source := "tests/elm/src",
  exposing := (.items [.var "main"]), imports := [⟨"Html", "Html", some (.items [.var "text"])⟩],
  types := [],
  aliases := [],
  defs := [
    .fn "main" [] (letDefs [.fn "m1" [] (caseOf (.atexp (chA '"')) [(P (pchar '"'), str "y"), (P pw, str "n")]), .fn "m2" [] (caseOf (tup2 (.atexp (chA '"')) (.atexp (.scon (.intF 0)))) [(P (ptup2 (P (pchar '"')) (P pw)), str "y"), (P pw, str "n")]), .fn "m3" [] (caseOf (ap (c "Just") [.atexp (chA '"')]) [(pcon "Just" [pchar '"'], str "y"), (P pw, str "n")]), .fn "m4" [] (caseOf (ap (c "Just") [tup2 (.atexp (chA '"')) (str "x")]) [(pcon "Just" [ptup2 (P (pchar '"')) (P pw)], str "y"), (P pw, str "n")]), .pat pw (ap (qv "Debug" "log") [str "m1_bareChar", v "m1"]), .pat pw (ap (qv "Debug" "log") [str "m2_charInTuple", v "m2"]), .pat pw (ap (qv "Debug" "log") [str "m3_charInMaybe", v "m3"]), .pat pw (ap (qv "Debug" "log") [str "m4_charInMaybeTuple", v "m4"])] (ap (v "text") [str "done"])) ],
  checks := [
    chk .check "m1_bareChar: \"y\"" .string,
    chk .check "m2_charInTuple: \"y\"" .string,
    chk .check "m3_charInMaybe: \"y\"" .string,
    chk .check "m4_charInMaybeTuple: \"y\"" .string ] }

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