Repository navigation
Add lazy provenance to DD backend based on Souffle - #23
Merged
Merged
Conversation
ajwdev
force-pushed
the
dd-provenance-souffle
branch
11 times, most recently
from
October 7, 2026 16:36
bdcbf92 to
d972d03
Compare
ajwdev
marked this pull request as ready for review
October 7, 2026 16:37
ajwdev
force-pushed
the
dd-provenance-souffle
branch
2 times, most recently
from
October 7, 2026 16:50
e091059 to
8e9e8f9
Compare
Annotate each derived fact with (rule_id, min_height) inside the fixpoint and rebuild one shortest proof on demand at ::why time by backward-chaining against the live session. The technique comes from the Souffle Datalog engine (Zhao, Subotic & Scholz, TOPLAS 2020). Every body step is covered: Scan/Join add premises, filters and column extensions replay through the same per-row helpers the dataflow uses, negated atoms render as absent-leaves, and aggregates are leaves. --provenance gives Engine a live provenance session that follows fact deltas. Rules added with ::define or ::source get provenance (the session rebuilds); ?- query rules are layered on without it, so queries cost the same either way. Design and known costs are in docs/lazy-provenance.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ajwdev
force-pushed
the
dd-provenance-souffle
branch
from
October 7, 2026 17:18
8e9e8f9 to
7cf4b22
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
tl;dr
::whynow works on the DD backend. Run with--backend dd --provenanceand you get one shortest proof for any fact the core rules derive. The technique was largely stolen from Soufflé (Zhao, Subotić & Scholz, TOPLAS 2020); the designwrite-up lives in
docs/lazy-provenance.md.How it works
Every derived fact carries two numbers: the rule that produced its shortest derivation, and that derivation's height (EDB facts are height 0). They're computed inside the fixpoint in a side collection, so
Rowis untouched and nothingchanges when the flag is off.
That's all we store. At
::whytime we backward-chain: find one grounding of the recorded rule whose premises all have strictly lower heights, then recurse into each premise. The strict decrease is what makes it terminate, even throughrecursive rules, with no cycle check.
Replay covers every
Stepvariant and reuses the same per-row helpers as the dataflow, so evaluation and replay can't drift apart. The replaymatchis exhaustive; a newStepwon't compile until it has a replay arm.Why lazy over eager
The eager version (on
dd-provenance) materializes every one-step derivation. You get all derivations for free, but the edge set grows with the number of ways a fact can be derived and has to churn on every delta. Lazy stores twointegers per fact. I'm going with lazy; eager stays on its branch for reference.
What we're giving up
::whyshows the shortest path from A to B, not every path. For escalation auditing that may not be enough; an all-paths walker could reuse the replay machinery minus the height guard.Fixes are in
docs/lazy-provenance.md§2.6 and deliberately deferred (make it correct, then make it fast).::defineand::sourcerules get it (the session rebuilds);?-queries don't, so they cost the same with or without--provenance(32 ms each on the fixtures).!rel(...) (absent)leaves and aggregates are leaves, same as the interpreter.Testing
dd_provenance_explains_every_shipped_factis the one I care about: every fact the shipped rules derive (1368 of them) has an annotation and a grounding whose premises are real facts with strictly lower, matching heights. By inductionevery proof tree bottoms out at EDB. It also checks that turning provenance on doesn't change the derived facts.
Beyond that there are session tests for transitive closure, stratified negation, aggregation, diamonds, multi-premise joins, constant heads and
CallFilter, plus engine tests for fact deltas,::define-style rule addition and?-query layering.
An independent review also fuzzed random adds and retracts against brute-force heights (linear, non-linear and mutual recursion) with an exact match every round. Said property tests aren't in this PR yet.
cargo fmt --check,cargo clippy --all-targets --locked -- -D warningsandcargo testare clean.Aside: upstream bug
Not caused by this PR, but
::whywill happily explain it: a variable repeated inside one atom (ex:edge(X, X)) loses its equality check in mangle-rs planning. Both backends return wrong (and different!) answers. Separate issue.🤖 Generated with Claude Code