Skip to content

Commit

Permalink
tidying
Browse files Browse the repository at this point in the history
  • Loading branch information
doug-q committed May 16, 2024
1 parent e155011 commit 5f3d891
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 47 deletions.
2 changes: 0 additions & 2 deletions hugr/src/hugr/serialize/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,6 @@ pub fn check_hugr_roundtrip(hugr: &Hugr, check_schema: bool) -> Hugr {
new_hugr
}

// for now this is only used in property testing, so otherwise configured out to
// avoid unused warnings.
fn check_testing_roundtrip(t: impl Into<TestingModel>) {
let before = Versioned::new(t.into());
let after_strict = ser_roundtrip_validate(&before, Some(&TESTING_SCHEMA_STRICT));
Expand Down
14 changes: 9 additions & 5 deletions hugr/src/ops/custom.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
//! Extensible operations.
use smol_str::SmolStr;
use std::sync::Arc;
use thiserror::Error;
#[cfg(test)]
use {
crate::proptest::{any_nonempty_smolstr, any_nonempty_string},
::proptest_derive::Arbitrary,
};

use crate::extension::{ConstFoldResult, ExtensionId, ExtensionRegistry, OpDef, SignatureError};
use crate::hugr::hugrmut::sealed::HugrMutInternals;
Expand Down Expand Up @@ -268,12 +272,12 @@ impl DataflowOpTrait for ExtensionOp {

/// An opaquely-serialized op that refers to an as-yet-unresolved [`OpDef`]
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[cfg_attr(test, derive(proptest_derive::Arbitrary))]
#[cfg_attr(test, derive(Arbitrary))]
pub struct OpaqueOp {
extension: ExtensionId,
#[cfg_attr(test, proptest(strategy = "crate::proptest::any_nonempty_smolstr()"))]
op_name: SmolStr,
#[cfg_attr(test, proptest(strategy = "crate::proptest::any_nonempty_string()"))]
#[cfg_attr(test, proptest(strategy = "any_nonempty_smolstr()"))]
op_name: OpName,
#[cfg_attr(test, proptest(strategy = "any_nonempty_string()"))]
description: String, // cache in advance so description() can return &str
args: Vec<TypeArg>,
signature: FunctionType,
Expand Down
17 changes: 10 additions & 7 deletions hugr/src/ops/dataflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ use crate::ops::StaticTag;
use crate::types::{EdgeKind, FunctionType, PolyFuncType, Type, TypeArg, TypeRow};
use crate::IncomingPort;

#[cfg(test)]
use ::proptest_derive::Arbitrary;

pub(crate) trait DataflowOpTrait {
const TAG: OpTag;
fn description(&self) -> &str;
Expand Down Expand Up @@ -51,7 +54,7 @@ pub trait IOTrait {
/// An input node.
/// The outputs of this node are the inputs to the function.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[cfg_attr(test, derive(proptest_derive::Arbitrary))]
#[cfg_attr(test, derive(Arbitrary))]
pub struct Input {
/// Input value types
pub types: TypeRow,
Expand All @@ -69,7 +72,7 @@ impl IOTrait for Input {

/// An output node. The inputs are the outputs of the function.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[cfg_attr(test, derive(proptest_derive::Arbitrary))]
#[cfg_attr(test, derive(Arbitrary))]
pub struct Output {
/// Output value types
pub types: TypeRow,
Expand Down Expand Up @@ -153,7 +156,7 @@ impl<T: DataflowOpTrait> StaticTag for T {
/// The port immediately following those those is connected to the def/declare
/// block with a [`EdgeKind::Function`] edge.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[cfg_attr(test, derive(proptest_derive::Arbitrary))]
#[cfg_attr(test, derive(Arbitrary))]
pub struct Call {
/// Signature of function being called
func_sig: PolyFuncType,
Expand Down Expand Up @@ -247,7 +250,7 @@ impl Call {
/// Call a function indirectly. Like call, but the function input is a value
/// (runtime, not static) dataflow edge, and thus does not need any type-args.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[cfg_attr(test, derive(proptest_derive::Arbitrary))]
#[cfg_attr(test, derive(Arbitrary))]
pub struct CallIndirect {
/// Signature of function being called
pub signature: FunctionType,
Expand All @@ -272,7 +275,7 @@ impl DataflowOpTrait for CallIndirect {

/// Load a static constant in to the local dataflow graph.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[cfg_attr(test, derive(proptest_derive::Arbitrary))]
#[cfg_attr(test, derive(Arbitrary))]
pub struct LoadConstant {
/// Constant type
pub datatype: Type,
Expand Down Expand Up @@ -323,7 +326,7 @@ impl LoadConstant {

/// Load a static function in to the local dataflow graph.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[cfg_attr(test, derive(proptest_derive::Arbitrary))]
#[cfg_attr(test, derive(Arbitrary))]
pub struct LoadFunction {
/// Signature of the function
func_sig: PolyFuncType,
Expand Down Expand Up @@ -410,7 +413,7 @@ pub trait DataflowParent {

/// A simply nested dataflow graph.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[cfg_attr(test, derive(proptest_derive::Arbitrary))]
#[cfg_attr(test, derive(Arbitrary))]
pub struct DFG {
/// Signature of DFG node
pub signature: FunctionType,
Expand Down
21 changes: 13 additions & 8 deletions hugr/src/ops/module.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
//! Module-level operations
use smol_str::SmolStr;
#[cfg(test)]
use {
crate::proptest::{any_nonempty_smolstr, any_nonempty_string},
::proptest_derive::Arbitrary,
};

use crate::types::{EdgeKind, FunctionType, PolyFuncType};
use crate::types::{Type, TypeBound};
Expand All @@ -11,7 +16,7 @@ use super::{impl_op_name, OpTag, OpTrait};

/// The root of a module, parent of all other `OpType`s.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[cfg_attr(test, derive(proptest_derive::Arbitrary))]
#[cfg_attr(test, derive(Arbitrary))]
pub struct Module;

impl_op_name!(Module);
Expand All @@ -34,10 +39,10 @@ impl OpTrait for Module {
///
/// Children nodes are the body of the definition.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[cfg_attr(test, derive(proptest_derive::Arbitrary))]
#[cfg_attr(test, derive(Arbitrary))]
pub struct FuncDefn {
/// Name of function
#[cfg_attr(test, proptest(strategy = "crate::proptest::any_nonempty_string()"))]
#[cfg_attr(test, proptest(strategy = "any_nonempty_string()"))]
pub name: String,
/// Signature of the function
pub signature: PolyFuncType,
Expand Down Expand Up @@ -70,10 +75,10 @@ impl OpTrait for FuncDefn {

/// External function declaration, linked at runtime.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[cfg_attr(test, derive(proptest_derive::Arbitrary))]
#[cfg_attr(test, derive(Arbitrary))]
pub struct FuncDecl {
/// Name of function
#[cfg_attr(test, proptest(strategy = "crate::proptest::any_nonempty_string()"))]
#[cfg_attr(test, proptest(strategy = "any_nonempty_string()"))]
pub name: String,
/// Signature of the function
pub signature: PolyFuncType,
Expand All @@ -100,10 +105,10 @@ impl OpTrait for FuncDecl {

/// A type alias definition, used only for debug/metadata.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[cfg_attr(test, derive(proptest_derive::Arbitrary))]
#[cfg_attr(test, derive(Arbitrary))]
pub struct AliasDefn {
/// Alias name
#[cfg_attr(test, proptest(strategy = "crate::proptest::any_nonempty_smolstr()"))]
#[cfg_attr(test, proptest(strategy = "any_nonempty_smolstr()"))]
pub name: SmolStr,
/// Aliased type
pub definition: Type,
Expand All @@ -127,7 +132,7 @@ impl OpTrait for AliasDefn {
#[cfg_attr(test, derive(proptest_derive::Arbitrary))]
pub struct AliasDecl {
/// Alias name
#[cfg_attr(test, proptest(strategy = "crate::proptest::any_nonempty_smolstr()"))]
#[cfg_attr(test, proptest(strategy = "any_nonempty_smolstr()"))]
pub name: SmolStr,
/// Flag to signify type is classical
pub bound: TypeBound,
Expand Down
10 changes: 3 additions & 7 deletions hugr/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use itertools::FoldWhile::{Continue, Done};
use itertools::{repeat_n, Itertools};
use serde::{Deserialize, Serialize};
#[cfg(test)]
use {crate::proptest::RecursionDepth, ::proptest::prelude::*};
use {crate::proptest::RecursionDepth, ::proptest::prelude::*, proptest_derive::Arbitrary};

use crate::extension::{ExtensionRegistry, SignatureError};
use crate::ops::AliasDecl;
Expand Down Expand Up @@ -72,7 +72,7 @@ impl EdgeKind {
#[derive(
Copy, Default, Clone, PartialEq, Eq, Hash, Debug, derive_more::Display, Serialize, Deserialize,
)]
#[cfg_attr(test, derive(proptest_derive::Arbitrary))]
#[cfg_attr(test, derive(Arbitrary))]
/// Bounds on the valid operations on a type in a HUGR program.
pub enum TypeBound {
/// The equality operation is valid on this type.
Expand Down Expand Up @@ -197,11 +197,7 @@ impl From<SumType> for Type {
}

#[derive(Clone, PartialEq, Debug, Eq, derive_more::Display)]
#[cfg_attr(
test,
derive(proptest_derive::Arbitrary),
proptest(params = "RecursionDepth")
)]
#[cfg_attr(test, derive(Arbitrary), proptest(params = "RecursionDepth"))]
/// Core types
pub enum TypeEnum {
// TODO optimise with Box<CustomType> ?
Expand Down
7 changes: 2 additions & 5 deletions hugr/src/types/poly_func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use itertools::Itertools;
use {
crate::proptest::RecursionDepth,
::proptest::{collection::vec, prelude::*},
proptest_derive::Arbitrary,
};

use super::type_param::{check_type_args, TypeArg, TypeParam};
Expand All @@ -20,11 +21,7 @@ use super::{FunctionType, Substitution};
#[derive(
Clone, PartialEq, Debug, Default, Eq, derive_more::Display, serde::Serialize, serde::Deserialize,
)]
#[cfg_attr(
test,
derive(proptest_derive::Arbitrary),
proptest(params = "RecursionDepth")
)]
#[cfg_attr(test, derive(Arbitrary), proptest(params = "RecursionDepth"))]
#[display(
fmt = "forall {}. {}",
"params.iter().map(ToString::to_string).join(\" \")",
Expand Down
8 changes: 2 additions & 6 deletions hugr/src/types/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,10 @@ use crate::extension::{ExtensionRegistry, ExtensionSet, SignatureError};
use crate::{Direction, IncomingPort, OutgoingPort, Port};

#[cfg(test)]
use {crate::proptest::RecursionDepth, ::proptest::prelude::*};
use {crate::proptest::RecursionDepth, ::proptest::prelude::*, proptest_derive::Arbitrary};

#[derive(Clone, Debug, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[cfg_attr(
test,
derive(proptest_derive::Arbitrary),
proptest(params = "RecursionDepth")
)]
#[cfg_attr(test, derive(Arbitrary), proptest(params = "RecursionDepth"))]
/// Describes the edges required to/from a node, and thus, also the type of a [Graph].
/// This includes both the concept of "signature" in the spec,
/// and also the target (value) of a call (static).
Expand Down
12 changes: 7 additions & 5 deletions hugr/src/types/type_param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
//! [`TypeDef`]: crate::extension::TypeDef
use itertools::Itertools;
#[cfg(test)]
use proptest_derive::Arbitrary;
use std::num::NonZeroU64;
use thiserror::Error;

Expand All @@ -20,7 +22,7 @@ use super::{check_typevar_decl, CustomType, Substitution, Type, TypeBound};
Clone, Debug, PartialEq, Eq, derive_more::Display, serde::Deserialize, serde::Serialize,
)]
#[display(fmt = "{}", "_0.map(|i|i.to_string()).unwrap_or(\"-\".to_string())")]
#[cfg_attr(test, derive(proptest_derive::Arbitrary))]
#[cfg_attr(test, derive(Arbitrary))]
pub struct UpperBound(Option<NonZeroU64>);
impl UpperBound {
fn valid_value(&self, val: u64) -> bool {
Expand Down Expand Up @@ -439,8 +441,8 @@ mod test {
type Parameters = RecursionDepth;
type Strategy = BoxedStrategy<Self>;
fn arbitrary_with(depth: Self::Parameters) -> Self::Strategy {
use proptest::collection::vec;
use proptest::strategy::Union;
use prop::collection::vec;
use prop::strategy::Union;
let mut strat = Union::new([
Just(Self::Extensions).boxed(),
any::<TypeBound>().prop_map(|b| Self::Type { b }).boxed(),
Expand Down Expand Up @@ -470,8 +472,8 @@ mod test {
type Parameters = RecursionDepth;
type Strategy = BoxedStrategy<Self>;
fn arbitrary_with(depth: Self::Parameters) -> Self::Strategy {
use proptest::collection::vec;
use proptest::strategy::Union;
use prop::collection::vec;
use prop::strategy::Union;
let mut strat = Union::new([
any::<u64>().prop_map(|n| Self::BoundedNat { n }).boxed(),
any::<ExtensionSet>()
Expand Down
4 changes: 2 additions & 2 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ setup:

# Run the pre-commit checks.
check:
poetry run pre-commit run --all-files
HUGR_TEST_SCHEMA=1 poetry run pre-commit run --all-files

# Run all the tests.
test language="[rust|python]" : (_run_lang language \
"HUGR_TEST_SCHEMA=\"1\" cargo test --features extension_inference" \
"HUGR_TEST_SCHEMA=1 cargo test --all-features" \
"poetry run pytest"
)

Expand Down

0 comments on commit 5f3d891

Please sign in to comment.