Skip to content

Solve

Type resolution for type inference.

This module contains the type resolution algorithm for type inference. A simple algorithm is used to resolve the types of the IR by comparing the input types with the output types.

ResolutionError dataclass

ResolutionError(
    expr: types.TypeAttribute, value: types.TypeAttribute
)

Bases: TypeResolutionResult

Type resolution result for failed resolution.

ResolutionOk dataclass

ResolutionOk()

Bases: TypeResolutionResult

Type resolution result for successful resolution.

TypeResolution dataclass

TypeResolution(
    vars: dict[types.TypeVar, types.TypeAttribute] = dict()
)

Type resolution algorithm for type inference.

solve

solve(
    annot: types.TypeAttribute, value: types.TypeAttribute
) -> TypeResolutionResult

Solve the type resolution problem.

This method compares the expected type annot with the actual type value and returns a result indicating whether the types match or not.

Parameters:

Name Type Description Default
annot TypeAttribute

The expected type.

required
value TypeAttribute

The actual type.

required

Returns:

Type Description
TypeResolutionResult

A TypeResolutionResult object indicating the result of the

TypeResolutionResult

resolution.

Source code in src/kirin/analysis/typeinfer/solve.py
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
def solve(
    self, annot: types.TypeAttribute, value: types.TypeAttribute
) -> TypeResolutionResult:
    """Solve the type resolution problem.

    This method compares the expected type `annot` with the actual
    type `value` and returns a result indicating whether the types
    match or not.

    Args:
        annot: The expected type.
        value: The actual type.

    Returns:
        A `TypeResolutionResult` object indicating the result of the
        resolution.
    """
    if isinstance(annot, types.TypeVar):
        return self.solve_TypeVar(annot, value)
    elif isinstance(annot, types.Generic):
        return self.solve_Generic(annot, value)
    elif isinstance(annot, types.Union):
        return self.solve_Union(annot, value)

    if annot.is_subseteq(value):
        return Ok
    else:
        return ResolutionError(annot, value)

substitute

substitute(typ: types.TypeAttribute) -> types.TypeAttribute

Substitute type variables in the type with their values.

This method substitutes type variables in the given type with their values. If the type is a generic type, the method recursively substitutes the type variables in the type arguments.

Parameters:

Name Type Description Default
typ TypeAttribute

The type to substitute.

required

Returns:

Type Description
TypeAttribute

The type with the type variables substituted.

Source code in src/kirin/analysis/typeinfer/solve.py
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
def substitute(self, typ: types.TypeAttribute) -> types.TypeAttribute:
    """Substitute type variables in the type with their values.

    This method substitutes type variables in the given type with their
    values. If the type is a generic type, the method recursively
    substitutes the type variables in the type arguments.

    Args:
        typ: The type to substitute.

    Returns:
        The type with the type variables substituted.
    """
    if isinstance(typ, types.TypeVar):
        return self.vars.get(typ, typ)
    elif isinstance(typ, types.Generic):
        return types.Generic(
            typ.body, *tuple(self.substitute(var) for var in typ.vars)
        )
    elif isinstance(typ, types.Union):
        return types.Union(self.substitute(t) for t in typ.types)
    return typ

TypeResolutionResult dataclass

TypeResolutionResult()

Base class for type resolution results.