Inception: A programmer's guide (spoiler warning)

I saw Inception last night. It's another in the series of ISBs (Intelligent Summer Blockbusters), in 2008 we had 'The Dark Knight', in 2009 - District 9, this year it's Inception. It is positively teeming with ideas - it might be the first ever psy-fi movie. It's worth viewing as much for the mental gymnastics it puts your mind through, as the physical gymnastics you see on-screen.

Inception really engaged the Programming part of my brain while I was watching it. It's the only movie I can think of whose plot can be expressed in pseudo-code - it even has exceptions! (the 'kick' which wakes up the dreamers from the inner dreams can be thought of as an InnerEarException).

In the movie, dream time is not synchronous with real time. An hour in dream time is just 5 minutes of real time and in a dream within a dream that difference is compounded so that in 5 minutes of real time, 12 hours could pass in a dream within a dream.This is explained as the brain functioning faster in dreams. The movie goes deep into dreams - 4 levels deep at one point so that what you are watching is a dream within a dream within a dream within a dream.

Fischer's dream (Kidnapped / Moving Van)

Arthur's dream (Hotel)

Eames' dream (Snow Fortress)

Cobb's dream (Limbo / in search of Saito)

Initially I thought of it as nested loops with the innermost loops consuming more CPU (In Cobb's innermost dream, Saito has grown old) but inner loops are re-entered so that doesn't really work as a good programming analogy. Another reason it doesn't work is the 'kick' which wakes up the Arthur's, Eames' and Cobb's dream simultaneously. The 'kick' is a disturbance in inner-ear equilibrium which induces the sensation of falling, forcing the dreamers to wake up (an InnerEarEquilibriumException in Java parlance). In the movie, the 'kick' isn't induced from the real world but by the protagonists/infiltrators in Fischer's outermost dream - this causes the innermost dreams to cease while Fischer's dream continues (this is why we see all the protoganists in the Van even after it has hit the water in Fischer's dream). The best programming analogies I can think of to explain Inception's truly mind-bending plot are multi-threading and exception-handling. Each dream can be thought of as a Thread wherein other dreams/threads are started. Asumme that each new Thread which is created gets a higher priority than the last thread so that more gets done in the later-created threads.

Time passes simultaneously in each thread just like in the movie there are 4 different timelines (though the perception of time passing differs in each). 3 of those 4 timelines get interrupted when the 'kick' (a moving Van which falls into a river) triggers the innermost dreamers to awake leaving just Fischer's outer dream which eventually also ends. This is where my programming analogy gets a little ropey - in programming an Exception normally percolates up from the innermost process. In the movie the trigger must be pulled simultaneously in Fischer's, Arthur's and Eames' dream. This is where I think the movie's logic falls short - The 'kick' should only be required in the outer dream and automatically cascade down to the inner dreams. This would make Arthur's efforts (roping the protagonists together in the zero-gravity hotel and planting explosives in the lift to trigger a G-shock), and Eames' efforts (blowing up the snowy mountain fortress) completely redundant and would make for a less action-packed movie though.

Other programming analogies spring to mind. Cobb explaining to Adrianne how dreamscapes are made...

In dreams we create and perceive at the same time.

That's Javascript's prototype-based Object orientation right there - the ability to create new Object types on the fly. It reminded me of this...

http://pixenate.com/pixenate/javascript/pxn8_imagemagick.js

... which is probably the most mind-bending snippet of code I've ever written. The malleability of dreams is like Javascript's prototyping. I'm off to re-read Douglas Crockford's 'Javascript: The good parts' and watch Waking Life again. I'm in the mood for some more mind-bending stuff.