Continuation Passing Style. Continuation Passing Style

Similar documents
Functional Programming and Haskell

CSCE 314 Programming Languages

Programming Languages Lecture 14: Sum, Product, Recursive Types

Shell CSCE 314 TAMU. Higher Order Functions

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

Note that in this definition, n + m denotes the syntactic expression with three symbols n, +, and m, not to the number that is the sum of n and m.

Logic - CM0845 Introduction to Haskell

The Worker/Wrapper Transformation

Higher Order Functions in Haskell

INTRODUCTION TO HASKELL

Program Calculus Calculational Programming

15 150: Principles of Functional Programming Some Notes on Regular Expression Matching

Functional Programming. Overview. Topics. Definition n-th Fibonacci Number. Graph

Shell CSCE 314 TAMU. Functions continued

CS 340 Spring 2019 Midterm Exam

CSCC24 Functional Programming Scheme Part 2

CSE341 Section 3. Standard-Library Docs, First-Class Functions, & More

CSE3322 Programming Languages and Implementation

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc.

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Lexical Analysis. Lexical analysis is the first phase of compilation: The file is converted from ASCII to tokens. It must be fast!

CSCI 3155: Homework Assignment 4

Haskell Overview II (2A) Young Won Lim 8/9/16

MoreIntro_annotated.v. MoreIntro_annotated.v. Printed by Zach Tatlock. Oct 04, 16 21:55 Page 1/10

Foundations. Yu Zhang. Acknowledgement: modified from Stanford CS242

The Haskell HOP: Higher-order Programming

Recursion. Tjark Weber. Functional Programming 1. Based on notes by Sven-Olof Nyström. Tjark Weber (UU) Recursion 1 / 37

Functional Programming. Pure Functional Programming

DVA337 HT17 - LECTURE 4. Languages and regular expressions

The Worker/Wrapper Transformation

CSE399: Advanced Programming. Handout 2

Recursion. Lars-Henrik Eriksson. Functional Programming 1. Based on a presentation by Tjark Weber and notes by Sven-Olof Nyström

CITS3211 FUNCTIONAL PROGRAMMING

Haskell: From Basic to Advanced. Part 3 A Deeper Look into Laziness

Talen en Compilers. Jurriaan Hage , period 2. November 13, Department of Information and Computing Sciences Utrecht University

MPRI course 2-4 Functional programming languages Exercises

λ calculus is inconsistent

Continuations and Continuation-Passing Style

ECS 120 Lesson 7 Regular Expressions, Pt. 1

Functional Logic Programming. Kristjan Vedel

Chapter 6 Abstract Datatypes

Recursion. What is Recursion? Simple Example. Repeatedly Reduce the Problem Into Smaller Problems to Solve the Big Problem

Introduction to Functional Programming in Haskell 1 / 56

Functional Languages. Hwansoo Han

CS 421, Fall Sample Final Questions & Solutions

Type Checking and Type Inference

Standard prelude. Appendix A. A.1 Classes

Combining Static and Dynamic Contract Checking for Curry

Haskell Overview II (2A) Young Won Lim 8/23/16

Fall Lecture 13 Thursday, October 11

Multi-paradigm Declarative Languages

Programming Languages

CSE-321 Programming Languages 2012 Midterm

Control in Sequential Languages

The Untyped Lambda Calculus

# true;; - : bool = true. # false;; - : bool = false 9/10/ // = {s (5, "hi", 3.2), c 4, a 1, b 5} 9/10/2017 4

Types and Type Inference

Lambda expressions, functions and binding

On the Logical Foundations of Staged Computation

Outline. Introduction Concepts and terminology The case for static typing. Implementing a static type system Basic typing relations Adding context

ML Built-in Functions

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell

Haskell Refresher Informatics 2D

Haskell & functional programming, some slightly more advanced stuff. Matteo Pradella

CSc 372. Comparative Programming Languages. 11 : Haskell Higher-Order Functions. Department of Computer Science University of Arizona

COS 320. Compiling Techniques

Programming Language Pragmatics

Haskell Overview II (2A) Young Won Lim 9/26/16

Programming Languages and Compilers (CS 421)

List of lectures. Lecture content. Symbolic Programming. TDDA69 Data and Program Structure Symbolic and Logic Programming Cyrille Berger

CSc 520 Principles of Programming Languages. Currying Revisited... Currying Revisited. 16: Haskell Higher-Order Functions

Lectures 12 and 13 and 14: Regular Expression Matching; Staging

An introduction introduction to functional functional programming programming using usin Haskell

Structural polymorphism in Generic Haskell

Programming Languages and Compilers (CS 421)

Mechanized Operational Semantics

n What is its running time? 9/18/17 2 n poor_rev [1,2,3] = n (poor_rev [1] = n ((poor_rev [1] =

CS 242. Fundamentals. Reading: See last slide

Programming Languages (CS 550) Lecture 4 Summary Scanner and Parser Generators. Jeremy R. Johnson

Functional Programming in Haskell for A level teachers

Introduction to OCaml

301AA - Advanced Programming

Week 5 Tutorial Structural Induction

CSE 105 THEORY OF COMPUTATION

CSCI.6962/4962 Software Verification Fundamental Proof Methods in Computer Science (Arkoudas and Musser) Chapter p. 1/27

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

Computability via Recursive Functions

Programming Languages 3. Definition and Proof by Induction

Haskell Introduction Lists Other Structures Data Structures. Haskell Introduction. Mark Snyder

Konzepte von Programmiersprachen

Exercise 1 ( = 24 points)

Lambda Calculus. Concepts in Programming Languages Recitation 6:

Verification of an ML compiler. Lecture 3: Closures, closure conversion and call optimisations

Programming Languages and Techniques (CIS120)

Lecture Notes on Program Equivalence

Recursive Definitions, Fixed Points and the Combinator

Programming Paradigms and Languages Introduction to Haskell. dr Robert Kowalczyk WMiI UŁ

Booleans (aka Truth Values) Programming Languages and Compilers (CS 421) Booleans and Short-Circuit Evaluation. Tuples as Values.

CSE 3302 Programming Languages Lecture 8: Functional Programming

CSCE 314 Programming Languages. Functional Parsers

Transcription:

161

162 Agenda functional programming recap problem: regular expression matcher continuation passing style (CPS) movie regular expression matcher based on CPS correctness proof, verification change of specification handout, summary, outlook, feedback

163 Regular Expressions a regular expression r over alphabet Σ is defined by r empty language ε empty word a letter a Σ r 1 r 2 concatenation r 1 + r 2 alternation/choice r Kleene star semantics of regular expression: L(r) Σ string s Σ matches r iff s L(r)

164 Problem given the regular expression r 0 = (0 + 1(01 0) 1) Does 101010011010111001001010 match r 0? Problems where to split the string alternation implementation?

165 CPS (informal) characterization a function is in CPS style if it never returns its result is passed to a continuation syntactically: all arguments λ expressions or identifiers continuations make control- and data-flow explicit future of computation as argument continuation maps intermediate result to final result Notice direct-style CPS identity id x = x id x k = k x type α α α (α β) β examples of k: identity or print in direct-style

CPS (informal) characterization a function is in CPS style if it never returns its result is passed to a continuation syntactically: all arguments λ expressions or identifiers continuations make control- and data-flow explicit future of computation as argument continuation maps intermediate result to final result direct-style CPS f x y k = sq x $ x 2 y 2 f x y = sq x - sq y \xx sq y $ \yy k $ (-) xx yy type Int Int Int Int Int (Int α) α Notice assume that sq x k is in CPS style 166

167 CPS (informal) characterization a function is in CPS style if it never returns its result is passed to a continuation syntactically: all arguments λ expressions or identifiers continuations make control- and data-flow explicit future of computation as argument continuation maps intermediate result to final result direct-style CPS fac 0 k = k 1 fac 0 = 1 factorial fac n k = fac (n-1) $ fac n = n * fac (n-1) \res k $ (*) n res type Int Int Int (Int α) α

168 CPS Thrice Transform thrice f x = f $ f $ f x to CPS, where f is in CPS too! What s the type of thrice in thrice f x k? x : α f is in CPS: α (α β) β k continuation: α β thrice : (α (α β) β) α (α β) β thrice f x k = f x $ \fx f fx $ \ffx f ffx $ \fffx k fffx $

169 CPS Fold cfold f z [] = z cfold f z (x:xs) = f x z (\y cfold f y xs) only the folded function f is in CPS one can easily use cfold to define foldl and foldr: foldr f a l = cfold (\x xs k f x $ k xs) a l foldl f a l = cfold (\x xs k f (k xs) x) a l

170 Further Information term CPS coined by Gerald J. Sussmann and Guy L. Steele (Scheme) CPS used as intermediate representation in functional compilers similar to SSA form in imperative compilers allows for tail call optimization (TCO) (no function stack needed) all control-flow explicit good for automated verification

Further Information occur in other languages as well (SML, Scheme) call-with-current continuation aka call/cc; takes a memory snapshot and allows to later go from there like a dynamic goto subtle control-flow manipulations (like exceptions), similarly to monads CPS monad talk: http://video.google.com/videoplay?docid=, where document id is -4851250372422374791 Drawback: makes your eyes bleed

172 Regular Expression Matcher Solution Idea Use continuations! function acc r s k of type Regexp Char String (String Bool) Bool k is a continuation such that for every s Σ, regular expression r and every total function k of type String Bool: if there exists s 1, s 2 Σ such that s = s 1 s 2, s 1 L(r) and k(s 2 ) evaluates to true, then acc r s k evaluates to true if for all s 1, s 2 Σ such that s = s 1 s 2, s 1 L(r) we have that k(s 2 ) evaluates to false, then acc r s k evaluates to false accept r s = acc r s null code of solution provided on webpage

173 Agenda functional programming recap problem: regular expression matcher continuation passing style (CPS) movie regular expression matcher based on CPS correctness proof, verification change of specification handout, summary, outlook, feedback

Verification Formal specification of correctness of acc: for every s Σ, regular expression r and every total function k of type String Bool: if there exists s 1, s 2 Σ such that s = s 1 s 2, s 1 L(r) and k(s 2 ) evaluates to true, then acc r s k evaluates to true if for all s 1, s 2 Σ such that s = s 1 s 2, s 1 L(r) we have that k(s 2 ) evaluates to false, then acc r s k evaluates to false How do we prove it? Induction on structure of regular expression!

175 Change of Specification The prove fails. What do we do? change the program? change the specification! program proven correct for regular expressions in standard form standard form: ε does not occur under a Kleene star good news: each regular expression can be transformed into standard form! use r = r + δ(r) δ(r) = ε if ε L(r) and otherwise r generates L(r) \ {ε} Homework! Section 4 of handout, implement

176 Agenda functional programming recap problem: regular expression matcher continuation passing style (CPS) movie regular expression matcher based on CPS correctness proof, verification change of specification handout, summary, outlook, feedback

177 What was left out? aka further reading tail call optimization (TCO) λ calculus discussion of laziness vs tail recursion thunks laziness, λ, and weak-head normal form concurrency practical programming

178 What is next? holidays Prolog Christmas new year...