Konzepte von Programmiersprachen

Similar documents
Procedures. EOPL3: Section 3.3 PROC and App B: SLLGEN

Lecture 3: Expressions

Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs. Lexical addressing

Parsing Scheme (+ (* 2 3) 1) * 1

Homework 3 COSE212, Fall 2018

Continuations and Continuation-Passing Style

1. For every evaluation of a variable var, the variable is bound.

Deferred operations. Continuations Structure and Interpretation of Computer Programs. Tail recursion in action.

Functional Programming. Pure Functional Programming

UNIT 3

CS 342 Lecture 8 Data Abstraction By: Hridesh Rajan

Comp 411 Principles of Programming Languages Lecture 7 Meta-interpreters. Corky Cartwright January 26, 2018

Register Machines. Connecting evaluators to low level machine code

Control in Sequential Languages

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

9 Objects and Classes

CVO103: Programming Languages. Lecture 5 Design and Implementation of PLs (1) Expressions

G Programming Languages - Fall 2012

Tail calls. Tail calls. (and their elimination) Functional loops. The problem. Advanced Compiler Construction Michel Schinz

Tail calls. Advanced Compiler Construction Michel Schinz

An Explicit-Continuation Metacircular Evaluator

First-class continuations. call/cc, stack-passing CEK machines

regsim.scm ~/umb/cs450/ch5.base/ 1 11/11/13

CMSC 330: Organization of Programming Languages

CS422 - Programming Language Design

Lecture 09: Data Abstraction ++ Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree.

Comp 311: Sample Midterm Examination

CS 342 Lecture 7 Syntax Abstraction By: Hridesh Rajan

Compilers: The goal. Safe. e : ASM

An Introduction to Scheme

Certified Memory Usage Analysis

Optimizing Scheme, part I

6.184 Lecture 4. Interpretation. Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson

Principles of Programming Languages

CSE341, Spring 2013, Final Examination June 13, 2013

Programming Languages and Compilers (CS 421)

Code example: sfact SICP Explicit-control evaluator. Example register machine: instructions. Goal: a tail-recursive evaluator.

Lecture08: Scope and Lexical Address

The role of semantic analysis in a compiler

n What is its running time? 9/18/17 2 n poor_rev [1,2,3] = n (poor_rev [1] = n ((poor_rev [1] =

An Elegant Weapon for a More Civilized Age

References and Exceptions. CS 565 Lecture 14 4/1/08

INF 212 ANALYSIS OF PROG. LANGS FUNCTION COMPOSITION. Instructors: Crista Lopes Copyright Instructors.

Lecture 5: Declarative Programming. The Declarative Kernel Language Machine. September 12th, 2011

CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures. Dan Grossman Autumn 2018

CS4215 Programming Language Implementation. Martin Henz

CS422 - Programming Language Design

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

Implementing Recursion

Lexical Addresses. let x = 1 y = 2 in let f = proc (x) +(x, y) in (f x) let _ = 1 _ = 2 in let _ = proc (_) +(<0,0>, <1,1>) in (<0,0> <1,0>)

CS558 Programming Languages

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

Call with Current Continuation Patterns

From Syntactic Sugar to the Syntactic Meth Lab:

Chapter 1. Fundamentals of Higher Order Programming

6.001, Spring Semester, 1998, Final Exam Solutions Your Name: 2 Part c: The procedure eval-until: (define (eval-until exp env) (let ((return (eval-seq

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

Programming Languages and Compilers (CS 421)

Supplementary Notes on Exceptions

Continuation Passing Style. Continuation Passing Style

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far

CS251 Programming Languages Handout # 36 Prof. Lyn Turbak April 4, 2007 Wellesley College Revised April 14, Scoping in Hofl

Verified compilation of a first-order Lisp language

Lecture 16: Object Programming Languages

A brief tour of history

TAIL RECURSION, SCOPE, AND PROJECT 4 11

Lecture 6: The Declarative Kernel Language Machine. September 13th, 2011

Agenda. CS301 Session 9. Abstract syntax: top level. Abstract syntax: expressions. The uscheme interpreter. Approaches to solving the homework problem

Iterators. Lecture 24: Iterators & Exceptions. Implementing Iterators. When Good Programs Go Bad! - where! Abstract over control structures (in Clu)!

Tail Recursion. ;; a recursive program for factorial (define fact (lambda (m) ;; m is non-negative (if (= m 0) 1 (* m (fact (- m 1))))))

Category Item Abstract Concrete

Discussion 12 The MCE (solutions)

Lectures Basics in Procedural Programming: Machinery

6.037 Lecture 4. Interpretation. What is an interpreter? Why do we need an interpreter? Stages of an interpreter. Role of each part of the interpreter

Processadors de Llenguatge II. Functional Paradigm. Pratt A.7 Robert Harper s SML tutorial (Sec II)

C311 Lab #3 Representation Independence: Representation Independent Interpreters

CS61A Discussion Notes: Week 11: The Metacircular Evaluator By Greg Krimer, with slight modifications by Phoebus Chen (using notes from Todd Segal)

COP4020 Fall 2006 Final Exam

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures

Lecture 12: Conditional Expressions and Local Binding

Begin at the beginning

INF 102 CONCEPTS OF PROG. LANGS FUNCTIONAL COMPOSITION. Instructors: James Jones Copyright Instructors.

Chapter 13. Recursion. Copyright 2016 Pearson, Inc. All rights reserved.

LECTURE 18. Control Flow

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

6.821 Programming Languages Handout Fall MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Compvter Science

Announcements. Scope, Function Calls and Storage Management. Block Structured Languages. Topics. Examples. Simplified Machine Model.

Programming Languages: Application and Interpretation

The Scheme Machine. Robert G. Burger. Indiana University Computer Science Department Lindley Hall 215 Bloomington, Indiana

The Compiler So Far. Lexical analysis Detects inputs with illegal tokens. Overview of Semantic Analysis

Data Abstraction. An Abstraction for Inductive Data Types. Philip W. L. Fong.

Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?

CS558 Programming Languages. Winter 2013 Lecture 4

Control in Sequential Languages

Lecture 8: Recursion and Iteration. Exceptions. Declarative Programming.

A Brief Introduction to Scheme (II)

CSc 520 Principles of Programming Languages. 26 : Control Structures Introduction

CSE 413 Midterm, May 6, 2011 Sample Solution Page 1 of 8

Implementing Continuations

Transcription:

Konzepte von Programmiersprachen Chapter 5: Continuation-Passing Interpreters Stefan Wehr Universität Freiburg 8. Juni 2009 Konzepte von Programmiersprachen 1 / 43

Motivation Continuations: abstraction of the notion of control context (Environments: abstraction of data contexts) Support for control operators: Exceptions Threads Coroutines call/cc Introducing continuations is a common transformation in compilers: Makes control-flow explicit Introduces names for intermediate results Konzepte von Programmiersprachen 2 / 43

What is a control context? Intuition (not a formal definition) the rest of the program things that happen after evaluating the current expression Konzepte von Programmiersprachen 3 / 43

Outline Rewrite interpreter for the LETREC language in continuation-passing style (CPS): make control context explicit by passing around a continuation Use a trick that allows our CPS interpreter to be written in languages without support for tail calls Perform a translation of our CPS interpreter to use jumps instead of procedure calls Use the CPS interpreter to implement exceptions (Use the CPS interpreter to implement threads) Konzepte von Programmiersprachen 4 / 43

A Continuation-Passing Interpreter Design principles for the CPS (continuation-passing style) interpreter: No call to value-of grows the control context of the underlying scheme interpreter all calls to value-of must be tail calls Make the control context explicit using continuations Konzepte von Programmiersprachen 5 / 43

What s a tail call, anyway? Definition A procedure call is in tail position if it is the last operation of the calling procedure. A tail call is a procedure call in tail position. A tail-recursive procedure is a procedure whose recursive calls are all tail calls. Tail calls give raise the tail call optimization: Do not push the stack frame of the procedure being called on the stack frame of the calling procedure. Instead, pop the stack frame of the calling procedure before performing the tail call. Tail-recursive procedures under tail call optimization require only constant stack space. Konzepte von Programmiersprachen 6 / 43

Recursive definition of the factorial function (define fact (lambda (n) (if (zero? n) 1 (* n (fact (- n 1)))))) Calculating with fact: (fact 3) = (* 3 (fact 2)) = (* 3 (* 2 (fact 1))) = (* 3 (* 2 (* 1 (fact 0)))) = (* 3 (* 2 (* 1 1))) = (* 3 (* 2 1)) = (* 3 2) = 6 Konzepte von Programmiersprachen 7 / 43

Tail recursive definition of the factorial function (define fact-iter (lambda (n) (fact-iter-acc n 1))) (define fact-iter-acc (lambda (n a) (if (zero? n) a (fact-iter-acc (- n 1) (* n a))))) Calculating with fact-iter: (fact-iter 3) = (fact-iter-acc 3 1) = (fact-iter-acc 2 3) = (fact-iter-acc 1 6) = (fact-iter-acc 0 6) = 6 Konzepte von Programmiersprachen 8 / 43

What s a continuation, anyway? Definition The continuation of an expression represents a procedure that takes the result of the expression and completes the computation. An interface for continuations FinalAnswer = ExpVal apply-cont : Cont * ExpVal -> FinalAnswer Konzepte von Programmiersprachen 9 / 43

CPS interpreter: value-of-program ;; value-of-program : Program -> FinalAnswer (define value-of-program (lambda (pgm) (cases program pgm (a-program (exp1) (value-of/k exp1 (init-env) (end-cont)))))) ;; value-of/k : Exp * Env * Cont -> FinalAnswer Specification of apply-cont (partial) (apply-cont (end-cont) val) = (begin (eopl:printf "End of computation.~%") val)) Konzepte von Programmiersprachen 10 / 43

CPS interpreter: value-of/k (1/6) ;; value-of/k : Exp * Env * Cont -> FinalAnswer (define value-of/k (lambda (exp env cont) (cases expression exp (const-exp (num) (apply-cont cont (num-val num))) (var-exp (var) (apply-cont cont (apply-env env var))) (proc-exp (var body) (apply-cont cont (proc-val (procedure var body env)))) (letrec-exp (p-name b-var p-body letrec-body) (value-of/k letrec-body (extend-env-rec p-name b-var p-body env) cont))... Konzepte von Programmiersprachen 11 / 43

CPS interpreter: value-of/k (2/6) (zero?-exp (exp1) (value-of/k exp1 env (zero1-cont cont))) Specification of apply-cont (continued, still partial) (apply-cont (zero1-cont cont) val) = (apply-cont cont (bool-val (zero? (expval->num val)))) Konzepte von Programmiersprachen 12 / 43

CPS interpreter: value-of/k (3/6) (let-exp (var exp1 body) (value-of/k exp1 env (let-exp-cont var body env cont))) Specification of apply-cont (continued, still partial) (apply-cont (let-exp-cont var body env cont) val) = (value-of/k body (extend-env var val env) cont) Konzepte von Programmiersprachen 13 / 43

CPS interpreter: value-of/k (4/6) (if-exp (exp1 exp2 exp3) (value-of/k exp1 env (if-test-cont exp2 exp3 env cont))) Specification of apply-cont (continued, still partial) (apply-cont (if-test-cont exp2 exp3 env cont) val) = (if (expval->bool val) (value-of/k exp2 env cont) (value-of/k exp3 env cont)) Konzepte von Programmiersprachen 14 / 43

CPS interpreter: value-of/k (5/6) (diff-exp (exp1 exp2) (value-of/k exp1 env (diff1-cont exp2 env cont))) Specification of apply-cont (continued, still partial) (apply-cont (diff1-cont exp2 env cont) val1) = (value-of/k exp2 env (diff2-cont val1 cont)) (apply-cont (diff2-cont val1 cont) val2) = (let ((num1 (expval->num val1)) (num2 (expval->num val2))) (apply-cont cont (num-val (- num1 num2)))) Konzepte von Programmiersprachen 15 / 43

CPS interpreter: value-of/k (6/6) (call-exp (rator rand) (value-of/k rator env (rator-cont rand env cont))) ))) ;; end definition of value-of/k Specification of apply-cont (continued) (apply-cont (rator-cont rand env cont) val1) = (value-of/k rand env (rand-cont val1 cont)) (apply-cont (rand-cont val1 cont) val2) = (let ((proc1 (expval->proc val1))) (apply-procedure/k proc1 val2 cont)) Konzepte von Programmiersprachen 16 / 43

CPS interpreter: apply-procedure/k ;; apply-procedure/k : Proc * ExpVal * Cont ;; -> FinalAnswer (define apply-procedure/k (lambda (proc1 val cont) (cases proc proc1 (procedure (var body saved-env) (value-of/k body (extend-env var val saved-env) cont))))) Konzepte von Programmiersprachen 17 / 43

Examples 1. (value-of/k <<-(-(44,11),3)>> ρ 0 #(struct:end-cont)) 2. (value-of/k <<(exp1 exp2)>> ρ 1 cont1) see blackboard Konzepte von Programmiersprachen 18 / 43

Implementing the specification for continuations Two options: Procedural representation Data structure representation Konzepte von Programmiersprachen 19 / 43

Procedural representation ;; Cont = ExpVal -> FinalAnswer ;; end-cont : () -> Cont (define end-cont (lambda () (lambda (val) (begin (eopl:printf "End of computation.~%") val)))) ;; zero1-cont : Cont -> Cont (define zero1-cont (lambda (cont) (lambda (val) (apply-cont cont (bool-val (zero? (expval->num val)))))))... ;; apply-cont : Cont * ExpVal -> FinalAnswer (define apply-cont (lambda (cont v) (cont v))) Konzepte von Programmiersprachen 20 / 43

Data structure representation (define-datatype continuation continuation? (end-cont) (zero1-cont (cont continuation?))... ) ;; apply-cont : Cont * ExpVal -> FinalAnswer (define apply-cont (lambda (cont val) (cases continuation cont (end-cont () (begin (eopl:printf "End of computation.~%") val)) (zero1-cont (saved-cont) (apply-cont saved-cont (bool-val (zero? (expval->num val)))))... ))) Konzepte von Programmiersprachen 21 / 43

A trampolined CPS interpreter Problems when porting the CPS interpreter to a procedural language: No first-class functions Solution: use data structure representation for continuations No support for tail calls danger of stack overflow because procedure calls only return when the CPS interpreter ends Solution: Trampolined interpreter (explained next) Konzepte von Programmiersprachen 22 / 43

Skeleton of our CPS interpreter ;; value-of-program : Program -> FinalAnswer (define value-of-program (... (value-of/k...)...)) ;; value-of/k : Exp * Env * Cont -> FinalAnswer (define value-of/k (... (value-of/k...) (apply-cont...)...)) ;; apply-cont : Cont * ExpVal -> FinalAnswer ;; (data structure representation) (define apply-cont (... (value-of/k...) (apply-cont...) (apply-procedure/k...)...)) ;; apply-procedure/k : Proc * ExpVal * Cont ;; -> FinalAnswer (define apply-procedure/k (... (value-of/k...)...)) Konzepte von Programmiersprachen 23 / 43

Example (CPS interpreter, tail calls) value-of-program Konzepte von Programmiersprachen 24 / 43

Example (CPS interpreter, tail calls) value-of/k Konzepte von Programmiersprachen 24 / 43

Example (CPS interpreter, tail calls) apply-cont Konzepte von Programmiersprachen 24 / 43

Example (CPS interpreter, tail calls) apply-procedure/k Konzepte von Programmiersprachen 24 / 43

Example (CPS interpreter, tail calls) value-of/k Konzepte von Programmiersprachen 24 / 43

Example (CPS interpreter, tail calls) apply-cont Konzepte von Programmiersprachen 24 / 43

Example (CPS interpreter, tail calls) apply-procedure/k Konzepte von Programmiersprachen 24 / 43

Example (CPS interpreter, tail calls) value-of/k Konzepte von Programmiersprachen 24 / 43

Example (CPS interpreter, tail calls)... Konzepte von Programmiersprachen 24 / 43

Example (CPS interpreter, no tail calls) value-of-program Konzepte von Programmiersprachen 25 / 43

Example (CPS interpreter, no tail calls) value-ok/k value-of-program Konzepte von Programmiersprachen 25 / 43

Example (CPS interpreter, no tail calls) apply-cont value-ok/k value-of-program Konzepte von Programmiersprachen 25 / 43

Example (CPS interpreter, no tail calls) apply-procedure/k apply-cont value-ok/k value-of-program Konzepte von Programmiersprachen 25 / 43

Example (CPS interpreter, no tail calls) value-ok/k apply-procedure/k apply-cont value-ok/k value-of-program Konzepte von Programmiersprachen 25 / 43

Example (CPS interpreter, no tail calls) apply-cont value-ok/k apply-procedure/k apply-cont value-ok/k value-of-program Konzepte von Programmiersprachen 25 / 43

Example (CPS interpreter, no tail calls) apply-procedure/k apply-cont value-ok/k apply-procedure/k apply-cont value-ok/k value-of-program Konzepte von Programmiersprachen 25 / 43

Example (CPS interpreter, no tail calls) value-of/k apply-procedure/k apply-cont value-ok/k apply-procedure/k apply-cont value-ok/k value-of-program Konzepte von Programmiersprachen 25 / 43

Example (CPS interpreter, no tail calls)... value-of/k apply-procedure/k apply-cont value-ok/k apply-procedure/k apply-cont value-ok/k value-of-program Konzepte von Programmiersprachen 25 / 43

Trampolined interpreter Observation: An unbounded chain of procedure calls always involves calls of apply-procedure/k. Break the chain inside apply-procedure/k: apply-procedure/k should not call value-of/k directly apply-procedure/k returns a zero-argument procedure that calls value-of/k A trampoline procedure then invokes this zero-argument procedure to keep the system going. Konzepte von Programmiersprachen 26 / 43

Example (Trampolined CPS interpreter, no tail calls) value-of-program Konzepte von Programmiersprachen 27 / 43

Example (Trampolined CPS interpreter, no tail calls) value-ok/k value-of-program Konzepte von Programmiersprachen 27 / 43

Example (Trampolined CPS interpreter, no tail calls) apply-cont value-ok/k value-of-program Konzepte von Programmiersprachen 27 / 43

Example (Trampolined CPS interpreter, no tail calls) apply-procedure/k apply-cont value-ok/k value-of-program Konzepte von Programmiersprachen 27 / 43

Example (Trampolined CPS interpreter, no tail calls) trampoline value-of-program Konzepte von Programmiersprachen 27 / 43

Example (Trampolined CPS interpreter, no tail calls) value-of/k trampoline value-of-program Konzepte von Programmiersprachen 27 / 43

Example (Trampolined CPS interpreter, no tail calls) apply-cont value-of/k trampoline value-of-program Konzepte von Programmiersprachen 27 / 43

Example (Trampolined CPS interpreter, no tail calls) apply-procedure/k apply-cont value-of/k trampoline value-of-program Konzepte von Programmiersprachen 27 / 43

Example (Trampolined CPS interpreter, no tail calls) trampoline value-of-program Konzepte von Programmiersprachen 27 / 43

Example (Trampolined CPS interpreter, no tail calls)... trampoline value-of-program Konzepte von Programmiersprachen 27 / 43

Skeleton of the trampolined CPS interpreter ;; value-of-program : Program -> FinalAnswer (define value-of-program (... (trampoline (value-of/k...))...)) ;; value-of/k : Exp * Env * Cont -> Bounce (define value-of/k (... (value-of/k...) (apply-cont...)...)) ;; apply-cont : Cont * ExpVal -> Bounce ;; (data structure representation) (define apply-cont (... (value-of/k...) (apply-cont...) (apply-procedure/k...)...)) ;; apply-procedure/k : Proc * ExpVal * Cont -> Bounce (define apply-procedure/k (... (lambda () (value-of/k...))...)) Konzepte von Programmiersprachen 28 / 43

trampoline ;; trampoline : Bounce -> FinalAnswer (define trampoline (lambda (bounce) (if (expval? bounce) bounce (trampoline (bounce))))) Konzepte von Programmiersprachen 29 / 43

trampoline ;; trampoline : Bounce -> FinalAnswer (define trampoline (lambda (bounce) (if (expval? bounce) bounce (trampoline (bounce))))) Bounce = ExpVal (() Bounce) Konzepte von Programmiersprachen 29 / 43

An interpreter with jumps Goal: Get rid off procedure calls in the interpreter, use jumps instead Insight I: Use shared variables instead of procedure parameters Insight II: A zero-argument tail call is the same as a jump Konzepte von Programmiersprachen 30 / 43

Example with procedure parameters letrec even(x) = if zero?(x) then 1 else (odd -(x,1)) odd(x) = if zero?(x) then 0 else (even -(x,1)) in (odd 13) Konzepte von Programmiersprachen 31 / 43

Example without procedure parameters let x = 0 in letrec even() = if zero?(x) then 1 else begin set x = -(x,1); (odd) end odd() = if zero?(x) then 0 else begin set x = -(x,1); (even) end in begin set x = 13; (odd) end Konzepte von Programmiersprachen 32 / 43

Example with jumps x = 13; goto odd; even: if (x == 0) { return 1; } else { x = x-1; goto odd; } odd: if (x == 0) { return 0; } else { x = x-1; goto even; } Konzepte von Programmiersprachen 33 / 43

Skeleton of our CPS interpreter ;; value-of-program : Program -> FinalAnswer (define value-of-program (... (value-of/k...)...)) ;; value-of/k : Exp * Env * Cont -> FinalAnswer (define value-of/k (... (value-of/k...) (apply-cont...)...)) ;; apply-cont : Cont * ExpVal -> FinalAnswer ;; (data structure representation) (define apply-cont (... (value-of/k...) (apply-cont...) (apply-procedure/k...)...)) ;; apply-procedure/k : Proc * ExpVal * Cont ;; -> FinalAnswer (define apply-procedure/k (... (value-of/k...)...)) Konzepte von Programmiersprachen 34 / 43

Introducing registers Relevant procedures of the CPS interpreter: (value-of/k exp env cont) (apply-cont cont val) (apply-procedure/k proc val cont) We need five global registers: reg_exp reg_env reg_cont reg_val reg_proc Konzepte von Programmiersprachen 35 / 43

Rewriting the interpreter Systematically replace fragments such as (define value-of/k (lambda (exp env cont) (cases expression exp (const-exp (num) (apply-cont cont (num-val num)))...))) by (define value-of/k (lambda (exp env cont) (cases expression exp (const-exp (num) (set! cont cont) (set! val (num-val num)) (apply-cont))...))) Konzepte von Programmiersprachen 36 / 43

Things to watch out for Register stay often unchanged from one procedure call to another omit redundant assignments such as (set! reg_cont reg_cont) Local bindings must not shadow global registers the prefix reg_ for register names takes care of that Attention is needed if the same registers is used more than once in a procedure call does not occur in our example Konzepte von Programmiersprachen 37 / 43

Skeleton of the interpreter with jumps (1/2) ;; reg_exp : Exp (define reg_exp uninitialized) ;; reg_env : Env (define reg_env uninitialized) ;; reg_cont : Cont (define reg_cont uninitialized) ;; reg_val : ExpVal (define reg_val uninitialized) ;; reg_proc : Proc (define reg_proc uninitialized) Konzepte von Programmiersprachen 38 / 43

Skeleton of the interpreter with jumps (2/2) ;; value-of-program : Program -> FinalAnswer (define value-of-program (... (value-of/k)...)) ;; value-of/k : () -> FinalAnswer (define value-of/k (... (value-of/k) (apply-cont)...)) ;; apply-cont : () -> FinalAnswer (define apply-cont (... (value-of/k) (apply-cont) (apply-procedure/k)...)) ;; apply-procedure/k : () -> FinalAnswer (define apply-procedure/k (... (value-of/k)...)) Konzepte von Programmiersprachen 39 / 43

Exceptions Two new productions: Expression ::= try Expression catch (Identifier) Expression try-exp (exp1 var handler-exp) Expression ::= raise Expression raise-exp (exp) Semantics: raise e evaluates e and then raises an exception with that value. try e 1 catch (x) e 2 first evaluates e 1. If no exception occurs while evaluating e 1, then the result of e 1 is the result of the whole expression. If an exception occurs while evaluating e 1, the exception value is bound to x and the handler e 2 is evaluated. Konzepte von Programmiersprachen 40 / 43

Implementing exceptions with continuations ;; value-of/k : Exp * Env * Cont -> FinalAnswer (define value-of/k (lambda (exp env cont) (cases expression exp... (try-exp (exp1 var handler-exp) (value-of/k exp1 env (try-cont var handler-exp env cont))) (raise-exp (exp1) (value-of/k exp1 env (raise1-cont cont)))))) Konzepte von Programmiersprachen 41 / 43

Specification of apply-cont (extended) (apply-cont (try-cont var handler-exp env cont) val) = (apply-cont cont val) (apply-cont (raise1-cont cont) val) = (apply-handler val cont) Konzepte von Programmiersprachen 42 / 43

apply-handler (apply-handler val cont) searches for the closest exception handler in cont and applies it to val ;; apply-handler : ExpVal * Cont -> FinalAnswer (define apply-handler (lambda (val cont) (cases continuation cont (try-cont (var handler-exp saved-env saved-cont) (value-of/k handler-exp (extend-env var val saved-env) saved-cont)) (end-cont () (report-uncaught-exception)) (diff1-cont (exp2 saved-env saved-cont) (apply-handler val saved-cont))...))) Note: The implementation relies on the data structure representation of continuations. It is also possible to implement apply-handler with a procedural representation of continuations (see EOPL, exercise 5.41). Konzepte von Programmiersprachen 43 / 43