Add access to the failure continuation in match.

This commit is contained in:
Sam Tobin-Hochstadt 2013-10-21 16:46:59 -04:00
parent 82bb5ba4c8
commit ec77a48d23
3 changed files with 22 additions and 3 deletions

View File

@ -34,7 +34,8 @@ arguments. If this procedure is invoked, it escapes back to the
pattern matching expression, and resumes the matching process as if pattern matching expression, and resumes the matching process as if
the pattern had failed to match. The @racket[body]s must not mutate the pattern had failed to match. The @racket[body]s must not mutate
the object being matched before calling the failure procedure, the object being matched before calling the failure procedure,
otherwise the behavior of matching is unpredictable. otherwise the behavior of matching is unpredictable. See also
@racket[failure-cont].
The grammar of @racket[pat] is as follows, where non-italicized The grammar of @racket[pat] is as follows, where non-italicized
identifiers are recognized symbolically (i.e., not by binding). identifiers are recognized symbolically (i.e., not by binding).
@ -518,6 +519,13 @@ b
A predicate for the exception raised in the case of a match failure. A predicate for the exception raised in the case of a match failure.
} }
@defform[(failure-cont)]{
Continues matching as if the current pattern failed. Note that unlike
use of the @racket[=>] form, this does @emph{not} escape the current
context, and thus should only be used in tail position with respect to
the @racket[match] form.
}
@; ---------------------------------------- @; ----------------------------------------

View File

@ -710,5 +710,11 @@
(match-let ([(list x y) (list 1 22)] [(list y z) '(2 3)]) (match-let ([(list x y) (list 1 22)] [(list y z) '(2 3)])
(list x y z)))) (list x y z))))
(comp 1
(match (cons 1 2)
[(cons a b)
(if (< a b)
(failure-cont)
0)]
[_ 1]))
)) ))

View File

@ -1,16 +1,21 @@
#lang racket/base #lang racket/base
(require racket/match/match (require racket/match/match "match/runtime.rkt"
(for-syntax racket/base)) (for-syntax racket/base))
(provide (except-out (all-from-out racket/match/match) (provide (except-out (all-from-out racket/match/match)
define-match-expander) define-match-expander)
failure-cont
(rename-out [define-match-expander* define-match-expander])) (rename-out [define-match-expander* define-match-expander]))
(define-for-syntax (no-old-match-form stx) (define-for-syntax (no-old-match-form stx)
(raise-syntax-error (raise-syntax-error
#f #f
"works only for constructor-based `match' form" "works only for constructor-based `match' form"
stx)) stx))
(define-syntax-rule (failure-cont) (fail))
(define-syntax define-match-expander* (define-syntax define-match-expander*
(syntax-rules () (syntax-rules ()
[(_ id expr) (define-match-expander id expr)] [(_ id expr) (define-match-expander id expr)]