Type Inference Data Flow
Relevant source files
The following files were used as context for generating this wiki page:
The Jazz language implements a strong, static type system featuring type inference for its core expression subset. The type inference engine is designed to handle functional programming constructs inspired by Haskell and Elixir, supporting features such as first-class functions, default currying, and partial application without requiring explicit type signatures for every binding.
In the active jazz-next compiler path, the type inference system acts as a plumbing layer that carries warnings and semantic errors derived from scope-aware analysis. It ensures that inferred types align with explicit signatures and manages the transition from analyzed ASTs to fully typed representations suitable for later compilation stages.
Sources: README.md:12-19, jazz-next/README.md:1-25
Compiler Pipeline and Data Flow
The data flow for type inference is integrated into the middle-end of the jazz-next compiler. It occurs after the surface syntax has been parsed and lowered into the analyzer-facing core AST, and typically runs in conjunction with or immediately following semantic analysis.
Type Inference Stages
- Lowering: The Parser-surface AST is converted to the Analyzer-AST boundary.
- Analysis: The
Analyzer.hsmodule performs scope-aware rebinding checks and validates the coherence between explicit type signatures and their associated bindings. - Inference: The
TypeInference.hsmodule processes the expressions to determine the types of untyped bindings and validates types against the provided signatures (e.g., checking if anInt -> Intassignment matches the expression). - Driver Integration: The
Driver.hsmodule gates the process based on warning-as-error configurations and propagates semantic errors into the final results.
Sources: jazz-next/README.md:12-25
Compilation Flow Diagram
The following diagram illustrates how data flows from the raw source code through the type inference system within the jazz-next implementation.
Sources: jazz-next/README.md:12-25
Key Components and Responsibilities
The type inference system relies on several specific modules within the jazz-next/src/JazzNext/Compiler/ directory to manage metadata and error states.
| Component | Responsibility | Relevant Files |
|---|---|---|
| AST Definition | Defines the core expression subset that supports static typing and inference. | AST.hs, Parser/AST.hs |
| Coherence Checker | Validates signature adjacency and ensures unbound variables are flagged before or during inference. | Analyzer.hs |
| Inference Engine | Implements the logic for deriving types from expressions and curried functions. | TypeInference.hs |
| Diagnostic Handler | Packages type mismatches and inference failures into deterministic warning/error payloads. | Diagnostics.hs, Warnings.hs |
Sources: README.md:12-18, jazz-next/README.md:12-25
Inference Logic for Core Features
Currying and Partial Application
Jazz functions are curried by default. The type inference system must track partial applications to correctly determine the resulting function type or value type. For example, applying a function with signature Num -> Num -> Num to a single Num argument must result in an inferred type of Num -> Num.
Sources: README.md:38-48
Purity Enforcement
The inference data flow is sensitive to the distinction between pure and impure functions. Impure functions (denoted by !) are tracked during analysis and inference to prevent them from being called within pure binding bodies. The Analyzer.hs provides stub enforcement for direct ! callee calls, which is then utilized by the inference and driver layers.
Sources: README.md:20-21, jazz-next/README.md:22
Diagnostic Integration
Type inference is not just a transformation but a source of semantic metadata. The TypeInference.hs module is specifically noted for "inference result plumbing" that carries both:
- Warnings: Such as scope-aware rebinding warnings.
- Semantic Errors: Such as type mismatches between an inferred expression and an explicit
::signature.
Sources: jazz-next/README.md:22-24
Implementation Status
| Feature | Status | Implementation Path |
|---|---|---|
| Strong Static Typing | Implemented (Core subset) | jazz-next/ |
| Type Inference | Implemented (Core subset) | jazz-next/ |
| Signature Coherence | Implemented | Analyzer.hs |
| ADTs / Pattern Matching | Planned / Aspirational | N/A |
| Full Effect Typing | Planned | N/A |
Sources: README.md:12-25, jazz-next/README.md:5-10
The type inference system is a critical component of the Jazz compiler's middle-end, bridging the gap between raw syntactic structures and validated semantic trees. By integrating closely with the diagnostic and analysis modules, it ensures that type safety is maintained while providing the flexibility of implicit typing for the core language expressions.
Sources: README.md:1-25, jazz-next/README.md:1-30