Good Math, Bad Math

Programming in Color (fixed)

Todays programming pathology is programs as art.

Start with a really simple stack based language, add in a crazy way of encoding instructions using color, and you end up with a masterpiece of beautiful insanity. It’s not too exciting from a purely computational point of view, but the programs are really great to look at. Yes, it’s a pathological language with truly beautiful source code!

*(The original version of this post had some trouble because I linked to the original images in-place,
which the owner of the Piet webpage had blocked. I didn’t realize he didn’t want links. I’ve since downloaded
the images, coverted them to jpegs, and posted them here. I initially thought that the problem with the images was formats, which is what I originally said in this explanation. It’s not the image format, but the linking; but converting the files to jpeg and uploading them removed the links that caused the problem.)*

Here’s the complete list of
instructions (without control flow):

1. **Push**: Push a value onto the stack.
2. **Pop**: discard the top value from the stack.
3. **Add**: Take the top two values off of the stack, add them, and push the result.
4. **Subtract**: like add, but subtract the values.
5. **Multiply**: like add, but multiply.
6. **Divide**: like add, but divide.
7. **Mod**: like add, by modulo.
8. **Not**: pop the top value off the stack; if it’s 0, push 1, otherwise push 0.
9. **Greater**: pop the top two values off the stack, push 1 if the second value on the
stack is greater than the top, otherwise, push 0.
10. **Roll**: pop the top two values of the stack. Call the top one “count”, and the second
one “depth”. Now, take the top “depth” set of values on the stack, and rotate them by taking
the value on top, and putting it on the bottom. Do the rotation count times.
11. **In**: read a value from input and push it on the stack.
12. **Out**: pop a value from the stack and print it.

So far, seems pretty trivial, right? Standard stack based language, nothing unusual.

Ok. So, now let’s start getting weird. We’re looking at a two dimensional language, so the control flow operators are all based on *direction*. There are two things that control the flow of the program: the direction of motion, and the direction of selection. The direction of motion describes where the pointer goes on a step; the direction of selection determines where it looks if it needs more information than it has under the instruction pointer. So the control flow operations are:

1. **Switch**: pop the top stack value *n*, and switch the selection direction *n* times.
2. **Rotate**: pop the top stack value *n*, and rotate the motion direction 90 degrees clockwise
*n* times.

Ok, getting a bit weird now… But still, overall, pretty mundane. This isn’t a particularly
interesting model of computation, nothing exciting about it. Certainly nothing do beat, say,
Snusp for the beauty of 2d programming.

Now… Here’s where it gets *really* interesting. How are instructions encoded? By *color*. The program source is a PNG image!

Yes, todays language is called “[Piet][piet]“, in honor of Piet Mondrian, a modern abstract artist, because programs are intended to look as much like a painting by Mondrian as possible. There are twenty colors in Piet that have semantic significance, given in the table below:

#FFC0C0
light red
#FFFFC0
light yellow
#C0FFC0
light green
#C0FFFF
light cyan
#C0C0FF
light blue
#FFC0FF
light magenta
#FF0000
red
#FFFF00
yellow
#00FF00
green
#00FFFF
cyan
#0000FF
blue
#FF00FF
magenta
#C00000
dark red
#C0C000
dark yellow
#00C000
dark green
#00C0C0
dark cyan
#0000C0
dark blue
#C000C0
dark magenta
#FFFFFF white #000000 black

Most commands are given by the color transition between the pixel under the instruction pointer, and the pixel next to it in the selection direction. You’ll definitely need to keep that table handy to see how the instructions work.

So, let’s look at how we encode instructions. We take the current pixel, and the pixel next to it in the selection direction, and consider their *difference* in the two dimensions given in the color table: hue, and darkness:

* **Add**: +1 hue; no change in lightness.
* **Divide**: +2 hue; no change in lightness.
* **Greater**: +3 hue; no change in lightness.
* **Dup**: +4 hue; no change in lightness.
* **In** (input character): +5 hue; no change in lightness.
* **Push**: No change in hue, +1 darkness.
* **Subtract**: +1 hue, +1 darkness.
* **Mod**: +2 hue, +1 darkness.
* **Rotate**: +3 hue, +1 darkness.
* **Roll**: +4 hue, +1 darkness.
* **Out** (output number): +5 hue, +1 darkness.
* **Pop**: No change in hue, +2 darkness.
* **Multiply**: +1 hue, +2 darkness.
* **Not**: +2 hue, +2 darkness.
* **Switch**: +3 hue, +2 darkness.
* **In** (input number): +4 hue, +2 darkness.
* **Out** (output character): +5 hue, +2 darkness.

Numbers in Piet come from the *size* of the color blocks. The number of pixels in the block correspond to a number. So, for example, a block of 6 light yellow pixels followed by a yellow pixel will push the number 6 onto a stack.

Black and white are special colors – white is a separator; white pixels are just skipped over as if they weren’t there. Black pixels are blocks; if the program hits a black pixel, it will try switching the selection directions (ignoring white pixels); if that is also black, it will try rotating the motion direction +90 degrees. If it can’t find any non-black pixels using an sequence of alternating SWITCH/ROTATE instructions, the program will halt.

So now, finally, you can look at a few nifty examples of [Piet programs][programs]. To make them easier to read, the Piet programs are scaled up, so the pixels are actually represented by largish squares. As usual, we’ll start with “Hello World”:

i-d5c69738accfbc52c9ae791915e7354d-Piet_hello_big.jpg

That *is* hello world. Each of the large color blocks is one of the characters; the narrow lines are stack manipulations and output statements; and the little black arrowhead toward the top left captures the pointer and forces the program to halt.

Moving on, here’s a much fancier “Hello world”:
i-32186be14a3d879b98276ab3d5609453-hw3-5.jpg

One that really meets the goal of looking like a Mondrian painting, this program prints
out “Piet”:

i-7fa13d814a7c19d41d26945f2dd5980c-Piet-4.jpg

My personal favorite Piet program; this one prints out the alphabet; and the author provided
an [annotated version][annote] of it to help you understand how it works! Here’s the program:

i-454241ff5d911f198702f5593be9d38a-alpha_filled_big.jpg

One last brilliant Piet program, which even the guy who *designed* Piet can’t figure out… It’s the towers of Hanoi in Piet!

i-b42ac95db44d57d13424e8747c70e341-hanoibig.jpg

[annote]: http://www.dangermouse.net/esoteric/piet/alpha_filled_trace.png
[programs]: http://www.dangermouse.net/esoteric/piet/samples.html
[piet]: http://www.dangermouse.net/esoteric/piet.html

Comments

  1. #1 Joe Shelby
    November 3, 2006

    programs as art? you’re not too far from reviewing Shakespeare (programs as poetry), are you? :)

  2. #2 Xanthir, FCD
    November 3, 2006

    Hmm. I’m not getting any of the Piet pictures. I can see the color table, but that’s probably just plain HTML tables. ^_^

    I run Avant, an IE6 variant.

  3. #3 Blake Stacey
    November 3, 2006

    I’m not seeing the Piet pictures either (and I fun Firefox). When I pull an image URL out of the page source and try it directly, it works.

  4. #4 David Harmon
    November 3, 2006

    Ye freaking gods, that’s bizarre….

    By the way, regarding ALPACA, I’ve found that the entire mine.nu domain seems to be empty and reclaimed by its provider. So where the heck *can* we get it?

  5. #5 Mark C. Chu-Carroll
    November 3, 2006

    David:

    Unfortunately, the catseye site, where all of Chris Pressey’s languages are hosted, has a long history of unreliability. I’d
    put a copy up here, except that it was in one of the unrecoverable directories from *my* dead computer. A lot of
    the esolangs are cached on the esolang wiki, esolangs.org.

  6. #6 David Harmon
    November 3, 2006

    Incidentally, (catseye.)mb.ca is also gone.

    Regarding Piet, I guess those hue and lightness values wrap around the table?

  7. #7 Harald Hanche-Olsen
    November 3, 2006

    Right, no pictures with firefox. But I ran Safari on the page, and then the missing pictures show up as question marks. Right clicking on one, I could choose to open the picture in a separate window, and then I got a whole little web page, basically saying that he does not allow other web sites to link to his pictures “as if they were their own”. Seems that Mark has violated a policy of the owner of the pictures, inadvertently I am sure, but still …

  8. #8 David Harmon
    November 3, 2006

    No such luck –the esolang.org link (catseye.webhop.net) goes through dyndns.com (again…) to the mine.nu address. And Chris Pressey’s user page points to webhop.net.

  9. #9 David Harmon
    November 3, 2006

    Feh. Perhaps Mr. Pressey should archive his languages at esolang?

  10. #10 David Harmon
    November 3, 2006

    Incidentally, the “commented alphabet” program link also leads to DM’s 403 page.

    While poking around the Esoteric Programming Languages WebRing, I stumbled across Whirl, which might be a candidate for your series: http://www.bigzaphod.org/whirl

  11. #11 Uffe
    November 4, 2006

    A nice new meaning of ‘The art of Computer Programming’!
    A similar exercise has been made converting music notation into colours. ‘Warm’ colours for ‘warm’ (harmonic) intervals and ‘cold’ colours for the more dissonant intervals. It makes beautiful paintings out of Bach fugues and Chopin etudes, and sort of repetitive fractal images from Irish folk songs.

  12. #12 Jos'h Fuller
    November 4, 2006

    Note that “annotated alphabet print” program link also goes to warning page. I had to disable referrers in FireFox to see it.

  13. #13 Haley Mills
    November 4, 2006

    This is like, so groovey! I wish there was a programming language with little hearts, and smilys in the zeroes!

  14. #14 fwiffo
    November 4, 2006

    DOH! I thought my Towers of Hanoi in Brainfuck was pretty good. Guess it ain’t nothin’ special.

  15. #15 Martijn Verburg
    November 6, 2006

    Piet has featured in an ARG (Alternative Reality Game) called Perplxcity (perplexcityDOTcom), it was truly a fun card to solve :-)

  16. #16 Stephen
    November 14, 2006

    Pretty. Unreadable even compared to APL.
    It’s just begging for an obfuscated Piet contest.

The site is currently under maintenance and will be back shortly. New comments have been disabled during this time, please check back soon.