-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support parameter constraints in module signature type equalities #13157
Comments
Note: it is currently possible to derive both interfaces by introducing a signature that encapsulates the use of the type parameters. module type Generic = sig
type 'a t
type 'a param
val return : 'a param -> 'a t
val map : ('a param -> 'b param) -> 'a t -> 'b t
end
module type Poly = Generic with type 'a param := 'a
module type Mono = sig
type t
type elt
include Generic with type 'a t = t and type 'a param = elt
end |
That is an interesting proposal. 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. |
I think the original goal can be achieved today: module type Poly = sig
type 'a elt
type 'a container constraint 'a = 'b elt
val return : 'a -> 'a container
val map : ('a -> 'b) -> 'a container -> 'b container
end
module type Mono = sig
type elt
type t
include Poly with type 'a elt := elt and type 'a container = t
end This gives the desired signature, but with |
Wow, nice! For the record, here is an alternative way to do this which is less nice -- which I wanted to avoid. module type Gen = sig
type 'a elt
type 'a t
val return : 'a elt -> 'a t
val map : ('a elt -> 'b elt) -> 'a t -> 'b t
end
module type Poly = Gen with type 'a elt := 'a
module type Mono = sig
type elt
type t
include Gen with type 'a elt := elt and type 'a t := t
end Between this and the approach proposed by @goldfirere above, I agree that people who really wants this have workarounds, and that the proposed new feature is not really worth the effort. Closing. |
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: