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

Add explicit interface methods #660

Merged
merged 1 commit into from
Oct 5, 2021

Conversation

apaszke
Copy link
Collaborator

@apaszke apaszke commented Oct 1, 2021

It is often useful to define type -> value mappings, and the standard
way to do that is through interfaces (type classes). However, because
all methods so far had implicit type parameters, any attempt to
associate e.g. an integer with a type was difficult: without explicit
type annotations it often ended up being ambiguous.

This patch allows specifying interface parameters between each method
name and the colon that begins its type annotations, with the mentioned
parameters becoming explicit type parameters of the generated methods.
This lets us remove the awkward TypeVehicle abstraction from prelude
and in the future should make it possible to define associated types.
For example, a Manifold a interface could have a method declared as
TangentSpace a : Type, which would later make it possible to mention
TangentSpace MyDataType in types.

@oxinabox
Copy link
Contributor

oxinabox commented Oct 1, 2021

would this unblock #343 ?

@apaszke
Copy link
Collaborator Author

apaszke commented Oct 1, 2021

Not yet, but almost! This PR + #659 + ~50 LOC more are what we need to unblock it!

@danieldjohnson
Copy link
Collaborator

I vaguely remember having a conversation where we considered spelling this as

interface Manifold a
  TangentSpace a : Type

as opposed to

interface Manifold a
  explicit TangentSpace : Type

Do you think that's worth doing?

@apaszke
Copy link
Collaborator Author

apaszke commented Oct 4, 2021

Oh, that's a nice one! It also has the benefit that you can (1) list a subset of type-class binders (2) permute them. My only concern would be that

someMethod a : a -> Int

might be a little hard to parse, because the a on the lhs implies an argument of type Type and the one on rhs of : implies an argument of type a. But I guess if someone has a method that does take an instance then they won't declare it with an explicit type parameter?

@apaszke
Copy link
Collaborator Author

apaszke commented Oct 5, 2021

I switched the implementation to use what @danieldjohnson has suggested. It has a temporary restriction that you either specify all or no type parameters (and without any permutations), but we can relax it in the future.

It is often useful to define type -> value mappings, and the standard
way to do that is through interfaces (type classes). However, because
all methods so far had implicit type parameters, any attempt to
associate e.g. an integer with a type was difficult: without explicit
type annotations it often ended up being ambiguous.

This patch allows specifying interface parameters between each method
name and the colon that begins its type annotations, with the mentioned
parameters becoming explicit type parameters of the generated methods.
This lets us remove the awkward `TypeVehicle` abstraction from prelude
and in the future should make it possible to define associated types.
For example, a `Manifold a` interface could have a method declared as
`TangentSpace a : Type`, which would later make it possible to mention
`TangentSpace MyDataType` in types.
@apaszke apaszke merged commit aee5616 into google-research:main Oct 5, 2021
@apaszke apaszke deleted the explicit-methods branch October 5, 2021 16:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants