DOC PREVIEW
U of I CS 421 - Lecture 20

This preview shows page 1-2-14-15-30-31 out of 31 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 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 31 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 31 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 31 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 31 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 31 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

OutlineOverviewContinuationsExceptionsCoroutinesOutlineOverviewContinuationsExceptionsCoroutinesCS421 Lecture 20: Control Flow1Mark [email protected] of Illinois at Urbana-ChampaignJuly 25, 20061Based on slides by Mattox Beckman, as updated by Vikram Adve, GulAgha, and Elsa GunterMark Hills CS421 Lecture 20: Control FlowOutlineOverviewContinuationsExceptionsCoroutinesOverviewContinuationsExceptionsCoroutinesMark Hills CS421 Lecture 20: Control FlowOutlineOverviewContinuationsExceptionsCoroutinesObjectivesBy the end of this lecture, you should◮be familiar with different kinds of control flow constructs◮understand the basics of continuations and the Schemecall/cc construct◮understand exceptions, especially the approach taken bymodern OO languages like Java◮know about coroutines at a high-level, including how theywork and what they can accomplishMark Hills CS421 Lecture 20: Control FlowOutlineOverviewContinuationsExceptionsCoroutinesClasses of Control FlowWe can break control flow up into a number of categories, basedon the purpose of the constructs.◮Invocation◮Direct calls: functions, subroutines◮Indirect calls: function pointers, class methods, closures◮Termination of Scope◮Structured: break, break to a label, exceptions, CPS◮Unstructured: goto, setjmp/long jmp, exit◮Selection◮Structured: if/then/else, continue, switch, case◮Unstructured: goto, computed goto, labeled entriesMark Hills CS421 Lecture 20: Control FlowOutlineOverviewContinuationsExceptionsCoroutinesClasses of Control Flow, cont.◮Iteration◮Precomputed iteration space: do, foreach◮Dynamic iteration space: for, while, recursion◮Concurrency◮Manual: processes, threads, futures, coroutines◮Automatic: constructs in concurrent/parallel frameworks forreductions◮Communication and synchronization techniques are critical◮You should have come across many of these already:if/then/else, switch/case, do, for, while, break, continue,probably foreach, goto, exitMark Hills CS421 Lecture 20: Control FlowOutlineOverviewContinuationsExceptionsCoroutinesContinuationsScheme and Call/CCExamplesSummaryContinuationsAt its core, the continuation is just “the rest of the computation”– it tells us what we have left to do.◮Continuations can be used to model many control flowconstructs◮Continuation manipulation is a fundamental feature of Scheme◮Also, very important to denotational semantics; continuationsallow modelling of complex control flowMark Hills CS421 Lecture 20: Control FlowOutlineOverviewContinuationsExceptionsCoroutinesContinuationsScheme and Call/CCExamplesSummaryCall/CCTo do something interesting with a continuation, we first need toget ahold of one. In Scheme, this is done using the call/ccfunction.◮The continuation is represented as a function expecting oneargument◮The call/cc call also expects a function that takes oneargument as its argument◮When call/cc is called, the current control context is savedand passed to the function argument of call/cc◮Invoking this argument then returns control to where we werewhen we invoked call/ccMark Hills CS421 Lecture 20: Control FlowOutlineOverviewContinuationsExceptionsCoroutinesContinuationsScheme and Call/CCExamplesSummaryCall/CC Example 11 (define call/cc call-with-current-continuation)The full name is a bit cumb ers ome, but s ome Schemes don’t define theabbreviation...1 (+ 1 (call/cc2 (lambda (k)3 (+ 2 (k 3)))))◮The continuation is (+ 1 []), where [] represents the“hole” where the continuation result will be placed◮The final result is just 4 (1 + 3), since we never add 2 to theresult.Mark Hills CS421 Lecture 20: Control FlowOutlineOverviewContinuationsExceptionsCoroutinesContinuationsScheme and Call/CCExamplesSummaryCall/CC Example 21 (define list-product2 (lambda (s)3 (call/cc4 (lambda (exit)5 (let recur ((s s))6 (if (null? s) 17 (if (= (car s) 0) (exit 0)8 (* (car s) (recur (cdr s))))))))))9 > (list-product ’(1 2 3 4 5))10 12011 > (list-product ’(1 2 0 3 4 5))12 0This allows us to “jump out” when we find a 0, meaning that theproduct of the entire list is 0Mark Hills CS421 Lecture 20: Control FlowOutlineOverviewContinuationsExceptionsCoroutinesContinuationsScheme and Call/CCExamplesSummaryContinuations, Overall◮The good: Using continuations with call/cc gives us aflexible way to encode jumps, meaning we could build upfeatures like exceptions.◮The bad: This could get cumbersome, though – if we have adedicated exception mechanism, even just syntactic sugar, itwould make programs clearer.Mark Hills CS421 Lecture 20: Control FlowOutlineOverviewContinuationsExceptionsCoroutinesError Handling TechniquesExceptionsExceptions in JavaEvaluationError HandlingHistorically, languages and language APIs have handled errors inseveral different ways:◮Have functions return values, outside of the normal range ofreturn values, that represent errors;◮Return a success or error code from the function, requiringactual return values to be in pointers or reference parameters;◮Pass an error handling routine (like a conti nuation) that thenormal routine can invoke when errors occur.Mark Hills CS421 Lecture 20: Control FlowOutlineOverviewContinuationsExceptionsCoroutinesError Handling TechniquesExceptionsExceptions in JavaEvaluationSpecial Return Values1 if ((he=gethostbyname(hostname)) == NULL) {2 perror("clientConnect: failed to resolve ip address");3 return HOSTNOTFOUND;4 }56 if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {7 perror("clientConnect: failed to create socked");8 return OTHERERROR;9 }Mark Hills CS421 Lecture 20: Control FlowOutlineOverviewContinuationsExceptionsCoroutinesError Handling TechniquesExceptionsExceptions in JavaEvaluationSuccess or Error Returns1 pthread_mutex_t theLock = PTHREAD_MUTEX_INITIALIZER;2 ...3 if (pthread_mutex_lock(&theLock) != 0) {4 perror("Could not acquire lock");5 return OTHERERROR;6 }7 ...8 if (pthread_mutex_unlock(&theLock) != 0) {9 perror("Could not release lock");10 return OTHERERROR;11 }Mark Hills CS421 Lecture 20: Control FlowOutlineOverviewContinuationsExceptionsCoroutinesError Handling TechniquesExceptionsExceptions in JavaEvaluationAre these good enough?These methods work, but all tend to be clumsy.◮Using return codes require picking values that can never bereturned from the function, and are easy to forget to check◮Having explicit return codes for success or failure again meansthese have to be checked, which clutters up the code andleads to


View Full Document

U of I CS 421 - Lecture 20

Documents in this Course
Lecture 2

Lecture 2

12 pages

Exams

Exams

20 pages

Lecture

Lecture

32 pages

Lecture

Lecture

21 pages

Lecture

Lecture

15 pages

Lecture

Lecture

4 pages

Lecture

Lecture

68 pages

Lecture

Lecture

68 pages

Lecture

Lecture

84 pages

s

s

32 pages

Parsing

Parsing

52 pages

Lecture 2

Lecture 2

45 pages

Midterm

Midterm

13 pages

LECTURE

LECTURE

10 pages

Lecture

Lecture

5 pages

Lecture

Lecture

39 pages

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