lec-6 - (fundef-arg-name the-function) (interp arg-exp...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
;; 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:4/lang/reader) ;<F1wae> :: =<nm> ; |{+<F1WAE> <F1WAE>} ; |{with{<id><F1WAE>}<F1WAE>} ; |{app} (define-type F1WAE (num (value number?)) (add (left F1WAE?) (right F1WAE?)) (sub (left F1WAE?) (right F1WAE?)) (with (name symbol?) (name-exp F1WAE?) (body F1WAE?)) (id (name symbol?))) (define-type FunDef (fun-def (name symbol?) (arg-name symbol?) (body F1WAE?)) ) (FunDef 'double 'n (add (id 'n) (id 'n))) ;interp: F1WAE list-of-Fundefs->number (define (interp exp fundefs) (type-case WAE exp (num (v) v) (add (l r) (+ (interp l fundefs) (interp r fundefs))) (sub (l r) (- (interp l fundefs) (interp r fundefs))) (with (name nmd body) (calc (subst body name (num (interp nmd fundefs))))) (id (name) (error "Free identifier")) (app (name arg-exp) (let ((the-function(find-function name fundefs))) (interp (subst (fundef-body the-function)
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: (fundef-arg-name the-function) (interp arg-exp fundefs))))))) (define (find-function name fundefs) (findf (lambda (candidate) (eq? (fundef-name candidate) name)) fundefs)) ; subst :: WAE symbol F1WAE -&gt; F1WAE ; Returns an expression that is a copy of the first expression ; with each free occurrence of the given symbol replaced by the ; second expression. (define (subst exp sub-id val) (type-case WAE exp (num (v) exp) (add (l r) (add (subst l sub-id val) (subst r sub-id val))) (sub (l r) (sub (subst l sub-id val) (subst r sub-id val))) (with (name nmd body) (if (eqv? name sub-id) ;special case: the sub-id gets bound here ;example: (with (x1) (with((+x1))(+xx))) (with name (subst nmd sub-id val) body); dont sub into the body ;regular case: sub-id not bound(yet) (with name (subst nmd sub-id val) (subst body sub-id val)))) (id (name) (if (eqv? name sub-id) val exp)) ;extra case (app (name arg-exp) (app name (subst arg-exp sub-id val)))))...
View Full Document

Page1 / 2

lec-6 - (fundef-arg-name the-function) (interp arg-exp...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online