Elm Exactly
Results / elm /

SingleCtorPairBoolIntBidiTest

Tags:elm-spec

Checks, in A Standard Definition of Elm?:rule sm-conbindrule d-pat-conrule d-atpat-conrule d-atpat-con-fail

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
Pattern matching and value extraction both work on two single-constructor types in one module, one wrapping a `Bool` and one wrapping an `Int`.

`matchBool` matches the literal patterns `WrapBool True` and `WrapBool False`,
giving `"yes"` and `"no"`. `matchInt` binds the wrapped `Int` and tests its sign,
giving `"positive"` for 7 and `"non-positive"` for -3. `extractBool` and
`extractInt` return the wrapped values `True` and `42`.

Results

elm-0.19.1

passran in 84 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.1/optimize for better performance and smaller assets.
match_true: "yes"
match_false: "no"
match_pos: "positive"
match_neg: "non-positive"
extract_bool: True
extract_int: 42

elm-0.19.2

passran in 62 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.2/optimize for better performance and smaller assets.
match_true: "yes"
match_false: "no"
match_pos: "positive"
match_neg: "non-positive"
extract_bool: True
extract_int: 42

elm-0.19.3

passran in 54 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.3/optimize for better performance and smaller assets.
match_true: "yes"
match_false: "no"
match_pos: "positive"
match_neg: "non-positive"
extract_bool: True
extract_int: 42

lamdera-1.5.0

passran in 62 ms
Output
match_true: "yes"
match_false: "no"
match_pos: "positive"
match_neg: "non-positive"
extract_bool: True
extract_int: 42

Discussions

Discuss this test·Search the discussions on GitHub

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

Source

The -- CHECK: lines give the expected output.

module SingleCtorPairBoolIntBidiTest exposing (main)
{-| Pattern matching and value extraction both work on two single-constructor types in one module, one wrapping a `Bool` and one wrapping an `Int`.
`matchBool` matches the literal patterns `WrapBool True` and `WrapBool False`,
giving `"yes"` and `"no"`. `matchInt` binds the wrapped `Int` and tests its sign,
giving `"positive"` for 7 and `"non-positive"` for -3. `extractBool` and
`extractInt` return the wrapped values `True` and `42`.
-}
-- TAGS: elm-spec, sm-conbind, d-pat-con, d-atpat-con, d-atpat-con-fail
-- CHECK: match_true: "yes"
-- CHECK: match_false: "no"
-- CHECK: match_pos: "positive"
-- CHECK: match_neg: "non-positive"
-- CHECK: extract_bool: True
-- CHECK: extract_int: 42
import Html exposing (text)
type WrapBool
= WrapBool Bool
type WrapInt
= WrapInt Int
matchBool : Bool -> String
matchBool b =
let
w =
WrapBool b
in
case w of
WrapBool True ->
"yes"
WrapBool False ->
"no"
matchInt : Int -> String
matchInt n =
let
w =
WrapInt n
in
case w of
WrapInt x ->
if x > 0 then
"positive"
else
"non-positive"
extractBool : WrapBool -> Bool
extractBool w =
case w of
WrapBool b ->
b
extractInt : WrapInt -> Int
extractInt w =
case w of
WrapInt n ->
n
main =
let
_ = Debug.log "match_true" (matchBool True)
_ = Debug.log "match_false" (matchBool False)
_ = Debug.log "match_pos" (matchInt 7)
_ = Debug.log "match_neg" (matchInt -3)
_ = Debug.log "extract_bool" (extractBool (WrapBool True))
_ = Debug.log "extract_int" (extractInt (WrapInt 42))
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_SingleCtorPairBoolIntBidiTest, in StandardElmMech/Generated/Elm_SingleCtorPairBoolF.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
match_true"yes""yes"t_elm_SingleCtorPairBoolIntBidiTest
match_false"no""no"t_elm_SingleCtorPairBoolIntBidiTest
match_pos"positive""positive"t_elm_SingleCtorPairBoolIntBidiTest
match_neg"non-positive""non-positive"t_elm_SingleCtorPairBoolIntBidiTest
extract_boolTrueTruet_elm_SingleCtorPairBoolIntBidiTest
extract_int4242t_elm_SingleCtorPairBoolIntBidiTest
/-- tests/elm/src/SingleCtorPairBoolIntBidiTest.elm -/
def g_elm_SingleCtorPairBoolIntBidiTest : Program := {
  name := "SingleCtorPairBoolIntBidiTest", source := "tests/elm/src",
  exposing := (.items [.var "main"]), imports := [⟨"Html", "Html", some (.items [.var "text"])⟩],
  types := [("WrapBool", [("WrapBool", 1)]), ("WrapInt", [("WrapInt", 1)])],
  aliases := [],
  defs := [
    .fn "matchBool" [pv "b"] (letDefs [.fn "w" [] (ap (c "WrapBool") [v "b"])] (caseOf (v "w") [(pcon "WrapBool" [pcA "True"], str "yes"), (pcon "WrapBool" [pcA "False"], str "no")])),
    .fn "matchInt" [pv "n"] (letDefs [.fn "w" [] (ap (c "WrapInt") [v "n"])] (caseOf (v "w") [(pcon "WrapInt" [pv "x"], ifte (bin ">" (v "x") (i 0)) (str "positive") (str "non-positive"))])),
    .fn "extractBool" [pv "w"] (caseOf (v "w") [(pcon "WrapBool" [pv "b"], v "b")]),
    .fn "extractInt" [pv "w"] (caseOf (v "w") [(pcon "WrapInt" [pv "n"], v "n")]),
    .fn "main" [] (letDefs [.pat pw (ap (qv "Debug" "log") [str "match_true", .atexp (.paren (ap (v "matchBool") [c "True"]))]), .pat pw (ap (qv "Debug" "log") [str "match_false", .atexp (.paren (ap (v "matchBool") [c "False"]))]), .pat pw (ap (qv "Debug" "log") [str "match_pos", .atexp (.paren (ap (v "matchInt") [i 7]))]), .pat pw (ap (qv "Debug" "log") [str "match_neg", .atexp (.paren (ap (v "matchInt") [.atexp (neg (A (i 3)))]))]), .pat pw (ap (qv "Debug" "log") [str "extract_bool", .atexp (.paren (ap (v "extractBool") [.atexp (.paren (ap (c "WrapBool") [c "True"]))]))]), .pat pw (ap (qv "Debug" "log") [str "extract_int", .atexp (.paren (ap (v "extractInt") [.atexp (.paren (ap (c "WrapInt") [i 42]))]))])] (ap (v "text") [str "done"])) ],
  checks := [
    chk .check "match_true: \"yes\"" .string,
    chk .check "match_false: \"no\"" .string,
    chk .check "match_pos: \"positive\"" .string,
    chk .check "match_neg: \"non-positive\"" .string,
    chk .check "extract_bool: True" .data,
    chk .check "extract_int: 42" .int ] }

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