Saturday, July 24, 2010

Solving a 30 Year Puzzle

I have been interested in programming languages for over 30 years. One of the first programming languages I was interested in was Algol 68. Indeed I have always been annoyed that all subsequent languages get some things wrong that Algol 68 got right. More on that later.

One thing Algol 68 gets wrong is that it conflates variables and pointers. For clarity I’ll use more modern notation, only consider variables, and restrict arrays to 1 dimension: Var<X> for the type of X variables and Vect<X> for the type of 0-based 1-dimensional arrays of X. I’ll also show explicitly where parameterless functions are called.

What I have always liked about Algol 68 (that all other languages get wrong) is that identifiers stand for some sort of constant. The core declaration syntax is just “type identifier = value”.
  • Int three = 3;
  • Var<Int> i = loc<Int>();
  • Vect<Int> one2three = (1,2,3);
loc<Int> returns a Var<Int> entity (a box you can put an Int into) from the stack. The distinction between Int and Var<Int> is crucial for thinking clearly about programming. Note that Vect<Int> is a constant array of Int. We also note that the type denotation on the left is redundant, since it is just the type of the expression on the right, and a modern language would leave that out.

Constant arrays are undoubtedly useful, but mostly people will want traditional arrays that can be updated in place. This is where Algol 68 makes a delicate decision. The obvious decision is that an updateable array is a Vect<Var<X>>. However that seemed to imply passing around lists of addresses of variables. This seemed undesirable. So instead (actually: as well) they made special rules for Var<Vect<X>>. So if we have:
  • Var<Vect<Int>> vvi = loc<Vect<Int>>(3) := (1,2,3);
The rule is that normal Algol 68 dereferencing doesn’t happen when you write, for example, vvi[2]. Instead some magic happens and vvi[2] becomes a Var<Int> that allows you to update vvi in place.

This is really sad. Without this then we can easily regard an array as a procedure, with subscripting and slicing being normal procedure calls. Then a lot of stuff could be moved out of the core language into the standard library. It is always good if stuff can be done in the standard library, because it raises the chance that authors of other libraries will be able to achieve a good result.

Indeed it would be nice to get Var out of the core language. Var itself interacts horribly with type inclusion/coercion, being neither covariant nor contravariant in the underlying type. So if every X is a Y, and every Y is a Z, and a procedure takes Var<Y> parameter then you can’t consistently pass either a Var<X> or a Var<Z> to the procedure. Also standard variable semantics are unsuitable for the modern world where concurrency is exploited at all scales. It seems that we need multiple Var-like types with different concurrency guarantees.

Getting back to the problem. First we need to understand a couple of things.

Representing values at run time

How many bits do you need to represent the number 3 in a computer? If you didn't get 0 then try this one. How many bits does it take to represent a number that might be 17 or 18? Now it is more obvious that the answer is 1 bit: 0 for 17 and 1 for 18 (or vice versa). Of course it might result in a shorter and/or faster program if the number is represented by 17 for 17 and 18 for 18, but that might not be the case.

Maybe you think that this only applies when the values are known at compile time. But JIT compilers are becoming the norm, so that distinction is disappearing. In an Algol 68 style language where every identifier stands for a constant, it may not be necessary to actually materialize the value at run time at all. Indeed that is the norm for Var values, since they are normally just a fixed offset from the frame pointer, and machines typically support that conveniently in the hardware.

Assembly Language had it right

Most of my early programming was in various assembly languages. Assembly language is just a convenience veneer on the machines underlying instruction set. In assembler identifiers are created in two ways:
  • A define statement gives an identifier a fixed meaning as a bit string.
  • A label statement gives an identifier a fixed meaning as a memory address of some code or of some reserved memory.

This was right. The first languages (Fortran, Algol) used identifiers for changing values. Algol 68 tried to get back on track, but slightly got it wrong. After that it was downhill all the way in the mainstream. On the side we had functional programming, but that doesn’t solve the problem of handling Var types correctly, it just tries to eliminate them.

The right answer

Putting it together we see that we can implement a normal updateable array in the natural way as a Vect<Var<X>>. It can normally be a sequence of consecutive Var<X> boxes. The compiler can remember this and doesn’t need to materialize the addresses of the Var<X> boxes. We still want to write:
  • Vect<Var<Int>> vvi = loc<Vect<Var<Int>>>(3) := (1,2,3);
but now we need to enhance the := assignment operator to do parallel assignment.

But what happens when we want to pass a Vect<Var<X>> as a parameter to a procedure? Mostly we want to send that as a start memory address and a length, and perhaps a stride. But we don’t want to abandon the option to send a fully general list of Var<X>, i.e. of pointers to Xs in memory. Both these representations are valid and don’t change the semantics of Vect<Var<X>>.

We need to embrace this. Each type can have multiple representations defined in terms of other more low level types. At the bottom things have to be defined in terms of the capabilities of the raw machine, or some abstract machine. So for a given procedure there are a finite number of implementations covering the possible input representations, and indeed the possible output representations. There is no reason for this to get out of control in the era of JIT compilation. It is hard to believe that duplicated implementations of procedures can work out worse than the technique commonly used for traits/interfaces of passing a pointer to the object and a pointer to a list of implemented methods.

Well anyway it will be interesting to investigate. My concept of a nice programming language seems within reach for the first time. The name will be COPL, for Closure Oriented Programming Language.

Tuesday, July 20, 2010

Negative numbers for Finance


Negative numbers for Finance

Negative numbers have been around for hundreds of years. It’s about time the world of finance got with this new development.

We are told that interest rates can’t go below zero in nominal terms. Why not? We’ve often had negative real interest rates, and that seems more significant. In periods of deflation, negative nominal interest rates might be positive in real terms anyway.

Consider the stock market. Currently people who expect inflation are buying stocks of companies with debt, expecting inflation to get rid of the debt. People who expect economic weakness and deflation are buying bonds at near 0 interest. Having this split market seems bad. It would be better if the people buying stocks also shorted bonds and people buying bonds were also shorting stocks. One way to do this would be to allow people to hold a negative amount of bonds. This would allow other people to hold more of positive amounts. Of course if you own a negative amount of a bond then every time there is a dividend then you have to pay that. This money then goes as normal dividend to the extra people with positive amounts of the bond. Negative holdings of stocks would be tricky because of the additional problems with voting and stock splits.

Monday, July 19, 2010

Unleashed submission

Necessary Inflation

[Note that this is a based on earlier posts. The last two paragraphs have some new stuff.]

Paul Krugman keeps telling us that we need to keep stimulating the economy in America and Europe. What he rarely says too loudly is that there is a limit to how much this can be done by borrowing. The only possible source of money for that stimulation is to print it. Of course it isn't literally printed: it is all just numbers in computers.

When GFC/I hit, then central banks did start printing money. They called it "Quantitative Easing". They used the printed money to buy bonds, and they promised that they would later unprint the money by selling the bonds. Printing money is criticized for causing inflation. Commentators seem to have no trouble saying this with a straight face when the reality in America and Europe is damaging deflation. We can certainly agree that unprinting money at this time will add to deflation.

So why is everyone so hysterical about inflation? We are usually pointed at Germany's bout of hyperinflation in the early 1920s as a lesson in the bad things that can happen. But hang on: After their burst of hyperinflation sorted out their economy, Germany got its act together, and only 15 years later they nearly conquered the whole of Europe. Meanwhile their victorious opponents who were also basket cases in the early 1920s didn't have inflation, and they were still basket cases 15 years later. So we need to understand what inflation does that is helpful. But first a foray into politics.

Listen to the debate between the stimulators, led by Paul Krugman, and the deficit-reducers, led by right-wing parties everywhere, with the left too nervous to criticize. You would think that this was a purely technical debate about how best to run the economy in the national interest. Maybe not. When governments act they produce winners and losers. This has the unfailing effect of bringing out the engines of disinformation on both sides. And inflation makes losers of the most powerful people on the planet: lenders and people with cash in the bank.

Inflation makes it easy for borrowers to pay back their loans. This means that borrowers come to fully own the real physical assets that were the collatoral for the loan. They then lavish care and attention on those assets and make them as productive as they can be. Consider on the other hand what happens when the economy is weak and there is also deflation. The borrowers know that they can never repay that loan. They squeeze as much value as they can out of the assets without the ability or incentive to properly maintain them. The employees who know how to operate the equipment leave. Eventually the lenders get the assets but the value of those assets is substantially reduced.

So who wins from deflation and loses from a bit of inflation? Hardly anyone. Except that this misses some important human traits. One is that we hate seeing other people get something for nothing, and there is no doubt that debtors get that with inflation. Also humans see gains and losses in relative terms: we like to do better than others, particularly our neighbors. That's why increasing standards of living don't increase happiness. So the lenders rightly see inflation as transferring real wealth from them to debtors.

How much inflation do we need? Our main experience since WWII is that economies run well with about 3% growth and 2% inflation. This means about 5% growth in the nominal economy (the size of the economy in dollars, without taking into account the decline in the value of the dollar). It seems likely that this applies whatever the growth rate. If the real growth rate is 10% then a 5% deflation rate won't cause any problem. On the other side of the ledger, if the real economy is declining at 2% for some reason, then you need 7% inflation to keep the wheels of commerce moving. (Note that we should be taking population change into account, so that the required growth in the nominal economy is probably 3% per head of population, assuming the added people are statistically similar to the existing population. But I'll ignore this subtlety.)

Why do we need this nominal growth? Without it then people who just sit on their money, and don't try to use it to generate wealth, do nearly as well as, or perhaps better than, the people who are trying to move the economy along.

To understand why money isn't, and can't be, a store of value, it is important to understand that you can't move wealth into the future through money. This seems like a strange statement when we do it every day. We put money in the money box and much later we get it out and spend it. But what really happens is this. When we put money in our money box we reduce the amount of money circulating. There is less money chasing the same amount of goods and services. So each bit of circulating money can get a bit more than it could before. There is a very small amount of deflation. When you get the money out of the money box to spend, then there is more money chasing the same amount of real stuff. So this reduces the value of all the other money circulating at that time. That's how the value gets back into the money that you kept in the money box. We need to understand how our economy might work if it is declining instead of growing, and to do that we need to think clearly about how money works. The important point here is that individuals can move wealth into the future through money, but society as a whole cannot. Society as a whole needs to move wealth into the future to prepare for difficult times, but it does this by storing non-perishable commodities and by building productive infrastructure.

If, as I and many others now believe, oil production rates have reached a plateau and will tend to decline from now on, then we need to change the fundamental infrastructure of society. This will lead to economic decline while we switch infrastructure. We need to understand how to run a declining economy to be as productive as possible. And we need to direct that production as much as possible to the infrastructure switch: electrification of transport, heating, farming and everything else, and the production of cheap electricity. Yes we need stimulus, but not random stimulus. Let's use the stimulus to make capital investments now, like electrified rail, that will protect us from the economic decline in the future.

To help us understand the issue, consider what Paul Krugman has recently written in his blog (http://krugman.blogs.nytimes.com/2010/07/17/more-on-deficit-limits/) "there’s also no question that right now, the proposition that the government can 'create wealth by printing money', which some other commenters call absurd, is the simple truth: deficit-financed government spending, paid for with either debt or newly created cash, will put resources that would otherwise be idle to work." By "right now" he means the American situation with high unemployment and zero interest rate. What he writes would be true in the situation that existed through most of the 20th Century where workers and working infrastructure were the only constraints and energy was cheap. But now we have infrastructure that is geared to oil use, and oil production can not be raised significantly at any affordable price. So when we stimulate we create activity that needs oil. This raises the price of oil, which negatively impacts other activities. So the Right's claim that government activity, particularly stimulus, steals from the real economy is correct in our oil-constrained world. However the Right's assumption that the free market will get us where we need to go will only be correct in the long run. We can't afford to be trying to electrify transport when oil production is actually declining, which will be happening in 10 years. Government needs to steal from the rest of the economy to force the transition to an electric economy as soon as possible. That's why stimulus should be used for energy use change and energy production infrastructure, and not for anything else.

Another recent blog post by Krugman (http://krugman.blogs.nytimes.com/2010/07/14/nobody-understands-the-liquidity-trap-wonkish/) explains why the America and Europe can't easily prevent deflation by just printing money to buy bonds. Governments need to print money and spend it. They could alternatively send it to the voters to try to restart Business As Usual, but that would be a mistake in our current oil production constrained situation.

Monday, July 5, 2010

Response to Pettis on Real Economics


Response to Pettis: http://mpettis.com/2010/07/what-do-banking-crises-have-to-do-with-consumption/.

Reading this excellent analysis one would hardly guess that money isn’t real wealth, and that the banks and other financial entities are merely facilitating the real economy. So what is going on in the real economy. Spending is a mix of people with the money (ex-savers) and people who intend to earn (who will be in debt after the spending). Also production is a mix of production for immediate consumption and creation of infrastructure for future production. The job of the financial system is to keep the balance between these things optimal. The situation today is that oil is getting harder, otherwise we wouldn’t be drilling at unmanageable depths. So we are perforce changing infrastructure (which started long ago when we stopped using oil for large electric power plants). So the optimal balance of activity needs to be skewed away from immediate consumption and towards creation of new infrastructure. It rather seems that our current financial system’s (unconscious) plan is to completely destroy the economy then rebuild it. Is this really necessary, or desirable?