PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/doc/smithnotes.tex

http://github.com/barak/scheme2c
LaTeX | 594 lines | 500 code | 94 blank | 0 comment | 0 complexity | 71b4efe22c607c976ca3dc65fe5358f3 MD5 | raw file
Possible License(s): Unlicense
  1. \documentclass[10pt]{article}
  2. \usepackage{fullpage}
  3. \usepackage{parskip}
  4. \usepackage{newcent}
  5. \usepackage{s2c}
  6. \title{\StoC\ notes for \emph{An Introduction to Scheme}}
  7. \author{Joel F. Bartlett}
  8. \date{}
  9. \begin{document}
  10. \maketitle
  11. \emph{An Introduction to Scheme}, by Jerry D. Smith, is a recent
  12. text on the programming language Scheme. Rather than being
  13. directed at a specific implementation of Scheme, it attempts
  14. to stick close to the dialect defined in the \emph{Revised$^4$
  15. Report on the Algorithmic Language Scheme} that is the base
  16. for many implementations including \StoC. This document
  17. provides section notes to point out the differences between TI
  18. PC Scheme used in the text and \StoC. The user will also
  19. find it helpful to read \emph{An Introduction to \StoC\ in 19
  20. Prompts} and have the \emph{\StoC\ Index to the Revised$^4$
  21. Report on the Algorithmic Language Scheme} and the
  22. \emph{Revised$^4$ Report on the Algorithmic Language Scheme}
  23. available for reference.
  24. \subsection*{1.8 (((((())))))}
  25. \StoC\ does not have an internal editor. Instead, users
  26. use the editor of their choice (which may or may not have
  27. parentheses matching) and then use \textbf{load} to load the file
  28. into the Scheme system.
  29. \subsection*{1.10 PC Scheme and the Listener}
  30. This is what the interaction on page 9 looks like in \StoC:
  31. \begin{quote}
  32. \begin{verbatim}
  33. > (+ 3 2)
  34. 5
  35. > (load "examples.sc")
  36. SQUARE
  37. "examples.sc"
  38. > (square 2)
  39. 4
  40. > (exit)
  41. \end{verbatim}
  42. \end{quote}
  43. User input is prompted by ``\texttt{>}'', \StoC\ files end with the
  44. suffix ``.sc'', and the command primitives \textbf{\%c} and \textbf{\%d} are
  45. not supported. User input is not evaluated until the user
  46. types return.
  47. \subsection*{2.2 The Scheme Listener}
  48. \StoC\ is started by the command \textbf{s2ci}. Once the command
  49. is executed, the window looks like this:
  50. \begin{quote}
  51. \begin{verbatim}
  52. $ s2ci
  53. Scheme->C -- 01sep91jfb -- Copyright 1989 Digital Equipment Corporation
  54. >
  55. \end{verbatim}
  56. \end{quote}
  57. When the evaluation of an expression results in an error, the
  58. debugger is entered. It prints an error message followed by a
  59. procedure call traceback. It then prompts the user with ``\texttt{>>}''
  60. to allow commands to inspect the state of the computation
  61. where the error occurred. For now, simply type control-D to
  62. return to the main read-eval-print loop.
  63. \begin{quote}
  64. \begin{verbatim}
  65. > (square 2)
  66. ***** SQUARE Top-level symbol is undefined
  67. (SQUARE 2) in ENV-0
  68. (EVAL ...)
  69. (SCREP_REP ...)
  70. (READ-EVAL-PRINT ...)
  71. >> ^D
  72. > (load "examples.sc")
  73. SQUARE
  74. "examples.sc"
  75. > (square 2)
  76. 4
  77. > (exit)
  78. \end{verbatim}
  79. \end{quote}
  80. \subsection*{2.3 Simple Arithmetic}
  81. Most \StoC\ systems do not have bignums. Numbers are
  82. represented as either 29-bit integers or 64-bit floating point
  83. values.
  84. Exercise for the student: Add bignums to \StoC.
  85. The boolean constant for true is \textbf{\#t} and false is \textbf{\#f}.
  86. In keeping with tradition, both the empty list \textbf{()} and \textbf{\#f}
  87. are considered to be false. It is good programming practice
  88. to not use the empty list \textbf{()} as a synonym for \textbf{\#f}, and in
  89. IEEE compliant Scheme's your program won't work as the empty
  90. list \textbf{()} is a synonym for \textbf{\#t}!
  91. \subsection*{2.4.2 Constants}
  92. The constants \textbf{\#!true}, \textbf{\#!false}, and \textbf{\#!null} are not implemented,
  93. use \textbf{\#t}, \textbf{\#f}, and \textbf{'()} respectively. The character
  94. constants \textbf{\#\textbackslash{}backspace}, \textbf{\#\textbackslash{}page}, and \textbf{\#\textbackslash{}rubout} are not
  95. implemented.
  96. \subsection*{2.4.4 Literal Expressions}
  97. The value of \textbf{\#f} is false, represented as \textbf{\#f}, which is
  98. not the same as the empty list \textbf{()}. While both \textbf{\#f} and
  99. \textbf{()} are considered to be false when evaluating a boolean
  100. expression, they are not equivalent. Note that the empty list
  101. is not a self-evaluating constant. In order to avoid an
  102. error, one must quote it when entering it into Scheme:
  103. \begin{quote}
  104. \begin{verbatim}
  105. > ()
  106. > (
  107. ***** EVAL Argument contains an item that is not self-evaluating: ()
  108. (EVAL ...)
  109. (SCREP_REP ...)
  110. (READ-EVAL-PRINT ...)
  111. >> ^D
  112. > '()
  113. ()
  114. >
  115. \end{verbatim}
  116. \end{quote}
  117. \subsection*{3.2 The Global Environment}
  118. There is only one top level environment that contains both
  119. user and system definitions.
  120. \subsection*{5.2 Logical Operators}
  121. Since \textbf{\#f} and the empty list \textbf{()} are different, \textbf{\#f} is
  122. always returned when a predicate returns false:
  123. \begin{quote}
  124. \begin{verbatim}
  125. > (<= 4 3)
  126. #F
  127. \end{verbatim}
  128. \end{quote}
  129. \subsection*{5.7 begin}
  130. In the text, the programming convention is that one calls
  131. \textbf{newline} and then calls \textbf{display}. In printing to the
  132. terminal with \StoC, it is better to call \textbf{display} and
  133. then call \textbf{newline} as a newline is not automatically
  134. generated when Scheme prompts the user for additional input.
  135. \subsection*{8.2 Characters}
  136. The following \#\textbackslash{}\emph{char-name} forms are supported: \textbf{\#\textbackslash{}formfeed},
  137. \textbf{\#\textbackslash{}linefeed}, \textbf{\#\textbackslash{}newline}, \textbf{\#\textbackslash{}return}, \textbf{\#\textbackslash{}space}, and \textbf{\#\textbackslash{}tab}.
  138. \subsection*{8.4.1 Character Predicates}
  139. The user need not define these as they are part of the system.
  140. \subsection*{8.4.3 String Conversion Functions}
  141. See the \StoC\ documentation for information about
  142. \textbf{number-\texttt{>}string} and \textbf{string-\texttt{>}number}.
  143. \subsection*{9.4 User-defined Port Operations}
  144. Ports in \StoC\ are represented as a pair of the symbol
  145. \textbf{port} and the procedure that implements it. For example:
  146. \begin{quote}
  147. \begin{verbatim}
  148. > (current-input-port)
  149. (PORT . #*PROCEDURE*)
  150. >
  151. \end{verbatim}
  152. \end{quote}
  153. Don't forget to put the call to \textbf{newline} after the second
  154. call to \textbf{display} when writing \textbf{addtwo}.
  155. \subsection*{9.6 Strings as Ports}
  156. Strings can be opened as input ports by \textbf{open-input-string} and
  157. as output ports by \textbf{open-output-string}. See the index for more
  158. details.
  159. \subsection*{9.7 A Utility for Reading Lines: read-ln}
  160. Users will not see either the backspace or rubout characters
  161. as they are handled by the workstation's terminal emulator.
  162. \subsection*{10.2 Debugging and Lexical Scope}
  163. A pretty-print procedure is provided, \textbf{pp}, but it does not
  164. pretty-print the text of a procedure.
  165. Breakpoints may be set on the entry and exit of any procedure
  166. defined in the top level environment. Rather than adding a
  167. call to \textbf{bkpt} as was done in the text, a user would set a
  168. breakpoint on each call to \textbf{\texttt{<}} and observe the value of
  169. \textbf{i}:
  170. \begin{quote}
  171. \begin{verbatim}
  172. > (one-to-y-sqrd 3)
  173. 0 -calls - (> 1 3)
  174. 0- i
  175. 1
  176. 0- ^D
  177. 0 -returns- #F
  178. 0- ^D
  179. \end{verbatim}
  180. \end{quote}
  181. \begin{quote}
  182. \begin{verbatim}
  183. 0 -calls - (> 0 3)
  184. 0- i
  185. 0
  186. 0- ^D
  187. 0 -returns- #F
  188. 0- ^D
  189. 0 -calls - (> -1 3)
  190. 0- i
  191. -1
  192. 0- ^D
  193. 0 -returns- #F
  194. 0- ^D
  195. 0 -calls - (> -2 3)
  196. 0- (top-level)
  197. >
  198. \end{verbatim}
  199. \end{quote}
  200. On each entry to \textbf{\texttt{>}}, the values of it's arguments are
  201. printed. The value of \textbf{i} can be examined by entering \textbf{i}
  202. followed by a return. When control-D or \textbf{(proceed)} is
  203. entered, the function is evaluated and the result is printed.
  204. To continue with the computation, enter control-D or
  205. \textbf{(proceed)}. Once it become clear that the program is in
  206. error, the user is able to return to the top level
  207. read-eval-print loop by entering \textbf{(top-level)}.
  208. Tracing is done using the \textbf{trace} and \textbf{untrace} commands:
  209. \begin{quote}
  210. \begin{verbatim}
  211. > (one-to-z-sqrd 3)
  212. 12
  213. > (trace one-to-z-sqrd)
  214. (ONE-TO-Z-SQRD)
  215. > (one-to-z-sqrd 3)
  216. (ONE-TO-Z-SQRD 3)
  217. ==> 12
  218. 12
  219. > (untrace one-to-z-sqrd)
  220. (ONE-TO-Z-SQRD)
  221. >
  222. \end{verbatim}
  223. \end{quote}
  224. The \textbf{bpt} command puts a breakpoint on both the entry and
  225. exit to a procedure. The \textbf{unbpt} command removes a
  226. breakpoint. Here's the example on page 141:
  227. \begin{quote}
  228. \begin{verbatim}
  229. > (bpt sqr)
  230. SQR
  231. > (one-to-z-sqrd 3)
  232. (ONE-TO-Z-SQRD 3)
  233. 1 -calls - (SQR 1)
  234. 1- ^D
  235. 1 -returns- 2
  236. 1- ^D
  237. \end{verbatim}
  238. \end{quote}
  239. \begin{quote}
  240. \begin{verbatim}
  241. 1 -calls - (SQR 2)
  242. 1- ^D
  243. 1 -returns- 4
  244. 1- ^D
  245. 1 -calls - (SQR 3)
  246. 1- ^D
  247. 1 -returns- 6
  248. 1- (top-level)
  249. > (unbpt sqr)
  250. (SQR)
  251. >
  252. \end{verbatim}
  253. \end{quote}
  254. When both \textbf{sqr} and \textbf{one-to-z-sqrd} are traced, one gets
  255. the following output.
  256. \begin{quote}
  257. \begin{verbatim}
  258. > (trace one-to-z-sqrd)
  259. (ONE-TO-Z-SQRD)
  260. > (trace sqr)
  261. (SQR)
  262. > (ONE-TO-Z-SQRD 3)
  263. (ONE-TO-Z-SQRD 3)
  264. (SQR 1)
  265. ==> 2
  266. (SQR 2)
  267. ==> 4
  268. (SQR 3)
  269. ==> 6
  270. ==> 12
  271. 12
  272. >
  273. \end{verbatim}
  274. \end{quote}
  275. Tracing the recursive function \textbf{ftl} produces the following
  276. output.
  277. \begin{quote}
  278. \begin{verbatim}
  279. > (trace ftl)
  280. (FTL)
  281. > (ftl 3)
  282. (FTL 3)
  283. (FTL 2)
  284. (FTL 1)
  285. (FTL 0)
  286. ==> 1
  287. ==> 1
  288. ==> 2
  289. ==> 6
  290. 6
  291. >
  292. \end{verbatim}
  293. \end{quote}
  294. \subsection*{10.3 Debugging in a Lexically Scoped Environment}
  295. Conditional breakpoints can be placed on procedures using the
  296. \textbf{bpt} special form. The second argument is a test procedure
  297. that is either the name of a top level procedure or a lambda
  298. expression defining a procedure. On each entry to the
  299. procedure, the test procedure is evaluated with the arguments
  300. to the breakpointed procedure. When the test procedure
  301. returns a true value, the breakpoint is taken.
  302. \begin{quote}
  303. \begin{verbatim}
  304. > (bpt one-to-n (lambda (x) (>= x 3)))
  305. ONE-TO-N
  306. > (mean-table 5)
  307. =======================================
  308. N MEAN OF 1 TO N
  309. 1 1
  310. 2 1.5
  311. 3
  312. 0 -calls - (ONE-TO-N 3)
  313. 0-
  314. \end{verbatim}
  315. \end{quote}
  316. The first time the argument to \textbf{one-to-n} is \textbf{\texttt{>=}} 3, the
  317. breakpoint is taken. Once at a breakpoint, the \textbf{backtrace}
  318. procedure allows the call stack and environments to be
  319. inspected.
  320. \begin{quote}
  321. \begin{verbatim}
  322. 0- (backtrace)
  323. (ONE-TO-N N) in ENV-0
  324. (/ (ONE-TO-N N) N) in ENV-1
  325. (DISPLAY (ONE-TO-N-MEAN N)) in ENV-2
  326. (BEGIN (NEWLINE) (DISPLAY N) (DISPLAY " ") (DIS ... in ENV-3
  327. (EVAL ...)
  328. (SCREP_REP ...)
  329. (READ-EVAL-PRINT ...)
  330. #F
  331. 0-
  332. \end{verbatim}
  333. \end{quote}
  334. Environments are identified by the symbols \textbf{env-}\emph{i}.
  335. The value of an environment is an a-list of symbols and their
  336. values. It's often useful to use \textbf{pp} to print out an
  337. environment. An expression may be evaluated within a specific
  338. environment by calling \textbf{eval} with two arguments, the
  339. expression and the environment.
  340. \begin{quote}
  341. \begin{verbatim}
  342. 0- env-1
  343. ((LOCATION . "inside one-to-n-mean") (N . 3))
  344. 0- env-3
  345. ((N . 3) (\d\o\l\o\o\p . #*PROCEDURE*) (PRINT-HEADER .#*PROCEDU
  346. RE*) (HEADER-LINE . "=======================================")(
  347. HIGH-BOUND . 5))
  348. 0- (pp env-3)
  349. ((N . 3)
  350. (\d\o\l\o\o\p . #*PROCEDURE*)
  351. (PRINT-HEADER . #*PROCEDURE*)
  352. (HEADER-LINE . "=======================================")
  353. (HIGH-BOUND . 5))#T
  354. 0- (eval 'high-bound env-3)
  355. 5
  356. 0- ^D
  357. 0 -returns- 6
  358. 0- ^D
  359. 2
  360. 4
  361. 0 -calls - (ONE-TO-N 4)
  362. 0- ^D
  363. 0 -returns- 10
  364. 0- ^D
  365. 2.5
  366. 5
  367. 0 -calls - (ONE-TO-N 5)
  368. 0- ^D
  369. 0 -returns- 15
  370. 0- ^D
  371. 3
  372. =======================================
  373. #F
  374. >
  375. \end{verbatim}
  376. \end{quote}
  377. The same techniques that one uses to explore a program that
  378. has hit a breakpoint can be used to investigate a running
  379. program. Here a simple loop is run. When the user enters
  380. control-C the running program is interrupted. A breakpoint is
  381. put on \textbf{eq?}\ and then the program is continued by typing
  382. control-D. Once the breakpoint is hit, \textbf{(eq?\ 0 0)} is
  383. executed, and then \textbf{proceed} is used to change the result
  384. returned by \textbf{eq?}, which causes the loop to complete.
  385. \begin{quote}
  386. \begin{verbatim}
  387. > (let loop ((i 0)) (if (eq? i 0) (loop i) 'done))
  388. ^C
  389. ***** INTERRUPT *****
  390. (EQ? I 0) in ENV-0
  391. (IF (EQ? I 0) (LOOP I) 'DONE) in ENV-1
  392. (EVAL ...)
  393. (SCREP_REP ...)
  394. (READ-EVAL-PRINT ...)
  395. >> (bpt eq?)
  396. EQ?
  397. >> ^D
  398. 0 -calls - (EQ? 0 0)
  399. 0- ^D
  400. 0 -returns- #T
  401. 0- (proceed #f)
  402. DONE
  403. >
  404. \end{verbatim}
  405. \end{quote}
  406. Finally, these techniques can be used to investigate the
  407. environment when an error occurs. Note that when control-D is
  408. entered to continue, Scheme returns to the top level
  409. read-eval-print loop.
  410. \begin{quote}
  411. \begin{verbatim}
  412. > (let ((i 0)) (car (car (car i))))
  413. ***** CAR Argument not a PAIR: 0
  414. (SCRT1_$_CAR-ERROR ...)
  415. (CAR ...)
  416. (CAR I) in ENV-0
  417. (CAR (CAR I)) in ENV-1
  418. (CAR (CAR (CAR I))) in ENV-2
  419. (EVAL ...)
  420. (SCREP_REP ...)
  421. (READ-EVAL-PRINT ...)
  422. >> i
  423. 0
  424. >> env-0
  425. ((I . 0))
  426. >> ^D
  427. >
  428. \end{verbatim}
  429. \end{quote}
  430. Exercise for the student: implement \textbf{assert}.
  431. \subsection*{11.3 dir: A Utility for Listing Filenames (Implementation-specific)}
  432. In order to implement this in \StoC\ you'll need to implement your
  433. own version of \textbf{sort!}\ and use \textbf{(open-input-port \texttt{"}ls\texttt{"})} to generate a
  434. list of file names.
  435. \subsection*{11.4 format: A Utility for Formatted Output}
  436. \StoC\ contains a procedure \textbf{format}. See the documentation for
  437. details.
  438. \subsection*{13.2 Memory Organization}
  439. Exercise for the student: implement \textbf{append!}.
  440. \subsection*{15.4 Macros [OPTIONAL]}
  441. \StoC\ macros implements ``expansion passing'' macros based upon the
  442. ideas found in \emph{Expansion-Passing Style: Beyond Conventional Macros},
  443. 1986 ACM Conference on Lisp and Functional Programming, 143--150.
  444. The simplest form of a macro is a constant. The arguments to the
  445. special form \textbf{define-constant} are the symbol identifying the
  446. constant and the expression to evaluate to calculate it's value.
  447. \begin{quote}
  448. \begin{verbatim}
  449. > (define-constant radius 23)
  450. RADIUS
  451. > (define-constant pi 3.14159)
  452. PI
  453. > (define-constant circumference (* pi radius 2))
  454. CIRCUMFERENCE
  455. > (define-constant area (* 3.14159 (* radius radius)))
  456. AREA
  457. > area
  458. 1661.90111
  459. >
  460. \end{verbatim}
  461. \end{quote}
  462. The second type of macro defines an in-line procedure. The form
  463. \textbf{define-in-line} associates a symbol with a procedure definition.
  464. All calls to the procedure are replaced by the lambda expression
  465. defining the procedure.
  466. \begin{quote}
  467. \begin{verbatim}
  468. > (define-in-line (plus3 x) (+ x 3))
  469. PLUS3
  470. > (plus3 5)
  471. 8
  472. >
  473. \end{verbatim}
  474. \end{quote}
  475. The most general form of macro expansion allows the user to
  476. examine a procedure call and then selectively cause further
  477. macro expansion. The definition for \textbf{plus3} can also be written
  478. as:
  479. \begin{quote}
  480. \begin{verbatim}
  481. > (define-macro plus3
  482. (lambda (form expander)
  483. (expander `(+ ,(cadr form) 3) expander)))
  484. PLUS3
  485. > (plus3 8)
  486. 11
  487. >
  488. \end{verbatim}
  489. \end{quote}
  490. The macro is defined by a procedure that takes two arguments:
  491. the form to be expanded, and a procedure to do further
  492. expansion. It's typical action is to build an expanded form
  493. and then call the further expansion procedure with the new form and the
  494. further expansion procedure as arguments. For examples of use
  495. of this type of macro expander, the reader is directed to the
  496. file \textbf{scrt/predef.sc} that defines the macros
  497. used by the compiler.
  498. N.B. Macro definitions may not be placed inside procedure
  499. definitions.
  500. \end{document}