Continuation Marks A john clements talk.

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

Current Techniques in Language-based Security David Walker COS 597B With slides stolen from: Steve Zdancewic University of Pennsylvania.
1 Mooly Sagiv and Greta Yorsh School of Computer Science Tel-Aviv University Modern Compiler Design.
1 Compiler Construction Intermediate Code Generation.
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.
Lambda Calculus and Lisp PZ03J. Lambda Calculus The lambda calculus is a model for functional programming like Turing machines are models for imperative.
Metacircular Evaluation SICP Chapter 4 Mark Boady.
Cs784(TK)1 Semantics of Procedures and Scopes. Kinds of Scope Static or Lexical scope –determined by structure of program –Scheme, C++, Java, and many.
(define applicative-eval (lambda (exp) (cond ((atomic? exp) (eval-atomic exp)) ((special-form? exp) (eval-special-form exp)) ((list-form? exp) (eval-list.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
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.
CS 152: Programming Language Paradigms February 24 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University
Cs7100(Prasad)L8Proc1 Procedures. cs7100(Prasad)L8Proc2 Primitive procedures  etc User-defined procedures –Naming a sequence of operations.
Plt /12/ Data Abstraction Programming Language Essentials 2nd edition Chapter 2.3 Representation Strategies for Data Types.
1 Names, Scopes and Bindings Aaron Bloomfield CS 415 Fall
Introduction to Scheme Lectures on The Scheme Programming Language, 2 nd Ed. R. Kent Dybvig.
A Theory of Hygienic Macros David Herman, Mitchell Wand Northeastern University.
OOP and Dynamic Method Binding Chapter 9. Object Oriented Programming Skipping most of this chapter Focus on 9.4, Dynamic method binding – Polymorphism.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language.
CS535 Programming Languages Chapter - 10 Functional Programming With Lists.
1 Env. Model Implementation & Analyzer Practice Session #10 Env. Model Implementation & Analyzer Practice Session #10.
Advanced Functional Programming Tim Sheard 1 Lecture 17 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture:
COMP 412, FALL Type Systems II C OMP 412 Rice University Houston, Texas Fall 2000 Copyright 2000, Robert Cartwright, all rights reserved. Students.
Debuggers. Errors in Computer Code Errors in computer programs are commonly known as bugs. Three types of errors in computer programs –Syntax errors –Runtime.
Operational Semantics of Scheme
Abstract Syntax cs7100 (Prasad) L7AST.
Edited by Original material by Eric Grimson
Compiler Construction (CS-636)
CSE341: Programming Languages Lecture 15 Macros
6.001 SICP Compilation Context: special purpose vs. universal machines
Introduction to Scheme
Semantic Analysis with Emphasis on Name Analysis
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Graph-Based Operational Semantics
Implementing Recursion
Env. Model Implementation
CSE341: Programming Languages Lecture 15 Macros
First-class continuations
The Metacircular Evaluator
Programming Languages and Compilers (CS 421)
Abstract Syntax Prabhaker Mateti 1.
CSE 341 Section 7 Winter 2018 Adapted from slides by Eric Mullen, Nicholas Shahan, Dan Grossman, and Tam Dang.
The Metacircular Evaluator
Implementing Subprograms
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.
CSE341: Programming Languages Lecture 15 Macros
Lecture 16: Tables and OOP
The Metacircular Evaluator (Continued)
Streams, Delayed Evaluation and a Normal Order Interpreter
Abstract Syntax cs7100 (Prasad) L7AST.
Adapted from slides by Nicholas Shahan and Dan Grossman
Lecture 12: Message passing The Environment Model
3.6 Interpreter: Recursion
6.001 SICP Variations on a Scheme
CSE341: Programming Languages Lecture 15 Macros
CSE341: Programming Languages Lecture 15 Macros
Assignments and Procs w/Params
COMPILERS Semantic Analysis
topics interpreters meta-linguistic abstraction eval and apply
Recursive Procedures and Scopes
More Scheme CS 331.
Defining Macros in Scheme
CSE341: Programming Languages Lecture 15 Macros
CSE341: Programming Languages Lecture 15 Macros
Presentation transcript:

Continuation Marks A john clements talk

What are Continuation Marks?

What are Continuation Marks? What are Continuations?

What are Continuation Marks? What are Continuations? Continuations Paren-hackers Contexts Stacks Compiler-writers Semanticists

Contexts Stacks Compiler-writers Semanticists (let*-2vals ([kept-vars (cond [(eq? tail-bound 'all) free-vars] [else (filter (lambda (varref) (ormap (lambda (binding) (bound-identifier=? binding varref)) tail-bound)) free-vars)])] [var-clauses (map (lambda (x) (list x (d->so `(quote-syntax ,x)))) kept-vars)] [let-bindings (filter (lambda (var) (case (syntax-property var 'stepper-binding-type) ((let-bound) #t) ((lambda-bound stepper-temp top-level) #f) (else (error 'make-debug-info "varref ~a's binding-type info \ was not recognized: ~a" (syntax-e var) (syntax-property var 'stepper-binding-type))))) [lifter-syms (map get-lifted-var let-bindings)] [quoted-lifter-syms (map (lambda (b) (d->so `(quote-syntax ,b))) lifter-syms)] [let-clauses (map list lifter-syms quoted-lifter-syms)]) (make-full-mark source label (append var-clauses (if lifting? let-clauses null))))

Contexts Stacks Compiler-writers Semanticists mark mark mark (let*-2vals ([kept-vars (cond [(eq? tail-bound 'all) free-vars] [else (filter (lambda (varref) (ormap (lambda (binding) (bound-identifier=? binding varref)) tail-bound)) free-vars)])] [var-clauses (map (lambda (x) (list x (d->so `(quote-syntax ,x)))) kept-vars)] [let-bindings (filter (lambda (var) (case (syntax-property var 'stepper-binding-type) ((let-bound) #t) ((lambda-bound stepper-temp top-level) #f) (else (error 'make-debug-info "varref ~a's binding-type info \ was not recognized: ~a" (syntax-e var) (syntax-property var 'stepper-binding-type))))) [lifter-syms (map get-lifted-var let-bindings)] [quoted-lifter-syms (map (lambda (b) (d->so `(quote-syntax ,b))) lifter-syms)] [let-clauses (map list lifter-syms quoted-lifter-syms)]) (make-full-mark source label (append var-clauses (if lifting? let-clauses null)))) mark mark mark

Closing the Loop Programming Language Marks

Closing the Loop Programming Language with marks

Closing the Loop oops! Programming Language with marks

Closing the Loop Programming Language with marks

Contexts Stacks Compiler-writers Semanticists mark mark mark (let*-2vals ([kept-vars (cond [(eq? tail-bound 'all) free-vars] [else (filter (lambda (varref) (ormap (lambda (binding) (bound-identifier=? binding varref)) tail-bound)) free-vars)])] [var-clauses (map (lambda (x) (list x (d->so `(quote-syntax ,x)))) kept-vars)] [let-bindings (filter (lambda (var) (case (syntax-property var 'stepper-binding-type) ((let-bound) #t) ((lambda-bound stepper-temp top-level) #f) (else (error 'make-debug-info "varref ~a's binding-type info \ was not recognized: ~a" (syntax-e var) (syntax-property var 'stepper-binding-type))))) [lifter-syms (map get-lifted-var let-bindings)] [quoted-lifter-syms (map (lambda (b) (d->so `(quote-syntax ,b))) lifter-syms)] [let-clauses (map list lifter-syms quoted-lifter-syms)]) (make-full-mark source label (append var-clauses (if lifting? let-clauses null)))) mark mark mark

Contexts Stacks Compiler-writers Semanticists (let*-2vals ([kept-vars (cond [(eq? tail-bound 'all) free-vars] [else (filter (lambda (varref) (ormap (lambda (binding) (bound-identifier=? binding varref)) tail-bound)) free-vars)])] [var-clauses (map (lambda (x) (list x (d->so `(quote-syntax ,x)))) kept-vars)] [let-bindings (filter (lambda (var) (case (syntax-property var 'stepper-binding-type) ((let-bound) #t) ((lambda-bound stepper-temp top-level) #f) (else (error 'make-debug-info "varref ~a's binding-type info \ was not recognized: ~a" (syntax-e var) (syntax-property var 'stepper-binding-type))))) [lifter-syms (map get-lifted-var let-bindings)] [quoted-lifter-syms (map (lambda (b) (d->so `(quote-syntax ,b))) lifter-syms)] [let-clauses (map list lifter-syms quoted-lifter-syms)]) (make-full-mark source label (append var-clauses (if lifting? let-clauses null))))

New Language Forms

New Language Forms Introducing marks: Observing marks: (with-continuation-mark M M) mark value body (current-continuation-marks)

New Language Forms Synopsis of Reduction Semantics: New Contexts: E = [] | (E M) | (V E) | … | (w-c-m V E) * *With restrictions on successive w-c-m’s New Reductions: E[(w-c-m V V)] -> E[V] E[(w-c-m V (w-c-m M M))] -> E[(w-c-m M M)] E[(c-c-m)] -> E[X(E)]

New Language Forms (define (loop x) (w-c-m x (loop (+ x 1)))) (w-c-m 0 (loop (+ 0 1))) -> (w-c-m 0 (loop 1)) -> (w-c-m 0 (w-c-m 1 (loop (+ 1 1)))) -> (w-c-m 1 (loop (+ 1 1))) -> (w-c-m 1 (loop 2)) -> (w-c-m 1 (w-c-m 2 (loop (+ 2 1)))) -> ...

New Language Forms (define (f x) (w-c-m x (+ (f x) 1))) (loop 0) -> (w-c-m 0 (+ (f 0) 1)) -> (w-c-m 0 (+ (w-c-m 0 (+ (f 0) 1)) 1)) -> (w-c-m 0 (+ (w-c-m 0 (+ (w-c-m 0 (+ (f 0) 1)) 1)) 1)) -> (w-c-m 0 (+ (w-c-m 0 (+ (w-c-m 0 (+ (w-c-m 0 (+ (f 0) 1)) 1)) 1)) 1)) -> ...

Expressiveness Conjecture: cannot be expressed in a “tail-call preserving way” by state, continuations, etc. Difficult to state claim clearly

Using Continuation Marks (to build a stepper)

(using continuation marks) Building a Stepper (using continuation marks)

Building a Stepper Why a Stepper? How to build a stepper? A stepper is a sound debugger A stepper is a complete debugger … with a suitable proof How to build a stepper? Annotation with continuation marks Non-invasive (well-defined) implementation

Building a Stepper Annotation: Marks must contain: Breakpoints Source position Values of free variables Breakpoints Where? Everywhere. No, even more than that.

Building a Stepper Pragmatics

Pragmatics Orthogonality of annotations Add a ‘key’ to each mark: Marks with different keys are orthogonal (with-continuation-mark M M M) key mark body (current-continuation-marks M)

Macro Reversal (cond [a b] [c d]) ?? R? R (if c d (error)) (if a b

“Needed” variables Variables stored in each mark Avoid duplication Store all variables which are not guaranteed to occur in an enclosing mark.

“Needed” variables Variable “a” is not needed in these contexts (letrec ([a 13] [b (+ a 14)] [c (lambda () a)]) (g a) Variable “a” is needed in these contexts

Opaque Values Closures: procedures, classes Structures, Units Put them in a table

Before & After Steps (+ 1 (if true 3 4)) -> (+ 1 3) Requires additional breakpoints for: • tail-position expressions • results of primitive applications

Unannotated Code Libraries Partial Annotation Complete Reconstruction not possible Detection necessary

Lifting & Renaming Newly created closures go to top level Names must be assigned at runtime Extra binding created

Lightweight Inspection (what else can we do with continuation marks?)

Stack Inspection Widely used security measure Associate privileges with ‘principals’ Inspect the stack to make guarantees about execution Implemented in a variety of ways: Stack Inspection “Security-passing style” ...

Stack Inspection … or using continuation marks Preserves tail-call optimization Place marks on the “bad” code T T G G OLD NEW

Aspect-Oriented Programming Allows textual separation of program aspects. May be implemented by a static annotation … with certain run-time decisions “cflow” rules in AspectJ But what about tail-call optimization? May be recovered with continuation marks

Lightweight Inspection Other uses waiting to be found…

Lightweight Inspection Other uses waiting to be found… Tell me yours!