From 117b68e6b74221ec85822f721c23c8245e2294fe Mon Sep 17 00:00:00 2001 From: Mark Koch <48097969+mark-koch@users.noreply.github.com> Date: Fri, 18 Oct 2024 12:04:04 +0100 Subject: [PATCH] fix: Enable len for linear arrays (#576) Closes #570 --- guppylang/prelude/builtins.py | 2 +- tests/error/array_errors/linear_len.err | 7 ------- tests/error/array_errors/linear_len.py | 17 ----------------- tests/integration/test_array.py | 18 ++++++++++++++++++ 4 files changed, 19 insertions(+), 25 deletions(-) delete mode 100644 tests/error/array_errors/linear_len.err delete mode 100644 tests/error/array_errors/linear_len.py diff --git a/guppylang/prelude/builtins.py b/guppylang/prelude/builtins.py index a1e12c5f..baa079df 100644 --- a/guppylang/prelude/builtins.py +++ b/guppylang/prelude/builtins.py @@ -553,7 +553,7 @@ def __getitem__(self: array[L, n], idx: int) -> L: ... def __setitem__(self: array[L, n], idx: int, value: L @ owned) -> None: ... @guppy.custom(checker=ArrayLenChecker()) - def __len__(self: array[T, n]) -> int: ... + def __len__(self: array[L, n]) -> int: ... @guppy.custom(NewArrayCompiler(), NewArrayChecker(), higher_order_value=False) def __new__(): ... diff --git a/tests/error/array_errors/linear_len.err b/tests/error/array_errors/linear_len.err deleted file mode 100644 index 0a429cf9..00000000 --- a/tests/error/array_errors/linear_len.err +++ /dev/null @@ -1,7 +0,0 @@ -Guppy compilation failed. Error in file $FILE:14 - -12: @guppy(module) -13: def main(qs: array[qubit, 42]) -> int: -14: return len(qs) - ^^^^^^^ -GuppyTypeError: Cannot instantiate non-linear type variable `T` in type `forall T, n: nat. array[T, n] -> int` with linear type `qubit` diff --git a/tests/error/array_errors/linear_len.py b/tests/error/array_errors/linear_len.py deleted file mode 100644 index 5f7cc069..00000000 --- a/tests/error/array_errors/linear_len.py +++ /dev/null @@ -1,17 +0,0 @@ -import guppylang.prelude.quantum as quantum -from guppylang.decorator import guppy -from guppylang.module import GuppyModule -from guppylang.prelude.builtins import array -from guppylang.prelude.quantum import qubit - - -module = GuppyModule("test") -module.load_all(quantum) - - -@guppy(module) -def main(qs: array[qubit, 42]) -> int: - return len(qs) - - -module.compile() \ No newline at end of file diff --git a/tests/integration/test_array.py b/tests/integration/test_array.py index a4b557ac..cedbb1c8 100644 --- a/tests/integration/test_array.py +++ b/tests/integration/test_array.py @@ -27,6 +27,24 @@ def main(xs: array[float, 42]) -> int: assert val.val.v == 42 +def test_len_linear(validate): + module = GuppyModule("test") + module.load(qubit) + + @guppy(module) + def main(qs: array[qubit, 42]) -> int: + return len(qs) + + package = module.compile() + validate(package) + + hg = package.module + [val] = [data.op for node, data in hg.nodes() if isinstance(data.op, ops.Const)] + assert isinstance(val, ops.Const) + assert isinstance(val.val, IntVal) + assert val.val.v == 42 + + def test_index(validate): @compile_guppy def main(xs: array[int, 5], i: int) -> int: