Skip to content

Commit c2f8b30

Browse files
authored
feat!: Rename 'Any' type bound to 'Linear' (#2421)
Closes #1356 The bound is still encoded as "A", so this doesn't break serialization. BREAKING CHANGE: Renamed the `Any` type bound to `Linear`
1 parent 68415f4 commit c2f8b30

File tree

34 files changed

+156
-150
lines changed

34 files changed

+156
-150
lines changed

hugr-core/src/builder/module.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ mod test {
220220
let mut module_builder = ModuleBuilder::new();
221221

222222
let qubit_state_type =
223-
module_builder.add_alias_declare("qubit_state", TypeBound::Any)?;
223+
module_builder.add_alias_declare("qubit_state", TypeBound::Linear)?;
224224

225225
let f_build = module_builder.define_function(
226226
"main",

hugr-core/src/extension/declarative/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ impl From<TypeDefBoundDeclaration> for TypeDefBound {
100100
bound: TypeBound::Copyable,
101101
},
102102
TypeDefBoundDeclaration::Any => Self::Explicit {
103-
bound: TypeBound::Any,
103+
bound: TypeBound::Linear,
104104
},
105105
}
106106
}

hugr-core/src/extension/op_def.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,7 @@ pub(super) mod test {
656656
const OP_NAME: OpName = OpName::new_inline("Reverse");
657657

658658
let ext = Extension::try_new_test_arc(EXT_ID, |ext, extension_ref| {
659-
const TP: TypeParam = TypeParam::RuntimeType(TypeBound::Any);
659+
const TP: TypeParam = TypeParam::RuntimeType(TypeBound::Linear);
660660
let list_of_var =
661661
Type::new_extension(list_def.instantiate(vec![TypeArg::new_var_use(0, TP)])?);
662662
let type_scheme = PolyFuncTypeRV::new(vec![TP], Signature::new_endo(vec![list_of_var]));
@@ -702,13 +702,13 @@ pub(super) mod test {
702702
&self,
703703
arg_values: &[TypeArg],
704704
) -> Result<PolyFuncTypeRV, SignatureError> {
705-
const TP: TypeParam = TypeParam::RuntimeType(TypeBound::Any);
705+
const TP: TypeParam = TypeParam::RuntimeType(TypeBound::Linear);
706706
let [TypeArg::BoundedNat(n)] = arg_values else {
707707
return Err(SignatureError::InvalidTypeArgs);
708708
};
709709
let n = *n as usize;
710710
let tvs: Vec<Type> = (0..n)
711-
.map(|_| Type::new_var_use(0, TypeBound::Any))
711+
.map(|_| Type::new_var_use(0, TypeBound::Linear))
712712
.collect();
713713
Ok(PolyFuncTypeRV::new(
714714
vec![TP.clone()],
@@ -752,9 +752,9 @@ pub(super) mod test {
752752

753753
// quick sanity check that we are validating the args - note changed bound:
754754
assert_eq!(
755-
def.validate_args(&args, &[TypeBound::Any.into()]),
755+
def.validate_args(&args, &[TypeBound::Linear.into()]),
756756
Err(SignatureError::TypeVarDoesNotMatchDeclaration {
757-
actual: TypeBound::Any.into(),
757+
actual: TypeBound::Linear.into(),
758758
cached: TypeBound::Copyable.into()
759759
})
760760
);
@@ -791,8 +791,8 @@ pub(super) mod test {
791791
"SimpleOp".into(),
792792
String::new(),
793793
PolyFuncTypeRV::new(
794-
vec![TypeBound::Any.into()],
795-
Signature::new_endo(vec![Type::new_var_use(0, TypeBound::Any)]),
794+
vec![TypeBound::Linear.into()],
795+
Signature::new_endo(vec![Type::new_var_use(0, TypeBound::Linear)]),
796796
),
797797
extension_ref,
798798
)?;
@@ -807,7 +807,7 @@ pub(super) mod test {
807807
def.compute_signature(&[arg.clone()]),
808808
Err(SignatureError::TypeArgMismatch(
809809
TermTypeError::TypeMismatch {
810-
type_: TypeBound::Any.into(),
810+
type_: TypeBound::Linear.into(),
811811
term: arg,
812812
}
813813
))

hugr-core/src/extension/prelude.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,10 @@ lazy_static! {
110110
PANIC_OP_ID,
111111
"Panic with input error".to_string(),
112112
PolyFuncTypeRV::new(
113-
[TypeParam::new_list_type(TypeBound::Any), TypeParam::new_list_type(TypeBound::Any)],
113+
[TypeParam::new_list_type(TypeBound::Linear), TypeParam::new_list_type(TypeBound::Linear)],
114114
FuncValueType::new(
115-
vec![TypeRV::new_extension(error_type.clone()), TypeRV::new_row_var_use(0, TypeBound::Any)],
116-
vec![TypeRV::new_row_var_use(1, TypeBound::Any)],
115+
vec![TypeRV::new_extension(error_type.clone()), TypeRV::new_row_var_use(0, TypeBound::Linear)],
116+
vec![TypeRV::new_row_var_use(1, TypeBound::Linear)],
117117
),
118118
),
119119
extension_ref,
@@ -124,10 +124,10 @@ lazy_static! {
124124
EXIT_OP_ID,
125125
"Exit with input error".to_string(),
126126
PolyFuncTypeRV::new(
127-
[TypeParam::new_list_type(TypeBound::Any), TypeParam::new_list_type(TypeBound::Any)],
127+
[TypeParam::new_list_type(TypeBound::Linear), TypeParam::new_list_type(TypeBound::Linear)],
128128
FuncValueType::new(
129-
vec![TypeRV::new_extension(error_type), TypeRV::new_row_var_use(0, TypeBound::Any)],
130-
vec![TypeRV::new_row_var_use(1, TypeBound::Any)],
129+
vec![TypeRV::new_extension(error_type), TypeRV::new_row_var_use(0, TypeBound::Linear)],
130+
vec![TypeRV::new_row_var_use(1, TypeBound::Linear)],
131131
),
132132
),
133133
extension_ref,
@@ -160,7 +160,7 @@ pub(crate) fn qb_custom_t(extension_ref: &Weak<Extension>) -> CustomType {
160160
TypeName::new_inline("qubit"),
161161
vec![],
162162
PRELUDE_ID,
163-
TypeBound::Any,
163+
TypeBound::Linear,
164164
extension_ref,
165165
)
166166
}
@@ -626,10 +626,10 @@ impl MakeOpDef for TupleOpDef {
626626
}
627627

628628
fn init_signature(&self, _extension_ref: &Weak<Extension>) -> SignatureFunc {
629-
let rv = TypeRV::new_row_var_use(0, TypeBound::Any);
629+
let rv = TypeRV::new_row_var_use(0, TypeBound::Linear);
630630
let tuple_type = TypeRV::new_tuple(vec![rv.clone()]);
631631

632-
let param = TypeParam::new_list_type(TypeBound::Any);
632+
let param = TypeParam::new_list_type(TypeBound::Linear);
633633
match self {
634634
TupleOpDef::MakeTuple => {
635635
PolyFuncTypeRV::new([param], FuncValueType::new(rv, tuple_type))
@@ -800,8 +800,8 @@ impl MakeOpDef for NoopDef {
800800
}
801801

802802
fn init_signature(&self, _extension_ref: &Weak<Extension>) -> SignatureFunc {
803-
let tv = Type::new_var_use(0, TypeBound::Any);
804-
PolyFuncType::new([TypeBound::Any.into()], Signature::new_endo(tv)).into()
803+
let tv = Type::new_var_use(0, TypeBound::Linear);
804+
PolyFuncType::new([TypeBound::Linear.into()], Signature::new_endo(tv)).into()
805805
}
806806

807807
fn description(&self) -> String {
@@ -912,8 +912,8 @@ impl MakeOpDef for BarrierDef {
912912

913913
fn init_signature(&self, _extension_ref: &Weak<Extension>) -> SignatureFunc {
914914
PolyFuncTypeRV::new(
915-
vec![TypeParam::new_list_type(TypeBound::Any)],
916-
FuncValueType::new_endo(TypeRV::new_row_var_use(0, TypeBound::Any)),
915+
vec![TypeParam::new_list_type(TypeBound::Linear)],
916+
FuncValueType::new_endo(TypeRV::new_row_var_use(0, TypeBound::Linear)),
917917
)
918918
.into()
919919
}

hugr-core/src/extension/resolution/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ fn resolve_custom_const(#[case] custom_const: impl CustomConst) {
333333
#[rstest]
334334
fn resolve_call() {
335335
let dummy_fn_sig = PolyFuncType::new(
336-
vec![TypeParam::RuntimeType(TypeBound::Any)],
336+
vec![TypeParam::RuntimeType(TypeBound::Linear)],
337337
Signature::new(vec![], vec![bool_t()]),
338338
);
339339

hugr-core/src/extension/type_def.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ impl TypeDefBound {
3434
#[must_use]
3535
pub fn any() -> Self {
3636
TypeDefBound::Explicit {
37-
bound: TypeBound::Any,
37+
bound: TypeBound::Linear,
3838
}
3939
}
4040

@@ -142,7 +142,7 @@ impl TypeDef {
142142
let args: Vec<_> = args.iter().collect();
143143
if indices.is_empty() {
144144
// Assume most general case
145-
return TypeBound::Any;
145+
return TypeBound::Linear;
146146
}
147147
least_upper_bound(indices.iter().map(|i| {
148148
let ta = args.get(*i);

hugr-core/src/hugr/serialize/test.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ fn serialize_types_roundtrip() {
519519
#[case(bool_t())]
520520
#[case(usize_t())]
521521
#[case(INT_TYPES[2].clone())]
522-
#[case(Type::new_alias(crate::ops::AliasDecl::new("t", TypeBound::Any)))]
522+
#[case(Type::new_alias(crate::ops::AliasDecl::new("t", TypeBound::Linear)))]
523523
#[case(Type::new_var_use(2, TypeBound::Copyable))]
524524
#[case(Type::new_tuple(vec![bool_t(),qb_t()]))]
525525
#[case(Type::new_sum([vec![bool_t(),qb_t()], vec![Type::new_unit_sum(4)]]))]
@@ -553,9 +553,9 @@ fn polyfunctype1() -> PolyFuncType {
553553
}
554554

555555
fn polyfunctype2() -> PolyFuncTypeRV {
556-
let tv0 = TypeRV::new_row_var_use(0, TypeBound::Any);
556+
let tv0 = TypeRV::new_row_var_use(0, TypeBound::Linear);
557557
let tv1 = TypeRV::new_row_var_use(1, TypeBound::Copyable);
558-
let params = [TypeBound::Any, TypeBound::Copyable].map(TypeParam::new_list_type);
558+
let params = [TypeBound::Linear, TypeBound::Copyable].map(TypeParam::new_list_type);
559559
let inputs = vec![
560560
TypeRV::new_function(FuncValueType::new(tv0.clone(), tv1.clone())),
561561
tv0,
@@ -572,11 +572,11 @@ fn polyfunctype2() -> PolyFuncTypeRV {
572572
#[case(polyfunctype1())]
573573
#[case(PolyFuncType::new([TypeParam::StringType], Signature::new_endo(vec![Type::new_var_use(0, TypeBound::Copyable)])))]
574574
#[case(PolyFuncType::new([TypeBound::Copyable.into()], Signature::new_endo(vec![Type::new_var_use(0, TypeBound::Copyable)])))]
575-
#[case(PolyFuncType::new([TypeParam::new_list_type(TypeBound::Any)], Signature::new_endo(type_row![])))]
576-
#[case(PolyFuncType::new([TypeParam::new_tuple_type([TypeBound::Any.into(), TypeParam::bounded_nat_type(2.try_into().unwrap())])], Signature::new_endo(type_row![])))]
575+
#[case(PolyFuncType::new([TypeParam::new_list_type(TypeBound::Linear)], Signature::new_endo(type_row![])))]
576+
#[case(PolyFuncType::new([TypeParam::new_tuple_type([TypeBound::Linear.into(), TypeParam::bounded_nat_type(2.try_into().unwrap())])], Signature::new_endo(type_row![])))]
577577
#[case(PolyFuncType::new(
578-
[TypeParam::new_list_type(TypeBound::Any)],
579-
Signature::new_endo(Type::new_tuple(TypeRV::new_row_var_use(0, TypeBound::Any)))))]
578+
[TypeParam::new_list_type(TypeBound::Linear)],
579+
Signature::new_endo(Type::new_tuple(TypeRV::new_row_var_use(0, TypeBound::Linear)))))]
580580
fn roundtrip_polyfunctype_fixedlen(#[case] poly_func_type: PolyFuncType) {
581581
check_testing_roundtrip(poly_func_type);
582582
}
@@ -585,11 +585,11 @@ fn roundtrip_polyfunctype_fixedlen(#[case] poly_func_type: PolyFuncType) {
585585
#[case(FuncValueType::new_endo(type_row![]).into())]
586586
#[case(PolyFuncTypeRV::new([TypeParam::StringType], FuncValueType::new_endo(vec![Type::new_var_use(0, TypeBound::Copyable)])))]
587587
#[case(PolyFuncTypeRV::new([TypeBound::Copyable.into()], FuncValueType::new_endo(vec![Type::new_var_use(0, TypeBound::Copyable)])))]
588-
#[case(PolyFuncTypeRV::new([TypeParam::new_list_type(TypeBound::Any)], FuncValueType::new_endo(type_row![])))]
589-
#[case(PolyFuncTypeRV::new([TypeParam::new_tuple_type([TypeBound::Any.into(), TypeParam::bounded_nat_type(2.try_into().unwrap())])], FuncValueType::new_endo(type_row![])))]
588+
#[case(PolyFuncTypeRV::new([TypeParam::new_list_type(TypeBound::Linear)], FuncValueType::new_endo(type_row![])))]
589+
#[case(PolyFuncTypeRV::new([TypeParam::new_tuple_type([TypeBound::Linear.into(), TypeParam::bounded_nat_type(2.try_into().unwrap())])], FuncValueType::new_endo(type_row![])))]
590590
#[case(PolyFuncTypeRV::new(
591-
[TypeParam::new_list_type(TypeBound::Any)],
592-
FuncValueType::new_endo(TypeRV::new_row_var_use(0, TypeBound::Any))))]
591+
[TypeParam::new_list_type(TypeBound::Linear)],
592+
FuncValueType::new_endo(TypeRV::new_row_var_use(0, TypeBound::Linear))))]
593593
#[case(polyfunctype2())]
594594
fn roundtrip_polyfunctype_varlen(#[case] poly_func_type: PolyFuncTypeRV) {
595595
check_testing_roundtrip(poly_func_type);
@@ -600,7 +600,7 @@ fn roundtrip_polyfunctype_varlen(#[case] poly_func_type: PolyFuncTypeRV) {
600600
#[case(ops::FuncDefn::new("polyfunc1", polyfunctype1()))]
601601
#[case(ops::FuncDecl::new("polyfunc2", polyfunctype1()))]
602602
#[case(ops::AliasDefn { name: "aliasdefn".into(), definition: Type::new_unit_sum(4)})]
603-
#[case(ops::AliasDecl { name: "aliasdecl".into(), bound: TypeBound::Any})]
603+
#[case(ops::AliasDecl { name: "aliasdecl".into(), bound: TypeBound::Linear})]
604604
#[case(ops::Const::new(Value::false_val()))]
605605
#[case(ops::Const::new(Value::function(crate::builder::test::simple_dfg_hugr()).unwrap()))]
606606
#[case(ops::Input::new(vec![Type::new_var_use(3,TypeBound::Copyable)]))]

hugr-core/src/hugr/validate/test.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ fn invalid_types() {
320320
"MyContainer",
321321
vec![usize_t().into()],
322322
EXT_ID,
323-
TypeBound::Any,
323+
TypeBound::Linear,
324324
&Arc::downgrade(&ext),
325325
));
326326
let mut hugr = identity_hugr_with_type(valid.clone()).0;
@@ -332,7 +332,7 @@ fn invalid_types() {
332332
"MyContainer",
333333
vec![valid.clone().into()],
334334
EXT_ID,
335-
TypeBound::Any,
335+
TypeBound::Linear,
336336
&Arc::downgrade(&ext),
337337
);
338338
assert_eq!(
@@ -354,7 +354,7 @@ fn invalid_types() {
354354
validate_to_sig_error(bad_bound.clone()),
355355
SignatureError::WrongBound {
356356
actual: TypeBound::Copyable,
357-
expected: TypeBound::Any
357+
expected: TypeBound::Linear
358358
}
359359
);
360360

@@ -363,22 +363,22 @@ fn invalid_types() {
363363
"MyContainer",
364364
vec![Type::new_extension(bad_bound).into()],
365365
EXT_ID,
366-
TypeBound::Any,
366+
TypeBound::Linear,
367367
&Arc::downgrade(&ext),
368368
);
369369
assert_eq!(
370370
validate_to_sig_error(nested),
371371
SignatureError::WrongBound {
372372
actual: TypeBound::Copyable,
373-
expected: TypeBound::Any
373+
expected: TypeBound::Linear
374374
}
375375
);
376376

377377
let too_many_type_args = CustomType::new(
378378
"MyContainer",
379379
vec![usize_t().into(), 3u64.into()],
380380
EXT_ID,
381-
TypeBound::Any,
381+
TypeBound::Linear,
382382
&Arc::downgrade(&ext),
383383
);
384384
assert_eq!(
@@ -393,8 +393,8 @@ fn typevars_declared() -> Result<(), Box<dyn std::error::Error>> {
393393
let f = FunctionBuilder::new(
394394
"myfunc",
395395
PolyFuncType::new(
396-
[TypeBound::Any.into()],
397-
Signature::new_endo(vec![Type::new_var_use(0, TypeBound::Any)]),
396+
[TypeBound::Linear.into()],
397+
Signature::new_endo(vec![Type::new_var_use(0, TypeBound::Linear)]),
398398
),
399399
)?;
400400
let [w] = f.input_wires_arr();
@@ -403,8 +403,8 @@ fn typevars_declared() -> Result<(), Box<dyn std::error::Error>> {
403403
let f = FunctionBuilder::new(
404404
"myfunc",
405405
PolyFuncType::new(
406-
[TypeBound::Any.into()],
407-
Signature::new_endo(vec![Type::new_var_use(1, TypeBound::Any)]),
406+
[TypeBound::Linear.into()],
407+
Signature::new_endo(vec![Type::new_var_use(1, TypeBound::Linear)]),
408408
),
409409
)?;
410410
let [w] = f.input_wires_arr();
@@ -413,7 +413,7 @@ fn typevars_declared() -> Result<(), Box<dyn std::error::Error>> {
413413
let f = FunctionBuilder::new(
414414
"myfunc",
415415
PolyFuncType::new(
416-
[TypeBound::Any.into()],
416+
[TypeBound::Linear.into()],
417417
Signature::new_endo(vec![Type::new_var_use(1, TypeBound::Copyable)]),
418418
),
419419
)?;
@@ -486,10 +486,10 @@ fn no_polymorphic_consts() -> Result<(), Box<dyn std::error::Error>> {
486486
}
487487

488488
pub(crate) fn extension_with_eval_parallel() -> Arc<Extension> {
489-
let rowp = TypeParam::new_list_type(TypeBound::Any);
489+
let rowp = TypeParam::new_list_type(TypeBound::Linear);
490490
Extension::new_test_arc(EXT_ID, |ext, extension_ref| {
491-
let inputs = TypeRV::new_row_var_use(0, TypeBound::Any);
492-
let outputs = TypeRV::new_row_var_use(1, TypeBound::Any);
491+
let inputs = TypeRV::new_row_var_use(0, TypeBound::Linear);
492+
let outputs = TypeRV::new_row_var_use(1, TypeBound::Linear);
493493
let evaled_fn = TypeRV::new_function(FuncValueType::new(inputs.clone(), outputs.clone()));
494494
let pf = PolyFuncTypeRV::new(
495495
[rowp.clone(), rowp.clone()],
@@ -498,7 +498,7 @@ pub(crate) fn extension_with_eval_parallel() -> Arc<Extension> {
498498
ext.add_op("eval".into(), String::new(), pf, extension_ref)
499499
.unwrap();
500500

501-
let rv = |idx| TypeRV::new_row_var_use(idx, TypeBound::Any);
501+
let rv = |idx| TypeRV::new_row_var_use(idx, TypeBound::Linear);
502502
let pf = PolyFuncTypeRV::new(
503503
[rowp.clone(), rowp.clone(), rowp.clone(), rowp.clone()],
504504
Signature::new(
@@ -548,13 +548,13 @@ fn list1ty(t: TypeRV) -> Term {
548548
#[test]
549549
fn row_variables() -> Result<(), Box<dyn std::error::Error>> {
550550
let e = extension_with_eval_parallel();
551-
let tv = TypeRV::new_row_var_use(0, TypeBound::Any);
551+
let tv = TypeRV::new_row_var_use(0, TypeBound::Linear);
552552
let inner_ft = Type::new_function(FuncValueType::new_endo(tv.clone()));
553553
let ft_usz = Type::new_function(FuncValueType::new_endo(vec![tv.clone(), usize_t().into()]));
554554
let mut fb = FunctionBuilder::new(
555555
"id",
556556
PolyFuncType::new(
557-
[TypeParam::new_list_type(TypeBound::Any)],
557+
[TypeParam::new_list_type(TypeBound::Linear)],
558558
Signature::new(inner_ft.clone(), ft_usz),
559559
),
560560
)?;
@@ -582,8 +582,8 @@ fn test_polymorphic_load() -> Result<(), Box<dyn std::error::Error>> {
582582
let id = m.declare(
583583
"id",
584584
PolyFuncType::new(
585-
vec![TypeBound::Any.into()],
586-
Signature::new_endo(vec![Type::new_var_use(0, TypeBound::Any)]),
585+
vec![TypeBound::Linear.into()],
586+
Signature::new_endo(vec![Type::new_var_use(0, TypeBound::Linear)]),
587587
),
588588
)?;
589589
let sig = Signature::new(

hugr-core/src/import.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,7 +1203,7 @@ impl<'a> Context<'a> {
12031203

12041204
/// Import a [`Term`] from a term that represents a static type or value.
12051205
fn import_term(&mut self, term_id: table::TermId) -> Result<Term, ImportError> {
1206-
self.import_term_with_bound(term_id, TypeBound::Any)
1206+
self.import_term_with_bound(term_id, TypeBound::Linear)
12071207
}
12081208

12091209
fn import_term_with_bound(
@@ -1542,7 +1542,7 @@ impl<'a> Context<'a> {
15421542
}
15431543
}
15441544
table::Term::Var(table::VarId(_, index)) => {
1545-
let var = RV::try_from_rv(RowVariable(*index as _, TypeBound::Any))
1545+
let var = RV::try_from_rv(RowVariable(*index as _, TypeBound::Linear))
15461546
.map_err(|_| error_invalid!("expected a closed list"))?;
15471547
types.push(TypeBase::new(TypeEnum::RowVar(var)));
15481548
}
@@ -1796,7 +1796,7 @@ impl LocalVar {
17961796
pub fn new(r#type: table::TermId) -> Self {
17971797
Self {
17981798
r#type,
1799-
bound: TypeBound::Any,
1799+
bound: TypeBound::Linear,
18001800
}
18011801
}
18021802
}

0 commit comments

Comments
 (0)