elm-in-elm / compiler

Elm compiler written in Elm

Home Page:https://elm-in-elm.github.io/compiler/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Wrong type inference for function type

xarvh opened this issue · comments

Given

module Meh exposing (..)

meh = \a b -> 3 + a

someFunction = meh 5

The correct inferred type for someFunction should be a -> Int but instead is a.

ie, the parsed AST declarations are:

    [ { body =
            Value
                ( Lambda
                    { argument = "a"
                    , body = ( Lambda { argument = "b", body = ( Plus ( Int 3, Int ) ( Argument "a", Int ), Int ) }, Function (Var 6) Int )
                    }
                , Function Int (Function (Var 6) Int)
                )
      , module_ = "Meh"
      , name = "meh"
      }
    , { body =
            Value
                ( Call
                    { argument =
                        ( Int 5
                        , Int
                        )
                    , fn = ( Var { module_ = "Meh", name = "meh" }, Function Int (Var 2) )
                    }
                , Var 2
                )
      , module_ = "Meh"
      , name = "someFunction"
      }
    ]

For folks not on our Discord (I mean, what the heck? 😉 ): this is likely because of IdSource and SubstitutionMap not being threaded through all calls, and will be (likely) fixed by the type-annotations branch PR (#65)

Looks like this is indeed fixed.

module Main exposing (..)

meh = \a b -> 3 + a

someFunction = meh 5

main = someFunction 9


Yields

Main.someFunction: Value { expression = Call { argument = Int 5, fn = Var { name = "meh", qualifiedness = PossiblyQualified Nothing } }, typeAnnotation = Nothing }
Main.meh: Value { expression = Lambda { arguments = ["a","b"], body = Plus (Int 3) (Argument "a") }, typeAnnotation = Nothing }
Main.main: Value { expression = Call { argument = Int 9, fn = Var { name = "someFunction", qualifiedness = PossiblyQualified Nothing } }, typeAnnotation = Nothing }

Thanks @gmilleramilar for checking! I think this was only fixed yesterday night when I added typechecking for variables (typechecking across declarations). Adding an integration test below ⬇️