home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / lisp / 2243 < prev    next >
Encoding:
Text File  |  1992-08-18  |  3.4 KB  |  96 lines

  1. Newsgroups: comp.lang.lisp
  2. Path: sparky!uunet!cs.utexas.edu!usc!zaphod.mps.ohio-state.edu!rpi!batcomputer!cornell!rochester!cantaloupe.srv.cs.cmu.edu!crabapple.srv.cs.cmu.edu!ram
  3. From: ram+@cs.cmu.edu (Rob MacLachlan)
  4. Subject: Re: Mutually Referential DEFSTRUCT Slot Types: How?
  5. Message-ID: <Bt6yHy.7ru.1@cs.cmu.edu>
  6. Sender: news@cs.cmu.edu (Usenet News System)
  7. Nntp-Posting-Host: lisp-pmax1.slisp.cs.cmu.edu
  8. Organization: School of Computer Science, Carnegie Mellon
  9. References: <EEIDE.92Aug12120727@asylum.cs.utah.edu> <16d8rrINNm7q@early-bird.think.com>
  10. Distribution: comp
  11. Date: Tue, 18 Aug 1992 18:10:35 GMT
  12. Lines: 82
  13.  
  14. In article <16d8rrINNm7q@early-bird.think.com> barmar@think.com (Barry Margolin) writes:
  15. >In article <EEIDE.92Aug12120727@asylum.cs.utah.edu> eeide%asylum.cs.utah.edu@cs.utah.edu (Eric Eide) writes:
  16. >>I would like to write something like the following in Common Lisp:
  17. >>
  18. >>  (defstruct a
  19. >>    (slot nil :type b))
  20. >>
  21. >>  (defstruct b
  22. >>    (slot nil :type a))
  23. >
  24.  
  25. The ANSI draft says that:
  26.     "it is permissable for an unknown type to appear in a declaration
  27.     at compile time, though a warning might be signalled in this
  28.     case."
  29.  
  30. The rationale (which didn't make it into the standard) for this
  31. decision was to support constructs such as the above.
  32.  
  33. Barmar suggests:
  34. >(defstruct a
  35. >  slot)
  36. >
  37. [...]
  38. >
  39. >(declaim (ftype a-slot (a) b))
  40. >
  41.  
  42. Nit: the syntax for the FTYPE declaration should be:
  43.     (declaim (ftype (function (a) b) a-slot))
  44.  
  45. >Except that this doesn't permit you to declare that the type of the new
  46. >value when SETFing A-SLOT must be B.
  47.  
  48. You could:
  49.     (declaim (ftype (function (b a) b) (setf a-slot)))
  50.  
  51. but the standard doesn't seem to require structure slot setters to be
  52. SETF functions (though the integration of structures and CLOS would
  53. suggest this approach.)
  54.  
  55. >>but I think that this type specifier would not give my compiler
  56. >>enough information to allow it to opencode my structure accessors.
  57. >>Speed is important in my application, but probably not critically
  58. >>so.
  59. >
  60. >I'm not sure that this particular set of declarations is likely to be used
  61. >by most implementations.  What kind of open coding do you expect it to
  62. >enable?
  63.  
  64. Any Common Lisp implementation should open-code uses of slot
  65. accessors as long as the structure definition has been seen by the
  66. compiler.  It won't matter what the slot types are or what is known
  67. about the types of the actual operands to the accessors (which are
  68. required to be of the correct type.)
  69.  
  70. CMU Common Lisp does respect to these declarations, but the main
  71. advantage of specifying the types of structure-valued slots is better
  72. run-time error detection and reduced cost for run-time type checking.
  73. In other words, it is more of a robustness advantage than an
  74. efficency advantage.  
  75.  
  76. There can be a significant efficency advantage for slot type
  77. declarations such as FIXNUM, SIMPLE-BASE-STRING, SIMPLE-VECTOR, etc.,
  78. since a good compiler will be able to automatically use the appropriate
  79. type-specific operations on values of those slots.
  80.  
  81. >>Second, how can I create an instance of either of these objects,
  82. >>while keeping the declared types accurate?
  83.  
  84. You can't.  Our coding practice for CMU Common Lisp is to
  85. explicitly specify how we are going to "ground out" the recursion:
  86.   (defstruct a
  87.     (slot nil :type (or b null)))
  88.  
  89.   (defstruct b
  90.     (slot (error "Slot must be supplied.") :type a))
  91.  
  92. You make the A first with a null SLOT, then make the B, then
  93. back-patch the A.
  94.  
  95.   Robert A. MacLachlan
  96.