Elm Exactly
Results / elm /

NumberDestructFieldTest

Tags:elm-spec

Checks, in A Standard Definition of Elm?:rule s-valbind-patrule s-dec-grouprule d-atexp-sconsection Number Specialisation

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
Number literals stored in a constructor's tuple, list, record or `Maybe`
field, then destructured in a `let` with a constructor pattern, take the type
`Float` from how they are later used.

Each of `T`, `L`, `R` and `M` has a type parameter for the number and an
extra `Int` field; the destructured value is used in `Float` arithmetic. In
`N ( ( 1, 2 ), 3 )` the pair's two numbers share one type parameter and are
used as `Float`s, while the third has its own parameter and is not, so
`nested` logs `(3.5,4)`.

Results

elm-0.19.1

passran in 56 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.1/optimize for better performance and smaller assets.
tuple field: 2.5
list field: 1.25
record field: 4.5
maybe field: 5.5
nested: (3.5,4)

elm-0.19.2

passran in 45 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.2/optimize for better performance and smaller assets.
tuple field: 2.5
list field: 1.25
record field: 4.5
maybe field: 5.5
nested: (3.5,4)

elm-0.19.3

passran in 49 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.3/optimize for better performance and smaller assets.
list field: 1.25
tuple field: 2.5
record field: 4.5
maybe field: 5.5
nested: (3.5,4)

lamdera-1.5.0

passran in 65 ms
Output
list field: 1.25
tuple field: 2.5
record field: 4.5
maybe field: 5.5
nested: (3.5,4)

Discussions

Discuss this test·Search the discussions on GitHub

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

Source

The -- CHECK: lines give the expected output.

module NumberDestructFieldTest exposing (main)
{-| Number literals stored in a constructor's tuple, list, record or `Maybe`
field, then destructured in a `let` with a constructor pattern, take the type
`Float` from how they are later used.
Each of `T`, `L`, `R` and `M` has a type parameter for the number and an
extra `Int` field; the destructured value is used in `Float` arithmetic. In
`N ( ( 1, 2 ), 3 )` the pair's two numbers share one type parameter and are
used as `Float`s, while the third has its own parameter and is not, so
`nested` logs `(3.5,4)`.
-}
-- TAGS: elm-spec, s-valbind-pat, s-dec-group, d-atexp-scon, sec:dynamic-core-numbers
-- CHECK: tuple field: 2.5
-- CHECK: list field: 1.25
-- CHECK: record field: 4.5
-- CHECK: maybe field: 5.5
-- CHECK: nested: (3.5,4)
import Html exposing (text)
type T a
= T ( a, Int ) Int
type L a
= L (List a) Int
type R a
= R { v : a } Int
type M a
= M (Maybe a) Int
type N a b
= N ( ( a, a ), b )
main =
let
tt =
T ( 2, 3 ) 0
(T ( t, _ ) _) =
tt
ll =
L [ 1 ] 0
(L xs _) =
ll
rr =
R { v = 4 } 0
(R r _) =
rr
mm =
M (Just 5) 0
(M m _) =
mm
n =
N ( ( 1, 2 ), 3 )
(N ( ( a, b ), c )) =
n
_ =
Debug.log "tuple field" (t + 0.5)
_ =
Debug.log "list field"
(case xs of
x :: _ ->
x + 0.25
[] ->
0.25
)
_ =
Debug.log "record field" (r.v + 0.5)
_ =
Debug.log "maybe field"
(case m of
Just z ->
z + 0.5
Nothing ->
0.5
)
_ =
Debug.log "nested" ( a + b + 0.5, c + 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_NumberDestructFieldTest, in StandardElmMech/Generated/Elm_M.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
tuple field2.52.5t_elm_NumberDestructFieldTest
list field1.251.25t_elm_NumberDestructFieldTest
record field4.54.5t_elm_NumberDestructFieldTest
maybe field5.55.5t_elm_NumberDestructFieldTest
nested(3.5,4)(3.5,4)t_elm_NumberDestructFieldTest
/-- tests/elm/src/NumberDestructFieldTest.elm -/
def g_elm_NumberDestructFieldTest : Program := {
  name := "NumberDestructFieldTest", source := "tests/elm/src",
  exposing := (.items [.var "main"]), imports := [⟨"Html", "Html", some (.items [.var "text"])⟩],
  types := [("L", [("L", 2)]), ("M", [("M", 2)]), ("N", [("N", 1)]), ("R", [("R", 2)]), ("T", [("T", 2)])],
  aliases := [],
  defs := [
    .fn "main" [] (letDefs [.fn "tt" [] (ap (c "T") [tup2 (.atexp (.scon (.intF 2))) (i 3), i 0]), .pat (.paren (pcon "T" [ptup2 (P (pv "t")) (P pw), pw])) (v "tt"), .fn "ll" [] (ap (c "L") [listE [.atexp (.scon (.intF 1))], i 0]), .pat (.paren (pcon "L" [pv "xs", pw])) (v "ll"), .fn "rr" [] (ap (c "R") [recE [("v", .atexp (.scon (.intF 4)))], i 0]), .pat (.paren (pcon "R" [pv "r", pw])) (v "rr"), .fn "mm" [] (ap (c "M") [.atexp (.paren (ap (c "Just") [.atexp (.scon (.intF 5))])), i 0]), .pat (.paren (pcon "M" [pv "m", pw])) (v "mm"), .fn "n" [] (ap (c "N") [tup2 (tup2 (.atexp (.scon (.intF 1))) (.atexp (.scon (.intF 2)))) (.atexp (.scon (.intF 3)))]), .pat (.paren (pcon "N" [ptup2 (P (ptup2 (P (pv "a")) (P (pv "b")))) (P (pv "c"))])) (v "n"), .pat pw (ap (qv "Debug" "log") [str "tuple field", .atexp (.paren (bin "+" (v "t") (.atexp (.scon (.float 5 (-1))))))]), .pat pw (ap (qv "Debug" "log") [str "list field", .atexp (.paren (caseOf (v "xs") [(.cons (P (pv "x")) (P pw), bin "+" (v "x") (.atexp (.scon (.float 25 (-2))))), (P (listP []), .atexp (.scon (.float 25 (-2))))]))]), .pat pw (ap (qv "Debug" "log") [str "record field", .atexp (.paren (bin "+" (.atexp (access (A (v "r")) "v")) (.atexp (.scon (.float 5 (-1))))))]), .pat pw (ap (qv "Debug" "log") [str "maybe field", .atexp (.paren (caseOf (v "m") [(pcon "Just" [pv "z"], bin "+" (v "z") (.atexp (.scon (.float 5 (-1))))), (P (pcA "Nothing"), .atexp (.scon (.float 5 (-1))))]))]), .pat pw (ap (qv "Debug" "log") [str "nested", tup2 (bin "+" (bin "+" (v "a") (v "b")) (.atexp (.scon (.float 5 (-1))))) (bin "+" (v "c") (.atexp (.scon (.intF 1))))])] (ap (v "text") [str "done"])) ],
  checks := [
    chk .check "tuple field: 2.5" .float,
    chk .check "list field: 1.25" .float,
    chk .check "record field: 4.5" .float,
    chk .check "maybe field: 5.5" .float,
    chk .check "nested: (3.5,4)" .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_NumberDestructFieldTest 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_NumberDestructFieldTest 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_NumberDestructFieldTest states that the program has a meaning by r:p-program, with both premises, and ident_elm_NumberDestructFieldTest that its specialisation is unique and is the reduced program above, both with no hypothesis about principality.