Compare commits

..

1 Commits

Author SHA1 Message Date
Johan B.W. de Vries
b40a6a4cdd Removes the special casing for foldl
Has to implement both functions as arguments and type
place holders (variables) for type constructors.
2025-04-27 12:54:34 +02:00
11 changed files with 119 additions and 192 deletions

View File

@ -8,7 +8,6 @@ from . import ourlang, prelude, wasm
from .runtime import calculate_alloc_size, calculate_member_offset from .runtime import calculate_alloc_size, calculate_member_offset
from .stdlib import alloc as stdlib_alloc from .stdlib import alloc as stdlib_alloc
from .stdlib import types as stdlib_types from .stdlib import types as stdlib_types
from .type3 import functions as type3functions
from .type3 import placeholders as type3placeholders from .type3 import placeholders as type3placeholders
from .type3 import typeclasses as type3classes from .type3 import typeclasses as type3classes
from .type3 import types as type3types from .type3 import types as type3types
@ -336,7 +335,7 @@ def tuple_instantiation(wgn: WasmGenerator, inp: ourlang.TupleInstantiation) ->
# Store each element individually # Store each element individually
offset = 0 offset = 0
for element, exp_type3 in zip(inp.elements, args, strict=True): for element, exp_type3 in zip(inp.elements, args):
if isinstance(exp_type3, type3placeholders.PlaceholderForType): if isinstance(exp_type3, type3placeholders.PlaceholderForType):
assert exp_type3.resolve_as is not None assert exp_type3.resolve_as is not None
assert isinstance(exp_type3.resolve_as, type3types.Type3) assert isinstance(exp_type3.resolve_as, type3types.Type3)
@ -435,10 +434,10 @@ def expression(wgn: WasmGenerator, inp: ourlang.Expression) -> None:
assert isinstance(inp.type3, type3types.Type3), type3placeholders.TYPE3_ASSERTION_ERROR assert isinstance(inp.type3, type3types.Type3), type3placeholders.TYPE3_ASSERTION_ERROR
type_var_map: Dict[type3functions.TypeVariable, type3types.Type3] = {} type_var_map: Dict[type3classes.TypeVariable, type3types.Type3] = {}
for type_var, arg_expr in zip(inp.operator.signature.args, [inp.left, inp.right, inp], strict=True): for type_var, arg_expr in zip(inp.operator.signature, [inp.left, inp.right, inp]):
if not isinstance(type_var, type3functions.TypeVariable): if not isinstance(type_var, type3classes.TypeVariable):
# Fixed type, not part of the lookup requirements # Fixed type, not part of the lookup requirements
continue continue
@ -477,8 +476,8 @@ def expression(wgn: WasmGenerator, inp: ourlang.Expression) -> None:
# FIXME: Duplicate code with BinaryOp # FIXME: Duplicate code with BinaryOp
type_var_map = {} type_var_map = {}
for type_var, arg_expr in zip(inp.function.signature.args, inp.arguments + [inp], strict=True): for type_var, arg_expr in zip(inp.function.signature, inp.arguments + [inp]):
if not isinstance(type_var, type3functions.TypeVariable): if not isinstance(type_var, type3classes.TypeVariable):
# Fixed type, not part of the lookup requirements # Fixed type, not part of the lookup requirements
continue continue

View File

@ -4,7 +4,6 @@ Contains the syntax tree for ourlang
from typing import Dict, Iterable, List, Optional, Union from typing import Dict, Iterable, List, Optional, Union
from . import prelude from . import prelude
from .type3.functions import FunctionSignature, TypeVariableContext
from .type3.placeholders import PlaceholderForType, Type3OrPlaceholder from .type3.placeholders import PlaceholderForType, Type3OrPlaceholder
from .type3.typeclasses import Type3ClassMethod from .type3.typeclasses import Type3ClassMethod
from .type3.types import Type3 from .type3.types import Type3
@ -270,22 +269,21 @@ class FunctionParam:
name: str name: str
type3: Type3OrPlaceholder type3: Type3OrPlaceholder
def __init__(self, name: str, type3: Type3) -> None: def __init__(self, name: str, type3: Optional[Type3]) -> None:
self.name = name self.name = name
self.type3 = type3 self.type3 = PlaceholderForType([self]) if type3 is None else type3
class Function: class Function:
""" """
A function processes input and produces output A function processes input and produces output
""" """
__slots__ = ('name', 'lineno', 'exported', 'imported', 'statements', 'signature', 'returns_type3', 'posonlyargs', ) __slots__ = ('name', 'lineno', 'exported', 'imported', 'statements', 'returns_type3', 'posonlyargs', )
name: str name: str
lineno: int lineno: int
exported: bool exported: bool
imported: Optional[str] imported: Optional[str]
statements: List[Statement] statements: List[Statement]
signature: FunctionSignature
returns_type3: Type3 returns_type3: Type3
posonlyargs: List[FunctionParam] posonlyargs: List[FunctionParam]
@ -295,7 +293,6 @@ class Function:
self.exported = False self.exported = False
self.imported = None self.imported = None
self.statements = [] self.statements = []
self.signature = FunctionSignature(TypeVariableContext(), [])
self.returns_type3 = prelude.none # FIXME: This could be a placeholder self.returns_type3 = prelude.none # FIXME: This could be a placeholder
self.posonlyargs = [] self.posonlyargs = []
@ -326,14 +323,12 @@ class StructConstructor(Function):
def __init__(self, struct_type3: Type3) -> None: def __init__(self, struct_type3: Type3) -> None:
super().__init__(f'@{struct_type3.name}@__init___@', -1) super().__init__(f'@{struct_type3.name}@__init___@', -1)
self.returns_type3 = struct_type3
st_args = prelude.struct.did_construct(struct_type3) st_args = prelude.struct.did_construct(struct_type3)
assert st_args is not None assert st_args is not None
for mem, typ in st_args.items(): for mem, typ in st_args.items():
self.posonlyargs.append(FunctionParam(mem, typ, )) self.posonlyargs.append(FunctionParam(mem, typ, ))
self.signature.args.append(typ)
self.returns_type3 = struct_type3
self.signature.args.append(struct_type3)
self.struct_type3 = struct_type3 self.struct_type3 = struct_type3

View File

@ -158,18 +158,9 @@ class OurVisitor:
_not_implemented(not node.args.posonlyargs, 'FunctionDef.args.posonlyargs') _not_implemented(not node.args.posonlyargs, 'FunctionDef.args.posonlyargs')
for arg in node.args.args: for arg in node.args.args:
if arg.annotation is None:
_raise_static_error(node, 'Must give a argument type')
arg_type = self.visit_type(module, arg.annotation)
# if isisntance(arg_type, TypeVariable):
# arg_type = PlaceHolderForType()
function.signature.args.append(arg_type)
function.posonlyargs.append(FunctionParam( function.posonlyargs.append(FunctionParam(
arg.arg, arg.arg,
arg_type, self.visit_type(module, arg.annotation) if arg.annotation else None,
)) ))
_not_implemented(not node.args.vararg, 'FunctionDef.args.vararg') _not_implemented(not node.args.vararg, 'FunctionDef.args.vararg')
@ -210,11 +201,11 @@ class OurVisitor:
else: else:
function.imported = 'imports' function.imported = 'imports'
if node.returns is None: # Note: `-> None` would be a ast.Constant if node.returns is not None: # Note: `-> None` would be a ast.Constant
_raise_static_error(node, 'Must give a return type') function.returns_type3 = self.visit_type(module, node.returns)
return_type = self.visit_type(module, node.returns) else:
function.signature.args.append(return_type) # FIXME: Mostly works already, needs to fix Function.returns_type3 and have it updated
function.returns_type3 = return_type raise NotImplementedError('Function without an explicit return type')
_not_implemented(not node.type_comment, 'FunctionDef.type_comment') _not_implemented(not node.type_comment, 'FunctionDef.type_comment')
@ -526,7 +517,7 @@ class OurVisitor:
func = module.functions[node.func.id] func = module.functions[node.func.id]
exp_arg_count = len(func.signature.args) - 1 exp_arg_count = len(func.posonlyargs) if isinstance(func, Function) else len(func.signature) - 1
if exp_arg_count != len(node.args): if exp_arg_count != len(node.args):
_raise_static_error(node, f'Function {node.func.id} requires {exp_arg_count} arguments but {len(node.args)} are given') _raise_static_error(node, f'Function {node.func.id} requires {exp_arg_count} arguments but {len(node.args)} are given')

View File

@ -186,7 +186,7 @@ class TupleMatchConstraint(ConstraintBase):
return [ return [
SameTypeConstraint(arg, oth_arg) SameTypeConstraint(arg, oth_arg)
for arg, oth_arg in zip(self.args, tp_args, strict=True) for arg, oth_arg in zip(self.args, tp_args)
] ]
raise NotImplementedError(exp_type) raise NotImplementedError(exp_type)
@ -361,11 +361,11 @@ class LiteralFitsConstraint(ConstraintBase):
res.extend( res.extend(
LiteralFitsConstraint(x, y) LiteralFitsConstraint(x, y)
for x, y in zip(tp_args, self.literal.value, strict=True) for x, y in zip(tp_args, self.literal.value)
) )
res.extend( res.extend(
SameTypeConstraint(x, y.type3) SameTypeConstraint(x, y.type3)
for x, y in zip(tp_args, self.literal.value, strict=True) for x, y in zip(tp_args, self.literal.value)
) )
return res return res
@ -409,11 +409,11 @@ class LiteralFitsConstraint(ConstraintBase):
res.extend( res.extend(
LiteralFitsConstraint(x, y) LiteralFitsConstraint(x, y)
for x, y in zip(st_args.values(), self.literal.value, strict=True) for x, y in zip(st_args.values(), self.literal.value)
) )
res.extend( res.extend(
SameTypeConstraint(x_t, y.type3, comment=f'{self.literal.struct_name}.{x_n}') SameTypeConstraint(x_t, y.type3, comment=f'{self.literal.struct_name}.{x_n}')
for (x_n, x_t, ), y in zip(st_args.items(), self.literal.value, strict=True) for (x_n, x_t, ), y in zip(st_args.items(), self.literal.value)
) )
return res return res

View File

@ -6,9 +6,8 @@ The constraints solver can then try to resolve all constraints.
from typing import Generator, List from typing import Generator, List
from .. import ourlang, prelude from .. import ourlang, prelude
from . import functions as functions
from . import placeholders as placeholders from . import placeholders as placeholders
from . import typeclasses as typeclasses from . import typeclasses as type3typeclasses
from . import types as type3types from . import types as type3types
from .constraints import ( from .constraints import (
CanBeSubscriptedConstraint, CanBeSubscriptedConstraint,
@ -56,51 +55,81 @@ def expression(ctx: Context, inp: ourlang.Expression) -> ConstraintGenerator:
raise NotImplementedError(expression, inp, inp.operator) raise NotImplementedError(expression, inp, inp.operator)
if isinstance(inp, ourlang.BinaryOp) or isinstance(inp, ourlang.FunctionCall): if isinstance(inp, ourlang.BinaryOp):
signature = inp.operator.signature if isinstance(inp, ourlang.BinaryOp) else inp.function.signature
arguments = [inp.left, inp.right] if isinstance(inp, ourlang.BinaryOp) else inp.arguments
func_name = f'({inp.operator.name})' if isinstance(inp, ourlang.BinaryOp) else inp.function.name
type_var_map = { type_var_map = {
x: placeholders.PlaceholderForType([]) x: placeholders.PlaceholderForType([])
for x in signature.args for x in inp.operator.signature
if isinstance(x, functions.TypeVariable) if isinstance(x, type3typeclasses.TypeVariable)
} }
for call_arg in arguments: yield from expression(ctx, inp.left)
yield from expression(ctx, call_arg) yield from expression(ctx, inp.right)
for type_var, constraint_list in signature.context.constraints.items(): for type_var in inp.operator.type3_class.args:
assert type_var in type_var_map # When can this happen? assert type_var in type_var_map # When can this happen?
for constraint in constraint_list:
if isinstance(constraint, functions.TypeVariableConstraint_TypeHasTypeClass):
yield MustImplementTypeClassConstraint( yield MustImplementTypeClassConstraint(
constraint.type_class3, inp.operator.type3_class,
type_var_map[type_var], type_var_map[type_var],
) )
continue
raise NotImplementedError(constraint) for sig_part, arg_expr in zip(inp.operator.signature, [inp.left, inp.right, inp]):
if isinstance(sig_part, type3typeclasses.TypeVariable):
for arg_no, (sig_part, arg_expr) in enumerate(zip(signature.args, arguments + [inp], strict=True)): yield SameTypeConstraint(type_var_map[sig_part], arg_expr.type3)
if arg_no == len(arguments):
comment = f'The type of a function call to {func_name} is the same as the type that the function returns'
else:
comment = f'The type of the value passed to argument {arg_no} of function {func_name} should match the type of that argument'
if isinstance(sig_part, functions.TypeVariable):
yield SameTypeConstraint(type_var_map[sig_part], arg_expr.type3, comment=comment)
continue continue
if isinstance(sig_part, type3types.Type3): if isinstance(sig_part, type3types.Type3):
yield SameTypeConstraint(sig_part, arg_expr.type3, comment=comment) yield SameTypeConstraint(sig_part, arg_expr.type3)
continue continue
raise NotImplementedError(sig_part) raise NotImplementedError(sig_part)
return return
if isinstance(inp, ourlang.FunctionCall):
if isinstance(inp.function, type3typeclasses.Type3ClassMethod):
# FIXME: Duplicate code with BinaryOp
type_var_map = {
x: placeholders.PlaceholderForType([])
for x in inp.function.signature
if isinstance(x, type3typeclasses.TypeVariable)
}
for call_arg in inp.arguments:
yield from expression(ctx, call_arg)
for type_var in inp.function.type3_class.args:
assert type_var in type_var_map # When can this happen?
yield MustImplementTypeClassConstraint(
inp.function.type3_class,
type_var_map[type_var],
)
for sig_part, arg_expr in zip(inp.function.signature, inp.arguments + [inp]):
if isinstance(sig_part, type3typeclasses.TypeVariable):
yield SameTypeConstraint(type_var_map[sig_part], arg_expr.type3)
continue
if isinstance(sig_part, type3types.Type3):
yield SameTypeConstraint(sig_part, arg_expr.type3)
continue
raise NotImplementedError(sig_part)
return
yield SameTypeConstraint(inp.function.returns_type3, inp.type3,
comment=f'The type of a function call to {inp.function.name} is the same as the type that the function returns')
assert len(inp.arguments) == len(inp.function.posonlyargs) # FIXME: Make this a Constraint
for fun_arg, call_arg in zip(inp.function.posonlyargs, inp.arguments):
yield from expression(ctx, call_arg)
yield SameTypeConstraint(fun_arg.type3, call_arg.type3,
comment=f'The type of the value passed to argument {fun_arg.name} of function {inp.function.name} should match the type of that argument')
return
if isinstance(inp, ourlang.TupleInstantiation): if isinstance(inp, ourlang.TupleInstantiation):
r_type = [] r_type = []
for arg in inp.elements: for arg in inp.elements:

View File

@ -1,67 +0,0 @@
from typing import TYPE_CHECKING, Any, Iterable, List, Union
if TYPE_CHECKING:
from .typeclasses import Type3Class
from .types import Type3
class TypeVariable:
"""
Types variable are used in function definition.
They are used in places where you don't know the exact type.
They are different from PlaceholderForType, as those are instanced
during type checking. These type variables are used solely in the
function's definition
"""
__slots__ = ('letter', )
letter: str
def __init__(self, letter: str) -> None:
assert len(letter) == 1, f'{letter} is not a valid type variable'
self.letter = letter
def __hash__(self) -> int:
return hash(self.letter)
def __eq__(self, other: Any) -> bool:
if not isinstance(other, TypeVariable):
raise NotImplementedError
return self.letter == other.letter
def __repr__(self) -> str:
return f'TypeVariable({repr(self.letter)})'
class TypeVariableConstraintBase:
__slots__ = ()
class TypeVariableConstraint_TypeHasTypeClass(TypeVariableConstraintBase):
__slots__ = ('type_class3', )
def __init__(self, type_class3: 'Type3Class') -> None:
self.type_class3 = type_class3
class TypeVariableContext:
__slots__ = ('constraints', )
constraints: dict[TypeVariable, list[TypeVariableConstraintBase]]
def __init__(self) -> None:
self.constraints = {}
def __copy__(self) -> 'TypeVariableContext':
result = TypeVariableContext()
result.constraints.update(self.constraints)
return result
class FunctionSignature:
__slots__ = ('context', 'args', )
context: TypeVariableContext
args: List[Union['Type3', TypeVariable]]
def __init__(self, context: TypeVariableContext, args: Iterable[Union['Type3', TypeVariable]]) -> None:
self.context = context.__copy__()
self.args = list(args)

View File

@ -1,11 +1,5 @@
from typing import Any, Dict, Iterable, List, Mapping, Optional, Union from typing import Any, Dict, Iterable, List, Mapping, Optional, Union
from .functions import (
FunctionSignature,
TypeVariable,
TypeVariableConstraint_TypeHasTypeClass,
TypeVariableContext,
)
from .types import Type3, TypeConstructor, TypeConstructor_Struct from .types import Type3, TypeConstructor, TypeConstructor_Struct
@ -75,18 +69,19 @@ class TypeReference:
Signature = List[Union[Type3, TypeVariable, list[TypeVariable]]] Signature = List[Union[Type3, TypeVariable, list[TypeVariable]]]
class Type3ClassMethod: class Type3ClassMethod:
__slots__ = ('name', 'signature', ) __slots__ = ('type3_class', 'name', 'signature', )
type3_class: 'Type3Class'
name: str name: str
signature: FunctionSignature signature: Signature
def __init__(self, name: str, signature: FunctionSignature) -> None:
def __init__(self, type3_class: 'Type3Class', name: str, signature: Signature) -> None:
self.type3_class = type3_class
self.name = name self.name = name
self.signature = signature self.signature = list(signature)
def __repr__(self) -> str: def __repr__(self) -> str:
return f'Type3ClassMethod({repr(self.name)}, {repr(self.signature)})' return f'Type3ClassMethod({repr(self.type3_class)}, {repr(self.name)}, {repr(self.signature)})'
class Type3Class: class Type3Class:
__slots__ = ('name', 'args', 'methods', 'operators', 'inherited_classes', ) __slots__ = ('name', 'args', 'methods', 'operators', 'inherited_classes', )
@ -107,26 +102,12 @@ class Type3Class:
) -> None: ) -> None:
self.name = name self.name = name
self.args = list(args) self.args = list(args)
context = TypeVariableContext()
for arg in args:
context.constraints[arg] = [
TypeVariableConstraint_TypeHasTypeClass(self)
]
# FIXME: Multi parameter class types
# To fix this, realise that an instantiation of a multi paramater type class
# means that the instantiation depends on the combination of type classes
# and so we can't store the type classes on the types anymore
# This also means constraints should store a tuple of types as its key
assert len(context.constraints) <= 1
self.methods = { self.methods = {
k: Type3ClassMethod(k, FunctionSignature(context, v)) k: Type3ClassMethod(self, k, v)
for k, v in methods.items() for k, v in methods.items()
} }
self.operators = { self.operators = {
k: Type3ClassMethod(k, FunctionSignature(context, v)) k: Type3ClassMethod(self, k, v)
for k, v in operators.items() for k, v in operators.items()
} }
self.inherited_classes = inherited_classes or [] self.inherited_classes = inherited_classes or []

View File

@ -64,7 +64,7 @@ class Suite:
write_header(sys.stderr, 'Memory (pre alloc)') write_header(sys.stderr, 'Memory (pre alloc)')
runner.interpreter_dump_memory(sys.stderr) runner.interpreter_dump_memory(sys.stderr)
for arg, arg_typ in zip(args, func_args, strict=True): for arg, arg_typ in zip(args, func_args):
assert not isinstance(arg_typ, type3placeholders.PlaceholderForType), \ assert not isinstance(arg_typ, type3placeholders.PlaceholderForType), \
'Cannot call polymorphic function from outside' 'Cannot call polymorphic function from outside'
@ -221,7 +221,7 @@ def _allocate_memory_stored_value(
assert len(val) == len(tp_args) assert len(val) == len(tp_args)
offset = adr offset = adr
for val_el_val, val_el_typ in zip(val, tp_args, strict=True): for val_el_val, val_el_typ in zip(val, tp_args):
assert not isinstance(val_el_typ, type3placeholders.PlaceholderForType) assert not isinstance(val_el_typ, type3placeholders.PlaceholderForType)
offset += _write_memory_stored_value(runner, offset, val_el_typ, val_el_val) offset += _write_memory_stored_value(runner, offset, val_el_typ, val_el_val)
@ -424,7 +424,7 @@ def _load_tuple_from_address(runner: runners.RunnerBase, typ_args: tuple[type3ty
return tuple( return tuple(
_unpack(runner, arg_typ, arg_bytes) _unpack(runner, arg_typ, arg_bytes)
for arg_typ, arg_bytes in zip(typ_args, _split_read_bytes(read_bytes, arg_sizes), strict=True) for arg_typ, arg_bytes in zip(typ_args, _split_read_bytes(read_bytes, arg_sizes))
) )
def _load_struct_from_address(runner: runners.RunnerBase, st_args: dict[str, type3types.Type3], adr: int) -> Any: def _load_struct_from_address(runner: runners.RunnerBase, st_args: dict[str, type3types.Type3], adr: int) -> Any:
@ -444,5 +444,5 @@ def _load_struct_from_address(runner: runners.RunnerBase, st_args: dict[str, typ
return { return {
arg_name: _unpack(runner, arg_typ, arg_bytes) arg_name: _unpack(runner, arg_typ, arg_bytes)
for arg_name, arg_typ, arg_bytes in zip(name_list, typ_list, _split_read_bytes(read_bytes, arg_sizes), strict=True) for arg_name, arg_typ, arg_bytes in zip(name_list, typ_list, _split_read_bytes(read_bytes, arg_sizes))
} }

View File

@ -282,7 +282,7 @@ if TYPE_NAME.startswith('tuple_') or TYPE_NAME.startswith('static_array_'):
elif TYPE_NAME.startswith('struct_'): elif TYPE_NAME.startswith('struct_'):
expect_type_error( expect_type_error(
TYPE + ' must be (u32, ) instead', TYPE + ' must be (u32, ) instead',
'The type of the value passed to argument 0 of function helper should match the type of that argument', 'The type of the value passed to argument x of function helper should match the type of that argument',
) )
else: else:
expect_type_error( expect_type_error(
@ -333,11 +333,11 @@ def testEntry() -> i32:
if TYPE_NAME.startswith('tuple_') or TYPE_NAME.startswith('static_array_') or TYPE_NAME.startswith('struct_'): if TYPE_NAME.startswith('tuple_') or TYPE_NAME.startswith('static_array_') or TYPE_NAME.startswith('struct_'):
expect_type_error( expect_type_error(
TYPE + ' must be (u32, ) instead', TYPE + ' must be (u32, ) instead',
'The type of the value passed to argument 0 of function helper should match the type of that argument', 'The type of the value passed to argument x of function helper should match the type of that argument',
) )
else: else:
expect_type_error( expect_type_error(
TYPE_NAME + ' must be (u32, ) instead', TYPE_NAME + ' must be (u32, ) instead',
'The type of the value passed to argument 0 of function helper should match the type of that argument', 'The type of the value passed to argument x of function helper should match the type of that argument',
) )
``` ```

View File

@ -1,8 +1,27 @@
import sys
import pytest import pytest
from ..helpers import Suite from ..helpers import Suite, write_header
from ..runners import RunnerWasmtime
def setup_interpreter(phash_code: str) -> RunnerWasmtime:
runner = RunnerWasmtime(phash_code)
runner.parse()
runner.compile_ast()
runner.compile_wat()
runner.interpreter_setup()
runner.interpreter_load()
write_header(sys.stderr, 'Phasm')
runner.dump_phasm_code(sys.stderr)
write_header(sys.stderr, 'Assembly')
runner.dump_wasm_wat(sys.stderr)
return runner
@pytest.mark.integration_test @pytest.mark.integration_test
def test_foldl_1(): def test_foldl_1():
code_py = """ code_py = """

View File

@ -1,20 +0,0 @@
import pytest
from phasm.exceptions import StaticError
from ..helpers import Suite
@pytest.mark.integration_test
def test_must_give_type_annotations():
code_py = """
def is_equal(lft, rgt) -> bool:
return lft == rgt
def testEntry() -> bool:
return is_equal(5, 15)
"""
suite = Suite(code_py)
with pytest.raises(StaticError, match='Must give a argument type'):
suite.run_code()