Skip to content

Commit

Permalink
remove type annots, fix doclinks, various comments
Browse files Browse the repository at this point in the history
  • Loading branch information
acl-cqc committed Jul 15, 2024
1 parent 3477596 commit 23db41d
Show file tree
Hide file tree
Showing 12 changed files with 35 additions and 34 deletions.
2 changes: 1 addition & 1 deletion hugr-core/src/extension/op_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ impl SignatureFunc {
args: &[TypeArg],
exts: &ExtensionRegistry,
) -> Result<FunctionType, SignatureError> {
let temp: TypeSchemeRV;
let temp: TypeSchemeRV; // to keep alive
let (pf, args) = match &self {
SignatureFunc::TypeScheme(custom) => {
custom.validate.validate(args, def, exts)?;
Expand Down
2 changes: 1 addition & 1 deletion hugr-core/src/extension/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ mod test {
.instantiate([])
.unwrap();

let ext_type: Type = Type::new_extension(ext_def);
let ext_type = Type::new_extension(ext_def);
assert_eq!(ext_type, ERROR_TYPE);

let error_val = ConstError::new(2, "my message");
Expand Down
4 changes: 2 additions & 2 deletions hugr-core/src/extension/type_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,14 +182,14 @@ mod test {
description: "Some parametrised type".into(),
bound: TypeDefBound::FromParams(vec![0]),
};
let typ: Type = Type::new_extension(
let typ = Type::new_extension(
def.instantiate(vec![TypeArg::Type {
ty: Type::new_function(FunctionType::new(vec![], vec![])),
}])
.unwrap(),
);
assert_eq!(typ.least_upper_bound(), TypeBound::Copyable);
let typ2: Type = Type::new_extension(def.instantiate([USIZE_T.into()]).unwrap());
let typ2 = Type::new_extension(def.instantiate([USIZE_T.into()]).unwrap());
assert_eq!(typ2.least_upper_bound(), TypeBound::Eq);

// And some bad arguments...firstly, wrong kind of TypeArg:
Expand Down
4 changes: 2 additions & 2 deletions hugr-core/src/hugr/serialize/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ fn constants_roundtrip() -> Result<(), Box<dyn std::error::Error>> {

#[test]
fn serialize_types_roundtrip() {
let g = Type::new_function(FunctionType::new_endo(vec![]));
let g: Type = Type::new_function(FunctionType::new_endo(vec![]));
check_testing_roundtrip(g.clone());

// A Simple tuple
Expand All @@ -392,7 +392,7 @@ fn serialize_types_roundtrip() {
let t = TypeRV::new_sum([type_row![USIZE_T], type_row![FLOAT64_TYPE]]);
check_testing_roundtrip(t);

let t: Type = Type::new_unit_sum(4);
let t = Type::new_unit_sum(4);
check_testing_roundtrip(t);
}

Expand Down
5 changes: 1 addition & 4 deletions hugr-core/src/hugr/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,8 @@ impl<'a, 'b> ValidationContext<'a, 'b> {
match &port_kind {
EdgeKind::Value(ty) => ty.validate(self.extension_registry, var_decls),
// Static edges must *not* refer to type variables declared by enclosing FuncDefns
// as these are only types at runtime. (Note the choice of `allow_row_vars` as `false` is arbitrary here.)
// as these are only types at runtime.
EdgeKind::Const(ty) => ty.validate(self.extension_registry, &[]),
// Allow function "value" to have unknown arity. A Call node will have to provide
// TypeArgs that produce a known arity, but a LoadFunction might pass the function
// value ("function pointer") around without knowing how to call it.
EdgeKind::Function(pf) => pf.validate(self.extension_registry),
_ => Ok(()),
}
Expand Down
6 changes: 3 additions & 3 deletions hugr-core/src/ops/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ mod test {
fn function_value(simple_dfg_hugr: Hugr) {
let v = Value::function(simple_dfg_hugr).unwrap();

let correct_type: Type = Type::new_function(FunctionType::new_endo(type_row![
let correct_type = Type::new_function(FunctionType::new_endo(type_row![
crate::extension::prelude::BOOL_T
]));

Expand Down Expand Up @@ -652,12 +652,12 @@ mod test {
let yaml_const: Value =
CustomSerialized::new(typ_int.clone(), YamlValue::Number(6.into()), ex_id.clone())
.into();
let classic_t: Type = Type::new_extension(typ_int.clone());
let classic_t = Type::new_extension(typ_int.clone());
assert_matches!(classic_t.least_upper_bound(), TypeBound::Eq);
assert_eq!(yaml_const.get_type(), classic_t);

let typ_qb = CustomType::new("my_type", vec![], ex_id, TypeBound::Eq);
let t: Type = Type::new_extension(typ_qb.clone());
let t = Type::new_extension(typ_qb.clone());
assert_ne!(yaml_const.get_type(), t);
}

Expand Down
5 changes: 2 additions & 3 deletions hugr-core/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub enum EdgeKind {
Value(Type),
/// A reference to a static constant value - must be a Copyable type
Const(Type),
/// A reference to a function i.e. [FuncDecl] or [FuncDefn]
/// A reference to a function i.e. [FuncDecl] or [FuncDefn].
///
/// [FuncDecl]: crate::ops::FuncDecl
/// [FuncDefn]: crate::ops::FuncDefn
Expand Down Expand Up @@ -259,7 +259,7 @@ impl<RV: MaybeRV> TypeEnum<RV> {
/// # use hugr::types::{Type, TypeBound};
/// # use hugr::type_row;
///
/// let sum: Type = Type::new_sum([type_row![], type_row![]]);
/// let sum = Type::new_sum([type_row![], type_row![]]);
/// assert_eq!(sum.least_upper_bound(), TypeBound::Eq);
/// ```
///
Expand Down Expand Up @@ -614,7 +614,6 @@ pub(crate) mod test {
assert_eq!(pred1, pred2);

let pred_direct = SumType::Unit { size: 2 };
// Pick <false> arbitrarily
assert_eq!(pred1, Type::from(pred_direct));
}

Expand Down
8 changes: 4 additions & 4 deletions hugr-core/src/types/poly_func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ use super::{signature::FuncTypeBase, MaybeRV, NoRV, RowVariable};
"params.iter().map(ToString::to_string).join(\" \")",
"body"
)]
pub struct TypeSchemeBase<ROWVARS: MaybeRV = RowVariable> {
pub struct TypeSchemeBase<RV: MaybeRV> {
/// The declared type parameters, i.e., these must be instantiated with
/// the same number of [TypeArg]s before the function can be called. This
/// defines the indices used by variables inside the body.
#[cfg_attr(test, proptest(strategy = "vec(any_with::<TypeParam>(params), 0..3)"))]
params: Vec<TypeParam>,
/// Template for the function. May contain variables up to length of [Self::params]
#[cfg_attr(test, proptest(strategy = "any_with::<FuncTypeBase<ROWVARS>>(params)"))]
body: FuncTypeBase<ROWVARS>,
#[cfg_attr(test, proptest(strategy = "any_with::<FuncTypeBase<RV>>(params)"))]
body: FuncTypeBase<RV>,
}

/// The polymorphic type of a [Call]-able function ([FuncDecl] or [FuncDefn]).
Expand Down Expand Up @@ -106,7 +106,7 @@ impl<RV: MaybeRV> TypeSchemeBase<RV> {
}

/// Create a new TypeSchemeBase given the kinds of the variables it declares
/// and the underlying function type.
/// and the underlying [FuncTypeBase].
pub fn new(params: impl Into<Vec<TypeParam>>, body: impl Into<FuncTypeBase<RV>>) -> Self {
Self {
params: params.into(),
Expand Down
2 changes: 1 addition & 1 deletion hugr-core/src/types/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl<RV: MaybeRV> TryFrom<SerSimpleType> for TypeBase<RV> {
SerSimpleType::Opaque(o) => TypeBase::new_extension(o),
SerSimpleType::Alias(a) => TypeBase::new_alias(a),
SerSimpleType::V { i, b } => TypeBase::new_var_use(i, b),
// We can't use new_row_var because that returns Type<true> not Type<RV>.
// We can't use new_row_var because that returns TypeRV not TypeBase<RV>.
SerSimpleType::R { i, b } => TypeBase::new(TypeEnum::RowVar(
RV::try_from_rv(RowVariable(i, b))
.map_err(|var| SignatureError::RowVarWhereTypeExpected { var })?,
Expand Down
21 changes: 13 additions & 8 deletions hugr-core/src/types/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ use {crate::proptest::RecursionDepth, ::proptest::prelude::*, proptest_derive::A

#[derive(Clone, Debug, Eq, serde::Serialize, serde::Deserialize)]
#[cfg_attr(test, derive(Arbitrary), proptest(params = "RecursionDepth"))]
/// Describes the edges required to/from a node (when ROWVARS=false);
/// or (when ROWVARS=true) the type of a [Graph] or the inputs/outputs from an OpDef
/// Describes the edges required to/from a node or inside a [FuncDefn] (when ROWVARS=[NoRV]);
/// or (when ROWVARS=[RowVariable]) the type of a higher-order [function value] or the inputs/outputs from an OpDef
///
/// ROWVARS specifies whether it may contain [RowVariable]s or not.
///
/// [Graph]: crate::ops::constant::Value::Function
/// [RowVariable]: crate::types::TypeEnum::RowVariable
/// [function value]: crate::ops::constant::Value::Function
/// [FuncDefn]: crate::ops::FuncDefn
pub struct FuncTypeBase<ROWVARS: MaybeRV> {
/// Value inputs of the function.
#[cfg_attr(test, proptest(strategy = "any_with::<TypeRowBase<ROWVARS>>(params)"))]
Expand All @@ -35,12 +35,17 @@ pub struct FuncTypeBase<ROWVARS: MaybeRV> {
pub extension_reqs: ExtensionSet,
}

/// The concept of "signature" in the spec - the edges required to/from a node or graph
/// and also the target (value) of a call (static).
/// The concept of "signature" in the spec - the edges required to/from a node
/// or within a [FuncDefn], also the target (value) of a call (static).
///
/// [FuncDefn]: crate::ops::FuncDefn
pub type FunctionType = FuncTypeBase<NoRV>;

/// A function that may contain [RowVariable]s and thus has potentially-unknown arity;
/// passable as a value round a Hugr (see [Type::new_function]) but not a valid node type.
/// used for [OpDef]'s and passable as a value round a Hugr (see [Type::new_function])
/// but not a valid node type.
///
/// [OpDef]: crate::extension::OpDef
pub type FunctionTypeRV = FuncTypeBase<RowVariable>;

impl<RV: MaybeRV> FuncTypeBase<RV> {
Expand All @@ -63,7 +68,7 @@ impl<RV: MaybeRV> FuncTypeBase<RV> {
Self {
input: input.into(),
output: output.into(),
extension_reqs: ExtensionSet::default(),
extension_reqs: ExtensionSet::new(),
}
}

Expand Down
6 changes: 3 additions & 3 deletions hugr-core/src/types/type_param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ impl UpperBound {
}
}

/// A *kind* of [TypeArg]. Thus, a parameter declared by a [TypeSchemeRV] (e.g. [OpDef]),
/// specifying a value that may (resp. must) be provided to instantiate it.
/// A *kind* of [TypeArg]. Thus, a parameter declared by a [TypeScheme] or [TypeSchemeRV],
/// specifying a value that must be provided statically in order to instantiate it.
///
/// [TypeScheme]: super::TypeScheme
/// [TypeSchemeRV]: super::TypeSchemeRV
/// [OpDef]: crate::extension::OpDef
#[derive(
Clone, Debug, PartialEq, Eq, derive_more::Display, serde::Deserialize, serde::Serialize,
)]
Expand Down
4 changes: 2 additions & 2 deletions hugr/benches/benchmarks/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ use criterion::{black_box, criterion_group, AxisScale, Criterion, PlotConfigurat
fn make_complex_type() -> Type {
let qb = QB_T;
let int = USIZE_T;
let q_register: Type = Type::new_tuple(vec![qb; 8]);
let q_register = Type::new_tuple(vec![qb; 8]);
let b_register = Type::new_tuple(vec![int; 8]);
let q_alias = Type::new_alias(AliasDecl::new("QReg", TypeBound::Any));
let sum: Type = Type::new_sum([q_register, q_alias]);
let sum = Type::new_sum([q_register, q_alias]);
Type::new_function(FunctionType::new(vec![sum], vec![b_register]))
}

Expand Down

0 comments on commit 23db41d

Please sign in to comment.