home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / prolog / 2235 < prev    next >
Encoding:
Text File  |  1992-12-14  |  4.2 KB  |  95 lines

  1. Newsgroups: comp.lang.prolog
  2. Path: sparky!uunet!gumby!destroyer!cs.ubc.ca!fornax!gregory
  3. From: gregory@cs.sfu.ca (Gregory Sidebottom)
  4. Subject: Re: Array implementations?
  5. In-Reply-To: joachim@ecrc.de's message of Mon, 14 Dec 1992 11:04:10 GMT
  6. Message-ID: <1992Dec14.185540.2981@cs.sfu.ca>
  7. Organization: CSS, Simon Fraser University, Burnaby, B.C., Canada
  8. References: <FAWCETT.92Dec10103127@iron.cs.umass.edu> <1992Dec14.110410.25384@ecrc.de>
  9. Date: Mon, 14 Dec 1992 18:55:40 GMT
  10. Lines: 83
  11.  
  12. In article <1992Dec14.110410.25384@ecrc.de> joachim@ecrc.de (Joachim Schimpf) writes:
  13.  
  14.    In article 92Dec10103127@iron.cs.umass.edu, fawcett@iron.cs.umass.edu (Tom Fawcett) writes:
  15.    >
  16.    >Michael Covington:
  17.    >>    Here's a reasonable substitute for a 15-element array:
  18.    >> 
  19.    >>      f(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o)
  20.    >> 
  21.    >>    Use 'arg' to access elements by numbered position.
  22.    >> 
  23.    >>    Limitations:
  24.    >>      (1) Intriniscally 1-dimensional (but you can make an array of arrays);
  25.    >>      (2) Some maximum number of elements (maybe 256).
  26.    >
  27.    >
  28.    >Greg Sidebottom:
  29.    >
  30.    >> I would add:
  31.    >>  (3) element update is O(size of array) in time and space, which is
  32.    >>      much worse than O(log size of array) for trees
  33.    >
  34.    >I think element access and update (via arg/3) is constant time, at least in
  35.    >Quintus Prolog.  The QP library file arrays.pl has such an implementation, and
  36.    >states that fetch and access are constant.
  37.  
  38. Yes, *access* is in constant time, *update* is not.  The only way to
  39. way to change a value in an array in Prolog implemented using
  40. structures is to copy it except put the new element where the old one
  41. was.  This takes linear time and space.  With trees, it takes log time
  42. and space on average, since you only have to traverse and copy one
  43. path in the tree.
  44.  
  45. Now you may be able to hack constant time access and update arrays
  46. into Prolog using assert/retract or by linking with some C code and
  47. using assignment, but that wouldn't be very Prolog like.  You would
  48. lose most of the advantages of Prolog and might as well use a
  49. procedural language.  
  50.  
  51. There is some research in the functional programming language
  52. community into providing logical versions of arrays with constant time
  53. access and update for the way arrays are usually used.  The idea is
  54. that if you change a value in an array by making a new copy, usually,
  55. you don't need to look at the old copy ever again.  If the compilers
  56. can recognize this 'single threaded' use of arrays, it safely
  57. implement the array operations using destructive assignment.  An
  58. approach for non-single threaded uses is to keep the original array
  59. and each reference to the array is associated with a set of changes
  60. that have taken place.  This can also can give close time constant
  61. time and space performance in many applications.  
  62.  
  63.    >
  64.    >> Don't think I am Prolog bashing, Prolog is my favorite language.  But
  65.    >> I think if you really need constant time access and update arrays, you
  66.    >> should probably use some other language.
  67.  
  68.    Let's get that straight: Prolog _has_ constant time access arrays,
  69.    they happen to be called structures, as Michael Covington has
  70.    pointed out correctly. 
  71.  
  72.    Now 'access' in Prolog means unification, which subsumes 'read' and
  73.    'assign once'. 
  74.  
  75. When I hear arrays, I expect access *and* update.  After all, many
  76. applications require the update of arrays as well.
  77.  
  78.    What you don't have in Prolog, neither in arrays nor elsewhere, is
  79.    destructive assignment. As a consequence, in a situation where you want a
  80.    slightly modified  version of an array that you already have (_and_
  81.    you don't need to keep the old version) you have to make a copy
  82.    where you would otherwise destructively modify the old array. 
  83.  
  84.  
  85.  ---------------------------------------------------------------------------
  86.     Joachim Schimpf                                 Email   joachim@ecrc.de 
  87.     European Computer-Industry Research Centre      Phone   +49 89 92699 111
  88.     Arabellastrasse 17, D-8000 Munich 81, Germany   Fax     +49 89 92699 170
  89.  
  90. Greg
  91. -- 
  92. Greg Sidebottom,                                      | There's not a word that
  93. Expert Systems Laboratory, Centre for Systems Science | goes here that rhymes
  94. Simon Fraser University, Burnaby BC V5A 1S6, Canada   | with anything. -CVB
  95.