Elm Exactly
Results / elm /

LetNumberDualUseTest

Tags:elm-spec

Checks, in A Standard Definition of Elm?:rule s-dec-grouprule s-atexp-longvarrule s-atexp-sconrule d-atexp-scon

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
An unannotated let-bound `number` value can be used as an `Int` and as a
`Float` in the same expression, because Elm generalizes the type of a `let`
binding and each use picks its own type.

Each case is a separate top-level value that computes
`round (toFloat (x + 0) + x * k)`: the `toFloat (x + 0)` use makes `x` an `Int`
and the `x * k` use, with `k` a `Float` literal, makes it a `Float`.

  - `scalar` uses `n = 5` directly: 5 + 4.0 logs 9.
  - `listSum` uses `List.sum` of `[ 1, 2, 3 ]`: 6 + 9.0 logs 15.
  - `tupleFirst` uses `Tuple.first` of `( 1, 2 )`: 1 + 1.5 rounds to 3.
  - `tupleCase` uses the product of a `( 1, 2 )` tuple destructured by `case`:
    2 + 3.0 logs 5.
  - `listHead` uses the head of `[ 1, 2, 3 ]` taken by a `case`: 1 + 1.5 rounds
    to 3.
  - `listTail` uses the product of the first two elements of a let-bound
    `[ 1, 2, 3 ]`, read by a `case` that matches past the head: 2 + 3.0 logs 5.
  - `caseProduct` is the same product with the list literal written directly as
    the `case` subject: it logs 5.

Results

elm-0.19.1

passran in 57 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.1/optimize for better performance and smaller assets.
scalar: 9
listSum: 15
tupleFirst: 3
tupleCase: 5
listHead: 3
listTail: 5
caseProduct: 5

elm-0.19.2

passran in 55 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.2/optimize for better performance and smaller assets.
scalar: 9
listSum: 15
tupleFirst: 3
tupleCase: 5
listHead: 3
listTail: 5
caseProduct: 5

elm-0.19.3

passran in 46 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.3/optimize for better performance and smaller assets.
scalar: 9
listSum: 15
tupleFirst: 3
tupleCase: 5
listHead: 3
listTail: 5
caseProduct: 5

lamdera-1.5.0

passran in 62 ms
Output
scalar: 9
listSum: 15
tupleFirst: 3
tupleCase: 5
listHead: 3
listTail: 5
caseProduct: 5

Discussions

Discuss this test·Search the discussions on GitHub

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

Source

The -- CHECK: lines give the expected output.

module LetNumberDualUseTest exposing (main)
{-| An unannotated let-bound `number` value can be used as an `Int` and as a
`Float` in the same expression, because Elm generalizes the type of a `let`
binding and each use picks its own type.
Each case is a separate top-level value that computes
`round (toFloat (x + 0) + x * k)`: the `toFloat (x + 0)` use makes `x` an `Int`
and the `x * k` use, with `k` a `Float` literal, makes it a `Float`.
- `scalar` uses `n = 5` directly: 5 + 4.0 logs 9.
- `listSum` uses `List.sum` of `[ 1, 2, 3 ]`: 6 + 9.0 logs 15.
- `tupleFirst` uses `Tuple.first` of `( 1, 2 )`: 1 + 1.5 rounds to 3.
- `tupleCase` uses the product of a `( 1, 2 )` tuple destructured by `case`:
2 + 3.0 logs 5.
- `listHead` uses the head of `[ 1, 2, 3 ]` taken by a `case`: 1 + 1.5 rounds
to 3.
- `listTail` uses the product of the first two elements of a let-bound
`[ 1, 2, 3 ]`, read by a `case` that matches past the head: 2 + 3.0 logs 5.
- `caseProduct` is the same product with the list literal written directly as
the `case` subject: it logs 5.
-}
-- TAGS: elm-spec, s-dec-group, s-atexp-longvar, s-atexp-scon, d-atexp-scon
-- CHECK: scalar: 9
-- CHECK: listSum: 15
-- CHECK: tupleFirst: 3
-- CHECK: tupleCase: 5
-- CHECK: listHead: 3
-- CHECK: listTail: 5
-- CHECK: caseProduct: 5
import Html exposing (text)
scalar : Int
scalar =
let
n =
5
in
round (toFloat (n + 0) + n * 0.8)
listSum : Int
listSum =
let
xs =
[ 1, 2, 3 ]
in
round (toFloat (List.sum xs + 0) + List.sum xs * 1.5)
tupleFirst : Int
tupleFirst =
let
p =
( 1, 2 )
in
round (toFloat (Tuple.first p + 0) + Tuple.first p * 1.5)
tupleCase : Int
tupleCase =
let
t =
( 1, 2 )
prod =
case t of
( a, b ) ->
a * b
in
round (toFloat (prod + 0) + prod * 1.5)
listHead : Int
listHead =
let
xs =
[ 1, 2, 3 ]
h =
case xs of
a :: _ ->
a
_ ->
0
in
round (toFloat (h + 0) + h * 1.5)
listTail : Int
listTail =
let
xs =
[ 1, 2, 3 ]
prod =
case xs of
a :: b :: _ ->
a * b
_ ->
0
in
round (toFloat (prod + 0) + prod * 1.5)
caseProduct : Int
caseProduct =
let
prod =
case [ 1, 2, 3 ] of
a :: b :: _ ->
a * b
_ ->
0
in
round (toFloat (prod + 0) + prod * 1.5)
main =
let
_ =
Debug.log "scalar" scalar
_ =
Debug.log "listSum" listSum
_ =
Debug.log "tupleFirst" tupleFirst
_ =
Debug.log "tupleCase" tupleCase
_ =
Debug.log "listHead" listHead
_ =
Debug.log "listTail" listTail
_ =
Debug.log "caseProduct" caseProduct
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_LetNumberDualUseTest, in StandardElmMech/Generated/Elm_LetN.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. It uses values of the core library, which the book leaves outside the definition: List.sum, Tuple.first. Lean evaluates each by its Elm text in elm/core 1.0.5, so the theorems about this program also trust that text ([library]).

/-- tests/elm/src/LetNumberDualUseTest.elm -/
def g_elm_LetNumberDualUseTest : Program := {
  name := "LetNumberDualUseTest", source := "tests/elm/src", native := true,
  library := ["List.sum", "Tuple.first"],
  exposing := (.items [.var "main"]), imports := [⟨"Html", "Html", some (.items [.var "text"])⟩],
  types := [],
  aliases := [],
  defs := [
    .fn "scalar" [] (letDefs [.fn "n" [] (.atexp (.scon (.intF 5))), .fn "n$Int" [] (i 5)] (ap (v "round") [.atexp (.paren (bin "+" (ap (v "toFloat") [.atexp (.paren (bin "+" (v "n$Int") (i 0)))]) (bin "*" (v "n") (.atexp (.scon (.float 8 (-1)))))))])),
    .fn "listSum" [] (letDefs [.fn "xs" [] (listE [.atexp (.scon (.intF 1)), .atexp (.scon (.intF 2)), .atexp (.scon (.intF 3))]), .fn "xs$Int" [] (listE [i 1, i 2, i 3])] (ap (v "round") [.atexp (.paren (bin "+" (ap (v "toFloat") [.atexp (.paren (bin "+" (ap (qv "List" "sum$Int") [v "xs$Int"]) (i 0)))]) (bin "*" (ap (qv "List" "sum") [v "xs"]) (.atexp (.scon (.float 15 (-1)))))))])),
    .fn "tupleFirst" [] (letDefs [.fn "p" [] (tup2 (.atexp (.scon (.intF 1))) (.atexp (.scon (.intF 2)))), .fn "p$Int$Float" [] (tup2 (i 1) (.atexp (.scon (.intF 2))))] (ap (v "round") [.atexp (.paren (bin "+" (ap (v "toFloat") [.atexp (.paren (bin "+" (ap (qv "Tuple" "first") [v "p$Int$Float"]) (i 0)))]) (bin "*" (ap (qv "Tuple" "first") [v "p"]) (.atexp (.scon (.float 15 (-1)))))))])),
    .fn "tupleCase" [] (letDefs [.fn "t" [] (tup2 (.atexp (.scon (.intF 1))) (.atexp (.scon (.intF 2)))), .fn "t$Int$Int" [] (tup2 (i 1) (i 2)), .fn "prod" [] (caseOf (v "t") [(P (ptup2 (P (pv "a")) (P (pv "b"))), bin "*" (v "a") (v "b"))]), .fn "prod$Int" [] (caseOf (v "t$Int$Int") [(P (ptup2 (P (pv "a")) (P (pv "b"))), bin "*" (v "a") (v "b"))])] (ap (v "round") [.atexp (.paren (bin "+" (ap (v "toFloat") [.atexp (.paren (bin "+" (v "prod$Int") (i 0)))]) (bin "*" (v "prod") (.atexp (.scon (.float 15 (-1)))))))])),
    .fn "listHead" [] (letDefs [.fn "xs" [] (listE [.atexp (.scon (.intF 1)), .atexp (.scon (.intF 2)), .atexp (.scon (.intF 3))]), .fn "xs$Int" [] (listE [i 1, i 2, i 3]), .fn "h" [] (caseOf (v "xs") [(.cons (P (pv "a")) (P pw), v "a"), (P pw, .atexp (.scon (.intF 0)))]), .fn "h$Int" [] (caseOf (v "xs$Int") [(.cons (P (pv "a")) (P pw), v "a"), (P pw, i 0)])] (ap (v "round") [.atexp (.paren (bin "+" (ap (v "toFloat") [.atexp (.paren (bin "+" (v "h$Int") (i 0)))]) (bin "*" (v "h") (.atexp (.scon (.float 15 (-1)))))))])),
    .fn "listTail" [] (letDefs [.fn "xs" [] (listE [.atexp (.scon (.intF 1)), .atexp (.scon (.intF 2)), .atexp (.scon (.intF 3))]), .fn "xs$Int" [] (listE [i 1, i 2, i 3]), .fn "prod" [] (caseOf (v "xs") [(.cons (P (pv "a")) (.cons (P (pv "b")) (P pw)), bin "*" (v "a") (v "b")), (P pw, .atexp (.scon (.intF 0)))]), .fn "prod$Int" [] (caseOf (v "xs$Int") [(.cons (P (pv "a")) (.cons (P (pv "b")) (P pw)), bin "*" (v "a") (v "b")), (P pw, i 0)])] (ap (v "round") [.atexp (.paren (bin "+" (ap (v "toFloat") [.atexp (.paren (bin "+" (v "prod$Int") (i 0)))]) (bin "*" (v "prod") (.atexp (.scon (.float 15 (-1)))))))])),
    .fn "caseProduct" [] (letDefs [.fn "prod" [] (caseOf (listE [.atexp (.scon (.intF 1)), .atexp (.scon (.intF 2)), .atexp (.scon (.intF 3))]) [(.cons (P (pv "a")) (.cons (P (pv "b")) (P pw)), bin "*" (v "a") (v "b")), (P pw, .atexp (.scon (.intF 0)))]), .fn "prod$Int" [] (caseOf (listE [i 1, i 2, i 3]) [(.cons (P (pv "a")) (.cons (P (pv "b")) (P pw)), bin "*" (v "a") (v "b")), (P pw, i 0)])] (ap (v "round") [.atexp (.paren (bin "+" (ap (v "toFloat") [.atexp (.paren (bin "+" (v "prod$Int") (i 0)))]) (bin "*" (v "prod") (.atexp (.scon (.float 15 (-1)))))))])),
    .fn "main" [] (letDefs [.pat pw (ap (qv "Debug" "log") [str "scalar", v "scalar"]), .pat pw (ap (qv "Debug" "log") [str "listSum", v "listSum"]), .pat pw (ap (qv "Debug" "log") [str "tupleFirst", v "tupleFirst"]), .pat pw (ap (qv "Debug" "log") [str "tupleCase", v "tupleCase"]), .pat pw (ap (qv "Debug" "log") [str "listHead", v "listHead"]), .pat pw (ap (qv "Debug" "log") [str "listTail", v "listTail"]), .pat pw (ap (qv "Debug" "log") [str "caseProduct", v "caseProduct"])] (ap (v "text") [str "done"])) ],
  checks := [
    chk .check "scalar: 9" .int,
    chk .check "listSum: 15" .int,
    chk .check "tupleFirst: 3" .int,
    chk .check "tupleCase: 5" .int,
    chk .check "listHead: 3" .int,
    chk .check "listTail: 5" .int,
    chk .check "caseProduct: 5" .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_LetNumberDualUseTest 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_LetNumberDualUseTest 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_LetNumberDualUseTest states that the program has a meaning by r:p-program, with both premises, and ident_elm_LetNumberDualUseTest that its specialisation is unique and is the reduced program above, both with no hypothesis about principality. One of these theorems is proved by native_decide ([native]).