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 PR integrates mode levels into type checking, and implements mode polymorphism, and introduces a new flag that turns on various degrees of the feature:
-extension mode_polymorphism_alpha
enables mode polymorphism, while
-extension mode_polymorphism_beta
enables levels, but applies
generalize_structure
rather thangeneralize
, and therefore does not infer polymorphic modes.The PR involves:
To implement copying, the existing copy scope in
btype.ml
is extended to include the mode copy scope.Finally, new mode variables are allocated either at level 0 --- whenever a mode variable should not be generalized, such as allocation modes --- or at the current level.
NB: the curry mode of a function with multiple arguments (i.e.
m0
in(a @ m1 -> b -> c) @ m0
) is set to level 0.Some notable invariants:
typedtree.ml
should always be at level 0. To guarantee this invariant,typecore.ml
allocates new mode variables above the inferred generic ones usingnewvar_above_if_nonzero
.zap_to_X
do nothing on generic mode variables. There are two exceptions: (1) if the mode_polymorphism_alpha extension is not turned on, generic modes are zapped duringremove_mode_and_jkind_variables
, and (2) polymorphic mode variables are printed as their zapped versions. To zap generic mode variables, usezap_to_X_force
.Since generic mode variables are no longer zapped, it is important to instead determine the level 0 modes they might point to. If a mode occurs only in covariant positions to other modes in the typed expression, it is zapped to floor, if it occurs only in contravariant positions to other modes in the typed expression, it is zapped to ceiling, and if it occurs in both, it is zapped to legacy.