Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 48 additions & 3 deletions hugr-core/src/std_extensions/collections/borrow_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,10 @@ pub enum BArrayUnsafeOpDef {
/// `return<size, elem_ty>: borrow_array<size, elem_ty>, index, elem_ty -> borrow_array<size, elem_ty>`
#[strum(serialize = "return")]
r#return,
/// `return<size, elem_ty>: borrow_array<size, elem_ty> -> ()`
/// `discard_empty_borrowed<size, elem_ty>: borrow_array<size, elem_ty> -> ()`
discard_empty_borrowed,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe discard_all_borrowed to avoid the confusion with discard_empty which does something different?

/// `new_empty_borrowed<size, elem_ty>: () -> borrow_array<size, elem_ty>`
new_empty_borrowed,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly, "new_all_borrowed" here?

}

impl BArrayUnsafeOpDef {
Expand Down Expand Up @@ -162,6 +164,9 @@ impl BArrayUnsafeOpDef {
Self::discard_empty_borrowed => {
PolyFuncTypeRV::new(params, FuncValueType::new(vec![array_ty], type_row![]))
}
Self::new_empty_borrowed => {
PolyFuncTypeRV::new(params, FuncValueType::new(type_row![], vec![array_ty]))
}
}
.into()
}
Expand Down Expand Up @@ -203,8 +208,9 @@ impl MakeOpDef for BArrayUnsafeOpDef {
"Put an element into a borrow array (panicking if there is an element already)"
}
Self::discard_empty_borrowed => {
"Discard an array where all elements have been borrowed"
"Discard a borrow array where all elements have been borrowed"
}
Self::new_empty_borrowed => "Create a new borrow array that contains no elements",
}
.into()
}
Expand Down Expand Up @@ -685,6 +691,25 @@ pub trait BArrayOpBuilder: GenericArrayOpBuilder {
self.add_dataflow_op(op.to_extension_op().unwrap(), vec![input])?;
Ok(())
}

/// Adds an operation to create a new empty borrowed array in the dataflow graph.
///
/// # Arguments
///
/// * `elem_ty` - The type of the elements in the array.
/// * `size` - The size of the array.
///
/// # Errors
///
/// Returns an error if building the operation fails.
fn add_new_empty_borrowed(&mut self, elem_ty: Type, size: u64) -> Result<Wire, BuildError> {
let op =
BArrayUnsafeOpDef::new_empty_borrowed.instantiate(&[size.into(), elem_ty.into()])?;
let [arr] = self
.add_dataflow_op(op.to_extension_op().unwrap(), vec![])?
.outputs_arr();
Ok(arr)
}
}

impl<D: Dataflow> BArrayOpBuilder for D {}
Expand All @@ -695,7 +720,7 @@ mod test {

use crate::{
builder::{DFGBuilder, Dataflow, DataflowHugr as _},
extension::prelude::{ConstUsize, qb_t},
extension::prelude::{ConstUsize, qb_t, usize_t},
ops::OpType,
std_extensions::collections::borrow_array::{
BArrayOpBuilder, BArrayUnsafeOp, BArrayUnsafeOpDef, borrow_array_type,
Expand Down Expand Up @@ -752,4 +777,24 @@ mod test {
builder.finish_hugr_with_outputs([el]).unwrap()
};
}

#[test]
fn test_new_empty_borrowed() {
let size = 5;
let elem_ty = usize_t();
let arr_ty = borrow_array_type(size, elem_ty.clone());
let _ = {
let mut builder =
DFGBuilder::new(Signature::new(vec![], vec![arr_ty.clone()])).unwrap();
let arr = builder
.add_new_empty_borrowed(elem_ty.clone(), size)
.unwrap();
let idx = builder.add_load_value(ConstUsize::new(3));
let val = builder.add_load_value(ConstUsize::new(202));
let arr_with_put = builder
.add_borrow_array_return(elem_ty, size, arr, idx, val)
.unwrap();
builder.finish_hugr_with_outputs([arr_with_put]).unwrap()
};
}
}
49 changes: 48 additions & 1 deletion hugr-py/src/hugr/std/_json_defs/collections/borrow_arr.json
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@
"discard_empty_borrowed": {
"extension": "collections.borrow_arr",
"name": "discard_empty_borrowed",
"description": "Discard an array where all elements have been borrowed",
"description": "Discard a borrow array where all elements have been borrowed",
"signature": {
"params": [
{
Expand Down Expand Up @@ -500,6 +500,53 @@
"signature": null,
"binary": true
},
"new_empty_borrowed": {
"extension": "collections.borrow_arr",
"name": "new_empty_borrowed",
"description": "Create a new borrow array that contains no elements",
"signature": {
"params": [
{
"tp": "BoundedNat",
"bound": null
},
{
"tp": "Type",
"b": "A"
}
],
"body": {
"input": [],
"output": [
{
"t": "Opaque",
"extension": "collections.borrow_arr",
"id": "borrow_array",
"args": [
{
"tya": "Variable",
"idx": 0,
"cached_decl": {
"tp": "BoundedNat",
"bound": null
}
},
{
"tya": "Type",
"ty": {
"t": "V",
"i": 1,
"b": "A"
}
}
],
"bound": "A"
}
]
}
},
"binary": false
},
"pop_left": {
"extension": "collections.borrow_arr",
"name": "pop_left",
Expand Down
49 changes: 48 additions & 1 deletion specification/std_extensions/collections/borrow_arr.json
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@
"discard_empty_borrowed": {
"extension": "collections.borrow_arr",
"name": "discard_empty_borrowed",
"description": "Discard an array where all elements have been borrowed",
"description": "Discard a borrow array where all elements have been borrowed",
"signature": {
"params": [
{
Expand Down Expand Up @@ -500,6 +500,53 @@
"signature": null,
"binary": true
},
"new_empty_borrowed": {
"extension": "collections.borrow_arr",
"name": "new_empty_borrowed",
"description": "Create a new borrow array that contains no elements",
"signature": {
"params": [
{
"tp": "BoundedNat",
"bound": null
},
{
"tp": "Type",
"b": "A"
}
],
"body": {
"input": [],
"output": [
{
"t": "Opaque",
"extension": "collections.borrow_arr",
"id": "borrow_array",
"args": [
{
"tya": "Variable",
"idx": 0,
"cached_decl": {
"tp": "BoundedNat",
"bound": null
}
},
{
"tya": "Type",
"ty": {
"t": "V",
"i": 1,
"b": "A"
}
}
],
"bound": "A"
}
]
}
},
"binary": false
},
"pop_left": {
"extension": "collections.borrow_arr",
"name": "pop_left",
Expand Down