Module

class hugr.build.function.Module(hugr: Hugr | None = None)[source]

Bases: DefinitionBuilder[Module]

Build a top-level HUGR module.

Examples

>>> m = Module()
>>> m.hugr.entrypoint_op()
Module()

Methods

add_alias_decl

Add a type alias declaration.

add_alias_defn

Add a type alias definition.

add_const

Add a static constant to the graph.

declare_function

Add a function declaration to the module.

define_function

Start building a function definition in the graph.

define_main

Define the 'main' function in the module.

module_root_builder

Allows access to the Module at the root of the Hugr (outside the scope of this builder, perhaps outside the entrypoint).

Attributes

metadata

Metadata associated with this module.

hugr

add_alias_decl(name: str, bound: TypeBound) Node[source]

Add a type alias declaration.

add_alias_defn(name: str, ty: Type) Node[source]

Add a type alias definition.

add_const(value: val.Value, parent: ToNode | None = None) Node

Add a static constant to the graph.

Parameters:
  • value – The constant value to add.

  • parent – The parent node of the constant. Defaults to the entrypoint node.

Returns:

The node holding the Const operation.

Example

>>> dfg = Dfg()
>>> const_n = dfg.add_const(val.TRUE)
>>> dfg.hugr[const_n].op
Const(TRUE)
declare_function(name: str, signature: PolyFuncType) Node[source]

Add a function declaration to the module.

Parameters:
  • name – The name of the function.

  • signature – The (polymorphic) signature of the function.

Returns:

The node representing the function declaration.

Examples

>>> from hugr.function import Module
>>> m = Module()
>>> sig = tys.PolyFuncType([], tys.FunctionType.empty())
>>> m.declare_function("f", sig)
Node(1)
define_function(name: str, input_types: TypeRow, output_types: TypeRow | None = None, type_params: list[TypeParam] | None = None) Function[source]

Start building a function definition in the graph.

Parameters:
  • name – The name of the function.

  • input_types – The input types for the function.

  • output_types – The output types for the function. If not provided, it will be inferred after the function is built.

  • type_params – The type parameters for the function, if polymorphic.

  • parent – The parent node of the constant. Defaults to the entrypoint node.

Returns:

The new function builder.

define_main(input_types: TypeRow) Function[source]

Define the ‘main’ function in the module. See define_function().

property metadata: dict[str, object]

Metadata associated with this module.

module_root_builder() Module

Allows access to the Module at the root of the Hugr (outside the scope of this builder, perhaps outside the entrypoint).