phasm/phasm/type5/constraints.py
Johan B.W. de Vries e68b49862c Notes
2025-07-19 16:27:33 +02:00

55 lines
1.4 KiB
Python

from typing import Any, Protocol
from ..build.base import BuildBase
from .kindexpr import KindExpr, Star
from .typeexpr import TypeExpr, TypeVariable
class ExpressionProtocol(Protocol):
"""
A protocol for classes that should be updated on substitution
"""
type5: TypeExpr | None
"""
The type to update
"""
class Context:
__slots__ = ("build", "placeholder_count", )
build: BuildBase[Any]
placeholder_count: int
def __init__(self, build: BuildBase[Any]) -> None:
self.build = build
self.placeholder_count = 0
def make_placeholder(self, *args: ExpressionProtocol, kind: KindExpr = Star()) -> TypeVariable:
self.placeholder_count += 1
return TypeVariable(kind, f"p_{self.placeholder_count}")
class ConstraintBase:
__slots__ = ("ctx", "comment",)
ctx: Context
comment: str | None
def __init__(self, ctx: Context, comment: str | None = None) -> None:
self.ctx = ctx
self.comment = comment
class LiteralFitsConstraint(ConstraintBase):
def __init__(self, ctx: Context, type: TypeExpr, val: Any, *, comment: str | None = None) -> None:
super().__init__(ctx, comment)
# TODO
class UnifyTypesConstraint(ConstraintBase):
def __init__(self, ctx: Context, lft: TypeExpr, rgt: TypeExpr, *, comment: str | None = None) -> None:
super().__init__(ctx, comment)
# TODO