Functional Reactive Programming Lecture 6, Designing and Using Combinators John Hughes.

Slides:



Advertisements
Similar presentations
Create a Simple Game in Scratch
Advertisements

Microsoft® Small Basic
Visual Basic: ballistics
Chapter 6 Photoshop and ImageReady: Part II The Web Warrior Guide to Web Design Technologies.
Microsoft® Small Basic
Introduction to Alice Web Design Section 8-2 Alice is named in honor of Lewis Carroll’s Alice in Wonderland.
CS514: Intermediate Course in Operating Systems Professor Ken Birman Vivek Vishnumurthy: TA.
Sept 4, 2001ICFP, Florence, Italy 1 Real-Time FRP Zhanyong Wan Yale University Department of Computer Science Joint work with: Walid Taha Paul Hudak.
Fun with Differentiation!
Essentials of Interactive Computer Graphics: Concepts and Implementation K. Sung, P. Shirley, S. Baer Intro and Chapter 1 Goals Give student some idea.
Sketchify Tutorial Graphics and Animation in Sketchify sketchify.sf.net Željko Obrenović
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.
Free Fall Lecture 3.
3.02 Explain basic motion graphic programming.
Introduction to Alice Alice is named in honor of Lewis Carroll’s Alice in Wonderland.
SE320: Introduction to Computer Games Week 8: Game Programming Gazihan Alankus.
INTRODUCTION TO SCRATCH. About Me Resources Scratch Website Learn Scratch Washington-Lee Computer.
PowerPoint Animation Part 2 The Car Race Dr Nitin Paranjape
CMSC 611: Advanced Computer Architecture Performance Some material adapted from Mohamed Younis, UMBC CMSC 611 Spr 2003 course slides Some material adapted.
Sketchlet Tutorial Defining Interaction and Logic in Sketchlet sketchlet.sf.net Željko Obrenović obren.info/
Kinetic energy. Equations The kinetic energy of a moving object is one half of the product of its mass multiplied by the square of its velocity. or.
Control technology. What and why? All the automatic electronic devices that regulate and control other devices. Control technology… …helps children understand.
Game Maker Day 2 Making a Maze Game.
Lecture 5: Interaction 1  Principles of Interactive Graphics  CMSCD2012  Dr David England, Room 711,  ex 2271 
CIS 540 Principles of Embedded Computation Spring Instructor: Rajeev Alur
Art 315 Lecture 5 Dr. J. Parker AB 606. Last time … We wrote our first program. We used a tool called GameMaker. The program we wrote causes a ball to.
1 Instant Data Warehouse Utilities Extended 2/4/ Today I am pleased to announce the publishing of some promised new functionality for the Instant.
My First Problem of the Day:. Point-Slope Equation of a Line: Linearization of f at x = a: or.
Tot 15 LTPDA Graphic User Interface summary and status N. Tateo 26/06/2007.
Output Design. Output design  Output can be: Displayed on a screen/VDU/monitor. Printed on paper as hard copy. Sound.
Introduction to Windows Programming
UML-1 3. Capturing Requirements and Use Case Model.
Lecture 19 Goals: Chapter 14 Assignment Periodic motion.
Obj: Introduction to Alice HW: Read handout and answer questions. Alice is named in honor of Lewis Carroll’s Alice in Wonderland Day 5.
Physics 207: Lecture 19, Pg 1 Lecture 19Goals: Chapter 14 Chapter 14  Interrelate the physics and mathematics of oscillations.  Draw and interpret oscillatory.
What are Buttons? Buttons can be clipart, pictures or animated gifs When you click on buttons they can send you to another slide Buttons can be used to.
Processor Architecture
Introduction to Alice Alice is named in honor of Lewis Carroll’s Alice in Wonderland.
SIMULINK-Tutorial 1 Class ECES-304 Presented by : Shubham Bhat.
Physics + Vectors References: xyz. Variable frame rates (review) Two options for handling it: – Option1: Cap frame rates When moving / rotating express.
Introduction to Alice Web Design Section 8-2 Alice is named in honor of Lewis Carroll’s Alice in Wonderland.
Introduction to Game Programming & Design III Lecture III.
(More) Event handling. Input and Windowed-OS’s Windowed OS’s (Windows, OSX, GUI-linux’s) send messages (events) when the user does something “on” the.
Creating a Simple Game in Scratch Barb Ericson Georgia Tech May 2009.
Physics 141MechanicsLecture 2 Kinematics in One Dimension Kinematics is the study of the motion of an object. We begin by studying a single particle in.
Thursday, 18 January 2007 Multimedia Architectures Ines Färber and Alexander G.M. Hoffmann Advisor: Thorsten Karrer Post Desktop User Interfaces.
Game Maker Tutorials Introduction Clickball IntroductionClickball Where is it? Shooting Where is it?Shooting.
Reference: What is it? A multimedia python library – Window Management – Graphics geometric shapes bitmaps (sprites) – Input Mouse Keyboard.
MOM! Phineas and Ferb are … Aims:
Functions Sec 51 Web Design.
Game Maker Intro to Programming Game Maker Pop Quiz (Both Groups)
a) More on Elastic Collisions
Goals Give student some idea what this class is about
AFP - Lecture 2 Domain Specific Embedded Languages
Functions Sec 8-11 Web Design.
Lecture 2 Domain Specific Embedded Languages
Understand Windows Forms Applications and Console-based Applications
Introduction to Programming
CSCI1600: Embedded and Real Time Software
CMSC 611: Advanced Computer Architecture
Benefits of PowerPoint
Copyright © Cengage Learning. All rights reserved.
Game Maker Intro to Programming Game Maker Pop Quiz (Both Groups)
CMSC 611: Advanced Computer Architecture
Click Summary Value Button to Show Source of Integral or Time
Physics 111: Lecture 15 Today’s Agenda
Presentation transcript:

Functional Reactive Programming Lecture 6, Designing and Using Combinators John Hughes

What is FRP? A DSEL designed for describing behaviour which varies over time. Functional “Reactive” Programs can react to events in their environment. First developed in the context of Functional Reactive Animation (Fran). Not a monad in sight...

Behaviours The most basic concept in FRP: Behaviour a  Time -> a Examples time :: Behaviour Time wiggle :: Behaviour Double wiggle = sin (pi*time) Overloading lets us treat behaviours as numbers.

Behaviours The most basic concept in FRP: Behaviour a  Time -> a Examples time :: Behaviour Time wiggle :: RealB wiggle = sin (pi*time) Abbreviate behaviour types

Behaviours and Animations Behaviours need not be numeric. clock :: StringB clock = lift1 show time clockImage :: ImageB clockImage = stringBIm clock Image behaviours are animations!

Moving Pictures Moving pictures can be created by moveXY: moveXY x y = move (vector2XY x y) anim = moveXY wiggle 0 mary where mary = importBitmap "maryface.bmp” Works on vectors

Stretching Pictures Images can be rescaled, by a behaviour: stretch wiggle mary

Delaying Behaviours Behaviours can be delayed using later: waggle = later 0.5 wiggle Out of phase In fact, we can transform the time in any way! wiggle `timeTransform` (time/2) Runs at half speed

Orbiting Mary orbitingMary = moveXY wiggle waggle mary

More Orbiting Fun orbit b = moveXY wiggle waggle b pic = orbit (stretch 0.5 (faster 3 (orbit mary)))

Combining Pictures We can combine pictures with over: pic = orbit (stretch 0.5 mary) `over` mary

Reactive Animations So far, these animations ignore their environment. How can we make them react to the user? displayU :: (User -> ImageB) -> IO () Can extract information about the user

Following the Mouse mouseMotion :: User -> Vector2B Follow the mouse: move (mouseMotion u) mary Follow the mouse with a delay: later 1 $ move (mouseMotion u) mary

Differential Calculus We can even differentiate and integrate behaviours! accel u = mouseMotion u velocity u = integral (accel u) u position u = initpos + integral (velocity u) u We can easily build physical models of differential equations! We’ll see a spring demo later Numerical methods inside

Reacting to Events Behaviours are continuous, but sometimes we should react to discrete events. Conceptually, events are Maybe a-behaviours! Implemented as a separate type. Event a  [(Time,a)]

Reacting to Events untilB :: Behaviour a -> Event (Behaviour a) -> Behaviour a (==>) :: Event a -> (a -> b) -> Event b (-=>) :: Event a -> b -> Event b Example: stop on mouse click orbit mary `untilB` lbp u -=> mary

Mouse Button Events lbp, lbr, rbp, rbr :: User -> Event () Left/right button Press/release Let’s make mary bigger while the mouse button is pressed! size u = 0.5 `untilB` nextUser_ lbp u ==> \u’-> 1.0 `untilB` nextUser_ lbr u’ ==> \u”-> size u” Event generates the next user state

Multiple Events We can combine events, to wait for whichever happens first updown n u = n `untilB` (nextUser_ lbp u ==> updown (n+1).|. nextUser_ rbp u ==> updown (n-1)) stretch (0.3*updown 3 u) mary

Generating Events from Behaviours Suppose we want to model a bouncing ball. We must detect collisions -- when the position reaches the ground! predicate :: BoolB -> User -> Event ()

Modelling a Bouncing Ball accel u = -1 speed u = 1+integral (accel u) u height u = integral (speed u) u `untilB` nextUser_ collision u ==> height where collision u = predicate (height u <* 0 &&* speed u <* (0::RealB)) u ball = stretch 0.1 circle Starred operators work on behaviours

Time for Conal Elliott’s Demos...

Assessment Fran provides a small number of composable operations on behaviours and events. With these a rich variety of animations can be expressed Performance is good, since rendering is done by standard software FRP works in many other contexts: - Frob for robotics - Fruit for graphical user interfaces

How Does it Work? Representing Behaviours Behaviour a = Time -> a would be much too inefficient. We would need to recompute the entire history to do an integral! Behaviour a = Time -> (a, Behaviour a) Simplified (faster) behaviour, useable at later times.

How Does it Work? Detecting Predicate Events predicate :: (Time->Bool) -> Event () would be far too inefficient! We would need to try every time (double precision floats!) to be sure to detect events!

How Does it Work? Detecting Predicate Events Key Idea: Interval analysis data Ival a = a `UpTo` a Behaviours become: data Behaviour a = Behaviour (Time -> (a, Behaviour a)) (Ival Time -> (Ival a, Behaviour a)) If f (t1`UpTo`t2) = False`UpTo`False, the event does not occur between t1 and t2.

Summary FRP is a non-monadic DSEL which makes time-dependent behaviour very simple to express. Excellent example of capturing the semantics of the application. It’s fun! Download Fran and try it out!

Summary FRP is a non-monadic DSEL which makes time-dependent behaviour very simple to express. Excellent example of capturing the semantics of the application. It’s fun! Download Fran and try it out! Now for Conal Elliott’s latest: a quick Pan demo!