Removing polymorphic compare from Core

Yes, this shouldn’t be too hard. A few things to watch for:

  • Abstract types can make it impossible to know from the typedtree whether a type is comparable. We have a syntax to declare through an abstraction boundary that a type is “immediate” (represented by an integer), but not the corresponding “comparable” or “eqtype” restriction. Either you extend the signature language, or you have to reject certain programs.

  • (=) or compare can be passed as argument to a function expecting an instance (List.sort compare : string list -> string list), so you have to check the instantiation sites of polymorphic operators, rather than just application sites.

  • Polymorphic comparison operators can be rebound, in a module or across module boundaries. In theory you should just warn on any operator at the type forall 'a. 'a -> 'a -> {int,bool}, they are either silly or non-parametric. In practice I’m not sure that rebinding polymorphic comparisons (rather than specialized instances) is a common practice.

Looking at how [@@immediate] is computed/checked/used in the compiler codebase could be a good entry point to get an idea of how to do this.

2 Likes