Skip to content

Commit

Permalink
use ops.Some
Browse files Browse the repository at this point in the history
  • Loading branch information
ss2165 committed Dec 19, 2024
1 parent 3991c04 commit 48d0c1b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
6 changes: 3 additions & 3 deletions guppylang/compiler/stmt_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ def _assign_tuple(self, lhs: TupleUnpack, port: Wire) -> None:
starred_ports = (
ports[len(left) : -len(right)] if right else ports[len(left) :]
)
opt_ty = ht.Option(get_element_type(array_ty).to_hugr())
opts = [self.builder.add_op(ops.Tag(1, opt_ty), p) for p in starred_ports]
array = self.builder.add_op(array_new(opt_ty, len(opts)), *opts)
elt = get_element_type(array_ty).to_hugr()
opts = [self.builder.add_op(ops.Some(elt), p) for p in starred_ports]
array = self.builder.add_op(array_new(ht.Option(elt), len(opts)), *opts)
self._assign(starred, array)

@_assign.register
Expand Down
11 changes: 6 additions & 5 deletions guppylang/std/_internal/compiler/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,12 @@ def build_classical_array(self, elems: list[Wire]) -> Wire:

def build_linear_array(self, elems: list[Wire]) -> Wire:
"""Lowers a call to `array.__new__` for linear arrays."""
elem_opt_ty = ht.Option(self.elem_ty)
elem_opts = [
self.builder.add_op(ops.Tag(1, elem_opt_ty), elem) for elem in elems
self.builder.add_op(ops.Some(self.elem_ty), elem) for elem in elems
]
return self.builder.add_op(array_new(elem_opt_ty, len(elems)), *elem_opts)
return self.builder.add_op(
array_new(ht.Option(self.elem_ty), len(elems)), *elem_opts
)

def compile(self, args: list[Wire]) -> list[Wire]:
if self.elem_ty.type_bound() == ht.TypeBound.Any:
Expand Down Expand Up @@ -232,7 +233,7 @@ def build_classical_setitem(
# See https://github.com/CQCL/guppylang/issues/629
elem_opt_ty = ht.Option(self.elem_ty)
idx = self.builder.add_op(convert_itousize(), idx)
elem_opt = self.builder.add_op(ops.Tag(1, elem_opt_ty), elem)
elem_opt = self.builder.add_op(ops.Some(self.elem_ty), elem)
result = self.builder.add_op(
array_set(elem_opt_ty, self.length), array, idx, elem_opt
)
Expand All @@ -246,7 +247,7 @@ def build_linear_setitem(
"""Lowers a call to `array.__setitem__` for linear arrays."""
# Embed the element into an optional
elem_opt_ty = ht.Option(self.elem_ty)
elem = self.builder.add_op(ops.Tag(1, elem_opt_ty), elem)
elem = self.builder.add_op(ops.Some(self.elem_ty), elem)
idx = self.builder.add_op(convert_itousize(), idx)
result = self.builder.add_op(
array_set(elem_opt_ty, self.length), array, idx, elem
Expand Down
6 changes: 3 additions & 3 deletions guppylang/std/_internal/compiler/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ def build_linear_setitem(
"""Lowers a call to `array.__setitem__` for linear arrays."""
# Embed the element into an optional
elem_opt_ty = ht.Option(elem_ty)
elem = self.builder.add_op(ops.Tag(1, elem_opt_ty), elem)
elem = self.builder.add_op(ops.Some(elem_ty), elem)
idx = self.builder.add_op(convert_itousize(), idx)
list_wire, result = self.builder.add_op(
list_set(elem_opt_ty), list_wire, idx, elem
Expand Down Expand Up @@ -274,7 +274,7 @@ def build_linear_push(
"""Lowers a call to `list.push` for linear lists."""
# Wrap element into an optional
elem_opt_ty = ht.Option(elem_ty)
elem_opt = self.builder.add_op(ops.Tag(1, elem_opt_ty), elem)
elem_opt = self.builder.add_op(ops.Some(elem_ty), elem)
list_wire = self.builder.add_op(list_push(elem_opt_ty), list_wire, elem_opt)
return CallReturnWires(regular_returns=[], inout_returns=[list_wire])

Expand Down Expand Up @@ -337,6 +337,6 @@ def _list_new_linear(
lst = builder.load(ListVal([], elem_ty=elem_opt_ty))
push_op = list_push(elem_opt_ty)
for elem in args:
elem_opt = builder.add_op(ops.Tag(1, elem_opt_ty), elem)
elem_opt = builder.add_op(ops.Some(elem_type), elem)
lst = builder.add_op(push_op, lst, elem_opt)
return lst

0 comments on commit 48d0c1b

Please sign in to comment.