home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / modula3 / 1144 < prev    next >
Encoding:
Text File  |  1993-01-24  |  4.0 KB  |  110 lines

  1. Newsgroups: comp.lang.modula3
  2. Path: sparky!uunet!gatech!concert!decwrl!src.dec.com!heydon
  3. From: heydon@src.dec.com
  4. Subject: Re: structural equivalence ==> no referential transparency
  5. Message-ID: <9301250046.AA01319@pebbles.pa.dec.com>
  6. To: comp.lang.modula3.usenet
  7. Sender: heydon@src.dec.com (Allan Heydon)
  8. Cc: sullivan@teal.csn.org
  9. Organization: DEC Systems Research Center
  10. X-In-Reply-To: Message of 24 Jan 93 16:52:42 GMT
  11.     from sullivan@teal.csn.org (Steve Sullivan)
  12.     <C1DAw0.ILz@csn.org>
  13. Date: Sun, 24 Jan 93 16:46:52 -0800
  14. X-Received: by pebbles.pa.dec.com; id AA01319; Sun, 24 Jan 93 16:46:52 -0800
  15. X-Mts: smtp
  16. Lines: 92
  17.  
  18. Steve Sullivan (sullivan@teal.csn.org) writes:
  19.  
  20. > Main proc:
  21. >  avec: ARRAY {1..100} OF LONGREAL;
  22. >  ...
  23. >  x = subproc( avec, k);
  24. >
  25. > subproc( avec, k):
  26. >   v = avec[k]         (* will access element k+1 *) 
  27. >
  28. > Although Modula 3 does have FIRST and LAST functions, they
  29. > seem more like afterthoughts than features:  even if the
  30. > above subproc were coded   v = avec[FIRST(avec)-1+k],
  31. > the subproc must still know the base of avec (hardcoded as "1").
  32. > And in any event "avec[FIRST(avec)-1+k]"
  33. > is far less clear to the reader than  "avec[k]".
  34.  
  35. Modula-3 makes a distinction between *fixed* an *open* arrays. A 
  36. fixed array declaration includes an index type, while an open array 
  37. declaration does not. The index type of an open array is [0..n-1], 
  38. where "n" is the run-time length of the array, so the first element 
  39. has index 0. You left out the type declarations for the "avec" and 
  40. "k" formal parameters to the "subproc" procedure in your example, 
  41. but I'm guessing that you meant for "avec" to be declared as an open 
  42. array like this (I've changed the names of the variables in "subproc" 
  43. to avoid confusion): 
  44.  
  45.   VAR
  46.     avec: ARRAY [1..100] OF LONGREAL;
  47.     k: [1..100];
  48.     x: LONGREAL;
  49.  
  50.   PROCEDURE subproc(avec2: ARRAY OF LONGREAL; j: CARDINAL): LONGREAL =
  51.     VAR v: LONGREAL; BEGIN
  52.       v := avec2[j];
  53.       RETURN v
  54.     END subproc;
  55.  
  56.   BEGIN
  57.     x := subproc(avec, k)
  58.   END Main.
  59.  
  60. As Steve points out, this code will assign "x" the value "avec[k+1]". 
  61. The problem is that he has not given a specification for "subproc". 
  62. If he did, it would state that "subproc" returns the value of "avec2" 
  63. at zero-based index "j". Therefore, to use the procedure correctly, 
  64. we must convert the actual argument "k" to a zero-based index in 
  65. the main body, like this: 
  66.  
  67.   BEGIN
  68.     x := subproc(avec, k - FIRST(avec))
  69.   END Main.
  70.  
  71. There is no way we can get around forcing such a conversion, since 
  72. the actual parameter "avec" and the formal parameter "avec2" have 
  73. different types. 
  74.  
  75. > However, for me their decision
  76. > not only invalidates all use of enumerated index sets (they
  77. > can't be used in subprocs), but destroys the usefulness of
  78. > subprocs for handling arrays of any sort.
  79.  
  80. Formal array arguments may certainly be fixed. For example, if we 
  81. had instead given the following signature to "subproc", in which 
  82. the array argument is a fixed array: 
  83.  
  84.   PROCEDURE subproc(avec2: ARRAY [1..100] OF LONGREAL; j: CARDINAL):
  85.       LONGREAL =
  86.     VAR v: LONGREAL; BEGIN
  87.       v := avec2[j];
  88.       RETURN v
  89.     END subproc;
  90.  
  91. then the arrays "avec" and "avec2" would have the same type, and 
  92. so we would not need to convert the index argument "k" in the call 
  93. to "subproc" by subtracting "FIRST(avec)". The limitation to using 
  94. fixed arrays as formal procedure arguments is that such procedures 
  95. can only be called with arrays of the specified length (or shape, 
  96. for multi-dimensional fixed arrays). 
  97.  
  98. See pgs 14-15 of Systems Programming with Modula-3 (SPwM3) for definitions 
  99. of fixed and open arrays; pg 24 for a definition of when one array 
  100. type is a subtype of another; and pg 26 for when one array type is 
  101. assignable to another. It is interesting to note that two one-dimensional 
  102. fixed arrays are subtypes of each other if they have the same base 
  103. type and the *number* of elements in their respective index types 
  104. are the same.
  105.  
  106. Allan Heydon                    heydon@src.dec.com
  107. DEC Systems Research Center            (415) 853-2142
  108. 130 Lytton Ave.
  109. Palo Alto, CA 94301
  110.