From: Thomas Weigert
Subject: hints re: apply, eval requested
Date: 
Message-ID: <WEIGERT.92Mar30153058@etlhit.etl.go.jp>
I am in the process of putting together something like my
own eval function, but there are some places where I am
stuck now.

For simplicity's sake lets assume that all this eval
function (lets call it meval) does, is the following. When 
"(meval fred 1 2 3)" is called

(i) meval's each argument 

(i) looks up whether it has some result cached for a
function calls to "fred" with the resultant arguments; this
result will be returned.

(ii) otherwise, it just passes the call to lisp, i.e.,
apply's fred to the resultant arguments; the result of this
will be returned.

I have the meval function working satisfactorily. However,
what I would like to do now is to have something paralleling
#'. I.e., I want to be able to create function objects that
could be applied by meval. The problem that I am struggling
with is that I cannot picture how I would go about doing
this. I would like to be able to say
  (mfunction '(lambda (x) (fred x)))
and obtain an object which could be applied to some
argument. However, this mfunction object must be savy to
the fact that, e.g., fred might have to be evaluated using
table lookup.

A related question is the following (this may be a commonly
asked question, but please bear with me, I  am coming
originally from a scheme background). I would like to have a
version of apply which does not care whether the object
applied to is a function or a macro or a special-form (this
is possible in scheme). 

Alternatively, I could work around this problem if I had a
version of eval which does not evaluate its arguments.

Any help is greatly appreciated.

Thomas.

+--------------------------------+---------------------------------+
| Thomas Weigert                 |                                 |
| Suiron-kenkyuu-shitsu          | ·······@{mcs.anl.gov,etl.go.jp} |
| Electrotechnical Laboratory    | +81-298-58-5918 (phone+fax)     |
+--------------------------------+---------------------------------+
--
+--------------------------------+---------------------------------+
| Thomas Weigert                 |                                 |
| Suiron-kenkyuu-shitsu          | ·······@{mcs.anl.gov,etl.go.jp} |
| Electrotechnical Laboratory    | +81-298-58-5918 (phone+fax)     |
+--------------------------------+---------------------------------+
From: Barry Margolin
Subject: Re: hints re: apply, eval requested
Date: 
Message-ID: <kteke4INNhjp@early-bird.think.com>
In article <·····················@etlhit.etl.go.jp> ·······@mcs.anl.gov writes:
>For simplicity's sake lets assume that all this eval
>function (lets call it meval) does, is the following. When 
>"(meval fred 1 2 3)" is called
>
>(i) meval's each argument 

Is MEVAL a macro?  If it's a function, the arguments will already have been
evaluated, so there's no need to evaluate them again.  Your syntax also
looks more like FUNCALL, so it would probably be better to use a name like
MFUNCALL or CACHED-FUNCALL.  You'd also need to use #'FRED so that you'll
get FRED's function binding (I'm assuming you're using Common Lisp, since
you didn't specify a dialect).

>(i) looks up whether it has some result cached for a
>function calls to "fred" with the resultant arguments; this
>result will be returned.

Wouldn't it be better to put this in the definition for FRED?  We have a
DEFUN-ENCACHEABLE macro that defines functions that encache their own
results in a table.  This way you don't need a special evaluator, so it
works when compiled.

>(ii) otherwise, it just passes the call to lisp, i.e.,
>apply's fred to the resultant arguments; the result of this
>will be returned.
>
>I have the meval function working satisfactorily. However,
>what I would like to do now is to have something paralleling
>#'. I.e., I want to be able to create function objects that
>could be applied by meval. The problem that I am struggling
>with is that I cannot picture how I would go about doing
>this. I would like to be able to say
>  (mfunction '(lambda (x) (fred x)))
>and obtain an object which could be applied to some
>argument. However, this mfunction object must be savy to
>the fact that, e.g., fred might have to be evaluated using
>table lookup.

Do you want to be able to hand one of these to a regular APPLY or FUNCALL?
MFUNCTION could go through the body of the lambda expression and replace
all the (fred x) forms with (meval fred x).

You might also want to check out *EVALHOOK* and *APPLYHOOK* to see whether
they can be used to do what you're doing.

>A related question is the following (this may be a commonly
>asked question, but please bear with me, I  am coming
>originally from a scheme background). I would like to have a
>version of apply which does not care whether the object
>applied to is a function or a macro or a special-form (this
>is possible in scheme). 

It's not possible in Common Lisp, nor do I think it's possible in Scheme.
Macros expect to receive the entire form, with unevaluated arguments, as an
argument, but APPLY expects its arguments to be evaluated and can't get the
entire form.  Special forms are handled specially by the interpreter or
compiler, and it doesn't make sense to apply them.

>Alternatively, I could work around this problem if I had a
>version of eval which does not evaluate its arguments.

EVAL only evaluates the arguments when the car of the form is a function
name.
-- 
Barry Margolin
System Manager, Thinking Machines Corp.

······@think.com          {uunet,harvard}!think!barmar