Core

Tracing turns an ordinary Python function into an executable IR.

autoform.trace(func, /, *, static=False)[source]

Build an IR by tracing a function’s execution.

Parameters:
  • func (Callable[[Unpack[A]], R]) – A callable that uses autoform primitives (format, concat, lm_call, etc.).

  • static (Tree[bool]) – Bool pytree matching the positional input structure. Mark a leaf True to keep that value fixed at trace time. Mark a leaf False to keep it as a normal runtime input. This is useful for ordinary Python control flow such as if statements. Later calls must pass the same values for leaves marked static.

Returns:

A tracer callable that takes positional arguments and returns an IR.

Return type:

Callable[[Unpack[A]], IR[Unpack[A], R]]

When a flag is marked static, tracing follows only the branch selected by that flag at trace time.

Example

>>> import autoform as af
>>> def label(is_error):
...     if is_error:
...         return "error"
...     return "ok"
>>> ir = af.trace(label, static=True)(True)
>>> ir.call(True)
'error'