optargs.scm 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. ;;;; optargs.scm -- support for optional arguments
  2. ;;;;
  3. ;;;; Copyright (C) 1997, 1998, 1999, 2001, 2002, 2004, 2006 Free Software Foundation, Inc.
  4. ;;;;
  5. ;;;; This library is free software; you can redistribute it and/or
  6. ;;;; modify it under the terms of the GNU Lesser General Public
  7. ;;;; License as published by the Free Software Foundation; either
  8. ;;;; version 2.1 of the License, or (at your option) any later version.
  9. ;;;;
  10. ;;;; This library is distributed in the hope that it will be useful,
  11. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. ;;;; Lesser General Public License for more details.
  14. ;;;;
  15. ;;;; You should have received a copy of the GNU Lesser General Public
  16. ;;;; License along with this library; if not, write to the Free Software
  17. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. ;;;;
  19. ;;;; Contributed by Maciej Stachowiak <mstachow@alum.mit.edu>
  20. ;;; Commentary:
  21. ;;; {Optional Arguments}
  22. ;;;
  23. ;;; The C interface for creating Guile procedures has a very handy
  24. ;;; "optional argument" feature. This module attempts to provide
  25. ;;; similar functionality for procedures defined in Scheme with
  26. ;;; a convenient and attractive syntax.
  27. ;;;
  28. ;;; exported macros are:
  29. ;;; let-optional
  30. ;;; let-optional*
  31. ;;; let-keywords
  32. ;;; let-keywords*
  33. ;;; lambda*
  34. ;;; define*
  35. ;;; define*-public
  36. ;;; defmacro*
  37. ;;; defmacro*-public
  38. ;;;
  39. ;;;
  40. ;;; Summary of the lambda* extended parameter list syntax (brackets
  41. ;;; are used to indicate grouping only):
  42. ;;;
  43. ;;; ext-param-list ::= [identifier]* [#:optional [ext-var-decl]+]?
  44. ;;; [#:key [ext-var-decl]+ [#:allow-other-keys]?]?
  45. ;;; [[#:rest identifier]|[. identifier]]?
  46. ;;;
  47. ;;; ext-var-decl ::= identifier | ( identifier expression )
  48. ;;;
  49. ;;; The characters `*', `+' and `?' are not to be taken literally; they
  50. ;;; mean respectively, zero or more occurences, one or more occurences,
  51. ;;; and one or zero occurences.
  52. ;;;
  53. ;;; Code:
  54. (define-module (ice-9 optargs)
  55. :export-syntax (let-optional
  56. let-optional*
  57. let-keywords
  58. let-keywords*
  59. define* lambda*
  60. define*-public
  61. defmacro*
  62. defmacro*-public))
  63. ;; let-optional rest-arg (binding ...) . body
  64. ;; let-optional* rest-arg (binding ...) . body
  65. ;; macros used to bind optional arguments
  66. ;;
  67. ;; These two macros give you an optional argument interface that is
  68. ;; very "Schemey" and introduces no fancy syntax. They are compatible
  69. ;; with the scsh macros of the same name, but are slightly
  70. ;; extended. Each of binding may be of one of the forms <var> or
  71. ;; (<var> <default-value>). rest-arg should be the rest-argument of
  72. ;; the procedures these are used from. The items in rest-arg are
  73. ;; sequentially bound to the variable namess are given. When rest-arg
  74. ;; runs out, the remaining vars are bound either to the default values
  75. ;; or to `#f' if no default value was specified. rest-arg remains
  76. ;; bound to whatever may have been left of rest-arg.
  77. ;;
  78. (defmacro let-optional (REST-ARG BINDINGS . BODY)
  79. (let-optional-template REST-ARG BINDINGS BODY 'let))
  80. (defmacro let-optional* (REST-ARG BINDINGS . BODY)
  81. (let-optional-template REST-ARG BINDINGS BODY 'let*))
  82. ;; let-keywords rest-arg allow-other-keys? (binding ...) . body
  83. ;; let-keywords* rest-arg allow-other-keys? (binding ...) . body
  84. ;; macros used to bind keyword arguments
  85. ;;
  86. ;; These macros pick out keyword arguments from rest-arg, but do not
  87. ;; modify it. This is consistent at least with Common Lisp, which
  88. ;; duplicates keyword args in the rest arg. More explanation of what
  89. ;; keyword arguments in a lambda list look like can be found below in
  90. ;; the documentation for lambda*. Bindings can have the same form as
  91. ;; for let-optional. If allow-other-keys? is false, an error will be
  92. ;; thrown if anything that looks like a keyword argument but does not
  93. ;; match a known keyword parameter will result in an error.
  94. ;;
  95. (defmacro let-keywords (REST-ARG ALLOW-OTHER-KEYS? BINDINGS . BODY)
  96. (let-keywords-template REST-ARG ALLOW-OTHER-KEYS? BINDINGS BODY 'let))
  97. (defmacro let-keywords* (REST-ARG ALLOW-OTHER-KEYS? BINDINGS . BODY)
  98. (let-keywords-template REST-ARG ALLOW-OTHER-KEYS? BINDINGS BODY 'let*))
  99. ;; some utility procedures for implementing the various let-forms.
  100. (define (let-o-k-template REST-ARG BINDINGS BODY let-type proc)
  101. (let ((bindings (map (lambda (x)
  102. (if (list? x)
  103. x
  104. (list x #f)))
  105. BINDINGS)))
  106. `(,let-type ,(map proc bindings) ,@BODY)))
  107. (define (let-optional-template REST-ARG BINDINGS BODY let-type)
  108. (if (null? BINDINGS)
  109. `(let () ,@BODY)
  110. (let-o-k-template REST-ARG BINDINGS BODY let-type
  111. (lambda (optional)
  112. `(,(car optional)
  113. (cond
  114. ((not (null? ,REST-ARG))
  115. (let ((result (car ,REST-ARG)))
  116. ,(list 'set! REST-ARG
  117. `(cdr ,REST-ARG))
  118. result))
  119. (else
  120. ,(cadr optional))))))))
  121. (define (let-keywords-template REST-ARG ALLOW-OTHER-KEYS? BINDINGS BODY let-type)
  122. (if (null? BINDINGS)
  123. `(let () ,@BODY)
  124. (let* ((kb-list-gensym (gensym "kb:G"))
  125. (bindfilter (lambda (key)
  126. `(,(car key)
  127. (cond
  128. ((assq ',(car key) ,kb-list-gensym)
  129. => cdr)
  130. (else
  131. ,(cadr key)))))))
  132. `(let* ((ra->kbl ,rest-arg->keyword-binding-list)
  133. (,kb-list-gensym (ra->kbl ,REST-ARG ',(map
  134. (lambda (x) (symbol->keyword (if (pair? x) (car x) x)))
  135. BINDINGS)
  136. ,ALLOW-OTHER-KEYS?)))
  137. ,(let-o-k-template REST-ARG BINDINGS BODY let-type bindfilter)))))
  138. (define (rest-arg->keyword-binding-list rest-arg keywords allow-other-keys?)
  139. (if (null? rest-arg)
  140. '()
  141. (let loop ((first (car rest-arg))
  142. (rest (cdr rest-arg))
  143. (accum '()))
  144. (let ((next (lambda (a)
  145. (if (null? (cdr rest))
  146. a
  147. (loop (cadr rest) (cddr rest) a)))))
  148. (if (keyword? first)
  149. (cond
  150. ((memq first keywords)
  151. (if (null? rest)
  152. (error "Keyword argument has no value.")
  153. (next (cons (cons (keyword->symbol first)
  154. (car rest)) accum))))
  155. ((not allow-other-keys?)
  156. (error "Unknown keyword in arguments."))
  157. (else (if (null? rest)
  158. accum
  159. (next accum))))
  160. (if (null? rest)
  161. accum
  162. (loop (car rest) (cdr rest) accum)))))))
  163. ;; lambda* args . body
  164. ;; lambda extended for optional and keyword arguments
  165. ;;
  166. ;; lambda* creates a procedure that takes optional arguments. These
  167. ;; are specified by putting them inside brackets at the end of the
  168. ;; paramater list, but before any dotted rest argument. For example,
  169. ;; (lambda* (a b #:optional c d . e) '())
  170. ;; creates a procedure with fixed arguments a and b, optional arguments c
  171. ;; and d, and rest argument e. If the optional arguments are omitted
  172. ;; in a call, the variables for them are bound to `#f'.
  173. ;;
  174. ;; lambda* can also take keyword arguments. For example, a procedure
  175. ;; defined like this:
  176. ;; (lambda* (#:key xyzzy larch) '())
  177. ;; can be called with any of the argument lists (#:xyzzy 11)
  178. ;; (#:larch 13) (#:larch 42 #:xyzzy 19) (). Whichever arguments
  179. ;; are given as keywords are bound to values.
  180. ;;
  181. ;; Optional and keyword arguments can also be given default values
  182. ;; which they take on when they are not present in a call, by giving a
  183. ;; two-item list in place of an optional argument, for example in:
  184. ;; (lambda* (foo #:optional (bar 42) #:key (baz 73)) (list foo bar baz))
  185. ;; foo is a fixed argument, bar is an optional argument with default
  186. ;; value 42, and baz is a keyword argument with default value 73.
  187. ;; Default value expressions are not evaluated unless they are needed
  188. ;; and until the procedure is called.
  189. ;;
  190. ;; lambda* now supports two more special parameter list keywords.
  191. ;;
  192. ;; lambda*-defined procedures now throw an error by default if a
  193. ;; keyword other than one of those specified is found in the actual
  194. ;; passed arguments. However, specifying #:allow-other-keys
  195. ;; immediately after the keyword argument declarations restores the
  196. ;; previous behavior of ignoring unknown keywords. lambda* also now
  197. ;; guarantees that if the same keyword is passed more than once, the
  198. ;; last one passed is the one that takes effect. For example,
  199. ;; ((lambda* (#:key (heads 0) (tails 0)) (display (list heads tails)))
  200. ;; #:heads 37 #:tails 42 #:heads 99)
  201. ;; would result in (99 47) being displayed.
  202. ;;
  203. ;; #:rest is also now provided as a synonym for the dotted syntax rest
  204. ;; argument. The argument lists (a . b) and (a #:rest b) are equivalent in
  205. ;; all respects to lambda*. This is provided for more similarity to DSSSL,
  206. ;; MIT-Scheme and Kawa among others, as well as for refugees from other
  207. ;; Lisp dialects.
  208. (defmacro lambda* (ARGLIST . BODY)
  209. (parse-arglist
  210. ARGLIST
  211. (lambda (non-optional-args optionals keys aok? rest-arg)
  212. ;; Check for syntax errors.
  213. (if (not (every? symbol? non-optional-args))
  214. (error "Syntax error in fixed argument declaration."))
  215. (if (not (every? ext-decl? optionals))
  216. (error "Syntax error in optional argument declaration."))
  217. (if (not (every? ext-decl? keys))
  218. (error "Syntax error in keyword argument declaration."))
  219. (if (not (or (symbol? rest-arg) (eq? #f rest-arg)))
  220. (error "Syntax error in rest argument declaration."))
  221. ;; generate the code.
  222. (let ((rest-gensym (or rest-arg (gensym "lambda*:G")))
  223. (lambda-gensym (gensym "lambda*:L")))
  224. (if (not (and (null? optionals) (null? keys)))
  225. `(let ((,lambda-gensym
  226. (lambda (,@non-optional-args . ,rest-gensym)
  227. ;; Make sure that if the proc had a docstring, we put it
  228. ;; here where it will be visible.
  229. ,@(if (and (not (null? BODY))
  230. (string? (car BODY)))
  231. (list (car BODY))
  232. '())
  233. (let-optional*
  234. ,rest-gensym
  235. ,optionals
  236. (let-keywords* ,rest-gensym
  237. ,aok?
  238. ,keys
  239. ,@(if (and (not rest-arg) (null? keys))
  240. `((if (not (null? ,rest-gensym))
  241. (error "Too many arguments.")))
  242. '())
  243. (let ()
  244. ,@BODY))))))
  245. (set-procedure-property! ,lambda-gensym 'arglist
  246. '(,non-optional-args
  247. ,optionals
  248. ,keys
  249. ,aok?
  250. ,rest-arg))
  251. ,lambda-gensym)
  252. `(lambda (,@non-optional-args . ,(if rest-arg rest-arg '()))
  253. ,@BODY))))))
  254. (define (every? pred lst)
  255. (or (null? lst)
  256. (and (pred (car lst))
  257. (every? pred (cdr lst)))))
  258. (define (ext-decl? obj)
  259. (or (symbol? obj)
  260. (and (list? obj) (= 2 (length obj)) (symbol? (car obj)))))
  261. ;; XXX - not tail recursive
  262. (define (improper-list-copy obj)
  263. (if (pair? obj)
  264. (cons (car obj) (improper-list-copy (cdr obj)))
  265. obj))
  266. (define (parse-arglist arglist cont)
  267. (define (split-list-at val lst cont)
  268. (cond
  269. ((memq val lst)
  270. => (lambda (pos)
  271. (if (memq val (cdr pos))
  272. (error (with-output-to-string
  273. (lambda ()
  274. (map display `(,val
  275. " specified more than once in argument list.")))))
  276. (cont (reverse (cdr (memq val (reverse lst)))) (cdr pos) #t))))
  277. (else (cont lst '() #f))))
  278. (define (parse-opt-and-fixed arglist keys aok? rest cont)
  279. (split-list-at
  280. #:optional arglist
  281. (lambda (before after split?)
  282. (if (and split? (null? after))
  283. (error "#:optional specified but no optional arguments declared.")
  284. (cont before after keys aok? rest)))))
  285. (define (parse-keys arglist rest cont)
  286. (split-list-at
  287. #:allow-other-keys arglist
  288. (lambda (aok-before aok-after aok-split?)
  289. (if (and aok-split? (not (null? aok-after)))
  290. (error "#:allow-other-keys not at end of keyword argument declarations.")
  291. (split-list-at
  292. #:key aok-before
  293. (lambda (key-before key-after key-split?)
  294. (cond
  295. ((and aok-split? (not key-split?))
  296. (error "#:allow-other-keys specified but no keyword arguments declared."))
  297. (key-split?
  298. (cond
  299. ((null? key-after) (error "#:key specified but no keyword arguments declared."))
  300. ((memq #:optional key-after) (error "#:optional arguments declared after #:key arguments."))
  301. (else (parse-opt-and-fixed key-before key-after aok-split? rest cont))))
  302. (else (parse-opt-and-fixed arglist '() #f rest cont)))))))))
  303. (define (parse-rest arglist cont)
  304. (cond
  305. ((null? arglist) (cont '() '() '() #f #f))
  306. ((not (pair? arglist)) (cont '() '() '() #f arglist))
  307. ((not (list? arglist))
  308. (let* ((copy (improper-list-copy arglist))
  309. (lp (last-pair copy))
  310. (ra (cdr lp)))
  311. (set-cdr! lp '())
  312. (if (memq #:rest copy)
  313. (error "Cannot specify both #:rest and dotted rest argument.")
  314. (parse-keys copy ra cont))))
  315. (else (split-list-at
  316. #:rest arglist
  317. (lambda (before after split?)
  318. (if split?
  319. (case (length after)
  320. ((0) (error "#:rest not followed by argument."))
  321. ((1) (parse-keys before (car after) cont))
  322. (else (error "#:rest argument must be declared last.")))
  323. (parse-keys before #f cont)))))))
  324. (parse-rest arglist cont))
  325. ;; define* args . body
  326. ;; define*-public args . body
  327. ;; define and define-public extended for optional and keyword arguments
  328. ;;
  329. ;; define* and define*-public support optional arguments with
  330. ;; a similar syntax to lambda*. They also support arbitrary-depth
  331. ;; currying, just like Guile's define. Some examples:
  332. ;; (define* (x y #:optional a (z 3) #:key w . u) (display (list y z u)))
  333. ;; defines a procedure x with a fixed argument y, an optional agument
  334. ;; a, another optional argument z with default value 3, a keyword argument w,
  335. ;; and a rest argument u.
  336. ;; (define-public* ((foo #:optional bar) #:optional baz) '())
  337. ;; This illustrates currying. A procedure foo is defined, which,
  338. ;; when called with an optional argument bar, returns a procedure that
  339. ;; takes an optional argument baz.
  340. ;;
  341. ;; Of course, define*[-public] also supports #:rest and #:allow-other-keys
  342. ;; in the same way as lambda*.
  343. (defmacro define* (ARGLIST . BODY)
  344. (define*-guts 'define ARGLIST BODY))
  345. (defmacro define*-public (ARGLIST . BODY)
  346. (define*-guts 'define-public ARGLIST BODY))
  347. ;; The guts of define* and define*-public.
  348. (define (define*-guts DT ARGLIST BODY)
  349. (define (nest-lambda*s arglists)
  350. (if (null? arglists)
  351. BODY
  352. `((lambda* ,(car arglists) ,@(nest-lambda*s (cdr arglists))))))
  353. (define (define*-guts-helper ARGLIST arglists)
  354. (let ((first (car ARGLIST))
  355. (al (cons (cdr ARGLIST) arglists)))
  356. (if (symbol? first)
  357. `(,DT ,first ,@(nest-lambda*s al))
  358. (define*-guts-helper first al))))
  359. (if (symbol? ARGLIST)
  360. `(,DT ,ARGLIST ,@BODY)
  361. (define*-guts-helper ARGLIST '())))
  362. ;; defmacro* name args . body
  363. ;; defmacro*-public args . body
  364. ;; defmacro and defmacro-public extended for optional and keyword arguments
  365. ;;
  366. ;; These are just like defmacro and defmacro-public except that they
  367. ;; take lambda*-style extended paramter lists, where #:optional,
  368. ;; #:key, #:allow-other-keys and #:rest are allowed with the usual
  369. ;; semantics. Here is an example of a macro with an optional argument:
  370. ;; (defmacro* transmorgify (a #:optional b)
  371. (defmacro defmacro* (NAME ARGLIST . BODY)
  372. (defmacro*-guts 'define NAME ARGLIST BODY))
  373. (defmacro defmacro*-public (NAME ARGLIST . BODY)
  374. (defmacro*-guts 'define-public NAME ARGLIST BODY))
  375. ;; The guts of defmacro* and defmacro*-public
  376. (define (defmacro*-guts DT NAME ARGLIST BODY)
  377. `(,DT ,NAME
  378. (,(lambda (transformer) (defmacro:transformer transformer))
  379. (lambda* ,ARGLIST ,@BODY))))
  380. ;;; optargs.scm ends here