• Aucun résultat trouvé

Lisp Manual

N/A
N/A
Protected

Academic year: 2022

Partager "Lisp Manual"

Copied!
487
0
0

Texte intégral

(1)
(2)

Lisp rvlachine Manual

Third Edition March 1981

Daniel Weinreb David Moon

This report describes research done at the Artificbl Intelligence Laboratory of lhe Massachusetts Institute of Technology. Support for the laboratory's artificial intelligcnce research is provided in part by the Advanced Research Projects Agency of the DCP~H tment of Defense LInder Office of Naval Research Contract numhcr NOOO 14-80-C-0505.

(3)

(OCopyright by the 1\lassachusctts Institute of Technolugy; Cambridge, Mass. 02139 All rights reserved.

(4)

Preface

The Lisp Machine manual describes both the language and the "operating system" of the Lisp machinc. The language, a dialect of Lisp. is completely documented by this manual. The software environment and operating-systcm-Iike parts of the system contain many things which are still in a state of flux. This manual confines itself primarily to the stabler parts of the system, and docs not address the window system and user interface at all. That documentation will be released as a separate volume at a later time.

Any comments. suggestions, or criticisms will be welcmned. Please send Arpa network mail to BUG-I.MMAN@MIT-AI.

Those not on the Arpanet may send U.S. mail to Danicl L Weimcb or David A. Moon

Room 926

545 Technology Square Camhridge. Mass. 02139

Note

The Lisp machine is a prodlll.:t of the efforts of many people too numerous to list here and of the "unique ellvironmeat of the M.l.T. Artificial Intelligence Laboratory.

Portions of this manual were written by IUchard Stallman. Mike McMahon, and Alan Bawden. The chapter un the LOOP iteration macro is a" reprint of. Laboratory for Computer Science memo TM-169. by Glenn Burke.

(5)
(6)

Lisp Machine Manual Summary Table of Contents

Sununary Table of Contents

1. Introduction . . . . 2. Primitive Object Types . 3. Evaluation . . . . 4. flow of Control. . . . . 5. Manipulating List Structure.

6. Symbols.

7. Numbers.

8. Arrays ..

9. Strings ..

lO. Functions.

II. Closures. . 12. Stack Groups . . 13. Locatives . . . 14. Subprimitives . 15. Areas . . . 16. The Compiler.

17. Macros . . . .

12. The LOOP Itera.Liou Macro 19. Dcfstmct . . . .

20. Objects, Message Passing, and Flavors . 2.1. The I/O System.

22. Naming ufFiles . . . . 23. Packages .. . . . . 24. tvlaintaining Large Systems 25. Processes . . . . 26. Errors and Debugging. . . 27. How to Read Assembly Language.

28. Querying the User.

29. Initializations . . . . 30. Dates and Times. . . . 31. Miscellaneous Useful Functions.

Concept Index.

Flavor Index . . Message Index.

Meter Index ..

Variable Index.

Function Index

. 1 . 6 .12 .30 .48 .78 .84 .98 115 124 144 149 156 158 177 181 191 204 226 245 276 332 345 359 377 389 .417 . 435

.' .

438

441 447 456 459 460 461 462 464

1(i'~IAR-81

(7)

Table of Contents ii

Table of Contents

1. Introduction. . . . 1.1 General Information . . 1.2 Structure of the Manual.

1.3 Nocational Conventions and i-Iclpful Notes 2. Primitive Object Types

2.1 Data Types . 2.2 Predicates.

3. Evaluation . 3.1 Variables.

3.2 Functions.

3.3 Some Functions and Special Forms . 3.4 Multiple Values.

4. Flow of Control.

4.1 Conditionals . . 4.2 Iteration . . ..

4.3 Non-J-ocal Exits.

4.4 Mapping . . . .

5. Manipulating List Structure.

5.l Conses . . . . 5.2 Lists . . . . . 5.3 Alteration of List Structure .

5.4 Cdr-Coding . . 5.5 Tables . . . 5.6 Lists as Tables. . 5.7 Association Lists 5.8 Property Lists . . 5.9 Hash Tables. . .

5.9.1 Hashing on Eq.

5.9.2 I-lashing on Equal .

5.9.3 Hash Tables and the Garbage Collector.

5.9.4 I-lash Primitive.

S.lO Sorting . . 5.11 Resources. . . 6. Symbols . . . . 6.1 1'111: Vallie Cell . 6.2 The Function Cell.

6.3 The Pn~perty List.

6.4 The Print Name. . 6.5 The Package Cell . 6.6 Creating Symbols.

7. Numbers . . . . 7.1 NUllleric Predicates.

7.2 Numeric Comparisons

Lisp Machine Manual

· 1

· 1

· 1 .2

· 6

· 6 .7 12 13 18 21 26 30 30 35 43 45 48 49 50 57 59 61 61 64 66 69 70 71 72 73

74

76 78 78 79 80 81 81 81 84 86 87

(8)

Lisp M<lchine Manual

7.3 Arithmetic . . . . 7.4 Transcendental Functions . . 7.5 Numeric Type Conversions.

7.6 Logical Operations on Numbers 7.7 Byte Mnnipulation Functions.

7.8 Random Numbers . . . . 7.9 24-Bit Numbers . . . . 7.10 Doubic-Precision Arithmetic 8. Arrays . . . .

8.1 Extra Features of Arrays . . . 8.2 Basic A rray Functions . . . .

8.3 Getting Inf0l1llJtion Abollt an Array.

8.4 Changing the Size of an Array 8.5 Arrays Overlaid With Lists . . 8.6 Adding to the End of an Array 8.7 Copying an Array . . . .

8.8 Matrices and Systems of Linear Equations.

8.9 Planes . . . . 8.10 Madisp Array COl11paribility.

9. Strings . . . . 9.1 Characters . . . . 9.2 Upper and LOW(.'f Case Letters . 1).3 Basic String Opcl'(ltions .

. 9.4 String Searching . . . . 9.5 110 to Strings . . . . 9.6 Maclisp-Compatible Functions.

10. Functions. . . .

10.1 What Is a Function? . . . . 10.2 Function Specs . . . . 10.3 Simple FUllction Definitions.

iii

lOA Operations the User Can Perform on Functions 10.5 Kinds of Functions . . .

10.5.1 Interpreted Functions . . 10.5.2 Compiled Functions . . . 10.5.3 Other Kinds of Functions.

10.6 Function-Defining Special Forms.

10.7 Lambda-List Keywords . . . . 10.8 How Programs Manipulate Definitions.

10.9 [-low Programs Examine Functions ..

W.lD Encapsulations. . . . lO.lO.l Rerwme- Within Encap:;ulations . 11. Closures. . . .

11. L What a Closure [s. . . . 1 L2 Examples of the the ufClosurcs.

11.3 Closure-ManipuJatingFul1ctions.

11.4 Entities. . . .

Table of Contents

.88 .90

.91 .91 .94 .95 .96 .97 .98 100

102 105 106 107 108 109

111

112 113 lIS 115 116 116 119 121 123 . 124 124 124 126 128

129 130 131 131 132 135 135 137 139 143 144 144 146 147 148

(9)

Table of Contents

12. Stack Groups . . . . 12.1 Resuming of Slack Groups.

12.2 Stack Group States . . . 12.3 Stack Group Functions. . . 12.4 Input/Output in Stack Groups.

12.5 An Example of Stack Groups.

13. Locatives . . . . 13.1 Cells and Locatives. . . . 13.2 Functions Which Operate on Locatives.

14. SlIbprimitives .

14.1 Data Types . . . . . 14.2 Forwarding . . . . . 14.3 Pointer Manipulation 14.4 Analyzing Structures.

14.5 Crcating Object<; . . . 14.6 l.ocking SlIbprimitive . 14.7 I/O Dcyice SlIbprimitives . 14.8 Special Memory Refcrencing.

14.9 Swragc l.ayout Definitions ..

I tl.l0 Function-Calling SlIbprimitives.

14.11 I.ambda-Binding Subprimitive . 14.12 The PagingSystem ..

14.13 Closurc SlIbprimitives 14.14 Microcode Variables . 14.15 Meters. . . . 15. Areas . . . .

15.1 Area Functions and Variables 15.2 Interesting Areas . . . . 16. The Compiler . . . .

16.1 The Basic Operations of the Compiler . 16.2 How to Invoke the Compiler.

16.3 Input to the Compiler . . . . 16.4 Compiler Declarations. . . . 16.5 Compiler Source-Level Optimizers.

16.6 Files thut lVlaclisp Must Compile.

16.7 Putting Data in QFASL Files.

17. Macros . . . . 17.1 Introduction to Macros ..

17.2 Aids for Dcfining Macros 17.2.1 Dcfmacro . . . . 17.2.2 Backquote . . . . 17.3 Substitutable Functions . 17.4 Aids for Debugging Macros 17.5 Displacing Macros . . . 17 Ji Advanced Features of Defmacro . 17.7 Function~; to Expand Macros.

17.8 Generalized Variables . . . .

iv Lisp Machine Manual

· 149

· 150

· 151

· 152

· 153

· 154

· 156

· 156

· 156

· 158

· 158

· 160

· 161

· 162

· 162 .163 .163

· 164

· 166

· 167

· 168

· 168

· 170 .171

· 173

· 177

· 178

· 180

· 181

.' · 181

· 181

· 182

· 184

· 187

· 188

· 189

· 191

· 191 .192

· 193 .194

· 197

· 198

· 198

· 199 .200

· 201 16-MAR-81

(10)

Lisp Machine Manual

18. The LOOP Iteration Macro 18.1 fntroduction. . . . 18.2 Clauses . . . .

18.2.1 fteration-Driving Clauses.

18.2.2 Bindings. . . . . 18.2.3 Entrance and Exit 18.2.4 Side Effects.

18.2.5 Values . . . . 18.2.6 Endtests . . . . . 18.2.7 Aggregated Boolean Tests 18.2.8 Conditionalization . . . . 18.2.9 Miscellaneous Other Clauses.

18.3 Loop Synonyms.

18.4 Dat.1. Types . . . . 18.5 Destntcturing . . . . 18.6 The Iteration Framework . 18.7 [temtion Paths . . . . 18.7.1 Pre-Defined Paths . . .

18.7.1.1 'Ibe Interned-Symbols Path . 18.7.1.2 Sequence Iteration . . . . 18.7.2 Defining Paths . . . . 18.7.2.1 An Example Path Definition.

19. Defstruct . . . . . . . 19.1 Introduction to Stntcture Macros 19.2 How to Use Oefstruct. . . . ..

19.3 Options to Defstntct . . . .

19.4 Using the Constructor and Alterant Macros 19.4.1 Constmctor Macros. . . . 19.4.2 Alterant Macros . . . . 19.4.3 By-position Constructor Macros

19.5 Byte Fields . . . . . 19.6 Groupc'd Arrays. . . .'.

19.7 Named Structures . . . . 19.8 The si:dcfstruct-description Structure . 19.9 Extensions to Defstruct . . . .

19.9.1 An Example . . . . 19.9.2 Syntax ofdefstruct-definc-type . 19.9.3 Optiuns to defstntct-definc-type.

20. Objects, Message Passing, and Flavors . 20.1 Introduction.

20.2 Objects . . . . 20.} Modularity . . . . . 2004 Generic Opcrations . 20.5 Generic Operations in Lisp 20.6 Simple Use of Flavors. . 20.7 Mixing Flavors . . 20.8 Flavor Functions . 20.9 Dell1avor Options.

20.10 Flavor Families

v

..

Table of Contents

204 204 205 206 208 210 210 210 212 213 213 214 215 215

· 216

· 217

· 218 219 220 220 221

· 223

· 226

· 226

· 228.

· 229 ., 235

· 235

· 236 236 237 238 239

· 240

· 242

· 242

· 242 243

· 245

· 245

· 245

· 246

· 249

· 250

· 251

· 255

· 258 264

· 268 IIl-MAR-81

. . I

(11)

Table of Contents

20.11 Vanilla Flavor . . . . 20.12 Method Combination . . . . 20.13 [mplemcntation of Flavors .

20.13.1 Order of Definition 20.13.2 Changing a Flavor.

20.13.3 Restrictions . . . . 20.14 Entities . . . . 20.15 Useful Editor Commands.

21. The I/O System . . . . . 21.1 The Character Set . . . 21.2 Printed Representation. . .

21.2.] What the Pri nler Produces.

2 L2.2 What The Reader Accepts.

2.1.2.3 Macro Characters. . . . . 21.2.4 Sharp-sign Abbrcviations . 21.2.5 Special Character Names.

21.2.6 The Readtable . 21.3 Input Functions. . 21.4 Output Functions . 21.5 [/0 Streams. . . . U.5.l What Streams Arc.

21.5.2 General Purpose Stream Operations.

21.5.3 Special Purpose Stream Opcrations . 21.5.4 Standard Streams. . . ..

21.5.5 Making Your OWI1 Stream 21.6 Formatted Output . . .

21.6.1 The Fonnat Function. . . 21.6.2 The Output Subsystem . . 21.6.3 Formatting Lisp Code . . . 21.7 Rubollt Handling . . . .

21.8 The :read and :print Stream Operations 21.9 Accessing Files . . . .

21.9.1 Loading Files . . . . 21.9.2 File Property Lists . . ..

21.9.3 File Stream Operations . 21.10 Accessing Directories.

22. Naming of Files . . . . . 22.1 Path names. . . . 22.2 Defaults and Merging . 22.3 Gcneric Path names . 22.4 Path name Functions ..

22.5 Path name Messages ..

22.6 Host File Systems Supported.

22.6.1

rrs ... .

22.6.2 TOPS-20 . . . . . 22.7 Maclisp COIl version . 22.8 Examples . . . .

vi Lisp Machine Manual

.269 .270 .272 .272 .273 .274 .274 .275 .276 .276 .280 .280 .283 .285 .286 .288 .289 .291 .294 .297 .297 .297 .300 .302 .303 .305 .305 .314 .318 .319 .322

. .

.323

.325 .326 .329 .330 .332 .332 . 335 .337 .337 .340 .341 .342 .343 .343 .344

lG-~'IAR '8t

(12)

Lisp Machine Manual

23. Packages . . . . 23.1 The Need for Multiple Contexts. . 23.2 The Organization of Name Spaces.

23.3 Shared Programs . . . . 23.4 Declaring Packages .. . . . 23.5 Packages and Writing Code.

23.6 Shadowing . . . . 23.7 Packages and Interning . . . 23.8 Status Infonnation . . . . . 23.9 Packages, Loading, and Compilation.

23.10 Subpackages. . . . 23.11 Initialization of the Package System.

23.12 Initial Packages. . . . . 24. Maintaining Large Systems

24.1 Defining a System.

24.2 Transfonnations. . . . . 24.3 Making a System . . . .

24.4 Adding New Keywords to make-system.

24.5 Adding New Options for defsystem . 24.6 More esoteric Transfonnations 24.7 The Patch Facility. .

24.7.1 Defining aSysLcm 24.7.2 Loading Patches.

24.7.3 Making Patches .

24.8 Saving New Versions: Disk Partitions . 24.8.1 Concepts. . . .

24.8.2 Manipulating the Label.

24.8.3 Updating Software . . . 24.8.4 Installing New Software 24.8.5 Installing New Microcode . 25. Processes ... .

25.1 The Scheduler. .. . 25.2 Locks . . . . 25.3 Creating a Process.

25.4 Process Messages . 25.4.1 Process Attributes 25.4.2 Run and Arrest Reasons 25.4.3 Bashing the Process. . 25.5 Pmcess Flavors . . . . . 25.6 Other Process Functions.

26. Errors and Debugging.

26.1 The Error System . . 26.1.1 Conditions. . . . 26.1.2 Error Condition!) . 26.1.3 Signalling Errors.

26.1.4 Standard Condition Names.

26.1.5 Errsct . . . .

26.2 The Debugger . . . .

vii

"

..

Table of Contents

345 345 346 347 348 349 350 351 354 354 355 356 357 359 359 361 362 363 364 365 366 369 370 370 371 371 372 373 374 375 377 378 381 382 383 383 385 385 386 387 389 389 389 391 393 396 397 .398 I{dvL\R-Sl

(13)

Table of Contents

26.2.1 Entering the Debugger ..

26.2.2 How to Use the Debugger.

26.2.3 Debugger Commands. . . 26.2.4 Summary of Commands. . 26.3 Tracing Function Execution . 26.4 Advising a Function. . .

26.4.1 Designing the Advice. . . 26.4.2 :around Advice. . . .

viii

26.4.3 Advising One Function Within Another ..

26.5 Stepping Through an Evaluation.

26.6 Evalhook . . . .

26.7 The MAR . . . . 26.8 Variable Monitoring . . . . 27. How to Read Assembly Language

27.1 Introduction . . . . 27.2 A More Advanced Example.

27.3 The Rest of the Instmctions . 27.4 Function Entry . . . . 27.5 Special Class IV Instmctions . 27.6 Estimating Run Time

28. Querying the User. . . 29. Initializations . . . . .

29.1 System Initialization Lists 30. Dates and Times. . . . 30.1 Getting the Time. . . . .

30.1.1 Elapsed Time in 60ths ofa·Second.

30.1.2 Elapsed Time ill Microseconds 30.2 Printing Dater. and Times . . 30.3 Reading Dates and Times . . 30.4 Time Conversions. . . .

.

30.5 internal Functions. . . . 31. Miscellaneous Useful Functions

31.1 Poking Around in the Lisp World 31.2 Utility Programs . . . .

31.3 The Lisp Top Level ..

31.4 The Garbage Collector.

31.5 Logging In . . . 11.6 Dribble Files. . . . 31.7 Status and SStatus . Concept Index.

Flavor Index. . Message Index.

Meter Index ..

Variable Index.

..

Lisp Machine Manual

.398 .399 .400 .403 .404 .407 .409 .410 .4lO .411 .413 .414 .415 .417 .417 .420 .423 .426 .428 .431 .435 .438 .439 .441 .442 .442 .442 .443 .443 .444 .445 .447 .447 .449 .451 .453 .453 .454 .454 .456 .459 .460 .46L .462

t6-MAR-8L

(14)

Lisp Machine Manual ix Table of Contents

Function Index . . . ' . . . 464

1('-MAR-81

(15)
(16)

Lisp Macl1ine ':vfamldl 1 In trodllction

L Introduetion

1.1

GCllcrallnformatioll

The Lisp Ivfaclline is a new computer system designed to provide a high performance and economical implcment<ltinll of the Lisp language. It is a personal computation system, which means I.hat processor'; and milin memories arc Illlt time-multiplexed: when using <I I.isp Machine, you get your o',vn prnc,:ssor and memory system [(H' the JUl"<llion of the session. It is designed this way to relie"e the problems of thc running af large (.isp programs on tilllc-sharing systems.

(\er~:thiug on the I.isp \lachinc is writtell ill Lisp, including all systcm programs: there is nevcr any IIced to program in machine languagc. The system is highly intera<.:ti\'c.

The !.isp iv\;lchinc execute:; a new dialect of Lisp calkJ Lisp ivlachine Lisp, dc\ eloped at the M.I.T. ,\("(ificlal (ntelligem:e I.aburatory nJr LIse in artifkial il1t~lligel1<.:e research and related fields.

It is c:Iosc\y rehlted tn the t\1aclisp dialect, and attclIlpv; to lIlaintain a good degree of cllllipatihilily wilh M,idbp, while also providing many illlpn)\Cmcnts and ncw Icature:;. ~Iaclisp,

in tum, is ba~ed on Lisp 1.5.

Tlli:, c1uclIlll:nL is r.lle rcf('rcllcc m<Jnual for the l.isp M<lchine (.isp language, This document is not d LlIl!lriaL ;111d it SlillwtiillC:; refcr~; to li.rilL'tilllls and C(I'H':Cpts that <lrc IWI c~phil!L'd until later

!11 the manual. It is as:,ullh!cl that YIlU havc a basic workiug k!lowledge ld' ~(lIl1C i i';p di:llect: you

\\.il1 he <;ble [() ngllfc (JIll the reST ut' the bnguagc from this manual.

Thert' arc also flCiliLics explaineJ in lhis il1anual [11<'[ ilr,: 110[ r.::a:ly P;ifl 'If the I.i"p \'lIIguage.

Some lit' lhese art' subrouti DC p:ll;bgc~; of general lISC, and llthers arc iouis used in writing prclgrall1S, Howcver, the: Lisp [\.(achinc window 3ystcm, :lild the major utility progrmns, are not dOcLllflcnted here.

i.2 Structure of the

~vlanm,l

The manual starts nut with an explanation of the language. Chapter 2 explains thc Jilfcrent primitive types of Lisp object. and presents some basic predicate functions for testing types.

Chapter J cxr.tains the process of evaluation, which is the !reart of the Lisp language. Chapter 4 introduces the basic Lisp control structures.

The next several chapters explain the details of thc various prmllt!ve data'"types of the language, and the n.mclions t.har deal with rhcm. Ch~lpter 5 deals 'Nith c(Jnscs and the highcr-level structures lha[ can be huilt out of them, Stich as trees, lists. association lists, ililJ propcrty lists.

Cl1al)lt~r G deals with symbols. chapter 7 with the \'ariOl~s kinds of number:., and chaptcr 8 with arrays, Char[cr 9 explains character ~trillgs, which all' a special killd of array.

Aller this there arc some dwptcrs that expbi:l more abllll{ functions. function-calling, and Idaled Ill<lttcrs; Cklptcr iO prcselll~ ~lll Ihe kinds uf "UIlI:li,HI'i in the i;1I1gLl<!gC. cxplains fUl1ction- specs, ~lllt! tdls 110\\ l(l lII:lIdpuhl.l' dci1t1itiolls of l'ulI«i(;n~;, Ch:lpkrs 11 and 12 discuss closures and stack-groups. {,liO t;H.:ilitics usc-Ilil f()f creating COrl!l:linc~ and otller ,itkll1cl'd control and'

aCCC:-iS struclures.

DSK:LMMAN:f.NTRO 42 16-MAR-81

(17)

Notational Conventions and Helpful Notes 2 Lisp Machine Manual

Ncxt, a fcw lowcr-Icvel isslles are dealt with. Chapter 13 explains locatives, which are a 'kind of pointer to memory cells. Chapter 14 explains the "subprimitivc" functions, which are primarily lIseful for implementation of the Lisp language itself and the Lisp t'-.Iachinc·s "operating system".

Chapter 15 discusses areas, which give you control over storage allocation and locality of reference.

Chaprer 16 discusses thc Lisp compiler. which converts Lisp programs into "machine languagc". Chapter 17 explains the I.isp macro f:lci Ii cy . which allows users to write, their own extensions to Lisp. cxtending both the interpreter and thc compiler. The next two chapters go into detail about two such extensiolls. one that provides a powerful itcration control stnlcture (chapter 18), and Olll' that provides a powerful data structure nlciliry (ch,lptcr 19).

Chapter 20 docllmcnts flavors. a language facility to provide, gcncric functions using the paradigm used in Smalltalk and the Actor tilln i lies of languages, l'alled "object-oricnted programming" or" "message passing". Flavors are widely lIsed by the system programs of [he Lisp 1\lachine, as well as being available to the user as a language feature.

Chapler 21 explains the Lisp ~,Iachine's Input/Output system, including streams and the

printed I'/'[lf('sclltaliul/ (If

r

-isp objects. Chapter 22 duculllents how Lo deal with pathnames (the names of files).

Chapter 23 d\!~crihes the pack.:ge SYS[cI1I. which allows many name spaces within a single Lisp ellvironment, Chapter 14 dllculllcnL<; Lhe, "system" fllciIi ty, which helps you create and mnintain programs that reside in many meso

Chapter 25 discusses the facilities for multiple processes :l11d how to. write programs that use concurrent computation. Chapter 26 explains how exceptional conditions (errors) can be handled by programs, handled by 'users, and debugged. Chapter 27 explains the instruction set of the Lisp Machine, and tells you how to examine the output of the' compiler. Chapter 28 documents some functions till' qUNying the liSeI', chapter 30 explains some funclions fix manipulating dates and timet), and chapter 31 contains other miscellaneous functions and facilities.

1.3 Notational Conventions and Helpful Notes

There arc several conventions of notation, and various points that should be understood before reading the manual to avoid confusion. This section explains those conventions.

The symbol "=}" will be used to indicate evaluation in examples. Thus, when you see "foo

=} nil", this means thc same thing as "the result of evaluating.too is (or would have been) nil".

The symbul "= =)" will be lIsed to indicate macro expansion in' examples. This, when you see "(foo bar)

= =>

(aref bar 0)", this means the same thing as "the resulL of macro-expanding (faD bur) is (or would have been) (are'A bar 0)".

A typical description of a Lisp fUllction looks like this:

DSK:UvIMAN;I.NTRO 42 16-M/\R-81

(18)

Lisp Machine Manual 3 Notational Conventions and Helpful Notes

funct ion-name argl arg2 &optional argJ (arg4 (faa 3»

The function-name function adds together argl and arg2, and then multiplies the result by arg3. If arg3 is not provided, the multiplication isn't done. function-name then returns a list whose first element is this result and whose second element is arg4. Here is an example:

(function-name 3 4) => (7 (3 food}) (function-name 122 'bar) => (6 bar)

Note the use of fonts (typefaces). The name of the function is in bold-face in the first line of the description. and the arguments are in italics. Within the text, printed representations of Lisp objects are in a difTerent bold-fact font, such as (+ faa 56), and argument references are italicized, such as argl and arg2. 1\ different, fixed-width font, such as funct i on-name, is used for Lisp examples that are set olf from the text.

The word "&optional" in the list of arguments lells you that all of the arguments past this point are optional. The default value can be specified explicitly, as with llrg4 whose dct~llIlt value is the result of evaluating the form (faa 3). If no default value is specified, it is the symbol nil.

This syntax is used in lambda-lists in the language, which are explained in section 3.2, page 18.

Argument lists may also contain "&rest", which is part of the same syntax.

Descriptions of variables, special forms, macros, and methods look like this:

typical-variable Variable

The variable typical-variable is used for typical things ....

do-three-times Special Fonn

A do-three-times form looks like (do-three-times foml) It evaluates fonn three times.

with-foo-bound-to-nil Macro

The form (with-foo-bound-to-nil form} fonn2 ... } evaluates the fonns with the symbol faa bound to nil. [t expands as follows:

(with-foo-bound-to-nil foml}

form2 ... ) ==>

(let «faa nil)}

form/

foml2 ... )

message-name arg/ arg2 &optional arg3 (to flavor-name)

This is the documentation of the effect of sending a message named message-name, with arguments arg/, arg2, and arg3, to an instance of flavor flavor-name.

Most numbers shown are in octal radix (base eight). Spelled out numbers and numbers followed by a decimal point are in decimal. This is because, by default, Lisp Machine Lisp types out numbers in base 8; don't be surprised by this. To change it, see the documentation 011 the variables ibase and base (page 283).

DSK:LMMAN;I.NTRO 42 16-MAR-81

(19)

Notational Conventions and Helpful Notes 4 Lisp Machine Manual

All uses of the phrase "Lisp reader", unless further qualified, refer to the part of Lisp which reads characters from 1I0streams (the read function), and not the person reading this manual.

There are several terms which are used widely in other references on Lisp, but are not used much in this document since they have become largely obsolete and misleading. For the benefit of those who may have seen them before, they are: "S-expression", which means a Lisp object;

"Dotted pair", which means a cons; and "Atom", which means, roughly, symbols and numbers and sometimes other things, but not conses. The terms "list" and "tree" are defined in chapter 5, page 48.

The characters acute accent (' ) (also called "single quote") and semicolon (;) have special meanings when typed to Lisp; they are examples of what are called macro characters. Though the mechanism of macro characters is not of immediate interest to the new user, it is important to understand the effect of Ulese two, which are used in the examples.

When the Lisp reader encounters a "''', it reads in the next Lisp object and encloses it in a quote special form. That is, 'faa-symbol turns into (quote faa-symbol), and '(cons 'a 'b) turns into (quote (cons (quote a) (quote b))). The reason for this is that "quote" would otherwise have to be typed in very frequently, and would look ugly.

The semicolon is used as a commenting character. When the Lisp reader sees one, the remainder of the line is discarded.

The character .. /" is used for quoting strange characters so that they arc not interpreted in their lIsual way by the Lisp reader, but rather are treated the way normal alphabetic characters are treated. So, tor example, in order to give a

"r

to the reader, you must type "1/",. the first

"/" quoting the second one. When a character is preceeded by a "I" it is said to be slashified.

Slashifyillg also turns off the effects of macro characters such as .. , " and ";".

The fi)lIowing characters also have special meanings, and may not be used in symbols without slashification. These characters are explained in detail in the section on printed-representation (section 21.2.2, page 283) .

"

#

. Double-quote delimits character strings.

. Number-sign introduces miscellaneous reader macros.

Backquote is used to construct list structure.

Comma is used in conjunction with backquote.

Colon is the package prefix.

Characters between pairs of vertical-bars are quoted.

8 Circle-cross lets you type in characters using their octal codes.

;\11 Lisp code in this manual is written in lower case. In fact, the reader turns all symbols into upper-case. and consequently everything prints out in upper case. You may write programs in whichever case you prefer.

You will see various symbols that have the colon (:) character in their names. By convention, all "keyword". symbols in the Lisp machine system have names starting with a colon. The colon character is not actually part of the print name; but is a package prefix indicating that the symbol belongs to the package with a null name, which means the user package. So, when you print

DSK:LMMAN;I.NTRO 42 16-MAR-81

(20)

· Lisp Machine Manual 5 Notational Conventions and Helpful Notes

such a symboL you won't see the colon if the current package is user. However, you should always type in the colons where the manual tells you to. This is all explained in chapter 23;

until you read that, just make believe that the colons are part of the names of the symbols, and don't worry that tl1CY sometimes don't get printed out for keyword symbols.

This manual documents a number of internal functions and variables, which can be identified by the "si:" prefix in their names. The "si" stands for "system internals". These functions and variables are documented here because they are things you sometimes need to know about.

However, they are considered internal to tlle system and their behavior is not as guaranteed as that of everything else. They may be changed in the future.

Lisp Machine Lisp is descended from Mac1isp, and a good deal of effort was expended to try to allow Maclisp programs to run in Lisp Machine l.isp. Throughout the manual, there are notes about differences between the dialects. For the new lIscr, it is important to note that many functions herein exist solely filr Maclisp compatibility: they should no/ be used in new programs.

Such functions are clearly marked in the text.

The Lisp Machine character set is not quite the same as that lIsed on I.T.S. nor on Multics;

it is described in full detail elsewhere in the manual. The important thing to note for now is that the character "newline" is the same as "return", and is represented by the number 215 octal.

(This Illllnber should nol be built into any programs.)

When the text speaks of "typing Control-Q" (for example), this means to hold down me CTRL key on the keyboard (either of the two), and, while holding it down, to strike tlle "Q"

key. Similarly, to lype "Meta"?", hold down eiuICf or lhl! META key:, and strike "P". To type

"Control-Meta-T" hold down both CTRL and META. Unlike ASCn, tllere arc no "control characters" in tlle character set; Control and Meta are merely things that can be typed on me keyboard.

rvIany of the functions refer to "areas". The area feature is only of interest to writers of large systems, and can be safely disregarded by me casual lIser. [t is described in chapter 15.

DSK:LMMAN; LNTRO 42 16-MAR-Sl

(21)

Primitive Object Types 6 Lisp Machine Manual

2. Prilnitive Object '"fypes

2.1 Data Types

This section enumerates some of the various different pmmt!ve types of objects in Lisp Machine Lisp. The types explained below include symbols, conses, various types of numbers, two kinds of compiled code objects, locatives, arrays, stack groups, and closures. With each is given the associated symbolic name, which is returned by the function data -type (page 158).

;\ symbol (these are sometimes called "atoms" or "atomic symbols" by other texts) has a print name, a bindillg, a definition. a property list. and a package.

The print name is a string, which may be obtained by the function get-pname (page 81).

This string serves 'IS the printed representation (sec section 21.2.1, page 280) of the symbol. Each symbol has a bindil/;j (sometimes also called the "value"), which may be any Lisp object. It is also referred to sometimes as the "contents of the value cell", since internally every symbol has a cell called the value cell which holds the binding. It is accessed by the symeval function (page 78), and updated by the set function (page 78). (That is, given a symbol, you usc symeval to find out what its binding is, ami usc set to change its binding.) Each symbol has a definition, which may also be any Lisp object. It is also referred to as the "contents of the function cell", since internally every symbol has a cell called the jLlIlclion cell which holds the definition. The definition can be accessed by the fsymeval Function (page 79), and updated with fset (page 79), although usually the tlll1ctions fdefinition and fdefine arc employed (page 135). The property list is a list of an c\cn number ()f elenwnts; it can be aCL"cs'>cd directly· by plist (page 80), and updated directly by setplist (page 80), although usually the functions get. putprop, and remprop (page 67) arc used. The property list is used (() associate any number of additional attributes with a symbol-attributes not used frequently enough to deserve their own cells as the value and deHnition do. Symbols also have a package cell, which indicates which "package" of names the symbol belongs to: This is explained further in the section on packages (chapter 23) and can be disregarded by the casual user.

The primitive function for creating symbols is make-symbol (page 82), although most symbols are created by read. intern, or fasload (which call make-symbol themselves.)

A COilS is an object that cares about two other objects, arbitrarily named the car and the cdr.

These objects can be accessed with car and cdr (page 49), and updated with rplaca and rplacd (page 57). The primitive function for creating conses is cons (page 49).

There arc several kinds of numbers in Lisp Machine Lisp. FiXIlUI71S represent integers in the range of -2t23 to 2t23-1. Bigllums represent integers of arbitrary size, but they are more expensive to use than fixl1ums because they occupy storage and arc slower. The system automatically converts between fixnums and bigl1ums as required. FlollulIls are floating-point numbers. SlJIall-jlollutllS arc another kind of floating-point numbers, with less range and precision, but less computational overhead. Other types of numbers are likely to be added in the future.

Sec chapter 7, page 84 for full details of these types and the cOllversions between them.

The usual form of compiled. executable code is a Lisp object called a "Function Entry Frame" or "FEF". A FEF contains the code for one function. This is analogous to what Maclisp calls a "subr pointer". FEFs arc produced by the Lisp Compiler (chapter 16, page 181), and are

DSK:LMMAN;FD.DTP 37 16-MAR-81

(22)

Lisp Machine Manual 7 Predicates

lIsually found as the definitions of symbols. The printed representation of a FEF includes its name, so that it can be identified.

Another Lisp object which represents executable code is a "micro-code entry". These are the microcoded primitive functions of the Lisp system, and user functions compiled into microcode.

About the only useful thing to do with any of these compiled code objects is to apply it to arguments. However, some functions are provided for examining such objects, for user convenience. See arglist (page 137), args-info (page l38), describe (page 448), and disassemble (page 448).

A locative (sec chapter 13, page 156) is a kind of a pointer to a single memory cell anywhere in the system. The contents of this cell can be accessed by cdr (see page 49) and updated by rplacd (see page 57) ..

An arra), (sec chapter 8, page 98) is a set of cells indexed by a tuple of integer subscripts.

The contents of the cells may be accessed and changed individually. There are several types of arrays. Some have cells which may contain any object, while others (numeric arrays) may only contain small positive numbers. Strings are a type of array; the clement.') are 8-bit unsigned numbers which encode characters.

A list is not a primitive data type, but rather a data structure made up Gut of conses and the symbol nil. See chapter 5, page 48.

2.2 Predicates

A predicate is a function which tests for some condition involving its arguments and returns the symbol t if the condition is true, or the symbol nil if it is not true. Most of the following predicates are for testing what data type an object has; some other general-purpose predicates are

also explained. .

By convention, the names of predicates usually end in the letter "p" (which stands for

"predicate").

The following predicates are for testing data types. These predicates return t if the argument is of the type indicated by the name of the function, nil if it is of some other type.

symbo lop arg

symbolp returns t if its argument is a symbol, otherwise nil.

nsymbolp arg

nsymbolp returns nil if its argument is a symbol, otherwise t.

11stp arg

listp returns t if its argument is a cons, otherwise nil. Note thut this means (listp nil) is nil even though nil is the empty list. [[his may be changed in the future.]

DSK:LMMAN;FD.DTP 37 16-MAR-81

(23)

Predicates 8 Lisp Machine Manual

n11 stp arg

nlistp returns t if its argument is anything besides a cons, otherwise nil. This is the recommended predicate for tcnninating iterations or recursions on lists. It is, in fact, identical to atom, and so (nlistp nil) returns t. [fhis may be changed in the future, if and when listp is changed.]

atom arg

TIle prcdicate atom returns t if its argument is not a cons, otherwise nil.

f1xp arg

fixp returns t if its argument is a fixnum or a bignum, otherwise nil.

f10atp arg

floatp returns t if its argument is a flonum or a small Honum, otherwise nil.

smal1-floatp arg

small-floatp returns t if arg is a small Honum, otherwise nil.

b1gp a~ .

bigp returns t if arg is a bignum, otherwise nil.

numberp arg

numberp returns t if its argument is any kind of number, otherwise nil.

str 1 ngp arg

stringp returns t if its argument is a string, otherwise nil.

arrayp arg

arrayp returns t if its argument is an array, otherwise nil. ,Note that strings are arrays.

subrp arg

subrp returns t if its argument is any compiled code object, otherwise nil. The Lisp Machine system doesn't use the term "subr", but the name of this function comes from Maclisp.

closurep arg

closurep returns t if its argument is a closure, otherwise nil.

ent1typ arg

entityp returns t if its argument is an entity, otherwise nil. See section 11.4, page 148 for information about "entities".

locat 1vep arg

locativep returns t if its argument is a locative, otherwise nil.

typep arg &optional type

typep is really two different functions. With one argument, typep is not really a predicate; it returns a symbol describing the type of its argument. With two arguments, typep is a predicate which returns t if arg is of type type, and nil otherwise. Note that an object can be "of" more than one type, since one type can be a subset of another.

DSK:LMMAN ;FD.DTP 37 16-MAR-81

(24)

I -isp Machine Manual 9

The symbols that can be returned by typep of one argument are:

:symbol :fixnum :bignum :flonum

arg is a symbol.

arg is a fixnum (not a bignum).

arg is a bignllm.

arg is a Honum (not a small-flonum).

:small-flonum arg is a small flonum.

:Iist :Iocative

arg is a cons.

arg is a locative pointer (see chapter 13, page 156).

:compiled - function

Predicates

mg is the machine code for a compiled function (sometimes called a FEF).

:microcode-function

arg is a function written in microcode.

:closure arg is a closure (see chapter iI, page 144).

:select - method

arg is a select-method table (see p"ge 131).

:stack -group ar!; is a stack-group (see chapter 12, page 149).

:string arg is a string.

:array arg is an array that is nota string.

:random Returned for any built-in data type that does not fit into one of the above categories.

faa An object of user-defined data type foo (any symbol). The primitive type of the object could be array, instance, or entity. See Named Structures, page 239, and Flavors, chapter 20, page 245.

The type argument to typep of two arguments can be any of the above keyword symbols (except for :random), the name of a user-dcfined data type (either a named structure or a flavor), or one of the following additional symbols:

:atom :fix :float :number :instance :entity

Any atom (as detennined by the atom predicate).

Any kind of fixed-point number (fixnum or bignum).

Any kind of floating-point number (flonum or small-flonllm).

Any kind of number.

An instance of any flavor. See chapter 20, page 245.

An entity. typep of one argument returns the name of the particular user- defined type of the entity, rather than :entity.

See also data -type, page 158.

Notc that (typep nil)

= >

:symbol, and (typep nil ':list)

= >

nil; the latter may be changed.

DSK:LMMAN;FD.DTP 37 16-MAR-81

(25)

Predicates 10 Lisp Machine Manual

The following functions are some other general purpose predicates.

eq x y

(eq x y) =

>

t if and only if x and yare the same object. It should be noted that things that print the same are not necessarily eq to each other. In particular, numbers with the same value need not be eq, and two similar lists are usually not eq.

Examples:

(eq 'a 'b) => nil (eq 'a 'a) => t

(eq (cons 'a 'b) (cons 'a 'b» => nil (setq x (cons 'a 'b» (eq x x) => t

Note that in Lisp Machine Lisp equal fixnums are eq; this is not true in Maclisp.

Equality does not imply eq-ness for other types 'of numbers. To compare numbers, use

=; see page 87.

naq x y

(neq x y) = (not (eq x y)). This is provided simply as an abbreviation for typing convenience.

equal x y

The equal predicate returns t if its argllments are similar (isomorphic) objects. (cf. eq) Two numbers are equal if Lhey have the Same value tind type (for exmnplc, a ftonum is never equal to a fixnum, even if = is true of them). For conses, equal is defined recursively as the two car's being equal and the two cdr's being equal. Two strings are equal if they have the same iength, and the characters composing them are the same; see string-equal, page 117. Alphabetic case is ignored (but see alphabetic-case-affects- string-comparison, page 116). All other objects are equal if and only if they are eq.

Thus equal could have been defined by:

(defun equal (x y) (cond «eq x y) t)

«neq (typep x) (typep y» nil)

«numberp x) (= x y»

«stringp x) (string-equal x y»

«listp x) (and (equal (car x) (car y»

(equal (cdr x) (cdr y»»»

As a consequence of the above definition, it can be seen that equal may compute forever when applied to looped list stmcture. In addition, eq always implies equal; that is, if (eq a b) then (equal a b). An intuitive definition of equal (which is not quite correct) is that two objects are equal if they look the same when printed out. For example:.

(setq a '(1 2 3») (setq b '(12 3») (eq a b) => nil (equal a b) => t

(equal "Faa" "foo") => t

DSK:LMMAN;FD.OP 28 16-MAR-81

(26)

Lisp Machine Manual

not x null x

11 Predicates

not returns t if x is nil, else nil. null is the same as not; both fimctions are included for the sake of clarity. Use null to check whether something is nil; use not to invert the sense of a logical value. Even though Lisp uses the symbol nil to represent falseness. you shouldn't make understanding of your program depend on this fortuitously. For example.

one. often writes:

(cond ((not (null 1st)} ... }

(

...

)}

rather than

(cond {1st ...

{

...

}}

There is no loss of efficiency, since these will compile into exactly the same instmctions.

DSK:LMMAN;FD.OP 28 16-MAR-81

(27)

Evaluation 12 Lisp Machine Manual

3. Evaluation

The following is a complete description of the actions taken by the evaluator, given a fonn to evaluate.

If f01111 is a number, the result is form.

If form is a string, the result is fOl1n.

If f017l1 is a symbol, the result is the binding of form. If f01711 is unbound, an error is signalled. The way symbols are bound is explained in section 3.l, page 13 below.

If form is not any of the above types, and is not a list, an error is signalled.

In all remaining cases, form is a list. The evaluator examines the car of the list to figure out wllat to do next. There arc three possibilities: this form may be a ,special f0/711, a macro fOl1n, or a plain-old jillletion forll1. Conceptually, the evaluator knows specially about all the symbols whose appearance in the car of a fOim make that form a special fDlm, but the way the evaluator actually works is as follows. If the car of the form is a symbol, the evaluator finds the object in

th~ function call of the symbol (sec chapter 6, page 78) and starts all over as if that object had been the car {If the list. If the car isn't a symbol. then if it's a cons whose car is the symbol macro. then this is a macro form; if it is a "special function" (sec page 129) then this is a special fOl1n; otherwise, it should be a regular function, and this is a function form.

If f0171l is a special foml. then it is handled accordingly; each special fOl1n works ditferently.

Ail of lhem arc documented in this manual. The internal workings of special fOlllS are explained in more detail in page 129, but this hardly ever affects you.

If f01711 is a macro form, then the macro is expanded as explained in chapter 17.

If f01111 is a function fonn, it calls for the application of a function to arguments. The car of the form is a function or the name of a function. The edr of the form is a list of subfonns.

Each subform is evaluated, sequentially. The values produced by evaluating the subforms are . called the "arguments" to the function. The function is then applied to those argul11ents.

Whatever results the function returns are the values of the original fonn.

There is a lot more to be said about evaluation. The way variables work and the ways in which they are manipulated, including the binding of arguments, is explained in section 3.1, page l3. A basic explanation of functions is in section 3.2, page 18. The way functions can return more than one value is explained in section 3.4, page 26. The description of all of the kinds of functions, and the means by which they are manipulated, is in chapter 10. Macros are explained in chapter 17. The evalhook facility, which lets you do something arbitrary whenever the evaluator is invoked, is explained in section 26.6, page 4l3. Special forms are described all over the manual; each special fonn is in the section on the facility it is part of.

DSK:LMMAN ;FD.EVA 69 16-MAR-S1

(28)

Lisp Machine Manual 13 Variables

3.1 Variables

In Lisp Machine Lisp, variables are implemented using symbols. Symbols are used for many things in the language, such as naming functions, naming special forms, and being keywords;

they are also useful to programs written in Lisp, as parts of data structures. But when the evaluator is given a symbol, it treats it as a variable, using the value cell to hold the value of the variable. If you evaluate a symbol, you get back the contents of the symbol's value cell.

There are two different ways of changing the value of a variable. One is to set the variable.

Setting a variable changes its value to a new Lisp object, and the previous value of the variable is forgotten. Setting of variables is usually done with the setq special form.

The other way to change the value of a variable is with binding (also called "lambda- binding"). When a variable is hound, its old value is first saved away, and then the value of the variable is made to be the new I .isp object. When the binding is undone, the saved value is restored to be the value of the variable. Bindings are always followed by unbindings. The way this is enforced is that binding is only done by special forms that are defined to bind some variabfes, then evaluate some sub forms, and then unbind those variables. So the variables are all unbound when the form is finished. This means that the evaluation of the form doesn't disturb the values of the variables that are bound; whatever their old value was, before the evaluation of the form. gets restored when the evaluation of the form is completed. If such a form is exited by a non-local exit of any kind, such as *throw (see page 43) or return (sec page 41), the bindings are undone whenever the form is exited.

The simplest construct fur binding variables is the let special flJrITl. The do and prog special Forms can also bind variables, in the same way let does, but they also control the tlow of the program and so arc explained elsewhere (see page 35). let* is just a sequential version of let; the other special Fonns below are only used for esoteric purposes.

Binding is an important part of the process of applying interpreted functions to arguments.

This is explained in the next section.

When a Lisp function is compiled, the compiler understands the use of symbols as variables.

However, the compiled code generated by the compiler does not actually use symbols to represent variables. Rather, the compiler converts the references to variables within the program into more efficient references, that do not involve symbols at all. A variable that has been changed by the compiler so that it is not implemented as a symbol is called a "local" variable. When a local variable is bound, a memory cell is allocated in a hidden, internal place (the Lisp control stack) and the value of the variable is stored in this cell. You cannot use a local variable without first binding it; you can only use a local variable inside of a special form that binds that variable.

Local variables do not have any "top level" value; they do not even exist outside of the form that binds them.

The variables which are associated with symbols (the kind which are used by non-compiled programs) are called "special" variables.

Local variables and special variables do not behave quite the same way, because "binding"

means different things for the two of them. Binding a special variable saves the old value away and then uses the value cell of the symbol to hold the new value, as explained above. Binding a local variable, however, does not do anything to the symbol. In fact, it creates a new memory cell to hold the value, i.e. a new local variable.

DSK:LMMAN;FD.EVA 69 16-MAR-81

(29)

Variables 14 I -isp Machine Manual

Thus, if you compile a function, it may do different things after it has been compiled. Here is an example:

(setq a 2) (defun foo ()

(let ((a 5)) (bar))) (defun bar ()

a)

( foo) => 5 (compile 'fool (foo) => 2

; Set the variable a to the value 2.

; Define a function named faa.

; Bind the symbol a to the value 5.

; CaU the function bar.

; Define a function nametl bar.

; [tjust returns the value of the variable a.

; Calling faa returns 5.

; Now compile faa.

; This time, calling faa returns 2.

This is a very bad thing, because the compiler is only supposed to speed things up, without changing what the function docs. Why did the function faa do something e1ilTerent when it was compiled? Because a was converted from a special variable into a loc<ll variable. After faa was compiled, it no longer had any effect on the vallie cell of the symbol a, and so the symbol retained its old contents, namely 2.

In most uses of variables in Lisp pro.'Srams, this problem doesn't collie lip. The reason it happened here is because the nl11ction bar refers to the symbol a without first binding a to anything. A reference to a variable that you didn't bind yourself is called a free reference; in this example, bar makes a free reference to a.

We mentioned above that you can't use a local variable without first binding it. Another way to say this is that you can't ever have a free reference to a local variable. If you try to do so, the compiler will complain. In order for our functions to work, the compiler must be told not to convert a into a local variable; a must remain a special variable. Normally, when a function is compiled, all variables in it are made to be "local". You can stop the compiler from making a variable local by "declaring" to the compiler that the variable is "special". When the compiler sees references to a variable that has been declared special, it uses the symbol . itself as the variable instead of making a local variable.

Variables can be declared by the special fOlms defvar and defconst (see below), or by explicit compiler declarations (see page 185). l11e most common use of special variables is as

"global" variables: variables used by many different nmctions throughout a program, that have top-level values.

Had bar been compiled, the compiler would have secn the ft'ce refcrence and printed a warning message: Warning: a declared special. It would have automatically declared a to be special and proceeded with the compilation. It knows that frec references mean that special declarations arc needed. But when a function is compiled that binds a variable that you want to be treated as a special variable but that you have not explicitly declared, there is, in general, no way for the compiler to automatically detect what has happened, and it will produce incorrect outpuLSo you must always provide declarations for all variables that you want to be treated as special variables.

DSK:LMMAN;FD.EVA 69 16-MAR-81

(30)

Lisp Machine Manual 15 Variables

When you declare a variable to be special using declare rather than local-declare, the declaration is "global"; that is, it applies wherever that variable name is seen. After fuzz has been declared special using declare, all following uses of fuzz will be treated by the compiler as references to the same special variable. Such variables arc called "global variables", because any fUllction can use them; their scope is not limited to one function. The special fOims defvar and defconst are useful for creating global variables; not only do they declare the variable special, but they also provide a place to specify its initial value, and a place to add documentation. In addition, since the names of these special fonl1s start with "def" and since they are used at the top-level of files, the Lisp Machine editor can find them easily.

Here are the special forms used for setting variables.

satq Special Form

The special fonn (setq l'arl jbrlll! l'ar2 jorIllL.) is used to set the value of a variable or of many variables. First jorm! is evaluated, and var! is set to the result. Then fOrtll2 is . evaluated, and var2 is set to the result, and so on for all the variables. setq returns the last value set, i.e. the result of the evaluation of its last sub form.

Example:

(setq x (+ 3 2 1) Y (cons x nil»

x is set to 6, y is set to (6), and the setq form returns (6). Note that the first set was performed before the second form was evaluated, allowing that form to use the new value of x.

psetq Special Fom!

A. psetq form is just like a setq form, except that the variables are set "in parallel"; first all of the torms are evaluated, and then the symbols are set to the resulting values.

Example:

(setq a 1) (setq b 2)

(psetq a b b a)

a => 2 b => 1

Here are the special forms used for binding variables.

1 at Special Form

let is used to bind some variables to some objects. A let fOim looks like (1 et (( var! vforml)

(var2 vform2)

' " ) bfonnl bform2

...

)

When this form is evaluated, first the vjomls are evaluated. Then the vars are bound to the values returned by the corresponding Ifunlls. Thus the bindings happen in parallel;

all the Ifo/"lIls are evaluated before any of the vars are bound. Finally, the bforllls are evaluated sequentially, the old values of the variables 'are restored, and the result of the last bfoml is returned.

DSK:LMMAN;FD.EVA 69 16-MAR-81

(31)

Variables 16 J.isp Machine Manual

lat- Special Form

. let* is the same as let except that the binding is sequential. Each l'ar is bound to the value of its 110rll1 before the next 1101711 is evaluated. This is useful when the computation of a 1101711 depends on the value of a variable bound in an earlier vform.

1 et-if Special Fonn

let- if is a variant of let in which the binding of variables is conditional. The variables must all be special variables. The special fOlm

( 1 e t -i f cond

(( vad val-I) (var-2 val-2) . .. ) body-fonlll bo(fj'-form2 ... )

first evaluates the predicate form condo J I' the result is non-nil, the value forms I'al-i, vaJ-2, etc. are evaluated and then the variables var-I, var-2. etc. are bound to them. If the result is nil, the vars and l'als arc ignored. Finally the body forms are evaluated.

1 at -9 lob a 11 y Special Foml

let-globally is similar in form to let (see page 15). The difference is that let-globally does not bind the variables; instead, it saves the old values and sets the variables, and sets up an unwind -protect (sec pagc 44) to set them back. The illlportant difference between let-globally and let is that when the current stack group (sec chapter 12, page 14\)) co-calls some other stack group, the old values of the variables arc /lot restored.

Thus let- globally makes the new valucs visible in all stack groups and processes that don't bind the variables themselves. not just thc current stack group.

pl'ogv Special Form

progv is a special form to provide d1C user with extra control over binding. It binds a list of special variables to a list of values, and d1en evaluates some forms. The lists of special variables and values are computed quantitics; d1is is· what makes progv different from let, prog, and do;

(progv symbol-list value-list f017lli /011112 ... )

first evaluates symbol-list and value-list, and dlen binds each symbol to the corresponding value. If too few values are supplied, d1C remaining symbols arc bound to nil. If too many values are supplicd, the excess values are ignored.

After fue symbols havc been bound to dIe values, the forms are evaluatcd, and finally the symbols' bindings are undone. The result rcturned is dIe value of the last form.

Example:

(setq a 'faa b 'bar)

(progv (list a b 'b) (list b) (list a b foo bar))

=> (faa nil bar nil)

During dle evaluation of d1e body of this progv, faa is bound to bar, bar is bound to nil, b is bound to nil, and a retains its top-level valuc faa.

DSK:LMMAN;FD.EVA 69 16-MAR-81

Références

Documents relatifs

set lispval.. This type is used mainly by arrays of general lisp objects. Value cells are created with the ptr function. The access time for an element of a

Common Lisp details the functions and the calling conventions of standard COM- MON LISP, while Tek COMMON LISP User Guide describe features of this implementation

In addition, it has been enhanced by a fast, solid implementation of Flavors and a rich, modeless top level with extensive intrinsic debugging facilities, and A

It would be inconvenient to do without side-effects entirely. However, as you read further, you may be surprised to discover how few you really need. And the more side-effects you

When you type expressions at a Command Prompt, or use CL’s load Function to load lisp source files directly, you are introducing interpreted code into the system.. This code does

Advanced Datatypes and Generic Programming 161 Varying speed for operations: In Common Lisp, if you create a small hash table, and then fill it with values, you will find

Macros took a major step forward with Lisp-Machine Lisp, which consolidated the vari- ous macro-defining techniques into two standardized features that were adopted throughout

(In Emacs, obarrays are vectors whose elements are initially zero. The emulator uses the same representation so that.. 16 2 THE INTERPRETER Emacs Lisp code that creates such