Title: Having Fun with Functions How you can use functions to: write interactive animation, control robots,
1Having Fun with Functions How you can use
functions to write interactive
animation, control robots, and compose music.
- Zhanyong Wan, Yale UniversitySeptember, 2001
- Acknowledgement Part of the slides provided by
Paul Hudak
2Roadmap
- ? Programming languages research at Yale
- Introduction to Haskell
- Domain specific languages
- FRP
- Haskore
- Conclusions and video
3Alan Perlis Epigrams in Programming
- A programming language is low level when its
programs require attention to the irrelevant. - If a listener nods his head when you're
explaining your program, wake him up. - Optimization hinders evolution.
- Once you understand how to write a program get
someone else to write it. - The use of a program to prove the 4-color theorem
will not change mathematics - it merely
demonstrates that the theorem, a challenge for a
century, is probably not important to
mathematics. - There are two ways to write error-free programs
only the third one works. - A language that doesn't affect the way you think
about programming, is not worth knowing.
4PL Research at Yale
- Faculty members (a partial list)
- Paul Hudak functional programming (Haskell),
domain specific languages, language theory. - John Peterson functional programming (Haskell),
domain specific languages, compilation - Zhong Shao functional programming (ML), type
theory, compilation techniques, proof-carrying
code. - Carsten Schuermann logical frameworks, theorem
proving, type theory, functional programming.
5Having Fun at Yale
audio
- John Peterson
- Rock climbing
- Skiing
- Kayaking
- Paul Hudak
- Music
- Soccer coaching
- Skiing
- Rock climbing
- Kayaking
6Roadmap
- Programming languages research at Yale
- ? Introduction to Haskell
- Domain specific languages
- FRP
- Haskore
- Conclusions and video
7Haskell by Examples
- Higher-order functions
- Functions are first-class values
- Currying
- max (Int,Int) ? Intmax (x,y) if x gt y then
x else ymax(5,6) ? 6 - max Int ? (Int ? Int)max x y if x gt y then
x else ymax 5 6 ? 6 - m5 Int ? Intm5 max 5 ltgt m5 y if 5 gt y
then 5 else ym5 4 ? 5m5 6 ? 6
In C, this would be int max(int, int)
8Haskell by Examples
- Recursion
- sum 0sum (xxs) x sum xs
- prod 1prod (xxs) x prod xs
- Higher-order functions improve modularization
- Modularization is the key to successful
programming! - fold f a afold f a (xxs) f x (fold f a
xs) - sum fold () 0
- prod fold () 1
- allTrue fold () True
- anyTrue fold () False
9Haskell by Examples
- Lazy evaluation
- Call-by-need
- f x y if x gt 0 then x else y f e1 e2
- Infinite data structures
- ones 1 ones ? 1 1 1 1 1
- numsFrom n n numsFrom (n1) ? n n1
n2 n3 - squares map (2) (numsFrom 0) ? 0 1 4
9 16 25 36 - take 5 squares ? 0, 1, 4, 9, 16
- fib 1 1 zipWith () fib (tail fib) ?
1 1 2 3 5 8 13 () 1 2
3 5 8 13 21 ---------------------
----------------1 1 2 3 5 8 13 21
34
10Haskell by Examples
- Lazy evaluation
- Another powerful tool for improving modularity
- sort xs take n xs smallest n x take n
(sort x) - Only practical for lazy languages
- Need-driven no unnecessary computation
- No large intermediate data structure
11Haskell by Examples
- Advanced type systems
- Polymorphic
- zipWith ?a,b,c. (a?b?c) ? a ? b ? c
- Type inference
- zipWith f _ zipWith f _ zipWith f
(xxs) (yys) f x y zipWith f xs ys - a ? b ? c ? d refine ?a ? b ? e ? d
refine ?(b ? e ? d) ? b ? e ? d rename
?(a ? b ? c) ? a ? b ? c
12Roadmap
- Programming languages research at Yale
- Introduction to Haskell
- ? Domain specific languages
- FRP
- Haskore
- Conclusions and video
13Animation in a General-Purpose Language
- Sketch of the code (taken from Elliotts Fran
manual) - allocate and initialize window, various drawing
surfaces and bitmaps - repeat until quit
- get time ( t )
- clear back buffer
- for each sprite (back to front)
- compute position, scale, etc. at t
- draw to back buffer
- flip back buffer to the screen
- deallocate bitmaps, drawing surfaces, window
- Lots of tedious, low-level code that you have to
write yourself - There is a better way!
14We Need Domain Specificity
- A domain-specific language (or DSL) is a language
that precisely captures a domain semantics no
more, and no less. - Examples
- SQL, UNIX shells, Perl, Prolog
- FRP, Haskore
15Advantages of DSL Approach
- Programs in the target domain are
- more concise
- quicker to write
- easier to maintain
- easier to reason about
- can often be written by non-programmers
Contribute to higher programmer productivity
Dominant cost in large SW systems
Verification, transform- ation, optimization
These are the same arguments in favor of any
high-level language. But in addition
Helps bridge gap between developer and user
16The Bottom Line
Conventional
Total SW Cost
Methodology
C2
Start
-
up
DSL
-
based
Costs
Methodology
C1
Software Life-Cycle
17DSLs Allow Faster Prototyping
Using the Spiral Model of Software Development
specify
specify
design
design
build
test
test
build
With DSL
Without DSL
18Roadmap
- Programming languages research at Yale
- Introduction to Haskell
- Domain specific languages
- ? FRP
- Haskore
- Conclusions and video
19Reactive Programming
- Reactive systems
- Continually react to stimuli
- Environment cannot wait system must respond
quickly - Run forever
- Examples
- Computer animation
- GUI
- Robotics
- Vision
- Control systems
- Real-time systems
20Functional Reactive Programming
- Functional Reactive Programming (FRP)
- Focuses on model instead of presentation
- Continuous time domain
- Discrete time domain
- Recursion higher-order functions
- Originally embedded in Haskell
- Continuous/discrete signals
- Behaviors values varying over time
- Events discrete occurrences
- Reactivity b1 until ev -gt b2
- Combinators until, switch, when, integral, etc
21What Is FRP Good for?
- Applications of FRP
- Fran Elliott and Hudak Interactive animation
- Frob Peterson, Hager, Hudak and Elliott
Robotics - FVision Reid, Peterson, Hudak and Hager Vision
- Frappé Courtney Java
- FranTk Sage, Fruit Courtney GUI
moveXY (sin time) 0 charlotte charlotte
importBitmap charlotte.bmp
22Basic Concepts
- Behaviors (Behavior a)
- Reactive, continuous-time-varying values
- time Behavior Time
- mouse Behavior Point2
- Events (Event a)
- Streams of discrete event occurrences
- lbp Event ()
- Switching
- b1 till lbp -gt b2
- Combinators
- Behaviors and events are both first-class
- Passed as arguments
- Returned by functions
- Composed using combinators
23Behaviors
- Time
- time Behavior Time
- Input
- mouse Behavior Point2
- Integration
- integral Behavior Double
- -gt Behavior Double
- (integral time)
- Constant behaviors
- lift0 a -gt Behavior a
- (lift0 5)
24Lifting
- Static value -gt behavior
- lift0 a -gt Behavior a
- lift1 (a -gt b) -gt Behavior a -gt Behavior b
- lift2 (a -gt b -gt c)
- -gt Behavior a -gt Behavior b -gt Behavior c
- ...
- Examples
- lift1 sin time
- ? sin time
- lift2 () (lift0 1) (lift1 sin time)
- ? 1 sin time
time
time
25Simple Behaviors
demo
- Hello, world!
- hello Behavior Picture
- hello text lift0 "Hello, world!"
- main1 animate hello
- Interaction mouse-following
- main2 animate moveTo mouse hello
- Time
- main3 animate text lift1 show time
26Geometry in FRP
- 2-D
- Points
- Vectors
- Common shapes
- Circle, line, polygon, arc, and etc
- Affine transformations
- Rotation
- Translation
- Reflection
- Scaling
- Shear
- Composition of the above
- 3-D as well
27Spatial Transformations
demo
- Spatial transformations -- dancing ball
- ball Behavior Picture
- ball withColor red stretch 0.1 circle
- wiggle, waggle
- Behavior Double -gt Behavior Double
- wiggle omega sin (omegatime)
- waggle omega cos (omegatime)
- main4 om1 om2 animate
- moveXY (wiggle om1) (waggle om2)
- moveTo mouse ball
28Spatial Composition
demo
- Spatial composition -- dancing ball text
- main5 animate moveTo mouse
- moveXY (wiggle 4) (waggle 4) ball over
- moveXY (wiggle 7) (waggle 3) hello
29Recursive Behaviors
- Damped motion of a mass dragged with a spring
- Integral equations
- Recursive
d pm po a ksd kfv v ? a dt po ? v dt
d
pm
v
po
ks d
-kf v
30Recursive Behaviors (contd)
demo
- FRP Code
- main6 animate
- moveTo po ball over withColor blue (line
mouseB po) - where
- ks 1
- kf 0.8
- d mouse .-. po
- a ks d kf v
- v integral a
- po vector2ToPoint2 integral v
- Declarative
- Concise
- Recursive
d pm po a ksd kfv v ? a dt po ? v dt
31Switching
demo
- Switching of behaviors
- color Behavior Color
- color red till lbp -gt blue
- mouseBall Behavior Picture
- mouseBall moveTo mouse stretch 0.5 circle
- main7 animate withColor color mouseBall
- Recursive switching
- cycle3 c1 c2 c3 c1 till lbp
- -gt cycle3 c2 c3 c1
- main8 animate
- withColor (cycle3 red green blue) mouseBall
32Paddle Ball in 17 Lines
video
- paddleball vel walls over paddle over pball
velwalls let upper paint blue (translate (
0,1.7) (rec 4.4 0.05)) left paint
blue (translate (-2.2,0) (rec 0.05
3.4)) right paint blue (translate (
2.2,0) (rec 0.05 3.4)) in upper over
left over rightpaddle paint red (translate
(fst mouse, -1.7) (rec 0.5 0.05))pball vel
let xvel vel stepAccum xbounce -gtgt
negate xpos integral xvel
xbounce when (xpos gt 2 xpos lt -2)
yvel vel stepAccum ybounce -gtgt
negate ypos integral yvel
ybounce when (ypos gt 1.5
ypos between (-2.0,-1.5)
fst mouse between (xpos-0.25,xpos0.25))
in paint yellow (translate (xpos, ypos) (ell
0.2 0.2))x between (a,b) x gt a x lt b
33Crouching Robots, Hidden Camera
- RoboCup
- Team controllers written in FRP
- Embedded robot controllers written in C(our
goal use FRP instead)
camera
Team B controller
Team A controller
radio
radio
34A Point-tracking Control System
- Input
- xd, yd, ?d desired position heading
- x, y, ? actual position heading
- Output
- u forward speed
- v rotational speed
?d
(xd,yd)
u
?
v
(x,y)
35A Point-tracking System
video
- u u_d - k1 z1
- v k2 derivativeB theta_d dTheta - r1
z1 - r2 z2 - dTheta lift1 normalizeAngle theta -
theta_d - u_d derivativeB x_d cos theta_d
derivativeB y_d sin theta_d - z1 (x x_d) cos theta (y y_d) sin
theta - z2 (x x_d) sin theta (y y_d) cos
theta - r1 ifZ (abs dTheta lt 0.01) 0 (u_d(1 -
cos dTheta)/dTheta) - r2 ifZ (abs dTheta lt 0.01) (-u_d)
(-u_d sin dTheta / dTheta)
36Roadmap
- Programming languages research at Yale
- Introduction to Haskell
- Domain specific languages
- FRP
- ? Haskore
- Conclusions and video
37Haskore
- Motivation
- Traditional music notation has many limitations
- Unable to express a composers intentions.
- Biased toward music that is humanly performable.
- Unable to express notions of algorithmic
composition.Haskore (Haskell Score) is a
library of Haskellfunctions and datatypes for
composing music.
38Basic Haskore Structure
type Pitch (PitchClass, Octave) data
PitchClass Cf C Cs Df D Ds Ef E
Es Ff F Fs Gf G Gs
Af A As Bf B Bs type Octave
Int data Music Note Pitch Dur --
a note Rest Dur -- a rest
Music Music -- sequential composition
Music Music -- parallel composition
Tempo Int Int Music -- scale the tempo
Trans Int Music -- transposition
Instr IName Music -- instrument
label type Dur Float -- in whole notes type
IName String type PName String
39Gloveby Tom Makucevichwith help from Paul
HudakComposed using Haskore, a Haskell DSL,and
rendered using csound.
40Roadmap
- Programming languages research at Yale
- Introduction to Haskell
- Domain specific languages
- FRP
- Haskore
- ? Conclusions and video
41Conclusions
video
- Haskell has changed the way many people think
about programming. It is worth knowing! - Functional programming community have some good
ideas. Lets start using them! - Functional programming is fun!
- Check out our web-site!
- Haskell http//haskell.org/
- FRP http//haskell.org/frp/
- Haskore http//haskell.org/haskore/
- Hugs http//haskell.org/hugs/
- Life at Yale is fun! (See the video)