Skip to content

Commit

Permalink
chore: remove unused Vector type (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jmgr authored Nov 18, 2024
1 parent b7959ee commit 7e0d6d1
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 80 deletions.
2 changes: 0 additions & 2 deletions nada_dsl/nada_types/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ def __init__(self, name):
"SecretUnsignedInteger",
"SecretBoolean",
"Array",
"Vector",
"Tuple",
"NTuple",
"Object",
Expand All @@ -54,7 +53,6 @@ def __init__(self, name):
Type["SecretBoolean"],
Type["Array"],
Type["ArrayType"],
Type["Vector"],
Type["Tuple"],
Type["NTuple"],
Type["Object"],
Expand Down
71 changes: 1 addition & 70 deletions nada_dsl/nada_types/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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": {
Expand Down Expand Up @@ -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.
Expand Down
13 changes: 5 additions & 8 deletions tests/compiler_frontend_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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, **{})
Expand All @@ -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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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]):
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 7e0d6d1

Please sign in to comment.