From: Kenneth P. Turvey
Subject: More problems.. DEFTYPE question.
Date: 
Message-ID: <slrn7khkcg.kv2.kturvey@pug1.sprocketshop.com>
When I compile the code below in a file using COMPILE-FILE, I get an
error indicating that StateType is not defined when the second form is
compiled.  If I just cut and paste the DEFTYPE form into the lisp reader
and then run COMPILE-FILE again everything works as one would expect. 

(deftype StateType () '(integer 0 23)) 

(defconstant *SolvedState*
  (make-array 20
              :element-type 'StateType
              :initial-element 0 ))


I suspect that this is due to the comment on page 63 of CLTL2, but I
could use a novice translation.  Any help would be appreciated.

|  X3J13 voted in March 1989 <50> to clarify that, while defining forms
| normally appear at top level, it is meaningful to place them in 
| non-top-level contexts; DEFTYPE must define the expander function
| within the enclosing lexical environment, not within the global
| environment.

Does this indicate that I may not use DEFTYPE without enclosing it in a
PROGN or something?  I would like it to be in effect for at least the
whole file.  How do I do this? 


-- 
Kenneth P. Turvey <·······@SprocketShop.com> 
----------------- http://www.tranquility.net/~kturvey

  Reasonable people adapt themselves to the world.  Unreasonable people
  attempt to adapt the world to themselves.  All progress, therefore,
  depends on unreasonable people.  -- George Bernard Shaw

From: Kent M Pitman
Subject: Re: More problems.. DEFTYPE question.
Date: 
Message-ID: <sfwbtfbrp7e.fsf@world.std.com>
·······@pug1.sprocketshop.com (Kenneth P. Turvey) writes:

> When I compile the code below in a file using COMPILE-FILE, I get an
> error indicating that StateType is not defined when the second form is
> compiled.  If I just cut and paste the DEFTYPE form into the lisp reader
> and then run COMPILE-FILE again everything works as one would expect. 
> 
> (deftype StateType () '(integer 0 23)) 
> 
> (defconstant *SolvedState*
>   (make-array 20
>               :element-type 'StateType
>               :initial-element 0 ))

Well, in the indicated implementation, if it works one way and not the
other, it may be an issue of compile-time side-effect.  Possibly
your deftype takes place at load-time, not compile-time.  The definition
says it should happen at compile-time, unless I'm mis-reading.  See the
last paragraph in the description of DEFTYPE in CLHS.

> I suspect that this is due to the comment on page 63 of CLTL2, but I
> could use a novice translation.  Any help would be appreciated.
> 
> |  X3J13 voted in March 1989 <50> to clarify that, while defining forms
> | normally appear at top level, it is meaningful to place them in 
> | non-top-level contexts; DEFTYPE must define the expander function
> | within the enclosing lexical environment, not within the global
> | environment.
> 
> Does this indicate that I may not use DEFTYPE without enclosing it in a
> PROGN or something?  I would like it to be in effect for at least the
> whole file.  How do I do this? 

You are confused here.  This basically says--well,--just what it says.
DEFTYPE may meaningfully use the lexical environment.  That is, you
can do:
 (let ((iii 3))
   (deftype three () `(integer ,iii ,iii)))
and deftype must process the expansion in the context of the LET
(i.e., in the lexical environment).
Put another way, the expander must be a closure over the lexical environment,
not, for example, a list that is EVAL'd later losing the original 
lexical environment.
From: Christopher R. Barry
Subject: Re: More problems.. DEFTYPE question.
Date: 
Message-ID: <8790aepivk.fsf@2xtreme.net>
·······@pug1.sprocketshop.com (Kenneth P. Turvey) writes:

> When I compile the code below in a file using COMPILE-FILE, I get an
> error indicating that StateType is not defined when the second form is
> compiled.  If I just cut and paste the DEFTYPE form into the lisp reader
> and then run COMPILE-FILE again everything works as one would expect. 
> 
> (deftype StateType () '(integer 0 23)) 
> 
> (defconstant *SolvedState*
>   (make-array 20
>               :element-type 'StateType
>               :initial-element 0 ))

I believe this is a bug in Allegro CL. This works fine in Lispworks
and CMU CL. As a fix for Allegro CL, wrap the DEFTYPE form like

  (eval-when (:compile-toplevel :load-toplevel)
  (deftype StateType () '(integer 0 23)) 
  )

Christopher
From: Kenneth P. Turvey
Subject: Re: More problems.. DEFTYPE question.
Date: 
Message-ID: <slrn7kmkao.4i9.kturvey@pug1.sprocketshop.com>
On Mon, 24 May 1999 15:45:55 GMT, Christopher R. Barry <······@2xtreme.net> wrote:

>I believe this is a bug in Allegro CL. This works fine in Lispworks
>and CMU CL. As a fix for Allegro CL, wrap the DEFTYPE form like
>
>  (eval-when (:compile-toplevel :load-toplevel)
>  (deftype StateType () '(integer 0 23)) 
>  )

That fixed it.  Thanks,

-- 
Kenneth P. Turvey <·······@SprocketShop.com> 
----------------- http://www.tranquility.net/~kturvey

  I could never learn to like her, except on a raft at sea with no other
  provisions in sight. 	
	-- Mark Twain
From: Tim Bradshaw
Subject: Re: More problems.. DEFTYPE question.
Date: 
Message-ID: <ey3n1yudq6p.fsf@lostwithiel.tfeb.org>
* Kenneth P Turvey wrote:
> When I compile the code below in a file using COMPILE-FILE, I get an
> error indicating that StateType is not defined when the second form is
> compiled.  If I just cut and paste the DEFTYPE form into the lisp reader
> and then run COMPILE-FILE again everything works as one would expect. 

> (deftype StateType () '(integer 0 23)) 

> (defconstant *SolvedState*
>   (make-array 20
>               :element-type 'StateType
>               :initial-element 0 ))


> I suspect that this is due to the comment on page 63 of CLTL2, but I
> could use a novice translation.  Any help would be appreciated.

No, I think it's just a bug.  I am *almost* sure (I don't actually
have the hyperspec right here on my screen...) that StateType should
be available at compile time in a case like this.  I'd file a bug
report with your vendor.

--tim
From: Thomas A. Russ
Subject: Re: More problems.. DEFTYPE question.
Date: 
Message-ID: <ymilnebkfzk.fsf@sevak.isi.edu>
I haven't actually used DEFTYPE, but from the nature of the problem you
describe, it looks like you need to use EVAL-WHEN around the DEFTYPE
form.  That way, the type definition can be made available in the
compilation environment:

(eval-when (:compile-toplevel :load-toplevel :execute)
  (deftype StateType () '(integer 0 23))
  )

If the type information is only needed for compilation and not at run
time, then you may be able to get away with just :compile-toplevel, but
my practice is to have everything available at all times.

-- 
Thomas A. Russ,  USC/Information Sciences Institute          ···@isi.edu