home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / pop / 216 < prev    next >
Encoding:
Internet Message Format  |  1993-01-24  |  14.2 KB

  1. Xref: sparky comp.lang.pop:216 comp.compilers:2243
  2. Newsgroups: comp.lang.pop,comp.compilers
  3. Path: sparky!uunet!think.com!spdcc!iecc!compilers-sender
  4. From: axs@cs.bham.ac.uk (Aaron Sloman)
  5. Subject: Compile-time vs Run-time
  6. Reply-To: axs@cs.bham.ac.uk (Aaron Sloman)
  7. Organization: School of Computer Science, University of Birmingham, UK
  8. Date: Sun, 24 Jan 1993 03:59:05 GMT
  9. Approved: compilers@iecc.cambridge.ma.us
  10. Message-ID: <93-01-170@comp.compilers>
  11. Keywords: pop-11
  12. Sender: compilers-sender@iecc.cambridge.ma.us
  13. Lines: 266
  14.  
  15.  -- Introduction
  16.  -- A minimal concept of compilation
  17.  -- Forms of interaction at run time
  18.  -- -- Case a: compile then execute.
  19.  -- -- Case b: alternating compilation and execution:
  20.  -- -- Case c: nested as well as alternated compilations and executions:
  21.  -- -- Case d: Merging compilation streams
  22.  -- -- Case e: allowing multiple interleaved compilation streams.
  23.  -- -- Case f: Time-shared concurrent streams:
  24.  -- To summarise:
  25.  -- Notes and qualifications
  26.  
  27. -- Introduction
  28.  
  29. There has been considerable discussion recently about dynamic vs lexical
  30. binding of variables in comp.lang.pop
  31.  
  32. My impression is that there are more distinctions between different
  33. variable binding schemes than have so far been clearly defined in the
  34. discussion, and I thought it might be useful to list them, if only because
  35. Pop-11 offers more choices to the programmer than any other language I
  36. know of. (Maybe this should be a chapter in the Pop-11 primer.)
  37.  
  38. Before I present the options regarding variable bindings I need to clarify
  39. some distinctions concerning compile time, run time, and compilation
  40. streams, as there are some issues concerning variable binding that concern
  41. only compile time, some concern only run time, and some involve relations
  42. between the two. These distinctions get complicated when there are
  43. different compilation streams in the same process.
  44.  
  45. This article merely goes into the compile-time/run-time distinction and
  46. the notion of a compilation stream. In a later article, when I have time,
  47. I'll return to variable binding.
  48.  
  49. I've cross-posted to comp.compilers in case someone who knows about other
  50. languages than Pop wishes to comment on the generality of my remarks.
  51.  
  52. -- A minimal concept of compilation
  53.  
  54. In what follows I shall talk about source code being "compiled",
  55. independently of whether it is compiled all the way to machine code, or to
  56. some intermediate interpreted form (e.g. lisp-like parse trees, or some
  57. interpreted intermediate machine language), or whether the source code is
  58. stored directly in the machine as strings of text and interpreted, or
  59. whether it is compiled into an object file which has to be linked and
  60. loaded before it can be run.
  61.  
  62. In this sense, compiling is just the process of taking source code from a
  63. file or from the terminal and storing it in the machine in some form in
  64. which it can then be run. This process can include compiling whole
  65. procedure definitions, or compiling individual commands or declarations
  66. such as "let X = 99", in Basic or "vars X = 99" in Pop-11.
  67.  
  68. -- Forms of interaction at run time
  69.  
  70. Before I go on to distinguish various questions about access to variables,
  71. I need to clarify an important notion, namely that the language allows the
  72. programmer to interact with programs at run time using the original
  73. programming language. Unfortunately this is not a simple concept, as there
  74. are several slightly different cases to be considered.
  75.  
  76. The question is whether there is a clearly separate phase in which
  77. source-code programs are read in ("compile time"), after which programs
  78. can only be executed ("run time"), or whether new source code can be added
  79. to the running system at any time, including "top level" source code
  80. instructions such as "print the value of X" or instructions to alter the
  81. contents of datastructures. Different cases will be distinguished.
  82.  
  83. -- -- Case a: compile then execute.
  84.  
  85. In most programming language systems (e.g. C, Pascal, Fortran) there is a
  86. sharp distinction between compile time and run time and they cannot be
  87. alternated or interleaved: for any program there is first a process of
  88. compilation, and then later on the program is run, perhaps on many
  89. occasions in different contexts. Within this paradigm, no program
  90. execution is possible till the whole program has been compiled (whether to
  91. machine code or an interpreted intermediate form), and once program
  92. execution has begun no more code can be added.
  93.  
  94. In such a system, it is not possible for the user to interact with the
  95. running system unless the program contains a procedure that reads in text
  96. typed by the user (or responds to mouse actions, or whatever), and
  97. performs appropriate actions. I.e. a special "command interpreter" may be
  98. part of the program, and it will generally allow a command language that
  99. is different from the programming language used to create the program.
  100.  
  101. -- -- Case b: alternating compilation and execution:
  102.  
  103. By contrast, in BASIC, and many AI languages (Lisp, Prolog, Pop-11) and
  104. functional languages (Scheme, ML, Miranda) a procedure to support
  105. interaction is provided by the language system itself in the form of a
  106. built in command interpreter or an incremental compiler that remains
  107. available at run time.
  108.  
  109. In its simplest form the compiler accepts, either from the user or from a
  110. specified file, commands which can either add declarations or procedure
  111. definitions or give commands to run system or user procedures.  Such a
  112. compiler simply reads in definitions, declarations and commands and obeys
  113. them, always returning to the top level read loop whenever something has
  114. been completed. Sometimes this is referred to in connection with Lisp as a
  115. "read-eval-print" loop. In this case, compilation and execution are
  116. alternated.
  117.  
  118. -- -- Case c: nested as well as alternated compilations and executions:
  119.  
  120. In more sophisticated systems, compiling and running can be nested as well
  121. as alternated. For instance many AI systems allow the compiler to be
  122. invoked as a subroutine by user procedures. Then besides the top-level
  123. invocation, there is a new invocation of the compiler. That in turn can
  124. read in new commands which cause procedures to run which invoke the
  125. compiler. In that case there can be several coexisting "compilation
  126. streams", a point I'll return to later. By contrast if the compiler is
  127. always a top-level process, which continues running after each command is
  128. completed, then there is always only one compilation stream.
  129.  
  130. Nesting is useful in several different contexts, e.g.
  131.  o  The decision to compile some additional files can be taken
  132.     by a user procedure at run time, depending on task requirements.
  133.  o  The process of compiling one file can cause the compiler to be
  134.     invoked to compile another file. The latter process will then have
  135.     its own compilation stream, in most languages.
  136.  o  A learning program can create and compile new procedures on the
  137.     basis of what it has learnt.
  138.  o  When testing programs it is sometimes useful to enter a "break" in
  139.     which the compiler is invoked to give the user a chance to type in
  140.     commands to interrogate values of variables, contents of
  141.     datastructures, the current call stack, etc. Alternatively this may
  142.     be required for the application, e.g. if the user is allowed to
  143.     tailor the application by typing in procedures to be invoked in
  144.     certain circumstances.
  145.  
  146. When compilation streams are nested, compilation process C1 may cause
  147. procedure P1 to be invoked which then invokes compilation process C2,
  148. which may run P2, etc. P1 will not resume execution until C2 is complete.
  149. Similarly C1 will not continue until procedure P1 is complete.
  150.  
  151. -- -- Case d: Merging compilation streams
  152.  
  153. Note that Poplog Pop-11 allows different compilation streams to be merged
  154. into a single stream using #_INCLUDE (or the macro "include" which copes
  155. with search lists for directories of files to be "included").
  156.  
  157. The advantage of allowing merged compilation streams as well as nested
  158. streams is that declarations that are normally local to a compilation
  159. stream, but which need to be shared between different libraries, need not
  160. be copied into all those libraries. Instead a single file contains the
  161. declarations, and it can be invoked by several different files. This is
  162. analogous to the use of #include in C, and the use of "source" in the Unix
  163. Cshell.
  164.  
  165. Note that the reason why this facility is required in Pop-11 is that there
  166. are certain types of variables that cannot be shared across different
  167. compilation streams, namely file-local, or stream-local lexical variables.
  168. Thus if such declarations are encountered in a in a file whose compilation
  169. is nested they will not be inherited by the calling environment.
  170.  
  171. Similarly if a unix shell script is run, the calling environment will not
  172. pick up any environment variables declared in the shell script, nor any
  173. directory changes, whereas using "source" to merge the script with the
  174. current shell overcomes this.
  175.  
  176. It is interesting that the need for the merging facility became apparent
  177. only after the introduction of lexical scoping at the level of whole
  178. files. The need would have been apparent earlier had there been other
  179. things local to the current compilation stream, such as the current
  180. directory.
  181.  
  182.  
  183. -- -- Case e: allowing multiple interleaved compilation streams.
  184.  
  185. A yet more sophisticated system can support multi-threading, or
  186. lightweight processes, which can resume or suspend one another. In that
  187. case procedure P1, instead of invoking C2 as a sub-routine may start it up
  188. as an independent compilation process. Then at some point C2 may be
  189. suspended, and P1 resumed. If P1 exits, then compilation process C1 is
  190. resumed. After a while it may be suspended and C2 resumed. For example, C1
  191. and C2 may run in different windows, and a mouse or keyboard event could
  192. be used to determine which compilation stream to use next to read in
  193. commands. So the processes C1 and C2 are then interleaved. (This is used,
  194. for example, by "immediate mode" in the Poplog editor, VED, where
  195. interleaving allows you to suspend a procedure definition or command,
  196. switch to another command stream to give commands to gain information
  197. about the language or the current state of the system, then return and
  198. complete the previous command.) Having different concurrent compilation
  199. streams in different windows for different purposes, can be helpful to the
  200. user, even if, in principle, it could all be done in one window using
  201. nested commands. Another case is where the system (like Poplog) actually
  202. supports different high level languages, in which case different
  203. concurrent compilation streams can be used for the different languages.
  204.  
  205.  
  206. -- -- Case f: Time-shared concurrent streams:
  207.  
  208. A yet more sophisticated system will allow "backgrounding" - i.e. while
  209. one process continues running "in the background", e.g. compiling large
  210. files, another process reacts to user commands. This requires some kind of
  211. scheduler which allocates time-slices between the different compilation
  212. streams and the processes they generate. In Pop-11 a scheduler is not
  213. built in, but the combination of timed interrupts and the process
  214. mechanism makes it fairly easy to implement. (See HELP ACTOR)
  215.  
  216. -- To summarise:
  217.  
  218. a. Simple systems allow only compilation then execution
  219. b. Interactive language systems allow alternating compilation and
  220.    execution processes.
  221. c. Systems in which the compiler is itself a procedure allow nested
  222.    compilations as well as alternating compilation and execution.
  223. d. Systems that allow compilation streams involving different files
  224.    to be merged.
  225. e. Systems that support multi-processing, permit interleaved compilation
  226.    streams, as well as nested and alternating compilation and execution
  227.    processes.
  228. f. A multi-processing system with a scheduler allows process switching
  229.    on a time-sliced basis instead of waiting for an active stream to
  230.    complete a sub-task and pause before another one can be resumed.
  231.  
  232.  
  233. -- Notes and qualifications
  234.  
  235. [Note 1: A non-interactive system that allows object files to be
  236. dynamically linked in while a program is running blurs the distinction
  237. between programs that allow interaction at run time and those that don't.
  238. If, for example, a running program checks every now and again whether you
  239. have created a new object file called "commands.o" and if so takes steps
  240. to link it in (possibly after giving it a unique name), and then runs its
  241. top level procedure you have a sort of run time command interpreter,
  242. though it may feel clumsy to use.]
  243.  
  244. [Note 2: The availability of a debugger, especially if it can be invoked
  245. after a program has started running, can also blur the distinction, if it
  246. provides commands for interrogating variables and data-structures, or
  247. calling procedures. If the debugger allows commands to be given in the
  248. same language as the original program, then it fits one of the previous
  249. categories. Conversely, languages that are fully interactive and allow
  250. nested compilation streams provide their own debugging facilities.]
  251.  
  252. [Note 3: There are operating system command languages like the Unix shell
  253. languages that support all the above facilities. For instance, a shell
  254. command file can spawn a new shell. Two windows on the same machine can
  255. run two shells concurrently.]
  256.  
  257. [Note 4: I have so far written as if variable binding environments, or
  258. identifier scopes, are tied entirely to compilation streams. But that is
  259. not the case, since when variables are local to a procedure the scope may
  260. be restricted to the portion of the compilation stream during which the
  261. procedure is being compiled, and in addition, if the same procedure is
  262. concurrently active more than once in a procedure calling hierarchy
  263. different bindings for those variables can be associated with each
  264. activation. Thus if the variable n is local to a recursive definition of
  265. factorial, then calling factorial(5) may create 5 different activations,
  266. in which the value of n is respectively 5, 4, 3, 2 and 1. This point is
  267. common to both lexical and dynamic binding, though shallow dynamic binding
  268. allows temporary sharing of activation values.]
  269.  
  270. Later, I hope to turn back to the discussion of different variable binding
  271. regimes. Meanwhile I'd welcome comments on all this. Have I missed
  272. anything important?
  273. -- 
  274. Aaron Sloman,
  275. School of Computer Science, The University of Birmingham, B15 2TT, England
  276. EMAIL   A.Sloman@cs.bham.ac.uk  OR A.Sloman@bham.ac.uk
  277. Phone: +44-(0)21-414-3711       Fax:   +44-(0)21-414-4281
  278. -- 
  279. Send compilers articles to compilers@iecc.cambridge.ma.us or
  280. {ima | spdcc | world}!iecc!compilers.  Meta-mail to compilers-request.
  281.