Skip to content

Commit

Permalink
refactor: rename some things to make them clearer
Browse files Browse the repository at this point in the history
  • Loading branch information
ss2165 committed Nov 21, 2023
1 parent a657c19 commit 4966e70
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ mod infer;
pub use infer::{infer_extensions, ExtensionSolution, InferExtensionError};

mod op_def;
pub use op_def::{CustomFunc, CustomValidate, OpDef};
pub use op_def::{CustomSignatureFunc, CustomValidator, OpDef};
mod type_def;
pub use type_def::{TypeDef, TypeDefBound};
pub mod prelude;
Expand Down
42 changes: 21 additions & 21 deletions src/extension/op_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::types::type_param::{check_type_args, TypeArg, TypeParam};
use crate::types::{FunctionType, PolyFuncType};
use crate::Hugr;

trait CustomSignatureFunc: Send + Sync {
trait ComputeSignature: Send + Sync {
/// Compute signature of node given the operation name,
/// values for the type parameters,
/// and 'misc' data from the extension definition YAML
Expand All @@ -31,7 +31,7 @@ trait CustomSignatureFunc: Send + Sync {

// Note this is very much a utility, rather than definitive;
// one can only do so much without the ExtensionRegistry!
impl<F, R: Into<PolyFuncType>> CustomSignatureFunc for F
impl<F, R: Into<PolyFuncType>> ComputeSignature for F
where
F: Fn(&[TypeArg]) -> Result<R, SignatureError> + Send + Sync,
{
Expand All @@ -46,7 +46,7 @@ where
}
}

trait CustomValidateFunc: Send + Sync {
trait ValidateSignature: Send + Sync {
/// Compute signature of node given the operation name,
/// values for the type parameters,
/// and 'misc' data from the extension definition YAML
Expand All @@ -61,7 +61,7 @@ trait CustomValidateFunc: Send + Sync {

// Note this is very much a utility, rather than definitive;
// one can only do so much without the ExtensionRegistry!
impl<F> CustomValidateFunc for F
impl<F> ValidateSignature for F
where
F: Fn(&[TypeArg]) -> Result<(), SignatureError> + Send + Sync,
{
Expand Down Expand Up @@ -99,14 +99,14 @@ pub trait CustomLowerFunc: Send + Sync {
) -> Option<Hugr>;
}

pub struct CustomFunc {
pub struct CustomSignatureFunc {
/// Type parameters passed to [func]. (The returned [PolyFuncType]
/// may require further type parameters, not declared here.)
static_params: Vec<TypeParam>,
func: Box<dyn CustomSignatureFunc>,
func: Box<dyn ComputeSignature>,
}

impl CustomFunc {
impl CustomSignatureFunc {
pub fn from_closure<F, R>(static_params: impl Into<Vec<TypeParam>>, func: F) -> Self
where
R: Into<PolyFuncType>,
Expand All @@ -120,14 +120,14 @@ impl CustomFunc {
}

#[derive(serde::Deserialize, serde::Serialize)]
pub struct CustomValidate {
pub struct CustomValidator {
#[serde(flatten)]
poly_func: PolyFuncType,
#[serde(skip)]
validate: Box<dyn CustomValidateFunc>,
validate: Box<dyn ValidateSignature>,
}

impl CustomValidate {
impl CustomValidator {
pub fn from_polyfunc(poly_func: impl Into<PolyFuncType>) -> Self {
Self {
poly_func: poly_func.into(),
Expand All @@ -136,7 +136,7 @@ impl CustomValidate {
}
pub fn new_with_validator(
poly_func: impl Into<PolyFuncType>,
validate: impl CustomValidateFunc + 'static,
validate: impl ValidateSignature + 'static,
) -> Self {
Self {
poly_func: poly_func.into(),
Expand All @@ -152,37 +152,37 @@ pub enum SignatureFunc {
// CustomSignatureFunc trait too, and replace this enum with Box<dyn CustomSignatureFunc>.
// However instead we treat all CustomFunc's as non-serializable.
#[serde(rename = "signature")]
TypeScheme(CustomValidate),
TypeScheme(CustomValidator),
#[serde(skip)]
CustomFunc(CustomFunc),
CustomFunc(CustomSignatureFunc),
}

impl Default for Box<dyn CustomValidateFunc> {
impl Default for Box<dyn ValidateSignature> {
fn default() -> Self {
Box::new(|&_: &_| Ok(()))
}
}

impl<T: Into<CustomFunc>> From<T> for SignatureFunc {
impl<T: Into<CustomSignatureFunc>> From<T> for SignatureFunc {
fn from(v: T) -> Self {
Self::CustomFunc(v.into())
}
}

impl From<PolyFuncType> for SignatureFunc {
fn from(v: PolyFuncType) -> Self {
Self::TypeScheme(CustomValidate::from_polyfunc(v))
Self::TypeScheme(CustomValidator::from_polyfunc(v))
}
}

impl From<FunctionType> for SignatureFunc {
fn from(v: FunctionType) -> Self {
Self::TypeScheme(CustomValidate::from_polyfunc(v))
Self::TypeScheme(CustomValidator::from_polyfunc(v))
}
}

impl From<CustomValidate> for SignatureFunc {
fn from(v: CustomValidate) -> Self {
impl From<CustomValidator> for SignatureFunc {
fn from(v: CustomValidator) -> Self {
Self::TypeScheme(v)
}
}
Expand Down Expand Up @@ -404,8 +404,8 @@ impl Extension {
}
}

/// Create an OpDef with custom binary code to compute the type scheme
/// (which may be polymorphic); and no "misc" or "lowering functions" defined.
/// Create an OpDef with `PolyFuncType`, `CustomSignatureFunc` or `CustomValidator`
/// ; and no "misc" or "lowering functions" defined.
pub fn add_op_simple(
&mut self,
name: SmolStr,
Expand Down
4 changes: 2 additions & 2 deletions src/extension/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use lazy_static::lazy_static;
use smol_str::SmolStr;

use crate::{
extension::{op_def::CustomFunc, ExtensionId, TypeDefBound},
extension::{op_def::CustomSignatureFunc, ExtensionId, TypeDefBound},
ops::LeafOp,
types::{
type_param::{TypeArg, TypeParam},
Expand Down Expand Up @@ -44,7 +44,7 @@ lazy_static! {
.add_op_simple(
SmolStr::new_inline(NEW_ARRAY_OP_ID),
"Create a new array from elements".to_string(),
CustomFunc::from_closure(vec![TypeParam::Type(TypeBound::Any), TypeParam::max_nat()],
CustomSignatureFunc::from_closure(vec![TypeParam::Type(TypeBound::Any), TypeParam::max_nat()],
|args: &[TypeArg]| {
let [TypeArg::Type { ty }, TypeArg::BoundedNat { n }] = args else {
panic!("should have been checked already.")
Expand Down
10 changes: 5 additions & 5 deletions src/std_extensions/arithmetic/int_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use super::int_types::{get_log_width, int_type_var, LOG_WIDTH_TYPE_PARAM};
use crate::extension::prelude::{sum_with_error, BOOL_T};
use crate::extension::CustomValidate;
use crate::extension::CustomValidator;
use crate::type_row;
use crate::types::{FunctionType, PolyFuncType};
use crate::utils::collect_array;
Expand Down Expand Up @@ -118,30 +118,30 @@ fn extension() -> Extension {
.add_op_simple(
"iwiden_u".into(),
"widen an unsigned integer to a wider one with the same value".to_owned(),
CustomValidate::new_with_validator(widen_poly.clone(), iwiden_sig_validate),
CustomValidator::new_with_validator(widen_poly.clone(), iwiden_sig_validate),
)
.unwrap();

extension
.add_op_simple(
"iwiden_s".into(),
"widen a signed integer to a wider one with the same value".to_owned(),
CustomValidate::new_with_validator(widen_poly, iwiden_sig_validate),
CustomValidator::new_with_validator(widen_poly, iwiden_sig_validate),
)
.unwrap();
extension
.add_op_simple(
"inarrow_u".into(),
"narrow an unsigned integer to a narrower one with the same value if possible"
.to_owned(),
CustomValidate::new_with_validator(narrow_poly.clone(), inarrow_sig_validate),
CustomValidator::new_with_validator(narrow_poly.clone(), inarrow_sig_validate),
)
.unwrap();
extension
.add_op_simple(
"inarrow_s".into(),
"narrow a signed integer to a narrower one with the same value if possible".to_owned(),
CustomValidate::new_with_validator(narrow_poly, inarrow_sig_validate),
CustomValidator::new_with_validator(narrow_poly, inarrow_sig_validate),
)
.unwrap();
extension
Expand Down
6 changes: 3 additions & 3 deletions src/std_extensions/logic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use itertools::Itertools;
use smol_str::SmolStr;

use crate::{
extension::{prelude::BOOL_T, CustomFunc, ExtensionId},
extension::{prelude::BOOL_T, CustomSignatureFunc, ExtensionId},
ops, type_row,
types::{
type_param::{TypeArg, TypeParam},
Expand Down Expand Up @@ -45,7 +45,7 @@ fn extension() -> Extension {
.add_op_simple(
SmolStr::new_inline(AND_NAME),
"logical 'and'".into(),
CustomFunc::from_closure(vec![H_INT], |arg_values: &[TypeArg]| {
CustomSignatureFunc::from_closure(vec![H_INT], |arg_values: &[TypeArg]| {
let Ok(TypeArg::BoundedNat { n }) = arg_values.iter().exactly_one() else {
panic!("should be covered by validation.")
};
Expand All @@ -62,7 +62,7 @@ fn extension() -> Extension {
.add_op_simple(
SmolStr::new_inline(OR_NAME),
"logical 'or'".into(),
CustomFunc::from_closure(vec![H_INT], |arg_values: &[TypeArg]| {
CustomSignatureFunc::from_closure(vec![H_INT], |arg_values: &[TypeArg]| {
let Ok(TypeArg::BoundedNat { n }) = arg_values.iter().exactly_one() else {
panic!("should be covered by validation.")
};
Expand Down

0 comments on commit 4966e70

Please sign in to comment.