Inspiration

Programmers who live in Python, Ruby or JavaScript often see static types as paperwork: long annotations and compilers that complain. But a type checker does something simple. It decides which pieces fit together. I wanted to make that visible, and to pick the most dynamic, live environment I know, a Smalltalk image, to show off one of the most static: OCaml's type inference. That pairing is the Frankenstein part.

What it does

You type OCaml into a window in a running Cuis Smalltalk image and press Check. Every line comes back as a drawing:

  • Base types are atoms with distinct outlines: a square int, a ribbon string, a triangle bool.
  • A function is a machine: holes on the left for what it accepts, a solid plug on the right for what it returns. Curried functions show a seam between sockets; a function that takes a function has a machine inside a socket.
  • Type variables are ghosts. Each letter keeps one colour per diagram, so 'a -> 'a reads as "the same ghost goes in and comes out".
  • Lists are decks, options are "shape or none" boxes, refs are boxes with a lid, tuples are trays.
  • Your own types are cards: variants with "or" bars between constructors, records with labelled drawers, wavy ones with a key when mutable, and a small marker where a type contains itself.
  • Objects are puppets with one button per method. An open object type, OCaml's < speak : 'a; .. >, has a torn edge: anything with at least these buttons fits. That is duck typing, checked before the program runs.
  • A type error is a spark between the shape OCaml found and the shape it expected, next to OCaml's own message.

Three example files are one click away: a tour of every shape, a duck typing example, and "bugs caught": the null-like value used as a string, strings added to numbers, swapped arguments, a write to an immutable field. A Legend window explains every shape in plain words.

How I built it

Two halves, joined by a subprocess and JSON:

  • OCaml helper (ocaml/oshapes.ml, about 200 lines). It uses OCaml's own compiler libraries to parse and type-check the code phrase by phrase, exactly as the OCaml toplevel does, and prints every value's type, every type definition and every error as a JSON tree. For type clashes it reads the found and expected types out of OCaml's unification trace, so errors become two real shapes, not parsed text.
  • Cuis Smalltalk package (cuis/TypeShapes.pck.st). It runs the helper through OSProcess in a background process, turns the JSON into a small type model and draws it with Morphic 3 vector graphics: 18 morph classes, each drawing its outline as a path.

I built it with Kiro CLI, spec first, with Kiro working directly inside the live image through the Cuis MCP server. The Kiro write-up covers that in detail.

The release bundle is built by a script from an untouched Cuis image: updates, packages and fonts are loaded headless, and another script extracts the bundle into a fresh folder without OCaml installed and runs the whole test suite there. 68 Smalltalk tests and 26 OCaml helper tests pass.

Challenges I ran into

  • Crashing the live image. Changing a class's superclass the quick way swapped a pointer without reshaping existing objects, and the VM segfaulted. The fix became a project rule and then a Kiro hook that blocks the mistake.
  • What works in my image is not what works in yours. My development image had extra packages loaded. The first bundle verification failed 32 of 62 tests because TypeShapes used a JSON parser it never declared. The helper also found OCaml's standard library at a path that only exists on my machine. Both were invisible until the bundle was tested somewhere clean.
  • Warnings in the wrong pipe. OCaml printed a warning to stderr, which ended up inside the JSON. Now the helper turns warnings off and the bridge keeps stderr apart.
  • Drawing see-through ghosts in a deck of cards blurred them together, so each card in a list's deck became an opaque sheet.

Accomplishments I'm proud of

  • Errors are drawn from OCaml's real type data, not from parsing its messages: the found and expected shapes come straight from the unification trace.
  • Property-based tests grounded in a research paper: Rémy and Vouillon's Objective ML (1998), the design behind OCaml's objects. On random method sets they check that an object fits a function exactly when it has every method the function calls. Pointed at a wrong belief about duck typing, the test shrinks a counterexample to one object with size and a function calling size and color.
  • A one-download bundle that passes its full test suite in a folder named judge's machine, quote and space included, with no OCaml installed.

What I learned

I greatly overlooked mcp as hollow shiny toys, but actually it was Cuis MCP what allowed this crazy science experiment to go on. Without it it would have been almost impossible to code in a live smalltalk image. I'm looking forward to find or create mcp for compilers of the languages I cherish like ocaml and haxe.

What's next

  • Read types from other languages into the same shapes: Haskell, F#, Gleam and ReScript all have the pieces the design language describes.
  • Interactive mode: drag a value onto a machine's socket and watch it snap, or spark.
  • Modules, polymorphic variants and GADTs, which are drawn as closed crates today.
  • A macOS bundle.

Built With

Share this project:

Updates

Submission history