Extending our interpreted language with Scheme’s set!

Slides:



Advertisements
Similar presentations
Higher-Order Functions and Loops c. Kathi Fisler,
Advertisements

Closures & Environments CS153: Compilers Greg Morrisett.
Compiling Web Scripts for Apache Jacob Matthews Luke Hoban Robby Findler Rice University.
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is a good way to learn more about programming languages  Interpreters are.
Assignments and Procs w/Params EOPL3 Chapter 4. Expressible vs. Denotable values Expressible Values –the language can express and compute these –represented.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
3.6 Interpreter: Recursion Recall Scheme's letrec (recursive let ): > (letrec ((fact (lambda (x) (if (zero? x) 1 (* x (fact (- x 1))))) (fact 6)) 720 Question:
1 Scheme Scheme is a functional language. Scheme is based on lambda calculus. lambda abstraction = function definition In Scheme, a function is defined.
Assemblers Dr. Monther Aldwairi 10/21/20071Dr. Monther Aldwairi.
6.001 SICP SICP – Evaluation I Recitation 11/19/2004 Eval review Evaluation examples define lambda apply New language elements.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
CEG 320/520: Computer Organization and Assembly Language Programming1 Assembly Language Programming Assembler Directives and The Symbol Table.
Cs784(Prasad)L123Assg1 Assignments. cs784(Prasad)L123Assg2 l-value vs. r-value Pascal/Ada: x := x + 1 C/Java: x = x + 1 l-value = location, address, reference,
MrFlow: Why MrSpidey Failed Philippe Meunier Paul Steckler.
PrasadL145OOL1 Managing Environments An Exercise in the Design, Analysis, Specification, and Implementation of the Core of an OOP Language. Object-Oriented.
Plt /12/ Data Abstraction Programming Language Essentials 2nd edition Chapter 2.3 Representation Strategies for Data Types.
3.5 Procedures Recall procedures (functions) in Scheme: (let ((f (lambda(y z) (+ y (- z 5))) (f 2 28)) We would like something similar in our toy language:
SICP Interpretation Parts of an interpreter Arithmetic calculator Names Conditionals and if Storing procedures in the environment Environment as.
Dr. Philip Cannata 1 Functions and Recursion. Dr. Philip Cannata 2 10 Java (Object Oriented) ASP RDF (Horn Clause Deduction, Semantic Web) Relation Jython.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 18: Think Globally, Mutate Locally.
Plt /17/ Environment-Passing Interpreters Programming Language Essentials 2nd edition Chapter 3.8 Parameter-Passing Variations.
1 Programming Languages (CS 550) Lecture 4 Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
1 Programming Languages (CS 550) Lecture 2 Summary Mini Language Interpreter Jeremy R. Johnson.
Scheme in Scheme 2. What’s next  Adding set!  Dynamic vs. lexical variable scope  Extending mcscheme v1 with libraries  Can mcscheme execute mcscheme?
CS 152: Programming Language Paradigms April 7 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
Environments, Stores, and Interpreters. Overview As we study languages we will build small languages that illustrate language features We will use two.
SICP Interpretation part 2 Store operators in the environment Environment as explicit parameter Defining new procedures.
Operational Semantics of Scheme
Lecture 4: Metacircles Eval Apply David Evans
Abstract Syntax cs7100 (Prasad) L7AST.
6.001 Jeopardy.
6.001 SICP Variations on a Scheme
6.001 SICP Object Oriented Programming
The interpreter.
Introduction to Scheme
The Environment Model*
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
On the Structure of Interpreters
Implementing Recursion
Closure conversion Compiling λ.
SSA in Scheme Static single assignment (SSA) :
زبان بدن Body Language.
First-class continuations
2.3 Representation Strategies for Data Types
Mini Language Interpreter Programming Languages (CS 550)
Computer Science 210 Computer Organization
The Metacircular Evaluator
FP Foundations, Scheme In Text: Chapter 14.
The Metacircular Evaluator
Abstract machines & interpreters
And more store-passing interpreters
3.7 Variable Assignment Recall instance variables in Python:
3.4 Local Binding Recall Scheme's let: > (let ((x 5)‏ (y 6))
6.001 SICP Further Variations on a Scheme
Streams, Delayed Evaluation and a Normal Order Interpreter
Abstract Syntax cs7100 (Prasad) L7AST.
3.6 Interpreter: Recursion
6.001 SICP Variations on a Scheme
Chapter 3: Environment-Passing Interpreters
Environments, Stores, and Interpreters
6.001 SICP Interpretation Parts of an interpreter
Symbol Table 薛智文 (textbook ch#2.7 and 6.5) 薛智文 96 Spring.
Assignments and Procs w/Params
Social Practice of the language: Describe and share information
Recursive Procedures and Scopes
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
Presentation transcript:

Extending our interpreted language with Scheme’s set! Adding Mutation Extending our interpreted language with Scheme’s set!

(define (sum from to) (define total 0) (let loop () (set! total (+ total from)) (set! from (+ from 1)) (if (<= from to) (loop) total)))

(define (sum from to) (begin (define total 0) (let loop () (set! total (+ total from)) (set! from (+ from 1)) (if (<= from to) (loop) total)))))

(let loop ([x ivx] [y ivy] …) body) (letrec ([loop (lambda (x y …) body)]) (loop ivx ivy …))

(define (sum from to) (define total 0) (let loop () (set! total (+ total from)) (set! from (+ from 1)) (if (<= from to) (loop) total)))

(define (sum from to) (let loop ([i from] [total 0]) (if (<= i to) (loop (+ i 1) (+ total i)) total)))

(e0, env) ⇓ ((λ (x) e2), env’) (e1, env) ⇓ v1 (e2, env’[x ↦ v1]) ⇓ v2 ((e0 e1), env) ⇓ v2 ((λ (x) e), env) ⇓ ((λ (x) e), env) (x, env) ⇓ env(x)

env maps variables to addresses (x, env, st) env maps variables to addresses st maps addresses to values The current size of the store is the next address: | st |

(e2, env’[x ↦ |st2|], st2[|st2| ↦ v1]) ⇓ (v2, st3) (e0, env, st0) ⇓ (((λ (x) e2), env’), st1) (e1, env, st1) ⇓ (v1, st2) ((e0 e1), env, st0) ⇓ (v2, st3) ((λ (x) e), env, st) ⇓ (((λ (x) e), env), st) (x, env, st) ⇓ (st(env(x)), st)

Store-passing style

(define (interp e env) (match e [(? symbol? x) (hash-ref env x)] [`(λ (,x) ,e0) `(clo (λ (,x) ,e0) ,env)] [`(,e0 ,e1) (define v0 (interp e0 env)) (define v1 (interp e1 env)) (match v0 [`(clo (λ (,x) ,e2) ,env) (interp e2 (hash-set env x v1))])]))

(define (interp e env st) (match e [(? symbol? x) (hash-ref env x)] [`(λ (,x) ,e0) `(clo (λ (,x) ,e0) ,env)] [`(,e0 ,e1) (define v0 (interp e0 env)) (define v1 (interp e1 env)) (match v0 [`(clo (λ (,x) ,e2) ,env) (interp e2 (hash-set env x v1))])]))

(define (interp e env st) (match e [(? symbol? x) (cons (hash-ref st (hash-ref env x)) st)] [`(λ (,x) ,e0) `(clo (λ (,x) ,e0) ,env)] [`(,e0 ,e1) (define v0 (interp e0 env)) (define v1 (interp e1 env)) (match v0 [`(clo (λ (,x) ,e2) ,env) (interp e2 (hash-set env x v1))])]))

(define (interp e env st) (match e [(? symbol? x) (cons (hash-ref st (hash-ref env x)) st)] [`(λ (,x) ,e0) (cons `(clo (λ (,x) ,e0) ,env) st)] [`(,e0 ,e1) (define v0 (interp e0 env)) (define v1 (interp e1 env)) (match v0 [`(clo (λ (,x) ,e2) ,env) (interp e2 (hash-set env x v1))])]))

(e2, env’[x ↦ |st2|], st2[|st2| ↦ v1]) ⇓ (v2, st3) (e0, env, st0) ⇓ (((λ (x) e2), env’), st1) (e1, env, st1) ⇓ (v1, st2) ((e0 e1), env, st0) ⇓ (v2, st3) ((λ (x) e), env, st) ⇓ (((λ (x) e), env), st) (x, env, st) ⇓ (st(env(x)), st)

(define (interp e env st) (match e [(? symbol? x) (cons (hash-ref st (hash-ref env x)) st)] [`(λ (,x) ,e0) (cons `(clo (λ (,x) ,e0) ,env) st)] [`(,e0 ,e1) (match-define (cons v0 st1) (interp e0 env st)) (match-define (cons v1 st2) (interp e1 env st1)) (match v0 [`(clo (λ (,x) ,e2) ,env) (interp e2 (hash-set env x v1))])]))

(define (interp e env st) (match e [(? symbol? x) (cons (hash-ref st (hash-ref env x)) st)] [`(λ (,x) ,e0) (cons `(clo (λ (,x) ,e0) ,env) st)] [`(,e0 ,e1) (match-define (cons v0 st1) (interp e0 env st)) (match-define (cons v1 st2) (interp e1 env st1)) (match v0 [`(clo (λ (,x) ,e2) ,env) (define addr (hash-count st2)) (interp e2 (hash-set env x addr) (hash-set st2 adds v1))])]))

Let’s code this up.