From: ···@usa.net
Subject: defstruct & defconstant in the same file
Date: 
Message-ID: <8fua5p$66i$1@nnrp1.deja.com>
Can I refer to functions created by defstruct in a defconstant
in the same file?

----- x.lisp -----
(defstruct x a b c)
(defconstant y (make-x))
----- x.lisp -----

$ cmucl -eval '(compile-file "x")'
Error in KERNEL:%COERCE-TO-FUNCTION:  the function MAKE-X is undefined.
.....

when I surround `defstruct' with 'eval-when (compile load eval)', this
works fine.
only CMUCL requires this, both CLISP and ACL work fine.

So, is CMUCL's behavior legal?


Sent via Deja.com http://www.deja.com/
Before you buy.

From: Tim Moore
Subject: Re: defstruct & defconstant in the same file
Date: 
Message-ID: <8fugbu$70a$0@216.39.145.192>
On Wed, 17 May 2000 ···@usa.net wrote:

> Can I refer to functions created by defstruct in a defconstant
> in the same file?
Yes, if you wrap it in eval-when, as you discovered.
> 
> ----- x.lisp -----
> (defstruct x a b c)
> (defconstant y (make-x))
> ----- x.lisp -----
> 
> $ cmucl -eval '(compile-file "x")'
> Error in KERNEL:%COERCE-TO-FUNCTION:  the function MAKE-X is undefined.
> .....
> 
> when I surround `defstruct' with 'eval-when (compile load eval)', this
> works fine.
> only CMUCL requires this, both CLISP and ACL work fine.
> 
> So, is CMUCL's behavior legal?

Yes.  From the hyperspec:

If a defconstant form appears as a top level form, the compiler must
recognize that name names a constant variable. An implementation may
choose to evaluate the
value-form at compile time, load time, or both. Therefore, users must
ensure that the initial-value can be evaluated at compile time (regardless
of whether or not
references to name appear in the file) and that it always evaluates to the
same value. 

And from the specification of defstruct:

The functions which defstruct generates are not defined in the
compile time environment, although the compiler may save enough
information about the functions to code subsequent calls inline. The #S
reader macro might or might not
recognize the newly defined structure type name at compile time. 

Tim
From: Tim Bradshaw
Subject: Re: defstruct & defconstant in the same file
Date: 
Message-ID: <ey3g0rh414c.fsf@cley.com>
* sds  wrote:

> So, is CMUCL's behavior legal?

I think so, although I could easily be wrong.  The definition of
DEFSTRUCT says:

    If a defstruct form appears as a top level form, the compiler must
    make the structure type name recognized as a valid type name in
    subsequent declarations (as for deftype) and make the structure
    slot readers known to setf. In addition, the compiler must save
    enough information about the structure type so that further
    defstruct definitions can use :include in a subsequent deftype in
    the same file to refer to the structure type name. The functions
    which defstruct generates are not defined in the compile time
    environment, although the compiler may save enough information
    about the functions to code subsequent calls inline. The #S reader
    macro might or might not recognize the newly defined structure
    type name at compile time.

Which means that the MAKE-x function need not be available at compile
time.

DEFCONSTANT then says:

    If a defconstant form appears as a top level form, the compiler
    must recognize that name names a constant variable. An
    implementation may choose to evaluate the value-form at compile
    time, load time, or both. Therefore, users must ensure that the
    initial-value can be evaluated at compile time (regardless of
    whether or not references to name appear in the file) and that it
    always evaluates to the same value.

So it needs to be the case that the MAKE-x function is available at
compile time.

So you need an EVAL-WHEN if you want to be sure.

I think!

--tim
From: Lieven Marchand
Subject: Re: defstruct & defconstant in the same file
Date: 
Message-ID: <3922eff9$0$12153@bru5-newsr2.be.uu.net>
···@usa.net writes:

> Can I refer to functions created by defstruct in a defconstant
> in the same file?
> 
> ----- x.lisp -----
> (defstruct x a b c)
> (defconstant y (make-x))
> ----- x.lisp -----
> 
> $ cmucl -eval '(compile-file "x")'
> Error in KERNEL:%COERCE-TO-FUNCTION:  the function MAKE-X is undefined.
> .....
> 
> when I surround `defstruct' with 'eval-when (compile load eval)', this
> works fine.
> only CMUCL requires this, both CLISP and ACL work fine.
> 

Which version of ACL are you using? In mine, it complains as it should:
USER(1): (compile-file "foo.lisp")
;;; Compiling file foo.lisp
; While compiling (:TOP-LEVEL-FORM "foo.lisp" 20):
Error: attempt to call `MAKE-X' which is an undefined function.
  [condition type: UNDEFINED-FUNCTION]

> So, is CMUCL's behavior legal?
> 

Yes. From CLHS 8.1 Macro DEFSTRUCT

# If a defstruct form appears as a top level form, the compiler must
# make the structure type name recognized as a valid type name in
# subsequent declarations (as for deftype) and make the structure slot
# readers known to setf. In addition, the compiler must save enough
# information about the structure type so that further defstruct
# definitions can use :include in a subsequent deftype in the same file
# to refer to the structure type name. The functions which defstruct
# generates are not defined in the compile time environment, although
# the compiler may save enough information about the functions to code
# subsequent calls inline. The #S reader macro might or might not
# recognize the newly defined structure type name at compile time.

The constuctor MAKE-X is one of the functions which defstruct generates.

ObOffTopic: shouldn't it be "the functions that defstruct generates"
or am I missing a subtlety of English grammar?

-- 
Lieven Marchand <···@bewoner.dma.be>
If there are aliens, they play Go. -- Lasker
From: Phil Stubblefield
Subject: Re: defstruct & defconstant in the same file
Date: 
Message-ID: <392305D8.630F10C9@rpal.rockwell.com>
Lieven Marchand <···@bewoner.dma.be> wrote:
> 
> [...]
> 
> The constuctor MAKE-X is one of the functions which defstruct
> generates.
> 
> ObOffTopic: shouldn't it be "the functions that defstruct
> generates" or am I missing a subtlety of English grammar?

I was taught that the proper word in this context is "that," but
from what I've read and heard over the years, using "which" is a
*very* common error.

A quick Google search turned up the following URL:

  http://www.quinion.com/words/usagenotes/un-which.htm

It explains the distinction between the two and discusses how the
usage has changed over time.


Phil Stubblefield
Rockwell Palo Alto Laboratory                               206/655-3204
http://www.rpal.rockwell.com/~phil                ····@rpal.rockwell.com
From: William Deakin
Subject: Re: defstruct & defconstant in the same file
Date: 
Message-ID: <3923B550.47C3931F@pindar.com>
Lieven Marchand wrote:

> The constuctor MAKE-X is one of the functions which defstruct generates.
>
> ObOffTopic: shouldn't it be "the functions that defstruct generates" or am
> I missing a subtlety of English grammar?

Both are commonly used [As an example: on BBC Radio I have heard both]. I
prefer the `that' form but this is a personal preference.

Cheers,

:) will
From: Jeff Dalton
Subject: Re: defstruct & defconstant in the same file
Date: 
Message-ID: <x21z2zddv2.fsf@todday.aiai.ed.ac.uk>
Anyway, there is supposedly a differece in meaning (or something very
close to meaning).  If the entities in question have already been
sufficiently specified, and you're just adding some information about
them, use "which"; but if the information is needed to identify what
you're talking about, use "that".  [Something like that.]

So... since "the functions" does not tell us what functions are
meant, and since "defstruct generates" completes the task of telling
us what functions are meant, it should be "the functions that
defstruct generates".