From: Scott McIntire
Subject: Function builder
Date: 
Message-ID: <opApa.319254$OV.354014@rwcrnsc54>
I'm trying to create a function from the value of a symbol. Here is my
attempt below. In Graham's macro book (and else where) it seems that one
defines a macro to build a function from an expression. What I am interested
in is taking the value of a symbol, perhaps processing it in some way, and
building a function. The code below will work on some lisps when loading but
will not work when compiling. The compiler error was that the there was an
attempt to take the value of the unbound variable 'rule.

(defun compiled-rule (rule)
  (macrolet ((fun () `#',(process-rule rule)))
    (fun)))

Any ideas?

From: Kaz Kylheku
Subject: Re: Function builder
Date: 
Message-ID: <cf333042.0304231434.1404aec5@posting.google.com>
"Scott McIntire" <····················@attbi.com> wrote in message news:<······················@rwcrnsc54>...
> I'm trying to create a function from the value of a symbol. Here is my
> attempt below. In Graham's macro book (and else where) it seems that one
> defines a macro to build a function from an expression. What I am interested
> in is taking the value of a symbol, perhaps processing it in some way, and
> building a function. The code below will work on some lisps when loading but
> will not work when compiling. The compiler error was that the there was an
> attempt to take the value of the unbound variable 'rule.
> 
> (defun compiled-rule (rule)
>   (macrolet ((fun () `#',(process-rule rule)))
>     (fun)))

Try:

  (defun compiled-rule (rule)
    (compile nil (eval `(function ,(process-rule rule))))

This asssumes that PROCESS-RULE produces a form that can serve as an
argument to the FUNCTION operator.

If PROCESS-RULE can be hacked to produce a lambda expression---a list
with the symbol LAMBDA in its first position, etc---then you can take
advantage of the provision that COMPILE takes a lambda expression
instead of a function object:

  (defun compiled-rule (rule)
    (compile nil (process-rule rule)))

You really can't get rid of the EVAL if you want to use the FUNCTION
operator to dynamically create a different function each time. A macro
won't do, because its substitution takes place once, at compile tmie.
From: Peter Seibel
Subject: Re: Function builder
Date: 
Message-ID: <m38yu113vs.fsf@javamonkey.com>
"Scott McIntire" <····················@attbi.com> writes:

> I'm trying to create a function from the value of a symbol. Here is
> my attempt below. In Graham's macro book (and else where) it seems
> that one defines a macro to build a function from an expression.
> What I am interested in is taking the value of a symbol, perhaps
> processing it in some way, and building a function. The code below
> will work on some lisps when loading but will not work when
> compiling. The compiler error was that the there was an attempt to
> take the value of the unbound variable 'rule.
> 
> (defun compiled-rule (rule)
>   (macrolet ((fun () `#',(process-rule rule)))
>     (fun)))

That's because macros (and macrolets) run at compile time. They
transform the source that exists at compile time; they know nothing
about the data that will exist later, at runtime.

> Any ideas?

Depending on what you're actually trying to do you might be able to
achieve the effect you want either by making compiled-rule a macro
itself or by some skillful use of EVAL-WHEN.

But step one has to be understanding why 'rule' has no value at
compile time.

-Peter

-- 
Peter Seibel                                      ·····@javamonkey.com

  The intellectual level needed   for  system design is  in  general
  grossly  underestimated. I am  convinced  more than ever that this
  type of work is very difficult and that every effort to do it with
  other than the best people is doomed to either failure or moderate
  success at enormous expense. --Edsger Dijkstra
From: Barry Margolin
Subject: Re: Function builder
Date: 
Message-ID: <bKApa.15$G25.476@paloalto-snr1.gtei.net>
In article <······················@rwcrnsc54>,
Scott McIntire <····················@attbi.com> wrote:
>I'm trying to create a function from the value of a symbol. Here is my
>attempt below. In Graham's macro book (and else where) it seems that one
>defines a macro to build a function from an expression. What I am interested
>in is taking the value of a symbol, perhaps processing it in some way, and
>building a function. The code below will work on some lisps when loading but
>will not work when compiling. The compiler error was that the there was an
>attempt to take the value of the unbound variable 'rule.
>
>(defun compiled-rule (rule)
>  (macrolet ((fun () `#',(process-rule rule)))
>    (fun)))
>
>Any ideas?

The compiler is trying to expand the macro at compile time, but the
variable RULE isn't available until the function is called at run time.
How can the compiler expand a macro based on information that won't be
available until later?

How about:

(defun compiled-rule (rule)
  (funcall (process-rule rule)))

-- 
Barry Margolin, ··············@level3.com
Genuity Managed Services, a Level(3) Company, Woburn, 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.
From: Scott McIntire
Subject: Re: Function builder
Date: 
Message-ID: <_bBpa.319660$OV.353917@rwcrnsc54>
The problem I have comes down to this:

When I try to use a function to form the body of a new lambda that is built
on the fly I either
a). Get a lambda expression that returns the body unevaluated
Example:
#'(lambda (make-body rule))
Or,
b). A lambda expression that is quoted.
Example:
`#'(lambda ,(make-body rule))

I want to take a symbol, take its value (which should be an expression),
possibly process that expression and have this new expression be the body of
a new function. What do I have to do?


"Barry Margolin" <··············@level3.com> wrote in message
·····················@paloalto-snr1.gtei.net...
> In article <······················@rwcrnsc54>,
> Scott McIntire <····················@attbi.com> wrote:
> >I'm trying to create a function from the value of a symbol. Here is my
> >attempt below. In Graham's macro book (and else where) it seems that one
> >defines a macro to build a function from an expression. What I am
interested
> >in is taking the value of a symbol, perhaps processing it in some way,
and
> >building a function. The code below will work on some lisps when loading
but
> >will not work when compiling. The compiler error was that the there was
an
> >attempt to take the value of the unbound variable 'rule.
> >
> >(defun compiled-rule (rule)
> >  (macrolet ((fun () `#',(process-rule rule)))
> >    (fun)))
> >
> >Any ideas?
>
> The compiler is trying to expand the macro at compile time, but the
> variable RULE isn't available until the function is called at run time.
> How can the compiler expand a macro based on information that won't be
> available until later?
>
> How about:
>
> (defun compiled-rule (rule)
>   (funcall (process-rule rule)))
>
> --
> Barry Margolin, ··············@level3.com
> Genuity Managed Services, a Level(3) Company, Woburn, 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.
From: Barry Margolin
Subject: Re: Function builder
Date: 
Message-ID: <HcCpa.17$G25.586@paloalto-snr1.gtei.net>
In article <······················@rwcrnsc54>,
Scott McIntire <····················@attbi.com> wrote:
>The problem I have comes down to this:
>
>When I try to use a function to form the body of a new lambda that is built
>on the fly I either
>a). Get a lambda expression that returns the body unevaluated
>Example:
>#'(lambda (make-body rule))
>Or,
>b). A lambda expression that is quoted.
>Example:
>`#'(lambda ,(make-body rule))
>
>I want to take a symbol, take its value (which should be an expression),
>possibly process that expression and have this new expression be the body of
>a new function. What do I have to do?

(defun expression-to-function (expr)
  (coerce `(lambda () ,expr) 'function))

-- 
Barry Margolin, ··············@level3.com
Genuity Managed Services, a Level(3) Company, Woburn, 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.
From: Pascal Costanza
Subject: Re: Function builder
Date: 
Message-ID: <costanza-5E2FF0.21111323042003@news.netcologne.de>
In article <······················@rwcrnsc54>,
 "Scott McIntire" <····················@attbi.com> wrote:

> I want to take a symbol, take its value (which should be an expression),
> possibly process that expression and have this new expression be the body of
> a new function. What do I have to do?

This just sounds like evaluating the expression, as in:

(defun make-lambda (expression)
  (lambda () (eval expression)))

But you have given just a technical description of what you want. What 
do you actually want to use this for?


Pascal

-- 
"If I could explain it, I wouldn't be able to do it."
A.M.McKenzie
From: Kalle Olavi Niemitalo
Subject: Re: Function builder
Date: 
Message-ID: <873ck9xbzm.fsf@Astalo.kon.iki.fi>
"Scott McIntire" <····················@attbi.com> writes:

> (defun compiled-rule (rule)
>   (macrolet ((fun () `#',(process-rule rule)))
>     (fun)))

So PROCESS-RULE returns a lambda expression, and COMPILED-RULE
should return a corresponding function.  This ought to do it:

  (defun compiled-rule (rule)
    (compile nil (process-rule rule)))

Or if you want an interpreted function:

  (defun not-quite-compiled-rule (rule)
    (coerce (process-rule rule) 'function))