Cse536 Functional Programming 1 7/16/2015 Lecture #15, Nov. 15, 2004 Todays Topics – Simple Animations - Review –Reactive animations –Vocabulary – Examples.

Slides:



Advertisements
Similar presentations
Introduction to Macromedia Director 8.5 – Lingo
Advertisements

Cse536 Functional Programming 1 1/13/2015 Lecture #13, Nov. 08, 2004 Today’s Topics –Simple animations –Buffered graphics –Animations in Haskell –Complex.
Haskell Chapter 7. Topics  Defining data types  Exporting types  Type parameters  Derived instances  Type synonyms  Either  Type classes  Not.
RAPTOR Syntax and Semantics By Lt Col Schorsch
Constructor and New Fields // Don't synch draw() with vertical retrace of monitor graphics.SynchronizeWithVerticalRetrace = false; IsFixedTimeStep = true;
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  ML lists are immutable.  Elements.
Functional Reactive Programming Lecture 6, Designing and Using Combinators John Hughes.
Events Chapter 7. Interactivity The real world is interactive User determines order of actions instead of programmer.
Events Chapter 7. Interactive Real world is interactive User determines order of actions instead of programmer.
© M. Winter COSC 4P41 – Functional Programming Abstract data types (ADTs) An ADT is a data type together with some functions to manipulate elements.
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
Chapter 19 An Imperative Robot Language. Motivation  In the previous chapter, monads were introduced.  In particular, state monads were described as.
Mrs. Chapman. Tabs (Block Categories) Commands Available to use Script Area where you type your code Sprite Stage All sprites in this project.
Cse536 Functional Programming 1 6/10/2015 Lecture #8, Oct. 20, 2004 Todays Topics –Sets and characteristic functions –Regions –Is a point in a Region –Currying.
0 PROGRAMMING IN HASKELL Chapter 5 - List Comprehensions.
Chapter 3 Simple Graphics. Side Effects and Haskell  All the programs we have seen so far have no “side-effects.” That is, programs are executed only.
Cse536 Functional Programming 1 6/11/2015 Lecture #5, Oct 11, 2004 Reading Assignments –Finish Chapter 3, Simple Graphics –Read Chapter 4, Shapes II, Drawing.
Cse536 Functional Programming 1 6/12/2015 Lecture #11, Nov. 01, 2004 Special Guest lecture by Tom Harke Today’s Topics –The Haskell Class system –Instance.
Chapter 17 Rendering Reactive Animations. Motivation  In the previous chapter we learned just enough about advanced IO and concurrency to develop a “renderer”
Cse536 Functional Programming 1 6/23/2015 Lecture #17, Dec. 1, 2004 Todays Topics – Higher Order types »Type constructors that take types as arguments.
Tim Sheard Oregon Graduate Institute Lecture 5: Review of homework 2 CS510 Sect FSC Winter 2004 Winter 2004.
Chapter 13 A Module of Simple Animations. Motivation  In the abstract, an animation is a continuous, time-varying image.  But in practice, it is a sequence.
Cse502 Functional Programming 1 6/26/2015 Lecture #11, Nov. 4, 2002 Todays Topics –Using Calculation to prove 2 functions are equal »An example from class.
Advanced Programming Andrew Black and Tim Sheard Lecture 4 Intro to Haskell.
Advanced Programming Handout 10 A Module of Simple Animations (SOE Chapter 13)
Chapter 15 A Module of Reactive Animations. Motivation  The design of animations in Chapter 13 is elegant, and in fact has the feel of a small domain-specific.
Chapter 5 Polymorphic and Higher-Order Functions.
Cse536 Functional Programming 1 6/30/2015 Lecture #16, Nov. 29, 2004 Todays Topics – Files, Channels, and Handles –IO exception handling –First Class Channels.
Cse536 Functional Programming 1 7/14/2015 Lecture #2, Sept 29, 2004 Reading Assignments –Begin Chapter 2 of the Text Home work #1 can be found on the webpage,
TODAY’S LECTURE Review Chapter 2 Go over exercises.
Mr. Wortzman. Tabs (Block Categories) Available Blocks Script Area Sprite Stage All sprites in this project.
0 PROGRAMMING IN HASKELL Typeclasses and higher order functions Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and.
Functional Programming Lecture 4 - More Lists Muffy Calder.
Ms. Deveny Second Semester  Introductions  Interview your table partner  Name  Why taking CPD  Computer experience  Favorite game?
Introduction to Scratch!
ECS 10 10/8. Outline Announcements Homework 2 questions Boolean expressions If/else statements State variables and avoiding sys.exit(…) Example: Coin.
Events Chapter 7 Part 2. While a Key is Pressed Event Specialized event An event occurs when you press a key and continues until you take your finger.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
11/1/20151 GC16/3011 Functional Programming Lecture 5 Miranda patterns, functions, recursion and lists.
© M. Winter COSC 4P41 – Functional Programming Programming with actions Why is I/O an issue? I/O is a kind of side-effect. Example: Suppose there.
CIS 3.5 Lecture 2.2 More programming with "Processing"
Lesson Two: Everything You Need to Know
CS324e - Elements of Graphics and Visualization Timing Framework.
1 CS 457/557: Functional Languages Lists and Algebraic Datatypes Mark P Jones Portland State University.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
Haskell Chapter 5, Part II. Topics  Review/More Higher Order Functions  Lambda functions  Folds.
1 CSC 221: Computer Programming I Fall 2009 Introduction to programming in Scratch  animation sprites  motion, control & sensing  costume changes 
Loops. About the Midterm Exam.. Exam on March 12 Monday (tentatively) Review on March 5.
0 PROGRAMMING IN HASKELL Typeclasses and higher order functions Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and.
© M. Winter COSC 4P41 – Functional Programming Some functions id :: a -> a id x = x const :: a -> b -> a const k _ = k ($) :: (a -> b) -> a -> b.
Set Comprehensions In mathematics, the comprehension notation can be used to construct new sets from old sets. {x2 | x  {1...5}} The set {1,4,9,16,25}
Set Comprehensions In mathematics, the comprehension notation can be used to construct new sets from old sets. {x2 | x  {1...5}} The set {1,4,9,16,25}
Polymorphic Functions
AFP - Lecture 2 Domain Specific Embedded Languages
Lecture 2 Domain Specific Embedded Languages
Learn… Create… Program
Chapter 10 Algorithms.
Higher Order Functions
Learn… Create… Program
Compiling Real-Time Functional Reactive Programming (RT-FRP)
PROGRAMMING IN HASKELL
CSE 3302 Programming Languages
Reactive Animations Todays Topics
Learn… Create… Program
Learn… Create… Program
Simple Animations Today’s Topics Reading Assignment Simple animations
Presentation transcript:

Cse536 Functional Programming 1 7/16/2015 Lecture #15, Nov. 15, 2004 Todays Topics – Simple Animations - Review –Reactive animations –Vocabulary – Examples – Implementation »behaviors »events Reading – Read Chapter 15 - A Module of Reactive Animations – Read Chapter 17 – Rendering Reactive Animations Homework –Assignment #7 on back of this handout –Due Monday Nov 29, 2004 – After Thanksgiving Break

Cse536 Functional Programming 2 7/16/2015 Review: Behavior A Behavior a can be thought of abstractly as a function from Time to a. In the chapter on functional animation, we animated Shape ’s, Region ’s, and Picture ’s. For example: dot = (ell ) ex1 = paint red (translate (0, time / 2) dot) Try It ex2 = paint blue (translate (sin time,cos time) dot) X coord Y coord

Cse536 Functional Programming 3 7/16/2015 Abstraction The power of animations is the ease with which they can be abstracted over, to flexibly create new animations from old. wander x y color = paint color (translate (x,y) dot) ex3 = wander (time /2) (sin time) red

Cse536 Functional Programming 4 7/16/2015 Example: The bouncing ball Suppose we wanted to animate a ball bouncing horizontally from wall to wall The Y position is constant, but the x position varies like: 0 +N-N

Cse536 Functional Programming 5 7/16/2015 Y axis X axis time Time `mod` N Period modula x y = (period,w) where (whole,fract) = properFraction x n = whole `mod` y period = (whole `div` y) w = (fromInt (toInt n)) + fract

Cse536 Functional Programming 6 7/16/2015 X axis Time `mod` N X axis 1 id 2 (N-) 3 negate4 (-N)

Cse536 Functional Programming 7 7/16/2015 Implementation bounce t = f fraction where (period,fraction) = modula t 2 f = funs !! (period `mod` 4) funs = [id,(2.0 -),negate,(\x -> x - 2.0)] ex4 = wander (lift1 bounce time) 0 yellow Remember this example. Reactive animations will make this much easier to do.

Cse536 Functional Programming 8 7/16/2015 Reactive Animations With a reactive animation, things do more than just change and move with time according to some algorithm. Reactive programs “react” to user stimuli, and real- time events, even virtual events, such as: –key press –button press –hardware interrupts –virtual event - program variable takes on some particular value We will try and illustrate this first by example, and then only later explain how it is implemented Example: color0 = red `switch` (lbp ->> blue) moon = (translate (sin time,cos time) dot) ex5 = paint color0 moon

Cse536 Functional Programming 9 7/16/2015 A Reactive Vocabulary Colors –Red,Blue,Yellow,Green,White :: Color –red, blue, yellow, green, white :: Behavior Color Shapes and Regions –Shape :: Shape -> Region –shape :: Behavior Shape -> Behavior Region –Ellipse,Rectangle :: Float -> Float -> Region –ell, rec :: Behavior Float -> Behavior Float -> Behavior Region –Translate :: (Float,Float) -> Region -> Region –translate :: (Behavior Float, Behavior Float) -> Behavior Region -> Behavior Region

Cse536 Functional Programming 10 7/16/2015 Operator and Event Vocabulary Numeric and Boolean Operators – (+), (*) :: Num a => Behavior a -> Behavior a -> Behavior a –negate :: Num a => Behavior a -> Behavior a –(>*),( =*),( Behavior a -> Behavior a -> Behavior Bool –(&&*),(||*) :: Behavior Bool -> Behavior Bool -> Behavior Bool Events –lbp :: Event () -- left button press –rbp :: Event () -- right button press –key :: Event Char -- key press –mm :: Event Vertex -- mouse motion

Cse536 Functional Programming 11 7/16/2015 Combinator Vocabulary Event Combinators –(->>) :: Event a -> b -> Event b –(=>>) :: Event a -> (a->b) -> Event b –(.|.) :: Event a -> Event a -> Event a –withElem :: Event a -> [b] -> Event (a,b) –withElem_ :: Event a -> [b] -> Event b Behavior and Event Combinators –switch :: Behavior a -> Event(Behavior a) -> Behavior a –snapshot_ :: Event a -> Behavior b -> Event b –step :: a -> Event a -> Behavior a –stepAccum :: a -> Event(a -> a) -> Behavior a

Cse536 Functional Programming 12 7/16/2015 Analyse Ex3. red,blue :: Behavior Color lbp :: Event () (->>) :: Event a -> b -> Event b switch :: Behavior a -> Event(Behavior a) -> Behavior a Event () Behavior Color color0 = red `switch` (lbp ->> blue) Event (Behavior Color) Behavior Color

Cse536 Functional Programming 13 7/16/2015 Either (.|.) and withElem color1 = red `switch` (lbp `withElem_` cycle [blue,red]) ex6 = paint color1 moon color2 = red `switch` ((lbp ->> blue).|. (key ->> yellow)) ex7 = paint color2 moon

Cse536 Functional Programming 14 7/16/2015 Key and Snapshot color3 = white `switch` (key =>> \c -> case c of ‘r' -> red ‘b' -> blue ‘y' -> yellow _ -> white ) ex8 = paint color3 moon color4 = white `switch` ((key `snapshot` color4) =>> \(c,old) -> case c of ‘r' -> red ‘b' -> blue ‘y' -> yellow _ -> constB old) ex9 = paint color4 moon

Cse536 Functional Programming 15 7/16/2015 Step :: a -> Event a -> Behavior a size '2' = size :: Char -> Float size '3' = 0.4 size '4' = 0.6 size '5' = 0.8 size '6' = 1.0 size '7' = 1.2 size '8' = 1.4 size '9' = 1.6 size _ = 0.1 growCircle :: Char -> Region growCircle x = Shape(Ellipse (size x) (size x)) ex10 = paint red (Shape(Ellipse 1 1) `step` (key =>> growCircle))

Cse536 Functional Programming 16 7/16/2015 stepAccum :: a -> Event(a -> a) -> Behavior a stepAccum takes a value and an event of a function. Everytime the event occurs, the function is applied to the old value to get a new value. power2 :: Event(Float -> Float) power2 = (lbp ->> \ x -> x*2).|. (rbp ->> \ x -> x * 0.5) dynSize = 1.0 `stepAccum` power2 ex11 = paint red (ell dynSize dynSize)

Cse536 Functional Programming 17 7/16/2015 Integral The combinator: – integral :: Behavior Float -> Behavior Float has a lot of interesting uses. If F :: Behavior Float (think function from time to Float) then integral F z is the area under the curve gotten by plotting F from 0 to z F x time axis z Integral F z

Cse536 Functional Programming 18 7/16/2015 Bouncing Ball revisited The bouncing ball has a constant velocity (either to the right, or to the left). Its position can be thought of as the integral of its velocity. At time t, the area under the curve is t, so the x position is t as well. If the ball had constant velocity 2, then the area under the curve is 2 * t, etc. If velocity is a constant ….

Cse536 Functional Programming 19 7/16/2015 Bouncing Ball again ex12 = wander x 0 yellow where xvel = 1 `stepAccum` (hit ->> negate) x = integral xvel left = x <=* -2.0 &&* xvel <*0 right = x >=* 2.0 &&* xvel >*0 hit = predicate (left ||* right)

Cse536 Functional Programming 20 7/16/2015 Mouse Motion The variable mm :: Event Vertex At every point in time it is an event that returns the mouse position. mouseDot = mm =>> \ (x,y) -> translate (constB x,constB y) dot ex13 = paint red (dot `switch` mouseDot)

Cse536 Functional Programming 21 7/16/2015 How does this work? Events are “real-time” actions that “happen” in the world. How do we mix Events and behaviors in some rational way. The Graphics Library supports a basic type that models these actions. type Time = Float data G.Event = Key { char :: Char, isDown :: Bool } | Button { pt :: Vertex, isLeft, isDown :: Bool } | MouseMove { pt :: Vertex } | Resize | Closed deriving Show type UserAction = G.Event

Cse536 Functional Programming 22 7/16/2015 Type of Behavior In simple animations, a Behavior was a function from time. But if we mix in events, then it must be a function from time and a list of events. First try: newtype Behavior1 a = Behavior1 ([(UserAction,Time)] -> Time -> a) User Actions are time stamped. Thus the value of a behavior (Behavior1 f) at time t is, f uas t, where uas is the list of user actions. Expensive because f has to “whittle” down uas at every sampling point (time t ), to find the events it is interested in.

Cse536 Functional Programming 23 7/16/2015 Solution Sample at monotonically increasing times, and keep the events in time order. Analogy: suppose we have two lists xs and ys and we want to test for each element in ys whether it is a member of xs –inList :: [Int] -> Int -> Bool –result :: [Bool] -- Same length as ys –result1 :: map (inList xs) ys What’s the cost of this operation? This is analagous to sampling a behavior at many times.

Cse536 Functional Programming 24 7/16/2015 If xs and ys are ordered... result2 :: [Bool] result2 = manyInList xs ys manyInList :: [Int] -> [Int] -> [Bool] manyInList [] _ = [] manyInList _ [] = [] manyInList (x:xs) (y:ys) = if y<x then manyInList xs (y:ys) else (y==x) : manyInList (x:xs) ys

Cse536 Functional Programming 25 7/16/2015 Behavior: Second try newtype Behavior2 a = Behavior2 ([(UserAction,Time)] -> [Time] -> [a]) See how this has structure similar to the manyInList problem? manyInList :: [Int] -> [Int] -> [Bool]

Cse536 Functional Programming 26 7/16/2015 Refinements newtype Behavior2 a = Behavior2 ([(UserAction,Time)] -> [Time] -> [a]) newtype Behavior3 a = Behavior3 ([UserAction] -> [Time] -> [a]) newtype Behavior4 a = Behavior4 ([Maybe UserAction] -> [Time] -> [a]) Final Solution newtype Behavior a = Behavior (([Maybe UserAction],[Time]) -> [a])

Cse536 Functional Programming 27 7/16/2015 Events newtype Event a = Event (([Maybe UserAction],[Time]) -> [Maybe a]) Note there is an isomorphism between the two types Event a and Behavior (Maybe a) We can think of an event, that at any particular time t, either occurs, or it doesn’t. Exercise: Write the two functions that make up the isomorphism: –toEvent :: Event a -> Behavior (Maybe a) –toBeh :: Behavior(Maybe a) -> Event a

Cse536 Functional Programming 28 7/16/2015 Intuition Intuitively it’s useful to think of a Behavior m as transforming two streams, one of user actions, the other of the corresponding time (the two streams always proceed in lock-step), into a stream of m things. User actions include things like –left and right button presses –key presses –mouse movement User Actions also include the “clock tick” that is used to time the animation. [ leftbutton, key ‘x’, clocktick, mousemove(x,y), …] [ 0.034, 0.65, 0.98, 1.29,... ] [ M1, m2, m3, … ]

Cse536 Functional Programming 29 7/16/2015 The Implementation time :: Behavior Time time = Behavior (\(_,ts) -> ts) constB :: a -> Behavior a constB x = Behavior (\_ -> repeat x) ([ua1,ua2,ua3, …],[t1,t2,t3, …]) ---> [t1, t2, t3, …] ([ua1,ua2,ua3, …],[t1,t2,t3, …]) ---> [x, x, x, …]

Cse536 Functional Programming 30 7/16/2015 Simple Behaviors red, blue :: Behavior Color red = constB Red blue = constB Blue lift0 :: a -> Behavior a lift0 = constB

Cse536 Functional Programming 31 7/16/2015 Notation We often have two versions of a function: xxx :: Behavior a -> (a -> b) -> T b xxx_ :: Behavior a -> b -> T b And two versions of some operators: (=>>) :: Event a -> (a->b) -> Event b (->>) :: Event a -> b -> Event b

Cse536 Functional Programming 32 7/16/2015 Lifting ordinary functions ($*) :: Behavior (a->b) -> Behavior a -> Behavior b Behavior ff $* Behavior fb = Behavior (\uts -> zipWith ($) (ff uts) (fb uts) where f $ x = f x lift1 :: (a -> b) -> (Behavior a -> Behavior b) lift1 f b1 = lift0 f $* b1 lift2 :: (a -> b -> c) -> (Behavior a -> Behavior b -> Behavior c) lift2 f b1 b2 = lift1 f b1 $* b2 ([t1,t2,t3, …],[f1,f2,f3, …]) ---> ([t1,t2,t3, …],[x1,x2,x3, …]) ---> ([t1,t2,t3, …],[f1 x1, f2 x2, f3 x3, …]

Cse536 Functional Programming 33 7/16/2015 Button Presses data G.Event = Key { char :: Char, isDown :: Bool } | Button { pt :: Vertex, isLeft, isDown :: Bool } | MouseMove { pt :: Vertex } lbp :: Event () lbp = Event (\(uas,_) -> map getlbp uas) where getlbp (Just (Button _ True True)) = Just () getlbp _ = Nothing ([Noting, Just (Button …), Nothing, Just(Button …), …], [t1,t2,t3, …]) ---> [Nothing, Just(), Nothing, Just(), …] Color0 = red `switch` (lbp --> blue)

Cse536 Functional Programming 34 7/16/2015 Key Strokes key :: Event Char key = Event (\(uas,_) -> map getkey uas) where getkey (Just (Key ch True)) = Just ch getkey _ = Nothing ([leftbut, key ‘z’ True, clock-tick, key ‘a’ True …], [t1, t2, t3, t4, …]) ---> [Nothing, Just ‘z’, Nothing, Just ‘a’, …]

Cse536 Functional Programming 35 7/16/2015 Mouse Movement mm :: Event Vertex mm = Event (\(uas,_) -> map getmm uas) where getmm (Just (MouseMove pt)) = Just (gPtToPt pt) getmm _ = Nothing mouse :: (Behavior Float, Behavior Float) mouse = (fstB m, sndB m) where m = (0,0) `step` mm ([Noting, Just (MouseMove …), Nothing, Just(MouseMove …), …], [t1,t2,t3, …]) ---> [Nothing, Just(x1,y1), Nothing, Just(x2,y2), …] ( (uas,ts) --> [x1,x2, …], (uas,ts) --> [y1, y2, …] )

Cse536 Functional Programming 36 7/16/2015 Behavior and Event Combinators switch :: Behavior a -> Event (Behavior a) -> Behavior a Behavior fb `switch` Event fe = memoB (Behavior -> loop us ts (fe uts) (fb uts))) where loop (_:us) (_:ts) ~(e:es) (b:bs) = b : case e of Nothing -> loop us ts es bs Just (Behavior fb') -> loop us ts es (fb' (us,ts)) ([Noting,Just (Beh [x,y,...] …),Nothing,Just(Beh [m,n,…])…], [t1,t2,t3, …]) ---> [fb1, fb2, x, y, m, n …]

Cse536 Functional Programming 37 7/16/2015 Event Transformer (map?) (=>>) :: Event a -> (a->b) -> Event b Event fe =>> f = Event (\uts -> map aux (fe uts)) where aux (Just a) = Just (f a) aux Nothing = Nothing (->>) :: Event a -> b -> Event b e ->> v = e =>> \_ -> v ([Noting, Just (Ev x), Nothing, Just(Ev y), …] --> f --> [Nothing, Just(f x), Nothing, Just(f y), …]

Cse536 Functional Programming 38 7/16/2015 withElem withElem :: Event a -> [b] -> Event (a,b) withElem (Event fe) bs = Event (\uts -> loop (fe uts) bs) where loop (Just a : evs) (b:bs) = Just (a,b) : loop evs bs loop (Nothing : evs) bs = Nothing : loop evs bs withElem_ :: Event a -> [b] -> Event b withElem_ e bs = e `withElem` bs =>> snd Infinite list ([Noting, Just x, Nothing, Just y, …]) ---> [b0,b1,b2,b3, …] -> [Nothing, Just(x,b0), Nothing, Just(y,b1), …]

Cse536 Functional Programming 39 7/16/2015 Either one event or another (.|.) :: Event a -> Event a -> Event a Event fe1.|. Event fe2 = Event (\uts -> zipWith aux (fe1 uts) (fe2 uts)) where aux Nothing Nothing = Nothing aux (Just x) _ = Just x aux _ (Just x) = Just x ([Noting, Just x, Nothing, Just y, …]) ---> [Nothing, Just a, Just b, Nothing, …] ---> [Nothing, Just x, Just b, Just y, …]

Cse536 Functional Programming 40 7/16/2015 Snapshot snapshot :: Event a -> Behavior b -> Event (a,b) Event fe `snapshot` Behavior fb = Event (\uts -> zipWith aux (fe uts) (fb uts)) where aux (Just x) y = Just (x,y) aux Nothing _ = Nothing snapshot_ :: Event a -> Behavior b -> Event b snapshot_ e b = e `snapshot` b =>> snd [Nothing, Just x, Nothing, Just y, …] ---> [b1, b2, b3, b4, …] ---> [Nothing, Just(x,b2), Nothing, Just(y,b4), …]

Cse536 Functional Programming 41 7/16/2015 step and stepAccum step :: a -> Event a -> Behavior a a `step` e = constB a `switch` e =>> constB stepAccum :: a -> Event (a->a) -> Behavior a a `stepAccum` e = b where b = a `step` (e `snapshot` b =>> uncurry ($)) X1 -> [Nothing, Just x2, Nothing, Just x3, …] ---> [x1, x1, x2, x2, x3,...] X1 -> [Noting, Just f, Nothing, Just g, …] ---> [x1, x1, f x1, (f x1), g(f x1),...]

Cse536 Functional Programming 42 7/16/2015 predicate predicate :: Behavior Bool -> Event () predicate (Behavior fb) = Event (\uts -> map aux (fb uts)) where aux True = Just () aux False = Nothing [True, True, False, True, False, …] ---> [Just(), Just(), Nothing, Just(), Nothing,...]

Cse536 Functional Programming 43 7/16/2015 integral integral :: Behavior Float -> Behavior Float integral (Behavior fb) = Behavior -> 0 : loop t 0 ts (fb uts)) where loop t0 acc (t1:ts) (a:as) = let acc' = acc + (t1-t0)*a in acc' : loop t1 acc' ts as F x time axis z Integral F z t0 t1 t2 t3 t4 ([ua0,ua1,ua2,ua3, …],[t0,t1,t2,t3, …]) ---> [0, Area t0-t1, Area t0-t2, Area t0-t3, …]

Cse536 Functional Programming 44 7/16/2015 Putting it all together reactimate :: String -> Behavior a -> (a -> IO Graphic) -> IO () reactimate title franProg toGraphic = runGraphics $ do w <- openWindowEx title (Just (0,0)) (Just (xWin,yWin)) drawBufferedGraphic (Just 30) (us,ts,addEvents) <- windowUser w addEvents let drawPic (Just p) = do g <- toGraphic p setGraphic w g addEvents getWindowTick w drawPic Nothing = return () let Event fe = sample `snapshot_` franProg mapM_ drawPic (fe (us,ts))

Cse536 Functional Programming 45 7/16/2015 The Channel Abstraction (us,ts,addEvents) <- windowUser w us, and ts are infinite streams made with channels. A Channel is a special kind of abstraction, in the multiprocessing paradigm. If you “pull” on the tail of a channel, and it is null, then you “wait” until something becomes available. addEvents :: IO () is a action which adds the latest user actions, thus extending the streams us and ts

Cse536 Functional Programming 46 7/16/2015 Making a Stream from a Channel makeStream :: IO ([a], a -> IO ()) makeStream = do ch <- newChan contents <- getChanContents ch return (contents, writeChan ch)

Cse536 Functional Programming 47 7/16/2015 A Reactive window windowUser :: Window -> IO ([Maybe UserAction], [Time], IO ()) windowUser w = do (evs, addEv) <- makeStream t0 <- timeGetTime let addEvents = let loop rt = do mev <- maybeGetWindowEvent w case mev of Nothing -> return () Just e -> addEv(rt, Just e) >> loop rt in do t <- timeGetTime let rt = w32ToTime (t-t0) loop rt addEv (rt, Nothing) return (map snd evs, map fst evs, addEvents)

Cse536 Functional Programming 48 7/16/2015 The “Paddle Ball” Game paddleball vel = walls `over` paddle `over` ball vel walls = let upper = paint blue (translate ( 0,1.7) (rec )) left = paint blue (translate (-2.2,0) (rec )) right = paint blue (translate ( 2.2,0) (rec )) in upper `over` left `over` right paddle = paint red (translate (fst mouse, -1.7) (rec )) x `between` (a,b) = x >* a &&* x <* b

Cse536 Functional Programming 49 7/16/2015 The “reactive” ball ball vel = let xvel = vel `stepAccum` xbounce ->> negate xpos = integral xvel xbounce = predicate (xpos >* 2 &&* xvel >* 0 ||* xpos <* -2 &&* xvel <* 0) yvel = vel `stepAccum` ybounce ->> negate ypos = integral yvel ybounce = predicate (ypos >* 1.5 &&* yvel >* 0 ||* ypos `between` (-2.0,-1.5) &&* fst mouse `between` (xpos- 0.25,xpos+0.25) &&* yvel <* 0) in paint yellow (translate (xpos, ypos) (ell )) main = test (paddleball 1)

Cse536 Functional Programming 50 7/16/2015 Last homework assigned Wednesday. See webpage. Due Wednesday Dec. 8, Final Exam scheduled for Wednesday Dec. 8, 2004