CORNELL CS 611 - Lecture 18 Continuations and Except

Unformatted text preview:

CS611 Lecture 18 Continuations and Exceptions 11 October 2006Lecturer: Dexter KozenLast time we introduced CPS as a restriction on the λ-calculus. This was helpful because programswritten in this restricted λ-calculus have a much simpler op e rational semantics. In fact, we defined theoperational semantics using only a single rule. Another advantage to CPS is that evaluation order decisionsare already determined. In general, CPS style is a more primitive model of computation and therefore easierto compile.Today we give CPS semantics for uML as a translation to a restricted form of uML. Our translation willalso produce strongly-typed uML programs. Then we will extend the translation to uML!. Finally, we showhow to extend uML to support exception handling.1 CPS Semantics for uML with Strong Typing1.1 Value TranslationTo supp ort strong typing, we introduce type tags that can be used to tag each value with its type.booleans 0 empty list 2 functions 4integers 1 pairs 3 error 5We can define functions NULL, BOOL, INT, PAIR, FUN, etc. to tag a raw value with its type; for example,BOOL true = (0, true). These can all be defined in terms of a function TAG4= λtx. (t, x). Then BOOL =TAG 0, etc.We also define functions CHECK-NULL, CHECK-BOOL, CHECK-INT, CHECK-PAIR, CHECK-FUN,etc. to check that a given tagged value is of the correct type, extract the original raw value, and pass it to acontinuation. For example, CHECK-PAIR is defined as:CHECK-PAIR4= λkv. if #1 v = 3 then k (#2 v) else halt ERRORwhere the parameter k is a continuation and the parameter v is a tagged value. If the tag is 3, indicatingthat the raw value is a pair, then we pass the raw value to the continuation. Otherwise we have encountereda runtime type error, so we halt and return an error value. We can also define these functions uniformly interms of a functionCHECK4= λtkv. if #1 v = t then k (#2 v) else halt ERRORThen CHECK-PAIR = CHECK 3, etc. These implementations satisfy the equationsCHECK t k (TAG t0v) =k v, if t = t0,halt ERROR, if t 6= t0.Note that the continuation-passing style affords some flexibility in the way errors are handled. We neednot call the continuation k, but may instead call a different continuation (halt in this example) correspondingto an error or exce ption handler.11.2 Expression TranslationTranslations are of the form E[[e]]ρk, which means, “Send the value of the expression e evaluated in theenvironment ρ to the continuation k.” The translations are:E[[x]] ρ k4= k (LOOKUP ρ “x”)E[[n]] ρ k4= k (INT n)E[[(e1, e2)]] ρ k4= E[[e1]] ρ (λv1. E[[e2]] ρ (λv2. k (PAIR (v1, v2))))E[[#1 e ]] ρ k4= E[[e]] ρ (CHECK-PAIR (λp. k (#1 p))E[[λx. e ]] ρ k4= k (FUN(λyk0. E[[e ]] (UPDATE ρ “x” y) k0))= k (FUN(λy. E[[e ]] (UPDATE ρ “x” y)))E[[e0e1]] ρ k4= E[[e0]] ρ (CHECK-FUN (λf. E[[e1]] ρ (λv. f vk)))E[[if e0then e1else e2]] ρ k4= E[[e0]] ρ (CHECK-BOOL (λb. if b then E[[e1]] ρ k else E[[e2]] ρ k)).2 CPS Semantics for uML!2.1 SyntaxSince uML! has references, we need to add a store σ to our notation. Thus we now have translations withthe form E[[e]]ρkσ, which means, “Evaluate e in the environment ρ with store σ and send the resulting valueand the new store to the continuation k.” A continuation is now a function of a value and a store; that is,a continuation k should have the form λvσ. · · · .The translation is:• Variable: E[[x]] ρ k σ4= k (LOOKUP ρ “x”) σ.If we think about this translation as a function and η-reduce away the σ, we obtainE[[x]] ρ k = λσ. k (LOOKUP ρ “x”) σ = k (LOOKUP ρ “x”).Note that in the η-reduced version, we have the same translation that we had when we translated uML.In general, any expression in uML! that is not state-aware can be η-reduced to the same translation asuML. Thus in order to translate to uML!, we need to add translation rules only for the functionality that isstate-aware.We assume that we have a type tag for locations and functions LOC and CHECK-LOC for tagging valuesas locations and checking those tags. We also assume that we have extended our LOOKUP and UPDATEfunctions to apply to stores.E[[ref e]] ρ k σ4= E[[e]] ρ (λvσ0. let (`, σ00) = (MALLOC σ0v) in k (LOC `) σ00) σE[[!e]] ρ k4= E[[e]] ρ (CHECK-LOC (λ`σ0. k (LOOKUP σ0“`”) σ0))E[[e1:= e2]] ρ k4= E[[e1]] ρ (CHECK-LOC (λ`. E[[e2]] ρ (λvσ0. k (NULL 0) (UPDATE σ0“`” v))))3 ExceptionsAn exception mechanism allows non-local transfer of control in exceptional situations. It is typically usedto handle abnormal, unexpected, or rarely occurring events. It can simplify code by allowing programmersto factor out these uncommon cas es.To add an e xception handling mechanism to uML, we first extend the syntax:e ::= . . . | raise s e | try e1handle (s x) e22Informally, the idea is that handle provides a handler e2to be invoked when the exception named s isencountered inside the expression e1. To raise an exception, the program calls raise s e, where s is the nameof an exception and e is an expression that will be passed to the handler as its argument x.Most languages use a dynamic scoping mechanism to find the handler for a given exception. When anexception is encountered, the language walks up the runtime call stack until a suitable exception handler isfound.3.1 Exceptions in uMLTo add exception support to our CPS translation, we add a handler environment h, which maps exceptionnames to continuations. We also extend our LOOKUP and UPDATE functions to accommodate handlerenvironments. Applied to a handler environment, LOOKUP returns the continuation bound to a givenexception name, and UPDATE rebinds an exception name to a new continuation.Now we can add exception support to our translation:E[[raise s e]] ρ k h4= E[[e]] ρ (LOOKUP h “s”) hE[[try e1handle (s x) e2]] ρ k h4= E[[e1]] ρ k (UPDATE h “s” ( λv. E[[e2]](UPDATE ρ “x” v) k h))E[[λx. e ]] ρ k h4= k (FUN (λyk0h0. E[[e ]] (UPDATE ρ “x” y) k0h0))= k (FUN (λy. E[[e ]] (UPDATE ρ “x” y)))E[[e0e1]] ρ k h4= E[[e0]] ρ (CHECK-FUN (λf. E[[e1]] ρ (λv. f vkh)))There are some subtle design decisions captured by this translation. For example, if e2raises exceptions in try e1handle (s x) e2, in this translation e2will not be invoked again. That is, e2cannot be


View Full Document

CORNELL CS 611 - Lecture 18 Continuations and Except

Download Lecture 18 Continuations and Except
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 18 Continuations and Except and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 18 Continuations and Except 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?