Elm Exactly
Results / elm /

WideRecordPatternTest

Tags:elm-spec

Checks, in A Standard Definition of Elm?:rule d-atexp-recordrule d-atexp-updaterule d-atpat-recordrule d-patrow-field

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 record with 28 `Int` fields reads back correctly through field access,
through a record pattern, and after a record update.

`make 1` sets field `fNN` to 1000 + NN. The test checks:

  - Access to `f27` and `f25`.
  - `viaPat`, which destructures `{ f27, f25 }` and returns `f27 * 1000 + f25`.
  - `upd`, which increments `f27` and sets `f26` to 7, read by access and by
    `viaPat`.

Results

elm-0.19.1

passran in 55 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.1/optimize for better performance and smaller assets.
access f27: 1027
access f25: 1025
pattern: 1028025
update f27: 1028
update f26: 7
update pattern: 1029025

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.
access f27: 1027
access f25: 1025
pattern: 1028025
update f27: 1028
update f26: 7
update pattern: 1029025

elm-0.19.3

passran in 44 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.3/optimize for better performance and smaller assets.
update pattern: 1029025
update f26: 7
update f27: 1028
pattern: 1028025
access f25: 1025
access f27: 1027

lamdera-1.5.0

passran in 40 ms
Output
update pattern: 1029025
update f26: 7
update f27: 1028
pattern: 1028025
access f25: 1025
access f27: 1027

Discussions

Discuss this test·Search the discussions on GitHub

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

Source

The -- CHECK: lines give the expected output.

module WideRecordPatternTest exposing (main)
{-| A record with 28 `Int` fields reads back correctly through field access,
through a record pattern, and after a record update.
`make 1` sets field `fNN` to 1000 + NN. The test checks:
- Access to `f27` and `f25`.
- `viaPat`, which destructures `{ f27, f25 }` and returns `f27 * 1000 + f25`.
- `upd`, which increments `f27` and sets `f26` to 7, read by access and by
`viaPat`.
-}
-- TAGS: elm-spec, d-atexp-record, d-atexp-update, d-atpat-record, d-patrow-field
-- CHECK: access f27: 1027
-- CHECK: access f25: 1025
-- CHECK: pattern: 1028025
-- CHECK: update f27: 1028
-- CHECK: update f26: 7
-- CHECK: update pattern: 1029025
import Html exposing (text)
type alias R =
{ f00 : Int
, f01 : Int
, f02 : Int
, f03 : Int
, f04 : Int
, f05 : Int
, f06 : Int
, f07 : Int
, f08 : Int
, f09 : Int
, f10 : Int
, f11 : Int
, f12 : Int
, f13 : Int
, f14 : Int
, f15 : Int
, f16 : Int
, f17 : Int
, f18 : Int
, f19 : Int
, f20 : Int
, f21 : Int
, f22 : Int
, f23 : Int
, f24 : Int
, f25 : Int
, f26 : Int
, f27 : Int
}
make : Int -> R
make base =
{ f00 = base + 999
, f01 = base + 1000
, f02 = base + 1001
, f03 = base + 1002
, f04 = base + 1003
, f05 = base + 1004
, f06 = base + 1005
, f07 = base + 1006
, f08 = base + 1007
, f09 = base + 1008
, f10 = base + 1009
, f11 = base + 1010
, f12 = base + 1011
, f13 = base + 1012
, f14 = base + 1013
, f15 = base + 1014
, f16 = base + 1015
, f17 = base + 1016
, f18 = base + 1017
, f19 = base + 1018
, f20 = base + 1019
, f21 = base + 1020
, f22 = base + 1021
, f23 = base + 1022
, f24 = base + 1023
, f25 = base + 1024
, f26 = base + 1025
, f27 = base + 1026
}
viaPat : R -> Int
viaPat { f27, f25 } =
f27 * 1000 + f25
upd : R -> R
upd r =
{ r | f27 = r.f27 + 1, f26 = 7 }
main =
let
base =
1 + (Char.toCode 'b' - Char.toCode 'a') - 1
r =
make base
_ =
Debug.log "access f27" (r.f27)
_ =
Debug.log "access f25" (r.f25)
_ =
Debug.log "pattern" (viaPat r)
_ =
Debug.log "update f27" ((upd r).f27)
_ =
Debug.log "update f26" ((upd r).f26)
_ =
Debug.log "update pattern" (viaPat (upd r))
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_WideRecordPatternTest, in StandardElmMech/Generated/Elm_TailRecT.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
access f2710271027t_elm_WideRecordPatternTest
access f2510251025t_elm_WideRecordPatternTest
pattern10280251028025t_elm_WideRecordPatternTest
update f2710281028t_elm_WideRecordPatternTest
update f2677t_elm_WideRecordPatternTest
update pattern10290251029025t_elm_WideRecordPatternTest
/-- tests/elm/src/WideRecordPatternTest.elm -/
def g_elm_WideRecordPatternTest : Program := {
  name := "WideRecordPatternTest", source := "tests/elm/src",
  exposing := (.items [.var "main"]), imports := [⟨"Html", "Html", some (.items [.var "text"])⟩],
  types := [],
  aliases := [("R", some ["f00", "f01", "f02", "f03", "f04", "f05", "f06", "f07", "f08", "f09", "f10", "f11", "f12", "f13", "f14", "f15", "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23", "f24", "f25", "f26", "f27"])],
  defs := [
    .fn "make" [pv "base"] (recE [("f00", bin "+" (v "base") (i 999)), ("f01", bin "+" (v "base") (i 1000)), ("f02", bin "+" (v "base") (i 1001)), ("f03", bin "+" (v "base") (i 1002)), ("f04", bin "+" (v "base") (i 1003)), ("f05", bin "+" (v "base") (i 1004)), ("f06", bin "+" (v "base") (i 1005)), ("f07", bin "+" (v "base") (i 1006)), ("f08", bin "+" (v "base") (i 1007)), ("f09", bin "+" (v "base") (i 1008)), ("f10", bin "+" (v "base") (i 1009)), ("f11", bin "+" (v "base") (i 1010)), ("f12", bin "+" (v "base") (i 1011)), ("f13", bin "+" (v "base") (i 1012)), ("f14", bin "+" (v "base") (i 1013)), ("f15", bin "+" (v "base") (i 1014)), ("f16", bin "+" (v "base") (i 1015)), ("f17", bin "+" (v "base") (i 1016)), ("f18", bin "+" (v "base") (i 1017)), ("f19", bin "+" (v "base") (i 1018)), ("f20", bin "+" (v "base") (i 1019)), ("f21", bin "+" (v "base") (i 1020)), ("f22", bin "+" (v "base") (i 1021)), ("f23", bin "+" (v "base") (i 1022)), ("f24", bin "+" (v "base") (i 1023)), ("f25", bin "+" (v "base") (i 1024)), ("f26", bin "+" (v "base") (i 1025)), ("f27", bin "+" (v "base") (i 1026))]),
    .fn "viaPat" [precord [("f27", "f27"), ("f25", "f25")]] (bin "+" (bin "*" (v "f27") (i 1000)) (v "f25")),
    .fn "upd" [pv "r"] (updE "r" [("f27", bin "+" (.atexp (access (A (v "r")) "f27")) (i 1)), ("f26", i 7)]),
    .fn "main" [] (letDefs [.fn "base" [] (bin "-" (bin "+" (i 1) (.atexp (.paren (bin "-" (ap (qv "Char" "toCode") [.atexp (chA 'b')]) (ap (qv "Char" "toCode") [.atexp (chA 'a')]))))) (i 1)), .fn "r" [] (ap (v "make") [v "base"]), .pat pw (ap (qv "Debug" "log") [str "access f27", .atexp (.paren (.atexp (access (A (v "r")) "f27")))]), .pat pw (ap (qv "Debug" "log") [str "access f25", .atexp (.paren (.atexp (access (A (v "r")) "f25")))]), .pat pw (ap (qv "Debug" "log") [str "pattern", .atexp (.paren (ap (v "viaPat") [v "r"]))]), .pat pw (ap (qv "Debug" "log") [str "update f27", .atexp (.paren (.atexp (access (.paren (ap (v "upd") [v "r"])) "f27")))]), .pat pw (ap (qv "Debug" "log") [str "update f26", .atexp (.paren (.atexp (access (.paren (ap (v "upd") [v "r"])) "f26")))]), .pat pw (ap (qv "Debug" "log") [str "update pattern", .atexp (.paren (ap (v "viaPat") [.atexp (.paren (ap (v "upd") [v "r"]))]))])] (ap (v "text") [str "done"])) ],
  checks := [
    chk .check "access f27: 1027" .int,
    chk .check "access f25: 1025" .int,
    chk .check "pattern: 1028025" .int,
    chk .check "update f27: 1028" .int,
    chk .check "update f26: 7" .int,
    chk .check "update pattern: 1029025" .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_WideRecordPatternTest 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_WideRecordPatternTest 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_WideRecordPatternTest states that the program has a meaning by r:p-program, with both premises, and ident_elm_WideRecordPatternTest that its specialisation is unique and is the reduced program above, both with no hypothesis about principality.