With the unifier in place, we now add a single construct to the language: cast M. The rule for cast M is a variation on the mode shift that calls the implicit coercion resolver instead of the unifier. The user doesn’t have to write the types involved in the cast, but the user does have to indicate that they intend a cast to be employed. The impact on users is that the “dream code” results in a type error:
bad : _ -> Monoid * Semigroup
bad X := (X, X)
// Error: expected Monoid == Semigroup
But if you add an explicit cast, the code works:
good : _ -> Monoid * Semigroup
good X := (X, cast X)
With my proposal, the user is in control of how unification and coercion interact. The user is in some sense intentionally orchestrating unification problems whose solution will unlock the coercions they want to fire. Because the unifier and the coercion preorder are individually well-behaved, the composite system is well-behaved with respect to user intentions.
There is a final very interesting wrinkle: the coercion mechanism can actually call the unifier. This is because the appropriate canonical coercion between theory refinements is a downcast, i.e. a cast that involves checking an equation:
downcast : (X : Monoid) -> {Semigroup | car => _}
downcast X := cast XIn the above, the placeholder _ gets elaborated to a metavariable ?0:Type, and the implicit coercion mechanism chooses the forgetful coercion from monoids to semigroups and emits the unification constraint ?0 ~ X.car.