EitherTypeTest
Tags:elm-spec
Checks, in A Standard Definition of Elm?:rule sm-datbindrule sm-conbindrule d-pat-conrule d-pat-con-fail
Translated into Lean for the mechanisation of the book: see its Lean translation.
A custom type with two type parameters and one constructor for each can be built, mapped and pattern-matched. `fromEither` returns the `Left` payload or a default, `mapLeft` applies a function to a `Left` payload only, and `isLeft` reports which constructor a value has.
Results
elm-0.19.1
passran in 74 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.1/optimize for better performance and smaller assets. left: 42 right: 0 mapLeft: 84 isLeft: True
elm-0.19.2
passran in 52 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.2/optimize for better performance and smaller assets. left: 42 right: 0 mapLeft: 84 isLeft: True
elm-0.19.3
passran in 44 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.3/optimize for better performance and smaller assets. left: 42 right: 0 mapLeft: 84 isLeft: True
lamdera-1.5.0
passran in 58 msOutput
left: 42 right: 0 mapLeft: 84 isLeft: True
Discussions
Discuss this test·Search the discussions on GitHub
Discussions are on GitHub; one about this test has [t:elm/EitherTypeTest] in its title.
Source
The -- CHECK: lines give the expected output.
module EitherTypeTest exposing (main)
{-| A custom type with two type parameters and one constructor for each can be
built, mapped and pattern-matched.
`fromEither` returns the `Left` payload or a default, `mapLeft` applies a
function to a `Left` payload only, and `isLeft` reports which constructor a
value has.
-}
-- TAGS: elm-spec, sm-datbind, sm-conbind, d-pat-con, d-pat-con-fail
-- CHECK: left: 42
-- CHECK: right: 0
-- CHECK: mapLeft: 84
-- CHECK: isLeft: True
import Html exposing (text)
type Either a b
= Left a
| Right b
fromEither e default =
case e of
Left x ->
x
Right _ ->
default
mapLeft f e =
case e of
Left x ->
Left (f x)
Right y ->
Right y
isLeft e =
case e of
Left _ ->
True
Right _ ->
False
main =
let
_ = Debug.log "left" (fromEither (Left 42) 0)
_ = Debug.log "right" (fromEither (Right "err") 0)
mapped = mapLeft (\x -> x * 2) (Left 42)
_ = Debug.log "mapLeft" (fromEither mapped 0)
_ = Debug.log "isLeft" (isLeft (Left 1))
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_EitherTypeTest, in StandardElmMech/Generated/Elm_CovTailRecPairParameters.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.
| Check | Expected (CHECK) | Lean evaluator | Proved by |
|---|---|---|---|
left | 42 | 42 | t_elm_EitherTypeTest |
right | 0 | 0 | t_elm_EitherTypeTest |
mapLeft | 84 | 84 | t_elm_EitherTypeTest |
isLeft | True | True | t_elm_EitherTypeTest |
/-- tests/elm/src/EitherTypeTest.elm -/
def g_elm_EitherTypeTest : Program := {
name := "EitherTypeTest", source := "tests/elm/src",
exposing := (.items [.var "main"]), imports := [⟨"Html", "Html", some (.items [.var "text"])⟩],
types := [("Either", [("Left", 1), ("Right", 1)])],
aliases := [],
defs := [
.fn "fromEither" [pv "e", pv "default"] (caseOf (v "e") [(pcon "Left" [pv "x"], v "x"), (pcon "Right" [pw], v "default")]),
.fn "mapLeft" [pv "f", pv "e"] (caseOf (v "e") [(pcon "Left" [pv "x"], ap (c "Left") [.atexp (.paren (ap (v "f") [v "x"]))]), (pcon "Right" [pv "y"], ap (c "Right") [v "y"])]),
.fn "isLeft" [pv "e"] (caseOf (v "e") [(pcon "Left" [pw], c "True"), (pcon "Right" [pw], c "False")]),
.fn "main" [] (letDefs [.pat pw (ap (qv "Debug" "log") [str "left", .atexp (.paren (ap (v "fromEither") [.atexp (.paren (ap (c "Left") [.atexp (.scon (.intF 42))])), .atexp (.scon (.intF 0))]))]), .pat pw (ap (qv "Debug" "log") [str "right", .atexp (.paren (ap (v "fromEither") [.atexp (.paren (ap (c "Right") [str "err"])), .atexp (.scon (.intF 0))]))]), .fn "mapped" [] (ap (v "mapLeft") [.atexp (.paren (lams [pv "x"] (bin "*" (v "x") (.atexp (.scon (.intF 2)))))), .atexp (.paren (ap (c "Left") [.atexp (.scon (.intF 42))]))]), .pat pw (ap (qv "Debug" "log") [str "mapLeft", .atexp (.paren (ap (v "fromEither") [v "mapped", .atexp (.scon (.intF 0))]))]), .pat pw (ap (qv "Debug" "log") [str "isLeft", .atexp (.paren (ap (v "isLeft") [.atexp (.paren (ap (c "Left") [.atexp (.scon (.intF 1))]))]))])] (ap (v "text") [str "done"])) ],
checks := [
chk .check "left: 42" .float,
chk .check "right: 0" .float,
chk .check "mapLeft: 84" .float,
chk .check "isLeft: 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_EitherTypeTest 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_EitherTypeTest 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_EitherTypeTest states that the program has a meaning by r:p-program, with both premises, and ident_elm_EitherTypeTest that its specialisation is unique and is the reduced program above, both with no hypothesis about principality.