Monday, May 23, 2016

Computation without compilation

The notion of programming is customarily associated with the practice of coding. Developing a program by coding requires the use of a compiler to translate the code you write into a form usable by the computational device.

I own a fairly simple machine, a DeLonghi TRN0812T space heater (the link is to a review by Consumer Reports, from which I also borrow the picture below). The primary purpose of the device is to heat a small room, but it can be programmed, which is my interest in this post. It can be programmed without requiring any written code to be compiled. Thus it does a (very simple) computation without compilation.

Two asides are needed: 1) the distinction between operating a machine versus programming a machine, and 2) bootstrap loading via toggle switches. In the interest of tl;dr I am going to defer these to later in this blog post.

Here is a picture of part of the control panel of the space heater. It shows two large switches which have four possible configurations: 0 (off), MIN (minimum), MED (medium), MAX (maximum). Above them, and not shown in the image, is a thermostat. Below the two large switches are 96 tiny switches arranged in a circle. These are used to program the heater.


The heater photographed above is set to maximum and programmed to run continuously, except from 5:30 a.m. when it will be switched off until 6:00 a.m. when it will switch back on. The time at which the picture was taken* is 10:30 p.m. and the heater is on.

Operating the machine is straight-forward. Turn it on to maximum (with all the tiny switches left in the factory setting: towards the center of the circle). Set the thermostat to its maximum position (again, the factory setting).When the room reaches the desired temperature, turn the thermostat down slowly until you hear a click, then stop at that point. The thermostat will then act to maintain that temperature. To save energy, you can switch off one of the large switches, thus setting the heater to either medium or minimum.

Aside: operating the machine requires you to be present and attentive. In contrast, programming the machine involves you doing something now which will have an effect later even in your absence.

Programming the machine is also quite simple. First, turn the knob clockwise (in the direction of the long arrow) until the pointer is aligned with a number representing the current time of day (in a 24 hour clock). Then push the tiny switches outward for all of the times (15 minute intervals) when you want the heater off.

Having programmed the machine, you can now leave it unattended and it will cycle on and off under control of your program. (The owners manual soberly points out that "you can ... program up to 48 cycles ... over a period of 24 hours")

I would like to point out that since there are 96 of the tiny switches, and since each one can occupy, independently, one of two positions, that there are 296 (=79,228,162,514,264,337,593,543,950,336**) possible programs. (The reader may find it interesting to compare this calculation to the approximation suggested by my Powers of 2 post which would have 296 to be approximately 64 x 1027, which is close... but quite a bit on the low side)

Of all the possible programs, I'll mention four or five. With all the switches out, the heater will never turn on -- not a very useful program, except, perhaps, as a practical joke. With all the switches in (the factory setting), the heater is always on. With every other switch out, you have one of the two possible programs which cycle the heater on and off 48 times over a 24 hour period (the possibility promised in the manual). Finally, the program currently on my heater, with half the switches in and the other half out, turns the heater off at 5:30 p.m. and back on again at 5:30 a.m.

A note about large numbers. If there were a billion owners and each owner tried a different program every day (with some mechanism to avoid duplication), it would take nearly 217,000 trillion years for all possible programs to be tried.

Finally, the aside about bootstrap loading. This heater intrigued me because it reminded me of old days in computing. Early machines would load their programs from punched paper tape. But the program which loaded the programs from paper tapes had to itself be loaded into the machine. And this was done by setting small switches to place an initial program into the machine. Wikipedia has a nice article about booting computers which you can find by clicking here.

This is a nice demonstration of a fact that I point out in my dissertation, which is that one can look at programming as choosing a number. Here instead of the more customary "think of a number between one and a hundred" we have, with this heater, "think of a number between one and 79,228,162,514,264,337,593,543,950,336." Once having the number, we can convert it to binary and set the 96 switches according to the ones and zeroes, programming it without needing to compile code.

* the arrow is pointing at the mark between 22 and 23, meaning that the current time when the picture was taken is 22:30 (24 hour time) which corresponds to 10:30 p.m.
** the number was computed using TOOL which can do large integer arithmetic on numbers of up to 147,456 digits. This number doesn't stress the system much because it is only 29 digits long.