|
| 1 | +"""Borrow array types and operations.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from dataclasses import dataclass |
| 6 | +from typing import cast |
| 7 | + |
| 8 | +import hugr.model as model |
| 9 | +from hugr import tys, val |
| 10 | +from hugr.std import _load_extension |
| 11 | +from hugr.utils import comma_sep_str |
| 12 | + |
| 13 | +EXTENSION = _load_extension("collections.borrow_arr") |
| 14 | + |
| 15 | + |
| 16 | +@dataclass(eq=False) |
| 17 | +class BorrowArray(tys.ExtType): |
| 18 | + """Fixed `size` borrow array of `ty` elements.""" |
| 19 | + |
| 20 | + def __init__(self, ty: tys.Type, size: int | tys.TypeArg) -> None: |
| 21 | + if isinstance(size, int): |
| 22 | + size = tys.BoundedNatArg(size) |
| 23 | + |
| 24 | + err_msg = ( |
| 25 | + f"Borrow array size must be a bounded natural or a nat variable, not {size}" |
| 26 | + ) |
| 27 | + match size: |
| 28 | + case tys.BoundedNatArg(_n): |
| 29 | + pass |
| 30 | + case tys.VariableArg(_idx, param): |
| 31 | + if not isinstance(param, tys.BoundedNatParam): |
| 32 | + raise ValueError(err_msg) # noqa: TRY004 |
| 33 | + case _: |
| 34 | + raise ValueError(err_msg) |
| 35 | + |
| 36 | + ty_arg = tys.TypeTypeArg(ty) |
| 37 | + |
| 38 | + self.type_def = EXTENSION.types["borrow_array"] |
| 39 | + self.args = [size, ty_arg] |
| 40 | + |
| 41 | + @property |
| 42 | + def ty(self) -> tys.Type: |
| 43 | + assert isinstance( |
| 44 | + self.args[1], tys.TypeTypeArg |
| 45 | + ), "Borrow array elements must have a valid type" |
| 46 | + return self.args[1].ty |
| 47 | + |
| 48 | + @property |
| 49 | + def size(self) -> int | None: |
| 50 | + """If the borrow array has a concrete size, return it. |
| 51 | +
|
| 52 | + Otherwise, return None. |
| 53 | + """ |
| 54 | + if isinstance(self.args[0], tys.BoundedNatArg): |
| 55 | + return self.args[0].n |
| 56 | + return None |
| 57 | + |
| 58 | + def type_bound(self) -> tys.TypeBound: |
| 59 | + return tys.TypeBound.Linear |
| 60 | + |
| 61 | + |
| 62 | +# Note that only borrow array values with no elements borrowed should be emitted. |
| 63 | +@dataclass |
| 64 | +class BorrowArrayVal(val.ExtensionValue): |
| 65 | + """Constant value for a statically sized borrow array of elements.""" |
| 66 | + |
| 67 | + v: list[val.Value] |
| 68 | + ty: BorrowArray |
| 69 | + |
| 70 | + def __init__(self, v: list[val.Value], elem_ty: tys.Type) -> None: |
| 71 | + self.v = v |
| 72 | + self.ty = BorrowArray(elem_ty, len(v)) |
| 73 | + |
| 74 | + def to_value(self) -> val.Extension: |
| 75 | + name = "BorrowArrayValue" |
| 76 | + # The value list must be serialized at this point, otherwise the |
| 77 | + # `Extension` value would not be serializable. |
| 78 | + vs = [v._to_serial_root() for v in self.v] |
| 79 | + element_ty = self.ty.ty._to_serial_root() |
| 80 | + serial_val = {"values": vs, "typ": element_ty} |
| 81 | + return val.Extension(name, typ=self.ty, val=serial_val) |
| 82 | + |
| 83 | + def __str__(self) -> str: |
| 84 | + return f"borrow_array({comma_sep_str(self.v)})" |
| 85 | + |
| 86 | + def to_model(self) -> model.Term: |
| 87 | + return model.Apply( |
| 88 | + "collections.borrow_array.const", |
| 89 | + [ |
| 90 | + model.Literal(len(self.v)), |
| 91 | + cast(model.Term, self.ty.ty.to_model()), |
| 92 | + model.List([value.to_model() for value in self.v]), |
| 93 | + ], |
| 94 | + ) |
0 commit comments