Elm Exactly
Results / elm /

TypeMismatchIfBranchTest

Tags:elm19-compile-errorelm-spec

Checks, in A Standard Definition of Elm?:rule s-match

Not translated into Lean for the mechanisation of the book: see why.

elm-0.19.1 passelm-0.19.2 passelm-0.19.3 passlamdera-1.5.0 pass
The compiler rejects an `if` whose `then` branch is a number and whose `else` branch is a `String`, with a TYPE MISMATCH error.

Results

elm-0.19.1

pass
Output
Compiling ...-- TYPE MISMATCH ------------------------------ src/TypeMismatchIfBranchTest.elm

The 2nd branch of this `if` does not match all the previous branches:

11| x = if True then 1 else "a"
                            ^^^
The 2nd branch is a string of type:

    String

But all the previous branches result in:

    number

Hint: All branches in an `if` must produce the same type of values. This way, no
matter which branch we take, the result is always a consistent shape. Read
<https://elm-lang.org/0.19.1/custom-types> to learn how to “mix” types.

Hint: Try using String.toInt to convert it to an integer?

Detected problems in 1 module.

elm-0.19.2

pass
Output
Compiling ...-- TYPE MISMATCH ------------------------------ src/TypeMismatchIfBranchTest.elm

The 2nd branch of this `if` does not match all the previous branches:

11| x = if True then 1 else "a"
                            ^^^
The 2nd branch is a string of type:

    String

But all the previous branches result in:

    number

Hint: All branches in an `if` must produce the same type of values. This way, no
matter which branch we take, the result is always a consistent shape. Read
<https://elm-lang.org/0.19.2/custom-types> to learn how to “mix” types.

Hint: Try using String.toInt to convert it to an integer?

Detected problems in 1 module.

elm-0.19.3

pass
Output
Compiling ...-- TYPE MISMATCH ------------------------------ src/TypeMismatchIfBranchTest.elm

The 2nd branch of this `if` does not match all the previous branches:

11| x = if True then 1 else "a"
                            ^^^
The 2nd branch is a string of type:

    String

But all the previous branches result in:

    number

Hint: All branches in an `if` must produce the same type of values. This way, no
matter which branch we take, the result is always a consistent shape. Read
<https://elm-lang.org/0.19.3/custom-types> to learn how to “mix” types.

Hint: Try using String.toInt to convert it to an integer?

Detected problems in 1 module.

lamdera-1.5.0

pass
Output
Detected problems in 1 module.
-- TYPE MISMATCH ------------------------------ src/TypeMismatchIfBranchTest.elm

The 2nd branch of this `if` does not match all the previous branches:

11| x = if True then 1 else "a"
                            ^^^
The 2nd branch is a string of type:

    String

But all the previous branches result in:

    number

Hint: All branches in an `if` must produce the same type of values. This way, no
matter which branch we take, the result is always a consistent shape. Read
<https://elm-lang.org/0.19.3/custom-types> to learn how to “mix” types.

Hint: Try using String.toInt to convert it to an integer?

Discussions

Discuss this test·Search the discussions on GitHub

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

Source

The -- CHECK: lines give the expected output.

module TypeMismatchIfBranchTest exposing (..)
{-| The compiler rejects an `if` whose `then` branch is a number and whose `else` branch is a `String`, with a TYPE MISMATCH error.
-}
-- TAGS: elm19-compile-error
-- TAGS: elm-spec, s-match
-- COMPILE-ERROR: TYPE MISMATCH, if branch
-- CHECK: -- TYPE MISMATCH -
x = if True then 1 else "a"

Lean translation

No reduced program for Lean's evaluator: the test expects a compile error, so it has no meaning to evaluate. Lean has the program as written, in source syntax, and proves what the static semantics says of it (below). See the Lean mechanisation for which tests are in Lean, and why.

Static semantics: Lean proves that the program as written is not legal, so a compiler must reject it (refuse_elm_TypeMismatchIfBranchTest, in StandardElmMech/Certs/). The kernel finds a core of the program's typing constraints that has no solution, so no derivation types it (core refutation, plan step 11) (Stat.not_legal_of_coreRefutes). It holds for every derivation, with no hypothesis about principality.