Elm Exactly
Results / elm /

ImportClosedTypeOpenItemTest

Tags:elm-spec

Checks, in A Standard Definition of Elm?:rule sm-item-opentyperule dm-expitem-open

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

Issue: elm/compiler#1979

`import M exposing (T(..))` of a type that `M` exposes closed imports the type and no
constructors.

A Standard Definition of Elm?, rules sm-item-opentype and dm-expitem-open: the item elaborates, and
imports the constructors that `M` exposes, here none. The program uses `Closed` only as a type, so
it is valid. This is the book's counterpart of <https://github.com/elm/compiler/issues/1979>,
whose test (`CompilerIssue1979ImportOpaqueConstructorsTest`) expects the import to be rejected.
<https://elm-test-suite.netlify.app/standard/rules/sm-item-opentype>

Results

elm-0.19.1

passran in 86 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.1/optimize for better performance and smaller assets.
closed: 9

elm-0.19.2

passran in 74 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.2/optimize for better performance and smaller assets.
closed: 9

elm-0.19.3

passran in 64 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.3/optimize for better performance and smaller assets.
closed: 9

lamdera-1.5.0

passran in 60 ms
Output
closed: 9

Discussions

Discuss this test·Search the discussions on GitHub

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

Source

The -- CHECK: lines give the expected output.

module ImportClosedTypeOpenItemTest exposing (main)
{-| `import M exposing (T(..))` of a type that `M` exposes closed imports the type and no
constructors.
A Standard Definition of Elm?, rules sm-item-opentype and dm-expitem-open: the item elaborates, and
imports the constructors that `M` exposes, here none. The program uses `Closed` only as a type, so
it is valid. This is the book's counterpart of <https://github.com/elm/compiler/issues/1979>,
whose test (`CompilerIssue1979ImportOpaqueConstructorsTest`) expects the import to be rejected.
<https://elm-test-suite.netlify.app/standard/rules/sm-item-opentype>
-}
-- TAGS: elm-spec, sm-item-opentype, dm-expitem-open
-- CHECK: closed: 9
import Html
import ModuleExposingHelper exposing (Closed(..))
roundTrip : Int -> Closed
roundTrip n =
ModuleExposingHelper.makeClosed n
main =
let
_ =
Debug.log "closed" (ModuleExposingHelper.unwrapClosed (roundTrip 9))
in
Html.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_ImportClosedTypeOpenItemTest, in StandardElmMech/Generated/Elm_Fli.lean). Lean's evaluator runs the program by the rules of Chapters 7 and 9, and its modules' 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. Its modules, in program order: tests/elm/src/ModuleExposingHelper.elm, tests/elm/src/ImportClosedTypeOpenItemTest.elm.

CheckExpected (CHECK)Lean evaluatorProved by
closed99t_elm_ImportClosedTypeOpenItemTest
/-- tests/elm/src/ImportClosedTypeOpenItemTest.elm, tests/elm/src/ModuleExposingHelper.elm -/
def g_elm_ImportClosedTypeOpenItemTest : Program := {
  name := "ImportClosedTypeOpenItemTest", source := "tests/elm/src",
  exposing := (.items [.var "main"]), imports := [⟨"Html", "Html", none⟩, ⟨"ModuleExposingHelper", "ModuleExposingHelper", some (.items [.opened "Closed"])⟩],
  types := [],
  aliases := [],
  defs := [
    .fn "roundTrip" [pv "n"] (ap (qv "ModuleExposingHelper" "makeClosed") [v "n"]),
    .fn "main" [] (letDefs [.pat pw (ap (qv "Debug" "log") [str "closed", .atexp (.paren (ap (qv "ModuleExposingHelper" "unwrapClosed") [.atexp (.paren (ap (v "roundTrip") [i 9]))]))])] (ap (qv "Html" "text") [str "done"])) ],
  helpers := [
   { name := "ModuleExposingHelper", path := "tests/elm/src/ModuleExposingHelper.elm",
     exposing := (.items [.tycon "Closed", .opened "Open", .tycon "Point", .tycon "Shape", .var "double", .var "exposedCount", .var "makeClosed", .var "makeShape", .var "unwrapClosed", .var "unwrapShape"]), imports := [],
     types := [("Closed", [("Closed", 1)]), ("Open", [("One", 0), ("Two", 1)]), ("Shape", [("Shape", 1)])],
     aliases := [("Point", some ["x", "y"])],
     defs := [
      .fn "hidden" [] (i 100),
      .fn "double" [pv "n"] (bin "*" (i 2) (v "n")),
      .fn "exposedCount" [] (bin "+" (v "hidden") (i 1)),
      .fn "makeClosed" [pv "n"] (ap (c "Closed") [v "n"]),
      .fn "unwrapClosed" [.paren (pcon "Closed" [pv "n"])] (v "n"),
      .fn "makeShape" [pv "n"] (ap (c "Shape") [v "n"]),
      .fn "unwrapShape" [.paren (pcon "Shape" [pv "n"])] (v "n") ] } ],
  checks := [
    chk .check "closed: 9" .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_ImportClosedTypeOpenItemTest 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_ImportClosedTypeOpenItemTest 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_ImportClosedTypeOpenItemTest states that the program has a meaning by r:p-program, with both premises, and ident_elm_ImportClosedTypeOpenItemTest that its specialisation is unique and is the reduced program above, both with no hypothesis about principality.