Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User defined aggregate #2282

Merged
merged 6 commits into from
Oct 17, 2022

Conversation

julienhenry
Copy link
Member

@julienhenry julienhenry commented Jun 10, 2022

Aggregates in Soufflé are very useful. However, there are use-cases where a developer would want to use its own aggregating function (instead of the intrinsics min, max, count, sum, etc.)
In this PR, I propose a way to use a user defined functor, declared as usual with a .functor ..., as the function to apply on an aggregate.

A example use of user-defined aggregate would be :

.type Interval = [lb: number, ub:number]

.functor lub(i1:Interval, i2:Interval):Interval stateful

.decl I(x:number, i:Interval)
.input I

.decl leastUpperBound(x:number, i:Interval)
leastUpperBound(x, lub) :- 
    I(x, _),
    lub = @@lub i : [0,0], {I(x, i)}.

The above example:

  • initializes the accumulating variable res to [0,0].
  • updates res = @lub(res, i) for each i satisfying I(x,i).

We can discuss on the syntax if the above one is not convenient...

Current limitations:

  • only stateful functors can be used
  • functors must have exactly two arguments, and the first argument being passed is always the accumulating variable.
  • Behavior may not be deterministic if functor is not commutative/associative...

@codecov
Copy link

codecov bot commented Jun 10, 2022

Codecov Report

Merging #2282 (b698a0b) into master (ba99149) will increase coverage by 0.03%.
The diff coverage is 83.60%.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2282      +/-   ##
==========================================
+ Coverage   77.39%   77.43%   +0.03%     
==========================================
  Files         461      468       +7     
  Lines       29004    29162     +158     
==========================================
+ Hits        22448    22581     +133     
- Misses       6556     6581      +25     
Impacted Files Coverage Δ
src/ast/Aggregator.h 100.00% <ø> (ø)
src/ast/analysis/Functor.h 50.00% <ø> (ø)
src/ast/analysis/typesystem/PolymorphicObjects.h 100.00% <ø> (ø)
src/ast/analysis/typesystem/Type.h 60.00% <ø> (ø)
...c/ast/analysis/typesystem/TypeConstrainsAnalysis.h 100.00% <ø> (ø)
src/ast/transform/InlineRelations.cpp 64.86% <0.00%> (-1.24%) ⬇️
src/ast/transform/MagicSet.cpp 91.05% <0.00%> (-0.89%) ⬇️
src/ast2ram/utility/TranslatorContext.h 100.00% <ø> (ø)
src/interpreter/Engine.h 100.00% <ø> (ø)
src/ram/utility/Visitor.h 54.54% <ø> (ø)
... and 35 more

@b-scholz
Copy link
Member

@julienhenry - there is a clash with your changes. Could you fix it?

@julienhenry
Copy link
Member Author

julienhenry commented Oct 15, 2022

@b-scholz I have updated to latest master branch. We still need to verify in the TypeChecker that the user-defined functor used in the aggregate is used correctly, there is no such check at the moment.
Also, I'm not very happy with the double '@' syntax. A single '@' would look better but causes conflicts in the parser.

@b-scholz
Copy link
Member

Thanks for the contribution! Please, can you update the documentation as well?

@b-scholz b-scholz merged commit d978549 into souffle-lang:master Oct 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants