Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite rule for equal? #649

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions lib/comprewrite.stk
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,58 @@
expr))
expr))
expr)))

(compiler:add-rewriter! ;; 'EQUAL?' rewriter
'equal?
;; (equal? ATOM ATOM) ==> #t or #f
;; (equal? ATOM x) ==> (eq? ATOM x)
;; (equal? x y) ==> (equal? x y)
(lambda (expr len env)
(if (= len 3)
;; The following are predicates to test wether an object can be compared
;; with 'eq?'. If there are possible predicates missing, that's fine -
;; some cases won't be optimized, but we'll be safe.
(let ((predicates-for-eq (list
fixnum?
char?
;; do NOT include symbol?
eof-object?
void?
(lambda (x) (memq x '(#t #f)))
(lambda (x) (and (real? x)
(inexact? x)
(not (rational? x))))))
(x (rewrite-expression (cadr expr) env))
(y (rewrite-expression (caddr expr) env)))
;; x-res and y-res are lists with the results of the predicates for the
;; first and second arguments to 'equal?'
;;
;; For (equal? a 20), we'll have:
;; x-res (#f #f #f #f #f #f)
;; y-res (#t #f #f #f #f #f)
(let ((x-res (map (lambda (pred?) (and (const-expr? x)
(not (symbol? (cadr expr)))
(pred? (const-value x))))
predicates-for-eq))
(y-res (map (lambda (pred?) (and (const-expr? y)
(not (symbol? (caddr expr)))
(pred? (const-value y))))
predicates-for-eq)))
(cond ((and (const-expr? x)
(const-expr? y))
;; two constants, just check if they're equal? and insert #t or #f:
(equal? x y))

((or (any (lambda (z) z) x-res)
(any (lambda (z) z) y-res))
;; At least one is constant, and is eq?-comparable, and that is
;; enough for us to use eq?:
(list 'eq?
;; One is constant and the other isn't, so we use
;; const-value for the constant and (cxr expr) for
;; the other:
(if (const-expr? x) (const-value x) (cadr expr))
(if (const-expr? y) (const-value y) (caddr expr))))

(else expr))))
expr)))