Description
The language for the first month is… Ada! Ada is a language with lots of modern features, yet was conceived much before most languages we know today. It’s main use is security, ensuring that as many cases as possible are taken care of at the start, so the final program has less bugs.
Specification
Thus, the challenge will be to implement it in a scenario where bugs could be catastropic - A nuclear reactor. Not a real one, but it must calculate and track values to ensure the safe operation. The program must meet this specification:
- The only value being controlled by the program is how much each control rod is inside the reactor
- The heat of the reactor should be monitered and controlled by rules
- The heat dissipate at 5% per second
- Heat is generated as 2x the current heat in the generator, minus 0.01% (of the final heat) for each 1% a control rod is down
- There are 100 control rods that can be any integer between 0% and 100% down
- The heat has a 3% chance of multiplying by 4 instead of 2, and a 3% chance of multiplying by 1 instead of 2
- Heat generated is equal to heat disipated, and should be maximised if possible
- When heat reaches 0, the reactor shuts down and must be resparked at 1J
- When heat reaches 500KJ, the reactor casing begins to melt down, and the reactor should be stopped for massive repair
- When heat reaches 750KJ, the reactor explodes causing a major disaster
- The application should print out the current heat and a control rod summary.
- The application should print errors if either 500KJ or 750KJ are reached
- The application should require the user to restart the reactor if it reaches 0KJ
- The reactor should shut down when the user attempts to stop the program running
It’s quite a long specification, but we will soon see if Ada is up to the task.
Final Report
It’s over! I’m happy with how the final product turned out, and it was able to get up to 20KJ/s in the end. You can find it on the GitHub here. I had to change the specification slightly - putting all the control rods instantly put the temperature to zero! As that wasn’t realistic, I divided energy into neutron energy and system energy, where the neutron energy represented the increase in temperature per second, and was what was affected by the control rods and exponential increase. It was temperature however which was used for the final output. Here is a snippet of some of the final output:
Heat is 396.0649KJ, Output of 20.8455KJ/s
Heat is 398.8112KJ, Output of 20.9900KJ/s
Heat is 396.9102KJ, Output of 20.8900KJ/s
Heat is 402.3201KJ, Output of 21.1747KJ/s
Heat is 402.4084KJ, Output of 21.1793KJ/s
Exit Control Signal Received. Shutting down reactor...
Heat is 382.2879KJ, Output of 20.1204KJ/s
Heat is 363.1735KJ, Output of 19.1143KJ/s
Heat is 345.0148KJ, Output of 18.1586KJ/s
Heat is 327.7640KJ, Output of 17.2507KJ/s
Heat is 311.3758KJ, Output of 16.3882KJ/s
Heat is 295.8070KJ, Output of 15.5687KJ/s
Heat is 281.0166KJ, Output of 14.7903KJ/s
Heat is 266.9657KJ, Output of 14.0508KJ/s
Heat is 253.6174KJ, Output of 13.3482KJ/s
You can see the output is stable but starts to go down once Ctrl+C is pressed. However, this just tells us that the program works! The real question is how good was Ada at achieving it?
Ada had an interesting way of doing things. You define the main function in its own class, and variable definitions are at the top of this class. A nice feature it allows is the ability to easily define your own types. For example in this code snippet:
procedure Main is
use types;
Choice : Float; -- Random Number Choice (0 to 1)
G : Generator; -- Random Number Generator
Temp_Potential : Kilojoule := 0.001; -- Temperature potential is the total neutron energy in the system
Temperature : Kilojoule := 0.000; -- Temperature is the absolute energy in the system (not inc neutrons)
Output : Kilojoule := 0.0;
Control_Rods : Rod_Array;
Absorption : Percent;
...
Kilojoule, Rod_Array and Percent were all types I defined in types.ads
:
package types is
type Percent is delta 10.0 ** (-2) digits 5;
type Index is range 1 .. 100;
type Rod_Array is array (Index) of Percent;
type Kilojoule is delta 10.0 ** (-4) digits 8;
end types;
You can see that the ranges of the types are specified. This means Kilojoule can use fixed decimal points to avoid floating point errors. This is nice for precision as most languages don’t have nice fixed decimal support. In addition typing is extremely strict - any errors or lack of conversion will be brought up as a compiler error. Whether from that or not I’m not sure, but extremely few bugs came up during development - most time was spent trying to get it compiled the first time, which is good for this kind of project.
That said, around half of the compiler errors that came up were ‘internal errors’, which is extremely inconvenient when trying to write a program and not knowing what’s causing the problem. Though I assume that’s a problem with this specific compiler (MinGW).
One thing I didn’t like about Ada was the way it handles multiple files. It requires you to use a with name
and create name.adb
/name.ads
for anything you created, encouraging the use of packages. These acted almost like an Object in Scala, while Classes were just a more complex type. However this is a style that one might get used to over time.
In addition, the distinction between functions and procedures was quite slim, which could make it confusing when to use each one.
One extremely well done feature was tasks, which makes concurrent programming extremely straightforward. However, for Ctrl+C specifically, this was more tricky requiring a specific set of code that could really be made much more generic.
If statements and loops were well done, with loops even allowing many different types for iterating over data structures and infinite looping.
The split of headers and source code was likely inspired by C, but seems somewhat redundant here given experience with languages such as Java. However this may be for compatibility with C as it seems to have a similar compilation process, and may be used in linking.
Overall it seems like a good language for its time. While I didn’t like it’s package management, it is much cleaner than C, and had extremely helpful specialised syntax for typing and multi-threading. However, the strict nature of the compiler can make writing code difficult, and the seperation of variable declaration and use can fill up space in the declaration with temporary variables. But if a strictly typed language is required to be compiled to a specific target machine and pointers are not required, Ada could make a nice alternative to C++ using many more intuitive methods of working with data.