מבוא מורחב למדעי המחשב בשפת Scheme תרגול 11. Metacircular Evaluator 4.1, pages 362-398 definitions file on web 2.

Slides:



Advertisements
Similar presentations
Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is a good way to learn more about programming languages  Interpreters are.
Advertisements

1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
Scheme in Scheme?!? …the metacircular evaluator….
1 The metacircular evaluator Names Extend the calculator to store intermediate results as named values (define x (+ 4 5)) store result as x (+ x.
1 Programming Languages (CS 550) Operational Semantics of Scheme using Substitution and the Lambda Calculus Jeremy R. Johnson TexPoint fonts used in EMF.
Metacircular Evaluation SICP Chapter 4 Mark Boady.
Evaluators for Functional Programming Chapter 4 1 Chapter 4 - Evaluators for Functional Programming.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 10. Environment Model 3.2, pages
1 Programming Languages (CS 550) Lecture 7 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts used in EMF. Read.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 8. Environment Model 3.2, pages
מבוא מורחב למדעי המחשב תרגול 13 Lazy Evaluation. Lazy Evaluation Implementation Two types of values – Delayed Values – Actual Values Introduce two functions.
1 The Metacircular Evaluator Chapter 4 Section 4.1 We will not cover every little detail in the lectures, so you MUST read Section 4.1 in full.
(define applicative-eval (lambda (exp) (cond ((atomic? exp) (eval-atomic exp)) ((special-form? exp) (eval-special-form exp)) ((list-form? exp) (eval-list.
1 Lecture 18 Continue Evaluator. 2 z9 true#t + twice Representing procedures (eval '(define twice (lambda (x) (+ x x))) GE) symbol primitive scheme procedure.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 11. Dotted tail notation (define (proc m1 m2. opt) ) Mandatory Arguments: m1, m2 Mandatory Arguments: m1, m2.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 26: In Praise of Idleness.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
Picking and Choosing… …Amb Above the Line. Intro to Nondeterminism So before we programmed to compute an answer to a problem. What if we set up a system.
SICP Interpretation part 1 Parts of an interpreter Arithmetic calculator Names Conditionals and if Store procedures in the environment.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 21. Overview 1. Dynamic Binding 2. Lazy Evaluation 3. More MC-Eval 2.
Lexical vs. Dynamic Scope …where do I point to?. Intro… Remember in Scheme whenever we call a procedure we pop a frame and point it to where the procedure.
6.001 SICP SICP – Evaluation I Recitation 11/19/2004 Eval review Evaluation examples define lambda apply New language elements.
SICP Variations on a Scheme Scheme Evaluator – A Grand Tour Techniques for language design: Interpretation: eval/apply Semantics vs. syntax Syntactic.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Scheme More MCE examples. Q1 new special form which defines global variables (static ) search the global environment – Variable exists: does nothing,
SICP Variations on a Scheme (2) Beyond Scheme – designing language variants: Lazy evaluation Complete conversion – normal order evaluator Upward.
The environment model evaluator and compiler 1 The env model evaluator Motivation In one word: Efficiency Saves repeated renaming and substitution: Using.
1 Saves repeated renaming and substitution: explicit substitution is replaced by variable bindings using new data structures (frame, environment). Can.
1 Continue Evaluator. 2 z9 true#t + twice Representing procedures (eval '(define twice (lambda (x) (+ x x))) GE) symbol primitive scheme procedure + symbol.
Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is aa good way to learn more about programming languages  Interpreters are.
1 The metacircular evaluator (Cont.) Defining new procedures (define (lambda? e) (tag-check e 'lambda)) (define (eval exp env) (cond ((number? exp)
Scheme in Scheme 1. Why implement Scheme in Scheme  Implementing a language is a good way to learn more about programming languages  Interpreters are.
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
CS220 Programming Principles 프로그래밍의 이해 2002 가을학기 Class 15: Meta-Circular Evaluator 한 태숙.
SICP Interpretation Parts of an interpreter Arithmetic calculator Names Conditionals and if Storing procedures in the environment Environment as.
Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is a good way to learn more about programming languages  Interpreters are.
The environment-based operational semantics Chapter
מבוא מורחב 1 Lecture #9. מבוא מורחב 2 Symbol: a primitive type constructors: (quote alpha) ==> quote is a special form. One argument: a name. selectors.
1 Lecture 20 Lazy Evaluation Continued (4.2.1, 4.2.2) MC-eval examples from exams (Time permitting)
1 Read-Eval-Print Loop (define (driver-loop) (prompt-for-input input-prompt) (let ((input (read))) (let ((output (eval input the-global-env))) (announce-output.
1 Programming Languages (CS 550) Lecture 4 Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
1 Env. Model Implementation & Analyzer Practice Session #10 Env. Model Implementation & Analyzer Practice Session #10.
1 Lecture 19 Dynamic Scoping Lazy Evaluation (4.2.1, 4.2.2)
1/ SICP Variations on a Scheme Scheme Evaluator – A Grand Tour Making the environment model concrete Defining eval defines the language –Provides.
CS 598 Scripting Languages Design and Implementation 10. Interpreters Part I: Lisp.
1 Lecture 19 Review last lecture on evaluator, Dynamic Scoping Lazy Evaluation (4.2.1, 4.2.2)
CS61A Lecture Colleen Lewis 1. Clicker poll Are you in a two person group for project 4? A)Yes – I have my partner B)No – I plan to work.
1 Vectors, binary search, and sorting. 2 We know about lists O(n) time to get the n-th item. Consecutive cons cell are not necessarily consecutive in.
1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language.
Evaluators for Functional Programming 1. How to describe (specify) a programming language? 1.Syntax: atoms, primitives, combination and abstraction means.
SICP Interpretation part 2 Store operators in the environment Environment as explicit parameter Defining new procedures.
Operational Semantics of Scheme
Edited by Original material by Eric Grimson
6.001 SICP Variations on a Scheme
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
Env. Model Implementation
Original material by Eric Grimson
The Metacircular Evaluator
Dynamic Scoping Lazy Evaluation
The Metacircular Evaluator
The Metacircular Evaluator (Continued)
Lecture 26: The Metacircular Evaluator Eval Apply
6.001 SICP Further Variations on a Scheme
Streams, Delayed Evaluation and a Normal Order Interpreter
6.001 SICP Variations on a Scheme
Lecture 14: The environment model (cont
6.001 SICP Interpretation Parts of an interpreter
topics interpreters meta-linguistic abstraction eval and apply
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

מבוא מורחב למדעי המחשב בשפת Scheme תרגול 11

Metacircular Evaluator 4.1, pages definitions file on web 2

Overview 1. Eval, apply 2. Change definition 3. Add let, let* 4. Add special forms and, or 5. Abstraction of frame scanning 3

4 Evaluator: a program that determines meaning of expressions in a programming language. Metacircular: written in the same language that it evaluates

Read-Eval-Print Loop (define (driver-loop) 1. PRINT PROMPT (prompt-for-input input-prompt) 2. READ INPUT EXPRESSION (let ((input (read))) 3. EVALUATE EXPRESSION (let ((output (mc-eval input the-global-environment))) … 4. PRINT RESULT (user-print output) 5. LOOP (driver-loop)))))) 5

Eval-Apply Mutual recursion between eval and apply To evaluate a compound expression means to evaluate the sub-expressions recursively, then apply the operator to the arguments. To apply a function to arguments means to evaluate the body of the function in a new environment. 6

Eval Evaluate expression in given environment (define (mc-eval exp env) (cond ((self-evaluating? exp) exp) … ((application? exp) (mc-apply (mc-eval (operator exp) env) (list-of-values (operands exp) env))) The name mc-eval is used to differ from the primitive eval 7

Apply Apply procedure to list of arguments (define (mc-apply procedure arguments) … (eval-sequence (procedure-body procedure) (extend-environment (procedure-parameters procedure) arguments (procedure-environment procedure)))) The name mc-apply is used to differ from the primitive apply 8

Change definition from (define x 7) to (x := 7) and from (define (square x) (* x x)) to ((square x) := (* x x)) 9

Change eval case from: (define (definition? exp) (tagged-list? exp 'define)) (define (tagged-list? exp tag) (if (pair? exp) (eq? (car exp) tag) false)) to: (define (definition? exp) (and (pair? exp) (pair? (cdr exp)) (eq? (cadr exp) ':=))) 10

Change selectors (define (definition-variable exp) (if (symbol? (cadr exp)) (cadr exp) (caadr exp))) (define (definition-value exp) (if (symbol? (cadr exp)) (caddr exp) (make-lambda (cdadr exp) (cddr exp)))) (car (caar (cdar 11

Adding let, let* Rearranging as another expression 12

let (let (( ) … ( )) ) is equivalent to ((lambda ( … ) ) … ) add a syntactic transformation let->combination 13

let - case in eval ((let? exp) (mc-eval (let->combination exp) env)) (define (let? exp) (tagged-list? exp ‘let)) let - predicate 14

let - constructors (define (make-combination function expressions) (cons function expressions)) 15

let - selectors (define (let-bindings exp) (cadr exp)) (define (let-body exp) (cddr exp)) (define (let-variables exp) (map car (let-bindings exp))) (define (let-expressions exp) (map cadr (let-bindings exp))) 16

let - evaluation (define (let->combination exp) (make-combination (make-lambda (let-variables exp) (let-body exp)) (let-expressions exp))) 17

let* The bindings of are performed sequentially. Each binding is made in an environment in which all of the preceding bindings are visible. (let* ((x 3) (y (+ x 2)) (z (+ x y 5))) (* x z)) > 39 18

let* (let* (( )… ( )) ) is equivalent to (let (( )) (let* (( )… ( )) )) add a syntactic transformation let*->nested-let 19

let* - example (let* (( ) ( )) ) = (let (( )) (let* ( )) )) = (let (( )) (let* () ))) = (let (( )) (let () ))) 20

let* - case in eval ((let*? exp) (mc-eval (let*->nested-let exp) env)) 21

let* - constructors (define (make-let bindings body) (cons ‘let (cons bindings body))) (define (make-let* bindings body) (cons ‘let* (cons bindings body))) 22

let* - predicates (define (let*? exp) (tagged-list exp ‘let*)) (define (no-bindings? bindings) (null? bindings)) 23

let* - selectors (define (let*-bindings exp) (cadr exp)) (define (let*-body exp) (cddr exp)) (define (let*-first-binding bindings) (car bindings)) (define (let*-rest-bindings bindings) (cdr bindings)) 24

let* - evaluation (define (let*->nested-let exp) (if (no-bindings? (let*-bindings exp)) (make-let (let*-bindings exp) (let*-body exp)) (make-let (list (let*-first-binding (let*-bindings exp)) ; ((v1 e1)) (make-let* (let*-rest-bindings (let*-bindings exp)) (let*-body exp)))) 25

Special forms: and, or Direct evaluation 26

and, or - cases in eval (define (mc-eval exp env) (cond ((self-evaluating? exp) exp) … ((and? exp) (eval-and (and-exps exp) env)) ((or? exp) (eval-or (or-exps exp) env)) ((application? exp) (mc-apply (mc-eval (operator exp) env) (list-of-values (operands exp) env))) 27

and, or - predicates (define (and? exp) (tagged-list? exp ‘and)) (define (or? exp) (tagged-list? exp ‘or)) (define (no-exps? exps) (null? exps)) (define (last-exp? exps) (null? (cdr exps))) 28

and, or - selectors (define (and-exps exp) (cdr exp)) (define (or-exps exp) (cdr exp)) (define (first-exp exps) (car exps)) (define (rest-exps exps) (cdr exps)) 29

and - evaluation from left to right EvaluationReturn value Any expression evaluates to false false All expressions evaluate to true value of last expression No expressionstrue

and - evaluation (define (eval-and exps env) (if (no-exps? exps) true (let ((first (mc-eval (first-exp exps) env))) (if (false? first) false (if (last-exp? exps) first (eval-and (rest-exps exps) env))))) 31

or - evaluation from left to right 32 EvaluationReturn value Any expression evaluates to true value of last expression All expressions evaluate to false false No expressionsfalse

or - evaluation (define (eval-or exps env) (if (no-exps? exps) false (let ((first (mc-eval (first-exp exps) env))) (if (true? first) first ;(if (last-exp? exps) false (eval-or (rest-exps exps) env))))) 33

Abstraction of frame scanning Exercise

35 Frames A frame is a set of bindings, represented as a pair of two lists: variables and values Constructor (make-frame variables values) Selectors (frame-variables frame) (frame-values frame) Mutator (add-binding-to-frame! var val frame)

36 Environments An environment consists of its first frame and an enclosing environment Constructor (extend-environment vars vals base-env) Selectors (first-frame env) (enclosing-environment env) Predefined environments the-global-environment the-empty-environment

37 Frame Scanning Performed by: lookup-variable-value invoked when evaluating names set-variable-value! invoked when evaluating set! expressions define-value! invoked when evaluating definitions

38 lookup-variable-value (define (lookup-variable-value var env) (define (env-loop env) (define (scan vars vals) (cond ((null? vars) (env-loop (enclosing-environment env))) ((eq? var (car vars)) (car vals)) (else (scan (cdr vars) (cdr vals))))) (if (eq? env the-empty-environment) (error "Unbound variable" var) (let ((frame (first-frame env))) (scan (frame-variables frame) (frame-values frame))))) (env-loop env))

39 set-variable-value! (define (set-variable-value! var val env) (define (env-loop env) (define (scan vars vals) (cond ((null? vars) (env-loop (enclosing-environment env))) ((eq? var (car vars)) (set-car! vals val)) (else (scan (cdr vars) (cdr vals))))) (if (eq? env the-empty-environment) (error "Unbound variable -- SET!" var) (let ((frame (first-frame env))) (scan (frame-variables frame) (frame-values frame))))) (env-loop env))

40 define-variable! (define (define-variable! var val env) (let ((frame (first-frame env))) (define (scan vars vals) (cond ((null? vars) (add-binding-to-frame! var val frame)) ((eq? var (car vars)) (set-car! vals val)) (else (scan (cdr vars) (cdr vals))))) (scan (frame-variables frame) (frame-values frame))))

41 All procedures are alike! Scan the frame for the variable’s name –If found, success operation (return value / set value) –If not in frame, failure operation (continue to next frame / add binding) Error on empty environment (if this can happen) We can get better abstraction –Capturing common patterns –Hiding frame scanning / mutation

42 Generic frame scan (define (scan-first-frame var env succeed fail) (define (scan vars vals) (cond ((null? vars) (fail)) ((eq? var (car vars)) (succeed vals)) (else (scan (cdr vars) (cdr vals))))) (if (eq? env the-empty-environment) (error "Unbound variable" var) (let ((frame (first-frame env))) (scan (frame-variables frame) (frame-values frame)))))

43 lookup-variable-value (define (lookup-variable-value var env) (define (succeed vals) (get-binding-value vals)) (define (fail) (scan-first-frame var (enclosing-environment env) succeed fail)) (scan-first-frame var env succeed fail)) (define (get-binding-value vals) (car vals))

44 set-variable-value! (define (set-variable-value! var val env) (define (succeed vals) (set-binding-value! vals val)) (define (fail) (scan-first-frame var (enclosing-environment env) succeed fail)) (scan-first-frame var env succeed fail)) (define (set-binding-value! vals val) (set-car! vals val))

45 define-variable! (define (define-variable! var val env) (define (succeed vals) (set-binding-value! vals val)) (define (fail) (add-binding-to-frame! var val (first-frame env))) (scan-first-frame var env succeed fail))