17

In Algol 68, I can declare the priority (precedence) of an operator-symbol:

    prio @ = 5;

(Higher number means higher precedence).

I can apparently redefine the priorities of built-in operators:

    begin
    prio + = 7, x = 6;
    print(2+3x4)
    end

This should result in printing the value 20, rather than the 14 that a standard parse would give.

Did actual implementations actually behave that way? With a quick scan, I see nothing in either the Informal Introduction to ALGOL 68 nor the ALGOL 68-R Users Guide to say it wouldn't. Still, I find that mutability to be a strange feature.

(I should try it and see but I don't have an ALGOL 68 system at my fingertips right now).

8
  • Somewhat unrelated: In Haskell you can also declare operator precedence, so I had to quickly try to re-define existing ones, but it looks like you cannot do that without also declaring them, so that would need some tricks to make it actually work... not that anybody would actually want to do that.
    – dirkt
    Apr 7 at 5:48
  • Why don't you try with an ALGOL 68 system before posting an question? Apr 7 at 5:48
  • Yes: I believe you can. Algol-68 Genie is here sw.ccs.bcs.org/CCs/g3/index.html and David Holdsworthy's restoration of Algol68R at sw.ccs.bcs.org/CCs/g3/index.html. I'll check out later and consult my copy of Lindsey and Van der Meulen Apr 7 at 7:34
  • I have GEORGE 3 and ALGOL-68R on a computer somewhere in the basement, but have to dig it out. Apr 7 at 11:55
  • 2
    @thebusybee - well, I thought the question might also be of interest to the RC community. Apr 7 at 11:56

2 Answers 2

21

Yes you can, and it is also scoped (as you would expect with an orthogonal language like Algol 68). On Algol 68 Genie you get the following result:

C:\Users\Brian>a68g SODave.a68
        +20        +14

C:\Users\Brian>type SODave.a68
BEGIN
  BEGIN
    PRIO + = 7, * = 6;
    print(2+3*4)
  END;
print(2+3*4)
END

C:\Users\Brian>

12
  • 1
    Wow, that sounds as annoying as the True, False = False, True annoyance in earlier versions of Python :-)
    – paxdiablo
    Apr 7 at 12:04
  • 5
    @paxdiablo One of my coworkers once redefined NIL on a Lisp Machine; it crashed almost immediately. And I hope we all remember that Fortran's pass-by-reference allowed reassigning numeric literals.
    – Barmar
    Apr 7 at 14:27
  • 1
    Ooh are we doing this? I like #define while if. Apr 7 at 16:43
  • 1
    One of the more esoteric features that no other commonly used language adopted.
    – cup
    Apr 7 at 17:14
  • 2
    @kaz Scoping does apply as shown in my edited example. Apr 7 at 21:18
4

This is just an answer to @Will Hartung's query about why priority changes were added. Say there is a list with two routines

ref List ListAdd(ref List ll, int item)
ref List ListRemove(ref List ll, int item)

We can code as

ListAdd(lll, item1);
ListRemove(lll, item2);

Instead of doing that, we can define two operators

op + = (ref List ll, int ii) ref List 
op - = (ref List ll, int ii) ref List

So we can do

lll := lll + 3;
lll := lll - 4;

What if we wanted to combine the operations?

lll := lll + 3 - 4;

Since + and - are the same priority, the order is indeterminate. It could be

lll := (lll + 3) - 4

or

lll := lll + (3 - 4);

If I remember correctly, the default priority of + and - is 6. There are obscure things that you remember even though you haven't used them for 40+ years. If we want the first case, we could change the priority of + to be 7 so that the + always happens first.

Alternatively, if you do no wish to redefine priorities, do it as two separate statements or just use parenthesis.

3
  • In ALGOL 68 was there a defined (by the language) associativity for operators at the same priority?
    – davidbak
    Apr 10 at 22:50
  • 1
    The associated priorities are defined in pg 130, sect 10.2.3.0 of the Revised Report on the Alogrithmic Language Algol 68, softwarepreservation.org/projects/ALGOL/report/… . The associativity is not defined. I remember being caught by that when I went from the Algol68-R (from RSRE, now Qinetiq) compiler to the Algol68-S (from Cambridge Uni) compiler.
    – cup
    Apr 12 at 7:10
  • Thanks, I didn't realize ALGOL 68 allowed you to define operators on data structures. Apr 14 at 21:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.