home / skills / a5c-ai / babysitter / type-inference-engine

This skill helps you implement and test type inference algorithms like Algorithm W and Hindley-Milner, guiding constraint solving and error diagnosis.

npx playbooks add skill a5c-ai/babysitter --skill type-inference-engine

Review the files below or copy the command above to add this skill to your agents.

Files (1)
SKILL.md
1.0 KB
---
name: type-inference-engine
description: Implement and test type inference algorithms including Algorithm W and constraint-based inference
allowed-tools:
  - Bash
  - Read
  - Write
  - Edit
  - Glob
  - Grep
metadata:
  specialization: computer-science
  domain: science
  category: programming-language-theory
  phase: 6
---

# Type Inference Engine

## Purpose

Provides expert guidance on implementing type inference algorithms for programming language implementations.

## Capabilities

- Algorithm W implementation
- Constraint generation and solving
- Unification with occurs check
- Let-polymorphism (Hindley-Milner)
- Principal type computation
- Type error diagnosis

## Usage Guidelines

1. **Constraint Generation**: Generate type constraints from expressions
2. **Unification**: Implement unification algorithm
3. **Generalization**: Handle let-polymorphism
4. **Error Messages**: Generate informative type errors
5. **Testing**: Validate inference on test cases

## Tools/Libraries

- Language workbenches
- Constraint solvers
- Type inference libraries

Overview

This skill implements and tests type inference algorithms for language prototypes and compilers. It focuses on Algorithm W and constraint-based inference, unification with occurs check, and let-polymorphism (Hindley-Milner). The goal is reliable principal type computation and actionable type error diagnostics for JavaScript-based tooling.

How this skill works

It generates type constraints from AST nodes and either solves them directly (constraint solving) or applies Algorithm W to produce principal types. A unifier resolves type equalities while performing the occurs check to avoid infinite types. The system generalizes types at let-bindings to support polymorphism and includes hooks for producing clear error messages and test assertions.

When to use it

  • Building a statically typed DSL or research language prototype
  • Adding type inference to an interpreter or compiler frontend
  • Teaching or demonstrating Hindley-Milner and Algorithm W
  • Diagnosing and improving type error messages
  • Validating type system changes with automated tests

Best practices

  • Start by defining a small, well-typed core language subset and AST shape
  • Implement constraint generation per expression node and keep constraints explicit
  • Write a robust unification routine that includes the occurs check to prevent recursive types
  • Generalize only at let-bindings and track free type variables precisely
  • Create a comprehensive test suite with positive and negative cases including principal-type checks and error snapshots

Example use cases

  • Implement Algorithm W to infer types for lambda calculus and let-expressions
  • Use constraint generation and solving to extend a type system with records or variants
  • Add informative type error diagnostics to a language server or REPL
  • Validate type inference correctness with a deterministic test harness and regression cases
  • Prototype polymorphic type inference for a new functional language feature

FAQ

Does this implement both Algorithm W and constraint-based approaches?

Yes. The skill includes implementations and tests for Algorithm W and a constraint-generation/solving pipeline so you can compare and extend either approach.

How are type errors reported?

Type errors are generated during constraint solving or unification with contextual source locations. The tool emphasizes concise, actionable messages and supports adding custom diagnostics for new language constructs.