[comp.lang.modula2] some questions about modula2

dalal@topaz.rutgers.edu (Mukesh Dalal) (08/13/87)

I am beginner in modula2 programming. I am confused about some
features:

1. Is the type checking based on name equivalence or structure
equivalence? More specifically, Is the following construct allowed:

	Type
	 P= Record
		...
	    End;
	 Q = P;
If yes, can a variable of type P be assigned a value of type Q and
vice-versa?

2. Can a global module export an item which it has imported?

3. Can the definition of a global-module M refer to a type P which is
neither declared there (either as opaque or transparent) nor is
imported from some other module but is defined/imported in the
corresponding implementation module of M?

4. Is the following construct allowed:

	Definition:
		procedure abc (..);
	Implementation:
		var x: ...;
		procedure abc (...);
			...
			x := ...;
		end;
		...

i.e., can an exported procedure make a change in a variable defined in
the module exporting that procedure?

I suspect that the ans to 4 is yes, but I want to make sure.


Thanks in advance for helpful responses.
-- 

Mukesh Dalal				Voice: (201)-878-1763
Dept. of Computer Sc. (Rutgers)		Net: dalal@topaz.rutgers.edu

	"To every action, there is an OVER-REACTION" 

hiebeler@csv.rpi.edu (David Hiebeler) (08/14/87)

References:


I will answer what I can in no order whatsoever.
4.  Exported procedures may reference variables which are 'hidden' in that
module, i.e. not exported.  I have a random # generator with a procedure
which looks something like this.
PROCEDURE Rndom():CARDINAL;
(* Return a CARDINAL between 0 and 999 *)
BEGIN
  Seed := sqrt(Seed);
  IF Seed<10 THEN Seed := (Seed+2)*Seed*Seed*Seed (* keep Seed from *)
						   (* getting too small *)
  END; (* IF *)
  RETURN TRUNC((Seed-TRUNC(Seed))*1000) (* or something like that, I forget *)
				      (* to get the first 3 decimal places *)
END Rndom;
-- Note this will update the Seed, which is invisible to the calling
module.  (I also have a procedure InitSeed
PROCEDURE InitSeed(NewSeed:REAL);
BEGIN
  Seed := NewSeed;
  IF Seed<10 THEN Seed := (Seed+11)*(Seed+1)*(Seed+1)*(Seed+1)
  END;  (* IF *)                 (* or something like this *)
END InitSeed;


2.  (Was it 2?)  A definition module referencing a type declared in the
implementation module.  -- Why would you want to do this?  It must be
a 'hidden' type, or you would declare it to be exported in the definition
module.  The only reason you need mention it in the definition is if
say, a procedure needs a var of that type as a parameter, in which
case, the type would have to be exported anyways.

                             -D.H.
----
David Hiebeler       hiebeler@csv.rpi.edu
Chatham, NY         "Illusions, Richard!  Every
(also Troy, NY)      bit of it illusions!"

hiebeler@csv.rpi.edu (David Hiebeler) (08/14/87)

References:


Oops.  I answered 3 and called it 2.  Oh well.
I just compiled something which included these type definitions:
TYPE P = RECORD
	   x,y : integer;
         END;
     Q = P;
--and the compiler gave me no trouble about it.

2. (Really 2 this time) Modules can export anything, even things which
they have imported.  I did this in order to make an InOut module, as
we did not have one here.  Instead, we had 'io' which has some C-type
routines (printf, scanf, etc. along with the type File and the vars
Input & Output).  So my InOut module imports File,Input,Output,Open,
and Close from 'io' and simply exports those things 'as is', plus
my own set of procedures (WriteLn, WriteReal, etc.).

I hope that helps some!  I'm not very organized, as you might have noticed.

                             -D.H.
----
David Hiebeler       hiebeler@csv.rpi.edu
Chatham, NY         "Illusions, Richard!  Every
(also Troy, NY)      bit of it illusions!"

randy@oresoft.UUCP (Randy Bush) (08/18/87)

In article <13951@topaz.rutgers.edu> dalal@topaz.rutgers.edu (Mukesh Dalal) writes:
>1. Is the type checking based on name equivalence or structure
>equivalence? 

Name equivalence.  E.g.
  TYPE t = [0..42];
  VAR  a : t;
       b : [0..42];
  PROCEDURE f ( VAR p : t );
       BEGIN END f;
  f(a) (*legal*);
  f(b) (*illegal*);

> More specifically, Is the following construct allowed:
>	Type
>	 P= Record
>		...
>	    End;
>	 Q = P;

Yes, as the two types are IDENTICAL, as Q is of type P.

>2. Can a global module export an item which it has imported?

  DEFINITION MODULE m;
  FROM Foo IMPORT Bar;
  EXPORT QUALIFIED Bar,  (* is not legal*)
                   Feen; (* is legal *)
  TYPE Feen = Bar;
  END m.

>3. Can the definition of a global-module M refer to a type P which is
>neither declared there (either as opaque or transparent) nor is
>imported from some other module but is defined/imported in the
>corresponding implementation module of M?

No.

>4. Is the following construct allowed:
>
>	Definition:
>		procedure abc (..);
>	Implementation:
>		var x: ...;
>		procedure abc (...);
>			...
>			x := ...;
>		end;
>		...

Yes.
-- 
Randy Bush, Compiler Group, Oregon Software, Portland Oregon (503) 245-2202
uucp: ..!tektronix!oresoft!randy       Telemail: RBush     Fidonet: 1:105/6

hiebeler@csv.rpi.edu.UUCP (08/18/87)

The f(a) and f(b) constructs are allowed in the
modula2 compiler I am using.  The short program module compiles AND runs
with no problems.
  where's modula3?!?

                             -D.H.
----
David Hiebeler       hiebeler@csv.rpi.edu
Chatham, NY         "Illusions, Richard!  Every
(also Troy, NY)      bit of it illusions!"

cca@newton.physics.purdue.edu (Charles C. Allen) (08/20/87)

> >2. Can a global module export an item which it has imported?
> 
>   DEFINITION MODULE m;
>   FROM Foo IMPORT Bar;
>   EXPORT QUALIFIED Bar,  (* is not legal*)
>                    Feen; (* is legal *)
>   TYPE Feen = Bar;
>   END m.

Could someone please show where this is explicitly disallowed in
Wirth?  The second edition is unclear, and nobody around here seems to
have the third edition (I'm rather reluctant to buy it with a
standards committee on the loose).

If it IS specifically disallowed, I'm going to be dissappointed.  It
is a good way of splitting up large modules into smaller sub-modules,
then packaging the results together.  For instance:

	DEFINITION MODULE listLife;
	EXPORT QUALIFIED ListNew, ListEmpty, ListFree;
	...
	END listLife.

	DEFINITION MODULE listAdd;
	EXPORT QUALIFIED ListAddFirst, ListAddLast;
	...
	END listAdd.

	DEFINITION MODULE listRemove;
	EXPORT QUALIFIED ListRemoveFirst, ListRemoveLast;
	...
	END listRemove.

	DEFINITION MODULE Lists;
	FROM listLife IMPORT ListNew, ListEmpty, ListFree;
	FROM listAdd IMPORT ListAddFirst, ListAddLast, ...;
	FROM listRemove IMPORT ListRemoveFirst, ListRemoveLast, ...;
	EXPORT QUALIFIED
		List,
		ListNew, ListEmpty, ListFree,
		ListAddFirst, ListAddLast,
		ListRemoveFirst, ListRemoveLast;
	TYPE List;
	END Lists.

The user just imports what he wants from the Lists module.

This is from a real-life example.  The actual code has about eight
sub-modules, with an average of 5 or 6 functions per sub-module.
Stuffing it all into one file (the only other alternative) makes the
code less readable, and greatly increases the compilation time when
changes are made.

Charlie Allen		cca@newton.physics.purdue.edu

randy@oresoft.UUCP (Randy Bush) (08/26/87)

In article <803@newton.physics.purdue.edu> cca@newton.physics.purdue.edu (Charles C. Allen) writes:
>> >2. Can a global module export an item which it has imported?
>> 
>>   DEFINITION MODULE m;
>>   FROM Foo IMPORT Bar;
>>   EXPORT QUALIFIED Bar,  (* is not legal*)
>>                    Feen; (* is legal *)
>>   TYPE Feen = Bar;
>>   END m.
>
>Could someone please show where this is explicitly disallowed in
>Wirth?  The second edition is unclear, and nobody around here seems to
>have the third edition (I'm rather reluctant to buy it with a
>standards committee on the loose).

PIM2-2, page 158, Chap 11, paragraph 1

"The export list specifies all identifiers of objects declared within the
module and used outside."

As import is not a declaration, an imported object may not be directly
exported.  This point has been discussed and worried over many times and
places, and, admittedly, the current resolution is less than satisfactory.

Also note that, as PIM2-3 does not provide export from a definition module,
the question becomes moot.
-- 
Randy Bush, Compiler Group, Oregon Software, Portland Oregon (503) 245-2202
uucp: ..!tektronix!oresoft!randy       Telemail: RBush     Fidonet: 1:105/6