external_functions¶
- wrenfold.external_functions.declare_external_function(name: str, arguments: Iterable[Tuple[str, Type]], return_type: Type) ExternalFunction ¶
Declare an external function. External functions are implemented by the user outside of the code generation framework. They can be invoked on symbolic expressions, and their returned values can be utilized as part of further symbolic expressions.
At code-generation time, the user can override the
format_custom_function_call
formatter to specify how their external function is mapped to actual output code. This can be used to achieve certain customizations:Mapping
name
to different functions in different target languages.Inserting casts or type conversions around arguments.
By necessity, wrenfold must assume that external functions are pure (without side effects). Any two identical calls (ie. having the same function and argument lists) are assumed to be interchangeable, and will be de-duplicated during transpilation.
The
arguments
sequence specifies the names and expected types of the function arguments. The types may be:One of the scalar/matrix types from type annotations.
A dataclass type corresponding to a user-declared type.
A subclass of
wrenfold.type_annotations.Opaque
.
- Parameters:
name – String name for the function.
arguments – Iterable of (name, type) pairs that define the argument list of the function.
return_type – Return type of the function.
- Returns:
An ExternalFunc object that can be invoked via the
__call__
operator. The args to__call__
should have types matching arguments.
- class wrenfold.external_functions.ExternalFunction(super_val: PyExternalFunction)¶
Callable object that represents a user-provided external function. External functions may be handwritten methods that live in the user’s codebase. Wrenfold can invoke these from generated code, provided they are declared with
declare_external_function()
.- __call__(*args, **kwargs) Any ¶
Generate expressions to represent an invocation of
self
.
- __init__(*args, **kwargs)¶
Overloaded function.
__init__(self: wrenfold.gen.PyExternalFunction, name: str, arguments: list[tuple[str, object]], return_type: object) -> None
Construct with name, arguments, and return type.
__init__(self: wrenfold.gen.PyExternalFunction, arg0: wrenfold.gen.PyExternalFunction) -> None
Copy constructor.