From: T. V. Raman
Subject: Weird lucid behaviour: calling macros inside files at top level
Date: 
Message-ID: <1992Aug11.203152.26760@cs.cornell.edu>
Hi!

I am running into a weird Lucid bug.

I have a macro that expands into a defstruct. The defstruct is of type
list, and the field names are obtained from a list. 
Thus 
(setf *list-of-dimensions* 
'(a b c))
 and then (define-point)
generates 
(defstruct (point 
(:named )
(:type list))
a b c)

The macro is expanding correctly, and the structure definition is
generated correctly if I compile and load the file and then call the
macro at the lucid prompt.  However If I place the macro call inside
the file at top level, I get some weird behaviour, namely:
The fields in the structure are no longer in the same order as they
appear in the list *list-of-dimensions*, but have been rotated one
step!

Is this is a bug or am I doing something I am not supposed to?

--Raman
-- 
   T. V. Raman <·····@cs.cornell.edu>Tel: (607)255-7421 R 272-2435
		       Office: 5162 Upson Hall,
Department of Computer Science, Cornell University Ithaca NY 14853-6201
		Res: 226 Bryant Avenue Ithaca NY 14850

From: Barry Margolin
Subject: Re: Weird lucid behaviour: calling macros inside files at top level
Date: 
Message-ID: <16a9a7INNq3@early-bird.think.com>
In article <······················@cs.cornell.edu> ·····@cs.cornell.edu (T. V. Raman) writes:
>I have a macro that expands into a defstruct. The defstruct is of type
>list, and the field names are obtained from a list. 

It would help if you posted the code.  It's hard to get a precise
understanding of what you're doing from the above description.

I suspect your problem is that you aren't assigning the list to the
variable at compile time (i.e. with (EVAL-WHEN (COMPILE) ...)), or you're
side effecting a constant.
-- 
Barry Margolin
System Manager, Thinking Machines Corp.

······@think.com          {uunet,harvard}!think!barmar
From: T. V. Raman
Subject: Re: Weird lucid behaviour: calling macros inside files at top level
Date: 
Message-ID: <1992Aug12.123449.5482@cs.cornell.edu>
······@think.com (Barry Margolin) writes:

>In article <······················@cs.cornell.edu> ·····@cs.cornell.edu (T. V. Raman) writes:
>>I have a macro that expands into a defstruct. The defstruct is of type
>>list, and the field names are obtained from a list. 

>It would help if you posted the code.  It's hard to get a precise
>understanding of what you're doing from the above description.

>I suspect your problem is that you aren't assigning the list to the
>variable at compile time (i.e. with (EVAL-WHEN (COMPILE) ...)), or you're
>side effecting a constant.

I suspect that the reason is the above. I have never used eval-when
and in my code I simply have

(defvar *list-of-dimensions* ...)

and some calls at top level in the file that push items on to this
list using
(push item *list-of-dimensions*)

So I guess there is something about evaluating things at compile-time
that I never learnt about. 

Could you summarize for me what eval-when does and where and how it is
normally used?

Thanks,

--Raman


p.S. If you feel that replying to this on the net would be of little
else to everyone else, I would appreciate it if you could reply to  me
by email


>-- 
>Barry Margolin
>System Manager, Thinking Machines Corp.

>······@think.com          {uunet,harvard}!think!barmar
-- 
   T. V. Raman <·····@cs.cornell.edu>Tel: (607)255-7421 R 272-2435
		       Office: 5162 Upson Hall,
Department of Computer Science, Cornell University Ithaca NY 14853-6201
		Res: 226 Bryant Avenue Ithaca NY 14850
From: Barry Margolin
Subject: Re: Weird lucid behaviour: calling macros inside files at top level
Date: 
Message-ID: <16d84jINNltb@early-bird.think.com>
In article <·····················@cs.cornell.edu> ·····@cs.cornell.edu (T. V. Raman) writes:
>······@think.com (Barry Margolin) writes:
>>I suspect your problem is that you aren't assigning the list to the
>>variable at compile time (i.e. with (EVAL-WHEN (COMPILE) ...)), or you're
>>side effecting a constant.
>
>I suspect that the reason is the above. I have never used eval-when
>and in my code I simply have
>
>(defvar *list-of-dimensions* ...)
>
>and some calls at top level in the file that push items on to this
>list using
>(push item *list-of-dimensions*)

The problem is that ordinary forms are executed only when the file is
loaded.  Thus, the PUSH won't happen until then.  However, macros are
expanded by the compiler.  So if you have a macro that expands into a
DEFSTRUCT, and it wants to use a global variable in computing the
expansion, then you must make sure that this variable has been assigned
before the macro is expanded.

You can do this in two ways.  One is to load the file that contains the
DEFVAR and PUSHes before compiling anything that uses the macro.

But if you want to use the macro in the same file as those forms appear,
you have to use EVAL-WHEN to force them to execute during the compilation
of the file.

(eval-when (compile load eval)
  (defvar ...)
  (push ...)
  ...)
-- 
Barry Margolin
System Manager, Thinking Machines Corp.

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