The Pāńini code

Parikshit Sanyal
Significant others
Published in
7 min readNov 19, 2022

…​man, it appears, has inherited essentially three brains. Frugal Nature in developing her paragon threw nothing away. The oldest of his brains is basically reptilian; the second has been inherited from lower mammals; and the third and newest brain is a late mammalian development which reaches a pinnacle in man and gives him his unique power of symbolic language.” [1]

— Paul McLean; Neurophysiologist

A quick search for ‘ancient literature’ will bring up a plethora of texts, ranging from Bronze age tablets, early Mesopotemian epics and Egyptian hieroglyphs. Most in the list — stones, clay tablets, papyrus — are now safely vaulted in museums. Few of them have survived in parts, like the Epic of Gilgamesh, and are available as annotated editions. Only one, unabridged, is still in print: the Ṛg Veda. And it predates writing. For most of its life, the Ṛg Veda has been handed down as word of mouth, and has thus survived — remarkably — till this day. [2] Which is no small testament to the power of the spoken word. It is no surpise then, that we are known for our loquaciousness (and I do not mean to self criticise). The spoken word holds the highest esteem, to the point of being sacred, in Indian culture. The tradition of the dialogue is millennia old: famous discourses in Indian philosophy are often written in dialogue (i.e. that between Dharma and Yudhiśhťhira, between Yama & Naciketā, and between Gārgí & Yājnāvalkya, and the most famous of all, Śrímadbhagvat Gítā, our holy text). The spoken word pervades Indian culture till this day: we ‘move words’ (‘बात चलाना’) to settle everything — from closing deals to arranging marriages , until the words are ‘cemented’ (‘बात पक्की’); our achievements ‘complete our sentences’ (‘बात बन गया’), unless of course, we fail and our ‘sentences’ remain incomplete (‘बात बिगड़ गयी’). (In fact, the emphasis on speaking is so pervasive, that it is often used as an euphemism; gynaecologists often ask ‘पति से बातचीत करते हैं ?’ (‘are you in speaking with your husband?’), and patients never get the wrong meaning.)

But there is more to the spoken word than just recollection and reciting; there is every evidence that words were used as code in the Veda. Consider the Vedic karma, or yajna (i.e. variably translated as ‘rituals’/ ‘sacrifices’ by experts), which are still a routine, mundane part of everyday life in India: no birth/ marriage/ death is complete without them. Indeed, they seem to be flourishing, both in India and its diaspora. [3]. The course of Vedic rites are to be performed with astute adherence to protocols; a single misplaced object (such as firewood) or even a mispronounced word would annul the entire endeavour. Prof Dasgupta mentions an anecdote from the Ṛg Veda:

Thus when Tvașțṛ performed a sacrifice for the production of a demon who would be able to kill his enemy Indra, owing to the mistaken accent of a single word the object was reversed and the demon produced was killed by Indra. [4]

— Surendranth Dasgupta; History of Indian Philosophy

Programmers, sound familiar? Anyone who’s ever missed a bracket, or a semicolon, or put a nested loop at the wrong level, will immediately feel for Tvașțṛ. How could a syntax error create so much havoc? What was the language, the platform, and the operating system?

The making of a formal language

At least a thousand years before Chirst, the first known formal language was conceived. The idea was a to construct a medium specifically suited for the task of analysis and reasoning — built to cut, sew and join thoughts together. The language thus produced, terse, strongly typed (borrowing a term from programming) and built like a swiss army knife, was called Sanskrit (‘refined’).

The original Sanskrit grammar, Aśhtādhyāyí (‘eight chapters’) by Pāńini (~ 300 BC?), was written like a manual; it starts with fourteen basic sequences of letters (‘axioms’) and builds up words (‘theorems’) from these sequences. The method is startingly similar to Euclidean geometry, where proofs and meta-proofs are built upon the foundation of axioms. Verily, Pāńini’s grammar has been hailed as “one of the greatest monuments of human intelligence” [5]

If one looks closely enough, Pāńini’s method for constructing compound words out of simpler ones [6] is one of the earliest known algorithms. The terseness of Sanskrit is largely due to such compound words, giving it a code-like favour. Every word is derived from a combination of two or more letters from the original fourteen sequences, i.e. each word is a ‘theorem’ in the Euclidean sense. Moreover, the rules of how to combine these letters are also derived from this set of sequences, i.e. the ‘code’ (rules) and ‘data’ (words) are encoded in the same table. (Any similarities to functional languages — like Lisp — is purely coincidental; to those languages, we say, you are two thousand years late to the party).

(And, and … the interchangeability of code and data is the foundation of the Turing machine, where both the ‘rules’ of operation and the operands themselves lie on a single, infinitely long tape, indistinguishable from each other. Panini’s algorithm is a limited-scope Turing machine.)

The fourteen basic sequences of Pāńini: listing of all the letters of sanskrit in a specific sequence

अ इ उ ण् |
ऋ ऌ क् |
ए ओ ङ् |
ऐ औ च् |
ह य व र ट् |
ल ण् |
ञ म ङ ण न म् |
झ भ ञ् |
घ ढ ध ष् |
ज ब ग ड द श् |
ख फ छ ठ थ च ट त व् |
क प य् |
श ष स र् |
ह ल् |

To cite a trivial example of how things work: Pāńini describes a generic method for conjugation (sandhi) of two words; if the first word ends with a particular letter, it will somehow merge with the first letter of the second word, and be transformed.

English speakers might find the idea of conjugation of letters, radical; it is however, one of the earliest tenets of most Indic languages, usually taught between ages 8–12

One of the rules is:

इको यण् अचि

What this means is:

  1. enlist all the letters between इ (‘i’) and क (‘k’) , excluding the consonants
  2. again, do the same for य (‘ya’) to ण (a harsh ’n’) , but this time include consonants
  3. tabulate them side by side
इ य्
उ व्
ऋ र्
ऌ ल्

The rules of sandhi is now a matter of table lookup: whenever उ (‘u’) takes part in a sandhi, it gets transformed to व् (‘w’)

सु + आगत = स्वागत (i.e. well + come = welcome)

In this case, the first word सु is स + उ; the terminal उ is converted to व् in the compound word. Again:

पितृ + आज्ञा = पित्राज्ञा
(the compound letters may show up garbled - in spite of unicode;
please don't mind)

The last letter in पितृ (प इ त ऋ) is ऋ, which gets transformed to an र्, as in

पित्राज्ञा = प इ त र् + आज्ञा

There are similar rules for combining entire words (samās), and clauses. Several layers of subordinate clauses can condense a great amount of information in small sentences, as in another rule:

यथास्थानम् अनुदेश: समानां
(yathāsankhyam anudeśah samānām)

Even in an natural language like English, which has undergrown evolutionary growth and borrowed from many other languages, the translation would take much more space than the original Sanskrit: ‘Items (in two categories) having the same (number) are connected (with each other) in their respective number (i.e. order).’ [7] Which roughly means that parts of speech must have one-to-one correspondence with each other, i.e. between nouns and adjectives, adverbs and verbs etc. This tendency towards short, cryptic statements is still persisting in Sanskrit. In fact, modern programming languages seem to have rediscovered the joy of brevity: to print the last 10 lines of a file in a Unix environment, use the command [8]

sed -e :a -e ‘$q;N;11,$D;ba’

Pāńini’s grammar inspired several commentaries, notably Patanjali’s Mahābhāśhya and much later, Bhartṛhari’s Vākyapadīya (~ 500 AD). By the time of Bhartṛhari’s writing, the idea that grammar was not just a linguist’s toolbox, but a path to salvation, was firmly established. [9] This has a lot to do with the Indian version of genesis. In western imagination, God is often imagined as the first mathematician (‘God created the integers’ etc) [10] or geometer [11]. Whereas, in Indian tradition, God (Brahmā, to be precise) is the first grammarian. The word is considered a form of energy (mantra), which is the founding block of not just the sentence, but the universe. The base principle of the universe is not the atom, but a series of questions and answers. This information centric view of the universe was propounded again in mid twentieth century, in the works of the physicist John A Wheeler [12] and Claude Shannon.

I hear a lot about Sanskrit being the ‘first programming language’ and whether it can be put to use in actual coding. One does not consider boolean algebra, Turing machine or lambda calculus as programming languages, although they are the foundations of every programming language in use today. In a similar vein, the architecture of Sanskrit demonstrates general principles, that have been re- and re-discovered several times over. A study of Sanskrit would certainly benefit a much broader audience than just the coder.

  1. Cesario J, Johnson DJ, Eisthen HL. Your Brain Is Not an Onion With a Tiny Reptile Inside. Curr Dir Psychol Sci 2020;29(3):255–60.
  2. Basham AL. The Wonder that was India. 3e. New York; Tapliner (1967): p31
  3. Baumann M. Templeisation: Continuity and Change of Hindu Traditions in Diaspora. Journal of Religion in Europe 2009;2(2):149–79.
  4. S Dasgupta, History of Indian Philosophy. Vol 1 (2018). Kolkata; Rupa:21
  5. Leonard Bloomfield, Language. 1933, New York:11
  6. P Kiparsky. Pāńinian linguistics. https://web.stanford.edu/class/linguist289/encyclopaedia001.pdf
  7. Panini’s Grammar and Computer Science. Saroja Bhate and Subhash Kak. Annals of the Bhandarkar Oriental Research Institute, vol. 72,1993, pp. 79–94
  8. Sed One-Liners Explained, Part II: Selective Printing of Certain Lines. https://catonmat.net/sed-one-liners-explained-part-two (internet) cited 04 Sep 2021
  9. Lawrence Ward Davis. Studies in Bhartṛhari’s Vākyapadiya. PhD Thesis (1978), U Massachusetts
  10. Weber, Heinrich L. 1891–1892. Kronecker. Jahresbericht der Deutschen Mathematiker-Vereinigung.2:5–23
  11. ‘God the geometer’, Austrian National Library, Public domain, via Wikimedia Commons, https://commons.wikimedia.org/wiki/File:God_the_Geometer.jpg
  12. Glattfelder J.B. (2019) A Universe Built of Information. In: Information — Consciousness — Reality. The Frontiers Collection. Springer, Cham. https://doi.org/10.1007/978-3-030-03633-1_13

--

--