Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions hugr-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ workspace = true
[features]
declarative = ["serde_yaml"]
zstd = ["dep:zstd"]
default = []

[lib]
bench = false
Expand Down
11 changes: 9 additions & 2 deletions hugr-core/src/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -863,15 +863,22 @@ impl<'a> Context<'a> {
TypeArg::Type { ty } => self.export_type(ty),
TypeArg::BoundedNat { n } => self.make_term(model::Literal::Nat(*n).into()),
TypeArg::String { arg } => self.make_term(model::Literal::Str(arg.into()).into()),
TypeArg::Sequence { elems } => {
// For now we assume that the sequence is meant to be a list.
TypeArg::List { elems } => {
let parts = self.bump.alloc_slice_fill_iter(
elems
.iter()
.map(|elem| table::SeqPart::Item(self.export_type_arg(elem))),
);
self.make_term(table::Term::List(parts))
}
TypeArg::Tuple { elems } => {
let parts = self.bump.alloc_slice_fill_iter(
elems
.iter()
.map(|elem| table::SeqPart::Item(self.export_type_arg(elem))),
);
self.make_term(table::Term::Tuple(parts))
}
TypeArg::Variable { v } => self.export_type_arg_var(v),
}
}
Expand Down
20 changes: 10 additions & 10 deletions hugr-core/src/extension/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ pub fn bool_t() -> Type {
/// Name of the prelude panic operation.
///
/// This operation can have any input and any output wires; it is instantiated
/// with two [`TypeArg::Sequence`]s representing these. The first input to the
/// with two [`TypeArg::List`]s representing these. The first input to the
/// operation is always an error type; the remaining inputs correspond to the
/// first sequence of types in its instantiation; the outputs correspond to the
/// second sequence of types in its instantiation. Note that the inputs and
Expand All @@ -189,7 +189,7 @@ pub const PANIC_OP_ID: OpName = OpName::new_inline("panic");
/// Name of the prelude exit operation.
///
/// This operation can have any input and any output wires; it is instantiated
/// with two [`TypeArg::Sequence`]s representing these. The first input to the
/// with two [`TypeArg::List`]s representing these. The first input to the
/// operation is always an error type; the remaining inputs correspond to the
/// first sequence of types in its instantiation; the outputs correspond to the
/// second sequence of types in its instantiation. Note that the inputs and
Expand Down Expand Up @@ -678,7 +678,7 @@ impl MakeExtensionOp for MakeTuple {
if def != TupleOpDef::MakeTuple {
return Err(OpLoadError::NotMember(ext_op.unqualified_id().to_string()))?;
}
let [TypeArg::Sequence { elems }] = ext_op.args() else {
let [TypeArg::List { elems }] = ext_op.args() else {
return Err(SignatureError::InvalidTypeArgs)?;
};
let tys: Result<Vec<Type>, _> = elems
Expand All @@ -692,7 +692,7 @@ impl MakeExtensionOp for MakeTuple {
}

fn type_args(&self) -> Vec<TypeArg> {
vec![TypeArg::Sequence {
vec![TypeArg::List {
elems: self
.0
.iter()
Expand Down Expand Up @@ -739,7 +739,7 @@ impl MakeExtensionOp for UnpackTuple {
if def != TupleOpDef::UnpackTuple {
return Err(OpLoadError::NotMember(ext_op.unqualified_id().to_string()))?;
}
let [TypeArg::Sequence { elems }] = ext_op.args() else {
let [TypeArg::List { elems }] = ext_op.args() else {
return Err(SignatureError::InvalidTypeArgs)?;
};
let tys: Result<Vec<Type>, _> = elems
Expand All @@ -753,7 +753,7 @@ impl MakeExtensionOp for UnpackTuple {
}

fn type_args(&self) -> Vec<TypeArg> {
vec![TypeArg::Sequence {
vec![TypeArg::List {
Copy link
Contributor

Choose a reason for hiding this comment

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

I find it quite surprising that panic, exit, (un)packTuple take TypeArg::List instead of TypeArg::Tuple. I guess it's specifically TypeArg::List(TypeTypeArg) so that all the args are types

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, that's how I understood the intention here.

elems: self
.0
.iter()
Expand Down Expand Up @@ -969,7 +969,7 @@ impl MakeExtensionOp for Barrier {
{
let _def = BarrierDef::from_def(ext_op.def())?;

let [TypeArg::Sequence { elems }] = ext_op.args() else {
let [TypeArg::List { elems }] = ext_op.args() else {
return Err(SignatureError::InvalidTypeArgs)?;
};
let tys: Result<Vec<Type>, _> = elems
Expand All @@ -985,7 +985,7 @@ impl MakeExtensionOp for Barrier {
}

fn type_args(&self) -> Vec<TypeArg> {
vec![TypeArg::Sequence {
vec![TypeArg::List {
elems: self
.type_row
.iter()
Expand Down Expand Up @@ -1132,7 +1132,7 @@ mod test {

let err = b.add_load_value(error_val);

const TYPE_ARG_NONE: TypeArg = TypeArg::Sequence { elems: vec![] };
const TYPE_ARG_NONE: TypeArg = TypeArg::List { elems: vec![] };
let op = PRELUDE
.instantiate_extension_op(&EXIT_OP_ID, [TYPE_ARG_NONE, TYPE_ARG_NONE])
.unwrap();
Expand All @@ -1147,7 +1147,7 @@ mod test {
fn test_panic_with_io() {
let error_val = ConstError::new(42, "PANIC");
let type_arg_q: TypeArg = TypeArg::Type { ty: qb_t() };
let type_arg_2q: TypeArg = TypeArg::Sequence {
let type_arg_2q: TypeArg = TypeArg::List {
elems: vec![type_arg_q.clone(), type_arg_q],
};
let panic_op = PRELUDE
Expand Down
9 changes: 7 additions & 2 deletions hugr-core/src/extension/resolution/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,13 @@ pub(super) fn collect_typearg_exts(
) {
match arg {
TypeArg::Type { ty } => collect_type_exts(ty, used_extensions, missing_extensions),
TypeArg::Sequence { elems } => {
for elem in elems {
TypeArg::List { elems } => {
for elem in elems.iter() {
collect_typearg_exts(elem, used_extensions, missing_extensions);
}
}
TypeArg::Tuple { elems } => {
for elem in elems.iter() {
collect_typearg_exts(elem, used_extensions, missing_extensions);
}
}
Expand Down
7 changes: 6 additions & 1 deletion hugr-core/src/extension/resolution/types_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,12 @@ pub(super) fn resolve_typearg_exts(
) -> Result<(), ExtensionResolutionError> {
match arg {
TypeArg::Type { ty } => resolve_type_exts(node, ty, extensions, used_extensions)?,
TypeArg::Sequence { elems } => {
TypeArg::List { elems } => {
for elem in elems.iter_mut() {
resolve_typearg_exts(node, elem, extensions, used_extensions)?;
}
}
TypeArg::Tuple { elems } => {
for elem in elems.iter_mut() {
resolve_typearg_exts(node, elem, extensions, used_extensions)?;
}
Expand Down
6 changes: 3 additions & 3 deletions hugr-core/src/hugr/validate/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -548,8 +548,8 @@ fn instantiate_row_variables() -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}

fn seq1ty(t: TypeRV) -> TypeArg {
TypeArg::Sequence {
fn list1ty(t: TypeRV) -> TypeArg {
TypeArg::List {
elems: vec![t.into()],
}
}
Expand Down Expand Up @@ -578,7 +578,7 @@ fn row_variables() -> Result<(), Box<dyn std::error::Error>> {
};
let par = e.instantiate_extension_op(
"parallel",
[tv.clone(), usize_t().into(), tv.clone(), usize_t().into()].map(seq1ty),
[tv.clone(), usize_t().into(), tv.clone(), usize_t().into()].map(list1ty),
)?;
let par_func = fb.add_dataflow_op(par, [func_arg, id_usz])?;
fb.finish_hugr_with_outputs(par_func.outputs())?;
Expand Down
13 changes: 8 additions & 5 deletions hugr-core/src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1173,14 +1173,17 @@ impl<'a> Context<'a> {
.map(|item| self.import_type_arg(*item))
.collect::<Result<_, _>>()?;

Ok(TypeArg::Sequence { elems })
Ok(TypeArg::List { elems })
}

table::Term::Tuple { .. } => {
// NOTE: While `TypeArg`s can represent tuples as
// `TypeArg::Sequence`s, this conflates lists and tuples. To
// avoid ambiguity we therefore report an error here for now.
Err(error_unsupported!("tuples as `TypeArg`"))
let elems = self
.import_closed_list(term_id)?
.iter()
.map(|item| self.import_type_arg(*item))
.collect::<Result<_, _>>()?;

Ok(TypeArg::Tuple { elems })
}

table::Term::Literal(model::Literal::Str(value)) => Ok(TypeArg::String {
Expand Down
2 changes: 1 addition & 1 deletion hugr-core/src/ops/controlflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ mod test {
outputs: vec![usize_t(), tv1].into(),
};
let cond2 = cond.substitute(&Substitution::new(&[
TypeArg::Sequence {
TypeArg::List {
elems: vec![usize_t().into(); 3],
},
qb_t().into(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ impl<AK: ArrayKind> MakeExtensionOp for GenericArrayScan<AK> {
TypeArg::BoundedNat { n: self.size },
self.src_ty.clone().into(),
self.tgt_ty.clone().into(),
TypeArg::Sequence {
TypeArg::List {
elems: self.acc_tys.clone().into_iter().map_into().collect(),
},
]
Expand Down Expand Up @@ -218,7 +218,7 @@ impl<AK: ArrayKind> HasConcrete for GenericArrayScanDef<AK> {
TypeArg::BoundedNat { n },
TypeArg::Type { ty: src_ty },
TypeArg::Type { ty: tgt_ty },
TypeArg::Sequence { elems: acc_tys },
TypeArg::List { elems: acc_tys },
] => {
let acc_tys: Result<_, OpLoadError> = acc_tys
.iter()
Expand Down
10 changes: 5 additions & 5 deletions hugr-core/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,7 @@ impl<'a> Substitution<'a> {
.expect("Undeclared type variable - call validate() ?");
debug_assert!(check_type_arg(arg, &TypeParam::new_list(bound)).is_ok());
match arg {
TypeArg::Sequence { elems } => elems
TypeArg::List { elems } => elems
.iter()
.map(|ta| {
match ta {
Expand Down Expand Up @@ -1022,7 +1022,7 @@ pub(crate) mod test {

let coln = e.get_type(&COLN).unwrap();
let c_of_cpy = coln
.instantiate([TypeArg::Sequence {
.instantiate([TypeArg::List {
elems: vec![Type::from(cpy.clone()).into()],
}])
.unwrap();
Expand All @@ -1037,7 +1037,7 @@ pub(crate) mod test {
);

let mut t = Type::new_extension(
coln.instantiate([TypeArg::Sequence {
coln.instantiate([TypeArg::List {
elems: vec![mk_opt(Type::from(cpy.clone())).into()],
}])
.unwrap(),
Expand All @@ -1056,7 +1056,7 @@ pub(crate) mod test {
(ct == &c_of_cpy).then_some(usize_t())
});
let mut t = Type::new_extension(
coln.instantiate([TypeArg::Sequence {
coln.instantiate([TypeArg::List {
elems: vec![Type::from(c_of_cpy.clone()).into(); 2],
}])
.unwrap(),
Expand All @@ -1065,7 +1065,7 @@ pub(crate) mod test {
assert_eq!(
t,
Type::new_extension(
coln.instantiate([TypeArg::Sequence {
coln.instantiate([TypeArg::List {
elems: vec![usize_t().into(); 2]
}])
.unwrap()
Expand Down
10 changes: 4 additions & 6 deletions hugr-core/src/types/poly_func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,14 +436,12 @@ pub(crate) mod test {
}
pf.instantiate(&[TypeArg::Type { ty: usize_t() }])
.unwrap_err();
pf.instantiate(&[TypeArg::Sequence {
elems: vec![usize_t().into(), TypeArg::Sequence { elems: seq2() }],
pf.instantiate(&[TypeArg::List {
elems: vec![usize_t().into(), TypeArg::List { elems: seq2() }],
}])
.unwrap_err();

let t2 = pf
.instantiate(&[TypeArg::Sequence { elems: seq2() }])
.unwrap();
let t2 = pf.instantiate(&[TypeArg::List { elems: seq2() }]).unwrap();
assert_eq!(
t2,
Signature::new(
Expand Down Expand Up @@ -471,7 +469,7 @@ pub(crate) mod test {

let inner3 = Type::new_function(Signature::new_endo(vec![usize_t(), bool_t(), usize_t()]));
let t3 = pf
.instantiate(&[TypeArg::Sequence {
.instantiate(&[TypeArg::List {
elems: vec![usize_t().into(), bool_t().into(), usize_t().into()],
}])
.unwrap();
Expand Down
Loading
Loading