Effect-driven CPS transformation in interpreters

Slides:



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

1 Scheme and Functional Programming Aaron Bloomfield CS 415 Fall 2005.
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.
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.
Higher Order Functions “I hope you’re convinced, by now, that programming languages with first-class functions let you find more opportunities for abstraction,
Metacircular Evaluation SICP Chapter 4 Mark Boady.
Distributed Meta- Programming Rui Shi, Chiyan Chen and Hongwei Xi Boston University.
1 Meta-Programming through Typeful Code Representation Chiyan Chen and Hongwei Xi Boston University.
Introduction to Artificial Intelligence Lisp Ruth Bergman Fall 2002.
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.
Distributed Meta- Programming (To appear GPCE’06) Rui Shi, Chiyan Chen and Hongwei Xi Boston University.
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.
Cs776 (Prasad)L15strm1 Reasoning with Functional Programs Optimization by source to source transformation Well-founded induction Streams : Infinite lists.
LISP 1.5 and beyond A very quick tour. Data Atoms (symbols) including numbers – All types of numbers including Roman! (well, in the early days) – Syntactically.
By Neng-Fa Zhou1 Evolution of programming languages –Machine language –Assembly language –Sub-routines and loop (Fortran) –Procedures and recursion (Algol,
CS 403: Programming Languages Lecture 6 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Plt /17/ Environment-Passing Interpreters Programming Language Essentials 2nd edition Chapter 3.8 Parameter-Passing Variations.
UMBC CMSC Common Lisp II. UMBC CMSC Input and Output Print is the most primitive output function > (print (list 'foo 'bar)) (FOO BAR) The.
Basic Introduction to Lisp
Functional Programming. Some Functional Languages Lisp Scheme - a dialect of Lisp Haskell Miranda.
Environments, Stores, and Interpreters. Overview As we study languages we will build small languages that illustrate language features We will use two.
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.
CS 603: Programming Language Organization Lecture 6 Spring 2003 Department of Computer Science University of Alabama Joel Jones.
SICP Interpretation part 2 Store operators in the environment Environment as explicit parameter Defining new procedures.
Introduction to Functional Programming Part 1 – The Basic Concepts Winter Young
CS314 – Section 5 Recitation 9
Functional Programming
Functional Programming
Additional Scheme examples
6.001 Jeopardy.
6.001 SICP Variations on a Scheme
6.001 SICP Compilation Context: special purpose vs. universal machines
The interpreter.
COP4020 Programming Languages
Original material by Eric Grimson
Nondeterministic Evaluation
Evolution of programming languages
First-class continuations
The Metacircular Evaluator
Objective caml Daniel Jackson MIT Lab for Computer Science 6898: Advanced Topics in Software Design March 18, 2002.
FP Foundations, Scheme In Text: Chapter 14.
CSE 341 Section 7 Winter 2018 Adapted from slides by Eric Mullen, Nicholas Shahan, Dan Grossman, and Tam Dang.
Dynamic Scoping Lazy Evaluation
The Metacircular Evaluator
And more store-passing interpreters
Closure Closure binds a first-class function and a lexical environment together This is a complex topic, so we will build up our understanding of it we.
Abstraction and Repetition
3.4 Local Binding Recall Scheme's let: > (let ((x 5)‏ (y 6))
The Metacircular Evaluator (Continued)
6.001 SICP Further Variations on a Scheme
Extending our interpreted language with Scheme’s set!
Streams, Delayed Evaluation and a Normal Order Interpreter
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
3.6 Interpreter: Recursion
6.001 SICP Variations on a Scheme
Chapter 3: Environment-Passing Interpreters
Environments, Stores, and Interpreters
Transformations of Functions
Closures and Streams cs7100(Prasad) L11Clos
Assignments and Procs w/Params
COP4020 Programming Languages
Common Lisp II.
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
The Python interpreter
Abstraction and Repetition
Presentation transcript:

Effect-driven CPS transformation in interpreters Dries Harnie

Table assignment struct { EXP tab,idx,val; } TAB; VAL tbl_assign(TAB tAB) { VAL tab = eval(tAB->tab); VAL idx = eval(tAB->idx); VAL val = eval(tAB->val); tab[idx] = val; }

Tpico: thunks

Table assignment in Tpico tab_thunk(TAB tAB) { TMP tmp = new tmp; tmp->tAB = tAB; thr_poke(ta1_thunk, tmp); eval(tAB->tab); } ta1_thunk(TMP_TAB tmp, VAL tab) { tmp->tab = tab; thr_poke(ta2_thunk, tmp); eval(tmp->tAB->idx); ta2_thunk(TMP_TAB tmp, VAL idx) { tmp->idx = idx; thr_poke(ta3_thunk, tmp); eval(tmp->tAB->val); } ta3_thunk(TMP_TAB tmp, VAL val) { tmp->val = val; thr_zap(); (tmp->tab)[tmp->idx] = tmp->val;

The perils of garbage collection struct { EXP tab,idx,val; } TAB; VAL tbl_assign(TAB tAB) { VAL tab = eval(tAB->tab); VAL idx = eval(tAB->idx); VAL val = eval(tAB->val); tab[idx] = val; } GC can happen at any of these points!

Thunkmaster Looks like Scheme: (define (map f l) (case l ((cons x xs) (cons (f x) (map f xs))) ((nil) (nil)))) (define (fac x) (if (< x 1) 1 (* x (fac (- x 1))))) (define (eval-tbl-assign TAB) (let* ((tab (eval (get-tab TAB))) (idx (eval (get-idx TAB))) (val (eval (get-val TAB)))) (set-tab! tab idx val))) (define (thr-loop val) (let ((thk (current-thunk))) (thr-loop (thk val))))

Thunkmaster Has effects: (define (map f l) (case l ((cons x xs) (cons (f x) (map f xs))) ((nil) (nil)))) Effects associate computations with side effects. If f ! print, then (map f l) ! print.

Thunkifying code (define (eval-tbl-assign TAB) (let* ((tab (eval (get-tab TAB))) (idx (eval (get-idx TAB))) (val (eval (get-val TAB)))) (set-tab! tab idx val))) (define (eval-tbl-assign TAB) (eval (get-tab TAB) (lambda (tab) (eval (get-idx TAB) (lambda (idx) (eval (get-val TAB) (lambda (val) (set-tab! tab idx val))))))))

Thunkifying code (define (eval-tbl-assign TAB) (eval (get-tab TAB) (lambda (tab) (eval (get-idx TAB) (lambda (idx) (eval (get-val TAB) (lambda (val) (set-tab! tab idx val)))))))) (define (eval-tbl-assign TAB) (defstruct (tmp tab idx)) (define (ta1 tmp tab) (thr-poke! ta2 (tmp-set-tab! Tmp tab)) (eval (get-idx TAB))) (define (ta2 tmp idx) (thr-poke! ta3 (tmp-set-idx! Tmp idx)) (eval (get-val TAB))) (define (ta3 tmp val) (thr-zap!) (set-tab! (tmp-get-tab tmp) (tmp-get-idx tmp) val))) (thr-poke! ta1 (new tmp)) (eval (get-tab TAB)))

Thunkifying code (defstruct (tmp TAB tab idx)) (define (ta1 tmp tab) (thr-poke! ta2 (tmp-set-tab! Tmp tab)) (eval (get-idx (tmp-get-TAB tmp)))) (define (ta2 tmp idx) (thr-poke! ta3 (tmp-set-idx! Tmp idx)) (eval (get-val (tmp-get-TAB tmp)))) (define (ta3 tmp val) (thr-zap!) (set-tab! (tmp-get-tab tmp) (tmp-get-idx tmp) val))) (define (eval-tbl-assign TAB) (thr-poke! ta1 (tmp-set-TAB! (new tmp) TAB)) (eval (get-tab TAB)))

Thesis: goals Use effect system to determine where CPS is needed. Find other places where CPS could help Tpico.