bhc-types
Type representation for the Basel Haskell Compiler.
Overview
This crate implements the type system for BHC, including type representations, kinds, schemes, and substitutions. It provides the foundation for type inference and checking based on Hindley-Milner with extensions for higher-kinded types, type classes, and M9 dependent types preview.
Features
- Hindley-Milner type inference foundation
- Higher-kinded types
- Type classes with functional dependencies
- Type families
- GADTs support
- Rank-N polymorphism (limited)
- M9 Preview: Shape-indexed tensors with compile-time dimension checking
Key Types
| Type | Description |
|---|---|
Ty |
The main type representation |
TyVar |
Type variables for polymorphism |
TyId |
Unique type identifiers |
Kind |
Kinds for higher-kinded types |
Scheme |
Polymorphic type schemes |
Constraint |
Type class constraints |
Subst |
Type substitution |
PrimTy |
Unboxed primitive types |
TyNat |
Type-level natural numbers (M9) |
TyList |
Type-level lists for shapes (M9) |
Usage
Basic Types
use ;
use Symbol;
// Create type variables
let a = new_star; // a :: *
// Create a function type: a -> a
let identity_ty = fun;
// Create a polymorphic scheme: forall a. a -> a
let identity_scheme = poly;
Kind System
use Kind;
// Basic kinds
let star = Star; // *
let arrow = star_to_star; // * -> *
// Higher-kinded types
let functor_kind = Arrow;
// M9: Tensor kinds
let nat = Nat; // Nat
let shape_kind = nat_list; // [Nat]
let tensor_kind = tensor_kind; // [Nat] -> * -> *
Type Substitution
use ;
let a = new_star;
let mut subst = new;
subst.insert;
// Apply substitution to `a -> a`
let ty = fun;
let result = subst.apply;
// Result: Int# -> Int#
Primitive Types (Numeric Profile)
use ;
// Unboxed primitives for zero-overhead computation
let int = int_prim; // Int# (64-bit signed)
let double = double_prim; // Double# (64-bit float)
let float = float_prim; // Float# (32-bit float)
// Check properties
assert!;
assert!;
assert_eq!;
Shape-Indexed Tensors (M9)
use ;
// Type-level natural: 1024
let dim = nat_lit;
// Shape: '[1024, 768]
let shape = shape;
// Tensor type: Tensor '[m, k] Float
// for matrix multiplication
// matmul :: Tensor '[m, k] Float -> Tensor '[k, n] Float -> Tensor '[m, n] Float
Type Variants
Kind Variants
Primitive Types
| Type | Haskell Name | Size | Description |
|---|---|---|---|
I32 |
Int32# |
4 bytes | 32-bit signed integer |
I64 |
Int# |
8 bytes | 64-bit signed integer |
U32 |
Word32# |
4 bytes | 32-bit unsigned integer |
U64 |
Word# |
8 bytes | 64-bit unsigned integer |
F32 |
Float# |
4 bytes | 32-bit IEEE 754 float |
F64 |
Double# |
8 bytes | 64-bit IEEE 754 double |
Char |
Char# |
4 bytes | Unicode code point |
Addr |
Addr# |
8 bytes | Machine pointer |
Design Notes
- Types are immutable and hashable for efficient comparison
- Substitutions are lazily applied for performance
- The
Errortype enables recovery from type errors - M9 type-level features prepare for dependent type support
Related Crates
bhc-typeck- Type inference and checkingbhc-hir- High-level IR that uses these typesbhc-core- Core IR with explicit type annotationsbhc-intern- Symbol interning for type names
Specification References
- H26-SPEC Section 4: Type System Specification
- H26-SPEC Section 6.2: Unboxed Type Requirements
- H26-SPEC Section 7: Tensor Model