pattern-subst

This commit is contained in:
Tony Garnock-Jones 2013-10-30 18:01:39 +00:00
parent 853f3b2f6c
commit 66215d40f7
1 changed files with 16 additions and 1 deletions

View File

@ -9,7 +9,8 @@
specialization?
ground?
intersect
intersect?)
intersect?
pattern-subst)
(struct exn:unification-failure ())
(define unification-failure (exn:unification-failure))
@ -105,3 +106,17 @@
(with-handlers ([exn:unification-failure? (lambda (e) #f)])
(unify a b)
#t))
(define (pattern-subst x from to)
(let walk ((x x))
(cond
[(equal? x from) to]
[(pair? x) (cons (walk (car x)) (walk (cdr x)))]
[(vector? x) (for/vector ([e (in-vector x)]) (walk e))]
[(non-object-struct? x)
(define-values (tx tx-skipped?) (struct-info x))
(when tx-skipped?
(error 'pattern-subst "Cannot substitute in (partially-)opaque structs: ~v" x))
(vector->struct (walk (struct->vector x #f)) tx)]
[(hash? x) (for/hash ([(k v) (in-hash x)]) (values k (walk v)))]
[else x])))