Elm Exactly
Results / elm /

HoleyAliasTwoParamsTest

Tags:elm-spec

Checks, in A Standard Definition of Elm?:rule sm-typbindrule s-ty-conrule sm-conbindrule 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 type alias with two type parameters, used as a constructor field, keeps each parameter bound to its own argument.

`Box` holds a `Pair Int String` and `RBox` a `Rec Int Float`. The test reads
each field back out with arithmetic or string operations of its expected type,
and also logs both values whole. `GBox x y` uses the parameters in opposite orders in its two fields, `Pair x y` and
`Rec y x`, and `gfirst` reads the Int from the first of them; it contributes 0
to `recInt`.

Results

elm-0.19.1

passran in 74 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.1/optimize for better performance and smaller assets.
pairInt: 42
pairStr: "hi"
recFloat: 2.5
recInt: 7
box: Box (42,"hi")
rbox: RBox { first = 7, second = 2.5 }

elm-0.19.2

passran in 80 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.2/optimize for better performance and smaller assets.
pairInt: 42
pairStr: "hi"
recFloat: 2.5
recInt: 7
box: Box (42,"hi")
rbox: RBox { first = 7, second = 2.5 }

elm-0.19.3

passran in 74 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.3/optimize for better performance and smaller assets.
box: Box (42,"hi")
rbox: RBox { first = 7, second = 2.5 }
pairStr: "hi"
pairInt: 42
recInt: 7
recFloat: 2.5

lamdera-1.5.0

passran in 63 ms
Output
box: Box (42,"hi")
rbox: RBox { first = 7, second = 2.5 }
pairStr: "hi"
pairInt: 42
recInt: 7
recFloat: 2.5

Discussions

Discuss this test·Search the discussions on GitHub

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

Source

The -- CHECK: lines give the expected output.

module HoleyAliasTwoParamsTest exposing (main)
{-| A type alias with two type parameters, used as a constructor field, keeps each parameter bound to its own argument.
`Box` holds a `Pair Int String` and `RBox` a `Rec Int Float`. The test reads
each field back out with arithmetic or string operations of its expected type,
and also logs both values whole. `GBox x y` uses the parameters in opposite orders in its two fields, `Pair x y` and
`Rec y x`, and `gfirst` reads the Int from the first of them; it contributes 0
to `recInt`.
-}
import Html exposing (text)
-- TAGS: elm-spec, sm-typbind, s-ty-con, sm-conbind, d-pat-con
-- CHECK: pairInt: 42
-- CHECK: pairStr: "hi"
-- CHECK: recFloat: 2.5
-- CHECK: recInt: 7
-- CHECK: box: Box (42,"hi")
-- CHECK: rbox: RBox { first = 7, second = 2.5 }
type alias Pair a b =
( a, b )
type alias Rec a b =
{ first : a, second : b }
type Box
= Box (Pair Int String)
type RBox
= RBox (Rec Int Float)
type GBox x y
= GBox (Pair x y) (Rec y x)
unbox : Box -> ( Int, String )
unbox (Box ( n, s )) =
( n + 0, s ++ "" )
runbox : RBox -> ( Float, Int )
runbox (RBox r) =
( r.second * 1.0, r.first * 1 )
gfirst : GBox Int Float -> Int
gfirst (GBox ( a, _ ) _) =
a + 0
main =
let
( n, s ) =
unbox (Box ( 42, "hi" ))
( f, i ) =
runbox (RBox { first = 7, second = 2.5 })
_ =
Debug.log "pairInt" n
_ =
Debug.log "pairStr" s
_ =
Debug.log "recFloat" f
_ =
Debug.log "recInt" (i + gfirst (GBox ( 0, 1.5 ) { first = 1.5, second = 0 }))
_ =
Debug.log "box" (Box ( 42, "hi" ))
_ =
Debug.log "rbox" (RBox { first = 7, second = 2.5 })
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_HoleyAliasTwoParamsTest, in StandardElmMech/Generated/Elm_Fli.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
pairInt4242t_elm_HoleyAliasTwoParamsTest
pairStr"hi""hi"t_elm_HoleyAliasTwoParamsTest
recFloat2.52.5t_elm_HoleyAliasTwoParamsTest
recInt77t_elm_HoleyAliasTwoParamsTest
boxBox (42,"hi")Box (42,"hi")t_elm_HoleyAliasTwoParamsTest
rboxRBox { first = 7, second = 2.5 }RBox { first = 7, second = 2.5 }t_elm_HoleyAliasTwoParamsTest
/-- tests/elm/src/HoleyAliasTwoParamsTest.elm -/
def g_elm_HoleyAliasTwoParamsTest : Program := {
  name := "HoleyAliasTwoParamsTest", source := "tests/elm/src",
  exposing := (.items [.var "main"]), imports := [⟨"Html", "Html", some (.items [.var "text"])⟩],
  types := [("Box", [("Box", 1)]), ("GBox", [("GBox", 2)]), ("RBox", [("RBox", 1)])],
  aliases := [("Pair", none), ("Rec", some ["first", "second"])],
  defs := [
    .fn "unbox" [.paren (pcon "Box" [ptup2 (P (pv "n")) (P (pv "s"))])] (tup2 (bin "+" (v "n") (i 0)) (bin "++" (v "s") (str ""))),
    .fn "runbox" [.paren (pcon "RBox" [pv "r"])] (tup2 (bin "*" (.atexp (access (A (v "r")) "second")) (.atexp (.scon (.float 10 (-1))))) (bin "*" (.atexp (access (A (v "r")) "first")) (i 1))),
    .fn "gfirst" [.paren (pcon "GBox" [ptup2 (P (pv "a")) (P pw), pw])] (bin "+" (v "a") (i 0)),
    .fn "main" [] (letDefs [.pat (ptup2 (P (pv "n")) (P (pv "s"))) (ap (v "unbox") [.atexp (.paren (ap (c "Box") [tup2 (i 42) (str "hi")]))]), .pat (ptup2 (P (pv "f")) (P (pv "i"))) (ap (v "runbox") [.atexp (.paren (ap (c "RBox") [recE [("first", i 7), ("second", .atexp (.scon (.float 25 (-1))))]]))]), .pat pw (ap (qv "Debug" "log") [str "pairInt", v "n"]), .pat pw (ap (qv "Debug" "log") [str "pairStr", v "s"]), .pat pw (ap (qv "Debug" "log") [str "recFloat", v "f"]), .pat pw (ap (qv "Debug" "log") [str "recInt", .atexp (.paren (bin "+" (v "i") (ap (v "gfirst") [.atexp (.paren (ap (c "GBox") [tup2 (i 0) (.atexp (.scon (.float 15 (-1)))), recE [("first", .atexp (.scon (.float 15 (-1)))), ("second", i 0)]]))])))]), .pat pw (ap (qv "Debug" "log") [str "box", .atexp (.paren (ap (c "Box") [tup2 (i 42) (str "hi")]))]), .pat pw (ap (qv "Debug" "log") [str "rbox", .atexp (.paren (ap (c "RBox") [recE [("first", i 7), ("second", .atexp (.scon (.float 25 (-1))))]]))])] (ap (v "text") [str "done"])) ],
  checks := [
    chk .check "pairInt: 42" .int,
    chk .check "pairStr: \"hi\"" .string,
    chk .check "recFloat: 2.5" .float,
    chk .check "recInt: 7" .int,
    chk .check "box: Box (42,\"hi\")" .data,
    chk .check "rbox: RBox { first = 7, second = 2.5 }" .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_HoleyAliasTwoParamsTest 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_HoleyAliasTwoParamsTest 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_HoleyAliasTwoParamsTest states that the program has a meaning by r:p-program, with both premises, and ident_elm_HoleyAliasTwoParamsTest that its specialisation is unique and is the reduced program above, both with no hypothesis about principality.