Polymorphism, subtyping and type inference in MLsub

I am very enthusiastic about the following paper: it brings new ideas and solves a problem that I did not expect to be solvable, namely usable type inference when both polymorphism and subtyping are implicit. (By "usable" here I mean that the inferred types are both compact and principal, while previous work generally had only one of those properties.)

Polymorphism, Subtyping, and Type Inference in MLsub
Stephen Dolan and Alan Mycroft

We present a type system combining subtyping and ML-style parametric polymorphism. Unlike previous work, our system supports type inference and has compact principal types. We demonstrate this system in the minimal language MLsub, which types a strict superset of core ML programs.

This is made possible by keeping a strict separation between the types used to describe inputs and those used to describe outputs, and extending the classical unification algorithm to handle subtyping constraints between these input and output types. Principal types are kept compact by type simplification, which exploits deep connections between subtyping and the algebra of regular languages. An implementation is available online.

The paper is full of interesting ideas. For example, one idea is that adding type variables to the base grammar of types -- instead of defining them by their substitution -- forces us to look at our type systems in ways that are more open to extension with new features. I would recommend looking at this paper even if you are interested in ML and type inference, but not subtyping, or in polymorphism and subtyping, but not type inference, or in subtyping and type inference, but not functional languages.

This paper is also a teaser for the first's author PhD thesis, Algebraic Subtyping. There is also an implementation available.

(If you are looking for interesting work on inference of polymorphism and subtyping in object-oriented languages, I would recommend Getting F-Bounded Polymorphism into Shape by Ben Greenman, Fabian Muehlboeck and Ross Tate, 2014.)

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Cool paper

This is indeed awesome work, and I'm glad to finally see it published as it deserves.

In case you'll miss POPL'17, Stephen Dolan already gave a talk about it at last year's (2015) ML workshop, which can be watched here.

Looks neat indeed.

Looks neat indeed. Interesting that a real solution to subtyping was found by sort of redefining the language describing the problem, ie. type variables no longer quantify over ground types solves many of the earlier difficulties in integrating subtyping.

The type syntax for MLSub actually looks simple enough to use by mere mortals too. Also neat that MLSub can type the self-application function, which isn't possible in ML.


Also neat that MLSub can type the self-application function, which isn't possible in ML.

That's a very persistent myth. It is easily possible in ML if you choose to make it so via equi-recursive function types. Problem is, in practice, you don't want this! Because many plain bugs become well-typed and lead to obscure errors downstream. See OCaml's -rectypes option, which was the default initially but was turned into an option because users complained. MLsub probably has this very problem.

Restrict subtyping to fine types

First, let me agree that this paper looks awesome - great bang/buck. I think your particular complaint here points to a need for better UX around subtyping. "Matrix or String" isn't a very useful inferred type, either. I think we need coarse types that unify by equality to rule out nonsense like this example or self-application with fine types that support subtyping.

Recursive types and type errors

Actually, MLsub types the self-application function nonrecursively, as ((a -> b) & a) -> b. You can call that passing e.g. a constant function returning integers (type Top -> int) and get back an integer, all without recursive types. Self-applying self-application does use recursive types, though, as you'd expect.

You're right about this making weird code become well-typed. MLsub is pretty willing to give a strange type to bad code, especially if you use currying pervasively (currying always interprets missing arguments as partial application, which can generate weird function types. Implementing multiple argument functions with tuples turns missing arguments into type errors).

On the other hand, I've found that the downstream type errors are less obscure than in unification-based systems. Because subtyping keeps track of the direction of data flow, type errors can always be explained by showing an introduction form in the program, a series of data-flow steps (e.g. from a variable's binding to its use), and then an elimination form into which the value doesn't fit.

So, compared to ML, more buggy definitions are initially accepted, but when you try to use them the type error makes more sense. I've not written large enough programs to know how the tradeoff pans out, though.

While you're here...

Any advice for someone who wants to read through your thesis?

Thesis draft and implementation

Stephen's thesis draft and implementation are available online.


I updated the top post to include the links.