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

Macro limitations #39

Closed
regexident opened this issue Apr 12, 2024 · 1 comment
Closed

Macro limitations #39

regexident opened this issue Apr 12, 2024 · 1 comment

Comments

@regexident
Copy link
Contributor

The README.MD file hints at macros being useful for scaffolding head/body items …

It may be useful to define macros that expand to either body items or head items. Ascent allows you to do this.

… which the MACROS.MD then provides further examples for and gives the following motivation …

Macros can be defined inside Ascent programs to avoid having to repeat code.

As somebody accustomed to Rust's native macro the above would make one assume that —just like in Rust itself— ascent's macros could be used in effectively every position in the code. In reality however it seems that ascent's macros are limited to clause position.

As such this works:

ascent! {
    // Facts:

    relation unique(isize);
    
    // Macros:

    macro shared($x: expr) {
        shared(Rc::new($x))
    }

    // Rules:
    
    relation shared(Rc<isize>);

    shared!(*x) <-- unique(x);
}

This however doesn't:

ascent! {
    // Facts:

    relation unique(isize);
    
    // Macros:

    macro rc($x: expr) {
        Rc::new($x)
    }

    // Rules:
    
    relation shared(Rc<isize>);

    shared(rc!(*x)) <-- unique(x);
    //     ^^
    // error: cannot find macro `rc` in this scope
}

This is rather surprising and unexpected.

As such I would expect the above to either be accepted or at least provide a more useful error message than the current "cannot find macro in this scope".

@s-arash
Copy link
Owner

s-arash commented Apr 13, 2024

The point of macro definitions in Ascent is to do what can't be done with normal Rust macros (i.e., expand to body or head clauses). If you need macros that expand to expressions (as in your second example), you can just define them as Rust macros.

Regarding better error messages, that's not really feasible, as the error message is coming from the Rust compiler, and not Ascent.

The readme mentions that macro invocations expand to body or head items. I'll update MACROS.MD to emphasize this fact.

@s-arash s-arash closed this as completed Apr 13, 2024
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

No branches or pull requests

2 participants