2 Desiderata Delimited continuations (shift/reset) for Standard ML of New Jersey Pure ML library (no...

download 2 Desiderata Delimited continuations (shift/reset) for Standard ML of New Jersey Pure ML library (no compiler hacking) Correct behavior in the presence.

If you can't read please download the document

description

3 Delimited continuations First-class continuation values, as with callcc Allow “computational sandbox” by delimiting portions of continuation available for capture Natural idiom for stratified computation: dynamic loading and evaluation, reflective towers, OSes, IDEs, … Elegant implementation of coroutines, generators

Transcript of 2 Desiderata Delimited continuations (shift/reset) for Standard ML of New Jersey Pure ML library (no...

2 Desiderata Delimited continuations (shift/reset) for Standard ML of New Jersey Pure ML library (no compiler hacking) Correct behavior in the presence of ML exceptions and exception handlers Good memory consumption 3 Delimited continuations First-class continuation values, as with callcc Allow computational sandbox by delimiting portions of continuation available for capture Natural idiom for stratified computation: dynamic loading and evaluation, reflective towers, OSes, IDEs, Elegant implementation of coroutines, generators 4 C Delimited continuations shift k => e C # e# ee reset C[ # e]C[ # [e]]C[ # [C[ shift k => e]]]C[] captured continuation not captured 5 ML + Delimited continuations e ::= | # e | shift k => e # e : evaluate e within a new delimiter shift k => e : capture and abort local continuation, bind it to k as a value, evaluate e k x : apply continuation-value k to x as a function (no jump) 6 Delimited control + exception handlers = delimited dynamic binding 7 (# (shift k => (k "") handle Exn => "inner"; raise Exn)) handle Exn => "outer" ((k "") handle Exn => "inner") handle Exn => "outer" k: fn v => #(v; raise Exn) How should exceptions work? handler outside the prompt handler inside the prompt 8 ((k "") handle Exn => "inner") handle Exn => "outer" ((#(""; raise Exn)) handle Exn => "inner") handle Exn => "outer" k: fn v => #(v; raise Exn) How should exceptions work? 9 ((#(""; raise Exn)) handle Exn => "inner") handle Exn => "outer" ((raise Exn) handle Exn => "inner") handle Exn => "outer" How should exceptions work? 10 ((raise Exn) handle Exn => "inner") handle Exn => "outer" ("inner") handle Exn => "outer" How should exceptions work? 11 ("inner") handle Exn => "outer" "inner" How should exceptions work? 12 Delimited dynamic binding captured handlers not captured local continuation meta- continuation 13 Meta-continuations 14 Meta-continuations C ::= | # CK ::= C * 15 Meta-continuations C ::= [ ] | C e | v C | C handle h => e K ::= halt | C :: K C[ # e], Ke, C :: K v, C :: KC[v], K push pop 16 Unsafe Implementation 17 Encoding the meta-continuation val mk : (answer ) list ref = ref [] fun push k = mk := k::(!mk) fun pop() = (case !mk of [] => raise MissingReset | k::ks => (mk := ks; k)) 18 Encoding local continuations Capture with callcc : junk border 19 An unsafe border [[ # e]] callcc lk => (push lk; let val result = [[e]] val return = pop() in return(result) end) C[ # e], K e, C :: K v, C :: K C[v], K 20 Implementation of shift [[ shift k => e]] callcc lk => (let val k = v => [[ #(lk v) ]] val result = [[e]] val return = pop() in return(result) end) 21 An unsafe border relevant handlers junk handlers let val result = raise 22 23 Example (# (shift k => (k "") handle Exn => "inner"; raise Exn)) handle Exn => "outer" ([]; raise Exn) 24 Example (# (shift k => (k "") handle Exn => "inner"; raise Exn)) handle Exn => "outer" "outer" 25 The Great Escape 26 The Great Escape relevant handlers junk handlers let val result = handle border handler 27 The Great Escape 28 The Great Escape 29 The Great Escape datatype tunneled = SUCCESS of answer | FAILURE of exn fun reify (thunk : unit answer) : tunneled = (SUCCESS (thunk ()) handle x => FAILURE x fun reflect (x : tunneled) : answer = (case x of SUCCESS v => v | FAILURE exn => raise exn) 30 The Great Escape [[ # e]] reflect (callcc lk => (push lk; let val result = reify () => [[e]] val return = pop() in return(result) end)) 31 Example (# (shift k => (k "") handle Exn => "inner"; raise Exn)) handle Exn => "outer" ([]; raise Exn) handle Exn => "outer" [] handle Exn => "inner" 32 Example (# (shift k => (k "") handle Exn => "inner"; raise Exn)) handle Exn => "outer" "inner" 33 The Great Escape In the pearl: Implementation in SML/NJ Proof of correctness (simulation) Space-efficient implementation (see paper) 34 Thank you!Google: Dave Herman Northeastern 35