Thursday, November 19, 2009

Sutra based Programming

Haven't software engineers had enough of programming languages? C and C++ were there for a while unchallenged. Then Java took over C/C++ pretty quickly. Even though you could create complex applications using Java and run on any platform, there is no dearth of new languages. Scala, Groovy, Ruby and very recently Go from Google.

All high-level programming languages do 4 basic things:

  1. Assignment
  2. Condition
  3. Loop
  4. Function/Procedure.
Loop and Function/Procedure/Subroutine are essentially glorified Goto.

Everything else is syntactic sugar.

Galileo has influenced us heavily to think math in terms of symbols. A mathematical symbol expresses an idea very concisely and effectively. Much more than a language could do.


x = 1
is more concise than saying "let x equal to 1".

Let me rephrase the above bereft of assumptions --

The mathematical expression "x = 1" is more concise than saying it in English "let x equal to 1".

But what if we could express "x = 1" much more concise in a natural language?

For e.g., let me just say

x 1
I just dropped the symbol "=". One symbol less! Whohoo! I also established a convention that whatever on left side is receiver and on the right side is provider. In a language like Sanskrit, for e.g., the "is/happens" is implicit (asti/bhavati). So no other verb is required. (There are other languages that exhibit this property too).

People think that just because ancient Sanskrit mathematicians did not use symbols, the works are not scientific. Symbol is just a convenience; if language could be more powerful than symbols, who needs them? As a side note, Sanskrita almost does not use punctuations (except for end of sentence - the pipeline character |). In contrast, English just can't be "communicated" without appropriate punctuations' usage.

To demonstrate the power of the language, let us look at a simple program using a pseudo-language.

//Pseudo-code to produce a random number and determine if its odd or even
s = 100;
r = (int) rand(s);
if (r % 2 == 0)
return "even";
return "odd";
The question is, can this program be expressed using natural language? Of course, we can write the whole program in plain English, but it wont be concise. (COBOL anyone, hello?)

Now lets apply the Sanskrit grammarian Paninian rules. I am going to keep the function names as is (in bold), but conjugate the variable names per Sanskrit rules (taking them to be consonant ending variables).

1: aSeSha: SUnyam  // No remainder 0 (No remainder equals 0)
2: s Satam         // s Satam asti (s is 100)
3: sa: rand r      // of s random is r (r is random of s)
4: int ca          // int also (r is also int of random of s)
5: even ra: aSeshe dvibhAjane  // "even" is of r during division of 2 when no reminder happens (on division by 2 of r has no reminder, it is even)
6: odd SeShe       // "odd" when there is a reminder

Here are the sutras in proper Sanskrit:

अशेष: शून्यम् |
स् शतम् |
स: वृथा र् |
अभिन्न: च |
समं र: अशेषे द्विभाजने |
विषमं शेषे ||

As you can see there are absolutely no mathematical symbols! A program is written purely by the expressive power of language. So what happened? How are they equal?

Using nominative case and the implicit "is", Panini eliminated the need for equals sign. Functions are defined via genetive case. Using the locative case, Panini provides the if-else condition. In effect, mathematical expressions are substituted by simply conjugating the variables.

That is the genius contribution of Panini to Sanskrita! Now a skilled poet could come and rearrange the above 6 sutras into a sloka format, and lo! there is sloka that tells us how to determine an even/odd number!

Let me attempt a half-baked sloka (May Sanskrita enthusiasts forgive me for such a blasphemy).

अशेषो शून्यं भूयात् सेकशतं वृथा रेफ: स: ।
अभिन्नश्च द्विभाजने विषमं समं शेषोऽशेषे ॥

Due to the occurrence of words like SeSha, SUnyam, aSeSha, dvibhAjane, abhinna the above can be mis-interpreted to refer Adisesha, SUnyavAda, Vishnu, Dvaita, Advaita etc. Now we have an example of a sloka referring to the gods and a mathematical algorithm encoded in it!


Sathya Srinivasan said...

Heheh. In doing so, you also look like a superstar and sound like an elitist!

"Writing computer programs in English is too descriptive and elaborate" - ever remember COBOL?! That was as close to writing computer programs in English if any and a "Hello World" would run into 3 pages!

What would be interesting to see is whether this sutra can be reconverted back into a 'runnable' program. I have heard that Sanskrit follows Backer-Naur form (or notation) and hence is very amenable to being parsed by a compiler. It will be cool to parse this back!

sfauthor said...

Nice posting. Do you know about these Sanskrit books?

vishvAs vAsuki said...

भवतः लेखनाः मे रोचन्ते, वासुदेव-वर्य।

परन्तु, 'People think that just because ancient Sanskrit mathematicians did not use symbols, the works are not scientific.' इति मर्शः विपर्ययः इति दृश्यते। कठिन-लिपिसु अपि अद्भुतान् गणितसत्यानां लेखनं शक्यते इति सामान्य-ज्ञानं प्रायः।

परन्तु, यदा प्रश्न-उचितं समीचीनाः लिपयः (notations) च पदाः (notions) च सूत्राः (theories) सन्ति, तदा उत्तरान्वेशणं अति सुलभं एव भविष्यति। उदाहरणार्थं, decimal notation विना, कथं 'transcendental' सङ्ख्यान् अवगन्तुं शक्नोति इति चिन्तयतु। तथा imaginary सङ्ख्यानां विना, कथं fourier analysis कर्तुं शनोत्ति?

Anonymous said...

if (r / 2 == 0)
return "even";
return "odd";

is it r%2 (r mod 2) or r / 2 ?

Vasu Srinivasan said...


r%2 is the right one. I have fixed it. Thanks for pointing out.