From: | George Neuner <gneuner2@comcast.net> |
Newsgroups: | comp.compilers |
Date: | 16 Feb 2005 20:56:07 -0500 |
Organization: | Compilers Central |
References: | 05-02-05305-02-056 05-02-062 |
Keywords: | functional, practice |
Posted-Date: | 16 Feb 2005 20:56:07 EST |
On 13 Feb 2005 22:46:08 -0500, "Carter Schonwald" <cartazio@gmail.com>
wrote:
>I'm not aware of any general characteristic of functional languages
>that precludes batch compilation, especially since such compilers are
>not unusual.
>
>The runtime creation of a function merely requires something along the
>lines of packing together a pointer to the beginning of the function's
>body and a pointer to the current variable environment into a tuple.
>
>If you meant something else, could you please clarify?
I don't want to start a debate about whether Lisp qualifies as a
"functional" language, however, Lisp programs have access to the
interpreter/compiler at run time and a running program can be changed
or extended on the fly by loading or generating new function
definitions.
Using these features, it is possible to write code that references
functions which don't exist at development time.
ex 1.
(setf code-for-add3 (list 'lambda (list 'x) (list '+ 'x 3)))
(setf code-for-add4 '(lambda (x) (+ x 4)))
:
(compile 'add3 code-for-add3)
(compile 'add4 code-for-add4)
(format t "~A = ~A: ~A~%" (add3 7) (add4 6)
(= (add3 7) (add4 6)))
:
The functions (add3) and (add4) are created from *data*, in this case
lists representing lamba expressions that define them.
The (compile) syntax shown creates named, top level functions that can
be directly referenced. (compile) can also create anonymous functions
which must be funcall'd.
ex2.
(setf code-for-my-function (read))
:
(compile my-function code-for-my-function))
(my-function ...)
:
Sans error checking, this example expects to read the list expression
defining (my-function) from standard input. It could just as well be
read from a file or any other input source.
A batch compile performed at development time could not have created
code for any of (add3), (add4) or (my-function) ... the sources for
the functions were not known until run time. [ I'll grant the
hypothetical "sufficiently smart compiler" might figure out (add4). ]
Many languages have some kind of "eval" for executing arbitrary code.
In Lisp (compile) differs from (eval) in that it has access to the
running program's environment, it does not execute the functions it
creates and it can create functions which are tightly bound to the
running program. In contrast (eval) compiles an expression in a clean
lexical environment creating an anonymous function which it then
executes and discards.
Most (all?) of the recognized functional languages allow functions to
be redefined and anonymous functions to be created, but I don't know
whether they routinely permit the same kind of run time extension
possible in Lisp.
George
Return to the
comp.compilers page.
Search the
comp.compilers archives again.