2.4 gjs-cselim
(require sicm/general/gjs-cselim) | package: rktsicm |
Some utilities for finding common subexpressions
procedure
(gjs/cselim expression [ not-worth-subdividing?]) → any/c expression : any/c not-worth-subdividing? : predicate/c = (λ (x) #f)
Given a (symbolic) expression, find the common subexpressions and rewrite the expression inserting let’s at the highest possible level. Any expression that is not-worth-subdividing? will be left as is.
gjs/cselim recognizes lambda, λ and plain let as introducing new scope. Named let, let-values, definitions, or other mechanisms that introduce new bindings will probably give the wrong result.
Examples:
> (gjs/cselim '(lambda (x) (/ (+ (* x 3) (- y z) (- x y) (* x 3)) (- y z)))) '(let ((g4957 (- y z))) (lambda (x) (let ((g4956 (* x 3))) (/ (+ g4956 g4957 (- x y) g4956) g4957))))
> (gjs/cselim '(list (+ 4 x) (let* ([x 4] [y (+ 4 x)]) (+ (* x y) (/ (* x y) (+ 4 x)))))) '(let ((g4962 (+ 4 x)) (g4966 (* x y))) (list g4962 (let* ((x 4) (y g4962)) (+ g4966 (/ g4966 g4962)))))
> (gjs/cselim '(list (+ 4 x) (let ([x 4] [y (+ 4 x)]) (+ (* x y) (/ (* x y) (+ 4 x)))))) '(let ((g4971 (+ 4 x))) (list g4971 (let ((x 4) (y (+ 4 x))) (let ((g4972 (* x y))) (+ g4972 (/ g4972 (+ 4 x)))))))
procedure
(occurs-in? variables expression) → (or/c #f #t list?)
variables : any/c expression : any/c
Check if any of the variables occur in the expression, but also works if variables or expression is an object instead of a list.