From: Deepak Goel
Subject: Macros returning multiple values?
Date: 
Message-ID: <Pine.GSO.4.10.9909300202260.14554-100000@fosters.umd.edu>
it seems: 
If i want to replace the expression 
(+ 1 2 3 4) by (+ 1 (e) 4) 
by defining a macro e, i am unable to do so, apparently because macros
cannot return multiple values (or have i got it wrong? if so, help!!)

Shouldn't lisp allow macros to return multiple values?

--Deepak

From: Eric Peterson
Subject: Re: Macros returning multiple values?
Date: 
Message-ID: <hhcpuz0fq5q.fsf@adms.mitre.org>
>>>>> "Deepak" == Deepak Goel <·····@Glue.umd.edu> writes:

    Deepak> it seems: If i want to replace the expression (+ 1 2 3 4)
    Deepak> by (+ 1 (e) 4) by defining a macro e, i am unable to do
    Deepak> so, apparently because macros cannot return multiple
    Deepak> values (or have i got it wrong? if so, help!!)

    Deepak> Shouldn't lisp allow macros to return multiple values?

The chief purpose of a Common Lisp macro is to return source code that
can be compiled.  I don't think that's really what you want to do
here.  Source code, in this context, basically means a single list.

How about making E a function:

(defun e () '(2 3))

(apply #'+ `(1 ,@(e) 4))


Multple values are a specially case and need to be treated specially.
You need to "catch" them in a multiple-value-bind or some such.
Without such a "catcher", all values but the first multiple value are
ignored. 

I hope that helps.



-- 
Eric L. Peterson;  MITRE Corp.;  Artificial Intelligence Technologies Center
1820 Dolley Madison Blvd.; MS: QUAN; McLean, VA  22102-3481 
····@ai.mitre.org  http://www.cs.umd.edu/users/ericp
"I never meta-class I didn't like"  -anon
From: Pierre R. Mai
Subject: Re: Macros returning multiple values?
Date: 
Message-ID: <87ogekxy8u.fsf@orion.dent.isdn.cs.tu-berlin.de>
Eric Peterson <········@mitre.org> writes:

> How about making E a function:
> 
> (defun e () '(2 3))
> 
> (apply #'+ `(1 ,@(e) 4))

Or indeed let it use multiple values to do this, which _might_ be both 
more efficient and expressive, depending on the situation:

(defun e () (values 2 3))

(multiple-value-call #'+ 1 (e) 4)
=> 10

You could also make e a macro, if this is more suitable for some
reason, e.g. when unevaluated arguments are to be used, like e.g.

(defmacro nice-macro-form (form &environment env)
  `(values ,form ',form ',(macroexpand form env)))

(multiple-value-call #'format t
                     "~A is the result of evaluating ~S~%via ~S~%"
                     (nice-macro-form (when (> 3 4) t)))

>> NIL is the result of evaluating (WHEN (> 3 4) T)
>> via (IF (> 3 4) (PROGN NIL T) (COND))
=> NIL

> Multple values are a specially case and need to be treated specially.
> You need to "catch" them in a multiple-value-bind or some such.
> Without such a "catcher", all values but the first multiple value are
> ignored. 

Indeed.

Regs, Pierre.

-- 
Pierre Mai <····@acm.org>         PGP and GPG keys at your nearest Keyserver
  "One smaller motivation which, in part, stems from altruism is Microsoft-
   bashing." [Microsoft memo, see http://www.opensource.org/halloween1.html]
From: Christopher R. Barry
Subject: Re: Macros returning multiple values?
Date: 
Message-ID: <87so3wp879.fsf@2xtreme.net>
Deepak Goel <·····@Glue.umd.edu> writes:

> it seems: 
> If i want to replace the expression 
> (+ 1 2 3 4) by (+ 1 (e) 4) 
> by defining a macro e, i am unable to do so, apparently because macros
> cannot return multiple values (or have i got it wrong? if so, help!!)
> 
> Shouldn't lisp allow macros to return multiple values?

They can return source code that returns mulitple values. (They can return
any source code you care to generate.) There's no way I know of to write
some macro E that will splice in multiple values the way you want. You could
have E return (+ 2 3) instead giving you (+ 1 (+ 2 3) 4), or you can take
the good advice you recieved in other replies, or you could think very hard
and clearly about what problem it is you are trying to solve and model Lisp
to it appropriately.

Christopher
From: Deepak Goel
Subject: Re: Macros returning multiple values?
Date: 
Message-ID: <Pine.GSO.4.10.9909301558001.16907-100000@fosters.umd.edu>
Thanks to you and all other replies. 

(What i was trying to do is to write a general macro "aliasb" that's a
macro-writer in itself. Then, for example (aliasb e 2 4) would replace (e)
by 2 4 whereever it sees it. So that lisp should be able to make sense of
(* (e) 4) and give 32 and make sense of (+ (e) 3 4) and give 13.)

Why should LISP not allow macros to return multiple pieces of code?


]Deepak Goel <·····@Glue.umd.edu> writes:
]
]> it seems: 
]> If i want to replace the expression 
]> (+ 1 2 3 4) by (+ 1 (e) 4) 
]> by defining a macro e, i am unable to do so, apparently because macros
]> cannot return multiple values (or have i got it wrong? if so, help!!)
]> 
]> Shouldn't lisp allow macros to return multiple values?
]
]They can return source code that returns mulitple values. (They can return
]any source code you care to generate.) There's no way I know of to write
]some macro E that will splice in multiple values the way you want. You could
]have E return (+ 2 3) instead giving you (+ 1 (+ 2 3) 4), or you can take
]the good advice you recieved in other replies, or you could think very hard
]and clearly about what problem it is you are trying to solve and model Lisp
]to it appropriately.
]
]Christopher
]
]
]

--Deepak
From: Dorai Sitaram
Subject: Re: Macros returning multiple values?
Date: 
Message-ID: <7t0nna$9ia$1@news.gte.com>
In article <········································@fosters.umd.edu>,
Deepak Goel  <·····@Glue.umd.edu> wrote:
>(What i was trying to do is to write a general macro "aliasb" that's a
>macro-writer in itself. Then, for example (aliasb e 2 4) would replace (e)
>by 2 4 whereever it sees it. So that lisp should be able to make sense of
>(* (e) 4) and give 32 and make sense of (+ (e) 3 4) and give 13.)
>
>Why should LISP not allow macros to return multiple pieces of code?

Hmm.  Maybe it's the time of day, but this strikes me
as a very good question!  I don't know the answer.  I
suspect the reason may well be historical.  Lisp macros
predated multiple values, and they have always been
envisaged as things that convert one input sexpr to one
output sexpr.  Multiple pieces of code do not form a
sexpr.  In any case, this is an issue of multiple-value
extraction rather than production.  (Nothing prevents a
Lisp macro from returning multiple values, the macro
call can't use but the first of them.)

Multiple-value extraction in Lisp is syntactically
verbose anyway.  The context that uses the multiple
values always goes to some effort to extract the
individual values.  Expecting the context of a macro
call to automatically extract multiple values in one
particular (however persuasive) manner when no other
consumer of multiple values gets such a free pass may
strike some as inequitable.  I'd love to hear what KMP,
EN, BM, and our other bigwigs have to say on this
matter.

Intrigued,

--d
From: Gareth McCaughan
Subject: Re: Macros returning multiple values?
Date: 
Message-ID: <867ll810el.fsf@g.local>
Deepak Goel wrote:

> (What i was trying to do is to write a general macro "aliasb" that's a
> macro-writer in itself. Then, for example (aliasb e 2 4) would replace (e)
> by 2 4 whereever it sees it. So that lisp should be able to make sense of
> (* (e) 4) and give 32 and make sense of (+ (e) 3 4) and give 13.)

Yes, but *why* do you want to do that?

> Why should LISP not allow macros to return multiple pieces of code?

It would complicate the job of the macro-expansion system.

It would make it easier to write code that looks correct but
is actually terribly wrong:

    (defmultimacro foo (form) (values form form))
    (if (oracle)
      (foo 123)
      99)

It would, therefore, make it potentially much harder to tell
whether a particular piece of code is correct or not.

It would provide almost nothing that you couldn't do as well
or better by other means (e.g., PROGN).

It would emphasise a wrong view of what a Lisp program actually
is (a list whose elements may be lists, etc) rather than a better
one (various kinds of form, which happen to be represented by
lists for convenience).

Incidentally, there's a brief discussion of a related issue
(multiple values from *functions* being spliced in in the
sort of way you describe) in the excellent paper on the
history of Lisp by (I think) Gabriel and Steele. I don't
remember where I found it, but a web search will certainly
find it.

-- 
Gareth McCaughan  ················@pobox.com
sig under construction
From: Barry Margolin
Subject: Re: Macros returning multiple values?
Date: 
Message-ID: <1R3J3.390$854.13187@burlma1-snr2>
In article <··············@g.local>,
Gareth McCaughan  <················@pobox.com> wrote:
>Deepak Goel wrote:
>> Why should LISP not allow macros to return multiple pieces of code?
>
>It would complicate the job of the macro-expansion system.

And the evaluator in general.  Currently, much of the behavior of the
evaluator is conceptually simple.  The way that function-call forms are
evaluated can be described as:

(defun eval-function-call-form (form)
  (apply (function-definition (head form))
         (mapcar #'eval (tail form))))

If macros were allowed to return multiple values, which would get spliced
into the argument list, this gets much more complicated.

-- 
Barry Margolin, ······@bbnplanet.com
GTE Internetworking, Powered by BBN, Burlington, 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: Gareth McCaughan
Subject: Re: Macros returning multiple values?
Date: 
Message-ID: <86k8p6zl1b.fsf@g.local>
Barry Margolin wrote:

[someone else:]
>>> Why should LISP not allow macros to return multiple pieces of code?
[me:]
>> It would complicate the job of the macro-expansion system.
> 
> And the evaluator in general.  Currently, much of the behavior of the
> evaluator is conceptually simple.  The way that function-call forms are
> evaluated can be described as:
> 
> (defun eval-function-call-form (form)
>   (apply (function-definition (head form))
>          (mapcar #'eval (tail form))))
> 
> If macros were allowed to return multiple values, which would get spliced
> into the argument list, this gets much more complicated.

Quite right. I'd deliberately not said that because I was
thinking that all the macros have vanished by the time the
evaluator proper starts work, but of course that's not
true.

-- 
Gareth McCaughan  ················@pobox.com
sig under construction
From: Christopher R. Barry
Subject: Re: Macros returning multiple values?
Date: 
Message-ID: <87670rppzx.fsf@2xtreme.net>
Deepak Goel <·····@Glue.umd.edu> writes:

> Thanks to you and all other replies. 
> 
> (What i was trying to do is to write a general macro "aliasb" that's a
> macro-writer in itself. Then, for example (aliasb e 2 4) would replace (e)
> by 2 4 whereever it sees it. So that lisp should be able to make sense of
> (* (e) 4) and give 32 and make sense of (+ (e) 3 4) and give 13.)
> 
> Why should LISP not allow macros to return multiple pieces of code?

I think experience with macro writing will teach you this best. Here's
something you might find interesting though that is kinda related:
DEFINE-SYMBOL-MACRO. Example:

  (define-symbol-macro e '(2 4))

You can now do

  > e
 => (2 4)

You can also now do

  > `(+ ,@e 3 4)
 => (+ 2 4 3 4)

And since you'll be interested in evaluating the resulting form
(without calling EVAL), wrap it in a MACROLET and you're done:

  > (macrolet ((do-it ()
		 `(+ ,@e 3 4)))
      (do-it))
 => 13

Nitpick: This last example could look a lot cleaner if CL had
anonymous macros. I remember writing a lot of DEFMACRO forms last
summer with multiple nested MACROLETs and things would have been a lot
nicer if I didn't have to name the macros MACROLET introduced.

  ((lambda (x y) (+ x y)) 3 5) => 8
  ((macrolambda (x y) `(+ ,x ,y)) 3 5) => 8

It would be nice if CL gave you a way to make forms like this.
Thinking about it though, I don't know how it could be done
cleanly.... Oh well.

Christopher
From: Hartmann Schaffer
Subject: Re: Macros returning multiple values?
Date: 
Message-ID: <NbVI3.178966$5r2.312315@tor-nn1.netcom.ca>
In article <········································@fosters.umd.edu>,
	Deepak Goel <·····@Glue.umd.edu> writes:
> Thanks to you and all other replies. 
> 
> (What i was trying to do is to write a general macro "aliasb" that's a
> macro-writer in itself. Then, for example (aliasb e 2 4) would replace (e)
> by 2 4 whereever it sees it. So that lisp should be able to make sense of
> (* (e) 4) and give 32 and make sense of (+ (e) 3 4) and give 13.)
> 
> Why should LISP not allow macros to return multiple pieces of code?

i would say that with what you want to do you open a whole can of
worms.  the only thing that connects multiple values is that they are
returned together from a function call.  most if not all possible uses
of multiple values is to assign each of them to a separate entity that
is clearly distingt from each other value, with no sequential relation.
what you want is a sequence of values that you want to use one after the
other.  Lisp provides this facility already with the ,@ construct

> ...

-- 

Hartmann Schaffer

It is better to fill your days with life than your life with days
From: Deepak Goel
Subject: Re: Macros returning multiple values?
Date: 
Message-ID: <Pine.GSO.4.10.9910010112450.19039-100000@fosters.umd.edu>
On Fri, 1 Oct 1999, Hartmann Schaffer wrote:

]In article <········································@fosters.umd.edu>,
]	Deepak Goel <·····@Glue.umd.edu> writes:
]> Thanks to you and all other replies. 
]> 
]> (What i was trying to do is to write a general macro "aliasb" that's a
]> macro-writer in itself. Then, for example (aliasb e 2 4) would replace (e)
]> by 2 4 whereever it sees it. So that lisp should be able to make sense of
]> (* (e) 4) and give 32 and make sense of (+ (e) 3 4) and give 13.)
]> 
]> Why should LISP not allow macros to return multiple pieces of code?
]
]i would say that with what you want to do you open a whole can of
]worms.  the only thing that connects multiple values is that they are
]returned together from a function call.  most if not all possible uses
]of multiple values is to assign each of them to a separate entity that
]is clearly distingt from each other value, with no sequential relation.
]what you want is a sequence of values that you want to use one after the
]other.  Lisp provides this facility already with the ,@ construct

hi

it does?

then how do i achieve the above desired effect using this?
From: Pierre R. Mai
Subject: Re: Macros returning multiple values?
Date: 
Message-ID: <873dvvxx0h.fsf@orion.dent.isdn.cs.tu-berlin.de>
Deepak Goel <·····@Glue.umd.edu> writes:

> ]other.  Lisp provides this facility already with the ,@ construct
> 
> hi
> 
> it does?
> 
> then how do i achieve the above desired effect using this?

While it is possible to achieve something of the sort you are trying
to achieve with various ugly hacks (wrapping the whole form in either
eval or a macrolet), it is neither directly the thing you wanted to
do, nor should you really do it, for reasons others have posted here
already.

There are most definitely better ways of achieving what you _really_
should be trying to achieve, so it would help to see some realistic
code/description of what you were trying to achieve in the first
place.

Regs, Pierre.

-- 
Pierre Mai <····@acm.org>         PGP and GPG keys at your nearest Keyserver
  "One smaller motivation which, in part, stems from altruism is Microsoft-
   bashing." [Microsoft memo, see http://www.opensource.org/halloween1.html]
From: Erik Naggum
Subject: Re: Macros returning multiple values?
Date: 
Message-ID: <3147667114175965@naggum.no>
* Deepak Goel <·····@Glue.umd.edu>
| Shouldn't lisp allow macros to return multiple values?

  no.

#:Erik