Idea: constraints should only have placeholders

This commit is contained in:
Johan B.W. de Vries 2025-05-05 17:33:44 +02:00
parent 0953975980
commit 4f19a5e503

View File

@ -33,13 +33,13 @@ class RequireTypeSubstitutes:
typing of the program, so this constraint can be updated.
"""
SubstitutionMap = Dict[placeholders.PlaceholderForType, types.Type3]
SubstitutionMap = Dict[PlaceholderForType, types.Type3]
NewConstraintList = List['ConstraintBase']
CheckResult = Union[None, SubstitutionMap, Error, NewConstraintList, RequireTypeSubstitutes]
HumanReadableRet = Tuple[str, Dict[str, Union[str, ourlang.Expression, types.Type3, placeholders.PlaceholderForType]]]
HumanReadableRet = Tuple[str, Dict[str, Union[str, ourlang.Expression, types.Type3, PlaceholderForType]]]
class Context:
"""
@ -99,9 +99,9 @@ class SameTypeConstraint(ConstraintBase):
"""
__slots__ = ('type_list', )
type_list: List[placeholders.Type3OrPlaceholder]
type_list: List[PlaceholderForType]
def __init__(self, *type_list: placeholders.Type3OrPlaceholder, comment: Optional[str] = None) -> None:
def __init__(self, *type_list: PlaceholderForType, comment: Optional[str] = None) -> None:
super().__init__(comment=comment)
assert len(type_list) > 1
@ -111,18 +111,10 @@ class SameTypeConstraint(ConstraintBase):
known_types: List[types.Type3] = []
phft_list = []
for typ in self.type_list:
if isinstance(typ, types.Type3):
known_types.append(typ)
continue
if isinstance(typ, placeholders.PlaceholderForType):
if typ.resolve_as is not None:
known_types.append(typ.resolve_as)
else:
phft_list.append(typ)
continue
raise NotImplementedError(typ)
if typ.resolve_as is not None:
known_types.append(typ.resolve_as)
else:
phft_list.append(typ)
if not known_types:
return RequireTypeSubstitutes()
@ -158,7 +150,7 @@ class SameTypeConstraint(ConstraintBase):
return f'SameTypeConstraint({args}, comment={repr(self.comment)})'
class TupleMatchConstraint(ConstraintBase):
def __init__(self, exp_type: placeholders.Type3OrPlaceholder, args: Iterable[placeholders.Type3OrPlaceholder], comment: str):
def __init__(self, exp_type: PlaceholderForType, args: Iterable[PlaceholderForType], comment: str):
super().__init__(comment=comment)
self.exp_type = exp_type
@ -166,7 +158,7 @@ class TupleMatchConstraint(ConstraintBase):
def check(self) -> CheckResult:
exp_type = self.exp_type
if isinstance(exp_type, placeholders.PlaceholderForType):
if isinstance(exp_type, PlaceholderForType):
if exp_type.resolve_as is None:
return RequireTypeSubstitutes()
@ -206,13 +198,13 @@ class MustImplementTypeClassConstraint(ConstraintBase):
context: Context
type_class3: Union[str, typeclasses.Type3Class]
types: list[placeholders.Type3OrPlaceholder]
types: list[PlaceholderForType]
DATA = {
'bytes': {'Foldable'},
}
def __init__(self, context: Context, type_class3: Union[str, typeclasses.Type3Class], types: list[placeholders.Type3OrPlaceholder], comment: Optional[str] = None) -> None:
def __init__(self, context: Context, type_class3: Union[str, typeclasses.Type3Class], types: list[PlaceholderForType], comment: Optional[str] = None) -> None:
super().__init__(comment=comment)
self.context = context
@ -222,10 +214,10 @@ class MustImplementTypeClassConstraint(ConstraintBase):
def check(self) -> CheckResult:
typ_list = []
for typ in self.types:
if isinstance(typ, placeholders.PlaceholderForType) and typ.resolve_as is not None:
if isinstance(typ, PlaceholderForType) and typ.resolve_as is not None:
typ = typ.resolve_as
if isinstance(typ, placeholders.PlaceholderForType):
if isinstance(typ, PlaceholderForType):
return RequireTypeSubstitutes()
typ_list.append(typ)
@ -267,12 +259,12 @@ class LiteralFitsConstraint(ConstraintBase):
"""
__slots__ = ('type3', 'literal', )
type3: placeholders.Type3OrPlaceholder
type3: PlaceholderForType
literal: Union[ourlang.ConstantPrimitive, ourlang.ConstantBytes, ourlang.ConstantTuple, ourlang.ConstantStruct]
def __init__(
self,
type3: placeholders.Type3OrPlaceholder,
type3: PlaceholderForType,
literal: Union[ourlang.ConstantPrimitive, ourlang.ConstantBytes, ourlang.ConstantTuple, ourlang.ConstantStruct],
comment: Optional[str] = None,
) -> None:
@ -296,7 +288,7 @@ class LiteralFitsConstraint(ConstraintBase):
'f64': None,
}
if isinstance(self.type3, placeholders.PlaceholderForType):
if isinstance(self.type3, PlaceholderForType):
if self.type3.resolve_as is None:
return RequireTypeSubstitutes()
@ -436,17 +428,17 @@ class CanBeSubscriptedConstraint(ConstraintBase):
"""
__slots__ = ('ret_type3', 'type3', 'index', 'index_phft', )
ret_type3: placeholders.Type3OrPlaceholder
type3: placeholders.Type3OrPlaceholder
ret_type3: PlaceholderForType
type3: PlaceholderForType
index: ourlang.Expression
index_phft: placeholders.Type3OrPlaceholder
index_phft: PlaceholderForType
def __init__(
self,
ret_type3: placeholders.PlaceholderForType,
type3: placeholders.PlaceholderForType,
ret_type3: PlaceholderForType,
type3: PlaceholderForType,
index: ourlang.Expression,
index_phft: placeholders.PlaceholderForType,
index_phft: PlaceholderForType,
comment: Optional[str] = None,
) -> None:
super().__init__(comment=comment)
@ -458,7 +450,7 @@ class CanBeSubscriptedConstraint(ConstraintBase):
def check(self) -> CheckResult:
exp_type = self.type3
if isinstance(exp_type, placeholders.PlaceholderForType):
if isinstance(exp_type, PlaceholderForType):
if exp_type.resolve_as is None:
return RequireTypeSubstitutes()