Download presentation
Presentation is loading. Please wait.
Published byCarmella Lindsay Parks Modified over 8 years ago
1
Meta-Circular Evaluation CMSC 11500 Introduction to Computer Programming November 20, 2002
2
Roadmap ● Recap: Streams ● Meta-circular Evaluation – Using Scheme to evaluate Scheme ● Building representations of scheme expressions – Starting point: Numeric expressions ● Data definitions – Numeric? ● Evaluating expressions ● Toward function application – Substitution
3
Recap: Streams ● Handling infinite data – Mechanism: (delay x) => promise; (force (delay x)) x ● Data definition: – (cons x (delay stream-of-numbers)) ● Template: – (define (fn-for-stream stream) –..(car stream)... (fn-for-stream (force (cdr stream))) ● Posints, filter-stream, map-stream, add-streams,...
4
Evaluators ● DrScheme: – 2 key actions ● Check structure of scheme expressions ● Evaluate scheme expressions ● Develop code to evaluate scheme expressions – In Scheme!! – “Meta-circular evaluation” ● Build evaluator for language in same language
5
Scheme Subset ● Simple arithmetic expressions – e.g. 3, (+ 1 4), (* 7 8), (+ (* 1 2) (+ 3 4)) ● With variables: – e.g. x, (+ 1 x), (* 3 y), (+ (+ 1 2) (*7 x)) ● Primitives: numbers, symbols, variables ● Arithmetic expressions: addition, multiplication – (define-struct add (left right)) – (define-struct mul (left right))
6
Simple Expressions& Representations ● 4 ● x ● (+ 1 2) ● (* 1 x) ● (+ (* 1 2) (+3 4)) ● 4 ● 'x ● (make-add 1 2) ● (make-mul 1 'x) ● (make-add – (make-mul 1 2) – (make-add 3 4))
7
Data Definition: Scheme Expressions ● A scheme-expression (s-exp) is – number – Symbol – (make-add left right) – (make-mul left right) ● Where left, right are s-exp
8
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))...
9
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
10
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))))))
11
Evaluate-Expression ● Contract: – Evaluate-expression: sexp -> number ● Purpose: – Compute value of a numeric expression
12
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))))))
13
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
14
Subst ● Contract: – Subst: symbol number sexp -> sexp ● Purpose: – Replace all instances of symbol with number in sexp
15
Subst (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))))))
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.