Friday, March 22, 2013

Walking through Compilation

I have to admit, now that I am at this point, I am a little uncomfortable with the idea that every lambda expression will be turned into a class.  It seems like I will end up with a lot of tiny classes, and that seems wasteful somehow.

However, until I see real evidence that it is a problem, I'm not going to worry about it. I can think of a couple of different ways that I might reduce the number of classes.

I'm going to try my current model out by creating a model Toyl program, then working out how it would be expressed in C#.  Then, I'll compile the C# program and look at how it is expressed in CIL.

So here's the model program.  It includes a curried function, a higher order function, and lazy evaluation.  This would compile into a DLL, because there is no impure code here to interact with the sullied world.
namespace ToylModel20130322 {
    AddAndMultiply = int function(int x, int y) {
      x < 0: Exception("x must not be less than zero");
      otherwise: (y + x) * x;

    C13 = 13;

    AddCAndMultiplyByC = AddAndMultiply(C13);

    ApplyTwice = <T> function(<T>function(<T>) f, <T> v) {

    TestFunction = int function() {
      ApplyTwice(AddCAndMultiplyByC, 72)

Fig. 10.1: A model Toyl program
There is something going on here that I haven't explicitly mentioned.  I am using the colon (:) and semicolon (;) as a ternary operator meaning "if...then...else". It makes the "case" blocks within function bodies look nice, but it might come back to haunt me since I am also using the semicolon as the declaration delimiter. Right now I think the two operators should exist in different domains within the code, but we'll see if I run into trouble as the process goes on.

The full C# code will probably be pretty long, so I'll work on it throughout the day and post it on github, and the next post will have my lessons learned.

No comments:

Post a Comment