Elm Exactly
Results / elm /

CaseNestedRecordAccessTest

Tags:elm-spec

Checks, in A Standard Definition of Elm?:rule d-pat-conrule d-atpat-conrule d-atpat-con-failrule 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
Checks a function that destructures a constructor in its argument and then
cases on a looked-up value, calling another function that also destructures a
constructor argument and cases on a record field.

`isIrrelevantTo` takes `(Branch _ pathPatterns)`, looks a key up in the list of
pairs, and returns `True` when the key is missing. When it is found it returns
the negation of `needsCheck`, which takes `(At _ info)` and cases on the
eleven-constructor `info.node`.

- `test1` and `test4` look up absent keys and expect `True`.
- `test2` finds a `Circle`, for which `needsCheck` is `True`, and expects `False`.
- `test3` finds a `Diamond`, for which `needsCheck` is `False`, and expects `True`.

Results

elm-0.19.1

passran in 58 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.1/optimize for better performance and smaller assets.
test1: True
test2: False
test3: True
test4: True

elm-0.19.2

passran in 56 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.2/optimize for better performance and smaller assets.
test1: True
test2: False
test3: True
test4: True

elm-0.19.3

passran in 68 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.3/optimize for better performance and smaller assets.
test1: True
test2: False
test3: True
test4: True

lamdera-1.5.0

passran in 90 ms
Output
test1: True
test2: False
test3: True
test4: True

Discussions

Discuss this test·Search the discussions on GitHub

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

Source

The -- CHECK: lines give the expected output.

module CaseNestedRecordAccessTest exposing (main)
{-| Checks a function that destructures a constructor in its argument and then
cases on a looked-up value, calling another function that also destructures a
constructor argument and cases on a record field.
`isIrrelevantTo` takes `(Branch _ pathPatterns)`, looks a key up in the list of
pairs, and returns `True` when the key is missing. When it is found it returns
the negation of `needsCheck`, which takes `(At _ info)` and cases on the
eleven-constructor `info.node`.
- `test1` and `test4` look up absent keys and expect `True`.
- `test2` finds a `Circle`, for which `needsCheck` is `True`, and expects `False`.
- `test3` finds a `Diamond`, for which `needsCheck` is `False`, and expects `True`.
-}
-- TAGS: elm-spec, d-pat-con, d-atpat-con, d-atpat-con-fail, d-patrow-field
-- CHECK: test1: True
-- CHECK: test2: False
-- CHECK: test3: True
-- CHECK: test4: True
import Html exposing (text)
type Shape
= Circle
| Square
| Triangle
| Diamond
| Pentagon
| Hexagon
| Heptagon
| Octagon
| Star
| Cross
| Arrow
type alias Pos =
{ line : Int
, col : Int
}
type alias Info =
{ name : String
, node : Shape
, size : Int
}
type Located a
= At Pos a
type Branch
= Branch Int (List ( String, Located Info ))
listLookup : a -> List ( a, b ) -> Maybe b
listLookup target list =
case list of
[] ->
Nothing
( key, value ) :: rest ->
if key == target then
Just value
else
listLookup target rest
{-| Returns `True` for `Circle`, `Square` and `Triangle` and `False` for every
other shape, unwrapping the `At` constructor in its argument pattern.
-}
needsCheck : Located Info -> Bool
needsCheck (At _ info) =
case info.node of
Circle ->
True
Square ->
True
Triangle ->
True
Diamond ->
False
Pentagon ->
False
Hexagon ->
False
Heptagon ->
False
Octagon ->
False
Star ->
False
Cross ->
False
Arrow ->
False
{-| Returns `True` when the path is absent from the branch's list, and otherwise
the negation of `needsCheck` for the value found under it.
-}
isIrrelevantTo : String -> Branch -> Bool
isIrrelevantTo selectedPath (Branch _ pathPatterns) =
case listLookup selectedPath pathPatterns of
Nothing ->
True
Just val ->
not (needsCheck val)
main =
let
pos =
{ line = 1, col = 1 }
pairs =
Branch 0
[ ( "a", At pos { name = "alpha", node = Diamond, size = 10 } )
, ( "b", At pos { name = "beta", node = Circle, size = 5 } )
]
_ = Debug.log "test1" (isIrrelevantTo "missing" pairs)
_ = Debug.log "test2" (isIrrelevantTo "b" pairs)
_ = Debug.log "test3" (isIrrelevantTo "a" pairs)
_ = Debug.log "test4" (isIrrelevantTo "nonexistent" pairs)
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_CaseNestedRecordAccessTest, in StandardElmMech/Generated/Elm_CaseF.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
test1TrueTruet_elm_CaseNestedRecordAccessTest
test2FalseFalset_elm_CaseNestedRecordAccessTest
test3TrueTruet_elm_CaseNestedRecordAccessTest
test4TrueTruet_elm_CaseNestedRecordAccessTest
/-- tests/elm/src/CaseNestedRecordAccessTest.elm -/
def g_elm_CaseNestedRecordAccessTest : Program := {
  name := "CaseNestedRecordAccessTest", source := "tests/elm/src",
  exposing := (.items [.var "main"]), imports := [⟨"Html", "Html", some (.items [.var "text"])⟩],
  types := [("Branch", [("Branch", 2)]), ("Located", [("At", 2)]), ("Shape", [("Circle", 0), ("Square", 0), ("Triangle", 0), ("Diamond", 0), ("Pentagon", 0), ("Hexagon", 0), ("Heptagon", 0), ("Octagon", 0), ("Star", 0), ("Cross", 0), ("Arrow", 0)])],
  aliases := [("Info", some ["name", "node", "size"]), ("Pos", some ["line", "col"])],
  defs := [
    .fn "listLookup" [pv "target", pv "list"] (caseOf (v "list") [(P (listP []), c "Nothing"), (.cons (P (ptup2 (P (pv "key")) (P (pv "value")))) (P (pv "rest")), ifte (bin "==" (v "key") (v "target")) (ap (c "Just") [v "value"]) (ap (v "listLookup") [v "target", v "rest"]))]),
    .fn "needsCheck" [.paren (pcon "At" [pw, pv "info"])] (caseOf (.atexp (access (A (v "info")) "node")) [(P (pcA "Circle"), c "True"), (P (pcA "Square"), c "True"), (P (pcA "Triangle"), c "True"), (P (pcA "Diamond"), c "False"), (P (pcA "Pentagon"), c "False"), (P (pcA "Hexagon"), c "False"), (P (pcA "Heptagon"), c "False"), (P (pcA "Octagon"), c "False"), (P (pcA "Star"), c "False"), (P (pcA "Cross"), c "False"), (P (pcA "Arrow"), c "False")]),
    .fn "isIrrelevantTo" [pv "selectedPath", .paren (pcon "Branch" [pw, pv "pathPatterns"])] (caseOf (ap (v "listLookup") [v "selectedPath", v "pathPatterns"]) [(P (pcA "Nothing"), c "True"), (pcon "Just" [pv "val"], ap (v "not") [.atexp (.paren (ap (v "needsCheck") [v "val"]))])]),
    .fn "main" [] (letDefs [.fn "pos" [] (recE [("line", .atexp (.scon (.intF 1))), ("col", .atexp (.scon (.intF 1)))]), .fn "pos$Int$Int" [] (recE [("line", i 1), ("col", i 1)]), .fn "pairs" [] (ap (c "Branch") [i 0, listE [tup2 (str "a") (ap (c "At") [v "pos$Int$Int", recE [("name", str "alpha"), ("node", c "Diamond"), ("size", i 10)]]), tup2 (str "b") (ap (c "At") [v "pos$Int$Int", recE [("name", str "beta"), ("node", c "Circle"), ("size", i 5)]])]]), .pat pw (ap (qv "Debug" "log") [str "test1", .atexp (.paren (ap (v "isIrrelevantTo") [str "missing", v "pairs"]))]), .pat pw (ap (qv "Debug" "log") [str "test2", .atexp (.paren (ap (v "isIrrelevantTo") [str "b", v "pairs"]))]), .pat pw (ap (qv "Debug" "log") [str "test3", .atexp (.paren (ap (v "isIrrelevantTo") [str "a", v "pairs"]))]), .pat pw (ap (qv "Debug" "log") [str "test4", .atexp (.paren (ap (v "isIrrelevantTo") [str "nonexistent", v "pairs"]))])] (ap (v "text") [str "done"])) ],
  checks := [
    chk .check "test1: True" .data,
    chk .check "test2: False" .data,
    chk .check "test3: True" .data,
    chk .check "test4: True" .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_CaseNestedRecordAccessTest 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_CaseNestedRecordAccessTest 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_CaseNestedRecordAccessTest states that the program has a meaning by r:p-program, with both premises, and ident_elm_CaseNestedRecordAccessTest that its specialisation is unique and is the reduced program above, both with no hypothesis about principality.