Tuesday, May 08, 2007

New Series on Closures in Visual Basic 9.0

If the term "lifting the variable x" leaves you scratching your head, read Jared Parsons' multi-part Closures in VB blog series. Jared is a developer on the VB team working with the VB compiler and debugger.

Jared describes lexical closures (usually just closures) as:

[T]he underpinnings for several new features in Visual Basic 9.0.  The[y] are part of the guts of Lambda and Query expressions. ... A closure is a feature which allows users to se[a]mlessly access an environment (locals, parameters and methods) from more than one function. ... Closures are responsible for making the single variable "x" available to [multiple] functions in a process that is referred to as "lifting the variable".

Update: 6/15/2007: Following are links to the first two four members of the series:

  1. Closures in VB Part 1: The Basics (May 2, 2007)
  2. Closures in VB Part 2: Method Calls (May 3, 2007)
  3. Closures in VB Part 3: Scope (May25, 2007)
  4. Closures in VB Part 4: Variable Lifetime (June 15, 2007)

Jared says:

This will be a several part series on Closures in VB 9.0; how they work, their limitations, pitfalls surrounding their use. 

So I'll add links to later series members as Jared posts them.

Update: 5/11/2007: If you'd really like to deep-dive into closures, check out these VB 9.0-based posts from Brian Beckmann:

  • Lambdas, Closures, Currying, and All That. Behind the scenes, I’m working on the lambda-execution mode for the IQ97. [IQ97 is a simulator of the HP97 programmable printing calculator that Brian wrote in a 2006 CTP of VB9.] I’m hoping for a few tweaks to VB and LINQ before I can do this satisfactorily, but I can take the opportunity now—with the current CTP —to illustrate the general technique of defining with lambda and what I hope it might look like in VB some time down the road.
  • Closures without Closures; Currying without Currying. Last installment, we found we could write a factorial function with a function call in the recursive branch, but not a recursive call. This ‘nearly recursive’ style allows us to write factorial without using its name.
  • Rebirth of the Y [Combinator]. Let’s review the bookends of our so-far successful foray into recursion elimination. The general theme has been replacement of recursive calls by self-application of functions adhering to recursive types .
  • A Hint and a Challenge about the search for a non-recursive implementation of the Y Combinator in VB 9.0.

Mike Champion's Brian Beckmann on LINQ underpinnings - Bringing functional programming to "Mort" post links to a Channel9 interview with Brian, a former cosmologist: Brian Beckman: Monads, Monoids, and Mort.

Update 5/13/2007: Mads Torgersen, a Microsoft program manager who's responsible for the design of the C# language, plows the ground in Brian's neighborhood to prove that you can write write a recursive lambda expression, using factorial as an example, and can substitute a named generic recursive method for the Y combinator. Here's the result:

i => new Func<Func<int,int>,Func<int,int>>(fac => x => x == 0 ? 1 : x * fac(x - 1))(new SelfApplicable<Func<Func<Func<int,int>,Func<int,int>>,Func<int,int>>>(y => f => x => f(y(y)(f))(x))(y => f => x => f(y(y)(f))(x))(fac => x => x == 0 ? 1 : x * fac(x - 1)))(i)

The preceding looks to me like a write-only function because it's impossible (for most folks) to read. Brian's VB examples are certainly more readable. (After reviewing this post, Sahil Malik said C# has become too complicated.) Mads has a few other interesting posts relating to C# 3.0 and LINQ in his "Language Designers Workshop" blog. (He isn't a prolific blogger.)

Update 5/11/2007: Amanda also says that VB 9.0 will get the Let keyword in Beta 2 and Beta 2 will deliver full suport of lambdas by VB 9.0.

Update 5/15/2007: Paul Vick also covers closures for VB programmers in his two-part series on lambda expressions and an earlier post:

PS: Amanda Silver has posted the slide deck from her VSLive! Orlando The .NET Language Integrated Query (LINQ) Framework presentation. She promises to update this post with links to sample code in a few days and answer questions related to her session.