The Duality of Object and Event references
Mathematical duality [2] is a very useful and elegant concept that gives us a nice way of speaking about objects or structures that behave in some way exactly conversely. There is no clear definition of what duality is. However, the idea is that when we have two structures that behave conversely and we know that something is true about the first one, then we get the opposite statement about the other structure "for free" (if you know how to translate from one structure to the other).
In this article, I'll talk about an interesting example of duality that (to my best knowledge) wasn't described by anyone before. The two dual structures are references between objects in a normal program and references between events in a reactive application. The statement that is going to become obvious thanks to the duality principle is describing which of the objects (or events) are garbage and can be safely collected by garbage collector.
This topic is in much more details discussed in a paper [4] I wrote with Don Syme and that I presented at the ISMM Workshop (see also my trip report from PLDI). In this article, I'll try to give an accessible description of the most interesting idea from the paper...
Read the complete article (English)
Monday, July 19, 2010
PLDI 2010 Trip Report
In June, I attended my first "big" academic conference Programming Languages Design and Implementation (PLDI 2010) in Toronto. I attended the conference, because I presented a paper that I wrote with Don Syme as a result of my internship at Microsoft Research, but I'll write more about that shortly (and thanks to MSR for partly supporting my attendance at the conference!)
As far as I understand it, the focus of the conference is more on implementation. Many people believe that the current programming languages are good enough and we need to make sure that they run well (e.g. compilers optimize code to run better on multi-core) and that we need better tools for working with them (e.g. automatic verification of the code we write), so these areas were the main focus of the conference. However, there were some very interesting talks on the design of programming languages, which is an area that I personally find more interesting...
Read the complete article (English)
Monday, July 05, 2010
Using custom grouping operators in LINQ
You can use LINQ to write queries that perform grouping of data using group by
or ordering of data using orderby clause. LINQ provides the default
(and the most common) implementation of both of the operations, but sometimes you
may need a slightly different behavior when grouping or ordering data (this article
is motivated by a question on StackOverflow
which needs to do exactly that for grouping).
Let's look at a simple example, which shows when we may need a different behavior when
grouping data. For example, we may have the following list of stock trades
containing a name of a stock and the price of the trade (stored for example as a list
of TradeInfo classes with properties Name and Price):
{ { Name = "MSFT", Price = 80.00 },
{ Name = "MSFT", Price = 70.00 },
{ Name = "GOOG", Price = 100.00 },
{ Name = "GOOG", Price = 200.00 },
{ Name = "GOOG", Price = 300.00 },
{ Name = "MSFT", Price = 30.00 },
{ Name = "MSFT", Price = 20.00 } }
Now, we may want to group adjacent trades into a single summary record which will contain the name of the stock (which is same for all trades in each group), the number of trades in the group and an average price in the group. The desired results are:
{ { Name = "MSFT", Count = 2, AvgPrice = 75.00 },
{ Name = "GOOG", Count = 3, AvgPrice = 200.00 },
{ Name = "MSFT", Count = 2, AvgPrice = 25.00 } }
The operation that we want to do is very similar to group by in LINQ, but
it doesn't do quite the same thing! If we used group by, we would get only
two groups as the result. However, as I wrote earlier, we want to group only
adjacent trades. You could write your own extension method to do this,
but then you need to leave the elegant LINQ query syntax. In this article, I'll show
you how to get the desired results using a simple LINQ query with a group by
clause...
Read the complete article (English)
Sunday, February 07, 2010
Accelerator and F# (IV.): Composing computations with quotations
In this article series, we're talking about the Accelerator project and I'm
presenting an F# library that I implemented, which allows you to use Accelerator [references]
in a more sophisticated way. We've seen two examples of using Accelerator directly
(see also introduction and Game of Life).
In the previous article
I introduced my F# library for working with Accelerator. We've seen F# functions from the
DataParallel module, we implemented an algorithm that rotates an image
using these functions and finally, we've seen that we can take this ordinary F# code
and run it using Accelerator. This is all possible thanks to F# quotations, which
we can use to get an AST (a source code) of an F# function we wrote (if the function
is marked in some special way).
In this part of the series, we're going to look at working with quotations explicitly. We'll use meta-programming techniques to work with Accelerator. Meta-programming means writing programs that manipulate with other programs or pieces of code. This is exactly what we're going to do in this article. We'll write an F# function (running on CPU) that builds a program, which we'll then run using Accelerator.
This is quite interesting approach, which isn't possible when we call Accelerator methods as standard F# functions or .NET methods. The benefit is that we'll clearly see which parts of program run on CPU and what parts execute on GPU or using X64 multi-core target. We could also perform more complicated optimizations with the code (because this wouldn't affect the readability). Just for your reference, here is the list of articles in this series in case you missed some of them:
- Accelerator and F# (I.): Introduction and calculating PI
- Accelerator and F# (II.): The Game of Life on GPU
- Accelerator and F# (III.): Data-parallel programs using F# quotations
- Accelerator and F# (IV.): Composing computations with quotations
However, enough with theory and let's take a look at some code samples! This time, we'll implement blurring of an image (also called convolution). Another example how to write this in F# using Accelerator is Satnam Singh's blog post [4]. Our example will be different, because we'll write the code as standard F# program and then have it translated to Accelerator automatically using quotations. We'll also talk about the architecture of the library that we're using and look at some performance results.
Read the complete article (English)
Tuesday, January 12, 2010
Accelerator and F# (III.): Data-parallel programs using F# quotations
If you've been following this article series, you already know that Accelerator is a MSR library [1, 2] that allows you to run code in parallel on either multi-core CPU or using shaders on GPU (see introduction). We also discussed a direct way to use Accelerator from F# (by calling Accelerator methods directly) and implemented Conway's Game of Life. In this article, we'll look at more sophisticated way of using Accelerator from F#. We'll introduce F# quotations and look at translating 'normal' F# code to use Accelerator.
In general, F# quotations allow us to treat F# code as data structure and manipulate with it. This is very similar to C# expression trees, but the F# implementation is more powerful. We can also mark a standard method or a function with a special attribute that tells the compiler to store quotation of the body. Then we can access the quotation and traverse it or modify it. In this article we'll use a function that takes an F# quotation (containing a limited set of functions) and executes it using MSR Accelerator. Implementing this functionality is a bit complicated, so we won't discuss the implementation now. We'll leave this for some future article of this series. In future, we'll also look at other interesting possibilities that we have when writing code using quotations. Here is a list of articles in this series and of the articles that I'm planning to add:
- Accelerator and F# (I.): Introduction and calculating PI
- Accelerator and F# (II.): The Game of Life on GPU
- Accelerator and F# (III.): Data-parallel programs using F# quotations
- Accelerator and F# (IV.): Composing computations with quotations
Read the complete article (English)
Monday, January 04, 2010
Accelerator and F# (II.): The Game of Life on GPU
In the previous article, I introduced the Microsoft Research Accelerator library. It allows us to write computations with arrays in C# and execute them in parallel on multi-core CPU or more interestingly, using GPU shaders. In the previous artcile, we've seen how Accelerator works and how it can be accessed from F#. In this article, we'll look at one more interesting F# demo - we'll implement the famous Conway's Game of Life [1] using Accelerator. We'll use a v2 version of Accelerator which has been announced just recently and is available from Microsoft Connect [2].
This article is the second one from a series about using Accelerator from F#. Today, we'll use Accelerator types directly from F# - this is the simplest possible approach and is very similar to the way you'd work with Accelerator in C#. However, we can use some nice F# features such as custom operators to make the code more readable. In the next article, we'll discuss a different approach - we'll look how to execute more "standard" F# code (that doesn't reference Accelerator explicitly) with Accelerator using F# quotations. The list of articles may change, but here is a list of articles that I'm currently planning to write:
- Accelerator and F# (I.): Introduction and calculating PI
- Accelerator and F# (II.): The Game of Life on GPU
- Accelerator and F# (III.): Data-parallel programs using F# quotations
- Accelerator and F# (IV.): Composing computations with quotations
Read the complete article (English)
Monday, December 28, 2009
Accelerator and F# (I.): Introduction and calculating PI
I already wrote about two projects that I worked on during an internship at MSR back in 2007 (ASP.NET support in F# and F# WebTools). Even though this was more than 2 years ago (and I did one more internship at MSR in the meantime), I still have one more project that I never published on the web. The folks from the F# team reminded me of this project recently, so I thought I could finally publish it. The project used Microsoft Research Accelerator [1, 2], which is a C# library for developing array-based computations and executing them on a GPU. More recently, the Accelerator team at MSR published Accelerator v2 [3], which was a good motivation to update my original project...
In this article, we'll look at the simplest way of using Accelerator from F#. Accelerator provides a managed interface that can be naturally used from both C# and F#. We can use a mix of method calls and overloaded operators to describe a computation. In F#, we'll also define our additional custom operators to make the code a bit nicer. After we introduce Accelerator using a simple C# demo, we'll look how to calculate an approximate value of the PI number using a Monte-Carlo method.
This article is the first one from a series about using Accelerator from F#. The list of articles may change, but here is a list of articles that I'm currently planning to write:
- Accelerator and F# (I.): Introduction and calculating PI
- Accelerator and F# (II.): The Game of Life on GPU
- Accelerator and F# (III.): Data-parallel programs using F# quotations
- Accelerator and F# (IV.): Composing computations with quotations
Read the complete article (English)
Monday, December 21, 2009
Imperative computation in F# (I.) - Returning results from a function
One of the limitations of F# is that it doesn't very well support some of the
advanced imperative language constructs such as break, continue
or imperative style of returning value from a function, meaning that you can't write
something like return false in the middle of the function. This has
good reasons. F# doesn't in principle have the notion of currently executing statement
and instead treat every code you write as an expression. Clearly, when there is no
current statement, we cannot jump to other statements. If you're looking
for more information about these basic principles, you can take a look at my book
Real World Functional
Programming, which covers this distinction in details in chapter 2, but we'll look
at a brief example that will clarify this idea shortly.
Often, there is really no need to use break or other imperative constructs
in F#, because you can write the same thing more elegantly using one of the provided higher
order function such as Seq.exists or Seq.tryfind. However,
there are still some cases where the imperative programming style makes it easier
to express our original intention. Also, implementing your own higher order
functions (akin to Seq.exists) would sometimes be much easier if we
could just use imperative return.
So, what can be done about this?
Read the complete article (English)
Thursday, March 19, 2009
CLinq - LINQ support for the C++/CLI language
I started working on this project, because I attended C++ class at our university and I had to do some application in C++. Because I hate doing useless projects I wanted to work on something interesting and so I started thinking whether it would be possible to enable LINQ support in C++/CLI...
C++/CLI is a very flexible language and the following example proves that enabling LINQ support in C++/CLI isn't impossible. The following database query returns name of contact and company for all customers living in London:
// create connection to database NorthwindData db(".. connection string .."); // declare database query Expr<Customers^> cvar = Var<Customers^>("c"); CQuery<String^>^ q = db.QCustomers ->Where(clq::fun(cvar, cvar.City == "London")) ->Select(clq::fun(cvar, cvar.ContactName + Expr<String^>(", ") + cvar.CompanyName)); // execute query and output results for each(String^ s in q->Query) Console::WriteLine(s);
If you are interested in more information about CLinq project you can...
Read the complete article (English)
Friday, March 02, 2007
Concepts behind the C# 3.0 language
One of the lectures that I attended last year was Programming Methodology and Philosophy of Programming Languages. The lecture was mostly about history of programming languages and how several features evolved, disappeared and than after many years appeared again in another programming language.
As I final work I decided to write an article that describes ideas that influenced the design of the C# 3.0 language. Some of these features are known from functional languages (for example from LISP or Haskell), some other were developed at Microsoft Research and appeared in the F# language or Cω. I also wanted to show in what ways are these features limited in the C# 3.0. I think that thanks to these limitation, the C# 3.0 is still a simple (or at least not difficult) to understand which is very important for mainstream language, but I find it interesting to know what is possible in other (less limited) languages.
- You can also download the article in PDF (404kB)
- The article is also available at CodeProject.com
Read the complete article (English)
Sunday, October 15, 2006
Slides and demos from F# presentation
This semester I attended Advanced .NET Seminar that was led by Tomas Matousek [^] who is one of the authors of Phalanger project [^] (Which is an amazing project by the way. It takes PHP source code and compiles it without any modification to .NET). Seminar was mostly focused on Rotor and .NET internals, so if you want to learn more about these topics you can look at Advanced .NET programming [^] slides (by Tomas Matousek).
I did one presentation at this seminar too. It was about the F# language developed at Microsoft Research. It was just a quick overview of F# features, because F# is very rich topic, so it coveres only the language (functional vs. imperative behavior), F# type system, compilation of F# constructs to .NET and interoperability with .NET (for example how to create windows forms application in F#). At the end, I also mentioned F# meta-programming that allows you to look at F# code as data.
- The F# language (67kB) - presentation slides in PDF
- F# Samples (54kB) - zipped VS 2005 solution with samples
Read the complete article (English)
Friday, June 09, 2006


