MPS 2023.3 Help

Typesystem Debugging

For debugging typesystem MPS provides Typesystem Trace - an integrated visual tool that gives you insight into the evaluation process that happens inside the typesystem engine.

Try it out for yourself

We prepared a dedicated sample language for you to easily experiment with the typesystem. Open the Expressions sample project that comes bundled with MPS and should be available among the sample projects in the user home folder.

tt11.png

The sample language

The language to experiment with is a simplified expression language with several types, four arithmetic operations (+, -, *, /), assignment (:=), two types of variable declarations and a variable reference. The editor is very basic with almost no customization, so editing the expressions will perhaps be quite rough. Nevertheless, we expect you to inspect the existing samples and debug their types more than writing new code, so the lack of smooth editing should not be an issue.

TT1.png

The language can be embedded into Java thanks to the SimpleMathWrapper concept, but no interaction between the language and BaseLanguage is possible.

The expression language supports six types, organized by subtyping rules into two branches:

  1. Element -> Number -> Float -> Long -> Int

  2. Element -> Bool

Inspecting the types

If you open the Simple example class, you can place the caret to any part of the expression or select a valid expression block. As soon as you press Ctrl+Shift+P, you'll see the type of the selected node in a pop-up dialog. As an alternative path, you can right-click any node and select Language Debug | Show Type in the context menu.

TT2.png
TT3.png

The Main sample class will give you a more involved example showing how Type-inference correctly propagates the suitable type to variables:

tt4.png

Just check the calculated types for yourself.

Type errors

The TypeError sample class shows a simple example of a type error. Just uncomment the code Ctrl+/ and check the reported error:

tt9.png

Since this variable declaration declares its type explicitly to be an Int, while the initializer is of type Float, the type-system reports an error. You may check the status bar at the bottom or hover over the incorrect piece of code.

Type-system Trace

When you press Ctrl+Shift+X or navigate through the pop-up menu, you get the Type-system Trace panel displayed on the right hand-side.

TT6.png

The Trace shows in Panel 2 all steps (i.e. type system rules) that the type-system engine executed. The steps are ordered top-to-bottom in the order in which they were performed. When you have Button 1 selected, the Panel 2 highlights the steps that directly or indirectly influence the type of the node selected in the editor (Panel 1). Panel 3 details the step selected in Panel 2 - it describes what changes have been made to the type-system engine's state in the step. The actual state of the engine's working memory is displayed in Panel 4.

Step-by-step debugging

The Simple sample class is probably the easiest one to start experimenting with. The types get resolved in six steps, following the typesystem rules specified in the language. F3 (Go to Source Node) will navigate you to the node, which is being affected by the current rule.

tt7.png
  1. The type of a variable declaration has to be a super-type of the type of the initializer. The aValue variable is assigned the a type-system variable, the initializer expression is assigned the b type-system variable and a>=b (b sub-type or equal type to a) is added into the working memory.

  2. Following the type-system rule for Arithmetic Expressions, b has to be a sub-type of Number, the value 10 is assigned the c variable, 1.3F is assigned the d variable and a when-concrete handler is added to wait for c to be calculated.

  3. Following the rules for float constants d is solved as Float.

  4. Following the rules for integer constants c is solved as Int. This triggers the when-concrete handler registered in step 2 and registered another when-concrete handler to wait for d. Since d has already been resolved to Float, the handler triggered and resolves b (the whole arithmetic expression) as Float. This also solves the earlier equation (step 2) that b<=Number.

  5. Now a can be resolved as Float, which also solves the step 1 equation that a>=b.

Last modified: 07 March 2024