Skip to content

Commit 83ea9e3

Browse files
committed
Import no longer needs to distinguish between type params and args.
1 parent eda80df commit 83ea9e3

File tree

2 files changed

+40
-129
lines changed

2 files changed

+40
-129
lines changed

hugr-core/src/import.rs

Lines changed: 36 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ use crate::{
2222
},
2323
types::{
2424
CustomType, FuncTypeBase, MaybeRV, PolyFuncType, PolyFuncTypeBase, RowVariable, Signature,
25-
Type, TypeArg, TypeBase, TypeBound, TypeEnum, TypeName, TypeRow, type_param::TypeParam,
26-
type_row::TypeRowBase,
25+
Term, Type, TypeArg, TypeBase, TypeBound, TypeEnum, TypeName, TypeRow,
26+
type_param::TypeParam, type_row::TypeRowBase,
2727
},
2828
};
2929
use fxhash::FxHashMap;
@@ -985,7 +985,7 @@ impl<'a> Context<'a> {
985985

986986
let type_args = args
987987
.iter()
988-
.map(|term| self.import_type_arg(*term))
988+
.map(|term| self.import_term(*term))
989989
.collect::<Result<Vec<TypeArg>, _>>()?;
990990

991991
self.static_edges.push((*symbol, node_id));
@@ -1008,7 +1008,7 @@ impl<'a> Context<'a> {
10081008
let func_sig = self.get_func_signature(*symbol)?;
10091009
let type_args = args
10101010
.iter()
1011-
.map(|term| self.import_type_arg(*term))
1011+
.map(|term| self.import_term(*term))
10121012
.collect::<Result<Vec<TypeArg>, _>>()?;
10131013

10141014
self.static_edges.push((*symbol, node_id));
@@ -1085,7 +1085,7 @@ impl<'a> Context<'a> {
10851085
let name = self.get_symbol_name(*node)?;
10861086
let args = params
10871087
.iter()
1088-
.map(|param| self.import_type_arg(*param))
1088+
.map(|param| self.import_term(*param))
10891089
.collect::<Result<Vec<_>, _>>()?;
10901090
let (extension, name) = self.import_custom_name(name)?;
10911091
let signature = self.get_node_signature(node_id)?;
@@ -1176,10 +1176,10 @@ impl<'a> Context<'a> {
11761176
for (index, param) in symbol.params.iter().enumerate() {
11771177
// NOTE: `PolyFuncType` only has explicit type parameters at present.
11781178
let bound = self.local_vars[&table::VarId(node, index as _)].bound;
1179-
imported_params
1180-
.push(self.import_type_param(param.r#type, bound).map_err(|err| {
1181-
error_context!(err, "type of parameter `{}`", param.name)
1182-
})?);
1179+
imported_params.push(
1180+
self.import_term_with_bound(param.r#type, bound)
1181+
.map_err(|err| error_context!(err, "type of parameter `{}`", param.name))?,
1182+
);
11831183
}
11841184

11851185
let body = self.import_func_type::<RV>(symbol.signature)?;
@@ -1188,63 +1188,58 @@ impl<'a> Context<'a> {
11881188
.map_err(|err| error_context!(err, "symbol `{}` defined by node {}", symbol.name, node))
11891189
}
11901190

1191-
/// Import a [`TypeParam`] from a term that represents a static type.
1192-
fn import_type_param(
1191+
/// Import a [`Term`] from a term that represents a static type or value.
1192+
fn import_term(&mut self, term_id: table::TermId) -> Result<Term, ImportError> {
1193+
self.import_term_with_bound(term_id, TypeBound::Any)
1194+
}
1195+
1196+
fn import_term_with_bound(
11931197
&mut self,
11941198
term_id: table::TermId,
11951199
bound: TypeBound,
1196-
) -> Result<TypeParam, ImportError> {
1200+
) -> Result<Term, ImportError> {
11971201
(|| {
11981202
if let Some([]) = self.match_symbol(term_id, model::CORE_STR_TYPE)? {
1199-
return Ok(TypeParam::StringType);
1203+
return Ok(Term::StringType);
12001204
}
12011205

12021206
if let Some([]) = self.match_symbol(term_id, model::CORE_NAT_TYPE)? {
1203-
return Ok(TypeParam::max_nat_type());
1207+
return Ok(Term::max_nat_type());
12041208
}
12051209

12061210
if let Some([]) = self.match_symbol(term_id, model::CORE_BYTES_TYPE)? {
1207-
return Ok(TypeParam::BytesType);
1211+
return Ok(Term::BytesType);
12081212
}
12091213

12101214
if let Some([]) = self.match_symbol(term_id, model::CORE_FLOAT_TYPE)? {
1211-
return Ok(TypeParam::FloatType);
1215+
return Ok(Term::FloatType);
12121216
}
12131217

12141218
if let Some([]) = self.match_symbol(term_id, model::CORE_TYPE)? {
12151219
return Ok(TypeParam::RuntimeType { b: bound });
12161220
}
12171221

1218-
if let Some([]) = self.match_symbol(term_id, model::CORE_STATIC)? {
1219-
return Err(error_unsupported!(
1220-
"`{}` as `TypeParam`",
1221-
model::CORE_STATIC
1222-
));
1223-
}
1224-
12251222
if let Some([]) = self.match_symbol(term_id, model::CORE_CONSTRAINT)? {
1226-
return Err(error_unsupported!(
1227-
"`{}` as `TypeParam`",
1228-
model::CORE_CONSTRAINT
1229-
));
1223+
return Err(error_unsupported!("`{}`", model::CORE_CONSTRAINT));
12301224
}
12311225

1232-
if let Some([]) = self.match_symbol(term_id, model::CORE_CONST)? {
1233-
return Err(error_unsupported!("`{}` as `TypeParam`", model::CORE_CONST));
1226+
if let Some([]) = self.match_symbol(term_id, model::CORE_STATIC)? {
1227+
return Ok(Term::StaticType);
12341228
}
12351229

12361230
if let Some([]) = self.match_symbol(term_id, model::CORE_CTRL_TYPE)? {
1237-
return Err(error_unsupported!(
1238-
"`{}` as `TypeParam`",
1239-
model::CORE_CTRL_TYPE
1240-
));
1231+
return Err(error_unsupported!("`{}`", model::CORE_CTRL_TYPE));
1232+
}
1233+
1234+
if let Some([]) = self.match_symbol(term_id, model::CORE_CONST)? {
1235+
return Err(error_unsupported!("`{}`", model::CORE_CONST));
12411236
}
12421237

12431238
if let Some([item_type]) = self.match_symbol(term_id, model::CORE_LIST_TYPE)? {
12441239
// At present `hugr-model` has no way to express that the item
12451240
// type of a list must be copyable. Therefore we import it as `Any`.
12461241
let param = Box::new(
1247-
self.import_type_param(item_type, TypeBound::Any)
1242+
self.import_term(item_type)
12481243
.map_err(|err| error_context!(err, "item type of list type"))?,
12491244
);
12501245
return Ok(TypeParam::ListType { param });
@@ -1256,95 +1251,13 @@ impl<'a> Context<'a> {
12561251
let params = (|| {
12571252
self.import_closed_list(item_types)?
12581253
.into_iter()
1259-
.map(|param| self.import_type_param(param, TypeBound::Any))
1254+
.map(|param| self.import_term(param))
12601255
.collect::<Result<_, _>>()
12611256
})()
12621257
.map_err(|err| error_context!(err, "item types of tuple type"))?;
12631258
return Ok(TypeParam::TupleType { params });
12641259
}
12651260

1266-
match self.get_term(term_id)? {
1267-
table::Term::Wildcard => Err(error_uninferred!("wildcard")),
1268-
1269-
table::Term::Var { .. } => Err(error_unsupported!("type variable as `TypeParam`")),
1270-
table::Term::Apply(symbol, _) => {
1271-
let name = self.get_symbol_name(*symbol)?;
1272-
Err(error_unsupported!("custom type `{}` as `TypeParam`", name))
1273-
}
1274-
1275-
table::Term::Tuple(_)
1276-
| table::Term::List { .. }
1277-
| table::Term::Func { .. }
1278-
| table::Term::Literal(_) => Err(error_invalid!("expected a static type")),
1279-
}
1280-
})()
1281-
.map_err(|err| error_context!(err, "term {} as `TypeParam`", term_id))
1282-
}
1283-
1284-
/// Import a `TypeArg` from a term that represents a static type or value.
1285-
fn import_type_arg(&mut self, term_id: table::TermId) -> Result<TypeArg, ImportError> {
1286-
(|| {
1287-
if let Some([]) = self.match_symbol(term_id, model::CORE_STR_TYPE)? {
1288-
return Err(error_unsupported!(
1289-
"`{}` as `TypeArg`",
1290-
model::CORE_STR_TYPE
1291-
));
1292-
}
1293-
1294-
if let Some([]) = self.match_symbol(term_id, model::CORE_NAT_TYPE)? {
1295-
return Err(error_unsupported!(
1296-
"`{}` as `TypeArg`",
1297-
model::CORE_NAT_TYPE
1298-
));
1299-
}
1300-
1301-
if let Some([]) = self.match_symbol(term_id, model::CORE_BYTES_TYPE)? {
1302-
return Err(error_unsupported!(
1303-
"`{}` as `TypeArg`",
1304-
model::CORE_BYTES_TYPE
1305-
));
1306-
}
1307-
1308-
if let Some([]) = self.match_symbol(term_id, model::CORE_FLOAT_TYPE)? {
1309-
return Err(error_unsupported!(
1310-
"`{}` as `TypeArg`",
1311-
model::CORE_FLOAT_TYPE
1312-
));
1313-
}
1314-
1315-
if let Some([]) = self.match_symbol(term_id, model::CORE_TYPE)? {
1316-
return Err(error_unsupported!("`{}` as `TypeArg`", model::CORE_TYPE));
1317-
}
1318-
1319-
if let Some([]) = self.match_symbol(term_id, model::CORE_CONSTRAINT)? {
1320-
return Err(error_unsupported!(
1321-
"`{}` as `TypeArg`",
1322-
model::CORE_CONSTRAINT
1323-
));
1324-
}
1325-
1326-
if let Some([]) = self.match_symbol(term_id, model::CORE_STATIC)? {
1327-
return Err(error_unsupported!("`{}` as `TypeArg`", model::CORE_STATIC));
1328-
}
1329-
1330-
if let Some([]) = self.match_symbol(term_id, model::CORE_CTRL_TYPE)? {
1331-
return Err(error_unsupported!(
1332-
"`{}` as `TypeArg`",
1333-
model::CORE_CTRL_TYPE
1334-
));
1335-
}
1336-
1337-
if let Some([]) = self.match_symbol(term_id, model::CORE_CONST)? {
1338-
return Err(error_unsupported!("`{}` as `TypeArg`", model::CORE_CONST));
1339-
}
1340-
1341-
if let Some([]) = self.match_symbol(term_id, model::CORE_LIST_TYPE)? {
1342-
return Err(error_unsupported!(
1343-
"`{}` as `TypeArg`",
1344-
model::CORE_LIST_TYPE
1345-
));
1346-
}
1347-
13481261
match self.get_term(term_id)? {
13491262
table::Term::Wildcard => Err(error_uninferred!("wildcard")),
13501263

@@ -1353,15 +1266,15 @@ impl<'a> Context<'a> {
13531266
.local_vars
13541267
.get(var)
13551268
.ok_or_else(|| error_invalid!("unknown variable {}", var))?;
1356-
let decl = self.import_type_param(var_info.r#type, var_info.bound)?;
1269+
let decl = self.import_term_with_bound(var_info.r#type, var_info.bound)?;
13571270
Ok(TypeArg::new_var_use(var.1 as _, decl))
13581271
}
13591272

13601273
table::Term::List { .. } => {
13611274
let elems = (|| {
13621275
self.import_closed_list(term_id)?
13631276
.iter()
1364-
.map(|item| self.import_type_arg(*item))
1277+
.map(|item| self.import_term(*item))
13651278
.collect::<Result<_, _>>()
13661279
})()
13671280
.map_err(|err| error_context!(err, "list items"))?;
@@ -1373,7 +1286,7 @@ impl<'a> Context<'a> {
13731286
let elems = (|| {
13741287
self.import_closed_list(term_id)?
13751288
.iter()
1376-
.map(|item| self.import_type_arg(*item))
1289+
.map(|item| self.import_term(*item))
13771290
.collect::<Result<_, _>>()
13781291
})()
13791292
.map_err(|err| error_context!(err, "tuple items"))?;
@@ -1395,17 +1308,15 @@ impl<'a> Context<'a> {
13951308
table::Term::Literal(model::Literal::Float(value)) => {
13961309
Ok(TypeArg::Float { value: *value })
13971310
}
1398-
table::Term::Func { .. } => {
1399-
Err(error_unsupported!("function constant as `TypeArg`"))
1400-
}
1311+
table::Term::Func { .. } => Err(error_unsupported!("function constant")),
14011312

14021313
table::Term::Apply { .. } => {
14031314
let ty = self.import_type(term_id)?;
14041315
Ok(TypeArg::Type { ty })
14051316
}
14061317
}
14071318
})()
1408-
.map_err(|err| error_context!(err, "term {} as `TypeArg`", term_id))
1319+
.map_err(|err| error_context!(err, "term {}", term_id))
14091320
}
14101321

14111322
/// Import a `Type` from a term that represents a runtime type.
@@ -1439,7 +1350,7 @@ impl<'a> Context<'a> {
14391350

14401351
let args = args
14411352
.iter()
1442-
.map(|arg| self.import_type_arg(*arg))
1353+
.map(|arg| self.import_term(*arg))
14431354
.collect::<Result<Vec<_>, _>>()
14441355
.map_err(|err| {
14451356
error_context!(err, "type argument of custom type `{}`", name)

hugr-core/src/types/type_param.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -766,15 +766,15 @@ mod test {
766766

767767
use proptest::prelude::*;
768768

769-
use super::super::{TypeArg, TypeArgVariable, TypeParam, UpperBound};
769+
use super::super::{TypeArgVariable, UpperBound};
770770
use crate::proptest::RecursionDepth;
771-
use crate::types::{Type, TypeBound};
771+
use crate::types::{Term, Type, TypeBound};
772772

773773
impl Arbitrary for TypeArgVariable {
774774
type Parameters = RecursionDepth;
775775
type Strategy = BoxedStrategy<Self>;
776776
fn arbitrary_with(depth: Self::Parameters) -> Self::Strategy {
777-
(any::<usize>(), any_with::<TypeParam>(depth))
777+
(any::<usize>(), any_with::<Term>(depth))
778778
.prop_map(|(idx, cached_decl)| Self {
779779
idx,
780780
cached_decl: Box::new(cached_decl),
@@ -783,7 +783,7 @@ mod test {
783783
}
784784
}
785785

786-
impl Arbitrary for TypeParam {
786+
impl Arbitrary for Term {
787787
type Parameters = RecursionDepth;
788788
type Strategy = BoxedStrategy<Self>;
789789
fn arbitrary_with(depth: Self::Parameters) -> Self::Strategy {

0 commit comments

Comments
 (0)