Tuesday, September 26, 2006

This semester, due to an issue with my funding situation at college, I found work with a systems group. So officially I am off working on PL theory for a while. The thing is, after about a year of doing PL, I am convinced that as programmers or language designers one should focus on building good abstractions that compose well. All other concerns are secondary to this goal.

 

The project I am involved with one that is concerned with creating a mechanism for programming the increasing for viable multicore machines that are available in the market. The objective being with coming up with a programming paradigm that makes it “easy” to program machines that have a large number of computing units (cores, processors etc).

 

The best description of the motivation for this is described in Herb Sutter's famous article -

The Free Lunch is Over: A Fundamental Turn Towards Concurrency in Software

http://www.gotw.ca/publications/concurrency-ddj.htm

Tuesday, September 26, 2006 10:08:45 AM (Eastern Standard Time, UTC-05:00)  #    Comments [4]  | 
 Wednesday, August 30, 2006

Since many of my blog entries have ceased being blog entries and have become more beacons of the fact that I am trudging through life, here is another life update.

 

I am back in Bloomington, Indiana after a great summer. After many years have I had a summer that left me with so many memories. I spent the summer in Cambridge, UK and like I was telling one of my friends here at IU (Indiana University) that Cambridge is like a distillation of the essence of several Bloomingtons put together, and then matured over several hundred years – a little like comparing a fine Scottish whisky to beer – maybe a 16year old Glenmorangie in burgundy wood to a Guinness.

 

I made it a point to travel as much as I could this summer – as much as time and finances would allow. And it was great. I look at some slight embarrassment at the rant about Cornwall below. But yes I had a great time at Cornwall. Cornwall aside, took several trips to London, went to Norfolk – Hunstanton, Cromer, Great Yarmouth. London’s a pretty amazing city and I haven’t been to most of the major touristy spots. Spent some time gallivanting around Cambridge – went to Peterborough and Ely – saw the famous cathedrals. Went to Granchester – the little village near Cambridge.

 

However most of the really great travel came towards the end of summer. I had a chance to finally go to Scotland. The trip to Scotland was primarily motivated by a long standing wish to go see Skye – the island of Skye – like in the famous Robert Stevenson poem “over the sea to Skye”. Scotland was amazing in many ways – firstly it was beautiful. But that aspect of it didn’t impress me much initially – in my mind I kept comparing it to the highranges in Kerala and not seeing much more there. But after a while you realize how much grander Scotland is – the deep lochs and the high lands and the people. I got a chance to spend two night at the bank of the Lochness in a little town called Fort Augustus. At Fort Augustus I picked up a liking for good single malt whisky. I never had a liking for hard liquor before – but this is great. Favourites? Hmm… I like a Glenmorangie (esp in Burgundy wood), The Macallan, Dalwhinnie; I like this really interesting whisky liquor called the Glayva…. Scotland was interesting in other ways too – here was a land that was under conflict with the English for many hundred years and they too, much like India tries to hold on fiercely to their traditions. Scotland of today is not much like that, but an Scotsman you meet will be only happy to make clear to you how they are not Englishmen. It was alsoa funny parallel that very often tartan patterns on kilts were like the checked patterns on mundu back in Kerala.

 

After Scotland the next major travel was to Paris and then to Rome. Paris was good, Rome was great. I think aside from a love for whisky another take away from summer is the need to be Italian and to speak Italian – I am so amazed by that language – I have taken an instinctive liking to it. It also partly has to do with a good Italian friend I made this summer (who also happens to be a logician in his free time). More on all that later. Honestly these days there is too much to write about, and so many things to say about every little thing – I need to sort out my time for blogging a little more carefully.

 

HairyCoo1.jpg

Hairy Coo!

Wednesday, August 30, 2006 1:07:18 AM (Eastern Standard Time, UTC-05:00)  #    Comments [6]  | 
 Monday, June 26, 2006
  Cornwall 

I think it had been baking inside me for a along long time now – this need to travel. The last time I really traveled was with Anand to Hampi, Gulbarga and Bidar many many years back. Of course since then I have traveled much, but nothing that had the sense of rawness that this weekend had.

 

This weekend I traveled to Cornwall. I grew up in Cochin which is a little costal town in Kerala, India. Cochin is a very beautiful place, but it is also, like many places that one grows up in a place that doesn’t let you realize how beautiful it really is until you have left it. Since leaving Cochin I have mainly been living very far away from the sea. So now that I was in England I looked around for the best piece of coastline I could find and decided to visit it. The best piece of coastline I heard of, as recommended to me by many people, was Cornwall. And so sometime mid last week I decided on a whim that I was going to Cornwall. Cornwall is the south eastern tip of England and is famous for its rocky , cliff lined coastlines.

 

The journey to Cornwall would be 12+ hours by bus from Cambridge because I pretty much had to travel all the way across the country. Not caring much for the strain of the journey, I decided that I would leave Friday night arrive on Saturday morning and leave Cornwall on Sunday night and arrive back at Cambridge on Monday morning by 9.00 and then show up for work. Needless to say, I did, though there were many points in the trip at which I felt I wouldn’t make it. And this being Monday evening, I haven’t had any sleep yet and my feet are killing me. But I loved it, every bit of it – even the hard hard parts.

 

If you have got your head messed up when you were really young and have read Bach and Pirsig and kept that sense of aching inside you – and then you watch a movie like the Motorcycle diaries and it stirs up everything – then at some point you just have to let it all go and travel and see if you really have it inside you to do so. I think this trip to Cornwall was a little sampler for me – to see if I have it in me – I think the answer is yes.

 

So this blog entry is a travelogue of sorts of this amazing weekend. Needless to say, I have many many pictures as my witness.

 

This was the plan – board the bus to Penzance, get off at St Erth and catch a local train to St Ives. Start walking to Pendeen from St Ives. That was supposedly 21km according to the guide book I had. I booked a little bed and breakfast at the Radjel Inn at Pendeen. The next day I would catch a bus to Land’s end and walk to Porthcurno. From there I would catch a bus to Penzance where I could cath my long distance bus back to Cambridge. That was the plan.

 

I arrived at St Ives by about 10.30. St Ives is beautiful – the sort of place I regret so much having to rush through. If I ever go back to Cornwall I hope to spend a day or teo just enjoying St Ives. It’s a little rocky coatal town with very scenic winding narrow roads and several beaches and lots and lots of seagulls.

 

 

Seagulls are interesting birds – if you have never had the chance to see lots of them up close before you are in for  treat. It was interesting seeing this one squaking at a sign that said “Don’t feed the seagulls”. Every now and then you would see a fat seagull with the personality of a duck wobbling up to you. I remember asking one of them if it had had any self respect and if it had heard of Jonathan and it left looking rather offended.

 

I sampled some of the traditional “Cornish Pasty”, got myself a sandwich and some bottled dribnk and I was off. Like the guide book (which is titled “Walking in Britain”, and I highly recommend it – expect I think the book is written by athletes of some sort) the walk was rather strenuous. 21km may not sound like much but winding up and down these cliffs by the sea it is really something else.

 

Every hour or so you would see another human being – but that aside all you see are miles of open grasslands and rugged cliffs and a beautiful blue green sea. If you break a leg or sprain your ankle you and pretty much finished unless someone comes by and finds you. I started out only by ~12.00 and sure enough I didn’t make the walk to Pendeen. After almost giving up several times along the way and once getting my feet caught in some bog I made it to Treen. I was out of food and water at it was about 6.45. I was sure I couldn’t manage any more. Treen was this little town which pretty much only had only one street and a dozen or less buildings. Most of the buildings were bed and breakfasts and there was one hotel! All in the is little place. And whats interesting is that the prices were very high! It seemed like it was popular place for people to give up.

 

There was this one person how I had crossed paths with several times during my walk who knew someone locally and managed to get a cab from somewhere. The town itself had no cabs or public transport. Hence I got a lift to Pendeen. I had a shower at the Radjel Inn (a nice place to stay if you are looking for a place to stay at Pendeen) and got myself some tradition Cornish mead and dinner from a local meadery. I slept like a log that night – esp considering the long days walk and the fact that I had got no sleep on the overnight bus.

 

The nest day morning, after a great breakfast I walked ~2miles to the lighthouse at Pendeen. At 9am its foggy and beautiful. There was a middle aged couple there peering intently out at the see – a little later the lady walsk up to me and ask me if I would like to see to a basking shark “Everyone should see a basking shark at some point in their lives” she said. And so I did.

 

I caught one two hourly bus that passes through Pendeen and got off at the Land’s end aerodrome. I heard (got a pamphlet at St Erth  railway station) that said that they sell rides for 29 pounds in a little Cessna. At the aerodrome they said that they need atleast two people for a ride or that I would have to pay for an additional child’s ticket for 15mins in the air. Seemed steep. Then it struck me that I could do something better – and I did – I got myself a  half hour training flight for 69 pounds. So I flew my first Cessna 172 (my first aircraft of any sort) on Sunday the 15th, June, 2006. It was brliiant – we flew all over Cornwall in those 30mins. I got to do most of the takeoff and landing by myself as well with instructions and occasional corrections by the trainer co pilot. I now have a little “Trial Flying Lesson” certificate on my desk that I am very proud off. My instructor was friendly guy named Ben who patiently answered my many questions about the aircraft. It was beautiful – you treat it well and the aircraft flies itself. It even has little landing light which are handy to scare the seagulls J

 

After my flight I caught the next bus to Lands end. Lands end is apparently the most scenic part of Cornwall – rightly so. However the guide book said that the walk from Lands end to Porthcurno is about 3.5 hours (and I remember I could do their walk the previous day). However it turned out that the last bus was two hours away and the bus driver told me that I could make the walk in less that two hours easily. Oh boy! I grueling one and half hours later I back away from the cliffs. This walk was scary – every now and then you are walking along a little much trail (like those in the middle of a paddy field – if you have ever walked through a paddy field you’ll know what I mean, with a steep rocky drop on one side). There were an occasional Jonathan and Fletcher to give me company.

 

After a while I could not keep up the pace and the focus and chickened out. As with all things in life, when you are doing something for an objective instead of the sake of doing it, your fears and difficulties multiply. I started to walk away for the cliff and cut across some fields and ran into some women who had a real map (yes, I didn’t have a real map – yes, I am slightly crazy). I seemed like I was less than half way there to Porthcurno and there was only 25mins to 5 when the last bus was. I hurried along and 15mins later reached a main road that said that Porthcurno is only 1.25 miles away. And I was pulling my last bit of energy together and started getting annoyed with myself for being so attached to the failures of reality and car came along and I had my ride to Porthcurno. A very friendly elderly lady who had been to India and said “At some level I know that some part of me is Indian” – I didn’t know what to say. She drove a little out of her way to drop me at Porthcurno.  I reached there few minutes before 5.00 in time for the bus and by now so thoroughly annoyed with myself for clinging so desperately to safety.

 

So I decided to do the only obvious thing, forget to bus and go sit on the beach. And I did just that and walked to the Porthcurno beach and say there till felt at home with my sense of struggly for having reached the place. And so a half and hour later when I headed back I noticed other troubled people trying to find a cab or some way to get out of there. I wasn’t overly bothered – would I miss my bus back from Penzance? Maybe. But then again maybe not – and sure enough there came the bus that I expected at 5.00 – a little bit of miscalculation and little bit of delay – but it was there just in time to pick me up when I had so conveniently finished with the beach.

 

Again I didn’t sleep to well on the over night bus back and I kept thinking of the ocean and cliffs and the wind. And I thought things I could say to myself when I had been weak at many times in my life and things that I could say to my friends when I had seen them weak. A trip like this is a very real experience, the beauty, the fatigue, the cost of a mistake, the realization if your own mortality are all very real things. This is the sort of trip that cut away many layers of flak that you accumulated on your thinking leaving you fresh and exposed and stronger.

 

Maybe I will do Cornwall again – maybe during this stay in the UK or maybe at some point later in life. Also now I have two places in the world that I would like to live the later years of life away from everything else – both may not happen, but its nice to know they are there – Bidar, Karnataka and Pendeen, Cornwall.

 

26th Monday June 2006.

 

 

Monday, June 26, 2006 2:57:19 PM (Eastern Standard Time, UTC-05:00)  #    Comments [0]  | 
 Thursday, June 15, 2006

 

One of the solutions that did not make it into my yield paper (though I sort of liked it) was one that used “braiding”.

 

The problem here is to do a breadth first traversal of a binary tree. I was looking for a solution using my pet control operator yield. The braiding solution occurred to some months back as a consequence. The solution that finally made it into the paper was one based on a more traditional queue based breadth first traversal (BFT from now on) which did have better perf than the braiding solution. I am however documenting the braiding solution here because I think its elegant at some level.

 

So how would one do a BFT of a binary tree? Look at this entry for a slightly more detailed problem description. Simple, we start out by defining braiding.

 

Say you have two yield based iterators. These are just computations that yield values during their process of evaluation. These iterators yield values of type Maybe. For those unfamiliar with the Haskell type “Maybe”, here is a definition –

data Maybe a = Just a | Nothing

 

It simply says that a value of type maybe is either some actual value or the special value nothing. If you desperately need an analog, think of it as a pointer or a reference – it either points to some object or its null.

 

So a stream of maybe-values looks like this

x1 x2 # x3 x4 x5 # # x6 # ..

 

where the ‘x’ denote actual values and the # denotes occasional ‘Nothing’. If one had two such streams of values, then one can braid them as follows. Consider the stream of xs’ and the stream of ys.

 

x1 x2 # x3 x4 x5 # # x6 # ..

y1 # y2 y3 # y4 y5 # y6 y7 # ..

 

On braiding gives a single stream with that switches between the original streams every time it sees a Nothing. This results is a single stream like this –

 

x1 x2 y1 # x3 x4 x5 y2 y3 # y4 y5 # x6 y6 y7 # ..

 

This is how braiding works.

 

So how does one do a BFT? Simple, one recursively traverses the tree as follows –

  • If you are at a leaf, yield the leaf value and then yield a Nothing.
  • If you are at a branch, yield the branch value, a Nothing and then braid the iterators of the left and right branches.

 

Simple – that’s a breadth first traversal. If you look at the algorithm closely you will realize that final stream produced will be in the breadth first order but will also contain occasional Nothing values – the Nothing values will be precisely in the those places when values of one level deeper in the tree are being produced. (Of course, you don’t want to expose the Nothings you can add a filter and remove them).

 

The other important observation, which is also a requirement of the problem, is that the values used to resume the iterator will be available to the branches or leafs that yielded the corresponding value – hence tree reconstruction is trivial.

 

-- This a breadth first tree walk that reconstructs the tree from the

-- the return values of the yield. It is expands the tree one level at

-- a time and internally uses Nothing to communicate level changes.

bfTreeWalk :: Tree a -> Yield i a (Tree i)

bfTreeWalk tr = suppressMaybe $ bft tr

    where

    bft (L v) = do (Just v') <- yield (Just v);

                   yield Nothing;

                   return (L v')

    bft (B v t1 t2) = do (Just v') <- yield (Just v);

                         yield Nothing;

                         (t1',t2') <- braidMaybe (bft t1)(bft t2);

                         return (B v' t1' t2')

 

Here is the tree walk in Haskell. Even if you are not able to run or fully grok the Haskell code above, you should be able to use it as a guide to reconstruct the solution in your pet language.

 

The supporting braid and suppress functions are below –

 

-- Convert maybe values into real values by ignoring the Nothing.

-- 1 2 10 # 3 30 # 5 6 70 50 # 8 ...

-- Becomes

-- 1 2 10 3 30 5 6 70 50 8 ...

suppressMaybe :: Yield (Maybe i) (Maybe o) r -> Yield i o r

suppressMaybe yb = runMapY yb sM

    where

    sM Nothing = return Nothing

    sM (Just v) = do r <- yield v;

                     return (Just r)

 

-- Alternates two iterators, every time one yields a Nothing

-- 1 2 # 3 # 5 6 # 8 ....

-- 10 # 30 # 70 50 # 80 ....

-- and makes (where # is a Nothing)

-- 1 2 10 # 3 30 # 5 6 70 50 # 8 .. 80 ... ....

braidMaybe :: Yield (Maybe i) (Maybe o) r  -> Yield (Maybe i) (Maybe o) r -> Yield (Maybe i) (Maybe o) (r, r)

braidMaybe yb1 yb2 = bM (runYield yb1) (runYield yb2) True

    where

    bM (Iterator (Just v) k) it ord = do i <- yield (Just v); bM (k i) it ord

    bM it1 it2 False = do yield Nothing; bM it2 (resume it1) True

    bM (Iterator Nothing k) it True = bM it (k Nothing) False

    bM (Done v1) (Done v2) True = return (v1, v2)

    bM (Done v) it True = bM it (Done v) False

    resume  (Iterator Nothing k) = (k Nothing)

    resume it = it

 

The code above uses a monadic yield implementation and can as easily be expressed by any language which supports a good yield operator. (As of now I have a yield implementation for Haskell and Scheme and I believe Sid has ones for Python and Ruby (?))

 

At this point I should note that I was referred to the naïve implementation of BFT using nested lists by Simon Peyton Jones. The cods is below

 

breadthFirstTW :: Tree Int -> [Int]

breadthFirstTW t = concat (bft t)

  where

     bft :: Tree a -> [[a]]

     bft (L x) = [[x]]

     bft (B t1 t2) = b_zip (bft t1) (bft t2)

 

     b_zip :: [[a]] -> [[a]] -> [[a]]

     b_zip [] ys = ys

     b_zip xs [] = xs

     b_zip (x:xs) (y:ys) = (x++y) : (b_zip xs ys)

 

The essential idea behind the braiding traversal is not very different from that of the nested solution with the operational encoding of nested lists using Maybe. However looking at the nested list solution, at this point, it is not apparent to me how it can be extended to reconstruct the tree.

 

The implementation of the monadic yield that this relies on is documented in my paper which I hope to put up on my academic website soon enough.

Thursday, June 15, 2006 1:48:43 PM (Eastern Standard Time, UTC-05:00)  #    Comments [0]  | 
 Monday, June 12, 2006
  Rotor 2.0 

I was attending a talk by Andrew Kennedy today when I heard that Rotor 2.0 has been released. So it has the Shared Source version of the cool new C# compiler and such. Fun Fun.

Download

Monday, June 12, 2006 12:22:49 PM (Eastern Standard Time, UTC-05:00)  #    Comments [1]  | 
 Wednesday, June 07, 2006

 

A romance with musicals which started with Jesus Christ Super Star a few years back, is coming back. JCSS was brilliant – amazing music, amazing lyrics, very interesting characters – a very interesting shift of perspective. Very cool. I remember watching JCSS completely for the first time at Deepak’s place back at Bangalore – at one of our many of dinner + music sessions where Deepak would treat us to the pleasures of the local vegetarian home delivery service and his beautiful taste in music.

 

If you have not watched JCSS, it is highly recommended –

http://www.imdb.com/title/tt0275434/

 

 

Since coming to Cambridge I have had a chance to sample many interesting things – vintage wines, abstract mathematical models such as pi calculus and category theory, involved pieces of software such as the Glasgow Haskell Compiler, programming paradigms such as STM and monadic yield, London, beautiful medieval cathedrals, beer festivals etc and some musicals.

 

Its the musicals that this entry is about. A few weeks I got to watch Rigoletto the opera – a live performance at the “Cambridge Corn Exchange”. I had originally expecting nothing – only by the mild curiosity that the brochure was and also intending to sample the often heard “stiff upper lip” British formal occasion.

 

What I saw were a lot nicely dressed older people who were very polite and very at-ease. Rigoletto was in Italian with English subtitles displayed on a screen above the stage. As I watched Rigoletto, minor details like there actually nude women on the stage got swept aside and the power of music and metaphors started to take over. I started not to notice the gasps from the audience about the directness of presentation and more on what would be going through these characters in the play, if it was all real.

 

After I left the opera, one of the last pieces “La donna e mobile” played in my mind for days afterwards. This is an English approximation of “La donna e mobile”

 

Woman is unpredictable, like a feather in the wind,

she changes her voice, and her thoughts

Always a sweet, pretty face,

in tears or in laughter, always lying

Woman is unpredictable, like a feather in the wind,

she changes her voice, and her thoughts

and her thoughts, and her thoughts

 

Always miserable, he that trusts in her

who confides in her, his unwary heart!

Yet nobody feels fully happy

who on that bosom doesn't drink love,

Woman is unpredictable, like a feather in the wind,

she changes her voice, and her thoughts

and her thoughts, and her thoughts!

 

This is being sung by an overly licentious Duke who is about to be murdered by an assassin paid by the jester of the Duke’s court. The assassin’s sister who usually woos his victim’s for him such that he can stab them when they are distracted now pleads with him to spare the Duke’s life. The assassin consents on the condition that someone else should come through the door who he will murder instead of the Duke. The jester wanted the Duke killed because he had dishonored his daughter. The jester’s daughter however overhears the conversation between the assassin and his sister and decides to sacrifices herself to save the Duke, despite knowing that he had cheated her. As all this unfolds one can hear the Duke singing in his room awaiting his mistress for the night, the assassin’s sister. Its delicate and is one of those climaxes that’s not easy to forget.

 

Beautiful.

 

Two days back I saw the Phantom of the Opera – honestly I don’t have many ways to describe this other than that it sent me reeling ever so often. Its brilliant, its powerful and it is so exquisitely done. Its one of those things that one has to put on their “to do list before you die”. As you watch the Phantom of the Opera so many layers unfold. It one of those things that awoke many sleeping ghosts in my mind.

 

 

 

In sleep he sang to me,

In dreams he came,

That voice which calls to me,

And speaks my name.

And do I dream again?

For now I find.

The Phantom of the Opera is there-

Inside my mind.

 

This is the version I saw -

http://www.imdb.com/title/tt0293508/

 

Many tracks in the Phantom of the Opera hit you with the raw pathos comparable with Mukesh Chand Mathur (aka Mukesh) singing “Dost dost na raha” in Sangam or “Baharon Phool Barsao” of Mohammed Rafi in Suraj. Brilliant.

 

In short, I am hooked. Any suggestions for what I can watch next? I know that comparing with Verdi’s Rigoletto or Andrew Loyd Weber’s JCSS or Phantom of the Opera is a tall order – I don’t expect it to. I just want to sample some more real music.

Wednesday, June 07, 2006 5:41:07 PM (Eastern Standard Time, UTC-05:00)  #    Comments [0]  | 
 Monday, June 05, 2006

Work at MSRC has been moving slowly, though I have been busy.

 

Parallel GC for Haskell

Not much to say on the Haskell parallel GC front except that I am yet to actually start writing any code. I have been trying to understand the existing code base and it has been a little over whelming. This is certainly not one of the best code bases that I have seen. Actually quiet far from it. There are functions of several hundred lines in length. The main GC function itself is 800+ lines long. I have been told that several people have got their PhDs of that GC.c file. I was expecting a pure functional language to contain a couple of core object types and everything else to be built around them. Far from it, there are 70+ types of objects in the system – they have special cased almost everything they can think of for efficiency sakes. Being a pure lazy language these things matter I guess.

 

That aside the source is littered with lots of interesting ideas. The problem with something so interconnected is that its hard to build incrementally on this. So I am little stuck. The GC has a copy collector and a mark compact collector in the same code base – they share a large amount of code. For a long time this confused me. The system switches from a copy collector to a mark compact collector depending upon memory usage of every step of every GC. The number of generations is runtime configurable. The number of steps per generation is runtime configurable. It’s a crazy, interesting system.

 

I have been making some notes over here, which I hope to continue updating over the period of my internship –

http://cvs.haskell.org/trac/ghc/wiki/GarbageCollectorNotes

 

Papers - Quantum

That aside, I helped complete – or rather didn’t help complete – a paper on Quantum Computing since I landed in Cambridge. I actually didn’t do much for that paper. I had some interesting ideas, but they remained undeveloped enough to not go into the final paper (though we initially thought it would). So well, in some sense my first paper is not really my first paper. :)

 

Papers - Yield

I finished my second paper since landing at Cambridge this past weekend. The paper is about yield, (yes, finally!). It is coauthored with Simon Peyton Jones and Amr Sabry, though most of the actual writing was done by me. So in many senses this paper will not be of comparable caliber to most Amr Sabry or Simon Peyton Jones papers. Writing this paper was quiet an experience – as it is, it was my first real paper. Usually, I am told, people don’t write their first papers completely themselves, they write a section or so. Secondly I had the names of two very respected computer scientists on this and I was afraid of writing accidental stupidity. They however did give me lots of comments over email. And at the end of it I think I have come out braver, a tab bit less afraid of writing papers.

 

The paper has been submitted to the Haskell Workshop 2006. Haskell is probably the most difficult language to suggest a new control flow operator. Being the pinnacle of pure functional programming, with monads and lazy evaluation it is a very very hard language to make a value proposition to in terms of adding control flow operators. All the same, I think we may have one or two interesting arguments in favor of yield.

 

Breadth First Renumbering

As an example of control flow based on lazy evaluation consider this problem: You are given a binary and asked to renumber the nodes in breadth first order, thus creating a new tree of the same structure but with changed node values.

 

 to

 

Think about it. See if you can do this by only one traversal of the tree and use constant space etc. Basically the best solution you can think up.

 

In Haskell, lazy evaluation is used as a control flow operation. But one that is an implicit control flow operation – in the sense that the programmer doesn’t have to specify the order of evaluation the system will figure it out, driven by need.

 

Here is the lazy Haskell solution -

 

data T a = B a (T a) (T a) | L a

       deriving Show

 

bfn :: ([Int], T a) -> ([Int], T Int)

bfn (k : ks, L a) = (k + 1 : ks, L k)

bfn (k : ks, B x a b) = (k+1 : ks'', B k a' b')

    where (ks', a') = bfn (ks, a)

          (ks'', b') = bfn (ks', b)

 

bfnum :: T a -> T Int

bfnum t = t'

    where (ks, t') = bfn (1 : ks, t)

 

It is just mind boggling that something like this can work – and work so perfectly. If you know how to read Haskell and you haven’t seen this solution before, get ready for a mild surprise. :) The implementation is adapted from the appendix of Chris Okasaki’s  paper –

http://portal.acm.org/citation.cfm?id=351253&dl=ACM&coll=ACM

 

Even if you don’t understand Haskell, try to solve this in your pet language and see what you come up with.

 

Haskell

Since I am in a mood for writing a bit – here is a little bit about Haskell. A tiny tiny intro. Haskell is a pure functional language. If you don’t know what that means, there is good chance that you haven’t seen one before. Also Haskell is one of the few languages that are lazy evaluated. Values are computed only on a need basis – hence one gets to write all sorts of crazy circular dependencies between function parameters and their results – if one is careful. Also it is strongly typed – Haskell is a laboratory for stuffy of type systems, if nothing else.

 

Lets do some examples. Immediately following the example, I write what it means.

 

n = 5

n is a function that takes no parameters and returns the value 5.

 

 

f n = n+1

f is a function that takes one parameter, namely n and return n+1. Hence if one writes (f 1) it is the equivalent of calling f with the parameter 1 and will return 2.

 

If you write code like the above, it will work. But usually people write the types of everything they write. If you miss out the types, Haskell has a type inferencer which will automatically figure out the types (and might occasionally complain if there are ambiguities).

 

So here is the definition of the function f with its type signature.

 

f :: Int -> Int

f n = n+1

So the type of f is written by putting two colons after f and is “from Int to Int”. In other words the function f takes an Int and returns an Int.

 

Here is the type of the function even, a function that checks if a number is even.

even :: Int -> Bool

It takes an Int and returns a Bool.

 

So what is the type of the function n?

n :: Int

n = 5

Simple? The type of n is simply Int.

 

Here is a function g that takes two parameters, namely x and y and returns their sum.

g :: Int -> Int -> Int

g x y = x+y

The type says that g takes an Int and yet another Int and returns an Int. The value of (g 5 10) is 15.

 

The interesting this is that in haskell, unlike in many other languages, the expression (g 5)  by itself has a meaning.

g5 :: Int -> Int

g5 = g 5

(g 5)  is a function that takes an Int  and returns an Int. Here I assigned the name g5 to be (g 5) hence is I say (g5 20) the result will be 25. The general idea behind applying only some of the parameters of a function thereby result in a new function that awaits the remaining argument is called “currying” – named after Haskell B Curry.

 

Does this interest you? If so you should consider reading up about Haskell a bit. Maybe I will drop a few simpler examples of lazy evaluation based niceties sometime.

 

Monday, June 05, 2006 4:42:30 PM (Eastern Standard Time, UTC-05:00)  #    Comments [0]  | 

Idea Foundry

 

I was trying to explain to someone the other day about what I am doing in computers. I didn’t have a concrete way to explain previously but these days this seems to be a good way -

 

There are people who need to solve some problem and they use a piece of software to solve it for them. For example someone would wish to buy a flight ticket and they buy it through a website. They are usually called users or end-users. We are all end users. Then there are people who write the software they used to solve their problem. For example the people who wrote the website, all of the complex interactions between reserving your ticket and handling the monetary aspects are handled by these folk. They are the people who developed your software. I used to once do that, or something very similar. Then if you think about it, there is yet another layer - the people who wrote the tools using which the developers of the website wrote their website. These people write tools so that other write tools which you as end users use. The chain of people who write tools which are used to build other tools is a pretty long chain. Its like any services industry – one service is consumed by another service and so – providing several levels of users and tool providers.

 

Somewhere high up this chain in computers usage are the people who write the compilers and the languages and other infrastructural things which everyone else uses. This is very close to where I am today, except maybe one level of abstraction beyond that. I am in the business of studying and developing languages and programming paradigms. These are the ideas that are made manifest by the folk who create real languages and write interpreters and compilers for those languages.

 

The business of being in programming languages is a tricky one at best. It is one of trying to create fundamental ideas related to the way people think about writing software. Writing software, designing languages etc are a lot like real languages and cultures. They come with a lot of overhead and inertia and the sort of languages people use tend to affect the way they perceive and express ideas about the world around them. To create new paradigms and approaches for programming usually entails walking upto some very smart people and tell them that you can better the way they think about the world – “See, here is another way, you just have to rewire your brain a bit and it will make perfect sense… “. Tricky business.

 

If I were to add another line or two of abstraction to my work, I would land up in what is well considered to be mathematics. That is not to say that it is all not mathematical in nature already, but it is only slightly more so than the mathematics that can be expressed in any field of life. I remember one of my last days in Hyd at Microsoft, when I was in the process of leaving for university. It was at the canteen at lunchtime that I was having this conversation with friends and I was trying to tell them how I saw computer science as science and that the fact that there was something called software and the software industry only incidental to its true nature.

 

The problem with computer science is that it’s a very very early science, so much so that many people in it don’t think of it as a science. They think of it more as an engineering discipline. Some refer to it as an art. I am more of the opinion that it is a science – the very early stages of a fundamental science about the nature of the universe and the reality of things in it. Computer science is a science of automata, a science of the nature of interaction of simple interacting components and the study of their behavior. Like all good scientists if you asked me for evidence that this was indeed the case, I must shy back. I don’t really have concrete evidence, but instead I must point in the direction of rice’s theorem, the curry-howard isomorphism, church-turing hypothesis, the pi calculus and say that I feel very strongly that this is the start of fundamental things.

 

 

Luton

 

I had been to Luton this weekend to see Jims, my neighbor back at Cochin and an old friend. Luton had a carnival happening that day – good fun! Lots of floats and people dancing and such. If it hadn’t been for the soggy UK weather and the near perfect unpredictability of the rain, it would have been lots of fun indeed.

 

Since I have been saying this to a lot of people, I might as well write it down: I grew up reading lots of Enid Blyton (yes..). The thing about Famous Five and such is that you actually do believe that they had wonderful summers in the UK and there was always lots of great food around. Not so. The weather is terrible – I expected a summer and came to this country without a single warm jacket – and its been rainy wet and cold in a perfectly unpredictable way. Cycling with numb fingers and a frozen face isn’t what nice south Indian boys like me were designed for (hear! Hear!).

 

That aside, back to the topic of Luton. I had to write this entry because Luton was a place different from any I had seen in a while. It has a large Indian and Pakistani community and I was genuinely surprised to see streets with urdu and hindi shop names and streets that could have been some part of India. It was also nice to see women in salwar kameez’s – rather fashionable salwar kameez’s at that.

 

It was all very nice, until it started getting to me. It happened suddenly at a moment when I wasn’t watching – my mind let open a torrent of things which used to disturb me about back home which came screaming back. The mad screaming mediocrity of the place started blocking out everything in my head. Why are these people like this? Why? Suddenly I started seeing the look people’s eyes judging you continuously to see  if you are one of them or one of someone else. The road blocks caused by bad parking and lack of courtesy while driving.  The dirt on the street, the littler, the attitude that I don’t care and it doesn’t matter. The whole overwrought cultural pretentiousness – what was that old phrase “we are like this only”. Indeed. The completely lack of politeness, the constant suspicion at an existential level, the lack of the slightest bit of subtlety. When I was finally in my bus back, part of my head was screaming “No not all Indians are like that, look at me, I am different, I care”. But I don’t think it mattered.

 

I think our country and the people lack subtlety simply because they are constantly hounded by a constant deafening noise at an existential level such that they are almost deaf to everything else. The noise of being hammered by “you have to be like this” “even if you chose to change it is futile” “this is the system, live in its rules” “don’t think” – everyday, all you life – it eats away large parts of you faculties. These things were upsetting.

 


 

Posted late, as usual.

Monday, June 05, 2006 4:27:10 AM (Eastern Standard Time, UTC-05:00)  #    Comments [0]  |