I’m developing a thick binding for glpk, the Gnu Linear Programming Toolkit. I’ve worked with (and on) the Sage computer algebra system before, and something it allows, which I’ve always liked, is the ability to add constraints to a linear program using ordinary mathematical notation; e.g.,
lp = MixedIntegerLinearProgram()
x, y = lp[0], lp[1]
lp.add_constraint(x + 1 <= 2)
While reading the RM, I realized that the same is possible in Ada, so I implemented it; for example, the following compiles and runs correctly:
declare
Program : Linear_Program (Num_Vars => 3, Num_Constraints => 3);
begin
X : constant Expression := Program (1);
Y : constant Expression := Program (2);
Z : constant Expression := Program (3);
Program.Set_Constraint (1, X + Y + Z <= 100.0);
Program.Set_Constraint (2, 10.0 * X + 4.0 * Y + 5.0 * Z <= 600.0);
Program.Set_Constraint (3, 2.0 * X + 2.0 * Y + 6.0 * Z <= 300.0);
end;
(I plan to remove Num_Vars and Num_Constraints as discriminants, and switch from Set_Constraint to Add_Constraint, given that someone may want to warm boot a solution after adding constraints.)
Then I recalled that the recent Advent of Code used a linear program whose constraints were actually equality. So I could, in principle, specify constraints as follows:
Program.Set_Constraint (1, X + Y + Z = 10.0);
…and I implemented that, too.
All in all, I entirely too pleased with myself.
But the use of = somehow triggered memories from when I first studied operator overloading, how many criticized its overuse, often in unintuitive ways.
I don’t recall reading those criticisms lately, and this does not strike me as unintuitive, but I was curious how regular Ada programmers feel about operator overloading:
- When is it a good idea to use?
- Do you think there’s something about Ada’s approach to it that makes it safer, or less prone to abuse, than other languages’?
- Does this seem like a legitimate case?
Thanks in advice for any advice.