methods.texi 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. @node Generic dispatch system
  2. @section Generic dispatch system
  3. @cindex multimethod dispatch
  4. @cindex type dispatch
  5. @cindex generic functions
  6. @cindex generic predicate dispatch
  7. @stindex methods
  8. @stindex meta-methods
  9. Scheme48 supports a CLOS-style generic procedure dispatch system, based
  10. on type predicates. The main interface is exported by @code{methods}.
  11. The internals of the system are exposed by the @code{meta-methods}
  12. structure, but they are not documented here. The generic dispatch
  13. system is used in Scheme48's @embedref{Writer, writer} and numeric
  14. system.
  15. @c @embedref{numeric system, Numeric system}.
  16. @dfn{Types} in Scheme48's generic dispatch system are represented using
  17. type predicates, rather than having every object have a single,
  18. well-defined `class.' The naming convention for simple types is to
  19. prefix the type name with a colon. The types support multiple
  20. inheritance. Method specificity is determined based on descending
  21. order of argument importance. That is, given two methods, @var{M} &
  22. @var{N}, such that they are both applicable to a given sequence of
  23. arguments, and an index @var{i} into that sequence, such that @var{i}
  24. is the first index in @var{M}'s & @var{N}'s lists of argument type
  25. specifiers, from left to right, where the type differs: if the type for
  26. @var{M}'s argument at @var{i} is more specific than the corresponding
  27. type in @var{N}'s specifiers, @var{M} is considered to be more specific
  28. than @var{N}, even if the remaining argument type specifiers in @var{N}
  29. are more specific.
  30. @deffn syntax define-simple-type name (supertype @dots{}) predicate
  31. Defines @var{name} to be a @dfn{simple type} with the given predicate
  32. and the given supertypes.
  33. @end deffn
  34. @deffn procedure singleton value @returns{} simple-type
  35. Creates a @dfn{singleton type} that matches only @var{value}.
  36. @end deffn
  37. @deffn syntax define-generic proc-name method-table-name [prototype]
  38. Defines @var{proc-name} to be a @dfn{generic procedure} that, when
  39. invoked, will dispatch on its arguments via the @dfn{method table} that
  40. @var{method-table-name} is defined to be and apply the most specific
  41. method it can determine defined in the @var{method-table-name} method
  42. table to its arguments. The convention for naming variables that will
  43. be bound to method tables is to add an ampersand to the front of the
  44. name. @var{Prototype} is a suggestion for what method prototypes
  45. should follow the shape of, but it is currently ignored.
  46. @end deffn
  47. @deffn syntax define-method method-table prototype body
  48. Adds a @dfn{method} to @var{method-table}, which is usually one defined
  49. by @code{define-generic}.@footnote{There is an internal interface, a
  50. sort of meta-object protocol, to the method dispatch system, but it is
  51. not yet documented.} @var{Prototype} should be a list whose elements
  52. may be either identifiers, in which case that parameter is not used for
  53. dispatching, or lists of two elements, the @code{car} of which is the
  54. parameter name and the @code{cadr} of which should evaluate to the type
  55. on which to dispatch. As in many generic dispatch systems of similar
  56. designs, methods may invoke the next-most-specific method. By default,
  57. the name @code{next-method} is bound in @var{body} to a nullary
  58. procedure that calls the next-most-specific method. The name of this
  59. procedure may be specified by the user by putting the sequence
  60. @code{"next" @var{next-method-name}} in @var{prototype}, in which case
  61. it will be @var{next-method-name} that is bound to that procedure. For
  62. example:
  63. @lisp
  64. (define-method &frob ((foo :bar) "next" frobozz)
  65. (if (mumble? foo)
  66. (frobozz) ; Invoke the next method.
  67. (yargh blargle foo)))@end lisp
  68. @end deffn
  69. A number of simple types are already defined & exported by the
  70. @code{methods} structure. Entries are listed as @code{@var{type-name}
  71. <- (@var{supertype} @dots{}), @var{predicate}}
  72. @itemize
  73. @item @code{:values <- (), (lambda (x) #t)} --- Abstract supertype of
  74. all run-time values
  75. @item @code{:value <- (:values), (lambda (x) #t)} --- Abstract
  76. supertype of all first-class values
  77. @item @code{:zero <- (:values), (lambda (x) #f)} --- Type that no
  78. objects satisfy
  79. @item @code{:number <- (:value), number?}
  80. @item @code{:complex <- (:number), complex?} --- (This happens to be
  81. equivalent to @code{:number}.)
  82. @item @code{:real <- (:complex), real?}
  83. @item @code{:rational <- (:real), rational?}
  84. @item @code{:integer <- (:rational), integer?}
  85. @item @code{:exact-integer <- (:integer),
  86. (lambda (x) (and (integer? x) (exact? x)))}
  87. @item @code{:boolean <- (:value), boolean?}
  88. @item @code{:symbol <- (:value), symbol?}
  89. @item @code{:char <- (:value), char?}
  90. @item @code{:null <- (:value), null?}
  91. @item @code{:pair <- (:value), pair?}
  92. @item @code{:vector <- (:value), vector?}
  93. @item @code{:string <- (:value), string?}
  94. @item @code{:procedure <- (:value), procedure?}
  95. @item @code{:input-port <- (:value), input-port?}
  96. @item @code{:output-port <- (:value), output-port?}
  97. @item @code{:eof-object <- (:value), eof-object?}
  98. @item @code{:record <- (:value), record?}
  99. @end itemize