New library(macros)

I pushed a new library(macros). This is intended to deal with the common need to declare compile time constants, write more complex terms using a simpler syntax, etc.

The overall idea is that a macro is used as #name(Arg, ...), i.e., is of the same #(callable), where # is a prefix operator, so we can write #callable.

Macros are defined using one of

#define(Macro, Expansion).
#define(Macro, Expansion) :- Body.

In addition, macro may be imported from another module using

#import(File).

Macros are scoped to a module.

Macro matching uses single sided unification (SSU) trough => rules. A very simple example is here:

:- use_module(library(macros)).
#define(max_size, 100).

Now we can use e.g.

(  Size < #max_size
-> ...

Using the Body, we can also do more fancy stuff. For example, we can define

#define(calc(Expr), Value) :- Value is Expr.

Now, we can anywhere use e.g. #calc(pi/2) for using this floating point constant. Of course, expression optimization can avoid evaluating pi/2 at runtime in A is X*pi/2, but our macro allows us to use the compile time evaluated value anywhere, not just in the context of arithmetic.

This was designed to deal with simplifying writing complex terms in a project.

Comments are welcome!

8 Likes