compilation of generated code 
Author Message
 compilation of generated code

I have a function that returns a function [i.e. a list like
(lambda (x) ....)]

Now I would like to use this generated function like any other
function, and to have the function compiled like any other function
(at compile-time). I believe if I do this:

(compile 'generated-function (generate))

..then GENERATED-FUNCTION is compiled at load-time. How can I change
this so that it is compiled at compile-time? If I put the above form
inside an eval-when (compile), GENERATED-FUNCTION is unbound when I
load the compiled file (which somehow seems reasonable to me).

In case it is unclear: By compile-time above I mean when I do
COMPILE-FILE on the relevant file.

Thanks,
--
Frode Vatvedt Fjeld



Sun, 21 Apr 2002 03:00:00 GMT  
 compilation of generated code

Quote:
> I have a function that returns a function [i.e. a list like
> (lambda (x) ....)]

> Now I would like to use this generated function like any other
> function, and to have the function compiled like any other function
> (at compile-time). I believe if I do this:

> (compile 'generated-function (generate))

> ..then GENERATED-FUNCTION is compiled at load-time. How can I change
> this so that it is compiled at compile-time? If I put the above form
> inside an eval-when (compile), GENERATED-FUNCTION is unbound when I
> load the compiled file (which somehow seems reasonable to me).

If the function which returns a function as its value is a compiled
function, then the returned function will also be compiled.  You don't
have to do anything special.

Here is an example (using Allegro CL):

;; create a function which returns a function

USER(22): (defun make-counter ()
            (let ((n 0))
              #'(lambda ()
                  (incf n))))
MAKE-COUNTER

;; create a function
USER(23): (setf c1 (make-counter))

;; it works
USER(24): (funcall c1)
1
USER(25): (funcall c1)
2
USER(26): (funcall c1)
3

;; notice:  the returned function is not compiled, although it
;; is in fact a function.
USER(27): (type-of c1)
FUNCTION
USER(28): (compiled-function-p c1)
NIL

;; compile the generator function
USER(29): (compile 'make-counter)
MAKE-COUNTER
NIL
NIL
USER(30): (setf c2 (make-counter))

;; now the functions it returns are also compiled
USER(31): (compiled-function-p c2)
T

;; it works too
USER(32): (funcall c2)
1
USER(33): (funcall c2)
2
USER(34): (funcall c2)
3

Hope this helps.

-matt



Sun, 21 Apr 2002 03:00:00 GMT  
 compilation of generated code

Quote:

> > I have a function that returns a function [i.e. a list like
> > (lambda (x) ....)]


Quote:
> Here is an example (using Allegro CL):

> ;; create a function which returns a function

> USER(22): (defun make-counter ()
>        (let ((n 0))
>          #'(lambda ()
>              (incf n))))

I may have been unclear about this, but my function-generating
function doesn't really generate objects of type function (or
closures), but lambda-lists.

Like this (would be nicer with backquote, I know):

(defun make-stupid-function (n)
  (list 'lambda ()
        (list '+ 1 n)))

: (make-stupid-function 3) => (lambda () (+ 1 3))
: (funcall (coerce (make-stupid-function 3) 'function)) => 4

I kind of solved the problem by making a macro that will call
make-stupid-function at read-time, so the whole thing expands to a
DEFUN form and really looks like any regular defun to the compiler.

--
Frode Vatvedt Fjeld



Sun, 21 Apr 2002 03:00:00 GMT  
 compilation of generated code


Quote:
>I may have been unclear about this, but my function-generating
>function doesn't really generate objects of type function (or
>closures), but lambda-lists.

Why not change your code to create closures?  That's usually the more
appropriate technique.

Quote:
>I kind of solved the problem by making a macro that will call
>make-stupid-function at read-time, so the whole thing expands to a
>DEFUN form and really looks like any regular defun to the compiler.

Macros don't run at read time, they run at compile time, so I don't
understand what you're saying here.

--

GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.



Sun, 21 Apr 2002 03:00:00 GMT  
 compilation of generated code

Quote:
> In case it is unclear: By compile-time above I mean when I do
> COMPILE-FILE on the relevant file.

If the information needed (the source form, really) to compile the
function is available at compile time, then you should use a macro,
which will do what you want, because all your doing is what the macro
system does.  If it's not, then you can't do it any other way, you
just need to compile the function at run time.

--tim



Sun, 21 Apr 2002 03:00:00 GMT  
 compilation of generated code

Quote:

> Why not change your code to create closures?  That's usually the
> more appropriate technique.

Because the program transformations are too complex. If I was to
return a closure, this closure would consist of the complete
code-transformer function, and the original transformed to-be data. So
the code that gets compiled in this case is really just the
transforming code, not its output (which is what I wanted to do).

Quote:
> Macros don't run at read time, they run at compile time, so I don't
> understand what you're saying here.

Hum.. yes, my mistake. But macros do run before the actual compilation
occurs, so I like to think of it as "before compile-time"
somehow. Anyways, it solved my problem. Thanks also to Tim Bradshaw
who suggested this.

--
Frode Vatvedt Fjeld



Sun, 21 Apr 2002 03:00:00 GMT  
 compilation of generated code

| Hum.. yes, my mistake.  But macros do run before the actual compilation
| occurs, so I like to think of it as "before compile-time" somehow.

  I suggest you don't, as it will only confuse you terribly.

#:Erik



Sun, 21 Apr 2002 03:00:00 GMT  
 compilation of generated code


Quote:

>> Why not change your code to create closures?  That's usually the
>> more appropriate technique.

>Because the program transformations are too complex. If I was to
>return a closure, this closure would consist of the complete
>code-transformer function, and the original transformed to-be data. So
>the code that gets compiled in this case is really just the
>transforming code, not its output (which is what I wanted to do).

I suggest you take a look at data-directed programming, as it sounds like
this may be what you're doing.

--

GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.



Sun, 21 Apr 2002 03:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. no system generated, please make sure c compilation ended correctly

2. Is it possible to generate code (as C or C++ code) from a LabView diagram

3. code to generate a unique client code

4. C code compilation with Symantec C++?

5. Compilation induced constants in Ada code?

6. code compilation

7. does renaming core commands affect byte code compilation?

8. Syntax motivation, compilation and reentrant code

9. Special compilation action for defalut compilation sematics

10. HW compilation vs SW compilation

11. problem in generated code: 'Invalid arg 6: Cannot coerce a SDWORD to SDWORD*

12. Wanted: SDK that generates Smalltalk Code ?

 

 
Powered by phpBB® Forum Software