home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / object / 3067 < prev    next >
Encoding:
Internet Message Format  |  1992-07-30  |  6.4 KB

  1. Path: sparky!uunet!zaphod.mps.ohio-state.edu!rpi!bu.edu!transfer!ceylon!newshost!jm13
  2. From: jm13@gte.com (Joseph D. Morrison)
  3. Newsgroups: comp.object
  4. Subject: Re: Class methods (was: Re: How to design a data structure library)
  5. Message-ID: <JM13.92Jul30141213@bermuda.gte.com>
  6. Date: 30 Jul 92 18:12:13 GMT
  7. References: <1992Jul23.123609.11699@bony1.bony.com>
  8.     <5618@vexpert.dbai.tuwien.ac.at>
  9.     <GEOFF.92Jul27100601@wodehouse.flash.bellcore.com>
  10.     <graham.712278529@galois>
  11. Sender: news@ceylon.gte.com
  12. Organization: GTE Laboratories, Waltham, Massachusetts, USA
  13. Lines: 168
  14. In-reply-to: graham@maths.su.oz.au's message of 27 Jul 92 23:08:49 GMT
  15.  
  16. In article <graham.712278529@galois> graham@maths.su.oz.au (Graham Matthews) writes:
  17.  
  18. > Personally I think this is just awful. I want to write
  19. >
  20. >         c := a + b;        OR
  21. >         c := a union b;
  22. >
  23. > Any other notation is completely non-suggestive.
  24.  
  25. I think we should worry about semantics first and syntax later.
  26. Rather than worry about what code we should write to express
  27. addition or union (a syntactic matter) we should be trying to
  28. deal with semantic problems, such as how to express the concept
  29. of addition given the inherent asymmetry of dispatching on only
  30. the first argument.
  31.  
  32. I don't have any answers here, just some thoughts which I think
  33. are relevant to this discussion.
  34.  
  35. First thought: it is a good idea to distinguish between mutable
  36. and immutable objects.
  37.  
  38. Mutable objects are the ones in which identity corresponds with
  39. allocation (i.e. you could imagine using the memory address as an
  40. OID). Objects that we define using C++ classes, for example, are
  41. mutable.  They have state which can change over time. Two
  42. different mutable objects can have the same value.
  43.  
  44. Immutable objects are the ones in which identity corresponds with
  45. VALUE. Semantically, two allocated immutable "objects" with the
  46. same value are actually the same object. You could imagine an
  47. implementation in which all possible immutable objects are
  48. created before the program starts, and the constructor functions
  49. for immutable objects always return one of the already-created
  50. objects. Alternately, the constructor functions could always
  51. allocate new hunks of memory (like mutable objects), but the
  52. object identity comparison (i.e. EQ in lisp) would be modified so
  53. that in the case of immutable objects it would do value
  54. (byte-for-byte) comparison, rather than a pointer comparison.
  55.  
  56. In most OO systems, "objects" are always mutable, and integers
  57. are immutable. In a functional programming language, all objects
  58. are immutable.
  59.  
  60. As a final example to clarify things, one could imagine a mutable
  61. set type MSET, and an immutable set type ISET.
  62.  
  63. You can say:
  64.  
  65.   a = MSET (43, 21, 100);
  66.   b = MSET (43, 21, 100);
  67.  
  68. a and b are two different sets. We could then say:
  69.  
  70.   INSERT (a, 113);
  71.  
  72. This would MODIFY "a" to have a fourth element. "b" would be
  73. unchanged. Then you could imagine:
  74.  
  75.   a = ISET (43, 21, 100);
  76.   b = ISET (43, 21, 100);
  77.  
  78. a and b refer to the SAME SET OBJECT. What if you then say:
  79.  
  80.   INSERT (a, 113);
  81.  
  82. Well, this operation just doesn't make sense for an ISET.  For
  83. ISETs, INSERT must be a FUNCTION which returns another ISET,
  84. since there is no concept of "changing an object's value". So you
  85. could say:
  86.  
  87.   a = INSERT (a, 113);
  88.  
  89. Now "a" refers to a DIFFERENT ISET OBJECT; the set (43, 21, 100,
  90. 113). If you then typed:
  91.  
  92.   b = INSERT (b, 113);
  93.  
  94. then b and a would once again refer to the same ISET object (43,
  95. 21, 100, 113). At this point, the expression EQ (a, b) would be
  96. true. and so on...
  97.  
  98. So let's generalize the original discussion. Consider:
  99.  
  100.       a = b + c;
  101.  
  102. where b and c are of some immutable type, and "+" is defined to
  103. compute a new value of that type.
  104.  
  105. The first observation is that we don't really need to worry about
  106. whether "+" allocates memory. When the objects involved are
  107. immutable, memory allocation becomes an implementation detail. In
  108. fact, when all objects involved are immutable, we can talk about
  109. "creating" an instance OR we can talk about "selecting" an
  110. instance. Semantically it's kind of the same.
  111.   
  112. Note that this holds for integers. We can say that the +
  113. operation "makes" a new integer or "selects" an integer.
  114.  
  115. Finally, note that it doesn't make sense to have an operation
  116. like:
  117.  
  118.   b += c;
  119.  
  120. if b and c are immutable. Just like for integers, we don't want
  121. to write:
  122.  
  123.   2 += 3;
  124.  
  125. it just wouldn't make sense!
  126.  
  127. Now consider "a = b + c" for mutable objects. *Now* we have to
  128. decide, does "+" allocate a new object or not? There is no
  129. "correct" answer to this; you simply have to decide. You can
  130. imagine a function which takes two objects and allocates a third
  131. one, or a different function which takes two objects and modifies
  132. one of them. You can imagine a language which lets you define
  133. either or both of these, then lets the application writer choose
  134. which version she wants to use later on.
  135.  
  136. I'm not claiming to solve any deep problems in this discussion;
  137. but I am attempting to point out that in this "a+b" question,
  138. there are two completely different problems, depending on whether
  139. we are dealing with mutable or immutable objects. So if we solve
  140. the problem for integers, we haven't solved it for regular
  141. classes, and vice versa.
  142.  
  143. Another thought on this discussion; "conventional" OO languages
  144. dispatch on the first argument. If we therefore want to do away
  145. with functions and make all operations "methods", we seem to be
  146. stuck with an asymmetry which seems unnatural in operations such
  147. as "+".
  148.  
  149. One solution (my favourite) is to use CLOS-type dispatching on
  150. all the arguments. No more asymmetry. (Of course, all you
  151. "encapsulation sympathizers" will be unhappy with this idea.)
  152.  
  153. But actually, I suspect that many people who are worried about
  154. this "a+b" problem aren't even thinking about type dispatching. I
  155. mean, say you know the types of "a" and "b" at compile time. You
  156. still have this interface problem; do you write "a += b" or
  157. "c = a + b" or what?
  158.  
  159. So to conclude, here's my suggestion: for immutable objects,
  160. always write:
  161.  
  162. c = a + b;
  163.  
  164. (modulo colons, semicolons, parentheses, etc.)
  165.  
  166. For mutable objects, if "+" creates (allocates) a new object,
  167. write the same expression:
  168.  
  169. c = a + b;
  170.  
  171. For mutable objects, if "+" does not create a new object, but
  172. rather, modifies one of the argument objects, write:
  173.  
  174. a += b;
  175.  
  176. or its moral equivalent. Boy can I get long-winded :-)
  177. Ok, my fire extinguisher is poised and ready...
  178.  
  179.         Joe Morrison
  180. --
  181. GTE Laboratories Incorporated         Email: jmorrison@gte.com
  182. 40 Sylvan Road                        Phone: (617) 466-2614
  183. Waltham, Massachusetts, 02254
  184.