Architecture Part 3

The Compiler & Plan IR

Translating intention into instruction: The immutable contract of the compiler.

The Compiler Contract

The Compiler is the bridge between the static AST and the dynamic Runtime. It adheres to a strict Determinism Contract:

"For any given AST input and Dictionary state, the output Plan IR is bit-for-bit identical."

There are no heuristics, no randomness, and no side effects during compilation. This allows Plans to be cached, hashed, and distributed safely.

Plan Intermediate Representation (IR)

The Plan IR is a tree of typed instructions. It resembles a Query Execution Plan in a database.

SetPlan

Instructions: Scan(PredID), Intersect(A, B), Union(A, B), Difference(A, B).

Output: Always a Bitset.

BoolPlan

Instructions: IsEmpty(SetPlan), Check(FactID).

Output: True/False.

RelationPlan

Instructions: Join(SetPlan, PredID), Compose(RelA, RelB).

Output: A RelationMatrix (rows of bitsets).

Optimization Passes

Because the Plan is an explicit data structure, we can run optimization passes on it before execution.

Example: Intersect(HugeSet, SmallSet).
The optimizer can reorder this to Intersect(SmallSet, HugeSet). When intersecting bitsets, iterating over the smaller set (the one with fewer set bits) is significantly faster. The Compiler can inspect the metadata of the sets (if available) or use static heuristics to optimize the Plan structure.