bhc-hir
High-level Intermediate Representation for the Basel Haskell Compiler.
Overview
This crate defines the HIR (High-level IR), a desugared representation that bridges the gap between the parsed AST and the typed Core IR. HIR preserves name resolution results while simplifying syntactic constructs.
Features
- Desugared patterns (guards, view patterns flattened)
- Resolved names with
DefIdreferences - Explicit binding groups for mutual recursion
- Type annotations preserved for type checking
- Pattern match compilation preparation
Key Types
| Type | Description |
|---|---|
HirId |
Unique identifier for HIR nodes |
DefId |
Definition identifier for resolved names |
DefRef |
Reference to a definition (local or external) |
Expr |
HIR expression enum |
Pat |
HIR pattern enum |
Item |
Top-level item (function, data, class, instance) |
Module |
Complete HIR module |
BindingGroup |
Group of mutually recursive bindings |
Usage
Lowering AST to HIR
use ;
use lower_module;
let hir_module = lower_module?;
// Access definitions
for item in &hir_module.items
Working with HIR IDs
use ;
// HIR IDs are unique within a module
let expr_id: HirId = hir_arena.alloc_expr;
// DefIds are unique across the compilation
let def_id: DefId = resolver.define;
// DefRef points to either local or external definitions
let def_ref = Local;
let def_ref = External;
Expression Variants
Pattern Variants
Binding Groups
Mutually recursive definitions are grouped together:
Desugaring Performed
| Source | HIR |
|---|---|
if c then t else e |
case c of { True -> t; False -> e } |
| Pattern guards | Nested case expressions |
do { stmts } |
Nested binds and sequences |
| List comprehensions | concatMap and filter |
where clauses |
let expressions |
| Multi-clause functions | Single function with case |
| Operator sections | Lambda expressions |
Design Notes
- HIR is the input to type checking
- Name resolution is complete at this stage
- Pattern match exhaustiveness not yet checked
- Types are optional (inferred during type checking)
- Maintains source spans for error reporting
Related Crates
bhc-ast- Input AST from parsingbhc-lower- AST to HIR loweringbhc-typeck- Type checks HIRbhc-hir-to-core- HIR to Core loweringbhc-span- Source locations
Specification References
- H26-SPEC Section 3.2: HIR Definition
- H26-SPEC Section 4: Type System (uses HIR)