How To View Inception Through Code

If you have not seen Inception yet, I highly recommend doing so before reading this post.

**** SPOILERS AHEAD ****

Inception Movie Poster

Inception combines the best elements of movies in the Simulism genre; such as Dark City, eXistenZ, The Thirteenth Floor and The Maxtrix Trilogy; and creates an original, thought provoking film.

Watching the film from the standpoint of a programmer, I could not help seeing the similarities between the plot devices and basic programming concepts.

The Kick

The Kick is the concept they use in the movie to return to a dream level above the existing one. For example, if you perceive that you are falling in a dream, you have a tendency to wake up.

The Kick can be portrayed in programming in several different ways:

Recursion

public bool Dream()
{
     kick = CheckForKick();
     if(kick) return true;
     return Dream();
}

* CheckForKick() implementation has been omitted.

In this recursion example, each call to function Dream() will call another sub-Dream – until the CheckForKick() function returns true, which can occur however many levels deep.

Exception Bubble

public bool Dream()
        {
            try
            {
                try
                {
                    try
                    {
                        throw new DreamException("Receive Kick");
                    }
                    catch(DreamException dx)
                    {
                        throw dx;
                    }
                }
                catch(DreamException dx)
                {
                    throw dx;
                }
            }
            catch(DreamException dx)
            {
                // Handle Kick thrown 3 levels deep.
                Console.WriteLine(dx.Message);
            }
        }

In this Exception example, an exception is thrown within a nested try block. Rather than handling the exception at that level, it’s bubbled up until is (hopefully) handled gracefully.

Stack

This can best be represented by the following infographic, which I reconstructed to be a stack. Here is the original source.

Inception Stack

A stack is one of the most fundamental data structures in computing. A stack operates in the following: items are added in a First-In, Last-Out (FIFO) manner. They are added via being “Pushed” onto the stack and removed via being “popped” from the Stack.

In the movie, each sub-dream is pushed onto the stack starting with reality. Our characters cannot return to a different dream level by popping the dream above off the stack.

Sedation

They use Sedation in the movie to prevent the dreamers from waking up any means other than “falling” on all dream levels above the first. At the first dream level, the only way to wake up is for the machine’s time to elapse.

Sedation in the movie can be explained through the use of Threading.

Threading

public void Inception()
{
     Machine mach = new Machine();
     Sedation sed = new Sedation();
     Thread machineThread = new Thread(new ThreadStart(mach.Start());
     Thread sedationThread = new Thread(new ThreadStart(sed.Sedate());
     try
     {
          sedationThread.Start();
          machineThread.Start();
          sedationThread.Join();
          machineThread.Join();
          ReturnToReality();
     }
     catch(Exception ex)
     {
          // Limbo ???
     }
}

* Machine implementation omitted. Sedation implementation below.

In this example, you can think of the sedation and starting the “dream machine” as being two separate threads – both induce unconsciousness, but in different ways.

The sedation occurs first as indicated by the sedationThread.Start(), followed by the machine start machineThread.Start(). The Sedation.Sedate() implementation may look like the following:

public void Sedate()
{
        Thread.Sleep(36000000); // 10 Hours
}

By calling join on the threads, the ReturnToReality() function will not get called until both the sedation wears off or the machine time elapses.

Limbo

Limbo is defined in the movie has a mental state where your mind doesn’t know if you are in reality or not. The only way out is to realize that Limbo is not reality.

I already hinted at how Limbo could be represented in code in the previous example – if there is an exception (i.e. death) during the running of the threads that does not allow them to complete. Also, Limbo could also be represented by the following:

Infinite Loop or Infinite Recursion

while(!IsAwareOfReality)
{
     CreateBuildings();
     CreateFromMemories();
     QuestionReality();
}

Most of the time that we are programming and find ourselves in an infinite loop, we are not aware of it until we start to question:

  • Why is this taking so long?
  • Why is my memory decreasing?
  • Why is my CPU pegged at 100%

Fortunately in programming there are ways to identify that we are in an infinite loop state.

If you are not aware of these conditions, you will stay there until your program runs out of memory or crashes (dies) or you realize you are in a loop (meet a condition) – by calling the QuestionReality() function.

Architect

Ellen Page

The last item I will mention is the role of Architect. In the movie, Ariadne (Ellen Page) is responsible for creating the dreamscapes. This shares similarities with Software Architecture. As a software architect you responsible for knowing the systems (the main characters in movie) that you work with and to create the best solutions (e.g. Never Ending Staircase) to new problems that need to be solved (Inception). Being able to see the world around you as objects with state and behavior is a useful skill for both understanding Object Oriented Design/Programming; and as a Software Architect.

I was surprised how many people shared similar thoughts with comparing the movie with programming. If you enjoyed my post, please read their perspectives:

  • Delicious
  • Share
  • Subscribe to RepeatGeek

Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

8 Comments

  1. Rick
    Posted July 31, 2010 at 6:34 pm | Permalink

    Nice post and at the same time pretty much sums up why Inception sucked, our dreams can’t be reduced to code, but Inception can. Nolan even proposes stabilized time in dreams, talk about reductionism.

  2. Posted July 31, 2010 at 8:18 pm | Permalink

    Some of these analogies are a stretch, but the stack and thread ones make sense.

    @Rick Inception did not mean to portray machine-induced dreams as regular, everyday dreams. Machine-induced dreams are way crazier and involve multiple levels and sharing; it is not claiming that the normal dreams in its world have these special properties as well.

    That means you can’t apply any current knowledge of “normal dreams” to machine-induced dreams, because they are in Inception’s sci-fi mesh of “unknown technology.”

  3. Posted July 31, 2010 at 8:31 pm | Permalink

    Max, thanks for your reply.

  4. logan
    Posted August 1, 2010 at 9:36 pm | Permalink

    well done sir

  5. Joe Darcy
    Posted August 4, 2010 at 1:34 am | Permalink

    Another summary of Inception in code:

    “Understanding Inception using try-with-resources”
    http://blogs.sun.com/darcy/entry/understanding_inception_using_try_with

  6. Prabhakar
    Posted August 4, 2010 at 1:56 am | Permalink

    I think similarly.
    I see everything as a recursion, where a kick is the terminating condition.

    Limbo is a special case where the recursive loop never terminates due because of the corruption of the terminating condition due to buffer overflow (lol)

    And sedation is the parameter passed to the recursive function that controls the recursion duration. Similar to your thread timeout logic. :P

  7. kamalcom
    Posted August 4, 2010 at 4:05 am | Permalink

    Nice comparison and nice movie :)

  8. dude
    Posted August 23, 2010 at 1:09 pm | Permalink

    Not really impressed with your logic…

    maybe a loop for checking the condition of the Kick?… and death…

    Movie had a “glitch” (in my opinion) how did they get the drug into dream? should have used some phsichy trick…

3 Trackbacks

  1. By === popurls.com === popular today on July 31, 2010 at 5:59 pm

    === popurls.com === popular today…

    yeah! this story has entered the popular today section on popurls.com…

  2. By How To View Inception Through Code | Zocials on July 31, 2010 at 6:16 pm

    [...] by admin on Saturday Jul 31, 2010 Under Zocials submitted by repeatgeek to programming [link] [5 comments] Social [...]

  3. By All things Inception | Lens on August 5, 2010 at 10:22 am

    [...] More programming link [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Subscribe to RepeatGeek

Subscribe via RSS

Enter your email address: