I am trying to use IntervalConstraintProgramming & associated packages to solve a geometric feasibility problem. In order to be able to use functions in constraints, the ICP package provides a macro for function definitions.
For example, one such function looks like this (phi1 is going to be an interval): @function calc_a3(phi1) = sqrt($a1^2 + $a2^2 - 2*$a1*$a2*cos(phi1));
I can go further and embed those functions in each other (both phis are intervals here): @function calc_l2_phi2(phi1,phi2) = (
a3 = calc_a3(phi1);
cos(phi2) * a3
)
Constraint creation with these functions works like this: @constraint $r - calc_a3(phi1) < 0
In Julia, you can refer to variables in functions from the scope the function was created in:
r = 5
get_r() = r
I want to achieve something similar using the @function macro (I could probably work around it, but it would make my life easier), but I have no clue about how quoting, AST parsing, etc works in macros, so I have tried the following at random (hoping that a Shakespeare sonnet comes out):
unfortunately my background is corporate, so Iâd rather not share the full story with the public, but I attempted to describe it in a private message to you.
In the meantime I found out (I am kind of goofy) that the @function macro works well, as evidenced by being able to say
Here, there is no problem with referring to outside scalars (not as parameters, mind you, just as constants, like in a closure). I got an error when I tried to do something along the lines of this (as an intermediate step):
r = 5
@function get_r(phi1) = $r
The reason for this is that it doesnât typecheck, @functions to be used in @constraints must return intervals. The error message (MethodError: no method matching iterate(::Expr)) startled me a bit, and I ran to the forums immediately. Sorry for that!