February 27, 2009

A Very Nice Environment Variable Solution

Filed under: linux — Joshua @ 9:26 pm

Snniffing around on a CentOS machine on which I am supposed to have extremely limited access - certainly no root access - I ran across an older gcc (3.4) that didn’t complain when I tried to use it. So I decided to take advantage of the opportunity to give myself Haskell - a Very Nice Funcitonal Programming Language®. This turned out to be a path fraught with difficulty, so I thought I’d post the solution.

  1. Download the GHC binaries - from here for Red Hat and related Linuxes. This absolutely MUST be able to find libedit.so.0 - which was the crux of my problem. Not only did this not seem to be preinstalled on the system anywhere, it took a lot of coaxing to get GHC to believe that I had installed it after I did so. Which leads to the next step:
  2. Download and Install libedit - I found some useable sourcecode here. Since I had no root access to the machine in question, I installed it in $HOME/opt/local/lib. Using a c compiler that’s nonstandard on the system you’re compiling on means you have to tell ./configure where to find it. Do this by setting the CC flag. In my case, the command was:
    $ ./configure --prefix=$HOME/opt/local/lib CC=gcc34
    

    Then do the normal make install thing.

  3. Install GHC - This involves basically the same configuration flags as before, only –prefix should be $HOME/opt/local - i.e. without the /lib on the end. Then make install. This will give you Haskell.
  4. Set up your PATH - Naturally since you installed Haskell in a strange place you have to tell your system where to look. I added this to my ~/.profile file:
    PATH=$HOME/opt/local/bin:$PATH
    LD_LIBRARY_PATH=$HOME/opt/local/lib
    export PATH
    export LD_LIBRARY_PATH
    

It’s that last environment variable that made the difference - the LD_LIBRARY_PATH. Before setting that, ghci (the Haskell interpreter) would fail to laod, complaining that it couldn’t find libedit.so.0 anywhere. None of the usual flags to ./configure seemed to work - including the ones (–with-edit-libraries - or whatever it was called, I can’t remember now - in any case, you don’t need it) that claim to be explicitly for the purpose of telling ghc where to find the line editor. I guess the point is that since this is a binary install, that part of the program was already precompiled in and now can’t be changed. So you have to point it to the right place at runtime - which is fine, as it turns out. Setting the runtime linker library path variable clears up the problem, and GHC/GHCI functions just fine.

February 24, 2009

A Weird Scoping Issue with Javascript in Firefox 2

Filed under: programming — Joshua @ 7:57 pm

My day job/research project here in Computational Linguistics graduate school is a web-based ICALL application for Russian - intended to be similar to Ohio State’s Tagarela project. For some time we’ve been scratching our heads over a strange bug that only shows up in Firefox 2, and today I finally tracked it down. It’s worth posting about because it seems to have to do with how Firefox 2 stores values for local variables, and if I’m right about what’s causing it it’s Highly Obscure - to the point where I’m not totally sure I could implement it in a browser of my own any way but by accident. Since I haven’t seen anything on the web about it, either I’m wrong about what’s causing it (and would like to be set straight), or it’s something no one’s really posted on and so maybe someone would benefit from reading this.

It shows up in a lesson where learners are presented with a vocabulary list - a vertical column of words that we make draggable using JQuery. There is a picture in the right-hand column, and the learner should drag the corresponding word to the picture. If they get it right it flashes green, goes to the bottom of the word list, and dragging is disabled on it. When they get them all right, all the words explode and disappear (again using JQuery effects). If you get a word wrong nothing happens: it stays in the list, and the same word appears under the picture in red with some feedback that says “incorrect” in Russian.

To get the functionality where the correct answer flashes green and then moves itself to the bottom of the list (bzw. the list “explodes” when it’s the last remaining item and you drag it to the picture), we obviously need a way to refer to that list item. We do this through JQuery by accessing the element by id - i.e. by getting a reference to that element in the DOM. Nothing so black magicky about that. The weird thing is that we only ever modify the element in any way if the user pairs it with a correct picture. That’s “weird” once you understand the bug, which is:

In Firefox 2 (and only Firefox 2), sometimes it happens that items that the user has tried to pair with a picture incorrectly are strangely immune to the visual effects we assign to correct pairings. That is, internally the system seems to know that they got the answer right (it displays “well done” correctly only when all words have been used correctly). However, if it’s one they’ve used before, the word won’t flash green, won’t go to the bottom of the list, and won’t have draggability turned off like it’s supposed to. It’s a bizarre little bug because SOMEHOW the browser is managing to remember that the user has tried this word before incorrectly, even though nowhere in the Javascript code do we store that information. (And indeed, other browsers seem blissfully ignorant of any previous decisions the user has made - as they should be.) That is, it only ever happens for items that the user has previously attempted to pair with the wrong picture.

OK - here’s the original code, followed by what fixed it. I’ll even go ahead and spoil the surprise: it involves moving a variable assignment into an if statement.

var checkAnswer = function () {
    var answerblank = $(questions[current]).find('div');
 	  var answered = answerblank.html();
    var storedID = $(questions[current]).find('div').attr('id');
 -->var listItem = $('#' + answerTable[answered]['selectionid']);
    if(storedID == answerTable[answered]['blankid']){
      answerblank.removeClass('answeredcorrectly answeredincorrectly');
      answerblank.addClass('answeredcorrectly');
      listItem.addClass('answeredcorrectly');
      listItem.effect('pulsate', {times: 3}, 500).end().remove();
      listItem.draggable('disable');
      listItem.appendTo('#selections');
      removeCurrent = true;
        $('#feedback').html('Правильно(Correct)!');
        timeVar = setTimeout(
          forward, 2000
        );
    } else {
      removeCurrent = false;
      answerblank.removeClass('answeredcorrectly answeredincorrectly');
      answerblank.addClass('answeredincorrectly');
      $('#feedback').html('Неправильно(Incorrect)');
    }
};

Here’s the fix:

var checkAnswer = function () {
    var answerblank = $(questions[current]).find('div');
 	  var answered = answerblank.html();
    var storedID = $(questions[current]).find('div').attr('id');
    if(storedID == answerTable[answered]['blankid']){
   -->var listItem = $('#' + answerTable[answered]['selectionid']);
      answerblank.removeClass('answeredcorrectly answeredincorrectly');
      answerblank.addClass('answeredcorrectly');
      listItem.addClass('answeredcorrectly');
      listItem.effect('pulsate', {times: 3}, 500).end().remove();
      listItem.draggable('disable');
      listItem.appendTo('#selections');
      removeCurrent = true;
        $('#feedback').html('Правильно(Correct)!');
        timeVar = setTimeout(
          forward, 2000
        );
    } else {
      removeCurrent = false;
      answerblank.removeClass('answeredcorrectly answeredincorrectly');
      answerblank.addClass('answeredincorrectly');
      $('#feedback').html('Неправильно(Incorrect)');
    }
};

The function is maybe a bit long for a blog example, but it should be easy to dissect. It grabs a bunch of information it needs about the relevant players - basically in the first line it finds where the answer was stored (in a div just below the picture), gets its ID, then finds the item in the vocab list corresponding to the text stored below the picture. It checks in an answer table to see if the id associated with the answer given corresponds to the id for the picture - if it does, it’s correct and it does all the neat effecty things via JQuery. If not, it does very little except say that it was incorrect. Crucially what it does NOT do is store any information about this answer anywhere. Notice that the function uses local

var

s.

The way I understand how Javascript works, if you declare a variable using the

var

keyword it is lexically scoped - meaning that doing this inside a function should make the variable only visible in this function. Importantly, the particular binding should only exist inside the particular function call.

Now notice what seems to be going wrong in Firefox 2. In the first snippet of code (the “buggy” version), the variable listItem is assigned on any call ito the function, even if it turns out that the answer is wrong and we don’t need it. So - admittedly - this was an inefficient way to do things. In the second snippet of code, the variable is only assigned if the answer is correct. As it turns out, that also guarantees that no further operations will ever be performed on the DOM item it refers to, since we disable draggability on correct items, move them to the bottom of the list, and in general make no more changes to them.

What seems to be happening in Firefox 2 - and I find this totally bizarre - is that this variable listItem is somehow static in a very strange way. It persists between calls to the function, but we can reassign it PROVIDED we don’t try to use anything on the right hand side in the assignment statement that it’s seen before. In other words, Firefox 2 seems to be somehow storing the history of assignments to this variable and just skipping performing any operations on it if the variable has previously had this value.

I have no clue how they implement this, or why they would even want to. I assume this was an accident - because it doesn’t seem like the kind of thing any browser would do on purpose - but that doesn’t help me any because I’m still left scratching my head thinking what kind of an implementation would KNOW that a variable in a particular scope had had a particular value before in the past, and knowing this then decide that it could skip performing operations on it for that reason???

And yet, that’s what I’ve come up with. Moving the variable assignment inside the if-statement (admittedly, a good idea for independent reasons, something I should have done from the get-go) so that the variable only ever gets assigned if we’re sure this is the last time we’re refering to this particular list item fixes everything.

Weird.

It was never a problem in any other browswer that I’m aware of - though we do most of our testing on Firefox - sometimes in recent versions of IE.

February 22, 2009

All Double-Priced Items now 50% off!

Filed under: politics — Joshua @ 2:10 pm

Here’s a MENSA question. A store doubles its prices, but immediately offers 50% off all merchandise. How much do you, the customer, save as a result?

That’s right: $0.00. Because half of double is what you started with. It’s one of those “if you spent more than 10seconds coming up with the answer to this one turn in your driver’s license immediately” kind of questions.

The funny thing is, it isn’t even a joke. Your federal government is pulling exactly this stunt right now. We’re less than one week out of the passage of the bit of self-parody known as the
American Recovery and Reinvestment Act of 2009 - a bit of spending that bloated the federal deficit beyond $1trillion. Now, Obama wants to “halve” the deficit “back” down to $533billion by the end of 2013.

The trouble with this plan is that the federal budget deficit has never been as high as $533billion EVER. Not in living memory, not over the 8 years of the Bush Administration, not EVER in the history of the Republic. How do I know? Why because I happen to have the nubmers from the White House OMB right here:

The highest deficit on record prior to this year was $438billion - and yes, that’s a Bush deficit. The one for this year blows everything out of the water at $1.1trillion - or about 1/13th of all the economic activity that the entire US economy performs in a year. So yeah, sure, Obama can cut it in “half” just by magically spending “only” the ridiculous amounts in the red that we were already spending before he took office. All he has to do is fail to sign another $1trillion stimulus bill, then sign more deficit spending bills than any other president and - hokus pokus - blam! He’s a fiscally responsible “deficit slasher.” The cries of outrage from the press at this brazen insult to public intelligence are expected to commence … sometime around whenever the next Republican gets elected.

I can’t stop quoting Jacob Sullum on this one. When asked during the election what he would miss most about President Bush, his response was a dry-witted “the idea that $438billion is a big budget deficit.” Funny how quickly parody becomes reality…

I think before Obama is allowed to run for office again, he should have to write the following on the board 2000 times:

2x/2 = x

February 21, 2009

Love Doesn’t Scale

Filed under: politics — Joshua @ 8:59 pm

I’m always on the lookout for neat aphorisms that nicely encapsulate important points of Libertarian thought. This is because I think Libertarianism argues from a weak position in modern society. There aren’t very many of us to begin with, and our views are rarely, if ever, given a fair hearing in the public forum, so anything that can get the point across sucintly is helpful.

Today I came across a true gem - from hacker Eric S. Raymond via the EconTalk Podcast.

Love doesn’t scale.

What a great way to put it!

Raymond was saying this as a reaction to one of Walter Williams’ favorite rhetorical questions, which goes something like this. We live in the city, and we have to eat, but we can’t produce our own food on the limited space we have in the city, so we absolutely depend on farmers to keep us fed. Do you want to bet your survival on the farmer who brings you chickens because he “loves mankind,” or do you want to bet your survival on the guy who brings you chickens out of a profit motive? Obviously, the guy who does it for money is more reliable: your future is solid with Capitalism.

The reason I like the way Eric Raymond puts it a lot better than Walter Williams’ already useful analogy - aside from how beautifully concise it is, I mean - is that Raymond’s version makes it clear that the issue isn’t a defect in love but a problem of perspective. If the guy bringing you the chickens is your dad, then love is probably a lot more reliable than money. You can’t always count on employment, but you can damn well count on your dad to care about your survival. So it isn’t that there’s anything wrong with love per se, it’s just that there’s something wrong with love on a large scale. We classical liberals get a bad rap for being heartless - but it’s just because we understand this and everyone else doesn’t. Washington doesn’t love me, Washington can’t love me, Washington shouldn’t love me, and you know what? I don’t even want it to try. I don’t give two figs about any politician’s professed “concern” for “the people” or any of that jazz because I know that love doesn’t work that way. It’s piss poor motivation for keeping the trains running (pace The Doobie Brothers), and it just isn’t the kind of thing that any person can feel for millions of people.

I think a lot of political confusion can be traced to the mistaken idea that love scales. It doesn’t.

February 18, 2009

Casting Stones

Filed under: complaints — Joshua @ 7:35 pm

A friend sends an article claiming that it is comforing news that less than 1% of American teens need cholesterol medication. Apparently, we are supposed to conclude from this that reports of childhood obesity in America are overstated. The friend then adds that he and I have agreed that there is too much obsession with weight in this country.

Actually, I’m not sure we ever agreed on that point exactly - but it didn’t seem worth splitting hairs over. In fact, I DO think a lot of Americans are way too fat, it IS disgusting, and I DO wish those people would do something about their weight (and I also think that in the overwhelming majority of cases it is within their power to do so - in other words, don’t give me the “it’s their hormones” bullshit). Where he and I probably agree is on two points: (1) other people’s weight is not a political issue and (2) just because Americans are fat it doesn’t follow that no one else is.

It’s this second point that I want to rant about. The first point is obvious: the government shouldn’t regulate people’s weight or health in any way - and this includes charging me for other people’s medical bills a la Obama’s dream of a single-payer Canadian style healtthcare system. In my ideal country a person’s weight would be his own concern, meaning that people who suffered health problems due to their lack of self-control when it comes to eating would be forced to pay for their own lifestyle choices rather than charging me for their stupidity, self esteem problems, and general lack of foresight. But I’m well aware that America is not that country, so that’s another discussion for another day.

I wanted to rant about the idea that obesity in America is somehow unique - or uniquely excessive. In fact, most of the countries that chide us about our weight are not that far behind us here. Europe - I’m talking about you. Canada - I’m talking about you. When I worked in Korea there was one Canadian who managed to be even more annoying than all the other Canadians complaining about Americans’ weight - this even as he stuffed his face with convenience store cake claiming that he didn’t have time to eat a proper lunch. Was he obese? No. Was he overweight and unhealthy? Definitely.

So memo to the rest of the world: just because US citizens are on average bigger than citizens of your country, you are not ipso facto off the hook. Either you are interested in health or you are not. If you are interested in health, then exactly how much more obese Americans are than people of your nationality is inconsequential to you as you already have your work cut out for you at home.

The same is largely true of global warming. Assuming that there is, in fact, a global warming problem (and I find that highly dubious, frankly), then it seems to me that foreign countries have the right to criticize the US for contributing to the problem only when they themselves no longer are. IF global warming is real, then reducing your emissions to something below that of the US’ doesn’t help anyone if you’re still above the emissions rate which scientists claim is harmful. It’s sort of like defending yourself at a murder trial by saying “well, yeah, I mean I stabbed him a COUPLE of times, but nowhere near as many times as Tyrone, so Tyrone is the real killer here.” Poppycock! The law bans stabbing, period. Or like those entirely pointless debates about whether Stalin or Hitler was worse. Who fucking cares? Once you’re killing in the millions, killing a few million less than your neighbor doesn’t make me feel any better about you as a person, sorry. If global warming is a clear and present danger as so many claim, then it isn’t enough to be better about it than the US. Your country is still an earth-killer until you’re not - period. And your population is still fat until it isn’t - period.

I am not a Christian - or a religious believer of any kind - but some of the Bible stories I heard in church as a kid make more sense to me as I get older. There’s that one, for example, about the rich man who comes to Jesus essentially asking for an exemption from paying taxes since, after all, God is his king and not Rome, right? And Jesus points to the picture of Caesar on the coin and says that since the money comes from Caesar, he has to pay his taxes, sorry. As a kid, this story didn’t make much sense to me - because what they taught us in Sunday School, after all, was that God was more important than the government. But as you get older you realize what was going on. God may come before the government, but just because God outranks the government doesn’t mean we don’t need a government. Quite the contrary - we DO need a government … for all those people who don’t believe in the same God we do. In fact, the man seeking an exemption from his taxes probably didn’t really believe in God at all, and Jesus wasn’t about to let him use God as an excuse to get out of his obligations to fellow men, etc. Likewise the even more famous story about stoning the prostitute - where Jesus says “let he who is without sin cast the first stone.” Again - as a child this seems ridiculous. Surely he can’t be saying we’re never allowed to criticize anyone? As you get older you realize that what the story is concerned with isn’t so much making sure that everyone is perfect as making sure that they criticize in good faith. You can only criticize others when you realize that doing so doesn’t absolve you of the responsibility to improve yourself as well.

And I think when I talked to my friend in the past about obesity it was probably THAT point that I was making. In fact, Americans are fat, and I make no excuses for them. What frustrates me is this idea that the fatness of Americans somehow means that Canadians who stuff their faces with cake can never ever be obese. It’s nonsense. What I object to isn’t complaining about fat Americans, in other words, it’s complaining about fat Americans as an excuse to complain about Americans. I myself am an equal opportunity employer here. I think fat people of all nationalities should lose weight, and the only conclusion I draw from the fact that there are more fat people in the US than in Europe is that some people read too much into statistics.

When Dogma and Clarity Collide

Filed under: linguistics, programming — Joshua @ 6:03 pm

I’ve always thought it ironic that we say “arguing semantics” to refer to tedious, pointless, vacuous, impractical arguments. It’s not that I can’t see where it comes from: after all, names are arbitrary, so if you’re arguing about what a word should mean in defiance of how people actually use it you’ve not only misunderstood how language works, you’re also wasting your time pissing into the wind. Nevertheless, when we say that names are “arbitrary” we don’t mean that the concepts themselves come from nowhere. This is why, for example, George Orwell has been misunderstood to have suggested that manipulating a person’s vocabulary would be a good way to control them. Anyone who’s given 1984 an honest read will know that he was saying something like the opposite of that: calling the Ministry of Propaganda the “Minstry of Truth” is merely misleading, and Winston was able to see through the charade fairly easily, and that is because propaganda exists as a real world concept even if you take away a person’s tools for refering to it conveniently.

If a word for a particular concept that a person needs to refer to frequently is not available, he will invent one. That’s why we call blogs “blogs,” for example, and not “online journals.” It isn’t that “online journal” would have been unclear, it’s just that what we now call “blogs” are a useful and identifiable thing in the world with associations that go beyond simply being someone’s journal that is posted online, and so they get a name that is unique to them.

Linguists like to see themselves as being on a quest from God to protect people from the Language Police - the people who go around telling you how to use words, not to split your infinitives, never to put prepositions at the ends of phrases, to always use whom in objective position, etc. In truth, these things are not violations of English grammar, they are simply the stylistic preferences of a certain class of snob. Unsurprisingly, the class has a name: we call them “prescriptivists.” But I think the term is both too broad and too narrow all at the same time.

It’s too broad because sometimes griping about language use is done to preserve a useful semantic distinction that popular use is killing off. Noah, for example, gets irritated by the common use of “linear” to mean “sequential.” In fact, there is a distinction there, and by the cannonical uses of the terms, “sequential” is more appropriate for most of the situations where people are in the habit of saying “linear.” Is Noah nitpicking? I personally don’t think so. The more people use “linear” to mean “sequential,” the less distinct the two terms become in practice, and this creates the potential for confusion (until such time as language invents a new term for “linear”). My own pet peeve here is people who say “utilize” to mean “use + I’m intelligent.” IN fact, there is a distinction between “use” and “utilize,” and it annoys me that it is being destroyed so that we can have a [+douchebag] marker for MBAs, which is surely semantically vacuous in most cases.

But in some ways it’s too narrow, because there it fails to capture the situation in a lot of academic communities where people tenaciously cling to standard useage when confronted with popular parlance improvements.

I came across a good Computer Science example of this today. Nathan, complaining that one of his programs wasn’t working today in Parsing Reading Group, refered to Python as a “call-by-value” language, at which point my jaw dropped.

Some background for the uninitiated. And - before I get started - I fully accept that I have been using these terms incorrectly for the past 6 years. That is, in fact, the whole point - more on that in a minute. In any case, people who study programming langauges make a distinction between “call-by-value” and “call-by-reference” evaluation strategies. The relevant question is what it means to pass an argument to a function.

Here’s some Python code for illustration:

def pop( argument ):
  argument.pop()
  #argument = argument[0]

mylist = [1,2,3]

print "BEFORE: ", mylist

pop(mylist)

print "AFTER: ", mylist

And here’s the result of running it:

BEFORE: [1, 2, 3]
AFTER: [1, 2]

My function “pop” just does what the built-in Python list method “pop” does: it removes the last element in the list. The point is that my list changed even outside the function.

To me, that has always been the crux of the distinction between call-by-reference and call-by-value. In call-by-reference, the name of the argument in the function refers to the same thing as what was passed in to the function. To wit - changing this thing inside the function changes it permanently. In other words, “argument” and “mylist” are the same thing in call-by-reference. My understanding of call-by-value was always that passing something in to the function meant that the argument in the function had the same value as what you passed in, but was NOT “the same thing.” To wit - changing this thing inside the function DOES NOT change it permanently, but only changes it inside the function. So - “argument” and “mylist” are different under call-by-value. So - according to my understanding, the result in a call-by-value langauge should have been:

BEFORE: [1, 2, 3]
AFTER: [1, 2, 3]

That is because the program only prints “mylist;” it never prints “argument.”

So I was surprised to hear Nathan call Python a “call-by-value” language, since it’s pretty clear to me that it’s call-by-reference.

Well, it turns out I’ve been using the terms incorrectly. If you’re just a bit more anal about things, “call-by-value” just means that you copy the value of the variable name and associate it with the name of the function argument. If it turns out that all variable names in your language are associated with some identifier and that that’s how you resolve them, then copying the value of the variable is functionally equivalent to giving the function the ability to modify your thing. You see, in Python the “value” of “mylist” here is … (hem haw simplify for expository purposes) … the memory address of the list [1, 2, 3]. The “value” isn’t the actual list, it’s a pointer to that list, in other words.

Now maybe you see my frustration. It strikes me as completely ridiculous to refer to passing a pointer as “call-by-value.” After all, the only reason we have variable names period is because humans are bad at recognizing sequences of numbers (like memory addresses), and good at recognizing sequences of letters (like variable names). A human surely employs a variable name NOT to say “memory location so-and-so” but rather to store the thing that he’s interested in computing over: the list in this case. Calling the “value” of a variable its memory address is … well, way too literal-minded, to put it mildly.

What I am interested in is whether the thing that I hand to my function will be modified globally, or only in the function. To me, this is the essence of the call-by-value/call-by-reference distinction. I can understand why someone might see copying a pointer as “call-by-value,” but I can’t understand why they would ever need to talk about that in that way. It seems like a much more efficient use of language to map the distinction to the semantic distinction of “whether or not a function’s modifications are visible outside the function” rather than the pretty useless distinction of “did anything - anything at all - get copied when we called our function!”

However, Nathan assures me - and Wikipedia backs him up - that this is, in fact, the official distinction. So passing a pointer in as an argument is still “call-by-value,” and it’s only ever “call-by-reference” if your compiler just subs in all instances of “mylist” and “argument” with the same memory address for you. Sheesh!

As it turns out, Python throws a wrench into the works anyway. If you noticed that commented out line in the code, here’s the result of uncommenting it and commenting out the first line, like so:

def pop( argument ):
  #argument.pop()
  argument = argument[0]

mylist = [1,2,3]

print "BEFORE: ", mylist

pop(mylist)

print "AFTER: ", mylist

Any guesses? Yup:

BEFORE: [1, 2, 3]
AFTER: [1, 2, 3]

Heh. Interestingly, making modifications to objects inside a function in Python is visible outside the function, but assignment to the name of the variable is not. In true call-by-reference langauge - where “mylist” and “argument” are the same thing - then the result should have been:

BEFORE: [1, 2, 3]
AFTER: 1

We assigned a new value to “argument,” which is the same thing as “mylist,” so “mylist” should also be 1. But it’s not. What gives?

What “gives,” of course, is that Nathan et al are “right” that Python is “call-by-value.” “Argument” and “mylist” are different variables, even though they have the same “value” at the time the function is called - “value” in shock quotes because this “value” isn’t a value in the normal sense of the term but just a memory address of some kind. So when we order the object pointed to by this variable to modify itself in some way - say, by calling “pop” on it - that modification happens to the thing being pointed to, and thus is visible outside the function. HOWEVER - since the interpreter keeps the variable names distinct (where, say, C++ wouldn’t if you passed the argument by reference - that is, by putting the ampersand after it in the function prototype), ASSIGNING to one is not the same thing as assigning to both. All

argument = argument[0]

means is “discontinue associating ‘argument’ with the memory address that ‘mylist’ points to and hereafter associate it with the first element in the list, which is 1.”

So in the terms of call-by-reference and call-by-value as I understand them, Python’s semantics are neither - or both. It isn’t “call-by-reference” because there are cases where “mylist” and “argument” are difference. But it seems silly to call it “call-by-value” if the so-called “value” in this case is a memory address that the programmer never sees and isn’t computing over.

The Python community - it turns out - is sensible enough to have found a new term for this: “call-by-sharing.” This isn’t even something they pulled out of their asses. It turns out that Barbara Liskov used it way back in the 60s to refer to the same kind of calling convention in the CLU programming language. Since Liskov is a Venerable Figure in CS, one would think that this term would be cannon.

But one would be wrong.

Nathan was able to convince me (via Wikipedia) that standard use in CS circles is in fact in the retarted sense mentioned earlier, where if you copy anything at all on entering a function it’s “call-by-value,” even if the “value” in question is just a memory address and nothing in the world that the programmer could possibly be reasoning over or attempting to compute.

So this is an example of where the term “prescriptivist” is entirely too narrow. Because basically - however much it may be true that it is standard practice in comptuer science circles to use “call-by-value” in this way, the term as used is misleading and ridiculous. If your profession discusses semantics from the programmers point of view in terms of how the programming language itself is implemented rather than what the result of running that program will be, then you have lost the plot and need to reconsider.

Nathan says that the way the Python community uses these terms is nonstandard. That may well be - but their usage strikes me as more practical and sensible than the way “real” computer scientists use the terms.

And so I find myself once again siding with a certain kind of “prescriptivist.” Sometimes preserving the traditional distinction (as in “use” vs. “utilize” or “linear” vs. “sequential”), prescriptivist though it be, is just more convenient than going with the herd. And sometimes the traditional distinction (as in call-by-value vs. call-by-reference) is suboptimal in some way and should be improved.

In either case, I think the intelligent thing to do is to side with the more precise formulation, and concerns about “prescriptivism” be damned.

UPDATE - The aforementioned Nathan has an entry about the same discussion on his own blog.

February 11, 2009

C’mon - it’s Netanyahu

Filed under: international, politics — Joshua @ 8:42 am

Israel held an election yesterday and the winner was … nobody, at least according to official reports. But the official reports are wrong. Netanyahu won and should be the next PM.

I don’t really know why precisely, but parliamentary systems seem prone to situations like this, where the country faces a choice between following the letter of the law or the spirit of the law, and somehow following the letter of the law is the disingenuous tack. I think of thinkgs like the recent “coup” attempt in Canada. There wasn’t anything illegal at all about the NDP-Liberal-Bloc coalition to try to oust Harper at the end of last year. In fact, the coalition was well within its rights. And yet, there was a general sense that it was improper all the same - and with good reason: no one’s understanding of a representational form of government allows the leadership to change radically without consulting the electorate. Unfortunately, this bug seems to be a staple of parliamentary systems the world over: it isn’t always clear when elections should be held, and when they are held, the conditions for victory aren’t spelled out as well as they might be.

Here’s the situation in Israel: Kadima technically won the majority of the seats - 28 of 120, compared to Likud’s 27. By tradition, if not expressly by law, Kadima gets the first chance to form the government, and we could expect that Livni, as head of Kadima, would be PM. But like in Canada last December, I think if Livni ends up being PM it shows an undue devotion to procedure over intent, for several reasons.

First - Kadima was never supposed to win this election. For most of the campaign season, they’ve been on their way out, reeling from corruption scandals, a lack of party unity, and a general feel among the electorate that they’re not serious enough about defending Israel from clear and present threats. All this changed with the Gaza offensive, which calmed a lot of nerves concerning the issue of whether Kadima was serious about defending Israel against attacks. And if the public in general had voted like it normally does - with a pretty even split between left-leaning and nationalist/religious parties (there are something like 50 parties standing for election in Israel at any given time), but the difference going to the left - then it would be safe to interpret Livni’s squeaker of a first-place finish as that the electorate was satisfied with the status quo. But that’s not what happened. What happened instead is that there was a sharp shift to the nationalist/religious parties, such that a newcomer nationalist party finished third. In other words, Kadima has only benefited from the recent Gaza offensive to the extent that it radicalized a lot of right-wing voters who, concerned that Kadima would get away with it and return to government, voted for even more nationalist parties than Likud. The temper of the vote, when you cut through the fog, is one of dissatisfaction with the anemic defense of the country against a Palestinian threat that is certainly not abating, not really a ringing endorsement of Kadima’s fitness to lead. So yes, Netanyahu “lost” by one seat. But there isn’t a shining bright line in the rules that says he can’t be PM. Indeed, the rules, such as they are, are vague on this point, saying only that the government needs to be stable. Given the general resurgence of nationalist and religious parties, it’s clear to everyone that Netanyahu has a better chance of forming a stable government in line with the wishes of the electorate than Livni does. Maybe it’s a break with tradition for the president to call the second-place finisher in to form a government first, but in absence of a clear legal presciption for what to do, it seems to me that the spirit of the law should trump any slavish devotion to tradition.

But of course, I’m not Israeli, so the take-home lesson for me as an American is that I’m pretty happy to have a stable congressional system rather than a fuzzy parliamentary one. That isn’t to say that the US is immune to letter-vs.-spirit-of-law conflicts, of course. We have one in living memory: the election of 2000. The difference with the US is that there is general agreement that when procedures are clear, they should be followed. There is also agreement that when procedures turn out to be unclear, they should be clarified and, if necessary, changed so that they are clear. And this is the appropriate way to run a government. What makes me nervous about parliamentary systems is … well, exactly the kind of thing that’s clear from the previous paragraph: that there’s a lot of mystical divination of what fuzzy things like the “mood” of the country or the “trend” of popular opinion is that get factored in to deciding what to do in case of close calls. A better way to approach things is to have a clear system in the sense that the output is unambiguous. What the output is on a given day may reveal some bugs, but the nice thing about bugs is you can track them down and fix them. Say what you will about Bush v. Gore - one thing that’s uncontroversial is that both camps and the majority of the voters were well aware of how the system worked before Florida. Not only do presidential campaigns build their strategies around electoral votes rather than popular votes, individual voters are also largely motivated to vote or not based on their knowledge of the electoral system and the chances that their state will play a decisive role in the contest. If it happened in this case that the winner didn’t win the popular vote, then perhaps that’s a bug that needs fixing (a matter for public discussion), but no one - especially not Al Gore or any of his more vocal supporters - was mystified as to how or why it happened.

The trouble with systems like Israel’s is that they blur the input-output distinction. It’s almost like there’s a different set of concerns at voting time than there are when it comes time to actually form the government. The danger here is that the system itself is taken to be correct without further discussion, and then everyone spends a lot of time peddling their theories about what it all means. There shouldn’t be any theories about what it means: the semantics of the function should be clear. X got elected president means that he won a majority of the electoral votes. X got elected PM could mean any number of things, really. And so the debate about whether it’s even proper gets all tangled up in the debate about how it happened in the first place. Those people maintaining that the victory wasn’t proper, for whatever reason, generally can’t get the discussion focused on fixing the system in any way since so much time is already eaten up deciding what the output of our function even means

What you can do for such systems, though, is place filters on the output. Israel has, in fact, tried this - in the 1990s Prime Minsiters were elected directly, separately from the Knesset itself. The hope was that it would stabilize things. When it produced no appreciable results, they went back to the old system. But perhaps they could try a similar filter: build in a runoff system for cases like this where there are a handful of plausible winners. So Israel could function like a normal parliamentary system for the most part, but when you have squeakers the public gets asked which of the two (or three, up to a suitable limit) they prefer. Since the problem is a lack of clarity in what the public wants, this would seem to fix things.

The operative word, of course, being “seem.” The trouble with filters is that they have a way of affecting the input. In this case, it’s plausible that it would radicalize the Israeli public in some way, drumming up votes for fringe parties owing to the relative safety of voting for them in the context of a near-inevitable (because Israel is something of an extreme case in terms of the number of viable parties participating in any given election) runoff. So there’s certainly no guarantee that it would work. But since the present system doesn’t seem to be working all that well either, I’m not sure that there’s much harm in trying.

In any case, here’s hoping Netanyahu gets to form the government. Like most fair-minded people, I’m not an unequivocal Israel-supporter. It’s a messy situation, and neither the Israelis nor the Palestinians negotiate in good faith, I think it’s fair to say. But at least in the past couple of cases Israel has been clearly in the right. If people start shooting rockets at you, you get to take the gloves off, period. Whether the Palestinian (and Lebanese before them) government is incapable or unwilling to reign in the militants is not really Israel’s concern. Once rockets start coming over the border - at least in the kind of numbers we’ve seen over the past 4 years - it is the duty of any sovereign government to stop them immediately. Americans who don’t get this point need to imagine what we would do if a group of Mexican militants were firing rockets into Arizona. It’s not very likely, I admit, but it’s not wholly implausible. There are such militants, and there is a certain amount of sympathy among the Mexican public and its political class for the idea that most of the western US actually belongs to Mexico by historical right. If a group of them started firing rockets into Arizona, and the Mexican government either couldn’t or wouldn’t do anything about it, I don’t think anyone could credibly argue that the military should sit on its thumbs and ignore it. The appropriate thing to do would be to invade Mexico, kill or arrest all the militants, and dismantle their artillery. Israel actually is in this situation, and there should be no need to discuss whether their invasions of Gaza or Lebanon were justified: they were. Netanyahu understands this, Livni doesn’t, and the future of the country hangs in some sense in the balance. It will be better for Israel if Netanyahu is allowed to win.

But of course, there is no recipe for stability that recommends a system where candidates are “allowed” to win. As in so many other cases, there is the way most of the rest of the world does things, and the way the US does things, and the way the US does things happens to be better.

February 10, 2009

The Gator’s Mouth is open on the ‘Greater Than’ Side…

Filed under: economics, politics — Joshua @ 8:37 am

Your tax dollars at work.

The government maintains fuel economy website showing which cars get what kind of MPG ratings, etc. - making it easy for you, the consumer, to compare. Since they take the manufacturer’s word for everything, I can’t imagine it’s terribly useful, but I did learn a couple of interesting things from it. For instance, they put the annual fuel costs of E85 (vehicles that can burn a blend that’s 85% ethanol) cars running on E85 next to cost of the same vehicle running on regular petrol. By way of example, here’s the Chevy HHR:

E85 Gas
Cost per Annum (in USD) 2225 1063

OK, so the way I read that there’s a $1162 (maximum) environmentalist stupidity tax on HHR drivers. But hey, it’s a voluntary tax, so the libertarian in me can’t complain. Quite the contrary - if they’re going to give away money to lost causes it’s probably better that it go to famers in Iowa than to Greenpeace, so I’ll take it.

What worries me is that the government doesn’t seem to get its own joke. Click on the little notepad icon next to “Energy Impact Score” and you’ll get a popup of pro-ethanol propaganda that includes the following list:

  • The U.S. uses more than 20 million barrels of oil each day
  • 60% of our oil is imported
  • 68% is used for transportation
  • Oil imports cost us $270 billion annually

OK - I don’t see any other way to interpret the inclusion of that last bit about oil imports “costing” us $270billion annually than that they want us to think we could save money by using ethanol instead. But THEIR OWN PAGE shows us we can’t! The very page that links this list, in fact, demonstrates that using ethanol is a lot MORE expensive than buying foreign oil - 1.9 times as expensive, in fact.

What these numbers imply is that if everyone owned one of these “swings both ways” vehicles and elected to run it on E85 all the time rather than gas, we as a nation would spend something like $513billion annually on transportation fuel rather than $270. How is this possibly a savings of any kind?

I suppose the government would argue that it’s a “savings” in the sense that this money is going to American producers of ethanol rather than foreign producers of petrol. OK, granted. But even this picture isn’t as rosy as they claim. Again, by their own numbers, if you look at the oil barrel histogram that they have in the Energy Impact section, you’ll notice that the gas-burner consumes more than twice as many barrels of domestic (those are the barrels in red, white and blue, in case you were wondering) gas as the corn-burner. So it’s all very well to say that the money would be going exclusively to American producers, but only at the cost of cutting production in another American industry by more than half. More than that, it isn’t as though people spend the $243billion they save annually by using gas instead of ethanol exclusively on foreign products. A lot of that money, in fact, goes to homegrown goods. Yet another obvious point is that most of the money that we spend on foriegn oil comes back to us anyway. Countries like Venezuela, Russia, Saudi Arabia and Iran produce little of anything on their own. The near-universal pattern of oil wealth is that industry in the country so “blessed” with huge oil reserves just never really gets off the ground. Why should it? There’s so much cheaper, better stuff available from foreign companies (many of which are American), so they buy that instead. So what this proposal really amounts to, when you think about it, is that average families that can’t really afford to donate an extra $1000 a year in fuel cost change, that being a significant portion of their incomes, are being asked to do exactly that so that a small handful of farmers in Iowa and almost nowhere else can be even richer than they already are at everyone else’s expense. It is, simply put, a transfer of wealth from (a) the public and (b) a particular industry (the domestic oil industry) to another one (the domestic ethanol industry), where this second industry is of dubious economic value, to say the least. Not my idea of a good idea.

But even bothering to take up these points is already too generous, because it still doesn’t erase the underlying issue that you can’t treat anything as a ’savings’ if it costs more money than the alternative. That’s such an elementary concept that you kind of have to sit back an marvel that the bureaucrats in charge of energy policy are selling this with a straight face. Either they’re colossally incompetent, or they think we are. Either way, it doesn’t make me feel too good about the policies are designed in this country.

February 5, 2009

How to Kill a Recovery (with bipartisan support)

Filed under: economics, politics — Joshua @ 5:38 am

It’s official: the Republicans are now every bit as stupid as the Democrats.

The link goes to an article in which Senate Republicans are claiming “victory” in wrangling over the now $1trillion+ ironically-named “stimulus” bill for having “forced” the Democrats to agree to an expansion in the first-time homeowner buying
credit. And a hard-fought “victory” it must have been - because who honestly would expect the Democrats to get behind a plan to shore up an inefficient industry by throwing a bunch of money at it?

Here’s why this is some grade A dumb shit.

First - in case people have forgotten, it was an overheated housing market that caused the current collapse in the first place. Long story short: housing prices were artificially high, leading to a huge misallocation of resources (in the form of investment) into this market. Too many homes got built, meaning prices had to fall, and fall they did, essentially wiping out a lot of people’s investment capital. The blinding bleeding obvious point here is that investment needs to come OUT of the housing market - NOT go in to it! The collapse of the housing market is an unmistakeable signal that it has as much money as it needs, thank you very much, having effectively exhausted its growth potential, and so that money needs to go into other sectors of the economy with more potential for growth. If there were a shortage of housing, housing prices would be rising, not falling. But prices are falling, meaning we have as many houses as we need - at least for now - so the LAST thing the government should be doing is trying to prop up housing prices, which just recreates the conditions that led to this mess in the first place. Granted, people have had their fingers seriously burned, so there’s no way any investor is going to bet the farm on real estate for the time being - but still. Why is the government directing investment money to a section of the economy that clearly doesn’t need it?

Second - there is already a rather generous first-time homeowner’s buying subsidy. You get $7500 in something like a tax-free, interest-free “loan” - which of course is only a “loan” because politicians say it is. In the real world, any time someone hands you money and you get to pay it back as and when you want without compensating them for inflationary losses, you’re being subsidized. It’s already stupid to give first-time home buyers this kind of a break, because it encourages high home prices. Once people can write off $7500 of their first home purchase, guess what happens, kids? Yes, that’s right, the general price of housing rises by almost that much. Real estate agents have less incentive to sell houses for reasonable prices when they know that every first-time buyer gets a $7500 get out of jail free card, and any second-time buyer will play along because he can sell his house to a first-time buyer at a compensatory markup. Like all government subsidies, this is bad for the people it claims to help, but very very good for the sellers it claims to protect people from - sellers who probably lobbied for it. The one saving grace of this idiotic bit of silliness is that it’s capped at $7500. So - it makes housing less affordable for entry-level participants, but at least it probably didn’t contribute too much to the hugely inflated costs of higher-end homes that was the real problem in the current mess. Which is why the Republicans are aiming to fix that loophole and make it bad across-the-board by fixing it at 10 percent of the cost of a new home rather than the dollar amount. Brilliant! Now there will be no official end to it. Housing prices can soar as high as they like, and the subsidy keeps pace. We, the taxpayers, can foot the bill for real estate agents padding their wallets, AND houses will become harder for first-time buyers to afford than they already are. That’s just … great.

It makes me really angry listening to this being sold as a benefit to home buyers. Bullshit. Home buyers are already at the receiving end of the best benefit anyone could offer them: a collapse in housing prices. Houses are more affordable than they’ve been in a decade. Homebuyers don’t even need any real help, let alone “help.” When the prices get low enough, the houses will sell. When the prices start to rise again, it will mean that we’ve finally found takers for all those extra homes that got built, and then, and only then, will it make economic sense to start building any more homes. The best thing the government can do at this point is stand aside and let the collapse happen. The collapse is happening, after all, because there was too much investment in housing. “Too much” as in “more than there should be.” “Too much” as in “more than is rational.” “Too much” as in “damaging to the rest of the economy because valuable resources are being misallocated.” Encouraging investment in a sector of the economy that clearly doesn’t need it is about the only guaranteed way to ensure that an economy as vibrant as the American one puts off its inevitable recovery.

Which is, of course, why this one enjoys bipartisan support. The Democrats have been bad at Economics since FDR. Fortunately for the country, there has always been a wing of the Republican Party that knew better. Unfortunately for those of us living here now, none of those Republicans seem to be in the Senate at present.

February 2, 2009

My Vista/Gentoo Dual Boot

Filed under: linux — Joshua @ 7:32 pm

I’ve just dual-booted my Dell Inspiron 513S with Windows Vista and Gentoo Linux, apparently with success, so I thought I’d post the step-by-step. (The steps specific to installing Gentoo are given in much more detail in the Gentoo Handbook.)

Best case: install Vista FIRST. Reason being: Vista overwrites the Master Boot Record with its own boot loader - since, you know, who would ever want to use anything but Vista? Anyway - the general strategy is to let Vista have its way when it installs itself (there’s nothing that I know of that can prevent it erasing your boot record, so just go with it), and then go back and fix things from Gentoo once you’ve installed it. Which basically just means: install Gentoo in the normal way with TWO BIG EXCEPTIONS. Those are - first DON’T erase the Vista partition in the part of the manual where it tells you to erase all the disk partitions. Second - increment everything having to do with naming partitions by 1. So - for example - when the manual talks about things like /dev/sda1, you use /dev/sda2 instead. This is because everything is shifted back in the hard drive space on account of Vista being at the front. What follows more or less paraphrases the Gentoo manual with the relevant changes thrown in.

So:

  1. Install Vista in the usual way. This is idiot-proof - just pop your disk in the drive and let it do what it do.
  2. Tell Vista to resize itself. You do this in Control Panel -> Administrative Tools -> “Create and format disk partitions.” Choose “Shrink Volume” from the menu and … do so…down to a reasonable size (Vista’s bloated, so leave it some space - I gave it about 65% of my 150GB HD).
  3. Download Gentoo Minimal Installation CD (note - this assumes you will have an internet connection when installing). I choose the minimal CD because - well, that’s what Gentoo’s all about. You do it all by hand, and the payoff is that you get a system that has only what you specifically asked for on it - no bloat. Go to one of Gentoo’s mirrors (I usually use the Madison Chemlab) and find an appropriate directory (my computer being an Athlon Dual-Core 64-bit, I went to releases/amd64/2008.0/installcd/) and download the iso for the minimal installation. Burn this .iso to a CD.
  4. Boot from the CD. This will probably involve changing the boot order in your BIOS. For Dells, hold down F12 as soon as you see the “DELL” logo and it will give you a menu from which you can select what your computer boots from. Naturally, you want the CD option.
  5. Choose the Gentoo Kernel from the menu. It gave me four choices. In all but a very few cases you want the first choice, which is simply (and helpfully) called gentoo
  6. .

  7. Test your network connection. You should now have a prompt that’s something like livecd# . In most cases, you won’t need to worry about hooking up your network. I didn’t. To make sure, type /sbin/ifconfig at the prompt to execute a program that gives you basic information about your network. If it gives you a bunch of gibberish next to an eth0 label you’re good to go. Something like this (borrowed from Gentoo website):

    eth0 Link encap:Ethernet HWaddr 00:50:BA:8F:61:7A
    inet addr:192.168.0.2 Bcast:192.168.0.255 Mask:255.255.255.0
    inet6 addr: fe80::50:ba8f:617a/10 Scope:Link
    UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
    RX packets:1498792 errors:0 dropped:0 overruns:0 frame:0
    TX packets:1284980 errors:0 dropped:0 overruns:0 carrier:0
    collisions:1984 txqueuelen:100
    RX bytes:485691215 (463.1 Mb) TX bytes:123951388 (118.2 Mb)
    Interrupt:11 Base address:0xe800

    You can test it against the Gentoo webpage like so:

    livecd# ping -c 3 http://www.gentoo.org/
  8. Partition your disk. Here’s the crucial step. Run fdisk (type it at the prompt) to split up the remaining space on your disk. In fdisk, if you type p it should show you the current partition map. There should be only one - for Vista. Delete any other partitions (type d and then give it the number of the partition you want to delete). As soon as Vista is the only partition left on your disk, you’ll want to create three for Gentoo. Type n to create a new partiton followed by p to make it a “primary” partition, number it 2 (so as not to confuse it with the Vista partition, which is primary partition 1) and hit enter to start it at the end of the last partition (i.e. accept the default), and then do +32M for the first (which will be your boot partition). Make this parition bootable (type a). Do a swap partition next - again n followed by p, number it 3 and do +512M. Then hit t to explicitly choose a type - the type in this case being 82 (swap). Now eat up the rest of the disk in a third linux primary partition (remember, since Vista is there this is actually the 4th partition on the disk, so number it 4!!!). Type w to save this partition format and exit fdisk.
  9. Create the filesystems. These partitions are unformatted, so you have to give them a file format (else how would the computer locate anything on them?). In my case, I’m using only one SATA drive, so each partition is called /dev/sda plus the number of the partition. So Vista is on /dev/sda1, my boot partition is on /dev/sda2, my swap partition on /dev/sda3, and my primary Linux partition on /dev/sda4. I just went with the setup suggested by the Gentoo manual, which is to format the boot partition with ext2, the main partition with ext3, and the swap partition as … well, a swap partition. The specific commands are:

    mke2fs /dev/sda2
    mke2fs -j /dev/sda4
    mkswap /dev/sda3
  10. “Mount” the disks - which is fancy speak for giving yourself access to them. The commands:

    mount /dev/sda4 /mnt/gentoo
    mkdir /mnt/gentoo/boot
    mount /dev/sda2 /mnt/gentoo/boot

    In English - this just tells the system to call the main Linux parition /mnt/gentoo for now, so that you can get to it to write the files to it. You then make a directory for your boot directory and … who woulda guessed? … tell the system to call your boot partition that.
  11. Put the Gentoo system files onto your new disk partitions. cd into /mnt/gentoo. Surf to the Gentoo mirrors list using links, which comes on your CD:

    links http://www.gentoo.org/main/en/mirrors.xml

    Choose a mirror (I used the Chemlab at University of Wisconsin Madison) and download a stage 3 tarball. The name will be something like stage3-amd64-2008.0.tar.bz2 Download it, and then unarchive it:

    tar xvpjf stage3-amd64-2008.0.tar.bz2
  12. Download Portage. Same procedure as before: go to a mirror, find a portage snapshot (portage is a nice Gentoo thing that helps you find and manage packages to download for your system - crucial to setup and maintenance), download it, and untar it:

    tar xvjf portage-latest.tar.bz2 -C /mnt/gentoo/usr

    It’s important to get that command exactly right (yes, it’s a capital -C) so that the packages go in the right place.
  13. Edit your /mnt/gentoo/etc/make.conf file appropriately. Best to read the docs for this one. In my case, I added the following lines:

    CFLAGS="-march=k8 -O2 -pipe"
    CXXFLAGS="${CFLAGS}"
    MAKEOPTS="-j3"
  14. Choose your mirrors - i.e. where to download the operating system from. Gentoo gives you a nice way to do this:

    mirrorselect -i -o >> /mnt/gentoo/etc/make.conf
  15. Enter the new environment. Up to now, you’ve been working from the CDROM. Now it’s time to work from within the actual hard drive. Like so:

    mount -t proc none /mnt/gentoo/proc
    mount -o bind /dev /mnt/gentoo/dev
    chroot /mnt/gentoo /bin/bash
    env-update && source /etc/profile
    export PS1="(chroot) $PS1"

    The first line mounts an interface system with the kernel (main operating system). The second does the same for the hardware. Then you change filesystems into a filesystem that is rooted in what you used to call /mnt/gentoo (aka /dev/sda4). Having changed here, you need to update your environment variables so they make sense from the present location. And the last line is just some candy for the prompt to remind you that you’re in a changed filesystem environment.

  16. Update your system. You’re in a kind of Gentoo system already - but one big problem with it is it will, almost by definition, be out of date, since you downloaded a snapshot of a system. Update it using emerge - Gentoo’s package manager and package downloader interface:

    emerge --sync

    Probably it will tell you that emerge itself is out of date - in which case fix this before you do anything else:

    emerge --oneshot portage
  17. Configure USE variables. These are flags that help Portage decide which packages to grab for you and how to compile them. I simply copied their suggestions (which assumes you’re going to be going the KDE rather than the Gnome route for your desktop later on):

    USE="-gtk -gnome qt3 qt4 kde dvd alsa cdr"

    Add this line to /etc/make.conf.
  18. Install your kernel. One of the two crucial steps. You have to grab your kernel from the Gentoo website:

    emerge gentoo-sources
  19. Configure your kernel. The step that makes Gentoo Gentoo. cd into /usr/src/linux and type make menuconfig. You’ll get a dazzling array of options. Include everything that’s necessary - especially making sure that your filesystem and human interface devices are enabled. Don’t worry too much about getting it wrong. It will be mostly right as is, for one thing. For another thing, there are help messages to guide you. For a final thing, recompiling the kernel is much easier than you think in case you mess anything up or forget anything.
  20. Compile the kernel and copy it to the right place. The crucial step. Do it like this:

    make && make modules_install
    cp arch/x86_64/boot/bzImage /boot/kernel-2.6.27-gentoo-r8

    (The name of your kernel might be slightly different, of course. Look in /usr/src/linux to be sure what it’s called.)
    It takes less time to compile than your friends have led you to believe. That’s because they used the default genkernel option, which can take 10 hours or so to compile because it includes EVERYTHING. If you want to include EVERYTHING…just to be safe…you should consider Ubuntu Linux instead. Gentoo is Gentoo because you can configure it to your particular situation.
  21. Create your fstab file. This is how Linux knows which parts of the harddrive it can access. Getting this right is one of the important steps in dual-booting. Edit /etc/fstab to look like this:

    /dev/sda2 /boot ext2 defaults,noatime 1 2
    /dev/sda3 none swap sw 0 0
    /dev/sda4 / ext3 noatime 0 1
    /dev/cdrom /mnt/cdrom auto noauto,user 0 0

    This is almost exactly the same as what the Gentoo manual tells you, bucept that the partition numbers are all += 1. This, of course, is because VISTA is on /dev/sda1, so everything is shifted down one.
  22. Configure your network. Which, in my case, meant doing very little since I don’t have a static IP address or designated domain name. Just add this to /etc/conf.d/net:

    config_eth0=( "dhcp" )
    dhcp_eth0="nodns nontp nonis"

    And then make sure that it loads at boot time:

    rc-update add net.eth0 default
  23. Set a root password. Just type passwd and follow the prompts.
  24. Deal with some details. Edit /etc/rc.conf, /etc/conf.d/keymaps, and /etc/conf.d/clock, doing all the obvious things. In the case of the clock file, the “obvious” thing is to add a line:

    CLOCK="local"

    to use your local timezone. This does rather assume that you’ve set up your local timezone already. I did that like so:

    cp /usr/share/zoneinfo/America/Indianapolis /etc/localtime

    Plenty of other timezones are listed in /usr/share/zoneinfo so pick the one that’s appropriate.
  25. Add some other necessaries. Specifically, add a system logger, a cron daemon, a file indexing system, and a dhcp client (because your network interface is only here courtesy of the CD):

    emerge syslog-ng
    rc-update add syslog-ng default
    emerge vixie-cron
    rc-update add vixie-cron default
    emerge slocate
    emerge dhcpcd
  26. Configure and load the bootloader. THE crucial step for dual-boot. Basically, the trick to dual-booting with Vista is to keep in mind that Vista messed up your Master Boot Record during install. So, you have to overwrite what it overwrote with your own bootloader. For AMD64 systems there’s really only GNU Grub. First grab it:

    emerge grub

    Then configure it by editing /boot/grub/grub.conf. Mine looks like this:

    default 0
    timeout 10

    splashimage=(hd0,1)/boot/grub/splash.xpm.gz

    title Gentoo Linux 2.6.27-r8
    root (hd0,1)
    kernel /boot/kernel-2.6.27-gentoo-r8 root=/dev/sda4 video=uvesafb:mtrr:3,ywrap,1024x768-32@85

    title Windows Vista
    rootnoverify (hd0,0)
    makeactive
    chainloader +1

    The first line means to load the entry 0 of all the entries here by default. That’s the Gentoo entry. Vista would be 1 (but only because I’ve listed it second). Timeout is the number of seconds it waits before booting this by default. I set this to 10. You may want to check that the spalshimage is there. The file is right where it says: /boot/grub/splash.xpm.gz. The first bit is Grub syntax - so a word of explanation. Grub is one of those things that counts from 0, so hd0 is your first (and in my case only) hard drive - aka /dev/sda. The 1 is crucially different from the Gentoo documentation. That’s - again - because VISTA is already at (hd0,0) (aka /dev/sda1) - i.e. the first partition on the first hard drive. So in my case my Linux boot parition is (hd0,1) (aka /dev/sda2). Give it a title (what appears in the boot menu that lets you select which OS you want at boot time - call it whatever you want), tell Grub where it is - (hd0,1) - and then tell it where, specifically, the kernel file is - in this case on partition 4 - that’s /dev/sda4 to Linux, (hd0,3) to Grub - and of course you have to tell it exactly which file we’re talking about. The video flag is dependent on what kind of monitor and graphics card you’re using. In my case, one of the nVidia GeForce series with a framebuffer. Coincidentally, the same setup in the Gentoo manual, but yours might be different (or you might not need it at all). In the case of Vista, Grub doesn’t know how to boot it directly - so most of these commands just tell it where to pass control to. We tell it that there’s another operating system at /dev/sda1, but not to worry about it (rootnoverify (hd0,0)), and to use the chainloader to load it. What that means it simply that this is a proprietary OS that has its own mysterious boot method, so pass control to it and let it handle things itself.

  27. Install Grub to the MBR. Like so:

    grep -v rootfs /proc/mounts > /etc/mtab
    grub-install --no-floppy /dev/sda

    The first line creates a file that Grub needs from information gathered earlier (I just do what the manual tells me), the second line tells Grub to install itself on the Master Boot Record - to wit, overwriting Vista’s MBR.
  28. Eject and reboot. You’re done. You have a working dual-boot with Vista and Gentoo! All that remains is to get out of your special environment and reboot:

    exit
    cd
    umount /mnt/gentoo/boot /mnt/gentoo/dev /mnt/gentoo/proc /mnt/gentoo
    reboot

    Of course, take the CD out of the drive and readjust your BIOS settings as necessary to boot from the hard drive. If everything worked, Grub should load and give you a menu letting you choose between Vista and Gentoo. Gentoo won’t be very impressive or functional at this stage, of course. It’s just a barebones thing that you have to set up as you go. Choosing Gentoo boots it directly, choosing Vista causes the screen to go black for a bit as control is passed to the Vista boot process, but Vista boots forthwith.

DONE!

It’s much easier than one is generally led to believe. Basically, you follow all the steps for installing Gentoo as outlined in the manual - the only REAL differences are (a) that you don’t erase all the partitions from your hard drive before proceeding but rather keep the Vista one around and (b) that all things dealing with partitions are incremented 1 higher than the Gentoo manual - since Vista occupies what that manual thinks of as the first partition. The rest of the process is exactly the same.

Knock on wood, of course - because I only did this today, so who knows what “gotchas” Vista has in store for me? None, hopefully, but we’ll see…