From 7e0d6d1fea4d7a97e0f0ce75256e6d4be3cf515f Mon Sep 17 00:00:00 2001 From: Jonathan Mercier-Ganady Date: Mon, 18 Nov 2024 09:49:06 +0000 Subject: [PATCH] chore: remove unused Vector type (#58) --- nada_dsl/nada_types/__init__.py | 2 - nada_dsl/nada_types/collections.py | 71 +----------------------------- tests/compiler_frontend_test.py | 13 +++--- 3 files changed, 6 insertions(+), 80 deletions(-) diff --git a/nada_dsl/nada_types/__init__.py b/nada_dsl/nada_types/__init__.py index d6f541b..f65a47f 100644 --- a/nada_dsl/nada_types/__init__.py +++ b/nada_dsl/nada_types/__init__.py @@ -34,7 +34,6 @@ def __init__(self, name): "SecretUnsignedInteger", "SecretBoolean", "Array", - "Vector", "Tuple", "NTuple", "Object", @@ -54,7 +53,6 @@ def __init__(self, name): Type["SecretBoolean"], Type["Array"], Type["ArrayType"], - Type["Vector"], Type["Tuple"], Type["NTuple"], Type["Object"], diff --git a/nada_dsl/nada_types/collections.py b/nada_dsl/nada_types/collections.py index 7a58e94..dbba76a 100644 --- a/nada_dsl/nada_types/collections.py +++ b/nada_dsl/nada_types/collections.py @@ -3,7 +3,7 @@ import copy from dataclasses import dataclass import inspect -from typing import Any, Dict, Generic, List, Optional +from typing import Any, Dict, Generic, List import typing from typing import TypeVar @@ -60,9 +60,6 @@ def to_mir(self): size = {"size": self.size} if self.size else {} contained_type = self.retrieve_inner_type() return {"Array": {"inner_type": contained_type, **size}} - if isinstance(self, Vector): - contained_type = self.retrieve_inner_type() - return {"Vector": {"inner_type": contained_type}} if isinstance(self, (Tuple, TupleType)): return { "Tuple": { @@ -604,72 +601,6 @@ def init_as_template_type(cls, contained_type) -> "Array[T]": return Array(child=None, contained_type=contained_type, size=None) -@dataclass -class Vector(Generic[T], Collection): - """ - The Vector Nada Type. - - This is the representation of Vector types in Nada MIR. - A Vector is similar to the Array type but the difference is that - its size may change at runtime. - """ - - contained_type: T - size: int - - def __init__(self, child, size, contained_type=None): - self.contained_type = ( - contained_type - if (child is None or contained_type is not None) - else get_inner_type(child) - ) - self.size = size - self.child = child if contained_type else getattr(child, "child", None) - self.child.store_in_ast(self.to_mir()) - - def __iter__(self): - raise NotAllowedException( - "Cannot iterate/for loop over a nada Vector," - + " use functional style Vector functions instead (map, reduce, zip)" - ) - - def map(self: "Vector[T]", function: NadaFunction[T, R]) -> "Vector[R]": - """The map operation for Nada Vectors.""" - return Vector( - size=self.size, - contained_type=function.return_type, - child=(Map(child=self, fn=function, source_ref=SourceRef.back_frame())), - ) - - def zip(self: "Vector[T]", other: "Vector[R]") -> "Vector[Tuple[T, R]]": - """The Zip operation for Nada Vectors.""" - return Vector( - size=self.size, - contained_type=Tuple.generic_type( - self.contained_type, other.contained_type - ), - child=Zip(left=self, right=other, source_ref=SourceRef.back_frame()), - ) - - def reduce( - self: "Vector[T]", function: NadaFunction[T, R], initial: Optional[R] = None - ) -> R: - """The reduce operation for Nada Vectors.""" - return function.return_type( - Reduce( - child=self, - fn=function, - initial=initial, - source_ref=SourceRef.back_frame(), - ) - ) # type: ignore - - @classmethod - def init_as_template_type(cls, contained_type) -> "Vector[T]": - """Construct an empty Vector with the given child type.""" - return Vector(child=None, contained_type=contained_type, size=None) - - class TupleNew(Generic[T, U]): """MIR Tuple new operation. diff --git a/tests/compiler_frontend_test.py b/tests/compiler_frontend_test.py index 74e5984..6469cbc 100644 --- a/tests/compiler_frontend_test.py +++ b/tests/compiler_frontend_test.py @@ -30,7 +30,7 @@ traverse_and_process_operations, ) from nada_dsl.nada_types import AllTypes, Party -from nada_dsl.nada_types.collections import Array, Vector, Tuple, NTuple, Object, unzip +from nada_dsl.nada_types.collections import Array, Tuple, NTuple, Object, unzip from nada_dsl.nada_types.function import NadaFunctionArg, NadaFunctionCall, nada_fn @@ -121,9 +121,7 @@ def test_duplicated_inputs_checks(): nada_dsl_to_nada_mir([output]) -@pytest.mark.parametrize( - ("input_type", "type_name", "size"), [(Array, "Array", 10), (Vector, "Vector", 10)] -) +@pytest.mark.parametrize(("input_type", "type_name", "size"), [(Array, "Array", 10)]) def test_array_type_conversion(input_type, type_name, size): inner_input = create_input(SecretInteger, "name", "party", **{}) collection = create_collection(input_type, inner_input, size, **{}) @@ -135,7 +133,6 @@ def test_array_type_conversion(input_type, type_name, size): ("input_type", "input_name"), [ (Array, "Array"), - # TODO(Vector, "Vector") ], ) def test_zip(input_type, input_name): @@ -196,7 +193,7 @@ def test_unzip(input_type: type[Array]): @pytest.mark.parametrize( ("input_type", "input_name"), [ - (Array, "Array"), # TODO (Vector, "Vector") + (Array, "Array"), ], ) def test_map(input_type, input_name): @@ -224,7 +221,7 @@ def nada_function(a: SecretInteger) -> SecretInteger: @pytest.mark.parametrize( ("input_type"), [ - (Array), # TODO (Vector, "Vector") + (Array), ], ) def test_reduce(input_type: type[Array]): @@ -402,7 +399,7 @@ def find_function_in_ast(fn_name: str): @pytest.mark.parametrize( ("input_type", "input_name"), [ - (Array, "Array"), # TODO(Vector, "Vector") + (Array, "Array"), ], ) def test_nada_function_using_matrix(input_type, input_name):