home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / ada / 2410 < prev    next >
Encoding:
Internet Message Format  |  1992-08-23  |  4.1 KB

  1. Path: sparky!uunet!cis.ohio-state.edu!ucbvax!agate!linus!linus.mitre.org!linus!mbunix!eachus
  2. From: eachus@Dr_No.mitre.org (Robert I. Eachus)
  3. Newsgroups: comp.lang.ada
  4. Subject: Re: MI - clutching at straws
  5. Message-ID: <EACHUS.92Aug23173050@Dr_No.mitre.org>
  6. Date: 23 Aug 92 22:30:50 GMT
  7. References: <1992Aug23.171350.27117@Urmel.Informatik.RWTH-Aachen.DE>
  8. Sender: news@linus.mitre.org (News Service)
  9. Organization: The Mitre Corp., Bedford, MA.
  10. Lines: 82
  11. In-Reply-To: pk@rwthi3.informatik.rwth-aachen.de's message of Sun, 23 Aug 92 17:13:50 GMT
  12. Nntp-Posting-Host: dr_no.mitre.org
  13.  
  14. In article <1992Aug23.171350.27117@Urmel.Informatik.RWTH-Aachen.DE> pk@rwthi3.informatik.rwth-aachen.de (Peter Klein) writes:
  15.  
  16.    Consider the following example: I start with an abstract data type
  17.    for a graph.  Now I augment the basic type in several independant
  18.    ways, let's say to make attributed, node- and edge-labeled graphs.
  19.    Finally, I would like to have some special graphs, e.g. abstract
  20.    syntax graphs or binary trees, which combine one or more of the
  21.    above extensions. How can I express this without violating the
  22.    abstractness of the involved data types? Maybe somebody's got some
  23.    clever answer. Otherwise, I will keep thinking that MI is sometimes
  24.    appropriate.
  25.  
  26.    Okay, challenge accepted...
  27.  
  28.    First, let's create the basic graph package.  The "normal" way to
  29. do this in Ada is to use a model where different graphs are different
  30. instances of a generic package, but other models are possible.  So
  31. lets use a generic which exports an abstract type...
  32.  
  33.    generic
  34.      type Node_Data is private;
  35.      type Edge_Data is private;
  36.    package Graphs is
  37.  
  38.      type Node is private;
  39.      type Edge is private;
  40.      type Graph is private;
  41.  
  42.      function Data (N: in Node) return Node_Data;
  43.      function Data (E: in Edge) return Edge_Data;
  44.      procedure Set_Data(N: in out Node; ND: in Node_Data);
  45.      procedure Set_Data(E: in out Edge; ED: in Edge_Data);
  46.      -- No graph parameter needed implicit in the node or edge.
  47.  
  48.      function Create return Graph;
  49.  
  50.      function Create(ND: in Node_Data G: in Graph) return Node;
  51.      -- Need to specify the graph.
  52.  
  53.      function Create(From, To: Node; ED: in Edge_Data) return Edge;
  54.      -- Graph come from the nodes which must belong to the same graph.
  55.  
  56.      --- etc,.
  57.   end Graph;
  58.  
  59.     In Ada 9X, I can play several games with this, such as making the
  60. graph type a tagged type. (And playing games in the body to make
  61. derivation work.)  But to do the type of thing you want to do, using a
  62. tagged types for the Edge_Data and Node_Data types may suffice.  I
  63. could also provide separate directed and undirected edge types, but it
  64. is probably better to leave the end of the edge distinguished at this
  65. level, and conceal this fact if necessary.
  66.  
  67.     In any case now most of the augmentations are natural and done by
  68. different instantiations.  The special graphs, such as binary trees,
  69. would be done by instantiating the GRAPHS package then adding
  70. additional operations at a higher level.  In Ada 9X this can be done
  71. by writing a binary tree generic which takes an instantiation of
  72. GRAPHS as a parameter.  In Ada 83, you can do the same sort of thing,
  73. but the declaration gets much messier.  (All of the visible types and
  74. operations of the GRAPHS package must be passed as individual generic
  75. parameters.)
  76.  
  77.     Now it is possible to decorate a single GRAPHS instance with all
  78. of the various special graph add ons.  In general, many of the
  79. combinations wouldn't make sense, but that is an issue (in Ada) for
  80. the programmer not the compiler.
  81.  
  82.     I could ring a lot of changes on the theme, but in general, the
  83. lesson is that, IN ADA, anything that might possibly be done with
  84. multiple inheritance is probably better done with generics.  Notice
  85. that in this example, I went out of my way to make single inheritance
  86. possible.  A pure generic solution seems much more appropriate.  This
  87. says nothing about whether or not MI is useful in other languages,
  88. just that there is no reason to add it to Ada.
  89. --
  90.  
  91.                     Robert I. Eachus
  92.  
  93. with STANDARD_DISCLAIMER;
  94. use  STANDARD_DISCLAIMER;
  95. function MESSAGE (TEXT: in CLEVER_IDEAS) return BETTER_IDEAS is...
  96.