Writing Magik with Github Copilot

December 27, 2024    generative ai copilot

These days AI is booming. Look anywhere and you’ll see claims that AI will replace everything or if not everything it will at least aid you during your work, making your work significantly easier. Of course, these claims are over the top. We do see claims that tools such as Github Copilot does provide a boost in productivity, although, it will not make developers redundant. While Copilot does work for generally used languages, does it work with Smallworld Magik?

Copilot is trained on a lot of source code from Github, as stated on the website. It should include all languages. Given that the most popular of 2024 is Python according to the Tiobe Index, it should be no surprise that Copilot is versed well in Python and its libraries. Other popular languages include C/C++, Java, C#, JavaScript, etc. All languages which are readily available. From my Python work, on projects such as Home Assistant, I can say that Copilot works well for Python. Likewise for Java, when it is used for, for example, magik-tools. And it’s not only the language Copilot can support a developer with. It also knows many frameworks, as learned from the many publicly available repositories, but also common tasks, file formats, etc.

There are some Magik projects hosted on Github publicly, although these are very scarce. Therefore, it seems unlikely that Copilot can support Magik developers. Or can it? Let’s try it! In this example I’ll try to build (the beginnings of) a PNG file reader. This example is superficial, but shows some of the Copilot features. It should be noted that the workspace does contain other Magik code, some containing similar functionality for reading (binary) file formats. The Copilot documentation suggests opening editors for relevant files, as these can get sent along for context.

First an empty file called png_reader.magik is created. As soon as an def_slotted_exemplar( is entered, Copilot starts a suggestion, as seen in the following screenshot, completing the name of the exemplar. While minor, it already provides a bit of insight that Copilot does understand a bit of Magik, or can at least derive it from other sources.

completing def\_slotted\_exemplar

When creating the png_reader.init() method, Copilot already suggests setting the slot in_stream. Almost correct, as the slot is named input_stream, but very close!

slot suggestion

Further along, when reading the PNG header, Copilot already knows the first byte should be of value 137, part of the PNG header. It suggests the check, although it is trying to use the hexadecimal notation. Also very close!

header check suggestion 1

Not all is well though. Copilot does not fully “understand” how errors and the like work in Magik at this moment, so it suggests _error("..."). Clearly this is not valid Magik. This most likely happens due to missing context.

header check suggestion 2

After some more examples, it does understand how to raise conditions.

header check suggestion 3

While the example here is minimal, it does show that Copilot does “understand” a bit of Magik and can provide suggestions. And while Copilot does suggest invalid code, it “learns” how to do things the Magik way.

Also, the suggestions seem to be just a single line to a few lines. However, even a small suggestion can be very helpful. Be aware however, that Copilot also does hallucinate and can suggest code which makes no sense. For other projects, which I cannot share at the moment, I’ve seen that the suggestions can become a little bit larger.

All in all, Copilot does seem to be able to provide proper suggestions for Magik code. While not included in this example, Coplit can provide explanations for code as well, including for Magik code as well. Clearly, Copilot is able to aid a Magik developer already. Given the speed at which new features are introduced, surely the effectiveness will only increase.