📜 ⬆️ ⬇️

Quipu is an esoteric programming language based on the Incas nodular script

A friend of mine, a historian by profession, gave me a wonderful idea about using the ancient mnemonic and counting systems in modern cryptography. In the process of his stories about the nodular writing of the Incas , I began to realize that everything new is well forgotten old and it would not be bad to somehow apply ancient experience in the modern world. The first thing that came to mind was cryptography. The most obvious thing is to simply convert the nodes from the threads into bytes and the cipher is ready. On the one hand, everything seemed clear, but then I remembered crypto-resistance and other cipher parameters and I realized that I did not have enough experience and knowledge in the field of cryptography in order to develop a new cipher alone.

Then I decided to try to present a certain esoteric programming language, the constructions of which can be written using Kipu's nodular writing. At first it seemed that it was impossible: I was inventing a language and trying to write a program for calculating factorial on it. The first three draft specifications went into the ballot box: languages ​​were no good. They looked as it should have been for esoteric languages, but they did not help me solve the problem, because were not complete by turing. Enthusiasm slowly faded away and this task seemed to me not over the shoulder. Gathering my strength, I decided that if I could write a program for calculating factorial, then the language works.

The fourth version of the language was successful: I wrote factorial, then the generation of the Fibonacci sequence and a dozen simple examples for a “sum of numbers from 0 to 99”. The language turned out what is needed: an unusual and at the same time with a simple clear idea. The main thing is that the language can solve any (or almost any) task that can be expressed as a computable function.
')

Inca and Kipu


(Paragraph from Wikipedia)

Kypu is an ancient Inca mnemonic and counting system, a kind of writing: it is a complex string plexus and knots made of South American camel (Alpaca and Llama) wool or cotton. It can be from a few hanging threads to 2000. It was used to send messages by messengers for hours on specially laid imperial roads, as well as in various aspects of public life (as a calendar, topographic system, for fixing taxes and laws, etc.) .

The decimal number system was used in the pile and mainly numbers were written on them using three types of nodes. The first portrayed dozens, the second - a group of units (2-9) and the third node denoted one. Thus, the group of nodes was interpreted as a number in which the corresponding nodes were imposed on the place of thousands, hundreds, tens and units.

Suppose that the entry “7d” denotes a sequence of 7 nodes denoting tens. “5i” - a group of single nodes of 5 pieces and “I” - a unit, and “x” - as a pass. Then the number 703 can be represented by the following sequence of nodes: “7dx3i”, the number 2013 - 2dx1d3i, etc.

These ideas formed the basis of the Quipu language for some changes.

Language Specifications


A Quipu program is a matrix of knots, each column of which forms a thread. The number of threads is limited to 26, by the number of characters in the Latin alphabet (in fact, there are 52, but more on that later). The number of knots on the thread is not limited. Each thread should be separated from its neighbors by at least one space and be equalized by a fixed number of indents from the beginning of the file along its entire length. In other words, a thread in the form of a “ladder” and not a column can hardly be called valid.

Quipu can handle two types of data - integers and strings.

The program is executed by threads (first the first thread, then the second, and so on) from top to bottom. At each step of execution, the value of the current node is calculated. Execution continues until the value of the last knot of the last thread is calculated or the stop is found. “::”.

Nodules can be divided into two types - simple and composite. Simple ones consist of a single matrix cell (a pair of characters per line, for example, “$ a”). Composite knots are a sequence of simple knots in a thread. Composite nodules are used to represent numbers and strings. For example, the string “Habr” in Quipu can be written like this:

'H 'a 'b 'r 


And the number of 2013 is as follows:
 2# 1@ 3& 


Each thread can be marked with a symbolic label. If there are no tags, all the threads are numbered in order by the labels “a” - “z”. In addition, each thread has its own value, which is equal to the value of the last node in it. You can refer to the value of the thread using a special knot - “$ a” (value of the thread “a”). The value of each thread is written in its zero nodule, which allows its use as an implicit argument in expressions.

The thread has two components - the main and initialization. For example, “a:” is the initializing component of the thread “a”, “a.” Is the main one. The thread can be represented as two components, and each separately. In the sequential execution of the program, only the main threads are used. Initializing threads are executed once when the first explicit or implicit reference is made to the value of the thread. Explicit refers to the execution of the “$ a” knot, implicitly means the use of the top zero thread nodule in expressions. If the thread has no initializing component, then its initial value is zero. There is a restriction on the types of nodules used in initialization threads: it is forbidden to use nodules that change the flow of program execution, i.e. transitions and knot "stop."

Some nodes do not have their own value, so their value is calculated as the value of the nearest significant node in front of them (if there are none, then the value of the thread, which is the zero node). Examples of such nodules are nodules for output and conditional transitions.

Nodule table (n is the current thread node)

NoduleValue
$ aThe value of the thread "a".
>>Enter the value from the terminal.
<<Output the value (n-1) of the node to the terminal.
1 # 4% 5 @ 1 &The number 1451: "#" - thousands, "%" - hundreds, "@" tens, "&" - units.
-The difference between (n - 2) and (n - 1) knots.
++The sum of (n - 2) and (n - 1) nodules.
**The product of a knot (n - 2) and a knot (n - 1).
//Integer division of a nodule (n - 2) by a nodule (n - 1).
%%The remainder of dividing the complete nodule (n - 2) by the nodule (n - 1).
<cConditional transition to the thread "c", if the value of the previous node is less than zero.
> cConditional transition to the thread "c", if the value of the previous node is greater than zero.
= withConditional transition to the thread "c", if the value of the previous node is zero.
? withUnconditional transition to the thread "with".
::Stop.
'Symbol.
\ nSpecial character.


Examples of programs


Hello World!

 a: a. 'H << 'e 'l 'l 'o ' 'W 'o 'r 'l 'd '! \n 


The sum of numbers from 0 to 99

 sice $i 1& $i $s ++ ++ 1% << -- =e ?s 


Factorial

 a: a. b: be >> $a 1& $a $b =e 1& << 1& ++ -- $b =e ** ?a 


Fibonacci

 sfd a: b: ab x: itoe $x $x ', 1& 1& $b $f >> 1& 1& 1& '. =e 2& ' ?i ++ << << << 1& -- << ?f ', ?e -- $i $f ' =o -- << ?o 1& =e -- $a =t $b 1& ++ << ', ' << 1& << 


Implementation


For quite a long time, I was haunted by the idea that it would be time to learn a new language, so to expand my horizons. The choice fell on Scala. I was always attracted to her so-called “excessive complexity” that everyone is talking about. Having written a couple of simple examples of ala “Hello World”, I imagined myself to be a hard-boiled rocky, took up the implementation of the Quipu interpreter. The task turned out to be more complicated than I thought, despite the fact that at the very beginning I gave myself an installation - to do something that would just work, without being obsessed with the beauty of design and implementation.

As a result, I received a working prototype in a day and a half. Got, really what I wanted. A sort of proof-of-concept ideas. I have written quite a bit of code and it looks a bit intimidating, especially for people with Scala development experience. However, I believe that a start has been made. The path “idea -> implementation” is traversed by minimal effort and the following steps can be aimed at improving the current implementation. I do not even rule out the possibility that everything will have to be rewritten from scratch, but I still hope that there is a reasonable amount in the original code and that large-scale refactoring can save it.

In the current implementation there are also a couple of minor problems that I decided not to fix yet, in light of the “all rewriting from scratch” perspective. Nevertheless, the problems of these big inconveniences do not deliver; on the contrary, Quipu programmers are taught to program their programs correctly. The following is a list of current issues:

1) Each thread must have a label. Threads without tags are not supported.
2) The program must end with an empty line, otherwise - the nodes on the last line will not be synthesized.

So. Interpreter source codes are available on GitHub: github.com/vkostyukov/quipu . I will be very glad to pull-requests with corrections of my codebred. In addition, I would be grateful for new examples of language use, which I will definitely post on the project page. I also promise a stylized T-shirt with the words “% username%, the first Quipu hacker.” To anyone who writes 99-bottles-of-beer to Quipu. Joke :)

What for?


There is a belief that by inventing a new programming language, the author practically condemns himself to failure. With this, I fully agree, and I believe that for the next 3-5 years the humankind has a resource on programming languages. Now it is difficult to come up with something new, useful and different from the well-known coffee giants. But this is probably true for the languages ​​of "big" and "serious", which are trying to solve all imaginable and inconceivable problems that the developer allegedly faces.

Esoteric languages ​​solve other problems - the problems of warming up the brain, unhealthy curiosity and self - flagellation of self - education. For such cases, 1000+ languages ​​have already been invented and implemented, the grammar of which is placed on a standard yellow sticker. Quipu is one such language. Languages ​​that help us programmers take a fresh look at known problems and get new sensations and experience from solving them, thereby developing in us the very ability to “think outside the box”.

Source: https://habr.com/ru/post/165661/


All Articles