1 / 20

Meta-Circular Evaluation: Functions

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

tasya
Download Presentation

Meta-Circular Evaluation: Functions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CMSC 11500 Introduction to Computer Programming November 22, 2002 Meta-Circular Evaluation:Functions

  2. 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

  3. 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)

  4. 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

  5. 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)

  6. 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)

  7. 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)))

  8. 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))))))

  9. 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

  10. 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”)))))

  11. 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

  12. 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”)))))

  13. Lookup-def • Contract: • Lookup-def: symbol def-list -> s-def or '() • Purpose: • Find definition with matching name in def-list

  14. Lookup-def (define (lookup-def name defs) (cond ((null? defs) '()) ((eq? name (def-name (car defs))) (car defs)) (else (lookup-def name (cdr defs)))))

  15. 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

  16. 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))))))

  17. Evaluate-Expression • Contract: • Evaluate-expression: sexp -> number • Purpose: • Compute value of a numeric expression

  18. 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))))))

  19. 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

  20. Subst • Contract: • Subst: symbol number sexp -> sexp • Purpose: • Replace all instances of symbol with number in sexp

More Related