MASTER THESIS Reactive Programming with Events
Total Page:16
File Type:pdf, Size:1020Kb
Charles University in Prague Faculty of Mathematics and Physics MASTER THESIS Tomáš Petříček Reactive Programming with Events Department of Theoretical Computer Science and Mathematical Logic Supervisor: Don Syme Study programme: Theoretical Computer Science 2010 I hereby certify that I wrote the thesis myself using only the referenced sources. I give consent with lending the thesis. Prague, 15 April, 2010 Tomáš Petříček 1 Contents Introduction ............................................................................................................... 7 1. Standard event handling techniques ................................................................... 7 2. Approach and existing work ................................................................................... 9 3. Main contributions of this thesis ......................................................................... 11 Background and related work ...........................................................................13 1. Functional techniques .............................................................................................. 13 2. First-class compositional events ......................................................................... 16 3. Monads and computation expressions.............................................................. 20 4. Synchronous languages ........................................................................................... 28 5. Concurrent languages .............................................................................................. 31 6. Reactive languages .................................................................................................... 37 Approach and problem description .................................................................43 1. Reactive programming ............................................................................................ 43 2. Issues and limitations .............................................................................................. 46 3. Approach description .............................................................................................. 48 4. Sample solution .......................................................................................................... 50 Garbage collection for reactive programs .....................................................52 1. Problems with mixing styles ................................................................................. 52 2. Garbage in the dual world ...................................................................................... 55 3. Garbage collection algorithm ................................................................................ 57 4. Implementing the reactive library ...................................................................... 61 5. Correspondence with the model ......................................................................... 66 6. Chapter summary and related work .................................................................. 68 Pattern matching for reactive and concurrent programming ...............70 1. Motivation .................................................................................................................... 71 2. Monadic pattern matching ..................................................................................... 74 3. Semantics ...................................................................................................................... 79 4. Merging computations ............................................................................................. 81 5. Choosing ........................................................................................................................ 87 6. Reasoning about monadic matching .................................................................. 88 7. Design alternatives and future work ................................................................. 90 8. Chapter summary and related work .................................................................. 94 Reactive event-driven computations...............................................................96 1. Reactive library by example .................................................................................. 97 2. Case Study: Simple reactive game .................................................................... 101 3. Formal semantics ................................................................................................... 108 4. Guarantees ................................................................................................................ 120 5. Abstract imperative computations .................................................................. 123 2 Ideas for future work ......................................................................................... 128 1. Garbage collection in reactive scenario ......................................................... 128 2. Compile time checking for match! ................................................................... 129 3. Committing monadic computations ................................................................ 130 4. Automatic verification of reactive programs ............................................... 131 5. Real-world reactive scenarios ........................................................................... 132 Overview of contributions and conclusion ................................................. 134 1. Overview of contributions .................................................................................. 134 2. Conclusion ................................................................................................................. 136 Imperative and object-oriented F# ............................................................... 141 3. Imperative programming .................................................................................... 141 4. Object-oriented programming .......................................................................... 142 Counting clicks using combinators ............................................................... 144 Merge for commutative monads .................................................................... 145 3 Title: Reactive Programming with Events Author: Tomáš Petříček Department: Department of Theoretical Computer Science and Mathematical Logic Supervisor: Don Syme, Microsoft Research Cambridge Supervisor's e-mail address: [email protected] Abstract: The reactive programming model is largely different to what we’re used to as we don’t have a full control over the application’s control flow. As a result, re- active applications need to be structured differently and need to be written using different patterns. We build an easier way for developing reactive applications. Our work integrates declarative and imperative approach to reactive programming and uses the notion of event as the unifying concept. We discuss the problem of me- mory management in this scenario and develop a technique for collecting not only events that are not reachable, but also events that cannot trigger any action. Next, we present a language extension that makes it possible to wait for occurrences of events that match some defined pattern. The extension isn’t bound to the reactive programming model and can be used for concurrent and parallel programming scenarios as well. Finally, we develop a reactive programming library that builds on top of the ideas discussed earlier and we present semantics of the library to enable formal reasoning about reactive programs. Keywords: reactive programming; monads; events; pattern matching Název práce: Reactive Programming with Events Autor: Tomáš Petříček Katedra: Katedra teoretické informatiky a matematické logiky Vedoucí práce: Don Syme, Microsoft Research Cambridge E-mail vedoucího: [email protected] Abstrakt: Programovací model pro reaktivní aplikace je v mnoha ohledech ne- obvyklý, protože nemáme plnou kontrolu nad řízením běhu aplikace. Z toho důvo- du mají reaktivní aplikace jinou strukturu a musí být psány pomocí jiných návr- hových vzorů. V této práci prezentujeme snazší způsob pro vývoj reaktivních apli- kací. Naše práce propojuje deklarativní a imperativní přístup pomocí sjedno- cujícího konceptu událostí. V první části práce se zaměříme na správu paměti a prezentujeme techniku pro automatické uvolňování objektů reprezentujících událost nejen v situaci kdy událost není referencována z programu ale také v situaci kdy událost již nemůže způsobit žádnou reakci. Dále prezentujeme roz- šíření jazyka, které umožňuje zápis kódu čekajícího na několik událostí. Toto roz- šíření není pevně vázané na reaktivní programování a je tedy možné využít ho například i pro paralelní programování. Na závěr prezentujeme knihovnu pro reaktivní programování, která využívá všechny výše uvedené myšlenky a popí- šeme sémantiku jazyka, která umožňuje formální dokazování vlastností reaktiv- ních programů. Keywords: reaktivní programování; monády; událost; porovnávání vzorů 4 Preface I did most of the work presented in this thesis during an internship at Microsoft Research Cambridge between October 2008 and April 2009, which was supervised by Don Syme. I am very glad that Don was kind enough to supervise not only the internship, but also my work on this thesis. My interest in reactive programming dates back to 2007 when I worked on my Bachelor thesis (6) dealing with web programming. In fact, some examples that I will