add 'match-pattern?'
This commit is contained in:
parent
fdf3fa6492
commit
676f744c1d
|
@ -601,6 +601,11 @@ See match-a-pattern.rkt for more details
|
||||||
[`(list ,pats ...) #f]
|
[`(list ,pats ...) #f]
|
||||||
[(? (compose not pair?)) #t])))
|
[(? (compose not pair?)) #t])))
|
||||||
|
|
||||||
|
;; match-pattern? : compiled-pattern exp -> boolean
|
||||||
|
(define (match-pattern? compiled-pattern exp)
|
||||||
|
(let ([results ((compiled-pattern-cp compiled-pattern) exp #f)])
|
||||||
|
(and results #t)))
|
||||||
|
|
||||||
;; match-pattern : compiled-pattern exp -> (union #f (listof bindings))
|
;; match-pattern : compiled-pattern exp -> (union #f (listof bindings))
|
||||||
(define (match-pattern compiled-pattern exp)
|
(define (match-pattern compiled-pattern exp)
|
||||||
(let ([results ((compiled-pattern-cp compiled-pattern) exp #f)])
|
(let ([results ((compiled-pattern-cp compiled-pattern) exp #f)])
|
||||||
|
@ -1952,6 +1957,7 @@ See match-a-pattern.rkt for more details
|
||||||
|
|
||||||
(provide/contract
|
(provide/contract
|
||||||
(match-pattern (compiled-pattern? any/c . -> . (or/c false/c (listof mtch?))))
|
(match-pattern (compiled-pattern? any/c . -> . (or/c false/c (listof mtch?))))
|
||||||
|
(match-pattern? (compiled-pattern? any/c . -> . boolean?))
|
||||||
(compile-pattern (-> compiled-lang? any/c boolean?
|
(compile-pattern (-> compiled-lang? any/c boolean?
|
||||||
compiled-pattern?))
|
compiled-pattern?))
|
||||||
|
|
||||||
|
|
|
@ -1009,7 +1009,10 @@
|
||||||
(reduction-relation-make-procs subj))])
|
(reduction-relation-make-procs subj))])
|
||||||
(make-coverage subj h))))]))
|
(make-coverage subj h))))]))
|
||||||
|
|
||||||
(define-syntax (test-match stx)
|
(define-syntax (test-match stx) (test-match/both stx #f))
|
||||||
|
(define-syntax (test-match? stx) (test-match/both stx #t))
|
||||||
|
|
||||||
|
(define-for-syntax (test-match/both stx boolean-only?)
|
||||||
(syntax-case stx ()
|
(syntax-case stx ()
|
||||||
[(form-name lang-exp pattern)
|
[(form-name lang-exp pattern)
|
||||||
(identifier? #'lang-exp)
|
(identifier? #'lang-exp)
|
||||||
|
@ -1019,8 +1022,7 @@
|
||||||
(rewrite-side-conditions/check-errs nts what #t #'pattern)])
|
(rewrite-side-conditions/check-errs nts what #t #'pattern)])
|
||||||
(with-syntax ([binders (map syntax-e (syntax->list #'(vars ...)))]
|
(with-syntax ([binders (map syntax-e (syntax->list #'(vars ...)))]
|
||||||
[name (syntax-local-infer-name stx)])
|
[name (syntax-local-infer-name stx)])
|
||||||
(syntax
|
#`(do-test-match lang-exp `side-condition-rewritten 'binders 'name #,boolean-only?))))]
|
||||||
(do-test-match lang-exp `side-condition-rewritten 'binders 'name)))))]
|
|
||||||
[(form-name lang-exp pattern expression)
|
[(form-name lang-exp pattern expression)
|
||||||
(identifier? #'lang-exp)
|
(identifier? #'lang-exp)
|
||||||
(syntax
|
(syntax
|
||||||
|
@ -1032,7 +1034,7 @@
|
||||||
|
|
||||||
(define-struct match (bindings) #:inspector #f)
|
(define-struct match (bindings) #:inspector #f)
|
||||||
|
|
||||||
(define (do-test-match lang pat binders context-name)
|
(define (do-test-match lang pat binders context-name boolean-only?)
|
||||||
(unless (compiled-lang? lang)
|
(unless (compiled-lang? lang)
|
||||||
(error 'redex-match "expected first argument to be a language, got ~e" lang))
|
(error 'redex-match "expected first argument to be a language, got ~e" lang))
|
||||||
(define name (or context-name
|
(define name (or context-name
|
||||||
|
@ -1040,13 +1042,15 @@
|
||||||
pat)))
|
pat)))
|
||||||
(define cpat (compile-pattern lang pat #t))
|
(define cpat (compile-pattern lang pat #t))
|
||||||
(define redex-match-proc
|
(define redex-match-proc
|
||||||
(λ (exp)
|
(if boolean-only?
|
||||||
(let ([ans (match-pattern cpat exp)])
|
(λ (exp) (match-pattern? cpat exp))
|
||||||
(and ans
|
(λ (exp)
|
||||||
(map (λ (m) (make-match (sort-bindings
|
(let ([ans (match-pattern cpat exp)])
|
||||||
(filter (λ (x) (memq (bind-name x) binders))
|
(and ans
|
||||||
(bindings-table (mtch-bindings m))))))
|
(map (λ (m) (make-match (sort-bindings
|
||||||
ans)))))
|
(filter (λ (x) (memq (bind-name x) binders))
|
||||||
|
(bindings-table (mtch-bindings m))))))
|
||||||
|
ans))))))
|
||||||
(if name
|
(if name
|
||||||
(procedure-rename redex-match-proc name)
|
(procedure-rename redex-match-proc name)
|
||||||
redex-match-proc))
|
redex-match-proc))
|
||||||
|
@ -2369,6 +2373,7 @@
|
||||||
(struct-out binds))
|
(struct-out binds))
|
||||||
|
|
||||||
(provide test-match
|
(provide test-match
|
||||||
|
test-match?
|
||||||
term-match
|
term-match
|
||||||
term-match/single
|
term-match/single
|
||||||
redex-let
|
redex-let
|
||||||
|
|
|
@ -40,6 +40,7 @@
|
||||||
check-redudancy)
|
check-redudancy)
|
||||||
|
|
||||||
(provide (rename-out [test-match redex-match])
|
(provide (rename-out [test-match redex-match])
|
||||||
|
(rename-out [test-match? redex-match?])
|
||||||
term-match
|
term-match
|
||||||
term-match/single
|
term-match/single
|
||||||
redex-let
|
redex-let
|
||||||
|
|
|
@ -344,6 +344,13 @@ matches, it returns a list of match structures describing the
|
||||||
matches. If the match fails, the procedure returns @racket[#f].
|
matches. If the match fails, the procedure returns @racket[#f].
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@defform*[[(redex-match? lang @#,ttpattern any)
|
||||||
|
(redex-match? lang @#,ttpattern)]]{
|
||||||
|
|
||||||
|
Like @racket[redex-match], except it returns only a boolean
|
||||||
|
indicating if the match was successful.
|
||||||
|
}
|
||||||
|
|
||||||
@defproc[(match? [val any/c]) boolean?]{
|
@defproc[(match? [val any/c]) boolean?]{
|
||||||
|
|
||||||
Determines if a value is a @tt{match} structure.
|
Determines if a value is a @tt{match} structure.
|
||||||
|
|
Loading…
Reference in New Issue
Block a user