Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug in type inference #2170

Closed
akoppela opened this issue Feb 3, 2021 · 9 comments
Closed

Bug in type inference #2170

akoppela opened this issue Feb 3, 2021 · 9 comments

Comments

@akoppela
Copy link

akoppela commented Feb 3, 2021

Quick Summary:
Seems like there is a bug in type inference. r1 and r2 should have identical types.

SSCCE

module TypeInferenceBug exposing (..)

import Url.Parser as P exposing ((</>), Parser)

sum p1 p2 =
    P.map ( ) (p1 </> p2)

r1 : Parser (Int -> a) a
r1 =
    P.int
        |> sum P.int
        |> sum P.int

r2 : Parser (Int -> b) b
r2 =
    [ P.int, P.int ]
        |> List.foldl sum P.int
  • Elm: 0.19.1
  • Browser: Any
  • Operating System: Any

Additional Details

The compiler throws an error for r2.

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

30|>    [ P.int, P.int ]
31|>        |> List.foldl sum P.int

The body is:

    Parser (Int -> Int) Int

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

    Parser (Int -> b) b

I found a fix for this as well. Mapping with identity function resolves the issue.

r2 : Parser (Int -> b) b
r2 =
    [ P.int, P.int ]
        |> List.foldl sum P.int
        |> P.map identity
@github-actions
Copy link

github-actions bot commented Feb 3, 2021

Thanks for reporting this! To set expectations:

  • Issues are reviewed in batches, so it can take some time to get a response.
  • Ask questions 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.

@avh4
Copy link
Member

avh4 commented Feb 3, 2021 via email

@akoppela
Copy link
Author

akoppela commented Feb 3, 2021

Sure, from the List module docs

So `foldl step state [1,2,3]` is like saying:

state
  |> step 1
  |> step 2
  |> step 3

which means that

    [ P.int, P.int ]
        |> List.foldl sum P.int

equals to

    P.int
        |> sum P.int
        |> sum P.int

Where do you see the difference?

@avh4
Copy link
Member

avh4 commented Feb 3, 2021 via email

@evancz
Copy link
Member

evancz commented Feb 3, 2021

If I understand correctly, this type error message is correctly identifying that the type of step in List.foldl step base list must be a single (a -> b -> b) that works for all list elements. So it's not a bug, but perhaps a candidate for an issue in the error-message-catalog repo.

@evancz evancz closed this as completed Feb 3, 2021
@akoppela
Copy link
Author

akoppela commented Feb 4, 2021

I do understand that under the hood P.Parser (Int -> a) a and P.Parser (Int -> Int) Int in the end are equal in eyes of Parser. It's probably a specific of the impementation for the Url.Parser module. E.g. this is not true for compiler

t1 : (Int -> Int) -> Int -> a
t1 =
    identity

It does want a to be Int. But that is true for Url.Parser map function.

This is valid.

p : Parser (Int -> Int) Int
p =
    P.int

t : Parser (Int -> a) a
t =
    P.map identity p

@akoppela
Copy link
Author

akoppela commented Feb 4, 2021

Or a more close example

This is not valid.

l : List (Int -> Int)
l =
    []

t : List (Int -> a)
t =
    List.map identity l

@evancz
Copy link
Member

evancz commented Feb 4, 2021

I think the original case should error. If you think there are cases here that should not error, please open an issue about those specific SSCCEs. I think all the cases mentioned here are working alright though. For example:

p : Parser (Int -> Int) Int

-- P.map : a -> Parser a b -> Parser (b -> c) c
-- P.map identity : Parser (t1 -> t1) b -> Parser (b -> c) c
-- P.map identity p : Parser (Int -> c) c

It seems like the root issue is just that it's freaky how the types work with the URL parsers.

@akoppela
Copy link
Author

akoppela commented Feb 4, 2021

Yes, indeed it’s about Url.Parser specifics. So no issue here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants