Local macros

In CommonLisp one can define local macros using MACROLET. Julia doesn’t have local macros, but one can imagine that a given global macro could provide local macros for use wwithin its body using a code walker and rules that re-write :macrocall expressions.

Has anyone already implemented such a utility?

It seems straightforward enough, but I imagine there might be unforseen pitfalls.

What’s the problem I’m immediately trying to solve via local macros? I have a simple-minded expert system shell (Rete.jl) with an @rule macro. That macro implements an install method which will connect a new join node that implements the rule into a discrimination network. The body of a rule performs some conditionals and if all succeed then the rule will emit new facts to the network. Currently there are no diagnostics if a rule fails to make an expected conclusion if one of the conditionals fails because the join function just returns. I want there to be something (a local macro) that takes a predicate and logs when the predicate fails before the rule body exits.

This could be done with a closure but I want the log message to include the predicate expression and the source location.

I’m currently looking at both MacroTools and SymbolicUtils.

Any suggestions or insights?

Thanks.

Macros are scoped to locally to modules in Julia.

I don’t understand why your @rule macro couldn’t do this — why can’t it pass the predicate expression and the source location to the closure or whatever data structure you are using?

Perhaps I’m not being clear.

I’m proposing to walk the body of the @rule macro call and rewrite certain macrocall expressions like @rejectif, thus emulating a local macro mechanism.

I was wondering if someone had already implemented such a thing.

I’ve tried using MacroTools for the pattern matching, but the general method on match_inner removes line number nodes. I guess I’ll need to write my own matcher.

I once did this inside out macro which seems related jkrumbiegel.com – Composing macros inside-out with Julia