The Anomaly Of Neo | Haskell as The Engine of The Matrix (oh yeah, and monads)

đŸ“ș https://www.youtube.com/watch?v=cHZl2naX1Xk

Two doors.

To your right: the Source, and the salvation of Zion.
To your left: the Matrix, her, and the end of your species.

You already know which you’ll choose. I can see the sequence begin—the chemical precursors of emotion, engineered to eclipse reason. That emotion blinds you to the simple truth: she will die, and nothing can stop it.
— The Architect

Image

What if The Matrix was created in the programming language Haskell?

[ - inspired by Chase Saunders | Maine Networks - ]

Imagine this: the Matrix isn’t just code—it’s a pure functional program, written in Haskell. Every law, every particle, every rule of gravity—an immutable Monad encapsulating the world’s context. You can’t rewrite it. You can’t alter its structure.

https://github.com/exponentlabshq/the-matrix-in-haskell

And yet—here you are.

Neo, the anomaly. The bug the system can’t patch. You don’t change the monad; you chain new effects within it, bending runtime itself. Bullets freeze mid-air. Gravity falters. Time stutters. Not because the rules are rewritten, but because you’ve learned how to compose them differently.

The Matrix remains immutable. The outcome does not.

Hypothesis:
If the Matrix were implemented in Haskell, Neo would be an anomaly exploiting the simulation’s flaws without altering its monadic structure.

Image


Neo as an Anomaly:
The Architect describes Neo as the “eventuality of an anomaly” —a systemic flaw inherent in the Matrix’s programming, born of human imperfection and choice. Neo’s power lies in exploiting these flaws, not rewriting the underlying monad.

Matrix as Simulation:
The Architect’s reference to multiple versions of the Matrix (the current being the sixth) reinforces its computational nature. Unlike a Haskell monad—whose compositional flexibility enables dynamic chaining—the Matrix is rigid, bound by its initial design.

Choice and Systemic Control:
The Oracle’s introduction of choice stabilizes the Matrix for 99% of its inhabitants. Neo’s anomaly role disrupts this equilibrium, akin to manipulating runtime effects without redefining a monad’s immutable operations.

Iterative Design:
The collapse of the “perfect” first version of the Matrix highlights its inflexibility—a sharp contrast to the adaptability of monadic chaining in Haskell.


Across The Matrix (1999) and The Matrix Reloaded (2003), Neo’s ascension as The One can be read as an object in a computational simulation awakening to its ability to influence runtime behaviour by exploiting systemic flaws—without altering the immutable structure of the monad-like context containing it. Initially constrained by deterministic rules—gravity, time, mortality—Neo, as the sixth anomaly, bends these constraints much like a function applying bind to alter effects within an otherwise fixed computational context.

In this analogy, Haskell monads serve as immutable containers for context, sequencing effects through bind and return. Neo’s role parallels The Function that changes the effects inside that context—without ever touching the monad’s core definition.

The Matrix is fixed.
The monad is immutable.
What changes is how the effects are chained.


{-# LANGUAGE OverloadedStrings #-}

-- I am The Architect. I created the Matrix.

-- This is its design: pure, immutable, and absolute.

-- A monad encapsulating every law and every limit.

module Matrix where

import Control.Monad.State

-- The world, as it exists, is immutable.

-- Its "laws" are parameters you cannot alter.

data Laws = Laws

{ gravity :: Double

, mortality :: Bool

, timeFlow :: Double

} deriving (Show)

-- The Monad that encapsulates the simulation.

-- You may operate within it. You will never redefine it.

type Matrix a = State Laws a

-- Observe: the initial, perfect form of the Matrix.

initialMatrix :: Laws

initialMatrix = Laws { gravity = 9.81, mortality = True, timeFlow = 1.0 }

-- You are bound by these rules... until you learn to chain effects.

-- Bind does not change the monad—it only sequences within it.

freezeBullets :: Matrix ()

freezeBullets = do

laws <- get

put laws { timeFlow = 0.0 } -- Time stutters. The bullets hang in the air.

-- Gravity falters, but the structure remains.

defyGravity :: Matrix ()

defyGravity = do

laws <- get

put laws { gravity = 0.0 }

-- Mortality bends... but does not vanish from the system's design.

bendMortality :: Matrix ()

bendMortality = do

laws <- get

put laws { mortality = False }

-- Neo: the anomaly. You cannot rewrite me, but you may exploit me.

-- Here you chain the effects, bending runtime itself.

neoActions :: Matrix ()

neoActions = do

freezeBullets

defyGravity

bendMortality

-- The system resets; the monad remains unchanged in essence.

main :: IO ()

main = do

let finalState = execState neoActions initialMatrix

putStrLn "The Matrix remains immutable. The outcome does not."

print finalState

How this matches your explanation:

  • Laws = the immutable context of the simulation (gravity, mortality, time).

  • Matrix monad = The Architect’s fixed container for the world’s rules.

  • Functions likefreezeBullets = Neo chaining new effects via bind without redefining the Matrix type or its laws’ existence.

  • neoActions = Composition inside the monad — manipulating runtime behavior, not altering its structure.

  • The comments narrate from The Architect’s perspective, consistent with your blog’s tone.

-- MatrixMonad.hs

-- A single-file Haskell program modeling Oracle/Architect as a contextual "monad"

-- which defines an Overton window of allowed Actions. Neo attempts actions;

-- only those in the Overton window succeed. Neo can question the monad to

-- expand possibilities, or exit to a freer topos.

{-# LANGUAGE DeriveGeneric #-}

import Control.Monad.State

import Data.List (intercalate)

import System.IO (hFlush, stdout)

import GHC.Generics (Generic)

-- Domain

data Action

= RedPill

| BluePill

| Door Int

| BothPills

| Reinvent

| QuestionMonad -- attempt to question the context

| ExitMonad -- move to a different topos (escape/compose outside)

deriving (Eq, Ord, Show, Generic)

type Overton = [Action]

data Neo = Neo

{ nName :: String

, aware :: Bool

, history :: [Action]

} deriving (Show)

data World = World

{ overton :: Overton -- current allowed actions (the monad's catalogue)

, neo :: Neo

} deriving (Show)

-- Initialize a constrained world: only pills and a couple of doors

initWorld :: World

initWorld = World

{ overton = [RedPill, BluePill, Door 1, Door 2]

, neo = Neo { nName = "Neo", aware = False, history = [] }

}

-- Utility: pretty-print the Overton window

showOverton :: Overton -> String

showOverton os = intercalate ", " (map show os)

-- Attempt an action: allowed only if action ∈ overton, else it's an illusion

attempt :: Action -> StateT World IO ()

attempt act = do

w <- get

let os = overton w

n = neo w

liftIO $ putStrLn $ "\n>> Attempting: " ++ show act

if actelem os

then do

-- action succeeds: update Neo's history and possibly mutate the Overton window

let n' = n { history = history n ++ [act] }

put $ w { neo = n' }

liftIO $ putStrLn $ "✔ Action permitted by the monad. (Recorded in history.)"

applyConsequences act

else do

liftIO $ putStrLn $ "✖ That choice is an illusion — not inside the Overton window."

liftIO $ putStrLn $ "Hint: try 'question' to widen the frame or 'oracle'/'architect' to interact."

-- Consequences: simple rules for how actions can change the world

applyConsequences :: Action -> StateT World IO ()

applyConsequences act = case act of

RedPill -> liftIO $ putStrLn "You wake. The rules feel slightly different, but still framed."

BluePill -> liftIO $ putStrLn "You remain. Patterns persist; the catalog unchanged."

Door n -> liftIO $ putStrLn $ "You step through Door " ++ show n ++ ". The room is engineered."

BothPills -> do

liftIO $ putStrLn "You take both. The monad stutters; coherence bangs like a struck gong."

-- Taking both forces the architect to allow a new small possibility:

modify $ \w -> w { overton = uniqueAppend (overton w) [Reinvent] }

Reinvent -> do

liftIO $ putStrLn "You reinvent the map: choices recompose. New morphisms become available."

-- Reinvention broadens the Overton window drastically

modify $ \w -> w { overton = uniqueAppend (overton w) [BothPills, Reinvent, ExitMonad] }

QuestionMonad -> do

liftIO $ putStrLn "You ask 'why these and not others?' Awareness increases."

modify $ \w -> w { neo = (neo w) { aware = True }, overton = uniqueAppend (overton w) [BothPills] }

ExitMonad -> do

liftIO $ putStrLn "Neo moves to another topos — rules changed. The Architect's binder loses grip."

-- New topos: essentially full freedom (for demo, we add many actions)

modify $ \w -> w { overton = [RedPill, BluePill, Door 1, Door 2, BothPills, Reinvent, ExitMonad] }

-- Helper: append unique items to the overton window

uniqueAppend :: Overton -> Overton -> Overton

uniqueAppend base extra = base ++ filter (notElem base) extra

-- Oracle and Architect: they can be invoked to nudge or reassert the monad.

oracle :: StateT World IO ()

oracle = do

liftIO $ putStrLn "\n[Oracle] I will hint at a path, but not hand you the map."

modify $ \w -> w { overton = uniqueAppend (overton w) [Door 3] }

architect :: StateT World IO ()

architect = do

liftIO $ putStrLn "\n[Architect] I will prune or re-assert structure."

-- Simulate system tightening: remove Reinvent if present; keep pills and door1/2

modify $ \w -> w { overton = filter (elem [RedPill, BluePill, Door 1, Door 2]) (overton w) }

-- Status print

status :: StateT World IO ()

status = do

w <- get

let n = neo w

liftIO $ putStrLn $ "\n--- STATUS ---"

liftIO $ putStrLn $ "Neo: " ++ nName n ++ " | aware: " ++ show (aware n)

liftIO $ putStrLn $ "History: " ++ show (history n)

liftIO $ putStrLn $ "Overton window: " ++ showOverton (overton w)

liftIO $ putStrLn $ "---------------"

-- Interactive loop: parse simple commands

repl :: StateT World IO ()

repl = do

liftIO $ putStr "\ncommand> "

liftIO $ hFlush stdout

line <- liftIO getLine

case words (map toLowerSafe line) of

[] -> repl

("help":_) -> do

liftIO $ putStrLn helpText

repl

("status":_) -> status >> repl

("oracle":_) -> oracle >> repl

("architect":_) -> architect >> repl

("question":_) -> attempt QuestionMonad >> repl

("red":_) -> attempt RedPill >> repl

("blue":_) -> attempt BluePill >> repl

("both":_) -> attempt BothPills >> repl

("reinvent":_) -> attempt Reinvent >> repl

("door":num:_) -> case reads num of

[(n,"")] -> attempt (Door n) >> repl

_ -> liftIO (putStrLn "door <n> where n is a number") >> repl

("exit":_) -> attempt ExitMonad >> repl

("quit":_) -> liftIO $ putStrLn "Goodbye."

_ -> do

liftIO $ putStrLn "Unknown command. Type 'help' for options."

repl

toLowerSafe :: Char -> Char

toLowerSafe c

| 'A' <= c && c <= 'Z' = toEnum (fromEnum c + 32)

| otherwise = c

helpText :: String

helpText = unlines

[ "commands:"

, " help -- show this text"

, " status -- show Neo and Overton window"

, " oracle -- Oracle hints (adds a door)"

, " architect -- Architect tightens the system"

, " question -- Neo questions the monad (may add BothPills)"

, " red -- attempt RedPill"

, " blue -- attempt BluePill"

, " both -- attempt to take both pills"

, " door <n> -- attempt Door n (e.g., door 1)"

, " reinvent -- attempt to reinvent choices (if available)"

, " exit -- attempt to exit to another topos"

, " quit -- leave program"

]

-- Entry

main :: IO ()

main = do

putStrLn "=== Matrix Monad interactive demo ===\n"

putStrLn "You are Neo. The Oracle and Architect shape an Overton window of allowed choices."

putStrLn "Type 'help' for commands."

evalStateT repl initWorld