From a61f4df66cb6ce11b342103af705145441ea9b5c Mon Sep 17 00:00:00 2001 From: Seyon Sivarajah Date: Wed, 21 Aug 2024 17:33:11 +0100 Subject: [PATCH] feat(hugr-py): use serialized extensions in python (#1459) Copies standard extensions to hugr-py source tree, as poetry won't include anything from outside the tree (not symlinks either). Existing op/type wrappers now just point to loaded extensions. Closes #1450 --- hugr-py/src/hugr/ops.py | 9 +- hugr-py/src/hugr/serialization/extension.py | 2 +- hugr-py/src/hugr/std/__init__.py | 12 + hugr-py/src/hugr/std/_json_defs/README.md | 2 + .../_json_defs/arithmetic/conversions.json | 222 ++ .../hugr/std/_json_defs/arithmetic/float.json | 593 ++++ .../_json_defs/arithmetic/float/types.json | 19 + .../hugr/std/_json_defs/arithmetic/int.json | 3142 +++++++++++++++++ .../std/_json_defs/arithmetic/int/types.json | 24 + .../src/hugr/std/_json_defs/collections.json | 143 + hugr-py/src/hugr/std/_json_defs/logic.json | 155 + hugr-py/src/hugr/std/_json_defs/prelude.json | 107 + hugr-py/src/hugr/std/_json_defs/ptr.json | 150 + hugr-py/src/hugr/std/float.py | 20 +- hugr-py/src/hugr/std/int.py | 27 +- hugr-py/src/hugr/std/logic.py | 13 +- justfile | 2 +- specification/schema/hugr_schema_live.json | 3 +- .../schema/hugr_schema_strict_live.json | 3 +- .../schema/testing_hugr_schema_live.json | 3 +- .../testing_hugr_schema_strict_live.json | 3 +- 21 files changed, 4602 insertions(+), 52 deletions(-) create mode 100644 hugr-py/src/hugr/std/_json_defs/README.md create mode 100644 hugr-py/src/hugr/std/_json_defs/arithmetic/conversions.json create mode 100644 hugr-py/src/hugr/std/_json_defs/arithmetic/float.json create mode 100644 hugr-py/src/hugr/std/_json_defs/arithmetic/float/types.json create mode 100644 hugr-py/src/hugr/std/_json_defs/arithmetic/int.json create mode 100644 hugr-py/src/hugr/std/_json_defs/arithmetic/int/types.json create mode 100644 hugr-py/src/hugr/std/_json_defs/collections.json create mode 100644 hugr-py/src/hugr/std/_json_defs/logic.json create mode 100644 hugr-py/src/hugr/std/_json_defs/prelude.json create mode 100644 hugr-py/src/hugr/std/_json_defs/ptr.json diff --git a/hugr-py/src/hugr/ops.py b/hugr-py/src/hugr/ops.py index 809696095..e00148eb8 100644 --- a/hugr-py/src/hugr/ops.py +++ b/hugr-py/src/hugr/ops.py @@ -4,7 +4,7 @@ from dataclasses import dataclass, field from functools import cached_property -from typing import TYPE_CHECKING, Protocol, TypeVar, runtime_checkable +from typing import TYPE_CHECKING, ClassVar, Protocol, TypeVar, runtime_checkable from typing_extensions import Self @@ -398,11 +398,12 @@ class RegisteredOp(AsExtOp): """ #: Known operation definition. - const_op_def: ext.OpDef # must be initialised by register_op + const_op_def: ClassVar[ext.OpDef] # may be set by registered_op decorator. - def op_def(self) -> ext.OpDef: + @classmethod + def op_def(cls) -> ext.OpDef: # override for AsExtOp.op_def - return self.const_op_def + return cls.const_op_def @dataclass() diff --git a/hugr-py/src/hugr/serialization/extension.py b/hugr-py/src/hugr/serialization/extension.py index 7ce2ef481..291f7e5c8 100644 --- a/hugr-py/src/hugr/serialization/extension.py +++ b/hugr-py/src/hugr/serialization/extension.py @@ -97,7 +97,7 @@ class OpDef(ConfiguredBaseModel, populate_by_name=True): misc: dict[str, Any] | None = None signature: PolyFuncType | None = None binary: bool = False - lower_funcs: list[FixedHugr] + lower_funcs: list[FixedHugr] = pd.Field(default_factory=list) def deserialize(self, extension: ext.Extension) -> ext.OpDef: return extension.add_op_def( diff --git a/hugr-py/src/hugr/std/__init__.py b/hugr-py/src/hugr/std/__init__.py index f01d3d421..f3dd70167 100644 --- a/hugr-py/src/hugr/std/__init__.py +++ b/hugr-py/src/hugr/std/__init__.py @@ -1 +1,13 @@ """Types and operations from the standard extension set.""" + +import pkgutil + +from hugr.ext import Extension +from hugr.serialization.extension import Extension as PdExtension + + +def _load_extension(name: str) -> Extension: + replacement = name.replace(".", "/") + json_str = pkgutil.get_data(__name__, f"_json_defs/{replacement}.json") + assert json_str is not None + return PdExtension.model_validate_json(json_str).deserialize() diff --git a/hugr-py/src/hugr/std/_json_defs/README.md b/hugr-py/src/hugr/std/_json_defs/README.md new file mode 100644 index 000000000..af064f00f --- /dev/null +++ b/hugr-py/src/hugr/std/_json_defs/README.md @@ -0,0 +1,2 @@ +The files in this directory are generated and copied in, don't edit by hand. See +repository justfile `gen-extensions` command. diff --git a/hugr-py/src/hugr/std/_json_defs/arithmetic/conversions.json b/hugr-py/src/hugr/std/_json_defs/arithmetic/conversions.json new file mode 100644 index 000000000..c23cba7c6 --- /dev/null +++ b/hugr-py/src/hugr/std/_json_defs/arithmetic/conversions.json @@ -0,0 +1,222 @@ +{ + "version": "0.1.0", + "name": "arithmetic.conversions", + "extension_reqs": [ + "arithmetic.float.types", + "arithmetic.int.types" + ], + "types": {}, + "values": {}, + "operations": { + "convert_s": { + "extension": "arithmetic.conversions", + "name": "convert_s", + "description": "signed int to float", + "signature": { + "params": [ + { + "tp": "BoundedNat", + "bound": 7 + } + ], + "body": { + "input": [ + { + "t": "Opaque", + "extension": "arithmetic.int.types", + "id": "int", + "args": [ + { + "tya": "Variable", + "idx": 0, + "cached_decl": { + "tp": "BoundedNat", + "bound": 7 + } + } + ], + "bound": "C" + } + ], + "output": [ + { + "t": "Opaque", + "extension": "arithmetic.float.types", + "id": "float64", + "args": [], + "bound": "C" + } + ], + "extension_reqs": [] + } + }, + "binary": false + }, + "convert_u": { + "extension": "arithmetic.conversions", + "name": "convert_u", + "description": "unsigned int to float", + "signature": { + "params": [ + { + "tp": "BoundedNat", + "bound": 7 + } + ], + "body": { + "input": [ + { + "t": "Opaque", + "extension": "arithmetic.int.types", + "id": "int", + "args": [ + { + "tya": "Variable", + "idx": 0, + "cached_decl": { + "tp": "BoundedNat", + "bound": 7 + } + } + ], + "bound": "C" + } + ], + "output": [ + { + "t": "Opaque", + "extension": "arithmetic.float.types", + "id": "float64", + "args": [], + "bound": "C" + } + ], + "extension_reqs": [] + } + }, + "binary": false + }, + "trunc_s": { + "extension": "arithmetic.conversions", + "name": "trunc_s", + "description": "float to signed int", + "signature": { + "params": [ + { + "tp": "BoundedNat", + "bound": 7 + } + ], + "body": { + "input": [ + { + "t": "Opaque", + "extension": "arithmetic.float.types", + "id": "float64", + "args": [], + "bound": "C" + } + ], + "output": [ + { + "t": "Sum", + "s": "General", + "rows": [ + [ + { + "t": "Opaque", + "extension": "arithmetic.int.types", + "id": "int", + "args": [ + { + "tya": "Variable", + "idx": 0, + "cached_decl": { + "tp": "BoundedNat", + "bound": 7 + } + } + ], + "bound": "C" + } + ], + [ + { + "t": "Opaque", + "extension": "prelude", + "id": "error", + "args": [], + "bound": "C" + } + ] + ] + } + ], + "extension_reqs": [] + } + }, + "binary": false + }, + "trunc_u": { + "extension": "arithmetic.conversions", + "name": "trunc_u", + "description": "float to unsigned int", + "signature": { + "params": [ + { + "tp": "BoundedNat", + "bound": 7 + } + ], + "body": { + "input": [ + { + "t": "Opaque", + "extension": "arithmetic.float.types", + "id": "float64", + "args": [], + "bound": "C" + } + ], + "output": [ + { + "t": "Sum", + "s": "General", + "rows": [ + [ + { + "t": "Opaque", + "extension": "arithmetic.int.types", + "id": "int", + "args": [ + { + "tya": "Variable", + "idx": 0, + "cached_decl": { + "tp": "BoundedNat", + "bound": 7 + } + } + ], + "bound": "C" + } + ], + [ + { + "t": "Opaque", + "extension": "prelude", + "id": "error", + "args": [], + "bound": "C" + } + ] + ] + } + ], + "extension_reqs": [] + } + }, + "binary": false + } + } +} diff --git a/hugr-py/src/hugr/std/_json_defs/arithmetic/float.json b/hugr-py/src/hugr/std/_json_defs/arithmetic/float.json new file mode 100644 index 000000000..8563fe57b --- /dev/null +++ b/hugr-py/src/hugr/std/_json_defs/arithmetic/float.json @@ -0,0 +1,593 @@ +{ + "version": "0.1.0", + "name": "arithmetic.float", + "extension_reqs": [ + "arithmetic.int.types" + ], + "types": {}, + "values": {}, + "operations": { + "fabs": { + "extension": "arithmetic.float", + "name": "fabs", + "description": "absolute value", + "signature": { + "params": [], + "body": { + "input": [ + { + "t": "Opaque", + "extension": "arithmetic.float.types", + "id": "float64", + "args": [], + "bound": "C" + } + ], + "output": [ + { + "t": "Opaque", + "extension": "arithmetic.float.types", + "id": "float64", + "args": [], + "bound": "C" + } + ], + "extension_reqs": [] + } + }, + "binary": false + }, + "fadd": { + "extension": "arithmetic.float", + "name": "fadd", + "description": "addition", + "signature": { + "params": [], + "body": { + "input": [ + { + "t": "Opaque", + "extension": "arithmetic.float.types", + "id": "float64", + "args": [], + "bound": "C" + }, + { + "t": "Opaque", + "extension": "arithmetic.float.types", + "id": "float64", + "args": [], + "bound": "C" + } + ], + "output": [ + { + "t": "Opaque", + "extension": "arithmetic.float.types", + "id": "float64", + "args": [], + "bound": "C" + } + ], + "extension_reqs": [] + } + }, + "binary": false + }, + "fceil": { + "extension": "arithmetic.float", + "name": "fceil", + "description": "ceiling", + "signature": { + "params": [], + "body": { + "input": [ + { + "t": "Opaque", + "extension": "arithmetic.float.types", + "id": "float64", + "args": [], + "bound": "C" + } + ], + "output": [ + { + "t": "Opaque", + "extension": "arithmetic.float.types", + "id": "float64", + "args": [], + "bound": "C" + } + ], + "extension_reqs": [] + } + }, + "binary": false + }, + "fdiv": { + "extension": "arithmetic.float", + "name": "fdiv", + "description": "division", + "signature": { + "params": [], + "body": { + "input": [ + { + "t": "Opaque", + "extension": "arithmetic.float.types", + "id": "float64", + "args": [], + "bound": "C" + }, + { + "t": "Opaque", + "extension": "arithmetic.float.types", + "id": "float64", + "args": [], + "bound": "C" + } + ], + "output": [ + { + "t": "Opaque", + "extension": "arithmetic.float.types", + "id": "float64", + "args": [], + "bound": "C" + } + ], + "extension_reqs": [] + } + }, + "binary": false + }, + "feq": { + "extension": "arithmetic.float", + "name": "feq", + "description": "equality test", + "signature": { + "params": [], + "body": { + "input": [ + { + "t": "Opaque", + "extension": "arithmetic.float.types", + "id": "float64", + "args": [], + "bound": "C" + }, + { + "t": "Opaque", + "extension": "arithmetic.float.types", + "id": "float64", + "args": [], + "bound": "C" + } + ], + "output": [ + { + "t": "Sum", + "s": "Unit", + "size": 2 + } + ], + "extension_reqs": [] + } + }, + "binary": false + }, + "ffloor": { + "extension": "arithmetic.float", + "name": "ffloor", + "description": "floor", + "signature": { + "params": [], + "body": { + "input": [ + { + "t": "Opaque", + "extension": "arithmetic.float.types", + "id": "float64", + "args": [], + "bound": "C" + } + ], + "output": [ + { + "t": "Opaque", + "extension": "arithmetic.float.types", + "id": "float64", + "args": [], + "bound": "C" + } + ], + "extension_reqs": [] + } + }, + "binary": false + }, + "fge": { + "extension": "arithmetic.float", + "name": "fge", + "description": "\"greater than or equal\"", + "signature": { + "params": [], + "body": { + "input": [ + { + "t": "Opaque", + "extension": "arithmetic.float.types", + "id": "float64", + "args": [], + "bound": "C" + }, + { + "t": "Opaque", + "extension": "arithmetic.float.types", + "id": "float64", + "args": [], + "bound": "C" + } + ], + "output": [ + { + "t": "Sum", + "s": "Unit", + "size": 2 + } + ], + "extension_reqs": [] + } + }, + "binary": false + }, + "fgt": { + "extension": "arithmetic.float", + "name": "fgt", + "description": "\"greater than\"", + "signature": { + "params": [], + "body": { + "input": [ + { + "t": "Opaque", + "extension": "arithmetic.float.types", + "id": "float64", + "args": [], + "bound": "C" + }, + { + "t": "Opaque", + "extension": "arithmetic.float.types", + "id": "float64", + "args": [], + "bound": "C" + } + ], + "output": [ + { + "t": "Sum", + "s": "Unit", + "size": 2 + } + ], + "extension_reqs": [] + } + }, + "binary": false + }, + "fle": { + "extension": "arithmetic.float", + "name": "fle", + "description": "\"less than or equal\"", + "signature": { + "params": [], + "body": { + "input": [ + { + "t": "Opaque", + "extension": "arithmetic.float.types", + "id": "float64", + "args": [], + "bound": "C" + }, + { + "t": "Opaque", + "extension": "arithmetic.float.types", + "id": "float64", + "args": [], + "bound": "C" + } + ], + "output": [ + { + "t": "Sum", + "s": "Unit", + "size": 2 + } + ], + "extension_reqs": [] + } + }, + "binary": false + }, + "flt": { + "extension": "arithmetic.float", + "name": "flt", + "description": "\"less than\"", + "signature": { + "params": [], + "body": { + "input": [ + { + "t": "Opaque", + "extension": "arithmetic.float.types", + "id": "float64", + "args": [], + "bound": "C" + }, + { + "t": "Opaque", + "extension": "arithmetic.float.types", + "id": "float64", + "args": [], + "bound": "C" + } + ], + "output": [ + { + "t": "Sum", + "s": "Unit", + "size": 2 + } + ], + "extension_reqs": [] + } + }, + "binary": false + }, + "fmax": { + "extension": "arithmetic.float", + "name": "fmax", + "description": "maximum", + "signature": { + "params": [], + "body": { + "input": [ + { + "t": "Opaque", + "extension": "arithmetic.float.types", + "id": "float64", + "args": [], + "bound": "C" + }, + { + "t": "Opaque", + "extension": "arithmetic.float.types", + "id": "float64", + "args": [], + "bound": "C" + } + ], + "output": [ + { + "t": "Opaque", + "extension": "arithmetic.float.types", + "id": "float64", + "args": [], + "bound": "C" + } + ], + "extension_reqs": [] + } + }, + "binary": false + }, + "fmin": { + "extension": "arithmetic.float", + "name": "fmin", + "description": "minimum", + "signature": { + "params": [], + "body": { + "input": [ + { + "t": "Opaque", + "extension": "arithmetic.float.types", + "id": "float64", + "args": [], + "bound": "C" + }, + { + "t": "Opaque", + "extension": "arithmetic.float.types", + "id": "float64", + "args": [], + "bound": "C" + } + ], + "output": [ + { + "t": "Opaque", + "extension": "arithmetic.float.types", + "id": "float64", + "args": [], + "bound": "C" + } + ], + "extension_reqs": [] + } + }, + "binary": false + }, + "fmul": { + "extension": "arithmetic.float", + "name": "fmul", + "description": "multiplication", + "signature": { + "params": [], + "body": { + "input": [ + { + "t": "Opaque", + "extension": "arithmetic.float.types", + "id": "float64", + "args": [], + "bound": "C" + }, + { + "t": "Opaque", + "extension": "arithmetic.float.types", + "id": "float64", + "args": [], + "bound": "C" + } + ], + "output": [ + { + "t": "Opaque", + "extension": "arithmetic.float.types", + "id": "float64", + "args": [], + "bound": "C" + } + ], + "extension_reqs": [] + } + }, + "binary": false + }, + "fne": { + "extension": "arithmetic.float", + "name": "fne", + "description": "inequality test", + "signature": { + "params": [], + "body": { + "input": [ + { + "t": "Opaque", + "extension": "arithmetic.float.types", + "id": "float64", + "args": [], + "bound": "C" + }, + { + "t": "Opaque", + "extension": "arithmetic.float.types", + "id": "float64", + "args": [], + "bound": "C" + } + ], + "output": [ + { + "t": "Sum", + "s": "Unit", + "size": 2 + } + ], + "extension_reqs": [] + } + }, + "binary": false + }, + "fneg": { + "extension": "arithmetic.float", + "name": "fneg", + "description": "negation", + "signature": { + "params": [], + "body": { + "input": [ + { + "t": "Opaque", + "extension": "arithmetic.float.types", + "id": "float64", + "args": [], + "bound": "C" + } + ], + "output": [ + { + "t": "Opaque", + "extension": "arithmetic.float.types", + "id": "float64", + "args": [], + "bound": "C" + } + ], + "extension_reqs": [] + } + }, + "binary": false + }, + "fsub": { + "extension": "arithmetic.float", + "name": "fsub", + "description": "subtraction", + "signature": { + "params": [], + "body": { + "input": [ + { + "t": "Opaque", + "extension": "arithmetic.float.types", + "id": "float64", + "args": [], + "bound": "C" + }, + { + "t": "Opaque", + "extension": "arithmetic.float.types", + "id": "float64", + "args": [], + "bound": "C" + } + ], + "output": [ + { + "t": "Opaque", + "extension": "arithmetic.float.types", + "id": "float64", + "args": [], + "bound": "C" + } + ], + "extension_reqs": [] + } + }, + "binary": false + }, + "ftostring": { + "extension": "arithmetic.float", + "name": "ftostring", + "description": "string representation", + "signature": { + "params": [], + "body": { + "input": [ + { + "t": "Opaque", + "extension": "arithmetic.float.types", + "id": "float64", + "args": [], + "bound": "C" + } + ], + "output": [ + { + "t": "Opaque", + "extension": "prelude", + "id": "string", + "args": [], + "bound": "C" + } + ], + "extension_reqs": [] + } + }, + "binary": false + } + } +} diff --git a/hugr-py/src/hugr/std/_json_defs/arithmetic/float/types.json b/hugr-py/src/hugr/std/_json_defs/arithmetic/float/types.json new file mode 100644 index 000000000..d590a83f4 --- /dev/null +++ b/hugr-py/src/hugr/std/_json_defs/arithmetic/float/types.json @@ -0,0 +1,19 @@ +{ + "version": "0.1.0", + "name": "arithmetic.float.types", + "extension_reqs": [], + "types": { + "float64": { + "extension": "arithmetic.float.types", + "name": "float64", + "params": [], + "description": "64-bit IEEE 754-2019 floating-point value", + "bound": { + "b": "Explicit", + "bound": "C" + } + } + }, + "values": {}, + "operations": {} +} diff --git a/hugr-py/src/hugr/std/_json_defs/arithmetic/int.json b/hugr-py/src/hugr/std/_json_defs/arithmetic/int.json new file mode 100644 index 000000000..6eb546736 --- /dev/null +++ b/hugr-py/src/hugr/std/_json_defs/arithmetic/int.json @@ -0,0 +1,3142 @@ +{ + "version": "0.1.0", + "name": "arithmetic.int", + "extension_reqs": [ + "arithmetic.int.types" + ], + "types": {}, + "values": {}, + "operations": { + "iabs": { + "extension": "arithmetic.int", + "name": "iabs", + "description": "convert signed to unsigned by taking absolute value", + "signature": { + "params": [ + { + "tp": "BoundedNat", + "bound": 7 + } + ], + "body": { + "input": [ + { + "t": "Opaque", + "extension": "arithmetic.int.types", + "id": "int", + "args": [ + { + "tya": "Variable", + "idx": 0, + "cached_decl": { + "tp": "BoundedNat", + "bound": 7 + } + } + ], + "bound": "C" + } + ], + "output": [ + { + "t": "Opaque", + "extension": "arithmetic.int.types", + "id": "int", + "args": [ + { + "tya": "Variable", + "idx": 0, + "cached_decl": { + "tp": "BoundedNat", + "bound": 7 + } + } + ], + "bound": "C" + } + ], + "extension_reqs": [] + } + }, + "binary": false + }, + "iadd": { + "extension": "arithmetic.int", + "name": "iadd", + "description": "addition modulo 2^N (signed and unsigned versions are the same op)", + "signature": { + "params": [ + { + "tp": "BoundedNat", + "bound": 7 + } + ], + "body": { + "input": [ + { + "t": "Opaque", + "extension": "arithmetic.int.types", + "id": "int", + "args": [ + { + "tya": "Variable", + "idx": 0, + "cached_decl": { + "tp": "BoundedNat", + "bound": 7 + } + } + ], + "bound": "C" + }, + { + "t": "Opaque", + "extension": "arithmetic.int.types", + "id": "int", + "args": [ + { + "tya": "Variable", + "idx": 0, + "cached_decl": { + "tp": "BoundedNat", + "bound": 7 + } + } + ], + "bound": "C" + } + ], + "output": [ + { + "t": "Opaque", + "extension": "arithmetic.int.types", + "id": "int", + "args": [ + { + "tya": "Variable", + "idx": 0, + "cached_decl": { + "tp": "BoundedNat", + "bound": 7 + } + } + ], + "bound": "C" + } + ], + "extension_reqs": [] + } + }, + "binary": false + }, + "iand": { + "extension": "arithmetic.int", + "name": "iand", + "description": "bitwise AND", + "signature": { + "params": [ + { + "tp": "BoundedNat", + "bound": 7 + } + ], + "body": { + "input": [ + { + "t": "Opaque", + "extension": "arithmetic.int.types", + "id": "int", + "args": [ + { + "tya": "Variable", + "idx": 0, + "cached_decl": { + "tp": "BoundedNat", + "bound": 7 + } + } + ], + "bound": "C" + }, + { + "t": "Opaque", + "extension": "arithmetic.int.types", + "id": "int", + "args": [ + { + "tya": "Variable", + "idx": 0, + "cached_decl": { + "tp": "BoundedNat", + "bound": 7 + } + } + ], + "bound": "C" + } + ], + "output": [ + { + "t": "Opaque", + "extension": "arithmetic.int.types", + "id": "int", + "args": [ + { + "tya": "Variable", + "idx": 0, + "cached_decl": { + "tp": "BoundedNat", + "bound": 7 + } + } + ], + "bound": "C" + } + ], + "extension_reqs": [] + } + }, + "binary": false + }, + "idiv_checked_s": { + "extension": "arithmetic.int", + "name": "idiv_checked_s", + "description": "as idivmod_checked_s but discarding the second output", + "signature": { + "params": [ + { + "tp": "BoundedNat", + "bound": 7 + } + ], + "body": { + "input": [ + { + "t": "Opaque", + "extension": "arithmetic.int.types", + "id": "int", + "args": [ + { + "tya": "Variable", + "idx": 0, + "cached_decl": { + "tp": "BoundedNat", + "bound": 7 + } + } + ], + "bound": "C" + }, + { + "t": "Opaque", + "extension": "arithmetic.int.types", + "id": "int", + "args": [ + { + "tya": "Variable", + "idx": 0, + "cached_decl": { + "tp": "BoundedNat", + "bound": 7 + } + } + ], + "bound": "C" + } + ], + "output": [ + { + "t": "Sum", + "s": "General", + "rows": [ + [ + { + "t": "Opaque", + "extension": "arithmetic.int.types", + "id": "int", + "args": [ + { + "tya": "Variable", + "idx": 0, + "cached_decl": { + "tp": "BoundedNat", + "bound": 7 + } + } + ], + "bound": "C" + } + ], + [ + { + "t": "Opaque", + "extension": "prelude", + "id": "error", + "args": [], + "bound": "C" + } + ] + ] + } + ], + "extension_reqs": [] + } + }, + "binary": false + }, + "idiv_checked_u": { + "extension": "arithmetic.int", + "name": "idiv_checked_u", + "description": "as idivmod_checked_u but discarding the second output", + "signature": { + "params": [ + { + "tp": "BoundedNat", + "bound": 7 + } + ], + "body": { + "input": [ + { + "t": "Opaque", + "extension": "arithmetic.int.types", + "id": "int", + "args": [ + { + "tya": "Variable", + "idx": 0, + "cached_decl": { + "tp": "BoundedNat", + "bound": 7 + } + } + ], + "bound": "C" + }, + { + "t": "Opaque", + "extension": "arithmetic.int.types", + "id": "int", + "args": [ + { + "tya": "Variable", + "idx": 0, + "cached_decl": { + "tp": "BoundedNat", + "bound": 7 + } + } + ], + "bound": "C" + } + ], + "output": [ + { + "t": "Sum", + "s": "General", + "rows": [ + [ + { + "t": "Opaque", + "extension": "arithmetic.int.types", + "id": "int", + "args": [ + { + "tya": "Variable", + "idx": 0, + "cached_decl": { + "tp": "BoundedNat", + "bound": 7 + } + } + ], + "bound": "C" + } + ], + [ + { + "t": "Opaque", + "extension": "prelude", + "id": "error", + "args": [], + "bound": "C" + } + ] + ] + } + ], + "extension_reqs": [] + } + }, + "binary": false + }, + "idiv_s": { + "extension": "arithmetic.int", + "name": "idiv_s", + "description": "as idivmod_s but discarding the second output", + "signature": { + "params": [ + { + "tp": "BoundedNat", + "bound": 7 + } + ], + "body": { + "input": [ + { + "t": "Opaque", + "extension": "arithmetic.int.types", + "id": "int", + "args": [ + { + "tya": "Variable", + "idx": 0, + "cached_decl": { + "tp": "BoundedNat", + "bound": 7 + } + } + ], + "bound": "C" + }, + { + "t": "Opaque", + "extension": "arithmetic.int.types", + "id": "int", + "args": [ + { + "tya": "Variable", + "idx": 0, + "cached_decl": { + "tp": "BoundedNat", + "bound": 7 + } + } + ], + "bound": "C" + } + ], + "output": [ + { + "t": "Opaque", + "extension": "arithmetic.int.types", + "id": "int", + "args": [ + { + "tya": "Variable", + "idx": 0, + "cached_decl": { + "tp": "BoundedNat", + "bound": 7 + } + } + ], + "bound": "C" + } + ], + "extension_reqs": [] + } + }, + "binary": false + }, + "idiv_u": { + "extension": "arithmetic.int", + "name": "idiv_u", + "description": "as idivmod_u but discarding the second output", + "signature": { + "params": [ + { + "tp": "BoundedNat", + "bound": 7 + } + ], + "body": { + "input": [ + { + "t": "Opaque", + "extension": "arithmetic.int.types", + "id": "int", + "args": [ + { + "tya": "Variable", + "idx": 0, + "cached_decl": { + "tp": "BoundedNat", + "bound": 7 + } + } + ], + "bound": "C" + }, + { + "t": "Opaque", + "extension": "arithmetic.int.types", + "id": "int", + "args": [ + { + "tya": "Variable", + "idx": 0, + "cached_decl": { + "tp": "BoundedNat", + "bound": 7 + } + } + ], + "bound": "C" + } + ], + "output": [ + { + "t": "Opaque", + "extension": "arithmetic.int.types", + "id": "int", + "args": [ + { + "tya": "Variable", + "idx": 0, + "cached_decl": { + "tp": "BoundedNat", + "bound": 7 + } + } + ], + "bound": "C" + } + ], + "extension_reqs": [] + } + }, + "binary": false + }, + "idivmod_checked_s": { + "extension": "arithmetic.int", + "name": "idivmod_checked_s", + "description": "given signed integer -2^{N-1} <= n < 2^{N-1} and unsigned 0 <= m < 2^N, generates signed q and unsigned r where q*m+r=n, 0<=r tys.ExtType: @@ -72,17 +67,15 @@ def to_value(self) -> val.Extension: ) -INT_OPS_EXTENSION = ext.Extension("arithmetic.int", ext.Version(0, 1, 0)) +INT_OPS_EXTENSION = _load_extension("arithmetic.int") -@INT_OPS_EXTENSION.register_op( - signature=ext.OpDefSig(tys.FunctionType.endo([_int_tv(0)] * 2)), -) @dataclass(frozen=True) -class idivmod_u(RegisteredOp): +class _DivModDef(RegisteredOp): """DivMod operation, has two inputs and two outputs.""" width: int = 5 + const_op_def: ClassVar[ext.OpDef] = INT_OPS_EXTENSION.operations["idivmod_u"] def type_args(self) -> list[tys.TypeArg]: return [tys.BoundedNatArg(n=self.width)] @@ -93,7 +86,7 @@ def cached_signature(self) -> tys.FunctionType | None: @classmethod def from_ext(cls, custom: ExtOp) -> Self | None: - if custom.op_def() != cls.const_op_def: + if custom.op_def() != cls.op_def(): return None match custom.args: case [tys.BoundedNatArg(n=a1)]: @@ -107,4 +100,4 @@ def __call__(self, a: ComWire, b: ComWire) -> Command: #: DivMod operation. -DivMod = idivmod_u() +DivMod = _DivModDef() diff --git a/hugr-py/src/hugr/std/logic.py b/hugr-py/src/hugr/std/logic.py index d104a64b8..05524c31c 100644 --- a/hugr-py/src/hugr/std/logic.py +++ b/hugr-py/src/hugr/std/logic.py @@ -3,26 +3,25 @@ from __future__ import annotations from dataclasses import dataclass -from typing import TYPE_CHECKING +from typing import TYPE_CHECKING, ClassVar -from hugr import ext, tys from hugr.ops import Command, DataflowOp, RegisteredOp +from hugr.std import _load_extension if TYPE_CHECKING: + from hugr import ext from hugr.ops import ComWire -EXTENSION = ext.Extension("logic", ext.Version(0, 1, 0)) +EXTENSION = _load_extension("logic") -@EXTENSION.register_op( - name="Not", - signature=ext.OpDefSig(tys.FunctionType.endo([tys.Bool])), -) @dataclass(frozen=True) class _NotOp(RegisteredOp): """Logical NOT operation.""" + const_op_def: ClassVar[ext.OpDef] = EXTENSION.operations["Not"] + def __call__(self, a: ComWire) -> Command: return DataflowOp.__call__(self, a) diff --git a/justfile b/justfile index 3194afa70..2f8beaec6 100644 --- a/justfile +++ b/justfile @@ -58,7 +58,7 @@ update-schema: # Generate serialized declarations for the standard extensions and prelude. gen-extensions: cargo run -p hugr-cli gen-extensions -o specification/std_extensions - + cp -r specification/std_extensions/* hugr-py/src/hugr/std/_json_defs/ build-py-docs: cd hugr-py/docs && ./build.sh diff --git a/specification/schema/hugr_schema_live.json b/specification/schema/hugr_schema_live.json index de354b052..0b8096a43 100644 --- a/specification/schema/hugr_schema_live.json +++ b/specification/schema/hugr_schema_live.json @@ -1084,8 +1084,7 @@ "required": [ "extension", "name", - "description", - "lower_funcs" + "description" ], "title": "OpDef", "type": "object" diff --git a/specification/schema/hugr_schema_strict_live.json b/specification/schema/hugr_schema_strict_live.json index 105d5db8c..ba23a6233 100644 --- a/specification/schema/hugr_schema_strict_live.json +++ b/specification/schema/hugr_schema_strict_live.json @@ -1084,8 +1084,7 @@ "required": [ "extension", "name", - "description", - "lower_funcs" + "description" ], "title": "OpDef", "type": "object" diff --git a/specification/schema/testing_hugr_schema_live.json b/specification/schema/testing_hugr_schema_live.json index 27bfbfb57..b58866307 100644 --- a/specification/schema/testing_hugr_schema_live.json +++ b/specification/schema/testing_hugr_schema_live.json @@ -1084,8 +1084,7 @@ "required": [ "extension", "name", - "description", - "lower_funcs" + "description" ], "title": "OpDef", "type": "object" diff --git a/specification/schema/testing_hugr_schema_strict_live.json b/specification/schema/testing_hugr_schema_strict_live.json index 4dcac4039..ffa2b42f4 100644 --- a/specification/schema/testing_hugr_schema_strict_live.json +++ b/specification/schema/testing_hugr_schema_strict_live.json @@ -1084,8 +1084,7 @@ "required": [ "extension", "name", - "description", - "lower_funcs" + "description" ], "title": "OpDef", "type": "object"