1 CS 312 – Lecture 28 Continuations –Probably the most confusing thing you’ve seen all semester… Course summary –Life after CS 312.

Slides:



Advertisements
Similar presentations
Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
Advertisements

Control-Flow Graphs & Dataflow Analysis CS153: Compilers Greg Morrisett.
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Winter 2013.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
Names and Bindings.
The Function Design Recipe CS 5010 Program Design Paradigms “Bootcamp” Lesson 1.1 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
CSE 326: Data Structures Lecture #4 Alon Halevy Spring Quarter 2001.
Introductory Computer Science Courses Past experiences & thoughts Haakon Ringberg, Thomson Research Paris & Princeton University.
Software Engineering and Design Principles Chapter 1.
CS 307 Fundamentals of Computer Science 1 Abstract Data Types many slides taken from Mike Scott, UT Austin.
Introduction to ML – Part 1 Kenny Zhu. Assignment 2 chive/fall07/cos441/assignments/a2.ht m
Trees. 2 Definition of a tree A tree is like a binary tree, except that a node may have any number of children Depending on the needs of the program,
Continuations COS 441 Princeton University Fall 2004.
Elaboration or: Semantic Analysis Compiler Baojian Hua
Reasons to study concepts of PL
Cse321, Programming Languages and Compilers 1 6/19/2015 Lecture #18, March 14, 2007 Syntax directed translations, Meanings of programs, Rules for writing.
Introduction to ML Last time: Basics: integers, Booleans, tuples,... simple functions introduction to data types This time, we continue writing an evaluator.
Programming Language Semantics Mooly SagivEran Yahav Schrirber 317Open space html://
Trees. Definition of a tree A tree is like a binary tree, except that a node may have any number of children –Depending on the needs of the program, the.
CS503: Tenth Lecture, Fall 2008 Review Michael Barnathan.
Data Structures, Spring 2004 © L. Joskowicz 1 DAST – Final Lecture Summary and overview What we have learned. Why it is important. What next.
Semantics for MinML COS 441 Princeton University Fall 2004.
1 ES 314 Advanced Programming Lec 2 Sept 3 Goals: Complete the discussion of problem Review of C++ Object-oriented design Arrays and pointers.
Data Structures and Programming.  John Edgar2.
Language Evaluation Criteria
Chapter 3 List Stacks and Queues. Data Structures Data structure is a representation of data and the operations allowed on that data. Data structure is.
Mathematical Modeling and Formal Specification Languages CIS 376 Bruce R. Maxim UM-Dearborn.
The Function Design Recipe CS 5010 Program Design Paradigms “Bootcamp” Lesson 1.1 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
CSE 341 Programming Languages Racket Datatype Style Programming Zach Tatlock Spring 2014.
MIS215 Module 0 - Intro 1 MIS 215 Module 0 Intro to Data Structures.
CSC 580 – Theory of Programming Languages, Spring, 2009 Week 9: Functional Languages ML and Haskell, Dr. Dale E. Parson.
CS 206 Introduction to Computer Science II 09 / 10 / 2009 Instructor: Michael Eckmann.
CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language.
December 4, Algorithms and Data Structures Lecture XV Simonas Šaltenis Aalborg University
Functional Programming With examples in F#. Pure Functional Programming Functional programming involves evaluating expressions rather than executing commands.
Recap form last time How to do for loops map, filter, reduce Next up: dictionaries.
Week 8 - Wednesday.  What did we talk about last time?  Level order traversal  BST delete  2-3 trees.
Software Development Problem Analysis and Specification Design Implementation (Coding) Testing, Execution and Debugging Maintenance.
12/9/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
Principle of Programming Lanugages 3: Compilation of statements Statements in C Assertion Hoare logic Department of Information Science and Engineering.
CS April 2002 Lazy Evaluation, Thunks, and Streams.
CS412/413 Introduction to Compilers Radu Rugina Lecture 13 : Static Semantics 18 Feb 02.
CSE 130 : Winter 2009 Programming Languages Lecture 11: What’s in a Name ?
CSE-321 Programming Languages Abstract Machine E POSTECH May 1, 2006 박성우.
1 Objective Caml (Ocaml) Aaron Bloomfield CS 415 Fall 2005.
Cs776 (Prasad)L2HOF1 Higher-Order Functions. cs776 (Prasad)L2HOF2 Higher-Order Functions A function that takes a function as argument and/or returns a.
Chapter 1: Preliminaries Lecture # 2. Chapter 1: Preliminaries Reasons for Studying Concepts of Programming Languages Programming Domains Language Evaluation.
서울대한양대 ( 안 산 ) 충남대 1년1년 컴퓨터기초 (C) 컴퓨터프로그래밍 (C, Java) 컴퓨터프로그래밍 (C) 2. 봄 프로그래밍 원리 (Scheme, ML) Structure & Interpretation of Computer Programs 프로그래밍 방법론.
LECTURE 10 Semantic Analysis. REVIEW So far, we’ve covered the following: Compilation methods: compilation vs. interpretation. The overall compilation.
Heath Carroll Bill Hanczaryk Rich Porter.  A Theory of Type Polymorphism in Programming ◦ Robin Milner (1977)  Milner credited with introducing the.
Programming Languages and Compilers (CS 421)
Java Programming Language
CS4450: Principles of Programming Languages
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2013.
Trees.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Zach Tatlock Winter 2018.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2017.
HW05: Graphs and Shortest Paths
CSE 341: Programming Langs
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Autumn 2018.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Autumn 2017.
Trees.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2016.
6.001 SICP Interpretation Parts of an interpreter
CS210- Lecture 3 Jun 6, 2005 Announcements
Computer Science 340 Software Design & Testing
Drew Wyborski Programming Languages
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2019.
Presentation transcript:

1 CS 312 – Lecture 28 Continuations –Probably the most confusing thing you’ve seen all semester… Course summary –Life after CS 312

2 Continuations SML/NJ has ability to capture a control context as a value: a continuation Continuation = “the rest of the program” Example: if x<0 then ~x else x fn(z:bool) => if z then ~x else x fn(a: int)=>a fn(a: int)=>~a

3 Continuations SML/NJ has ability to capture a control context as a value: a continuation Continuation = “the rest of the program” Example: if x<0 then ~x else x So we can rewrite the above code as (fn(z:bool) => if z then ~x else x) (x < 0) fn(z:bool) => if z then ~x else x

4 Continuations in ML In CPS, functions don’t return – they just pass their value to another function. Open SMLofNJ.Cont: ‘a cont is a continuation expecting a value of type ‘a throw: ‘a cont -> ‘a -> ‘b throws control to continuation, never comes back throw c v means: “You are c. Here is value v for you, take it and take the control flow too; I’ve done my job and I will disappear, so don’t come back to me with your result.”

5 Continuations in ML callcc : (‘a cont->`a)->`a callcc f invokes f passing it the current continuation callcc f means: “I will now pass control to f, so that f can perform some computation." throw = sending a value to a continuation callcc = sending a continuation to a function When f is done, it will send something of type `a to the continuation it received. directly, using a throw, or indirectly, with another callcc So, the continuation must have type `a cont and f will have type (`a cont -> `a). In the end, it will be as though the callcc f had returned `a.

6 Example if (x < 0) then ~x else x One way to write it in Continuation Passing Style: let fun f (x: int) (c: int cont): int = throw c (~x) fun g (x: int) (c: int cont): int = throw c (x) fun t (y: bool*int as (z, x)) (c: int cont): int = if z then callcc (f x) else callcc (g x) fun h (x: int) (c: int cont): int = callcc ( t ((x<0), x)) in callcc (h ~10 ) end

7 Handling errors Can be used in place of exceptions to send control to an arbitrary place let fun g(n: real) (errors: int option cont) : int option = if n < 0.0 then throw errors NONE else SOME(Real.trunc(Math.sqrt(n))) fun f (x:int) (y:int) (errors: int option cont): int option = if y = 0 then throw errors NONE else SOME(x div y+valOf(g 10.0 errors)) in case callcc(f 13 3) of NONE => "runtime error" | SOME(z) => "Answer is "^Int.toString(z) end

8 First-class continuations Can store continuations for future use! let val cref: int cont option ref = ref NONE fun f(c: int cont): int = (cref := SOME(c); 5) in callcc f; case !cref of NONE => "finished" | SOME(co) => throw co 4 end

9 Continuations - summary Control context is encoded as a value and passed around in the program Can be used to transfer control flow to arbitrary points in the program –related feature 1: gotos in low-level code –related feature 2: setjmp/longjmp in C –useful for exceptions –useful in compilers and interpreters "You need to learn continuations about three times before they really start making sense."

10 What happened in CS 312? Design and specification of programs –modules and interfaces –documenting functions and ADTs –programming in functional style –testing Data structures and algorithms –collections –graphs –showing correctness and complexity Programming languages –Features and methodologies –models of evaluation –implementation

11 Life after SML 312 is not about SML or even about functional programming Lessons apply to Java, C, C++, etc.

12 Design Break up your program into modules with clearly defined interfaces (signatures) Use abstract data types (data abstractions) Good interfaces are narrow, implementable, but adequate Avoid stateful abstractions, imperative operations unless compelling justification Testing strategy and test cases: coverage

13 Specification Good specifications: clear, simple, concise, accurate Think about your audience Avoid over-specification Abstraction barrier: user should not need to know implementation/representation Convince someone that every spec is met Specify representation invariants and abstraction functions

14 Data structures and algorithms Collections (ordered and unordered) –Sets, maps –Lists, arrays –Hash tables –Binary search trees (red-black, splay, B-trees) –Priority queues/heaps Graphs –BFS, DFS, Dijkstra CS 482 and 472

15 Data structures and algorithms String and regular-expression matching –CS 381/481 Mutable vs. immutable data structures Locality –Everywhere (theoretical and applied courses)

16 Correctness and complexity Using specifications, invariants to reason about correctness Constructing, solving recurrence relations Worst-case run time, average case run time, amortized run time Proofs by induction CS 482

17 Programming languages Features –Higher-order functions –Explicit refs –Recursive types and functions –Lazy vs. eager evaluation –Concurrency Evaluation models (semantics) –Substitution –Environments and closures Implementation –Type checking and type inference –Memory management, garbage collection CS 411, 412