roc-lang / roc

A fast, friendly, functional language.
https://roc-lang.org
Universal Permissive License v1.0
4.46k stars 314 forks source link

Adding type annotations can prevent valid code compiling #6879

Open quelgar opened 4 months ago

quelgar commented 4 months ago

roc nightly pre-release, built from commit 3abc2769375 on Fri Jul 5 09:39:37 UTC 2024

This compiles fine:

cons = \h, t -> Cons h t
empty = Empty
consList = \l -> l |> List.walkBackwards empty \state, elem -> cons elem state

but if type annotations are added:

ConsList a : [Cons a (ConsList a), Empty]

cons : a, ConsList a -> ConsList a
cons = \h, t -> Cons h t

empty : ConsList a
empty = Empty

consList : List a -> ConsList a
consList = \l -> l |> List.walkBackwards empty \state, elem -> cons elem state

results in odd error messages:

── TYPE MISMATCH in test.roc ───────────────────────────────────────────────────

This 3rd argument to |> has an unexpected type:

12│  consList = \l -> l |> List.walkBackwards empty \state, elem -> cons elem state
                                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The argument is an anonymous function of type:

    ConsList a, a -> ConsList a

But |> needs its 3rd argument to be:

    ConsList a, a -> ConsList a

── TYPE MISMATCH in test.roc ───────────────────────────────────────────────────

Something is off with the body of the consList definition:

11│  consList : List a -> ConsList a
12│  consList = \l -> l |> List.walkBackwards empty \state, elem -> cons elem state
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

This walkBackwards call produces:

    ConsList a

But the type annotation on consList says it should be:

    ConsList a

────────────────────────────────────────────────────────────────────────────────