forked from cisco/ChezScheme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsystem.stex
5023 lines (4248 loc) · 196 KB
/
system.stex
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
% Copyright 2005-2016 Cisco Systems, Inc.
%
% Licensed under the Apache License, Version 2.0 (the "License");
% you may not use this file except in compliance with the License.
% You may obtain a copy of the License at
%
% http://www.apache.org/licenses/LICENSE-2.0
%
% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS,
% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
% See the License for the specific language governing permissions and
% limitations under the License.
\chapter{System Operations\label{CHPTSYSTEM}}
This chapter describes operations for
handling exceptions, interrupts, environments,
compilation and evaluation, profiling,
controlling the operation of the system,
timing and statistics,
defining and setting parameters,
and
querying the operating system environment.
\schemeinit
(load "docond.ss")
\endschemeinit
\section{Exceptions\label{SECTSYSTEMEXCEPTIONS}}
\index{exception handling}{\ChezScheme} provides some extensions to the
Revised$^6$ Report exception-handling mechanism, including mechanisms
for producing formatted error messages, displaying conditions,
and redefining the base exception handler.
These extensions are described in this section.
%----------------------------------------------------------------------------
\entryheader
\formdef{warning}{\categoryprocedure}{(warning \var{who} \var{msg} \var{irritant} \dots)}
\returns unspecified
\listlibraries
\endentryheader
\scheme{warning} raises a continuable exception with condition type
\scheme{&warning} and should be used to describe situations for which the
\scheme{&warning} condition type is appropriate, typically a situation
that should not prevent the program from continuing but might result
in a more serious problem at some later point.
The continuation object with which the exception is raised also includes
a \scheme{&who} condition whose who field is \var{who} if \var{who} is
not \scheme{#f}, a \scheme{&message} condition whose message field is
\var{msg}, and an \scheme{&irritants} condition whose irritants field
is \scheme{(\var{irritant} \dots)}.
\var{who} must be a string, a symbol, or \scheme{#f} identifying the procedure
or syntactic form reporting the warning upon whose behalf the warning is being
reported.
It is usually best to identify a procedure the programmer has called rather
than some other procedure the programmer may not be aware is involved in
carrying out the operation.
\var{msg} must be a string and should describe the exceptional situation.
The irritants may be any Scheme objects and should include values that may
have caused or been materially involved in the exceptional situation.
%----------------------------------------------------------------------------
\entryheader
\formdef{assertion-violationf}{\categoryprocedure}{(assertion-violationf \var{who} \var{msg} \var{irritant} \dots)}
\returns does not return
\formdef{errorf}{\categoryprocedure}{(errorf \var{who} \var{msg} \var{irritant} \dots)}
\returns does not return
\formdef{warningf}{\categoryprocedure}{(warningf \var{who} \var{msg} \var{irritant} \dots)}
\returns unspecified
\listlibraries
\endentryheader
\index{formatted error messages}%
These procedures are like \scheme{assertion-violation}, \scheme{error},
and \scheme{warning} except
that \var{msg} is assumed to be a format string, as if in a call to
\scheme{format} (Section~\ref{SECTFORMAT}), with
\scheme{\var{irritant} \dots} treated as the additional arguments to
\scheme{format}.
This allows programs to control the appearance of the error message, at
least when the default exception handler is in place.
For each of these procedures, the continuation object with which the exception
is raised includes a \scheme{&format} condition to signify that the string
contained in the condition object's \scheme{&message} condition is a
\scheme{format} string and the objects contained in the condition object's
\scheme{&irritants} condition should be treated as the additional
\scheme{format} arguments.
%----------------------------------------------------------------------------
\entryheader
\conditionformdef{(define-condition-type &format &condition
make-format-condition format-condition?)}
\endentryheader
\noindent
Presence of this condition type within a compound condition indicates
that the string provided by the \scheme{&message} condition, if
present, is a \scheme{format} string and the list of objects provided by
the \scheme{&irritants} condition, if present, should be treated as
additional \scheme{format} arguments.
\showit
%----------------------------------------------------------------------------
\entryheader
\conditionformdef{(define-condition-type &source &condition
make-source-condition source-condition?
(form source-condition-form))}
\endentryheader
\noindent
This condition type can be included within a compound condition when a
source expression can be identified in situations in which a
\scheme{&syntax} condition would be inappropriate, such as when a
run-time assertion violation is detected.
The \scheme{form} argument should be an s-expression or syntax object
representing the source expression.
\showit
%----------------------------------------------------------------------------
\entryheader
\conditionformdef{(define-condition-type &continuation &condition
make-continuation-condition continuation-condition?
(continuation condition-continuation))}
\endentryheader
\noindent
This condition type can be included within a compound condition to indicate
the current continuation at the point where the exception described by the
condition occurred.
The continuation of a failed \scheme{assert} or a call to
\scheme{assertion-violation}, \scheme{assertion-violationf},
\scheme{error}, \scheme{errorf}, or \scheme{syntax-error} is now included
via this condition type in the conditions passed to \scheme{raise}.
The \scheme{continuation} argument should be a continuation.
\showit
%----------------------------------------------------------------------------
\entryheader
\formdef{display-condition}{\categoryprocedure}{(display-condition \var{obj})}
\formdef{display-condition}{\categoryprocedure}{(display-condition \var{obj} \var{textual-output-port})}
\returns unspecified
\listlibraries
\endentryheader
If \var{textual-output-port} is not supplied, it defaults to the current output port.
This procedure displays a message to the effect that an exception
has occurred with value \var{obj}.
If \var{obj} is a condition (Chapter~\ref{TSPL:CHPTEXCEPTIONS} of
{\TSPLFOUR}), it displays information encapsulated within the condition,
handling messages, \var{who} conditions, irritants, source information,
etc., as appropriate.
%----------------------------------------------------------------------------
\entryheader
\formdef{default-exception-handler}{\categoryprocedure}{(default-exception-handler \var{obj})}
\returns unspecified
\listlibraries
\endentryheader
This procedure is the default value of the \scheme{base-exception-handler}
parameter called on a condition when no other exception handler has been
defined or when all dynamically established exception handlers have chosen
not to handle the condition.
It first displays \var{obj}, as if with \scheme{display-condition}, to the
console error port.
For non-serious warning conditions, it returns immediately after displaying
the condition.
For serious or other non-warning conditions, it
saves the condition in the parameter \scheme{debug-condition}, where
\scheme{debug} (Section~\ref{SECTDEBUGINTERACTIVE}) can retrieve it and
allow it to be inspected.
If the \scheme{debug-on-exception} parameter is set to \scheme{#f} (the
default unless the \index{\scheme{--debug-on-exception} command-line
option}\scheme{--debug-on-exception} command-line option is provided), the
handler prints a message instructing the user to type \scheme{(debug)} to
enter the debugger, then resets to the current caf\'e.
Otherwise, the handler invokes \scheme{debug} directly and resets if
\scheme{debug} returns.
If an I/O exception occurs while attempting to display the condition,
the default exception handler resets (as if by calling \scheme{reset}).
The intent is to avoid an infinite regression (ultimately ending
in exhaustion of memory) in which the process repeatedly recurs
back to the default exception handler trying to write to a console-error
port (typically stderr) that is no longer writable, e.g., due to
the other end of a pipe or socket having been closed.
%----------------------------------------------------------------------------
\entryheader
\formdef{debug-on-exception}{\categoryglobalparameter}{debug-on-exception}
\listlibraries
\endentryheader
The value of this parameter determines whether the default exception handler
immediately enters the debugger immediately when it receives a serious or
non-warning condition.
If the \index{\scheme{--debug-on-exception} command-line option}\scheme{--debug-on-exception}
command-line option (Section~\ref{SECTUSEINTERACTION}) has been provided, the
initial value of this parameter is \scheme{#t}.
Otherwise, the initial value is \scheme{#f}.
%----------------------------------------------------------------------------
\entryheader
\formdef{base-exception-handler}{\categorythreadparameter}{base-exception-handler}
\listlibraries
\endentryheader
The value of this parameter must be a procedure, and the procedure
should accept one argument.
The default value of \scheme{base-exception-handler} is
the procedure \scheme{default-exception-handler}.
The value of this parameter is invoked whenever no exception handler
established by a program has chosen to handle an exception.
%----------------------------------------------------------------------------
\entryheader
\formdef{debug-condition}{\categorythreadparameter}{debug-condition}
\listlibraries
\endentryheader
This parameter is used by the default exception handler to hold the
last serious or non-warning condition received by the handler, where
it can be inspected via the \scheme{debug} procedure
(Section~\ref{SECTDEBUGINTERACTIVE}).
It can also be invoked by user code to store or retrieve a
condition.
%----------------------------------------------------------------------------
\entryheader
\formdef{current-exception-state}{\categorythreadparameter}{current-exception-state}
\listlibraries
\endentryheader
\scheme{current-exception-state} may be used to get or set
the current exception state.
When called without arguments, \scheme{current-exception-state} returns
an \emph{exception state} comprising the current stack of handlers established
by \scheme{with-exception-handler} and \scheme{guard}.
When called with a single argument, which must be an exception state,
\scheme{current-exception-state} sets the exception state.
%----------------------------------------------------------------------------
\entryheader
\formdef{create-exception-state}{\categoryprocedure}{(create-exception-state)}
\formdef{create-exception-state}{\categoryprocedure}{(create-exception-state \var{procedure})}
\listlibraries
\endentryheader
\scheme{create-exception-state} creates an exception
state whose stack of exception handlers is empty except for, in effect,
an infinite number of occurrences of \emph{handler} at its
base.
\var{handler} must be a procedure, and should accept one argument.
If not provided, \var{handler} defaults to a procedure equivalent
to the value of the following expression.
\schemedisplay
(lambda (x) ((base-exception-handler) x))
\endschemedisplay
\section{Interrupts\label{SECTSYSTEMINTERRUPTS}}
\index{interrupts}{\ChezScheme} allows programs to control
the action of the Scheme system when various events
occur, including an interrupt from the
keyboard, the expiration of an internal timer set by \scheme{set-timer},
a breakpoint caused by a call to \scheme{break}, or a request from the
storage manager to initiate a garbage collection.
These mechanisms are described in this section, except for the
collect request mechanism, which is described in Section~\ref{SECTSMGMTGC}.
Timer, keyboard, and collect-request interrupts are supported via a counter
that is decremented approximately once for each call to a nonleaf procedure.
(A leaf procedure is one that does not itself make any calls.)
When no timer is running, this counter is set to a default value (1000
in Version~9) when a program starts or after an interrupt occurs.
If a timer is set (via \scheme{set-timer}), the counter is set to the
minimum of the default value and the number of ticks to which the timer is
set.
When the counter reaches zero, the system looks to see if the timer
is set and has expired or if a keyboard or collect request interrupt
has occurred.
If so, the current procedure call is pended (``put on hold'') while the
appropriate interrupt handler is invoked to handle the interrupt.
When (if) the interrupt handler returns, the pended call takes place.
Thus, timer, keyboard, and collect-request interrupts effectively occur
synchronously with respect to the procedure call mechanism, and
keyboard and collect request interrupts may be delayed by a number
of calls equal to the default timer value.
Calls to the break handler occur immediately
whenever \scheme{break} is called.
%----------------------------------------------------------------------------
\entryheader
\formdef{break}{\categoryprocedure}{(break \var{who} \var{msg} \var{irritant} \dots)}
\formdef{break}{\categoryprocedure}{(break \var{who})}
\formdef{break}{\categoryprocedure}{(break)}
\returns unspecified
\listlibraries
\endentryheader
\noindent
The arguments to \scheme{break} follow the protocol described above for
\scheme{errorf}.
The default break handler (see \scheme{break-handler}) displays a message and
invokes the \index{debugger}debugger.
The format string and objects may be omitted, in which case the
message issued by the default break handler identifies the break
using the \var{who} argument but provides no more information
about the break.
If the \var{who} argument is omitted as well, no message is generated.
The default break handler returns normally if the debugger
exits normally.
%----------------------------------------------------------------------------
\entryheader
\formdef{break-handler}{\categorythreadparameter}{break-handler}
\listlibraries
\endentryheader
\noindent
The value of this parameter must be a procedure.
The current break handler is called by \scheme{break}, which passes
along its arguments.
See \scheme{break} for a description of the default break
handler.
The example below shows how to disable breaks.
\schemedisplay
(break-handler (lambda args (void)))
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{keyboard-interrupt-handler}{\categorythreadparameter}{keyboard-interrupt-handler}
\listlibraries
\endentryheader
\noindent
The value of this parameter must be a procedure.
The keyboard-interrupt handler is called (with no arguments) when
a keyboard interrupt occurs.
The default keyboard-interrupt handler invokes the interactive
\index{debugger}debugger.
If the debugger exits normally the interrupted computation is
resumed.
The example below shows how to install a keyboard-interrupt handler
that resets without invoking the debugger.
\schemedisplay
(keyboard-interrupt-handler
(lambda ()
(newline (console-output-port))
(reset)))
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader\label{desc:set-timer}
\formdef{set-timer}{\categoryprocedure}{(set-timer \var{n})}
\returns previous current timer value
\listlibraries
\endentryheader
\noindent
\index{timer interrupts}\var{n} must be a nonnegative integer.
When \var{n} is nonzero, \scheme{set-timer} starts an internal timer with
an initial value of \var{n}.
When \var{n} ticks elapse, a timer interrupt occurs, resulting in
invocation of the timer interrupt handler.
Each tick corresponds roughly to one nonleaf procedure call (see the
introduction to this section); thus, ticks are not
uniform time units but instead depend heavily on how much work is done
by each procedure call.
When \var{n} is zero, \scheme{set-timer} turns the timer off.
The value returned in either case is the value of the timer before the
call to \scheme{set-timer}.
A return value of 0 should not be taken to imply that the timer was not on;
the return value may also be 0 if the timer was just about to fire when
the call to \scheme{set-timer} occurred.
The engine mechanism (Section~\ref{SECTENGINES}) is built on top of the
timer interrupt so timer interrupts should not be used with engines.
%----------------------------------------------------------------------------
\entryheader
\formdef{timer-interrupt-handler}{\categorythreadparameter}{timer-interrupt-handler}
\listlibraries
\endentryheader
\noindent
\index{timer interrupts}The value of this parameter must be a procedure.
The timer interrupt handler is called by the system when the internal timer
(set by \scheme{set-timer}) expires.
The default handler raises an exception with condition type \scheme{&assertion}
to say that the handler has not
been defined; any program that uses the timer should redefine the
handler before setting the timer.
%----------------------------------------------------------------------------
\entryheader
\formdef{disable-interrupts}{\categoryprocedure}{(disable-interrupts)}
\formdef{enable-interrupts}{\categoryprocedure}{(enable-interrupts)}
\returns disable count
\listlibraries
\endentryheader
\noindent
\scheme{disable-interrupts} disables the handling of interrupts,
including timer, keyboard, and collect request interrupts.
\scheme{enable-interrupts} re-enables these interrupts.
The system maintains a disable count that starts at zero; when zero,
interrupts are enabled.
Each call to \scheme{disable-interrupts} increments the count,
effectively disabling interrupts.
Each call to \scheme{enable-interrupts} decrements the count, if
not already zero, effectively enabling interrupts.
For example, two calls to \scheme{disable-interrupts} followed by one call to
\scheme{enable-interrupts} leaves interrupts disabled.
Calls to \scheme{enable-interrupts} when the count is already zero
(and interrupts are enabled) have no effect.
The value returned by either procedure is the number of calls to
\scheme{enable-interrupts} required to enable interrupts.
Great care should be exercised when using these procedures, since disabling
interrupts inhibits the normal processing of keyboard interrupts,
timer interrupts, and, perhaps most importantly, collect request interrupts.
Since garbage collection does not happen automatically when interrupts are
disabled, it is possible for the storage allocator to run out of space
unnecessarily should interrupts be disabled for a long period of time.
The \scheme{with-interrupts-disabled} syntactic form should be used instead of
these more primitive procedures whenever possible,
since \scheme{with-interrupts-disabled} ensures that interrupts are re-enabled
whenever a nonlocal exit occurs, such as when an exception is handled by
the default exception handler.
%----------------------------------------------------------------------------
\entryheader
\formdef{with-interrupts-disabled}{\categorysyntax}{(with-interrupts-disabled \var{body_1} \var{body_2} \dots)}
\formdef{critical-section}{\categorysyntax}{(critical-section \var{body_1} \var{body_2} \dots)}
\returns the values of the body \scheme{\var{body_1} \var{body_2} \dots}
\listlibraries
\endentryheader
\noindent
\scheme{with-interrupts-disabled} evaluates the body
\scheme{\var{body_1} \var{body_2} \dots} with interrupts disabled.
That is, upon entry, interrupts are disabled, and
upon exit, interrupts are re-enabled.
Thus, \scheme{with-interrupts-disabled} allows the implementation of indivisible
operations in nonthreaded versions of {\ChezScheme} or within a single thread
in threaded versions of {\ChezScheme}.
\scheme{critical-section} is the same as \scheme{with-interrupts-disabled} and
is provided for backward compatibility.
\scheme{with-interrupts-disabled} can be defined as follows.
\schemedisplay
(define-syntax with-interrupts-disabled
(syntax-rules ()
[(_ b1 b2 ...)
(dynamic-wind
disable-interrupts
(lambda () b1 b2 ...)
enable-interrupts)]))
\endschemedisplay
\noindent
The use of \scheme{dynamic-wind} ensures that interrupts are
disabled whenever the body of the \scheme{with-interrupts-disabled} expression
is active and re-enabled whenever it is not.
Since calls to \scheme{disable-interrupts} are counted (see the
discussion under \scheme{disable-interrupts} and
\scheme{enable-interrupts} above), \scheme{with-interrupts-disabled}
expressions may be nested with the desired effect.
%----------------------------------------------------------------------------
\entryheader
\formdef{register-signal-handler}{\categoryprocedure}{(register-signal-handler \var{sig} \var{procedure})}
\returns unspecified
\listlibraries
\endentryheader
\noindent
\scheme{register-signal-handler} is used to
establish a signal handler for a given low-level signal.
\var{sig} must be an exact integer identifying a valid signal, and
\var{procedure} should accept one argument.
See your host system's \scheme{<signal.h>} or documentation for a list
of valid signals and their numbers.
After a signal handler for a given signal has been registered, receipt
of the specified signal results in a call to the handler.
The handler is passed the signal number, allowing the same handler to
be used for different signals while differentiating among them.
Signals handled in this fashion are treated like keyboard interrupts in
that the handler is not called immediately when the signal is delivered
to the process, but rather at some procedure call boundary after the
signal is delivered.
It is generally not a good idea, therefore, to establish handlers for
memory faults, illegal instructions, and the like, since the code that
causes the fault or illegal instruction will continue to execute
(presumably erroneously) for some time before the handler is invoked.
\scheme{register-signal-handler} is supported only on Unix-based
systems.
\section{Environments\label{SECTMISCENVIRONMENTS}}
Environments are first-class objects containing identifier bindings.
They are similar to modules but, unlike modules, may be manipulated
at run time.
Environments may be provided as optional arguments to \scheme{eval},
\scheme{expand}, and the procedures that define, assign, or
reference top-level values.
There are several built-in environments, and new environments can
be created by copying existing environments or selected bindings
from existing environments.
Environments can be mutable or immutable.
A mutable environment can be extended with new bindings, its
existing bindings can be modified, and its variables can be assigned.
An immutable environment cannot be modified in any of these ways.
%----------------------------------------------------------------------------
\entryheader
\formdef{environment?}{\categoryprocedure}{(environment? \var{obj})}
\returns \scheme{#t} if \var{obj} is an environment, otherwise \scheme{#f}
\listlibraries
\endnoskipentryheader
\schemedisplay
(environment? (interaction-environment)) ;=> #t
(environment? 'interaction-environment) ;=> #f
(environment? (copy-environment (scheme-environment))) ;=> #t
(environment? (environment '(prefix (rnrs) $rnrs-))) ;=> #t
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{environment-mutable?}{\categoryprocedure}{(environment-mutable? \var{env})}
\returns \scheme{#t} if \var{env} is mutable, otherwise \scheme{#f}
\listlibraries
\endnoskipentryheader
\schemedisplay
(environment-mutable? (interaction-environment)) ;=> #t
(environment-mutable? (scheme-environment)) ;=> #f
(environment-mutable? (copy-environment (scheme-environment))) ;=> #t
(environment-mutable? (environment '(prefix (rnrs) $rnrs-))) ;=> #f
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{scheme-environment}{\categoryprocedure}{(scheme-environment)}
\returns an environment
\listlibraries
\endentryheader
\noindent
\scheme{scheme-environment} returns an environment containing
the initial top-level bindings.
This environment corresponds to the \scheme{scheme} module.
The environment returned by this procedure is immutable.
\schemedisplay
(define cons 3)
(top-level-value 'cons (scheme-environment)) ;=> #<procedure cons>
(set-top-level-value! 'cons 3 (scheme-environment)) ;=> \var{exception}
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{ieee-environment}{\categoryprocedure}{(ieee-environment)}
\returns an IEEE/ANSI standard compatibility environment
\listlibraries
\endentryheader
\noindent
\scheme{ieee-environment} returns an environment containing
bindings for the keywords and variables whose meanings are
defined by the IEEE/ANSI Standard for Scheme~\cite{IEEE:1178}.
The bindings for each of the identifiers in the IEEE environment are those
of the corresponding Revised$^6$ Report library, so this does not provide
full backward compatibility.
The environment returned by this procedure is immutable.
\schemedisplay
(define cons 3)
(top-level-value 'cons (ieee-environment)) ;=> #<procedure cons>
(set-top-level-value! 'cons 3 (ieee-environment)) ;=> \var{exception}
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{interaction-environment}{\categorythreadparameter}{interaction-environment}
\listlibraries
\endentryheader
\noindent
The original value of \scheme{interaction-environment} is the default
top-level environment.
It is initially set to a mutable copy of
\scheme{(scheme-environment)} and which may be extended or otherwise
altered by top-level definitions and assignments.
It may be set to any environment, mutable or not, to change the
default top-level evaluation environment.
An expression's top-level bindings resolve to the environment that is
in effect when the expression is expanded, and changing the value
of this parameter has no effect on running code.
Changes affect only code that is subsequently expanded, e.g., as the
result of a call to \scheme{eval}, \scheme{load}, or
\scheme{compile-file}.
\schemedisplay
(define cons 3)
cons ;=> 3
(top-level-value 'cons (interaction-environment)) ;=> 3
(interaction-environment (scheme-environment))
cons ;=> #<procedure cons>
(set! cons 3) ;=> \var{exception: attempt to assign immutable variable}
(define cons 3) ;=> \var{exception: invalid definition in immutable environment}
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{copy-environment}{\categoryprocedure}{(copy-environment \var{env})}
\formdef{copy-environment}{\categoryprocedure}{(copy-environment \var{env} \var{mutable?})}
\formdef{copy-environment}{\categoryprocedure}{(copy-environment \var{env} \var{mutable?} \var{syms})}
\returns a new environment
\listlibraries
\endentryheader
\scheme{copy-environment} returns a copy of \var{env}, i.e., a new
environment that contains the same bindings as \var{env}.
The environment is mutable if \var{mutable?} is omitted or true;
if \var{mutable?} is false, the environment is immutable.
The set of bindings copied from \var{env} to the new environment
is determined by \var{syms}, which defaults to the value of
\scheme{(environment-symbols \var{env})}.
The binding, if any, for each element of \var{syms} is copied to the
new environment, and no other bindings are present in the new
environment.
In the current implementation, the storage space used by an environment
is never collected, so repeated use of \scheme{copy-environment} will
eventually cause the system to run out of memory.
\schemedisplay
(define e (copy-environment (scheme-environment)))
(eval '(define cons +) e)
(eval '(cons 3 4) e) ;=> 7
(eval '(cons 3 4) (scheme-environment)) ;=> (3 . 4)
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{environment-symbols}{\categoryprocedure}{(environment-symbols \var{env})}
\returns a list of symbols
\listlibraries
\endentryheader
This procedure returns a list of symbols representing the identifiers
bound in environment \var{env}.
It is primarily useful in building the list of symbols to be copied
from one environment to another.
\schemedisplay
(define listless-environment
(copy-environment
(scheme-environment)
#t
(remq 'list (environment-symbols (scheme-environment)))))
(eval '(let ([x (cons 3 4)]) x) listless-environment) ;=> (3 . 4)
(eval '(list 3 4) listless-environment) ;=> \var{exception}
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{apropos-list}{\categoryprocedure}{(apropos-list \var{s})}
\formdef{apropos-list}{\categoryprocedure}{(apropos-list \var{s} \var{env})}
\returns see below
\listlibraries
\endentryheader
This procedure returns a selected list of symbols and pairs.
Each symbol in the list represents an identifier bound in \var{env}.
Each pair represents a set of identifiers exported by a
predefined library or a library previously defined or loaded
into the system.
The car of the pair is the library name, and the cdr is a list
of symbols.
If \var{s} is a string, only entries whose names have \var{s} as a
substring are included, and if \var{s} is a symbol, only those whose names
have the name of \var{s} as a substring are selected.
If no environment is provided, it defaults to the value of
\scheme{interaction-environment}.
\schemedisplay
(library (a) (export a-vector-sortof) (import (rnrs))
(define a-vector-sortof '(vector 1 2 3)))
(apropos-list 'vector-sort) ;=>
(vector-sort vector-sort!
((a) a-vector-sortof)
((chezscheme) vector-sort vector-sort!)
((rnrs) vector-sort vector-sort!)
((rnrs sorting) vector-sort vector-sort!)
((scheme) vector-sort vector-sort!))
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{apropos}{\categoryprocedure}{(apropos \var{s})}
\formdef{apropos}{\categoryprocedure}{(apropos \var{s} \var{env})}
\returns unspecified
\listlibraries
\endentryheader
\scheme{apropos} is like \scheme{apropos-list} except the information is
displayed to the current output port, as shown in the following
transcript.
\schemedisplay
> (library (a) (export a-vector-sortof) (import (rnrs))
(define a-vector-sortof '(vector 1 2 3)))
> (apropos 'vector-sort)
interaction environment:
vector-sort, vector-sort!
(a):
a-vector-sortof
(chezscheme):
vector-sort, vector-sort!
(rnrs):
vector-sort, vector-sort!
(rnrs sorting):
vector-sort, vector-sort!
(scheme):
vector-sort, vector-sort!
\endschemedisplay
\section{Compilation, Evaluation, and Loading\label{SECTMISCCOMPILEEVAL}}
%----------------------------------------------------------------------------
\noskipentryheader
\formdef{eval}{\categoryprocedure}{(eval \var{obj})}
\formdef{eval}{\categoryprocedure}{(eval \var{obj} \var{env})}
\returns value of the Scheme form represented by \var{obj}
\listlibraries
\endnoskipentryheader
\noindent
\scheme{eval} treats \var{obj} as the representation of an expression.
It evaluates the expression in environment \var{env} and returns
its value.
If no environment is provided, it defaults to the environment
returned by \scheme{interaction-environment}.
Single-argument \scheme{eval} is a {\ChezScheme} extension.
{\ChezScheme} also permits \var{obj} to be the representation of a
nonexpression form, i.e., a definition, whenever the environment
is mutable.
{\ChezScheme} further allows \var{obj} to be an annotation
(Section~\ref{SECTSYNTAXANNOTATIONS}), and the default evaluators
make use of annotations to incorporate source-file
information in error messages and associate source-file
information with compiled code.
In {\ChezScheme}, \scheme{eval} is actually a wrapper that simply
passes its arguments to the current evaluator.
(See \scheme{current-eval}.)
The default evaluator is \scheme{compile}, which expands the
expression via the current expander (see
\scheme{current-expand}), compiles it,
executes the resulting code, and returns its value.
If the environment argument, \var{env}, is present,
\scheme{compile} passes it along to the current expander,
which is \scheme{sc-expand} by default.
%----------------------------------------------------------------------------
\entryheader
\formdef{current-eval}{\categorythreadparameter}{current-eval}
\listlibraries
\endentryheader
\noindent
\scheme{current-eval} determines the evaluation procedure used by the
procedures \index{\scheme{eval}}\scheme{eval}, \scheme{load}, and
\scheme{new-cafe}.
\scheme{current-eval} is initially bound to the value of
\index{\scheme{compile}}\scheme{compile}.
(In {\PetiteChezScheme}, it is initially bound to the value of
\index{\scheme{interpret}}\scheme{interpret}.)
The evaluation procedureshould expect one or two arguments: an object
to evaluate and an optional environment.
The second argument might be an annotation
(Section~\ref{SECTSYNTAXANNOTATIONS}).
\schemedisplay
(current-eval interpret)
(+ 1 1) ;=> 2
(current-eval (lambda (x . ignore) x))
(+ 1 1) ;=> (+ 1 1)
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{compile}{\categoryprocedure}{(compile \var{obj})}
\formdef{compile}{\categoryprocedure}{(compile \var{obj} \var{env})}
\returns value of the Scheme form represented by \var{obj}
\listlibraries
\endentryheader
\noindent
\var{obj}, which can be an annotation (Section~\ref{SECTSYNTAXANNOTATIONS})
or unannotated value, is treated as a Scheme expression, expanded with the
current expander (the value of \scheme{current-expand}) in the specified
environment (or the interaction environment, if no environment
is provided), compiled to machine code, and executed.
\scheme{compile} is the default value of the \scheme{current-eval}
parameter.
%----------------------------------------------------------------------------
\entryheader
\formdef{interpret}{\categoryprocedure}{(interpret \var{obj})}
\formdef{interpret}{\categoryprocedure}{(interpret \var{obj} \var{env})}
\returns value of the Scheme form represented by \var{obj}
\listlibraries
\endentryheader
\noindent
\scheme{interpret} is like \scheme{compile}, except that the expression
is interpreted rather than compiled.
\scheme{interpret} may be used as a replacement for \scheme{compile},
with the following caveats:
\begin{itemize}
\item
Interpreted code runs significantly slower.
\item
Inspector information is not generated for
interpreted code, so the inspector is not as useful for interpreted
code as it is for compiled code.
\item
Foreign procedure expressions cannot be
interpreted, so the interpreter invokes the compiler for all
foreign procedure expressions (this is done transparently).
\end{itemize}
\noindent
\scheme{interpret} is sometimes faster than \scheme{compile} when the
form to be evaluated is short running, since it avoids some of the
work done by \scheme{compile} prior to evaluation.
%----------------------------------------------------------------------------
\entryheader
\formdef{load}{\categoryprocedure}{(load \var{path})}
\formdef{load}{\categoryprocedure}{(load \var{path} \var{eval-proc})}
\returns unspecified
\listlibraries
\endentryheader
\noindent
\var{path} must be a string.
\scheme{load} reads and evaluates the contents of the file specified by
\var{path}.
The file may contain source or object code.
By default, \scheme{load} employs \scheme{eval} to evaluate each source
expression found in a source file.
If \var{eval-proc} is specified, \scheme{load} uses this procedure instead.
\var{eval-proc} must accept one argument, the expression to evaluate.
The expression passed to \var{eval-proc} might be an annotation
(Section~\ref{SECTSYNTAXANNOTATIONS}) or an unannotated value.
The \var{eval-proc} argument
facilitates the implementation of embedded Scheme-like languages
and the use of alternate
evaluation mechanisms to be used for Scheme programs.
\var{eval-proc} can be put to other uses as well.
For example,
\schemedisplay
(load "myfile.ss"
(lambda (x)
(pretty-print
(if (annotation? x)
(annotation-stripped x)
x))
(newline)
(eval x)))
\endschemedisplay
\noindent
pretty-prints each expression before evaluating it.
\index{\scheme{source-directories}}%
The parameter \scheme{source-directories} (Section~\ref{SECTSYSTEMSOURCE})
determines the set of directories searched for source files not identified
by absolute path names.
%----------------------------------------------------------------------------
\entryheader
\formdef{load-library}{\categoryprocedure}{(load-library \var{path})}
\formdef{load-library}{\categoryprocedure}{(load-library \var{path} \var{eval-proc})}
\returns unspecified
\listlibraries
\endentryheader
\scheme{load-library} is identical to \scheme{load} except
that it treats the input file as if it were prefixed by an implicit
\scheme{#!r6rs}.
This effectively disables any non-R6RS lexical
syntax except where subsequently overridden by \scheme{#!chezscheme}.
%----------------------------------------------------------------------------
\entryheader
\formdef{load-program}{\categoryprocedure}{(load-program \var{path})}
\formdef{load-program}{\categoryprocedure}{(load-program \var{path} \var{eval-proc})}
\returns unspecified
\listlibraries
\endentryheader
\noindent
\var{path} must be a string.
\scheme{load-program} reads and evaluates the contents of the file specified by
\var{path}.
The file may contain source or object code.
If it contains source code, \scheme{load-program} wraps
the code in a \scheme{top-level-program} form so that the file's
content is treated as an RNRS top-level program
(Section~\ref{TSPL:SECTLIBPROGRAMS} of {\TSPLFOUR}).
By default, \scheme{load-program} employs \scheme{eval} to evaluate each source
expression found in the file.
If \var{eval-proc} is specified, \scheme{load-program} uses this procedure instead.
\var{eval-proc} must accept one argument, the expression to evaluate.
The expression passed to \var{eval-proc} might be an annotation
(Section~\ref{SECTSYNTAXANNOTATIONS}) or an unannotated value.
\index{\scheme{source-directories}}%
The parameter \scheme{source-directories} (Section~\ref{SECTSYSTEMSOURCE})
determines the set of directories searched for source files not identified
by absolute path names.
%----------------------------------------------------------------------------
\entryheader
\formdef{visit}{\categoryprocedure}{(visit \var{path})}
\returns unspecified
\listlibraries
\endentryheader
\noindent
\var{path} must be a string.
\scheme{visit} reads the named file, which must contain compiled object
code compatible with the current machine type and version, and it
runs those portions of the compiled object code that
establish compile-time information or correspond to expressions
identified as ``visit'' time by \scheme{eval-when} forms contained in
the original source file.
For example, assume the file \scheme{t1.ss} contains the following
forms: