Skip to content

Commit

Permalink
fix: Fix lowering of list.__len__ (#577)
Browse files Browse the repository at this point in the history
Now that CQCL/hugr#1543 is fixed, we can use
the correct op for list length lowering
  • Loading branch information
mark-koch authored Oct 17, 2024
1 parent 7a9d5da commit c698b43
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
28 changes: 23 additions & 5 deletions guppylang/prelude/_internal/compiler/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
from guppylang.definition.custom import CustomCallCompiler
from guppylang.definition.value import CallReturnWires
from guppylang.error import InternalGuppyError
from guppylang.prelude._internal.compiler.arithmetic import convert_itousize
from guppylang.prelude._internal.compiler.arithmetic import (
convert_ifromusize,
convert_itousize,
)
from guppylang.prelude._internal.compiler.prelude import (
build_error,
build_panic,
Expand Down Expand Up @@ -91,10 +94,7 @@ def list_length(elem_type: ht.Type) -> ops.ExtOp:
"""Returns a list `length` operation."""
list_type = hugr.std.collections.list_type(elem_type)
return _instantiate_list_op(
"length",
elem_type,
[list_type],
[list_type, ht.Either([elem_type], [ht.Unit])],
"length", elem_type, [list_type], [list_type, ht.USize()]
)


Expand Down Expand Up @@ -301,6 +301,24 @@ def compile(self, args: list[Wire]) -> list[Wire]:
raise InternalGuppyError("Call compile_with_inouts instead")


class ListLengthCompiler(CustomCallCompiler):
"""Compiler for the `list.__len__` function."""

def compile_with_inouts(self, args: list[Wire]) -> CallReturnWires:
[list_wire] = args
[elem_ty_arg] = self.type_args
assert isinstance(elem_ty_arg, TypeArg)
elem_ty = elem_ty_arg.ty.to_hugr()
if elem_ty_arg.ty.linear:
elem_ty = ht.Option(elem_ty)
list_wire, length = self.builder.add_op(list_length(elem_ty), list_wire)
length = self.builder.add_op(convert_ifromusize(), length)
return CallReturnWires(regular_returns=[length], inout_returns=[list_wire])

def compile(self, args: list[Wire]) -> list[Wire]:
raise InternalGuppyError("Call compile_with_inouts instead")


def list_new(
builder: DfBase[ops.DfParentOp], elem_type: ht.Type, args: list[Wire]
) -> Wire:
Expand Down
4 changes: 2 additions & 2 deletions guppylang/prelude/builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
)
from guppylang.prelude._internal.compiler.list import (
ListGetitemCompiler,
ListLengthCompiler,
ListPopCompiler,
ListPushCompiler,
ListSetitemCompiler,
Expand Down Expand Up @@ -515,8 +516,7 @@ def __getitem__(self: list[L], idx: int) -> L: ...
@guppy.custom(ListSetitemCompiler())
def __setitem__(self: list[L], idx: int, value: L @ owned) -> None: ...

# TODO: https://github.com/CQCL/hugr/issues/1543
@guppy.hugr_op(unsupported_op("length"))
@guppy.custom(ListLengthCompiler())
def __len__(self: list[L]) -> int: ...

@guppy.custom(checker=UnsupportedChecker(), higher_order_value=False)
Expand Down

0 comments on commit c698b43

Please sign in to comment.