Subject: Re: [closures] compilation of
From: rpw3@rigden.engr.sgi.com (Rob Warnock)
Date: 1999/12/04
Newsgroups: comp.lang.lisp
Message-ID: <82aqo2$8lhp6@fido.engr.sgi.com>
H. Tunc Simsek <simsek@EECS.Berkeley.Edu> wrote:
+---------------
| Why can't you compile closures?
| (let ((x 1))
|   (defun dene () 
|       x))
| (compile 'dene)
+---------------

Well, you *can*, just not exactly *that* way. ;-}  Whenever you compile
a top-level function, any closures within it also get compiled. So you
can define (and then compile) a top-level "closure generator" that does
what you want, albeit slightly awkwardly [though that can be fixed with
suitable macrology]. My normal example of such things is a generator of
counters, but let's use your example [aside: what does "dene" mean?]:

	> (defun make-dene (&key (init 0))
	    (let ((x init))
	      #'(lambda () x)))
	MAKE-DENE
	> (setf (symbol-function 'dene) (make-dene :init 1))
	#<Interpreted Function "LAMBDA (&KEY (INIT 0))" {90808E9}>
	> (dene)
	1
	> (describe 'dene)
	DENE is an internal symbol in the COMMON-LISP-USER package.
	Function: #<Interpreted Function "LAMBDA (&KEY (INIT 0))" {90BA369}>
	Function arguments: There are no arguments.
	Its defined argument types are: NIL
	Its result type is: *
	Its closure environment is:
	  0: 1
	Its definition is:
	  (LAMBDA () X)
	>

Now compile "make-dene", and see how it changes things:

	> (compile 'make-dene)
	Compiling LAMBDA (&KEY (INIT 0)): 
	Compiling Top-Level Form: 
	MAKE-DENE
	NIL
	NIL
	> (setf (symbol-function 'dene) (make-dene :init 23))
	#<Closure Over Function "LAMBDA (&KEY (INIT 0))" {90A5C19}>
	> (dene)
	23
	> (describe 'dene)
	DENE is an internal symbol in the COMMON-LISP-USER package.
	Function: #<Closure Over Function "LAMBDA (&KEY (INIT 0))" {90A62E1}>
	Function arguments: There are no arguments.
	Its defined argument types are: NIL
	Its result type is: T
	On Saturday, 12/4/99 02:20:12 am PST it was compiled from:
	#(#'(LAMBDA # #))
	Its closure environment is:
	0: 23
	> 

Is that close enough to what you were looking for?


-Rob

-----
Rob Warnock, 8L-846		rpw3@sgi.com
Applied Networking		http://reality.sgi.com/rpw3/
Silicon Graphics, Inc.		Phone: 650-933-1673
1600 Amphitheatre Pkwy.		FAX: 650-933-0511
Mountain View, CA  94043	PP-ASEL-IA