api-procedures.texi 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  1. @c -*-texinfo-*-
  2. @c This is part of the GNU Guile Reference Manual.
  3. @c Copyright (C) 1996, 1997, 2000, 2001, 2002, 2003, 2004
  4. @c Free Software Foundation, Inc.
  5. @c See the file guile.texi for copying conditions.
  6. @page
  7. @node Procedures and Macros
  8. @section Procedures and Macros
  9. @menu
  10. * Lambda:: Basic procedure creation using lambda.
  11. * Primitive Procedures:: Procedures defined in C.
  12. * Optional Arguments:: Handling keyword, optional and rest arguments.
  13. * Procedure Properties:: Procedure properties and meta-information.
  14. * Procedures with Setters:: Procedures with setters.
  15. * Macros:: Lisp style macro definitions.
  16. * Syntax Rules:: Support for R5RS @code{syntax-rules}.
  17. * Syntax Case:: Support for the @code{syntax-case} system.
  18. * Internal Macros:: Guile's internal representation.
  19. @end menu
  20. @node Lambda
  21. @subsection Lambda: Basic Procedure Creation
  22. @cindex lambda
  23. @c FIXME::martin: Review me!
  24. A @code{lambda} expression evaluates to a procedure. The environment
  25. which is in effect when a @code{lambda} expression is evaluated is
  26. enclosed in the newly created procedure, this is referred to as a
  27. @dfn{closure} (@pxref{About Closure}).
  28. When a procedure created by @code{lambda} is called with some actual
  29. arguments, the environment enclosed in the procedure is extended by
  30. binding the variables named in the formal argument list to new locations
  31. and storing the actual arguments into these locations. Then the body of
  32. the @code{lambda} expression is evaluation sequentially. The result of
  33. the last expression in the procedure body is then the result of the
  34. procedure invocation.
  35. The following examples will show how procedures can be created using
  36. @code{lambda}, and what you can do with these procedures.
  37. @lisp
  38. (lambda (x) (+ x x)) @result{} @r{a procedure}
  39. ((lambda (x) (+ x x)) 4) @result{} 8
  40. @end lisp
  41. The fact that the environment in effect when creating a procedure is
  42. enclosed in the procedure is shown with this example:
  43. @lisp
  44. (define add4
  45. (let ((x 4))
  46. (lambda (y) (+ x y))))
  47. (add4 6) @result{} 10
  48. @end lisp
  49. @deffn syntax lambda formals body
  50. @var{formals} should be a formal argument list as described in the
  51. following table.
  52. @table @code
  53. @item (@var{variable1} @dots{})
  54. The procedure takes a fixed number of arguments; when the procedure is
  55. called, the arguments will be stored into the newly created location for
  56. the formal variables.
  57. @item @var{variable}
  58. The procedure takes any number of arguments; when the procedure is
  59. called, the sequence of actual arguments will converted into a list and
  60. stored into the newly created location for the formal variable.
  61. @item (@var{variable1} @dots{} @var{variablen} . @var{variablen+1})
  62. If a space-delimited period precedes the last variable, then the
  63. procedure takes @var{n} or more variables where @var{n} is the number
  64. of formal arguments before the period. There must be at least one
  65. argument before the period. The first @var{n} actual arguments will be
  66. stored into the newly allocated locations for the first @var{n} formal
  67. arguments and the sequence of the remaining actual arguments is
  68. converted into a list and the stored into the location for the last
  69. formal argument. If there are exactly @var{n} actual arguments, the
  70. empty list is stored into the location of the last formal argument.
  71. @end table
  72. The list in @var{variable} or @var{variablen+1} is always newly
  73. created and the procedure can modify it if desired. This is the case
  74. even when the procedure is invoked via @code{apply}, the required part
  75. of the list argument there will be copied (@pxref{Fly Evaluation,,
  76. Procedures for On the Fly Evaluation}).
  77. @var{body} is a sequence of Scheme expressions which are evaluated in
  78. order when the procedure is invoked.
  79. @end deffn
  80. @node Primitive Procedures
  81. @subsection Primitive Procedures
  82. @cindex primitives
  83. @cindex primitive procedures
  84. Procedures written in C can be registered for use from Scheme,
  85. provided they take only arguments of type @code{SCM} and return
  86. @code{SCM} values. @code{scm_c_define_gsubr} is likely to be the most
  87. useful mechanism, combining the process of registration
  88. (@code{scm_c_make_gsubr}) and definition (@code{scm_define}).
  89. @deftypefun SCM scm_c_make_gsubr (const char *name, int req, int opt, int rst, fcn)
  90. Register a C procedure @var{FCN} as a ``subr'' --- a primitive
  91. subroutine that can be called from Scheme. It will be associated with
  92. the given @var{name} but no environment binding will be created. The
  93. arguments @var{req}, @var{opt} and @var{rst} specify the number of
  94. required, optional and ``rest'' arguments respectively. The total
  95. number of these arguments should match the actual number of arguments
  96. to @var{fcn}. The number of rest arguments should be 0 or 1.
  97. @code{scm_c_make_gsubr} returns a value of type @code{SCM} which is a
  98. ``handle'' for the procedure.
  99. @end deftypefun
  100. @deftypefun SCM scm_c_define_gsubr (const char *name, int req, int opt, int rst, fcn)
  101. Register a C procedure @var{FCN}, as for @code{scm_c_make_gsubr}
  102. above, and additionally create a top-level Scheme binding for the
  103. procedure in the ``current environment'' using @code{scm_define}.
  104. @code{scm_c_define_gsubr} returns a handle for the procedure in the
  105. same way as @code{scm_c_make_gsubr}, which is usually not further
  106. required.
  107. @end deftypefun
  108. @code{scm_c_make_gsubr} and @code{scm_c_define_gsubr} automatically
  109. use @code{scm_c_make_subr} and also @code{scm_makcclo} if necessary.
  110. It is advisable to use the gsubr variants since they provide a
  111. slightly higher-level abstraction of the Guile implementation.
  112. @node Optional Arguments
  113. @subsection Optional Arguments
  114. @c FIXME::martin: Review me!
  115. Scheme procedures, as defined in R5RS, can either handle a fixed number
  116. of actual arguments, or a fixed number of actual arguments followed by
  117. arbitrarily many additional arguments. Writing procedures of variable
  118. arity can be useful, but unfortunately, the syntactic means for handling
  119. argument lists of varying length is a bit inconvenient. It is possible
  120. to give names to the fixed number of argument, but the remaining
  121. (optional) arguments can be only referenced as a list of values
  122. (@pxref{Lambda}).
  123. Guile comes with the module @code{(ice-9 optargs)}, which makes using
  124. optional arguments much more convenient. In addition, this module
  125. provides syntax for handling keywords in argument lists
  126. (@pxref{Keywords}).
  127. Before using any of the procedures or macros defined in this section,
  128. you have to load the module @code{(ice-9 optargs)} with the statement:
  129. @cindex @code{optargs}
  130. @lisp
  131. (use-modules (ice-9 optargs))
  132. @end lisp
  133. @menu
  134. * let-optional Reference:: Locally binding optional arguments.
  135. * let-keywords Reference:: Locally binding keywords arguments.
  136. * lambda* Reference:: Creating advanced argument handling procedures.
  137. * define* Reference:: Defining procedures and macros.
  138. @end menu
  139. @node let-optional Reference
  140. @subsubsection let-optional Reference
  141. @c FIXME::martin: Review me!
  142. The syntax @code{let-optional} and @code{let-optional*} are for
  143. destructuring rest argument lists and giving names to the various list
  144. elements. @code{let-optional} binds all variables simultaneously, while
  145. @code{let-optional*} binds them sequentially, consistent with @code{let}
  146. and @code{let*} (@pxref{Local Bindings}).
  147. @deffn {library syntax} let-optional rest-arg (binding @dots{}) expr @dots{}
  148. @deffnx {library syntax} let-optional* rest-arg (binding @dots{}) expr @dots{}
  149. These two macros give you an optional argument interface that is very
  150. @dfn{Schemey} and introduces no fancy syntax. They are compatible with
  151. the scsh macros of the same name, but are slightly extended. Each of
  152. @var{binding} may be of one of the forms @var{var} or @code{(@var{var}
  153. @var{default-value})}. @var{rest-arg} should be the rest-argument of the
  154. procedures these are used from. The items in @var{rest-arg} are
  155. sequentially bound to the variable names are given. When @var{rest-arg}
  156. runs out, the remaining vars are bound either to the default values or
  157. @code{#f} if no default value was specified. @var{rest-arg} remains
  158. bound to whatever may have been left of @var{rest-arg}.
  159. After binding the variables, the expressions @var{expr} @dots{} are
  160. evaluated in order.
  161. @end deffn
  162. @node let-keywords Reference
  163. @subsubsection let-keywords Reference
  164. @code{let-keywords} and @code{let-keywords*} extract values from
  165. keyword style argument lists, binding local variables to those values
  166. or to defaults.
  167. @deffn {library syntax} let-keywords args allow-other-keys? (binding @dots{}) body @dots{}
  168. @deffnx {library syntax} let-keywords* args allow-other-keys? (binding @dots{}) body @dots{}
  169. @var{args} is evaluated and should give a list of the form
  170. @code{(#:keyword1 value1 #:keyword2 value2 @dots{})}. The
  171. @var{binding}s are variables and default expressions, with the
  172. variables to be set (by name) from the keyword values. The @var{body}
  173. forms are then evaluated and the last is the result. An example will
  174. make the syntax clearest,
  175. @example
  176. (define args '(#:xyzzy "hello" #:foo "world"))
  177. (let-keywords args #t
  178. ((foo "default for foo")
  179. (bar (string-append "default" "for" "bar")))
  180. (display foo)
  181. (display ", ")
  182. (display bar))
  183. @print{} world, defaultforbar
  184. @end example
  185. The binding for @code{foo} comes from the @code{#:foo} keyword in
  186. @code{args}. But the binding for @code{bar} is the default in the
  187. @code{let-keywords}, since there's no @code{#:bar} in the args.
  188. @var{allow-other-keys?} is evaluated and controls whether unknown
  189. keywords are allowed in the @var{args} list. When true other keys are
  190. ignored (such as @code{#:xyzzy} in the example), when @code{#f} an
  191. error is thrown for anything unknown.
  192. @code{let-keywords} is like @code{let} (@pxref{Local Bindings}) in
  193. that all bindings are made at once, the defaults expressions are
  194. evaluated (if needed) outside the scope of the @code{let-keywords}.
  195. @code{let-keywords*} is like @code{let*}, each binding is made
  196. successively, and the default expressions see the bindings previously
  197. made. This is the style used by @code{lambda*} keywords
  198. (@pxref{lambda* Reference}). For example,
  199. @example
  200. (define args '(#:foo 3))
  201. (let-keywords* args #f
  202. ((foo 99)
  203. (bar (+ foo 6)))
  204. (display bar))
  205. @print{} 9
  206. @end example
  207. The expression for each default is only evaluated if it's needed,
  208. ie. if the keyword doesn't appear in @var{args}. So one way to make a
  209. keyword mandatory is to throw an error of some sort as the default.
  210. @example
  211. (define args '(#:start 7 #:finish 13))
  212. (let-keywords* args #t
  213. ((start 0)
  214. (stop (error "missing #:stop argument")))
  215. ...)
  216. @result{} ERROR: missing #:stop argument
  217. @end example
  218. @end deffn
  219. @node lambda* Reference
  220. @subsubsection lambda* Reference
  221. When using optional and keyword argument lists, @code{lambda} for
  222. creating a procedure then @code{let-optional} or @code{let-keywords}
  223. is a bit lengthy. @code{lambda*} combines the features of those
  224. macros into a single convenient syntax.
  225. @deffn {library syntax} lambda* ([var@dots{}] @* [#:optional vardef@dots{}] @* [#:key vardef@dots{} [#:allow-other-keys]] @* [#:rest var | . var]) @* body
  226. @sp 1
  227. Create a procedure which takes optional and/or keyword arguments
  228. specified with @code{#:optional} and @code{#:key}. For example,
  229. @lisp
  230. (lambda* (a b #:optional c d . e) '())
  231. @end lisp
  232. is a procedure with fixed arguments @var{a} and @var{b}, optional
  233. arguments @var{c} and @var{d}, and rest argument @var{e}. If the
  234. optional arguments are omitted in a call, the variables for them are
  235. bound to @code{#f}.
  236. @code{lambda*} can also take keyword arguments. For example, a procedure
  237. defined like this:
  238. @lisp
  239. (lambda* (#:key xyzzy larch) '())
  240. @end lisp
  241. can be called with any of the argument lists @code{(#:xyzzy 11)},
  242. @code{(#:larch 13)}, @code{(#:larch 42 #:xyzzy 19)}, @code{()}.
  243. Whichever arguments are given as keywords are bound to values (and
  244. those not given are @code{#f}).
  245. Optional and keyword arguments can also have default values to take
  246. when not present in a call, by giving a two-element list of variable
  247. name and expression. For example in
  248. @lisp
  249. (lambda* (foo #:optional (bar 42) #:key (baz 73))
  250. (list foo bar baz))
  251. @end lisp
  252. @var{foo} is a fixed argument, @var{bar} is an optional argument with
  253. default value 42, and baz is a keyword argument with default value 73.
  254. Default value expressions are not evaluated unless they are needed,
  255. and until the procedure is called.
  256. Normally it's an error if a call has keywords other than those
  257. specified by @code{#:key}, but adding @code{#:allow-other-keys} to the
  258. definition (after the keyword argument declarations) will ignore
  259. unknown keywords.
  260. If a call has a keyword given twice, the last value is used. For
  261. example,
  262. @lisp
  263. ((lambda* (#:key (heads 0) (tails 0))
  264. (display (list heads tails)))
  265. #:heads 37 #:tails 42 #:heads 99)
  266. @print{} (99 42)
  267. @end lisp
  268. @code{#:rest} is a synonym for the dotted syntax rest argument. The
  269. argument lists @code{(a . b)} and @code{(a #:rest b)} are equivalent
  270. in all respects. This is provided for more similarity to DSSSL,
  271. MIT-Scheme and Kawa among others, as well as for refugees from other
  272. Lisp dialects.
  273. When @code{#:key} is used together with a rest argument, the keyword
  274. parameters in a call all remain in the rest list. This is the same as
  275. Common Lisp. For example,
  276. @lisp
  277. ((lambda* (#:key (x 0) #:allow-other-keys #:rest r)
  278. (display r))
  279. #:x 123 #:y 456)
  280. @print{} (#:x 123 #:y 456)
  281. @end lisp
  282. @code{#:optional} and @code{#:key} establish their bindings
  283. successively, from left to right, as per @code{let-optional*} and
  284. @code{let-keywords*}. This means default expressions can refer back
  285. to prior parameters, for example
  286. @lisp
  287. (lambda* (start #:optional (end (+ 10 start)))
  288. (do ((i start (1+ i)))
  289. ((> i end))
  290. (display i)))
  291. @end lisp
  292. @end deffn
  293. @node define* Reference
  294. @subsubsection define* Reference
  295. @c FIXME::martin: Review me!
  296. Just like @code{define} has a shorthand notation for defining procedures
  297. (@pxref{Lambda Alternatives}), @code{define*} is provided as an
  298. abbreviation of the combination of @code{define} and @code{lambda*}.
  299. @code{define*-public} is the @code{lambda*} version of
  300. @code{define-public}; @code{defmacro*} and @code{defmacro*-public} exist
  301. for defining macros with the improved argument list handling
  302. possibilities. The @code{-public} versions not only define the
  303. procedures/macros, but also export them from the current module.
  304. @deffn {library syntax} define* formals body
  305. @deffnx {library syntax} define*-public formals body
  306. @code{define*} and @code{define*-public} support optional arguments with
  307. a similar syntax to @code{lambda*}. They also support arbitrary-depth
  308. currying, just like Guile's define. Some examples:
  309. @lisp
  310. (define* (x y #:optional a (z 3) #:key w . u)
  311. (display (list y z u)))
  312. @end lisp
  313. defines a procedure @code{x} with a fixed argument @var{y}, an optional
  314. argument @var{a}, another optional argument @var{z} with default value 3,
  315. a keyword argument @var{w}, and a rest argument @var{u}.
  316. @lisp
  317. (define-public* ((foo #:optional bar) #:optional baz) '())
  318. @end lisp
  319. This illustrates currying. A procedure @code{foo} is defined, which,
  320. when called with an optional argument @var{bar}, returns a procedure
  321. that takes an optional argument @var{baz}.
  322. Of course, @code{define*[-public]} also supports @code{#:rest} and
  323. @code{#:allow-other-keys} in the same way as @code{lambda*}.
  324. @end deffn
  325. @deffn {library syntax} defmacro* name formals body
  326. @deffnx {library syntax} defmacro*-public name formals body
  327. These are just like @code{defmacro} and @code{defmacro-public} except that they
  328. take @code{lambda*}-style extended parameter lists, where @code{#:optional},
  329. @code{#:key}, @code{#:allow-other-keys} and @code{#:rest} are allowed with the usual
  330. semantics. Here is an example of a macro with an optional argument:
  331. @lisp
  332. (defmacro* transmorgify (a #:optional b)
  333. (a 1))
  334. @end lisp
  335. @end deffn
  336. @node Procedure Properties
  337. @subsection Procedure Properties and Meta-information
  338. @c FIXME::martin: Review me!
  339. Procedures always have attached the environment in which they were
  340. created and information about how to apply them to actual arguments. In
  341. addition to that, properties and meta-information can be stored with
  342. procedures. The procedures in this section can be used to test whether
  343. a given procedure satisfies a condition; and to access and set a
  344. procedure's property.
  345. The first group of procedures are predicates to test whether a Scheme
  346. object is a procedure, or a special procedure, respectively.
  347. @code{procedure?} is the most general predicates, it returns @code{#t}
  348. for any kind of procedure. @code{closure?} does not return @code{#t}
  349. for primitive procedures, and @code{thunk?} only returns @code{#t} for
  350. procedures which do not accept any arguments.
  351. @rnindex procedure?
  352. @deffn {Scheme Procedure} procedure? obj
  353. @deffnx {C Function} scm_procedure_p (obj)
  354. Return @code{#t} if @var{obj} is a procedure.
  355. @end deffn
  356. @deffn {Scheme Procedure} closure? obj
  357. @deffnx {C Function} scm_closure_p (obj)
  358. Return @code{#t} if @var{obj} is a closure.
  359. @end deffn
  360. @deffn {Scheme Procedure} thunk? obj
  361. @deffnx {C Function} scm_thunk_p (obj)
  362. Return @code{#t} if @var{obj} is a thunk.
  363. @end deffn
  364. @c FIXME::martin: Is that true?
  365. @cindex procedure properties
  366. Procedure properties are general properties to be attached to
  367. procedures. These can be the name of a procedure or other relevant
  368. information, such as debug hints.
  369. @deffn {Scheme Procedure} procedure-name proc
  370. @deffnx {C Function} scm_procedure_name (proc)
  371. Return the name of the procedure @var{proc}
  372. @end deffn
  373. @deffn {Scheme Procedure} procedure-source proc
  374. @deffnx {C Function} scm_procedure_source (proc)
  375. Return the source of the procedure @var{proc}.
  376. @end deffn
  377. @deffn {Scheme Procedure} procedure-environment proc
  378. @deffnx {C Function} scm_procedure_environment (proc)
  379. Return the environment of the procedure @var{proc}.
  380. @end deffn
  381. @deffn {Scheme Procedure} procedure-properties proc
  382. @deffnx {C Function} scm_procedure_properties (proc)
  383. Return @var{obj}'s property list.
  384. @end deffn
  385. @deffn {Scheme Procedure} procedure-property obj key
  386. @deffnx {C Function} scm_procedure_property (obj, key)
  387. Return the property of @var{obj} with name @var{key}.
  388. @end deffn
  389. @deffn {Scheme Procedure} set-procedure-properties! proc alist
  390. @deffnx {C Function} scm_set_procedure_properties_x (proc, alist)
  391. Set @var{obj}'s property list to @var{alist}.
  392. @end deffn
  393. @deffn {Scheme Procedure} set-procedure-property! obj key value
  394. @deffnx {C Function} scm_set_procedure_property_x (obj, key, value)
  395. In @var{obj}'s property list, set the property named @var{key} to
  396. @var{value}.
  397. @end deffn
  398. @cindex procedure documentation
  399. Documentation for a procedure can be accessed with the procedure
  400. @code{procedure-documentation}.
  401. @deffn {Scheme Procedure} procedure-documentation proc
  402. @deffnx {C Function} scm_procedure_documentation (proc)
  403. Return the documentation string associated with @code{proc}. By
  404. convention, if a procedure contains more than one expression and the
  405. first expression is a string constant, that string is assumed to contain
  406. documentation for that procedure.
  407. @end deffn
  408. @node Procedures with Setters
  409. @subsection Procedures with Setters
  410. @c FIXME::martin: Review me!
  411. @c FIXME::martin: Document `operator struct'.
  412. @cindex procedure with setter
  413. @cindex setter
  414. A @dfn{procedure with setter} is a special kind of procedure which
  415. normally behaves like any accessor procedure, that is a procedure which
  416. accesses a data structure. The difference is that this kind of
  417. procedure has a so-called @dfn{setter} attached, which is a procedure
  418. for storing something into a data structure.
  419. Procedures with setters are treated specially when the procedure appears
  420. in the special form @code{set!} (REFFIXME). How it works is best shown
  421. by example.
  422. Suppose we have a procedure called @code{foo-ref}, which accepts two
  423. arguments, a value of type @code{foo} and an integer. The procedure
  424. returns the value stored at the given index in the @code{foo} object.
  425. Let @code{f} be a variable containing such a @code{foo} data
  426. structure.@footnote{Working definitions would be:
  427. @lisp
  428. (define foo-ref vector-ref)
  429. (define foo-set! vector-set!)
  430. (define f (make-vector 2 #f))
  431. @end lisp
  432. }
  433. @lisp
  434. (foo-ref f 0) @result{} bar
  435. (foo-ref f 1) @result{} braz
  436. @end lisp
  437. Also suppose that a corresponding setter procedure called
  438. @code{foo-set!} does exist.
  439. @lisp
  440. (foo-set! f 0 'bla)
  441. (foo-ref f 0) @result{} bla
  442. @end lisp
  443. Now we could create a new procedure called @code{foo}, which is a
  444. procedure with setter, by calling @code{make-procedure-with-setter} with
  445. the accessor and setter procedures @code{foo-ref} and @code{foo-set!}.
  446. Let us call this new procedure @code{foo}.
  447. @lisp
  448. (define foo (make-procedure-with-setter foo-ref foo-set!))
  449. @end lisp
  450. @code{foo} can from now an be used to either read from the data
  451. structure stored in @code{f}, or to write into the structure.
  452. @lisp
  453. (set! (foo f 0) 'dum)
  454. (foo f 0) @result{} dum
  455. @end lisp
  456. @deffn {Scheme Procedure} make-procedure-with-setter procedure setter
  457. @deffnx {C Function} scm_make_procedure_with_setter (procedure, setter)
  458. Create a new procedure which behaves like @var{procedure}, but
  459. with the associated setter @var{setter}.
  460. @end deffn
  461. @deffn {Scheme Procedure} procedure-with-setter? obj
  462. @deffnx {C Function} scm_procedure_with_setter_p (obj)
  463. Return @code{#t} if @var{obj} is a procedure with an
  464. associated setter procedure.
  465. @end deffn
  466. @deffn {Scheme Procedure} procedure proc
  467. @deffnx {C Function} scm_procedure (proc)
  468. Return the procedure of @var{proc}, which must be either a
  469. procedure with setter, or an operator struct.
  470. @end deffn
  471. @deffn {Scheme Procedure} setter proc
  472. Return the setter of @var{proc}, which must be either a procedure with
  473. setter or an operator struct.
  474. @end deffn
  475. @node Macros
  476. @subsection Lisp Style Macro Definitions
  477. @cindex macros
  478. @cindex transformation
  479. Macros are objects which cause the expression that they appear in to be
  480. transformed in some way @emph{before} being evaluated. In expressions
  481. that are intended for macro transformation, the identifier that names
  482. the relevant macro must appear as the first element, like this:
  483. @lisp
  484. (@var{macro-name} @var{macro-args} @dots{})
  485. @end lisp
  486. In Lisp-like languages, the traditional way to define macros is very
  487. similar to procedure definitions. The key differences are that the
  488. macro definition body should return a list that describes the
  489. transformed expression, and that the definition is marked as a macro
  490. definition (rather than a procedure definition) by the use of a
  491. different definition keyword: in Lisp, @code{defmacro} rather than
  492. @code{defun}, and in Scheme, @code{define-macro} rather than
  493. @code{define}.
  494. @fnindex defmacro
  495. @fnindex define-macro
  496. Guile supports this style of macro definition using both @code{defmacro}
  497. and @code{define-macro}. The only difference between them is how the
  498. macro name and arguments are grouped together in the definition:
  499. @lisp
  500. (defmacro @var{name} (@var{args} @dots{}) @var{body} @dots{})
  501. @end lisp
  502. @noindent
  503. is the same as
  504. @lisp
  505. (define-macro (@var{name} @var{args} @dots{}) @var{body} @dots{})
  506. @end lisp
  507. @noindent
  508. The difference is analogous to the corresponding difference between
  509. Lisp's @code{defun} and Scheme's @code{define}.
  510. @code{false-if-exception}, from the @file{boot-9.scm} file in the Guile
  511. distribution, is a good example of macro definition using
  512. @code{defmacro}:
  513. @lisp
  514. (defmacro false-if-exception (expr)
  515. `(catch #t
  516. (lambda () ,expr)
  517. (lambda args #f)))
  518. @end lisp
  519. @noindent
  520. The effect of this definition is that expressions beginning with the
  521. identifier @code{false-if-exception} are automatically transformed into
  522. a @code{catch} expression following the macro definition specification.
  523. For example:
  524. @lisp
  525. (false-if-exception (open-input-file "may-not-exist"))
  526. @equiv{}
  527. (catch #t
  528. (lambda () (open-input-file "may-not-exist"))
  529. (lambda args #f))
  530. @end lisp
  531. @node Syntax Rules
  532. @subsection The R5RS @code{syntax-rules} System
  533. @cindex R5RS syntax-rules system
  534. R5RS defines an alternative system for macro and syntax transformations
  535. using the keywords @code{define-syntax}, @code{let-syntax},
  536. @code{letrec-syntax} and @code{syntax-rules}.
  537. The main difference between the R5RS system and the traditional macros
  538. of the previous section is how the transformation is specified. In
  539. R5RS, rather than permitting a macro definition to return an arbitrary
  540. expression, the transformation is specified in a pattern language that
  541. @itemize @bullet
  542. @item
  543. does not require complicated quoting and extraction of components of the
  544. source expression using @code{caddr} etc.
  545. @item
  546. is designed such that the bindings associated with identifiers in the
  547. transformed expression are well defined, and such that it is impossible
  548. for the transformed expression to construct new identifiers.
  549. @end itemize
  550. @noindent
  551. The last point is commonly referred to as being @dfn{hygienic}: the R5RS
  552. @code{syntax-case} system provides @dfn{hygienic macros}.
  553. For example, the R5RS pattern language for the @code{false-if-exception}
  554. example of the previous section looks like this:
  555. @lisp
  556. (syntax-rules ()
  557. ((_ expr)
  558. (catch #t
  559. (lambda () expr)
  560. (lambda args #f))))
  561. @end lisp
  562. @cindex @code{syncase}
  563. In Guile, the @code{syntax-rules} system is provided by the @code{(ice-9
  564. syncase)} module. To make these facilities available in your code,
  565. include the expression @code{(use-syntax (ice-9 syncase))} (@pxref{Using
  566. Guile Modules}) before the first usage of @code{define-syntax} etc. If
  567. you are writing a Scheme module, you can alternatively include the form
  568. @code{#:use-syntax (ice-9 syncase)} in your @code{define-module}
  569. declaration (@pxref{Creating Guile Modules}).
  570. @menu
  571. * Pattern Language:: The @code{syntax-rules} pattern language.
  572. * Define-Syntax:: Top level syntax definitions.
  573. * Let-Syntax:: Local syntax definitions.
  574. @end menu
  575. @node Pattern Language
  576. @subsubsection The @code{syntax-rules} Pattern Language
  577. @node Define-Syntax
  578. @subsubsection Top Level Syntax Definitions
  579. define-syntax: The gist is
  580. (define-syntax <keyword> <transformer-spec>)
  581. makes the <keyword> into a macro so that
  582. (<keyword> ...)
  583. expands at _compile_ or _read_ time (i.e. before any
  584. evaluation begins) into some expression that is
  585. given by the <transformer-spec>.
  586. @node Let-Syntax
  587. @subsubsection Local Syntax Definitions
  588. @node Syntax Case
  589. @subsection Support for the @code{syntax-case} System
  590. @node Internal Macros
  591. @subsection Internal Representation of Macros and Syntax
  592. Internally, Guile uses three different flavors of macros. The three
  593. flavors are called @dfn{acro} (or @dfn{syntax}), @dfn{macro} and
  594. @dfn{mmacro}.
  595. Given the expression
  596. @lisp
  597. (foo @dots{})
  598. @end lisp
  599. @noindent
  600. with @code{foo} being some flavor of macro, one of the following things
  601. will happen when the expression is evaluated.
  602. @itemize @bullet
  603. @item
  604. When @code{foo} has been defined to be an @dfn{acro}, the procedure used
  605. in the acro definition of @code{foo} is passed the whole expression and
  606. the current lexical environment, and whatever that procedure returns is
  607. the value of evaluating the expression. You can think of this a
  608. procedure that receives its argument as an unevaluated expression.
  609. @item
  610. When @code{foo} has been defined to be a @dfn{macro}, the procedure used
  611. in the macro definition of @code{foo} is passed the whole expression and
  612. the current lexical environment, and whatever that procedure returns is
  613. evaluated again. That is, the procedure should return a valid Scheme
  614. expression.
  615. @item
  616. When @code{foo} has been defined to be a @dfn{mmacro}, the procedure
  617. used in the mmacro definition of `foo' is passed the whole expression
  618. and the current lexical environment, and whatever that procedure returns
  619. replaces the original expression. Evaluation then starts over from the
  620. new expression that has just been returned.
  621. @end itemize
  622. The key difference between a @dfn{macro} and a @dfn{mmacro} is that the
  623. expression returned by a @dfn{mmacro} procedure is remembered (or
  624. @dfn{memoized}) so that the expansion does not need to be done again
  625. next time the containing code is evaluated.
  626. The primitives @code{procedure->syntax}, @code{procedure->macro} and
  627. @code{procedure->memoizing-macro} are used to construct acros, macros
  628. and mmacros respectively. However, if you do not have a very special
  629. reason to use one of these primitives, you should avoid them: they are
  630. very specific to Guile's current implementation and therefore likely to
  631. change. Use @code{defmacro}, @code{define-macro} (@pxref{Macros}) or
  632. @code{define-syntax} (@pxref{Syntax Rules}) instead. (In low level
  633. terms, @code{defmacro}, @code{define-macro} and @code{define-syntax} are
  634. all implemented as mmacros.)
  635. @deffn {Scheme Procedure} procedure->syntax code
  636. @deffnx {C Function} scm_makacro (code)
  637. Return a macro which, when a symbol defined to this value appears as the
  638. first symbol in an expression, returns the result of applying @var{code}
  639. to the expression and the environment.
  640. @end deffn
  641. @deffn {Scheme Procedure} procedure->macro code
  642. @deffnx {C Function} scm_makmacro (code)
  643. Return a macro which, when a symbol defined to this value appears as the
  644. first symbol in an expression, evaluates the result of applying
  645. @var{code} to the expression and the environment. For example:
  646. @lisp
  647. (define trace
  648. (procedure->macro
  649. (lambda (x env)
  650. `(set! ,(cadr x) (tracef ,(cadr x) ',(cadr x))))))
  651. (trace @i{foo})
  652. @equiv{}
  653. (set! @i{foo} (tracef @i{foo} '@i{foo})).
  654. @end lisp
  655. @end deffn
  656. @deffn {Scheme Procedure} procedure->memoizing-macro code
  657. @deffnx {C Function} scm_makmmacro (code)
  658. Return a macro which, when a symbol defined to this value appears as the
  659. first symbol in an expression, evaluates the result of applying
  660. @var{code} to the expression and the environment.
  661. @code{procedure->memoizing-macro} is the same as
  662. @code{procedure->macro}, except that the expression returned by
  663. @var{code} replaces the original macro expression in the memoized form
  664. of the containing code.
  665. @end deffn
  666. In the following primitives, @dfn{acro} flavor macros are referred to
  667. as @dfn{syntax transformers}.
  668. @deffn {Scheme Procedure} macro? obj
  669. @deffnx {C Function} scm_macro_p (obj)
  670. Return @code{#t} if @var{obj} is a regular macro, a memoizing macro or a
  671. syntax transformer.
  672. @end deffn
  673. @deffn {Scheme Procedure} macro-type m
  674. @deffnx {C Function} scm_macro_type (m)
  675. Return one of the symbols @code{syntax}, @code{macro} or
  676. @code{macro!}, depending on whether @var{m} is a syntax
  677. transformer, a regular macro, or a memoizing macro,
  678. respectively. If @var{m} is not a macro, @code{#f} is
  679. returned.
  680. @end deffn
  681. @deffn {Scheme Procedure} macro-name m
  682. @deffnx {C Function} scm_macro_name (m)
  683. Return the name of the macro @var{m}.
  684. @end deffn
  685. @deffn {Scheme Procedure} macro-transformer m
  686. @deffnx {C Function} scm_macro_transformer (m)
  687. Return the transformer of the macro @var{m}.
  688. @end deffn
  689. @deffn {Scheme Procedure} cons-source xorig x y
  690. @deffnx {C Function} scm_cons_source (xorig, x, y)
  691. Create and return a new pair whose car and cdr are @var{x} and @var{y}.
  692. Any source properties associated with @var{xorig} are also associated
  693. with the new pair.
  694. @end deffn
  695. @c Local Variables:
  696. @c TeX-master: "guile.texi"
  697. @c End: