200 likes | 346 Views
CMSC 11500 Introduction to Computer Programming November 22, 2002. Meta-Circular Evaluation: Functions. Roadmap. Recap: Meta-circular evaluation Extending the Evaluator Representing functions Functions as s-expressions Function definitions Data structure and definition
E N D
CMSC 11500 Introduction to Computer Programming November 22, 2002 Meta-Circular Evaluation:Functions
Roadmap • Recap: Meta-circular evaluation • Extending the Evaluator • Representing functions • Functions as s-expressions • Function definitions • Data structure and definition • Evaluating with functions • From one to many • Summary
Recap: Meta-circular Evaluation • Writing scheme code to evaluate scheme code • Representing scheme expressions: • A scheme-expression (s-exp): • 1) number • 2) symbol • 3) (make-add s-exp s-exp) • 4) (make-mull s-exp s-exp) • Template: • (define (fn-for-sexp sexp) • (cond ((number? sexp) ... • ((symbol? sexp)... • ((add? sexp)...(fn-for-sexp (add-left sexp))(fn-for-sexp (add-right sexp) • ((mul? sexp)..(fn-for-sexp (mul-left sexp))(fn-for-sexp (mul-right sexp)
Extending the Evaluator • Simple function applications • e.g. (f 1), (g (+ 2 3)), (f (* 2 (+ 3 4)) • Data structure: • (define-struct app (name arg)) • Where name is symbol, arg is s-exp
Data Definition: Scheme ExpressionsExtended • A scheme-expression (s-exp) is • number • Symbol • (make-add left right) • (make-mul left right) • Where left, right are s-exp • (make-app name arg)
Function Definition • Represention: • e.g. For (define (f x) (* x x)) • Components: • Function name, parameter name, body • (define-struct def (name arg body)) • Where name, arg: symbol; body: s-exp • A scheme-definition (s-def) is: • (make-def name arg body)
S-Exp Template (define (fn-for-s-exp sexp) (cond ((number? sexp) ...) ((symbol? sexp) ...) ((add? sexp) ... (fn-for-sexp (add-left sexp))... ... (fn-for-sexp (add-right sexp))... ((mul? sexp) ... (fn-for-sexp (mul-left sexp))... ... (fn-for-sexp (mul-right sexp))... ((fn-app? sexp) ...???...(fn-for-sexp (fn-app-arg sexp)))
Subst – revised (define (subst var val sexp) (cond ((number? sexp) sexp) ((symbol? sexp) (if (symbol=? var sexp) val sexp)) ((add? sexp) (make-add (subst var val (add-left sexp)) (subst var val (add-right sexp)))) ((mul? sexp) (make-mul (subst var val (mul-left sexp)) (subst var val (mul-right sexp)))) ((app? sexp) (make-app (app-name sexp) (subst var val (app-arg sexp))))))
Eval-with-one-def • Incorporate function evaluation • Restricted to a single defined function • Process: • Evaluate argument • Substitute argument for param in function body • Evaluate body of function • Contract: • Eval-with-one-def: sexp s-def -> number
Eval-with-one-def (define (eval-with-one-def sexp def1) (cond ((number? sexp) sexp) ((symbol? sexp) (error “undefined symbol”) ((add? sexp) ( + (eval-with-one-def (add-left sexp) def1) (eval-with-one-def (add-right sexp)def1))) ((mul? sexp) (* (eval-with-one-def (mul-left sexp) def1) (eval-with-one-def (mul-right sexp) def1))) ((app? sexp) (if (eq? (app-name sexp) (def-name def1)) (eval-with-one-def (subst (def-arg def1) (eval-with-one-def (app-arg sexp)) (def-body def1)) def1) (error “no such function defined”)))))
Multiple Definitions • Store definitions in list: • A def-list is: • (listof s-def) • Extend evaluator • (eval-with-defs sexp defs) • Contract: • Eval-with-defs: sexp: s-exp, and defs: def-list • Purpose: • Evaluate scheme-expression with multiple possible defs
Eval-with-defs (define (eval-with-one-def sexp defs) (cond ((number? sexp) sexp) ((symbol? sexp) (error “undefined symbol”) ((add? sexp) ( + (eval-with-defs (add-left sexp) defs) (eval-with-defs (add-right sexp)defs))) ((mul? sexp) (* (eval-with-defs (mul-left sexp) defs) (eval-with-defs (mul-right sexp) defs))) ((app? sexp) (let ((defn (lookup-def (app-name sexp) defs))) (if (null? defn) (eval-with-defs (subst (def-arg defn) (eval-with-defs (app-arg sexp)) (def-body defn)) defs) (error “no such function defined”)))))
Lookup-def • Contract: • Lookup-def: symbol def-list -> s-def or '() • Purpose: • Find definition with matching name in def-list
Lookup-def (define (lookup-def name defs) (cond ((null? defs) '()) ((eq? name (def-name (car defs))) (car defs)) (else (lookup-def name (cdr defs)))))
Numeric? • Don't know how to evaluate “defines” • So restrict to pure numeric expressions • No variables • Contract: numeric?: sexp -> boolean • Purpose: Determine if a pure numeric expression
Numeric? (define (numeric? sexp) (cond ((number? sexp) #t) ((symbol? sexp) #f) ((add? sexp) (and (numeric? (add-left sexp)) (numeric? (add-right sexp)))) ((mul? sexp) (and (numeric? (mul-left sexp)) (numeric? (mul-right sexp))))))
Evaluate-Expression • Contract: • Evaluate-expression: sexp -> number • Purpose: • Compute value of a numeric expression
Evaluate-Expression (define (evaluate-expression sexp) (cond ((number? sexp) sexp) ((symbol? sexp) (error “cannot eval var”)) ((add? sexp) (+ (evaluate-expression (add-left sexp)) (evaluate-expression (add-right sexp)))) ((mul? sexp) (* (evaluate-expression (mul-left sexp)) (evaluate-expression (mul-right sexp))))))
Toward Function Application • (define (f x) (* x x)) • Apply: (f 1) => 1 • In application, bind formal parameter to invoking val • E.g. Bind x to 1 • Substitute 1 for all instances of x in scope of function • e.g. (* 1 1) • To build evaluator for functions, • Need substitution
Subst • Contract: • Subst: symbol number sexp -> sexp • Purpose: • Replace all instances of symbol with number in sexp