Add ground? and fix variables-in
This commit is contained in:
parent
634e795a6c
commit
f1f7cc0d8c
15
unify.rkt
15
unify.rkt
|
@ -10,6 +10,7 @@
|
||||||
wild
|
wild
|
||||||
wild?
|
wild?
|
||||||
non-wild?
|
non-wild?
|
||||||
|
ground?
|
||||||
variables-in
|
variables-in
|
||||||
unify
|
unify
|
||||||
unify/env
|
unify/env
|
||||||
|
@ -69,12 +70,24 @@
|
||||||
(define (variables-in x)
|
(define (variables-in x)
|
||||||
(let walk ((x x) (acc (set)))
|
(let walk ((x x) (acc (set)))
|
||||||
(cond
|
(cond
|
||||||
[(variable? x) (set-add acc x)]
|
[(wild? x) (set-add acc x)]
|
||||||
[(pair? x) (walk (car x) (walk (cdr x) acc))]
|
[(pair? x) (walk (car x) (walk (cdr x) acc))]
|
||||||
[(vector? x) (foldl walk acc (vector->list x))]
|
[(vector? x) (foldl walk acc (vector->list x))]
|
||||||
[(non-object-struct? x) (walk (struct->vector x #f) acc)]
|
[(non-object-struct? x) (walk (struct->vector x #f) acc)]
|
||||||
[else acc])))
|
[else acc])))
|
||||||
|
|
||||||
|
;; Any -> Boolean
|
||||||
|
;; True iff the term is completely ground, that is has no variables or
|
||||||
|
;; canonical-variables in it.
|
||||||
|
(define (ground? x)
|
||||||
|
(let walk ((x x))
|
||||||
|
(cond
|
||||||
|
[(wild? x) #f]
|
||||||
|
[(pair? x) (and (walk (car x)) (walk (cdr x)))]
|
||||||
|
[(vector? x) (andmap walk (vector->list x))]
|
||||||
|
[(non-object-struct? x) (walk (struct->vector x #f))]
|
||||||
|
[else #t])))
|
||||||
|
|
||||||
;; Variable Any -> Boolean
|
;; Variable Any -> Boolean
|
||||||
(define (occurs? var val)
|
(define (occurs? var val)
|
||||||
(let walk ((x val))
|
(let walk ((x val))
|
||||||
|
|
Loading…
Reference in New Issue