#BabelOfCode 2024
Week 9
Language: Ada (Spark?)

Confidence level: High

PREV WEEK: https://mastodon.social/@mcc/114463342416949024
NEXT WEEK: https://mastodon.social/@mcc/114582208242623432

So I start reading the manual for Ada. I think: This is *great!* This has all these features, conveniences etc I was wishing for for years in C++, and didn't get until I jumped to Rust. I might have been using this for games in 2010 if I'd dug into it then!

Then I start writing an actual Ada program. It turns out to be basically a pain in the ass.

Now, it's possible that the reason I'm unhappy is I jumped directly into something Ada is not designed for. However, that thing I jumped into was "load a file at runtime into a string whose length is not known at compile time". I don't think this should be so hard in a recently-revised language. (I seem to be tragically consistent in this challenge at accidentally picking languages whose input systems prefer structured data-- like Ada and Fortran-- on puzzles where I'm parsing raw ASCII art.)
To pick at this. "Software" can mean a do of different things! There's environments where you spend lots of time poking at text strings— text files in a world where the UNIX "everything is a file" philosophy won, webdev. There's really important environments where they don't! If you're writing backend software interacting with databases and network services, maybe it turns out Ada is *perfect* for this environment and you don't care about the friction on files and variable-length strings. Dunno.
Anyway. I was able to open a file whose name is given on the command line, but not read lines from it (the line reader only wants to work with stdin). I was not able to take an arbitrary-length string from stdin (I hardcoded to 10,000 bytes, the length of my largest input). I wasted a bunch of time on it, decided this wasn't important, and moved on. I'm trying to write "professional" software in this challenge and Ada, at least for the UNIX-y cmdline-and-files environment, did not reward that.

Compromises on basic things continue. I want to create an array whose size is not known at compile time. I believe I can declare this with

Mem : Array (Natural range <>) of Integer range -1..Integer'Last;

But this errors there's no initialization. You can initialize an array to a fixed value with (Others => -1), but then it doesn't know the size. I wind up making a sub-procedure *just* because that is the only syntax I can find for initializing an array to a size. https://github.com/mcclure/aoc2024/blob/b46079ddcb5c45d52af5882e940a593050c7beb9/09-01-disk/src/puzzle.adb#L14

aoc2024/09-01-disk/src/puzzle.adb at b46079ddcb5c45d52af5882e940a593050c7beb9 · mcclure/aoc2024

Advent of Code 2024 challenge (laid-back/"babel" version) - mcclure/aoc2024

GitHub

Small observations:

- Inner procedures and functions of a larger procedure and function can access the outer procedure's local variables. That's nice. I suspect this is hiding some sort of horrible restriction on recursion, but it's nice.

- The error messages, at least in "gnat" the open source Ada compiler, are NOT good. I think this is downstream from the language having lots of minor similar-but-distinct concepts instead of single powerful concepts. There's a lot of jargon and lots of edges

Something I don't know how you'll feel about: You know how in every programming language except Lua you index arrays from 0, and in Lua you index arrays from 1? In Ada, if I'm understanding this correctly, you choose whether your code indexes arrays from 0 or 1. *On an array by array basis*. You could mix 0- and 1-indexed arrays in the same code. You could have an array which contains 10 elements at indexes 10 through 19 inclusive, if you wanted. An array is a map with integer keys in a range

Man writing Ada is *really* making me think I was too hard on Haskell

Maybe the problem with Ada was that the things it was trying to do were too advanced for the tools that were available to the designers at the time.

I'm having an awful problem with a very straightforward bit of code because I want to scan over some code iterating a variable erratically. But I don't have any good value to give *before iteration begins*— because Ada integer types are range-limited, I can't use "0". This would be no problem at all I had Option<>. But I don't have Option<>.

Guh. Part 1 done. It was ok once I actually had my data loaded into memory, but every moment up until there was pulling teeth, and honestly the ergonomics weren't *great* after that. The final insult was it taking a startling amount of time trying to figure out how to convert an Integer to a Long_Integer when it turned out my result was over 32 bits. It's Long_Integer(), but StackOverflow was a bunch of "how do I cast in Ada?" "*bragging* In Ada, you don't NEED to cast!"

https://github.com/mcclure/aoc2024/blob/9048d0cd5d29fc8f987c4f7369360279199fc2b0/09-01-disk/src/puzzle.adb

aoc2024/09-01-disk/src/puzzle.adb at 9048d0cd5d29fc8f987c4f7369360279199fc2b0 · mcclure/aoc2024

Advent of Code 2024 challenge (laid-back/"babel" version) - mcclure/aoc2024

GitHub

I pushed through Part 2 just to get it over with. I could have done a very clean, efficient implementation in any other language, but Ada makes creating new arrays enough of a pain I just wound up like… doing it the dumb nested loops way. Whatever. I realized in literally the final line of code I wrote that part of why I was struggling with loops was I didn't know "exit" existed.

It works. It's even relatively efficient. I don't feel proud of this code.

https://github.com/mcclure/aoc2024/blob/3adbb87169b82453caafd75308837c203247edc4/09-02-disk/src/puzzle.adb#L80

aoc2024/09-02-disk/src/puzzle.adb at 3adbb87169b82453caafd75308837c203247edc4 · mcclure/aoc2024

Advent of Code 2024 challenge (laid-back/"babel" version) - mcclure/aoc2024

GitHub
There's a lot I'm curious about in Ada. I'd really like to know more about its builtin Task primitive. I find the basic "in the small" writing of Ada frictionful enough I have lost my curiosity about attempting to experience its high-level primitives. I was originally intending to do part 1 of this puzzle in Ada and part 2 in Spark, but I don't… I just kinda want to stop. This puzzle has taken very almost the whole week, and part of that is WestJet's fault (long story), but I want to move on.

A problem I anticipated with this "Babel of Code" project and sure am hitting now: The AOC puzzles are a great way to learn a language, but they'll always focus on *only part* of a language. When I hit smalltalk, I'll have little opportunity to use objects. Multiprocessing, or verification capabilities like Spark, can be applied in *some* puzzles, but it's hard to know *which* puzzles until it's half complete. Like, what formal properties does THIS puzzle have to verify?

https://adventofcode.com/2024/day/9

Day 9 - Advent of Code 2024

@mcc The thread made me mildly curious, so I took a stab at it.

Caveat, while I am on WG 9 and an observer on the Ada Rapporteur Group, I am there only as a liaison from Unicode; I have only written Ada recreationally, and not much of it at that. My father used to chair the ARG and work on Ada compilers; most of what I know is from him.

https://github.com/eggrobin/ada-playground/blob/master/src/ada_playground.2.ada

ada-playground/src/ada_playground.2.ada at master · eggrobin/ada-playground

Contribute to eggrobin/ada-playground development by creating an account on GitHub.

GitHub

@mcc One thing where Ada shines that is relevant here is the ease with which you can define lots of integer types and subtypes and use them just like the predefined ones.

In this case, there are clearly two kinds of integers, and mixing those up is a bug—except in the weird checksum, where you needed a conversion anyway—: the block indices (and file sizes in blocks), and the file indices.

@mcc In my code at least, I rely on the file sizes being nonzero; this gets checked when reading the input just by introducing this subtype here, never mentioned again: https://github.com/eggrobin/ada-playground/blob/39ea262b9e581f88fd7aa5b9baf528ef1f6e1ace/src/ada_playground.2.ada#L11-L14.

On the runtime-sized array thing, I am recursing and returning a slice here: https://github.com/eggrobin/ada-playground/blob/39ea262b9e581f88fd7aa5b9baf528ef1f6e1ace/src/ada_playground.2.ada#L40-L63.

I think gnat has a “secondary stack” that lives on the heap, but some implementations for heap-averse applications returned those on the stack (with the caller not popping while the result is needed).

ada-playground/src/ada_playground.2.ada at 39ea262b9e581f88fd7aa5b9baf528ef1f6e1ace · eggrobin/ada-playground

Contribute to eggrobin/ada-playground development by creating an account on GitHub.

GitHub
@mcc Of course if I were writing this “for real” I would probably just use a Vector (still indexed on File_ID, the package is generic on both Index_Type and Element_Type) instead of recursively doubling arrays; the container libraries have been around for twenty years now.
@mcc I have never tried SPARK; but looking at the issues you were running into there, I wonder if things might be a bit smoother if you were more aggressively typing things to start with, instead of doing it all with subtypes of Standard.Integer.
@mcc On verbosity, it is an amusing coincidence that my code is 179 lines to your 180, despite having that pile of type declarations at the beginning and doing both parts.