From: levy
Subject: Poor man's partial evaluator
Date: 
Message-ID: <1166653767.180273.312010@79g2000cws.googlegroups.com>
Hmm, two mails on one day... :-)

Assume that one has an algorithm f and one wants to partial evaluate it
_runtime_ by binding one of its arguments to a CLOS object. Actually we
are generating a partial evaluated slot accessor for a particular
object/slot pair. There are calls to other slot accessors on that and
some other objects in f, calls to make-instance, etc. some of which can
be partial evaluated. The function f is a complex function, so it is
split up into g, h, etc.

On the other hand for debug purposes one wants to have the original
(generic) functions too, because debugging is easier that way.

We had some ideas, but not really satisfied with any of them, but here
they are:

1. (compile nil `(lambda (x) (f x ,o))) while declaring the functions
f, g, h, etc. inline and implement a compiler-macro for those functions
which might be evaluated at compile time.

Pros:
Don't have to touch the generic f, g, h, etc. functions.

Cons:
This messes with inline declarations which might cause trouble in some
other places and we might also need some special around the compile
call (maybe inlining is possible in that compile only?), so that the
compiler-macros will not be mislead. I think of object accessors which
might have side effects, but will not in this context.

2. Use lamda. What else can we say here? :-) Ok, somewhat less briefly:
capture partial evaluated stuff from lexical variables in lamdas and
rewrite f, g, h, etc. to return a lamda which has to be called to
actually do the job. Link those lamdas by capturing them in variables
and using funcall. Call the top level f function with the parameters
for which it will partial evaluate the function and store the returned
lambda.

Pros:
This solution is the least magical I think.

Cons:
For this the functions has to be refactored quite a bit and it will be
hard to debug the generic version, because of the two passes.

3. Use macro. This is somewhat tricky I think. Write a macro which
defines two things: the non partial evaluated version as a function and
the partial evaluated version as a macro. Use , and ,@ where things
have to be calculated at compile time and use ` or ' where not. Call
compile on the top level macro call to f during runtime and store the
returned function.

Pros:
Clearly separated generic and partial evaluating versions.

Cons:
This also requires refactoring the functions and has problems when
trying to jump to the source from the stack frames in the debugger.

Any other idea? Do we miss something?

Cheers,
levy