Type and methods info in macro

Hello,

I’ve been attempting to implement a simple APL in julia. Something similar has been done here: https://github.com/shashi/APL.jl, but that is an interpreter. I wanted to do it as a syntax transformation to use as much existing julia code as possible.

The problem I’m having is parsing lines like + reduce v where v is an Array. The trouble is, this could be parsed in two ways:

  • ( + )( ( reduce )( v ) ) if both + and reduce takes a single argument
  • ( reduce )( +, v ) if (correctly) reduce takes two arguments, a function and an Array

The second option is correct in this case, as + reduce v is equivalent to reduce(+, v). However, another input could be - abs x, which is equivalent to -(abs(x)) if x is a number and should thus be parsed at ( - )( ( abs )( x ) ).

My initial pipeline was a string macro doing something like

  1. Tokenize string
  2. Convert symbols like +, reduce and v to Symbols
  3. Use a @generated function to extract types from those Symbols
  4. Use a disambiguation scheme to parse the resulting list of types
  5. Convert the parsed tree from 4 into a julia Expr to return

My immediate problem is that typeof(reduce) isn’t really informative, but this is what I get in a @generated function. Ideally, I want to be able to use something like applicable but on the type level. This way, I could simply write

@generated function is_applicable(f, x, y)
type_applicable(f, x, y)
end

I haven’t been able to find anything like this.

My second concern is that splitting the string, converting to Symbols and unquoting those into the expression seems kind of roundabout. Is there a better way to do what I’m going for?

Best regards and thanks in advance

  • Andreas