TP

# Internship project: Reactive pattern matching

I already mentioned that I was doing my second internship with Don Syme at Microsoft Research in Cambridge. This time, I was in Cambridge for 6 months from October until April, so it has been more than a month since I left, but as you can guess I didn't have time to write anything about the internship until now... There isn't much to say though, because the internship was simply fantastic. Cambridge is a beautiful place (here are some autumn and winter photos), the Microsoft Research lab in Cambridge is full of smart people, so it is a perferct working environment (except that you realize that you're not as clever as you think :-)). Also, it is just a few meters away from the Computer Laboratory of the Cambridge University, so there are always many interesting talks and seminars. So, big thanks to Don Syme, James Margetson and everyone else who I had a chance to work with during the internship.

One of the reasons why I didn't have much time to write about the internship earlier is that I was invited to the Lang.NET Symposium shortly after the end of the internship. I had a chance to talk about my project there as well and there is even a video recording from the talk (the link is below), so you can watch it to find out more about my recent F# work.

I was working on a couple of things during the internship. During the first couple of months, I was helping the F# team on Visual Studio language service for F#. This includes IntelliSense, syntax highlighting and things like that. All the improvements that I and others on the team worked on should be available in Visual Studio 2010 from the beta 1 version, so I believe the IntelliSense will work noticeably better than it does in the CTP. The Language Service for F# is quite interesting project in many ways, because the F# compiler needs to do a lot of work in background (most importantly type inference). This means that getting the right information for the IntelliSense popup window needs to use several different approaches to make sure that you'll see useful items even when the compiler hasn't completed background checking or when the source code just changed.

## Reactive Pattern Matching

The other thing that I was working larg part of the time is Reactive Pattern Matching. The goal of the project was to make it easier to write F# applications that react to external events from the user interface or for example from background working threads. The project focuses on applications (or parts of applications) that are highly reactive and it is based on the F# asynchronous workflows. Here are a couple of points about reactive applications and about the way we tried to make writing them easier in F#:

• Single threaded - The application (or for example GUI component) runs as a single threaded component, but the code is written by creating multiple agents that express parts of the behavior. This makes it easier to reason about how the external events are handled. This of course doesn't mean that the whole application is single-threaded. You can have various background threads doing all sorts of computations. Only the synchronization point (e.g. the GUI) is single-threaded.
• Quick reactions - The agents that we write spend most of the time waiting for some events, which means that the application can consist of a large number of agents. This means that agents are used to structure the source code based on what kind of "workflows" the application performs.
• Minimal direct language support - The project uses mostly the existing F# language with a single extension to allow pattern matching in computation expressions (using the match! construct), which is a more generally useful language feature. Aside from this single extension, most of the work is to find the right programming model, which is just a question of implementing the right library and computation expression builders.

The project is inspired by Functional Reactive Programming and it takes some ideas from my Reactive LINQ and also from some examples I wrote for chapter 16 of my book. However, it is still quite far from being complete and I'm planning to continue working on it, so you'll definitely hear more about this.

## My talk at Lang.NET Symposium

As I mentioned, shortly after returning from Cambridge to Prague, I was on the plane again, but this time to Redmond for the Lang.NET Symposium. This year, there were quite a few interesting talks. Ted Leung wrote has a nice summary for some of them. I quite liked the fact that the talks ranged from more theoretical and research oriented topics. For example, the talk Links: Web programming without tiers by Philip Wadlers was quite interesting for me, because the Links project is closely related to my F# WebTools. On the other hand there were talks about Microsoft projects that are becoming mainstream including Code Contracts and Luke Hoban's talk Productization of F#, which both talk about things that are going to be available in Visual Studio 2010.

There was also a very interesting talk from Erik Meijer about the Live Labs Reactive Framework. The talk was a lot of fun, but more importantly, it was about project that's closely related to what I was working on - in particular it is related to my Reactive LINQ project and Reactive Pattern Matching for F#. I also had a chance to chat with Wes Dyer, who is working on this project.

Click on the image to go to the video.

My talk at Lang.NET was about the Reactive Pattern Matching project that I started working on during the internship with Don Syme. The talk provides some background on asynchronous programming in F# using asynchronous workflows and then focuses on the project I was working on. About half of the talk uses only language features that are already available in F#, namely asynchronous workflows, but uses them differently - to write single threaded reactive code that uses asynchronous workflows to wait for events in general rather than just for waiting of a completion of asynchronous I/O operation.

In the second part, the talk looks at reactive programming using the match! construct, which can be used for pattern matching over events. This lets you succinctly write code that for says things like "Wait for the first occurrence MouseDown event or for the MouseMove event when the X location is more than 100".

Finally, the talk also mentions how the match! primitive can be used for encoding other interesting programming models. This part didn't limit to single-threaded reactive programming, but talked about concurrency models such as working with futures (from the Task Parallel Library) or Joins known from the Cω language.