home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 213a.lha / Scheme / overview.doc < prev    next >
Text File  |  1996-02-14  |  14KB  |  409 lines

  1. ======================
  2. OVERVIEW OF THE SYSTEM
  3. ======================
  4.  
  5. Scheme
  6. Version 1.2
  7. 19-March-1988
  8.  
  9.  
  10. TOPICS
  11. ------
  12. STARTING THE SYSTEM
  13. INITIALIZATION FILE
  14. GARBAGE COLLECTION AND MEMORY MANAGEMENT
  15. INDEX TO EXAMPLE CODE
  16. MORE INFORMATION ABOUT SCHEME
  17. BUG REPORTS AND OTHER COMMUNICATION
  18.  
  19.  
  20.  
  21. ================================================================================
  22.  
  23. STARTING THE SYSTEM
  24. -------------------
  25.  
  26. The system may be run either from the Workbench (by double-clicking
  27. its icon) or from the CLI (by issuing its name from the command line).
  28.  
  29. The system requires approximately 285k bytes to start, of which there must
  30. be two contiguous blocks of 64k each.  Heap memory can be dynamically
  31. resized after startup; the total memory usage can be reduced to a minimum
  32. of approximately 185k.    The system's stacks (there are two in this
  33. implementation) are allocated internally, so the task may be started with
  34. AmigaDOS' default 4k stack.
  35.  
  36. Below, it is assumed that the Scheme system program is named "Scheme" and
  37. resides in some directory accessible in your CLI Path.
  38.  
  39. Here is a quick example of running the system, starting from the CLI:
  40.  
  41.     1> Scheme is fun!
  42.     Scheme
  43.     Version 1.2  19-March-1988
  44.     Implemented by Ed Puckett
  45.     MIT Branch PO
  46.     PO Box 61
  47.     Cambridge, MA  02139
  48.  
  49.  
  50.     :=> command-name
  51.     Scheme
  52.     :=> command-line
  53.     is fun!
  54.  
  55.     :=> (write command-line)
  56.     "is fun!\
  57.     "#u
  58.  
  59. The ":=> " prompt is issued by the system read-eval-print-loop.  This loop
  60. is interface with the Scheme system, and it does just what its name
  61. implies: it reads an expression, evaluates it, prints the result and loops
  62. back to read another expression.  (Some people colorfully refer to the
  63. read-eval-print-loop as a "Listener".)
  64.  
  65. Two variables (among others) are introduced in the above example:
  66. command-name and command-line.    These can be used by your programs to
  67. determine if the system was started from the Workbench or from the CLI,
  68. and to determine what arguments may have been issued on the CLI command line.
  69.  
  70. If run from the Workbench, the symbols command-name and command-line
  71. will both be bound to empty strings.  From the CLI, they are bound to
  72. repectively, the name of the executable file (usually "Scheme", unless
  73. you rename it) and the remainder of the command line.
  74.  
  75. Notice that applying write to command-line's value showed us that it
  76. includes a newline character.  The string representation of a newline is
  77. the string escape character \ followed by a newline.  This is why the rest
  78. of the string is on the next line.  The #u which appears after the closing
  79. double quote is the return value from write.
  80.  
  81. We can also see the individual characters that comprise this string:
  82.  
  83.     :=> (write (string->list command-line))
  84.     (#\i #\s #\space #\f #\u #\n #\! #\lf)#u
  85.  
  86. To leave the system, simply cause an end-of-file (by pressing CTRL-\) or
  87. else call the procedure abort-system:
  88.  
  89.     :=> (abort-system)
  90.     1>
  91.  
  92. See the section MORE INFORMATION ABOUT SCHEME for references to other
  93. literature on Scheme.  Extensions particular to this implementation are
  94. described in the file "ext.doc".
  95.  
  96.  
  97.  
  98. ================================================================================
  99.  
  100. INITIALIZATION FILE
  101. -------------------
  102.  
  103. Just prior to entering the read-eval-print-loop for the first time, the
  104. system attempts to open the file "S:Scheme-init.scm".  If this file exists,
  105. it is loaded before the first read-eval-print-loop prompt is issued.  You
  106. may put any valid Scheme expressions into this file.
  107.  
  108. Some uses for the initialization file:
  109.  
  110.     * Load your own frequently-used procedures.
  111.  
  112.     * Parse the command line, and take action on it.  For example,
  113.       you could evaluate each file specified on the command line and
  114.       then exit.  If no files were given, then you could do the normal
  115.       read-eval-print-loop.  This would be something like Unix's /bin/sh.
  116.  
  117.     * Start your own custom read-eval-print-loop.  See repl.scm for a
  118.       crude example.
  119.  
  120.  
  121.  
  122. ================================================================================
  123.  
  124. GARBAGE COLLECTION AND MEMORY MANAGEMENT
  125. ----------------------------------------
  126.  
  127. The garbage collector combines both mark/sweep and copying methods.
  128. Storage for strings and ports is allocated outside the heap memory (via
  129. AllocMem) and is collected in a mark/sweep fashion.  All other memory (such
  130. as that for pairs and vectors) is allocated from the heap, and is collected
  131. by the copying method.
  132.  
  133. The copying method is reasonably fast (this system collects about 13500
  134. pairs/second), and takes time proportional to the current amount of storage
  135. used (i.e., storage which is not currently garbage).
  136.  
  137. It has two major drawbacks.  One is that it destroys objects' locality of
  138. reference.  The other is that only half of the allocated memory is ever
  139. available for use.  This is because it maintains two equal-sized blocks
  140. of memory, a working block and a free block.
  141.  
  142. The locality issue does not really affect the Amiga since no virtual
  143. addressing is employed.  As for the amount of memory required, what the
  144. heck, we can always add more memory (!).
  145.  
  146. Seriously though, the copying method's speed was its major attraction.
  147. Typically, I see 1/4 second delays (for instance, in the middle of
  148. printing) if I see them at all.  Only when a lot of internal structures are
  149. allocated do the delays become very noticable.
  150.  
  151. Strings contain no references to other objects on the heap, and it seemed a
  152. shame to clog up the heap with them; each collection would move each string.
  153.  
  154. Ports, too, contain no heap references, but there was another reason for
  155. collecting them with mark/sweep.  Since the mark/sweep scans unused as well
  156. as used objects, unused ports are merely closed by the garbage collector
  157. during the sweep phase.  (It is by no means impossible to detect unused
  158. open ports in the heap, but the mark/sweep method seemed easier.)
  159.  
  160. Incidentally, the default error handler calls the garbage collector.  This
  161. aids in the following circumstance.  Suppose you are loading a file (with
  162. the primitive procedure "load"), and an error is detected in the file.  If
  163. the port associated with the file from which you were reading were left
  164. open, you would not be able to modify the file because it would be locked.
  165. So the error handler cleans out the registers, removing load's reference to
  166. the port, and calls the garbage collector which in turn closes the port,
  167. thereby unlocking the file.
  168.  
  169. Changing Memory Size
  170. --------------------
  171. Several procedures are provided by which you can dynamically change the
  172. size of the heap: extend-size, shrink-size, change-size and minimize-size.
  173. (See "ext.doc" for detailed descriptions of these.)
  174.  
  175. These procedures start by garbage collecting into the free block
  176. (extend-size skips this step).    Because the copying method compacts
  177. storage, it is then possible to determine the minimum amount of memory that
  178. the system must have.  Two new blocks of the requested size are now
  179. allocated, and another collection is performed into one of the new blocks.
  180. Now the old pair of blocks can be discarded, and the new ones installed in
  181. their place.
  182.  
  183. These routines will fail if there is not enough memory available for both
  184. the old and new pairs of blocks simultaneously.  Though a very conservative
  185. measure, it seems too risky to trust that a freed block can be reallocated
  186. if the new blocks can not be.
  187.  
  188. Finally, the heap management system will attempt to increase the size of
  189. the heap if heap storage is requested and none is available.  It currently
  190. adds increments of #x800 bytes (i.e. 256 pairs).  If the reallocation is
  191. unsuccessful, the system will abort.  (This needs to be changed so that
  192. you can at least decide!!!)
  193.  
  194. This naive strategy should instead attempt reallocation based on the amount
  195. ultimately needed.  Each heap reallocation is time-consuming, and may
  196. fragment the Amiga system memory so that further reallocations are
  197. impossible.  However, out-of-memory is currently detected at a quite low
  198. level, and it is tough to determine the ultimate need.
  199.  
  200. You may enjoy watching the memory on a graphically-displayed memory monitor
  201. as the system is trying to allocate a lot of memory.  You can cause this
  202. by calling minimize-size and then appending a long list to another list.
  203. Though it's probably not something you want to happen when you're doing
  204. real work, it's fun to watch this memory "shell game".
  205.  
  206.  
  207.  
  208. ================================================================================
  209.  
  210. INDEX TO EXAMPLE CODE
  211. ---------------------
  212.  
  213. HOP.scm
  214. -------
  215. This file contains useful higher-order procedures which I load into my
  216. system during initialization.  For example, repeat is useful for testing
  217. something repeatedly:
  218.  
  219.     :=> (repeat
  220.       (lambda ()
  221.         (do-test)        ; some time-consuming test
  222.         (display "."))      ; progress indicator
  223.       10)
  224.     ..........#u
  225.  
  226. The procedure "filter" allows you to select elements from a list for which
  227. a predicate returns true:
  228.  
  229.     :=> (filter even? '(0 1 2 3 4 5 6 7 8 9))
  230.     (0 2 4 6 8)
  231.  
  232. The procedure "repeated" forms the n-fold composition of a function.
  233. Here it is used to define multiplication in terms of addition:
  234.  
  235.     :=> (define (mult m n)
  236.       ((repeated (lambda (x) (+ n x)) m) 0))
  237.     mult
  238.  
  239.     :=> (mult 200 340)
  240.     68000
  241.  
  242.  
  243.  
  244. dump.scm
  245. --------
  246.  
  247. This file has many procedures and definitions pertinent to the system
  248. internals.  Careless use of these functions will probably cause a crash!
  249. That's why they're so fun!
  250.  
  251. The representation of many objects is noted in this file, and procedures
  252. for dumping things like environments are defined.
  253.  
  254.  
  255.  
  256. load-patches.scm
  257. ----------------
  258.  
  259. This file shows examples of how you may modify existing procedures.
  260. Its main functions are "cd" and "load".
  261.  
  262. Use "cd" to change the current directory.  Then, "load" will prepend this
  263. name when relative pathnames are given to it:
  264.  
  265.     :=> (cd "Scheme:tests")
  266.     #u
  267.  
  268.     :=> (cd)
  269.     Scheme:tests/    ; with no arguments, returns current directory
  270.  
  271.     :=> (load "xxx")
  272.     *** ERROR: could-not-open-port
  273.     Scheme:tests/xxx
  274.  
  275.     :=> (load "test-cwcc.scm")
  276.     #u
  277.  
  278.     :=> (load "test-tests.scm" "test-dwind.scm")
  279.     #u            ; more than one file can be specified; loaded in order
  280.  
  281.     :=> (load)
  282.     #u            ; reloads last specified list of files
  283.  
  284.  
  285.  
  286. repl.scm
  287. --------
  288. This file defines a custom read-eval-print-loop.  To start it, apply the
  289. function repl to a read function, an eval function and a print function.
  290. Typical choices for these are read, eval and display:
  291.  
  292.     :=> (repl read eval display)
  293.  
  294.     1=> (repl read eval display)
  295.  
  296.     2=> <eof>
  297.  
  298.     1=>
  299.  
  300. Each time it is invoked, it increases the level number displayed in the
  301. prompt.  Each time you exit a level, the number is decremented.
  302. These repl's execute in their own environments.  When you leave
  303. one, all of its definitions are lost.
  304.  
  305. Commands can be easily passed to AmigaDOS.  Simply enter the command
  306. prefixed with the character ~.    It is not necessary to separate the ~ and
  307. the rest of the command, and you needn't enter parentheses.  Everything up
  308. to the end of the line will be sent to AmigaDOS.
  309.  
  310.     1=> ~dir S:
  311.       .edrc                   Scheme-init.scm
  312.       Startup-Sequence
  313.  
  314.     1=>
  315.  
  316. These read-eval-print-loops install their own error handler:
  317.  
  318.     1=> )
  319.     Yeeow!  #u
  320.     Illegal right parenthesis
  321.     Interrupt flags/mask: #x0/#x2
  322.  
  323.     1=>
  324.  
  325. If you install your own error handler, you don't fall out of your subsystem
  326. just because you get an error.
  327.  
  328. All in all, the code in repl.scm is a hack-job.  It represents a lot of
  329. experimentation (actually playing) with the system.  For example, it would
  330. be much better to dynamically-wind the level number rather than using a
  331. global variable.  But it provides a decent example of fun things that you
  332. can do.
  333.  
  334.  
  335.  
  336. special-forms.scm
  337. -----------------
  338. This file gives an example of how to add new special forms to the system
  339. via the primitive procedure add-syntax!.  The special forms "let*" and
  340. "case" are defined.  Other special-forms manipulation procedures are
  341. delete-syntax! and get-syntax-definitions.
  342.  
  343.  
  344.  
  345. streams.scm
  346. -----------
  347. Streams are essentially lists whose "cdr" is delayed, i.e., it is not
  348. evaluated until its value is required, and then that value is stored for
  349. future accesses.
  350.  
  351. This file shows example streams functions.  After loading it, try the
  352. following:
  353.  
  354.     (display-stream ones)
  355.     (display-stream natural-numbers)
  356.     (display-stream fibonacci-numbers)
  357.     (display-stream prime-numbers)
  358.  
  359. These all generate infinite streams.  You can stop these by pressing CTRL-C.
  360. If you display the prime numbers twice, you will observe memoization in
  361. action -- the previously computed part of the stream come back quite
  362. quickly.
  363.  
  364. These functions use LOTS of memory if you display long portions of the
  365. streams.  The system will abort if it runs out of memory.
  366.  
  367.  
  368.  
  369. ================================================================================
  370.  
  371. MORE INFORMATION ABOUT SCHEME
  372. -----------------------------
  373.  
  374. Two wonderful sources of information on Scheme are:
  375.  
  376.     The book "Structure and Interpretation of Computer Programs"
  377.     by Harold Abelson and Gerald Jay Sussman with Julie Sussman.
  378.     Published by The MIT Press, Cambridge, Massachussets.
  379.  
  380.     The "Revised^3 Report on the Algorithmic Language Scheme"
  381.     Editors: Jonathan Rees and William Clinger.
  382.     MIT Artificial Intelligence Laboratory Memo 848a.
  383.  
  384. The bibliographies of these will direct you to a wealth of information.
  385.  
  386.  
  387.  
  388. ================================================================================
  389.  
  390. BUG REPORTS AND OTHER COMMUNICATION
  391. -----------------------------------
  392. If you find bugs, please let me know.  If possible, send code which will
  393. reproduce the bug, or at least a description of how the bug might be
  394. reproduced.  If the system crashes, it would help to know the GURU
  395. MEDITATION NUMBER and information about your system configuration.
  396. Crashes are most likely the result of the garbage collector getting hold of
  397. some untagged datum.
  398.  
  399. Suggestions and/or comments are welcome.
  400. Have fun!
  401.  
  402. Ed Puckett
  403. MIT Branch PO
  404. PO Box 61
  405. Cambridge, MA  02139
  406. (617) 536-8876
  407. qix@hx.lcs.mit.edu
  408.  
  409.