elm / compiler

Compiler for Elm, a functional language for reliable webapps.
https://elm-lang.org/
BSD 3-Clause "New" or "Revised" License
7.48k stars 659 forks source link

Type error for correct code using a polymorphic recursive function #2275

Open jefelino opened 1 year ago

jefelino commented 1 year ago

The compiler complains if a polymorphic function recursively calls the same function with a different type parameter.

SSCCE

silly : List a -> String
silly xs =
    case xs of
        [] ->
            "Hello, world!"

        x :: rest ->
            silly (List.map Just rest)

So we should have silly [1,2,3] = silly [Just 2, Just 3] = silly [Just (Just 3)] = silly [] = "Hello, world!".

Running elm make gives this output:

-- TYPE MISMATCH ------------------------------------------------- src/Silly.elm

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

11|             silly (List.map Just rest)
                       ^^^^^^^^^^^^^^^^^^
This `map` call produces:

    List (Maybe a)

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

    List a

For a workaround, the following compiles.

silly : List a -> String
silly xs =
    case xs of
        [] ->
            "Hello, world!"

        x :: rest ->
            silly2 (List.map Just rest)

silly2 : List a -> String
silly2 = silly
github-actions[bot] commented 1 year ago

Thanks for reporting this! To set expectations:

Finally, please be patient with the core team. They are trying their best with limited resources.