My various dotfiles

chap-3.texi 250KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048
  1. @node Evaluation and Compilation, Types and Classes, Syntax, Top
  2. @chapter Evaluation and Compilation
  3. @menu
  4. * Evaluation::
  5. * Compilation::
  6. * Declarations::
  7. * Lambda Lists::
  8. * Error Checking in Function Calls::
  9. * Traversal Rules and Side Effects::
  10. * Destructive Operations::
  11. * Evaluation and Compilation Dictionary::
  12. @end menu
  13. @node Evaluation, Compilation, Evaluation and Compilation, Evaluation and Compilation
  14. @section Evaluation
  15. @c including concept-eval
  16. @i{Execution} of @i{code} can be accomplished by a variety of means ranging
  17. from direct interpretation of a @i{form} representing a @i{program}
  18. to invocation of @i{compiled code} produced by a @i{compiler}.
  19. @i{Evaluation}
  20. @IGindex{evaluation}
  21. is the process by which a @i{program} is @i{executed} in @r{Common Lisp}.
  22. The mechanism of @i{evaluation} is manifested
  23. both implicitly through the effect of the @i{Lisp read-eval-print loop},
  24. and explicitly through the presence of the @i{functions}
  25. @b{eval},
  26. @b{compile},
  27. @b{compile-file},
  28. and @b{load}.
  29. Any of these facilities might share the same execution strategy,
  30. or each might use a different one.
  31. The behavior of a @i{conforming program} processed by @b{eval}
  32. and by @b{compile-file} might differ; see @ref{Semantic Constraints}.
  33. @i{Evaluation} can be understood in terms of a model in which an
  34. interpreter recursively traverses a @i{form} performing each
  35. step of the computation as it goes.
  36. This model, which describes the semantics of @r{Common Lisp} @i{programs},
  37. is described in @ref{The Evaluation Model}.
  38. @menu
  39. * Introduction to Environments::
  40. * The Evaluation Model::
  41. * Lambda Expressions::
  42. * Closures and Lexical Binding::
  43. * Shadowing::
  44. * Extent::
  45. * Return Values::
  46. @end menu
  47. @node Introduction to Environments, The Evaluation Model, Evaluation, Evaluation
  48. @subsection Introduction to Environments
  49. A @i{binding}
  50. @IGindex{binding}
  51. is an association between a @i{name} and
  52. that which the name denotes. @i{Bindings} are @i{established}
  53. in a @i{lexical environment} or a @i{dynamic environment}
  54. by particular @i{special operators}.
  55. An @i{environment}
  56. @IGindex{environment}
  57. is a set of @i{bindings} and other information
  58. used during evaluation (@i{e.g.}, to associate meanings with names).
  59. @i{Bindings} in an @i{environment} are partitioned into @i{namespaces}
  60. @IGindex{namespace}
  61. .
  62. A single @i{name} can simultaneously have more than one
  63. associated @i{binding} per @i{environment},
  64. but can have only one associated @i{binding} per @i{namespace}.
  65. @menu
  66. * The Global Environment::
  67. * Dynamic Environments::
  68. * Lexical Environments::
  69. * The Null Lexical Environment::
  70. * Environment Objects::
  71. @end menu
  72. @node The Global Environment, Dynamic Environments, Introduction to Environments, Introduction to Environments
  73. @subsubsection The Global Environment
  74. The @i{global environment}
  75. @IGindex{global environment}
  76. is that part of an @i{environment}
  77. that contains @i{bindings} with both @i{indefinite scope}
  78. and @i{indefinite extent}.
  79. The @i{global environment} contains, among other things, the following:
  80. @table @asis
  81. @item @t{*}
  82. @i{bindings} of @i{dynamic variables} and @i{constant variables}.
  83. @item @t{*}
  84. @i{bindings} of @i{functions}, @i{macros}, and @i{special operators}.
  85. @item @t{*}
  86. @i{bindings} of @i{compiler macros}.
  87. @item @t{*}
  88. @i{bindings} of @i{type} and @i{class} @i{names}
  89. @item @t{*}
  90. information about @i{proclamations}.
  91. @end table
  92. @node Dynamic Environments, Lexical Environments, The Global Environment, Introduction to Environments
  93. @subsubsection Dynamic Environments
  94. A @i{dynamic environment}
  95. @IGindex{dynamic environment}
  96. for @i{evaluation} is that part of an
  97. @i{environment} that contains @i{bindings} whose duration
  98. is bounded by points of @i{establishment} and @i{disestablishment}
  99. within the execution of the @i{form} that
  100. established the @i{binding}.
  101. A @i{dynamic environment} contains, among other things, the following:
  102. @table @asis
  103. @item @t{*}
  104. @i{bindings} for @i{dynamic variables}.
  105. @item @t{*}
  106. information about @i{active} @i{catch tags}.
  107. @item @t{*}
  108. information about @i{exit points} established by @b{unwind-protect}.
  109. @item @t{*}
  110. information about @i{active} @i{handlers} and @i{restarts}.
  111. @end table
  112. The @i{dynamic environment} that is active at any given point
  113. in the @i{execution} of a @i{program} is referred to by
  114. definite reference as ``the current @i{dynamic environment},''
  115. or sometimes as just ``the @i{dynamic environment}.''
  116. Within a given @i{namespace},
  117. a @i{name} is said to be @i{bound}
  118. in a @i{dynamic environment} if there is a @i{binding}
  119. associated with its @i{name} in the @i{dynamic environment}
  120. or, if not, there is a @i{binding}
  121. associated with its name in the @i{global environment}.
  122. @node Lexical Environments, The Null Lexical Environment, Dynamic Environments, Introduction to Environments
  123. @subsubsection Lexical Environments
  124. A @i{lexical environment}
  125. @IGindex{lexical environment}
  126. for @i{evaluation} at some position in a @i{program}
  127. is that part of the @i{environment} that contains information having
  128. @i{lexical scope} within the @i{forms} containing that position.
  129. A @i{lexical environment} contains, among other things, the following:
  130. @table @asis
  131. @item @t{*}
  132. @i{bindings} of @i{lexical variables} and @i{symbol macros}.
  133. @item @t{*}
  134. @i{bindings} of @i{functions} and @i{macros}.
  135. (Implicit in this is information about those @i{compiler macros}
  136. that are locally disabled.)
  137. @item @t{*}
  138. @i{bindings} of @i{block tags}.
  139. @item @t{*}
  140. @i{bindings} of @i{go tags}.
  141. @item @t{*}
  142. information about @i{declarations}.
  143. @end table
  144. The @i{lexical environment} that is active at any given position
  145. in a @i{program} being semantically processed is referred to by
  146. definite reference as ``the current @i{lexical environment},''
  147. or sometimes as just ``the @i{lexical environment}.''
  148. Within a given @i{namespace},
  149. a @i{name} is said to be @i{bound} in a @i{lexical environment}
  150. if there is a @i{binding}
  151. associated with its @i{name}
  152. in the @i{lexical environment} or, if not, there is a @i{binding}
  153. associated with its name in the @i{global environment}.
  154. @node The Null Lexical Environment, Environment Objects, Lexical Environments, Introduction to Environments
  155. @subsubsection The Null Lexical Environment
  156. The @i{null lexical environment}
  157. @IGindex{null lexical environment}
  158. is equivalent to the @i{global environment}.
  159. Although in general the representation of an @i{environment} @i{object}
  160. is @i{implementation-dependent}, @b{nil} can be used in any situation where an
  161. @i{environment} @i{object} is called for in order to denote
  162. the @i{null lexical environment}.
  163. @node Environment Objects, , The Null Lexical Environment, Introduction to Environments
  164. @subsubsection Environment Objects
  165. Some @i{operators} make use of an @i{object},
  166. called an @i{environment object}
  167. @IGindex{environment object}
  168. ,
  169. that represents the set of @i{lexical bindings} needed to perform
  170. semantic analysis on a @i{form} in a given @i{lexical environment}.
  171. The set of @i{bindings} in an @i{environment object}
  172. may be a subset of the @i{bindings} that would be needed to actually
  173. perform an @i{evaluation}; for example, @i{values} associated with
  174. @i{variable} @i{names} and @i{function names} in the corresponding
  175. @i{lexical environment} might not be available in an @i{environment object}.
  176. The @i{type} and nature of an @i{environment object} is @i{implementation-dependent}.
  177. The @i{values} of @i{environment parameters} to @i{macro functions}
  178. are examples of @i{environment objects}.
  179. The @i{object} @b{nil} when used as an @i{environment object}
  180. denotes the @i{null lexical environment};
  181. see @ref{The Null Lexical Environment}.
  182. @node The Evaluation Model, Lambda Expressions, Introduction to Environments, Evaluation
  183. @subsection The Evaluation Model
  184. A @r{Common Lisp} system evaluates @i{forms} with respect to lexical,
  185. dynamic, and global @i{environments}. The following sections
  186. describe the components of the @r{Common Lisp} evaluation model.
  187. @menu
  188. * Form Evaluation::
  189. * Symbols as Forms::
  190. * Lexical Variables::
  191. * Dynamic Variables::
  192. * Constant Variables::
  193. * Symbols Naming Both Lexical and Dynamic Variables::
  194. * Conses as Forms::
  195. * Special Forms::
  196. * Macro Forms::
  197. * Function Forms::
  198. * Lambda Forms::
  199. * Self-Evaluating Objects::
  200. * Examples of Self-Evaluating Objects::
  201. @end menu
  202. @node Form Evaluation, Symbols as Forms, The Evaluation Model, The Evaluation Model
  203. @subsubsection Form Evaluation
  204. @i{Forms} fall into three categories:
  205. @i{symbols}, @i{conses}, and @i{self-evaluating objects}.
  206. The following sections explain these categories.
  207. @node Symbols as Forms, Lexical Variables, Form Evaluation, The Evaluation Model
  208. @subsubsection Symbols as Forms
  209. If a @i{form} is a @i{symbol},
  210. then it is either a @i{symbol macro} or a @i{variable}.
  211. The @i{symbol} names a @i{symbol macro}
  212. if there is a @i{binding} of the @i{symbol} as a @i{symbol macro}
  213. in the current @i{lexical environment}
  214. (see @b{define-symbol-macro} and @b{symbol-macrolet}).
  215. If the @i{symbol} is a @i{symbol macro},
  216. its expansion function is obtained.
  217. The expansion function is a function of two arguments, and is invoked
  218. by calling the @i{macroexpand hook} with
  219. the expansion function as its first argument,
  220. the @i{symbol} as its second argument,
  221. and an @i{environment object} (corresponding to the current @i{lexical environment})
  222. as its third argument.
  223. The @i{macroexpand hook}, in turn, calls the expansion function with the
  224. @i{form} as its first argument and the @i{environment} as its second argument.
  225. The @i{value} of the expansion function, which is passed through
  226. by the @i{macroexpand hook}, is a @i{form}.
  227. This resulting @i{form} is processed in place of the original @i{symbol}.
  228. If a @i{form} is a @i{symbol} that is not a @i{symbol macro},
  229. then it is the @i{name} of a @i{variable}, and the @i{value} of that
  230. @i{variable} is returned. There are three kinds of variables:
  231. @i{lexical variables},
  232. @i{dynamic variables},
  233. and
  234. @i{constant variables}.
  235. A @i{variable} can store one @i{object}.
  236. The main operations on a @i{variable} are
  237. to @i{read}_1 and
  238. to @i{write}_1
  239. its @i{value}.
  240. An error of @i{type} @b{unbound-variable} should be signaled if
  241. an @i{unbound variable} is referenced.
  242. @i{Non-constant variables} can be @i{assigned} by using @b{setq}
  243. or @i{bound}_3 by using @b{let}.
  244. Figure 3--1 lists some @i{defined names} that
  245. are applicable to assigning, binding, and defining @i{variables}.
  246. @group
  247. @noindent
  248. @w{ boundp let progv }
  249. @w{ defconstant let* psetq }
  250. @w{ defparameter makunbound set }
  251. @w{ defvar multiple-value-bind setq }
  252. @w{ lambda multiple-value-setq symbol-value }
  253. @noindent
  254. @w{ Figure 3--1: Some Defined Names Applicable to Variables}
  255. @end group
  256. The following is a description of each kind of variable.
  257. @node Lexical Variables, Dynamic Variables, Symbols as Forms, The Evaluation Model
  258. @subsubsection Lexical Variables
  259. A @i{lexical variable} is a @i{variable} that can be referenced only within
  260. the @i{lexical scope} of the @i{form} that establishes that @i{variable};
  261. @i{lexical variables} have @i{lexical scope}.
  262. Each time a @i{form} creates a @i{lexical binding} of a @i{variable},
  263. a @i{fresh} @i{binding} is @i{established}.
  264. Within the @i{scope} of a @i{binding} for a @i{lexical variable} @i{name},
  265. uses of that @i{name} as a @i{variable} are considered to be references
  266. to that @i{binding} except where the @i{variable} is @i{shadowed}_2
  267. by a @i{form} that @i{establishes} a @i{fresh} @i{binding} for that
  268. @i{variable} @i{name},
  269. or by a @i{form} that locally @i{declares} the @i{name} @b{special}.
  270. A @i{lexical variable} always has a @i{value}.
  271. There is no @i{operator} that introduces a @i{binding} for a
  272. @i{lexical variable} without giving it an initial @i{value}, nor
  273. is there any @i{operator} that can make a @i{lexical variable} be @i{unbound}.
  274. @i{Bindings} of @i{lexical variables} are found in the @i{lexical environment}.
  275. @node Dynamic Variables, Constant Variables, Lexical Variables, The Evaluation Model
  276. @subsubsection Dynamic Variables
  277. A @i{variable} is a @i{dynamic variable} if one of the following
  278. conditions hold:
  279. @table @asis
  280. @item @t{*}
  281. It is locally declared or globally proclaimed @b{special}.
  282. @item @t{*}
  283. It occurs textually within a @i{form} that
  284. creates a @i{dynamic binding} for a @i{variable} of the @i{same} @i{name},
  285. and the @i{binding} is not @i{shadowed}_2 by a @i{form}
  286. that creates a @i{lexical binding} of the same @i{variable} @i{name}.
  287. @end table
  288. A @i{dynamic variable} can be referenced at any time in any @i{program};
  289. there is no textual limitation on references to @i{dynamic variables}.
  290. At any given time, all @i{dynamic variables} with a given name refer to
  291. exactly one @i{binding}, either in the @i{dynamic environment}
  292. or in the @i{global environment}.
  293. The @i{value} part of the @i{binding} for a @i{dynamic variable} might
  294. be empty; in this case, the @i{dynamic variable} is said to have no @i{value},
  295. or to be @i{unbound}. A @i{dynamic variable} can be made @i{unbound}
  296. by using @b{makunbound}.
  297. The effect of @i{binding} a @i{dynamic variable} is to create
  298. a new @i{binding} to which all references to that @i{dynamic variable}
  299. in any @i{program} refer for the duration of the @i{evaluation} of the @i{form}
  300. that creates the @i{dynamic binding}.
  301. A @i{dynamic variable} can be referenced outside the @i{dynamic extent} of
  302. a @i{form} that @i{binds} it. Such a @i{variable} is sometimes called
  303. a ``global variable'' but is still in all respects just a @i{dynamic variable}
  304. whose @i{binding} happens to exist in the @i{global environment} rather than in some
  305. @i{dynamic environment}.
  306. A @i{dynamic variable} is @i{unbound}
  307. unless and until explicitly assigned a value, except for
  308. those variables whose initial value is
  309. defined in this specification or by an @i{implementation}.
  310. @node Constant Variables, Symbols Naming Both Lexical and Dynamic Variables, Dynamic Variables, The Evaluation Model
  311. @subsubsection Constant Variables
  312. Certain variables, called @i{constant variables}, are reserved as ``named constants.''
  313. The consequences are undefined if an attempt is made to
  314. assign a value to,
  315. or create
  316. a @i{binding} for a @i{constant variable},
  317. except that a `compatible' redefinition of a @i{constant variable}
  318. using @b{defconstant} is permitted; see the @i{macro} @b{defconstant}.
  319. @i{Keywords},
  320. @i{symbols} defined by @r{Common Lisp} or the @i{implementation}
  321. as constant (such as @b{nil}, @b{t}, and @b{pi}),
  322. and @i{symbols} declared as constant using @b{defconstant}
  323. are @i{constant variables}.
  324. @node Symbols Naming Both Lexical and Dynamic Variables, Conses as Forms, Constant Variables, The Evaluation Model
  325. @subsubsection Symbols Naming Both Lexical and Dynamic Variables
  326. The same @i{symbol} can name both
  327. a @i{lexical variable}
  328. and a @i{dynamic variable},
  329. but never in the same @i{lexical environment}.
  330. In the following example, the @i{symbol} @t{x} is used,
  331. at different times,
  332. as the @i{name} of a @i{lexical variable}
  333. and as the @i{name} of a @i{dynamic variable}.
  334. @example
  335. (let ((x 1)) ;Binds a special variable X
  336. (declare (special x))
  337. (let ((x 2)) ;Binds a lexical variable X
  338. (+ x ;Reads a lexical variable X
  339. (locally (declare (special x))
  340. x)))) ;Reads a special variable X
  341. @result{} 3
  342. @end example
  343. @node Conses as Forms, Special Forms, Symbols Naming Both Lexical and Dynamic Variables, The Evaluation Model
  344. @subsubsection Conses as Forms
  345. A @i{cons} that is used as a @i{form} is called a @i{compound form}.
  346. If the @i{car} of that @i{compound form} is a @i{symbol},
  347. that @i{symbol} is the @i{name} of an @i{operator},
  348. and the @i{form} is either a @i{special form}, a @i{macro form},
  349. or a @i{function form}, depending on the @i{function} @i{binding}
  350. of the @i{operator} in the current @i{lexical environment}.
  351. If the @i{operator} is neither a @i{special operator}
  352. nor a @i{macro name}, it is assumed to be a @i{function name}
  353. (even if there is no definition for such a @i{function}).
  354. If the @i{car} of the @i{compound form} is not a @i{symbol},
  355. then that @i{car} must be a @i{lambda expression},
  356. in which case the @i{compound form} is a @i{lambda form}.
  357. How a @i{compound form} is processed depends on whether it is
  358. classified as a @i{special form}, a @i{macro form},
  359. a @i{function form}, or a @i{lambda form}.
  360. @node Special Forms, Macro Forms, Conses as Forms, The Evaluation Model
  361. @subsubsection Special Forms
  362. A @i{special form} is a @i{form} with special syntax,
  363. special evaluation rules, or both, possibly manipulating the
  364. evaluation environment, control flow, or both.
  365. A @i{special operator} has access to
  366. the current @i{lexical environment}
  367. and the current @i{dynamic environment}.
  368. Each @i{special operator} defines the manner in which its @i{subexpressions}
  369. are treated---which are @i{forms}, which are special syntax, @i{etc.}
  370. Some @i{special operators} create new
  371. lexical or dynamic @i{environments} for use during the
  372. @i{evaluation} of @i{subforms}
  373. of the @i{special form}. For example, @b{block} creates a
  374. new @i{lexical environment} that is the same as the one in force
  375. at the point of evaluation of the @b{block} @i{form}
  376. with the addition of a @i{binding} of the @b{block} name
  377. to an @i{exit point} from the @b{block}.
  378. The set of @i{special operator} @i{names} is fixed in @r{Common Lisp};
  379. no way is provided for the user to define a @i{special operator}.
  380. Figure 3--2 lists all of the @r{Common Lisp} @i{symbols}
  381. that have definitions as @i{special operators}.
  382. @group
  383. @noindent
  384. @w{ block let* return-from }
  385. @w{ catch load-time-value setq }
  386. @w{ eval-when locally symbol-macrolet }
  387. @w{ flet macrolet tagbody }
  388. @w{ function multiple-value-call the }
  389. @w{ go multiple-value-prog1 throw }
  390. @w{ if progn unwind-protect }
  391. @w{ labels progv }
  392. @w{ let quote }
  393. @noindent
  394. @w{ Figure 3--2: Common Lisp Special Operators }
  395. @end group
  396. @node Macro Forms, Function Forms, Special Forms, The Evaluation Model
  397. @subsubsection Macro Forms
  398. If the @i{operator} names a @i{macro},
  399. its associated @i{macro function} is applied
  400. to the entire @i{form} and the result of that application is
  401. used in place of the original @i{form}.
  402. Specifically, a @i{symbol} names a @i{macro} in a given @i{lexical environment} if
  403. @b{macro-function} is @i{true} of the
  404. @i{symbol} and that @i{environment}.
  405. The @i{function} returned by @b{macro-function}
  406. is a @i{function} of two arguments, called the
  407. expansion function.
  408. The expansion function is invoked by calling the @i{macroexpand hook} with
  409. the expansion function as its first argument,
  410. the entire @i{macro form} as its second argument,
  411. and an @i{environment object} (corresponding to the current @i{lexical environment})
  412. as its third argument.
  413. The @i{macroexpand hook}, in turn, calls the expansion function with the
  414. @i{form} as its first argument and the @i{environment} as its second argument.
  415. The @i{value} of the expansion function, which is passed through
  416. by the @i{macroexpand hook}, is a @i{form}.
  417. The returned @i{form} is @i{evaluated} in place of the original @i{form}.
  418. The consequences are undefined if a @i{macro function} destructively modifies
  419. any part of its @i{form} argument.
  420. A @i{macro name} is not a @i{function designator},
  421. and cannot be used as the @i{function} argument to @i{functions}
  422. such as @b{apply}, @b{funcall}, or @b{map}.
  423. An @i{implementation} is free to implement a @r{Common Lisp} @i{special operator}
  424. as a @i{macro}. An @i{implementation} is free to implement any
  425. @i{macro} @i{operator} as a @i{special operator}, but only
  426. if an equivalent definition of the @i{macro} is also provided.
  427. Figure 3--3 lists some @i{defined names} that are applicable
  428. to @i{macros}.
  429. @group
  430. @noindent
  431. @w{ *macroexpand-hook* macro-function macroexpand-1 }
  432. @w{ defmacro macroexpand macrolet }
  433. @noindent
  434. @w{ Figure 3--3: Defined names applicable to macros }
  435. @end group
  436. @node Function Forms, Lambda Forms, Macro Forms, The Evaluation Model
  437. @subsubsection Function Forms
  438. If the @i{operator} is a @i{symbol} naming a @i{function},
  439. the @i{form} represents a @i{function form},
  440. and the @i{cdr} of the list contains the @i{forms}
  441. which when evaluated will supply the arguments passed to the @i{function}.
  442. When a @i{function name} is not defined,
  443. an error of @i{type} @b{undefined-function} should be signaled at run time;
  444. see @ref{Semantic Constraints}.
  445. A @i{function form} is evaluated as follows:
  446. The @i{subforms} in the @i{cdr} of the original @i{form}
  447. are evaluated in left-to-right order in the current lexical and
  448. dynamic @i{environments}. The @i{primary value} of each
  449. such @i{evaluation} becomes an @i{argument} to the named @i{function};
  450. any additional @i{values} returned by the @i{subforms} are discarded.
  451. The @i{functional value} of the @i{operator}
  452. is retrieved from the @i{lexical environment},
  453. and that @i{function} is invoked with the indicated arguments.
  454. Although the order of @i{evaluation} of
  455. the @i{argument} @i{subforms} themselves is
  456. strictly left-to-right, it is not specified whether
  457. the definition of the @i{operator} in a @i{function form} is looked up
  458. before the @i{evaluation} of the @i{argument} @i{subforms},
  459. after the @i{evaluation} of the @i{argument} @i{subforms},
  460. or between the @i{evaluation} of any two @i{argument} @i{subforms}
  461. if there is more than one such @i{argument} @i{subform}.
  462. For example, the following might return 23 or~24.
  463. @example
  464. (defun foo (x) (+ x 3))
  465. (defun bar () (setf (symbol-function 'foo) #'(lambda (x) (+ x 4))))
  466. (foo (progn (bar) 20))
  467. @end example
  468. A @i{binding} for a @i{function name} can be @i{established} in
  469. one of several ways. A @i{binding} for a @i{function name} in
  470. the @i{global environment} can be @i{established} by
  471. @b{defun},
  472. @b{setf} of @b{fdefinition},
  473. @b{setf} of @b{symbol-function},
  474. @b{ensure-generic-function},
  475. @b{defmethod} (implicitly, due to @b{ensure-generic-function}),
  476. or
  477. @b{defgeneric}.
  478. A @i{binding} for a @i{function name} in the @i{lexical environment}
  479. can be @i{established} by
  480. @b{flet}
  481. or @b{labels}.
  482. Figure 3--4 lists some @i{defined names} that are applicable to @i{functions}.
  483. @group
  484. @noindent
  485. @w{ apply fdefinition mapcan }
  486. @w{ call-arguments-limit flet mapcar }
  487. @w{ complement fmakunbound mapcon }
  488. @w{ constantly funcall mapl }
  489. @w{ defgeneric function maplist }
  490. @w{ defmethod functionp multiple-value-call }
  491. @w{ defun labels reduce }
  492. @w{ fboundp map symbol-function }
  493. @noindent
  494. @w{ Figure 3--4: Some function-related defined names }
  495. @end group
  496. @node Lambda Forms, Self-Evaluating Objects, Function Forms, The Evaluation Model
  497. @subsubsection Lambda Forms
  498. A @i{lambda form} is similar to a @i{function form}, except that
  499. the @i{function name} is replaced by a @i{lambda expression}.
  500. A @i{lambda form} is equivalent to using @i{funcall} of a
  501. @i{lexical closure} of the @i{lambda expression} on the given @i{arguments}.
  502. (In practice, some compilers are more likely to produce inline code
  503. for a @i{lambda form} than for an arbitrary named function
  504. that has been declared @b{inline}; however, such a difference
  505. is not semantic.)
  506. For further information, see @ref{Lambda Expressions}.
  507. @node Self-Evaluating Objects, Examples of Self-Evaluating Objects, Lambda Forms, The Evaluation Model
  508. @subsubsection Self-Evaluating Objects
  509. A @i{form} that is neither a @i{symbol} nor a @i{cons} is
  510. defined to be a @i{self-evaluating object}. @i{Evaluating}
  511. such an @i{object} @i{yields} the @i{same} @i{object}
  512. as a result.
  513. Certain specific @i{symbols} and @i{conses} might also happen
  514. to be ``self-evaluating'' but only as a special case of a more
  515. general set of rules for the @i{evaluation} of @i{symbols} and
  516. @i{conses}; such @i{objects} are not considered to be
  517. @i{self-evaluating objects}.
  518. The consequences are undefined if @i{literal objects} (including
  519. @i{self-evaluating objects}) are destructively modified.
  520. @node Examples of Self-Evaluating Objects, , Self-Evaluating Objects, The Evaluation Model
  521. @subsubsection Examples of Self-Evaluating Objects
  522. @i{Numbers}, @i{pathnames}, and @i{arrays} are examples of
  523. @i{self-evaluating objects}.
  524. @example
  525. 3 @result{} 3
  526. #c(2/3 5/8) @result{} #C(2/3 5/8)
  527. #p"S:[BILL]OTHELLO.TXT" @result{} #P"S:[BILL]OTHELLO.TXT"
  528. #(a b c) @result{} #(A B C)
  529. "fred smith" @result{} "fred smith"
  530. @end example
  531. @node Lambda Expressions, Closures and Lexical Binding, The Evaluation Model, Evaluation
  532. @subsection Lambda Expressions
  533. In a @i{lambda expression},
  534. the body is evaluated in a lexical @i{environment} that is formed by
  535. adding the @i{binding} of
  536. each @i{parameter} in the @i{lambda list}
  537. with the corresponding @i{value} from the @i{arguments}
  538. to the current lexical @i{environment}.
  539. For further discussion of how @i{bindings} are @i{established}
  540. based on the @i{lambda list}, see @ref{Lambda Lists}.
  541. The body of a @i{lambda expression} is an @i{implicit progn};
  542. the @i{values} it returns are returned by the @i{lambda expression}.
  543. @node Closures and Lexical Binding, Shadowing, Lambda Expressions, Evaluation
  544. @subsection Closures and Lexical Binding
  545. A @i{lexical closure} is a @i{function} that can refer to and alter
  546. the values of @i{lexical bindings} @i{established} by @i{binding} @i{forms}
  547. that textually include the function definition.
  548. Consider this code, where @t{x} is not declared @b{special}:
  549. @example
  550. (defun two-funs (x)
  551. (list (function (lambda () x))
  552. (function (lambda (y) (setq x y)))))
  553. (setq funs (two-funs 6))
  554. (funcall (car funs)) @result{} 6
  555. (funcall (cadr funs) 43) @result{} 43
  556. (funcall (car funs)) @result{} 43
  557. @end example
  558. The @b{function} @i{special form} coerces a
  559. @i{lambda expression} into a @i{closure} in which the
  560. @i{lexical environment} in effect when the @i{special form} is
  561. evaluated is captured along with the @i{lambda expression}.
  562. The function @t{two-funs} returns a @i{list} of two
  563. @i{functions}, each of which refers to the @i{binding} of the
  564. variable @t{x} created on entry to the function @t{two-funs} when it
  565. was called.
  566. This variable has the value @t{6}
  567. initially, but @b{setq} can alter this @i{binding}.
  568. The @i{lexical closure} created for the first
  569. @i{lambda expression} does not ``snapshot'' the @i{value} @t{6} for @t{x}
  570. when the @i{closure} is created; rather it captures the @i{binding} of @t{x}.
  571. The second @i{function} can be used to alter the @i{value} in the same (captured)
  572. @i{binding} (to @t{43}, in the example), and
  573. this altered variable binding then affects the value returned by the first @i{function}.
  574. In situations where a @i{closure} of a
  575. @i{lambda expression} over the same set of @i{bindings} may be
  576. produced more than once, the various resulting @i{closures} may
  577. or may not be @i{identical}, at the discretion of the @i{implementation}.
  578. That is, two @i{functions} that are behaviorally
  579. indistinguishable might or might not be @i{identical}.
  580. Two @i{functions} that are behaviorally distinguishable are @i{distinct}.
  581. For example:
  582. @example
  583. (let ((x 5) (funs '()))
  584. (dotimes (j 10)
  585. (push #'(lambda (z)
  586. (if (null z) (setq x 0) (+ x z)))
  587. funs))
  588. funs)
  589. @end example
  590. The result of the above @i{form} is a @i{list} of ten @i{closures}.
  591. Each requires only the @i{binding} of @t{x}.
  592. It is the same @i{binding} in each case,
  593. but the ten @i{closure} @i{objects} might or might not be @i{identical}.
  594. On the other hand, the result of the @i{form}
  595. @example
  596. (let ((funs '()))
  597. (dotimes (j 10)
  598. (let ((x 5))
  599. (push (function (lambda (z)
  600. (if (null z) (setq x 0) (+ x z))))
  601. funs)))
  602. funs)
  603. @end example
  604. is also a @i{list} of ten @i{closures}.
  605. However, in this case no two of the @i{closure} @i{objects} can
  606. be @i{identical} because each @i{closure} is closed over a distinct
  607. @i{binding} of @t{x}, and these @i{bindings} can be behaviorally
  608. distinguished because of the use of @b{setq}.
  609. The result of the @i{form}
  610. @example
  611. (let ((funs '()))
  612. (dotimes (j 10)
  613. (let ((x 5))
  614. (push (function (lambda (z) (+ x z)))
  615. funs)))
  616. funs)
  617. @end example
  618. is a @i{list} of ten @i{closure} @i{objects} that
  619. might or might not be @i{identical}.
  620. A different @i{binding} of @t{x} is involved for
  621. each @i{closure}, but the @i{bindings} cannot be distinguished
  622. because their values are the @i{same} and immutable (there being no occurrence
  623. of @b{setq} on @t{x}). A compiler could internally
  624. transform the @i{form} to
  625. @example
  626. (let ((funs '()))
  627. (dotimes (j 10)
  628. (push (function (lambda (z) (+ 5 z)))
  629. funs))
  630. funs)
  631. @end example
  632. where the @i{closures} may be @i{identical}.
  633. It is possible that a @i{closure} does not
  634. close over any variable bindings.
  635. In the code fragment
  636. @example
  637. (mapcar (function (lambda (x) (+ x 2))) y)
  638. @end example
  639. the function @t{(lambda (x) (+ x 2))} contains no references to any outside
  640. object. In this case, the same @i{closure} might be returned
  641. for all evaluations of the @b{function} @i{form}.
  642. @node Shadowing, Extent, Closures and Lexical Binding, Evaluation
  643. @subsection Shadowing
  644. If two @i{forms} that @i{establish} @i{lexical bindings} with
  645. the same @i{name} N are textually nested, then references to N
  646. within the inner @i{form} refer to the @i{binding} established by
  647. the inner @i{form}; the inner @i{binding} for N
  648. @i{shadows}
  649. @IGindex{shadow}
  650. the outer @i{binding} for N. Outside the inner
  651. @i{form} but inside the outer one, references to N refer to the
  652. @i{binding} established by the outer @i{form}. For example:
  653. @example
  654. (defun test (x z)
  655. (let ((z (* x 2)))
  656. (print z))
  657. z)
  658. @end example
  659. The @i{binding} of the variable @t{z} by
  660. @b{let} shadows
  661. the @i{parameter} binding for the function @t{test}. The reference to the
  662. variable @t{z} in the @b{print} @i{form} refers to the @b{let} binding.
  663. The reference to @t{z} at the end of the function @t{test}
  664. refers to the @i{parameter} named @t{z}.
  665. Constructs that are lexically scoped act as if new names were
  666. generated for each @i{object} on each execution. Therefore,
  667. dynamic shadowing cannot occur. For example:
  668. @example
  669. (defun contorted-example (f g x)
  670. (if (= x 0)
  671. (funcall f)
  672. (block here
  673. (+ 5 (contorted-example g
  674. #'(lambda () (return-from here 4))
  675. (- x 1))))))
  676. @end example
  677. Consider the call @t{(contorted-example nil nil 2)}. This produces
  678. @t{4}. During the course of execution, there are three
  679. calls to @t{contorted-example}, interleaved with two
  680. blocks:
  681. @example
  682. (contorted-example nil nil 2)
  683. (block here{{}_1} ...)
  684. (contorted-example nil #'(lambda () (return-from here{{}_1} 4)) 1)
  685. (block here{{}_2} ...)
  686. (contorted-example #'(lambda () (return-from here{{}_1} 4))
  687. #'(lambda () (return-from here{{}_2} 4))
  688. 0)
  689. (funcall f)
  690. where f @result{} #'(lambda () (return-from here{{}_1} 4))
  691. (return-from here{{}_1} 4)
  692. @end example
  693. At the time the @t{funcall} is executed
  694. there are two @b{block} @i{exit points} outstanding, each apparently
  695. named @t{here}.
  696. The @b{return-from} @i{form} executed as a result of the @t{funcall}
  697. operation
  698. refers to the outer outstanding @i{exit point}
  699. (here{{}_1}), not the
  700. inner one (here{{}_2}).
  701. It
  702. refers to that @i{exit point} textually visible at the point of
  703. execution of @b{function}
  704. (here abbreviated by the @t{#'} syntax) that resulted
  705. in creation of the @i{function} @i{object} actually invoked by
  706. @b{funcall}.
  707. If, in this example, one were to change the @t{(funcall f)} to
  708. @t{(funcall g)}, then the value of the call @t{(contorted-example nil nil 2)}
  709. would be @t{9}. The value would change because
  710. @b{funcall} would cause the
  711. execution of @t{(return-from here{{}_2} 4)}, thereby causing
  712. a return from the inner @i{exit point} (here{{}_2}).
  713. When that occurs, the value @t{4} is returned from the
  714. middle invocation of @t{contorted-example}, @t{5} is added to that
  715. to get @t{9}, and that value is returned from the outer block
  716. and the outermost call to @t{contorted-example}. The point
  717. is that the choice of @i{exit point}
  718. returned from has nothing to do with its
  719. being innermost or outermost; rather,
  720. it depends on the lexical environment
  721. that is packaged up with a @i{lambda expression} when
  722. @b{function} is executed.
  723. @node Extent, Return Values, Shadowing, Evaluation
  724. @subsection Extent
  725. @t{Contorted-example} works only because the
  726. @i{function} named by @t{f} is invoked during the @i{extent} of the
  727. @i{exit point}.
  728. Once the flow of execution has left the block,
  729. the @i{exit point} is @i{disestablished}. For example:
  730. @example
  731. (defun invalid-example ()
  732. (let ((y (block here #'(lambda (z) (return-from here z)))))
  733. (if (numberp y) y (funcall y 5))))
  734. @end example
  735. One might expect the call @t{(invalid-example)} to produce @t{5}
  736. by the following incorrect reasoning:
  737. @b{let} binds @t{y} to the
  738. value of @b{block}; this value is a @i{function} resulting
  739. from the @i{lambda expression}. Because @t{y} is not a number, it is
  740. invoked on the value @t{5}. The @b{return-from} should then
  741. return this value from the
  742. @i{exit point} named @t{here}, thereby
  743. exiting from the block again and giving @t{y} the value @t{5}
  744. which, being a number, is then returned as the value of the call
  745. to @t{invalid-example}.
  746. The argument fails only because @i{exit points} have
  747. @i{dynamic extent}. The argument is correct up to the execution of
  748. @b{return-from}. The execution of @b{return-from}
  749. should signal an error of @i{type} @b{control-error}, however, not
  750. because it cannot refer to the @i{exit point}, but because it
  751. does correctly refer to an @i{exit point} and that
  752. @i{exit point} has been @i{disestablished}.
  753. A reference by name to a dynamic @i{exit point} binding such as
  754. a @i{catch tag} refers to the most recently
  755. @i{established} @i{binding} of that name that has not been
  756. @i{disestablished}. For example:
  757. @example
  758. (defun fun1 (x)
  759. (catch 'trap (+ 3 (fun2 x))))
  760. (defun fun2 (y)
  761. (catch 'trap (* 5 (fun3 y))))
  762. (defun fun3 (z)
  763. (throw 'trap z))
  764. @end example
  765. Consider the call @t{(fun1 7)}. The result is @t{10}. At the time
  766. the @b{throw} is executed, there are two outstanding catchers with the
  767. name @t{trap}: one established within procedure @t{fun1}, and the other
  768. within procedure @t{fun2}. The latter is the more recent, and so the
  769. value @t{7} is returned from @b{catch} in @t{fun2}.
  770. Viewed from within @t{fun3}, the @b{catch}
  771. in @t{fun2} shadows the one in @t{fun1}.
  772. Had @t{fun2} been defined as
  773. @example
  774. (defun fun2 (y)
  775. (catch 'snare (* 5 (fun3 y))))
  776. @end example
  777. then the two @i{exit points}
  778. would have different @i{names}, and therefore the one
  779. in @t{fun1} would not be shadowed. The result would then have been @t{7}.
  780. @node Return Values, , Extent, Evaluation
  781. @subsection Return Values
  782. Ordinarily the result of calling a @i{function} is a single @i{object}.
  783. Sometimes, however, it is convenient for a function to compute several
  784. @i{objects} and return them.
  785. In order to receive other than exactly one value from a @i{form},
  786. one of several @i{special forms} or @i{macros} must be used to request those
  787. values. If a @i{form} produces @i{multiple values} which were not
  788. requested in this way, then the first value is given to the caller and
  789. all others are discarded; if the @i{form} produces zero values,
  790. then the caller receives @b{nil} as a value.
  791. Figure 3--5 lists
  792. some @i{operators} for receiving @i{multiple values}_2.
  793. These @i{operators} can be used to specify
  794. one or more @i{forms} to @i{evaluate}
  795. and where to put the @i{values} returned by those @i{forms}.
  796. @group
  797. @noindent
  798. @w{ multiple-value-bind multiple-value-prog1 return-from }
  799. @w{ multiple-value-call multiple-value-setq throw }
  800. @w{ multiple-value-list return }
  801. @noindent
  802. @w{ Figure 3--5: Some operators applicable to receiving multiple values}
  803. @end group
  804. The @i{function} @b{values} can produce @i{multiple values}_2.
  805. @t{(values)} returns zero values;
  806. @t{(values @i{form})} returns the @i{primary value} returned by @i{form};
  807. @t{(values @i{form1} @i{form2})} returns two values,
  808. the @i{primary value} of @i{form1}
  809. and the @i{primary value} of @i{form2};
  810. and so on.
  811. See @b{multiple-values-limit} and @b{values-list}.
  812. @c end of including concept-eval
  813. @node Compilation, Declarations, Evaluation, Evaluation and Compilation
  814. @section Compilation
  815. @c including concept-compile
  816. @menu
  817. * Compiler Terminology::
  818. * Compilation Semantics::
  819. * File Compilation::
  820. * Literal Objects in Compiled Files::
  821. * Exceptional Situations in the Compiler::
  822. @end menu
  823. @node Compiler Terminology, Compilation Semantics, Compilation, Compilation
  824. @subsection Compiler Terminology
  825. The following terminology is used in this section.
  826. The @i{compiler}
  827. @IGindex{compiler}
  828. is a utility that translates code into an
  829. @i{implementation-dependent} form that might be represented or
  830. executed efficiently.
  831. The term @i{compiler}
  832. @IGindex{compiler}
  833. refers to both of the @i{functions}
  834. @b{compile} and @b{compile-file}.
  835. The term @i{compiled code}
  836. @IGindex{compiled code}
  837. refers to
  838. @i{objects} representing compiled programs, such as @i{objects} constructed
  839. by @b{compile} or by @b{load} when @i{loading} a @i{compiled file}.
  840. The term @i{implicit compilation}
  841. @IGindex{implicit compilation}
  842. refers to @i{compilation}
  843. performed during @i{evaluation}.
  844. The term @i{literal object}
  845. @IGindex{literal object}
  846. refers to
  847. a quoted @i{object}
  848. or a @i{self-evaluating object}
  849. or an @i{object} that is a substructure of such an @i{object}.
  850. A @i{constant variable} is not itself a @i{literal object}.
  851. The term @i{coalesce}
  852. @IGindex{coalesce}
  853. is defined as follows.
  854. Suppose @t{A} and @t{B} are two @i{literal constants} in the @i{source code},
  855. and that @t{A'} and @t{B'} are the corresponding @i{objects} in the @i{compiled code}.
  856. If @t{A'} and @t{B'} are @b{eql} but
  857. @t{A} and @t{B} are not @b{eql}, then it is said
  858. that @t{A} and @t{B} have been coalesced by the compiler.
  859. The term @i{minimal compilation}
  860. @IGindex{minimal compilation}
  861. refers to actions the compiler
  862. must take at @i{compile time}. These actions are specified in
  863. @ref{Compilation Semantics}.
  864. The verb @i{process}
  865. @IGindex{process}
  866. refers to performing @i{minimal compilation},
  867. determining the time of evaluation for a @i{form},
  868. and possibly @i{evaluating} that @i{form} (if required).
  869. The term @i{further compilation}
  870. @IGindex{further compilation}
  871. refers to
  872. @i{implementation-dependent} compilation beyond @i{minimal compilation}.
  873. That is, @i{processing} does not imply complete compilation.
  874. Block compilation and generation of machine-specific instructions are
  875. examples of further compilation.
  876. Further compilation is permitted to take place at @i{run time}.
  877. Four different @i{environments} relevant to compilation are
  878. distinguished:
  879. the @i{startup environment},
  880. the @i{compilation environment},
  881. the @i{evaluation environment}, and
  882. the @i{run-time environment}.
  883. The @i{startup environment}
  884. @IGindex{startup environment}
  885. is
  886. the @i{environment} of the @i{Lisp image}
  887. from which the @i{compiler} was invoked.
  888. The @i{compilation environment}
  889. @IGindex{compilation environment}
  890. is maintained by the compiler
  891. and is used to hold definitions and declarations to be used internally
  892. by the compiler. Only those parts of a definition needed for correct
  893. compilation are saved. The @i{compilation environment} is used
  894. as the @i{environment} @i{argument} to macro expanders called by
  895. the compiler. It is unspecified whether a definition available in the
  896. @i{compilation environment} can be used in an @i{evaluation}
  897. initiated in the @i{startup environment} or @i{evaluation environment}.
  898. The @i{evaluation environment}
  899. @IGindex{evaluation environment}
  900. is a @i{run-time environment}
  901. in which macro expanders and code specified by @b{eval-when}
  902. to be evaluated are evaluated. All evaluations initiated by the
  903. @i{compiler} take place in the @i{evaluation environment}.
  904. The @i{run-time environment}
  905. @IGindex{run-time environment}
  906. is the
  907. @i{environment} in which the program being compiled will be executed.
  908. The @i{compilation environment} inherits from
  909. the @i{evaluation environment},
  910. and the @i{compilation environment} and @i{evaluation environment}
  911. might be @i{identical}.
  912. The @i{evaluation environment} inherits from
  913. the @i{startup environment},
  914. and the @i{startup environment} and @i{evaluation environment}
  915. might be @i{identical}.
  916. The term @i{compile time}
  917. @IGindex{compile time}
  918. refers to the duration of time that
  919. the compiler is processing @i{source code}.
  920. At @i{compile time},
  921. only the @i{compilation environment}
  922. and the @i{evaluation environment}
  923. are available.
  924. The term @i{compile-time definition}
  925. @IGindex{compile-time definition}
  926. refers to a definition in
  927. the @i{compilation environment}.
  928. For example, when compiling a file,
  929. the definition of a function might be retained in the @i{compilation environment}
  930. if it is declared @b{inline}.
  931. This definition might not be available in the @i{evaluation environment}.
  932. The term @i{run time}
  933. @IGindex{run time}
  934. refers to the duration of time that the
  935. loader is loading compiled code or compiled code is being executed.
  936. At run time, only the @i{run-time environment} is available.
  937. The term @i{run-time definition}
  938. @IGindex{run-time definition}
  939. refers to a definition in the
  940. @i{run-time environment}.
  941. The term @i{run-time compiler}
  942. @IGindex{run-time compiler}
  943. refers to the @i{function} @b{compile}
  944. or @i{implicit compilation}, for which the compilation and run-time
  945. @i{environments} are maintained in the same @i{Lisp image}.
  946. Note that when the @i{run-time compiler} is used,
  947. the @i{run-time environment}
  948. and @i{startup environment}
  949. are the same.
  950. @node Compilation Semantics, File Compilation, Compiler Terminology, Compilation
  951. @subsection Compilation Semantics
  952. Conceptually, compilation is a process that traverses code, performs
  953. certain kinds of syntactic and semantic analyses using information
  954. (such as proclamations and @i{macro} definitions) present in the
  955. @i{compilation environment}, and produces equivalent, possibly
  956. more efficient code.
  957. @menu
  958. * Compiler Macros::
  959. * Purpose of Compiler Macros::
  960. * Naming of Compiler Macros::
  961. * When Compiler Macros Are Used::
  962. * Notes about the Implementation of Compiler Macros::
  963. * Minimal Compilation::
  964. * Semantic Constraints::
  965. @end menu
  966. @node Compiler Macros, Purpose of Compiler Macros, Compilation Semantics, Compilation Semantics
  967. @subsubsection Compiler Macros
  968. A @i{compiler macro} can be defined for a @i{name}
  969. that also names a @i{function} or @i{macro}.
  970. That is, it is possible for a
  971. @i{function name} to name both a @i{function} and a @i{compiler macro}.
  972. A @i{function name} names a @i{compiler macro} if @b{compiler-macro-function}
  973. is @i{true} of the @i{function name} in the @i{lexical environment} in which
  974. it appears. Creating a @i{lexical binding} for the @i{function name}
  975. not only creates a new local @i{function} or
  976. @i{macro} definition, but also @i{shadows}_2 the @i{compiler macro}.
  977. The @i{function} returned by @b{compiler-macro-function}
  978. is a @i{function} of two arguments, called the
  979. expansion function. To expand a @i{compiler macro},
  980. the expansion function is invoked by calling the @i{macroexpand hook} with
  981. the expansion function as its first argument,
  982. the entire compiler macro @i{form} as its second argument,
  983. and the current compilation @i{environment}
  984. (or with the current lexical @i{environment},
  985. if the @i{form} is being processed by something
  986. other than @b{compile-file})
  987. as its third argument.
  988. The @i{macroexpand hook}, in turn, calls the expansion function with the
  989. @i{form} as its first argument and the @i{environment} as its second argument.
  990. The return value from the expansion function, which is passed through
  991. by the @i{macroexpand hook}, might either be the @i{same} @i{form},
  992. or else a form that can, at the discretion of the @i{code} doing the expansion,
  993. be used in place of the original @i{form}.
  994. @group
  995. @noindent
  996. @w{ *macroexpand-hook* compiler-macro-function define-compiler-macro }
  997. @noindent
  998. @w{ Figure 3--6: Defined names applicable to compiler macros }
  999. @end group
  1000. @node Purpose of Compiler Macros, Naming of Compiler Macros, Compiler Macros, Compilation Semantics
  1001. @subsubsection Purpose of Compiler Macros
  1002. The purpose of the @i{compiler macro} facility is to permit
  1003. selective source code transformations as optimization advice
  1004. to the @i{compiler}. When a @i{compound form} is being
  1005. processed (as by the compiler), if the @i{operator} names a
  1006. @i{compiler macro} then the @i{compiler macro function} may be
  1007. invoked on the form, and the resulting expansion recursively processed
  1008. in preference to performing the usual processing on the original @i{form}
  1009. according to its normal interpretation as a @i{function form} or
  1010. @i{macro form}.
  1011. A @i{compiler macro function}, like a @i{macro function},
  1012. is a @i{function} of two @i{arguments}: the entire call @i{form}
  1013. and the @i{environment}. Unlike an ordinary @i{macro function}, a
  1014. @i{compiler macro function} can decline to provide an expansion merely by
  1015. returning a value that is the @i{same} as the original @i{form}.
  1016. The consequences are undefined if a @i{compiler macro function}
  1017. destructively modifies any part of its @i{form} argument.
  1018. The @i{form} passed to the compiler macro function can either be a @i{list}
  1019. whose @i{car} is the function name, or a @i{list} whose @i{car} is
  1020. @b{funcall} and whose @i{cadr} is a list @t{(function @i{name})};
  1021. note that this affects destructuring of the form argument by the
  1022. @i{compiler macro function}.
  1023. @b{define-compiler-macro} arranges for destructuring of arguments to be
  1024. performed correctly for both possible formats.
  1025. When @b{compile-file} chooses to expand a @i{top level form} that is
  1026. a @i{compiler macro} @i{form}, the expansion is also treated as a @i{top level form}
  1027. for the purposes of @b{eval-when} processing; see @ref{Processing of Top Level Forms}.
  1028. @node Naming of Compiler Macros, When Compiler Macros Are Used, Purpose of Compiler Macros, Compilation Semantics
  1029. @subsubsection Naming of Compiler Macros
  1030. @i{Compiler macros} may be defined for @i{function names} that name
  1031. @i{macros} as well as @i{functions}.
  1032. @i{Compiler macro} definitions are strictly global. There is no provision
  1033. for defining local @i{compiler macros} in the way that @b{macrolet}
  1034. defines local @i{macros}. Lexical bindings of a function name shadow any
  1035. compiler macro definition associated with the name as well as its
  1036. global @i{function} or @i{macro} definition.
  1037. Note that the presence of a compiler macro definition does not affect
  1038. the values returned by
  1039. functions that access @i{function} definitions (@i{e.g.}, @b{fboundp})
  1040. or @i{macro} definitions (@i{e.g.}, @b{macroexpand}).
  1041. Compiler macros are global, and the function
  1042. @b{compiler-macro-function} is sufficient to resolve their interaction
  1043. with other lexical and global definitions.
  1044. @node When Compiler Macros Are Used, Notes about the Implementation of Compiler Macros, Naming of Compiler Macros, Compilation Semantics
  1045. @subsubsection When Compiler Macros Are Used
  1046. The presence of a @i{compiler macro} definition for a @i{function} or @i{macro}
  1047. indicates that it is desirable for the @i{compiler} to use the expansion
  1048. of the @i{compiler macro} instead of the original @i{function form} or
  1049. @i{macro form}. However, no language processor
  1050. (compiler, evaluator, or other code walker) is ever required to actually
  1051. invoke @i{compiler macro functions}, or to
  1052. make use of the resulting expansion if it does invoke
  1053. a @i{compiler macro function}.
  1054. When the @i{compiler} encounters a @i{form} during processing that represents
  1055. a call to a @i{compiler macro} @i{name} (that is not declared @b{notinline}),
  1056. the @i{compiler} might expand the @i{compiler macro},
  1057. and might use the expansion in place of the original @i{form}.
  1058. When @b{eval} encounters a @i{form} during processing that represents
  1059. a call to a @i{compiler macro} @i{name} (that is not declared @b{notinline}),
  1060. @b{eval} might expand the @i{compiler macro},
  1061. and might use the expansion in place of the original @i{form}.
  1062. There are two situations in which a @i{compiler macro} definition must not be
  1063. applied by any language processor:
  1064. @table @asis
  1065. @item @t{*}
  1066. The global function name binding associated with the compiler
  1067. macro is shadowed by a lexical binding of the function name.
  1068. @item @t{*}
  1069. The function name has been declared or proclaimed @b{notinline} and
  1070. the call form appears within the scope of the declaration.
  1071. @end table
  1072. It is unspecified whether @i{compiler macros} are expanded or used in any other
  1073. situations.
  1074. @node Notes about the Implementation of Compiler Macros, Minimal Compilation, When Compiler Macros Are Used, Compilation Semantics
  1075. @subsubsection Notes about the Implementation of Compiler Macros
  1076. Although it is technically permissible, as described above,
  1077. for @b{eval} to treat @i{compiler macros} in the same situations
  1078. as @i{compiler} might, this is not necessarily a good idea in
  1079. @i{interpreted implementations}.
  1080. @i{Compiler macros} exist for the purpose of trading compile-time speed
  1081. for run-time speed. Programmers who write @i{compiler macros} tend to
  1082. assume that the @i{compiler macros} can take more time than normal @i{functions}
  1083. and @i{macros} in order to produce code which is especially optimal for use
  1084. at run time. Since @b{eval} in an @i{interpreted implementation}
  1085. might perform semantic analysis of the same form multiple times, it might be
  1086. inefficient in general for the @i{implementation} to choose to call
  1087. @i{compiler macros} on every such @i{evaluation}.
  1088. Nevertheless, the decision about what to do in these situations is left to
  1089. each @i{implementation}.
  1090. @node Minimal Compilation, Semantic Constraints, Notes about the Implementation of Compiler Macros, Compilation Semantics
  1091. @subsubsection Minimal Compilation
  1092. @i{Minimal compilation} is defined as follows:
  1093. @table @asis
  1094. @item @t{*}
  1095. All @i{compiler macro}
  1096. @IGindex{compiler macro}
  1097. calls appearing in the
  1098. @i{source code} being compiled are expanded, if at all, at compile time;
  1099. they will not be expanded at run time.
  1100. @item @t{*}
  1101. All @i{macro}
  1102. @IGindex{macro}
  1103. and
  1104. @i{symbol macro}
  1105. @IGindex{symbol macro}
  1106. calls
  1107. appearing in the source code being compiled are expanded at compile time
  1108. in such a way that they will not be expanded again at run time.
  1109. @b{macrolet}
  1110. @IRindex{macrolet}
  1111. and
  1112. @b{symbol-macrolet}
  1113. @IRindex{symbol-macrolet}
  1114. are effectively replaced by
  1115. @i{forms} corresponding to their bodies in which calls to
  1116. @i{macros} are replaced by their expansions.
  1117. @item @t{*}
  1118. The first @i{argument} in a @b{load-time-value}
  1119. @IRindex{load-time-value}
  1120. @i{form}
  1121. in @i{source code} processed by @b{compile}
  1122. @IRindex{compile}
  1123. is @i{evaluated} at @i{compile time};
  1124. in @i{source code} processed by @b{compile-file}
  1125. @IRindex{compile-file}
  1126. ,
  1127. the compiler arranges for it to be @i{evaluated} at @i{load time}.
  1128. In either case, the result of the @i{evaluation}
  1129. is remembered and used later as the value of the
  1130. @b{load-time-value} @i{form} at @i{execution time}.
  1131. @end table
  1132. @node Semantic Constraints, , Minimal Compilation, Compilation Semantics
  1133. @subsubsection Semantic Constraints
  1134. All @i{conforming programs} must obey the following constraints,
  1135. which are designed to minimize the observable differences
  1136. between compiled and interpreted programs:
  1137. @table @asis
  1138. @item @t{*}
  1139. Definitions of any referenced @i{macros}
  1140. must be present in the @i{compilation environment}.
  1141. Any @i{form} that is a @i{list}
  1142. beginning with a @i{symbol} that does not name a
  1143. @i{special operator} or a @i{macro} defined in the
  1144. @i{compilation environment} is treated by the compiler as a
  1145. function call.
  1146. @item @t{*}
  1147. @b{Special} proclamations for @i{dynamic variables}
  1148. must be made in the @i{compilation environment}. Any @i{binding}
  1149. for which there is no @b{special} declaration or proclamation in
  1150. the @i{compilation environment} is treated by the compiler as
  1151. a @i{lexical binding}.
  1152. @item @t{*}
  1153. The definition of a function that is defined and
  1154. declared @b{inline} in the @i{compilation environment} must be
  1155. the same at run time.
  1156. @item @t{*}
  1157. Within a @i{function} named F, the compiler may
  1158. (but is not required to)
  1159. assume that an apparent recursive call to a @i{function} named F
  1160. refers to the same definition of F,
  1161. unless that function has been declared @b{notinline}.
  1162. The consequences of redefining such a recursively defined @i{function} F
  1163. while it is executing are undefined.
  1164. @item @t{*}
  1165. A call within a file to a named function that is
  1166. defined in the same file refers to that function, unless that function
  1167. has been declared @b{notinline}. The consequences are unspecified
  1168. if functions are redefined individually at run time or multiply
  1169. defined in the same file.
  1170. @item @t{*}
  1171. The argument syntax and number of return values for
  1172. all functions whose @b{ftype} is declared at compile time must
  1173. remain the same at run time.
  1174. @item @t{*}
  1175. @i{Constant variables} defined in
  1176. the @i{compilation environment} must have a @i{similar} value at
  1177. run time. A reference to
  1178. a @i{constant variable}
  1179. in @i{source code} is equivalent to a reference to
  1180. a @i{literal} @i{object} that is the @i{value} of the @i{constant variable}.
  1181. @item @t{*}
  1182. Type definitions made with @b{deftype} or
  1183. @b{defstruct} in the @i{compilation environment} must
  1184. retain the same definition at run time. Classes defined by @b{defclass}
  1185. in the @i{compilation environment} must be defined
  1186. at run time to have the same @i{superclasses} and same
  1187. @i{metaclass}.
  1188. This implies that @i{subtype}/@i{supertype} relationships of
  1189. @i{type specifiers} must not change between @i{compile time} and @i{run time}.
  1190. @item @t{*}
  1191. Type declarations present in the compilation
  1192. @i{environment} must accurately describe the corresponding values at run time;
  1193. otherwise, the consequences are undefined. It is permissible
  1194. for an unknown @i{type} to appear in a declaration at
  1195. compile time, though a warning might be signaled in such a case.
  1196. @item @t{*}
  1197. Except in the situations explicitly listed above, a
  1198. @i{function} defined in the @i{evaluation environment}
  1199. is permitted to have a different definition or a different @i{signature}
  1200. at run time, and the run-time definition prevails.
  1201. @end table
  1202. @i{Conforming programs} should not be written using any additional
  1203. assumptions about consistency between the run-time
  1204. @i{environment} and the startup, evaluation, and compilation
  1205. @i{environments}.
  1206. Except where noted, when a compile-time and a run-time definition are
  1207. different, one of the following occurs at run time:
  1208. @table @asis
  1209. @item @t{*}
  1210. an error of @i{type} @b{error} is signaled
  1211. @item @t{*}
  1212. the compile-time definition prevails
  1213. @item @t{*}
  1214. the run-time definition prevails
  1215. @end table
  1216. If the @i{compiler} processes a @i{function form} whose @i{operator}
  1217. is not defined at compile time, no error is signaled at compile time.
  1218. @node File Compilation, Literal Objects in Compiled Files, Compilation Semantics, Compilation
  1219. @subsection File Compilation
  1220. The @i{function} @b{compile-file} performs compilation of
  1221. @i{forms} in a file following the rules specified in @ref{Compilation Semantics},
  1222. and produces an output file that can be loaded by using @b{load}.
  1223. Normally, the @i{top level forms} appearing in a file compiled with
  1224. @b{compile-file} are evaluated only when the resulting
  1225. compiled file is loaded, and not when the file is compiled. However,
  1226. it is typically the case that some forms in the file need to be evaluated
  1227. at compile time so the
  1228. remainder of the file can be read and compiled correctly.
  1229. The @b{eval-when} @i{special form} can be used to control
  1230. whether a @i{top level form} is evaluated at compile time, load
  1231. time, or both. It is possible to specify any of three situations with
  1232. @b{eval-when}, denoted by the symbols @t{:compile-toplevel},
  1233. @t{:load-toplevel}, and @t{:execute}. For top level
  1234. @b{eval-when} forms, @t{:compile-toplevel} specifies that the
  1235. compiler must evaluate the body at compile time, and @t{:load-toplevel} specifies that the compiler must arrange to evaluate
  1236. the body at load time. For non-top level @b{eval-when} forms,
  1237. @t{:execute} specifies that the body must be executed in the run-time
  1238. @i{environment}.
  1239. The behavior of this @i{form} can be more precisely understood in
  1240. terms of a model of how @b{compile-file} processes forms in
  1241. a file to be compiled. There are two processing modes, called
  1242. ``not-compile-time'' and ``compile-time-too''.
  1243. Successive forms are read from the file by @b{compile-file}
  1244. and processed in not-compile-time mode; in this mode,
  1245. @b{compile-file} arranges for forms to be evaluated only at load time
  1246. and not at compile time. When @b{compile-file} is in
  1247. compile-time-too mode, forms are evaluated both at compile time and
  1248. load time.
  1249. @menu
  1250. * Processing of Top Level Forms::
  1251. * Processing of Defining Macros::
  1252. * Constraints on Macros and Compiler Macros::
  1253. @end menu
  1254. @node Processing of Top Level Forms, Processing of Defining Macros, File Compilation, File Compilation
  1255. @subsubsection Processing of Top Level Forms
  1256. Processing of @i{top level forms} in the file compiler is defined
  1257. as follows:
  1258. @table @asis
  1259. @item 1.
  1260. If the @i{form} is a @i{compiler macro form}
  1261. (not disabled by a @b{notinline} @i{declaration}),
  1262. the @i{implementation} might or might not choose to compute
  1263. the @i{compiler macro expansion} of the @i{form} and,
  1264. having performed the expansion, might or might not choose to process the result
  1265. as a @i{top level form} in the same processing mode
  1266. (compile-time-too or not-compile-time).
  1267. If it declines to obtain or use the expansion, it must process the original @i{form}.
  1268. @item 2.
  1269. If the form is a @i{macro form},
  1270. its @i{macro expansion} is computed and processed as a
  1271. @i{top level form} in
  1272. the same processing mode (compile-time-too or not-compile-time).
  1273. @item 3.
  1274. If the form is a @b{progn} form, each of its
  1275. body @i{forms} is sequentially processed as a
  1276. @i{top level form} in the same processing mode.
  1277. @item 4.
  1278. If the form is a @b{locally},
  1279. @b{macrolet}, or @b{symbol-macrolet},
  1280. @b{compile-file} establishes the appropriate bindings and processes the
  1281. body forms as @i{top level forms} with those bindings in effect
  1282. in the same processing mode. (Note that this implies that the lexical
  1283. @i{environment} in which @i{top level forms} are processed
  1284. is not necessarily the @i{null lexical environment}.)
  1285. @item 5.
  1286. If the form is an @b{eval-when}
  1287. @IRindex{eval-when}
  1288. form, it is
  1289. handled according to Figure 3--7.
  1290. plus .5 fil
  1291. \offinterlineskip
  1292. @group
  1293. @noindent
  1294. @w{ @b{CT} @b{LT} @b{E} @b{Mode} @b{Action} @b{New Mode} }
  1295. @w{ _________________________________________________}
  1296. @w{ Yes Yes --- --- Process compile-time-too }
  1297. @w{ No Yes Yes CTT Process compile-time-too }
  1298. @w{ No Yes Yes NCT Process not-compile-time }
  1299. @w{ No Yes No --- Process not-compile-time }
  1300. @w{ Yes No --- --- Evaluate --- }
  1301. @w{ No No Yes CTT Evaluate --- }
  1302. @w{ No No Yes NCT Discard --- }
  1303. @w{ No No No --- Discard --- }
  1304. @end group
  1305. @w{ Figure 3--7: EVAL-WHEN processing}
  1306. Column @b{CT} indicates whether @t{:compile-toplevel} is specified.
  1307. Column @b{LT} indicates whether @t{:load-toplevel} is specified.
  1308. Column @b{E} indicates whether @t{:execute} is specified.
  1309. Column @b{Mode} indicates the processing mode;
  1310. a dash (---) indicates that the processing mode is not relevant.
  1311. The @b{Action} column specifies one of three actions:
  1312. @table @asis
  1313. @item @t{}
  1314. @b{Process:} process the body as @i{top level forms} in the
  1315. specified mode.
  1316. @item @t{}
  1317. @b{Evaluate:} evaluate the body in the dynamic execution
  1318. context of the compiler, using the @i{evaluation environment} as
  1319. the global environment and the @i{lexical environment} in which
  1320. the @b{eval-when} appears.
  1321. @item @t{}
  1322. @b{Discard:} ignore the @i{form}.
  1323. @end table
  1324. The @b{New Mode} column indicates the new processing mode.
  1325. A dash (---) indicates the compiler remains in its current mode.
  1326. @item 6.
  1327. Otherwise, the form is a @i{top level form} that
  1328. is not one of the special cases. In compile-time-too mode, the
  1329. compiler first evaluates the form in the evaluation
  1330. @i{environment} and then minimally compiles it. In not-compile-time
  1331. mode, the @i{form} is simply minimally compiled. All @i{subforms}
  1332. are treated as @i{non-top-level forms}.
  1333. Note that @i{top level forms} are processed in the order in
  1334. which they textually appear in the file and that each
  1335. @i{top level form} read by the compiler is processed before the next is
  1336. read. However, the order of processing (including macro expansion) of
  1337. @i{subforms} that are not @i{top level forms} and the order of
  1338. further compilation is unspecified as long as Common Lisp semantics
  1339. are preserved.
  1340. @end table
  1341. @b{eval-when} forms cause compile-time evaluation only at
  1342. top level. Both @t{:compile-toplevel} and @t{:load-toplevel} situation specifications
  1343. are ignored for @i{non-top-level forms}. For @i{non-top-level forms},
  1344. an @b{eval-when}
  1345. specifying the @t{:execute} situation is treated as an @i{implicit progn}
  1346. including the @i{forms} in the body of the @b{eval-when} @i{form};
  1347. otherwise, the @i{forms} in the body are ignored.
  1348. @node Processing of Defining Macros, Constraints on Macros and Compiler Macros, Processing of Top Level Forms, File Compilation
  1349. @subsubsection Processing of Defining Macros
  1350. Defining @i{macros} (such as @b{defmacro} or @b{defvar})
  1351. appearing within a file being processed by @b{compile-file}
  1352. normally have compile-time side effects which affect how subsequent @i{forms}
  1353. in the same @i{file} are compiled. A convenient model for explaining how these
  1354. side effects happen is that the defining macro expands into one or
  1355. more @b{eval-when} @i{forms}, and that the calls which cause the compile-time
  1356. side effects to happen appear
  1357. in the body of an @t{(eval-when (:compile-toplevel) ...)} @i{form}.
  1358. The compile-time side effects may cause information about the definition to
  1359. be stored differently than if the defining macro had been processed in the
  1360. `normal' way (either interpretively or by loading the compiled file).
  1361. In particular, the information stored by the defining @i{macros} at compile time
  1362. might or might not be available to the interpreter (either during or after compilation),
  1363. or during subsequent calls to the @i{compiler}. For example,
  1364. the following code is nonportable because it assumes that the @i{compiler}
  1365. stores the macro definition of @t{foo} where it is available to the interpreter:
  1366. @example
  1367. (defmacro foo (x) `(car ,x))
  1368. (eval-when (:execute :compile-toplevel :load-toplevel)
  1369. (print (foo '(a b c))))
  1370. @end example
  1371. A portable way to do the same thing would be to include the macro
  1372. definition inside the @b{eval-when} @i{form}, as in:
  1373. @example
  1374. (eval-when (:execute :compile-toplevel :load-toplevel)
  1375. (defmacro foo (x) `(car ,x))
  1376. (print (foo '(a b c))))
  1377. @end example
  1378. Figure 3--8 lists macros that make definitions
  1379. available both in the compilation and run-time @i{environments}.
  1380. It is not specified whether definitions made available in the
  1381. @i{compilation environment} are available in the evaluation
  1382. @i{environment}, nor is it specified whether they are available
  1383. in subsequent compilation units or subsequent invocations of the
  1384. compiler. As with @b{eval-when}, these compile-time side
  1385. effects happen only when the defining macros appear at
  1386. top level.
  1387. @group
  1388. @noindent
  1389. @w{ declaim define-modify-macro defsetf }
  1390. @w{ defclass define-setf-expander defstruct }
  1391. @w{ defconstant defmacro deftype }
  1392. @w{ define-compiler-macro defpackage defvar }
  1393. @w{ define-condition defparameter }
  1394. @noindent
  1395. @w{ Figure 3--8: Defining Macros That Affect the Compile-Time Environment}
  1396. @end group
  1397. @node Constraints on Macros and Compiler Macros, , Processing of Defining Macros, File Compilation
  1398. @subsubsection Constraints on Macros and Compiler Macros
  1399. Except where explicitly stated otherwise, no @i{macro} defined in
  1400. the @r{Common Lisp} standard produces an expansion that could cause any of the
  1401. @i{subforms} of the @i{macro form} to be treated as
  1402. @i{top level forms}. If an @i{implementation} also provides a
  1403. @i{special operator} definition of a @r{Common Lisp} @i{macro},
  1404. the @i{special operator} definition must be semantically equivalent
  1405. in this respect.
  1406. @i{Compiler macro} expansions must also have the same
  1407. top level evaluation semantics as the @i{form} which they replace.
  1408. This is of concern both to @i{conforming implementations} and to
  1409. @i{conforming programs}.
  1410. @node Literal Objects in Compiled Files, Exceptional Situations in the Compiler, File Compilation, Compilation
  1411. @subsection Literal Objects in Compiled Files
  1412. The functions @b{eval} and @b{compile} are
  1413. required to ensure that @i{literal objects} referenced within the resulting
  1414. interpreted or compiled code objects are the @i{same} as the
  1415. corresponding @i{objects} in the @i{source code}.
  1416. @b{compile-file}, on the other hand,
  1417. must produce a @i{compiled file} that, when loaded with
  1418. @b{load}, constructs the @i{objects} defined by the
  1419. @i{source code} and produces references to them.
  1420. In the case of @b{compile-file}, @i{objects}
  1421. constructed by @b{load} of the @i{compiled file} cannot be spoken
  1422. of as being the @i{same} as the @i{objects} constructed at
  1423. compile time, because the @i{compiled file} may be loaded into a different
  1424. @i{Lisp image} than the one in which it was compiled. This section
  1425. defines the concept of @i{similarity} which relates
  1426. @i{objects} in the @i{evaluation environment} to the
  1427. corresponding @i{objects} in the @i{run-time environment}.
  1428. The constraints on @i{literal objects} described in this section
  1429. apply only to @b{compile-file};
  1430. @b{eval} and @b{compile} do not copy or coalesce constants.
  1431. @menu
  1432. * Externalizable Objects::
  1433. * Similarity of Literal Objects::
  1434. * Similarity of Aggregate Objects::
  1435. * Definition of Similarity::
  1436. * Extensions to Similarity Rules::
  1437. * Additional Constraints on Externalizable Objects::
  1438. @end menu
  1439. @node Externalizable Objects, Similarity of Literal Objects, Literal Objects in Compiled Files, Literal Objects in Compiled Files
  1440. @subsubsection Externalizable Objects
  1441. The fact that the @i{file compiler} represents @i{literal} @i{objects}
  1442. externally in a @i{compiled file} and must later reconstruct suitable
  1443. equivalents of those @i{objects} when that @i{file} is loaded
  1444. imposes a need for constraints on the nature of the @i{objects} that can be
  1445. used as @i{literal} @i{objects} in @i{code} to be processed
  1446. by the @i{file compiler}.
  1447. An @i{object} that can be used as a @i{literal} @i{object}
  1448. in @i{code} to be processed by the @i{file compiler} is called an
  1449. @i{externalizable object}
  1450. @IGindex{externalizable object}
  1451. .
  1452. We define that two @i{objects} are @i{similar}
  1453. @IGindex{similar}
  1454. if they satisfy
  1455. a two-place conceptual equivalence predicate (defined below), which is
  1456. independent of the @i{Lisp image} so that the two @i{objects} in
  1457. different @i{Lisp images} can be understood to be equivalent under
  1458. this predicate. Further, by inspecting the definition of this conceptual
  1459. predicate, the programmer can anticipate what aspects of an @i{object}
  1460. are reliably preserved by @i{file compilation}.
  1461. The @i{file compiler} must cooperate with the @i{loader} in order to
  1462. assure that in each case where an @i{externalizable object} is processed
  1463. as a @i{literal object}, the @i{loader} will construct a @i{similar}
  1464. @i{object}.
  1465. The set of @i{objects} that are @i{externalizable objects}
  1466. @IGindex{externalizable object}
  1467. are those
  1468. for which the new conceptual term ``@i{similar}'' is defined, such that
  1469. when a @i{compiled file} is @i{loaded}, an @i{object} can be constructed
  1470. which can be shown to be @i{similar} to the original @i{object} which
  1471. existed at the time the @i{file compiler} was operating.
  1472. @node Similarity of Literal Objects, Similarity of Aggregate Objects, Externalizable Objects, Literal Objects in Compiled Files
  1473. @subsubsection Similarity of Literal Objects
  1474. @node Similarity of Aggregate Objects, Definition of Similarity, Similarity of Literal Objects, Literal Objects in Compiled Files
  1475. @subsubsection Similarity of Aggregate Objects
  1476. Of the @i{types} over which @i{similarity} is defined,
  1477. some are treated as aggregate objects. For these types,
  1478. @i{similarity} is defined recursively.
  1479. We say that an @i{object} of these types has certain ``basic qualities''
  1480. and to satisfy the @i{similarity} relationship, the values of the
  1481. corresponding qualities of the two @i{objects} must also be similar.
  1482. @node Definition of Similarity, Extensions to Similarity Rules, Similarity of Aggregate Objects, Literal Objects in Compiled Files
  1483. @subsubsection Definition of Similarity
  1484. Two @i{objects} S (in @i{source code}) and C (in @i{compiled code})
  1485. are defined to be @i{similar} if and only if
  1486. they are both of one of the @i{types} listed here
  1487. (or defined by the @i{implementation})
  1488. and they both satisfy all additional requirements of @i{similarity}
  1489. indicated for that @i{type}.
  1490. @table @asis
  1491. @item @b{number}
  1492. Two @i{numbers} S and C are @i{similar} if they are of the same @i{type}
  1493. and represent the same mathematical value.
  1494. @item @b{character}
  1495. Two @i{simple} @i{characters} S and C are @i{similar}
  1496. if they have @i{similar} @i{code} @i{attributes}.
  1497. @i{Implementations} providing additional, @i{implementation-defined}
  1498. @i{attributes} must define whether and how @i{non-simple} @i{characters}
  1499. can be regarded as @i{similar}.
  1500. @item @b{symbol}
  1501. Two @i{apparently uninterned} @i{symbols} S and C are @i{similar}
  1502. if their
  1503. @i{names}
  1504. are @i{similar}.
  1505. Two @i{interned} symbols S and C are @i{similar}
  1506. if their @i{names} are @i{similar},
  1507. and if either S is accessible in the @i{current package} at compile time
  1508. and C is accessible in the @i{current package} at load time,
  1509. or C is accessible in the @i{package} that is @i{similar} to
  1510. the @i{home package} of S.
  1511. (Note that @i{similarity} of
  1512. @i{symbols} is dependent
  1513. on neither the @i{current readtable} nor how the @i{function} @b{read} would
  1514. parse the @i{characters} in the @i{name} of the @i{symbol}.)
  1515. @item @b{package}
  1516. Two @i{packages} S and C are @i{similar} if their @i{names} are @i{similar}.
  1517. Note that although a @i{package} @i{object} is an @i{externalizable object},
  1518. the programmer is responsible for ensuring that the corresponding @i{package} is
  1519. already in existence when code referencing it as a @i{literal} @i{object}
  1520. is @i{loaded}. The @i{loader} finds the corresponding @i{package} @i{object}
  1521. as if by calling @b{find-package} with that @i{name} as an @i{argument}.
  1522. An error is signaled by the @i{loader} if no @i{package} exists at load time.
  1523. @item @b{random-state}
  1524. Two @i{random states} S and C are @i{similar} if S
  1525. would always produce the same sequence of pseudo-random numbers
  1526. as a @i{copy}_5 of C
  1527. when given as the @i{random-state} @i{argument} to the @i{function} @b{random},
  1528. assuming equivalent @i{limit} @i{arguments} in each case.
  1529. (Note that since C has been processed by the @i{file compiler},
  1530. it cannot be used directly as an @i{argument} to @b{random}
  1531. because @b{random} would perform a side effect.)
  1532. @item @b{cons}
  1533. Two @i{conses}, S and C, are @i{similar} if
  1534. the @i{car}_2 of S is @i{similar} to the @i{car}_2 of C,
  1535. and the @i{cdr}_2 of S is @i{similar} to the @i{cdr}_2 of C.
  1536. @item @b{array}
  1537. Two one-dimensional @i{arrays}, S and C, are @i{similar} if
  1538. the @i{length} of S is @i{similar} to the @i{length} of C,
  1539. the @i{actual array element type} of S is @i{similar} to
  1540. the @i{actual array element type} of C,
  1541. and each @i{active} @i{element} of S is @i{similar} to
  1542. the corresponding @i{element} of C.
  1543. Two @i{arrays} of @i{rank} other than one, S and C, are @i{similar} if
  1544. the @i{rank} of S is @i{similar} to the @i{rank} of C,
  1545. each @i{dimension}_1 of S is @i{similar} to
  1546. the corresponding @i{dimension}_1 of C,
  1547. the @i{actual array element type} of S is @i{similar} to
  1548. the @i{actual array element type} of C,
  1549. and each @i{element} of S is @i{similar} to
  1550. the corresponding @i{element} of C.
  1551. In addition,
  1552. if S is a @i{simple array}, then C must also be a @i{simple array}.
  1553. If S is a @i{displaced array},
  1554. has a @i{fill pointer},
  1555. or is @i{actually adjustable},
  1556. C is permitted to lack any or all of these qualities.
  1557. @item @b{hash-table}
  1558. Two @i{hash tables} S and C are @i{similar} if they meet the following
  1559. three requirements:
  1560. @table @asis
  1561. @item 1.
  1562. They both have the same test
  1563. (@i{e.g.}, they are both @b{eql} @i{hash tables}).
  1564. @item 2.
  1565. There is a unique one-to-one correspondence between the keys of
  1566. the two @i{hash tables}, such that the corresponding keys are
  1567. @i{similar}.
  1568. @item 3.
  1569. For all keys, the values associated with two corresponding keys
  1570. are @i{similar}.
  1571. @end table
  1572. If there is more than one possible one-to-one correspondence between
  1573. the keys of S and C, the consequences are unspecified.
  1574. A @i{conforming program} cannot use a table such as S as an
  1575. @i{externalizable constant}.
  1576. @item @b{pathname}
  1577. Two @i{pathnames} S and C are @i{similar} if all corresponding
  1578. @i{pathname components} are @i{similar}.
  1579. @item @b{function}
  1580. @i{Functions} are not @i{externalizable objects}.
  1581. @item @b{structure-object} and @b{standard-object}
  1582. A general-purpose concept of @i{similarity} does not exist for @i{structures}
  1583. and @i{standard objects}.
  1584. However, a @i{conforming program} is permitted to define a @b{make-load-form}
  1585. @i{method} for any @i{class} K defined by that @i{program} that is
  1586. a @i{subclass} of either @b{structure-object} or @b{standard-object}.
  1587. The effect of such a @i{method} is to define that an @i{object} S of @i{type} K
  1588. in @i{source code} is @i{similar} to an @i{object} C of @i{type} K
  1589. in @i{compiled code} if C was constructed from @i{code} produced by
  1590. calling @b{make-load-form} on S.
  1591. @end table
  1592. @node Extensions to Similarity Rules, Additional Constraints on Externalizable Objects, Definition of Similarity, Literal Objects in Compiled Files
  1593. @subsubsection Extensions to Similarity Rules
  1594. Some @i{objects}, such as @i{streams}, @b{readtables}, and @b{methods}
  1595. are not @i{externalizable objects} under the definition of similarity given above.
  1596. That is, such @i{objects} may not portably appear as @i{literal} @i{objects}
  1597. in @i{code} to be processed by the @i{file compiler}.
  1598. An @i{implementation} is permitted to extend the rules of similarity,
  1599. so that other kinds of @i{objects} are @i{externalizable objects}
  1600. for that @i{implementation}.
  1601. If for some kind of @i{object}, @i{similarity} is
  1602. neither defined by this specification
  1603. nor by the @i{implementation},
  1604. then the @i{file compiler} must signal an error upon encountering such
  1605. an @i{object} as a @i{literal constant}.
  1606. @node Additional Constraints on Externalizable Objects, , Extensions to Similarity Rules, Literal Objects in Compiled Files
  1607. @subsubsection Additional Constraints on Externalizable Objects
  1608. If two @i{literal objects} appearing in the source code for a single file
  1609. processed with
  1610. the @i{file compiler}
  1611. are the @i{identical},
  1612. the corresponding @i{objects} in the @i{compiled code}
  1613. must also be the @i{identical}.
  1614. With the exception of @i{symbols} and @i{packages}, any two
  1615. @i{literal objects}
  1616. in @i{code} being processed by
  1617. the @i{file compiler}
  1618. may be @i{coalesced}
  1619. if and only if they are @i{similar};
  1620. if they are either both @i{symbols} or both @i{packages},
  1621. they may only be @i{coalesced} if and only if they are @i{identical}.
  1622. @i{Objects} containing circular references can
  1623. be @i{externalizable objects}.
  1624. The @i{file compiler} is required to preserve @b{eql}ness of
  1625. substructures within a @i{file}.
  1626. Preserving @b{eql}ness means that subobjects that are
  1627. the @i{same}
  1628. in the @i{source code} must
  1629. be
  1630. the @i{same}
  1631. in the corresponding @i{compiled code}.
  1632. In addition, the following are constraints on the handling of
  1633. @i{literal objects} by the @i{file compiler}:
  1634. @table @asis
  1635. @item @t{}
  1636. @b{array:} If an @i{array} in the source code is a
  1637. @i{simple array}, then the corresponding @i{array}
  1638. in the compiled code will also be a @i{simple array}. If
  1639. an @i{array} in the source code is displaced, has a
  1640. @i{fill pointer}, or is @i{actually adjustable}, the corresponding
  1641. @i{array} in the compiled code might lack any or all of these
  1642. qualities. If an @i{array} in the source code has a fill
  1643. pointer, then the corresponding @i{array} in the compiled
  1644. code might be only the size implied by the fill pointer.
  1645. @item @t{}
  1646. @b{packages:} The loader is required to find the
  1647. corresponding @i{package} @i{object} as if by calling
  1648. @b{find-package} with the package name as an argument.
  1649. An error of @i{type} @b{package-error} is signaled if no
  1650. @i{package} of that name exists at load time.
  1651. @item @t{}
  1652. @b{random-state:} A constant @i{random state}
  1653. object cannot be used as the state argument
  1654. to the @i{function} @b{random} because @b{random} modifies this data structure.
  1655. @item @t{}
  1656. @b{structure, standard-object:}
  1657. @i{Objects} of @i{type} @b{structure-object} and @b{standard-object}
  1658. may appear in compiled constants if there is an
  1659. appropriate @b{make-load-form} method defined for that
  1660. @i{type}.
  1661. The @i{file compiler} calls @b{make-load-form} on any @i{object}
  1662. that is referenced as a @i{literal object} if the @i{object} is a
  1663. @i{generalized instance} of @b{standard-object},
  1664. @b{structure-object}, @b{condition}, or any of a
  1665. (possibly empty) @i{implementation-dependent} set of other @i{classes}.
  1666. The @i{file compiler} only calls @b{make-load-form} once for
  1667. any given @i{object} within a single @i{file}.
  1668. @item @t{}
  1669. @b{symbol:} In order to guarantee that @i{compiled files} can be @i{loaded}
  1670. correctly, users must ensure that the @i{packages} referenced in those @i{files}
  1671. are defined consistently at compile time and load time. @i{Conforming programs}
  1672. must satisfy the following requirements:
  1673. @table @asis
  1674. @item 1.
  1675. The @i{current package} when a @i{top level form} in the @i{file}
  1676. is processed by @b{compile-file} must be the same as the @i{current package}
  1677. when the @i{code} corresponding to that @i{top level form} in the
  1678. @i{compiled file} is executed by @b{load}. In particular:
  1679. @table @asis
  1680. @item a.
  1681. Any @i{top level form} in a @i{file} that alters
  1682. the @i{current package} must change it to a @i{package}
  1683. of the same @i{name} both at compile time and at load time.
  1684. @item b.
  1685. If the first @i{non-atomic} @i{top level form} in the @i{file}
  1686. is not an @b{in-package} @i{form}, then the @i{current package}
  1687. at the time @b{load} is called must be a @i{package} with the
  1688. same @i{name} as the package that was the @i{current package}
  1689. at the time @b{compile-file} was called.
  1690. @end table
  1691. @item 2.
  1692. For all @i{symbols}
  1693. appearing lexically within a @i{top level form} that
  1694. were @i{accessible} in the @i{package} that was the @i{current package}
  1695. during processing of that @i{top level form} at compile time, but
  1696. whose @i{home package} was another @i{package}, at load time there must
  1697. be a @i{symbol} with the same @i{name} that is @i{accessible} in both the
  1698. load-time @i{current package} and in the @i{package}
  1699. with the same @i{name} as the
  1700. compile-time @i{home package}.
  1701. @item 3.
  1702. For all @i{symbols} represented in the @i{compiled file}
  1703. that were @i{external symbols} in
  1704. their @i{home package} at compile time, there must be a @i{symbol} with the
  1705. same @i{name} that is an @i{external symbol} in the @i{package}
  1706. with the same @i{name} at load time.
  1707. @end table
  1708. If any of these conditions do not hold, the @i{package} in which the @i{loader} looks
  1709. for the affected @i{symbols} is unspecified. @i{Implementations} are permitted
  1710. to signal an error or to define this behavior.
  1711. @end table
  1712. @node Exceptional Situations in the Compiler, , Literal Objects in Compiled Files, Compilation
  1713. @subsection Exceptional Situations in the Compiler
  1714. @b{compile} and @b{compile-file} are permitted to
  1715. signal errors and warnings, including errors due to compile-time
  1716. processing of @t{(eval-when (:compile-toplevel) ...)} forms,
  1717. macro expansion, and conditions signaled by the compiler itself.
  1718. @i{Conditions} of @i{type} @b{error} might be signaled by the compiler
  1719. in situations where the compilation cannot proceed without intervention.
  1720. In addition to situations for which the standard specifies that
  1721. @i{conditions} of @i{type} @b{warning} must or might be signaled,
  1722. warnings might be signaled in situations where the compiler can
  1723. determine that the consequences are undefined or that a run-time
  1724. error will be signaled. Examples of this situation are as follows:
  1725. violating type declarations,
  1726. altering or assigning the value of a constant defined with @b{defconstant},
  1727. calling built-in Lisp functions with a wrong number of arguments or malformed keyword
  1728. argument lists,
  1729. and using unrecognized declaration specifiers.
  1730. The compiler is permitted to issue warnings about matters of
  1731. programming style as conditions of @i{type} @b{style-warning}.
  1732. Examples of this situation are as follows:
  1733. redefining a function using a different argument list,
  1734. calling a function with a wrong number of arguments,
  1735. not declaring @b{ignore} of a local variable that is not referenced,
  1736. and referencing a variable declared @b{ignore}.
  1737. Both @b{compile} and @b{compile-file} are permitted
  1738. (but not required) to @i{establish} a @i{handler}
  1739. for @i{conditions} of @i{type} @b{error}. For example, they
  1740. might signal a warning, and restart compilation from some
  1741. @i{implementation-dependent} point in order to let the
  1742. compilation proceed without manual intervention.
  1743. Both @b{compile} and @b{compile-file} return three
  1744. values, the second two indicating whether the source code being compiled
  1745. contained errors and whether style warnings were issued.
  1746. Some warnings might be deferred until the end of compilation.
  1747. See @b{with-compilation-unit}.
  1748. @c end of including concept-compile
  1749. @node Declarations, Lambda Lists, Compilation, Evaluation and Compilation
  1750. @section Declarations
  1751. @c including concept-decls
  1752. @i{Declarations}
  1753. @IGindex{declaration}
  1754. provide a way of specifying information for use by
  1755. program processors, such as the evaluator or the compiler.
  1756. @i{Local declarations}
  1757. @IGindex{local declaration}
  1758. can be embedded in executable code using @b{declare}.
  1759. @i{Global declarations}
  1760. @IGindex{global declaration}
  1761. ,
  1762. or @i{proclamations}
  1763. @IGindex{proclamation}
  1764. ,
  1765. are established by @b{proclaim} or @b{declaim}.
  1766. The @b{the} @i{special form} provides a shorthand notation for
  1767. making a @i{local declaration} about the @i{type} of the
  1768. @i{value} of a given @i{form}.
  1769. The consequences are undefined if a program violates a @i{declaration}
  1770. or a @i{proclamation}.
  1771. @menu
  1772. * Minimal Declaration Processing Requirements::
  1773. * Declaration Specifiers::
  1774. * Declaration Identifiers::
  1775. * Declaration Scope::
  1776. @end menu
  1777. @node Minimal Declaration Processing Requirements, Declaration Specifiers, Declarations, Declarations
  1778. @subsection Minimal Declaration Processing Requirements
  1779. In general, an @i{implementation} is free to ignore
  1780. @i{declaration specifiers} except for the
  1781. @b{declaration}
  1782. @IRindex{declaration}
  1783. ,
  1784. @b{notinline}
  1785. @IRindex{notinline}
  1786. ,
  1787. @b{safety}
  1788. @IRindex{safety}
  1789. ,
  1790. and @b{special}
  1791. @IRindex{special}
  1792. @i{declaration specifiers}.
  1793. A @b{declaration} @i{declaration} must suppress warnings
  1794. about unrecognized @i{declarations} of the kind that it declares.
  1795. If an @i{implementation} does not produce warnings about
  1796. unrecognized declarations, it may safely ignore this @i{declaration}.
  1797. A @b{notinline} @i{declaration} must be recognized by any @i{implementation}
  1798. that supports inline functions or @i{compiler macros} in order to disable those facilities.
  1799. An @i{implementation} that does not use inline functions or @i{compiler macros}
  1800. may safely ignore this @i{declaration}.
  1801. A @b{safety} @i{declaration} that increases the current safety level
  1802. must always be recognized. An @i{implementation} that always processes
  1803. code as if safety were high may safely ignore this @i{declaration}.
  1804. A @b{special} @i{declaration} must be processed by all @i{implementations}.
  1805. @node Declaration Specifiers, Declaration Identifiers, Minimal Declaration Processing Requirements, Declarations
  1806. @subsection Declaration Specifiers
  1807. A @i{declaration specifier}
  1808. @IGindex{declaration specifier}
  1809. is an @i{expression} that can appear at
  1810. top level of a @b{declare} expression or a @b{declaim} form, or as
  1811. the argument to @b{proclaim}.
  1812. It is a @i{list} whose @i{car} is a @i{declaration identifier},
  1813. and whose @i{cdr} is data interpreted according to rules specific to
  1814. the @i{declaration identifier}.
  1815. @node Declaration Identifiers, Declaration Scope, Declaration Specifiers, Declarations
  1816. @subsection Declaration Identifiers
  1817. Figure 3--9 shows a list of all
  1818. @i{declaration identifiers}
  1819. @IGindex{declaration identifier}
  1820. defined by this standard.
  1821. @group
  1822. @noindent
  1823. @w{ declaration ignore special }
  1824. @w{ dynamic-extent inline type }
  1825. @w{ ftype notinline }
  1826. @w{ ignorable optimize }
  1827. @noindent
  1828. @w{ Figure 3--9: Common Lisp Declaration Identifiers}
  1829. @end group
  1830. An implementation is free to support other (@i{implementation-defined})
  1831. @i{declaration identifiers} as well.
  1832. A warning might be issued
  1833. if a @i{declaration identifier}
  1834. is not among those defined above,
  1835. is not defined by the @i{implementation},
  1836. is not a @i{type} @i{name},
  1837. and has not been declared in a @b{declaration} @i{proclamation}.
  1838. @menu
  1839. * Shorthand notation for Type Declarations::
  1840. @end menu
  1841. @node Shorthand notation for Type Declarations, , Declaration Identifiers, Declaration Identifiers
  1842. @subsubsection Shorthand notation for Type Declarations
  1843. A @i{type specifier} can be used as a @i{declaration identifier}.
  1844. @t{(@i{type-specifier} @{@i{var}@}{*})} is taken as shorthand for
  1845. @t{(type @i{type-specifier} @{@i{var}@}{*})}.
  1846. @node Declaration Scope, , Declaration Identifiers, Declarations
  1847. @subsection Declaration Scope
  1848. @i{Declarations} can be divided into two kinds: those that apply to the
  1849. @i{bindings} of @i{variables} or @i{functions}; and those that
  1850. do not apply to @i{bindings}.
  1851. A @i{declaration} that appears at the head of a binding @i{form}
  1852. and applies to a @i{variable} or @i{function} @i{binding}
  1853. made by that @i{form} is called a @i{bound declaration}
  1854. @IGindex{bound declaration}
  1855. ;
  1856. such a @i{declaration} affects both the @i{binding} and
  1857. any references within the @i{scope} of the @i{declaration}.
  1858. @i{Declarations} that are not @i{bound declarations} are called
  1859. @i{free declarations}
  1860. @IGindex{free declaration}
  1861. .
  1862. A @i{free declaration} in a @i{form} F1 that applies to a @i{binding}
  1863. for a @i{name} N @i{established} by some @i{form} F2
  1864. of which F1 is a @i{subform}
  1865. affects only references to N within F1; it does not to apply to
  1866. other references to N outside of F1, nor does it affect the manner
  1867. in which the @i{binding} of N by F2 is @i{established}.
  1868. @i{Declarations} that do not apply to @i{bindings} can only appear
  1869. as @i{free declarations}.
  1870. The @i{scope} of a @i{bound declaration} is the same as the
  1871. @i{lexical scope}
  1872. of the @i{binding} to which it applies;
  1873. for @i{special variables},
  1874. this means the @i{scope} that the @i{binding}
  1875. would have had had it been a @i{lexical binding}.
  1876. Unless explicitly stated otherwise, the @i{scope} of a
  1877. @i{free declaration} includes only the body @i{subforms} of
  1878. the @i{form} at whose head it appears, and no other @i{subforms}.
  1879. The @i{scope} of @i{free declarations} specifically does not
  1880. include @i{initialization forms} for @i{bindings} established
  1881. by the @i{form} containing the @i{declarations}.
  1882. Some @i{iteration forms} include step, end-test, or result
  1883. @i{subforms} that are also included in the @i{scope}
  1884. of @i{declarations} that appear in the @i{iteration form}.
  1885. Specifically, the @i{iteration forms} and @i{subforms} involved
  1886. are:
  1887. @table @asis
  1888. @item @t{*}
  1889. @b{do}, @b{do*}:
  1890. @i{step-forms}, @i{end-test-form}, and @i{result-forms}.
  1891. @item @t{*}
  1892. @b{dolist}, @b{dotimes}:
  1893. @i{result-form}
  1894. @item @t{*}
  1895. @b{do-all-symbols}, @b{do-external-symbols}, @b{do-symbols}:
  1896. @i{result-form}
  1897. @end table
  1898. @menu
  1899. * Examples of Declaration Scope::
  1900. @end menu
  1901. @node Examples of Declaration Scope, , Declaration Scope, Declaration Scope
  1902. @subsubsection Examples of Declaration Scope
  1903. Here is an example illustrating the @i{scope} of @i{bound declarations}.
  1904. @example
  1905. (let ((x 1)) ;[1] 1st occurrence of x
  1906. (declare (special x)) ;[2] 2nd occurrence of x
  1907. (let ((x 2)) ;[3] 3rd occurrence of x
  1908. (let ((old-x x) ;[4] 4th occurrence of x
  1909. (x 3)) ;[5] 5th occurrence of x
  1910. (declare (special x)) ;[6] 6th occurrence of x
  1911. (list old-x x)))) ;[7] 7th occurrence of x
  1912. @result{} (2 3)
  1913. @end example
  1914. The first occurrence of @t{x} @i{establishes} a @i{dynamic binding}
  1915. of @t{x} because of the @b{special} @i{declaration} for @t{x}
  1916. in the second line. The third occurrence of @t{x} @i{establishes} a
  1917. @i{lexical binding} of @t{x} (because there is no @b{special}
  1918. @i{declaration} in the corresponding @b{let} @i{form}).
  1919. The fourth occurrence of @t{x} @i{x} is a reference to the
  1920. @i{lexical binding} of @t{x} established in the third line.
  1921. The fifth occurrence of @t{x} @i{establishes} a @i{dynamic binding}
  1922. of @i{x} for the body of the @b{let} @i{form} that begins on
  1923. that line because of the @b{special} @i{declaration} for @t{x}
  1924. in the sixth line. The reference to @t{x} in the fourth line is not
  1925. affected by the @b{special} @i{declaration} in the sixth line
  1926. because that reference is not within the ``would-be @i{lexical scope}''
  1927. of the @i{variable} @t{x} in the fifth line. The reference to @t{x}
  1928. in the seventh line is a reference to the @i{dynamic binding} of @i{x}
  1929. @i{established} in the fifth line.
  1930. Here is another example, to illustrate the @i{scope} of a
  1931. @i{free declaration}. In the following:
  1932. @example
  1933. (lambda (&optional (x (foo 1))) ;[1]
  1934. (declare (notinline foo)) ;[2]
  1935. (foo x)) ;[3]
  1936. @end example
  1937. the @i{call} to @t{foo} in the first line might be
  1938. compiled inline even though the @i{call} to @t{foo} in
  1939. the third line must not be. This is because
  1940. the @b{notinline} @i{declaration}
  1941. for @t{foo} in the second line applies only to the body on the
  1942. third line. In order to suppress inlining for both @i{calls},
  1943. one might write:
  1944. @example
  1945. (locally (declare (notinline foo)) ;[1]
  1946. (lambda (&optional (x (foo 1))) ;[2]
  1947. (foo x))) ;[3]
  1948. @end example
  1949. or, alternatively:
  1950. @example
  1951. (lambda (&optional ;[1]
  1952. (x (locally (declare (notinline foo)) ;[2]
  1953. (foo 1)))) ;[3]
  1954. (declare (notinline foo)) ;[4]
  1955. (foo x)) ;[5]
  1956. @end example
  1957. Finally, here is an example that shows the @i{scope} of
  1958. @i{declarations} in an @i{iteration form}.
  1959. @example
  1960. (let ((x 1)) ;[1]
  1961. (declare (special x)) ;[2]
  1962. (let ((x 2)) ;[3]
  1963. (dotimes (i x x) ;[4]
  1964. (declare (special x))))) ;[5]
  1965. @result{} 1
  1966. @end example
  1967. In this example, the first reference to @t{x} on the fourth line is to
  1968. the @i{lexical binding} of @t{x} established on the third line.
  1969. However, the second occurrence of @t{x} on the fourth line lies within
  1970. the @i{scope} of the @i{free declaration} on the fifth line
  1971. (because this is the @i{result-form} of the @b{dotimes})
  1972. and therefore refers to the @i{dynamic binding} of @t{x}.
  1973. @c end of including concept-decls
  1974. @node Lambda Lists, Error Checking in Function Calls, Declarations, Evaluation and Compilation
  1975. @section Lambda Lists
  1976. @c including concept-bvl
  1977. A @i{lambda list}
  1978. @IGindex{lambda list}
  1979. is a @i{list} that
  1980. specifies a set of @i{parameters} (sometimes called @i{lambda variables})
  1981. and a protocol for receiving @i{values} for those @i{parameters}.
  1982. There are several kinds of @i{lambda lists}.
  1983. @group
  1984. @noindent
  1985. @w{ Context Kind of Lambda List }
  1986. @w{ @b{defun} @i{form} @i{ordinary lambda list} }
  1987. @w{ @b{defmacro} @i{form} @i{macro lambda list} }
  1988. @w{ @i{lambda expression} @i{ordinary lambda list} }
  1989. @w{ @b{flet} local @i{function} definition @i{ordinary lambda list} }
  1990. @w{ @b{labels} local @i{function} definition @i{ordinary lambda list} }
  1991. @w{ @b{handler-case} @i{clause} specification @i{ordinary lambda list} }
  1992. @w{ @b{restart-case} @i{clause} specification @i{ordinary lambda list} }
  1993. @w{ @b{macrolet} local @i{macro} definition @i{macro lambda list} }
  1994. @w{ @b{define-method-combination} @i{ordinary lambda list} }
  1995. @w{ @b{define-method-combination} @t{:arguments} option @i{define-method-combination arguments lambda list} }
  1996. @w{ @b{defstruct} @t{:constructor} option @i{boa lambda list} }
  1997. @w{ @b{defgeneric} @i{form} @i{generic function lambda list} }
  1998. @w{ @b{defgeneric} @i{method} clause @i{specialized lambda list} }
  1999. @w{ @b{defmethod} @i{form} @i{specialized lambda list} }
  2000. @w{ @b{defsetf} @i{form} @i{defsetf lambda list} }
  2001. @w{ @b{define-setf-expander} @i{form} @i{macro lambda list} }
  2002. @w{ @b{deftype} @i{form} @i{deftype lambda list} }
  2003. @w{ @b{destructuring-bind} @i{form} @i{destructuring lambda list} }
  2004. @w{ @b{define-compiler-macro} @i{form} @i{macro lambda list} }
  2005. @w{ @b{define-modify-macro} @i{form} @i{define-modify-macro lambda list} }
  2006. @noindent
  2007. @w{ Figure 3--10: What Kind of Lambda Lists to Use }
  2008. @end group
  2009. Figure 3--11 lists some @i{defined names} that are applicable
  2010. to @i{lambda lists}.
  2011. @group
  2012. @noindent
  2013. @w{ lambda-list-keywords lambda-parameters-limit }
  2014. @noindent
  2015. @w{ Figure 3--11: Defined names applicable to lambda lists}
  2016. @end group
  2017. @menu
  2018. * Ordinary Lambda Lists::
  2019. * Generic Function Lambda Lists::
  2020. * Specialized Lambda Lists::
  2021. * Macro Lambda Lists::
  2022. * Destructuring Lambda Lists::
  2023. * Boa Lambda Lists::
  2024. * Defsetf Lambda Lists::
  2025. * Deftype Lambda Lists::
  2026. * Define-modify-macro Lambda Lists::
  2027. * Define-method-combination Arguments Lambda Lists::
  2028. * Syntactic Interaction of Documentation Strings and Declarations::
  2029. @end menu
  2030. @node Ordinary Lambda Lists, Generic Function Lambda Lists, Lambda Lists, Lambda Lists
  2031. @subsection Ordinary Lambda Lists
  2032. An @i{ordinary lambda list}
  2033. @IGindex{ordinary lambda list}
  2034. is used to describe how a set of
  2035. @i{arguments} is received by an @i{ordinary} @i{function}.
  2036. The @i{defined names} in Figure 3--12 are those which use
  2037. @i{ordinary lambda lists}:
  2038. @group
  2039. @noindent
  2040. @w{ define-method-combination handler-case restart-case }
  2041. @w{ defun labels }
  2042. @w{ flet lambda }
  2043. @noindent
  2044. @w{ Figure 3--12: Standardized Operators that use Ordinary Lambda Lists}
  2045. @end group
  2046. An @i{ordinary lambda list} can contain the @i{lambda list keywords} shown
  2047. in Figure 3--13.
  2048. @group
  2049. @noindent
  2050. @w{ @b{&allow-other-keys} @b{&key} @b{&rest} }
  2051. @w{ @b{&aux} @b{&optional} }
  2052. @noindent
  2053. @w{ Figure 3--13: Lambda List Keywords used by Ordinary Lambda Lists}
  2054. @end group
  2055. Each @i{element} of a @i{lambda list} is either a parameter specifier
  2056. or a @i{lambda list keyword}.
  2057. Implementations are free to provide additional @i{lambda list keywords}.
  2058. For a list of all @i{lambda list keywords}
  2059. used by the implementation, see @b{lambda-list-keywords}.
  2060. The syntax for @i{ordinary lambda lists} is as follows:
  2061. @w{@i{lambda-list} ::=@r{(}@{@i{var}@}{*}}
  2062. @w{ @t{[}{&optional} @{@i{var} |
  2063. @r{(}@i{var} @r{[}init-form @r{[}supplied-p-parameter@r{]}@r{]}@r{)}@}{*}@t{]}}
  2064. @w{ @t{[}{&rest} @i{var}@t{]}}
  2065. @w{ @t{[}{&key} @{@i{var} |
  2066. @r{(}@{@i{var} |
  2067. @r{(}@i{keyword-name} @i{var}@r{)}@}
  2068. @r{[}init-form @r{[}supplied-p-parameter@r{]}@r{]}@r{)}@}{*} pt @r{[}@t{&allow-other-keys}@r{]}@t{]}}
  2069. @w{ @t{[}{&aux} @{@i{var} | @r{(}@i{var} @r{[}@i{init-form}@r{]}@r{)}@}{*}@t{]}@r{)}}
  2070. @w{ }
  2071. A @i{var} or @i{supplied-p-parameter} must be a @i{symbol}
  2072. that is not the name of a @i{constant variable}.
  2073. An @i{init-form} can be any @i{form}.
  2074. Whenever any @i{init-form} is evaluated for any parameter
  2075. specifier, that @i{form} may refer to any parameter variable to
  2076. the left of the specifier in which the @i{init-form} appears,
  2077. including any @i{supplied-p-parameter} variables, and may rely
  2078. on the fact that no other parameter variable has yet been bound
  2079. (including its own parameter variable).
  2080. A @i{keyword-name} can be any @i{symbol},
  2081. but by convention is normally a @i{keyword}_1;
  2082. all @i{standardized} @i{functions} follow that convention.
  2083. An @i{ordinary lambda list} has five parts, any or all of which may be empty.
  2084. For information about the treatment of argument mismatches,
  2085. see @ref{Error Checking in Function Calls}.
  2086. @menu
  2087. * Specifiers for the required parameters::
  2088. * Specifiers for optional parameters::
  2089. * A specifier for a rest parameter::
  2090. * Specifiers for keyword parameters::
  2091. * Suppressing Keyword Argument Checking::
  2092. * Examples of Suppressing Keyword Argument Checking::
  2093. * Specifiers for @b{&aux} variables::
  2094. * Examples of Ordinary Lambda Lists::
  2095. @end menu
  2096. @node Specifiers for the required parameters, Specifiers for optional parameters, Ordinary Lambda Lists, Ordinary Lambda Lists
  2097. @subsubsection Specifiers for the required parameters
  2098. These are all the parameter specifiers up to
  2099. the first @i{lambda list keyword};
  2100. if there are no @i{lambda list keywords},
  2101. then all the specifiers are for required parameters.
  2102. Each required parameter is specified by a parameter variable @i{var}.
  2103. @i{var} is bound as a lexical variable unless it is declared @b{special}.
  2104. If there are @t{n} required parameters (@t{n} may be zero),
  2105. there must be at least @t{n} passed arguments, and the
  2106. required parameters are bound to the first @t{n} passed arguments;
  2107. see @ref{Error Checking in Function Calls}.
  2108. The other parameters are then processed using any remaining arguments.
  2109. @node Specifiers for optional parameters, A specifier for a rest parameter, Specifiers for the required parameters, Ordinary Lambda Lists
  2110. @subsubsection Specifiers for optional parameters
  2111. @IRindex{&optional}
  2112. If @b{&optional} is present,
  2113. the optional parameter specifiers are those following
  2114. @b{&optional}
  2115. up to the next @i{lambda list keyword} or the end of the list.
  2116. If optional parameters are specified, then each one is processed as
  2117. follows. If any unprocessed arguments remain, then the parameter variable
  2118. @i{var} is bound to the next remaining argument, just as for a required
  2119. parameter. If no arguments remain, however, then @i{init-form}
  2120. is evaluated, and the parameter variable
  2121. is bound to the resulting value
  2122. (or to @b{nil} if no @i{init-form} appears
  2123. in the parameter specifier).
  2124. If another variable name @i{supplied-p-parameter}
  2125. appears in the specifier, it is bound
  2126. to @i{true} if an argument had been available, and to @i{false} if no
  2127. argument remained (and therefore @i{init-form} had to be evaluated).
  2128. @i{Supplied-p-parameter}
  2129. is bound not to an argument but to a value indicating whether or not
  2130. an argument had been supplied for the corresponding @i{var}.
  2131. @node A specifier for a rest parameter, Specifiers for keyword parameters, Specifiers for optional parameters, Ordinary Lambda Lists
  2132. @subsubsection A specifier for a rest parameter
  2133. @IRindex{&rest}
  2134. @b{&rest}, if present, must be followed by a single @i{rest parameter}
  2135. specifier, which in turn must be followed by another
  2136. @i{lambda list keyword} or the end of the @i{lambda list}. After all
  2137. optional parameter specifiers have been processed, then there may or
  2138. may not be a @i{rest parameter}. If there is a @i{rest parameter}, it is
  2139. bound to a @i{list} of all as-yet-unprocessed arguments. If
  2140. no unprocessed arguments remain, the @i{rest parameter} is bound to the
  2141. @i{empty list}. If there is no @i{rest parameter} and there are no
  2142. @i{keyword parameters}, then an error
  2143. should be signaled if
  2144. any unprocessed arguments remain; see @ref{Error Checking in Function Calls}.
  2145. The value of a @i{rest parameter}
  2146. is permitted, but not required, to share structure with the
  2147. last argument to @b{apply}.
  2148. @IRindex{&key}
  2149. @IRindex{&allow-other-keys}
  2150. @node Specifiers for keyword parameters, Suppressing Keyword Argument Checking, A specifier for a rest parameter, Ordinary Lambda Lists
  2151. @subsubsection Specifiers for keyword parameters
  2152. If @b{&key}
  2153. is present, all specifiers up to the next @i{lambda list keyword}
  2154. or the end of the @i{list} are keyword parameter specifiers.
  2155. When keyword parameters are processed,
  2156. the same arguments are processed that
  2157. would be made into a @i{list} for a @i{rest parameter}.
  2158. It is permitted to specify both @b{&rest} and @b{&key}.
  2159. In this case the remaining arguments are used for both purposes;
  2160. that is, all remaining arguments are made into a @i{list} for the
  2161. @i{rest parameter}, and are also processed for the @b{&key} parameters.
  2162. If @b{&key} is specified, there must remain
  2163. an even number of arguments; see @ref{Odd Number of Keyword Arguments}.
  2164. These arguments are considered as pairs,
  2165. the first argument in each pair being interpreted as a name
  2166. and the second as the corresponding value.
  2167. The first @i{object} of each pair must be a @i{symbol};
  2168. see @ref{Invalid Keyword Arguments}.
  2169. The keyword parameter specifiers may optionally be followed by the
  2170. @i{lambda list keyword} @b{&allow-other-keys}.
  2171. In each keyword parameter specifier must be a name @i{var} for
  2172. the parameter variable.
  2173. If the @i{var} appears alone or in a @t{(@i{var} @i{init-form})}
  2174. combination, the keyword name used when matching @i{arguments} to @i{parameters}
  2175. is a @i{symbol} in the @t{KEYWORD} @i{package} whose @i{name} is the
  2176. @i{same} (under @b{string=}) as @i{var}'s.
  2177. If the notation @t{((@i{keyword-name} @i{var}) @i{init-form})} is used,
  2178. then the keyword name used to match @i{arguments} to @i{parameters} is
  2179. @i{keyword-name}, which may be a @i{symbol} in any @i{package}.
  2180. (Of course, if it is not a @i{symbol} in the @t{KEYWORD} @i{package},
  2181. it does not necessarily self-evaluate, so care must be taken when calling the function
  2182. to make sure that normal evaluation still yields the keyword name.)
  2183. Thus
  2184. @example
  2185. (defun foo (&key radix (type 'integer)) ...)
  2186. @end example
  2187. means exactly the same as
  2188. @example
  2189. (defun foo (&key ((:radix radix)) ((:type type) 'integer)) ...)
  2190. @end example
  2191. The keyword parameter specifiers are, like all parameter specifiers,
  2192. effectively processed from left to right. For each keyword parameter
  2193. specifier, if there is an argument pair whose name matches that
  2194. specifier's name (that is, the names are @b{eq}), then the
  2195. parameter variable for that specifier is bound to the second item (the
  2196. value) of that argument pair. If more than one such argument pair
  2197. matches, the leftmost argument pair is used. If no such argument pair
  2198. exists, then the @i{init-form} for that specifier is evaluated and
  2199. the parameter variable is bound to that value (or to @b{nil} if no
  2200. @i{init-form} was specified). @i{supplied-p-parameter} is
  2201. treated as for @b{&optional} parameters: it is bound to @i{true} if there
  2202. was a matching argument pair, and to @i{false} otherwise.
  2203. Unless keyword argument checking is suppressed,
  2204. an argument pair must a name matched by a parameter specifier;
  2205. see @ref{Unrecognized Keyword Arguments}.
  2206. If keyword argument checking is suppressed,
  2207. then it is permitted for an argument pair
  2208. to match no parameter specifier, and the argument pair is ignored, but
  2209. such an argument pair is accessible through the @i{rest parameter} if
  2210. one was supplied. The purpose of these mechanisms is to allow sharing
  2211. of argument lists among several @i{lambda expressions} and to
  2212. allow either the caller or the called @i{lambda expression} to
  2213. specify that such sharing may be taking place.
  2214. Note that if @b{&key} is present, a keyword argument of @t{:allow-other-keys}
  2215. is always permitted---regardless of whether the associated value is @i{true}
  2216. or @i{false}. However, if the value is @i{false}, other non-matching
  2217. keywords are not tolerated (unless @b{&allow-other-keys} was used).
  2218. Furthermore, if the receiving argument list specifies a regular argument which
  2219. would be flagged by @t{:allow-other-keys}, then @t{:allow-other-keys} has both
  2220. its special-cased meaning (identifying whether additional keywords are permitted)
  2221. and its normal meaning (data flow into the function in question).
  2222. @node Suppressing Keyword Argument Checking, Examples of Suppressing Keyword Argument Checking, Specifiers for keyword parameters, Ordinary Lambda Lists
  2223. @subsubsection Suppressing Keyword Argument Checking
  2224. If @b{&allow-other-keys} was specified in the @i{lambda list} of a @i{function},
  2225. @i{keyword}_2 @i{argument} checking is suppressed in calls
  2226. to that @i{function}.
  2227. If the @t{:allow-other-keys} @i{argument} is @i{true} in a call to a @i{function},
  2228. @i{keyword}_2 @i{argument} checking is suppressed
  2229. in that call.
  2230. The @t{:allow-other-keys} @i{argument} is permissible in all situations involving
  2231. @i{keyword}_2 @i{arguments}, even when its associated @i{value}
  2232. is @i{false}.
  2233. @node Examples of Suppressing Keyword Argument Checking, Specifiers for @b{&aux} variables, Suppressing Keyword Argument Checking, Ordinary Lambda Lists
  2234. @subsubsection Examples of Suppressing Keyword Argument Checking
  2235. @example
  2236. ;;; The caller can supply :ALLOW-OTHER-KEYS T to suppress checking.
  2237. ((lambda (&key x) x) :x 1 :y 2 :allow-other-keys t) @result{} 1
  2238. ;;; The callee can use &ALLOW-OTHER-KEYS to suppress checking.
  2239. ((lambda (&key x &allow-other-keys) x) :x 1 :y 2) @result{} 1
  2240. ;;; :ALLOW-OTHER-KEYS NIL is always permitted.
  2241. ((lambda (&key) t) :allow-other-keys nil) @result{} T
  2242. ;;; As with other keyword arguments, only the left-most pair
  2243. ;;; named :ALLOW-OTHER-KEYS has any effect.
  2244. ((lambda (&key x) x)
  2245. :x 1 :y 2 :allow-other-keys t :allow-other-keys nil)
  2246. @result{} 1
  2247. ;;; Only the left-most pair named :ALLOW-OTHER-KEYS has any effect,
  2248. ;;; so in safe code this signals a PROGRAM-ERROR (and might enter the
  2249. ;;; debugger). In unsafe code, the consequences are undefined.
  2250. ((lambda (&key x) x) ;This call is not valid
  2251. :x 1 :y 2 :allow-other-keys nil :allow-other-keys t)
  2252. @end example
  2253. @node Specifiers for @b{&aux} variables, Examples of Ordinary Lambda Lists, Examples of Suppressing Keyword Argument Checking, Ordinary Lambda Lists
  2254. @subsubsection Specifiers for @b{&aux} variables
  2255. @IRindex{&aux}
  2256. These are not really parameters. If the @i{lambda list keyword}
  2257. @b{&aux} is present, all specifiers after it are auxiliary variable
  2258. specifiers. After all parameter specifiers have been processed, the
  2259. auxiliary variable specifiers (those following {&aux}) are processed
  2260. from left to right. For each one, @i{init-form} is evaluated and
  2261. @i{var} is bound to that value (or to @b{nil} if no @i{init-form}
  2262. was specified). @b{&aux} variable processing is analogous to
  2263. @b{let*} processing.
  2264. @example
  2265. (lambda (x y &aux (a (car x)) (b 2) c) (list x y a b c))
  2266. @equiv{} (lambda (x y) (let* ((a (car x)) (b 2) c) (list x y a b c)))
  2267. @end example
  2268. @node Examples of Ordinary Lambda Lists, , Specifiers for @b{&aux} variables, Ordinary Lambda Lists
  2269. @subsubsection Examples of Ordinary Lambda Lists
  2270. Here are some examples involving @i{optional parameters} and @i{rest parameters}:
  2271. @example
  2272. ((lambda (a b) (+ a (* b 3))) 4 5) @result{} 19
  2273. ((lambda (a &optional (b 2)) (+ a (* b 3))) 4 5) @result{} 19
  2274. ((lambda (a &optional (b 2)) (+ a (* b 3))) 4) @result{} 10
  2275. ((lambda (&optional (a 2 b) (c 3 d) &rest x) (list a b c d x)))
  2276. @result{} (2 NIL 3 NIL NIL)
  2277. ((lambda (&optional (a 2 b) (c 3 d) &rest x) (list a b c d x)) 6)
  2278. @result{} (6 T 3 NIL NIL)
  2279. ((lambda (&optional (a 2 b) (c 3 d) &rest x) (list a b c d x)) 6 3)
  2280. @result{} (6 T 3 T NIL)
  2281. ((lambda (&optional (a 2 b) (c 3 d) &rest x) (list a b c d x)) 6 3 8)
  2282. @result{} (6 T 3 T (8))
  2283. ((lambda (&optional (a 2 b) (c 3 d) &rest x) (list a b c d x))
  2284. 6 3 8 9 10 11)
  2285. @result{} (6 t 3 t (8 9 10 11))
  2286. @end example
  2287. Here are some examples involving @i{keyword parameters}:
  2288. @example
  2289. ((lambda (a b &key c d) (list a b c d)) 1 2) @result{} (1 2 NIL NIL)
  2290. ((lambda (a b &key c d) (list a b c d)) 1 2 :c 6) @result{} (1 2 6 NIL)
  2291. ((lambda (a b &key c d) (list a b c d)) 1 2 :d 8) @result{} (1 2 NIL 8)
  2292. ((lambda (a b &key c d) (list a b c d)) 1 2 :c 6 :d 8) @result{} (1 2 6 8)
  2293. ((lambda (a b &key c d) (list a b c d)) 1 2 :d 8 :c 6) @result{} (1 2 6 8)
  2294. ((lambda (a b &key c d) (list a b c d)) :a 1 :d 8 :c 6) @result{} (:a 1 6 8)
  2295. ((lambda (a b &key c d) (list a b c d)) :a :b :c :d) @result{} (:a :b :d NIL)
  2296. ((lambda (a b &key ((:sea c)) d) (list a b c d)) 1 2 :sea 6) @result{} (1 2 6 NIL)
  2297. ((lambda (a b &key ((c c)) d) (list a b c d)) 1 2 'c 6) @result{} (1 2 6 NIL)
  2298. @end example
  2299. Here are some examples involving @i{optional parameters}, @i{rest parameters},
  2300. and @i{keyword parameters} together:
  2301. @example
  2302. ((lambda (a &optional (b 3) &rest x &key c (d a))
  2303. (list a b c d x)) 1)
  2304. @result{} (1 3 NIL 1 ())
  2305. ((lambda (a &optional (b 3) &rest x &key c (d a))
  2306. (list a b c d x)) 1 2)
  2307. @result{} (1 2 NIL 1 ())
  2308. ((lambda (a &optional (b 3) &rest x &key c (d a))
  2309. (list a b c d x)) :c 7)
  2310. @result{} (:c 7 NIL :c ())
  2311. ((lambda (a &optional (b 3) &rest x &key c (d a))
  2312. (list a b c d x)) 1 6 :c 7)
  2313. @result{} (1 6 7 1 (:c 7))
  2314. ((lambda (a &optional (b 3) &rest x &key c (d a))
  2315. (list a b c d x)) 1 6 :d 8)
  2316. @result{} (1 6 NIL 8 (:d 8))
  2317. ((lambda (a &optional (b 3) &rest x &key c (d a))
  2318. (list a b c d x)) 1 6 :d 8 :c 9 :d 10)
  2319. @result{} (1 6 9 8 (:d 8 :c 9 :d 10))
  2320. @end example
  2321. As an example of the use of @b{&allow-other-keys} and
  2322. @t{:allow-other-keys}, consider a @i{function} that takes two named
  2323. arguments of its own and also accepts additional named arguments to be
  2324. passed to @b{make-array}:
  2325. @example
  2326. (defun array-of-strings (str dims &rest named-pairs
  2327. &key (start 0) end &allow-other-keys)
  2328. (apply #'make-array dims
  2329. :initial-element (subseq str start end)
  2330. :allow-other-keys t
  2331. named-pairs))
  2332. @end example
  2333. This @i{function} takes a @i{string} and dimensioning
  2334. information and returns an @i{array} of the specified
  2335. dimensions, each of whose elements is the specified
  2336. @i{string}. However, @t{:start} and @t{:end} named
  2337. arguments may be used to specify that a substring of the given
  2338. @i{string} should be used. In addition, the presence of
  2339. @b{&allow-other-keys} in the @i{lambda list} indicates that the
  2340. caller may supply additional named arguments; the @i{rest parameter}
  2341. provides access to them. These additional named arguments are passed
  2342. to @b{make-array}. The @i{function} @b{make-array}
  2343. normally does not allow the named arguments @t{:start}
  2344. and @t{:end} to be used, and an error should be
  2345. signaled if such named arguments are supplied to @b{make-array}.
  2346. However, the presence in the call to @b{make-array}
  2347. of the named argument @t{:allow-other-keys} with
  2348. a @i{true} value causes any extraneous named arguments, including
  2349. @t{:start} and @t{:end}, to be acceptable and ignored.
  2350. @node Generic Function Lambda Lists, Specialized Lambda Lists, Ordinary Lambda Lists, Lambda Lists
  2351. @subsection Generic Function Lambda Lists
  2352. A @i{generic function lambda list}
  2353. @IGindex{generic function lambda list}
  2354. is used to describe the overall shape of
  2355. the argument list to be accepted by a @i{generic function}.
  2356. Individual @i{method} @i{signatures} might contribute additional
  2357. @i{keyword parameters} to the @i{lambda list} of the @i{effective method}.
  2358. A @i{generic function lambda list} is used by @b{defgeneric}.
  2359. A @i{generic function lambda list} has the following syntax:
  2360. @w{@i{lambda-list} ::=@r{(}@{@i{var}@}{*}}
  2361. @w{ @t{[}{&optional} @{@i{var} | @r{(}@i{var}@r{)}@}{*}@t{]}}
  2362. @w{ @t{[}{&rest} @i{var}@t{]}}
  2363. @w{ @t{[}{&key} @{@i{var} | @r{(}@{@i{var} |
  2364. @r{(}@i{keyword-name} @i{var}@r{)}@}{)}@}{*} pt @r{[}@t{&allow-other-keys}@r{]}@t{]}@r{)}}
  2365. @w{ }
  2366. A @i{generic function lambda list} can contain the @i{lambda list keywords} shown
  2367. in Figure 3--14.
  2368. @group
  2369. @noindent
  2370. @w{ @b{&allow-other-keys} @b{&optional} }
  2371. @w{ @b{&key} @b{&rest} }
  2372. @noindent
  2373. @w{ Figure 3--14: Lambda List Keywords used by Generic Function Lambda Lists}
  2374. @end group
  2375. A @i{generic function lambda list} differs from an @i{ordinary lambda list}
  2376. in the following ways:
  2377. @table @asis
  2378. @item Required arguments
  2379. Zero or more @i{required parameters} must be specified.
  2380. @item Optional and keyword arguments
  2381. @i{Optional parameters} and @i{keyword parameters} may not have
  2382. default initial value forms nor use supplied-p parameters.
  2383. @item Use of @b{&aux}
  2384. The use of @b{&aux} is not allowed.
  2385. @end table
  2386. @node Specialized Lambda Lists, Macro Lambda Lists, Generic Function Lambda Lists, Lambda Lists
  2387. @subsection Specialized Lambda Lists
  2388. A @i{specialized lambda list}
  2389. @IGindex{specialized lambda list}
  2390. is used to @i{specialize} a @i{method}
  2391. for a particular @i{signature} and to describe how @i{arguments} matching
  2392. that @i{signature} are received by the @i{method}.
  2393. The @i{defined names} in Figure 3--15 use @i{specialized lambda lists}
  2394. in some way; see the dictionary entry for each for information about how.
  2395. @group
  2396. @noindent
  2397. @w{ defmethod defgeneric }
  2398. @noindent
  2399. @w{ Figure 3--15: Standardized Operators that use Specialized Lambda Lists}
  2400. @end group
  2401. A @i{specialized lambda list} can contain the @i{lambda list keywords} shown
  2402. in Figure 3--16.
  2403. @group
  2404. @noindent
  2405. @w{ @b{&allow-other-keys} @b{&key} @b{&rest} }
  2406. @w{ @b{&aux} @b{&optional} }
  2407. @noindent
  2408. @w{ Figure 3--16: Lambda List Keywords used by Specialized Lambda Lists}
  2409. @end group
  2410. A @i{specialized lambda list} is syntactically the same as an @i{ordinary lambda list}
  2411. except that each @i{required parameter} may optionally be associated with a @i{class}
  2412. or @i{object} for which that @i{parameter} is @i{specialized}.
  2413. @w{@i{lambda-list} ::=@r{(}@{@i{var} | @r{(}@i{var} @r{[}@i{specializer}@r{]}@r{)}@}{*}}
  2414. @w{ @t{[}{&optional} @{@i{var} |
  2415. @r{(}@i{var} @r{[}init-form @r{[}supplied-p-parameter@r{]}@r{]}@r{)}@}{*}@t{]}}
  2416. @w{ @t{[}{&rest} @i{var}@t{]}}
  2417. @w{ @t{[}{&key} @{@i{var} |
  2418. @r{(}@{@i{var} |
  2419. @r{(}@i{keyword-name} @i{var}@r{)}@}
  2420. @r{[}init-form @r{[}supplied-p-parameter@r{]}@r{]}@r{)}@}{*} @r{[}@t{&allow-other-keys}@r{]}@t{]}}
  2421. @w{ @t{[}{&aux} @{@i{var} | @r{(}@i{var} @r{[}@i{init-form}@r{]}@r{)}@}{*}@t{]}@r{)}}
  2422. @w{ }
  2423. @node Macro Lambda Lists, Destructuring Lambda Lists, Specialized Lambda Lists, Lambda Lists
  2424. @subsection Macro Lambda Lists
  2425. A @i{macro lambda list}
  2426. @IGindex{macro lambda list}
  2427. is used in describing @i{macros}
  2428. defined by the @i{operators} in Figure 3--17.
  2429. @group
  2430. @noindent
  2431. @w{ define-compiler-macro defmacro macrolet }
  2432. @w{ define-setf-expander }
  2433. @noindent
  2434. @w{ Figure 3--17: Operators that use Macro Lambda Lists}
  2435. @end group
  2436. With the additional restriction that
  2437. an @i{environment parameter} may appear only once
  2438. (at any of the positions indicated),
  2439. a @i{macro lambda list} has the following syntax:
  2440. {
  2441. @w{@i{reqvars} ::=@{@i{var} | !@i{pattern}@}{*}}
  2442. @w{@i{optvars} ::=@t{[}{&optional} @{@i{var} |
  2443. @r{(}{@{@i{var} | !@i{pattern}@}} @r{[}init-form @r{[}supplied-p-parameter@r{]}@r{]}@r{)}@}{*}@t{]}}
  2444. @w{@i{restvar} ::=@t{[}@{{@t{&rest}} | {&body}@} @i{@{@i{var} | !@i{pattern}@}}@t{]}}
  2445. @w{@i{keyvars} ::=@r{[}{&key} @{@i{var} |
  2446. @r{(}@{@i{var} |
  2447. @r{(}@i{keyword-name} {@{@i{var} | !@i{pattern}@}}@r{)}@}
  2448. @r{[}init-form @r{[}supplied-p-parameter@r{]}@r{]}@r{)}@}{*}}
  2449. @w{ @r{[}@t{&allow-other-keys}@r{]}@r{]}}
  2450. {
  2451. @w{@i{auxvars} ::=@t{[}{&aux} @{@i{var} | @r{(}{@i{var}} @r{[}@i{init-form}@r{]}@r{)}@}{*}@t{]}}
  2452. }
  2453. @w{@i{envvar} ::=@t{[}{&environment} @i{var}@t{]}}
  2454. @w{@i{wholevar} ::=@t{[}{&whole} @i{var}@t{]}}
  2455. @w{@i{lambda-list} ::=@r{(}!@i{wholevar} !@i{envvar} !@i{reqvars} !@i{envvar} !@i{optvars} !@i{envvar}}
  2456. @w{ !@i{restvar} !@i{envvar} !@i{keyvars} !@i{envvar} !@i{auxvars} !@i{envvar}@r{)} |}
  2457. @w{ @r{(}!@i{wholevar} !@i{envvar} !@i{reqvars} !@i{envvar} !@i{optvars} !@i{envvar} @t{.} @i{var}@r{)}}
  2458. @w{@i{pattern} ::=@r{(}!@i{wholevar} !@i{reqvars} !@i{optvars} !@i{restvar} !@i{keyvars} !@i{auxvars}@r{)} |}
  2459. @w{ @r{(}!@i{wholevar} !@i{reqvars} !@i{optvars} @t{.} @i{var}@r{)}}
  2460. }
  2461. A @i{macro lambda list} can contain
  2462. the @i{lambda list keywords} shown in Figure 3--18.
  2463. @group
  2464. @noindent
  2465. @w{ @b{&allow-other-keys} @b{&environment} @b{&rest} }
  2466. @w{ @b{&aux} @b{&key} @b{&whole} }
  2467. @w{ @b{&body} @b{&optional} }
  2468. @noindent
  2469. @w{ Figure 3--18: Lambda List Keywords used by Macro Lambda Lists}
  2470. @end group
  2471. @i{Optional parameters} (introduced by @b{&optional}) and
  2472. @i{keyword parameters} (introduced by @b{&key})
  2473. can be supplied in a @i{macro lambda list},
  2474. just as in an @i{ordinary lambda list}.
  2475. Both may contain default initialization forms and @i{supplied-p parameters}.
  2476. @b{&body}
  2477. @IRindex{&body}
  2478. is identical in function to @b{&rest},
  2479. but it can be used to inform certain output-formatting
  2480. and editing functions that the remainder of the @i{form} is
  2481. treated as a body, and should be indented accordingly.
  2482. Only one of @b{&body} or @b{&rest} can be used at any particular level;
  2483. see @ref{Destructuring by Lambda Lists}.
  2484. @b{&body} can appear at any level of a
  2485. @i{macro lambda list};
  2486. for details, see @ref{Destructuring by Lambda Lists}.
  2487. @b{&whole}
  2488. @IRindex{&whole}
  2489. is followed by a single variable that is bound to the
  2490. entire macro-call form; this is the value that the @i{macro function}
  2491. receives as its first argument.
  2492. If @b{&whole} and a following variable appear,
  2493. they must appear first in @i{lambda-list},
  2494. before any other parameter or @i{lambda list keyword}.
  2495. @b{&whole} can appear at any level of a @i{macro lambda list}.
  2496. At inner levels, the @b{&whole} variable is bound to
  2497. the corresponding part of the argument,
  2498. as with @b{&rest}, but unlike @b{&rest}, other arguments are also allowed.
  2499. The use of @b{&whole} does not affect the pattern of arguments
  2500. specified.
  2501. @b{&environment}
  2502. @IRindex{&environment}
  2503. is followed by a single variable that is bound
  2504. to an @i{environment} representing the @i{lexical environment} in which the
  2505. macro call is to be interpreted.
  2506. This @i{environment}
  2507. should be used with
  2508. @b{macro-function},
  2509. @b{get-setf-expansion},
  2510. @b{compiler-macro-function},
  2511. and
  2512. @b{macroexpand}
  2513. (for example) in computing the expansion of the macro, to ensure that any
  2514. @i{lexical bindings} or definitions established in the
  2515. @i{compilation environment} are taken into account.
  2516. @b{&environment} can only appear at the top level of a
  2517. @i{macro lambda list}, and can only
  2518. appear once, but can appear anywhere in that list;
  2519. the @b{&environment} @i{parameter} is @i{bound} along with @b{&whole}
  2520. before any other @i{variables} in the @i{lambda list}, regardless of where
  2521. @b{&environment} appears in the @i{lambda list}.
  2522. The @i{object} that is bound to the
  2523. @i{environment parameter} has @i{dynamic extent}.
  2524. Destructuring allows a @i{macro lambda list} to express
  2525. the structure of a macro call syntax.
  2526. If no @i{lambda list keywords} appear,
  2527. then the @i{macro lambda list} is a @i{tree}
  2528. containing parameter names at the leaves.
  2529. The pattern and the @i{macro form} must have compatible @i{tree structure};
  2530. that is, their @i{tree structure} must be equivalent,
  2531. or it must differ only in that some @i{leaves} of the pattern
  2532. match @i{non-atomic} @i{objects} of the @i{macro form}.
  2533. For information about error detection in this @i{situation},
  2534. see @ref{Destructuring Mismatch}.
  2535. A destructuring @i{lambda list}
  2536. (whether at top level or embedded)
  2537. can
  2538. be dotted, ending
  2539. in a parameter name. This situation is treated exactly as if the
  2540. parameter name that ends the @i{list} had appeared preceded by @b{&rest}.
  2541. It is permissible for a @i{macro} @i{form} (or a @i{subexpression} of a
  2542. @i{macro} @i{form})
  2543. to be a @i{dotted list}
  2544. only when @t{(... &rest var)} or @t{(... . var)} is used to match
  2545. it. It is the responsibility of the @i{macro} to recognize and deal
  2546. with such situations.
  2547. [Editorial Note by KMP: Apparently the dotted-macro-forms cleanup doesn't allow for
  2548. the macro to `manually' notice dotted forms and fix them as well.
  2549. It shouldn't be required that this be done only by &REST or
  2550. a dotted pattern; it should only matter that ultimately the
  2551. non-macro result of a full-macro expansion not contain dots.
  2552. Anyway, I plan to address this editorially unless someone
  2553. raises an objection.]
  2554. @menu
  2555. * Destructuring by Lambda Lists::
  2556. * Data-directed Destructuring by Lambda Lists::
  2557. * Examples of Data-directed Destructuring by Lambda Lists::
  2558. * Lambda-list-directed Destructuring by Lambda Lists::
  2559. @end menu
  2560. @node Destructuring by Lambda Lists, Data-directed Destructuring by Lambda Lists, Macro Lambda Lists, Macro Lambda Lists
  2561. @subsubsection Destructuring by Lambda Lists
  2562. Anywhere in a @i{macro lambda list} where a parameter
  2563. name can appear, and where @i{ordinary lambda list} syntax
  2564. (as described in @ref{Ordinary Lambda Lists}) does not
  2565. otherwise allow a @i{list}, a @i{destructuring lambda list}
  2566. can appear in place
  2567. of the parameter name. When this is done, then the argument
  2568. that would match the parameter is treated as a (possibly dotted) @i{list},
  2569. to be used as an argument list for satisfying the
  2570. parameters in the embedded @i{lambda list}.
  2571. This is known as destructuring.
  2572. Destructuring is the process of decomposing a compound @i{object} into
  2573. its component parts, using an abbreviated, declarative syntax, rather
  2574. than writing it out by hand using the primitive component-accessing
  2575. functions. Each component part is bound to a variable.
  2576. A destructuring operation requires an @i{object} to be decomposed,
  2577. a pattern that specifies what components are to be extracted, and the names
  2578. of the variables whose values are to be the components.
  2579. @node Data-directed Destructuring by Lambda Lists, Examples of Data-directed Destructuring by Lambda Lists, Destructuring by Lambda Lists, Macro Lambda Lists
  2580. @subsubsection Data-directed Destructuring by Lambda Lists
  2581. In data-directed destructuring,
  2582. the pattern is a sample @i{object} of the @i{type} to be decomposed.
  2583. Wherever a component is to be extracted,
  2584. a @i{symbol} appears in the pattern;
  2585. this @i{symbol} is the name of the variable whose value will be that component.
  2586. @node Examples of Data-directed Destructuring by Lambda Lists, Lambda-list-directed Destructuring by Lambda Lists, Data-directed Destructuring by Lambda Lists, Macro Lambda Lists
  2587. @subsubsection Examples of Data-directed Destructuring by Lambda Lists
  2588. An example pattern is
  2589. @t{(a b c)}
  2590. which destructures a list of three elements. The variable @t{a} is assigned
  2591. to the first element, @t{b} to the second, etc. A more complex example
  2592. is
  2593. @t{((first . rest) . more)}
  2594. The important features of data-directed destructuring are its syntactic
  2595. simplicity and the ability to extend it to lambda-list-directed destructuring.
  2596. @node Lambda-list-directed Destructuring by Lambda Lists, , Examples of Data-directed Destructuring by Lambda Lists, Macro Lambda Lists
  2597. @subsubsection Lambda-list-directed Destructuring by Lambda Lists
  2598. An extension of data-directed destructuring of @i{trees} is
  2599. lambda-list-directed destructuring. This derives from the analogy
  2600. between the three-element destructuring pattern
  2601. @t{(first second third)}
  2602. and the three-argument @i{lambda list}
  2603. @t{(first second third)}
  2604. Lambda-list-directed destructuring is identical to data-directed destructuring
  2605. if no @i{lambda list keywords} appear in the pattern.
  2606. Any list in the pattern (whether a sub-list or the whole pattern itself)
  2607. that contains a @i{lambda list keyword} is interpreted specially.
  2608. Elements of the list to the left of the first
  2609. @i{lambda list keyword} are treated as destructuring patterns, as usual, but the
  2610. remaining elements of the list are treated like a function's
  2611. @i{lambda list}
  2612. except that where a variable would normally be required, an arbitrary
  2613. destructuring pattern is allowed. Note that in case of ambiguity,
  2614. @i{lambda list} syntax is preferred over destructuring syntax. Thus, after
  2615. @b{&optional} a list of elements is a list of a destructuring pattern
  2616. and a default value form.
  2617. The detailed behavior of each @i{lambda list keyword} in a
  2618. lambda-list-directed destructuring
  2619. pattern is as follows:
  2620. @table @asis
  2621. @item @b{&optional}
  2622. Each following element is a variable or a list of a destructuring
  2623. pattern, a default value form, and a supplied-p variable. The default value and
  2624. the supplied-p variable can be omitted.
  2625. If the list being destructured ends
  2626. early, so that it does not have an element to match against this destructuring
  2627. (sub)-pattern, the default form is evaluated and destructured instead. The
  2628. supplied-p variable receives the value
  2629. @b{nil} if the default form is used, @b{t} otherwise.
  2630. @item @b{&rest}, @b{&body}
  2631. The next element is a destructuring pattern that matches the
  2632. rest of the list. @b{&body} is identical to @b{&rest} but declares that what
  2633. is being matched is a list of forms that constitutes the body of @i{form}.
  2634. This next element must be the last unless a @i{lambda list keyword} follows it.
  2635. @item @b{&aux}
  2636. The remaining elements are not destructuring patterns at all, but are
  2637. auxiliary variable bindings.
  2638. @item @b{&whole}
  2639. The next element is a destructuring pattern that matches the entire
  2640. form in a macro, or the entire @i{subexpression} at inner levels.
  2641. @item @b{&key}
  2642. Each following element is one of
  2643. @table @asis
  2644. @item @t{}
  2645. a @i{variable},
  2646. @item or
  2647. a list of a variable,
  2648. an optional initialization form,
  2649. and an optional supplied-p variable.
  2650. @item or
  2651. a list of a list of a keyword and a destructuring pattern,
  2652. an optional initialization form,
  2653. and an optional supplied-p variable.
  2654. @end table
  2655. The rest of the list being destructured
  2656. is taken to be alternating keywords and values and is taken apart appropriately.
  2657. @item @b{&allow-other-keys}
  2658. Stands by itself.
  2659. @end table
  2660. @node Destructuring Lambda Lists, Boa Lambda Lists, Macro Lambda Lists, Lambda Lists
  2661. @subsection Destructuring Lambda Lists
  2662. A @i{destructuring lambda list}
  2663. @IGindex{destructuring lambda list}
  2664. is used by @b{destructuring-bind}.
  2665. @i{Destructuring lambda lists} are closely related to
  2666. @i{macro lambda lists}; see @ref{Macro Lambda Lists}.
  2667. A @i{destructuring lambda list} can contain all of the
  2668. @i{lambda list keywords} listed for @i{macro lambda lists}
  2669. except for @b{&environment}, and supports destructuring in the
  2670. same way. Inner @i{lambda lists} nested within a @i{macro lambda list}
  2671. have the syntax of @i{destructuring lambda lists}.
  2672. A @i{destructuring lambda list} has the following syntax:
  2673. {
  2674. @w{@i{reqvars} ::=@{@i{var} | !@i{lambda-list}@}{*}}
  2675. @w{@i{optvars} ::=@t{[}{&optional} @{@i{var} |
  2676. @r{(}{@{@i{var} | !@i{lambda-list}@}} @r{[}init-form @r{[}supplied-p-parameter@r{]}@r{]}@r{)}@}{*}@t{]}}
  2677. @w{@i{restvar} ::=@t{[}@{{@t{&rest}} | {&body}@} @i{@{@i{var} | !@i{lambda-list}@}}@t{]}}
  2678. @w{@i{keyvars} ::=@r{[}{&key} @{@i{var} |
  2679. @r{(}@{@i{var} |
  2680. @r{(}@i{keyword-name} {@{@i{var} | !@i{lambda-list}@}}@r{)}@}
  2681. @r{[}init-form @r{[}supplied-p-parameter@r{]}@r{]}@r{)}@}{*}}
  2682. @w{ @r{[}@t{&allow-other-keys}@r{]}@r{]}}
  2683. {
  2684. @w{@i{auxvars} ::=@t{[}{&aux} @{@i{var} | @r{(}{@i{var}} @r{[}@i{init-form}@r{]}@r{)}@}{*}@t{]}}
  2685. }
  2686. @w{@i{envvar} ::=@t{[}{&environment} @i{var}@t{]}}
  2687. @w{@i{wholevar} ::=@t{[}{&whole} @i{var}@t{]}}
  2688. @w{@i{lambda-list} ::=@r{(}!@i{wholevar} !@i{reqvars} !@i{optvars} !@i{restvar} !@i{keyvars} !@i{auxvars}@r{)} |}
  2689. @w{ @r{(}!@i{wholevar} !@i{reqvars} !@i{optvars} @t{.} @i{var}@r{)}}
  2690. }
  2691. @node Boa Lambda Lists, Defsetf Lambda Lists, Destructuring Lambda Lists, Lambda Lists
  2692. @subsection Boa Lambda Lists
  2693. A @i{boa lambda list}
  2694. @IGindex{boa lambda list}
  2695. is a @i{lambda list} that is syntactically
  2696. like an @i{ordinary lambda list}, but that is processed in
  2697. ``@b{b}y @b{o}rder of @b{a}rgument'' style.
  2698. A @i{boa lambda list} is used only in a @b{defstruct} @i{form},
  2699. when explicitly specifying the @i{lambda list}
  2700. of a constructor @i{function} (sometimes called a ``boa constructor'').
  2701. The @b{&optional}, @b{&rest}, @b{&aux},
  2702. @b{&key}, and @b{&allow-other-keys}
  2703. @i{lambda list keywords} are recognized in a @i{boa lambda list}.
  2704. The way these @i{lambda list keywords} differ from their
  2705. use in an @i{ordinary lambda list} follows.
  2706. Consider this example, which describes how @b{destruct} processes
  2707. its @t{:constructor} option.
  2708. @example
  2709. (:constructor create-foo
  2710. (a &optional b (c 'sea) &rest d &aux e (f 'eff)))
  2711. @end example
  2712. This defines @t{create-foo} to be a constructor of one or more arguments.
  2713. The first argument is used to initialize the @t{a} slot. The second
  2714. argument is used to initialize the @t{b} slot. If there isn't any
  2715. second argument, then the default value given in the body of the
  2716. @b{defstruct} (if given) is used instead.
  2717. The third argument is used to
  2718. initialize the @t{c} slot. If there isn't any third argument, then the
  2719. symbol @t{sea} is used instead. Any arguments following the third
  2720. argument are collected into a @i{list}
  2721. and used to initialize the @t{d}
  2722. slot. If there are three or fewer arguments, then @b{nil} is placed in
  2723. the @t{d} slot. The @t{e} slot is not initialized;
  2724. its initial value is @i{implementation-defined}.
  2725. Finally, the @t{f} slot is initialized to contain the symbol @t{eff}.
  2726. @b{&key} and @b{&allow-other-keys} arguments default
  2727. in a manner similar to that of @b{&optional} arguments: if no default
  2728. is supplied in the @i{lambda list} then the default value
  2729. given in the body of the @b{defstruct} (if given) is used instead.
  2730. For example:
  2731. @example
  2732. (defstruct (foo (:constructor CREATE-FOO (a &optional b (c 'sea)
  2733. &key (d 2)
  2734. &aux e (f 'eff))))
  2735. (a 1) (b 2) (c 3) (d 4) (e 5) (f 6))
  2736. (create-foo 10) @result{} #S(FOO A 10 B 2 C SEA D 2 E @i{implemention-dependent} F EFF)
  2737. (create-foo 10 'bee 'see :d 'dee)
  2738. @result{} #S(FOO A 10 B BEE C SEE D DEE E @i{implemention-dependent} F EFF)
  2739. @end example
  2740. If keyword arguments of the form
  2741. @t{((@i{key} @i{var}) @r{[}@i{default} @r{[}@i{svar}@r{]}@r{]})}
  2742. are specified, the @i{slot} @i{name} is matched with @i{var}
  2743. (not @i{key}).
  2744. The actions taken in the @t{b} and @t{e} cases were carefully
  2745. chosen to allow the user to specify all possible behaviors.
  2746. The @b{&aux} variables can be used to completely override the default
  2747. initializations given in the body.
  2748. If no default value is supplied for an @i{aux variable} variable,
  2749. the consequences are undefined if an attempt is later made to read
  2750. the corresponding @i{slot}'s value before a value is explicitly assigned.
  2751. If such a @i{slot} has a @t{:type} option specified,
  2752. this suppressed initialization does not imply a type mismatch situation;
  2753. the declared type is only required to apply when the @i{slot} is finally assigned.
  2754. With this definition, the following can be written:
  2755. @example
  2756. (create-foo 1 2)
  2757. @end example
  2758. instead of
  2759. @example
  2760. (make-foo :a 1 :b 2)
  2761. @end example
  2762. and @t{create-foo} provides defaulting different
  2763. from that of @t{make-foo}.
  2764. Additional arguments that do not correspond to slot names but
  2765. are merely present to supply values used in subsequent initialization
  2766. computations are allowed.
  2767. For example, in the definition
  2768. @example
  2769. (defstruct (frob (:constructor create-frob
  2770. (a &key (b 3 have-b) (c-token 'c)
  2771. (c (list c-token (if have-b 7 2))))))
  2772. a b c)
  2773. @end example
  2774. the @t{c-token} argument is used merely to supply a value used in the
  2775. initialization of the @t{c} slot. The @i{supplied-p parameters}
  2776. associated with @i{optional parameters} and @i{keyword parameters}
  2777. might also be used this way.
  2778. @node Defsetf Lambda Lists, Deftype Lambda Lists, Boa Lambda Lists, Lambda Lists
  2779. @subsection Defsetf Lambda Lists
  2780. A @i{defsetf lambda list}
  2781. @IGindex{defsetf lambda list}
  2782. is used by @b{defsetf}.
  2783. A @i{defsetf lambda list} has the following syntax:
  2784. @w{@i{lambda-list} ::=@r{(}@{@i{var}@}{*}}
  2785. @w{ @t{[}{&optional} @{@i{var} |
  2786. @r{(}@i{var} @r{[}init-form @r{[}supplied-p-parameter@r{]}@r{]}@r{)}@}{*}@t{]}}
  2787. @w{ @t{[}{&rest} @i{var}@t{]}}
  2788. @w{ @t{[}{&key} @{@i{var} |
  2789. @r{(}@{@i{var} |
  2790. @r{(}@i{keyword-name} @i{var}@r{)}@}
  2791. @r{[}init-form @r{[}supplied-p-parameter@r{]}@r{]}@r{)}@}{*} pt @r{[}@t{&allow-other-keys}@r{]}@t{]}}
  2792. @w{ @t{[}{&environment} @i{var}@t{]}}
  2793. A @i{defsetf lambda list} can contain the @i{lambda list keywords} shown
  2794. in Figure 3--19.
  2795. @group
  2796. @noindent
  2797. @w{ @b{&allow-other-keys} @b{&key} @b{&rest} }
  2798. @w{ @b{&environment} @b{&optional} }
  2799. @noindent
  2800. @w{ Figure 3--19: Lambda List Keywords used by Defsetf Lambda Lists}
  2801. @end group
  2802. A @i{defsetf lambda list} differs from an @i{ordinary lambda list}
  2803. only in that it does not permit the use of @b{&aux},
  2804. and that it permits use of @b{&environment},
  2805. which introduces an @i{environment parameter}.
  2806. @node Deftype Lambda Lists, Define-modify-macro Lambda Lists, Defsetf Lambda Lists, Lambda Lists
  2807. @subsection Deftype Lambda Lists
  2808. A @i{deftype lambda list}
  2809. @IGindex{deftype lambda list}
  2810. is used by @b{deftype}.
  2811. A @i{deftype lambda list} has the same syntax as a @i{macro lambda list},
  2812. and can therefore contain the @i{lambda list keywords} as a @i{macro lambda list}.
  2813. A @i{deftype lambda list} differs from a @i{macro lambda list}
  2814. only in that if no @i{init-form} is supplied for an @i{optional parameter}
  2815. or @i{keyword parameter} in the @i{lambda-list}, the default @i{value}
  2816. for that @i{parameter} is the @i{symbol} @b{*} (rather than @b{nil}).
  2817. @node Define-modify-macro Lambda Lists, Define-method-combination Arguments Lambda Lists, Deftype Lambda Lists, Lambda Lists
  2818. @subsection Define-modify-macro Lambda Lists
  2819. A @i{define-modify-macro lambda list}
  2820. @IGindex{define-modify-macro lambda list}
  2821. is used by
  2822. @b{define-modify-macro}.
  2823. A @i{define-modify-macro lambda list} can contain the
  2824. @i{lambda list keywords} shown in Figure 3--20.
  2825. @group
  2826. @noindent
  2827. @w{ @b{&optional} @b{&rest} }
  2828. @noindent
  2829. @w{ Figure 3--20: Lambda List Keywords used by Define-modify-macro Lambda Lists}
  2830. @end group
  2831. @i{Define-modify-macro lambda lists} are similar to
  2832. @i{ordinary lambda lists}, but do not support keyword arguments.
  2833. @b{define-modify-macro} has no need match keyword arguments, and
  2834. a @i{rest parameter} is sufficient. @i{Aux variables} are also
  2835. not supported, since @b{define-modify-macro} has no body @i{forms}
  2836. which could refer to such @i{bindings}. See the @i{macro} @b{define-modify-macro}.
  2837. @node Define-method-combination Arguments Lambda Lists, Syntactic Interaction of Documentation Strings and Declarations, Define-modify-macro Lambda Lists, Lambda Lists
  2838. @subsection Define-method-combination Arguments Lambda Lists
  2839. A @i{define-method-combination arguments lambda list}
  2840. @IGindex{define-method-combination arguments lambda list}
  2841. is used by
  2842. the @t{:arguments} option to @b{define-method-combination}.
  2843. A @i{define-method-combination arguments lambda list} can contain the
  2844. @i{lambda list keywords} shown in Figure 3--21.
  2845. @group
  2846. @noindent
  2847. @w{ @b{&allow-other-keys} @b{&key} @b{&rest} }
  2848. @w{ @b{&aux} @b{&optional} @b{&whole} }
  2849. @noindent
  2850. @w{ Figure 3--21: Lambda List Keywords used by Define-method-combination arguments Lambda Lists}
  2851. @end group
  2852. @i{Define-method-combination arguments lambda lists} are similar to
  2853. @i{ordinary lambda lists}, but also permit the use of @b{&whole}.
  2854. @node Syntactic Interaction of Documentation Strings and Declarations, , Define-method-combination Arguments Lambda Lists, Lambda Lists
  2855. @subsection Syntactic Interaction of Documentation Strings and Declarations
  2856. In a number of situations, a @i{documentation string} can appear amidst a
  2857. series of @b{declare} @i{expressions} prior to a series of @i{forms}.
  2858. In that case, if a @i{string} S appears where a @i{documentation string} is
  2859. permissible and is not followed by
  2860. either a @b{declare} @i{expression}
  2861. or a @i{form}
  2862. then S is taken to be a @i{form};
  2863. otherwise, S is taken as a @i{documentation string}.
  2864. The consequences are unspecified if more than one such @i{documentation string}
  2865. is present.
  2866. @c end of including concept-bvl
  2867. @node Error Checking in Function Calls, Traversal Rules and Side Effects, Lambda Lists, Evaluation and Compilation
  2868. @section Error Checking in Function Calls
  2869. @c including concept-args
  2870. @menu
  2871. * Argument Mismatch Detection::
  2872. @end menu
  2873. @node Argument Mismatch Detection, , Error Checking in Function Calls, Error Checking in Function Calls
  2874. @subsection Argument Mismatch Detection
  2875. @menu
  2876. * Safe and Unsafe Calls::
  2877. * Error Detection Time in Safe Calls::
  2878. * Too Few Arguments::
  2879. * Too Many Arguments::
  2880. * Unrecognized Keyword Arguments::
  2881. * Invalid Keyword Arguments::
  2882. * Odd Number of Keyword Arguments::
  2883. * Destructuring Mismatch::
  2884. * Errors When Calling a Next Method::
  2885. @end menu
  2886. @node Safe and Unsafe Calls, Error Detection Time in Safe Calls, Argument Mismatch Detection, Argument Mismatch Detection
  2887. @subsubsection Safe and Unsafe Calls
  2888. A @i{call} is a @i{safe call}
  2889. @IGindex{safe call}
  2890. if each of the following is
  2891. either @i{safe} @i{code} or @i{system code} (other than
  2892. @i{system code} that results from @i{macro expansion} of
  2893. @i{programmer code}):
  2894. @table @asis
  2895. @item @t{*}
  2896. the @i{call}.
  2897. @item @t{*}
  2898. the definition of the @i{function} being @i{called}.
  2899. @item @t{*}
  2900. the point of @i{functional evaluation}
  2901. @end table
  2902. The following special cases require some elaboration:
  2903. @table @asis
  2904. @item @t{*}
  2905. If the @i{function} being called is a @i{generic function},
  2906. it is considered @i{safe} if all of the following are
  2907. @i{safe code} or @i{system code}:
  2908. @table @asis
  2909. @item --
  2910. its definition (if it was defined explicitly).
  2911. @item --
  2912. the @i{method} definitions for all @i{applicable} @i{methods}.
  2913. @item --
  2914. the definition of its @i{method combination}.
  2915. @end table
  2916. @item @t{*}
  2917. For the form @t{(coerce @i{x} 'function)},
  2918. where @i{x} is a @i{lambda expression},
  2919. the value of the @i{optimize quality} @b{safety}
  2920. in the global environment at the time the @b{coerce}
  2921. is @i{executed} applies to the resulting @i{function}.
  2922. @item @t{*}
  2923. For a call to the @i{function} @b{ensure-generic-function}, the value of the
  2924. @i{optimize quality} @b{safety} in the @i{environment}
  2925. @i{object} passed as the @t{:environment} @i{argument} applies
  2926. to the resulting @i{generic function}.
  2927. @item @t{*}
  2928. For a call to @b{compile} with a @i{lambda expression} as the
  2929. @i{argument}, the value of the @i{optimize quality} @b{safety}
  2930. in the @i{global environment} at the time @b{compile} is @i{called}
  2931. applies to the resulting @i{compiled function}.
  2932. @item @t{*}
  2933. For a call to @b{compile} with only one argument, if the original definition
  2934. of the @i{function} was @i{safe}, then the resulting @i{compiled function}
  2935. must also be @i{safe}.
  2936. @item @t{*}
  2937. A @i{call} to a @i{method} by @b{call-next-method} must be
  2938. considered @i{safe} if each of the following is
  2939. @i{safe code} or @i{system code}:
  2940. @table @asis
  2941. @item --
  2942. the definition of the @i{generic function} (if it was defined explicitly).
  2943. @item --
  2944. the @i{method} definitions for all @i{applicable} @i{methods}.
  2945. @item --
  2946. the definition of the @i{method combination}.
  2947. @item --
  2948. the point of entry into the body of the @i{method defining form},
  2949. where the @i{binding} of @b{call-next-method} is established.
  2950. @item --
  2951. the point of @i{functional evaluation} of the name @b{call-next-method}.
  2952. @end table
  2953. @end table
  2954. An @i{unsafe call}
  2955. @IGindex{unsafe call}
  2956. is a @i{call} that is not a @i{safe call}.
  2957. The informal intent is that the @i{programmer} can rely on a @i{call}
  2958. to be @i{safe}, even when @i{system code} is involved, if all reasonable
  2959. steps have been taken to ensure that the @i{call} is @i{safe}.
  2960. For example, if a @i{programmer} calls @b{mapcar} from @i{safe}
  2961. @i{code} and supplies a @i{function} that was @i{compiled}
  2962. as @i{safe}, the @i{implementation} is required to ensure that
  2963. @b{mapcar} makes a @i{safe call} as well.
  2964. @node Error Detection Time in Safe Calls, Too Few Arguments, Safe and Unsafe Calls, Argument Mismatch Detection
  2965. @subsubsection Error Detection Time in Safe Calls
  2966. If an error is signaled in a @i{safe call},
  2967. the exact point of the @i{signal} is @i{implementation-dependent}.
  2968. In particular, it might be signaled at compile time or at run time,
  2969. and if signaled at run time,
  2970. it might be prior to, during, or after @i{executing} the @i{call}.
  2971. However, it is always prior to the execution of the body of the @i{function}
  2972. being @i{called}.
  2973. @node Too Few Arguments, Too Many Arguments, Error Detection Time in Safe Calls, Argument Mismatch Detection
  2974. @subsubsection Too Few Arguments
  2975. It is not permitted to supply too few @i{arguments} to a @i{function}.
  2976. Too few arguments means fewer @i{arguments} than the number of @i{required parameters}
  2977. for the @i{function}.
  2978. If this @i{situation} occurs in a @i{safe call},
  2979. an error of @i{type} @b{program-error} must be signaled;
  2980. and in an @i{unsafe call} the @i{situation} has undefined consequences.
  2981. @node Too Many Arguments, Unrecognized Keyword Arguments, Too Few Arguments, Argument Mismatch Detection
  2982. @subsubsection Too Many Arguments
  2983. It is not permitted to supply too many @i{arguments} to a @i{function}.
  2984. Too many arguments means more @i{arguments} than the number of @i{required parameters}
  2985. plus the number of @i{optional parameters}; however, if the @i{function}
  2986. uses @b{&rest} or @b{&key}, it is not possible for it to receive too many arguments.
  2987. If this @i{situation} occurs in a @i{safe call},
  2988. an error of @i{type} @b{program-error} must be signaled;
  2989. and in an @i{unsafe call} the @i{situation} has undefined consequences.
  2990. @node Unrecognized Keyword Arguments, Invalid Keyword Arguments, Too Many Arguments, Argument Mismatch Detection
  2991. @subsubsection Unrecognized Keyword Arguments
  2992. It is not permitted to supply a keyword argument to a @i{function}
  2993. using a name that is not recognized by that @i{function}
  2994. unless keyword argument checking is suppressed as described
  2995. in @ref{Suppressing Keyword Argument Checking}.
  2996. If this @i{situation} occurs in a @i{safe call},
  2997. an error of @i{type} @b{program-error} must be signaled;
  2998. and in an @i{unsafe call} the @i{situation} has undefined consequences.
  2999. @node Invalid Keyword Arguments, Odd Number of Keyword Arguments, Unrecognized Keyword Arguments, Argument Mismatch Detection
  3000. @subsubsection Invalid Keyword Arguments
  3001. It is not permitted to supply a keyword argument to a @i{function}
  3002. using a name that is not a @i{symbol}.
  3003. If this @i{situation} occurs in a @i{safe call},
  3004. an error of @i{type} @b{program-error} must be signaled
  3005. unless keyword argument checking is suppressed as described
  3006. in @ref{Suppressing Keyword Argument Checking};
  3007. and in an @i{unsafe call} the @i{situation} has undefined consequences.
  3008. @node Odd Number of Keyword Arguments, Destructuring Mismatch, Invalid Keyword Arguments, Argument Mismatch Detection
  3009. @subsubsection Odd Number of Keyword Arguments
  3010. An odd number of @i{arguments} must not be supplied for the @i{keyword parameters}.
  3011. If this @i{situation} occurs in a @i{safe call},
  3012. an error of @i{type} @b{program-error} must be signaled
  3013. unless keyword argument checking is suppressed as described
  3014. in @ref{Suppressing Keyword Argument Checking};
  3015. and in an @i{unsafe call} the @i{situation} has undefined consequences.
  3016. @node Destructuring Mismatch, Errors When Calling a Next Method, Odd Number of Keyword Arguments, Argument Mismatch Detection
  3017. @subsubsection Destructuring Mismatch
  3018. When matching a @i{destructuring lambda list} against a @i{form},
  3019. the pattern and the @i{form} must have compatible @i{tree structure},
  3020. as described in @ref{Macro Lambda Lists}.
  3021. Otherwise, in a @i{safe call},
  3022. an error of @i{type} @b{program-error} must be signaled;
  3023. and in an @i{unsafe call} the @i{situation} has undefined consequences.
  3024. @node Errors When Calling a Next Method, , Destructuring Mismatch, Argument Mismatch Detection
  3025. @subsubsection Errors When Calling a Next Method
  3026. If @b{call-next-method} is called with @i{arguments}, the ordered
  3027. set of @i{applicable} @i{methods} for the changed set of @i{arguments}
  3028. for @b{call-next-method} must be the same as the ordered set of
  3029. @i{applicable} @i{methods} for the original @i{arguments} to the
  3030. @i{generic function}, or else an error should be signaled.
  3031. The comparison between the set of methods applicable to the
  3032. new arguments and the set applicable to the original arguments is
  3033. insensitive to order differences among methods with the same
  3034. specializers.
  3035. If @b{call-next-method} is called with @i{arguments} that specify
  3036. a different ordered set of @i{applicable} methods and there is no
  3037. @i{next method} available, the test for different methods and the
  3038. associated error signaling (when present) takes precedence over calling
  3039. @b{no-next-method}.
  3040. @c end of including concept-args
  3041. @node Traversal Rules and Side Effects, Destructive Operations, Error Checking in Function Calls, Evaluation and Compilation
  3042. @section Traversal Rules and Side Effects
  3043. @c including concept-traversal
  3044. The consequences are undefined
  3045. when @i{code} executed during an @i{object-traversing} operation
  3046. destructively modifies the @i{object} in a way that might affect the
  3047. ongoing traversal operation.
  3048. In particular, the following rules apply.
  3049. @table @asis
  3050. @item @b{List traversal}
  3051. For @i{list} traversal operations, the @i{cdr} chain of the
  3052. @i{list} is not allowed to be destructively modified.
  3053. @item @b{Array traversal}
  3054. For @i{array} traversal operations, the @i{array} is not allowed
  3055. to be adjusted and its @i{fill pointer}, if any, is not allowed to
  3056. be changed.
  3057. @item @b{Hash-table traversal}
  3058. For @i{hash table} traversal operations, new elements may not be added
  3059. or deleted except that the element corresponding to the current hash key
  3060. may be changed or removed.
  3061. @item @b{Package traversal}
  3062. For @i{package} traversal operations (@i{e.g.}, @b{do-symbols}),
  3063. new @i{symbols} may not be @i{interned} in or @i{uninterned}
  3064. from the @i{package} being traversed
  3065. or any @i{package} that it uses except that the
  3066. current @i{symbol} may be @i{uninterned} from the @i{package}
  3067. being traversed.
  3068. @end table
  3069. @c end of including concept-traversal
  3070. @node Destructive Operations, Evaluation and Compilation Dictionary, Traversal Rules and Side Effects, Evaluation and Compilation
  3071. @section Destructive Operations
  3072. @c including concept-destruction
  3073. @menu
  3074. * Modification of Literal Objects::
  3075. * Transfer of Control during a Destructive Operation::
  3076. @end menu
  3077. @node Modification of Literal Objects, Transfer of Control during a Destructive Operation, Destructive Operations, Destructive Operations
  3078. @subsection Modification of Literal Objects
  3079. The consequences are undefined if @i{literal} @i{objects}
  3080. are destructively modified. For this purpose, the following operations
  3081. are considered @i{destructive}:
  3082. @table @asis
  3083. @item @b{random-state}
  3084. Using it as an @i{argument} to the @i{function} @b{random}.
  3085. @item @b{cons}
  3086. Changing the @i{car}_1 or @i{cdr}_1 of the @i{cons},
  3087. or performing a @i{destructive} operation on an @i{object} which is either
  3088. the @i{car}_2 or the @i{cdr}_2 of the @i{cons}.
  3089. @item @b{array}
  3090. Storing a new value into some element of the @i{array},
  3091. or performing a @i{destructive} operation
  3092. on an @i{object} that is already such an @i{element}.
  3093. Changing the @i{fill pointer}, @i{dimensions}, or displacement of
  3094. the @i{array} (regardless of whether the @i{array} is @i{actually adjustable}).
  3095. Performing a @i{destructive} operation on another @i{array}
  3096. that is displaced to the @i{array} or that otherwise shares its contents
  3097. with the @i{array}.
  3098. @item @b{hash-table}
  3099. Performing a @i{destructive} operation on any @i{key}.
  3100. Storing a new @i{value}_4 for any @i{key},
  3101. or performing a @i{destructive} operation
  3102. on any @i{object} that is such a @i{value}.
  3103. Adding or removing entries from the @i{hash table}.
  3104. @item @b{structure-object}
  3105. Storing a new value into any slot,
  3106. or performing a @i{destructive} operation on an @i{object}
  3107. that is the value of some slot.
  3108. @item @b{standard-object}
  3109. Storing a new value into any slot,
  3110. or performing a @i{destructive} operation on an @i{object}
  3111. that is the value of some slot.
  3112. Changing the class of the @i{object} (@i{e.g.}, using the @i{function} @b{change-class}).
  3113. @item @b{readtable}
  3114. Altering the @i{readtable case}.
  3115. Altering the syntax type of any character in this readtable.
  3116. Altering the @i{reader macro function} associated with any @i{character}
  3117. in the @i{readtable}, or altering the @i{reader macro functions}
  3118. associated with @i{characters} defined as @i{dispatching macro characters}
  3119. in the @i{readtable}.
  3120. @item @b{stream}
  3121. Performing I/O operations on the @i{stream},
  3122. or @i{closing} the @i{stream}.
  3123. @item All other standardized types
  3124. [This category includes, for example, @b{character},
  3125. @b{condition},
  3126. @b{function},
  3127. @b{method-combination},
  3128. @b{method},
  3129. @b{number},
  3130. @b{package},
  3131. @b{pathname},
  3132. @b{restart},
  3133. and @b{symbol}.]
  3134. There are no @i{standardized} @i{destructive} operations
  3135. defined on @i{objects} of these @i{types}.
  3136. @end table
  3137. @node Transfer of Control during a Destructive Operation, , Modification of Literal Objects, Destructive Operations
  3138. @subsection Transfer of Control during a Destructive Operation
  3139. Should a transfer of control out of a @i{destructive} operation occur
  3140. (@i{e.g.}, due to an error) the state of the @i{object} being modified is
  3141. @i{implementation-dependent}.
  3142. @menu
  3143. * Examples of Transfer of Control during a Destructive Operation::
  3144. @end menu
  3145. @node Examples of Transfer of Control during a Destructive Operation, , Transfer of Control during a Destructive Operation, Transfer of Control during a Destructive Operation
  3146. @subsubsection Examples of Transfer of Control during a Destructive Operation
  3147. The following examples illustrate some of the many ways in which the
  3148. @i{implementation-dependent} nature of the modification can manifest
  3149. itself.
  3150. @example
  3151. (let ((a (list 2 1 4 3 7 6 'five)))
  3152. (ignore-errors (sort a #'<))
  3153. a)
  3154. @result{} (1 2 3 4 6 7 FIVE)
  3155. @i{OR}@result{} (2 1 4 3 7 6 FIVE)
  3156. @i{OR}@result{} (2)
  3157. (prog foo ((a (list 1 2 3 4 5 6 7 8 9 10)))
  3158. (sort a #'(lambda (x y) (if (zerop (random 5)) (return-from foo a) (> x y)))))
  3159. @result{} (1 2 3 4 5 6 7 8 9 10)
  3160. @i{OR}@result{} (3 4 5 6 2 7 8 9 10 1)
  3161. @i{OR}@result{} (1 2 4 3)
  3162. @end example
  3163. @c end of including concept-destruction
  3164. @node Evaluation and Compilation Dictionary, , Destructive Operations, Evaluation and Compilation
  3165. @section Evaluation and Compilation Dictionary
  3166. @c including dict-eval-compile
  3167. @menu
  3168. * lambda (Symbol)::
  3169. * lambda::
  3170. * compile::
  3171. * eval::
  3172. * eval-when::
  3173. * load-time-value::
  3174. * quote::
  3175. * compiler-macro-function::
  3176. * define-compiler-macro::
  3177. * defmacro::
  3178. * macro-function::
  3179. * macroexpand::
  3180. * define-symbol-macro::
  3181. * symbol-macrolet::
  3182. * *macroexpand-hook*::
  3183. * proclaim::
  3184. * declaim::
  3185. * declare::
  3186. * ignore::
  3187. * dynamic-extent::
  3188. * type::
  3189. * inline::
  3190. * ftype::
  3191. * declaration::
  3192. * optimize::
  3193. * special::
  3194. * locally::
  3195. * the::
  3196. * special-operator-p::
  3197. * constantp::
  3198. @end menu
  3199. @node lambda (Symbol), lambda, Evaluation and Compilation Dictionary, Evaluation and Compilation Dictionary
  3200. @subsection lambda [Symbol]
  3201. @subsubheading Syntax::
  3202. @code{lambda} @i{lambda-list {[[@{@i{declaration}@}{*} | @i{documentation}]]} @{@i{form}@}{*}}
  3203. @subsubheading Arguments::
  3204. @i{lambda-list}---an @i{ordinary lambda list}.
  3205. @i{declaration}---a @b{declare} @i{expression}; not evaluated.
  3206. @i{documentation}---a @i{string}; not evaluated.
  3207. @i{form}---a @i{form}.
  3208. @subsubheading Description::
  3209. A @i{lambda expression} is a @i{list} that can be used in place of a
  3210. @i{function name} in certain contexts to denote a @i{function} by
  3211. directly describing its behavior rather than indirectly by referring to the
  3212. name of an @i{established} @i{function}.
  3213. @i{Documentation} is attached to the denoted @i{function} (if any
  3214. is actually created) as a @i{documentation string}.
  3215. @subsubheading See Also::
  3216. @b{function},
  3217. @ref{documentation; (setf documentation)}
  3218. ,
  3219. @ref{Lambda Expressions},
  3220. @ref{Lambda Forms},
  3221. @ref{Syntactic Interaction of Documentation Strings and Declarations}
  3222. @subsubheading Notes::
  3223. The @i{lambda form}
  3224. @example
  3225. ((lambda @i{lambda-list} . @i{body}) . @i{arguments})
  3226. @end example
  3227. is semantically equivalent to the @i{function form}
  3228. @example
  3229. (funcall #'(lambda @i{lambda-list} . @i{body}) . @i{arguments})
  3230. @end example
  3231. @node lambda, compile, lambda (Symbol), Evaluation and Compilation Dictionary
  3232. @subsection lambda [Macro]
  3233. @code{lambda} @i{lambda-list {[[@{@i{declaration}@}{*} | @i{documentation}]]} @{@i{form}@}{*}} @result{} @i{@i{function}}
  3234. @subsubheading Arguments and Values::
  3235. @i{lambda-list}---an @i{ordinary lambda list}.
  3236. @i{declaration}---a @b{declare} @i{expression}; not evaluated.
  3237. @i{documentation}---a @i{string}; not evaluated.
  3238. @i{form}---a @i{form}.
  3239. @i{function}---a @i{function}.
  3240. @subsubheading Description::
  3241. Provides a shorthand notation for a @b{function} @i{special form}
  3242. involving a @i{lambda expression} such that:
  3243. @example
  3244. (lambda @i{lambda-list} {[[@{@i{declaration}@}{*} | @i{documentation}]]} @{@i{form}@}{*})
  3245. @equiv{} (function (lambda @i{lambda-list} {[[@{@i{declaration}@}{*} | @i{documentation}]]} @{@i{form}@}{*}))
  3246. @equiv{} #'(lambda @i{lambda-list} {[[@{@i{declaration}@}{*} | @i{documentation}]]} @{@i{form}@}{*})
  3247. @end example
  3248. @subsubheading Examples::
  3249. @example
  3250. (funcall (lambda (x) (+ x 3)) 4) @result{} 7
  3251. @end example
  3252. @subsubheading See Also::
  3253. @b{lambda} (symbol)
  3254. @subsubheading Notes::
  3255. This macro could be implemented by:
  3256. @example
  3257. (defmacro lambda (&whole form &rest bvl-decls-and-body)
  3258. (declare (ignore bvl-decls-and-body))
  3259. `#',form)
  3260. @end example
  3261. @node compile, eval, lambda, Evaluation and Compilation Dictionary
  3262. @subsection compile [Function]
  3263. @code{compile} @i{name {&optional} definition} @result{} @i{function, warnings-p, failure-p}
  3264. @subsubheading Arguments and Values::
  3265. @i{name}---a @i{function name}, or @b{nil}.
  3266. @i{definition}---a @i{lambda expression} or a @i{function}.
  3267. The default is the function definition of @i{name} if it names a @i{function},
  3268. or the @i{macro function} of @i{name} if it names a @i{macro}.
  3269. The consequences are undefined if no @i{definition} is supplied
  3270. when the @i{name} is @b{nil}.
  3271. @i{function}---the @i{function-name},
  3272. or a @i{compiled function}.
  3273. @i{warnings-p}---a @i{generalized boolean}.
  3274. @i{failure-p}---a @i{generalized boolean}.
  3275. @subsubheading Description::
  3276. Compiles an @i{interpreted function}.
  3277. @b{compile} produces a @i{compiled function} from @i{definition}.
  3278. If the @i{definition} is a @i{lambda expression},
  3279. it is coerced to a @i{function}.
  3280. If the @i{definition} is already a @i{compiled function},
  3281. @b{compile} either produces that function itself (@i{i.e.}, is an identity operation)
  3282. or an equivalent function.
  3283. [Editorial Note by KMP: There are a number of ambiguities here that still need resolution.]
  3284. If the @i{name} is @b{nil},
  3285. the resulting @i{compiled function} is returned directly as the @i{primary value}.
  3286. If a @i{non-nil} @i{name} is given,
  3287. then the resulting @i{compiled function} replaces
  3288. the existing @i{function} definition of @i{name}
  3289. and the @i{name} is returned as the @i{primary value};
  3290. if @i{name} is a @i{symbol} that names a @i{macro},
  3291. its @i{macro function} is updated
  3292. and the @i{name} is returned as the @i{primary value}.
  3293. @i{Literal objects} appearing in code processed by
  3294. the @b{compile} function are neither copied nor @i{coalesced}.
  3295. The code resulting from the execution of @b{compile}
  3296. references @i{objects} that are @b{eql} to the corresponding
  3297. @i{objects} in the source code.
  3298. @b{compile} is permitted, but not required, to @i{establish}
  3299. a @i{handler} for @i{conditions} of @i{type} @b{error}.
  3300. For example, the @i{handler} might issue a warning and
  3301. restart compilation from some @i{implementation-dependent} point
  3302. in order to let the compilation proceed without manual intervention.
  3303. The @i{secondary value}, @i{warnings-p}, is @i{false}
  3304. if no @i{conditions} of @i{type} @b{error} or @b{warning}
  3305. were detected by the compiler, and @i{true} otherwise.
  3306. The @i{tertiary value}, @i{failure-p}, is @i{false}
  3307. if no @i{conditions} of @i{type} @b{error} or @b{warning}
  3308. (other than @b{style-warning})
  3309. were detected by the compiler, and @i{true} otherwise.
  3310. @subsubheading Examples::
  3311. @example
  3312. (defun foo () "bar") @result{} FOO
  3313. (compiled-function-p #'foo) @result{} @i{implementation-dependent}
  3314. (compile 'foo) @result{} FOO
  3315. (compiled-function-p #'foo) @result{} @i{true}
  3316. (setf (symbol-function 'foo)
  3317. (compile nil '(lambda () "replaced"))) @result{} #<Compiled-Function>
  3318. (foo) @result{} "replaced"
  3319. @end example
  3320. @subsubheading Affected By::
  3321. @b{*error-output*},
  3322. @b{*macroexpand-hook*}.
  3323. The presence of macro definitions and proclamations.
  3324. @subsubheading Exceptional Situations::
  3325. The consequences are undefined if the @i{lexical environment} surrounding the
  3326. @i{function} to be compiled contains any @i{bindings} other than those for
  3327. @i{macros}, @i{symbol macros}, or @i{declarations}.
  3328. For information about errors detected during the compilation process,
  3329. see @ref{Exceptional Situations in the Compiler}.
  3330. @subsubheading See Also::
  3331. @ref{compile-file}
  3332. @node eval, eval-when, compile, Evaluation and Compilation Dictionary
  3333. @subsection eval [Function]
  3334. @code{eval} @i{form} @result{} @i{@{@i{result}@}{*}}
  3335. @subsubheading Arguments and Values::
  3336. @i{form}---a @i{form}.
  3337. @i{results}---the @i{values} @i{yielded} by the @i{evaluation} of @i{form}.
  3338. @subsubheading Description::
  3339. Evaluates @i{form} in the current @i{dynamic environment}
  3340. and the @i{null lexical environment}.
  3341. @b{eval} is a user interface to the evaluator.
  3342. The evaluator expands macro calls as if through the use of @b{macroexpand-1}.
  3343. Constants appearing in code
  3344. processed by @b{eval} are
  3345. not copied nor coalesced. The code resulting from the execution of
  3346. @b{eval}
  3347. references @i{objects}
  3348. that are @b{eql} to the corresponding @i{objects} in
  3349. the source code.
  3350. @subsubheading Examples::
  3351. @example
  3352. (setq form '(1+ a) a 999) @result{} 999
  3353. (eval form) @result{} 1000
  3354. (eval 'form) @result{} (1+ A)
  3355. (let ((a '(this would break if eval used local value))) (eval form))
  3356. @result{} 1000
  3357. @end example
  3358. @subsubheading See Also::
  3359. @b{macroexpand-1},
  3360. @ref{The Evaluation Model}
  3361. @subsubheading Notes::
  3362. To obtain the current dynamic value of a @i{symbol},
  3363. use of @b{symbol-value} is equivalent (and usually preferable)
  3364. to use of @b{eval}.
  3365. Note that an @b{eval} @i{form} involves two levels of @i{evaluation}
  3366. for its @i{argument}. First, @i{form} is @i{evaluated} by the
  3367. normal argument evaluation mechanism as would occur with any @i{call}.
  3368. The @i{object} that results from this normal @i{argument} @i{evaluation}
  3369. becomes the @i{value} of the @i{form} @i{parameter}, and is then
  3370. @i{evaluated} as part of the @b{eval} @i{form}.
  3371. For example:
  3372. @example
  3373. (eval (list 'cdr (car '((quote (a . b)) c)))) @result{} b
  3374. @end example
  3375. The @i{argument} @i{form} @t{(list 'cdr (car '((quote (a . b)) c)))} is evaluated
  3376. in the usual way to produce the @i{argument} @t{(cdr (quote (a . b)))};
  3377. @b{eval} then evaluates its @i{argument}, @t{(cdr (quote (a . b)))}, to produce @t{b}.
  3378. Since a single @i{evaluation} already occurs for any @i{argument} @i{form}
  3379. in any @i{function form},
  3380. @b{eval} is sometimes said to perform ``an extra level of evaluation.''
  3381. @node eval-when, load-time-value, eval, Evaluation and Compilation Dictionary
  3382. @subsection eval-when [Special Operator]
  3383. @code{eval-when} @i{@r{(}@{@i{situation}@}{*}@r{)} @{@i{form}@}{*}} @result{} @i{@{@i{result}@}{*}}
  3384. @subsubheading Arguments and Values::
  3385. @i{situation}---One of the @i{symbols}
  3386. @t{:compile-toplevel}
  3387. @IKindex{compile-toplevel}
  3388. ,
  3389. @t{:load-toplevel}
  3390. @IKindex{load-toplevel}
  3391. ,
  3392. @t{:execute}
  3393. @IKindex{execute}
  3394. ,
  3395. @b{compile}
  3396. @IRindex{compile}
  3397. ,
  3398. @b{load}
  3399. @IRindex{load}
  3400. , or
  3401. @b{eval}
  3402. @IRindex{eval}
  3403. .
  3404. The use of @b{eval}, @b{compile}, and @b{load} is deprecated.
  3405. @i{forms}---an @i{implicit progn}.
  3406. @i{results}---the @i{values} of the @i{forms} if they are executed,
  3407. or @b{nil} if they are not.
  3408. @subsubheading Description::
  3409. The body of an @b{eval-when} form is processed as an @i{implicit progn},
  3410. but only in the @i{situations} listed.
  3411. The use of the @i{situations} @t{:compile-toplevel} (or @t{compile}) and
  3412. @t{:load-toplevel} (or @t{load}) controls whether and when @i{evaluation}
  3413. occurs when @b{eval-when} appears as a @i{top level form} in
  3414. code processed by @b{compile-file}. See @ref{File Compilation}.
  3415. The use of the @i{situation} @t{:execute} (or @t{eval}) controls whether
  3416. evaluation occurs for other @b{eval-when} @i{forms}; that is,
  3417. those that are not @i{top level forms}, or those in code processed by
  3418. @b{eval} or @b{compile}. If the @t{:execute} situation is
  3419. specified in such a @i{form}, then the body @i{forms} are processed as
  3420. an @i{implicit progn}; otherwise, the @b{eval-when} @i{form}
  3421. returns @b{nil}.
  3422. @b{eval-when}
  3423. normally appears as a @i{top level form}, but it is meaningful
  3424. for it to appear as a @i{non-top-level form}.
  3425. However, the compile-time side
  3426. effects described in @ref{Compilation}
  3427. only take place when @b{eval-when} appears as a
  3428. @i{top level form}.
  3429. @subsubheading Examples::
  3430. One example of the use of @b{eval-when} is that for the
  3431. compiler to be able to read a file properly when it uses user-defined
  3432. @i{reader macros}, it is necessary to write
  3433. @example
  3434. (eval-when (:compile-toplevel :load-toplevel :execute)
  3435. (set-macro-character #\$ #'(lambda (stream char)
  3436. (declare (ignore char))
  3437. (list 'dollar (read stream))))) @result{} T
  3438. @end example
  3439. This causes the call to @b{set-macro-character} to be executed
  3440. in the compiler's execution environment, thereby modifying its
  3441. reader syntax table.
  3442. @example
  3443. ;;; The EVAL-WHEN in this case is not at toplevel, so only the :EXECUTE
  3444. ;;; keyword is considered. At compile time, this has no effect.
  3445. ;;; At load time (if the LET is at toplevel), or at execution time
  3446. ;;; (if the LET is embedded in some other form which does not execute
  3447. ;;; until later) this sets (SYMBOL-FUNCTION 'FOO1) to a function which
  3448. ;;; returns 1.
  3449. (let ((x 1))
  3450. (eval-when (:execute :load-toplevel :compile-toplevel)
  3451. (setf (symbol-function 'foo1) #'(lambda () x))))
  3452. ;;; If this expression occurs at the toplevel of a file to be compiled,
  3453. ;;; it has BOTH a compile time AND a load-time effect of setting
  3454. ;;; (SYMBOL-FUNCTION 'FOO2) to a function which returns 2.
  3455. (eval-when (:execute :load-toplevel :compile-toplevel)
  3456. (let ((x 2))
  3457. (eval-when (:execute :load-toplevel :compile-toplevel)
  3458. (setf (symbol-function 'foo2) #'(lambda () x)))))
  3459. ;;; If this expression occurs at the toplevel of a file to be compiled,
  3460. ;;; it has BOTH a compile time AND a load-time effect of setting the
  3461. ;;; function cell of FOO3 to a function which returns 3.
  3462. (eval-when (:execute :load-toplevel :compile-toplevel)
  3463. (setf (symbol-function 'foo3) #'(lambda () 3)))
  3464. ;;; #4: This always does nothing. It simply returns NIL.
  3465. (eval-when (:compile-toplevel)
  3466. (eval-when (:compile-toplevel)
  3467. (print 'foo4)))
  3468. ;;; If this form occurs at toplevel of a file to be compiled, FOO5 is
  3469. ;;; printed at compile time. If this form occurs in a non-top-level
  3470. ;;; position, nothing is printed at compile time. Regardless of context,
  3471. ;;; nothing is ever printed at load time or execution time.
  3472. (eval-when (:compile-toplevel)
  3473. (eval-when (:execute)
  3474. (print 'foo5)))
  3475. ;;; If this form occurs at toplevel of a file to be compiled, FOO6 is
  3476. ;;; printed at compile time. If this form occurs in a non-top-level
  3477. ;;; position, nothing is printed at compile time. Regardless of context,
  3478. ;;; nothing is ever printed at load time or execution time.
  3479. (eval-when (:execute :load-toplevel)
  3480. (eval-when (:compile-toplevel)
  3481. (print 'foo6)))
  3482. @end example
  3483. @subsubheading See Also::
  3484. @ref{compile-file}
  3485. , @ref{Compilation}
  3486. @subsubheading Notes::
  3487. The following effects are logical consequences of the definition of
  3488. @b{eval-when}:
  3489. @table @asis
  3490. @item @t{*}
  3491. Execution of a single @b{eval-when}
  3492. expression executes the body code at most once.
  3493. @item @t{*}
  3494. @i{Macros} intended for use in @i{top level forms}
  3495. should be written so that side-effects are done by the @i{forms}
  3496. in the macro expansion. The macro-expander itself should not do
  3497. the side-effects.
  3498. For example:
  3499. Wrong:
  3500. @example
  3501. (defmacro foo ()
  3502. (really-foo)
  3503. `(really-foo))
  3504. @end example
  3505. Right:
  3506. @example
  3507. (defmacro foo ()
  3508. `(eval-when (:compile-toplevel :execute :load-toplevel) (really-foo)))
  3509. @end example
  3510. Adherence to this convention means that such @i{macros} behave
  3511. intuitively when appearing as @i{non-top-level forms}.
  3512. @item @t{*}
  3513. Placing a variable binding around an @b{eval-when} reliably
  3514. captures the binding because the compile-time-too mode cannot occur
  3515. (@i{i.e.}, introducing a variable binding means that the @b{eval-when}
  3516. is not a @i{top level form}).
  3517. For example,
  3518. @example
  3519. (let ((x 3))
  3520. (eval-when (:execute :load-toplevel :compile-toplevel) (print x)))
  3521. @end example
  3522. prints @t{3}
  3523. at execution (@i{i.e.}, load) time, and does not print anything at
  3524. compile time. This is important so that expansions of
  3525. @b{defun} and
  3526. @b{defmacro}
  3527. can be done in terms of @b{eval-when} and can correctly capture
  3528. the @i{lexical environment}.
  3529. @example
  3530. (defun bar (x) (defun foo () (+ x 3)))
  3531. @end example
  3532. might expand into
  3533. @example
  3534. (defun bar (x)
  3535. (progn (eval-when (:compile-toplevel)
  3536. (compiler::notice-function-definition 'foo '(x)))
  3537. (eval-when (:execute :load-toplevel)
  3538. (setf (symbol-function 'foo) #'(lambda () (+ x 3))))))
  3539. @end example
  3540. which would be treated by the above rules the same as
  3541. @example
  3542. (defun bar (x)
  3543. (setf (symbol-function 'foo) #'(lambda () (+ x 3))))
  3544. @end example
  3545. when the definition of @t{bar} is not a @i{top level form}.
  3546. @end table
  3547. @node load-time-value, quote, eval-when, Evaluation and Compilation Dictionary
  3548. @subsection load-time-value [Special Operator]
  3549. @code{load-time-value} @i{form {&optional} read-only-p} @result{} @i{object}
  3550. @subsubheading Arguments and Values::
  3551. @i{form}---a @i{form}; evaluated as described below.
  3552. @i{read-only-p}---a @i{boolean}; not evaluated.
  3553. @i{object}---the @i{primary value} resulting from evaluating @i{form}.
  3554. @subsubheading Description::
  3555. @b{load-time-value} provides a mechanism for delaying evaluation of @i{form}
  3556. until the expression is in the run-time environment; see @ref{Compilation}.
  3557. @i{Read-only-p} designates whether the result can be considered a
  3558. @i{constant object}.
  3559. If @b{t},
  3560. the result is a read-only quantity that can,
  3561. if appropriate to the @i{implementation},
  3562. be copied into read-only space and/or @i{coalesced} with @i{similar}
  3563. @i{constant objects} from other @i{programs}.
  3564. If @b{nil} (the default),
  3565. the result must be neither copied nor coalesced;
  3566. it must be considered to be potentially modifiable data.
  3567. If a @b{load-time-value} expression is processed by @b{compile-file},
  3568. the compiler performs its normal semantic processing (such as macro expansion
  3569. and translation into machine code) on @i{form}, but arranges for the
  3570. execution of @i{form} to occur at load time in a @i{null lexical environment},
  3571. with the result of this @i{evaluation} then being treated as
  3572. a @i{literal object}
  3573. at run time. It is guaranteed that the evaluation of @i{form}
  3574. will take place only once when the @i{file} is @i{loaded}, but
  3575. the order of evaluation with respect to the evaluation of
  3576. @i{top level forms} in the file is @i{implementation-dependent}.
  3577. @ITindex{order of evaluation}
  3578. @ITindex{evaluation order}
  3579. If a @b{load-time-value} expression appears within a function compiled
  3580. with @b{compile}, the @i{form} is evaluated at compile time in a
  3581. @i{null lexical environment}. The result of this compile-time evaluation
  3582. is treated as
  3583. a @i{literal object}
  3584. in the compiled code.
  3585. If a @b{load-time-value} expression is processed by @b{eval},
  3586. @i{form} is evaluated in a @i{null lexical environment},
  3587. and one value is returned. Implementations that implicitly compile
  3588. (or partially compile) expressions processed by @b{eval}
  3589. might evaluate @i{form} only once, at the time this compilation is performed.
  3590. If the @i{same} @i{list} @t{(load-time-value @i{form})} is
  3591. evaluated or compiled more than once, it is @i{implementation-dependent}
  3592. whether @i{form} is evaluated only once or is evaluated more than once.
  3593. This can happen both when an expression being evaluated or compiled shares
  3594. substructure, and when the @i{same} @i{form} is processed by @b{eval} or
  3595. @b{compile} multiple times.
  3596. Since a @b{load-time-value} expression can be
  3597. referenced in more than one place and can be evaluated multiple times
  3598. by @b{eval}, it is
  3599. @i{implementation-dependent} whether each execution returns
  3600. a fresh @i{object}
  3601. or returns the same @i{object} as some other execution.
  3602. Users must use caution when destructively modifying the resulting
  3603. @i{object}.
  3604. If two lists @t{(load-time-value @i{form})}
  3605. that are the @i{same} under @b{equal} but are not @i{identical}
  3606. are evaluated or compiled,
  3607. their values always come from distinct evaluations of @i{form}.
  3608. Their @i{values} may not be coalesced
  3609. unless @i{read-only-p} is @b{t}.
  3610. @subsubheading Examples::
  3611. @example
  3612. ;;; The function INCR1 always returns the same value, even in different images.
  3613. ;;; The function INCR2 always returns the same value in a given image,
  3614. ;;; but the value it returns might vary from image to image.
  3615. (defun incr1 (x) (+ x #.(random 17)))
  3616. (defun incr2 (x) (+ x (load-time-value (random 17))))
  3617. ;;; The function FOO1-REF references the nth element of the first of
  3618. ;;; the *FOO-ARRAYS* that is available at load time. It is permissible for
  3619. ;;; that array to be modified (e.g., by SET-FOO1-REF); FOO1-REF will see the
  3620. ;;; updated values.
  3621. (defvar *foo-arrays* (list (make-array 7) (make-array 8)))
  3622. (defun foo1-ref (n) (aref (load-time-value (first *my-arrays*) nil) n))
  3623. (defun set-foo1-ref (n val)
  3624. (setf (aref (load-time-value (first *my-arrays*) nil) n) val))
  3625. ;;; The function BAR1-REF references the nth element of the first of
  3626. ;;; the *BAR-ARRAYS* that is available at load time. The programmer has
  3627. ;;; promised that the array will be treated as read-only, so the system
  3628. ;;; can copy or coalesce the array.
  3629. (defvar *bar-arrays* (list (make-array 7) (make-array 8)))
  3630. (defun bar1-ref (n) (aref (load-time-value (first *my-arrays*) t) n))
  3631. ;;; This use of LOAD-TIME-VALUE permits the indicated vector to be coalesced
  3632. ;;; even though NIL was specified, because the object was already read-only
  3633. ;;; when it was written as a literal vector rather than created by a constructor.
  3634. ;;; User programs must treat the vector v as read-only.
  3635. (defun baz-ref (n)
  3636. (let ((v (load-time-value #(A B C) nil)))
  3637. (values (svref v n) v)))
  3638. ;;; This use of LOAD-TIME-VALUE permits the indicated vector to be coalesced
  3639. ;;; even though NIL was specified in the outer situation because T was specified
  3640. ;;; in the inner situation. User programs must treat the vector v as read-only.
  3641. (defun baz-ref (n)
  3642. (let ((v (load-time-value (load-time-value (vector 1 2 3) t) nil)))
  3643. (values (svref v n) v)))
  3644. @end example
  3645. @subsubheading See Also::
  3646. @ref{compile-file}
  3647. ,
  3648. @ref{compile}
  3649. ,
  3650. @ref{eval}
  3651. ,
  3652. @ref{Minimal Compilation},
  3653. @ref{Compilation}
  3654. @subsubheading Notes::
  3655. @b{load-time-value} must appear outside of quoted structure in a
  3656. ``for @i{evaluation}'' position. In situations which would appear to call
  3657. for use of @b{load-time-value} within a quoted structure,
  3658. the @i{backquote} @i{reader macro} is probably called for;
  3659. see @ref{Backquote}.
  3660. Specifying @b{nil} for @i{read-only-p} is not a way to force an object
  3661. to become modifiable if it has already been made read-only. It is only a way
  3662. to say that, for an object that is modifiable, this operation is not intended
  3663. to make that object read-only.
  3664. @node quote, compiler-macro-function, load-time-value, Evaluation and Compilation Dictionary
  3665. @subsection quote [Special Operator]
  3666. @code{quote} @i{object} @result{} @i{object}
  3667. @subsubheading Arguments and Values::
  3668. @i{object}---an @i{object}; not evaluated.
  3669. @subsubheading Description::
  3670. The @b{quote} @i{special operator} just returns @i{object}.
  3671. The consequences are undefined if @i{literal objects} (including
  3672. @i{quoted objects}) are destructively modified.
  3673. @subsubheading Examples::
  3674. @example
  3675. (setq a 1) @result{} 1
  3676. (quote (setq a 3)) @result{} (SETQ A 3)
  3677. a @result{} 1
  3678. 'a @result{} A
  3679. ''a @result{} (QUOTE A)
  3680. '''a @result{} (QUOTE (QUOTE A))
  3681. (setq a 43) @result{} 43
  3682. (list a (cons a 3)) @result{} (43 (43 . 3))
  3683. (list (quote a) (quote (cons a 3))) @result{} (A (CONS A 3))
  3684. 1 @result{} 1
  3685. '1 @result{} 1
  3686. "foo" @result{} "foo"
  3687. '"foo" @result{} "foo"
  3688. (car '(a b)) @result{} A
  3689. '(car '(a b)) @result{} (CAR (QUOTE (A B)))
  3690. #(car '(a b)) @result{} #(CAR (QUOTE (A B)))
  3691. '#(car '(a b)) @result{} #(CAR (QUOTE (A B)))
  3692. @end example
  3693. @subsubheading See Also::
  3694. @ref{Evaluation},
  3695. @ref{Single-Quote},
  3696. @ref{Compiler Terminology}
  3697. @subsubheading Notes::
  3698. The textual notation @t{'@i{object}} is equivalent to @t{(quote @i{object})};
  3699. see @ref{Compiler Terminology}.
  3700. Some @i{objects}, called @i{self-evaluating objects},
  3701. do not require quotation by @b{quote}.
  3702. However, @i{symbols} and @i{lists} are used to represent parts of programs,
  3703. and so would not be useable as constant data in a program without @b{quote}.
  3704. Since @b{quote} suppresses the @i{evaluation} of these @i{objects},
  3705. they become data rather than program.
  3706. @node compiler-macro-function, define-compiler-macro, quote, Evaluation and Compilation Dictionary
  3707. @subsection compiler-macro-function [Accessor]
  3708. @code{compiler-macro-function} @i{name {&optional} environment} @result{} @i{function}
  3709. (setf (@code{ compiler-macro-function} @i{name {&optional} environment}) new-function)@*
  3710. @subsubheading Arguments and Values::
  3711. @i{name}---a @i{function name}.
  3712. @i{environment}---an @i{environment} @i{object}.
  3713. @i{function}, @i{new-function}---a @i{compiler macro function}, or @b{nil}.
  3714. @subsubheading Description::
  3715. @i{Accesses} the @i{compiler macro function} named @i{name}, if any,
  3716. in the @i{environment}.
  3717. A value of @b{nil} denotes the absence of a @i{compiler macro function} named @i{name}.
  3718. @subsubheading Exceptional Situations::
  3719. The consequences are undefined if @i{environment} is @i{non-nil}
  3720. in a use of @b{setf} of @b{compiler-macro-function}.
  3721. @subsubheading See Also::
  3722. @ref{define-compiler-macro}
  3723. , @ref{Compiler Macros}
  3724. @node define-compiler-macro, defmacro, compiler-macro-function, Evaluation and Compilation Dictionary
  3725. @subsection define-compiler-macro [Macro]
  3726. @code{define-compiler-macro} @i{name lambda-list {[[@{@i{declaration}@}{*} | @i{documentation}]]} @{@i{form}@}{*}}@*
  3727. @result{} @i{name}
  3728. @subsubheading Arguments and Values::
  3729. @i{name}---a @i{function name}.
  3730. @i{lambda-list}---a @i{macro lambda list}.
  3731. @i{declaration}---a @b{declare} @i{expression}; not evaluated.
  3732. @i{documentation}---a @i{string}; not evaluated.
  3733. @i{form}---a @i{form}.
  3734. @subsubheading Description::
  3735. [Editorial Note by KMP: This definition probably needs to be fully expanded to not
  3736. refer through the definition of defmacro, but should suffice for now.]
  3737. This is the normal mechanism for defining a @i{compiler macro function}.
  3738. Its manner of definition is the same as for @b{defmacro}; the only
  3739. differences are:
  3740. @table @asis
  3741. @item @t{*}
  3742. The @i{name} can be a @i{function name} naming
  3743. any @i{function} or @i{macro}.
  3744. @item @t{*}
  3745. The expander function is installed as a @i{compiler macro function}
  3746. for the @i{name}, rather than as a @i{macro function}.
  3747. @item @t{*}
  3748. The @b{&whole} argument is bound to the form argument that
  3749. is passed to the @i{compiler macro function}. The remaining lambda-list
  3750. parameters are specified as if this form contained the function name in the
  3751. @i{car} and the actual arguments in the @i{cdr}, but if the @i{car}
  3752. of the actual form is the symbol @b{funcall}, then the destructuring of
  3753. the arguments is actually performed using its @i{cddr} instead.
  3754. @item @t{*}
  3755. @i{Documentation} is attached as a @i{documentation string}
  3756. to @i{name} (as kind @b{compiler-macro})
  3757. and to the @i{compiler macro function}.
  3758. @item @t{*}
  3759. Unlike an ordinary @i{macro}, a @i{compiler macro}
  3760. can decline to provide an expansion merely by returning a form that is
  3761. the @i{same} as the original (which can be obtained by using
  3762. @b{&whole}).
  3763. @end table
  3764. @subsubheading Examples::
  3765. @example
  3766. (defun square (x) (expt x 2)) @result{} SQUARE
  3767. (define-compiler-macro square (&whole form arg)
  3768. (if (atom arg)
  3769. `(expt ,arg 2)
  3770. (case (car arg)
  3771. (square (if (= (length arg) 2)
  3772. `(expt ,(nth 1 arg) 4)
  3773. form))
  3774. (expt (if (= (length arg) 3)
  3775. (if (numberp (nth 2 arg))
  3776. `(expt ,(nth 1 arg) ,(* 2 (nth 2 arg)))
  3777. `(expt ,(nth 1 arg) (* 2 ,(nth 2 arg))))
  3778. form))
  3779. (otherwise `(expt ,arg 2))))) @result{} SQUARE
  3780. (square (square 3)) @result{} 81
  3781. (macroexpand '(square x)) @result{} (SQUARE X), @i{false}
  3782. (funcall (compiler-macro-function 'square) '(square x) nil)
  3783. @result{} (EXPT X 2)
  3784. (funcall (compiler-macro-function 'square) '(square (square x)) nil)
  3785. @result{} (EXPT X 4)
  3786. (funcall (compiler-macro-function 'square) '(funcall #'square x) nil)
  3787. @result{} (EXPT X 2)
  3788. (defun distance-positional (x1 y1 x2 y2)
  3789. (sqrt (+ (expt (- x2 x1) 2) (expt (- y2 y1) 2))))
  3790. @result{} DISTANCE-POSITIONAL
  3791. (defun distance (&key (x1 0) (y1 0) (x2 x1) (y2 y1))
  3792. (distance-positional x1 y1 x2 y2))
  3793. @result{} DISTANCE
  3794. (define-compiler-macro distance (&whole form
  3795. &rest key-value-pairs
  3796. &key (x1 0 x1-p)
  3797. (y1 0 y1-p)
  3798. (x2 x1 x2-p)
  3799. (y2 y1 y2-p)
  3800. &allow-other-keys
  3801. &environment env)
  3802. (flet ((key (n) (nth (* n 2) key-value-pairs))
  3803. (arg (n) (nth (1+ (* n 2)) key-value-pairs))
  3804. (simplep (x)
  3805. (let ((expanded-x (macroexpand x env)))
  3806. (or (constantp expanded-x env)
  3807. (symbolp expanded-x)))))
  3808. (let ((n (/ (length key-value-pairs) 2)))
  3809. (multiple-value-bind (x1s y1s x2s y2s others)
  3810. (loop for (key) on key-value-pairs by #'cddr
  3811. count (eq key ':x1) into x1s
  3812. count (eq key ':y1) into y1s
  3813. count (eq key ':x2) into x2s
  3814. count (eq key ':y1) into y2s
  3815. count (not (member key '(:x1 :x2 :y1 :y2)))
  3816. into others
  3817. finally (return (values x1s y1s x2s y2s others)))
  3818. (cond ((and (= n 4)
  3819. (eq (key 0) :x1)
  3820. (eq (key 1) :y1)
  3821. (eq (key 2) :x2)
  3822. (eq (key 3) :y2))
  3823. `(distance-positional ,x1 ,y1 ,x2 ,y2))
  3824. ((and (if x1-p (and (= x1s 1) (simplep x1)) t)
  3825. (if y1-p (and (= y1s 1) (simplep y1)) t)
  3826. (if x2-p (and (= x2s 1) (simplep x2)) t)
  3827. (if y2-p (and (= y2s 1) (simplep y2)) t)
  3828. (zerop others))
  3829. `(distance-positional ,x1 ,y1 ,x2 ,y2))
  3830. ((and (< x1s 2) (< y1s 2) (< x2s 2) (< y2s 2)
  3831. (zerop others))
  3832. (let ((temps (loop repeat n collect (gensym))))
  3833. `(let ,(loop for i below n
  3834. collect (list (nth i temps) (arg i)))
  3835. (distance
  3836. ,@@(loop for i below n
  3837. append (list (key i) (nth i temps)))))))
  3838. (t form))))))
  3839. @result{} DISTANCE
  3840. (dolist (form
  3841. '((distance :x1 (setq x 7) :x2 (decf x) :y1 (decf x) :y2 (decf x))
  3842. (distance :x1 (setq x 7) :y1 (decf x) :x2 (decf x) :y2 (decf x))
  3843. (distance :x1 (setq x 7) :y1 (incf x))
  3844. (distance :x1 (setq x 7) :y1 (incf x) :x1 (incf x))
  3845. (distance :x1 a1 :y1 b1 :x2 a2 :y2 b2)
  3846. (distance :x1 a1 :x2 a2 :y1 b1 :y2 b2)
  3847. (distance :x1 a1 :y1 b1 :z1 c1 :x2 a2 :y2 b2 :z2 c2)))
  3848. (print (funcall (compiler-macro-function 'distance) form nil)))
  3849. @t{ |> } (LET ((#:G6558 (SETQ X 7))
  3850. @t{ |> } (#:G6559 (DECF X))
  3851. @t{ |> } (#:G6560 (DECF X))
  3852. @t{ |> } (#:G6561 (DECF X)))
  3853. @t{ |> } (DISTANCE :X1 #:G6558 :X2 #:G6559 :Y1 #:G6560 :Y2 #:G6561))
  3854. @t{ |> } (DISTANCE-POSITIONAL (SETQ X 7) (DECF X) (DECF X) (DECF X))
  3855. @t{ |> } (LET ((#:G6567 (SETQ X 7))
  3856. @t{ |> } (#:G6568 (INCF X)))
  3857. @t{ |> } (DISTANCE :X1 #:G6567 :Y1 #:G6568))
  3858. @t{ |> } (DISTANCE :X1 (SETQ X 7) :Y1 (INCF X) :X1 (INCF X))
  3859. @t{ |> } (DISTANCE-POSITIONAL A1 B1 A2 B2)
  3860. @t{ |> } (DISTANCE-POSITIONAL A1 B1 A2 B2)
  3861. @t{ |> } (DISTANCE :X1 A1 :Y1 B1 :Z1 C1 :X2 A2 :Y2 B2 :Z2 C2)
  3862. @result{} NIL
  3863. @end example
  3864. @subsubheading See Also::
  3865. @ref{compiler-macro-function}
  3866. ,
  3867. @ref{defmacro}
  3868. ,
  3869. @ref{documentation; (setf documentation)}
  3870. ,
  3871. @ref{Syntactic Interaction of Documentation Strings and Declarations}
  3872. @subsubheading Notes::
  3873. The consequences of writing a @i{compiler macro} definition for a function
  3874. in the @t{COMMON-LISP} @i{package} are undefined; it is quite possible that in some
  3875. @i{implementations} such an attempt would override an equivalent or equally
  3876. important definition. In general, it is recommended that a programmer only
  3877. write @i{compiler macro} definitions for @i{functions} he or she personally
  3878. maintains--writing a @i{compiler macro} definition for a function maintained
  3879. elsewhere is normally considered a violation of traditional rules of modularity
  3880. and data abstraction.
  3881. @node defmacro, macro-function, define-compiler-macro, Evaluation and Compilation Dictionary
  3882. @subsection defmacro [Macro]
  3883. @code{defmacro} @i{name lambda-list {[[@{@i{declaration}@}{*} | @i{documentation}]]} @{@i{form}@}{*}}@*
  3884. @result{} @i{name}
  3885. @subsubheading Arguments and Values::
  3886. @i{name}---a @i{symbol}.
  3887. @i{lambda-list}---a @i{macro lambda list}.
  3888. @i{declaration}---a @b{declare} @i{expression}; not evaluated.
  3889. @i{documentation}---a @i{string}; not evaluated.
  3890. @i{form}---a @i{form}.
  3891. @subsubheading Description::
  3892. Defines @i{name} as a @i{macro}
  3893. by associating a @i{macro function} with that @i{name}
  3894. in the global environment.
  3895. The @i{macro function} is defined in the same @i{lexical environment}
  3896. in which the @b{defmacro} @i{form} appears.
  3897. The parameter variables in @i{lambda-list} are bound to
  3898. destructured portions of the macro call.
  3899. The expansion function
  3900. accepts two arguments, a @i{form} and an
  3901. @i{environment}. The expansion function returns a @i{form}.
  3902. The body of the expansion function is specified by @i{forms}.
  3903. @i{Forms} are executed in order. The value of the
  3904. last @i{form} executed is returned as the expansion of the
  3905. @i{macro}.
  3906. The body @i{forms} of the expansion function (but not the @i{lambda-list})
  3907. are implicitly enclosed in a @i{block} whose name is @i{name}.
  3908. The @i{lambda-list} conforms to the requirements described in @ref{Macro Lambda Lists}.
  3909. @i{Documentation} is attached as a @i{documentation string}
  3910. to @i{name} (as kind @b{function})
  3911. and to the @i{macro function}.
  3912. @b{defmacro} can be used to redefine a @i{macro} or to replace
  3913. a @i{function} definition with a @i{macro} definition.
  3914. Recursive expansion of the @i{form} returned must terminate,
  3915. including the expansion of other @i{macros} which are @i{subforms}
  3916. of other @i{forms} returned.
  3917. The consequences are undefined if the result of fully macroexpanding
  3918. a @i{form}
  3919. contains any @i{circular} @i{list structure} except in @i{literal objects}.
  3920. If a @b{defmacro} @i{form} appears as a @i{top level form},
  3921. the @i{compiler} must store the @i{macro} definition at compile time,
  3922. so that occurrences of the macro later on in the file can be expanded correctly.
  3923. Users must ensure that the body of the @i{macro} can be evaluated at
  3924. compile time if it is referenced within the @i{file} being @i{compiled}.
  3925. @subsubheading Examples::
  3926. @example
  3927. (defmacro mac1 (a b) "Mac1 multiplies and adds"
  3928. `(+ ,a (* ,b 3))) @result{} MAC1
  3929. (mac1 4 5) @result{} 19
  3930. (documentation 'mac1 'function) @result{} "Mac1 multiplies and adds"
  3931. (defmacro mac2 (&optional (a 2 b) (c 3 d) &rest x) `'(,a ,b ,c ,d ,x)) @result{} MAC2
  3932. (mac2 6) @result{} (6 T 3 NIL NIL)
  3933. (mac2 6 3 8) @result{} (6 T 3 T (8))
  3934. (defmacro mac3 (&whole r a &optional (b 3) &rest x &key c (d a))
  3935. `'(,r ,a ,b ,c ,d ,x)) @result{} MAC3
  3936. (mac3 1 6 :d 8 :c 9 :d 10) @result{} ((MAC3 1 6 :D 8 :C 9 :D 10) 1 6 9 8 (:D 8 :C 9 :D 10))
  3937. @end example
  3938. The stipulation that
  3939. an embedded @i{destructuring lambda list} is permitted only
  3940. where @i{ordinary lambda list} syntax would permit a parameter name
  3941. but not a @i{list} is made to prevent ambiguity. For example,
  3942. the following is not valid:
  3943. @example
  3944. (defmacro loser (x &optional (a b &rest c) &rest z)
  3945. ...)
  3946. @end example
  3947. because @i{ordinary lambda list} syntax does permit a
  3948. @i{list} following @t{&optional};
  3949. the list @t{(a b &rest c)} would be interpreted as describing an
  3950. optional parameter named @t{a} whose default value is that of the
  3951. form @t{b}, with a supplied-p parameter named @b{&rest} (not valid),
  3952. and an extraneous symbol @t{c} in the list (also not valid). An almost
  3953. correct way to express this is
  3954. @example
  3955. (defmacro loser (x &optional ((a b &rest c)) &rest z)
  3956. ...)
  3957. @end example
  3958. The extra set of parentheses removes the ambiguity. However, the
  3959. definition is now incorrect because a macro call such as @t{(loser (car pool))}
  3960. would not provide any argument form for the lambda list @t{(a b &rest c)},
  3961. and so the default value against which to match the @i{lambda list} would be
  3962. @b{nil} because no explicit default value was specified.
  3963. The consequences of this are unspecified
  3964. since the empty list, @b{nil}, does not have @i{forms} to satisfy the
  3965. parameters @t{a} and @t{b}. The fully correct definition would be either
  3966. @example
  3967. (defmacro loser (x &optional ((a b &rest c) '(nil nil)) &rest z)
  3968. ...)
  3969. @end example
  3970. or
  3971. @example
  3972. (defmacro loser (x &optional ((&optional a b &rest c)) &rest z)
  3973. ...)
  3974. @end example
  3975. These differ slightly: the first requires that if the macro call
  3976. specifies @t{a} explicitly then it must also specify @t{b} explicitly,
  3977. whereas the second does not have this requirement. For example,
  3978. @example
  3979. (loser (car pool) ((+ x 1)))
  3980. @end example
  3981. would be a valid call for the second definition but not for the first.
  3982. @example
  3983. (defmacro dm1a (&whole x) `',x)
  3984. (macroexpand '(dm1a)) @result{} (QUOTE (DM1A))
  3985. (macroexpand '(dm1a a)) is an error.
  3986. (defmacro dm1b (&whole x a &optional b) `'(,x ,a ,b))
  3987. (macroexpand '(dm1b)) is an error.
  3988. (macroexpand '(dm1b q)) @result{} (QUOTE ((DM1B Q) Q NIL))
  3989. (macroexpand '(dm1b q r)) @result{} (QUOTE ((DM1B Q R) Q R))
  3990. (macroexpand '(dm1b q r s)) is an error.
  3991. @end example
  3992. @example
  3993. (defmacro dm2a (&whole form a b) `'(form ,form a ,a b ,b))
  3994. (macroexpand '(dm2a x y)) @result{} (QUOTE (FORM (DM2A X Y) A X B Y))
  3995. (dm2a x y) @result{} (FORM (DM2A X Y) A X B Y)
  3996. (defmacro dm2b (&whole form a (&whole b (c . d) &optional (e 5))
  3997. &body f &environment env)
  3998. ``(,',form ,,a ,',b ,',(macroexpand c env) ,',d ,',e ,',f))
  3999. ;Note that because backquote is involved, implementations may differ
  4000. ;slightly in the nature (though not the functionality) of the expansion.
  4001. (macroexpand '(dm2b x1 (((incf x2) x3 x4)) x5 x6))
  4002. @result{} (LIST* '(DM2B X1 (((INCF X2) X3 X4))
  4003. X5 X6)
  4004. X1
  4005. '((((INCF X2) X3 X4)) (SETQ X2 (+ X2 1)) (X3 X4) 5 (X5 X6))),
  4006. T
  4007. (let ((x1 5))
  4008. (macrolet ((segundo (x) `(cadr ,x)))
  4009. (dm2b x1 (((segundo x2) x3 x4)) x5 x6)))
  4010. @result{} ((DM2B X1 (((SEGUNDO X2) X3 X4)) X5 X6)
  4011. 5 (((SEGUNDO X2) X3 X4)) (CADR X2) (X3 X4) 5 (X5 X6))
  4012. @end example
  4013. @subsubheading See Also::
  4014. @ref{define-compiler-macro}
  4015. ,
  4016. @ref{destructuring-bind}
  4017. ,
  4018. @ref{documentation; (setf documentation)}
  4019. ,
  4020. @ref{macroexpand; macroexpand-1}
  4021. ,
  4022. @b{*macroexpand-hook*},
  4023. @b{macrolet},
  4024. @ref{macro-function}
  4025. ,
  4026. @ref{Evaluation},
  4027. @ref{Compilation},
  4028. @ref{Syntactic Interaction of Documentation Strings and Declarations}
  4029. @node macro-function, macroexpand, defmacro, Evaluation and Compilation Dictionary
  4030. @subsection macro-function [Accessor]
  4031. @code{macro-function} @i{symbol {&optional} environment} @result{} @i{function}
  4032. (setf (@code{ macro-function} @i{symbol {&optional} environment}) new-function)@*
  4033. @subsubheading Arguments and Values::
  4034. @i{symbol}---a @i{symbol}.
  4035. @i{environment}---an @i{environment} @i{object}.
  4036. @i{function}---a @i{macro function} or @b{nil}.
  4037. @i{new-function}---a @i{macro function}.
  4038. @subsubheading Description::
  4039. Determines whether @i{symbol} has a function definition
  4040. as a macro in the specified @i{environment}.
  4041. If so, the macro expansion function, a function of two arguments,
  4042. is returned. If @i{symbol} has no function definition
  4043. in the lexical environment @i{environment}, or its definition
  4044. is not a @i{macro}, @b{macro-function} returns @b{nil}.
  4045. It is possible for both @b{macro-function} and
  4046. @b{special-operator-p}
  4047. to return @i{true} of @i{symbol}. The @i{macro} definition must
  4048. be available for use by programs that understand only the standard
  4049. @r{Common Lisp} @i{special forms}.
  4050. @subsubheading Examples::
  4051. @example
  4052. (defmacro macfun (x) '(macro-function 'macfun)) @result{} MACFUN
  4053. (not (macro-function 'macfun)) @result{} @i{false}
  4054. @end example
  4055. @example
  4056. (macrolet ((foo (&environment env)
  4057. (if (macro-function 'bar env)
  4058. ''yes
  4059. ''no)))
  4060. (list (foo)
  4061. (macrolet ((bar () :beep))
  4062. (foo))))
  4063. @result{} (NO YES)
  4064. @end example
  4065. @subsubheading Affected By::
  4066. @t{(setf macro-function)}, @b{defmacro}, and @b{macrolet}.
  4067. @subsubheading Exceptional Situations::
  4068. The consequences are undefined if @i{environment} is @i{non-nil}
  4069. in a use of @b{setf} of @b{macro-function}.
  4070. @subsubheading See Also::
  4071. @ref{defmacro}
  4072. , @ref{Evaluation}
  4073. @subsubheading Notes::
  4074. @b{setf} can be used with @b{macro-function} to install
  4075. a @i{macro} as a symbol's global function definition:
  4076. @example
  4077. (setf (macro-function symbol) fn)
  4078. @end example
  4079. The value installed must be a @i{function} that accepts two arguments,
  4080. the entire macro call and an @i{environment},
  4081. and computes the expansion for that call.
  4082. Performing this operation causes @i{symbol} to have only that
  4083. macro definition as its global function definition; any previous
  4084. definition, whether as a @i{macro} or as a
  4085. @i{function}, is lost.
  4086. @node macroexpand, define-symbol-macro, macro-function, Evaluation and Compilation Dictionary
  4087. @subsection macroexpand, macroexpand-1 [Function]
  4088. @code{macroexpand} @i{form {&optional} env} @result{} @i{expansion, expanded-p}
  4089. @code{macroexpand-} @i{1} @result{} @i{form {&optional} env}
  4090. {expansion, expanded-p}
  4091. @subsubheading Arguments and Values::
  4092. @i{form}---a @i{form}.
  4093. @i{env}---an @i{environment} @i{object}.
  4094. The default is @b{nil}.
  4095. @i{expansion}---a @i{form}.
  4096. @i{expanded-p}---a @i{generalized boolean}.
  4097. @subsubheading Description::
  4098. @b{macroexpand} and @b{macroexpand-1} expand @i{macros}.
  4099. If @i{form} is a @i{macro form},
  4100. then @b{macroexpand-1} expands the @i{macro form} call once.
  4101. @b{macroexpand}
  4102. repeatedly expands @i{form} until it is no longer a @i{macro form}.
  4103. In effect, @b{macroexpand} calls @b{macroexpand-1} repeatedly
  4104. until the @i{secondary value} it returns is @b{nil}.
  4105. If @i{form} is a @i{macro form},
  4106. then the @i{expansion} is a @i{macro expansion}
  4107. and @i{expanded-p} is @i{true}.
  4108. Otherwise,
  4109. the @i{expansion} is the given @i{form}
  4110. and @i{expanded-p} is @i{false}.
  4111. Macro expansion is carried out as follows.
  4112. Once @b{macroexpand-1} has
  4113. determined that the @i{form} is a @i{macro form},
  4114. it obtains an appropriate expansion @i{function} for the
  4115. @i{macro} or @i{symbol macro}.
  4116. The value of
  4117. @b{*macroexpand-hook*} is
  4118. coerced to a @i{function} and
  4119. then called as a @i{function} of three arguments:
  4120. the expansion @i{function},
  4121. the @i{form},
  4122. and the @i{env}.
  4123. The @i{value} returned from this call is taken to be the expansion
  4124. of the @i{form}.
  4125. In addition to @i{macro} definitions in the global environment,
  4126. any local macro definitions established within @i{env} by @b{macrolet}
  4127. or @b{symbol-macrolet} are considered.
  4128. If only @i{form} is supplied as an argument,
  4129. then the environment is effectively null, and only global macro definitions
  4130. as established by @b{defmacro} are considered.
  4131. @i{Macro} definitions are shadowed by local @i{function} definitions.
  4132. @subsubheading Examples::
  4133. @example
  4134. (defmacro alpha (x y) `(beta ,x ,y)) @result{} ALPHA
  4135. (defmacro beta (x y) `(gamma ,x ,y)) @result{} BETA
  4136. (defmacro delta (x y) `(gamma ,x ,y)) @result{} EPSILON
  4137. (defmacro expand (form &environment env)
  4138. (multiple-value-bind (expansion expanded-p)
  4139. (macroexpand form env)
  4140. `(values ',expansion ',expanded-p))) @result{} EXPAND
  4141. (defmacro expand-1 (form &environment env)
  4142. (multiple-value-bind (expansion expanded-p)
  4143. (macroexpand-1 form env)
  4144. `(values ',expansion ',expanded-p))) @result{} EXPAND-1
  4145. ;; Simple examples involving just the global environment
  4146. (macroexpand-1 '(alpha a b)) @result{} (BETA A B), @i{true}
  4147. (expand-1 (alpha a b)) @result{} (BETA A B), @i{true}
  4148. (macroexpand '(alpha a b)) @result{} (GAMMA A B), @i{true}
  4149. (expand (alpha a b)) @result{} (GAMMA A B), @i{true}
  4150. (macroexpand-1 'not-a-macro) @result{} NOT-A-MACRO, @i{false}
  4151. (expand-1 not-a-macro) @result{} NOT-A-MACRO, @i{false}
  4152. (macroexpand '(not-a-macro a b)) @result{} (NOT-A-MACRO A B), @i{false}
  4153. (expand (not-a-macro a b)) @result{} (NOT-A-MACRO A B), @i{false}
  4154. ;; Examples involving lexical environments
  4155. (macrolet ((alpha (x y) `(delta ,x ,y)))
  4156. (macroexpand-1 '(alpha a b))) @result{} (BETA A B), @i{true}
  4157. (macrolet ((alpha (x y) `(delta ,x ,y)))
  4158. (expand-1 (alpha a b))) @result{} (DELTA A B), @i{true}
  4159. (macrolet ((alpha (x y) `(delta ,x ,y)))
  4160. (macroexpand '(alpha a b))) @result{} (GAMMA A B), @i{true}
  4161. (macrolet ((alpha (x y) `(delta ,x ,y)))
  4162. (expand (alpha a b))) @result{} (GAMMA A B), @i{true}
  4163. (macrolet ((beta (x y) `(epsilon ,x ,y)))
  4164. (expand (alpha a b))) @result{} (EPSILON A B), @i{true}
  4165. (let ((x (list 1 2 3)))
  4166. (symbol-macrolet ((a (first x)))
  4167. (expand a))) @result{} (FIRST X), @i{true}
  4168. (let ((x (list 1 2 3)))
  4169. (symbol-macrolet ((a (first x)))
  4170. (macroexpand 'a))) @result{} A, @i{false}
  4171. (symbol-macrolet ((b (alpha x y)))
  4172. (expand-1 b)) @result{} (ALPHA X Y), @i{true}
  4173. (symbol-macrolet ((b (alpha x y)))
  4174. (expand b)) @result{} (GAMMA X Y), @i{true}
  4175. (symbol-macrolet ((b (alpha x y))
  4176. (a b))
  4177. (expand-1 a)) @result{} B, @i{true}
  4178. (symbol-macrolet ((b (alpha x y))
  4179. (a b))
  4180. (expand a)) @result{} (GAMMA X Y), @i{true}
  4181. ;; Examples of shadowing behavior
  4182. (flet ((beta (x y) (+ x y)))
  4183. (expand (alpha a b))) @result{} (BETA A B), @i{true}
  4184. (macrolet ((alpha (x y) `(delta ,x ,y)))
  4185. (flet ((alpha (x y) (+ x y)))
  4186. (expand (alpha a b)))) @result{} (ALPHA A B), @i{false}
  4187. (let ((x (list 1 2 3)))
  4188. (symbol-macrolet ((a (first x)))
  4189. (let ((a x))
  4190. (expand a)))) @result{} A, @i{false}
  4191. @end example
  4192. @subsubheading Affected By::
  4193. @b{defmacro},
  4194. @b{setf} of @b{macro-function},
  4195. @b{macrolet},
  4196. @b{symbol-macrolet}
  4197. @subsubheading See Also::
  4198. @b{*macroexpand-hook*},
  4199. @ref{defmacro}
  4200. ,
  4201. @ref{setf; psetf}
  4202. of
  4203. @ref{macro-function}
  4204. ,
  4205. @b{macrolet},
  4206. @ref{symbol-macrolet}
  4207. ,
  4208. @ref{Evaluation}
  4209. @subsubheading Notes::
  4210. Neither @b{macroexpand} nor @b{macroexpand-1}
  4211. makes any explicit attempt to expand @i{macro forms} that are
  4212. either @i{subforms} of the @i{form}
  4213. or @i{subforms} of the @i{expansion}.
  4214. Such expansion might occur implicitly, however,
  4215. due to the semantics or implementation of the @i{macro function}.
  4216. @node define-symbol-macro, symbol-macrolet, macroexpand, Evaluation and Compilation Dictionary
  4217. @subsection define-symbol-macro [Macro]
  4218. @code{define-symbol-macro} @i{symbol expansion}@*
  4219. @result{} @i{symbol}
  4220. @subsubheading Arguments and Values::
  4221. @i{symbol}---a @i{symbol}.
  4222. @i{expansion}---a @i{form}.
  4223. @subsubheading Description::
  4224. Provides a mechanism for globally affecting the @i{macro expansion}
  4225. of the indicated @i{symbol}.
  4226. Globally establishes an expansion function for the @i{symbol macro}
  4227. named by @i{symbol}.
  4228. The only guaranteed property of an expansion @i{function} for a @i{symbol macro}
  4229. is that when it is applied to the @i{form} and the @i{environment} it returns
  4230. the correct expansion. (In particular, it is @i{implementation-dependent}
  4231. whether the expansion is conceptually stored in the expansion function,
  4232. the @i{environment}, or both.)
  4233. Each global reference to @i{symbol} (@i{i.e.}, not @i{shadowed}_2 by a
  4234. @i{binding} for a @i{variable} or @i{symbol macro} named by
  4235. the same @i{symbol}) is expanded by the normal macro expansion process;
  4236. see @ref{Symbols as Forms}.
  4237. The expansion of a @i{symbol macro} is subject to further @i{macro expansion}
  4238. in the same @i{lexical environment} as the @i{symbol macro} reference,
  4239. exactly analogous to normal @i{macros}.
  4240. The consequences are unspecified if a @b{special} declaration is made for
  4241. @i{symbol} while in the scope of this definition (@i{i.e.}, when it is not
  4242. @i{shadowed}_2 by a @i{binding} for a @i{variable}
  4243. or @i{symbol macro} named by the same @i{symbol}).
  4244. Any use of @b{setq} to set the value of
  4245. the @i{symbol}
  4246. while in the scope of this definition
  4247. is treated as if it were a @b{setf}.
  4248. @b{psetq} of @i{symbol}
  4249. is treated as if it were a @b{psetf}, and
  4250. @b{multiple-value-setq}
  4251. is treated as if it were a @b{setf} of @b{values}.
  4252. A @i{binding} for a @i{symbol macro} can be @i{shadowed}_2
  4253. by @b{let} or @b{symbol-macrolet}.
  4254. @subsubheading Examples::
  4255. @example
  4256. (defvar *things* (list 'alpha 'beta 'gamma)) @result{} *THINGS*
  4257. (define-symbol-macro thing1 (first *things*)) @result{} THING1
  4258. (define-symbol-macro thing2 (second *things*)) @result{} THING2
  4259. (define-symbol-macro thing3 (third *things*)) @result{} THING3
  4260. thing1 @result{} ALPHA
  4261. (setq thing1 'ONE) @result{} ONE
  4262. *things* @result{} (ONE BETA GAMMA)
  4263. (multiple-value-setq (thing2 thing3) (values 'two 'three)) @result{} TWO
  4264. thing3 @result{} THREE
  4265. *things* @result{} (ONE TWO THREE)
  4266. (list thing2 (let ((thing2 2)) thing2)) @result{} (TWO 2)
  4267. @end example
  4268. @subsubheading Exceptional Situations::
  4269. If @i{symbol} is already defined as a @i{global variable},
  4270. an error of @i{type} @b{program-error} is signaled.
  4271. @subsubheading See Also::
  4272. @ref{symbol-macrolet}
  4273. ,
  4274. @ref{macroexpand; macroexpand-1}
  4275. @node symbol-macrolet, *macroexpand-hook*, define-symbol-macro, Evaluation and Compilation Dictionary
  4276. @subsection symbol-macrolet [Special Operator]
  4277. @code{symbol-macrolet} @i{@r{(}@{{(}symbol expansion@r{)}@}{*}@r{)}
  4278. @{@i{declaration}@}{*}
  4279. @{@i{form}@}{*}}@*
  4280. @result{} @i{@{@i{result}@}{*}}
  4281. @subsubheading Arguments and Values::
  4282. @i{symbol}---a @i{symbol}.
  4283. @i{expansion}---a @i{form}.
  4284. @i{declaration}---a @b{declare} @i{expression}; not evaluated.
  4285. @i{forms}---an @i{implicit progn}.
  4286. @i{results}---the @i{values} returned by the @i{forms}.
  4287. @subsubheading Description::
  4288. @b{symbol-macrolet} provides a mechanism for
  4289. affecting the @i{macro expansion} environment for @i{symbols}.
  4290. @b{symbol-macrolet} lexically establishes expansion functions
  4291. for each of the @i{symbol macros} named by @i{symbols}.
  4292. The only guaranteed property of an expansion @i{function} for a @i{symbol macro}
  4293. is that when it is applied to the @i{form} and the @i{environment} it returns
  4294. the correct expansion. (In particular, it is @i{implementation-dependent}
  4295. whether the expansion is conceptually stored in the expansion function,
  4296. the @i{environment}, or both.)
  4297. Each reference to @i{symbol} as a variable within the lexical @i{scope}
  4298. of @b{symbol-macrolet} is expanded by the normal macro expansion process;
  4299. see @ref{Symbols as Forms}.
  4300. The expansion of a symbol macro is subject to further macro expansion
  4301. in the same lexical environment as the symbol macro invocation, exactly
  4302. analogous to normal @i{macros}.
  4303. Exactly the same @i{declarations} are allowed as for @b{let}
  4304. with one exception: @b{symbol-macrolet} signals an error
  4305. if a @b{special} declaration names one of the @i{symbols}
  4306. being defined by @b{symbol-macrolet}.
  4307. When the @i{forms} of the @b{symbol-macrolet} form are expanded,
  4308. any use of @b{setq} to set the value of one of the specified variables
  4309. is treated as if it were a @b{setf}.
  4310. @b{psetq} of a @i{symbol} defined as a symbol macro
  4311. is treated as if it were a @b{psetf}, and
  4312. @b{multiple-value-setq}
  4313. is treated as if it were a @b{setf} of @b{values}.
  4314. The use of @b{symbol-macrolet} can be shadowed by @b{let}.
  4315. In other words, @b{symbol-macrolet} only substitutes for occurrences
  4316. of @i{symbol} that would be in the @i{scope} of a lexical binding of
  4317. @i{symbol} surrounding the @i{forms}.
  4318. @subsubheading Examples::
  4319. @example
  4320. ;;; The following is equivalent to
  4321. ;;; (list 'foo (let ((x 'bar)) x)),
  4322. ;;; not
  4323. ;;; (list 'foo (let (('foo 'bar)) 'foo))
  4324. (symbol-macrolet ((x 'foo))
  4325. (list x (let ((x 'bar)) x)))
  4326. @result{} (foo bar)
  4327. @i{NOT}@result{} (foo foo)
  4328. (symbol-macrolet ((x '(foo x)))
  4329. (list x))
  4330. @result{} ((FOO X))
  4331. @end example
  4332. @subsubheading Exceptional Situations::
  4333. If an attempt is made to bind a @i{symbol} that is defined as a @i{global variable},
  4334. an error of @i{type} @b{program-error} is signaled.
  4335. If @i{declaration} contains a @b{special} declaration
  4336. that names one of the @i{symbols} being bound by @b{symbol-macrolet},
  4337. an error of @i{type} @b{program-error} is signaled.
  4338. @subsubheading See Also::
  4339. @ref{with-slots}
  4340. ,
  4341. @ref{macroexpand; macroexpand-1}
  4342. @subsubheading Notes::
  4343. The special form @b{symbol-macrolet} is the basic mechanism that is used to
  4344. implement @b{with-slots}.
  4345. If a @b{symbol-macrolet} @i{form} is a @i{top level form},
  4346. the @i{forms} are also processed as @i{top level forms}.
  4347. See @ref{File Compilation}.
  4348. @node *macroexpand-hook*, proclaim, symbol-macrolet, Evaluation and Compilation Dictionary
  4349. @subsection *macroexpand-hook* [Variable]
  4350. @subsubheading Value Type::
  4351. a @i{designator} for a @i{function} of three @i{arguments}:
  4352. a @i{macro function},
  4353. a @i{macro form},
  4354. and an @i{environment} @i{object}.
  4355. @subsubheading Initial Value::
  4356. a @i{designator} for a function that is equivalent to the @i{function} @b{funcall},
  4357. but that might have additional @i{implementation-dependent} side-effects.
  4358. @subsubheading Description::
  4359. Used as the expansion interface hook by @b{macroexpand-1} to
  4360. control the @i{macro expansion} process.
  4361. When a @i{macro form} is to be expanded,
  4362. this @i{function} is called with three arguments:
  4363. the @i{macro function},
  4364. the @i{macro form},
  4365. and the @i{environment} in which the @i{macro form} is to be expanded.
  4366. The @i{environment} @i{object} has @i{dynamic extent};
  4367. the consequences are undefined if the @i{environment} @i{object} is
  4368. referred to outside the @i{dynamic extent} of the macro expansion function.
  4369. @subsubheading Examples::
  4370. @example
  4371. (defun hook (expander form env)
  4372. (format t "Now expanding: ~S~
  4373. (funcall expander form env)) @result{} HOOK
  4374. (defmacro machook (x y) `(/ (+ ,x ,y) 2)) @result{} MACHOOK
  4375. (macroexpand '(machook 1 2)) @result{} (/ (+ 1 2) 2), @i{true}
  4376. (let ((*macroexpand-hook* #'hook)) (macroexpand '(machook 1 2)))
  4377. @t{ |> } Now expanding (MACHOOK 1 2)
  4378. @result{} (/ (+ 1 2) 2), @i{true}
  4379. @end example
  4380. @subsubheading See Also::
  4381. @ref{macroexpand; macroexpand-1}
  4382. , @b{macroexpand-1},
  4383. @ref{funcall}
  4384. , @ref{Evaluation}
  4385. @subsubheading Notes::
  4386. The net effect of the chosen initial value is to just invoke the
  4387. @i{macro function}, giving it the @i{macro form} and
  4388. @i{environment} as its two arguments.
  4389. Users or user programs can @i{assign} this @i{variable} to
  4390. customize or trace the @i{macro expansion} mechanism. Note, however,
  4391. that this @i{variable} is a global resource, potentially shared by
  4392. multiple @i{programs}; as such, if any two @i{programs} depend for
  4393. their correctness on the setting of this @i{variable}, those
  4394. @i{programs} may not be able to run in the same @i{Lisp image}.
  4395. For this reason, it is frequently best to confine its uses to debugging
  4396. situations.
  4397. Users who put their own function into @b{*macroexpand-hook*}
  4398. should consider saving the previous value of the hook, and calling that
  4399. value from their own.
  4400. @node proclaim, declaim, *macroexpand-hook*, Evaluation and Compilation Dictionary
  4401. @subsection proclaim [Function]
  4402. @code{proclaim} @i{declaration-specifier} @result{} @i{@i{implementation-dependent}}
  4403. @subsubheading Arguments and Values::
  4404. @i{declaration-specifier}---a @i{declaration specifier}.
  4405. @subsubheading Description::
  4406. @i{Establishes} the @i{declaration} specified by @i{declaration-specifier}
  4407. in the @i{global environment}.
  4408. Such a @i{declaration}, sometimes called a @i{global declaration}
  4409. or a @i{proclamation}, is always in force unless locally @i{shadowed}.
  4410. @i{Names} of @i{variables} and @i{functions} within
  4411. @i{declaration-specifier} refer to @i{dynamic variables}
  4412. and global @i{function} definitions, respectively.
  4413. Figure 3--22 shows a list of @i{declaration identifiers}
  4414. that can be used with @b{proclaim}.
  4415. @group
  4416. @noindent
  4417. @w{ declaration inline optimize type }
  4418. @w{ ftype notinline special }
  4419. @noindent
  4420. @w{ Figure 3--22: Global Declaration Specifiers}
  4421. @end group
  4422. An implementation is free to support other (@i{implementation-defined})
  4423. @i{declaration identifiers} as well.
  4424. @subsubheading Examples::
  4425. @example
  4426. (defun declare-variable-types-globally (type vars)
  4427. (proclaim `(type ,type ,@@vars))
  4428. type)
  4429. ;; Once this form is executed, the dynamic variable *TOLERANCE*
  4430. ;; must always contain a float.
  4431. (declare-variable-types-globally 'float '(*tolerance*))
  4432. @result{} FLOAT
  4433. @end example
  4434. @subsubheading See Also::
  4435. @ref{declaim}
  4436. ,
  4437. @b{declare},
  4438. @ref{Compilation}
  4439. @subsubheading Notes::
  4440. Although the @i{execution} of a @b{proclaim} @i{form}
  4441. has effects that might affect compilation, the compiler does not make
  4442. any attempt to recognize and specially process @b{proclaim} @i{forms}.
  4443. A @i{proclamation} such as the following, even if a @i{top level form},
  4444. does not have any effect until it is executed:
  4445. @example
  4446. (proclaim '(special *x*))
  4447. @end example
  4448. If compile time side effects are desired, @b{eval-when} may be useful.
  4449. For example:
  4450. @example
  4451. (eval-when (:execute :compile-toplevel :load-toplevel)
  4452. (proclaim '(special *x*)))
  4453. @end example
  4454. In most such cases, however, it is preferrable to use @b{declaim} for
  4455. this purpose.
  4456. Since @b{proclaim} @i{forms} are ordinary @i{function forms},
  4457. @i{macro forms} can expand into them.
  4458. @node declaim, declare, proclaim, Evaluation and Compilation Dictionary
  4459. @subsection declaim [Macro]
  4460. @code{declaim} @i{@{@i{declaration-specifier}@}{*}} @result{} @i{@i{implementation-dependent}}
  4461. @subsubheading Arguments and Values::
  4462. @i{declaration-specifier}---a @i{declaration specifier}; not evaluated.
  4463. @subsubheading Description::
  4464. Establishes the @i{declarations} specified by the @i{declaration-specifiers}.
  4465. If a use of this macro appears as a @i{top level form} in a @i{file}
  4466. being processed by the @i{file compiler}, the proclamations are also made
  4467. at compile-time. As with other defining macros, it is unspecified whether or
  4468. not the compile-time side-effects of a @b{declaim} persist after the
  4469. @i{file} has been @i{compiled}.
  4470. @subsubheading Examples::
  4471. @subsubheading See Also::
  4472. @b{declare},
  4473. @ref{proclaim}
  4474. @node declare, ignore, declaim, Evaluation and Compilation Dictionary
  4475. @subsection declare [Symbol]
  4476. @subsubheading Syntax::
  4477. @code{declare} @i{@{@i{declaration-specifier}@}{*}}
  4478. @subsubheading Arguments::
  4479. @i{declaration-specifier}---a @i{declaration specifier}; not evaluated.
  4480. @subsubheading Description::
  4481. A @b{declare} @i{expression}, sometimes called a @i{declaration},
  4482. can occur only at the beginning of the bodies of certain @i{forms};
  4483. that is, it may be preceded only by other @b{declare} @i{expressions},
  4484. or by a @i{documentation string} if the context permits.
  4485. A @b{declare} @i{expression} can occur in a @i{lambda expression}
  4486. or in any of the @i{forms} listed in Figure 3--23.
  4487. @group
  4488. @noindent
  4489. @w{ defgeneric do-external-symbols prog }
  4490. @w{ define-compiler-macro do-symbols prog* }
  4491. @w{ define-method-combination dolist restart-case }
  4492. @w{ define-setf-expander dotimes symbol-macrolet }
  4493. @w{ defmacro flet with-accessors }
  4494. @w{ defmethod handler-case with-hash-table-iterator }
  4495. @w{ defsetf labels with-input-from-string }
  4496. @w{ deftype let with-open-file }
  4497. @w{ defun let* with-open-stream }
  4498. @w{ destructuring-bind locally with-output-to-string }
  4499. @w{ do macrolet with-package-iterator }
  4500. @w{ do* multiple-value-bind with-slots }
  4501. @w{ do-all-symbols pprint-logical-block }
  4502. @noindent
  4503. @w{ Figure 3--23: Standardized Forms In Which Declarations Can Occur }
  4504. @end group
  4505. A @b{declare} @i{expression} can only occur
  4506. where specified by the syntax of these @i{forms}.
  4507. The consequences of attempting to evaluate a @b{declare} @i{expression}
  4508. are undefined. In situations where such @i{expressions} can appear,
  4509. explicit checks are made for their presence and they are never actually evaluated;
  4510. it is for this reason that they
  4511. are called ``@b{declare} @i{expressions}''
  4512. rather than ``@b{declare} @i{forms}.''
  4513. @i{Macro forms} cannot expand into declarations;
  4514. @b{declare} @i{expressions} must appear as actual @i{subexpressions} of
  4515. the @i{form} to which they refer.
  4516. Figure 3--24 shows a list of @i{declaration identifiers}
  4517. that can be used with @b{declare}.
  4518. @group
  4519. @noindent
  4520. @w{ dynamic-extent ignore optimize }
  4521. @w{ ftype inline special }
  4522. @w{ ignorable notinline type }
  4523. @noindent
  4524. @w{ Figure 3--24: Local Declaration Specifiers}
  4525. @end group
  4526. An implementation is free to support other (@i{implementation-defined})
  4527. @i{declaration identifiers} as well.
  4528. @subsubheading Examples::
  4529. @example
  4530. (defun nonsense (k x z)
  4531. (foo z x) ;First call to foo
  4532. (let ((j (foo k x)) ;Second call to foo
  4533. (x (* k k)))
  4534. (declare (inline foo) (special x z))
  4535. (foo x j z))) ;Third call to foo
  4536. @end example
  4537. In this example,
  4538. the @b{inline} declaration applies
  4539. only to the third call to @t{foo}, but not to the first or second ones.
  4540. The @b{special} declaration of @t{x} causes @b{let}
  4541. to make a dynamic @i{binding} for @t{x}, and causes the reference to
  4542. @t{x}
  4543. in the body of @b{let} to be a dynamic reference.
  4544. The reference to @t{x} in the second call to @t{foo} is a local reference
  4545. to the second parameter of @t{nonsense}.
  4546. The reference to @t{x} in the first call to @t{foo} is a local
  4547. reference, not a @b{special} one. The @b{special} declaration of @t{z}
  4548. causes the reference to @t{z} in the
  4549. third
  4550. call
  4551. to @t{foo} to be a dynamic reference; it does not
  4552. refer to the parameter to @t{nonsense} named @t{z}, because that
  4553. parameter @i{binding} has not been declared to be @b{special}.
  4554. (The @b{special} declaration of @t{z} does not appear in the body
  4555. of @b{defun}, but in an inner @i{form}, and therefore does not
  4556. affect the @i{binding} of the @i{parameter}.)
  4557. @subsubheading Exceptional Situations::
  4558. The consequences of trying to use a @b{declare} @i{expression} as
  4559. a @i{form} to be @i{evaluated} are undefined.
  4560. [Editorial Note by KMP: Probably we need to say something here about ill-formed
  4561. declare expressions.]
  4562. @subsubheading See Also::
  4563. @ref{proclaim}
  4564. ,
  4565. @ref{Type Specifiers},
  4566. @b{declaration},
  4567. @b{dynamic-extent},
  4568. @b{ftype},
  4569. @b{ignorable},
  4570. @b{ignore},
  4571. @b{inline},
  4572. @b{notinline},
  4573. @b{optimize},
  4574. @b{type}
  4575. @node ignore, dynamic-extent, declare, Evaluation and Compilation Dictionary
  4576. @subsection ignore, ignorable [Declaration]
  4577. @subsubheading Syntax::
  4578. @t{@r{(}ignore @{@i{var} | @r{(}@b{function} @i{fn}@r{)}@}{*}@r{)}}
  4579. @t{@r{(}ignorable @{@i{var} | @r{(}@b{function} @i{fn}@r{)}@}{*}@r{)}}
  4580. @subsubheading Arguments::
  4581. @i{var}---a @i{variable} @i{name}.
  4582. @i{fn}---a @i{function} @i{name}.
  4583. @subsubheading Valid Context::
  4584. @i{declaration}
  4585. @subsubheading Binding Types Affected::
  4586. @i{variable}, @i{function}
  4587. @subsubheading Description::
  4588. The @b{ignore} and @b{ignorable} declarations
  4589. refer to @i{for-value} @i{references}
  4590. to @i{variable} @i{bindings} for the @i{vars}
  4591. and to @i{function} @i{bindings} for the @i{fns}.
  4592. An @b{ignore} @i{declaration} specifies that
  4593. @i{for-value} @i{references} to the indicated @i{bindings}
  4594. will not
  4595. occur within the scope of the @i{declaration}.
  4596. Within the @i{scope} of such a @i{declaration},
  4597. it is desirable
  4598. for a compiler to issue a warning about
  4599. the presence of
  4600. either a @i{for-value} @i{reference} to any @i{var} or @i{fn},
  4601. or a @b{special} @i{declaration} for any @i{var}.
  4602. An @b{ignorable} @i{declaration} specifies that
  4603. @i{for-value} @i{references} to the indicated @i{bindings}
  4604. might or might not
  4605. occur within the scope of the @i{declaration}.
  4606. Within the @i{scope} of such a @i{declaration},
  4607. it is not desirable
  4608. for a compiler to issue a warning about
  4609. the presence or absence of
  4610. either a @i{for-value} @i{reference} to any @i{var} or @i{fn},
  4611. or a @b{special} @i{declaration} for any @i{var}.
  4612. When not within the @i{scope}
  4613. of a @b{ignore} or @b{ignorable} @i{declaration},
  4614. it is desirable
  4615. for a compiler to issue a warning about
  4616. any @i{var} for which there is
  4617. neither a @i{for-value} @i{reference}
  4618. nor a @b{special} @i{declaration},
  4619. or about
  4620. any @i{fn} for which there is
  4621. no @i{for-value} @i{reference}.
  4622. Any warning about a ``used'' or ``unused'' @i{binding} must be of @i{type} @b{style-warning},
  4623. and may not affect program semantics.
  4624. The @i{stream variables} established by
  4625. @b{with-open-file},
  4626. @b{with-open-stream},
  4627. @b{with-input-from-string},
  4628. and @b{with-output-to-string},
  4629. and all @i{iteration variables} are, by definition, always ``used''.
  4630. Using @t{(declare (ignore @i{v}))},
  4631. for such a @i{variable} @i{v} has unspecified consequences.
  4632. @subsubheading See Also::
  4633. @b{declare}
  4634. @node dynamic-extent, type, ignore, Evaluation and Compilation Dictionary
  4635. @subsection dynamic-extent [Declaration]
  4636. @subsubheading Syntax::
  4637. @t{(dynamic-extent [[@{@i{var}@}{*} |
  4638. @r{(}@b{function} @i{fn}@r{)}@r{*}]])}
  4639. @subsubheading Arguments::
  4640. @i{var}---a @i{variable} @i{name}.
  4641. @i{fn}---a @i{function} @i{name}.
  4642. @subsubheading Valid Context::
  4643. @i{declaration}
  4644. @subsubheading Binding Types Affected::
  4645. @i{variable}, @i{function}
  4646. @subsubheading Description::
  4647. In some containing @i{form}, @i{F}, this declaration
  4648. asserts for each @i{var_i} (which need not be bound by @i{F}),
  4649. and for each @i{value} @i{v_@{ij@}} that @i{var_i} takes on,
  4650. and for each @i{object} @i{x_@{ijk@}} that
  4651. is
  4652. an @i{otherwise inaccessible part} of @i{v_@{ij@}} at any time when
  4653. @i{v_@{ij@}}
  4654. becomes the value of @i{var_i},
  4655. that just after the execution of @i{F} terminates,
  4656. @i{x_@{ijk@}} is either @i{inaccessible}
  4657. (if @i{F} established a @i{binding} for @i{var_i})
  4658. or still an @i{otherwise inaccessible part} of the current value of
  4659. @i{var_i} (if @i{F} did not establish a @i{binding}
  4660. for @i{var_i}).
  4661. The same relation holds for each @i{fn_i},
  4662. except that the @i{bindings} are in the @i{function} @i{namespace}.
  4663. The compiler is permitted to use
  4664. this information in any way that is appropriate to the @i{implementation}
  4665. and that does not conflict with the semantics of @r{Common Lisp}.
  4666. @b{dynamic-extent} declarations can be @i{free declarations}
  4667. or @i{bound declarations}.
  4668. The @i{vars} and @i{fns} named in a @b{dynamic-extent}
  4669. declaration must not refer to @i{symbol macro} or @i{macro} bindings.
  4670. @subsubheading Examples::
  4671. Since stack allocation of the initial value entails knowing at the
  4672. @i{object}'s creation time that the @i{object} can be
  4673. @i{stack-allocated}, it is not generally useful to make a
  4674. @b{dynamic-extent} @i{declaration} for @i{variables}
  4675. which have no lexically apparent initial value.
  4676. For example, it is probably useful to write:
  4677. @example
  4678. (defun f ()
  4679. (let ((x (list 1 2 3)))
  4680. (declare (dynamic-extent x))
  4681. ...))
  4682. @end example
  4683. This would permit those compilers that wish to do so to @i{stack allocate}
  4684. the list held by the local variable @t{x}. It is permissible,
  4685. but in practice probably not as useful, to write:
  4686. @example
  4687. (defun g (x) (declare (dynamic-extent x)) ...)
  4688. (defun f () (g (list 1 2 3)))
  4689. @end example
  4690. Most compilers would probably not @i{stack allocate} the @i{argument}
  4691. to @t{g} in @t{f} because it would be a modularity violation for the compiler
  4692. to assume facts about @t{g} from within @t{f}. Only an implementation that
  4693. was willing to be responsible for recompiling @t{f} if the definition of @t{g}
  4694. changed incompatibly could legitimately @i{stack allocate} the @i{list}
  4695. argument to @t{g} in @t{f}.
  4696. Here is another example:
  4697. @example
  4698. (declaim (inline g))
  4699. (defun g (x) (declare (dynamic-extent x)) ...)
  4700. (defun f () (g (list 1 2 3)))
  4701. (defun f ()
  4702. (flet ((g (x) (declare (dynamic-extent x)) ...))
  4703. (g (list 1 2 3))))
  4704. @end example
  4705. In the previous example, some compilers might determine that optimization was
  4706. possible and others might not.
  4707. A variant of this is the so-called ``stack allocated rest list''
  4708. that can be achieved (in implementations supporting the optimization) by:
  4709. @example
  4710. (defun f (&rest x)
  4711. (declare (dynamic-extent x))
  4712. ...)
  4713. @end example
  4714. Note that although the initial value of @t{x} is not explicit, the @t{f}
  4715. function is responsible for assembling the list @t{x} from the passed arguments,
  4716. so the @t{f} function can be optimized by the compiler to construct a
  4717. @i{stack-allocated} list instead of a heap-allocated list in implementations
  4718. that support such.
  4719. In the following example,
  4720. @example
  4721. (let ((x (list 'a1 'b1 'c1))
  4722. (y (cons 'a2 (cons 'b2 (cons 'c2 nil)))))
  4723. (declare (dynamic-extent x y))
  4724. ...)
  4725. @end example
  4726. The @i{otherwise inaccessible parts} of @t{x} are three
  4727. @i{conses}, and the @i{otherwise inaccessible parts}
  4728. of @t{y} are three other @i{conses}.
  4729. None of the symbols @t{a1}, @t{b1}, @t{c1}, @t{a2},
  4730. @t{b2}, @t{c2}, or @b{nil} is an
  4731. @i{otherwise inaccessible part} of @t{x} or @t{y} because each
  4732. is @i{interned} and hence @i{accessible} by the @i{package}
  4733. (or @i{packages}) in which it is @i{interned}.
  4734. However, if a freshly allocated @i{uninterned} @i{symbol} had
  4735. been used, it would have been an @i{otherwise inaccessible part} of
  4736. the @i{list} which contained it.
  4737. @example
  4738. ;; In this example, the implementation is permitted to @i{stack allocate}
  4739. ;; the list that is bound to X.
  4740. (let ((x (list 1 2 3)))
  4741. (declare (dynamic-extent x))
  4742. (print x)
  4743. :done)
  4744. @t{ |> } (1 2 3)
  4745. @result{} :DONE
  4746. ;; In this example, the list to be bound to L can be @i{stack-allocated}.
  4747. (defun zap (x y z)
  4748. (do ((l (list x y z) (cdr l)))
  4749. ((null l))
  4750. (declare (dynamic-extent l))
  4751. (prin1 (car l)))) @result{} ZAP
  4752. (zap 1 2 3)
  4753. @t{ |> } 123
  4754. @result{} NIL
  4755. ;; Some implementations might open-code LIST-ALL-PACKAGES in a way
  4756. ;; that permits using @i{stack allocation} of the list to be bound to L.
  4757. (do ((l (list-all-packages) (cdr l)))
  4758. ((null l))
  4759. (declare (dynamic-extent l))
  4760. (let ((name (package-name (car l))))
  4761. (when (string-search "COMMON-LISP" name) (print name))))
  4762. @t{ |> } "COMMON-LISP"
  4763. @t{ |> } "COMMON-LISP-USER"
  4764. @result{} NIL
  4765. ;; Some implementations might have the ability to @i{stack allocate}
  4766. ;; rest lists. A declaration such as the following should be a cue
  4767. ;; to such implementations that stack-allocation of the rest list
  4768. ;; would be desirable.
  4769. (defun add (&rest x)
  4770. (declare (dynamic-extent x))
  4771. (apply #'+ x)) @result{} ADD
  4772. (add 1 2 3) @result{} 6
  4773. (defun zap (n m)
  4774. ;; Computes (RANDOM (+ M 1)) at relative speed of roughly O(N).
  4775. ;; It may be slow, but with a good compiler at least it
  4776. ;; doesn't waste much heap storage. :-@}
  4777. (let ((a (make-array n)))
  4778. (declare (dynamic-extent a))
  4779. (dotimes (i n)
  4780. (declare (dynamic-extent i))
  4781. (setf (aref a i) (random (+ i 1))))
  4782. (aref a m))) @result{} ZAP
  4783. (< (zap 5 3) 3) @result{} @i{true}
  4784. @end example
  4785. The following are in error, since the value of @t{x} is used outside of its
  4786. @i{extent}:
  4787. @example
  4788. (length (list (let ((x (list 1 2 3))) ; Invalid
  4789. (declare (dynamic-extent x))
  4790. x)))
  4791. (progn (let ((x (list 1 2 3))) ; Invalid
  4792. (declare (dynamic-extent x))
  4793. x)
  4794. nil)
  4795. @end example
  4796. @subsubheading See Also::
  4797. @b{declare}
  4798. @subsubheading Notes::
  4799. The most common optimization is to @i{stack allocate} the
  4800. initial value of the @i{objects} named by the @i{vars}.
  4801. It is permissible for an implementation to simply ignore this declaration.
  4802. @node type, inline, dynamic-extent, Evaluation and Compilation Dictionary
  4803. @subsection type [Declaration]
  4804. @subsubheading Syntax::
  4805. @t{(type @i{typespec} @{@i{var}@}{*})}
  4806. @t{(@i{typespec} @{@i{var}@}{*})}
  4807. @subsubheading Arguments::
  4808. @i{typespec}---a @i{type specifier}.
  4809. @i{var}---a @i{variable} @i{name}.
  4810. @subsubheading Valid Context::
  4811. @i{declaration} or @i{proclamation}
  4812. @subsubheading Binding Types Affected::
  4813. @i{variable}
  4814. @subsubheading Description::
  4815. Affects
  4816. only variable @i{bindings} and specifies that the
  4817. @i{vars} take on
  4818. values only of the specified @i{typespec}.
  4819. In particular, values assigned to the variables by @b{setq},
  4820. as well as the initial values of the @i{vars} must be of
  4821. the specified @i{typespec}.
  4822. @b{type} declarations never apply to function @i{bindings} (see @b{ftype}).
  4823. A type declaration of a @i{symbol}
  4824. defined by @b{symbol-macrolet} is equivalent
  4825. to wrapping a @b{the}
  4826. expression around the expansion of that @i{symbol},
  4827. although the @i{symbol}'s @i{macro expansion} is not actually affected.
  4828. The meaning of a type declaration
  4829. is equivalent to changing each reference to
  4830. a variable (@i{var}) within the scope of the
  4831. declaration to @t{(the @i{typespec} @i{var})},
  4832. changing each expression assigned to the
  4833. variable (@i{new-value}) within the scope of the declaration to
  4834. @t{(the @i{typespec} @i{new-value})},
  4835. and executing
  4836. @t{(the @i{typespec} @i{var})} at the moment the scope of the declaration
  4837. is entered.
  4838. A @i{type} declaration is valid in all declarations. The interpretation
  4839. of a type declaration is as follows:
  4840. @table @asis
  4841. @item 1.
  4842. During the execution of any reference to the
  4843. declared variable within the scope of the declaration, the consequences
  4844. are
  4845. undefined
  4846. if
  4847. the value of the declared variable is not of the declared @i{type}.
  4848. @item 2.
  4849. During the execution of any
  4850. @b{setq} of the declared variable within the scope
  4851. of the declaration, the consequences are
  4852. undefined
  4853. if the newly assigned value of the
  4854. declared variable is not of the declared @i{type}.
  4855. @item 3.
  4856. At the moment the
  4857. scope of the declaration is entered, the consequences are
  4858. undefined
  4859. if the value of the
  4860. declared variable is not of the declared @i{type}.
  4861. @end table
  4862. A @i{type} declaration affects only variable references within
  4863. its scope.
  4864. If nested @i{type} declarations refer to the same variable,
  4865. then the value of the variable must be a member of the intersection of
  4866. the declared @i{types}.
  4867. If there is a local @t{type} declaration for a dynamic
  4868. variable, and there is also a global @t{type} proclamation for that same
  4869. variable, then the value of the variable within the scope of the local
  4870. declaration must be a member of the intersection of the two declared
  4871. @i{types}.
  4872. @b{type} declarations can be @i{free declarations}
  4873. or @i{bound declarations}.
  4874. A @i{symbol} cannot be both the name of a @i{type} and the name of a
  4875. declaration. Defining a @i{symbol} as the @i{name} of a @i{class},
  4876. @i{structure}, @i{condition}, or @i{type}, when the @i{symbol}
  4877. has been @i{declared} as a declaration name, or vice versa, signals an error.
  4878. Within the @i{lexical scope} of an @b{array} type declaration,
  4879. all references to @i{array} @i{elements} are assumed to satisfy the
  4880. @i{expressed array element type} (as opposed to the @i{upgraded array element type}).
  4881. A compiler can treat
  4882. the code within the scope of the @b{array} type declaration as if each
  4883. @i{access} of an @i{array} @i{element} were surrounded by an appropriate
  4884. @b{the} form.
  4885. @subsubheading Examples::
  4886. @example
  4887. (defun f (x y)
  4888. (declare (type fixnum x y))
  4889. (let ((z (+ x y)))
  4890. (declare (type fixnum z))
  4891. z)) @result{} F
  4892. (f 1 2) @result{} 3
  4893. ;; The previous definition of F is equivalent to
  4894. (defun f (x y)
  4895. ;; This declaration is a shorthand form of the TYPE declaration
  4896. (declare (fixnum x y))
  4897. ;; To declare the type of a return value, it's not necessary to
  4898. ;; create a named variable. A THE special form can be used instead.
  4899. (the fixnum (+ x y))) @result{} F
  4900. (f 1 2) @result{} 3
  4901. @end example
  4902. @example
  4903. (defvar *one-array* (make-array 10 :element-type '(signed-byte 5)))
  4904. (defvar *another-array* (make-array 10 :element-type '(signed-byte 8)))
  4905. (defun frob (an-array)
  4906. (declare (type (array (signed-byte 5) 1) an-array))
  4907. (setf (aref an-array 1) 31)
  4908. (setf (aref an-array 2) 127)
  4909. (setf (aref an-array 3) (* 2 (aref an-array 3)))
  4910. (let ((foo 0))
  4911. (declare (type (signed-byte 5) foo))
  4912. (setf foo (aref an-array 0))))
  4913. (frob *one-array*)
  4914. (frob *another-array*)
  4915. @end example
  4916. The above definition of @t{frob} is equivalent to:
  4917. @example
  4918. (defun frob (an-array)
  4919. (setf (the (signed-byte 5) (aref an-array 1)) 31)
  4920. (setf (the (signed-byte 5) (aref an-array 2)) 127)
  4921. (setf (the (signed-byte 5) (aref an-array 3))
  4922. (* 2 (the (signed-byte 5) (aref an-array 3))))
  4923. (let ((foo 0))
  4924. (declare (type (signed-byte 5) foo))
  4925. (setf foo (the (signed-byte 5) (aref an-array 0)))))
  4926. @end example
  4927. Given an implementation in which
  4928. @i{fixnums} are 29 bits but @b{fixnum} @i{arrays}
  4929. are upgraded to signed 32-bit @i{arrays},
  4930. the following
  4931. could be compiled with all @i{fixnum} arithmetic:
  4932. @example
  4933. (defun bump-counters (counters)
  4934. (declare (type (array fixnum *) bump-counters))
  4935. (dotimes (i (length counters))
  4936. (incf (aref counters i))))
  4937. @end example
  4938. @subsubheading See Also::
  4939. @b{declare},
  4940. @ref{declaim}
  4941. ,
  4942. @ref{proclaim}
  4943. @subsubheading Notes::
  4944. @t{(@i{typespec} @{@i{var}@}{*})}
  4945. is an abbreviation for @t{(type @i{typespec} @{@i{var}@}{*})}.
  4946. A @b{type} declaration for the arguments to a function does not
  4947. necessarily imply anything about the type of the result. The following
  4948. function is not permitted to be compiled using @i{implementation-dependent}
  4949. @i{fixnum}-only arithmetic:
  4950. @example
  4951. (defun f (x y) (declare (fixnum x y)) (+ x y))
  4952. @end example
  4953. To see why, consider @t{(f most-positive-fixnum 1)}.
  4954. Common Lisp defines that @t{F} must return a @i{bignum} here, rather
  4955. than signal an error or produce a mathematically incorrect result.
  4956. If you have special knowledge such ``@i{fixnum} overflow'' cases will
  4957. not come up, you can declare the result value to be in the @i{fixnum}
  4958. range, enabling some compilers to use more efficient arithmetic:
  4959. @example
  4960. (defun f (x y)
  4961. (declare (fixnum x y))
  4962. (the fixnum (+ x y)))
  4963. @end example
  4964. Note, however, that in the three-argument case, because of the possibility
  4965. of an implicit intermediate value growing too large, the following will not
  4966. cause @i{implementation-dependent} @i{fixnum}-only arithmetic to be used:
  4967. @example
  4968. (defun f (x y)
  4969. (declare (fixnum x y z))
  4970. (the fixnum (+ x y z)))
  4971. @end example
  4972. To see why, consider @t{(f most-positive-fixnum 1 -1).}
  4973. Although the arguments and the result are all @i{fixnums}, an intermediate
  4974. value is not a @i{fixnum}. If it is important that
  4975. @i{implementation-dependent} @i{fixnum}-only arithmetic be selected
  4976. in @i{implementations} that provide it,
  4977. consider writing something like this instead:
  4978. @example
  4979. (defun f (x y)
  4980. (declare (fixnum x y z))
  4981. (the fixnum (+ (the fixnum (+ x y)) z)))
  4982. @end example
  4983. @node inline, ftype, type, Evaluation and Compilation Dictionary
  4984. @subsection inline, notinline [Declaration]
  4985. @subsubheading Syntax::
  4986. @t{(inline @{@i{function-name}@}{*})}
  4987. @t{(notinline @{@i{function-name}@}{*})}
  4988. @subsubheading Arguments::
  4989. @i{function-name}---a @i{function name}.
  4990. @subsubheading Valid Context::
  4991. @i{declaration} or @i{proclamation}
  4992. @subsubheading Binding Types Affected::
  4993. @i{function}
  4994. @subsubheading Description::
  4995. @b{inline} specifies that
  4996. it is desirable for the compiler to produce inline calls
  4997. to the @i{functions} named by @i{function-names};
  4998. that is, the code for a specified @i{function-name}
  4999. should be integrated into the calling routine, appearing ``in line''
  5000. in place of a procedure call.
  5001. A compiler is free to ignore this declaration.
  5002. @b{inline} declarations never apply to variable @i{bindings}.
  5003. If one of the @i{functions} mentioned has a lexically apparent local definition
  5004. (as made by @b{flet} or @b{labels}), then the declaration
  5005. applies to that local definition and not to the global function definition.
  5006. While no @i{conforming implementation} is required to perform inline expansion
  5007. of user-defined functions, those @i{implementations} that do attempt
  5008. to recognize the following paradigm:
  5009. To define a @i{function} @t{f} that is not @b{inline} by default
  5010. but for which @t{(declare (inline f))} will make @i{f} be locally inlined,
  5011. the proper definition sequence is:
  5012. @example
  5013. (declaim (inline f))
  5014. (defun f ...)
  5015. (declaim (notinline f))
  5016. @end example
  5017. The @b{inline} proclamation preceding the @b{defun} @i{form}
  5018. ensures that the @i{compiler} has the opportunity save the information
  5019. necessary for inline expansion, and the @b{notinline} proclamation
  5020. following the @b{defun} @i{form} prevents @t{f} from being expanded
  5021. inline everywhere.
  5022. @b{notinline} specifies that it is
  5023. undesirable to compile the @i{functions}
  5024. named by @i{function-names} in-line.
  5025. A compiler is not free to ignore this declaration;
  5026. calls to the specified functions must be implemented as out-of-line subroutine calls.
  5027. If one of the @i{functions}
  5028. mentioned has a lexically apparent local definition
  5029. (as made by @b{flet} or @b{labels}), then the declaration
  5030. applies to that local definition and not to the global function definition.
  5031. In the presence of a @i{compiler macro} definition for
  5032. @i{function-name}, a @b{notinline} declaration prevents that
  5033. @i{compiler macro} from being used.
  5034. An @b{inline} declaration may be used to encourage use of
  5035. @i{compiler macro} definitions. @b{inline} and @b{notinline}
  5036. declarations otherwise have no effect when the lexically visible definition
  5037. of @i{function-name} is a @i{macro} definition.
  5038. @b{inline} and @b{notinline} declarations can be @i{free declarations} or
  5039. @i{bound declarations}.
  5040. @b{inline} and @b{notinline} declarations of functions that
  5041. appear before the body of a
  5042. @b{flet}
  5043. or @b{labels}
  5044. @i{form} that defines that function are @i{bound declarations}.
  5045. Such declarations in other contexts are @i{free declarations}.
  5046. @subsubheading Examples::
  5047. @example
  5048. ;; The globally defined function DISPATCH should be open-coded,
  5049. ;; if the implementation supports inlining, unless a NOTINLINE
  5050. ;; declaration overrides this effect.
  5051. (declaim (inline dispatch))
  5052. (defun dispatch (x) (funcall (get (car x) 'dispatch) x))
  5053. ;; Here is an example where inlining would be encouraged.
  5054. (defun top-level-1 () (dispatch (read-command)))
  5055. ;; Here is an example where inlining would be prohibited.
  5056. (defun top-level-2 ()
  5057. (declare (notinline dispatch))
  5058. (dispatch (read-command)))
  5059. ;; Here is an example where inlining would be prohibited.
  5060. (declaim (notinline dispatch))
  5061. (defun top-level-3 () (dispatch (read-command)))
  5062. ;; Here is an example where inlining would be encouraged.
  5063. (defun top-level-4 ()
  5064. (declare (inline dispatch))
  5065. (dispatch (read-command)))
  5066. @end example
  5067. @subsubheading See Also::
  5068. @b{declare},
  5069. @ref{declaim}
  5070. ,
  5071. @ref{proclaim}
  5072. @node ftype, declaration, inline, Evaluation and Compilation Dictionary
  5073. @subsection ftype [Declaration]
  5074. @subsubheading Syntax::
  5075. @t{(ftype @i{type} @{@i{function-name}@}{*})}
  5076. @subsubheading Arguments::
  5077. @i{function-name}---a @i{function name}.
  5078. @i{type}---a @i{type specifier}.
  5079. @subsubheading Valid Context::
  5080. @i{declaration} or @i{proclamation}
  5081. @subsubheading Binding Types Affected::
  5082. @i{function}
  5083. @subsubheading Description::
  5084. Specifies that the @i{functions} named by @i{function-names} are of
  5085. the functional type @i{type}.
  5086. For example:
  5087. @example
  5088. (declare (ftype (function (integer list) t) ith)
  5089. (ftype (function (number) float) sine cosine))
  5090. @end example
  5091. If one of the @i{functions} mentioned has a lexically apparent local definition
  5092. (as made by @b{flet} or @b{labels}), then the declaration
  5093. applies to that local definition and not to the global function definition.
  5094. @b{ftype} declarations never apply to variable @i{bindings} (see @t{type}).
  5095. The lexically apparent bindings of @i{function-names} must not be
  5096. @i{macro} definitions. (This is because @b{ftype} declares the
  5097. functional definition of each @i{function name} to be of a particular
  5098. subtype of @b{function}, and @i{macros} do not denote
  5099. @i{functions}.)
  5100. @b{ftype}
  5101. declarations
  5102. can be @i{free declarations} or @i{bound declarations}.
  5103. @b{ftype} declarations of functions that appear before the body of a
  5104. @b{flet}
  5105. or @b{labels}
  5106. @i{form} that defines that function are @i{bound declarations}.
  5107. Such declarations in other contexts are @i{free declarations}.
  5108. @subsubheading See Also::
  5109. @b{declare},
  5110. @ref{declaim}
  5111. ,
  5112. @ref{proclaim}
  5113. @node declaration, optimize, ftype, Evaluation and Compilation Dictionary
  5114. @subsection declaration [Declaration]
  5115. @subsubheading Syntax::
  5116. @t{(declaration @{@i{name}@}{*})}
  5117. @subsubheading Arguments::
  5118. @i{name}---a @i{symbol}.
  5119. @subsubheading Valid Context::
  5120. @i{proclamation} only
  5121. @subsubheading Description::
  5122. Advises the compiler that each @i{name} is a valid but potentially
  5123. non-standard declaration name. The purpose of this is to tell one
  5124. compiler not to issue warnings for declarations meant for another
  5125. compiler or other program processor.
  5126. @subsubheading Examples::
  5127. @example
  5128. (declaim (declaration author target-language target-machine))
  5129. (declaim (target-language ada))
  5130. (declaim (target-machine IBM-650))
  5131. (defun strangep (x)
  5132. (declare (author "Harry Tweeker"))
  5133. (member x '(strange weird odd peculiar)))
  5134. @end example
  5135. @subsubheading See Also::
  5136. @ref{declaim}
  5137. ,
  5138. @ref{proclaim}
  5139. @node optimize, special, declaration, Evaluation and Compilation Dictionary
  5140. @subsection optimize [Declaration]
  5141. @subsubheading Syntax::
  5142. @t{(optimize @{@i{quality} | (@i{quality} @i{value})@}{*})}
  5143. @IRindex{compilation-speed}
  5144. @IRindex{debug}
  5145. @IRindex{safety}
  5146. @IRindex{space}
  5147. @IRindex{speed}
  5148. @subsubheading Arguments::
  5149. @i{quality}---an @i{optimize quality}.
  5150. @i{value}---one of the @i{integers} @t{0}, @t{1}, @t{2}, or @t{3}.
  5151. @subsubheading Valid Context::
  5152. @i{declaration} or @i{proclamation}
  5153. @subsubheading Description::
  5154. Advises the compiler that each @i{quality} should be given attention
  5155. according to the specified corresponding @i{value}.
  5156. Each @i{quality} must be a @i{symbol} naming an @i{optimize quality};
  5157. the names and meanings of the standard @i{optimize qualities} are shown in
  5158. Figure 3--25.
  5159. @group
  5160. @noindent
  5161. @w{ Name Meaning }
  5162. @w{ @b{compilation-speed} speed of the compilation process }
  5163. @w{ @b{debug} ease of debugging }
  5164. @w{ @b{safety} run-time error checking }
  5165. @w{ @b{space} both code size and run-time space }
  5166. @w{ @b{speed} speed of the object code }
  5167. @noindent
  5168. @w{ Figure 3--25: Optimize qualities }
  5169. @end group
  5170. There may be other, @i{implementation-defined} @i{optimize qualities}.
  5171. A @i{value} @t{0} means that the corresponding @i{quality} is totally
  5172. unimportant, and @t{3} that the @i{quality} is extremely important;
  5173. @t{1} and @t{2} are intermediate values, with @t{1} the
  5174. neutral value.
  5175. @t{(@i{quality} 3)} can be abbreviated to @i{quality}.
  5176. Note that @i{code} which has the optimization @t{(safety 3)},
  5177. or just @b{safety},
  5178. is called @i{safe} @i{code}.
  5179. The consequences are unspecified if a @i{quality} appears more than once
  5180. with @i{different} @i{values}.
  5181. @subsubheading Examples::
  5182. @example
  5183. (defun often-used-subroutine (x y)
  5184. (declare (optimize (safety 2)))
  5185. (error-check x y)
  5186. (hairy-setup x)
  5187. (do ((i 0 (+ i 1))
  5188. (z x (cdr z)))
  5189. ((null z))
  5190. ;; This inner loop really needs to burn.
  5191. (declare (optimize speed))
  5192. (declare (fixnum i))
  5193. ))
  5194. @end example
  5195. @subsubheading See Also::
  5196. @b{declare},
  5197. @ref{declaim}
  5198. ,
  5199. @ref{proclaim}
  5200. ,
  5201. @ref{Declaration Scope}
  5202. @subsubheading Notes::
  5203. An @b{optimize} declaration never applies to either a @i{variable} or
  5204. a @i{function} @i{binding}. An @b{optimize} declaration can only
  5205. be a @i{free declaration}. For more information, see @ref{Declaration Scope}.
  5206. @node special, locally, optimize, Evaluation and Compilation Dictionary
  5207. @subsection special [Declaration]
  5208. @subsubheading Syntax::
  5209. @t{(special @{@i{var}@}{*})}
  5210. @subsubheading Arguments::
  5211. @i{var}---a @i{symbol}.
  5212. @subsubheading Valid Context::
  5213. @i{declaration} or @i{proclamation}
  5214. @subsubheading Binding Types Affected::
  5215. @i{variable}
  5216. @subsubheading Description::
  5217. Specifies that all of
  5218. the @i{vars} named are dynamic.
  5219. This specifier affects variable @i{bindings} and
  5220. affects references.
  5221. All variable @i{bindings} affected are made to be dynamic @i{bindings},
  5222. and affected variable references refer to the current dynamic
  5223. @i{binding}.
  5224. For example:
  5225. @example
  5226. (defun hack (thing *mod*) ;The binding of the parameter
  5227. (declare (special *mod*)) ; *mod* is visible to hack1,
  5228. (hack1 (car thing))) ; but not that of thing.
  5229. (defun hack1 (arg)
  5230. (declare (special *mod*)) ;Declare references to *mod*
  5231. ;within hack1 to be special.
  5232. (if (atom arg) *mod*
  5233. (cons (hack1 (car arg)) (hack1 (cdr arg)))))
  5234. @end example
  5235. A @b{special} declaration does not affect inner @i{bindings}
  5236. of a @i{var}; the inner @i{bindings} implicitly shadow
  5237. a @b{special} declaration and must be explicitly re-declared to
  5238. be @b{special}.
  5239. @b{special} declarations never apply to function @i{bindings}.
  5240. @b{special} declarations can be either @i{bound declarations},
  5241. affecting both a binding and references, or @i{free declarations},
  5242. affecting only references, depending on whether the declaration is
  5243. attached to a variable binding.
  5244. When used in a @i{proclamation}, a @b{special}
  5245. @i{declaration specifier}
  5246. applies to all @i{bindings} as well as to all references of the
  5247. mentioned variables. For example, after
  5248. @example
  5249. (declaim (special x))
  5250. @end example
  5251. then in a function definition such as
  5252. @example
  5253. (defun example (x) ...)
  5254. @end example
  5255. the parameter @t{x} is bound as a dynamic variable
  5256. rather than as a lexical variable.
  5257. @subsubheading Examples::
  5258. @example
  5259. (defun declare-eg (y) ;this y is special
  5260. (declare (special y))
  5261. (let ((y t)) ;this y is lexical
  5262. (list y
  5263. (locally (declare (special y)) y)))) ;this y refers to the
  5264. ;special binding of y
  5265. @result{} DECLARE-EG
  5266. (declare-eg nil) @result{} (T NIL)
  5267. @end example
  5268. @example
  5269. (setf (symbol-value 'x) 6)
  5270. (defun foo (x) ;a lexical binding of x
  5271. (print x)
  5272. (let ((x (1+ x))) ;a special binding of x
  5273. (declare (special x)) ;and a lexical reference
  5274. (bar))
  5275. (1+ x))
  5276. (defun bar ()
  5277. (print (locally (declare (special x))
  5278. x)))
  5279. (foo 10)
  5280. @t{ |> } 10
  5281. @t{ |> } 11
  5282. @result{} 11
  5283. @end example
  5284. @example
  5285. (setf (symbol-value 'x) 6)
  5286. (defun bar (x y) ;[1] 1st occurrence of x
  5287. (let ((old-x x) ;[2] 2nd occurrence of x -- same as 1st occurrence
  5288. (x y)) ;[3] 3rd occurrence of x
  5289. (declare (special x))
  5290. (list old-x x)))
  5291. (bar 'first 'second) @result{} (FIRST SECOND)
  5292. @end example
  5293. @example
  5294. (defun few (x &optional (y *foo*))
  5295. (declare (special *foo*))
  5296. ...)
  5297. @end example
  5298. The reference to @t{*foo*}
  5299. in the first line of this example is not @b{special}
  5300. even though there is a @b{special} declaration in the second line.
  5301. @example
  5302. (declaim (special prosp)) @result{} @i{implementation-dependent}
  5303. (setq prosp 1 reg 1) @result{} 1
  5304. (let ((prosp 2) (reg 2)) ;the binding of prosp is special
  5305. (set 'prosp 3) (set 'reg 3) ;due to the preceding proclamation,
  5306. (list prosp reg)) ;whereas the variable reg is lexical
  5307. @result{} (3 2)
  5308. (list prosp reg) @result{} (1 3)
  5309. (declaim (special x)) ;x is always special.
  5310. (defun example (x y)
  5311. (declare (special y))
  5312. (let ((y 3) (x (* x 2)))
  5313. (print (+ y (locally (declare (special y)) y)))
  5314. (let ((y 4)) (declare (special y)) (foo x)))) @result{} EXAMPLE
  5315. @end example
  5316. In the contorted code above, the outermost and innermost @i{bindings} of
  5317. @t{y} are dynamic,
  5318. but the middle
  5319. binding is lexical. The two arguments to @t{+} are different,
  5320. one being the value, which is @t{3}, of the lexical variable
  5321. @t{y}, and the other being the value of the dynamic variable named @t{y}
  5322. (a @i{binding}
  5323. of which happens, coincidentally, to lexically surround it at
  5324. an outer level). All the @i{bindings}
  5325. of @t{x} and references to @t{x}
  5326. are dynamic, however, because of the proclamation that @t{x} is
  5327. always @b{special}.
  5328. @subsubheading See Also::
  5329. @ref{defparameter; defvar}
  5330. ,
  5331. @b{defvar}
  5332. @node locally, the, special, Evaluation and Compilation Dictionary
  5333. @subsection locally [Special Operator]
  5334. @code{locally} @i{@{@i{declaration}@}{*} @{@i{form}@}{*}} @result{} @i{@{@i{result}@}{*}}
  5335. @subsubheading Arguments and Values::
  5336. @i{Declaration}---a @b{declare} @i{expression}; not evaluated.
  5337. @i{forms}---an @i{implicit progn}.
  5338. @i{results}---the @i{values} of the @i{forms}.
  5339. @subsubheading Description::
  5340. Sequentially evaluates a body of @i{forms}
  5341. in a @i{lexical environment} where the given @i{declarations} have effect.
  5342. @subsubheading Examples::
  5343. @example
  5344. (defun sample-function (y) ;this y is regarded as special
  5345. (declare (special y))
  5346. (let ((y t)) ;this y is regarded as lexical
  5347. (list y
  5348. (locally (declare (special y))
  5349. ;; this next y is regarded as special
  5350. y))))
  5351. @result{} SAMPLE-FUNCTION
  5352. (sample-function nil) @result{} (T NIL)
  5353. (setq x '(1 2 3) y '(4 . 5)) @result{} (4 . 5)
  5354. ;;; The following declarations are not notably useful in specific.
  5355. ;;; They just offer a sample of valid declaration syntax using LOCALLY.
  5356. (locally (declare (inline floor) (notinline car cdr))
  5357. (declare (optimize space))
  5358. (floor (car x) (cdr y))) @result{} 0, 1
  5359. @end example
  5360. @example
  5361. ;;; This example shows a definition of a function that has a particular set
  5362. ;;; of OPTIMIZE settings made locally to that definition.
  5363. (locally (declare (optimize (safety 3) (space 3) (speed 0)))
  5364. (defun frob (w x y &optional (z (foo x y)))
  5365. (mumble x y z w)))
  5366. @result{} FROB
  5367. ;;; This is like the previous example, except that the optimize settings
  5368. ;;; remain in effect for subsequent definitions in the same compilation unit.
  5369. (declaim (optimize (safety 3) (space 3) (speed 0)))
  5370. (defun frob (w x y &optional (z (foo x y)))
  5371. (mumble x y z w))
  5372. @result{} FROB
  5373. @end example
  5374. @subsubheading See Also::
  5375. @b{declare}
  5376. @subsubheading Notes::
  5377. The @b{special} declaration may be used with @b{locally}
  5378. to affect references to, rather than @i{bindings} of, @i{variables}.
  5379. If a @b{locally} @i{form} is a @i{top level form}, the body @i{forms}
  5380. are also processed as @i{top level forms}. See @ref{File Compilation}.
  5381. @node the, special-operator-p, locally, Evaluation and Compilation Dictionary
  5382. @subsection the [Special Operator]
  5383. @code{the} @i{value-type form} @result{} @i{@{@i{result}@}{*}}
  5384. @subsubheading Arguments and Values::
  5385. @i{value-type}---a @i{type specifier}; not evaluated.
  5386. @i{form}---a @i{form}; evaluated.
  5387. @i{results}---the @i{values} resulting from the @i{evaluation} of @i{form}.
  5388. These @i{values} must conform to the @i{type} supplied by @i{value-type};
  5389. see below.
  5390. @subsubheading Description::
  5391. @b{the} specifies that the @i{values}_@{1a@} returned by @i{form}
  5392. are of the @i{types} specified by @i{value-type}.
  5393. The consequences are undefined if any @i{result}
  5394. is not of the declared type.
  5395. It is permissible for @i{form} to @i{yield} a different number of @i{values}
  5396. than are specified by @i{value-type}, provided that the values
  5397. for which @i{types} are declared are indeed of those @i{types}.
  5398. Missing values are treated as @b{nil} for the purposes of checking their @i{types}.
  5399. Regardless of number of @i{values} declared by @i{value-type},
  5400. the number of @i{values} returned by the @b{the} @i{special form} is the same as
  5401. the number of @i{values} returned by @i{form}.
  5402. @subsubheading Examples::
  5403. @example
  5404. (the symbol (car (list (gensym)))) @result{} #:G9876
  5405. (the fixnum (+ 5 7)) @result{} 12
  5406. (the (values) (truncate 3.2 2)) @result{} 1, 1.2
  5407. (the integer (truncate 3.2 2)) @result{} 1, 1.2
  5408. (the (values integer) (truncate 3.2 2)) @result{} 1, 1.2
  5409. (the (values integer float) (truncate 3.2 2)) @result{} 1, 1.2
  5410. (the (values integer float symbol) (truncate 3.2 2)) @result{} 1, 1.2
  5411. (the (values integer float symbol t null list)
  5412. (truncate 3.2 2)) @result{} 1, 1.2
  5413. (let ((i 100))
  5414. (declare (fixnum i))
  5415. (the fixnum (1+ i))) @result{} 101
  5416. (let* ((x (list 'a 'b 'c))
  5417. (y 5))
  5418. (setf (the fixnum (car x)) y)
  5419. x) @result{} (5 B C)
  5420. @end example
  5421. @subsubheading Exceptional Situations::
  5422. The consequences are undefined if
  5423. the @i{values} @i{yielded} by the @i{form}
  5424. are not of the @i{type} specified by @i{value-type}.
  5425. @subsubheading See Also::
  5426. @b{values}
  5427. @subsubheading Notes::
  5428. The @b{values} @i{type specifier} can be used to indicate the types
  5429. of @i{multiple values}:
  5430. @example
  5431. (the (values integer integer) (floor x y))
  5432. (the (values string t)
  5433. (gethash the-key the-string-table))
  5434. @end example
  5435. @b{setf} can be used with @b{the} type declarations.
  5436. In this case the declaration is transferred to the form that
  5437. specifies the new value. The resulting @b{setf} @i{form}
  5438. is then analyzed.
  5439. @node special-operator-p, constantp, the, Evaluation and Compilation Dictionary
  5440. @subsection special-operator-p [Function]
  5441. @code{special-operator-p} @i{symbol} @result{} @i{generalized-boolean}
  5442. @subsubheading Arguments and Values::
  5443. @i{symbol}---a @i{symbol}.
  5444. @i{generalized-boolean}---a @i{generalized boolean}.
  5445. @subsubheading Description::
  5446. Returns @i{true} if @i{symbol} is a @i{special operator};
  5447. otherwise, returns @i{false}.
  5448. @subsubheading Examples::
  5449. @example
  5450. (special-operator-p 'if) @result{} @i{true}
  5451. (special-operator-p 'car) @result{} @i{false}
  5452. (special-operator-p 'one) @result{} @i{false}
  5453. @end example
  5454. @subsubheading Exceptional Situations::
  5455. Should signal @b{type-error} if its argument is not a @i{symbol}.
  5456. @subsubheading Notes::
  5457. Historically, this function was called @t{special-form-p}. The name was
  5458. finally declared a misnomer and changed, since it returned true for
  5459. @i{special operators}, not @i{special forms}.
  5460. @node constantp, , special-operator-p, Evaluation and Compilation Dictionary
  5461. @subsection constantp [Function]
  5462. @code{constantp} @i{form {&optional} environment} @result{} @i{generalized-boolean}
  5463. @subsubheading Arguments and Values::
  5464. @i{form}---a @i{form}.
  5465. @i{environment}---an @i{environment} @i{object}.
  5466. The default is @b{nil}.
  5467. @i{generalized-boolean}---a @i{generalized boolean}.
  5468. @subsubheading Description::
  5469. Returns @i{true} if @i{form} can be determined
  5470. by the @i{implementation} to be a @i{constant form}
  5471. in the indicated @i{environment};
  5472. otherwise, it returns @i{false} indicating either
  5473. that the @i{form} is not a @i{constant form}
  5474. or that it cannot be determined whether or not @i{form} is a @i{constant form}.
  5475. The following kinds of @i{forms} are considered @i{constant forms}:
  5476. @table @asis
  5477. @item @t{*}
  5478. @i{Self-evaluating objects}
  5479. (such as @i{numbers},
  5480. @i{characters},
  5481. and the various kinds of @i{arrays})
  5482. are always considered @i{constant forms}
  5483. and must be recognized as such by @b{constantp}.
  5484. @item @t{*}
  5485. @i{Constant variables}, such as @i{keywords},
  5486. symbols defined by @r{Common Lisp} as constant (such as @b{nil}, @b{t}, and @b{pi}),
  5487. and symbols declared as constant by the user in the indicated @i{environment}
  5488. using @b{defconstant}
  5489. are always considered @i{constant forms}
  5490. and must be recognized as such by @b{constantp}.
  5491. @item @t{*}
  5492. @b{quote} @i{forms} are always considered @i{constant forms}
  5493. and must be recognized as such by @b{constantp}.
  5494. @item @t{*}
  5495. An @i{implementation} is permitted, but not required, to detect
  5496. additional @i{constant forms}. If it does, it is also permitted,
  5497. but not required, to make use of information in the @i{environment}.
  5498. Examples of @i{constant forms} for which @b{constantp} might
  5499. or might not return @i{true} are:
  5500. @t{(sqrt pi)},
  5501. @t{(+ 3 2)},
  5502. @t{(length '(a b c))},
  5503. and
  5504. @t{(let ((x 7)) (zerop x))}.
  5505. @end table
  5506. If an @i{implementation} chooses to make use of the @i{environment}
  5507. information, such actions as expanding @i{macros} or performing function
  5508. inlining are permitted to be used, but not required;
  5509. however, expanding @i{compiler macros} is not permitted.
  5510. @subsubheading Examples::
  5511. @example
  5512. (constantp 1) @result{} @i{true}
  5513. (constantp 'temp) @result{} @i{false}
  5514. (constantp ''temp)) @result{} @i{true}
  5515. (defconstant this-is-a-constant 'never-changing) @result{} THIS-IS-A-CONSTANT
  5516. (constantp 'this-is-a-constant) @result{} @i{true}
  5517. (constantp "temp") @result{} @i{true}
  5518. (setq a 6) @result{} 6
  5519. (constantp a) @result{} @i{true}
  5520. (constantp '(sin pi)) @result{} @i{implementation-dependent}
  5521. (constantp '(car '(x))) @result{} @i{implementation-dependent}
  5522. (constantp '(eql x x)) @result{} @i{implementation-dependent}
  5523. (constantp '(typep x 'nil)) @result{} @i{implementation-dependent}
  5524. (constantp '(typep x 't)) @result{} @i{implementation-dependent}
  5525. (constantp '(values this-is-a-constant)) @result{} @i{implementation-dependent}
  5526. (constantp '(values 'x 'y)) @result{} @i{implementation-dependent}
  5527. (constantp '(let ((a '(a b c))) (+ (length a) 6))) @result{} @i{implementation-dependent}
  5528. @end example
  5529. @subsubheading Affected By::
  5530. The state of the global environment (@i{e.g.}, which @i{symbols} have been
  5531. declared to be the @i{names} of @i{constant variables}).
  5532. @subsubheading See Also::
  5533. @ref{defconstant}
  5534. @c end of including dict-eval-compile
  5535. @c %**end of chapter