Code that writes code: macros,
@evalloops,@generatedfunctions, and runtimeeval. What Sophia sees of each, what it would take to see more, and why “more” means asking Julia. 2026-10-20.Sources: the Julia manual, Metaprogramming (macro hygiene,
esc, generated functions); JuliaSyntax.jl andMeta.lower; Pelenitsyn et al., Type Stability in Julia, OOPSLA 2021 (on generated functions in practice); Julia Name Resolution; Julia Conformance.
What Sophia does today
| Construct | Example | What Sophia stores | Correct? |
|---|---|---|---|
| macro call around a definition | @inline f(x) = …, @propagate_inbounds function … | the definition, hashed as written (the macro name is part of the syntax) | as source, yes; as semantics, approximate: the macro may change the definition |
| macro call producing definitions | @deprecate f(x) g(x) | nothing for f (since v7 also no spurious unit from a keyword default inside it) | honest: unknown |
@eval loop | for f in (:sin, :cos); @eval Base.$f(x::T) = …; end | the loop is not a unit; a qualified head with $ belongs to no generic function | honest: unknown |
@generated function | @generated function f(x) … end | the generator as a function | the stored object is the generator, not the methods it produces per type |
| macro definition | macro m(ex) … end | a unit of kind macro | yes |
runtime eval / include with a computed path | include(joinpath(dir, "x.jl")) | dynamic include recorded; functions of that module stay open | honest |
On LinearAlgebra, Julia reports 4 933 methods from the package’s files; Sophia stores 2 920 methods. About 1 059 of Julia’s methods sit at lines where Sophia stores no method at all: generated by @eval loops and macros, default constructors from struct definitions, and refused definitions (Julia Conformance).
Why tree-sitter cannot do better
A macro is a function from syntax to syntax that runs in the macro’s module, at load time. Its output depends on its definition (possibly in another package), on hygiene, and sometimes on global state. Expanding it statically means running Julia. @eval loops are ordinary loops that call eval, so their output depends on the loop’s values.
Options
- Hash as written (today). The definition’s identity covers the macro call’s syntax. Two definitions that expand identically but are written differently get different ids. Sound as source identity; says nothing about what runs.
- Ask Julia for the expansion, record it as a derived program.
macroexpand(M, ex)orMeta.lower(M, ex)in a loaded environment gives the expanded code. Store it as a transformation record (SPEC-006 §6a): by = the macro (its id and the world it was defined in), input = the call’s id, output = the expansion’s id, relation =expands-to. The expansion can be cached and rebuilt (Object Categories 5). - Ask Julia for the methods directly. For definitions that only exist after expansion (
@evalloops), the method table is the ground truth: each method’s file, line and signature, asmethods.jlalready prints. Sophia records “these methods come from this source span” as evidence.
Option 2 is the principled one: the expansion is a derived program whose identity is input-addressed, exactly like an AD derivative (AD as a Materialization Case). It needs a Julia process in the loop, which Sophia does not have yet. It is experiment E-4 in Semantic Boundary Experiments.
Generated functions
A @generated function’s body runs at compile time on the argument types and returns the code to compile. So its methods are a function of (generator, argument types, world), which is the same identity shape as a specialisation or a derivative. Sophia should store the generator (authored) and treat each generated body as a derived program, cached per signature. Nothing of this is implemented.