Sunday, March 6, 2011

Factoring, in Forth and fiction

I was recently watching a movie that utterly sucked.

It was a kids' movie, which, in the modern style, made several allusions to grownup movies and pop culture in order to try to keep parents entertained. This works alright most of the time, but in this particular movie, something was hideously, unfunnily wrong.

The problem was that the cultural references seemed randomly inserted. They had nothing to do with the plot. A cartoon character would turn towards the camera and say "my name is Neo", but there was nothing Matrix-y in the character's situation, and his name wasn't Neo. It was as if the script writer had gotten the notion that quoting things was inherently funny. He forgot that things had to fit.

Making things fit in fiction is harder than you might think. The person on the receiving end of your story wants things to fit, yes, she wants to be bombarded with significant events, but she doesn't want to be able to piece things together so well that she knows where you're leading her. That would be cheesy (I think the definition of cheesy is knowing how you're being manipulated. When the too-rousing music plays behind the presidents' speech, or the groom on bended knee spouts out verbal saccharine, you feel weird because you know exactly what the writer wants you to feel, and how he's getting you to feel it.)

Things in life don't fit. Truly meaningless and unfunny things happen all the time. This is why life, unedited, makes bad TV.

When we come to a movie or book, we want something better described by the anthropology of religious ritual. Namely, we want:

(A) For things to feel random enough that we don't know where they're going, so we can identify the story subconsciously with our lives.

(B) For the sum of the random events to be a surprising, beautiful, logical edifice, so that we can be helped in believing that our lives will turn out thus.

So what's the best way for an author to achieve this?

Let's talk about Forth.

I love Forth because it saved me from my inclinations. Before Forth I tended to write in BASIC, and when I wrote, I wrote monolithic, innefficient things with no backspacing, while typing a little faster than I could think. Commonly in my code, you'd find things like:

a% = 5 + 5
b% = a% * 2
' do (oh wait, uncomment this later)
c% = b% / 3
d% = c%

I was thinking aloud. I was coding extemporaneously. I was freestyling. Who knows what the next line's going to be? Not me. But we're going to get this sucker done, and it's going to work.

You just can't do that with Forth. Or at least I can't.

Phrases like " 0 1 ROT 0 ?DO OVER + SWAP LOOP DROP" don't read like effortless prose to me. In fact, ideas expressed in more than just a few lines of ROT SWAP OVER DUP DROP are so incomprehensible that I'd rather pick up a book in Sumerian.

Because of this, I'm forced to program more deliberately. Also because of this, Forth programmers are big on factoring. So much so that Philip Koopman says that

: SUM-OF-SQUARES ( a b -- c ) DUP * SWAP DUP * + ;

should really be articulated as

: SQUARED ( n -- nsquared ) DUP * ;

In a well-factored program, everything is a one-liner, or close. You have a short word for squaring numbers, and then another for adding. Everything's compartmentalized and atomistic. No action should bleed into another action. The programmer should go over his code when he's finished, and then go over it again, and then again, every time splitting things up into tighter, more discrete units.

The process is the opposite of the process for writing good fiction.

Your reader will shelf your book and laugh uncomfortably if in some crucial moment your hero develops godlike powers and makes his problems dissappear with a blink.

That is, unless you've aluded to these godlike powers in other scenes. But as we've discussed, you can't just drop the line "He started to wonder if he had godlike powers." Remember? That's cheesy. It's too direct.

So what do authors do? When they're done with their first draft, they refactor. They actually anti-factor.

The story's discrete elements will be something like:

(1) Setting: we're in an apartment
(2) Introducing the friends
(3) The conversation about dreams
. . .
(8) Criminal interrogation
. . .
(426) He discovers he has godlike powers and slays all his enemies

A good writer will immediately look at this list and say - "Well I can't just let on that he's a criminal in the middle of the story. Let's have the friends reference that in (2). We'll put something in the scenery that suggests that too."

Given a thousand successful iterations like this, the author ends up with a perfect mess of spaghetti code. Everything will be significant, but the audience won't be able to tell where it's all going.

And in the end, if it's done right, when the protagonist says "My name is Neo", the audience will recall, conciously or not, everything that name hearkens back to. And they will gasp.

  Buy me a coffee to support posts like this.

  (Or, you know, a house. If you're just like a bored billionaire or something.)