higher-order macros [ Python syntax in Lisp and Scheme

higher-order macros [ Python syntax in Lisp and Scheme

Post by james ande » Sat, 11 Oct 2003 01:07:44



Andreas Rossberg wrote:

any macro which cannot be implemented as a single quasiquoted form is likely
to be implemented by calling a function which computes the expansion. the only
difference between a macro function and any "normal" defined function is that
the former is not necessarily any symbol's function value. an auxiliary
function will be a function like any other function: anonymous, defined,
available in some given lexical context only. whatever. there are no intrinsic
restrictions on the computation which it performs. it need only admit to the
reality, that the environment is that of the compiler. eg, definitions which
are being compiled in the given unit "exist" if so specified only.

i am curious whether the availability of tail call elimination can have any
effect on the space performance of a function which is, in general, being
called to compute expressions for inclusion in a larger form. my intuition
says it would not matter.


what would that mean? a macro-proper's argument is generally an s-expression,
and the macro function proper is not bound to a symbol and not necessarily
directly funcallable, but i suppose one could come up with use cases for
mutual recursion among the auxiliary functions.

the generated expressions, on the other hand, often exhibit mutual references.
in this regard, one might want, for example to look at j.schmidt's meta
implementation.[1] perhaps, in some sense, the mutual references which it
generates could be considered "higher-order", but that doesn't feel right.

there's also the issue, that there is nothing which prevents a macro function
from interpreting some aspects of the argument expression as instructions for
operations to be performed at compile-time. eg. constant folding. depending on
how the macro might establish constancy, i'm not sure what "order" that is.


why one would ever think of doing that is beyond me, but given the standard y
operator definition [0],

? (DEFUN Y (F)
( (LAMBDA (G) #'(LAMBDA (H) (FUNCALL (FUNCALL F (FUNCALL G G)) H)))
#'(LAMBDA (G) #'(LAMBDA (H) (FUNCALL (FUNCALL F (FUNCALL G G))
H)))))

Y

should one feel compelled to do so, one might resort to something like

? (defmacro print* (&rest forms)
`(progn ,@(funcall (y #'(lambda (fn)
#'(lambda (forms)
(unless (null forms)
(cons `(print ,(first forms))
(funcall fn (rest forms)))))))
forms)))

PRINT*
? (macroexpand '(print* (list 1 2) "asdf" 'q))
(PROGN (PRINT (LIST 1 2)) (PRINT "asdf") (PRINT 'Q))
T
? (print* (list 1 2) "asdf" 'q)

(1 2)
"asdf"
Q
Q
?


hmm... i never thought of it that way.


[0] http://www.nhplace.com/kent/Papers/Technical-Issues.html
[1] http://www.cliki.net/Meta
 
 
 

higher-order macros [ Python syntax in Lisp and Scheme

Post by oleg » Sat, 11 Oct 2003 07:52:32

Macros generating macros and macros that take other macros as
arguments are quite common in Scheme. CPS-macros in particular are
higher-order macros as they take macro-continuations. Macro-lambda and
macro-apply (in syntax-rules) are further examples of higher-order
macros. Macros are closely related to multi-stage computations. In
MetaOCaml, code is just a regular value. You can write regular OCaml
functions that take, e.g., an argument of a type (char code) and
returns a value of (int->int code). These functions can be
higher-order.

Syntax-rule-level ??!lambda and ??!apply:
http://www.yqcomputer.com/ ~oleg/ftp/Scheme/macros.html

Haskell functions as CL/Scheme macros:
http://www.yqcomputer.com/ ~oleg/ftp/Scheme/lazy-macro-computation.txt

http://www.yqcomputer.com/

 
 
 

higher-order macros [ Python syntax in Lisp and Scheme

Post by james ande » Sat, 11 Oct 2003 17:22:56


interesting, but to contrast that with the referenced meta implementation, the
form of the macro argument matters:

hof hom
apply op form macro-expand (cons op form) or form
(either immediately or relegated to the compiler)

fixed-point functions fixed-point forms


are there examples where these little beasties are used in production?

...
 
 
 

higher-order macros [ Python syntax in Lisp and Scheme

Post by Michael Sp » Sun, 12 Oct 2003 05:37:38

>>>>> "james" == james anderson < XXXX@XXXXX.COM > writes:

james> are there examples where these little beasties are used in production?

If you mean Oleg's little beasties, then the answer is yes, and in
mission-critical, million-dollar-stake applications at that.

--
Cheers =8-} Mike
Friede, Vkerverstdigung und erhaupt blabla
 
 
 

higher-order macros [ Python syntax in Lisp and Scheme

Post by james ande » Sun, 12 Oct 2003 05:48:46


is one at liberty to give anything more than a rhetorical answer?

i would be interested to observe how they express themselves in the large.