;; The first three lines of this file were inserted by DrScheme. They record metadata ;; about the language level of this file in a form that our tools can easily process. #reader(planet plai/plai:1:3/lang/reader) (define-type FunDef [fundef (fun-name symbol?) (arg-name symbol?) (body F1WAE?)]) (define-type F1WAE [num (n number?)] [add (lhs F1WAE?) (rhs F1WAE?)] [sub (lhs F1WAE?) (rhs F1WAE?)] [with (name symbol?) (named-expr F1WAE?) (body F1WAE?)] [id (name symbol?)] [app (fun-name symbol?) (arg-expr F1WAE?)]) ; called-functions : F1WAE List-of-DefFun -> list-of-symbols (define (called-functions e lod) (append (cf-in-expr e) (cf-in-defns lod))) ; cf-in-expr : F1WAE -> list-of-symbols (define (cf-in-expr e) (type-case F1WAE e [num (n) empty] [add (l r) (append (cf-in-expr l) (cf-in-expr r))] [sub (l r) (append (cf-in-expr l) (cf-in-expr r))] [with (id rhs body) (append (cf-in-expr rhs) (cf-in-expr body))] [id (name) empty] [app (fname arg-expr) (append (list fname) (cf-in-expr arg-expr))])) (test (cf-in-expr (num 0)) empty) (test (cf-in-expr (app 'f (num 0))) (list 'f)) (test (cf-in-expr (add (app 'f (num 0)) (app 'h (num 12)))) (list 'f 'h)) (test (cf-in-expr (sub (app 'f (num 0)) (app 'h (num 12)))) (list 'f 'h)) (test (cf-in-expr (with 'z (app 'f (num 0)) (app 'h (num 12)))) (list 'f 'h)) (test (cf-in-expr (id 'x)) empty) ; cf-in-defns : list-of-FunDef -> list-of-symbols (define (cf-in-defns lod) (cond [(empty? lod) empty] [(cons? lod) (append (cf-in-defn (first lod)) (cf-in-defns (rest lod)))])) ; cf-in-defn : FunDef -> list-of-symbols (define (cf-in-defn d) (type-case FunDef d [fundef (fname arg-name body-expr) (cf-in-expr body-expr)])) (test (cf-in-defn (fundef 'g 'y (app 'f (num 1)))) (list 'f)) (test (cf-in-defn (fundef 'g 'y (num 0))) empty) (test (cf-in-defns empty) empty) (test (cf-in-defns (list (fundef 'g 'x (app 'f (num 0))) (fundef 'h 'y (app 'g (num 11))))) (list 'f 'g)) (test (called-functions (num 0) empty) empty) (test (called-functions (app 'f (num 0)) empty) (list 'f)) (test (called-functions (add (app 'f (num 0)) (app 'h (num 12))) empty) (list 'f 'h)) (test (called-functions (sub (app 'f (num 0)) (app 'h (num 12))) empty) (list 'f 'h)) (test (called-functions (with 'z (app 'f (num 0)) (app 'h (num 12))) empty) (list 'f 'h)) (test (called-functions (id 'x) empty) empty) (test (called-functions (num 0) (list (fundef 'g 'x (app 'f (num 0))) (fundef 'h 'y (app 'g (num 11))))) (list 'f 'g))