Elm Exactly
Results / elm /

CompilerIssue1964AnnotatedPolymorphicRecursionWrapperTest

Tags:elm19-bugelm-spec

Checks, in A Standard Definition of Elm?:rule sm-topdec-valuerule s-valbind-var

Translated into Lean for the mechanisation of the book: see its Lean translation.

elm-0.19.1 failelm-0.19.2 failelm-0.19.3 faillamdera-1.5.0 fail

Issue: elm/compiler#1964

A recursive call at a wrapped type argument is rejected despite the annotation.

<https://github.com/elm/compiler/issues/1964>

`depth : Nested a -> Int` is annotated, so the recursive call at `Nested (Wrapper a)` is
well-typed (polymorphic recursion with an annotation). Elm 0.19.1 reports a type mismatch
(`Nested (Wrapper a)` vs `Nested a`). It should compile, and `depth` of the two-level `value`
is 2.

Results

elm-0.19.1

fail
compile failed (exit 1):
Dependencies ready!
Compiling ...-- TYPE MISMATCH - src/CompilerIssue1964AnnotatedPolymorphicRecursionWrapperTest.elm

The 1st argument to `depth` is not what I expect:

34|             1 + depth inner
                          ^^^^^
This `inner` value is a:

    Nested (Wrapper a)

But `depth` needs the 1st argument to be:

    Nested a

Hint: Your type annotation uses type variable `a` which means ANY type of value
can flow through, but your code is saying it specifically wants a `Wrapper`
value. Maybe change your type annotation to be more specific? Maybe change the
code to be more general?

Read <https://elm-lang.org/0.19.1/type-annotations> for more advice!

Detected problems in 1 module.

elm-0.19.2

fail
compile failed (exit 1):
Dependencies ready!
Compiling ...-- TYPE MISMATCH - src/CompilerIssue1964AnnotatedPolymorphicRecursionWrapperTest.elm

The 1st argument to `depth` is not what I expect:

34|             1 + depth inner
                          ^^^^^
This `inner` value is a:

    Nested (Wrapper a)

But `depth` needs the 1st argument to be:

    Nested a

Hint: Your type annotation uses type variable `a` which means ANY type of value
can flow through, but your code is saying it specifically wants a `Wrapper`
value. Maybe change your type annotation to be more specific? Maybe change the
code to be more general?

Read <https://elm-lang.org/0.19.2/type-annotations> for more advice!

Detected problems in 1 module.

elm-0.19.3

fail
compile failed (exit 1):
Compiling ...-- TYPE MISMATCH - src/CompilerIssue1964AnnotatedPolymorphicRecursionWrapperTest.elm

The 1st argument to `depth` is not what I expect:

34|             1 + depth inner
                          ^^^^^
This `inner` value is a:

    Nested (Wrapper a)

But `depth` needs the 1st argument to be:

    Nested a

Hint: Your type annotation uses type variable `a` which means ANY type of value
can flow through, but your code is saying it specifically wants a `Wrapper`
value. Maybe change your type annotation to be more specific? Maybe change the
code to be more general?

Read <https://elm-lang.org/0.19.3/type-annotations> for more advice!

Detected problems in 1 module.

lamdera-1.5.0

fail
compile failed (exit 1):
Detected problems in 1 module.
-- TYPE MISMATCH - src/CompilerIssue1964AnnotatedPolymorphicRecursionWrapperTest.elm

The 1st argument to `depth` is not what I expect:

34|             1 + depth inner
                          ^^^^^
This `inner` value is a:

    Nested (Wrapper a)

But `depth` needs the 1st argument to be:

    Nested a

Hint: Your type annotation uses type variable `a` which means ANY type of value
can flow through, but your code is saying it specifically wants a `Wrapper`
value. Maybe change your type annotation to be more specific? Maybe change the
code to be more general?

Read <https://elm-lang.org/0.19.3/type-annotations> for more advice!

Discussions

Discuss this test·Search the discussions on GitHub

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

Source

The -- CHECK: lines give the expected output.

module CompilerIssue1964AnnotatedPolymorphicRecursionWrapperTest exposing (..)
{-| A recursive call at a wrapped type argument is rejected despite the annotation.
<https://github.com/elm/compiler/issues/1964>
`depth : Nested a -> Int` is annotated, so the recursive call at `Nested (Wrapper a)` is
well-typed (polymorphic recursion with an annotation). Elm 0.19.1 reports a type mismatch
(`Nested (Wrapper a)` vs `Nested a`). It should compile, and `depth` of the two-level `value`
is 2.
-}
-- TAGS: elm19-bug
-- TAGS: elm-spec, sm-topdec-value, s-valbind-var
-- CHECK: depth: 2
import Html exposing (text)
type Nested a
= Nested (Nested (Wrapper a))
| Leaf a
type Wrapper a
= Wrapper a
depth : Nested a -> Int
depth thing =
case thing of
Nested inner ->
1 + depth inner
Leaf _ ->
0
value : Nested ()
value =
Nested (Nested (Leaf (Wrapper (Wrapper ()))))
main =
let
_ =
Debug.log "depth" (depth value)
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_CompilerIssue1964AnnotatedPolymorphicRecursionWrapperTest, in StandardElmMech/Generated/Elm_CombinatorSP.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
depth22t_elm_CompilerIssue1964AnnotatedPolymorphicRecursionWrapperTest
/-- tests/elm/src/CompilerIssue1964AnnotatedPolymorphicRecursionWrapperTest.elm -/
def g_elm_CompilerIssue1964AnnotatedPolymorphicRecursionWrapperTest : Program := {
  name := "CompilerIssue1964AnnotatedPolymorphicRecursionWrapperTest", source := "tests/elm/src",
  exposing := .all, imports := [⟨"Html", "Html", some (.items [.var "text"])⟩],
  types := [("Nested", [("Nested", 1), ("Leaf", 1)]), ("Wrapper", [("Wrapper", 1)])],
  aliases := [],
  defs := [
    .fn "depth" [pv "thing"] (caseOf (v "thing") [(pcon "Nested" [pv "inner"], bin "+" (i 1) (ap (v "depth") [v "inner"])), (pcon "Leaf" [pw], i 0)]),
    .fn "value" [] (ap (c "Nested") [.atexp (.paren (ap (c "Nested") [.atexp (.paren (ap (c "Leaf") [.atexp (.paren (ap (c "Wrapper") [.atexp (.paren (ap (c "Wrapper") [.atexp .unit]))]))]))]))]),
    .fn "main" [] (letDefs [.pat pw (ap (qv "Debug" "log") [str "depth", .atexp (.paren (ap (v "depth") [v "value"]))])] (ap (v "text") [str "done"])) ],
  checks := [
    chk .check "depth: 2" .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_CompilerIssue1964AnnotatedPolymorphicRecursionWrapperTest 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_CompilerIssue1964AnnotatedPolymorphicRecursionWrapperTest 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_CompilerIssue1964AnnotatedPolymorphicRecursionWrapperTest states that the program has a meaning by r:p-program, with both premises, and ident_elm_CompilerIssue1964AnnotatedPolymorphicRecursionWrapperTest that its specialisation is unique and is the reduced program above, both with no hypothesis about principality.