Case statements containing base type labels
Given the declarations Shape = RECORD x, y: REAL END Rectangle = RECORD (Shape) w, h: REAL END Circle = RECORD (Shape) r: REAL END does a case statement containing labels for all three types have well defined behavior? CASE s OF Rectangle: ... | Circle: ... | Shape: ... END In this case a rectangle and a circle respectively is also a shape so it's not clear what label is selected and the order of the labels should not matter. Regards, August
August In Oberon-07 it is defined as follows: The type T of the case expression (case variable) may also be a record or pointer type. Then the case labels must be extensions of T, and in the statements Si labelled by Ti, the case variable is considered as of type Ti. PROCEDURE Draw (s: Shape) BEGIN CASE s OF Rectangle: ... | Circle: ... | Shape: ... • this is not allowed as “Shape” is not an extension of “Shape” END END Draw; Jörg Von: Oberon <oberon-bounces@lists.inf.ethz.ch> im Auftrag von August Karlstrom <fusionfile@gmail.com> Datum: Montag, 28. August 2023 um 12:53 An: oberon@lists.inf.ethz.ch <oberon@lists.inf.ethz.ch> Betreff: [Oberon] Case statements containing base type labels Given the declarations Shape = RECORD x, y: REAL END Rectangle = RECORD (Shape) w, h: REAL END Circle = RECORD (Shape) r: REAL END does a case statement containing labels for all three types have well defined behavior? CASE s OF Rectangle: ... | Circle: ... | Shape: ... END In this case a rectangle and a circle respectively is also a shape so it's not clear what label is selected and the order of the labels should not matter. Regards, August -- Oberon@lists.inf.ethz.ch mailing list for ETH Oberon and related systems https://lists.inf.ethz.ch/mailman/listinfo/oberon
On Mon, Aug 28, 2023, at 13:15, joerg.straube@iaeth.ch wrote:
August
In Oberon-07 it is defined as follows: The type T of the case expression (case variable) may also be a record or pointer type. Then the case labels *must be extensions of T*, and in the statements Si labelled by Ti, the case variable is considered as of type Ti.
PROCEDURE Draw (s: Shape) BEGIN CASE s OF Rectangle: ... | Circle: ... | Shape: ... this is not allowed as “Shape” is not an extension of “Shape” END END Draw;
Jörg
A record type is also an extension of itself. So that is valid. -- Diego Sardina
Diego Okay. Then it can be seen as ELSE of the CASE 😊 Jörg Von: Oberon <oberon-bounces@lists.inf.ethz.ch> im Auftrag von Diego Sardina <dsar@eml.cc> Datum: Montag, 28. August 2023 um 13:40 An: oberon@lists.inf.ethz.ch <oberon@lists.inf.ethz.ch> Betreff: Re: [Oberon] Case statements containing base type labels On Mon, Aug 28, 2023, at 13:15, joerg.straube@iaeth.ch<mailto:joerg.straube@iaeth.ch> wrote: August In Oberon-07 it is defined as follows: The type T of the case expression (case variable) may also be a record or pointer type. Then the case labels must be extensions of T, and in the statements Si labelled by Ti, the case variable is considered as of type Ti. PROCEDURE Draw (s: Shape) BEGIN CASE s OF Rectangle: ... | Circle: ... | Shape: ... • this is not allowed as “Shape” is not an extension of “Shape” END END Draw; Jörg A record type is also an extension of itself. So that is valid. -- Diego Sardina
So I think the answer to the original question is that *in the reference implementation, not the report* it is well-defined, and the order does matter: it's a cascaded if...then...else of type tests; However that's perhaps against the original spirit of CASE, and what it *should* be in general is perhaps what Joerg said, the base type case is effectively ELSE, and might even preferably be implemented that way... I love it when everyone's right ;-) Cheers, Paul On 2023-08-28 13:05, joerg.straube@iaeth.ch wrote:
Diego
Okay. Then it can be seen as ELSE of the CASE 😊
Jörg
Von: Oberon <oberon-bounces@lists.inf.ethz.ch> im Auftrag von Diego Sardina <dsar@eml.cc> Datum: Montag, 28. August 2023 um 13:40 An: oberon@lists.inf.ethz.ch <oberon@lists.inf.ethz.ch> Betreff: Re: [Oberon] Case statements containing base type labels
On Mon, Aug 28, 2023, at 13:15, joerg.straube@iaeth.ch wrote:
August
In Oberon-07 it is defined as follows:
The type T of the case expression (case variable) may also be a record or pointer type. Then the
case labels must be extensions of T, and in the statements Si labelled by Ti, the case variable is
considered as of type Ti.
PROCEDURE Draw (s: Shape)
BEGIN
CASE s OF Rectangle: ... | Circle: ... | Shape: ... ß this is not allowed as “Shape” is not an extension of “Shape” END END Draw;
Jörg
A record type is also an extension of itself. So that is valid.
--
Diego Sardina -- Oberon@lists.inf.ethz.ch mailing list for ETH Oberon and related systems https://lists.inf.ethz.ch/mailman/listinfo/oberon
On 2023-08-28 14:19, Paul Reed wrote:
So I think the answer to the original question is that *in the reference implementation, not the report* it is well-defined, and the order does matter: it's a cascaded if...then...else of type tests;
However that's perhaps against the original spirit of CASE, and what it *should* be in general is perhaps what Joerg said, the base type case is effectively ELSE, and might even preferably be implemented that way...
"NO, NO! He should look only at the Oberon-Report. This is the definitive document. But never the compiler! Oberon-Users do not have access to the compiler source. The very idea of a high-level language was that it must not be defined with reference to a computer (or compiler), but be clearly defined as an abstract machine, the language report. I am afraid that this notion got lost over the past decades, where in case of doubt one does not analyze the situation, but simply, due to interactive usage, try and try again. Also, the "open source" is a terrible promoter of deserting this most useful idea. Another consequence is that "everyone" creates his own compiler (language?) version, compromising the very idea of a common language serving communication. Please do not consider my compiler as the definition of Oberon, and in case of doubt read the Report. It is the privilege of a report to leave certain constructs undefined. In this case a user should simply avoid using the (undefined) feature." (Words of wisdom from Niklaus Wirth) https://lists.inf.ethz.ch/pipermail/oberon/2020/014095.html /August
On 2023-08-28 13:15, joerg.straube@iaeth.ch wrote:
case labels *must be extensions of T*
As mentioned by Diego, type extension is defined so that each record type is a (trivial) extension of itself: "A type T extends a type T0, if it equals T0, or if it directly extends an extension of T0." https://miasap.se/obnc/oberon-report.html#sec6.3 However, the same dilemma occurs also if we consider strict extensions, for example CASE animal OF Squirrel: ... | Whale: ... | Mammal: ... END /August
On Mon, Aug 28, 2023 at 10:00 PM August Karlstrom <fusionfile@gmail.com> wrote:
However, the same dilemma occurs also if we consider strict extensions, for example
CASE animal OF Squirrel: ... | Whale: ... | Mammal: ... END
This is just equivalent to (and makes as much sense as): IF animal IS Squirrel THEN ... ELSIF animal IS Whale THEN ... ELSIF animal is Mammal THEN ... END Refer to the pair of examples on P 61 of Programming in Oberon, 2014: https://people.inf.ethz.ch/wirth/Oberon/PIO.pdf Regards, Chris -- Chris Burrows CFB Software https://www.astrobe.com
August As Chris said, the CASE is ”just” a cascade of IF-ELSE. Assume you have TYPE Animal = RECORD age: INTEGER END Mammal = RECORD (Animal) laysEggs: BOOLEAN END; Squirrel = RECORD (Mammal) hiddenNuts: INTEGER END; Whale = RECORD (Mammal) weight: INTEGER END; PROCEDURE Feed(animal: Animal) BEGIN CASE animal OF Squirrel: Out.String(”nuts for winter:”) Out.Int(animal.hiddenNuts, 0)| Whale: Out.String(“weight in tons:”); Out.Int(animal.weight) | Mammal: Out.String(“Mammal”) END; Out.Ln END Feed; VAR s: Squirrel; s.hiddenNuts := 10; Feed(s); (* writes out the nbr of nuts *) VAR w: Whale; s.weight := 330; Feed(w); (* writes out its weight *) VAR m: Mammal; Feed(m); (* writes “Mammal” *) Let’s assume, with Feed() as given above you define TYPE Platypus = RECORD (Mammal) livesInAustralia: BOOLEAN END; Bird = RECORD (Animal) nbrOfFeathers: INTEGER END; VAR p: Platypus; Feed(p); (* writes “ Mammal” as p is a Mammal *) VAR b: Bird; b.nbrOfFeathers := 2500; Feed(b); (* writes nothing as b is neither a Squirrel nor a Whale nor a Mammal *) br Jörg Von: Oberon <oberon-bounces@lists.inf.ethz.ch> im Auftrag von Chris Burrows <cfbsoftware@gmail.com> Datum: Montag, 28. August 2023 um 15:37 An: ETH Oberon and related systems <oberon@lists.inf.ethz.ch> Betreff: Re: [Oberon] Case statements containing base type labels On Mon, Aug 28, 2023 at 10:00 PM August Karlstrom <fusionfile@gmail.com<mailto:fusionfile@gmail.com>> wrote: However, the same dilemma occurs also if we consider strict extensions, for example CASE animal OF Squirrel: ... | Whale: ... | Mammal: ... END This is just equivalent to (and makes as much sense as): IF animal IS Squirrel THEN ... ELSIF animal IS Whale THEN ... ELSIF animal is Mammal THEN ... END Refer to the pair of examples on P 61 of Programming in Oberon, 2014: https://people.inf.ethz.ch/wirth/Oberon/PIO.pdf Regards, Chris -- Chris Burrows CFB Software https://www.astrobe.com
August You are right: by only reading the Oberon-07 report https://people.inf.ethz.ch/wirth/Oberon/Oberon07.Report.pdf without having the background info given in chapter 23.4 https://people.inf.ethz.ch/wirth/Oberon/PIO.pdf you don’t know that the “CASE with types” must be seen as an IF-ELSE cascade (implying that the order is important!). This code snippet CASE animal OF Squirrel: Out.String(”Squirrel”)| Whale: Out.String(“Whale”) | Mammal: Out.String(“Mammal”) END; and this snippet CASE animal OF Mammal: Out.String(“Mammal”) | Squirrel: Out.String(”Squirrel”) | Whale: Out.String(“Whale”) END; is NOT the same. br Jörg Von: Oberon <oberon-bounces@lists.inf.ethz.ch> im Auftrag von joerg.straube@iaeth.ch <joerg.straube@iaeth.ch> Datum: Montag, 28. August 2023 um 18:50 An: ETH Oberon and related systems <oberon@lists.inf.ethz.ch> Betreff: Re: [Oberon] Case statements containing base type labels August As Chris said, the CASE is ”just” a cascade of IF-ELSE. Assume you have TYPE Animal = RECORD age: INTEGER END Mammal = RECORD (Animal) laysEggs: BOOLEAN END; Squirrel = RECORD (Mammal) hiddenNuts: INTEGER END; Whale = RECORD (Mammal) weight: INTEGER END; PROCEDURE Feed(animal: Animal) BEGIN CASE animal OF Squirrel: Out.String(”nuts for winter:”) Out.Int(animal.hiddenNuts, 0)| Whale: Out.String(“weight in tons:”); Out.Int(animal.weight) | Mammal: Out.String(“Mammal”) END; Out.Ln END Feed; VAR s: Squirrel; s.hiddenNuts := 10; Feed(s); (* writes out the nbr of nuts *) VAR w: Whale; s.weight := 330; Feed(w); (* writes out its weight *) VAR m: Mammal; Feed(m); (* writes “Mammal” *) Let’s assume, with Feed() as given above you define TYPE Platypus = RECORD (Mammal) livesInAustralia: BOOLEAN END; Bird = RECORD (Animal) nbrOfFeathers: INTEGER END; VAR p: Platypus; Feed(p); (* writes “ Mammal” as p is a Mammal *) VAR b: Bird; b.nbrOfFeathers := 2500; Feed(b); (* writes nothing as b is neither a Squirrel nor a Whale nor a Mammal *) br Jörg Von: Oberon <oberon-bounces@lists.inf.ethz.ch> im Auftrag von Chris Burrows <cfbsoftware@gmail.com> Datum: Montag, 28. August 2023 um 15:37 An: ETH Oberon and related systems <oberon@lists.inf.ethz.ch> Betreff: Re: [Oberon] Case statements containing base type labels On Mon, Aug 28, 2023 at 10:00 PM August Karlstrom <fusionfile@gmail.com<mailto:fusionfile@gmail.com>> wrote: However, the same dilemma occurs also if we consider strict extensions, for example CASE animal OF Squirrel: ... | Whale: ... | Mammal: ... END This is just equivalent to (and makes as much sense as): IF animal IS Squirrel THEN ... ELSIF animal IS Whale THEN ... ELSIF animal is Mammal THEN ... END Refer to the pair of examples on P 61 of Programming in Oberon, 2014: https://people.inf.ethz.ch/wirth/Oberon/PIO.pdf Regards, Chris -- Chris Burrows CFB Software https://www.astrobe.com
On 2023-08-28 15:20, Chris Burrows wrote:
Refer to the pair of examples on P 61 of Programming in Oberon, 2014:
https://people.inf.ethz.ch/wirth/Oberon/PIO.pdf <https://people.inf.ethz.ch/wirth/Oberon/PIO.pdf>
The examples you refer to in "Programming in Oberon" are in the context of message records and all labels are (proper) direct extensions of the base type Message so there is no ambiguity in this case; the labels can be arranged in any order without affecting the behavior of the case statement (as it should be). My current conclusion is that a case statement where one type label is an extension of another is not well-defined. I think an ambitious compiler could even reject such a statement, similar to how repeated case labels can be handled. /August
On Mon, 28 Aug 2023 19:27:44 +0200, August Karlstrom wrote:
My current conclusion is that a case statement where one type label is an extension of another is not well-defined.
I agree. IMHO it was a bad idea to mix the WITH statement and the CASE statement at all. The WITH statement was defined by IF/ELSIF so no problem here: For example in "Object-Oriented Programming in Oberon-2" (Mössenböck 1993) Appendix A: "WITH v: T1 DO S1 | v: T2 DO S2 ELSE S3 END has the following meaning: if the dynamic type of v is T1, then the statement sequence S1 is executed, where v is regarded as if it had the static type T1; else if the dynamic type of v is T2, then S2 is executed, where v is regarded as if it had the static type T2; else S3 is executed. T1 and T2 must be extensions of T0." As explained by Wirth in "Differences between Revised Oberon and Oberon" (Wirth 2011) "The case statement performs the same function as the if statement. However, it is intended to use a different technique of implementation, namely a single, indexed branch instead of a cascade of conditional branches." The 2011 version of Oberon-07 only allows INTEGER and CHAR for CASE statements. Mixing with type tests makes no sense as they will most likely be implemented as several IF - ELSIF statements.
I think an ambitious compiler could even reject such a statement, similar to how repeated case labels can be handled.
I think it should reject such code as the case labels are not distinct from each other. Otherwise the semantics of the CASE statement would change for this case from the original intention and the other variant with INTEGER/CHAR labels which must be distinct. Greetings, chris
Unfortunately I didn't have much time to partecipate to this interesting conversation, however I have already talked in the past about the CASE/WITH statements. I agree it was a bad idea to merge WITH in the CASE statement, in all Wirth languages the CASE statement was always implemented as a jump table (of course I'm excluding Turbo Pascal and the other variants that are not made directly by Wirth). The WITH statement requires another kind of code generation. Regarding the implementation of the WITH statement, in an extensible system or in an environment where new classes are added in the hierarchy, the only implementation possible is the sequence of IF, ELSIF, etc. To generate a jump table all the classes must be known a priori. Adding a new class (even in another module) means to rebuild the jump table and the module that uses it (breaking separate compilation). A jump table could be implemented for Component Pascal WITH statement for those hierarchies that are closed and can't be extended anymore (or "final" with Java terminology). This is also discussed in Appel - Modern Compiler Implementation in Java (pages 291 and 292), where the author talks about the Modula-3 TYPECASE. I extracted the part and uploaded in the link below (it will be removed in 30 days since it's protected material). https://postimg.cc/GHS47H6k -- Diego Sardina
participants (6)
-
August Karlstrom -
chris -
Chris Burrows -
Diego Sardina -
joerg.straube@iaeth.ch -
Paul Reed