Regarding literals and loop indices modes / programming friction
jpl
jpl.algol68@gmail.com
Sun Jan 25 13:37:31 GMT 2026
Hej Mohammad-Reza,
Your kind reply truly brightened my day. It opened my eyes to several
ways to approach the issue. I had not ever thought of using "ops" so
freely, and they do indeed provide abilities to reduce friction.
Especially cool was the SIN/COS monadic operator concept.
Thank You, James
On Sun, Jan 25, 2026 at 2:09 PM Mohammad-Reza Nabipoor
<mnabipoor@gnu.org> wrote:
>
>
> Hi James.
>
>
> On Sun, Jan 25, 2026 at 11:11:39AM +0100, jpl wrote:
> > Greetings All,
> >
> > I have added some local operators for complex arithmetic to allow
> > proceeding with FFT work.
>
>
> Nice.
>
>
> >
> > 1. Are literals unable to be automatically coerced to the target of
> > the initialization?
> >
> > For example:
> >
> > int i:=1; (works as expected)
> >
> > long int il:= 1; (fails with error: 'int' cannot be coerced to
> > 'long int'))
> > long int il:= Leng 1 (using Leng works fine)
> >
>
>
> Yes. Or you can use `long int il := long 1;', as `long 1' is a denotation of
> mode `long int'.
>
> And the error message from compiler says
>
> ‘int’ cannot be coerced to ‘long int’ in a strong-unit
>
> The RHS of initialized variable-declarations is "strong context".
> And there's no coercion from `int' to `long int' in that context.
>
>
>
> > 2. Must we manually use "Leng" on a counter when operating on long real?
> >
> > long real zl;
> >
> > for count to 3 do
> > zl +:= Leng count
> > od;
>
>
> There's no widening coercion from `int' to `long real'; if `count' was
> of mode `long int', you didn't need that.
>
>
> >
> > If so, this creates a large amount of "friction" when changing (for
> > example) loops of a DFT calculation. Is it possible to decrease the
> > amount of friction?
> >
> > Example when using mode "compl":
> >
> > for i from Lwb x to Upb x do
> > for j from Lwb x to Upb x do
> > w := (cos(-(i*j)*2*pi/n) , sin(-(i*j)*2*pi/n));
> > y[i] := y[i] + w * x[j]
> > od
> > od;
> >
> > Example when using mode "long compl":
> >
> > for i from Lwb x to Upb x do
> > for j from Lwb x to Upb x do
> >
> > w := (long_cos(-(Leng i * Leng j) * Leng 2 * long_pi / Leng(n) ) ,
> > long_sin(-(Leng i * Leng j) * Leng 2 * long_pi / Leng(n) ));
> >
> > y[i] := y[i] + w * x[j]
> > od
> > od;
> >
>
>
> I played with the code and came up with something like this:
>
> ```algol68
> (
> { Helper operator to make new complex numbers. }
> op C = (real r) long compl: (LENG r, LENG 0.0);
>
> [] long compl x = (C 1.2, C 2.3, C 4.5);
> [3] long compl y;
> long real n = long 1;
>
> { Tricks to make life easier. }
> op * = (int i, long real lr) long real: LENG i * lr;
> long real pi = long_pi;
> proc sin = (long real lr) long real: long_sin (lr),
> cos = (long real lr) long real: long_cos (lr);
>
> for i from LWB x to UPB x
> do
> for j from LWB x to UPB x
> do
> long compl w;
>
> w := (cos (-(i*j)*2*pi/n), sin (-(i*j)*2*pi/n));
> y[i] := y[i] + w * x[j]
> od
> od;
>
> skip
> )
> ```
>
> Obviously you have to define all the operators for combination of integral and real
> numbers, I just did `op *' for `int, long real' just to get it compiled.
>
> Another way is to define SIN/COS as monadic operators, and then enjoying the overloading
> capabilities:
>
> ```algol68
> op SIN = (real r) real: sin (r),
> SIN = (long real lr) long real: long_sin (lr),
> COS = (real r) real: cos (r),
> COS = (long real lr) long real: long_cos (lr);
> ```
>
> And yes, the code snippet will not result to executable due to Jose's laziness :D
>
> fatal error: no lowering routine installed for construct. jemarch has been lazy
> 25 | y[i] := y[i] + w * x[j]
>
>
> Thanks for bringing it up, it really worth discussion how to make things easier to
> read and write.
>
>
> Regards,
> Mohammad-Reza
More information about the Algol68
mailing list