gm2
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: proctype and procedure type checking fully implemented


From: Fischlin Andreas
Subject: Re: proctype and procedure type checking fully implemented
Date: Mon, 22 Apr 2024 08:33:43 +0000

Dear Benjamin,

Agreed, but let’s keep it simple.

In Wirth’s book PIM1 (I would call this the birth of M2) we find on p. 155

Operand types are said to be assignment compatible, if either they are compatible or both are INTEGER or CARDINAL or subranges with base types INTEGER or CARDINAL.

or our case with actual and formal parameters of procedures on p. 156

The types of corresponding actual and formal parameters must be identical In the case of variable parameters, or assignment compatible in the case of value parameters.

or in the report part of PIM1, i.e. p.161

The types of corresponding actual and formal parameters must be identical In the case of variable parameters, or assignment compatible in the case of value parameters.

And then I would say your fine wording

"Modula-2 follows loose name equivalence, except for assignments between (1) INTEGER and CARDINAL, (2) INTEGER and LONGINT, (3) LONGINT and CARDINAL, (4) REAL and LONGREAL, where implicit type conversion shall occur.”

while absent from the report is nevertheless the common assumption and practice in M2 since its beginning and I would argue we should continue using it, even if it makes compilers a bit more complex. I disagreed with Niklaus Wirth always on the point that programmer's life as a user of a language needs also to be considered when designing and/or defining a language, not only the compiler builder’s one. Keep it as simple as possible for all!

Cheers,
Andreas


ETH Zurich
Prof. em. Dr. Andreas Fischlin
Systems Ecology - Institute of Biogeochemistry and Pollutant Dynamics
CHN E 24
Universitaetstrasse 16
8092 Zurich
SWITZERLAND


+41 44 633-6090 phone
+41 79 595-4050 mobile

             Make it as simple as possible, but distrust it!
________________________________________________________________________














On 22 Apr 2024, at 02:05, Benjamin Kowarsch <trijezdci@gmail.com> wrote:



On Mon, 22 Apr 2024 at 00:24, Fischlin Andreas wrote:
 In M2 assignments such as

x := y;

or

y := x;

with

VAR x: CARDINAL; y: INTEGER;

have always been legal. Not only in Pascal, but also in Modula-2. The only additional effort needed by a compiler is to generate code that checks for range errors at run time as INTEGER and CARDINAL typically overlap only by the upper (positive) half of INTEGER.

BTW, I took out my old Classic Mac and tested with MacMETH, one of the best PIM compilers, which was first largely written by Niklaus Wirth himself. Compile and run-time errors are encountered exactly for the cases as described in my previous e-mail.

I do not see that here would be any type promotion necessary.

What you described there is type promotion, or as Wirth called it, type inclusion.

I have an email from Wirth from several years ago in which he described this as one of the things "that turned out to be a very bad idea" and as a consequence "corrected this in the latest version of Oberon".

The trouble with Wirth's "Programming in Modula-2" is that the language report contained therein leaves a lot of things unspecified. I am not sure what the ISO standard and its formal semantics definitions say, but Modula-2 is universally described and accepted as following name equivalence.

Name equivalence comes in two variants: loose and strict.

Generally, with name equivalence any two types that have different names are not compatible. With loose name equivalence, types defined as derivatives of others are compatible, with strict name equivalence they are not.

TYPE Celsius = REAL; Fahrenheit = REAL;

Under loose name equivalence Celsius, Fahrenheit and REAL are all compatible with each other.

Under strict name equivalence Celsius, Fahrenheit and REAL are all incompatible with each other.

PIM and ISO Modula-2 follow loose name equivalence.

Therefore, types INTEGER and CARDINAL are not equivalent and entities thereof *should* not be compatible.

Type conversions should be used.

VAR i : INTEGER; n : CARDINAL;
i := VAL(INTEGER, n);
n := VAL(CARDINAL, i);

A compiler needs to generate runtime code to check the values upon conversion, which could then indeed lead to a runtime error when there is no conversion equivalent for the value in the target type's value range.

This is what name equivalence means.

I do not doubt that there are compilers, even written by WIrth himself that don't consequently follow name equivalence, but they should have and Wirth definitely changed his mind on this away from type promotion/type inclusion semantics.

There are other things not clearly specified in the PIM language report such as the semantics of the unary minus which Wirth implemented in an EBNF deduced manner and most compiler implementors followed suit, but some compiler implementors chose to implement mathematically correct (MOCKA and ACK).

A language should be defined by a written specification, not by any implementation.

Clearly, if an implementation goes against mathematical rules, it cannot be considered correct simply because Wirth implemented it that way. If any of Wirth's compilers had implemented 1+1=3, we wouldn't consider that correct either.

I therefore postulate that the mathematically correct interpretation is the only correct way to implement the unary minus and all other compilers, including Wirth's compilers simply have this wrong.

If exceptions to first principle rules are desired, a specification should explicitly name them. For example

"Modula-2 follows loose name equivalence, except for assignments between (1) INTEGER and CARDINAL, (2) INTEGER and LONGINT, (3) LONGINT and CARDINAL, (4) REAL and LONGREAL, where implicit type conversion shall occur."

Absent such explicit exceptions in the specification, any such exceptions in a compiler *should* be regarded as wrong.

regards
benjamin



Attachment: smime.p7s
Description: S/MIME cryptographic signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]