Time Travel and Programming


Nowadays, not only science fiction writers write about time travel. After the thoughts of ancient philosophers, formulas of the general theory of relativity, wormhole models, new theories and even projects continue to appear. True, many of them require black holes, infinitely long cylinders, negative-mass matter, and other artifacts for their work. Does all this bring us closer to the creation of a time machine? It is difficult to talk about this objectively, without understanding the essence of the question - what is time. Over several centuries, this understanding has increased, in fact, slightly. Perhaps with the advent of programming the situation will change? After all, it is there that many answers await us.
Looking ahead
Soon, we will see that the passage of time and the execution of a computer program are almost the same thing. Such an analogy allows us to illustrate the story with understandable examples, and gives many interesting discoveries.

Next article in the series: Time Travel and Programming 2: Paradoxes

0. Time and space


To begin with, we need to decide as a first approximation what time is. While without programming - be patient a bit. The term "time" is usually understood as the visible course of events, the process of continuous change in the surrounding world. At the same time, the most important and obvious feature of these changes is overlooked. What happened in the universe from the moment of the big bang to the present day? From the singularity point, in stages (at first elementary particles, then hydrogen atoms, ...) a complex multilevel system was formed, consisting of galaxies, stars, planets, at least one of which contains intelligent creatures reading the hub from the computer screen. These colossal changes would have been simply impossible without some control mechanisms (built into reality itself). The world is not just changing - it is gradually changing from simple to complex. Therefore,
Time is the fundamental mechanism of development that governs the course of events.

Compare the Universe with a children's toy - a kaleidoscope. Unique pictures flash there - it seems to be also a constant change. There is also a causal connection - the current position of the colored glass (forming the picture) depends on the previous one. What is the difference? In simple terms, the lack of development (learning). You can twist a kaleidoscope for at least a million years - nothing qualitatively new will appear in it. This does not happen in nature: the cosmos, the biosphere are evolving, science, society, any enterprise, collective, human being are developing — in general, everything, any system. If the passage of time were “just a movement”, then the world would remain in a state of chaos, there would not be any qualitative changes in it.

If time is why deliberate change occurs, then what is space ? The object of these changes. In other words, space is structure, time is dynamics . I understand that a certain “emptiness where everything is located” is more associated with space (and this “everyone” is called “matter”). But there is also a geometric approach to describing the nature of matter, where any particles and radiation are inhomogeneities of space. Like ripples on the surface of a lake.
The notion of time as a physical field participating in all processes as a structuring principle can be found in the theory of “causal mechanics”. Its author is the Soviet astronomer N.A. Kozyrev, and about his heroic work, amazing experiments - you need to write a separate article.

1. Starting point


We have time and space on the one hand, and data and code on the other. It is logical to correlate them as follows:
Space is data, time is code.
As time changes space, so code changes data. We have already managed to deal with the fundamental property of time: it does not just change, but purposefully structures the space, reducing its entropy (chaos). The code that introduces chaos into the data certainly occurs (all kinds of randomizers), but for the most part, it processes the data in order to organize it, or to obtain new data - in general, the output is something completely new. The simplest example: sorting an array. The code describes the dynamics of bringing the array structure to a more ordered form. In another example, the input is a cloud of gas and dust, over 4.6 billion years a certain code has transformed it into our solar system (it is clear that the code is somewhat more complicated than the “bubble” algorithm).
The inseparability of time and space is also easy to illustrate.
x; // что x? Нет действия
++;  // что инкрементировать? Нет данных
x++;  // пример минимальной программы 

Try to write the shortest (not empty) program: it will definitely have some action on some data (ie, the “atom” of the program is the “data + action” link).

So, in the real world, space cannot exist without time. As the hero of the story “Time Machine” said: “try to imagine a cube that does not exist for a single moment!” This is an important conclusion: space (data) exists only during the passage of time (code execution).

2. Basic concepts


Atom: a unit of code is a command (containing an action on data). The unit of time flow is an event. What does the team do? Changes data. What does the event do? Changes the space. Commands are added to the code, events are added to the chain.

Cause: commands are logically connected, and in addition to being executed sequentially, the result of one is often the input to the other. Also, the event has input and output data (the structure of the space before and after).

World line: in physics, this is the sequence of existence of a certain object. It corresponds to the code execution flow, i.e. code + data.

So, once again, we draw an analogy between the chain of commands and the thread of events. An event is an “action on data”, i.e. change of space under the control of time. The input data for it is the previous state of space (which, as we recall, is “data”), at the output we get again the changed space.

3. Fractality


In the form of a diagram of actions, you can depict the world line of any object. For example, the room where the person is sitting:
It is obvious that many events run through this stream in parallel: the person is sitting at the computer, the cat is sleeping on a chair, tea is cooling down on the table, etc. Each of these flows also has its own embedded flows, it can be traced down to the movement of molecules, etc. The principle of nesting dolls, or fractality, which applies both to structure and dynamics (and to both of them simultaneously). Therefore, the Universe is “initially multithreaded,” because contains an infinite number of parallel events. And all these events are naturally representable in the form of a hierarchical tree.


A complete analogy with programs, most of which are not reduced to a linear sequence of commands and, as a rule, consist of various structural units embedded in each other. A function consists of calls to other functions, and each of them describes its “scale of activity”. The decision algorithm operates on facts, the function calls nested in it calculate facts on the basis of data, invoking mathematical functions for this ... and so on.

Since we have touched on multithreading, you may have a question about thread synchronization, transaction isolation levels. Consider the example of "two writers." There are two threads, one variable, both threads try to write a number to it in parallel. It is necessary to put lock on the record, i.e. he will write down the first, only then the second (he will have to wait). Yes, the second will overwrite the results of the first. It is clear that, due to technical features, there is no other way ... but do you have the feeling that the real world works differently?

Instead of a variable, we now have some area of ​​space. Two streams recording numbers are two rays of light entering there. What will happen? Simply put, their energy is summed up. There is no streaming synchronization; the superposition principle works. Perhaps someday there will be computers in which, while simultaneously writing to the variable the numbers 1 and 2, the result will be 3.

4. Time machine


Time has the following properties:
1. Principle of causality - events follow in the strict order in which the cause precedes the effect.
2. The principle of alternativeness - any event has several outcome options.
3. The principle of cyclicality - events are repeated.

I think it’s understandable with the first one - without a causal connection, “today” would not depend on “yesterday” in any way, and a sequence of changes leading to a certain result would not be possible (imagine a code where the next command is randomly selected).

Thanks to the second, we live in a world of relative freedom of choice, and not the total predetermination of events.

The third is mentioned less frequently, although we observe the cyclical nature of time constantly, in absolutely all processes: heartbeats, breathing, day and night changes, seasons, ups and downs of production, economic fluctuations, the water cycle in nature, and much more. Cyclicity follows from the very idea of ​​time: if time is an algorithm, then it must have stages. And they will be constantly repeated. In a first approximation, we are talking about two stages (analysis-synthesis, recession-growth, etc.), which, again, we see in all processes (since this is a universal law).

Already guessed it? The above properties exactly correspond to the main point of structural programming: any executable algorithm can be represented by a combination of 3 control structures: sequence, branching, and loop. This statement is proved by the Böhm-Jacopini theorem (sometimes mistakenly attributed to Dijkstra). We will consider these three constructions as the basic set .

As you know, Dijkstra was an ardent opponent of the goto operator (unconditional transition), and advocated using only the basic set. However, this statement is implicit in the code.
First, function calls: the address of the dial peer is pushed onto the stack, and a transition to the subroutine (another piece of code) occurs.
Secondly, a cycle is a combination of a conditional operator and a conditional branch operator. Yes, we are talking about different levels: C code will not contain transitions, they will appear only at the level of machine instructions into which the program will be compiled. We can assume that the time itself is arranged in a similar way: in addition to the basic set, there is a transition instruction . Without transitions (albeit implicit), the program would be primitive: just a sequence of commands, without loops, without the ability to allocate code in functions and call them. Similarly, the transition operator is a necessary component of the time mechanism. If we continue the analogy, then without it, in natural processes there would be no cyclicity, and the possibility of a multilevel (modular) organization of process control. In general, nothing would work. Time travel is an important mechanism of reality.

Let's see what the movement looks like :
The diagram shows that the movement does not occur at the same point, but with a shift along the vertical axis (space), because these are lines not of code (time), but of space + time (data + code).

It should be noted that in addition to jumps in time, there is the concept of a change in the speed of the passage of time . For example, the world around it “froze”, and a person can make out the wings of a flying bee, overtake the sound of the phrase he said, etc. Here again, causal mechanics of Kozyrev come to the rescue. The influx of time into the system makes it more streamlined, i.e. the density of time (and the speed of processes) is higher where there is a more ordered structure. The speed of time is the degree of data ordering. Where is it easier to find a book - on a neat shelf or in a dumped pile? Who will find a solution faster - the one whose knowledge is put in order, or the one who has cereal in his head? Returning to the array example. Let there now be two arrays: chaotic and sorted. The task is: to find the given number in the array. Obviously, in the second array it will be faster (the search code, of course, is different). Relatively speaking, the second array is more “developed” than the first, and all processes occur in it faster.

5. Streams of time


Suppose spacetime is perfectly described by the concept of OOP. This, among other things, will mean that the data can exist not only in the form of parameters received and returned by functions, but also simply be stored in the fields of objects. We remember that space and time do not exist separately, therefore space (data) exists only in the process of time progress (code execution). And this is the paradigm of functional programming.
Let us depict a sequence of events (as data changing when passing through some code):
int x = 0;
x = x++;
x = x * x;

The time code is not classes, not variables, but a set of functions that describe data transformations (i.e., space structures). What was the initial data is hard to say. When the process was started, countless threads of Time code execution formed, and the space we see is just a slice of data transmitted from one function to another.

Alas, I am not at all an expert on FP, but still try to portray the above example without using variables:
int f1() { return 0; }
int f1(int x) { return x++; }
int f3(int x) { return x*x; }
Запуск цепочки: f3(f2(f(1))) – вот он, ход времени.

A description of the passage of time in the form of a program written in a functional programming language solves the fundamental question: where is the past “stored”? Is it right to understand that the present is “what is” and the past is “its saved record”? No and no again. Nothing is stored anywhere, there is simply a chain of functions through which data passes. Each moment of time accepts “input data”, changes them, and “passes on”. We describe this as a sequence of code execution, but this is not entirely true, because if the code is time, then it is executed in some kind of “time”. You read this article sequentially, but it already exists in its entirety. The present is the point of our perception, physically the current moment is no different from any other, in the past or the future.

Comparing event chains with function call chains, we did not take into account the possibility of splitting (Fork) and merging (Join) flows.

6. The tree of worlds


I think everyone is familiar with Everett’s theory of a “dividing universe.” At each event, reality splits into many copies, embodying all possible outcomes of the event. Throwing a coin, the Universe is immediately divided into two: in one reality an eagle falls out, in the other - a tails (relatively speaking, of course). Coin position - this will be the only difference between these realities. However, after some time, realities can diverge: for example, depending on the outcome, a person commits one or another act, which can lead to even greater discrepancies. In general, it looks like a tree with a rapidly growing crown, the root of which is the moment of the creation of the Universe. The fact that there must exist a process of merging realities that compensates for this growth is not all thought, and some theorists generally deny this possibility. Но мы к ней еще вернемся.

The theory of parallel realities has two excellent illustrations in programming: multithreading and the SVN version control system.

We have already said that we see a lot of events around us, occurring parallel to each other - they are separated by space (and maybe also time). For example, we drop two coins at once, something falls out on them independently of each other. Now we are talking about the third form of separation of events, when they occupy the same space and time, but are in different branches of reality. Separating a code execution thread (i.e. creating threads) in activity diagrams is known as a FORK operation.

And here is a very interesting moment! Streams in a program are created much less frequently than if statements are encountered. Perhaps something similar happens in reality? Those. parallel worlds are formed not for every event without exception, but only at certain moments (determined by the code, i.e. the flow of time). Otherwise it would be strange: where we see if, Fork is actually hidden.

The second example to the topic of parallel worlds is SVN. For those who are not up to date, a brief explanation: when developing a software product, the code is stored on the server (in the repository). Each developer on the disk has a local copy of it, and there are two main operations: Update - upload changes from the repository to oneself; Commit - upload their changes to the server. But for our topic, the most interesting is not even this, but in such things as diff and branch . When a change occurs in the repository (even if one character is changed in one of the million files) a new revision is automatically created, with the number one greater than the current one. The server stores all revisions, and allows you to deflate not only the latest (HEAD) revision, but also any friend. If SVN would make a copy of the entire repository for each change, it would work for a very long time, and the disk space would quickly run out. Instead, he only remembers the difference, i.e. in which files which lines have changed, deleted, or added. This delta between the two revisions is called diff. All that is on the server now is, ultimately, the sum of the diffs of all revisions. Sometimes branches are made from the main project (called trunk), which allows you to make changes independently in the trunk and branches. The fate of the branch is determined by the developers, based on her task. In some cases, it flows back into the trunk. SVN toolkit works with revisions and branches exclusively in diff terms. We can always see the diff between revisions (i.e. what exactly has changed from the previous revision). We can roll a commit made in one branch onto another branch - and again we will see exactly diff.

What does this analogy with SVN give us?
Nature always strives to follow the path of least energy consumption. I think that one of the reasons why Everett’s theory is hard to fit in your head is as follows. The idea that as a result of a collision of two atoms with the Universe (which is several tens of orders of magnitude larger) makes several complete copies - any sane person will seem, at least, strange. And here everything is clear to programmers: only the difference between the Universes (diff) is copied and “stored”.

So what about JOIN?
The forces of nature are always symmetrical, and if realities can branch, then they can also stick together. I adhere to the theory that the Universe arose as a result of the big bang, and someday (very soon) it will change its expansion to compression (two phases of the cycle: analysis and synthesis). Here we are talking about space, and there is also a tree of parallel realities expanding with space - it also should eventually come down to a point. Without a mechanism for merging realities, this would not be possible.
The existence of a force of gravity that seeks to unite reality can be illustrated by the following example.

You need to come to a specific address, and there are many ways to do this. There is a branching: in one reality you fly on a flip bubble, in another on a teleport, in the third generally on foot. On the one hand, these realities tend to disperse, to accumulate maximum diff, as some events drag others along (went on foot, met friends - went to a cafe, flew on a flip, fell into an antigravity pit - spilled coffee on clothes, flew home). On the other hand, there is a goal (to get to a given point) that defines the event vector. Deviations can be stronger than this vector, but not always. Most likely, one way or another, you still get to the place - the reality collapsed.
Purely physical merging of realities can be described by the same principle of superposition, as in the example with two rays of light. Here we come to another interesting conclusion: each point in space-time is a peak not only for the cluster of subsequent alternative events, but also as a result of the merger of a number of previous (parallel) events.

7. The laws of time


Consider some interesting features of the work of time (again, through the eyes of programmers).

Paradoxes are brunches
The most fierce paradox that many fantastic characters fear is the "grandfather paradox." Consider it on the example of the film "Back to the Future." Marty has moved from 1985. in 1955, and prevented the acquaintance of their parents. A new branch appears (code execution thread, SVN branch, whatever), i.e. alternative reality. Marty will never be born in her, but our Marty will remain in this world. Moving back to 1985, he will fall into the future of this alternative world. But Marty coped with the task - he not only introduced his parents, but also influenced their character. Returning in 1985, he was shocked by the positive changes that had occurred with his family. What happened? He did not let the brunch break up much with the trunk (in terms of its appearance in the world), and rolled the patch:
=================================================== ==================
- Hill Valley / Makflayev Family (revision 1 ++ ++
Hill Valley / Makflayev Family (working copy)
-Successful Biff Tannen - head
-Broken car
-Brother in a tracksuit
+ Beef wipes cars
+ Toyta Statler in the garage
+ Brother in a jacket and trousers

================================ =====================================
Time loops are recursive calculations
In the wonderful film Groundhog Day, poor Phil lived the same day hundreds of times until he brought it to perfection. Each “next day” he did not start from scratch - he remembered all the previous ones, had accumulated experience (for example, playing the piano), and this continued until a certain condition was triggered.

In one day, Phil spun about 10 years - i.e. "some" three and a half thousand iterations. Theoretically, there may be more, but not infinite. This conclusion is dictated by the law of conservation of energy. The time loop, which contains an infinite number of turns, is equivalent to an infinite energy density of events and an infinite density of information in this fragment of history. Most likely, a loop with an unconditional jump simply gradually "de-energizes", because she will be outside the main stream of time. If we draw an analogy with the program, that is, there is a certain mechanism that limits the maximum number of iterations for loops.

At the end of the cycle


But what, the law of time is such: we returned to the starting point, but already at a qualitatively new level. Whether to adhere to the orthodox paradigms of time, or use any opportunities for new understandings, is the choice.
Why is there not a plowed field for programmers here? Maybe the creator of Time was an IT-person to the bone marrow? It's all about logic. There are basic tasks, and there are their logical solutions. Keep diff instead of a full copy. Loop a set of actions, instead of replicating it. Any technical specialist in a wide variety of areas will face similar challenges. The device of reality is not an exception, there were similar tasks there too, and "someone" solved them. More precisely, everything is of course the opposite: we ourselves, without knowing it, repeat the decisions already put into reality (calling it logic, a systematic approach, etc.).
So the key to understanding the structure of reality falls into our hands. About time, you can not only shoot fiction, or documentaries (in essence, as a rule, are meaningless). To touch the secret of time, you do not need a LHC, or knowledge of a quantummech (although at a certain stage, perhaps this will not hurt). Every programmer already has everything necessary to study the nature of time - his way of thinking.

UPD
We touched on a very complicated topic, which caused a lot of discussion, the result of which - the answers and emphasis on what was said earlier - is offered for reading.

1. The main idea of ​​the article is not that the Universe is a computer, virtual reality, etc. The starting point of the article is that time is a process control mechanism . This is what builds the chain of events according to a certain algorithm (therefore, time = code), due to which the order of the Universe increases with time. One of the basic properties of this algorithm is cyclicality (there have already been examples). ALL PROCESSES in the Universe are cyclical in nature. Yes, the Earth’s movement around the Sun is connected with gravity, and it would seem, what does the time have to do with it? We can see this principle (the presence of a center and the rotation of elements around it) on any scale: from an atom (nucleus, electrons), to space (star systems, galaxies, according to one theory - the Universe itself, everything rotates). Rotational motion is the most fundamental, because all other types of movement are its special cases. If rotation is the basis of physical reality, then there is cyclicity in everything. And this is a property of time (because we are talking about the dynamics of processes).

2. Comparing time and program, we need to forget that the program is running . The very idea of ​​"fulfillment" implies the existence of a certain current moment, "present" - and it is only in our perception. If you look at eternity, we will see the world lines of objects stretched from the past to the future, i.e. any system exists "at once" in all moments. If we compare this world line with a certain sequence of code commands (time) and data (exchanged by this code) - can we say that this code is executed sequentially? Those. first this action, then this, after some time - there will be this ... and now we are talking about the "flow of time in time", i.e. we describe time through itself - a vicious circle. An adequate paradigm of the flow of time needs to be considered further.

3. A lot of questions about cycling. Once again: cyclicity is not a repetition of those events that have already happened. This is a repetition of the controlling effects of time. It comes from outside, from that system of which the object is a part. There are many such influences - the response to them gives rise to the rhythm of events. For example, a glass stands on a window, and he “meets” the sunrise regularly, in increments of a day.

4. Many are interested in the question of actual displacements (especially in the past), which did not have enough time to fully cover the article (but whether it will be the second is not yet clear). Add a few words.
To fall into the past is not to “recover data”, and where will you restore them? The line (t) of the runk (ie, the “arrow of time”), and the line of brunch branched off from it when transferred to the past - all these are chains of not “code”, but “code + data”. Here you need to think in the paradigm of functional programming: there is NO data outside the code execution! There are no variables whose state needs to be restored. If you have flown to another point in the "code execution graph" (that is, to the past), there will be the same data as "you used to be". How to do this in practice? Reasoning will lead us beyond the topic of programming.

5. A small continuation of the article can be found here .