You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Sometimes it is natural to consider two different interfaces for a data structure, one where the container type is parametrized, and one where it is monomorphic. I propose to let us instantiate the polymorphic signature into the monomorphic signature by using a type constraint to transform occurrences of the type parameter.
For example, if we have
module type Poly=sigtype'a tvalreturn : 'a -> 'atvalmap : ('a -> 'b) -> 'at -> 'btend
then I propose to write
module type Mono=sigtypeelttypetincludePolywithtype'a t := tconstraint'a=eltend
and to have the type-checker understand this as the following signature:
module type Mono=sigtypeelttypetvalreturn : elt -> tvalmap : (elt -> elt) -> t -> tend
To apply the equation type 'a t = u constraint 'a = foo, for each occurrence of bar t in the signature, we unify foo with bar and apply the resulting substitution. We fail with an error if foo and bar are not unifiable, for example if we used int t in another item of the Poly signature above.
The text was updated successfully, but these errors were encountered:
Note: it is currently possible to derive both interfaces by introducing a signature that encapsulates the use of the type parameters.
module type Generic=sigtype'a ttype'a paramvalreturn : 'aparam -> 'atvalmap : ('aparam -> 'bparam) -> 'at -> 'btendmodule type Poly=Genericwithtype 'a param := 'a
module type Mono=sigtypettypeeltincludeGenericwithtype'a t = tandtype 'a param = eltend
That is an interesting proposal.
However, it requires an important amount of work to be implemented: basically, you need to both substitute the occurrences of t and unify its arguments. There is no code doing that in the type checker yet.
Since doing that also means that you have to re-check the coherence of all the types, I suppose that there is no soundness problem; it would have to be detected anyway.
If we want to do things like that, it probably means that we want a way to reason about types at a higher level, rather than just substitution and unification, that would be more amenable to this kinds of extra checks.
Sometimes it is natural to consider two different interfaces for a data structure, one where the container type is parametrized, and one where it is monomorphic. I propose to let us instantiate the polymorphic signature into the monomorphic signature by using a type constraint to transform occurrences of the type parameter.
For example, if we have
then I propose to write
and to have the type-checker understand this as the following signature:
To apply the equation
type 'a t = u constraint 'a = foo
, for each occurrence ofbar t
in the signature, we unifyfoo
withbar
and apply the resulting substitution. We fail with an error iffoo
andbar
are not unifiable, for example if we usedint t
in another item of thePoly
signature above.The text was updated successfully, but these errors were encountered: