📜 ⬆️ ⬇️

Simula - 50 years of OOP

The article is a very brief introduction to Simula.

Simula ((SIMIUlation LAnguage)), the first programming language with classes and objects, is undeservedly almost forgotten, but from which the modern OOP has grown in the form in which it is present in our code. This is a language for simulation of reality. Developers of new programming languages ​​“looked around” at Simula when adding object-oriented programming mechanisms to their language. However, Simula is mentioned so seldom that in Russian Wikipedia at the time of publication there was only Halloween, and the network has a bunch of outdated links.



Bjorn Straustrup, speaking of the reasons for creating C ++, said: “This language arose because the author needed to write interrupt-driven modeling programs. The SIMULA-67 language is ideal for this if efficiency is not considered. ”

Why do I need it
I was led to Simula static methods in Java. More precisely, it all started with a review of the report of Egor Bugayenko, who calls static methods evil in Java. In short, in his opinion, they introduce elements of procedural programming into Java, breaking the PLO. Accordingly, the question arose - why did these methods appear in the Java language - what guided the creators of the language? Looking for the answer to this, the question got to Smalltalk (1971), from which (which is no secret) OOP was taken in Java. As it turned out, static methods were there, only they are called class methods. But Smalltalk did not originate from the air either. His predecessor was Simula (1965). As it turned out, similar methods were in there. Of course they were also called differently. In particular, I met the name "free block". A "free blocks" - this is the usual procedure. After all, the ancestor of Simula was Algol 60. And the first versions of Simula were a “superstructure” above Algol 60.

Story


In fact, the language is over 50 years old. The first version, known as Simula I, appeared in 1964-1965. It was an extension of the language Algol-60 (or rather, a preprocessor). The authors of the language are Norwegian scientists Kristen Nygor and Ole-Johan Dahl. The first versions of the language worked on 36-bit UNIVAC 1107 computers.

Already in 1967, Simula-67 was released, which is now called just Simula.
In our country, the first version of the language was launched on the Ural-16 computer.
In 1968, the language standard was officially frozen.

The language is distributed in the 70s of the last century, but then its fame dies away.
There are several reasons for such a drop in popularity, but in modern terms - a lack of marketing (a slowly developing product, price, weak PR)

Installation


We will train with the help of GNU Cim, which is actually a Simula code compiler in C.

Installing on Linux

$ wget http://simula67.at.ifi.uio.no/Cim/cim-3.37.tar.gz $ tar -xf cim-3.37.tar.gz $ cd cim-3.37 $ ./configure $ make $ sudo make install $ sudo ldconfig /usr/local/lib 

Mac installation

 $ wget http://simula67.at.ifi.uio.no/Cim/cim-3.37.tar.gz $ tar -xf cim-3.37.tar.gz $ cd cim-3.37 $ CFLAGS='-O0 -m32' ./configure $ make $ sudo make install 

Installation for Windows
Download the program from http://folk.uio.no/simula67/cim.shtml

Hello world!


We write in the notebook program:

 Begin OutText ("Hello World!"); Outimage; End; 

Save it as test1.cim
Then we do this (Linux version):

  cim test1.cim ./test1 

And we get the long-awaited
Hello World!

As can be seen from the syntax - classic Algol 60. Most of the questions on this code will not be, good in our country as a primary language for learning use its descendant Pascal, which is not very different in appearance.

Feature classes


 Begin Class Rectangle (Width, Height); Real Width, Height; Begin Real Area, Perimeter; Procedure Update; Begin Area := Width * Height; OutText("Rectangle is updating, Area = "); OutFix(Area,2,8); OutImage; Perimeter := 2*(Width + Height); OutText("Rectangle is updating, Perimeter = "); OutFix(Perimeter,2,8); OutImage; End of Update; Update; OutText("Rectangle created: "); OutFix(Width,2,6); OutFix(Height,2,6); OutImage; End of Rectangle; Ref(Rectangle) R; R :- New Rectangle(50, 40); End; 

Result:

Rectangle is updating, Area = 2000.00
Rectangle is updating, Perimeter = 180.00
Rectangle created: 50.00 40.00


Inside the main procedure, we created a Rectangle class with two parameters Width and Height, such as Real - that is, real numbers. According to the modern, these are the constructor's parameters - but they exist not only at the time of the creation of the object, but also during its existence. In addition, we added the Area and Perimeter attributes of the same type. As we see, they are calculated when the Update procedure is called. Since this procedure is declared inside the class, it becomes a method. What goes below the attributes and methods is called the life or the body (Life, Body) of an object.
Those. actually this is the code that is called when creating a new object. “Ref (Rectangle) R;” means the declaration of the R variable of the Rectangle class. The next line is the initialization of this variable.

Pay attention to the OutText call inside the Update procedure - is it not an analogue of the static method in Java?

Now let's look at inheritance


 Begin Class Rectangle (Width, Height); Real Width, Height; Begin Real Area, Perimeter; Procedure Update; Begin Area := Width * Height; OutText("Rectangle is updating, Area = "); OutFix(Area,2,8); OutImage; Perimeter := 2*(Width + Height); OutText("Rectangle is updating, Perimeter = "); OutFix(Perimeter,2,8); OutImage; End of Update; Update; OutText("Rectangle created: "); OutFix(Width,2,6); OutFix(Height,2,6); OutImage; End of Rectangle; Rectangle Class ColouredRectangle (Color); Text Color; Begin OutText("ColouredRectangle created, color = "); OutText(Color); OutImage; End of ColouredRectangle; Ref(Rectangle) Cr; Cr :- New ColouredRectangle(10, 20, "Green"); End; 

Result:

Rectangle is updating, Area = 200.00
Rectangle is updating, Perimeter = 60.00
Rectangle created: 10.00 20.00
ColoredRectangle created, color = Green

As you can see from the example, a subclass is defined as a normal class, but with a prefix of the name of the parent's class.

And you need to specify only additional parameters. Parameters that the parent had will be added automatically.

As you can see, when creating an object, you need to list all the parameters - first the parent, then the descendant.

Real world simulation


Now let's "simulate" something. Still, this language was created initially for modeling real-world objects.

Let's model work of the pedestrian traffic light. A traffic light has only two states. Every minute a new person approaches the transition and, depending on the state of the traffic light, either crosses the street or waits.

 Simulation Begin Procedure report (message); Text message; Begin OutFix (Time, 2, 0); OutText (": " & message); OutImage; End; Integer u; Ref (Semaphor) s; Integer i; Process Class Semaphor; Begin Boolean isRed; Ref (Head) waitingPersons; waitingPersons:- New Head; isRed := false; report ("GREEN ON"); While True Do Begin Hold (2.3); isRed := true; report ("RED ON"); Hold (4.25); isRed := false; report ("GREEN ON"); While not s.waitingPersons.Empty Do Begin Activate s.waitingPersons.First; s.waitingPersons.First.Out; End; End; End; Process Class Person (pid); Integer pid; Begin OutFix (Time, 2, 0); OutText (" Peson "); OutInt(pid, 3); OutText (" is near the crossover."); OutImage; If s.isRed Then Begin Wait (s.waitingPersons); End; OutFix (Time, 2, 0); OutText (" Peson "); OutInt(pid, 3); OutText (" is going."); OutImage; End; Process Class PersonGenerator; Begin While True Do Begin i := i + 1; Hold (1); Activate new Person(i); End; End; s:- New Semaphor; i := 0; Activate s; Activate New PersonGenerator; Hold (20); End; 

Result:

0.00: GREEN ON
1.00 Peson 1 is near the crossover.
1.00 Peson 1 is going.
2.00 Peson 2 is near the crossover.
2.00 Peson 2 is going.
2.30: RED ON
3.00 Peson 3 is near the crossover.
4.00 Peson 4 is near the crossover.
5.00 Peson 5 is near the crossover.
6.00 Peson 6 is near the crossover.
6.55: GREEN ON
6.55 Peson 3 is going.
6.55 Peson 4 is going.
6.55 Peson 5 is going.
6.55 Peson 6 is going.
7.00 Peson 7 is near the crossover.
7.00 peson 7 is going.
8.00 peson 8 is near the crossover.
8.00 peson 8 is going.
8.85: RED ON
9.00 Peson 9 is near the crossover.
10.00 Peson 10 is near the crossover.
11.00 peson 11 is near the crossover.
12.00 peson 12 is near the crossover.
13.00 Peson 13 is near the crossover.
13.10: GREEN ON
13.10 Peson 9 is going.
13.10 Peson 10 is going.
13.10 Peson 11 is going.
13.10 Peson 12 is going.
13.10 Peson 13 is going.
14.00 Peson 14 is near the crossover.
14.00 Peson 14 is going.
15.00 Peson 15 is near the crossover.
15.00 Peson 15 is going.
15.40: RED ON
16.00 Peson 16 is near the crossover.
17.00 Peson 17 is near the crossover.
18.00 Peson 18 is near the crossover.
19.00 Peson 19 is near the crossover.
19.65: GREEN ON
19.65 Peson 16 is going.
19.65 Peson 17 is going.
19.65 Peson 18 is going.
19.65 Peson 19 is going.

The main procedure starts with Simulation. The total simulation time is 20 minutes. The report procedure is auxiliary, it outputs data with the current value of the simulation time. Traffic light Semaphor in the status of "green" is 2.3 minutes, in the status of "red" - 4.25 minutes. The state is set by the isRed variable. The waitingPersons line is pedestrians who could not immediately cross the street as there was a red traffic light.

PersonGenerator is a pedestrian generator - every minute a new pedestrian approaches a traffic light. Hold plays the role of a simulation time course.

Process - the ancestor of all classes of "active" participants in this simulation.
As we can see, the code is quite intuitive and not complicated, a simple imitation can be done without a very deep immersion in the language.

Studying old programming languages ​​you can find out the origin of many things in modern languages. In this case, it is striking how much Simula is ahead of its time if you consider the computers on which it worked. I will not say that it makes sense to write code in this language. Because not all examples even from an English wiki work the first time. In particular, difficulties have arisen with virtual methods - although this is most likely related to a specific compiler. But for learning tasks and for simple simulation Simula seemed to me quite comfortable.

Useful links:

1. Installation for Linux, Mac, Windows
2. Introduction to the PLO from the University of Malta , and there is even a translation into Russian
3. Article as much as Knut himself
4. A site with a large number of working links

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


All Articles