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...