compiler
compiler copied to clipboard
Type error for correct code using a polymorphic recursive function
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
- Elm: 0.19.1
- Browser: N/A
- Operating System: MacOS 11.6.7
Thanks for reporting this! To set expectations:
- Issues are reviewed in batches, so it can take some time to get a response.
- Ask questions in a community forum. You will get an answer quicker that way!
- If you experience something similar, open a new issue. We like duplicates.
Finally, please be patient with the core team. They are trying their best with limited resources.