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

Examples #40

Merged
merged 8 commits into from
Apr 14, 2024
Merged

Examples #40

merged 8 commits into from
Apr 14, 2024

Conversation

regexident
Copy link
Contributor

It's much easier to start using ascent (and especially its custom DSL) if there is a list of examples for both, individual language features, as well as more complete use-cases.

This PR aims to plant a first seed for such an extensive pool of examples with an initial set of …

  • Examples focussing on individual language features of ascent:

    • ascent_for_in_clause.rs
    • ascent_agg_clause.rs
    • ascent_disjunction_clause.rs
    • ascent_generic_program.rs
    • ascent_if_clause.rs
    • ascent_if_let_clause.rs
    • ascent_lattice.rs
    • ascent_let_clause.rs
    • ascent_macros_rule.rs
    • ascent_negation_clause.rs
  • Examples showcasing simple yet well-known algorithms:

    • fibonacci.rs
    • fizz_buzz.rs
    • transitive_graph_closure.rs
  • Examples showcasing real-world use-cases:

    • context_sensitive_flow_graph_with_records.rs
    • context_sensitive_flow_graph.rs
    • def_use_chains.rs
    • sequences_using_recursive_records.rs
    • var_points_to.rs

@kmicinski
Copy link
Collaborator

Happy to see this commit. This looks like a good start. We should consider adding some even larger examples, I know I recently saw an Ascent version of https://github.com/plast-lab/doop-mirror/blob/master/souffle-logic/analyses/micro/self-contained.dl, which comes complete with some sizable EDBs. It is a very small version of DOOP (not using more complex analysis features, but a suitable basis). I am also interested in adding a few more complex examples of program analyses in Ascent that we could contribute to the examples folder if this is approved.

@regexident
Copy link
Contributor Author

Absolutely! The lack of available Datalog source code is a major hurdle when getting started with the language, imho.

In pretty much every other major language there is more openly available source code available than one could reasonably read in a lifetime. Yet for Datalog you're likely to reach the end (of the stuff that's easily available/discoverable) by the end of the day, unless you descend into scientific journal papers and stuff.

So every example is worth its weight in gold.

@s-arash
Copy link
Owner

s-arash commented Apr 13, 2024

@regexident Thanks for the PR, this is great work!

fn main() {
let mut prog = AscentProgram::default();

prog.number = vec![
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we generate this from a range instead?

Copy link
Contributor Author

@regexident regexident Apr 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure!

I'm not sure if FizzBuzz (and this specific implementation in particular) is much of a great example for ascent to begin with, as in it isn't really a particularly interesting problem, but since it's a well-known yet simple one I figured why not include it.

(I also tried implementing the Sieve of Eratosthenes but failed miserably. 😅)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, Sieve of Eratosthenes doesn't look like a good fit for Datalog (it may be doable with lattices, but I don't think it would end up being a good example).

BTW, you may have forgotten to generate prog.number from a range here!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, you may have forgotten to generate prog.number from a range here!

I did! Fixed.

relation len(Rc<List<char>>, usize);

len(nil!(), 0);
len(Rc::clone(&l), n + 1) <-- char(c), len(r, n), let l = cons!(c.clone(), r.clone()), list(Rc::clone(&l));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would also work:

len(l, n + 1) <-- char(c), len(r, n), let l = cons!(c.clone(), r.clone()), list(&l);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not quite, unfortunately:

error[E0507]: cannot move out of `l`, a captured variable in an `FnMut` closure
  --> ascent/examples/lists_using_recursive_enums.rs:58:9
   |
58 | ...en(l, n + 1) <-- char(c), len(r, n), let l = cons!(c.clone(), r.clone()), list(&...
   |       ^                                     -                                ---- captured by this `FnMut` closure
   |       |                                     |
   |       |                                     captured outer variable
   |       move occurs because `l` has type `Rc<List<char>>`, which does not implement the `Copy` trait

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, ok. I'll have to look into it. Meanwhile, this should work:

len(l.clone(), n + 1) <-- char(c), len(r, n), let l = cons!(c.clone(), r.clone()), list(&l);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works! Fixed.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I see why that wouldn't work. The last clause could have multiple matches in general (not the case here), so we can't take ownership of the value in the head clause.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that makes a lot of sense.

fn main() {
let mut prog = AscentProgram::default();

prog.number = vec![
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, Sieve of Eratosthenes doesn't look like a good fit for Datalog (it may be doable with lattices, but I don't think it would end up being a good example).

BTW, you may have forgotten to generate prog.number from a range here!

@regexident regexident force-pushed the examples branch 2 times, most recently from 5f28e26 to 6917e9a Compare April 14, 2024 18:46

// The rule above could alternatively also be written
// using the following short-hand syntax:
// some(y) <-- option(?Some(y));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it'd be better to uncomment this. This would be a redundant rule, but I think it's fine for an example showing Ascent features.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@s-arash s-arash merged commit 26b94a8 into s-arash:master Apr 14, 2024
4 checks passed
@regexident regexident deleted the examples branch April 14, 2024 20:44
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.

3 participants