-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathr5rs.tar
8953 lines (7483 loc) · 340 KB
/
r5rs.tar
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
r5rs/ 40755 2464 322 0 6473337660 10064 5 ustar kelsey CSI r5rs/basic.tex 100644 2464 322 35033 6473103745 12003 0 ustar kelsey CSI %\vfill\eject
\chapter{Basic concepts}
\label{basicchapter}
\section{Variables, syntactic keywords, and regions}
\label{specialformsection}
\label{variablesection}
An identifier\index{identifier} may name a type of syntax, or it may name
a location where a value can be stored. An identifier that names a type
of syntax is called a {\em syntactic keyword}\mainindex{syntactic keyword}
and is said to be {\em bound} to that syntax. An identifier that names a
location is called a {\em variable}\mainindex{variable} and is said to be
{\em bound} to that location. The set of all visible
bindings\mainindex{binding} in effect at some point in a program is
known as the {\em environment} in effect at that point. The value
stored in the location to which a variable is bound is called the
variable's value. By abuse of terminology, the variable is sometimes
said to name the value or to be bound to the value. This is not quite
accurate, but confusion rarely results from this practice.
\todo{Define ``assigned'' and ``unassigned'' perhaps?}
\todo{In programs without side effects, one can safely pretend that the
variables are bound directly to the arguments. Or:
In programs without \ide{set!}, one can safely pretend that the
variable is bound directly to the value. }
\vest Certain expression types are used to create new kinds of syntax
and bind syntactic keywords to those new syntaxes, while other
expression types create new locations and bind variables to those
locations. These expression types are called {\em binding constructs}.
\mainindex{binding construct}
Those that bind syntactic keywords are listed in section~\ref{macrosection}.
The most fundamental of the variable binding constructs is the
{\cf lambda} expression, because all other variable binding constructs
can be explained in terms of {\cf lambda} expressions. The other
variable binding constructs are {\cf let}, {\cf let*}, {\cf letrec},
and {\cf do} expressions (see sections~\ref{lambda}, \ref{letrec}, and
\ref{do}).
%Note: internal definitions not mentioned here.
\vest Like Algol and Pascal, and unlike most other dialects of Lisp
except for Common Lisp, Scheme is a statically scoped language with
block structure. To each place where an identifier is bound in a program
there corresponds a \defining{region} of the program text within which
the binding is visible. The region is determined by the particular
binding construct that establishes the binding; if the binding is
established by a {\cf lambda} expression, for example, then its region
is the entire {\cf lambda} expression. Every mention of an identifier
refers to the binding of the identifier that established the
innermost of the regions containing the use. If there is no binding of
the identifier whose region contains the use, then the use refers to the
binding for the variable in the top level environment, if any
(chapters~\ref{expressionchapter} and \ref{initialenv}); if there is no
binding for the identifier,
it is said to be \defining{unbound}.\mainindex{bound}\index{top level
environment}
\todo{Mention that some implementations have multiple top level environments?}
\todo{Pitman sez: needs elaboration in case of {\tt(let ...)}}
\todo{Pitman asks: say something about vars created after scheme starts?
{\tt (define x 3) (define (f) x) (define (g) y) (define y 4)}
Clinger replies: The language was explicitly
designed to permit a view in which no variables are created after
Scheme starts. In files, you can scan out the definitions beforehand.
I think we're agreed on the principle that interactive use should
approximate that behavior as closely as possible, though we don't yet
agree on which programming environment provides the best approximation.}
\section{Disjointness of types}
\label{disjointness}
No object satisfies more than one of the following predicates:
\begin{scheme}
boolean? pair?
symbol? number?
char? string?
vector? port?
procedure?%
\end{scheme}
These predicates define the types {\em boolean}, {\em pair}, {\em
symbol}, {\em number}, {\em char} (or {\em character}), {\em string}, {\em
vector}, {\em port}, and {\em procedure}. The empty list is a special
object of its own type; it satisfies none of the above predicates.
\mainindex{type}\schindex{boolean?}\schindex{pair?}\schindex{symbol?}
\schindex{number?}\schindex{char?}\schindex{string?}\schindex{vector?}
\schindex{port?}\schindex{procedure?}\index{empty list}
Although there is a separate boolean type,
any Scheme value can be used as a boolean value for the purpose of a
conditional test. As explained in section~\ref{booleansection}, all
values count as true in such a test except for \schfalse{}.
% and possibly the empty list.
% The only value that is guaranteed to count as
% false is \schfalse{}. It is explicitly unspecified whether the empty list
% counts as true or as false.
This report uses the word ``true'' to refer to any
Scheme value except \schfalse{}, and the word ``false'' to refer to
\schfalse{}. \mainindex{true} \mainindex{false}
\section{External representations}
\label{externalreps}
An important concept in Scheme (and Lisp) is that of the {\em external
representation} of an object as a sequence of characters. For example,
an external representation of the integer 28 is the sequence of
characters ``{\tt 28}'', and an external representation of a list consisting
of the integers 8 and 13 is the sequence of characters ``{\tt(8 13)}''.
The external representation of an object is not necessarily unique. The
integer 28 also has representations ``{\tt \#e28.000}'' and ``{\tt\#x1c}'', and the
list in the previous paragraph also has the representations ``{\tt( 08 13
)}'' and ``{\tt(8 .\ (13 .\ ()))}'' (see section~\ref{listsection}).
Many objects have standard external representations, but some, such as
procedures, do not have standard representations (although particular
implementations may define representations for them).
An external representation may be written in a program to obtain the
corresponding object (see {\cf quote}, section~\ref{quote}).
External representations can also be used for input and output. The
procedure {\cf read} (section~\ref{read}) parses external
representations, and the procedure {\cf write} (section~\ref{write})
generates them. Together, they provide an elegant and powerful
input/output facility.
Note that the sequence of characters ``{\tt(+ 2 6)}'' is {\em not} an
external representation of the integer 8, even though it {\em is} an
expression evaluating to the integer 8; rather, it is an external
representation of a three-element list, the elements of which are the symbol
{\tt +} and the integers 2 and 6. Scheme's syntax has the property that
any sequence of characters that is an expression is also the external
representation of some object. This can lead to confusion, since it may
not be obvious out of context whether a given sequence of characters is
intended to denote data or program, but it is also a source of power,
since it facilitates writing programs such as interpreters and
compilers that treat programs as data (or vice versa).
The syntax of external representations of various kinds of objects
accompanies the description of the primitives for manipulating the
objects in the appropriate sections of chapter~\ref{initialenv}.
\section{Storage model}
\label{storagemodel}
Variables and objects such as pairs, vectors, and strings implicitly
denote locations\mainindex{location} or sequences of locations. A string, for
example, denotes as many locations as there are characters in the string.
(These locations need not correspond to a full machine word.) A new value may be
stored into one of these locations using the {\tt string-set!} procedure, but
the string continues to denote the same locations as before.
An object fetched from a location, by a variable reference or by
a procedure such as {\cf car}, {\cf vector-ref}, or {\cf string-ref}, is
equivalent in the sense of \ide{eqv?} % and \ide{eq?} ??
(section~\ref{equivalencesection})
to the object last stored in the location before the fetch.
Every location is marked to show whether it is in use.
No variable or object ever refers to a location that is not in use.
Whenever this report speaks of storage being allocated for a variable
or object, what is meant is that an appropriate number of locations are
chosen from the set of locations that are not in use, and the chosen
locations are marked to indicate that they are now in use before the variable
or object is made to denote them.
In many systems it is desirable for constants\index{constant} (i.e. the values of
literal expressions) to reside in read-only-memory. To express this, it is
convenient to imagine that every object that denotes locations is associated
with a flag telling whether that object is mutable\index{mutable} or
immutable\index{immutable}. In such systems literal constants and the strings
returned by \ide{symbol->string} are immutable objects, while all objects
created by the other procedures listed in this report are mutable. It is an
error to attempt to store a new value into a location that is denoted by an
immutable object.
\section{Proper tail recursion}
\label{proper tail recursion}
Implementations of Scheme are required to be
{\em properly tail-recursive}\mainindex{proper tail recursion}.
Procedure calls that occur in certain syntactic
contexts defined below are `tail calls'. A Scheme implementation is
properly tail-recursive if it supports an unbounded number of active
tail calls. A call is {\em active} if the called procedure may still
return. Note that this includes calls that may be returned from either
by the current continuation or by continuations captured earlier by
{\cf call-with-current-continuation} that are later invoked.
In the absence of captured continuations, calls could
return at most once and the active calls would be those that had not
yet returned.
A formal definition of proper tail recursion can be found
in~\cite{propertailrecursion}.
\begin{rationale}
Intuitively, no space is needed for an active tail call because the
continuation that is used in the tail call has the same semantics as the
continuation passed to the procedure containing the call. Although an improper
implementation might use a new continuation in the call, a return
to this new continuation would be followed immediately by a return
to the continuation passed to the procedure. A properly tail-recursive
implementation returns to that continuation directly.
Proper tail recursion was one of the central ideas in Steele and
Sussman's original version of Scheme. Their first Scheme interpreter
implemented both functions and actors. Control flow was expressed using
actors, which differed from functions in that they passed their results
on to another actor instead of returning to a caller. In the terminology
of this section, each actor finished with a tail call to another actor.
Steele and Sussman later observed that in their interpreter the code
for dealing with actors was identical to that for functions and thus
there was no need to include both in the language.
\end{rationale}
A {\em tail call}\mainindex{tail call} is a procedure call that occurs
in a {\em tail context}. Tail contexts are defined inductively. Note
that a tail context is always determined with respect to a particular lambda
expression.
\begin{itemize}
\item The last expression within the body of a lambda expression,
shown as \hyper{tail expression} below, occurs in a tail context.
\begin{grammar}%
(l\=ambda \meta{formals}
\>\arbno{\meta{definition}} \arbno{\meta{expression}} \meta{tail expression})
\end{grammar}%
\item If one of the following expressions is in a tail context,
then the subexpressions shown as \meta{tail expression} are in a tail context.
These were derived from rules in the grammar given in
chapter~\ref{formalchapter} by replacing some occurrences of \meta{expression}
with \meta{tail expression}. Only those rules that contain tail contexts
are shown here.
\begin{grammar}%
(if \meta{expression} \meta{tail expression} \meta{tail expression})
(if \meta{expression} \meta{tail expression})
(cond \atleastone{\meta{cond clause}})
(cond \arbno{\meta{cond clause}} (else \meta{tail sequence}))
(c\=ase \meta{expression}
\>\atleastone{\meta{case clause}})
(c\=ase \meta{expression}
\>\arbno{\meta{case clause}}
\>(else \meta{tail sequence}))
(and \arbno{\meta{expression}} \meta{tail expression})
(or \arbno{\meta{expression}} \meta{tail expression})
(let (\arbno{\meta{binding spec}}) \meta{tail body})
(let \meta{variable} (\arbno{\meta{binding spec}}) \meta{tail body})
(let* (\arbno{\meta{binding spec}}) \meta{tail body})
(letrec (\arbno{\meta{binding spec}}) \meta{tail body})
(let-syntax (\arbno{\meta{syntax spec}}) \meta{tail body})
(letrec-syntax (\arbno{\meta{syntax spec}}) \meta{tail body})
(begin \meta{tail sequence})
(d\=o \=(\arbno{\meta{iteration spec}})
\> \>(\meta{test} \meta{tail sequence})
\>\arbno{\meta{expression}})
{\rm where}
\meta{cond clause} \: (\meta{test} \meta{tail sequence})
\meta{case clause} \: ((\arbno{\meta{datum}}) \meta{tail sequence})
\meta{tail body} \: \arbno{\meta{definition}} \meta{tail sequence}
\meta{tail sequence} \: \arbno{\meta{expression}} \meta{tail expression}
\end{grammar}%
\item
If a {\cf cond} expression is in a tail context, and has a clause of
the form {\cf (\hyperi{expression} => \hyperii{expression})}
then the (implied) call to
the procedure that results from the evaluation of \hyperii{expression} is in a
tail context. \hyperii{expression} itself is not in a tail context.
\end{itemize}
Certain built-in procedures are also required to perform tail calls.
The first argument passed to \ide{apply} and to
\ide{call-with-current-continuation}, and the second argument passed to
\ide{call-with-values}, must be called via a tail call.
Similarly, \ide{eval} must evaluate its argument as if it
were in tail position within the \ide{eval} procedure.
In the following example the only tail call is the call to {\cf f}.
None of the calls to {\cf g} or {\cf h} are tail calls. The reference to
{\cf x} is in a tail context, but it is not a call and thus is not a
tail call.
\begin{scheme}%
(lambda ()
(if (g)
(let ((x (h)))
x)
(and (g) (f))))
\end{scheme}%
\begin{note}
Implementations are allowed, but not required, to
recognize that some non-tail calls, such as the call to {\cf h}
above, can be evaluated as though they were tail calls.
In the example above, the {\cf let} expression could be compiled
as a tail call to {\cf h}. (The possibility of {\cf h} returning
an unexpected number of values can be ignored, because in that
case the effect of the {\cf let} is explicitly unspecified and
implementation-dependent.)
\end{note}