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

Ambiguous method error in class definition when method doesn't include both parameterized types #1273

Open
Izaakwltn opened this issue Oct 1, 2024 · 2 comments

Comments

@Izaakwltn
Copy link
Collaborator

When defining a class with multiple parameterized types, each method must employ both types.:

(coalton-toplevel

  (define-class (C :a :b)
    (m1 :a)
    (m2 (:a -> :b))))

Compiling returns:

  error: 
    (during macroexpansion of (COALTON-TOPLEVEL
      (DEFINE-CLASS #
        ...)))
    error: Ambiguous method
      --> <macroexpansion>:3:4
       |
     3 |      (M1 :A)
       |      ^^^^^^^ the method is ambiguous

This also errors:

(define-class (C :a :b)
    (m1 (Unit -> :a))
    (m2 (:a -> :b)))

This may be just a Hindley-Milner rule that I'm not yet privy to.

@YarinHeffes
Copy link
Collaborator

So, I don't know the answer, but I spent some time thinking about this and I think this is not a bug, and here's why. If I define-instance both (C Integer UFix) and (C Integer U32), then the compiler will not know which definition of (the Integer m1) to codegen. Maybe someone else (@stylewarning?) can confirm.

I don't think this is obvious, so we should definitely:

  • document this, either in how-typeclasses-are-compiled.md or intro-to-coalton.md, and/or
  • perhaps include a hint in the error message with a clear explanation of "ambiguous."

Maybe we only need to do the second item, but it wouldn't hurt to do both.

@stylewarning
Copy link
Member

@YarinHeffes has the correct explanation.

To get the above class to work, you can also have a "phantom type":

(define-class (C :a :b) 
  (m1 (Proxy :a -> :b)) 
  (m2 (:a -> :b))) 

Proxy :a has no distinct values (it is inhabited by a single runtime value called Proxy), and is thus just used to pass type information around, for example:

(m1 (the String Proxy))

Alternatively, one can restructure their type classes:

(define-class (P :a) 
  (m1 (:a)))

(define-class (P :a => C :a :b) 
  (m2 (:a -> :b))) 

This of course only works if m1 is truly invariant to :b.

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