6 min read
|
Saved February 14, 2026
|
Copied!
Do you care about this?
This article explains bidirectional type checking, a method that simplifies type inference and checking in programming languages. It outlines a straightforward implementation using a small language and demonstrates how to build a functional type checker. Readers can learn how type annotations and abstract syntax trees work together in this process.
If you do, here's more
Type checkers can seem both straightforward and intricate. The author, influenced by David Christiansen, introduces Bidirectional Type Checking as a clear approach to understanding type systems. This method involves inferring types and checking types, requiring only annotations at function definitions. The example code provided, a simple function named `sillyExample`, demonstrates a valid type check even without extensive annotations.
The article presents a basic implementation of a bidirectional type checker in about 100 lines of code. It outlines the data structures used, such as `Type`, `Expr`, and `Context`, which facilitate the type-checking process. The author emphasizes the utility of an abstract syntax tree for managing code, making it easier to check types incrementally. The `infer` and `check` functions are central to this implementation, allowing for recursive type checking of expressions like function calls and blocks.
An example of adding new features, such as supporting addition, illustrates how the type checker can be extended. The implementation checks that only numbers can be added together, showcasing how the bidirectional nature of the checker allows for flexible interleaving of type inference and checking. The author hints at further challenges, like handling type coercion, inviting readers to experiment with the code. Overall, the article provides a practical foundation for building and understanding a type checker.
Questions about this article
No questions yet.