Missing Missing Persons

I’m having a hard time focusing on work today. I just found out that a good friend and ex-teammate Robert Satterwhite passed away last week.

I spent a year on the .NET Adoption Team with Robert back in 2002 & 2003 – basically my last year in the Microsoft field organization. NAT – as were were known – was a national team of developer and architecture evangelists, so while we were all teammates, we didn’t see much of each other in person. However, Rob and I both lived in the Puget Sound area and called on many of the same customers – I was an architect evangelist and Robert was a developer evangelist – so we ended up working together often. We drove together to Olympia many times- he drove and I hacked code in a kind of strange pair programming effort.

Robert grew up in the Pacific NW and had spent almost his entire career as an enterprise technology sales engineer here, so he had friends everywhere. I often used the lame line from the Dragnet movie with Dan Aykroyd: “They ought to put you in Missing Persons. You know everybody.” because it seemed like everywhere we went we’d run into someone he grew up with, had worked with or had sold to. If we hung around before or after a sales call – coffee shop in Olympia before a meeting, debriefing with the sales rep on the steps outside of Costco HQ in Issaquah, etc – chances are someone would recognize him.

When I was leaving the NAT, Robert and I went to Phoenix so I could hand off my customers there to him. I joked that at least I could be sure we wouldn’t run into anyone he knew in Arizona. Yet, it turned out he used to work with the woman who was sitting in front of him on the flight home. She and her husband owned car dealerships in Phoenix and Tacoma. I was astonished – what are the frakking odds? – but Rob just shrugged his shoulders. It happened to him all the time so I guess he was used to it.

I only saw him off and on since I left the team, though as fate would have it I did see him last week on the day he died. I was heading into building 18 for a meeting as he was pulling into the parking lot – I hadn’t realized he was working on campus. We exchanged the usual pleasantries and promised that we’d get together soon. He was blocking traffic, so it was a short conversation. 24 hours later, he was dead.

Rob wasn’t your prototypical blue monster, but he sure touched a lot of people and he made the world a better place by just being himself. You will be missed, Rob.

Monadic Philosophy Part 5 – Reader Comments

Barry Kelly thinks that “programmers would understand monads better if they were described as a design pattern”. I agree 100% and would love to see a monad design pattern written out using p&p’s pattern form. The one thing I would note on this is that certain language constructs can make working with certain design patterns easier. For example, C# obviously has great language level support for the Iterator design pattern. Once you’ve got language level support, it doesn’t really feel like a design pattern anymore, it feels like a language feature. I mean, given that you can write OO code in a language like C, does that mean technically OO is a “design pattern”. I don’t think so.

A commenter named atp warned me not to “fall into the newbie trap of thinking that monads are about sequencing operations. They aren’t. A large number of monads (for example, Reader) are commutative and do not enforce any sort of statement ordering.” Fair enough. For example, you switch the order of some LINQ operators and still end up with the same result. If you switch Where and Select, you should end up with the same output (assuming the where clause isn’t invalidated by the select projection). But from a C#/F# perspective, I don’t really care about monads for enforcing order anyway – the language has that natively. I care much more about the context flow aspect of monads, which it sounds like atp thinks we should be focused on anyway. Works for me.

Finally, Yuri K. pointed out that we aren’t really stuck with the nested lambda expression syntax in C#. In Luke Hoban’s Monadic Parser Combinators using C# 3.0 post, he implements a Where, Select and SelectMany extension method for his Parser delegate type, which allows him to plug into C#’s query comprehension syntax. He’s 100% correct and I considered including this fact in my post. However, the mapping between query comprehension and the Bind and Result functions is a little murky, so I skipped it.

For C# query comprehensions, basically SelectMany does double duty, not only binding the parser and the parser generating function (which Luke called ‘selector’), but also taking the two parse values and calling to a projector function and returning the projection return value in a Result. By implementing SelectMany, you can rewrite the TwoValues parser like this:

static Parser<string> QueryTwoItems()  
{  
    return from v1 in Item()
           from v2 in Item()
           select string.Format("{0}{1}", v1, v2);  
}

which looks pretty much identical to the F# monadic syntax version. Luke also implements Where, which I have in my F# parser library as Satisfy. Where takes a parser and only returns the parser result if the provided boolean predicate returns true. Select is a projection, similar to SelectMany but only used with a single parser. I have a couple of specific projectors in my F# library (Ignore which tosses the parse result and Listify which turns a single result into a single item list) but I haven’t had any need for a generic projector like Select. I’m assuming Luke only implemented Select to make the query comprehension work when you don’t have multiple from statements.

Nobody Cares That I Called McCain a Dick?

In Febuary, when I said “Romney’s a real douche bag”, several upset commenters vowed never to read my blog again. I was even called a bigot (though Dale jumped in and explained how wrong a charge that is). Frankly, anytime I run a political post, especially ones where I call prominent Republicans names, that’s the reaction I expect.

So it kinda surprised me that the response to calling McCain a dick and his campaign an embarrassment was so tepid. No outrage. No promises to unsubscribe nor predictions that others would do likewise. Dale sounds like he agrees that McCain’s campaign is an embarrassment, even though he clearly supports McCain. RandyG doesn’t have anything to say about McCain one way or the other, but he thinks Obama is a “pompous dick”. Only BigJ provides any defense of McCain at all, but it’s an extremely weak defense. He described McCain as “not afraid to tick off a powerful, influential, big money contributor”, which I would expect is hard to say out loud with a straight face. Given that McCain has raked in millions in contributions from the oil industry after flip-flopping on offshore drilling, describing the Hilton’s $4,600 contribution as “powerful, influential, big money” is laughable. Hell, McCain is returning ten times that amount in “unusual” (read: possibly illegal) contributions because they “just didn’t sound right”.

I figure there are two main possible reasons for the lack of outrage:

  • All my readers who are willing to unsubscribe because I don’t agree with their political view have already left, leaving only those who agree with or simply ignore my political posts.
  • The number of people willing to stand up and defend McCain, even anonymously in my small corner of the Internet, is very small.

Personally, I probably a little of both, but I’m guessing it’s more the latter than the former.

Including the Batteries in IronPython

Fans of Python use the phrase “batteries included” to describe the standard library, which covers everything from asynchronous processing to zip files.
About Python

I’m very happy to announce that as of IronPython Beta 4, we are including the Python standard library in an IronPython release. In addition to the usual binary and source zip archives, we’re shipping an MSI installer that gives you the option to include the library files. I mentioned a couple of weeks ago that I’ve been spending a lot of time with lawyers, and this is the first step towards making the IronPython project as open as possible. Note the use of the word “first” in that previous statement. In other words, I’ve got more lawyers to talk to.

We’ve added a new standard library component category to the IronPython Issue Tracker. So if you find any issues or incompatibilities with using the standard library with IronPython, please files bugs on them. We haven’t figured out what set of libraries we’re going to ship, but we don’t want to include ones that we know wont work. For example, Kevin Chu reported on the mailing list that importing the sqlite3 module fails. This is because we don’t have a managed version of the _sqlite3 C-based module. So that’s one we’ll probably remove outright.

Also, this is the first release we’ve distributed as an MSI file, and it looks some folks on the mailing list have had some troubles with it. If you have issues, please file setup bugs as well.

Monadic Philosophy Part 4 – The Parser Monad in F#

In the last post, I built out a basic parser monad in C#. While the approach worked OK, the syntax is still a little foreign to your typical .NET programmer, what with it’s nested anonymous functions and all. Now, I’m going to translate that code to F# and take a look at the special monadic syntax F# supports that makes using monads as easy any sequential code.

First, let’s translate our Parser delegate, Bind, Result and Item functions over to F#. Just for kicks, let’s also port over the final version of TwoItems too.

type Parser<'input, 'result> = 'input-> ('result * 'input) option

// the Bind function, defined as a custom operator
let (>>=) p f : Parser<'i,'r> =  
    fun input ->
        match p input with
        | Some(value, input) -> (f value) input
        | None -> None

let Result v : Parser<'i,'r> = fun input -> Some(v, input)

let Item : Parser<string, char> =  
    fun input ->
        if string.IsNullOrEmpty(input)  
            then None
            else Some(input.[0], input.Substring(1))

let BestTwoItems =  
    Item >>= (fun v1 ->  
    Item >>= (fun v2 ->  
    Result (sprintf "%c%c" v1 v2)))

First, we start with the declaration of the Parser type. Unlike C#, F# has built in support for tuples, so I didn’t bother to define a Result type (just the Result function). A Parser is declared to be a function that takes in some generic input type and returns an optional tuple pairing the result with the remaining input to be parsed. As I’ve blogged before, F#’s option type is kinda like C#’s Nullable type, so a parser that returns None is considered to have failed to parse the input.

Next up is are the monad functions Bind and Result. The only significant change from the C# version is that I used the custom operator >>= for the Bind function. So instead of calling Item().Bind(some\_function), we can call Item \>\>= some\_function. F# functions aren’t attached to a type like C# extension methods are, so this is the only way to get the more readable infix notation. I’m using >>= as the bind operator because that’s the operator Haskell uses for their monad function. Other than the custom operator name, Bind and Result work identically to their C# counterparts. Note, I explicitly specified the return type of Bind, Result and Item, but I didn’t have to. F# can infer the types of all the parameters from usage just fine. I added the type specifications for the reader, in case you’re not familiar with F#’s syntax.

Likewise, Item is identical to the C# version including using strings as the parse input, except for than the F# syntax. Typically, in a real parsing app you would use an intrinsic list of chars instead of strings, since F#s list is a much more efficient data structure than strings for operations that strip characters off the head of the list (like parsers are wont to do). However, I wanted to make this code as similar to the previous code, so I stuck with strings.

Finally, we have BestTwoItems. Again, syntax aside, it’s exactly like it’s C# cousin though I did use the slightly more compact sprintf function instead of string.Format. Again, while BestTwoItems it works well, it uses the same nested anonymous function syntax from the C# version. Maybe I shouldn’t have called it “BestTwoItems”!

However, in F# it’s possible to define a custom syntax for your monad that let’s you write the function this way:

let VeryBestTwoItems =
    parse {
        let! v1 = Item
        let! v2 = Item
        return sprintf "%c%c" v1 v2 }

With this monadic syntax, we’ve now completely eliminated not only the Parser delegate and the input string, but also the nested anonymous functions needed by the Bind function, making the code appear completely sequential.

The secret to making this work is the parse monad object. It the code above, the word parse almost feels like a language keyword, but it’s not. It’s actually an instance of an parse monad object with a specific signature. F# knows how to take the syntax above and combine it with the parse monad object to produce the right code. Here’s the parse monad:

type ParseMonad() =
    member w.Delay(f) = fun input -> f () input  
    member w.Return(v) = Result v  
    member w.Bind(p, f) = p >>= f

let parse = ParseMonad()

As you can see, there’s an obvious direct correlation of Result and Bind functions we defined last time and the Return and Bind methods in the ParseMonad. The only thing we haven’t seen before is the Delay method. Monads are one of of F#’s many delayed expressions. F# wraps the entire monad in a call to Delay to ensure the monad isn’t executed prematurely.

As per the F# grammar spec, there are several other functions you can define on your monad if you so choose. My “real” parser monad also implements Zero and Combine. Zero returns a parser that unconditionally fails. By defining Zero on my monad object, I can write ifs without elses, the parser monad will implicitly inject Zero clause as your else statement. Combine combines results (I know, a shocker!). I use it as a prioritized choice. In other words, when you Combine two parsers, you only call the second parser if calling the first parsers fails. Prioritized Choice is used very often in PEGs, which is why I chose to define it this way.

F# monadic syntax also support For, Let, While, Using, TryFinally and TryWith. Frankly, I haven’t spent much time thinking about scenarios where you’d use these other syntax elements. The only one that’s obvious to me is Using for deterministic finalization, which you could see using anywhere you access IDispoasble objects. Here’s hoping the F# folks document in detail how to use this powerful syntax.

So that’s it for basic monads in F#. I’ve gotten some great comments (and one less than great comments) as I’ve written this series. In my last post on monads (in this series at least) I’ll repost some of those comments as well as provide some concluding thoughts.