RecordNarrow03FullAnnotationTest
Tags:elm-spec
Checks, in A Standard Definition of Elm?:rule s-valbind-varrule s-ty-conrule d-patrow-field
Translated into Lean for the mechanisation of the book: see its Lean translation.
A let-bound helper annotated with concrete record aliases, folded over two nested records with `List.foldl`, reads the right nested field. This is the fully annotated form of the program in `RecordOpenNarrowFieldIndexTest`: `buildSibling : ( Int, Outer ) -> Int -> Int`, with `Outer` and `Inner` closed record aliases, so the helper's argument type names every field. The helper reads `captures`, `params` and `lambdaId` of `inner`; both members hold `LambdaId 999`, so the sum is 1998.
Results
elm-0.19.1
passran in 57 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.1/optimize for better performance and smaller assets. uid: 1998
elm-0.19.2
passran in 65 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.2/optimize for better performance and smaller assets. uid: 1998
elm-0.19.3
passran in 70 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.3/optimize for better performance and smaller assets. uid: 1998
lamdera-1.5.0
passran in 75 msOutput
uid: 1998
Discussions
Discuss this test·Search the discussions on GitHub
Discussions are on GitHub; one about this test has [t:elm/RecordNarrow03FullAnnotationTest] in its title.
Source
The -- CHECK: lines give the expected output.
module RecordNarrow03FullAnnotationTest exposing (main)
{-| A let-bound helper annotated with concrete record aliases, folded over two nested records with `List.foldl`, reads the right nested field.
This is the fully annotated form of the program in `RecordOpenNarrowFieldIndexTest`:
`buildSibling : ( Int, Outer ) -> Int -> Int`, with `Outer` and `Inner` closed
record aliases, so the helper's argument type names every field. The helper
reads `captures`, `params` and `lambdaId` of `inner`; both members hold
`LambdaId 999`, so the sum is 1998.
-}
-- TAGS: elm-spec, s-valbind-var, s-ty-con, d-patrow-field
-- CHECK: uid: 1998
import Html exposing (text)
type LambdaId
= LambdaId Int
type alias Inner =
{ lambdaId : LambdaId
, captures : List Int
, params : List Int
, closureKind : Maybe Int
, captureAbi : Maybe Int
}
type alias Outer =
{ name : String
, inner : Inner
, body : Int
, monoType : Int
}
main =
let
innerVal : Inner
innerVal =
{ lambdaId = LambdaId 999
, captures = []
, params = []
, closureKind = Nothing
, captureAbi = Nothing
}
outer1 : Outer
outer1 =
{ name = "test1", inner = innerVal, body = 0, monoType = 0 }
outer2 : Outer
outer2 =
{ name = "test2", inner = innerVal, body = 0, monoType = 0 }
members : List ( Int, Outer )
members =
[ ( 0, outer1 ), ( 1, outer2 ) ]
buildSibling : ( Int, Outer ) -> Int -> Int
buildSibling tup acc =
let
member =
Tuple.second tup
inner =
member.inner
_ =
List.length inner.captures
_ =
List.length inner.params
(LambdaId uid) =
inner.lambdaId
in
uid + acc
result =
List.foldl buildSibling 0 members
_ =
Debug.log "uid" result
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_RecordNarrow03FullAnnotationTest, in StandardElmMech/Generated/Elm_RecordAccessorFunctionT.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.foldl, List.length, Tuple.second. Lean evaluates each by its Elm text in elm/core 1.0.5, so the theorems about this program also trust that text ([library]).
| Check | Expected (CHECK) | Lean evaluator | Proved by |
|---|---|---|---|
uid | 1998 | 1998 | t_elm_RecordNarrow03FullAnnotationTest |
/-- tests/elm/src/RecordNarrow03FullAnnotationTest.elm -/
def g_elm_RecordNarrow03FullAnnotationTest : Program := {
name := "RecordNarrow03FullAnnotationTest", source := "tests/elm/src",
library := ["List.foldl", "List.length", "Tuple.second"],
exposing := (.items [.var "main"]), imports := [⟨"Html", "Html", some (.items [.var "text"])⟩],
types := [("LambdaId", [("LambdaId", 1)])],
aliases := [("Inner", some ["lambdaId", "captures", "params", "closureKind", "captureAbi"]), ("Outer", some ["name", "inner", "body", "monoType"])],
defs := [
.fn "main" [] (letDefs [.fn "innerVal" [] (recE [("lambdaId", ap (c "LambdaId") [i 999]), ("captures", listE []), ("params", listE []), ("closureKind", c "Nothing"), ("captureAbi", c "Nothing")]), .fn "outer1" [] (recE [("name", str "test1"), ("inner", v "innerVal"), ("body", i 0), ("monoType", i 0)]), .fn "outer2" [] (recE [("name", str "test2"), ("inner", v "innerVal"), ("body", i 0), ("monoType", i 0)]), .fn "members" [] (listE [tup2 (i 0) (v "outer1"), tup2 (i 1) (v "outer2")]), .fn "buildSibling" [pv "tup", pv "acc"] (letDefs [.fn "member" [] (ap (qv "Tuple" "second") [v "tup"]), .fn "inner" [] (.atexp (access (A (v "member")) "inner")), .pat pw (ap (qv "List" "length") [.atexp (access (A (v "inner")) "captures")]), .pat pw (ap (qv "List" "length") [.atexp (access (A (v "inner")) "params")]), .pat (.paren (pcon "LambdaId" [pv "uid"])) (.atexp (access (A (v "inner")) "lambdaId"))] (bin "+" (v "uid") (v "acc"))), .fn "result" [] (ap (qv "List" "foldl") [v "buildSibling", i 0, v "members"]), .pat pw (ap (qv "Debug" "log") [str "uid", v "result"])] (ap (v "text") [str "done"])) ],
checks := [
chk .check "uid: 1998" .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_RecordNarrow03FullAnnotationTest 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_RecordNarrow03FullAnnotationTest 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_RecordNarrow03FullAnnotationTest states that the program has a meaning by r:p-program, with both premises, and ident_elm_RecordNarrow03FullAnnotationTest that its specialisation is unique and is the reduced program above, both with no hypothesis about principality.