DOC PREVIEW
Berkeley COMPSCI 164 - Lecture 24

This preview shows page 1-2-24-25 out of 25 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Other Control Flow ideas: Throw, Catch, Continuations and Call/CCLecture OutlineWhat control is missing in conventional languages?What is NOT dynamic in MJ?Non-local ExitsCatch and Throw in Lisp / similar to Java tryCatch and Throw / another exampleOther errors..Call with Current Continuation, call/cc is part of Scheme: More powerfulExamples of call/cc in SchemePrint-table using call/cc in SchemeAn amazing additional feature of call/cc in SchemeAn application: “automatic backtracking” or “nondeterministic programming”An application: “automatic backtracking” or “nondeterministic programming”To implement Call/CC we first need to talk about continuationsHuh? I’m confused.Continuation version of +, *Continuation version of +, *, tracedA slightly more elaborate exampleCompiling from continuations is obviousHow hard is it to implement an interpreter supporting call/cc?Interpreter with continuationsInterp-begin with continuationTracing the interpreter supporting call/ccHow about adding call/cc?Prof. Fateman CS164 Lecture 24 1Other Control Flow ideas: Throw, Catch, Continuations and Call/CCLecture 24Prof. Fateman CS164 Lecture 24 2Lecture Outline• Conventional control flow is not everything• Throw/catch are dynamic returns in Lisp• Call/CC goes even further (in Scheme)• Continuation passing is really neatProf. Fateman CS164 Lecture 24 3What control is missing in conventional languages?Consider these variations:Type declaration… var x: (if z then int else boolean)x= (if (z) then foo else bar) (u) /*possible in functional MJ */Define a method…Void f() {goto(quit);} /*somewhere (where!?) is a label quit: */Prof. Fateman CS164 Lecture 24 4What is NOT dynamic in MJ?Return can only be to the saved location prior to the callExceptions (e.g. Java’s try) don’t exist, nor is there a way to handle them.Asynchronous operations (e.g. spawned processes) missingAll these (and more) are in some languages.Prof. Fateman CS164 Lecture 24 5Non-local ExitsRationale: something extremely rare and (generally) bad happens in your program, and you want to pop up to some level, rejecting partial computations.You cannot wind your way out of a deeply nested chain of calls in any reasonable fashion: you would have to check the return value ofevery function.You would like to “goto” some currently active program with some information.(Note that without some information you would not know how you got to this spot, or that anything went wrong!) Basis for error handling, but also other applications… maybe you don’t want to exit? E.g. Replace all divide-by-zero results by “INF”, but CONTINUE program sequence.Prof. Fateman CS164 Lecture 24 6Catch and Throw in Lisp / similar to Java try(catch tag body)(throw tag value)(catch ‘tag (print 1) (throw ‘tag 2) (print 3))Prints 1 and returns 2, without printing 3Prof. Fateman CS164 Lecture 24 7Catch and Throw / another example(defun print-table(L)(catch ‘not-a-number (mapcar #’print-sqrt L)))(defun print-sqrt(x)(print (sqrt (must-be-number x))))(defun must-be-number(x)(if (numberp x) x (throw ‘not-a-number ‘Huh?)))(print-table ‘(1 4 x)) Î12Huh?Note that the catch is NOT IN THE LEXICAL SCOPE of the throw and bypasses the return from must-be-number, print-sqrt, mapcar.Prof. Fateman CS164 Lecture 24 8Other errors..Built-in errors do a kind of throw. CL has an elaborate error-handler for catching these errors according to classifications, and ways of substituting computations But here is the most primitive…(ignore-errors (/ 0 0))nil#<division-by-zero @ #x20e9b6da>Returns 2 values: nil, if an error, and the type of errorSpecifically, ignore-errors executes forms in a dynamic environment where a handler for conditions of type error has been established; if invoked, it handles such conditions by returning two values, nil and thecondition that was signaled, from the ignore-errors form. (more refined handling of errors is done by handler-case )Prof. Fateman CS164 Lecture 24 9Call with Current Continuation, call/cc is part of Scheme: More powerfulCall/cc is a normal procedure that takes a single argument, say comp. The function comp is itself a procedure of one argument.(call/cc comp) ;; calls comp and returns what comp returns The trick is, what is the argument to comp? It is a procedure. Call it cc, which is the current continuation point. If cc is applied to some value Z, that value Z is returned as the value of the call to call/ccProf. Fateman CS164 Lecture 24 10Examples of call/cc in SchemeCall/cc is a normal procedure that takes a single argument, say comp. The function comp is itself a procedure of one argument.(+ 1 (call/cc (lambda(cc)(+ 20 300))));; doesn’t use the ccÆ 321(+ 1 (call/cc (lambda(cc)(+ 20 (cc 300)))));; uses ccÆ 301;; effectively this throws 300 out of the call/cc. Equivalent;; to ((lambda(val)(+ 1 val))(catch ‘cc ((lambda(v)(+ 20 v))(throw ‘cc 300))))Or ((lambda(val) (+ 1 val)) 300)Prof. Fateman CS164 Lecture 24 11Print-table using call/cc in Scheme(define (print-table L)(call/cc(lambda(escape)(set! not-a-number escape) ;;set some global variable(map print-sqrt L))))(define (print-sqrt x)(write (sqrt (must-be-number x))))(define (must-be-number x)(if (numberp x) x (not-a-number ‘Huh?))Prof. Fateman CS164 Lecture 24 12An amazing additional feature of call/cc in Scheme(+ 1 (call/cc (lambda(cc)(set! old-cc cc)(+ 20 (cc 300)))));; doesn’t use the cc, but saves itÆ 301(old-cc 500);; uses ccÆ 501;; effectively old-cc returns FOR THE SECOND TIME to the point;; in the computation where 1 is added, this time returning 501.;; Can’t do this in common lisp. Throw/catch do not have “indefinite extent”…(+ 1 (catch ‘tag (+ 20 (throw ‘tag 300)))) Æ 301(throw ‘tag 500) Æ errorProf. Fateman CS164 Lecture 24 13An application: “automatic backtracking” or “nondeterministic programming”You may have seen this in CS61a.Let amb be the “ambiguous” operator that returns one of its two arguments, chosen at random. Amb must be a special form or macro since it must not evaluate both arguments… (amb x y) =(if (random-choice) (lambda() x)(lambda() y))(define (integer) (amb 1 (+1 (integer)))) ;; returns a random integer(define (prime) ;;return some prime number(let ((n (integer)))(if (prime? n) (fail))))Prof. Fateman CS164 Lecture 24 14An application: “automatic backtracking” or “nondeterministic programming”(define


View Full Document

Berkeley COMPSCI 164 - Lecture 24

Documents in this Course
Lecture 8

Lecture 8

40 pages

Load more
Download Lecture 24
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 24 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 24 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?