owned this note
owned this note
Published
Linked with GitHub
The purpose of this document is to serve as a focused basis of discussion for the design of an interfaces system for Julia. It is intended to capture the outcome of several discussions I've had with different groups of people over the past few months. The design described herein is intended to be the minimal basis on which to build further. It does not capture everything that everyone wants to do, but hopefully it captures a common sense of what most people agree on is a reasonable place to start.
# Guiding Principles
The first question to answer here is what the purpose of an interface system is. There are differing opinions on this and different goals, but I think the most pressing issue is what I like to call the "compositional whodunnit". What I mean by that is that when composing different packages providing abstract functionality, it is easy to run into situations where something breaks due to leaky abstractions. In such situations, it is extremely hard to differentiate between:
1. A bug in the package providing the interface
2. A bug in the package using the interface
3. A fundamental disagreement on what the interface is supposed to mean
This becomes expontentially more difficult the more packages and interfaces are involved, and as such issues of this nature often require triage by developers familiar with large fractions of the ecosystem.
In some ways, the fundamental problem here is a lack of documentation.
We do not have a practice of writing down what our interfaces are, so
even those who wish to write code against an interface are generally unable to do so.
In my personal opinion, this is the major part of problem and an opinionated and structured format for documenting interface expectations and requirements is already 90% of a solution, though we can and should of course try to do better still.
That said (and I think this is the main point of my argument), because of this, to the extent that there is a conflict between the ability to clearly specify an interface and the ability to check that a particular implementation satisfies an interface, we should err on the side of expressability, rather than verification. This is in some sense the exact opposite trade off that static languages take, where the expressivity is exactly the expressivity of the interface.
With these thoughts in mind, I would like to propose the following set of principles:
1. (Formal) verification is great, but should not get in the way of expressivity.
2. An initial solution should be a Test-extension, not a semantic change to the language
3. Provide tooling to assist the user in detecting interface violations *in a particular instance*
# An implementation roadmap
Based on various discussions, I think we should being with interfaces that are simple list of methods (and in an immediate second step their return types). The initial semantics of an interface would be that an interface is deemed satisfied if, for the given type, it is not possible for the signature to throw a MethodError. As a notional syntax, we might consider:
```
@interface Indexable T->begin
getindex(::T, ::Int)
end
@interface HasLength T->begin
length(::T)::Int
end
```
The reason for choosing the absence of a MethodErorr, rather than a weaker notion like method existence is that it bootstraps more nicely to correctness checking users of an interface. For example, we might have a function like:
```
function get_all(x)
for i = 1:length(x)
x[i]
end
end
```
We could have a function like (again, syntax notional) `check(get_all, Length | Indexable)` that would go through and check, whether (under the assumption that the methods of the interfaces themselves do not throw method errors), the function get_all can throw a method error. In particular, this would then allow for interface composition.
An additional important consideration is that we have the ability (as of last year - added precisely for this purpose) to ask the compiler to check whether a given signature has the possiblity to throw a MethodError, as well as the ability to provide nice user-facing printing of such analysis (using JET).
I thus propose the following roadmap:
1. Ability to specify/check a list of methods (no return types)
- Implemented by asing whether `MethodError` is in the compilers inferred exctype
2. Ability to check an implementation against an interface
- Implemented by giving JET an overlay method table and asking the same question
3. Add return types to the above
- Should be a trivial addition to 2, but a separate feature
4. Design a system to associate interfaces with abstract types
- Some thoughts below, but to be designed
5. Write interfaces for Base and see how things work out
6. Integrate interfaces into Base's existing `MethodError` error hinting.
Importantly, none of these require additions to base (with the possible exception of step 2, which is supported in principle, but nobody has every really tried).
That said, I know that this is not powerful enough to implement many of the different kinds of interfaces that people want, for example, dependent return types `getindex(::T, ::Int)::eltype(T)`, value constraints `length(size(T)) == ndims(T)`, etc. Those additions are comptabile with this notion of interfaces, but are deliberately excluded from the MVP because they are harder to do.
# Thoughts on associating interfaces with abstract types
I propose that we use method tables for type-interface association. The reason for this is that julia programmers are already used to thinking about method tables and how they merge between different packages. An example way to do this might be to say something like (syntax notional):
```
Interfaces.shoud_satisfy(Indexable)(::AbstractArray) = true
```
Then a macro like `@check_all_interfaces(MyPackage)` would go through and ask the compiler to check all interfaces declared as above in `MyPackage`.
Importantly, a definition like this would allow opt out for special snowflake types like:
```
# Does not satisfy the interface because it does not return `Int`, even though it is an AbstractArray.
Interfaces.shoud_satisfy(HasLength)(::SymbolicArray) = false
```
In addition, we could adjust the `MethodError` handler to give more informative error message when a concrete interface violation is detected (however, this is trickier than it may seem, because the MethodError being thrown may not necessarily be the one that violates the interface itself (rather the fact of a MethodErorr being thrown there could violate some interface higher in the stack) - so I would suggest punting on this for now).
# Is this the same as traits?
Traits and interfaces are somewhat orthogonal. To my mind, the biggest differences is that for traits people generally want to be able to use them to affect the runtime semantics of the program. This proposal does not do that. However, it is of course possible to use any of the existing trait systems and use this proposal to specify their semantics. The biggest issue for a dedicated traits system is that I don't think there is sufficient design space between what is current achievable with traits using macros (esp when enhanced with this proposal) and the place where you start wanting a compiler to do algorithmic selection for you (which needs a completely seprate mechanism). That said, we can revisit that question at a later time.