Spatial languages: Writing code in 2D
This article challenges the fundamental assumption that code must be written in one dimension, proposing a shift to 'spatial languages' that leverage 2D space. It uses a novel andFlip operator and quantum computing analogies to demonstrate how this could simplify complex logic, especially for managing temporary variables. The piece serves as a deep technical exploration into how language design impacts problem-solving, making it appealing to those interested in programming paradigms and cognitive biases.
The Lowdown
The article "Spatial languages: Writing code in 2D" provocatively explores the idea of transcending traditional one-dimensional, left-to-right programming by introducing "spatial languages" that utilize an extra dimension for expression. The author argues that our current coding conventions are largely a product of tools like terminals and keyboards, and that embracing 2D could unlock new ways of thinking about and solving problems, particularly those involving multi-arity functions and state management.
- Challenging 1D Expressions: The core premise questions why expressions must be linear, suggesting that vertical arrangement could simplify complex operations.
- The
andFlipOperator (@@): A custom 3-arity operator,andFlip, is introduced to illustrate the limitations of standard infix notation for operations that genuinely involve three arguments. Its spatial representation allows for clearer expression. - Practical Example: Chicken Coop Door: A real-world scenario of an automatic chicken coop door highlights how the
@@operator can elegantly handle state toggling based on multiple conditions, maintaining state even when inputs fluctuate. - Vertical Chaining & Uncomputation: The article delves into "vertical chaining" through puzzles that demonstrate how temporary variables (
t1) can be reset or 'uncomputed' within a single 2D expression. This concept draws direct parallels to crucial techniques in quantum computing, such as borrowed ancilla qubits. - Defining Spatial Operators (
maj,uma): New spatial operators likemaj(majority) anduma(un-majority and add) are defined using geometric shapes, reflecting their use in quantum ripple-carry adders. - Building a 3-Bit Adder: A complex 3-bit adder is constructed using these spatial operators, showcasing how a multi-stage process can be represented in a single, self-cleaning 2D expression that manages intermediate states efficiently.
- A Tour of Other Spatial Languages: The author provides a quick overview of existing 2D and spatial languages, including Befunge, Orca, Racket #2d, Hexagony, and Ladder Logic, acknowledging that the concept has historical roots.
- "This Is Water" Metaphor: The discussion concludes by relating spatial languages to the Sapir-Whorf hypothesis and David Foster Wallace's "This Is Water" speech, suggesting that challenging our ingrained linguistic and technological biases can lead to profound new insights and approaches in programming.
Ultimately, the article makes a compelling case for re-evaluating the fundamental structure of programming languages, proposing that a shift to spatial arrangements could lead to more intuitive and powerful ways of expressing computation, particularly for advanced concepts like quantum circuits.