match.scm 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  1. ;;; match.scm -- portable hygienic pattern matcher
  2. ;;;
  3. ;;; Copyright (C) 2010, 2011, 2012, 2020 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 numbers)
  216. #:use-module (hoot primitives)
  217. #:use-module (hoot pairs)
  218. #:use-module (hoot vectors)
  219. #:export (match
  220. match-lambda
  221. match-lambda*
  222. match-let
  223. match-let*
  224. match-letrec))
  225. ;; Support for record matching.
  226. (define-syntax-rule (struct? obj) (%struct? obj))
  227. (define-syntax-rule (struct-vtable struct) (%struct-vtable struct))
  228. (define-syntax-rule (struct-ref struct i) (%struct-ref struct i))
  229. ;; slot-ref, slot-set!, is-a? borrowed from upstream Guile.
  230. (define-syntax slot-ref
  231. (syntax-rules ()
  232. ((_ rtd rec n)
  233. (struct-ref rec n))))
  234. (define-syntax slot-set!
  235. (syntax-rules ()
  236. ((_ rtd rec n value)
  237. (struct-set! rec n value))))
  238. (define-syntax is-a?
  239. (syntax-rules ()
  240. ((_ rec rtd)
  241. (and (struct? rec)
  242. (eq? (struct-vtable rec) rtd)))))
  243. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  244. ;; force compile-time syntax errors with useful messages
  245. (define-syntax match-syntax-error
  246. (syntax-rules ()
  247. ((_) (match-syntax-error "invalid match-syntax-error usage"))))
  248. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  249. ;;> @subsubsection{Syntax}
  250. ;;> @subsubsubsection{@rawcode{(match expr (pattern . body) ...)@br{}
  251. ;;> (match expr (pattern (=> failure) . body) ...)}}
  252. ;;> The result of @var{expr} is matched against each @var{pattern} in
  253. ;;> turn, according to the pattern rules described in the previous
  254. ;;> section, until the the first @var{pattern} matches. When a match is
  255. ;;> found, the corresponding @var{body}s are evaluated in order,
  256. ;;> and the result of the last expression is returned as the result
  257. ;;> of the entire @scheme{match}. If a @var{failure} is provided,
  258. ;;> then it is bound to a procedure of no arguments which continues,
  259. ;;> processing at the next @var{pattern}. If no @var{pattern} matches,
  260. ;;> an error is signaled.
  261. ;; The basic interface. MATCH just performs some basic syntax
  262. ;; validation, binds the match expression to a temporary variable `v',
  263. ;; and passes it on to MATCH-NEXT. It's a constant throughout the
  264. ;; code below that the binding `v' is a direct variable reference, not
  265. ;; an expression.
  266. (define-syntax match
  267. (syntax-rules ()
  268. ((match)
  269. (match-syntax-error "missing match expression"))
  270. ((match atom)
  271. (match-syntax-error "no match clauses"))
  272. ((match (app ...) (pat . body) ...)
  273. (let ((v (app ...)))
  274. (match-next v ((app ...) (set! (app ...))) (pat . body) ...)))
  275. ((match #(vec ...) (pat . body) ...)
  276. (let ((v #(vec ...)))
  277. (match-next v (v (set! v)) (pat . body) ...)))
  278. ((match atom (pat . body) ...)
  279. (let ((v atom))
  280. (match-next v (atom (set! atom)) (pat . body) ...)))
  281. ))
  282. ;; MATCH-NEXT passes each clause to MATCH-ONE in turn with its failure
  283. ;; thunk, which is expanded by recursing MATCH-NEXT on the remaining
  284. ;; clauses. `g+s' is a list of two elements, the get! and set!
  285. ;; expressions respectively.
  286. (define-syntax match-next
  287. (syntax-rules (=>)
  288. ;; no more clauses, the match failed
  289. ((match-next v g+s)
  290. ;; Here we call error in non-tail context, so that the backtrace
  291. ;; can show the source location of the failing match form.
  292. (begin
  293. (raise (make-match-error v))
  294. #f))
  295. ;; named failure continuation
  296. ((match-next v g+s (pat (=> failure) . body) . rest)
  297. (let ((failure (lambda () (match-next v g+s . rest))))
  298. ;; match-one analyzes the pattern for us
  299. (match-one v pat g+s (match-drop-ids (begin . body)) (failure) ())))
  300. ;; anonymous failure continuation, give it a dummy name
  301. ((match-next v g+s (pat . body) . rest)
  302. (match-next v g+s (pat (=> failure) . body) . rest))))
  303. ;; MATCH-ONE first checks for ellipsis patterns, otherwise passes on to
  304. ;; MATCH-TWO.
  305. (define-syntax match-one
  306. (syntax-rules ()
  307. ;; If it's a list of two or more values, check to see if the
  308. ;; second one is an ellipsis and handle accordingly, otherwise go
  309. ;; to MATCH-TWO.
  310. ((match-one v (p q . r) g+s sk fk i)
  311. (match-check-ellipsis
  312. q
  313. (match-extract-vars p (match-gen-ellipsis v p r g+s sk fk i) i ())
  314. (match-two v (p q . r) g+s sk fk i)))
  315. ;; Go directly to MATCH-TWO.
  316. ((match-one . x)
  317. (match-two . x))))
  318. ;; This is the guts of the pattern matcher. We are passed a lot of
  319. ;; information in the form:
  320. ;;
  321. ;; (match-two var pattern getter setter success-k fail-k (ids ...))
  322. ;;
  323. ;; usually abbreviated
  324. ;;
  325. ;; (match-two v p g+s sk fk i)
  326. ;;
  327. ;; where VAR is the symbol name of the current variable we are
  328. ;; matching, PATTERN is the current pattern, getter and setter are the
  329. ;; corresponding accessors (e.g. CAR and SET-CAR! of the pair holding
  330. ;; VAR), SUCCESS-K is the success continuation, FAIL-K is the failure
  331. ;; continuation (which is just a thunk call and is thus safe to expand
  332. ;; multiple times) and IDS are the list of identifiers bound in the
  333. ;; pattern so far.
  334. (define-syntax match-two
  335. (syntax-rules (_ ___ ..1 *** quote quasiquote ? $ = and or not set! get!)
  336. ((match-two v () g+s (sk ...) fk i)
  337. (if (null? v) (sk ... i) fk))
  338. ((match-two v (quote p) g+s (sk ...) fk i)
  339. (if (equal? v 'p) (sk ... i) fk))
  340. ((match-two v (quasiquote p) . x)
  341. (match-quasiquote v p . x))
  342. ((match-two v (and) g+s (sk ...) fk i) (sk ... i))
  343. ((match-two v (and p q ...) g+s sk fk i)
  344. (match-one v p g+s (match-one v (and q ...) g+s sk fk) fk i))
  345. ((match-two v (or) g+s sk fk i) fk)
  346. ((match-two v (or p) . x)
  347. (match-one v p . x))
  348. ((match-two v (or p ...) g+s sk fk i)
  349. (match-extract-vars (or p ...) (match-gen-or v (p ...) g+s sk fk i) i ()))
  350. ((match-two v (not p) g+s (sk ...) fk i)
  351. (match-one v p g+s (match-drop-ids fk) (sk ... i) i))
  352. ((match-two v (get! getter) (g s) (sk ...) fk i)
  353. (let ((getter (lambda () g))) (sk ... i)))
  354. ((match-two v (set! setter) (g (s ...)) (sk ...) fk i)
  355. (let ((setter (lambda (x) (s ... x)))) (sk ... i)))
  356. ((match-two v (? pred . p) g+s sk fk i)
  357. (if (pred v) (match-one v (and . p) g+s sk fk i) fk))
  358. ((match-two v (= proc p) . x)
  359. (let ((w (proc v))) (match-one w p . x)))
  360. ((match-two v (p ___ . r) g+s sk fk i)
  361. (match-extract-vars p (match-gen-ellipsis v p r g+s sk fk i) i ()))
  362. ((match-two v (p) g+s sk fk i)
  363. (if (and (pair? v) (null? (cdr v)))
  364. (let ((w (car v)))
  365. (match-one w p ((car v) (set-car! v)) sk fk i))
  366. fk))
  367. ((match-two v (p *** q) g+s sk fk i)
  368. (match-extract-vars p (match-gen-search v p q g+s sk fk i) i ()))
  369. ((match-two v (p *** . q) g+s sk fk i)
  370. (match-syntax-error "invalid use of ***" (p *** . q)))
  371. ((match-two v (p ..1) g+s sk fk i)
  372. (if (pair? v)
  373. (match-one v (p ___) g+s sk fk i)
  374. fk))
  375. ((match-two v ($ rec p ...) g+s sk fk i)
  376. (if (is-a? v rec)
  377. (match-record-refs v rec 0 (p ...) g+s sk fk i)
  378. fk))
  379. ((match-two v (p . q) g+s sk fk i)
  380. (if (pair? v)
  381. (let ((w (car v)) (x (cdr v)))
  382. (match-one w p ((car v) (set-car! v))
  383. (match-one x q ((cdr v) (set-cdr! v)) sk fk)
  384. fk
  385. i))
  386. fk))
  387. ((match-two v #(p ...) g+s . x)
  388. (match-vector v 0 () (p ...) . x))
  389. ((match-two v _ g+s (sk ...) fk i) (sk ... i))
  390. ;; Not a pair or vector or special literal, test to see if it's a
  391. ;; new symbol, in which case we just bind it, or if it's an
  392. ;; already bound symbol or some other literal, in which case we
  393. ;; compare it with EQUAL?.
  394. ((match-two v x g+s (sk ...) fk (id ...))
  395. (let-syntax
  396. ((new-sym?
  397. (syntax-rules (id ...)
  398. ((new-sym? x sk2 fk2) sk2)
  399. ((new-sym? y sk2 fk2) fk2))))
  400. (new-sym? random-sym-to-match
  401. (let ((x v)) (sk ... (id ... x)))
  402. (if (equal? v x) (sk ... (id ...)) fk))))
  403. ))
  404. ;; QUASIQUOTE patterns
  405. (define-syntax match-quasiquote
  406. (syntax-rules (unquote unquote-splicing quasiquote)
  407. ((_ v (unquote p) g+s sk fk i)
  408. (match-one v p g+s sk fk i))
  409. ((_ v ((unquote-splicing p) . rest) g+s sk fk i)
  410. (if (pair? v)
  411. (match-one v
  412. (p . tmp)
  413. (match-quasiquote tmp rest g+s sk fk)
  414. fk
  415. i)
  416. fk))
  417. ((_ v (quasiquote p) g+s sk fk i . depth)
  418. (match-quasiquote v p g+s sk fk i #f . depth))
  419. ((_ v (unquote p) g+s sk fk i x . depth)
  420. (match-quasiquote v p g+s sk fk i . depth))
  421. ((_ v (unquote-splicing p) g+s sk fk i x . depth)
  422. (match-quasiquote v p g+s sk fk i . depth))
  423. ((_ v (p . q) g+s sk fk i . depth)
  424. (if (pair? v)
  425. (let ((w (car v)) (x (cdr v)))
  426. (match-quasiquote
  427. w p g+s
  428. (match-quasiquote-step x q g+s sk fk depth)
  429. fk i . depth))
  430. fk))
  431. ((_ v #(elt ...) g+s sk fk i . depth)
  432. (if (vector? v)
  433. (let ((ls (vector->list v)))
  434. (match-quasiquote ls (elt ...) g+s sk fk i . depth))
  435. fk))
  436. ((_ v x g+s sk fk i . depth)
  437. (match-one v 'x g+s sk fk i))))
  438. (define-syntax match-quasiquote-step
  439. (syntax-rules ()
  440. ((match-quasiquote-step x q g+s sk fk depth i)
  441. (match-quasiquote x q g+s sk fk i . depth))))
  442. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  443. ;; Utilities
  444. ;; Takes two values and just expands into the first.
  445. (define-syntax match-drop-ids
  446. (syntax-rules ()
  447. ((_ expr ids ...) expr)))
  448. (define-syntax match-tuck-ids
  449. (syntax-rules ()
  450. ((_ (letish args (expr ...)) ids ...)
  451. (letish args (expr ... ids ...)))))
  452. (define-syntax match-drop-first-arg
  453. (syntax-rules ()
  454. ((_ arg expr) expr)))
  455. ;; To expand an OR group we try each clause in succession, passing the
  456. ;; first that succeeds to the success continuation. On failure for
  457. ;; any clause, we just try the next clause, finally resorting to the
  458. ;; failure continuation fk if all clauses fail. The only trick is
  459. ;; that we want to unify the identifiers, so that the success
  460. ;; continuation can refer to a variable from any of the OR clauses.
  461. (define-syntax match-gen-or
  462. (syntax-rules ()
  463. ((_ v p g+s (sk ...) fk (i ...) ((id id-ls) ...))
  464. (let ((sk2 (lambda (id ...) (sk ... (i ... id ...)))))
  465. (match-gen-or-step v p g+s (match-drop-ids (sk2 id ...)) fk (i ...))))))
  466. (define-syntax match-gen-or-step
  467. (syntax-rules ()
  468. ((_ v () g+s sk fk . x)
  469. ;; no OR clauses, call the failure continuation
  470. fk)
  471. ((_ v (p) . x)
  472. ;; last (or only) OR clause, just expand normally
  473. (match-one v p . x))
  474. ((_ v (p . q) g+s sk fk i)
  475. ;; match one and try the remaining on failure
  476. (let ((fk2 (lambda () (match-gen-or-step v q g+s sk fk i))))
  477. (match-one v p g+s sk (fk2) i)))
  478. ))
  479. ;; We match a pattern (p ...) by matching the pattern p in a loop on
  480. ;; each element of the variable, accumulating the bound ids into lists.
  481. ;; Look at the body of the simple case - it's just a named let loop,
  482. ;; matching each element in turn to the same pattern. The only trick
  483. ;; is that we want to keep track of the lists of each extracted id, so
  484. ;; when the loop recurses we cons the ids onto their respective list
  485. ;; variables, and on success we bind the ids (what the user input and
  486. ;; expects to see in the success body) to the reversed accumulated
  487. ;; list IDs.
  488. (define-syntax match-gen-ellipsis
  489. (syntax-rules ()
  490. ((_ v p () g+s (sk ...) fk i ((id id-ls) ...))
  491. (match-check-identifier p
  492. ;; simplest case equivalent to (p ...), just bind the list
  493. (let ((w v))
  494. (if (list? w)
  495. (match-one w p g+s (sk ...) fk i)
  496. fk))
  497. ;; simple case, match all elements of the list
  498. (let loop ((ls v) (id-ls '()) ...)
  499. (cond
  500. ((null? ls)
  501. (let ((id (reverse id-ls)) ...) (sk ... i)))
  502. ((pair? ls)
  503. (let ((w (car ls)))
  504. (match-one w p ((car ls) (set-car! ls))
  505. (match-drop-ids (loop (cdr ls) (cons id id-ls) ...))
  506. fk i)))
  507. (else
  508. fk)))))
  509. ((_ v p r g+s sk fk (i ...) ((id id-ls) ...))
  510. (match-verify-no-ellipsis
  511. r
  512. (match-bound-identifier-memv
  513. p
  514. (i ...)
  515. ;; p is bound, match the list up to the known length, then
  516. ;; match the trailing patterns
  517. (let loop ((ls v) (expect p))
  518. (cond
  519. ((null? expect)
  520. (match-one ls r (#f #f) sk fk (i ...)))
  521. ((pair? ls)
  522. (let ((w (car ls))
  523. (e (car expect)))
  524. (if (equal? (car ls) (car expect))
  525. (match-drop-ids (loop (cdr ls) (cdr expect)))
  526. fk)))
  527. (else
  528. fk)))
  529. ;; general case, trailing patterns to match, keep track of the
  530. ;; remaining list length so we don't need any backtracking
  531. (let* ((tail-len (length 'r))
  532. (ls v)
  533. (len (and (list? ls) (length ls))))
  534. (if (or (not len) (< len tail-len))
  535. fk
  536. (let loop ((ls ls) (n len) (id-ls '()) ...)
  537. (cond
  538. ((= n tail-len)
  539. (let ((id (reverse id-ls)) ...)
  540. (match-one ls r (#f #f) sk fk (i ... id ...))))
  541. ((pair? ls)
  542. (let ((w (car ls)))
  543. (match-one w p ((car ls) (set-car! ls))
  544. (match-drop-ids
  545. (loop (cdr ls) (- n 1) (cons id id-ls) ...))
  546. fk
  547. (i ...))))
  548. (else
  549. fk))))))))))
  550. ;; This is just a safety check. Although unlike syntax-rules we allow
  551. ;; trailing patterns after an ellipsis, we explicitly disable multiple
  552. ;; ellipses at the same level. This is because in the general case
  553. ;; such patterns are exponential in the number of ellipses, and we
  554. ;; don't want to make it easy to construct very expensive operations
  555. ;; with simple looking patterns. For example, it would be O(n^2) for
  556. ;; patterns like (a ... b ...) because we must consider every trailing
  557. ;; element for every possible break for the leading "a ...".
  558. (define-syntax match-verify-no-ellipsis
  559. (syntax-rules ()
  560. ((_ (x . y) sk)
  561. (match-check-ellipsis
  562. x
  563. (match-syntax-error
  564. "multiple ellipsis patterns not allowed at same level")
  565. (match-verify-no-ellipsis y sk)))
  566. ((_ () sk)
  567. sk)
  568. ((_ x sk)
  569. (match-syntax-error "dotted tail not allowed after ellipsis" x))))
  570. ;; To implement the tree search, we use two recursive procedures. TRY
  571. ;; attempts to match Y once, and on success it calls the normal SK on
  572. ;; the accumulated list ids as in MATCH-GEN-ELLIPSIS. On failure, we
  573. ;; call NEXT which first checks if the current value is a list
  574. ;; beginning with X, then calls TRY on each remaining element of the
  575. ;; list. Since TRY will recursively call NEXT again on failure, this
  576. ;; effects a full depth-first search.
  577. ;;
  578. ;; The failure continuation throughout is a jump to the next step in
  579. ;; the tree search, initialized with the original failure continuation
  580. ;; FK.
  581. (define-syntax match-gen-search
  582. (syntax-rules ()
  583. ((match-gen-search v p q g+s sk fk i ((id id-ls) ...))
  584. (letrec ((try (lambda (w fail id-ls ...)
  585. (match-one w q g+s
  586. (match-tuck-ids
  587. (let ((id (reverse id-ls)) ...)
  588. sk))
  589. (next w fail id-ls ...) i)))
  590. (next (lambda (w fail id-ls ...)
  591. (if (not (pair? w))
  592. (fail)
  593. (let ((u (car w)))
  594. (match-one
  595. u p ((car w) (set-car! w))
  596. (match-drop-ids
  597. ;; accumulate the head variables from
  598. ;; the p pattern, and loop over the tail
  599. (let ((id-ls (cons id id-ls)) ...)
  600. (let lp ((ls (cdr w)))
  601. (if (pair? ls)
  602. (try (car ls)
  603. (lambda () (lp (cdr ls)))
  604. id-ls ...)
  605. (fail)))))
  606. (fail) i))))))
  607. ;; the initial id-ls binding here is a dummy to get the right
  608. ;; number of '()s
  609. (let ((id-ls '()) ...)
  610. (try v (lambda () fk) id-ls ...))))))
  611. ;; Vector patterns are just more of the same, with the slight
  612. ;; exception that we pass around the current vector index being
  613. ;; matched.
  614. (define-syntax match-vector
  615. (syntax-rules (___)
  616. ((_ v n pats (p q) . x)
  617. (match-check-ellipsis q
  618. (match-gen-vector-ellipsis v n pats p . x)
  619. (match-vector-two v n pats (p q) . x)))
  620. ((_ v n pats (p ___) sk fk i)
  621. (match-gen-vector-ellipsis v n pats p sk fk i))
  622. ((_ . x)
  623. (match-vector-two . x))))
  624. ;; Check the exact vector length, then check each element in turn.
  625. (define-syntax match-vector-two
  626. (syntax-rules ()
  627. ((_ v n ((pat index) ...) () sk fk i)
  628. (if (vector? v)
  629. (let ((len (vector-length v)))
  630. (if (= len n)
  631. (match-vector-step v ((pat index) ...) sk fk i)
  632. fk))
  633. fk))
  634. ((_ v n (pats ...) (p . q) . x)
  635. (match-vector v (+ n 1) (pats ... (p n)) q . x))))
  636. (define-syntax match-vector-step
  637. (syntax-rules ()
  638. ((_ v () (sk ...) fk i) (sk ... i))
  639. ((_ v ((pat index) . rest) sk fk i)
  640. (let ((w (vector-ref v index)))
  641. (match-one w pat ((vector-ref v index) (vector-set! v index))
  642. (match-vector-step v rest sk fk)
  643. fk i)))))
  644. ;; With a vector ellipsis pattern we first check to see if the vector
  645. ;; length is at least the required length.
  646. (define-syntax match-gen-vector-ellipsis
  647. (syntax-rules ()
  648. ((_ v n ((pat index) ...) p sk fk i)
  649. (if (vector? v)
  650. (let ((len (vector-length v)))
  651. (if (>= len n)
  652. (match-vector-step v ((pat index) ...)
  653. (match-vector-tail v p n len sk fk)
  654. fk i)
  655. fk))
  656. fk))))
  657. (define-syntax match-vector-tail
  658. (syntax-rules ()
  659. ((_ v p n len sk fk i)
  660. (match-extract-vars p (match-vector-tail-two v p n len sk fk i) i ()))))
  661. (define-syntax match-vector-tail-two
  662. (syntax-rules ()
  663. ((_ v p n len (sk ...) fk i ((id id-ls) ...))
  664. (let loop ((j n) (id-ls '()) ...)
  665. (if (>= j len)
  666. (let ((id (reverse id-ls)) ...) (sk ... i))
  667. (let ((w (vector-ref v j)))
  668. (match-one w p ((vector-ref v j) (vector-set! v j))
  669. (match-drop-ids (loop (+ j 1) (cons id id-ls) ...))
  670. fk i)))))))
  671. (define-syntax match-record-refs
  672. (syntax-rules ()
  673. ((_ v rec n (p . q) g+s sk fk i)
  674. (let ((w (slot-ref rec v n)))
  675. (match-one w p ((slot-ref rec v n) (slot-set! rec v n))
  676. (match-record-refs v rec (+ n 1) q g+s sk fk) fk i)))
  677. ((_ v rec n () g+s (sk ...) fk i)
  678. (sk ... i))))
  679. ;; Extract all identifiers in a pattern. A little more complicated
  680. ;; than just looking for symbols, we need to ignore special keywords
  681. ;; and non-pattern forms (such as the predicate expression in ?
  682. ;; patterns), and also ignore previously bound identifiers.
  683. ;;
  684. ;; Calls the continuation with all new vars as a list of the form
  685. ;; ((orig-var tmp-name) ...), where tmp-name can be used to uniquely
  686. ;; pair with the original variable (e.g. it's used in the ellipsis
  687. ;; generation for list variables).
  688. ;;
  689. ;; (match-extract-vars pattern continuation (ids ...) (new-vars ...))
  690. (define-syntax match-extract-vars
  691. (syntax-rules (_ ___ ..1 *** ? $ = quote quasiquote and or not get! set!)
  692. ((match-extract-vars (? pred . p) . x)
  693. (match-extract-vars p . x))
  694. ((match-extract-vars ($ rec . p) . x)
  695. (match-extract-vars p . x))
  696. ((match-extract-vars (= proc p) . x)
  697. (match-extract-vars p . x))
  698. ((match-extract-vars (quote x) (k ...) i v)
  699. (k ... v))
  700. ((match-extract-vars (quasiquote x) k i v)
  701. (match-extract-quasiquote-vars x k i v (#t)))
  702. ((match-extract-vars (and . p) . x)
  703. (match-extract-vars p . x))
  704. ((match-extract-vars (or . p) . x)
  705. (match-extract-vars p . x))
  706. ((match-extract-vars (not . p) . x)
  707. (match-extract-vars p . x))
  708. ;; A non-keyword pair, expand the CAR with a continuation to
  709. ;; expand the CDR.
  710. ((match-extract-vars (p q . r) k i v)
  711. (match-check-ellipsis
  712. q
  713. (match-extract-vars (p . r) k i v)
  714. (match-extract-vars p (match-extract-vars-step (q . r) k i v) i ())))
  715. ((match-extract-vars (p . q) k i v)
  716. (match-extract-vars p (match-extract-vars-step q k i v) i ()))
  717. ((match-extract-vars #(p ...) . x)
  718. (match-extract-vars (p ...) . x))
  719. ((match-extract-vars _ (k ...) i v) (k ... v))
  720. ((match-extract-vars ___ (k ...) i v) (k ... v))
  721. ((match-extract-vars *** (k ...) i v) (k ... v))
  722. ((match-extract-vars ..1 (k ...) i v) (k ... v))
  723. ;; This is the main part, the only place where we might add a new
  724. ;; var if it's an unbound symbol.
  725. ((match-extract-vars p (k ...) (i ...) v)
  726. (let-syntax
  727. ((new-sym?
  728. (syntax-rules (i ...)
  729. ((new-sym? p sk fk) sk)
  730. ((new-sym? any sk fk) fk))))
  731. (new-sym? random-sym-to-match
  732. (k ... ((p p-ls) . v))
  733. (k ... v))))
  734. ))
  735. ;; Stepper used in the above so it can expand the CAR and CDR
  736. ;; separately.
  737. (define-syntax match-extract-vars-step
  738. (syntax-rules ()
  739. ((_ p k i v ((v2 v2-ls) ...))
  740. (match-extract-vars p k (v2 ... . i) ((v2 v2-ls) ... . v)))
  741. ))
  742. (define-syntax match-extract-quasiquote-vars
  743. (syntax-rules (quasiquote unquote unquote-splicing)
  744. ((match-extract-quasiquote-vars (quasiquote x) k i v d)
  745. (match-extract-quasiquote-vars x k i v (#t . d)))
  746. ((match-extract-quasiquote-vars (unquote-splicing x) k i v d)
  747. (match-extract-quasiquote-vars (unquote x) k i v d))
  748. ((match-extract-quasiquote-vars (unquote x) k i v (#t))
  749. (match-extract-vars x k i v))
  750. ((match-extract-quasiquote-vars (unquote x) k i v (#t . d))
  751. (match-extract-quasiquote-vars x k i v d))
  752. ((match-extract-quasiquote-vars (x . y) k i v d)
  753. (match-extract-quasiquote-vars
  754. x
  755. (match-extract-quasiquote-vars-step y k i v d) i () d))
  756. ((match-extract-quasiquote-vars #(x ...) k i v d)
  757. (match-extract-quasiquote-vars (x ...) k i v d))
  758. ((match-extract-quasiquote-vars x (k ...) i v d)
  759. (k ... v))
  760. ))
  761. (define-syntax match-extract-quasiquote-vars-step
  762. (syntax-rules ()
  763. ((_ x k i v d ((v2 v2-ls) ...))
  764. (match-extract-quasiquote-vars x k (v2 ... . i) ((v2 v2-ls) ... . v) d))
  765. ))
  766. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  767. ;; Gimme some sugar baby.
  768. ;;> Shortcut for @scheme{lambda} + @scheme{match}. Creates a
  769. ;;> procedure of one argument, and matches that argument against each
  770. ;;> clause.
  771. (define-syntax match-lambda
  772. (syntax-rules ()
  773. ((_ (pattern . body) ...) (lambda (expr) (match expr (pattern . body) ...)))))
  774. ;;> Similar to @scheme{match-lambda}. Creates a procedure of any
  775. ;;> number of arguments, and matches the argument list against each
  776. ;;> clause.
  777. (define-syntax match-lambda*
  778. (syntax-rules ()
  779. ((_ (pattern . body) ...) (lambda expr (match expr (pattern . body) ...)))))
  780. ;;> Matches each var to the corresponding expression, and evaluates
  781. ;;> the body with all match variables in scope. Raises an error if
  782. ;;> any of the expressions fail to match. Syntax analogous to named
  783. ;;> let can also be used for recursive functions which match on their
  784. ;;> arguments as in @scheme{match-lambda*}.
  785. (define-syntax match-let
  786. (syntax-rules ()
  787. ((_ ((var value) ...) . body)
  788. (match-let/helper let () () ((var value) ...) . body))
  789. ((_ loop ((var init) ...) . body)
  790. (match-named-let loop () ((var init) ...) . body))))
  791. ;;> Similar to @scheme{match-let}, but analogously to @scheme{letrec}
  792. ;;> matches and binds the variables with all match variables in scope.
  793. (define-syntax match-letrec
  794. (syntax-rules ()
  795. ((_ ((var value) ...) . body)
  796. (match-let/helper letrec () () ((var value) ...) . body))))
  797. (define-syntax match-let/helper
  798. (syntax-rules ()
  799. ((_ let ((var expr) ...) () () . body)
  800. (let ((var expr) ...) . body))
  801. ((_ let ((var expr) ...) ((pat tmp) ...) () . body)
  802. (let ((var expr) ...)
  803. (match-let* ((pat tmp) ...)
  804. . body)))
  805. ((_ let (v ...) (p ...) (((a . b) expr) . rest) . body)
  806. (match-let/helper
  807. let (v ... (tmp expr)) (p ... ((a . b) tmp)) rest . body))
  808. ((_ let (v ...) (p ...) ((#(a ...) expr) . rest) . body)
  809. (match-let/helper
  810. let (v ... (tmp expr)) (p ... (#(a ...) tmp)) rest . body))
  811. ((_ let (v ...) (p ...) ((a expr) . rest) . body)
  812. (match-let/helper let (v ... (a expr)) (p ...) rest . body))))
  813. (define-syntax match-named-let
  814. (syntax-rules ()
  815. ((_ loop ((pat expr var) ...) () . body)
  816. (let loop ((var expr) ...)
  817. (match-let ((pat var) ...)
  818. . body)))
  819. ((_ loop (v ...) ((pat expr) . rest) . body)
  820. (match-named-let loop (v ... (pat expr tmp)) rest . body))))
  821. ;;> @subsubsubsection{@rawcode{(match-let* ((var value) ...) body ...)}}
  822. ;;> Similar to @scheme{match-let}, but analogously to @scheme{let*}
  823. ;;> matches and binds the variables in sequence, with preceding match
  824. ;;> variables in scope.
  825. (define-syntax match-let*
  826. (syntax-rules ()
  827. ((_ () . body)
  828. (let () . body))
  829. ((_ ((pat expr) . rest) . body)
  830. (match expr (pat (match-let* rest . body))))))
  831. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  832. ;; Otherwise COND-EXPANDed bits.
  833. ;; This *should* work, but doesn't :(
  834. ;; (define-syntax match-check-ellipsis
  835. ;; (syntax-rules (...)
  836. ;; ((_ ... sk fk) sk)
  837. ;; ((_ x sk fk) fk)))
  838. ;; This is a little more complicated, and introduces a new let-syntax,
  839. ;; but should work portably in any R[56]RS Scheme. Taylor Campbell
  840. ;; originally came up with the idea.
  841. (define-syntax match-check-ellipsis
  842. (syntax-rules ()
  843. ;; these two aren't necessary but provide fast-case failures
  844. ((match-check-ellipsis (a . b) success-k failure-k) failure-k)
  845. ((match-check-ellipsis #(a ...) success-k failure-k) failure-k)
  846. ;; matching an atom
  847. ((match-check-ellipsis id success-k failure-k)
  848. (let-syntax ((ellipsis? (syntax-rules ()
  849. ;; iff `id' is `...' here then this will
  850. ;; match a list of any length
  851. ((ellipsis? (foo id) sk fk) sk)
  852. ((ellipsis? other sk fk) fk))))
  853. ;; this list of three elements will only match the (foo id) list
  854. ;; above if `id' is `...'
  855. (ellipsis? (a b c) success-k failure-k)))))
  856. ;; This is portable but can be more efficient with non-portable
  857. ;; extensions. This trick was originally discovered by Oleg Kiselyov.
  858. (define-syntax match-check-identifier
  859. (syntax-rules ()
  860. ;; fast-case failures, lists and vectors are not identifiers
  861. ((_ (x . y) success-k failure-k) failure-k)
  862. ((_ #(x ...) success-k failure-k) failure-k)
  863. ;; x is an atom
  864. ((_ x success-k failure-k)
  865. (let-syntax
  866. ((sym?
  867. (syntax-rules ()
  868. ;; if the symbol `abracadabra' matches x, then x is a
  869. ;; symbol
  870. ((sym? x sk fk) sk)
  871. ;; otherwise x is a non-symbol datum
  872. ((sym? y sk fk) fk))))
  873. (sym? abracadabra success-k failure-k)))))
  874. (define-syntax match-bound-identifier-memv
  875. (syntax-rules ()
  876. ((match-bound-identifier-memv a (id ...) sk fk)
  877. (match-check-identifier
  878. a
  879. (let-syntax
  880. ((memv?
  881. (syntax-rules (id ...)
  882. ((memv? a sk2 fk2) fk2)
  883. ((memv? anything-else sk2 fk2) sk2))))
  884. (memv? random-sym-to-match sk fk))
  885. fk))))