match.scm 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  1. ;;; match.scm -- portable hygienic pattern matcher
  2. ;;;
  3. ;;; Copyright (C) 2010, 2011, 2012, 2020, 2024 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 3 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. ;; The above license applies to the file as a whole as some code is
  19. ;; borrowed from upstream Guile, but the majority of the code falls
  20. ;; under the public domain. See below.
  21. ;; This code is written by Alex Shinn and placed in the
  22. ;; Public Domain. All warranties are disclaimed.
  23. ;;> @example-import[(srfi 9)]
  24. ;;> This is a full superset of the popular @hyperlink[
  25. ;;> "http://www.cs.indiana.edu/scheme-repository/code.match.html"]{match}
  26. ;;> package by Andrew Wright, written in fully portable @scheme{syntax-rules}
  27. ;;> and thus preserving hygiene.
  28. ;;> The most notable extensions are the ability to use @emph{non-linear}
  29. ;;> patterns - patterns in which the same identifier occurs multiple
  30. ;;> times, tail patterns after ellipsis, and the experimental tree patterns.
  31. ;;> @subsubsection{Patterns}
  32. ;;> Patterns are written to look like the printed representation of
  33. ;;> the objects they match. The basic usage is
  34. ;;> @scheme{(match expr (pat body ...) ...)}
  35. ;;> where the result of @var{expr} is matched against each pattern in
  36. ;;> turn, and the corresponding body is evaluated for the first to
  37. ;;> succeed. Thus, a list of three elements matches a list of three
  38. ;;> elements.
  39. ;;> @example{(let ((ls (list 1 2 3))) (match ls ((1 2 3) #t)))}
  40. ;;> If no patterns match an error is signaled.
  41. ;;> Identifiers will match anything, and make the corresponding
  42. ;;> binding available in the body.
  43. ;;> @example{(match (list 1 2 3) ((a b c) b))}
  44. ;;> If the same identifier occurs multiple times, the first instance
  45. ;;> will match anything, but subsequent instances must match a value
  46. ;;> which is @scheme{equal?} to the first.
  47. ;;> @example{(match (list 1 2 1) ((a a b) 1) ((a b a) 2))}
  48. ;;> The special identifier @scheme{_} matches anything, no matter how
  49. ;;> many times it is used, and does not bind the result in the body.
  50. ;;> @example{(match (list 1 2 1) ((_ _ b) 1) ((a b a) 2))}
  51. ;;> To match a literal identifier (or list or any other literal), use
  52. ;;> @scheme{quote}.
  53. ;;> @example{(match 'a ('b 1) ('a 2))}
  54. ;;> Analogous to its normal usage in scheme, @scheme{quasiquote} can
  55. ;;> be used to quote a mostly literally matching object with selected
  56. ;;> parts unquoted.
  57. ;;> @example|{(match (list 1 2 3) (`(1 ,b ,c) (list b c)))}|
  58. ;;> Often you want to match any number of a repeated pattern. Inside
  59. ;;> a list pattern you can append @scheme{...} after an element to
  60. ;;> match zero or more of that pattern (like a regexp Kleene star).
  61. ;;> @example{(match (list 1 2) ((1 2 3 ...) #t))}
  62. ;;> @example{(match (list 1 2 3) ((1 2 3 ...) #t))}
  63. ;;> @example{(match (list 1 2 3 3 3) ((1 2 3 ...) #t))}
  64. ;;> Pattern variables matched inside the repeated pattern are bound to
  65. ;;> a list of each matching instance in the body.
  66. ;;> @example{(match (list 1 2) ((a b c ...) c))}
  67. ;;> @example{(match (list 1 2 3) ((a b c ...) c))}
  68. ;;> @example{(match (list 1 2 3 4 5) ((a b c ...) c))}
  69. ;;> More than one @scheme{...} may not be used in the same list, since
  70. ;;> this would require exponential backtracking in the general case.
  71. ;;> However, @scheme{...} need not be the final element in the list,
  72. ;;> and may be succeeded by a fixed number of patterns.
  73. ;;> @example{(match (list 1 2 3 4) ((a b c ... d e) c))}
  74. ;;> @example{(match (list 1 2 3 4 5) ((a b c ... d e) c))}
  75. ;;> @example{(match (list 1 2 3 4 5 6 7) ((a b c ... d e) c))}
  76. ;;> @scheme{___} is provided as an alias for @scheme{...} when it is
  77. ;;> inconvenient to use the ellipsis (as in a syntax-rules template).
  78. ;;> The @scheme{..1} syntax is exactly like the @scheme{...} except
  79. ;;> that it matches one or more repetitions (like a regexp "+").
  80. ;;> @example{(match (list 1 2) ((a b c ..1) c))}
  81. ;;> @example{(match (list 1 2 3) ((a b c ..1) c))}
  82. ;;> The boolean operators @scheme{and}, @scheme{or} and @scheme{not}
  83. ;;> can be used to group and negate patterns analogously to their
  84. ;;> Scheme counterparts.
  85. ;;> The @scheme{and} operator ensures that all subpatterns match.
  86. ;;> This operator is often used with the idiom @scheme{(and x pat)} to
  87. ;;> bind @var{x} to the entire value that matches @var{pat}
  88. ;;> (c.f. "as-patterns" in ML or Haskell). Another common use is in
  89. ;;> conjunction with @scheme{not} patterns to match a general case
  90. ;;> with certain exceptions.
  91. ;;> @example{(match 1 ((and) #t))}
  92. ;;> @example{(match 1 ((and x) x))}
  93. ;;> @example{(match 1 ((and x 1) x))}
  94. ;;> The @scheme{or} operator ensures that at least one subpattern
  95. ;;> matches. If the same identifier occurs in different subpatterns,
  96. ;;> it is matched independently. All identifiers from all subpatterns
  97. ;;> are bound if the @scheme{or} operator matches, but the binding is
  98. ;;> only defined for identifiers from the subpattern which matched.
  99. ;;> @example{(match 1 ((or) #t) (else #f))}
  100. ;;> @example{(match 1 ((or x) x))}
  101. ;;> @example{(match 1 ((or x 2) x))}
  102. ;;> The @scheme{not} operator succeeds if the given pattern doesn't
  103. ;;> match. None of the identifiers used are available in the body.
  104. ;;> @example{(match 1 ((not 2) #t))}
  105. ;;> The more general operator @scheme{?} can be used to provide a
  106. ;;> predicate. The usage is @scheme{(? predicate pat ...)} where
  107. ;;> @var{predicate} is a Scheme expression evaluating to a predicate
  108. ;;> called on the value to match, and any optional patterns after the
  109. ;;> predicate are then matched as in an @scheme{and} pattern.
  110. ;;> @example{(match 1 ((? odd? x) x))}
  111. ;;> The field operator @scheme{=} is used to extract an arbitrary
  112. ;;> field and match against it. It is useful for more complex or
  113. ;;> conditional destructuring that can't be more directly expressed in
  114. ;;> the pattern syntax. The usage is @scheme{(= field pat)}, where
  115. ;;> @var{field} can be any expression, and should result in a
  116. ;;> procedure of one argument, which is applied to the value to match
  117. ;;> to generate a new value to match against @var{pat}.
  118. ;;> Thus the pattern @scheme{(and (= car x) (= cdr y))} is equivalent
  119. ;;> to @scheme{(x . y)}, except it will result in an immediate error
  120. ;;> if the value isn't a pair.
  121. ;;> @example{(match '(1 . 2) ((= car x) x))}
  122. ;;> @example{(match 4 ((= sqrt x) x))}
  123. ;;> The record operator @scheme{$} is used as a concise way to match
  124. ;;> records defined by SRFI-9 (or SRFI-99). The usage is
  125. ;;> @scheme{($ rtd field ...)}, where @var{rtd} should be the record
  126. ;;> type descriptor specified as the first argument to
  127. ;;> @scheme{define-record-type}, and each @var{field} is a subpattern
  128. ;;> matched against the fields of the record in order. Not all fields
  129. ;;> must be present.
  130. ;;> @example{
  131. ;;> (let ()
  132. ;;> (define-record-type employee
  133. ;;> (make-employee name title)
  134. ;;> employee?
  135. ;;> (name get-name)
  136. ;;> (title get-title))
  137. ;;> (match (make-employee "Bob" "Doctor")
  138. ;;> (($ employee n t) (list t n))))
  139. ;;> }
  140. ;;> The @scheme{set!} and @scheme{get!} operators are used to bind an
  141. ;;> identifier to the setter and getter of a field, respectively. The
  142. ;;> setter is a procedure of one argument, which mutates the field to
  143. ;;> that argument. The getter is a procedure of no arguments which
  144. ;;> returns the current value of the field.
  145. ;;> @example{(let ((x (cons 1 2))) (match x ((1 . (set! s)) (s 3) x)))}
  146. ;;> @example{(match '(1 . 2) ((1 . (get! g)) (g)))}
  147. ;;> The new operator @scheme{***} can be used to search a tree for
  148. ;;> subpatterns. A pattern of the form @scheme{(x *** y)} represents
  149. ;;> the subpattern @var{y} located somewhere in a tree where the path
  150. ;;> from the current object to @var{y} can be seen as a list of the
  151. ;;> form @scheme{(x ...)}. @var{y} can immediately match the current
  152. ;;> object in which case the path is the empty list. In a sense it's
  153. ;;> a 2-dimensional version of the @scheme{...} pattern.
  154. ;;> As a common case the pattern @scheme{(_ *** y)} can be used to
  155. ;;> search for @var{y} anywhere in a tree, regardless of the path
  156. ;;> used.
  157. ;;> @example{(match '(a (a (a b))) ((x *** 'b) x))}
  158. ;;> @example{(match '(a (b) (c (d e) (f g))) ((x *** 'g) x))}
  159. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  160. ;; Notes
  161. ;; The implementation is a simple generative pattern matcher - each
  162. ;; pattern is expanded into the required tests, calling a failure
  163. ;; continuation if the tests fail. This makes the logic easy to
  164. ;; follow and extend, but produces sub-optimal code in cases where you
  165. ;; have many similar clauses due to repeating the same tests.
  166. ;; Nonetheless a smart compiler should be able to remove the redundant
  167. ;; tests. For MATCH-LET and DESTRUCTURING-BIND type uses there is no
  168. ;; performance hit.
  169. ;; The original version was written on 2006/11/29 and described in the
  170. ;; following Usenet post:
  171. ;; http://groups.google.com/group/comp.lang.scheme/msg/0941234de7112ffd
  172. ;; and is still available at
  173. ;; http://synthcode.com/scheme/match-simple.scm
  174. ;; It's just 80 lines for the core MATCH, and an extra 40 lines for
  175. ;; MATCH-LET, MATCH-LAMBDA and other syntactic sugar.
  176. ;;
  177. ;; A variant of this file which uses COND-EXPAND in a few places for
  178. ;; performance can be found at
  179. ;; http://synthcode.com/scheme/match-cond-expand.scm
  180. ;;
  181. ;; 2021/06/21 - fix for `(a ...)' patterns where `a' is already bound
  182. ;; (thanks to Andy Wingo)
  183. ;; 2020/09/04 - [OMITTED IN GUILE] perf fix for `not`; rename `..=', `..=', `..1' per SRFI 204
  184. ;; 2020/08/21 - [OMITTED IN GUILE] fixing match-letrec with unhygienic insertion
  185. ;; 2020/07/06 - [OMITTED IN GUILE] adding `..=' and `..=' patterns; fixing ,@ patterns
  186. ;; 2016/10/05 - [OMITTED IN GUILE] treat keywords as literals, not identifiers, in Chicken
  187. ;; 2016/03/06 - fixing named match-let (thanks to Stefan Israelsson Tampe)
  188. ;; 2015/05/09 - fixing bug in var extraction of quasiquote patterns
  189. ;; 2014/11/24 - [OMITTED IN GUILE] adding Gauche's `@' pattern for named record field matching
  190. ;; 2012/12/26 - wrapping match-let&co body in lexical closure
  191. ;; 2012/11/28 - fixing typo s/vetor/vector in largely unused set! code
  192. ;; 2012/05/23 - fixing combinatorial explosion of code in certain or patterns
  193. ;; 2011/09/25 - fixing bug when directly matching an identifier repeated in
  194. ;; the pattern (thanks to Stefan Israelsson Tampe)
  195. ;; 2011/01/27 - fixing bug when matching tail patterns against improper lists
  196. ;; 2010/09/26 - adding `..1' patterns (thanks to Ludovic Courtès)
  197. ;; 2010/09/07 - fixing identifier extraction in some `...' and `***' patterns
  198. ;; 2009/11/25 - adding `***' tree search patterns
  199. ;; 2008/03/20 - fixing bug where (a ...) matched non-lists
  200. ;; 2008/03/15 - removing redundant check in vector patterns
  201. ;; 2008/03/06 - you can use `...' portably now (thanks to Taylor Campbell)
  202. ;; 2007/09/04 - fixing quasiquote patterns
  203. ;; 2007/07/21 - allowing ellipsis patterns in non-final list positions
  204. ;; 2007/04/10 - fixing potential hygiene issue in match-check-ellipsis
  205. ;; (thanks to Taylor Campbell)
  206. ;; 2007/04/08 - clean up, commenting
  207. ;; 2006/12/24 - bugfixes
  208. ;; 2006/12/01 - non-linear patterns, shared variables in OR, get!/set!
  209. (define-module (ice-9 match)
  210. #:pure
  211. #:use-module (hoot eq)
  212. #:use-module (hoot equal)
  213. #:use-module (hoot errors)
  214. #:use-module (hoot lists)
  215. #:use-module (hoot not)
  216. #:use-module (hoot numbers)
  217. #:use-module ((hoot primitives)
  218. #:select (%struct? %struct-vtable %struct-ref))
  219. #:use-module (hoot pairs)
  220. #:use-module (hoot syntax)
  221. #:use-module (hoot vectors)
  222. #:export (match
  223. match-lambda
  224. match-lambda*
  225. match-let
  226. match-let*
  227. match-letrec))
  228. ;; Support for record matching.
  229. (define-syntax-rule (struct? obj) (%struct? obj))
  230. (define-syntax-rule (struct-vtable struct) (%struct-vtable struct))
  231. (define-syntax-rule (struct-ref struct i) (%struct-ref struct i))
  232. ;; slot-ref, slot-set!, is-a? borrowed from upstream Guile.
  233. (define-syntax slot-ref
  234. (syntax-rules ()
  235. ((_ rtd rec n)
  236. (struct-ref rec n))))
  237. (define-syntax slot-set!
  238. (syntax-rules ()
  239. ((_ rtd rec n value)
  240. (struct-set! rec n value))))
  241. (define-syntax is-a?
  242. (syntax-rules ()
  243. ((_ rec rtd)
  244. (and (struct? rec)
  245. (eq? (struct-vtable rec) rtd)))))
  246. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  247. ;; force compile-time syntax errors with useful messages
  248. (define-syntax match-syntax-error
  249. (syntax-rules ()
  250. ((_) (match-syntax-error "invalid match-syntax-error usage"))))
  251. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  252. ;;> @subsubsection{Syntax}
  253. ;;> @subsubsubsection{@rawcode{(match expr (pattern . body) ...)@br{}
  254. ;;> (match expr (pattern (=> failure) . body) ...)}}
  255. ;;> The result of @var{expr} is matched against each @var{pattern} in
  256. ;;> turn, according to the pattern rules described in the previous
  257. ;;> section, until the the first @var{pattern} matches. When a match is
  258. ;;> found, the corresponding @var{body}s are evaluated in order,
  259. ;;> and the result of the last expression is returned as the result
  260. ;;> of the entire @scheme{match}. If a @var{failure} is provided,
  261. ;;> then it is bound to a procedure of no arguments which continues,
  262. ;;> processing at the next @var{pattern}. If no @var{pattern} matches,
  263. ;;> an error is signaled.
  264. ;; The basic interface. MATCH just performs some basic syntax
  265. ;; validation, binds the match expression to a temporary variable `v',
  266. ;; and passes it on to MATCH-NEXT. It's a constant throughout the
  267. ;; code below that the binding `v' is a direct variable reference, not
  268. ;; an expression.
  269. (define-syntax match
  270. (syntax-rules ()
  271. ((match)
  272. (match-syntax-error "missing match expression"))
  273. ((match atom)
  274. (match-syntax-error "no match clauses"))
  275. ((match (app ...) (pat . body) ...)
  276. (let ((v (app ...)))
  277. (match-next v ((app ...) (set! (app ...))) (pat . body) ...)))
  278. ((match #(vec ...) (pat . body) ...)
  279. (let ((v #(vec ...)))
  280. (match-next v (v (set! v)) (pat . body) ...)))
  281. ((match atom (pat . body) ...)
  282. (let ((v atom))
  283. (match-next v (atom (set! atom)) (pat . body) ...)))
  284. ))
  285. ;; MATCH-NEXT passes each clause to MATCH-ONE in turn with its failure
  286. ;; thunk, which is expanded by recursing MATCH-NEXT on the remaining
  287. ;; clauses. `g+s' is a list of two elements, the get! and set!
  288. ;; expressions respectively.
  289. (define-syntax match-next
  290. (syntax-rules (=>)
  291. ;; no more clauses, the match failed
  292. ((match-next v g+s)
  293. ;; Here we call error in non-tail context, so that the backtrace
  294. ;; can show the source location of the failing match form.
  295. (begin
  296. (raise (make-match-error v))
  297. #f))
  298. ;; named failure continuation
  299. ((match-next v g+s (pat (=> failure) . body) . rest)
  300. (let ((failure (lambda () (match-next v g+s . rest))))
  301. ;; match-one analyzes the pattern for us
  302. (match-one v pat g+s (match-drop-ids (begin . body)) (failure) ())))
  303. ;; anonymous failure continuation, give it a dummy name
  304. ((match-next v g+s (pat . body) . rest)
  305. (match-next v g+s (pat (=> failure) . body) . rest))))
  306. ;; MATCH-ONE first checks for ellipsis patterns, otherwise passes on to
  307. ;; MATCH-TWO.
  308. (define-syntax match-one
  309. (syntax-rules ()
  310. ;; If it's a list of two or more values, check to see if the
  311. ;; second one is an ellipsis and handle accordingly, otherwise go
  312. ;; to MATCH-TWO.
  313. ((match-one v (p q . r) g+s sk fk i)
  314. (match-check-ellipsis
  315. q
  316. (match-extract-vars p (match-gen-ellipsis v p r g+s sk fk i) i ())
  317. (match-two v (p q . r) g+s sk fk i)))
  318. ;; Go directly to MATCH-TWO.
  319. ((match-one . x)
  320. (match-two . x))))
  321. ;; This is the guts of the pattern matcher. We are passed a lot of
  322. ;; information in the form:
  323. ;;
  324. ;; (match-two var pattern getter setter success-k fail-k (ids ...))
  325. ;;
  326. ;; usually abbreviated
  327. ;;
  328. ;; (match-two v p g+s sk fk i)
  329. ;;
  330. ;; where VAR is the symbol name of the current variable we are
  331. ;; matching, PATTERN is the current pattern, getter and setter are the
  332. ;; corresponding accessors (e.g. CAR and SET-CAR! of the pair holding
  333. ;; VAR), SUCCESS-K is the success continuation, FAIL-K is the failure
  334. ;; continuation (which is just a thunk call and is thus safe to expand
  335. ;; multiple times) and IDS are the list of identifiers bound in the
  336. ;; pattern so far.
  337. (define-syntax match-two
  338. (syntax-rules (_ ___ ..1 *** quote quasiquote ? $ = and or not set! get!)
  339. ((match-two v () g+s (sk ...) fk i)
  340. (if (null? v) (sk ... i) fk))
  341. ((match-two v (quote p) g+s (sk ...) fk i)
  342. (if (equal? v 'p) (sk ... i) fk))
  343. ((match-two v (quasiquote p) . x)
  344. (match-quasiquote v p . x))
  345. ((match-two v (and) g+s (sk ...) fk i) (sk ... i))
  346. ((match-two v (and p q ...) g+s sk fk i)
  347. (match-one v p g+s (match-one v (and q ...) g+s sk fk) fk i))
  348. ((match-two v (or) g+s sk fk i) fk)
  349. ((match-two v (or p) . x)
  350. (match-one v p . x))
  351. ((match-two v (or p ...) g+s sk fk i)
  352. (match-extract-vars (or p ...) (match-gen-or v (p ...) g+s sk fk i) i ()))
  353. ((match-two v (not p) g+s (sk ...) fk i)
  354. (match-one v p g+s (match-drop-ids fk) (sk ... i) i))
  355. ((match-two v (get! getter) (g s) (sk ...) fk i)
  356. (let ((getter (lambda () g))) (sk ... i)))
  357. ((match-two v (set! setter) (g (s ...)) (sk ...) fk i)
  358. (let ((setter (lambda (x) (s ... x)))) (sk ... i)))
  359. ((match-two v (? pred . p) g+s sk fk i)
  360. (if (pred v) (match-one v (and . p) g+s sk fk i) fk))
  361. ((match-two v (= proc p) . x)
  362. (let ((w (proc v))) (match-one w p . x)))
  363. ((match-two v (p ___ . r) g+s sk fk i)
  364. (match-extract-vars p (match-gen-ellipsis v p r g+s sk fk i) i ()))
  365. ((match-two v (p) g+s sk fk i)
  366. (if (and (pair? v) (null? (cdr v)))
  367. (let ((w (car v)))
  368. (match-one w p ((car v) (set-car! v)) sk fk i))
  369. fk))
  370. ((match-two v (p *** q) g+s sk fk i)
  371. (match-extract-vars p (match-gen-search v p q g+s sk fk i) i ()))
  372. ((match-two v (p *** . q) g+s sk fk i)
  373. (match-syntax-error "invalid use of ***" (p *** . q)))
  374. ((match-two v (p ..1) g+s sk fk i)
  375. (if (pair? v)
  376. (match-one v (p ___) g+s sk fk i)
  377. fk))
  378. ((match-two v ($ rec p ...) g+s sk fk i)
  379. (if (is-a? v rec)
  380. (match-record-refs v rec 0 (p ...) g+s sk fk i)
  381. fk))
  382. ((match-two v (p . q) g+s sk fk i)
  383. (if (pair? v)
  384. (let ((w (car v)) (x (cdr v)))
  385. (match-one w p ((car v) (set-car! v))
  386. (match-one x q ((cdr v) (set-cdr! v)) sk fk)
  387. fk
  388. i))
  389. fk))
  390. ((match-two v #(p ...) g+s . x)
  391. (match-vector v 0 () (p ...) . x))
  392. ((match-two v _ g+s (sk ...) fk i) (sk ... i))
  393. ;; Not a pair or vector or special literal, test to see if it's a
  394. ;; new symbol, in which case we just bind it, or if it's an
  395. ;; already bound symbol or some other literal, in which case we
  396. ;; compare it with EQUAL?.
  397. ((match-two v x g+s (sk ...) fk (id ...))
  398. (let-syntax
  399. ((new-sym?
  400. (syntax-rules (id ...)
  401. ((new-sym? x sk2 fk2) sk2)
  402. ((new-sym? y sk2 fk2) fk2))))
  403. (new-sym? random-sym-to-match
  404. (let ((x v)) (sk ... (id ... x)))
  405. (if (equal? v x) (sk ... (id ...)) fk))))
  406. ))
  407. ;; QUASIQUOTE patterns
  408. (define-syntax match-quasiquote
  409. (syntax-rules (unquote unquote-splicing quasiquote)
  410. ((_ v (unquote p) g+s sk fk i)
  411. (match-one v p g+s sk fk i))
  412. ((_ v ((unquote-splicing p) . rest) g+s sk fk i)
  413. (if (pair? v)
  414. (match-one v
  415. (p . tmp)
  416. (match-quasiquote tmp rest g+s sk fk)
  417. fk
  418. i)
  419. fk))
  420. ((_ v (quasiquote p) g+s sk fk i . depth)
  421. (match-quasiquote v p g+s sk fk i #f . depth))
  422. ((_ v (unquote p) g+s sk fk i x . depth)
  423. (match-quasiquote v p g+s sk fk i . depth))
  424. ((_ v (unquote-splicing p) g+s sk fk i x . depth)
  425. (match-quasiquote v p g+s sk fk i . depth))
  426. ((_ v (p . q) g+s sk fk i . depth)
  427. (if (pair? v)
  428. (let ((w (car v)) (x (cdr v)))
  429. (match-quasiquote
  430. w p g+s
  431. (match-quasiquote-step x q g+s sk fk depth)
  432. fk i . depth))
  433. fk))
  434. ((_ v #(elt ...) g+s sk fk i . depth)
  435. (if (vector? v)
  436. (let ((ls (vector->list v)))
  437. (match-quasiquote ls (elt ...) g+s sk fk i . depth))
  438. fk))
  439. ((_ v x g+s sk fk i . depth)
  440. (match-one v 'x g+s sk fk i))))
  441. (define-syntax match-quasiquote-step
  442. (syntax-rules ()
  443. ((match-quasiquote-step x q g+s sk fk depth i)
  444. (match-quasiquote x q g+s sk fk i . depth))))
  445. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  446. ;; Utilities
  447. ;; Takes two values and just expands into the first.
  448. (define-syntax match-drop-ids
  449. (syntax-rules ()
  450. ((_ expr ids ...) expr)))
  451. (define-syntax match-tuck-ids
  452. (syntax-rules ()
  453. ((_ (letish args (expr ...)) ids ...)
  454. (letish args (expr ... ids ...)))))
  455. (define-syntax match-drop-first-arg
  456. (syntax-rules ()
  457. ((_ arg expr) expr)))
  458. ;; To expand an OR group we try each clause in succession, passing the
  459. ;; first that succeeds to the success continuation. On failure for
  460. ;; any clause, we just try the next clause, finally resorting to the
  461. ;; failure continuation fk if all clauses fail. The only trick is
  462. ;; that we want to unify the identifiers, so that the success
  463. ;; continuation can refer to a variable from any of the OR clauses.
  464. (define-syntax match-gen-or
  465. (syntax-rules ()
  466. ((_ v p g+s (sk ...) fk (i ...) ((id id-ls) ...))
  467. (let ((sk2 (lambda (id ...) (sk ... (i ... id ...)))))
  468. (match-gen-or-step v p g+s (match-drop-ids (sk2 id ...)) fk (i ...))))))
  469. (define-syntax match-gen-or-step
  470. (syntax-rules ()
  471. ((_ v () g+s sk fk . x)
  472. ;; no OR clauses, call the failure continuation
  473. fk)
  474. ((_ v (p) . x)
  475. ;; last (or only) OR clause, just expand normally
  476. (match-one v p . x))
  477. ((_ v (p . q) g+s sk fk i)
  478. ;; match one and try the remaining on failure
  479. (let ((fk2 (lambda () (match-gen-or-step v q g+s sk fk i))))
  480. (match-one v p g+s sk (fk2) i)))
  481. ))
  482. ;; We match a pattern (p ...) by matching the pattern p in a loop on
  483. ;; each element of the variable, accumulating the bound ids into lists.
  484. ;; Look at the body of the simple case - it's just a named let loop,
  485. ;; matching each element in turn to the same pattern. The only trick
  486. ;; is that we want to keep track of the lists of each extracted id, so
  487. ;; when the loop recurses we cons the ids onto their respective list
  488. ;; variables, and on success we bind the ids (what the user input and
  489. ;; expects to see in the success body) to the reversed accumulated
  490. ;; list IDs.
  491. (define-syntax match-gen-ellipsis
  492. (syntax-rules ()
  493. ((_ v p () g+s (sk ...) fk i ((id id-ls) ...))
  494. (match-check-identifier p
  495. ;; simplest case equivalent to (p ...), just bind the list
  496. (let ((w v))
  497. (if (list? w)
  498. (match-one w p g+s (sk ...) fk i)
  499. fk))
  500. ;; simple case, match all elements of the list
  501. (let loop ((ls v) (id-ls '()) ...)
  502. (cond
  503. ((null? ls)
  504. (let ((id (reverse id-ls)) ...) (sk ... i)))
  505. ((pair? ls)
  506. (let ((w (car ls)))
  507. (match-one w p ((car ls) (set-car! ls))
  508. (match-drop-ids (loop (cdr ls) (cons id id-ls) ...))
  509. fk i)))
  510. (else
  511. fk)))))
  512. ((_ v p r g+s sk fk (i ...) ((id id-ls) ...))
  513. (match-verify-no-ellipsis
  514. r
  515. (match-bound-identifier-memv
  516. p
  517. (i ...)
  518. ;; p is bound, match the list up to the known length, then
  519. ;; match the trailing patterns
  520. (let loop ((ls v) (expect p))
  521. (cond
  522. ((null? expect)
  523. (match-one ls r (#f #f) sk fk (i ...)))
  524. ((pair? ls)
  525. (let ((w (car ls))
  526. (e (car expect)))
  527. (if (equal? (car ls) (car expect))
  528. (match-drop-ids (loop (cdr ls) (cdr expect)))
  529. fk)))
  530. (else
  531. fk)))
  532. ;; general case, trailing patterns to match, keep track of the
  533. ;; remaining list length so we don't need any backtracking
  534. (let* ((tail-len (length 'r))
  535. (ls v)
  536. (len (and (list? ls) (length ls))))
  537. (if (or (not len) (< len tail-len))
  538. fk
  539. (let loop ((ls ls) (n len) (id-ls '()) ...)
  540. (cond
  541. ((= n tail-len)
  542. (let ((id (reverse id-ls)) ...)
  543. (match-one ls r (#f #f) sk fk (i ... id ...))))
  544. ((pair? ls)
  545. (let ((w (car ls)))
  546. (match-one w p ((car ls) (set-car! ls))
  547. (match-drop-ids
  548. (loop (cdr ls) (- n 1) (cons id id-ls) ...))
  549. fk
  550. (i ...))))
  551. (else
  552. fk))))))))))
  553. ;; This is just a safety check. Although unlike syntax-rules we allow
  554. ;; trailing patterns after an ellipsis, we explicitly disable multiple
  555. ;; ellipses at the same level. This is because in the general case
  556. ;; such patterns are exponential in the number of ellipses, and we
  557. ;; don't want to make it easy to construct very expensive operations
  558. ;; with simple looking patterns. For example, it would be O(n^2) for
  559. ;; patterns like (a ... b ...) because we must consider every trailing
  560. ;; element for every possible break for the leading "a ...".
  561. (define-syntax match-verify-no-ellipsis
  562. (syntax-rules ()
  563. ((_ (x . y) sk)
  564. (match-check-ellipsis
  565. x
  566. (match-syntax-error
  567. "multiple ellipsis patterns not allowed at same level")
  568. (match-verify-no-ellipsis y sk)))
  569. ((_ () sk)
  570. sk)
  571. ((_ x sk)
  572. (match-syntax-error "dotted tail not allowed after ellipsis" x))))
  573. ;; To implement the tree search, we use two recursive procedures. TRY
  574. ;; attempts to match Y once, and on success it calls the normal SK on
  575. ;; the accumulated list ids as in MATCH-GEN-ELLIPSIS. On failure, we
  576. ;; call NEXT which first checks if the current value is a list
  577. ;; beginning with X, then calls TRY on each remaining element of the
  578. ;; list. Since TRY will recursively call NEXT again on failure, this
  579. ;; effects a full depth-first search.
  580. ;;
  581. ;; The failure continuation throughout is a jump to the next step in
  582. ;; the tree search, initialized with the original failure continuation
  583. ;; FK.
  584. (define-syntax match-gen-search
  585. (syntax-rules ()
  586. ((match-gen-search v p q g+s sk fk i ((id id-ls) ...))
  587. (letrec ((try (lambda (w fail id-ls ...)
  588. (match-one w q g+s
  589. (match-tuck-ids
  590. (let ((id (reverse id-ls)) ...)
  591. sk))
  592. (next w fail id-ls ...) i)))
  593. (next (lambda (w fail id-ls ...)
  594. (if (not (pair? w))
  595. (fail)
  596. (let ((u (car w)))
  597. (match-one
  598. u p ((car w) (set-car! w))
  599. (match-drop-ids
  600. ;; accumulate the head variables from
  601. ;; the p pattern, and loop over the tail
  602. (let ((id-ls (cons id id-ls)) ...)
  603. (let lp ((ls (cdr w)))
  604. (if (pair? ls)
  605. (try (car ls)
  606. (lambda () (lp (cdr ls)))
  607. id-ls ...)
  608. (fail)))))
  609. (fail) i))))))
  610. ;; the initial id-ls binding here is a dummy to get the right
  611. ;; number of '()s
  612. (let ((id-ls '()) ...)
  613. (try v (lambda () fk) id-ls ...))))))
  614. ;; Vector patterns are just more of the same, with the slight
  615. ;; exception that we pass around the current vector index being
  616. ;; matched.
  617. (define-syntax match-vector
  618. (syntax-rules (___)
  619. ((_ v n pats (p q) . x)
  620. (match-check-ellipsis q
  621. (match-gen-vector-ellipsis v n pats p . x)
  622. (match-vector-two v n pats (p q) . x)))
  623. ((_ v n pats (p ___) sk fk i)
  624. (match-gen-vector-ellipsis v n pats p sk fk i))
  625. ((_ . x)
  626. (match-vector-two . x))))
  627. ;; Check the exact vector length, then check each element in turn.
  628. (define-syntax match-vector-two
  629. (syntax-rules ()
  630. ((_ v n ((pat index) ...) () sk fk i)
  631. (if (vector? v)
  632. (let ((len (vector-length v)))
  633. (if (= len n)
  634. (match-vector-step v ((pat index) ...) sk fk i)
  635. fk))
  636. fk))
  637. ((_ v n (pats ...) (p . q) . x)
  638. (match-vector v (+ n 1) (pats ... (p n)) q . x))))
  639. (define-syntax match-vector-step
  640. (syntax-rules ()
  641. ((_ v () (sk ...) fk i) (sk ... i))
  642. ((_ v ((pat index) . rest) sk fk i)
  643. (let ((w (vector-ref v index)))
  644. (match-one w pat ((vector-ref v index) (vector-set! v index))
  645. (match-vector-step v rest sk fk)
  646. fk i)))))
  647. ;; With a vector ellipsis pattern we first check to see if the vector
  648. ;; length is at least the required length.
  649. (define-syntax match-gen-vector-ellipsis
  650. (syntax-rules ()
  651. ((_ v n ((pat index) ...) p sk fk i)
  652. (if (vector? v)
  653. (let ((len (vector-length v)))
  654. (if (>= len n)
  655. (match-vector-step v ((pat index) ...)
  656. (match-vector-tail v p n len sk fk)
  657. fk i)
  658. fk))
  659. fk))))
  660. (define-syntax match-vector-tail
  661. (syntax-rules ()
  662. ((_ v p n len sk fk i)
  663. (match-extract-vars p (match-vector-tail-two v p n len sk fk i) i ()))))
  664. (define-syntax match-vector-tail-two
  665. (syntax-rules ()
  666. ((_ v p n len (sk ...) fk i ((id id-ls) ...))
  667. (let loop ((j n) (id-ls '()) ...)
  668. (if (>= j len)
  669. (let ((id (reverse id-ls)) ...) (sk ... i))
  670. (let ((w (vector-ref v j)))
  671. (match-one w p ((vector-ref v j) (vector-set! v j))
  672. (match-drop-ids (loop (+ j 1) (cons id id-ls) ...))
  673. fk i)))))))
  674. (define-syntax match-record-refs
  675. (syntax-rules ()
  676. ((_ v rec n (p . q) g+s sk fk i)
  677. (let ((w (slot-ref rec v n)))
  678. (match-one w p ((slot-ref rec v n) (slot-set! rec v n))
  679. (match-record-refs v rec (+ n 1) q g+s sk fk) fk i)))
  680. ((_ v rec n () g+s (sk ...) fk i)
  681. (sk ... i))))
  682. ;; Extract all identifiers in a pattern. A little more complicated
  683. ;; than just looking for symbols, we need to ignore special keywords
  684. ;; and non-pattern forms (such as the predicate expression in ?
  685. ;; patterns), and also ignore previously bound identifiers.
  686. ;;
  687. ;; Calls the continuation with all new vars as a list of the form
  688. ;; ((orig-var tmp-name) ...), where tmp-name can be used to uniquely
  689. ;; pair with the original variable (e.g. it's used in the ellipsis
  690. ;; generation for list variables).
  691. ;;
  692. ;; (match-extract-vars pattern continuation (ids ...) (new-vars ...))
  693. (define-syntax match-extract-vars
  694. (syntax-rules (_ ___ ..1 *** ? $ = quote quasiquote and or not get! set!)
  695. ((match-extract-vars (? pred . p) . x)
  696. (match-extract-vars p . x))
  697. ((match-extract-vars ($ rec . p) . x)
  698. (match-extract-vars p . x))
  699. ((match-extract-vars (= proc p) . x)
  700. (match-extract-vars p . x))
  701. ((match-extract-vars (quote x) (k ...) i v)
  702. (k ... v))
  703. ((match-extract-vars (quasiquote x) k i v)
  704. (match-extract-quasiquote-vars x k i v (#t)))
  705. ((match-extract-vars (and . p) . x)
  706. (match-extract-vars p . x))
  707. ((match-extract-vars (or . p) . x)
  708. (match-extract-vars p . x))
  709. ((match-extract-vars (not . p) . x)
  710. (match-extract-vars p . x))
  711. ;; A non-keyword pair, expand the CAR with a continuation to
  712. ;; expand the CDR.
  713. ((match-extract-vars (p q . r) k i v)
  714. (match-check-ellipsis
  715. q
  716. (match-extract-vars (p . r) k i v)
  717. (match-extract-vars p (match-extract-vars-step (q . r) k i v) i ())))
  718. ((match-extract-vars (p . q) k i v)
  719. (match-extract-vars p (match-extract-vars-step q k i v) i ()))
  720. ((match-extract-vars #(p ...) . x)
  721. (match-extract-vars (p ...) . x))
  722. ((match-extract-vars _ (k ...) i v) (k ... v))
  723. ((match-extract-vars ___ (k ...) i v) (k ... v))
  724. ((match-extract-vars *** (k ...) i v) (k ... v))
  725. ((match-extract-vars ..1 (k ...) i v) (k ... v))
  726. ;; This is the main part, the only place where we might add a new
  727. ;; var if it's an unbound symbol.
  728. ((match-extract-vars p (k ...) (i ...) v)
  729. (let-syntax
  730. ((new-sym?
  731. (syntax-rules (i ...)
  732. ((new-sym? p sk fk) sk)
  733. ((new-sym? any sk fk) fk))))
  734. (new-sym? random-sym-to-match
  735. (k ... ((p p-ls) . v))
  736. (k ... v))))
  737. ))
  738. ;; Stepper used in the above so it can expand the CAR and CDR
  739. ;; separately.
  740. (define-syntax match-extract-vars-step
  741. (syntax-rules ()
  742. ((_ p k i v ((v2 v2-ls) ...))
  743. (match-extract-vars p k (v2 ... . i) ((v2 v2-ls) ... . v)))
  744. ))
  745. (define-syntax match-extract-quasiquote-vars
  746. (syntax-rules (quasiquote unquote unquote-splicing)
  747. ((match-extract-quasiquote-vars (quasiquote x) k i v d)
  748. (match-extract-quasiquote-vars x k i v (#t . d)))
  749. ((match-extract-quasiquote-vars (unquote-splicing x) k i v d)
  750. (match-extract-quasiquote-vars (unquote x) k i v d))
  751. ((match-extract-quasiquote-vars (unquote x) k i v (#t))
  752. (match-extract-vars x k i v))
  753. ((match-extract-quasiquote-vars (unquote x) k i v (#t . d))
  754. (match-extract-quasiquote-vars x k i v d))
  755. ((match-extract-quasiquote-vars (x . y) k i v d)
  756. (match-extract-quasiquote-vars
  757. x
  758. (match-extract-quasiquote-vars-step y k i v d) i () d))
  759. ((match-extract-quasiquote-vars #(x ...) k i v d)
  760. (match-extract-quasiquote-vars (x ...) k i v d))
  761. ((match-extract-quasiquote-vars x (k ...) i v d)
  762. (k ... v))
  763. ))
  764. (define-syntax match-extract-quasiquote-vars-step
  765. (syntax-rules ()
  766. ((_ x k i v d ((v2 v2-ls) ...))
  767. (match-extract-quasiquote-vars x k (v2 ... . i) ((v2 v2-ls) ... . v) d))
  768. ))
  769. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  770. ;; Gimme some sugar baby.
  771. ;;> Shortcut for @scheme{lambda} + @scheme{match}. Creates a
  772. ;;> procedure of one argument, and matches that argument against each
  773. ;;> clause.
  774. (define-syntax match-lambda
  775. (syntax-rules ()
  776. ((_ (pattern . body) ...) (lambda (expr) (match expr (pattern . body) ...)))))
  777. ;;> Similar to @scheme{match-lambda}. Creates a procedure of any
  778. ;;> number of arguments, and matches the argument list against each
  779. ;;> clause.
  780. (define-syntax match-lambda*
  781. (syntax-rules ()
  782. ((_ (pattern . body) ...) (lambda expr (match expr (pattern . body) ...)))))
  783. ;;> Matches each var to the corresponding expression, and evaluates
  784. ;;> the body with all match variables in scope. Raises an error if
  785. ;;> any of the expressions fail to match. Syntax analogous to named
  786. ;;> let can also be used for recursive functions which match on their
  787. ;;> arguments as in @scheme{match-lambda*}.
  788. (define-syntax match-let
  789. (syntax-rules ()
  790. ((_ ((var value) ...) . body)
  791. (match-let/helper let () () ((var value) ...) . body))
  792. ((_ loop ((var init) ...) . body)
  793. (match-named-let loop () ((var init) ...) . body))))
  794. ;;> Similar to @scheme{match-let}, but analogously to @scheme{letrec}
  795. ;;> matches and binds the variables with all match variables in scope.
  796. (define-syntax match-letrec
  797. (syntax-rules ()
  798. ((_ ((var value) ...) . body)
  799. (match-let/helper letrec () () ((var value) ...) . body))))
  800. (define-syntax match-let/helper
  801. (syntax-rules ()
  802. ((_ let ((var expr) ...) () () . body)
  803. (let ((var expr) ...) . body))
  804. ((_ let ((var expr) ...) ((pat tmp) ...) () . body)
  805. (let ((var expr) ...)
  806. (match-let* ((pat tmp) ...)
  807. . body)))
  808. ((_ let (v ...) (p ...) (((a . b) expr) . rest) . body)
  809. (match-let/helper
  810. let (v ... (tmp expr)) (p ... ((a . b) tmp)) rest . body))
  811. ((_ let (v ...) (p ...) ((#(a ...) expr) . rest) . body)
  812. (match-let/helper
  813. let (v ... (tmp expr)) (p ... (#(a ...) tmp)) rest . body))
  814. ((_ let (v ...) (p ...) ((a expr) . rest) . body)
  815. (match-let/helper let (v ... (a expr)) (p ...) rest . body))))
  816. (define-syntax match-named-let
  817. (syntax-rules ()
  818. ((_ loop ((pat expr var) ...) () . body)
  819. (let loop ((var expr) ...)
  820. (match-let ((pat var) ...)
  821. . body)))
  822. ((_ loop (v ...) ((pat expr) . rest) . body)
  823. (match-named-let loop (v ... (pat expr tmp)) rest . body))))
  824. ;;> @subsubsubsection{@rawcode{(match-let* ((var value) ...) body ...)}}
  825. ;;> Similar to @scheme{match-let}, but analogously to @scheme{let*}
  826. ;;> matches and binds the variables in sequence, with preceding match
  827. ;;> variables in scope.
  828. (define-syntax match-let*
  829. (syntax-rules ()
  830. ((_ () . body)
  831. (let () . body))
  832. ((_ ((pat expr) . rest) . body)
  833. (match expr (pat (match-let* rest . body))))))
  834. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  835. ;; Otherwise COND-EXPANDed bits.
  836. ;; This *should* work, but doesn't :(
  837. ;; (define-syntax match-check-ellipsis
  838. ;; (syntax-rules (...)
  839. ;; ((_ ... sk fk) sk)
  840. ;; ((_ x sk fk) fk)))
  841. ;; This is a little more complicated, and introduces a new let-syntax,
  842. ;; but should work portably in any R[56]RS Scheme. Taylor Campbell
  843. ;; originally came up with the idea.
  844. (define-syntax match-check-ellipsis
  845. (syntax-rules ()
  846. ;; these two aren't necessary but provide fast-case failures
  847. ((match-check-ellipsis (a . b) success-k failure-k) failure-k)
  848. ((match-check-ellipsis #(a ...) success-k failure-k) failure-k)
  849. ;; matching an atom
  850. ((match-check-ellipsis id success-k failure-k)
  851. (let-syntax ((ellipsis? (syntax-rules ()
  852. ;; iff `id' is `...' here then this will
  853. ;; match a list of any length
  854. ((ellipsis? (foo id) sk fk) sk)
  855. ((ellipsis? other sk fk) fk))))
  856. ;; this list of three elements will only match the (foo id) list
  857. ;; above if `id' is `...'
  858. (ellipsis? (a b c) success-k failure-k)))))
  859. ;; This is portable but can be more efficient with non-portable
  860. ;; extensions. This trick was originally discovered by Oleg Kiselyov.
  861. (define-syntax match-check-identifier
  862. (syntax-rules ()
  863. ;; fast-case failures, lists and vectors are not identifiers
  864. ((_ (x . y) success-k failure-k) failure-k)
  865. ((_ #(x ...) success-k failure-k) failure-k)
  866. ;; x is an atom
  867. ((_ x success-k failure-k)
  868. (let-syntax
  869. ((sym?
  870. (syntax-rules ()
  871. ;; if the symbol `abracadabra' matches x, then x is a
  872. ;; symbol
  873. ((sym? x sk fk) sk)
  874. ;; otherwise x is a non-symbol datum
  875. ((sym? y sk fk) fk))))
  876. (sym? abracadabra success-k failure-k)))))
  877. (define-syntax match-bound-identifier-memv
  878. (syntax-rules ()
  879. ((match-bound-identifier-memv a (id ...) sk fk)
  880. (match-check-identifier
  881. a
  882. (let-syntax
  883. ((memv?
  884. (syntax-rules (id ...)
  885. ((memv? a sk2 fk2) fk2)
  886. ((memv? anything-else sk2 fk2) sk2))))
  887. (memv? random-sym-to-match sk fk))
  888. fk))))