Revising coercion mechanism to allow for implicit arguments #3032
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This allows to define coercions like this the one below:
or
which are parametrized by an implicit an universes.
The previous mechanism only allowed for coercions of type
a -> b
, where botha
andb
had to be top-level names without any arguments (e.g.term
andterm_view
). This relaxes the shape to allow for coercions of shapex1:t1 -> ... -> xn:tn -> a a_args -> b b_args
, with any amount of argument before the last one (which should be implicit so they can be solved, but that's not required by this code) and allowinga
andb
to be applied to any number of expressions.I still kept the requirement that
a
andb
be top-level names to easily distinguish which coercions may apply, and be as efficient as before for the existing cases. It's these two types which define the coercion to apply, there's no backtracking.Now, when we have a term
e
of typea ...
and expected type ofb ...
, we will find the coercionf
(if any) and attempt to typecheckedf e
at typeb ...
. This will instantiatef
's implicits and, if it suceeds, we take this elaborated term to be the coerced one.A different design is possible via typeclasses, but 1) there's a bootstrapping problem to still solve (see #2969 for some related context), 2) they don't work well wrt refinements, while here we can be smarter about them.
cc @bollu who requested this, maybe give this branch a try?