features.texi 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  1. @node System features
  2. @section System features
  3. Scheme48 provides a variety of miscellaneous features built-in to the
  4. system.
  5. @menu
  6. * Miscellaneous features::
  7. * Various utilities::
  8. * Filenames::
  9. * Fluid/dynamic bindings::
  10. * ASCII character encoding::
  11. * Integer enumerations::
  12. * Cells::
  13. * Queues::
  14. * Hash tables::
  15. * Weak references::
  16. * Type annotations::
  17. * Explicit renaming macros::
  18. @end menu
  19. @node Miscellaneous features
  20. @subsection Miscellaneous features
  21. @stindex features
  22. The structure @code{features} provides some very miscellaneous features
  23. in Scheme48.
  24. @cindex immutability
  25. @cindex mutability
  26. @deffn procedure immutable? object @returns{} boolean
  27. @deffnx procedure make-immutable! object @returns{} object
  28. All Scheme objects in Scheme48 have a flag determining whether or not
  29. they may be mutated. All immediate Scheme objects (@code{()},
  30. @code{#f}, @etc{}) are immutable; all fixnums (small integers) are
  31. immutable; and all stored objects --- vectors, pairs, @etc{} --- may be
  32. mutable. @code{Immutable?} returns @code{#t} if @var{object} may not
  33. be mutated, and @code{make-immutable!}, a bit ironically, modifies
  34. @var{object} so that it may not be mutated, if it was not already
  35. immutable, and returns it.
  36. @lisp
  37. (immutable? #t) @result{} #t
  38. (define p (cons 1 2))
  39. (immutable? p) @result{} #f
  40. (car p) @result{} 1
  41. (set-car! p 5)
  42. (car p) @result{} 5
  43. (define q (make-immutable! p))
  44. (eq? p q) @result{} #t
  45. (car p) @result{} 5
  46. (immutable? q) @result{} #t
  47. (set-car! p 6) @error{} immutable pair@end lisp
  48. @end deffn
  49. @deffn procedure string-hash string @returns{} integer-hash-code
  50. Computes a basic but fast hash of @var{string}.
  51. @lisp
  52. (string-hash "Hello, world!") @result{} 1161@end lisp
  53. @end deffn
  54. @cindex forcing buffered output
  55. @cindex output port buffer forcing
  56. @cindex buffered output forcing
  57. @cindex flushing output buffers
  58. @deffn procedure force-output port @returns{} unspecified
  59. Forces all buffered output to be sent out of @var{port}.
  60. This is identical to the binding of the same name exported by the
  61. @embedref{Ports, @code{i/o} structure}.
  62. @end deffn
  63. @cindex noise output
  64. @deffn procedure current-noise-port @returns{} output-port
  65. The current noise port is a port for sending noise messages that are
  66. inessential to the operation of a program.
  67. @end deffn
  68. @stindex silly
  69. The @code{silly} structure exports a single procedure, implemented as
  70. a VM primitive for the silly reason of efficiency, hence the name of
  71. the structure.@footnote{The author of this manual is not at fault for
  72. this nomenclature.} It is used in an inner loop of the reader.
  73. @deffn procedure reverse-list->string char-list count @returns{} string
  74. Returns a string of the first @var{count} characters in
  75. @var{char-list}, in reverse. It is a serious error if @var{char-list}
  76. is not a list whose length is at least @var{count}; the error is not
  77. detected by the VM, so bogus pointers may be involved as a result.
  78. Use this routine with care in inner loops.
  79. @end deffn
  80. @stindex debug-messages
  81. The @code{debug-messages} structure exports a procedure for emitting
  82. very basic debugging messages for low-level problems.
  83. @deffn procedure debug-message item @dots{} @returns{} unspecified
  84. Prints @var{item} @dots{} directly to an error port,@footnote{On Unix,
  85. this is @code{stderr}, the standard I/O error output file.}
  86. eliding buffering and thread synchronization on the Scheme side.
  87. Objects are printed as follows:
  88. @itemize
  89. @item Fixnums (small integers) are written in decimal.
  90. @item Characters are written literally with a @code{#\} prefix. No
  91. naming translation is performed, so the space and newline characters
  92. are written literally, not as @code{#\space} or @code{#\newline}.
  93. @item Records are written as #@{@var{type-name}@}, where
  94. @var{type-name} is the name of the record's type.
  95. @item Strings and symbols are written literally.
  96. @item Booleans and the empty list are written normally, @ie{} as
  97. @code{#t}, @code{#f}, or @code{()}.
  98. @item Pairs are written as @code{(...)}.
  99. @item Vectors are written as @code{#(...)}.
  100. @item Objects of certain primitive types are written as
  101. #@{@var{type}@}: procedures, templates, locations, code (byte)
  102. vectors, and continuations.@footnote{Continuations here are in the
  103. sense of VM stack frames, not escape procedures as obtained using
  104. @code{call-with-current-continuation}.}
  105. @item Everything else is printed as #@{???@}.
  106. @end itemize
  107. @end deffn
  108. @stindex code-quote
  109. The @code{code-quote} structure exports a variant of @code{quote} that
  110. is useful in some sophisticated macros.
  111. @deffn {special form} code-quote object @returns{} object
  112. Evaluates to the literal value of @var{object}. This is semantically
  113. identical to @code{quote}, but @var{object} may be anything, and the
  114. compiler will not signal any warnings regarding its value, while such
  115. warnings would be signalled for @code{quote} expressions that do not
  116. wrap readable S-expressions: arbitrary, compound, unreadable data may
  117. be stored in @code{code-quote}. Values computed at compile-time may
  118. thus be transmitted to run-time code. However, care should be taken
  119. in doing this.
  120. @end deffn
  121. @node Various utilities
  122. @subsection Various utilities
  123. @stindex util
  124. The @code{util} structure contains some miscellaneous utility routines
  125. extensively used internally in the run-time system. While they are not
  126. meant to compose a comprehensive library (such as, for example, [SRFI
  127. 1]), they were found useful in building the run-time system without
  128. introducing massive libraries into the core of the system.
  129. @cindex unspecific
  130. @cindex unspecified
  131. @deffn procedure unspecific @returns{} unspecific
  132. Returns Scheme48's @dfn{unspecific} token, which is used wherever R5RS
  133. uses the term `unspecific' or `unspecified.' In this manual, the term
  134. `unspecified' is used to mean that the values returned by a particular
  135. procedure are not specified and may be anything, including a varying
  136. number of values, whereas `unspecific' refers to Scheme48's specific
  137. `unspecific' value that the @code{unspecific} procedure returns.
  138. @end deffn
  139. @deffn procedure reduce kons knil list @returns{} final-knil
  140. Reduces @var{list} by repeatedly applying @var{kons} to elements of
  141. @var{list} and the current @var{knil} value. This is the fundamental
  142. list recursion operator.
  143. @lisp
  144. (reduce @var{kons} @var{knil}
  145. (cons @var{elt@sub{1}}
  146. (cons @var{elt@sub{2}}
  147. (@dots{}(cons @var{elt@sub{N}} '())@dots{}))))
  148. @equiv{}
  149. (@var{kons} @var{elt@sub{1}}
  150. (@var{kons} @var{elt@sub{2}}
  151. (@dots{}(@var{kons} @var{elt@sub{N}} @var{knil})@dots{})))@end lisp
  152. Example:
  153. @lisp
  154. (reduce append '() '((1 2 3) (4 5 6) (7 8 9)))
  155. @result{} (1 2 3 4 5 6 7 8 9)
  156. (append '(1 2 3)
  157. (append '(4 5 6)
  158. (append '(7 8 9) '())))
  159. @result{} (1 2 3 4 5 6 7 8 9)@end lisp
  160. @end deffn
  161. @deffn procedure fold combiner list accumulator @returns{} final-accumulator
  162. Folds @var{list} into an accumulator by repeatedly combining each
  163. element into an accumulator with @var{combiner}. This is the
  164. fundamental list iteration operator.
  165. @lisp
  166. (fold @var{combiner}
  167. (list @var{elt@sub{1}} @var{elt@sub{2}} @dots{} @var{elt@sub{N}})
  168. @var{accumulator})
  169. @equiv{}
  170. (let* ((accum@sub{1} (@var{combiner} @var{elt@sub{1}} @var{accumulator}))
  171. (accum@sub{2} (@var{combiner} @var{elt@sub{2}} accum@sub{1}))
  172. @dots{}
  173. (accum@sub{N} (@var{combiner} @var{elt@sub{N}} accum@sub{N-1})))
  174. accum@sub{N})@end lisp
  175. Example:
  176. @lisp
  177. (fold cons '() '(a b c d))
  178. @result{} (d c b a)
  179. (cons 'd (cons 'c (cons 'b (cons 'a '()))))
  180. @result{} (d c b a)@end lisp
  181. @end deffn
  182. @deffn procedure fold->2 combiner list accumulator@sub{1} accumulator@sub{2} @returns{} [final-accumulator@sub{1} final-accumulator@sub{2}]
  183. @deffnx procedure fold->3 combiner list accumulator@sub{1} accumulator@sub{2} accumulator@sub{3} @returns{} [final-accumulator@sub{1} final-accumulator@sub{2} final-accumulator@sub{3}]
  184. Variants of @code{fold} for two and three accumulators, respectively.
  185. @lisp
  186. ;;; Partition @var{list} by elements that satisfy @var{pred?} and those
  187. ;;; that do not.
  188. (fold->2 (lambda (elt satisfied unsatisfied)
  189. (if (@var{pred?} elt)
  190. (values (cons elt satisfied) unsatisfied)
  191. (values satisfied (cons elt unsatisfied))))
  192. @var{list}
  193. '() '())@end lisp
  194. @end deffn
  195. @deffn procedure filter predicate list @returns{} filtered-list
  196. Returns a list of all elements in @var{list} that satisfy
  197. @var{predicate}.
  198. @lisp
  199. (filter odd? '(3 1 4 1 5 9 2 6 5 3 5))
  200. @result{} (3 1 1 5 9 5 3 5)@end lisp
  201. @end deffn
  202. @deffn procedure posq object list @returns{} integer or @code{#f}
  203. @deffnx procedure posv object list @returns{} integer or @code{#f}
  204. @deffnx procedure position object list @returns{} integer or @code{#f}
  205. These find the position of the first element equal to @var{object} in
  206. @var{list}. @code{Posq} compares elements by @code{eq?}; @code{posv}
  207. compares by @code{eqv?}; @code{position} compares by @code{equal?}.
  208. @lisp
  209. (posq 'c '(a b c d e f))
  210. @result{} 2
  211. (posv 1/2 '(1 1/2 2 3/2))
  212. @result{} 1
  213. (position '(d . e) '((a . b) (b . c) (c . d) (d . e) (e . f)))
  214. @result{} 3@end lisp
  215. @end deffn
  216. @deffn procedure any predicate list @returns{} value or @code{#f}
  217. @deffnx procedure every predicate list @returns{} boolean
  218. @code{Any} returns the value that @var{predicate} returns for the first
  219. element in @var{list} for which @var{predicate} returns a true value;
  220. if no element of @var{list} satisfied @var{predicate}, @code{any}
  221. returns @code{#f}. @code{Every} returns @code{#t} if every element of
  222. @var{list} satisfies @var{predicate}, or @code{#f} if there exist any
  223. that do not.
  224. @lisp
  225. (any (lambda (x) (and (even? x) (sqrt x)))
  226. '(0 1 4 9 16))
  227. @result{} 2
  228. (every odd? '(1 3 5 7 9))
  229. @result{} #t@end lisp
  230. @end deffn
  231. @deffn procedure sublist list start end @returns{} list
  232. Returns a list of the elements in @var{list} including & after that at
  233. the index @var{start} and before the index @var{end}.
  234. @lisp
  235. (sublist '(a b c d e f g h i) 3 6) @result{} (d e f)@end lisp
  236. @end deffn
  237. @deffn procedure last list @returns{} value
  238. Returns the last element in @var{list}. @code{Last}'s effect is
  239. undefined if @var{list} is empty.
  240. @lisp
  241. (last '(a b c)) @result{} c@end lisp
  242. @end deffn
  243. @deffn procedure insert object list elt< @returns{} list
  244. Inserts @var{object} into the sorted list @var{list}, comparing the
  245. order of @var{object} and each element by @var{elt<}.
  246. @lisp
  247. (insert 3 '(0 1 2 4 5) <) @result{} (0 1 2 3 4 5)@end lisp
  248. @end deffn
  249. @node Filenames
  250. @subsection Filenames
  251. @stindex filenames
  252. There are some basic filename manipulation facilities exported by the
  253. @code{filenames} structure.@footnote{The facilities Scheme48 provides
  254. are very rudimentary, and they are not intended to act as a coherent
  255. and comprehensive pathname or logical name facility such as that of
  256. Common Lisp. However, they served the basic needs of Scheme48's build
  257. process when they were originally created.}
  258. @defvr constant *scheme-file-type* @returns{} symbol
  259. @defvrx constant *load-file-type* @returns{} symbol
  260. @code{*Scheme-file-type*} is a symbol denoting the file extension that
  261. Scheme48 assumes for Scheme source files; any other extension, for
  262. instance in the filename list of a structure definition, must be
  263. written explicitly. @code{*Load-file-type*} is a symbol denoting the
  264. preferable file extension to load files from. (@code{*Load-file-type*}
  265. was used mostly in bootstrapping Scheme48 from Pseudoscheme or T long
  266. ago and is no longer very useful.)
  267. @end defvr
  268. @deffn procedure file-name-directory filename @returns{} string
  269. @deffnx procedure file-name-nondirectory filename @returns{} string
  270. @code{File-name-directory} returns the directory component of the
  271. filename denoted by the string @var{filename}, including a trailing
  272. separator (on Unix, @code{/}). @code{File-name-nondirectory} returns
  273. everything but the directory component of the filename denoted by the
  274. string @var{filename}, including the extension.
  275. @lisp
  276. (file-name-directory "/usr/local/lib/scheme48/scheme48.image")
  277. @result{} "/usr/local/lib/scheme48/"
  278. (file-name-nondirectory "/usr/local/lib/scheme48/scheme48.image")
  279. @result{} "scheme48.image"
  280. (file-name-directory "scheme48.image")
  281. @result{} ""
  282. (file-name-nondirectory "scheme48.image")
  283. @result{} "scheme48.image"@end lisp
  284. @end deffn
  285. @cindex namelists
  286. @dfn{Namelists} are platform-independent means by which to name files.
  287. They are represented as readable S-expressions of any of the following
  288. forms:
  289. @table @code
  290. @item @var{basename}
  291. represents a filename with only a basename and no directory or file
  292. type/extension;
  293. @item (@var{directory} @var{basename} [@var{type}])
  294. represents a filename with a single preceding directory component and
  295. an optional file type/extension; and
  296. @item ((@var{directory} @dots{}) @var{basename} [@var{type}])
  297. represents a filename with a sequence of directory components, a
  298. basename, and an optional file type/extension.
  299. @end table
  300. Each atomic component --- that is, the basename, the type/extension,
  301. and each individual directory component --- may be either a string or
  302. a symbol. Symbols are converted to the canonical case of the host
  303. operating system by @code{namestring} (on Unix, lowercase); the case of
  304. string components is not touched.
  305. @deffn procedure namestring namelist directory default-type @returns{} string
  306. Converts @var{namelist} to a string in the format required by the host
  307. operating system.@footnote{However, the current standard distribution
  308. of Scheme48 is specific to Unix: the current code implements only Unix
  309. filename facilities.} If @var{namelist} did not have a directory
  310. component, @var{directory}, a string in the underlying operating
  311. system's format for directory prefixes, is added to the resulting
  312. namestring; and, if @var{namelist} did not have a type/extension,
  313. @var{default-type}, which may be a string or a symbol and which should
  314. @emph{not} already contain the host operating system's delimiter
  315. (usually a dot), is appended to the resulting namestring.
  316. @var{Directory} or @var{default-type} may be @code{#f}, in which case
  317. they are not prefixed or appended to the resulting filename.
  318. @lisp
  319. (namestring 'foo #f #f) @result{} "foo"
  320. (namestring 'foo "bar" 'baz) @result{} "bar/foo.baz"
  321. (namestring '(rts defenum) "scheme" 'scm)
  322. @result{} "scheme/rts/defenum.scm"
  323. (namestring '((foo bar) baz quux) "zot" #f)
  324. @result{} "zot/foo/bar/baz.quux"
  325. (namestring "zot/foo/bar/baz.quux" #f "mumble")
  326. @result{} "zot/foo/bar/baz.quux.mumble"@end lisp
  327. @end deffn
  328. @subsubsection Filename translations
  329. @cindex filename translations
  330. Scheme48 keeps a registry of @dfn{filename translations}, translations
  331. from filename prefixes to the real prefixes. This allows abstraction
  332. of actual directory prefixes without necessitating running Scheme code
  333. to construct directory pathnames (for example, in configuration files).
  334. Interactively, in the usual command processor, users can set filename
  335. translations with the @command{,translate}; @pxref{Basic commands}.
  336. @deffn procedure translations @returns{} string/string-alist
  337. Returns the alist of filename translations.
  338. @end deffn
  339. @deffn procedure set-translation! from to @returns{} unspecified
  340. Adds a filename prefix translation, overwriting an existing one if one
  341. already existed.
  342. @end deffn
  343. @deffn procedure translate filename @returns{} translated-filename
  344. Translates the first prefix of @var{filename} found in the registry of
  345. translations and returns the translated filename.
  346. @end deffn
  347. @lisp
  348. (set-translation! "s48" "/home/me/scheme/scheme48/scheme")
  349. (translate (namestring '(bcomp frame) "s48" 'scm))
  350. @result{} "/home/me/scheme/scheme48/scheme/bcomp/frame.scm"
  351. (translate (namestring "comp-packages" "s48" 'scm))
  352. @result{} "/home/me/scheme/scheme48/scheme/comp-packages.scm"
  353. (translate "s48/frobozz")
  354. @result{} "/home/me/scheme/scheme48/scheme/frobozz"
  355. (set-translation! "scheme48" "s48")
  356. (translate (namestring '((scheme48 big) filename) #f 'scm))
  357. @result{} scheme48/big/filename.scm
  358. (translate (translate (namestring '((scheme48 big) filename) #f 'scm)))
  359. @result{} "/home/me/scheme/scheme48/scheme/big/filename.scm"@end lisp
  360. @cindex @code{=scheme48/}
  361. One filename translation is built-in, mapping @code{=scheme48/} to the
  362. directory of system files in a Scheme48 installation, which on Unix is
  363. typically a directory in @code{/usr/local/lib}.
  364. @lisp
  365. (translate "=scheme48/scheme48.image")
  366. @result{} /usr/local/scheme48/scheme48.image@end lisp
  367. @node Fluid/dynamic bindings
  368. @subsection Fluid/dynamic bindings
  369. @cindex fluid bindings
  370. @cindex dynamic bindings
  371. @stindex fluids
  372. The @code{fluids} structure provides a facility for dynamically bound
  373. resources, like special variables in Common Lisp, but with first-class,
  374. unforgeable objects.
  375. Every @embedref{Multithreading,thread} in Scheme48 maintains a
  376. @dfn{fluid or dynamic environment}. It maps @dfn{fluid descriptors} to
  377. their values, much like a lexical environment maps names to their
  378. values. The dynamic environment is implemented by deep binding and
  379. dynamically scoped. Fluid variables are represented as first-class
  380. objects for which there is a top-level value and possibly a binding in
  381. the current dynamic environment. Escape procedures, as created with
  382. Scheme's @code{call-with-current-continuation}, also store & preserve
  383. the dynamic environment at the time of their continuation's capture and
  384. restore it when invoked.
  385. The convention for naming variables that are bound to fluid objects
  386. is to add a prefix of @code{$} (dollar sign); @eg{}, @code{$foo}.
  387. @deffn procedure make-fluid top-level-value @returns{} fluid
  388. Fluid constructor.
  389. @end deffn
  390. @deffn procedure fluid fl @returns{} value
  391. @deffnx procedure set-fluid! fl value @returns{} unspecified
  392. @deffnx procedure fluid-cell-ref fluid-cell @returns{} value
  393. @deffnx procedure fluid-cell-set! fluid-cell value @returns{} unspecified
  394. @code{Fluid} returns the value that the current dynamic environment
  395. associates with @var{fl}, if it has an association; if not, it returns
  396. @var{fl}'s top-level value, as passed to @code{make-fluid} to create
  397. @var{fl}. @code{Set-fluid!} assigns the value of the association in
  398. the current dynamic environment for @var{fl} to @var{value}, or, if
  399. there is no such association, it assigns the top-level value of
  400. @var{fl} to @var{value}. Direct assignment of fluids is deprecated,
  401. however, and may be removed in a later release; instead, programmers
  402. should use fluids that are bound to @embedref{Cells, mutable cells}.
  403. @code{Fluid-cell-ref} and @code{fluid-cell-set!} are conveniences for
  404. this; they simply call the corresponding cell operations after
  405. fetching the cell that the fluid refers to by using @code{fluid}.
  406. @end deffn
  407. @deffn procedure let-fluid fluid value thunk @returns{} values
  408. @deffnx procedure let-fluids fluid@sub{0} value@sub{0} fluid@sub{1} value@sub{1} @dots{} thunk @returns{} values
  409. These dynamically bind their fluid arguments to the corresponding value
  410. arguments and apply @var{thunk} with the new dynamic environment,
  411. restoring the old one after @var{thunk} returns and returning the value
  412. it returns.
  413. @end deffn
  414. @lisp
  415. (define $mumble (make-fluid 0))
  416. (let ((a (fluid $mumble))
  417. (b (let-fluid $mumble 1
  418. (lambda () (fluid $mumble))))
  419. (c (fluid $mumble))
  420. (d (let-fluid $mumble 2
  421. (lambda ()
  422. (let-fluid $mumble 3
  423. (lambda () (fluid $mumble)))))))
  424. (list a b c d))
  425. @result{} (0 1 0 3)
  426. (let ((note (lambda (when)
  427. (display when)
  428. (display ": ")
  429. (write (fluid $mumble))
  430. (newline))))
  431. (note 'initial)
  432. (let-fluid $mumble 1 (lambda () (note 'let-fluid)))
  433. (note 'after-let-fluid)
  434. (let-fluid $mumble 1
  435. (lambda ()
  436. (note 'outer-let-fluid)
  437. (let-fluid $mumble 2 (lambda () (note 'inner-let-fluid)))))
  438. (note 'after-inner-let-fluid)
  439. ((call-with-current-continuation
  440. (lambda (k)
  441. (lambda ()
  442. (let-fluid $mumble 1
  443. (lambda ()
  444. (note 'let-fluid-within-cont)
  445. (let-fluid $mumble 2
  446. (lambda () (note 'inner-let-fluid-within-cont)))
  447. (k (lambda () (note 'let-fluid-thrown)))))))))
  448. (note 'after-throw))
  449. @print{} initial: 0
  450. @print{} let-fluid: 1
  451. @print{} after-let-fluid: 0
  452. @print{} outer-let-fluid: 1
  453. @print{} inner-let-fluid: 2
  454. @print{} let-fluid-within-cont: 1
  455. @print{} inner-let-fluid-within-cont: 2
  456. @print{} let-fluid-thrown: 0
  457. @print{} after-throw: 0@end lisp
  458. @node ASCII character encoding
  459. @subsection ASCII character encoding
  460. @stindex ascii
  461. These names are exported by the @code{ascii} structure.
  462. @deffn procedure char->ascii char @returns{} ascii-integer
  463. @deffnx procedure ascii->char ascii-integer @returns{} character
  464. These convert characters to and from their integer ASCII encodings.
  465. @code{Char->ascii} and @code{ascii->char} are similar to R5RS's
  466. @code{char->integer} and @code{integer->char}, but they are guaranteed
  467. to use the ASCII encoding. Scheme48's @code{integer->char} and
  468. @code{char->integer} deliberately do not use the ASCII encoding to
  469. encourage programmers to make use of only what R5RS guarantees.
  470. @lisp
  471. (char->ascii #\a) @result{} 97
  472. (ascii->char 97) @result{} #\a@end lisp
  473. @end deffn
  474. @defvr constant ascii-limit @returns{} integer
  475. @defvrx constant ascii-whitespaces @returns{} ascii-integer-list
  476. @code{Ascii-limit} is an integer that is one greater than the highest
  477. number that @code{char->ascii} may return or @code{ascii->char} will
  478. accept. @code{Ascii-whitespaces} is a list of the integer encodings of
  479. all characters that are considered whitespace: space (32), horizontal
  480. tab (9), line-feed/newline (10), vertical tab (11), form-feed/page (12),
  481. and carriage return (13).
  482. @end defvr
  483. @node Integer enumerations
  484. @subsection Integer enumerations
  485. @stindex enumerated
  486. Scheme48 provides a facility for @dfn{integer enumerations}, somewhat
  487. akin to C enums. The names described in this section are exported by
  488. the @code{enumerated} structure.
  489. @strong{Note:} These enumerations are @emph{not} compatible with the
  490. @embedref{Enumerated/finite types and sets, enumerated/finite type
  491. facility}.
  492. @deffn syntax define-enumeration enumeration-name (enumerand-name @dots{})
  493. Defines @var{enumeration-name} to be a static enumeration. (Note that
  494. it is @emph{not} a regular variable. It is actually a macro, though its
  495. exact syntax is not exposed; it must be exported with the
  496. @embedref{Static type system, @code{:syntax} type}.)
  497. @var{Enumeration-name} thereafter may be used with the enumeration
  498. operators described below.
  499. @end deffn
  500. @deffn syntax enum enumeration-name enumerand-name @returns{} enumerand-integer
  501. @deffnx syntax components enumeration-name @returns{} component-vector
  502. @code{Enum} expands to the integer value represented symbolically by
  503. @var{enumerand-name} in the enumeration @var{enumeration-name} as
  504. defined by @code{define-enumeration}. @code{Components} expands to a
  505. literal vector of the components in @var{enumeration-name} as defined
  506. by @code{define-enumeration}. In both cases, @var{enumerand-name} must
  507. be written literally as the name of the enumerand; see
  508. @code{name->enumerand} for extracting an enumerand's integer given a
  509. run-time symbol naming an enumerand.
  510. @end deffn
  511. @deffn syntax enumerand->name enumerand-integer enumeration-name @returns{} symbol
  512. @deffnx syntax name->enumerand enumerand-name enumeration-name @returns{} integer-enumerand
  513. @code{Enumerand->name} expands to a form that evaluates to the symbolic
  514. name that the integer value of the expression @var{enumerand-integer}
  515. is mapped to by @var{enumeration-name} as defined by
  516. @code{define-enumeration}. @code{Name->enumerand} expands to a form
  517. that evaluates to the integer value of the enumerand in
  518. @var{enumeration-name} that is represented symbolically by the value of
  519. the expression @var{enumerand-name}.
  520. @end deffn
  521. @stindex enum-case
  522. The @code{enum-case} structure provides a handy utility of the same
  523. name for dispatching on enumerands.
  524. @deffn syntax enum-case
  525. @lisp
  526. (enum-case @var{enumeration-name} @var{key}
  527. ((@var{enumerand-name} @dots{}) @var{body})
  528. @dots{}
  529. [(else @var{else-body})])@end lisp
  530. Matches @var{key} with the clause one of whose names maps in
  531. @var{enumeration-name} to the integer value of @var{key}. @var{Key}
  532. must be an exact, non-negative integer. If no matching clause is
  533. found, and @var{else-body} is present, @code{enum-case} will evaluate
  534. @var{else-body}; if @var{else-body} is not present, @code{enum-case}
  535. will return an unspecific value.
  536. @end deffn
  537. Examples:
  538. @lisp
  539. (define-enumeration foo
  540. (bar
  541. baz))
  542. (enum foo bar) @result{} 0
  543. (enum foo baz) @result{} 1
  544. (enum-case foo (enum foo bar)
  545. ((baz) 'x)
  546. (else 'y))
  547. @result{} y
  548. (enum-case foo (enum foo baz)
  549. ((bar) 'a)
  550. ((baz) 'b))
  551. @result{} b
  552. (enumerand->name 1 foo) @result{} baz
  553. (name->enumerand 'bar foo) @result{} 0
  554. (components foo) @result{} #(bar baz)@end lisp
  555. @c Include the sections on the various kinds of data structures.
  556. @include system/data.texi
  557. @node Type annotations
  558. @subsection Type annotations
  559. @cindex type system loopholes
  560. @cindex loopholes in the type system
  561. @stindex loopholes
  562. Scheme48 allows optional type annotations with the @code{loophole}
  563. special form from the @code{loopholes} structure.
  564. @deffn syntax loophole type expression @returns{} values
  565. This is exactly equivalent in semantics to @var{expression}, except the
  566. static type analyzer is informed that the whole expression has the type
  567. @var{type}. For details on the form of @var{type}, @pxref{Static type
  568. system}.
  569. @end deffn
  570. Type annotations can be used for several different purposes:
  571. @itemize @bullet
  572. @item
  573. simply to give more information to the static type analyzer;
  574. @item
  575. to work as a simple abstract data type facility: passing a type name
  576. that does not already exist creates a new disjoint value type; and
  577. @item
  578. to prevent the type system from generating warnings in the rare cases
  579. where it would do so incorrectly, such as in the @code{primitive-cwcc},
  580. @code{primitive-catch}, and @code{with-continuation} devices (to be
  581. documented in a later edition of this manual).
  582. @end itemize
  583. To see an example of the second use, see @file{rts/jar-defrecord.scm}
  584. in Scheme48's source tree.
  585. @strong{Note:} Type annotations do @emph{not} damage the safety of
  586. Scheme's type system. They affect only the static type analyzer, which
  587. does not change run-time object representations; it only checks type
  588. soundness of code and generates warnings for programs that would cause
  589. run-time type errors.
  590. @node Explicit renaming macros
  591. @subsection Explicit renaming macros
  592. @cindex low-level macros
  593. @cindex macros, low-level
  594. @cindex unhygienic macros
  595. @cindex macros, unhygienic
  596. Scheme48 supports a simple low-level macro system based on explicitly
  597. renaming identifiers to preserve hygiene. The macro system is
  598. well-integrated with the module system; @pxref{Macros in concert with
  599. modules}.
  600. @dfn{Explicit renaming} macro transformers operate on simple
  601. S-expressions extended with @dfn{identifiers}, which are like symbols
  602. but contain more information about lexical context. In order to
  603. preserve that lexical context, transformers must explicitly call a
  604. @dfn{renamer} procedure to produce an identifier with the proper scope.
  605. To test whether identifiers have the same denotation, transformers are
  606. also given an identifier comparator.
  607. The facility provided by Scheme48 is almost identical to the explicit
  608. renaming macro facility described in [Clinger 91].@footnote{For the
  609. sake of avoiding any potential copyright issues, the paper is not
  610. duplicated here, and instead the author of this manual has written the
  611. entirety of this section.} It differs only by the @code{transformer}
  612. keyword, which is described in the paper but not used by Scheme48, and
  613. in the annotation of auxiliary names.
  614. @deffn syntax define-syntax name transformer [aux-names]
  615. Introduces a derived syntax @var{name} with the given transformer,
  616. which may be an explicit renaming transformer procedure, a pair whose
  617. car is such a procedure and whose cdr is a list of auxiliary
  618. identifiers, or the value of a @code{syntax-rules} expression. In the
  619. first case, the added operand @var{aux-names} may, and usually should
  620. except in the case of local (non-exported) syntactic bindings, be a
  621. list of all of the auxiliary top-level identifiers used by the macro.
  622. @end deffn
  623. Explicit renaming transformer procedures are procedures of three
  624. arguments: an input form, an identifier renamer procedure, and an
  625. identifier comparator procedure. The input form is the whole form of
  626. the macro's invocation (including, at the car, the identifier whose
  627. denotation was the syntactic binding). The identifier renamer accepts
  628. an identifier as an argument and returns an identifier that is
  629. hygienically renamed to refer absolutely to the identifier's denotation
  630. in the environment of the macro's definition, not in the environment of
  631. the macro's usage. In order to preserve hygiene of syntactic
  632. transformations, macro transformers must call this renamer procedure
  633. for any literal identifiers in the output. The renamer procedure is
  634. referentially transparent; that is, two invocations of it with the same
  635. arguments in terms of @code{eq?} will produce the same results in the
  636. sense of @code{eq?}.
  637. For example, this simple transformer for a @code{swap!} macro is
  638. incorrect:
  639. @lisp
  640. (define-syntax swap!
  641. (lambda (form rename compare)
  642. (let ((a (cadr form))
  643. (b (caddr form)))
  644. `(LET ((TEMP ,a))
  645. (SET! ,a ,b)
  646. (SET! ,b TEMP)))))@end lisp
  647. @noindent
  648. The introduction of the literal identifier @code{temp} into the output
  649. may conflict with one of the input variables if it were to also be
  650. named @code{temp}: @code{(swap! temp foo)} or @code{(swap! bar temp)}
  651. would produce the wrong result. Also, the macro would fail in another
  652. very strange way if the user were to have a local variable named
  653. @code{let} or @code{set!}, or it would simply produce invalid output if
  654. there were no binding of @code{let} or @code{set!} in the environment
  655. in which the macro was used. These are basic problems of abstraction:
  656. the user of the macro should not need to know how the macro is
  657. internally implemented, notably with a @code{temp} variable and using
  658. the @code{let} and @code{set!} special forms.
  659. Instead, the macro must hygienically rename these identifiers using
  660. the renamer procedure it is given, and it should list the top-level
  661. identifiers it renames (which cannot otherwise be extracted
  662. automatically from the macro's definition):
  663. @lisp
  664. (define-syntax swap!
  665. (lambda (form rename compare)
  666. (let ((a (cadr form))
  667. (b (caddr form)))
  668. `(,(rename 'LET) ((,(rename 'TEMP) ,a))
  669. (,(rename 'SET!) ,a ,b)
  670. (,(rename 'SET!) ,b ,(rename 'TEMP)))))
  671. (LET SET!))@end lisp
  672. However, some macros are unhygienic by design, @ie{} they insert
  673. identifiers into the output intended to be used in the environment of
  674. the macro's usage. For example, consider a @code{loop} macro that
  675. loops endlessly, but binds a variable named @code{exit} to an escape
  676. procedure to the continuation of the @code{loop} expression, with
  677. which the user of the macro can escape the loop:
  678. @lisp
  679. (define-syntax loop
  680. (lambda (form rename compare)
  681. (let ((body (cdr form)))
  682. `(,(rename 'CALL-WITH-CURRENT-CONTINUATION)
  683. (,(rename 'LAMBDA) (EXIT) ; Literal, unrenamed EXIT.
  684. (,(rename 'LET) ,(rename 'LOOP) ()
  685. ,@@body
  686. (,(rename 'LOOP)))))))
  687. (CALL-WITH-CURRENT-CONTINUATION LAMBDA LET))@end lisp
  688. Note that macros that expand to @code{loop} must also be unhygienic;
  689. for instance, this na@"{@dotless{i}}ve definition of a
  690. @code{loop-while} macro is incorrect, because it hygienically renames
  691. @code{exit} automatically by of the definition of @code{syntax-rules},
  692. so the identifier it refers to is not the one introduced
  693. unhygienically by @code{loop}:
  694. @lisp
  695. (define-syntax loop-while
  696. (syntax-rules ()
  697. ((LOOP-WHILE test body ...)
  698. (LOOP (IF (NOT test)
  699. (EXIT)) ; Hygienically renamed.
  700. body ...))))@end lisp
  701. @noindent
  702. Instead, a transformer must be written to not hygienically rename
  703. @code{exit} in the output:
  704. @lisp
  705. (define-syntax loop-while
  706. (lambda (form rename compare)
  707. (let ((test (cadr form))
  708. (body (cddr form)))
  709. `(,(rename 'LOOP)
  710. (,(rename 'IF) (,(rename 'NOT) ,test)
  711. (EXIT)) ; Not hygienically renamed.
  712. ,@@body)))
  713. (LOOP IF NOT))@end lisp
  714. To understand the necessity of annotating macros with the list of
  715. auxiliary names they use, consider the following definition of the
  716. @code{delay} form, which transforms @code{(delay @var{exp})} into
  717. @code{(make-promise (lambda () @var{exp}))}, where @code{make-promise}
  718. is some non-exported procedure defined in the same module as the
  719. @code{delay} macro:
  720. @lisp
  721. (define-syntax delay
  722. (lambda (form rename compare)
  723. (let ((exp (cadr form)))
  724. `(,(rename 'MAKE-PROMISE) (,(rename 'LAMBDA) () ,exp)))))@end lisp
  725. @noindent
  726. This preserves hygiene as necessary, but, while the compiler can know
  727. whether @code{make-promise} is @emph{exported} or not, it cannot in
  728. general determine whether @code{make-promise} is @emph{local}, @ie{}
  729. not accessible in any way whatsoever, even in macro output, from any
  730. other modules. In this case, @code{make-promise} is @emph{not} local,
  731. but the compiler cannot in general know this, and it would be an
  732. unnecessarily heavy burden on the compiler, the linker, and related
  733. code-processing systems to assume that all bindings are not local. It
  734. is therefore better@footnote{However, the current compiler in Scheme48
  735. does not require this, though the static linker does.} to annotate such
  736. definitions with the list of auxiliary names used by the transformer:
  737. @lisp
  738. (define-syntax delay
  739. (lambda (form rename compare)
  740. (let ((exp (cadr form)))
  741. `(,(rename 'MAKE-PROMISE) (,(rename 'LAMBDA) () ,exp))))
  742. (MAKE-PROMISE LAMBDA))@end lisp