compiler icon indicating copy to clipboard operation
compiler copied to clipboard

Compiler does not match 2 identical types.

Open rupertlssmith opened this issue 7 years ago • 6 comments

Elm 0.19 (Works fine on Elm 0.18) Linux Debian 9

Error Message:

This `iterativeSearch` call produces:

    List state -> SearchResult state

But the type annotation on `iterativeCostIncreasing` says it should be:

    List state -> SearchResult state

SSCCE:

module TypingBug exposing (...)

type SearchResult state
    = Complete
    | Goal 

iterativeSearch : SearchResult state
iterativeSearch =
    let
        -- iteration : Int -> SearchResult state
        iteration count =
            evaluate count Goal

        -- evaluate : Int -> SearchResult state -> SearchResult state
        evaluate count result =                        
            case result of
                Complete ->
                    iteration (count + 1)
                 
                Goal ->
                    Goal 
    in
    iteration 0


iterativeCostIncreasing : SearchResult state
iterativeCostIncreasing =
    iterativeSearch 

Removing one of SearchResult constructors makes the problem go away. Adding in one of type annotations within the let block makes the problem go away. Moving the function definitions from the let block to top level does not make the problem go away.

Seems like where type inference is needed to deduce the SearchResult type yields a type that does not match an identical one from a type annotation, but a certain level of complexity is needed in the code to force this.

SSCCE is not terribly meaningful code, it was derived by cutting down this: https://github.com/the-sett/ai-search/blob/typing-error/src/Search.elm

rupertlssmith avatar Aug 28 '18 08:08 rupertlssmith

Anyone running into strange typing issues like this in 0.19, there seems to be a workaround: Add more type annotations to your code until you flush out the problem.

rupertlssmith avatar Aug 28 '18 08:08 rupertlssmith

And thanks for all your hard work on this fantastic compiler and eco-system. :+1:

rupertlssmith avatar Aug 28 '18 08:08 rupertlssmith

I ran into something similar trying to port an 0.18 package. The error ended up disappearing after I wiped elm-stuff, it seems like something got corrupted in there. Does this help here?

robx avatar Aug 28 '18 09:08 robx

No, in this case nothing to do with corrupted elm-stuff.

rupertlssmith avatar Aug 28 '18 11:08 rupertlssmith

Still an issue in 0.19.1-beta-1.

Workaround of adding a type annotation still works in 0.19.1-beta-1.

rupertlssmith avatar Aug 07 '19 14:08 rupertlssmith

I'm pretty sure it is the same bug. I started a much larger example, and then successively reduced the code while still getting a type error, until I got it down to:

module Alpha exposing (..)

type S i = S

alpha : S i
alpha =
  let
    step items_ =
      case items_ of
        [] -> S
        _ :: rest -> next rest

    -- next : List Int -> S i
    next rest =
        step rest
  in
    step [ 1 ]


beta : S i
beta =
    alpha

Here's the error:

Detected problems in 1 module.
-- TYPE MISMATCH ----------------------------------------------------- Alpha.elm

Something is off with the body of the `beta` definition:

22|     alpha
        ^^^^^
This `alpha` value is a:

    S i

But the type annotation on `beta` says it should be:

    S i

Adding in the commented out type annotation on next clears it up.

I can't reduce this further. For example, inlining next into step causes the issue to go away. So something to do with mutually recursive local functions.

mzero avatar Jan 20 '25 21:01 mzero