My own typesystem

Hi,

Generically I need to write my own typesysytem (for example I have interface concept "ISelfDefinedType" and a couple of concept classes inherited from this interface: StringType and IntegerType). Also I have value presentation of this types: (Interface "IValue" and inherited classes: StringValue and IntegerValue). And I have class "Assignment" which has children "ISelfDefinedType" and "IValue". I need for each selected type it will be possible to choose corresponding value-type (For example for IntegerType possible only IntegerValue). I think I must use "typesystem" for this purpose, but I can't found right object model. :-( I tried to use concept links (I've created type "SelfDefinedType" and define "IValue" aggregate concept link) but in inherited classes (StringType, IntegerType) i can't choose any type for this concept link. :-(

TIA

2 comments
Comment actions Permalink

Hi,

the topic of typesystems raises every so often, maybe its time for the pattern folks to dig out some DSL pattern.

Well ok, in this case it would be a platform specific pattern collection for MPS. As JetBrains has already built several products with MPS and even brought one to the commercial selling stage (YouTrack) they surely could tell some stories - but do they have time and delight to do so?

Best regards,

jens

0
Comment actions Permalink

Hi Oleg,

you can easily achieve what you want (or at least the most important part) via typesystem. I created a small project adressing your model described (see attached). In the solution you can see that you are only allowed to assign values that are equal to each other in type. If you assign an int to a string you get a red underlined error-indication (if you do not see the error-indication you probably have to regenerate the language)

The type-rules are as follows:

1.) ISelfDefinedType (the base to your IntegerType and StringType) defines its type to be the node itself (typeof(type) :==: type;). That is one of several ways you can define the "initial type" - the root of your type-equations

2.) AValue (in my model you don't need IntegerValue or StringValue because you define type by children itsType) defines its type to be the same as the one defined for child itsType (typeof(value) :==: typeof(value.itsType);)

3.) the Assignment defines the type of left part (reference to AValue) to be equal to type of right part (reference to AValue) by defining the equation as follows: typeof(assignment.left) :==: typeof(assignment.right);

That's it. Now you have continuous definition for your type-dependencies. The rest is done by MPS.

The reason for the red underline in the solution is that for the sample "sA = iA" in interaction with "string sA" and "int iA" the equation 3) has no way to become true:

3) typeof(assignment.left) :==: typeof(assignment.right);

2) typeof(typeof(value.itsType)) :==: typeof(typeof(value.itsType));

1) typeof(typeof(type)) :==: typeof(typeof(type));

whereas the left type is resolved to your "initial type" StringType (string) and the right type is resolved to your "initial type" IntegerType (int)

Now is still open, that MPS allows you to select iA or iB as the right side of "sA = ?". That can be fixed by defining the search scope as needed (i.e. to return only the AValue-instances with the same type as the type of the referenced left AValue-instance). This is explained in other forum-entries (f.e. http://www.jetbrains.net/devnet/message/5143718#5143718).

Aside of ":==:" you have also the possibility to define stromg subtyping (":<<=:"), weak subtyping (":<=:") and the like -> see typesystem-documentation.

Mirko



Attachment(s):
sample_own_typesystem.tar.gz
0

Please sign in to leave a comment.