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

ConstantRewritePass

HeadConstantRewritePass

SemiJoinPass

Runs optimize_semi_joins to a fixed point. Nim handles the outer loop in compileToHir; we keep it inside the pass for symmetry with the other rule-rewrites.

WildcardRewritePass

Functions

optimize_semi_joins

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 is Target Filter, and replace the target clause (dropping the filter clause) in the original rule.

rewrite_constants

Pass 0: body-constant rewriting.

rewrite_head_constants

Pass 1: head-constant rewriting.

rewrite_wildcards

Pre-pass: gensym each _-named LVAR to _gen<N>.

API

class srdatalog.ir.hir.rule_rewrite.ConstantRewritePass[source]
info

‘PassInfo(…)’

run(rules, decls)[source]
class srdatalog.ir.hir.rule_rewrite.HeadConstantRewritePass[source]
info

‘PassInfo(…)’

run(rules, decls)[source]
class srdatalog.ir.hir.rule_rewrite.SemiJoinPass[source]

Runs optimize_semi_joins to 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(…)’

run(rules, decls)[source]
class srdatalog.ir.hir.rule_rewrite.WildcardRewritePass[source]
info

‘PassInfo(…)’

run(rules, decls)[source]
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 is Target 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 parseClauseArg in 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.inc ordering.