srdatalog.ir.hir.rule_rewrite¶
Rule-rewrite passes (HIR Pass 0 / Pass 1). Mirror src/srdatalog/hir/constant_rewriting.nim and head_constant_rewriting.nim.
Semi-join optimization (Pass 1.5) is 385 lines and is deferred to a later turn; it doesn’t fire on simple programs anyway.
Counters are per-pass-invocation (reset each call). Nim uses a
compileTime counter that persists across a single macro compilation —
for fixtures the tool compiles once, so starting at 0 matches.
Module Contents¶
Classes¶
Runs |
|
Functions¶
Single pass. Rule must have semi_join=True and body.len > 2 to be
considered. For each candidate target/filter pair, synthesise an
|
|
Pass 0: body-constant rewriting. |
|
Pass 1: head-constant rewriting. |
|
Pre-pass: gensym each |
API¶
- class srdatalog.ir.hir.rule_rewrite.SemiJoinPass[source]¶
Runs
optimize_semi_joinsto a fixed point. Nim handles the outer loop in compileToHir; we keep it inside the pass for symmetry with the other rule-rewrites.- info¶
‘PassInfo(…)’
- srdatalog.ir.hir.rule_rewrite.optimize_semi_joins(rules: list[srdatalog.dsl.Rule], decls: list[srdatalog.ir.hir.types.RelationDecl]) tuple[list[srdatalog.dsl.Rule], list[srdatalog.ir.hir.types.RelationDecl]][source]¶
Single pass. Rule must have semi_join=True and body.len > 2 to be considered. For each candidate target/filter pair, synthesise an
_SJ_Target_Filter_<keptIndices>relation whose generator rule isTarget ⋈ Filter, and replace the target clause (dropping the filter clause) in the original rule.Mirrors src/srdatalog/hir/semi_join_optimization.nim. Fixed-point iteration lives in SemiJoinPass.run (Nim does it in compileToHir).
- srdatalog.ir.hir.rule_rewrite.rewrite_constants(rules: list[srdatalog.dsl.Rule], decls: list[srdatalog.ir.hir.types.RelationDecl]) tuple[list[srdatalog.dsl.Rule], list[srdatalog.ir.hir.types.RelationDecl]][source]¶
Pass 0: body-constant rewriting.
For each positive body clause with constant args, replace every Const with a fresh LVar (_cN) and append a Filter clause asserting equality immediately after the rewritten atom. Matches Nim’s output byte-for-byte when fresh-name counters start at 0 (default).
- srdatalog.ir.hir.rule_rewrite.rewrite_head_constants(rules: list[srdatalog.dsl.Rule], decls: list[srdatalog.ir.hir.types.RelationDecl]) tuple[list[srdatalog.dsl.Rule], list[srdatalog.ir.hir.types.RelationDecl]][source]¶
Pass 1: head-constant rewriting.
For each head with constant args, replace each Const with a fresh LVar (_hcN) and append a Let clause to the body binding that variable to the constant’s C++ expression. Runs AFTER body-constant rewriting.
- srdatalog.ir.hir.rule_rewrite.rewrite_wildcards(rules: list[srdatalog.dsl.Rule], decls: list[srdatalog.ir.hir.types.RelationDecl]) tuple[list[srdatalog.dsl.Rule], list[srdatalog.ir.hir.types.RelationDecl]][source]¶
Pre-pass: gensym each
_-named LVAR to_gen<N>.Mirrors
parseClauseArgin src/srdatalog/syntax.nim where, at macro parse time, every_(or_-prefixed) identifier is replaced with a fresh unique name drawn from a monotonically increasing counter. Two_slots in the same atom must be independent variables, which requires a fresh name per occurrence.Counter starts at 1 to match Nim’s
wildcardCounter.incordering.