Skip to content

Commit

Permalink
refactor!: rename HugrView function type methods + simplify behaviour
Browse files Browse the repository at this point in the history
Closes #957

- `mono_fn_type` now enforces function value monomorphism
- get_df_function_type -> inner_function_type so it matches the actual op function being called more obviously.
- The culprit `get_function_type` changed to `poly_func_type` which _only_ reports polymorphic typr from func defn/decl

refactor(hugr-core): rename `get_df_function_type`
BREAKING-CHANGE: rename `get_df_function_type` to `inner_function_type`

refactor(hugr-core): `get_function_type` replaced with `poly_func_type` which only reports polymorphic function type for Func(Defn/Decl) rooted HUGRs
  • Loading branch information
ss2165 committed Jul 5, 2024
1 parent df9b4cc commit 27802cb
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 20 deletions.
14 changes: 6 additions & 8 deletions hugr-core/src/hugr/views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,11 +332,10 @@ pub trait HugrView: HugrInternals {
/// signature corresponding to the input and output node of its sibling
/// graph. Otherwise, returns `None`.
///
/// In contrast to [`get_function_type`][HugrView::get_function_type], this
/// In contrast to [`poly_func_type`][HugrView::poly_func_type], this
/// method always return a concrete [`FunctionType`].
fn get_df_function_type(&self) -> Option<FunctionType> {
let op = self.get_optype(self.root());
op.inner_function_type()
fn inner_function_type(&self) -> Option<FunctionType> {
self.root_type().inner_function_type()
}

/// Returns the function type defined by this HUGR.
Expand All @@ -349,12 +348,11 @@ pub trait HugrView: HugrInternals {
/// of the function.
///
/// Otherwise, returns `None`.
fn get_function_type(&self) -> Option<PolyFuncType> {
let op = self.get_optype(self.root());
match op {
fn poly_func_type(&self) -> Option<PolyFuncType> {
match self.root_type() {
OpType::FuncDecl(decl) => Some(decl.signature.clone()),
OpType::FuncDefn(defn) => Some(defn.signature.clone()),
_ => op.inner_function_type().map(PolyFuncType::from),
_ => None,
}
}

Expand Down
6 changes: 3 additions & 3 deletions hugr-core/src/hugr/views/descendants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,13 +261,13 @@ pub(super) mod test {
assert_eq!(region.children(inner).count(), 2);

assert_eq!(
region.get_function_type(),
region.poly_func_type(),
Some(FunctionType::new_endo(type_row![NAT, QB]).into())
);
let inner_region: DescendantsGraph = DescendantsGraph::try_new(&hugr, inner)?;
assert_eq!(
inner_region.get_function_type(),
Some(FunctionType::new(type_row![NAT], type_row![NAT]).into())
inner_region.inner_function_type(),
Some(FunctionType::new(type_row![NAT], type_row![NAT]))
);

Ok(())
Expand Down
2 changes: 1 addition & 1 deletion hugr-core/src/hugr/views/sibling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ mod test {
fn flat_mut(mut simple_dfg_hugr: Hugr) {
simple_dfg_hugr.update_validate(&PRELUDE_REGISTRY).unwrap();
let root = simple_dfg_hugr.root();
let signature = simple_dfg_hugr.get_df_function_type().unwrap().clone();
let signature = simple_dfg_hugr.inner_function_type().unwrap().clone();

let sib_mut = SiblingMut::<CfgID>::try_new(&mut simple_dfg_hugr, root);
assert_eq!(
Expand Down
16 changes: 8 additions & 8 deletions hugr-core/src/ops/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,14 +326,14 @@ pub enum ConstTypeError {

/// Hugrs (even functions) inside Consts must be monomorphic
fn mono_fn_type(h: &Hugr) -> Result<FunctionType, ConstTypeError> {
if let Some(pf) = h.get_function_type() {
if let Ok(ft) = pf.try_into() {
return Ok(ft);
}
}
Err(ConstTypeError::NotMonomorphicFunction {
let err = || ConstTypeError::NotMonomorphicFunction {
hugr_root_type: h.root_type().clone(),
})
};
if let Some(pf) = h.poly_func_type() {
return pf.try_into().map_err(|_| err());
}

h.inner_function_type().ok_or_else(err)
}

impl Value {
Expand Down Expand Up @@ -447,7 +447,7 @@ impl Value {
match self {
Self::Extension { e } => format!("const:custom:{}", e.name()),
Self::Function { hugr: h } => {
let Some(t) = h.get_function_type() else {
let Ok(t) = mono_fn_type(h) else {
panic!("HUGR root node isn't a valid function parent.");
};
format!("const:function:[{}]", t)
Expand Down

0 comments on commit 27802cb

Please sign in to comment.