TypeInference

Struct TypeInference 

Source
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

Source

pub fn new() -> Self

Create a new type inference instance.

Source

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.

Source

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 check
  • env - The type environment containing variable bindings
§Returns

The inferred type, or a type error if inference fails.

Source

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.

Source

pub fn solve_constraints(&mut self) -> Result<Substitution, TypeError>

Solve all accumulated constraints using unification.

Returns a substitution that satisfies all constraints.

Source

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.

Source

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();

Trait Implementations§

Source§

impl Default for TypeInference

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

§

impl<T> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.