by Miguel de Icaza

Data Loss

My T30 hard drive died today with all of my information. If you know of a good data recovery service that deals with Linux file systems, please let me know ([email protected]).

Now I have an excuse for not replying to pending mail.

Update: I did recover all my data from a company that does disk drive recovery. I was fortunate enough that only the beginning of the hard drive got scratched, and my Linux data partition was not there. Now a long and painful process of pulling the data back begins.

Posted on 25 Apr 2003


by Miguel de Icaza

Generators Zen

I got most of the iterators support on the Mono C# compiler working now, hopefully these will be checked in CVS as soon as I polish a few things. You can read a small tutorial about them here.

I think that this has more potential than advertised.

Basically, the iterators support in C# simplifies the implementation of methods that return IEnumerable and IEnumerator classes. Typically an implementor would have to create a helper class that would track the state of the enumerable class, and implement a number of methods: MoveNext, Reset, and GetCurrent. Implementing this state machine is not only boring, but also error prone.

Implementing these patterns are so boring, that the average developer fights the system, and designs clever workarounds: From passing a delegate (a method pointer) to be called back, and have the enumerator work linearly; to construct an array with all the results, and returning this; or the worst, exposing the internals of their object. These solutions might work, but they are not available to the platform developers, as they have to provide the right framework for developers; This also means that average developers wont have their code integrate nicely with the underlying platform and interoperate nicely with others.

The C# iterator support works around the problem. Now it is trivial to implement these enumeration interfaces. There is no excuse to not use the system pattern, as it is so simple to use. Lame sample follows:

	IEnumerable CountToThree ()
	{
		print ("About to say one");
		yield 1;
		print ("About to say two");
		yield 2;
		print ("About to say three");
		yield 3;
	}

When invoked, say from the foreach construct:

	foreach (int value in CountToThree){
		print (value);
	}

The message "About to say one" would be printed, then, the routine would return, and the value would be printed by the print in the foreach main loop. Then, when the next value is about to be retrieved, the execution will be resumed where it left, and the string "About to say two" would be printed, and the value 2 returned.

Notice something: the values are not pre-computed ahead of time and returned: they are returned as they are consumed. This is implemented by a clever state machine and an internal class generated by the C# compiler.

Now, today as I explained my excitement to Ettore on the yield keyword, I realized that it could have more uses outside the scope of implementing enumerator interfaces. Part of the beauty of the yield is that from the developer perspective, it suspends the execution of the method at that point, only to be resumed later on.

This beauty is not obvious at first. It took quite some time for me to assimilate this. Lets repeat it again: the yield keyword suspends the execution of the routine, only to be resumed later. This is not only interesting, this is absolutely fabulous.

What the designers of C# have done here, is that they have taken an annoying and error prone pattern and have made a language extension that effectively addresses a problem.

But it seems like yield could be used for a lot more. In fact, over the years I have implemented plenty of state machines, in the presence of non-blocking operations. Non-blocking protocols handlers, parsers, and GUI applications. It starts to feel like a waste to have this functionality under-used.

For example, an streaming XML parser looks like this:

HandleToken (int token)
{
	switch (state){
		case START:
			if (match (token, "<")){
				state = "<";
			error ();
		case "<":
			if (match (token, identifier)){
				state = IDENTIFIER;
			error ();
		case IDENTIFIER:
			if (match (token, OPEN_QUOTES)){
				state = QUOTE_HANDLING;
			} if (match (token, ">"){
				state = START;
			}
	}
}

It would be fascinating if this could be implemeted with yield:

HandleToken (int token)
{
	open:
	if (match (token, "<")){
		yield;
		if (match (token, identifier)){
			yield;
			if (match (token, OPEN_QUOTES)){
				...
			} if (match (token, ">")){
				yield;
				goto open; 	// you know you love it.
			}
		}
	}
}

Ok, that is probably not the best example, as this is just the first time I have thought of this (Am sure the lisp, scheme, icon people have better examples of this). The one things missing here is how to resume the method, and how to provide any new parameters to it. Maybe a resume (method, args) would do the trick.

But the same sort of patterns exist on GUI applications: for example consider the state handling of user input while doing region selection (or the other hundred states tracked by Gnumeric for example).

Extending C# string support

A few things I would like to see in C#, these do not even require changes to the language, but only to the String class:

class String {
	// 
	// Returns the string STR replicated COUNT times.
	//
	static string operator * (String str, int count);

	//
	// Splits the string used the provided string
	//
	static string operator / (String str, String sep);
	static string operator / (String str, char c);

	//
	// Extracts a range of characters, notice that String.Substring is annoying
	//
	static string this [int start, int end] 
}

string s = "Hello World;

print (s [0, 4]);  	// prints "Hello"
print (s [-2, 2]);	// prints "ld"
print (s * 2);		// prints "Hello WorldHello World"
string [] j = s / " ";  // returns {"Hello", "World"}

Wikis

Been fascinated about the Gtk# Wiki

Posted on 22 Apr 2003


by Miguel de Icaza

After Zoltan got us some nice 20% of performance back in the compiler by providing the GC with object maps and having the compiler drop unused structures early on, I felt compelled to do some performance work myself. My results not as impressive as Zoltan, got us another 10% from using a negative type lookup (it was there, but not fully used) and another 5% from using the new namespace information (Paolo got us a new Mono method to fetch all the namespaces in a type).

Also been fixing plenty of bugs in the compiler so Paolo wont complain when I look at C# 2.0 features. There is one bug that I have tried to fix four times now, and made some progress. I have patches to fix part of this, but not to gracefully handle some of the side effects.

After doing pure bug fixing this weekend, I started the work on C# Iterators, a fascinating new addition to the language. Initially I got pretty scared at them, because I did not have any ideas on how to handle the iterator state. Now that I figured how trivial and simple it is, I started to implement it. This is going to be very nice to use.

Posted on 21 Apr 2003


by Miguel de Icaza

Only two hospitals are functioning in Baghdad after all the looting and robbing after the city was "liberated". The only building that has been protected from looting was the Iraqi Ministry of Oil.

But that is just a coincidence. In the meantime, the history of civilization, and the history of Iraq has been destroyed, set on fire. And the Bush administration is distracting people now with an increased set of verbal attacks on Syria.

The era of dictatorship has ended in Iraq. The new century of idiocy reigns on the world.

Posted on 15 Apr 2003


by Miguel de Icaza

A couple of interesting photographs from the fall of Baghdad.

John Pilger on Blair's responsibility on war crimes:

A BBC television producer, moments before he was wounded by an American fighter aircraft that killed 18 people with "friendly fire", spoke to his mother on a satellite phone. Holding the phone over his head so that she could hear the sound of the American planes overhead, he said: "Listen, that's the sound of freedom."

OrbitaFM, my favorite mexican radio station is broadcasting on the Internet. Finally I can keep up.

Today: Global March against the war: Occupation is not Liberation, for more details go to the Intenrational Answer site.

I used to wait for Robert Fisk's coverage to reach the sites I track (CommonDreams, ZMag and Jornada), but with his daily coverage from Baghdad, covering the other side of the story, I have been tracking him directly on the Independent newspaper.

The latest articles from Robert Fisk (which Duncan and I had the priviledge of listening to at MIT two months ago):

Michael Moore's has an awesome letter on his web page. The best bits: his movie, his books, and his web site are getting more hits now than ever. His book (Stupid White Men) has jumped for the fourth time to the New York Times bestseller list (very unusual); His documentary Bowling for Columbine has been ordered more than Chicago;

He looks over various cases of people who criticize the war, and finds that the media is not telling you the truth: there is no backlash against those who protest the war, but quite the opposite. But I guess that is what we get for consuming news from a single source.

My new book from John Pilger, arrived this week. The first chapter talks about Indonesia and the dictatorship that was established there on the 60's (Guess who was behind sponsoring it?) and the mass killings of dissenters. I had heard the stories about East Timor, but never got into the details of what happened. Corporate greed, and just like in the case of Vietnam: the desire to control one of the most resource rich parts of the world (tin, oil, rubber, coal).

The IMF and the World Bank appear once again as the stars of the show: sinking Indonesia into deep poverty, and promoting what can best be described as an enslaved society.

Finished Greg Palast's new edition of `The Best Democracy Money Can Buy'. You should go to your library or bookstore, and read this book now. One of the few books that got Nat upset.

I strongly recommend the two books above. Do not wait, just get them today (or come by my office to grab them)

Today's miggy-economic advise to countries all over the world: keep the World Bank and the IMF loans away; Do not cave to pressure to change your laws in favor of foreign investment; Do not erode your civil rights, and civil liberties in favor of a unproven economic plans (do not forget to ask for references, because there is not a single good reference); Do not sign any free-trade agreements; Push for high tariffs if you want to survive; Provide state-sponsored social programs. (Hint: Chile does not count, they smartly cheated on the IMF/WorldBank)

Posted on 12 Apr 2003


by Miguel de Icaza

Everybody is linking to this article, so am jumpint into the bandwagon. This is an article written before the war started.

Quotes:

One of the fundamental findings of cognitive science is that people think in terms of frames and metaphors -- conceptual structures like those we have been describing. The frames are in the synapses of our brains -- physically present in the form of neural circuitry. When the facts don't fit the frames, the frames are kept and the facts ignored.

And:

First, the anti-war movement, properly understood, is not just, or even primarily, a movement against the war. It is a movement against the overall direction that the Bush administration is moving in. Second, such a movement, to be effective, needs to say clearly what it is for, not just what it is against.

Third, it must have a clearly articulated moral vision, with values rather than mere interests determining its political direction.

As the war begins, we should look ahead to transforming the anti-war movement into a movement that powerfully articulates progressive values and changes the course of our nation to where those values take us. The war has begun a discussion about values. Let's continue it.

Posted on 09 Apr 2003


by Miguel de Icaza

Robert Fisk reports from Baghdad, on the American/British invasion.

Ali Ismail Abbas, 12, wounded during an airstrike according to hospital sources, lies in a hospital bed in Baghdad, April 6, 2003. Abbas was fast asleep when war shattered his life. A missile obliterated his home and most of his family, leaving him orphaned, badly burned and blowing off both his arms.

More photos of the victims of the invasion.

Posted on 08 Apr 2003