> ## Content Index
> Fetch the complete content index at: https://dsl-consultancy.ghost.io/llms.txt
> Use this file to discover other available public pages before exploring further.

# Visualizing computed types
- URL: https://dsl-consultancy.ghost.io/visualizing-computed-types/
- Published: 2026-11-06T12:53:21.000Z
- Updated: 2026-11-17T11:00:05.000Z
- Author: Meinte Boersma

💡

This is post 12 in my blog series on MyFPL — see [the index/table of contents](https://codeberg.org/dslmeinte/my-fpl/src/branch/main/blogs/index.adoc?ref=dsl-consultancy.ghost.io).  
  
The code in this and previous blog posts can also be found [here, on Codeberg](https://codeberg.org/dslmeinte/my-fpl?ref=dsl-consultancy.ghost.io). Check out the repo at the commit marked “blog 12”.

In this blog post, we’re going to trigger type computation on our programs, and then visualize the computed types. We’ll re-use the mechanism that we implemented for the reductions of the values of a program:

1. Compute the reduction of a values of a program
2. Store that reduction inside an annotation node
3. Have the projection extract that annotation node and the reduced value, and visualize it.

There’s a small difference: there’s precisely one reduction for every value in the `values` containment feature of a `Program` node, but we want to compute types for *every* typable node in the whole AST. Luckily, this difference can be dealt with using a simple expression involving built-in LionWeb functionality.

## Defining an annotation to store computed types on

As before for the reduction of a value, we define a new `ComputedType` annotation that holds a computed type in its `type` containment feature. We add the following code to the `packages/build/src/structure.ts` file:

```
const ComputedType = factory.annotation("ComputedType").annotating(Typable)
factory.containment(ComputedType, "type").ofType(Type)
```

Listing 12\. 1\. Defining the `ComputedType` annotation (in the `packages/build/src/structure.ts` file)

After running the NPM `generate` task in the `build` package, we can work with the new `ComputedType` class.

We can visualize the new classifier (on the meta-level) with the following diagram:

![computedType meta level](https://storage.ghost.io/c/eb/4d/eb4df6b9-0e4f-4d29-b975-4075848c0001/content/images/2026/11/computedType-meta-level.png)

The `ComputedType` annotation as part of MyFPL’s structure

On the node-level, this would look as follows:

![computedType node level](https://storage.ghost.io/c/eb/4d/eb4df6b9-0e4f-4d29-b975-4075848c0001/content/images/2026/11/computedType-node-level.png)

A `Typable` node, the `ComputedType` node that annotates the former, and the computed type itself

## Adding computed types to the AST

Just as we did for the reductions of values, we implement a function to add computed types to a whole program, in the `packages/my-fpl/src/type-computer.ts`:

```
import { nodeBaseReader } from "@lionweb/class-core" ❶
import { nodesExtractorUsing } from "@lionweb/core"
import { newId } from "./ids.js"
import { BinaryOperation, BooleanLiteral, BooleanType, ComputedType, Program, Typable, Type } from "./MyFPL.g.js"
import { isTypable } from "./reflection.g.js"

export const addTypesTo = (program: Program) => { ❷
    for (const node of nodesExtractorUsing(nodeBaseReader)(program)) { ❸
        if (isTypable(node)) { ❹
            const computedTypeAnno = ComputedType.create(newId()) ❺
            computedTypeAnno.type = typeOf(node) ❻
            node.addAnnotation(computedTypeAnno) ❼
        }
    }
}
```

Listing 12\. 2\. Adding computed types to all typable AST nodes (in the `packages/my-fpl/type-computer.ts` file)

1. Import various new things from the corresponding LionWeb dependencies, or files in the `my-fpl` package. On line 4, the types `BooleanType`, `ComputedType`, and `Program` are added.
2. Define a `addTypesTo` function that takes a program, which is the root node (of concept `Program`) of the entire AST.
3. Loop over *all* nodes of the given program. The expression `nodesExtractorUsing(nodeBaseReader)` produces a function that takes an AST node, and returns *all* nodes *(including annotation instances)* that are descendants of the given AST node, or that AST node itself. So, the expression `nodesExtractorUsing(nodeBaseReader)(program)` produces all nodes the given program.
4. Only try to compute types for typable nodes, i.e. instances of the `Typable` interface, detected using the `isTypable` function.
5. Instantiate a `ComputedType` annotation node with a randomly-generated ID.
6. Compute the type of `node`, and assign that type to the `type` feature of the annotation node.
7. Add the annotation node to `node`.

Next, we have to call this function, from the file `packages/my-fpl/src/example.ts`:

```
import { addComputedTypesTo } from "./type-computer.js"

addReductionsTo(myProgram) ❶
addComputedTypesTo(myProgram)
```

Listing 12\. 3\. Triggering adding computed types to all typable AST nodes (in the `packages/my-fpl/example.ts` file)

1. This is a pre-existing line of code — and it’s the only one in this listing. Having that line there, ensures that the reductions of the values of the program are computed and added to the program’s AST *before* we trigger type computation. Because the `addComputedTypesTo` function loops over all nodes including all annotation instances, this means that we also compute types for all typable nodes in the reductions of the values of the program.

## Visualizing the computed types

Now, we want to visualize the computed types on the projected AST. This is ultimately a UI/UX matter. There’s a myriad ways we could do this, depending on the amount of code we feel like spending on HTML, CSS, and additional JavaScript. Personally, I don’t feel like spending any of that, at the moment, so I chose to simply use HTML’s good ol’ `title` attribute to render information on the visualization of typable nodes as a poor man’s tooltip-of-sorts. But, before we can put in those `title` attributes, we need to come up with what text to display.

```
import { ..., BooleanType, ... } from "./MyFPL.g.js" ❶

export const typeOn = (node: Typable) => ❷
    node.annotations.find((anno) =>
        anno instanceof ComputedType)?.type

export const typeAsText = (type: Type): string => { ❸
    if (type instanceof BooleanType) { ❹
        return "boolean"
    }
    throw new Error(concept not handled by typeAsText: ${type.classifier.name}) ❺
}
```

Listing 12\. 4\. Rendering computed types as text (in the `packages/my-fpl/example.ts` file)

1. Also import the `BooleanType` type, for the implementation of the `typeAsText` function.
2. Define a `typeOn` function that extracts the *first* `ComputedType` annotation node from the given typable node, and returns the value of the `type` containment feature. Although we try to ensure that all typable nodes contain a `ComputedType` annotation node, we also handle the situation if that’s not the case. In that case, this function returns `undefined`.
3. Define a `typeAsText` that renders a `Type` node as text.
4. Handle a boolean type — currently, the only concrete `Type` concept.
5. Throw an `Error` with a meaningful message if we forget to handle a concrete `Type` concept.

Now, we can use these functions in the `Projection` function.

```
import { ..., Typable } from "./MyFPL.g.js" ❶
import { typeAsText, typeOn } from "./type-computer.js"

const titleTextFor = (node: Typable) => { ❷
    const maybeType = typeOn(node) ❸
    return maybeType === undefined ? (no type for node with ID=${node.id}) : typeAsText(maybeType) ❹
}
```

Listing 12\. 5\. Rendering computed types as text suitable for the `title` attribute (in the `packages/my-fpl/projection.tsx` file)

1. Also import the `Typable` type.
2. Define a `titleTextFor` function that returns a suitable text for the `title` attribute of the HTML element returned by the `Projection` for a given node.
3. Try to extract the computed type from the given `node` — `maybeType` will be `undefined` in case there isn’t one.
4. Return a textual rendering of the computed type, or some “debug-like” information in case no computed type was extracted.

Finally, we want to use the `titleTextFor` function in the `Projection` function. Recall that this function is essentially consists of blocks of the following form:

```
if (node instanceof <name of concrete concept>) {
    return <<tag name> className="..." [other attributes]> ❶
        ...
    </<tag name>>
}
```

Listing 12\. 6\. Rendering a computed type as a `title` attribute (in the `packages/my-fpl/projection.tsx` file)

1. The <*tag name*\> is typically either “`span`” or “`div`”.

For every concrete, typable concept we handle in the `Projection` function, we simply add the following code to the \[*other attributes*\] section: `title={titleTextFor(node)}`. Currently, the only concrete, typable concepts in MyFPL’s structure are `BooleanLiteral`, and `BinaryOperation`. I’ll leave the actual code change as an exercise for the reader — and you can always compare with [my code implementation on Codeberg](https://codeberg.org/dslmeinte/my-fpl/src/branch/main/packages/my-fpl/src/projection.tsx?ref=dsl-consultancy.ghost.io).

After doing so, we can build the code, run the transpiled `dist/example.js`, and look at the `example.html` in the browser — (both of these files relative from the `packages/my-fpl/` directory). This allows us to see the computed types of typable AST nodes by hovering over its visualized projection.

![screenshot title](https://storage.ghost.io/c/eb/4d/eb4df6b9-0e4f-4d29-b975-4075848c0001/content/images/2026/11/screenshot-title.png)

Inspect the computed type of the `false` boolean literal by (mouse-)hovering over it, and look at the text that appears

💡

In this blog post, we computed types for typable AST nodes, and visualized those. In the next blog post, we’re going to add the integer type to MyFPL. Then, it starts making sense to do **type checking* through constraints that use computed types. We’ll implement a constraints checker the blog post (#14) to do exactly that.

© 2026 Meinte Boersma (DSL Consultancy)