Skip to content

algebra

Algebraic interfaces and helpers used by Finch operators.

SingletonOperatorFType

Bases: ImmutableStructFType, FinchOperatorFType

A stateless callable type with one canonical operator value.

Source code in src/finch/algebra/algebra.py
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
class SingletonOperatorFType(ImmutableStructFType, FinchOperatorFType):
    """A stateless callable type with one canonical operator value."""

    @property
    @abstractmethod
    def operator(self) -> FinchOperator: ...

    @property
    def struct_name(self):
        return type(self).__name__.removeprefix("_").removesuffix("FType")

    @property
    def struct_fields(self):
        return []

    def from_fields(self):
        return self.operator

    def __call__(self, value):
        if ftype(value) == self:
            return value
        raise TypeError(f"Expected an operator of type {self}")

cansplitpush(op)

Whether a reduction can split-push through the same operator expression.

Source code in src/finch/algebra/algebra.py
198
199
200
def cansplitpush(op: FinchOperatorFType) -> bool:
    """Whether a reduction can split-push through the same operator expression."""
    return repeat_operator(op) is not None and is_commutative(op) and is_associative(op)

fixpoint_type(op, z, t)

Determines the fixpoint type after repeated calling the given operation.

Parameters:

Name Type Description Default
op CallableFType

The operation to evaluate.

required
z Any

The initial value.

required
t FType

The type to evaluate against.

required

Returns:

Type Description
FType

The fixpoint type.

Source code in src/finch/algebra/algebra.py
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
def fixpoint_type(op: CallableFType, z: Any, t: FType) -> FType:
    """
    Determines the fixpoint type after repeated calling the given operation.

    Args:
        op: The operation to evaluate.
        z: The initial value.
        t: The type to evaluate against.

    Returns:
        The fixpoint type.
    """
    s = set()
    z_type = ftype(z)
    r = z_type
    while r not in s:
        s.add(r)
        r = return_type(op, z_type, t)
    return r

is_specializable_value(val)

A constant is only worth specializing on if some operator's identity or annihilator rule can fire against it; see SPECIALIZABLE_VALUES.

Source code in src/finch/algebra/algebra.py
113
114
115
116
117
118
119
120
121
def is_specializable_value(val: Any) -> bool:
    """
    A constant is only worth specializing on if some operator's identity or
    annihilator rule can fire against it; see `SPECIALIZABLE_VALUES`.
    """
    try:
        return any(bool(val == candidate) for candidate in SPECIALIZABLE_VALUES)
    except (TypeError, ValueError):
        return False

repeat_operator(op)

If there exists an operator g such that f(x, x, ..., x) (n times) is equal to g(x, n), then return g.

Source code in src/finch/algebra/algebra.py
189
190
191
192
193
194
195
def repeat_operator(op: FinchOperatorFType):
    """
    If there exists an operator g such that
    f(x, x, ..., x)  (n times)  is equal to g(x, n),
    then return g.
    """
    return op.repeat_operator()

type_max(type_)

Returns the maximum value of the given type.

Parameters:

Name Type Description Default
type_ FDTypeOrdered

The type to determine the maximum value for.

required

Returns:

Type Description
Any

The maximum value of the given type.

Raises:

Type Description
AttributeError

If the maximum value is not implemented for the given type.

Source code in src/finch/algebra/algebra.py
173
174
175
176
177
178
179
180
181
182
183
184
185
186
def type_max(type_: FDTypeOrdered) -> Any:
    """
    Returns the maximum value of the given type.

    Args:
        type_: The type to determine the maximum value for.

    Returns:
        The maximum value of the given type.

    Raises:
        AttributeError: If the maximum value is not implemented for the given type.
    """
    return type_.type_max

type_min(type_)

Returns the minimum value of the given type.

Parameters:

Name Type Description Default
type_ FDTypeOrdered

The type to determine the minimum value for.

required

Returns:

Type Description
Any

The minimum value of the given type.

Raises:

Type Description
AttributeError

If the minimum value is not implemented for the given type.

Source code in src/finch/algebra/algebra.py
157
158
159
160
161
162
163
164
165
166
167
168
169
170
def type_min(type_: FDTypeOrdered) -> Any:
    """
    Returns the minimum value of the given type.

    Args:
        type_: The type to determine the minimum value for.

    Returns:
        The minimum value of the given type.

    Raises:
        AttributeError: If the minimum value is not implemented for the given type.
    """
    return type_.type_min