Support arbitrary tuples #34
Closed
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.
This PR aims to make
datafrog
less dependent on(Key, Value)
tuples, lessening the burden of having to create lots and lots of intermediary variables that do little more than move an element to the front of the tuple, so it can be used as key.Before
As such where you currently have to introduce auxiliary variables of
(Key, Value)
tuples:After
… with this PR you simply extract the key from an arbitrary tuple index using a closure:
This cuts down the maintenance burden of having to keep several additional auxiliary variables and relations up-to-date, reducing the mental overhead and brings your datafrog Rust code a little bit closer to their corresponding datalog rules.
@frankmcsherry @nikomatsakis I hope that these changes are not in conflict with the soundness of the API of datafrog or the semantics of Datalog?
Performance
I did some performance testing with criterion, but did not find any consistent and noteworthy performance regressions for existing APIs and found the overhead of the
…_by
method variants minimal.Known limitations
Unfortunately this PR only allows lenses to return references to arbitrary elements of the tuple (via
Accessor1: Fn(&Tuple1) -> &Key
), but no owned ad-hoc values.I had initially hoped to be able to allow for arbitrary Key values, such as ad-hoc tuples or other computed values. This would have allowed for a returning
(&X, &Y)
as key for(X, Y, Z)
tuple, rather than just single individual elements of it. Support for arbitrary tuples as keys would have been very convenient for scenarios with complex n-ary keys such as borrow_check.rs.Changing
Accessor1: Fn(&Tuple1) -> &Key,
toAccessor1: Fn(&Tuple1) -> Key,
unfortunately causes accessors that return a reference to the tuple or elements of it, such as|tuple| &tuple
or|tuple| &tuple.0
to trigger this language limitation. Unfortunately those are the most commonly used accessors and I'm not aware of a workaround that would allow both, arbitrary keys and borrowed tuple element keys.Minimal reproducible snippet
Error:
This is part #3 of a stacked pull-request and adds upon #33: