Defining New Special Forms (unless test body) (if (not test) body #f) (when (not test) body)

Slides:



Advertisements
Similar presentations
Closures & Environments CS153: Compilers Greg Morrisett.
Advertisements

Variables, Environments and Closures. Overview We will Touch on the notions of variable extent and scope Introduce the notions of lexical scope and.
G make_counter params: body: count = 0 def counter():... my_counter params: body: count += 1... E1 count0 counter E2E2.
Recap 1.Programmer enters expression 2.ML checks if expression is “well-typed” Using a precise set of rules, ML tries to find a unique type for the expression.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
1 The metacircular evaluator Names Extend the calculator to store intermediate results as named values (define x (+ 4 5)) store result as x (+ x.
Fall 2008Programming Development Techniques 1 Topic 2 Scheme and Procedures and Processes September 2008.
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.
1 Scheme Scheme is a functional language. Scheme is based on lambda calculus. lambda abstraction = function definition In Scheme, a function is defined.
6.001 SICP SICP – October environment Trevor Darrell 32-D512 Office Hour: W web page:
6.001 SICP 1 Normal (Lazy) Order Evaluation Memoization Streams.
CS 312 Spring 2004 Lecture 18 Environment Model. Substitution Model Represents computation as doing substitutions for bound variables at reduction of.
CSE S. Tanimoto Macros 1 Defining Macros in Lisp Extensibility: A language is extensible if the language can be extended. New Lisp control structures.
CS 312 Spring 2002 Lecture 16 The Environment Model.
The BRENDA interpreter A interpreter for a subset of Dylan written in Dylan.
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.
1 Scheme Although Scheme is syntactically a simple language, it supports sophisticated modeling: higher-order functions are a powerful tool in describing.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Administrivia Project 3 Part A due 3/29 (Monday after SB) Part B due 4/5 (a week after)  Everyone with a partner that wants a partner?  Extra Office.
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.
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
Streams and Lazy Evaluation A signal processing view of computation CD player DA con- verter ampspeakerdigital signal audio signal amplified audio signal.
CSE S. Tanimoto Lisp Defining Macros in Lisp Extensibility: A language is extensible if the language can be extended. New Lisp control structures.
The environment-based operational semantics Chapter
CSE 230 The -Calculus. Background Developed in 1930’s by Alonzo Church Studied in logic and computer science Test bed for procedural and functional PLs.
Variables, Environments and Closures. Overview Touch on the notions of variable extent and scope Introduce the notions of lexical scope and dynamic.
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 Env. Model Implementation & Analyzer Practice Session #10 Env. Model Implementation & Analyzer Practice Session #10.
Fall 2008Programming Development Techniques 1 Topic 17 Assignment, Local State, and the Environment Model of Evaluation Section 3.1 & 3.2.
1 COSC generating functions, templates, and macros Yves Lespérance Adapted from Peter Roosen-Runge.
1 Lecture 19 Dynamic Scoping Lazy Evaluation (4.2.1, 4.2.2)
Macros and general code walkers in Lisp: how useful! or, how useful? Ernst van Waning
PS4 #6: if you got points off because you didn’t do an intersect with accessible and the start states, submit a re- grade. Today: let, let*, letrec in.
The Environment Model an extension of the substitution model more "operational" fully explains static scoping and the process by which variable names are.
Procedure Definitions and Semantics Procedures support control abstraction in programming languages. In most programming languages, a procedure is defined.
Operational Semantics of Scheme
Lecture 4: Metacircles Eval Apply David Evans
Mapping Methods With Arguments
Defining Macros in Lisp
6.001 SICP Variations on a Scheme
The interpreter.
The Environment Model*
Env. Model Implementation
Important Concepts from Clojure
Important Concepts from Clojure
Variables, Environments and Closures

Dynamic Scoping Lazy Evaluation
3.4 Local Binding Recall Scheme's let: > (let ((x 5)‏ (y 6))
Lecture 26: The Metacircular Evaluator Eval Apply
6.001 SICP Further Variations on a Scheme
Streams, Delayed Evaluation and a Normal Order Interpreter
Lecture 13 - Assignment and the environments model Chapter 3
BOGGLE Create as many words as you can using the letters given.
341 midterm review Xinrong Zhao Autumn 2018.
6.001 SICP Variations on a Scheme
Defining Macros in Lisp
Important Concepts from Clojure
6.001 SICP Interpretation Parts of an interpreter
topics interpreters meta-linguistic abstraction eval and apply
Algebra Substitution a = 3 ; b = 4 and c = -1 2a + c b2 – a2 2b2
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
Rules of evaluation The value of a number is itself.
Defining Macros in Scheme
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

Defining New Special Forms (unless test body) (if (not test) body #f) (when (not test) body)

(inc! x) (set! x (inc x))

(define unless (method ((test ) (body )) (if (not test) body #f))) (define inc! (method ((x )) (set! x (inc x)))) doesn't work!

Macros Ordinary functions 1. Evaluate the arguments 2. Apply function to (values of) the arguments 3. Return the result of step 2 Macros 1. Substitute the (unevaluated) args textually in the body 2. Evaluate the result 3. Return the result of step 2.

class special form macro (macro (params) pre-body) 1. The pre-body is evaluated twice : once to do the textual substitution and once to compute the value. 2. The first evaluation is done in the environment of the closure, the second is done in the environment of the call.

Want (inc! z) to expand to (set! z (inc z)) (define (inc! ) (macro (x) (list 'set! x (list 'inc x))))

(define (inc! ) (macro (x) (list 'set! x (list 'inc x)))) pre-body

1. Evaluate the pre-body in the environment of the closure with params bound to unevaluated arguments 2. Evaluate the resulting expression in the environment of the macro call.

(define (inc! ) (macro (x) (list 'set! x (list 'inc x)))) (define z 3) => 3 (inc! z) => (set! z (inc z)) => 4 z => 4

(define (unless ) (macro (tst bdy) (list 'if (list 'not tst) bdy '#f))) (define x 2) => 2 (unless (= x 0) (/ 2 x)) => (if (not (= x 0)) (/ 2 x) #f) => 1

EVALUATION (define-generic-function brenda-eval ((obj ) (env ))) (add-method brenda-eval (method ((obj ) (env )) obj)) (add-method brenda-eval (method ((obj ) (env )) (lookup obj env))) (add-method brenda-eval (method ((obj ) (env )) (brenda-apply (brenda-eval (head obj) env) (tail obj) env)))

APPLICATION (define-generic-function brenda-apply ((obj ) (args ) (env ))) (add-method brenda-apply (method ((obj ) (args ) (env )) ((calls obj) args env))) (add-method brenda-apply (method ((obj ) (args ) (e )) (bind (((evaled-args ) (map (method (x) (brenda-eval x e)) args))) (when (args-params-match? (params obj) evaled-args) (bind ((new-env (expand-environment (params obj) evaled-args (env obj)))) (eval-sequence (body obj) new-env))))))

MACROS (add-method brenda-apply (method ((obj ) (args ) (e )) (when (args-params-match? (params obj) args) (bind ((new-env (expand-environment (params obj) args (env obj)))) (brenda-eval (eval-sequence (body obj) new-env) e)))))