Is Ada a good choice to develop a chess engine?

In this article I want to share my opinion regarding the choice of Ada as a programming language to be used for a chess engine. This article targets both at programmers who already know Ada and programmers who don’t. While reading please do not forget that I will not say that Ada is the best choice but I want readers to know what really Ada can do for you if you are a chess engine developer or, more in generale, just hobbyst programmer.

The choice of a language to start writing your engine

If we look at the strongest chess engine, they are written in C/C++. According to CPW the strongest chess engine in non C/C++ is Booot, written in Delphy, and the second one is CuckooChess, written in Java. In the moment I’m writing, Booot is the 8th in the computer chess rating list ranking with 3200+ Elo and CuckooChess is at 128th place with 2500+ Elo.

It’s not that hard to figure it out why C/C++ is absolute the preferred way. It’s the fastest language in terms of speed of execution and there is a very huge literature on the internet written in C/C++. All of us know the limits of both C and C++ but also we all know the features of those languages. Therefore, if you are looking for a language to start your work from scratch, by choosing either C or C++ you will never make a “mistake”.

Thanks to the great work of Alex Morozov we know that yes, it is possible to write a very strong chess engine also in other compiled language than C. And if you look on the internet you can find many beautiful engines written also in Python (that is well known to be very “slow”) or in Basic or in other “slow” languages. They are not very strong, maybe, but… there is a “but”.

In my expirience, I see that the choice of C/C++ is overrated. Many people, without any knowledge of chess programming, immediately start by choosing one of them because of the speed of execution. If you read chess programming forums you will also find easily that many chess engines have never seen the light because some kind of bug was too hard to find and the programmer gave up.

It is my opinion that those programmers thinks that C/C++ is the only choice available to write a strong engine and they don’t realize that their engine will most certainly never be so strong. Even if they can do it, it is my opinion that they can do it with any other compiled language. If someone thinks that a 2500+ elo chess engine is not enough strong, he’s wrong.

There is an incredible amount of work behind a chess engine. A great number of engines are so called derived, that means that the programmer started by cloning another engine. I don’t say that it is easy. But I can say that is much harder to write a chess engine from scratch.

The process involves a lot of coding, bug fixing and most of the time is spent in debugging some fucking code that does not do what you want it to do. That means that if you want to have a chance to finish your first draft you need a serious good debugging enviroment. That means, also, to be able to write easily debuggable code. Just to give you and idea, a chess engine can evaluate more than 500k positions per seconds in a common laptop (billions of positions in a match). Do you have the slightest idea what does it mean if you code blows up in the middle of a game and you have to look into a recursion depth of 7-8 function calls to find what the hell has happened? Well, those numbers are referred to a very basic chess engine like TSCP is.

It is even harder in the measure of how strong your engine is. Improving a chess engine is relatively easy in the beginning but after that you need to work hard for getting just a bunch of elo points. In my opnion programmers should consider this obstacles with more focus than they do in the choise of the language for the “speed of execution”.

I want to encourage you to think that, being an hobby project, you should first try to design a project by thinking that maybe it is better to have a working engine in Python that a non-working one in C/C++. And maybe one day, with more experience and knowledge, you can switch to another language as part of the improvement of the engine.

So, why you choose Ada for AdaChess?

If you don’t agree with my premise I won’t blame you. I am quite sure, anyway, that most of the people will agree with many of my ideas.

I choosed Ada for one, simple, reason: I love that language.

AdaChess, in the moment I’m writing, is the strongest chess engine written in Ada (well… the fact that it is the only one should not be mentioned…). If I would have loved one other language, I would have written an engine by using that language. I don’t care how strong the engine is, I just care what I can do by feeling satisfied of my work. I care to have a working engine and improving it, writing cool algorithms and yes, I care also make it stronger but not as the only and final target of the project, rather as a way to misure it.

I have many features in AdaChess that makes the engine slower (and therefore weaker) than the equivalent without those features. But they are those features that makes the identity of AdaChess. I am confident that I could also write a stronger engine in C. But it would not be fun, neither would worth it.

Is then a reason to try it?

Well, that’s a good question. In my opinion Ada is an awesome programming language, it has an incredible amount of features and an excellent design.

First of all, let me say that Ada code is compiled by the gcc, therefore you have the same debugger as C/C++ and the same code profiler. Ada has also it’s own development environment, called GPS, but you can use Emacs, Vim or Eclipse too.

Ada is also really fast, in terms of speed of execution. When you compile your software by using the full optimization you can obtain a source code that is in the average 1,7x slower than C – as you can see in those benchmark. According to the meaning of the branching factor, a 2x speed can be translated in chess engine that search 1 ply deeper and that – see Ferreira – is less than to 100+ elo points. Does C really worth those 100 elo points? It’s up to you.

But that’s not the point. To understand Ada it is better to compare it with C.

The C programming language has clearly designed to be an high level Assembly. His syntax is really concise and the language gives to the programmers a lot of freedom – and therefore the responsibility. While C is a great language, the experience reveal that writing correct software is a very hard task. Therefore, C is not a good when it comes to develop software that must guarantees an high level of reliability.

For such purpose we need to look towards a different attitude and here is where it comes Ada. His design target to help the developer in writing very high level quality code and prevent the failure of the final program. Ada is, in fact, the only choice for developing software related to the safety and security. Airplanes, train, ships, satellite, nuclear power… behind those system there is code written in Ada.

To achieve his target, Ada provide a set of features – some of them unique. I will tell you just some of them, for the whole list you are encouraged to search on the internet for further details.

Ada is a strong-typing language – strong typing is a feature that use the type system for preventing and detecting error in compile time – and it has a clear, safe and unambiguous syntax. Ada encourage the code readability and comes with check at compile-time and run-time to detect overflows, range-check failure, uninitialized variables and in general all kind of hidden bugs.

Being created for software design, Ada is a great choice for software engineering.  By using Ada, you will spend more time in coding cool stuffs than in fixing annoying bugs.

Ada is object oriented (though it doesn’t call class its object) and the code is organized in packages and subpackages. It supports all the features that you desire in a compiled language: operator overloading, safe pointers (called access types in Ada), templates (called generics in Ada), exceptions – Ada has a very advanced exceptions handling system – it support – of course – the procedure/function overloading (also by return type) and inlining.

Ada provides the developer the range type, it has then the native support for multi-threading and is capable of creating contract-based software – through preconditions, postconditions and type invariant. – and much more.

Conclusion

It should be clear that Ada has been a nice idea for writing my engine.

As I wrote many lines before, it is my opinion that the ability – and therefore the choice – of C/C++ as language for chess programming is overrated. Only the strongest engine can argue of it. The others, in my opinion, could choose their favourite one. If you’re favourite is C, then it’s ok, if not don’t worry! Don’t think that C itself will make your engine strong, it’s your code that does it.

In this article I tried to explain not only why Ada is a good idea but why I think that mostly every language is a good choice if you enjoy your hobby. Any computer language comes with some idioms in their mind: lear them, use it and have fun! You can do an excellent work, have a lot of challenges and fun even if your engine will not beat Kasparov!

Alessandro Iavicoli

I am an italian computer software developer. I am passionate about Ada and the game of chess, and as a consequence I do also chess program in Ada. I like very much dancing cuban salsa, which I am a certificated teacher too.

Leave a Reply

Your email address will not be published. Required fields are marked *