pub struct TypeInference { /* private fields */ }Expand description
Type inference engine implementing Algorithm W.
Maintains state for fresh type variable generation and constraint accumulation.
Implementations§
Source§impl TypeInference
impl TypeInference
Sourcepub fn fresh_var(&mut self) -> TypeVar
pub fn fresh_var(&mut self) -> TypeVar
Generate a fresh type variable.
Each call produces a unique type variable that hasn’t been used before.
Sourcepub fn infer(&mut self, expr: &Expr, env: &TypeEnv) -> Result<Type, TypeError>
pub fn infer(&mut self, expr: &Expr, env: &TypeEnv) -> Result<Type, TypeError>
Infer the type of an expression in the given environment.
This is the main entry point for type inference. It generates constraints and returns a type (possibly containing type variables).
§Arguments
expr- The expression to type checkenv- The type environment containing variable bindings
§Returns
The inferred type, or a type error if inference fails.
Sourcepub fn infer_pattern(
&mut self,
pattern: &Pattern,
scrutinee_ty: &Type,
env: &TypeEnv,
) -> Result<TypeEnv, TypeError>
pub fn infer_pattern( &mut self, pattern: &Pattern, scrutinee_ty: &Type, env: &TypeEnv, ) -> Result<TypeEnv, TypeError>
Infer pattern bindings and check pattern type matches scrutinee.
Returns an extended environment with pattern variable bindings.
Sourcepub fn solve_constraints(&mut self) -> Result<Substitution, TypeError>
pub fn solve_constraints(&mut self) -> Result<Substitution, TypeError>
Solve all accumulated constraints using unification.
Returns a substitution that satisfies all constraints.
Sourcepub fn unify(&self, t1: &Type, t2: &Type) -> Result<Substitution, TypeError>
pub fn unify(&self, t1: &Type, t2: &Type) -> Result<Substitution, TypeError>
Unify two types using Robinson’s unification algorithm.
Returns a substitution that makes the types equal, or an error if unification fails.
Sourcepub fn infer_and_solve(
&mut self,
expr: &Expr,
env: &TypeEnv,
) -> Result<Type, TypeError>
pub fn infer_and_solve( &mut self, expr: &Expr, env: &TypeEnv, ) -> Result<Type, TypeError>
Convenience method: infer type and solve constraints in one step.
This is the main entry point for type checking.
§Example
use fusabi_frontend::inference::TypeInference;
use fusabi_frontend::types::TypeEnv;
use fusabi_frontend::ast::{Expr, Literal};
let mut inference = TypeInference::new();
let env = TypeEnv::new();
let expr = Expr::Lit(Literal::Int(42));
let ty = inference.infer_and_solve(&expr, &env).unwrap();