From: Eli Gottlieb
Subject: Pure Curiousity
Date: 
Message-ID: <FcsGf.1996$j53.894@twister.nyroc.rr.com>
Is there an equivalent of apply that works on special forms?

From: ········@gmail.com
Subject: Re: Pure Curiousity
Date: 
Message-ID: <1139432876.906007.127730@g44g2000cwa.googlegroups.com>
Eli wrote:
>Is there an equivalent of apply that works on special forms?

Maybe this would be an intersting read?
http://www.linearity.org/bawden/mtt/

Nick
From: Kaz Kylheku
Subject: Re: Pure Curiousity
Date: 
Message-ID: <1139433628.421276.176850@g44g2000cwa.googlegroups.com>
Eli Gottlieb wrote:
> Is there an equivalent of apply that works on special forms?

Functions don't have complete control over the evaluation of a function
call. It's the call site which reduces argument lists and prepares the
call, at run time. So there is considerable flexibility in how input
can be prepared for a function, and various applicators. FUNCALL,
APPLY, MAPCAR and so on.

The input to a special form is syntax. The special form analyzes syntax
and applies arbitrary semantics to it. So there is no way to factor out
any of that evaluation outside of the control of that special form. So
in order to make an applicator for special forms, you have to accept
that the input is syntax which includes the special form's operator.
You generate that syntax, and then pass it to EVAL.

(defun special-funcall (operator &rest args)
  (eval `(,operator ,@args)))

(defun special-apply (operator &rest args)
  (eval `(,operator ,@(butlast args) ,@(first (last args))))


(special-funcall 'let '((x 3)) '(list (* x x)))

--> (9)


(special-apply 'let '(((x 3)) (list x)))

--> (3)
From: Eli Gottlieb
Subject: Re: Pure Curiousity
Date: 
Message-ID: <SOuGf.115$Zl1.5@twister.nyroc.rr.com>
Kaz Kylheku wrote:
> Eli Gottlieb wrote:
> 
>>Is there an equivalent of apply that works on special forms?
> 
> 
> Functions don't have complete control over the evaluation of a function
> call. It's the call site which reduces argument lists and prepares the
> call, at run time. So there is considerable flexibility in how input
> can be prepared for a function, and various applicators. FUNCALL,
> APPLY, MAPCAR and so on.
> 
> The input to a special form is syntax. The special form analyzes syntax
> and applies arbitrary semantics to it. So there is no way to factor out
> any of that evaluation outside of the control of that special form. So
> in order to make an applicator for special forms, you have to accept
> that the input is syntax which includes the special form's operator.
> You generate that syntax, and then pass it to EVAL.
> 
> (defun special-funcall (operator &rest args)
>   (eval `(,operator ,@args)))
> 
> (defun special-apply (operator &rest args)
>   (eval `(,operator ,@(butlast args) ,@(first (last args))))
> 
> 
> (special-funcall 'let '((x 3)) '(list (* x x)))
> 
> --> (9)
> 
> 
> (special-apply 'let '(((x 3)) (list x)))
> 
> --> (3)
> 
Wasn't I hearing a couple days ago that eval wasn't good to use, because 
it doesn't know the lexical environment?  Whatever happened to that?
From: Kaz Kylheku
Subject: Re: Pure Curiousity
Date: 
Message-ID: <1139441012.434616.154850@g47g2000cwa.googlegroups.com>
Eli Gottlieb wrote:
> Kaz Kylheku wrote:
> > Eli Gottlieb wrote:
> >
> >>Is there an equivalent of apply that works on special forms?
> >
> >
> > Functions don't have complete control over the evaluation of a function
> > call. It's the call site which reduces argument lists and prepares the
> > call, at run time. So there is considerable flexibility in how input
> > can be prepared for a function, and various applicators. FUNCALL,
> > APPLY, MAPCAR and so on.
> >
> > The input to a special form is syntax. The special form analyzes syntax
> > and applies arbitrary semantics to it. So there is no way to factor out
> > any of that evaluation outside of the control of that special form. So
> > in order to make an applicator for special forms, you have to accept
> > that the input is syntax which includes the special form's operator.
> > You generate that syntax, and then pass it to EVAL.
> >
> > (defun special-funcall (operator &rest args)
> >   (eval `(,operator ,@args)))
> >
> > (defun special-apply (operator &rest args)
> >   (eval `(,operator ,@(butlast args) ,@(first (last args))))
> >
> >
> > (special-funcall 'let '((x 3)) '(list (* x x)))
> >
> > --> (9)
> >
> >
> > (special-apply 'let '(((x 3)) (list x)))
> >
> > --> (3)
> >
> Wasn't I hearing a couple days ago that eval wasn't good to use, because
> it doesn't know the lexical environment?  Whatever happened to that?

APPLY also doesn't know the lexical environment. All evaluation of the
parameters happens outside of APPLY, so that's irrelevant.

The same can be said about SPECIAL-APPLY, and SPECIAL-FUNCALL.

They are functions. The lexical environment isn't passed implicitly
into functions. So this won't work:

   (let ((x 3))
     (special-funcall 'progn '(+ x x)))

Why should it? The expression is a literal. The X within that literal
is just a data symbol, and isn't in the scope of the LET at all.
Everything is passed into a function, which applies the operator to the
given syntax. So of course the surrounding lexical scope is invisible.

If you had a Lisp with an extension that allows lexical scopes into
EVAL, you could add an optional environment argument to
SPECIAL-FUNCALL, which would be passed down to the embedded EVAL.

You can also use backquote to interpolate values into the form:

   (let ((x 3))
     (special-funcall 'progn '(+ ,x ,x)))

   --> 6

All the evaluation that happens before the call to SPECIAL-FUNCALL does
have the lexical scope available.
From: Marcin 'Qrczak' Kowalczyk
Subject: Re: Pure Curiousity
Date: 
Message-ID: <87ek2dioq2.fsf@qrnik.zagroda>
Eli Gottlieb <···········@gmail.com> writes:

> Is there an equivalent of apply that works on special forms?

In general it makes no sense conceptually, so it can't exist.

Imagine it exists and is called apply-macro. If
   (let ((x (+ 2 2)))
      (apply #'+ (list x 3)))
is equivalent to
   (+ (+ 2 2) 3)
then
   (let ((x ((y 1))))
      (apply-macro #'let (list x y)))
would perhaps be equivalent to
   (let ((y 1)) y)
but it makes no sense because ((y 1)) is not an expression,
no matter what apply-macro tries to do afterwards.

-- 
   __("<         Marcin Kowalczyk
   \__/       ······@knm.org.pl
    ^^     http://qrnik.knm.org.pl/~qrczak/
From: Kaz Kylheku
Subject: Re: Pure Curiousity
Date: 
Message-ID: <1139434242.120102.104710@g43g2000cwa.googlegroups.com>
Marcin 'Qrczak' Kowalczyk wrote:
> Eli Gottlieb <···········@gmail.com> writes:
>
> > Is there an equivalent of apply that works on special forms?
>
> In general it makes no sense conceptually, so it can't exist.
>
> Imagine it exists and is called apply-macro. If
>    (let ((x (+ 2 2)))
>       (apply #'+ (list x 3)))
> is equivalent to
>    (+ (+ 2 2) 3)
> then
>    (let ((x ((y 1))))
>       (apply-macro #'let (list x y)))
> would perhaps be equivalent to
>    (let ((y 1)) y)
> but it makes no sense because ((y 1)) is not an expression,
> no matter what apply-macro tries to do afterwards.

(apply-macro #'let ...)

makes no sense because #'LET is (FUNCTION LET).  You have to live with
(APPLY-MACRO 'LET ...).

With SPECIAL-APPLY defined as:

  (defun special-apply (operator &rest args) (print args)
    (eval `(,operator ,@(butlast args) ,@(first (last args)))))

We can make it work like this:

  (let ((x '((y 1))))
    (special-apply 'let (list x 'y)))

  --> 1

No problem! (Aside from the stupidity of the whole thing).
From: Eli Gottlieb
Subject: Re: Pure Curiousity
Date: 
Message-ID: <qJsGf.107$Zl1.36@twister.nyroc.rr.com>
Marcin 'Qrczak' Kowalczyk wrote:
> Eli Gottlieb <···········@gmail.com> writes:
> 
> 
>>Is there an equivalent of apply that works on special forms?
> 
> 
> In general it makes no sense conceptually, so it can't exist.
> 
> Imagine it exists and is called apply-macro. If
>    (let ((x (+ 2 2)))
>       (apply #'+ (list x 3)))
> is equivalent to
>    (+ (+ 2 2) 3)

Making sense.

> then
>    (let ((x ((y 1))))
>       (apply-macro #'let (list x y)))
> would perhaps be equivalent to
>    (let ((y 1)) y)
> but it makes no sense because ((y 1)) is not an expression,
> no matter what apply-macro tries to do afterwards.
> 
Not so making sense.  That code with apply-macro in it doesn't read at 
all well.
From: Pascal Costanza
Subject: Re: Pure Curiousity
Date: 
Message-ID: <44vcbaF4372oU1@individual.net>
Eli Gottlieb wrote:
> Is there an equivalent of apply that works on special forms?

What special forms can do is to control the evaluation of (some of) its 
arguments. This is handled by (effectively) compile-time processing of 
those special forms.

If you want to control evaluation at runtime, there are different 
possibilities. In the context of Lisp, reflection is one of the most 
thoroughly researched approaches. But this is not light material. See 
http://library.readscheme.org/page11.html for more information.


Pascal

-- 
My website: http://p-cos.net
Closer to MOP & ContextL:
http://common-lisp.net/project/closer/