How to avoid duplication of data like SML in structures and signatures?

Given a signature A with data type t , say

 signature A = sig datatype t = T of int | S of string end 

Is it possible to provide an implementation (structure) that does not have t repeated? For example, in the next signature, the definition of t repeated. This is great for small data types, but a bit awkward for large ones.

 structure AImpl : A = struct datatype t = T of int | S of string end 

My intention is simply to give an interface so that all announcements can be recognized. But I do not want each implementation to repeat the definition of a data type.

Although it seems that the signature and structure may include a data type from another structure, then it would be impossible to recognize the data type declaration only by checking the signature. For instance:

 structure AData = struct datatype t = T of int | S of string end signature A = sig datatype t = datatype AData.t end structure a : A = struct open AData end 

Of course, this approach, although not the one that satisfies, is acceptable if I put both AData and A in the same .sig file.

+5
source share
1 answer

No, this is not possible due to the way signature matching rules work in sml.

When you enter a non-abstract type in a signature, each structure with this signature should provide the same type of binding. The reason is that sml signatures are mainly used to hide structure details.

For example, you can use an abstract type to hide data type data from structure users:

 signature A = sig type t end structure AImpl :> A = struct datatype t = T of int | S of string end 

Or you can open only one type constructor:

 signature A = sig type t val T : int -> t end structure AImpl :> A = struct datatype t = T of int | S of string end val test = AImpl.T 12; 
+1
source

Source: https://habr.com/ru/post/1206409/


All Articles