Advanced Programming Handout 10 A Module of Simple Animations (SOE Chapter 13)

Slides:



Advertisements
Similar presentations
Escape Sequences \n newline \t tab \b backspace \r carriage return
Advertisements

Higher-Order Functions and Loops c. Kathi Fisler,
Programming with App Inventor Computing Institute for K-12 Teachers Summer 2012 Workshop.
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
Methods Java 5.1 A quick overview of methods
Chapter 6 Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and.
A Third Look At ML 1. Outline More pattern matching Function values and anonymous functions Higher-order functions and currying Predefined higher-order.
Cse536 Functional Programming 1 1/13/2015 Lecture #13, Nov. 08, 2004 Today’s Topics –Simple animations –Buffered graphics –Animations in Haskell –Complex.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
Kathleen Fisher cs242 Reading: “A history of Haskell: Being lazy with class”,A history of Haskell: Being lazy with class Section 6.4 and Section 7 “Monads.
CSE 123 Symbolic Processing. Declaring Symbolic Variables and Constants To enable symbolic processing, the variables and constants involved must first.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Apply-to-all A functional form that takes a single function as a parameter and yields a list of values obtained.
Chapter 19 An Imperative Robot Language. Motivation  In the previous chapter, monads were introduced.  In particular, state monads were described as.
Using Types Slides thanks to Mark Jones. 2 Expressions Have Types: The type of an expression tells you what kind of value you might expect to see if you.
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.
VBA Modules, Functions, Variables, and Constants
Chapter 17 Rendering Reactive Animations. Motivation  In the previous chapter we learned just enough about advanced IO and concurrency to develop a “renderer”
Chapter 4 Shapes II: Drawing Shapes. Recall the Shape Datatype data Shape = Rectangle Side Side | Ellipse Radius Radius | RtTriangle Side Side | Polygon.
Advanced Programming Handout 9 Qualified Types (SOE Chapter 12)
1 Chapter 3 Arithmetic Expressions. 2 Chapter 3 Topics l Overview of Java Data Types l Numeric Data Types l Declarations for Numeric Expressions l Simple.
Advanced Programming Handout 12 Higher-Order Types (SOE Chapter 18)
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.
Lec 5 Feb 10 Goals: analysis of algorithms (continued) O notation summation formulas maximum subsequence sum problem (Chapter 2) three algorithms image.
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.
FORTRAN.  Fortran or FORmula TRANslation was developed in the 1950's by IBM as an alternative to Assembly Language. First successfull high level language.
Chapter 12 Qualified Types. Motivation  What should the principal type of (+) be? Int -> Int -> Int-- too specific a -> a -> a-- too general  It seems.
Cse536 Functional Programming 1 7/16/2015 Lecture #15, Nov. 15, 2004 Todays Topics – Simple Animations - Review –Reactive animations –Vocabulary – Examples.
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
Local Definitions, Scope, Functional Abstraction, and Polymorphism.
Computer Science 1000 Spreadsheets II Permission to redistribute these slides is strictly prohibited without permission.
XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 1 Microsoft Office Access 2003 Tutorial 11 – Using and Writing Visual Basic for Applications.
Operators, Functions and Modules1 Pattern Matching & Recursion.
Computer Science 111 Fundamentals of Programming I Basic Program Elements.
Haskell. 2 GHC and HUGS Haskell 98 is the current version of Haskell GHC (Glasgow Haskell Compiler, version 7.4.1) is the version of Haskell I am using.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
Case Study - Fractions Timothy Budd Oregon State University.
Variables and Functions. Open your Encoder program Let’s begin by opening the “Labyrinth Auto Straight” code. Save this file as Labyrinth with variables.
Calculations Chapter 11 Library of math functions, and constants cos, sin, tan, abs, min, max, log, random, sqrt, pow, exp Constants.PI,.E Use care with.
ME6104: CAD. Module 4. ME6104: CAD. Module 4. Systems Realization Laboratory Module 4 Matlab ME 6104 – Fundamentals of Computer-Aided Design.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
A Third Look At ML Chapter NineModern Programming Languages, 2nd ed.1.
Introduction to Methods. Previously discussed There are similarities in make up of that can help you remember the construct of a class a class in the.
Topic Review for Exam 1 0.Infrastructure: You must be able to use MATLAB, Blackboard Exams, and Blackboard submissions 1. Computer Developing algorithms.
Functions. Motivation What is a function? A function is a self-contained unit of program code designed to accomplish a particular task. We already used.
Lecture on Set! And Local CS 2135 Copyright Kathi Fisler, 2002 This material requires Advanced Language Level.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
0 PROGRAMMING IN HASKELL Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources) Odds and Ends,
Haskell. GHC and HUGS Haskell 98 is the current version of Haskell GHC (Glasgow Haskell Compiler, version 7.4.1) is the version of Haskell I am using.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
CSE 501N Fall ‘09 03: Class Members 03 September 2009 Nick Leidenfrost.
CS 115 Lecture 5 Math library; building a project Taken from notes by Dr. Neil Moore.
Chapter 7 User-Defined Methods.
Types CSCE 314 Spring 2016.
Haskell.
AFP - Lecture 2 Domain Specific Embedded Languages
JavaScript: Functions.
Lecture 2 Domain Specific Embedded Languages
PROGRAMMING IN HASKELL
Type & Typeclass Syntax in function
CHAPTER 3: String And Numeric Data In Python
Reactive Animations Todays Topics
Defining Classes and Methods
Simple Animations Today’s Topics Reading Assignment Simple animations
Presentation transcript:

Advanced Programming Handout 10 A Module of Simple Animations (SOE Chapter 13)

Motivation In the abstract, an animation is a continuous, time-varying image. In practice, it is a sequence of static images displayed in succession so rapidly that it looks continuous. Our goal is to present to the programmer an abstract view of animations that hides the practical details. In addition, we will generalize animations to be continuous, time-varying quantities of any value, not just images.

Representing Animations As usual, we will use our most powerful tool, functions, to represent animations: type Animation a = Time -> a type Time = Float Examples: rubberBall :: Animation Shape rubberBall t = Ellipse (sin t) (cos t) revolvingBall :: Animation Region revolvingBall t = let ball = Shape (Ellipse ) in Translate (sin t, cos t) ball planets :: Animation Picture planets t = let p1 = Region Red (Shape (rubberBall t)) p2 = Region Yellow (revolvingBall t) in p1 `Over` p2 tellTime :: Animation String tellTime t = "The time is: " ++ show t

An Animator Given a function... animate :: String -> Animation Graphic -> IO ( )...we could then execute (display) the previous animations like this: main1 :: IO ( ) main1 = animate "Animated Shape“ (withColor Blue. shapeToGraphic. rubberBall) main2 :: IO ( ) main2 = animate "Animated Text“ (text (100,200). tellTime)

Definition of “animate” animate :: String -> Animation Graphic -> IO ( ) animate title anim = runGraphics $ do w <- openWindowEx title (Just (0,0)) (Just (xWin,yWin)) drawBufferedGraphic (Just 30) t0 <- timeGetTime let loop = do t <- timeGetTime let ft = intToFloat (word32ToInt (t-t0)) / 1000 setGraphic w (anim ft) getWindowTick w loop loop See text for details...

Common Operations We can define many operations on animations based on the underlying type. For example, for Pictures: emptyA :: Animation Picture emptyA t = EmptyPic overA :: Animation Picture -> Animation Picture -> Animation Picture overA a1 a2 t = a1 t `Over` a2 t overManyA :: [Animation Picture] -> Animation Picture overManyA = foldr overA emptyA We can do a similar thing for Shapes, etc. Also, for numeric animations, we could define functions like addA, multA, and so on. But there is a better way... (naturally)

Behaviors Basic definition (replacing Animation ): newtype Behavior a = Beh (Time -> a) Recall that newtype creates a single-argument datatype with (time and space) efficiency the same as a simple type declaration. (So then what is the difference??)

Behaviors We need to use newtype here because type synonyms are not allowed in type class instance declarations -- only types declared with data or newtype.

Constant Behaviors Given a scalar value x, we can lift it to a constant behavior that, at all times t, yields x : lift0 :: a -> Behavior a lift0 x = Beh (\t -> x)

Dependent Behaviors Given a function f, we can lift it to a function on behaviors that, at a given time t, samples its argument and passes the result through f : lift1 :: (a -> b) -> (Behavior a -> Behavior b) lift1 f (Beh a) = Beh (\t -> f (a t))

Numeric Behaviors instance Num a => Num (Behavior a) where (+) = lift2 (+) (*) = lift2 (*) negate = lift1 negate abs = lift1 abs signum = lift1 signum fromInteger = lift0. fromInteger...where: lift0 :: a -> Behavior a lift0 x = Beh (\t -> x) lift1 :: (a -> b) -> (Behavior a -> Behavior b) lift1 f (Beh a) = Beh (\t -> f (a t)) lift2 :: (a -> b -> c) -> (Behavior a -> Behavior b -> Behavior c) lift2 g (Beh a) (Beh b) = Beh (\t -> g (a t) (b t)) instance Floating a => Floating (Behavior a) where pi = lift0 pi sqrt = lift1 sqrt exp = lift1 exp log = lift1 log sin = lift1 sin cos = lift1 cos tan = lift1 tan etc....and similarly for the other basic classes (Fractional, etc.)

Type Class Magic Furthermore, define time as a behavior: time :: Behavior Time time = Beh (\t -> t) Now consider the expression “ time + 42 ”: time + 42  unfold overloaded defs of time, (+), and 42 (lift2 (+)) (Beh (\t -> t)) (Beh (\t -> 42))  unfold lift2 (\ (Beh a) (Beh b) -> Beh (\t -> a t + b t) ) (Beh (\t -> t)) (Beh (\t -> 42))  perform some anonymous function applications Beh (\t -> (\t -> t) t + (\t -> 42) t )  and two more Beh (\t -> t + 42) this is cool

New Type Classes Besides lifting existing type classes such as Num to behaviors, we can define new classes for manipulating behaviors. For example: class Combine a where empty:: a over :: a -> a -> a instance Combine Picture where empty = EmptyPic over = Over instance Combine a => Combine (Behavior a) where empty = lift0 empty over = lift2 over overMany :: Combine a => [a] -> a overMany = foldr over empty

Hiding More Detail We have not yet hidden all the “practical” details of animation – in particular time itself. But through more aggressive lifting... reg = lift2 Region shape = lift1 Shape ell = lift2 Ellipse red = lift0 Red yellow = lift0 Yellow translate (Beh a1, Beh a2) (Beh r) -- note subtlety here = Beh (\t -> Translate (a1 t, a2 t) (r t))... we can redefine our red revolving ball without referring to time at all: revolvingBallB :: Behavior Picture revolvingBallB = let ball = shape (ell ) in reg red (translate (sin time, cos time) ball)

More Liftings Comparison operators: (>*) :: Ord a => Behavior a -> Behavior a -> Behavior Bool (>*) = lift2 (>) Conditional behaviors: cond :: Behavior Bool -> Behavior a -> Behavior a -> Behavior a cond = lift3 (\p c a -> if p then c else a) For example, a flashing color: flash :: Behavior Color flash = cond (sin time >* 0) red yellow

Time Travel A function for translating a behavior through time: timeTrans :: Behavior Time -> Behavior a -> Behavior a timeTrans (Beh f) (Beh a) = Beh (a. f) For example: timeTrans (2*time) anim-- double speed (timeTrans (5+time) anim) `over` anim-- one anim 5 sec behind another timeTrans (negate time) anim-- go backwards Any kind of behavior can be time transformed: flashingBall :: Behavior Picture flashingBall = let ball = shape (ell ) in reg (timeTrans (8*time) flash) (translate (sin time, cos time) ball)

Final Example revolvingBalls :: Behavior Picture revolvingBalls = overMany [ timeTrans (time + t*pi/4) flashingBall | t <- map lift0 [0..7] ] See SOE for a more substantial example: a kaleidoscope program. (The details of its construction can be skimmed, but you may enjoy running it...)