Skip to content

Commit

Permalink
fix!: Replace LoadFunction::signature with `LoadFunction::instantia…
Browse files Browse the repository at this point in the history
…tion` (#1756)

Closes #1755

This change breaks serialisation. We will already break serialisation
with the next release, so we judge this to be acceptable.

BREAKING CHANGE: The `LoadFunction::signature` field is removed. Replace
uses with `DataflowOpTrait::signature()`.
  • Loading branch information
doug-q authored Dec 11, 2024
1 parent 34ce691 commit 5b50d1d
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 28 deletions.
2 changes: 1 addition & 1 deletion hugr-core/src/extension/resolution/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub(crate) fn collect_op_types_extensions(
OpType::LoadConstant(lc) => collect_type_exts(&lc.datatype, &mut used, &mut missing),
OpType::LoadFunction(lf) => {
collect_signature_exts(lf.func_sig.body(), &mut used, &mut missing);
collect_signature_exts(&lf.signature, &mut used, &mut missing);
collect_signature_exts(&lf.instantiation, &mut used, &mut missing);
}
OpType::DFG(dfg) => collect_signature_exts(&dfg.signature, &mut used, &mut missing),
OpType::OpaqueOp(op) => {
Expand Down
2 changes: 1 addition & 1 deletion hugr-core/src/extension/resolution/types_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pub fn resolve_op_types_extensions(
}
OpType::LoadFunction(lf) => {
resolve_signature_exts(node, lf.func_sig.body_mut(), extensions, used_extensions)?;
resolve_signature_exts(node, &mut lf.signature, extensions, used_extensions)?;
resolve_signature_exts(node, &mut lf.instantiation, extensions, used_extensions)?;
}
OpType::DFG(dfg) => {
resolve_signature_exts(node, &mut dfg.signature, extensions, used_extensions)?
Expand Down
20 changes: 11 additions & 9 deletions hugr-core/src/ops/dataflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ use super::{impl_op_name, OpTag, OpTrait};
use crate::extension::{ExtensionRegistry, ExtensionSet, SignatureError};
use crate::ops::StaticTag;
use crate::types::{EdgeKind, PolyFuncType, Signature, Type, TypeArg, TypeRow};
use crate::IncomingPort;
use crate::{type_row, IncomingPort};

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

/// Trait implemented by all dataflow operations.
pub trait DataflowOpTrait {
Expand Down Expand Up @@ -347,7 +347,7 @@ pub struct LoadFunction {
/// The type arguments that instantiate `func_sig`.
pub type_args: Vec<TypeArg>,
/// The instantiation of `func_sig`.
pub signature: Signature, // Cache, so we can fail in try_new() not in signature()
pub instantiation: Signature, // Cache, so we can fail in try_new() not in signature()
}
impl_op_name!(LoadFunction);
impl DataflowOpTrait for LoadFunction {
Expand All @@ -358,7 +358,10 @@ impl DataflowOpTrait for LoadFunction {
}

fn signature(&self) -> Cow<'_, Signature> {
Cow::Borrowed(&self.signature)
Cow::Owned(Signature::new(
type_row![],
Type::new_function(self.instantiation.clone()),
))
}

fn static_input(&self) -> Option<EdgeKind> {
Expand All @@ -377,11 +380,10 @@ impl LoadFunction {
) -> Result<Self, SignatureError> {
let type_args: Vec<_> = type_args.into();
let instantiation = func_sig.instantiate(&type_args, exts)?;
let signature = Signature::new(TypeRow::new(), vec![Type::new_function(instantiation)]);
Ok(Self {
func_sig,
type_args,
signature,
instantiation,
})
}

Expand Down Expand Up @@ -410,12 +412,12 @@ impl LoadFunction {
self.type_args.clone(),
extension_registry,
)?;
if other.signature == self.signature {
if other.instantiation == self.instantiation {
Ok(())
} else {
Err(SignatureError::LoadFunctionIncorrectlyAppliesType {
cached: self.signature.clone(),
expected: other.signature.clone(),
cached: self.instantiation.clone(),
expected: other.instantiation.clone(),
})
}
}
Expand Down
10 changes: 2 additions & 8 deletions hugr-py/src/hugr/_serialization/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,18 +367,12 @@ class LoadFunction(DataflowOp):
op: Literal["LoadFunction"] = "LoadFunction"
func_sig: PolyFuncType
type_args: list[stys.TypeArg]
signature: FunctionType
instantiation: FunctionType

def deserialize(self) -> ops.LoadFunc:
signature = self.signature.deserialize()
assert len(signature.input) == 0
(f_ty,) = signature.output
assert isinstance(
f_ty, tys.FunctionType
), "Expected single function type output"
return ops.LoadFunc(
self.func_sig.deserialize(),
f_ty,
self.instantiation.deserialize(),
deser_it(self.type_args),
)

Expand Down
2 changes: 1 addition & 1 deletion hugr-py/src/hugr/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -1237,7 +1237,7 @@ def _to_serial(self, parent: Node) -> sops.LoadFunction:
parent=parent.idx,
func_sig=self.signature._to_serial(),
type_args=ser_it(self.type_args),
signature=self.outer_signature()._to_serial(),
instantiation=self.instantiation._to_serial(),
)

def outer_signature(self) -> tys.FunctionType:
Expand Down
4 changes: 2 additions & 2 deletions specification/schema/hugr_schema_live.json
Original file line number Diff line number Diff line change
Expand Up @@ -1054,15 +1054,15 @@
"title": "Type Args",
"type": "array"
},
"signature": {
"instantiation": {
"$ref": "#/$defs/FunctionType"
}
},
"required": [
"parent",
"func_sig",
"type_args",
"signature"
"instantiation"
],
"title": "LoadFunction",
"type": "object"
Expand Down
4 changes: 2 additions & 2 deletions specification/schema/hugr_schema_strict_live.json
Original file line number Diff line number Diff line change
Expand Up @@ -1054,15 +1054,15 @@
"title": "Type Args",
"type": "array"
},
"signature": {
"instantiation": {
"$ref": "#/$defs/FunctionType"
}
},
"required": [
"parent",
"func_sig",
"type_args",
"signature"
"instantiation"
],
"title": "LoadFunction",
"type": "object"
Expand Down
4 changes: 2 additions & 2 deletions specification/schema/testing_hugr_schema_live.json
Original file line number Diff line number Diff line change
Expand Up @@ -1054,15 +1054,15 @@
"title": "Type Args",
"type": "array"
},
"signature": {
"instantiation": {
"$ref": "#/$defs/FunctionType"
}
},
"required": [
"parent",
"func_sig",
"type_args",
"signature"
"instantiation"
],
"title": "LoadFunction",
"type": "object"
Expand Down
4 changes: 2 additions & 2 deletions specification/schema/testing_hugr_schema_strict_live.json
Original file line number Diff line number Diff line change
Expand Up @@ -1054,15 +1054,15 @@
"title": "Type Args",
"type": "array"
},
"signature": {
"instantiation": {
"$ref": "#/$defs/FunctionType"
}
},
"required": [
"parent",
"func_sig",
"type_args",
"signature"
"instantiation"
],
"title": "LoadFunction",
"type": "object"
Expand Down

0 comments on commit 5b50d1d

Please sign in to comment.