input format, ";", ":", ";\"
The cadabra book says Lines always have to be terminated with either a “;” or a “:”.
In cadabra2-cli 2.4.2 this works with expressions "a:=something", but not with algorithms. Algorithm with ":" produces an "invalid syntax" error. Algorithm with ";" works as expected, with output. Algorithm without ";" or ":" works without output, but does not modify "_" expression:
#!/usr/bin/cadabra2-cli
a:= a = a1:
substitute($b=b1$, $b1=b2$);
substitute(a, $a1=a2$)
print("print(_):", _)
print("print(a):", a)
Output ('_' contains result of first substitute, not last one):
b = b2
print(_): b = b2
print(a): a = a2
Adding '\' after substitute($b=b1$, $b1=b2$); suppresses output of this line and makes the next line to set "_". This is the only way I found to stack multiple algorithms with using "_" and without producing output.
It would be good to have better documentation to understand this behaviour...
Yeah, this is a bit of a weird left-over from the days that Cadabra did not use Python for its programming language. If I ever find a few hours of spare time just sitting in a corner, I'll clean this up :-(
Another related thing: ";" in the beginning of a line produces strange results.
ex :=
a + b
;
does not work as expected, replacing ";" with ":" or adding space before ";" changes things.
Potentially related.
Running the below script
A := u -> 1.
_;
A;
B := u -> 2.
_;
A;
B;
outputs
u → 1
u → 1
u → 2
u → 2 (<-- expected output from A; would be u->1)
u → 2
So the command "A;" prints two different outputs
Ubuntu, Cadabra 2.3.6 (build private dated 2022-01-31)
(edit: clearer variable names)
The first issue is, on second thought, actually intended. The logic is that if you use ; to display the last expression, then _ gets assigned a reference to the last expression. If you don't do that (if you do not terminate that substitute(...) with a ;) then _ remains unchanged. A simple workaround is
a:= a = a1:
substitute($b=b1$, $b1=b2$);
_ = substitute(a, $a1=a2$)
That is: set the 'last expression' variable '_' manually.
The other issues are now solved in 2248d92257 (on the microtex branch, will become master soon).