Sanskrit: the first programming language ?

What Sanskrit grammar can teach us about coding


At present, I work as a software engineer at Agolo, a machine learning summarization company. But years ago, I taught Sanskrit grammar in India at a university. I lived in India over the span of about 9 years, and had the awesome opportunity of studying Sanskrit grammar intensely with one of India’s top scholars.

image of Panini, Sanskrit grammarian

Looking back at my studies of Sanskrit grammar, I see some striking similarities between the world of coding and the world of Sanskrit grammar. Francis Sullivan said about algorithms, “Algorithms are the poetry of computation. Just like verse, they can be terse, allusive, dense, and even mysterious. But once unlocked, they cast a brilliant new light on some aspect of computing.” I would argue that Sanskrit grammatical rules fit this description to a tee.

The Sanskrit grammar I speak of is very far removed from the systems of grammar we are used to. I am not talking about the boring type of grammar that involves tagging of parts of speech, categorizing syntax and declensions, etc. It is true that Sanskrit is taught in this way in most universities, but this is extremely different from how I learned Sanskrit grammar from my teacher.

The difference is that traditional Sanskrit grammar is comprised of sutras — terse formulas that act in conjunction with one another to comprise complex rule systems. And while all sutras share this characteristic of being able to convey deep meaning in short words, I would argue that Panini’s grammar sutras (called “Ashtadhyayi”) are even more compact and mathematically dense than any other set of sutras.

Here’s a fact to prove what I mean. The book Comprehensive English Grammar is roughly 2000 pages & 5 lbs, while Panini’s grammar sutras are about 1% of that — about 20 pages. Although it is said that brevity is the soul of wit, Panini had other reasons to keep it short. The lack of printing and distribution of books meant that the texts had to be passed down orally and recited. Try reciting a 2000 page text by heart! So, something had to be done to condense those rules into a format that could be memorized and recited. Thus, sutras were developed.

Even the way Panini structures the Sanskrit alphabet reads like a system of code. Observe:

Maheshvara Sutras — formulaic representation of Sanskrit alphabet

All of Panini’s grammar is based on these 14 sounds. The legend is that Panini overheard the 14 sounds from Shiva’s drum, and they awakened within him perfect knowledge of language. Besides the myth, these 14 sounds are a very powerful program.

Notice that if we ignore the consonant at the end of each sound, the first 4 sutras comprise all the vowels, a, i, u, ṛ, ḷ, e, o, ai, and au. Similarly, the last 7 sutras comprise all consonants. By connecting one of the letters with a terminating-consonant, for example, al, we represent all the letters within that formation — all the letters of the alphabet. ac represents all vowels, and hal represents all consonants. Furthermore, the symbol aṇ represents the letters a, i, and u.

This allows Panini to make rules such as iko yaṇ aci. This means that the letters of ik (i, u, ṛ, ḷ) are replaced by yaṇ (ya, va, ra, la), when a vowel (ac) precedes them. For example, in the phrase ‘tasmai gurave namaḥ,’ there is a mixing of the phrases ‘guro’ and ‘e,’ after which it becomes ‘gurav’ and ‘e’, and thus ‘gurave’ (this means salutations to the teacher).

This is really just scratching the surface. There is also the issue of scope in Panini’s sutras, which is also seen in coding. However, I can gladly share more about this topic if there is interest. My main point is that Sanskrit grammar has such a fresh and unique way of viewing a language that is worthy of appreciation.

Tom Goldenberg is a web and mobile developer, and the co-creator of the iOS Bhagavad Gita app. Follow on Twitter. Feedback is always appreciated!