Teaching Haskell To Juggle

         Phil Freeman
Agenda
•   Theory
•   Finding valid patterns and their states
•   Why Haskell?
•   If there’s time:
    – Animating siteswaps with HOpenGL
    – Type level siteswaps
Siteswap Notation
• Encodes timing of throws
• Ignores hand positions
• E.g.
  – 3-ball cascade ‘3,3,3,3,…’
  – 3-ball shower ‘5,1,5,1,…’
  – 4-ball fountain ‘4,4,4,4,…’
Siteswap Notation
• Hands alternate
  – Even numbers indicate throws to the same hand
  – Odd numbers indicate throws to the other hand
• Current ball will be thrown again after N-1
  beats
• Period = length (* 2)
• Number of props = average value
• 0 is a pause with an empty hand
Space Time Diagrams
• Tracks the orbit of each balls between the
  hands
• Axes represent
  – Time
  – Ball position
• Time axis is divided into beats
Space Time Diagrams
                            LH

333

                            RH
                            LH

15

                            RH

                            LH

423

                            RH
State Diagrams
•   Tracks when balls in the air will land
•   An × indicates a ball scheduled to land
•   A - indicates a free hand
•   E.g.
    – 3-ball cascade: ×××--
    – 3-ball shower: ×-×-× → ××-×- → ×-×-×
    – 4-ball fountain: ××××-
State Diagrams
• To “throw” a 0, shift the pattern to the left:
  – -×××- →0 ×××--
• To throw to height N, shift and insert an × at
  the Nth place:
  – ×××-- →5 ××--×
• State diagrams and heights form the vertices
  and edges of a directed graph.
• Valid siteswaps are cycles in this graph.
State Diagrams


 ×××--                 ××-×-



 -×××-                 ×-××-




E.g. 3 props, maximum height 4
Translation
             Siteswap       Space Time      State Diagram
Period       Length (* 2)   Tiling Period   Cycle Length (* 2)
# of Props   Average        # of Orbits     # of ×’s
Height       Value          Horizontal      Max node label
                            Distance        length
Why Haskell?
• Type safety
• Side effects are isolated
• Succinct
  – Pattern matching
  – Polymorphism
  – Higher order functions
Code Break
•   Finding Valid Siteswaps
•   throw, throwMany, foldM
•   graph, generateGraph
•   E.g. all patterns decorating a 3-cascade
•   E.g. over combinator
HOpenGL
• Provides a high-level interface to the OpenGL
  bindings.
• Computations take place in IO
• E.g. preservingMatrix :: IO a -> IO a
• Use powerful existing tools, e.g. mapM
Code Break
• Animating Siteswaps
• IORef
• E.g. insert
Type Level Siteswaps
• Idea: Can we use the type system to identify
  valid siteswaps?
• E.g. Can we improve on the type of (++) for
  concatenating patterns?
• Use singleton types, type classes and fundeps
  to encode the state graph in the type system.
Code Break
• Type Level Siteswaps
Questions?

Teaching haskell to juggle

  • 1.
    Teaching Haskell ToJuggle Phil Freeman
  • 2.
    Agenda • Theory • Finding valid patterns and their states • Why Haskell? • If there’s time: – Animating siteswaps with HOpenGL – Type level siteswaps
  • 3.
    Siteswap Notation • Encodestiming of throws • Ignores hand positions • E.g. – 3-ball cascade ‘3,3,3,3,…’ – 3-ball shower ‘5,1,5,1,…’ – 4-ball fountain ‘4,4,4,4,…’
  • 4.
    Siteswap Notation • Handsalternate – Even numbers indicate throws to the same hand – Odd numbers indicate throws to the other hand • Current ball will be thrown again after N-1 beats • Period = length (* 2) • Number of props = average value • 0 is a pause with an empty hand
  • 5.
    Space Time Diagrams •Tracks the orbit of each balls between the hands • Axes represent – Time – Ball position • Time axis is divided into beats
  • 6.
    Space Time Diagrams LH 333 RH LH 15 RH LH 423 RH
  • 7.
    State Diagrams • Tracks when balls in the air will land • An × indicates a ball scheduled to land • A - indicates a free hand • E.g. – 3-ball cascade: ×××-- – 3-ball shower: ×-×-× → ××-×- → ×-×-× – 4-ball fountain: ××××-
  • 8.
    State Diagrams • To“throw” a 0, shift the pattern to the left: – -×××- →0 ×××-- • To throw to height N, shift and insert an × at the Nth place: – ×××-- →5 ××--× • State diagrams and heights form the vertices and edges of a directed graph. • Valid siteswaps are cycles in this graph.
  • 9.
    State Diagrams ×××-- ××-×- -×××- ×-××- E.g. 3 props, maximum height 4
  • 10.
    Translation Siteswap Space Time State Diagram Period Length (* 2) Tiling Period Cycle Length (* 2) # of Props Average # of Orbits # of ×’s Height Value Horizontal Max node label Distance length
  • 11.
    Why Haskell? • Typesafety • Side effects are isolated • Succinct – Pattern matching – Polymorphism – Higher order functions
  • 12.
    Code Break • Finding Valid Siteswaps • throw, throwMany, foldM • graph, generateGraph • E.g. all patterns decorating a 3-cascade • E.g. over combinator
  • 13.
    HOpenGL • Provides ahigh-level interface to the OpenGL bindings. • Computations take place in IO • E.g. preservingMatrix :: IO a -> IO a • Use powerful existing tools, e.g. mapM
  • 14.
    Code Break • AnimatingSiteswaps • IORef • E.g. insert
  • 15.
    Type Level Siteswaps •Idea: Can we use the type system to identify valid siteswaps? • E.g. Can we improve on the type of (++) for concatenating patterns? • Use singleton types, type classes and fundeps to encode the state graph in the type system.
  • 16.
    Code Break • TypeLevel Siteswaps
  • 17.