Magic function

From APL Wiki
Jump to navigation Jump to search

In APL implementation, magic functions are a technique that allows parts of an interpreter written in a language such as C to be implemented in APL. Much like APL code stored in an APL string is evaluated during the execution of the Execute primitive, a magic function evaluates APL code stored in a C (or other implementation language) string while executing a primitive or derived function. Magic functions can allow APL implementers to work more quickly by using a higher-level language, and can even improve performance as APL allows the programmer to more easily access fast primitive implementations.[1] Often they are used for testing new functionality or implementation techniques, as they can be easily changed during prototyping, and translated back to the implementation language after the code has stabilized.

Magic functions are not the only way to implement parts of an interpreter in APL: while a magic function is written as part of the interpreter's source code and called during execution, it may also be possible to define parts of the interpreter using external APL code executed at startup. Functionality like user commands is often written in APL in this way, and ngn/apl even uses this technique to implement primitives, as it allows primitives to be freely reassigned.

During the time we were implementing the very first version of Dyalog, a friend, who was not a programmer, asked me how much of it was coded in APL. I thought he was bonkers.

We now have exactly this internal "magic function" prototyping facility.

John Scholes[2]