This might be a general ML question, but perhaps you can help... I'm curious why type functions cannot be shared, but datatypes can: the following code is fine:
---- signature A = sig type T end;
signature B = sig type T datatype Pair = Pair of T * T end;
signature CA = sig structure a : A end; signature CB = sig structure b : B end;
signature D = sig structure b : B structure cb : CB sharing cb.b = b structure ca : CA sharing ca.a = b end; -----
However, the following complains:
----- signature A = sig type T end;
signature B = sig type T type Pair = T * T end;
signature CA = sig structure a : A end; signature CB = sig structure b : B end;
signature D = sig structure b : B structure cb : CB sharing cb.b = b structure ca : CA sharing ca.a = b end; -----
with the error message:
Error: Cannot share: (D.cb.b.Pair) is a type function Found near sig structure b : B structure cb : CB sharing cb.b = b structure ca : CA sharing ca.a = b end
Static errors (pass2)
Is there a theoretical reason for this? (I cannot think of one)
I have been thinking of things like: type tname = .... as type abbreviations to save me typing, and I'm curious why wrapping the type into a datatype should allow sharing... ?
cheers, lucas
Lucas Dixon wrote:
This might be a general ML question, but perhaps you can help... I'm curious why type functions cannot be shared, but datatypes can
Is there a theoretical reason for this? (I cannot think of one)
I have been thinking of things like: type tname = .... as type abbreviations to save me typing, and I'm curious why wrapping the type into a datatype should allow sharing... ?
In ML 97 structure sharing is just a derived form of type sharing. You are allowed to share types which are flexible i.e. not bound to external types and are not type functions. If sharing of general type functions were allowed then potentially the compiler would have to produce the equality between two arbitrary type functions.
For example, signature SA = sig type S type T = S * S end signature SB = sig type P type 'a Q type S type T = P Q end signature S = sig structure A:SA and B: SB sharing A = B end; If this were legal the compiler would have to work out the possible interactions between P, 'a Q and S.
When you use datatypes only the types are shared. The right hand side of the datatype is ignored and there is no check that the sharing will result in a signature that can be matched. For example, signature SA = sig datatype T = X of int | Y of bool end signature SB = sig datatype T = A of string end signature S = sig structure A:SA and B: SB sharing A = B end; is legal but unmatchable.
David