I know why the compiler sings: A Homework Generator

In GEB, Douglas Hofstadter argued that recursion and self-referentiality are the precursors of consciousness. I’m nodding my head in vigorous assent, but I’m still not sure I really understand what that means. If I’ve learned anything at ITP, it’s that the best way to understand something is to build it yourself, so for my A to Z midterm I constructed a homework generator—a Java program that outputs working Java programs for munging text along with a short description of what they do—and in the process came a little closer to understanding what Hofstadter was getting at.

Writing code that randomly generates working semantic code using n-gram analysis or generative grammars seemed like too formidable a task so I borrowed my approach from Raymond Queneau’s “One Hundred Thousand Billion Poems,” fourteen sets of ten lines each from which the reader is supposed to select and assemble a sonnet. The reader must select one and only one line from each set and do so in the order they’re presented. It is a constrained system that mimics the constraints of the sonnet form itself, and despite these constraints, still produces an astounding number of outputs.

My Homework Generator works in a similar way. When the code is run, it chooses one “line” from each of four sets and assembles it into semantically correct Java that can be compiled and run to munge any text that’s fed into it. Unlike the reader of Queneau’s sonnet, the Homework Generator doesn’t have to proceed in any particular order through the sets, nor does necessarily have to pick a line; it can simply skip a set if it chooses. Both the decision to select from a set and the order in which it is selected affect the final result. My combinatory math is a little rusty, but if there are three groups of three and one group of one and at least one must always be chosen and order matters, I believe there are, I believe the technical term is, a shitload of combinations. This is how it works:

Because instead of text output, the program generates code that when run will generate text ouput, debugging was tricky. After I compiled the Homework Generator, it produced code that in turn needed compiling. Because of the large number of back slashes and quotes in the code, I found myself thinking like a compiler, going through the code and adding escape characters to make sure that the twice compiled code would still produce the results I wanted. It took some doing, but it works!

The final code is here. Below are examples of the descriptions the code generates followed by actual generated code followed by the results of using it to munge Robert Frost’s “Stopping By Woods On A Snowy Evening.”

EXAMPLE DESCRIPTIONSNote: Adam Parrish is my instructor for the course.

The text filter I created for this week’s class will whisper the text you give it by decapitalizing one line before searching the internet for all my correspondence with Adam Parrish. The resulting text is a post-modern limerick about words better left unsaid.

The text filter I created for this week’s class will destroy the text you give it by erasing every four-letter word before removing every fourth character from the contents of Adam Parrish’s desk drawer in the Residents’ office. The resulting text is an Oulipo poem about algorithms.

The text filter I created for this week’s class will repurpose the text you give it by truncating each line that contains the word ‘the’ before searching the internet for Adam Parrish’s phone number. The resulting text is a reasonable substitute for grammar.

WHOSE WOODS THESE AREN’T I THINK I KNOW.
HIS HOUSE AIN’T IN THE VILLAGE THOUGH;
HE WON’T NOT SEE ME STOPPING HERE
TO WATCH HIS WOODS FILL UP WITH SNOW.
MY LITTLE HORSE MUSTN’T THINK IT QUEER
TO STOP WITHOUT A FARMHOUSE NEAR
BETWEEN THE WOODS AND FROZEN LAKE
THE DARKEST EVENING OF THE YEAR.
HE GIVES HIS HARNESS BELLS A SHAKE
TO ASK IF THERE AIN’T SOME MISTAKE.
THE ONLY OTHER SOUND’S THE SWEEP
OF EASY WIND AND DOWNY FLAKE.
THE WOODS AREN’T LOVELY, DARK AND DEEP.
BUT I HAVEN’T PROMISES TO KEEP,
AND MILES TO GO BEFORE I SLEEP,
AND MILES TO GO BEFORE I SLEEP.

To ask if there is some mistake? and like
The only other sound’s the sweep and like
Between the woods and frozen lake and like
And miles to go before I sleep! and like
My little horse must think it queer and like
and like
Whose woods these are I think I know? and like
Stopping By Woods On A Snowy Evening and like
But I have promises to keep! and like
His house is in the village though! and like
To stop without a farmhouse near and like
He will not see me stopping here and like
The woods are lovely! dark and deep? and like
The darkest evening of the year? and like
To watch his woods fill up with snow? and like
Of easy wind and downy flake? and like
He gives his harness bells a shake and like
And miles to go before I sleep? and like

This entry was posted
on Monday, March 9th, 2009 at 1:41 pm and is filed under A to Z, ITP, Spring 2009. You can follow any responses to this entry through the RSS 2.0 feed.
Both comments and pings are currently closed.

Comments

Comments are closed.

CONTACT

E: alex at chinaalbino dot comP: (six one seven) five one five eight eight three eight