softscheme.sch 390 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320
  1. ; Soft Scheme -- Copyright (C) 1993, 1994 Andrew K. Wright
  2. ;
  3. ; This program is free software; you can redistribute it and/or modify
  4. ; it under the terms of the GNU General Public License as published by
  5. ; the Free Software Foundation; either version 2 of the License, or
  6. ; (at your option) any later version.
  7. ;
  8. ; This program is distributed in the hope that it will be useful,
  9. ; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. ; GNU General Public License for more details.
  12. ;
  13. ; You should have received a copy of the GNU General Public License
  14. ; along with this program; if not, write to the Free Software
  15. ; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ;
  17. ; Packaged as a single file for Larceny by Lars T Hansen.
  18. ; Modified 2000-02-15 by lth.
  19. ;
  20. ; Compilation notes.
  21. ;
  22. ; The macro definitions for MATCH in this file depend on the presence of
  23. ; certain helper functions in the compilation environment, eg. match:andmap.
  24. ; (That is not a problem when loading this file, but it is an issue when
  25. ; compiling it.) The easiest way to provide the helper functions during
  26. ; compilation is to load match.sch into the compilation environment before
  27. ; compiling.
  28. ;
  29. ; Once compiled, this program is self-contained.
  30. ; The SoftScheme benchmark performs soft typing on a program and prints
  31. ; a diagnostic report. All screen output is captured in an output
  32. ; string port, which is subsequently discarded. (There is a moderate
  33. ; amount of output). No file I/O occurs while the program is running.
  34. (define (softscheme-benchmark)
  35. (let ((expr `(begin ,@(readfile "ss-input.scm")))
  36. (out (open-output-string)))
  37. (run-benchmark "softscheme"
  38. (lambda ()
  39. (with-output-to-port out
  40. (lambda ()
  41. (soft-def expr #f)))))
  42. (newline)
  43. (display (string-length (get-output-string out)))
  44. (display " characters of output written.")
  45. (newline)))
  46. ;;; Define defmacro, macro?, and macroexpand-1.
  47. (define *macros* '())
  48. (define-syntax
  49. defmacro
  50. (transformer
  51. (lambda (exp rename compare)
  52. (define (arglist? x)
  53. (or (symbol? x)
  54. (null? x)
  55. (and (pair? x)
  56. (symbol? (car x))
  57. (arglist? (cdr x)))))
  58. (if (not (and (list? exp)
  59. (>= (length exp) 4)
  60. (symbol? (cadr exp))
  61. (arglist? (caddr exp))))
  62. (error "Bad macro definition: " exp))
  63. (let ((name (cadr exp))
  64. (args (caddr exp))
  65. (body (cdddr exp)))
  66. `(begin
  67. (define-syntax
  68. ,name
  69. (transformer
  70. (lambda (_defmacro_exp
  71. _defmacro_rename
  72. _defmacro_compare)
  73. (apply (lambda ,args ,@body) (cdr _defmacro_exp)))))
  74. (set! *macros*
  75. (cons (cons ',name
  76. (lambda (_exp)
  77. (apply (lambda ,args ,@body) (cdr _exp))))
  78. *macros*))
  79. )))))
  80. (define (macroexpand-1 exp)
  81. (cond ((pair? exp)
  82. (let ((probe (assq (car exp) *macros*)))
  83. (if probe ((cdr probe) exp) exp)))
  84. (else exp)))
  85. (define (macro? keyword)
  86. (and (symbol? keyword) (assq keyword *macros*)))
  87. ;;; Other compatibility hacks
  88. (define slib:error error)
  89. (define force-output flush-output-port)
  90. (define format
  91. (let ((format format))
  92. (lambda (port . rest)
  93. (if (not port)
  94. (let ((s (open-output-string)))
  95. (apply format s rest)
  96. (get-output-string s))
  97. (apply format port rest)))))
  98. (define gentemp
  99. (let ((gensym gensym)) (lambda () (gensym "G"))))
  100. (define getenv
  101. (let ((getenv getenv))
  102. (lambda (x)
  103. (or (getenv x)
  104. (if (string=? x "HOME")
  105. "Ertevann:Desktop folder:"
  106. #f)))))
  107. ;;; The rest of the file should be more or less portable.
  108. (define match-file #f)
  109. (define installation-directory #f)
  110. (define customization-file #f)
  111. (define fastlibrary-file #f)
  112. (define st:version
  113. "Larceny Version 0.18, April 21, 1995")
  114. (define match:version
  115. "Version 1.18, July 17, 1995")
  116. (define match:error
  117. (lambda (val . args)
  118. (for-each pretty-print args)
  119. (slib:error "no matching clause for " val)))
  120. (define match:andmap
  121. (lambda (f l)
  122. (if (null? l)
  123. (and)
  124. (and (f (car l)) (match:andmap f (cdr l))))))
  125. (define match:syntax-err
  126. (lambda (obj msg) (slib:error msg obj)))
  127. (define match:disjoint-structure-tags '())
  128. (define match:make-structure-tag
  129. (lambda (name)
  130. (if (or (eq? match:structure-control 'disjoint)
  131. match:runtime-structures)
  132. (let ((tag (gentemp)))
  133. (set! match:disjoint-structure-tags
  134. (cons tag match:disjoint-structure-tags))
  135. tag)
  136. (string->symbol
  137. (string-append "<" (symbol->string name) ">")))))
  138. (define match:structure?
  139. (lambda (tag)
  140. (memq tag match:disjoint-structure-tags)))
  141. (define match:structure-control 'vector)
  142. (define match:set-structure-control
  143. (lambda (v) (set! match:structure-control v)))
  144. (define match:set-error
  145. (lambda (v) (set! match:error v)))
  146. (define match:error-control 'error)
  147. (define match:set-error-control
  148. (lambda (v) (set! match:error-control v)))
  149. (define match:disjoint-predicates
  150. (cons 'null
  151. '(pair? symbol?
  152. boolean?
  153. number?
  154. string?
  155. char?
  156. procedure?
  157. vector?)))
  158. (define match:vector-structures '())
  159. (define match:expanders
  160. (letrec ((genmatch
  161. (lambda (x clauses match-expr)
  162. (let* ((length>= (gentemp))
  163. (eb-errf (error-maker match-expr))
  164. (blist (car eb-errf))
  165. (plist (map (lambda (c)
  166. (let* ((x (bound (validate-pattern
  167. (car c))))
  168. (p (car x))
  169. (bv (cadr x))
  170. (bindings (caddr x))
  171. (code (gentemp))
  172. (fail (and (pair? (cdr c))
  173. (pair? (cadr c))
  174. (eq? (caadr c) '=>)
  175. (symbol? (cadadr c))
  176. (pair? (cdadr c))
  177. (null? (cddadr c))
  178. (pair? (cddr c))
  179. (cadadr c)))
  180. (bv2 (if fail (cons fail bv) bv))
  181. (body (if fail (cddr c) (cdr c))))
  182. (set! blist
  183. (cons `(,code (lambda ,bv2 ,@body))
  184. (append bindings blist)))
  185. (list p
  186. code
  187. bv
  188. (and fail (gentemp))
  189. #f)))
  190. clauses))
  191. (code (gen x
  192. '()
  193. plist
  194. (cdr eb-errf)
  195. length>=
  196. (gentemp))))
  197. (unreachable plist match-expr)
  198. (inline-let
  199. `(let ((,length>=
  200. (lambda (n) (lambda (l) (>= (length l) n))))
  201. ,@blist)
  202. ,code)))))
  203. (genletrec
  204. (lambda (pat exp body match-expr)
  205. (let* ((length>= (gentemp))
  206. (eb-errf (error-maker match-expr))
  207. (x (bound (validate-pattern pat)))
  208. (p (car x))
  209. (bv (cadr x))
  210. (bindings (caddr x))
  211. (code (gentemp))
  212. (plist (list (list p code bv #f #f)))
  213. (x (gentemp))
  214. (m (gen x
  215. '()
  216. plist
  217. (cdr eb-errf)
  218. length>=
  219. (gentemp)))
  220. (gs (map (lambda (_) (gentemp)) bv)))
  221. (unreachable plist match-expr)
  222. `(letrec ((,length>=
  223. (lambda (n) (lambda (l) (>= (length l) n))))
  224. ,@(map (lambda (v) `(,v #f)) bv)
  225. (,x ,exp)
  226. (,code
  227. (lambda ,gs
  228. ,@(map (lambda (v g) `(set! ,v ,g)) bv gs)
  229. ,@body))
  230. ,@bindings
  231. ,@(car eb-errf))
  232. ,m))))
  233. (gendefine
  234. (lambda (pat exp match-expr)
  235. (let* ((length>= (gentemp))
  236. (eb-errf (error-maker match-expr))
  237. (x (bound (validate-pattern pat)))
  238. (p (car x))
  239. (bv (cadr x))
  240. (bindings (caddr x))
  241. (code (gentemp))
  242. (plist (list (list p code bv #f #f)))
  243. (x (gentemp))
  244. (m (gen x
  245. '()
  246. plist
  247. (cdr eb-errf)
  248. length>=
  249. (gentemp)))
  250. (gs (map (lambda (_) (gentemp)) bv)))
  251. (unreachable plist match-expr)
  252. `(begin
  253. ,@(map (lambda (v) `(define ,v #f)) bv)
  254. ,(inline-let
  255. `(let ((,length>=
  256. (lambda (n) (lambda (l) (>= (length l) n))))
  257. (,x ,exp)
  258. (,code
  259. (lambda ,gs
  260. ,@(map (lambda (v g) `(set! ,v ,g)) bv gs)
  261. (cond (#f #f))))
  262. ,@bindings
  263. ,@(car eb-errf))
  264. ,m))))))
  265. (pattern-var?
  266. (lambda (x)
  267. (and (symbol? x)
  268. (not (dot-dot-k? x))
  269. (not (memq x
  270. '(quasiquote
  271. quote
  272. unquote
  273. unquote-splicing
  274. ?
  275. _
  276. $
  277. =
  278. and
  279. or
  280. not
  281. set!
  282. get!
  283. ...
  284. ___))))))
  285. (dot-dot-k?
  286. (lambda (s)
  287. (and (symbol? s)
  288. (if (memq s '(... ___))
  289. 0
  290. (let* ((s (symbol->string s)) (n (string-length s)))
  291. (and (<= 3 n)
  292. (memq (string-ref s 0) '(#\. #\_))
  293. (memq (string-ref s 1) '(#\. #\_))
  294. (match:andmap
  295. char-numeric?
  296. (string->list (substring s 2 n)))
  297. (string->number (substring s 2 n))))))))
  298. (error-maker
  299. (lambda (match-expr)
  300. (cond ((eq? match:error-control 'unspecified)
  301. (cons '() (lambda (x) `(cond (#f #f)))))
  302. ((memq match:error-control '(error fail))
  303. (cons '() (lambda (x) `(match:error ,x))))
  304. ((eq? match:error-control 'match)
  305. (let ((errf (gentemp)) (arg (gentemp)))
  306. (cons `((,errf
  307. (lambda (,arg)
  308. (match:error ,arg ',match-expr))))
  309. (lambda (x) `(,errf ,x)))))
  310. (else
  311. (match:syntax-err
  312. '(unspecified error fail match)
  313. "invalid value for match:error-control, legal values are")))))
  314. (unreachable
  315. (lambda (plist match-expr)
  316. (for-each
  317. (lambda (x)
  318. (if (not (car (cddddr x)))
  319. (begin
  320. (display "Warning: unreachable pattern ")
  321. (display (car x))
  322. (display " in ")
  323. (display match-expr)
  324. (newline))))
  325. plist)))
  326. (validate-pattern
  327. (lambda (pattern)
  328. (letrec ((simple?
  329. (lambda (x)
  330. (or (string? x)
  331. (boolean? x)
  332. (char? x)
  333. (number? x)
  334. (null? x))))
  335. (ordinary
  336. (lambda (p)
  337. (let ((g88 (lambda (x y)
  338. (cons (ordinary x) (ordinary y)))))
  339. (if (simple? p)
  340. ((lambda (p) p) p)
  341. (if (equal? p '_)
  342. ((lambda () '_))
  343. (if (pattern-var? p)
  344. ((lambda (p) p) p)
  345. (if (pair? p)
  346. (if (equal? (car p) 'quasiquote)
  347. (if (and (pair? (cdr p))
  348. (null? (cddr p)))
  349. ((lambda (p) (quasi p)) (cadr p))
  350. (g88 (car p) (cdr p)))
  351. (if (equal? (car p) 'quote)
  352. (if (and (pair? (cdr p))
  353. (null? (cddr p)))
  354. ((lambda (p) p) p)
  355. (g88 (car p) (cdr p)))
  356. (if (equal? (car p) '?)
  357. (if (and (pair? (cdr p))
  358. (list? (cddr p)))
  359. ((lambda (pred ps)
  360. `(? ,pred
  361. ,@(map ordinary ps)))
  362. (cadr p)
  363. (cddr p))
  364. (g88 (car p) (cdr p)))
  365. (if (equal? (car p) '=)
  366. (if (and (pair? (cdr p))
  367. (pair? (cddr p))
  368. (null? (cdddr p)))
  369. ((lambda (sel p)
  370. `(= ,sel ,(ordinary p)))
  371. (cadr p)
  372. (caddr p))
  373. (g88 (car p) (cdr p)))
  374. (if (equal? (car p) 'and)
  375. (if (and (list? (cdr p))
  376. (pair? (cdr p)))
  377. ((lambda (ps)
  378. `(and ,@(map ordinary
  379. ps)))
  380. (cdr p))
  381. (g88 (car p) (cdr p)))
  382. (if (equal? (car p) 'or)
  383. (if (and (list? (cdr p))
  384. (pair? (cdr p)))
  385. ((lambda (ps)
  386. `(or ,@(map ordinary
  387. ps)))
  388. (cdr p))
  389. (g88 (car p) (cdr p)))
  390. (if (equal? (car p) 'not)
  391. (if (and (list? (cdr p))
  392. (pair? (cdr p)))
  393. ((lambda (ps)
  394. `(not ,@(map ordinary
  395. ps)))
  396. (cdr p))
  397. (g88 (car p) (cdr p)))
  398. (if (equal? (car p) '$)
  399. (if (and (pair? (cdr p))
  400. (symbol?
  401. (cadr p))
  402. (list? (cddr p)))
  403. ((lambda (r ps)
  404. `($ ,r
  405. ,@(map ordinary
  406. ps)))
  407. (cadr p)
  408. (cddr p))
  409. (g88 (car p) (cdr p)))
  410. (if (equal?
  411. (car p)
  412. 'set!)
  413. (if (and (pair? (cdr p))
  414. (pattern-var?
  415. (cadr p))
  416. (null? (cddr p)))
  417. ((lambda (p) p) p)
  418. (g88 (car p)
  419. (cdr p)))
  420. (if (equal?
  421. (car p)
  422. 'get!)
  423. (if (and (pair? (cdr p))
  424. (pattern-var?
  425. (cadr p))
  426. (null? (cddr p)))
  427. ((lambda (p) p) p)
  428. (g88 (car p)
  429. (cdr p)))
  430. (if (equal?
  431. (car p)
  432. 'unquote)
  433. (g88 (car p)
  434. (cdr p))
  435. (if (equal?
  436. (car p)
  437. 'unquote-splicing)
  438. (g88 (car p)
  439. (cdr p))
  440. (if (and (pair? (cdr p))
  441. (dot-dot-k?
  442. (cadr p))
  443. (null? (cddr p)))
  444. ((lambda (p
  445. ddk)
  446. `(,(ordinary
  447. p)
  448. ,ddk))
  449. (car p)
  450. (cadr p))
  451. (g88 (car p)
  452. (cdr p)))))))))))))))
  453. (if (vector? p)
  454. ((lambda (p)
  455. (let* ((pl (vector->list p))
  456. (rpl (reverse pl)))
  457. (apply vector
  458. (if (and (not (null? rpl))
  459. (dot-dot-k?
  460. (car rpl)))
  461. (reverse
  462. (cons (car rpl)
  463. (map ordinary
  464. (cdr rpl))))
  465. (map ordinary pl)))))
  466. p)
  467. ((lambda ()
  468. (match:syntax-err
  469. pattern
  470. "syntax error in pattern")))))))))))
  471. (quasi (lambda (p)
  472. (let ((g109 (lambda (x y)
  473. (cons (quasi x) (quasi y)))))
  474. (if (simple? p)
  475. ((lambda (p) p) p)
  476. (if (symbol? p)
  477. ((lambda (p) `',p) p)
  478. (if (pair? p)
  479. (if (equal? (car p) 'unquote)
  480. (if (and (pair? (cdr p))
  481. (null? (cddr p)))
  482. ((lambda (p) (ordinary p))
  483. (cadr p))
  484. (g109 (car p) (cdr p)))
  485. (if (and (pair? (car p))
  486. (equal?
  487. (caar p)
  488. 'unquote-splicing)
  489. (pair? (cdar p))
  490. (null? (cddar p)))
  491. (if (null? (cdr p))
  492. ((lambda (p) (ordinary p))
  493. (cadar p))
  494. ((lambda (p y)
  495. (append
  496. (ordlist p)
  497. (quasi y)))
  498. (cadar p)
  499. (cdr p)))
  500. (if (and (pair? (cdr p))
  501. (dot-dot-k? (cadr p))
  502. (null? (cddr p)))
  503. ((lambda (p ddk)
  504. `(,(quasi p) ,ddk))
  505. (car p)
  506. (cadr p))
  507. (g109 (car p) (cdr p)))))
  508. (if (vector? p)
  509. ((lambda (p)
  510. (let* ((pl (vector->list p))
  511. (rpl (reverse pl)))
  512. (apply vector
  513. (if (dot-dot-k?
  514. (car rpl))
  515. (reverse
  516. (cons (car rpl)
  517. (map quasi
  518. (cdr rpl))))
  519. (map ordinary pl)))))
  520. p)
  521. ((lambda ()
  522. (match:syntax-err
  523. pattern
  524. "syntax error in pattern"))))))))))
  525. (ordlist
  526. (lambda (p)
  527. (cond ((null? p) '())
  528. ((pair? p)
  529. (cons (ordinary (car p)) (ordlist (cdr p))))
  530. (else
  531. (match:syntax-err
  532. pattern
  533. "invalid use of unquote-splicing in pattern"))))))
  534. (ordinary pattern))))
  535. (bound (lambda (pattern)
  536. (letrec ((pred-bodies '())
  537. (bound (lambda (p a k)
  538. (cond ((eq? '_ p) (k p a))
  539. ((symbol? p)
  540. (if (memq p a)
  541. (match:syntax-err
  542. pattern
  543. "duplicate variable in pattern"))
  544. (k p (cons p a)))
  545. ((and (pair? p)
  546. (eq? 'quote (car p)))
  547. (k p a))
  548. ((and (pair? p) (eq? '? (car p)))
  549. (cond ((not (null? (cddr p)))
  550. (bound `(and (? ,(cadr p))
  551. ,@(cddr p))
  552. a
  553. k))
  554. ((or (not (symbol?
  555. (cadr p)))
  556. (memq (cadr p) a))
  557. (let ((g (gentemp)))
  558. (set! pred-bodies
  559. (cons `(,g ,(cadr p))
  560. pred-bodies))
  561. (k `(? ,g) a)))
  562. (else (k p a))))
  563. ((and (pair? p) (eq? '= (car p)))
  564. (cond ((or (not (symbol?
  565. (cadr p)))
  566. (memq (cadr p) a))
  567. (let ((g (gentemp)))
  568. (set! pred-bodies
  569. (cons `(,g ,(cadr p))
  570. pred-bodies))
  571. (bound `(= ,g ,(caddr p))
  572. a
  573. k)))
  574. (else
  575. (bound (caddr p)
  576. a
  577. (lambda (p2 a)
  578. (k `(= ,(cadr p)
  579. ,p2)
  580. a))))))
  581. ((and (pair? p) (eq? 'and (car p)))
  582. (bound*
  583. (cdr p)
  584. a
  585. (lambda (p a)
  586. (k `(and ,@p) a))))
  587. ((and (pair? p) (eq? 'or (car p)))
  588. (bound (cadr p)
  589. a
  590. (lambda (first-p first-a)
  591. (let or* ((plist (cddr p))
  592. (k (lambda (plist)
  593. (k `(or ,first-p
  594. ,@plist)
  595. first-a))))
  596. (if (null? plist)
  597. (k plist)
  598. (bound (car plist)
  599. a
  600. (lambda (car-p
  601. car-a)
  602. (if (not (permutation
  603. car-a
  604. first-a))
  605. (match:syntax-err
  606. pattern
  607. "variables of or-pattern differ in"))
  608. (or* (cdr plist)
  609. (lambda (cdr-p)
  610. (k (cons car-p
  611. cdr-p)))))))))))
  612. ((and (pair? p) (eq? 'not (car p)))
  613. (cond ((not (null? (cddr p)))
  614. (bound `(not (or ,@(cdr p)))
  615. a
  616. k))
  617. (else
  618. (bound (cadr p)
  619. a
  620. (lambda (p2 a2)
  621. (if (not (permutation
  622. a
  623. a2))
  624. (match:syntax-err
  625. p
  626. "no variables allowed in"))
  627. (k `(not ,p2)
  628. a))))))
  629. ((and (pair? p)
  630. (pair? (cdr p))
  631. (dot-dot-k? (cadr p)))
  632. (bound (car p)
  633. a
  634. (lambda (q b)
  635. (let ((bvars (find-prefix
  636. b
  637. a)))
  638. (k `(,q
  639. ,(cadr p)
  640. ,bvars
  641. ,(gentemp)
  642. ,(gentemp)
  643. ,(map (lambda (_)
  644. (gentemp))
  645. bvars))
  646. b)))))
  647. ((and (pair? p) (eq? '$ (car p)))
  648. (bound*
  649. (cddr p)
  650. a
  651. (lambda (p1 a)
  652. (k `($ ,(cadr p) ,@p1) a))))
  653. ((and (pair? p)
  654. (eq? 'set! (car p)))
  655. (if (memq (cadr p) a)
  656. (k p a)
  657. (k p (cons (cadr p) a))))
  658. ((and (pair? p)
  659. (eq? 'get! (car p)))
  660. (if (memq (cadr p) a)
  661. (k p a)
  662. (k p (cons (cadr p) a))))
  663. ((pair? p)
  664. (bound (car p)
  665. a
  666. (lambda (car-p a)
  667. (bound (cdr p)
  668. a
  669. (lambda (cdr-p a)
  670. (k (cons car-p
  671. cdr-p)
  672. a))))))
  673. ((vector? p)
  674. (boundv
  675. (vector->list p)
  676. a
  677. (lambda (pl a)
  678. (k (list->vector pl) a))))
  679. (else (k p a)))))
  680. (boundv
  681. (lambda (plist a k)
  682. (let ((g115 (lambda () (k plist a))))
  683. (if (pair? plist)
  684. (if (and (pair? (cdr plist))
  685. (dot-dot-k? (cadr plist))
  686. (null? (cddr plist)))
  687. ((lambda () (bound plist a k)))
  688. (if (null? plist)
  689. (g115)
  690. ((lambda (x y)
  691. (bound x
  692. a
  693. (lambda (car-p a)
  694. (boundv
  695. y
  696. a
  697. (lambda (cdr-p a)
  698. (k (cons car-p cdr-p)
  699. a))))))
  700. (car plist)
  701. (cdr plist))))
  702. (if (null? plist)
  703. (g115)
  704. (match:error plist))))))
  705. (bound*
  706. (lambda (plist a k)
  707. (if (null? plist)
  708. (k plist a)
  709. (bound (car plist)
  710. a
  711. (lambda (car-p a)
  712. (bound*
  713. (cdr plist)
  714. a
  715. (lambda (cdr-p a)
  716. (k (cons car-p cdr-p) a))))))))
  717. (find-prefix
  718. (lambda (b a)
  719. (if (eq? b a)
  720. '()
  721. (cons (car b) (find-prefix (cdr b) a)))))
  722. (permutation
  723. (lambda (p1 p2)
  724. (and (= (length p1) (length p2))
  725. (match:andmap
  726. (lambda (x1) (memq x1 p2))
  727. p1)))))
  728. (bound pattern
  729. '()
  730. (lambda (p a)
  731. (list p (reverse a) pred-bodies))))))
  732. (inline-let
  733. (lambda (let-exp)
  734. (letrec ((occ (lambda (x e)
  735. (let loop ((e e))
  736. (cond ((pair? e)
  737. (+ (loop (car e)) (loop (cdr e))))
  738. ((eq? x e) 1)
  739. (else 0)))))
  740. (subst (lambda (e old new)
  741. (let loop ((e e))
  742. (cond ((pair? e)
  743. (cons (loop (car e)) (loop (cdr e))))
  744. ((eq? old e) new)
  745. (else e)))))
  746. (const?
  747. (lambda (sexp)
  748. (or (symbol? sexp)
  749. (boolean? sexp)
  750. (string? sexp)
  751. (char? sexp)
  752. (number? sexp)
  753. (null? sexp)
  754. (and (pair? sexp)
  755. (eq? (car sexp) 'quote)
  756. (pair? (cdr sexp))
  757. (symbol? (cadr sexp))
  758. (null? (cddr sexp))))))
  759. (isval?
  760. (lambda (sexp)
  761. (or (const? sexp)
  762. (and (pair? sexp)
  763. (memq (car sexp)
  764. '(lambda quote
  765. match-lambda
  766. match-lambda*))))))
  767. (small?
  768. (lambda (sexp)
  769. (or (const? sexp)
  770. (and (pair? sexp)
  771. (eq? (car sexp) 'lambda)
  772. (pair? (cdr sexp))
  773. (pair? (cddr sexp))
  774. (const? (caddr sexp))
  775. (null? (cdddr sexp)))))))
  776. (let loop ((b (cadr let-exp))
  777. (new-b '())
  778. (e (caddr let-exp)))
  779. (cond ((null? b)
  780. (if (null? new-b) e `(let ,(reverse new-b) ,e)))
  781. ((isval? (cadr (car b)))
  782. (let* ((x (caar b)) (n (occ x e)))
  783. (cond ((= 0 n) (loop (cdr b) new-b e))
  784. ((or (= 1 n) (small? (cadr (car b))))
  785. (loop (cdr b)
  786. new-b
  787. (subst e x (cadr (car b)))))
  788. (else
  789. (loop (cdr b) (cons (car b) new-b) e)))))
  790. (else (loop (cdr b) (cons (car b) new-b) e)))))))
  791. (gen (lambda (x sf plist erract length>= eta)
  792. (if (null? plist)
  793. (erract x)
  794. (let* ((v '())
  795. (val (lambda (x) (cdr (assq x v))))
  796. (fail (lambda (sf)
  797. (gen x sf (cdr plist) erract length>= eta)))
  798. (success
  799. (lambda (sf)
  800. (set-car! (cddddr (car plist)) #t)
  801. (let* ((code (cadr (car plist)))
  802. (bv (caddr (car plist)))
  803. (fail-sym (cadddr (car plist))))
  804. (if fail-sym
  805. (let ((ap `(,code
  806. ,fail-sym
  807. ,@(map val bv))))
  808. `(call-with-current-continuation
  809. (lambda (,fail-sym)
  810. (let ((,fail-sym
  811. (lambda ()
  812. (,fail-sym ,(fail sf)))))
  813. ,ap))))
  814. `(,code ,@(map val bv)))))))
  815. (let next ((p (caar plist))
  816. (e x)
  817. (sf sf)
  818. (kf fail)
  819. (ks success))
  820. (cond ((eq? '_ p) (ks sf))
  821. ((symbol? p)
  822. (set! v (cons (cons p e) v))
  823. (ks sf))
  824. ((null? p) (emit `(null? ,e) sf kf ks))
  825. ((equal? p ''()) (emit `(null? ,e) sf kf ks))
  826. ((string? p) (emit `(equal? ,e ,p) sf kf ks))
  827. ((boolean? p) (emit `(equal? ,e ,p) sf kf ks))
  828. ((char? p) (emit `(equal? ,e ,p) sf kf ks))
  829. ((number? p) (emit `(equal? ,e ,p) sf kf ks))
  830. ((and (pair? p) (eq? 'quote (car p)))
  831. (emit `(equal? ,e ,p) sf kf ks))
  832. ((and (pair? p) (eq? '? (car p)))
  833. (let ((tst `(,(cadr p) ,e)))
  834. (emit tst sf kf ks)))
  835. ((and (pair? p) (eq? '= (car p)))
  836. (next (caddr p) `(,(cadr p) ,e) sf kf ks))
  837. ((and (pair? p) (eq? 'and (car p)))
  838. (let loop ((p (cdr p)) (sf sf))
  839. (if (null? p)
  840. (ks sf)
  841. (next (car p)
  842. e
  843. sf
  844. kf
  845. (lambda (sf) (loop (cdr p) sf))))))
  846. ((and (pair? p) (eq? 'or (car p)))
  847. (let ((or-v v))
  848. (let loop ((p (cdr p)) (sf sf))
  849. (if (null? p)
  850. (kf sf)
  851. (begin
  852. (set! v or-v)
  853. (next (car p)
  854. e
  855. sf
  856. (lambda (sf) (loop (cdr p) sf))
  857. ks))))))
  858. ((and (pair? p) (eq? 'not (car p)))
  859. (next (cadr p) e sf ks kf))
  860. ((and (pair? p) (eq? '$ (car p)))
  861. (let* ((tag (cadr p))
  862. (fields (cdr p))
  863. (rlen (length fields))
  864. (tst `(,(symbol-append tag '?) ,e)))
  865. (emit tst
  866. sf
  867. kf
  868. (let rloop ((n 1))
  869. (lambda (sf)
  870. (if (= n rlen)
  871. (ks sf)
  872. (next (list-ref fields n)
  873. `(,(symbol-append tag '- n)
  874. ,e)
  875. sf
  876. kf
  877. (rloop (+ 1 n)))))))))
  878. ((and (pair? p) (eq? 'set! (car p)))
  879. (set! v (cons (cons (cadr p) (setter e p)) v))
  880. (ks sf))
  881. ((and (pair? p) (eq? 'get! (car p)))
  882. (set! v (cons (cons (cadr p) (getter e p)) v))
  883. (ks sf))
  884. ((and (pair? p)
  885. (pair? (cdr p))
  886. (dot-dot-k? (cadr p)))
  887. (emit `(list? ,e)
  888. sf
  889. kf
  890. (lambda (sf)
  891. (let* ((k (dot-dot-k? (cadr p)))
  892. (ks (lambda (sf)
  893. (let ((bound (list-ref
  894. p
  895. 2)))
  896. (cond ((eq? (car p) '_)
  897. (ks sf))
  898. ((null? bound)
  899. (let* ((ptst (next (car p)
  900. eta
  901. sf
  902. (lambda (sf)
  903. #f)
  904. (lambda (sf)
  905. #t)))
  906. (tst (if (and (pair? ptst)
  907. (symbol?
  908. (car ptst))
  909. (pair? (cdr ptst))
  910. (eq? eta
  911. (cadr ptst))
  912. (null? (cddr ptst)))
  913. (car ptst)
  914. `(lambda (,eta)
  915. ,ptst))))
  916. (assm `(match:andmap
  917. ,tst
  918. ,e)
  919. (kf sf)
  920. (ks sf))))
  921. ((and (symbol?
  922. (car p))
  923. (equal?
  924. (list (car p))
  925. bound))
  926. (next (car p)
  927. e
  928. sf
  929. kf
  930. ks))
  931. (else
  932. (let* ((gloop (list-ref
  933. p
  934. 3))
  935. (ge (list-ref
  936. p
  937. 4))
  938. (fresh (list-ref
  939. p
  940. 5))
  941. (p1 (next (car p)
  942. `(car ,ge)
  943. sf
  944. kf
  945. (lambda (sf)
  946. `(,gloop
  947. (cdr ,ge)
  948. ,@(map (lambda (b
  949. f)
  950. `(cons ,(val b)
  951. ,f))
  952. bound
  953. fresh))))))
  954. (set! v
  955. (append
  956. (map cons
  957. bound
  958. (map (lambda (x)
  959. `(reverse
  960. ,x))
  961. fresh))
  962. v))
  963. `(let ,gloop
  964. ((,ge ,e)
  965. ,@(map (lambda (x)
  966. `(,x
  967. '()))
  968. fresh))
  969. (if (null? ,ge)
  970. ,(ks sf)
  971. ,p1)))))))))
  972. (case k
  973. ((0) (ks sf))
  974. ((1) (emit `(pair? ,e) sf kf ks))
  975. (else
  976. (emit `((,length>= ,k) ,e)
  977. sf
  978. kf
  979. ks)))))))
  980. ((pair? p)
  981. (emit `(pair? ,e)
  982. sf
  983. kf
  984. (lambda (sf)
  985. (next (car p)
  986. (add-a e)
  987. sf
  988. kf
  989. (lambda (sf)
  990. (next (cdr p)
  991. (add-d e)
  992. sf
  993. kf
  994. ks))))))
  995. ((and (vector? p)
  996. (>= (vector-length p) 6)
  997. (dot-dot-k?
  998. (vector-ref p (- (vector-length p) 5))))
  999. (let* ((vlen (- (vector-length p) 6))
  1000. (k (dot-dot-k?
  1001. (vector-ref p (+ vlen 1))))
  1002. (minlen (+ vlen k))
  1003. (bound (vector-ref p (+ vlen 2))))
  1004. (emit `(vector? ,e)
  1005. sf
  1006. kf
  1007. (lambda (sf)
  1008. (assm `(>= (vector-length ,e) ,minlen)
  1009. (kf sf)
  1010. ((let vloop ((n 0))
  1011. (lambda (sf)
  1012. (cond ((not (= n vlen))
  1013. (next (vector-ref
  1014. p
  1015. n)
  1016. `(vector-ref
  1017. ,e
  1018. ,n)
  1019. sf
  1020. kf
  1021. (vloop (+ 1
  1022. n))))
  1023. ((eq? (vector-ref
  1024. p
  1025. vlen)
  1026. '_)
  1027. (ks sf))
  1028. (else
  1029. (let* ((gloop (vector-ref
  1030. p
  1031. (+ vlen
  1032. 3)))
  1033. (ind (vector-ref
  1034. p
  1035. (+ vlen
  1036. 4)))
  1037. (fresh (vector-ref
  1038. p
  1039. (+ vlen
  1040. 5)))
  1041. (p1 (next (vector-ref
  1042. p
  1043. vlen)
  1044. `(vector-ref
  1045. ,e
  1046. ,ind)
  1047. sf
  1048. kf
  1049. (lambda (sf)
  1050. `(,gloop
  1051. (- ,ind
  1052. 1)
  1053. ,@(map (lambda (b
  1054. f)
  1055. `(cons ,(val b)
  1056. ,f))
  1057. bound
  1058. fresh))))))
  1059. (set! v
  1060. (append
  1061. (map cons
  1062. bound
  1063. fresh)
  1064. v))
  1065. `(let ,gloop
  1066. ((,ind
  1067. (- (vector-length
  1068. ,e)
  1069. 1))
  1070. ,@(map (lambda (x)
  1071. `(,x
  1072. '()))
  1073. fresh))
  1074. (if (> ,minlen
  1075. ,ind)
  1076. ,(ks sf)
  1077. ,p1)))))))
  1078. sf))))))
  1079. ((vector? p)
  1080. (let ((vlen (vector-length p)))
  1081. (emit `(vector? ,e)
  1082. sf
  1083. kf
  1084. (lambda (sf)
  1085. (emit `(equal?
  1086. (vector-length ,e)
  1087. ,vlen)
  1088. sf
  1089. kf
  1090. (let vloop ((n 0))
  1091. (lambda (sf)
  1092. (if (= n vlen)
  1093. (ks sf)
  1094. (next (vector-ref p n)
  1095. `(vector-ref ,e ,n)
  1096. sf
  1097. kf
  1098. (vloop (+ 1
  1099. n)))))))))))
  1100. (else
  1101. (display "FATAL ERROR IN PATTERN MATCHER")
  1102. (newline)
  1103. (error #f "THIS NEVER HAPPENS"))))))))
  1104. (emit (lambda (tst sf kf ks)
  1105. (cond ((in tst sf) (ks sf))
  1106. ((in `(not ,tst) sf) (kf sf))
  1107. (else
  1108. (let* ((e (cadr tst))
  1109. (implied
  1110. (cond ((eq? (car tst) 'equal?)
  1111. (let ((p (caddr tst)))
  1112. (cond ((string? p) `((string? ,e)))
  1113. ((boolean? p)
  1114. `((boolean? ,e)))
  1115. ((char? p) `((char? ,e)))
  1116. ((number? p) `((number? ,e)))
  1117. ((and (pair? p)
  1118. (eq? 'quote (car p)))
  1119. `((symbol? ,e)))
  1120. (else '()))))
  1121. ((eq? (car tst) 'null?) `((list? ,e)))
  1122. ((vec-structure? tst) `((vector? ,e)))
  1123. (else '())))
  1124. (not-imp
  1125. (case (car tst)
  1126. ((list?) `((not (null? ,e))))
  1127. (else '())))
  1128. (s (ks (cons tst (append implied sf))))
  1129. (k (kf (cons `(not ,tst)
  1130. (append not-imp sf)))))
  1131. (assm tst k s))))))
  1132. (assm (lambda (tst f s)
  1133. (cond ((equal? s f) s)
  1134. ((and (eq? s #t) (eq? f #f)) tst)
  1135. ((and (eq? (car tst) 'pair?)
  1136. (memq match:error-control '(unspecified fail))
  1137. (memq (car f) '(cond match:error))
  1138. (guarantees s (cadr tst)))
  1139. s)
  1140. ((and (pair? s)
  1141. (eq? (car s) 'if)
  1142. (equal? (cadddr s) f))
  1143. (if (eq? (car (cadr s)) 'and)
  1144. `(if (and ,tst ,@(cdr (cadr s))) ,(caddr s) ,f)
  1145. `(if (and ,tst ,(cadr s)) ,(caddr s) ,f)))
  1146. ((and (pair? s)
  1147. (equal? (car s) 'call-with-current-continuation)
  1148. (pair? (cdr s))
  1149. (pair? (cadr s))
  1150. (equal? (caadr s) 'lambda)
  1151. (pair? (cdadr s))
  1152. (pair? (cadadr s))
  1153. (null? (cdr (cadadr s)))
  1154. (pair? (cddadr s))
  1155. (pair? (car (cddadr s)))
  1156. (equal? (caar (cddadr s)) 'let)
  1157. (pair? (cdar (cddadr s)))
  1158. (pair? (cadar (cddadr s)))
  1159. (pair? (caadar (cddadr s)))
  1160. (pair? (cdr (caadar (cddadr s))))
  1161. (pair? (cadr (caadar (cddadr s))))
  1162. (equal? (caadr (caadar (cddadr s))) 'lambda)
  1163. (pair? (cdadr (caadar (cddadr s))))
  1164. (null? (cadadr (caadar (cddadr s))))
  1165. (pair? (cddadr (caadar (cddadr s))))
  1166. (pair? (car (cddadr (caadar (cddadr s)))))
  1167. (pair? (cdar (cddadr (caadar (cddadr s)))))
  1168. (null? (cddar (cddadr (caadar (cddadr s)))))
  1169. (null? (cdr (cddadr (caadar (cddadr s)))))
  1170. (null? (cddr (caadar (cddadr s))))
  1171. (null? (cdadar (cddadr s)))
  1172. (pair? (cddar (cddadr s)))
  1173. (null? (cdddar (cddadr s)))
  1174. (null? (cdr (cddadr s)))
  1175. (null? (cddr s))
  1176. (equal? f (cadar (cddadr (caadar (cddadr s))))))
  1177. (let ((k (car (cadadr s)))
  1178. (fail (car (caadar (cddadr s))))
  1179. (s2 (caddar (cddadr s))))
  1180. `(call-with-current-continuation
  1181. (lambda (,k)
  1182. (let ((,fail (lambda () (,k ,f))))
  1183. ,(assm tst `(,fail) s2))))))
  1184. ((and #f
  1185. (pair? s)
  1186. (equal? (car s) 'let)
  1187. (pair? (cdr s))
  1188. (pair? (cadr s))
  1189. (pair? (caadr s))
  1190. (pair? (cdaadr s))
  1191. (pair? (car (cdaadr s)))
  1192. (equal? (caar (cdaadr s)) 'lambda)
  1193. (pair? (cdar (cdaadr s)))
  1194. (null? (cadar (cdaadr s)))
  1195. (pair? (cddar (cdaadr s)))
  1196. (null? (cdddar (cdaadr s)))
  1197. (null? (cdr (cdaadr s)))
  1198. (null? (cdadr s))
  1199. (pair? (cddr s))
  1200. (null? (cdddr s))
  1201. (equal? (caddar (cdaadr s)) f))
  1202. (let ((fail (caaadr s)) (s2 (caddr s)))
  1203. `(let ((,fail (lambda () ,f)))
  1204. ,(assm tst `(,fail) s2))))
  1205. (else `(if ,tst ,s ,f)))))
  1206. (guarantees
  1207. (lambda (code x)
  1208. (let ((a (add-a x)) (d (add-d x)))
  1209. (let loop ((code code))
  1210. (cond ((not (pair? code)) #f)
  1211. ((memq (car code) '(cond match:error)) #t)
  1212. ((or (equal? code a) (equal? code d)) #t)
  1213. ((eq? (car code) 'if)
  1214. (or (loop (cadr code))
  1215. (and (loop (caddr code)) (loop (cadddr code)))))
  1216. ((eq? (car code) 'lambda) #f)
  1217. ((and (eq? (car code) 'let) (symbol? (cadr code)))
  1218. #f)
  1219. (else (or (loop (car code)) (loop (cdr code)))))))))
  1220. (in (lambda (e l)
  1221. (or (member e l)
  1222. (and (eq? (car e) 'list?)
  1223. (or (member `(null? ,(cadr e)) l)
  1224. (member `(pair? ,(cadr e)) l)))
  1225. (and (eq? (car e) 'not)
  1226. (let* ((srch (cadr e))
  1227. (const-class (equal-test? srch)))
  1228. (cond (const-class
  1229. (let mem ((l l))
  1230. (if (null? l)
  1231. #f
  1232. (let ((x (car l)))
  1233. (or (and (equal? (cadr x) (cadr srch))
  1234. (disjoint? x)
  1235. (not (equal?
  1236. const-class
  1237. (car x))))
  1238. (equal?
  1239. x
  1240. `(not (,const-class
  1241. ,(cadr srch))))
  1242. (and (equal? (cadr x) (cadr srch))
  1243. (equal-test? x)
  1244. (not (equal?
  1245. (caddr srch)
  1246. (caddr x))))
  1247. (mem (cdr l)))))))
  1248. ((disjoint? srch)
  1249. (let mem ((l l))
  1250. (if (null? l)
  1251. #f
  1252. (let ((x (car l)))
  1253. (or (and (equal? (cadr x) (cadr srch))
  1254. (disjoint? x)
  1255. (not (equal?
  1256. (car x)
  1257. (car srch))))
  1258. (mem (cdr l)))))))
  1259. ((eq? (car srch) 'list?)
  1260. (let mem ((l l))
  1261. (if (null? l)
  1262. #f
  1263. (let ((x (car l)))
  1264. (or (and (equal? (cadr x) (cadr srch))
  1265. (disjoint? x)
  1266. (not (memq (car x)
  1267. '(list? pair?
  1268. null?))))
  1269. (mem (cdr l)))))))
  1270. ((vec-structure? srch)
  1271. (let mem ((l l))
  1272. (if (null? l)
  1273. #f
  1274. (let ((x (car l)))
  1275. (or (and (equal? (cadr x) (cadr srch))
  1276. (or (disjoint? x)
  1277. (vec-structure? x))
  1278. (not (equal?
  1279. (car x)
  1280. 'vector?))
  1281. (not (equal?
  1282. (car x)
  1283. (car srch))))
  1284. (equal?
  1285. x
  1286. `(not (vector? ,(cadr srch))))
  1287. (mem (cdr l)))))))
  1288. (else #f)))))))
  1289. (equal-test?
  1290. (lambda (tst)
  1291. (and (eq? (car tst) 'equal?)
  1292. (let ((p (caddr tst)))
  1293. (cond ((string? p) 'string?)
  1294. ((boolean? p) 'boolean?)
  1295. ((char? p) 'char?)
  1296. ((number? p) 'number?)
  1297. ((and (pair? p)
  1298. (pair? (cdr p))
  1299. (null? (cddr p))
  1300. (eq? 'quote (car p))
  1301. (symbol? (cadr p)))
  1302. 'symbol?)
  1303. (else #f))))))
  1304. (disjoint?
  1305. (lambda (tst)
  1306. (memq (car tst) match:disjoint-predicates)))
  1307. (vec-structure?
  1308. (lambda (tst)
  1309. (memq (car tst) match:vector-structures)))
  1310. (add-a (lambda (a)
  1311. (let ((new (and (pair? a) (assq (car a) c---rs))))
  1312. (if new (cons (cadr new) (cdr a)) `(car ,a)))))
  1313. (add-d (lambda (a)
  1314. (let ((new (and (pair? a) (assq (car a) c---rs))))
  1315. (if new (cons (cddr new) (cdr a)) `(cdr ,a)))))
  1316. (c---rs
  1317. '((car caar . cdar)
  1318. (cdr cadr . cddr)
  1319. (caar caaar . cdaar)
  1320. (cadr caadr . cdadr)
  1321. (cdar cadar . cddar)
  1322. (cddr caddr . cdddr)
  1323. (caaar caaaar . cdaaar)
  1324. (caadr caaadr . cdaadr)
  1325. (cadar caadar . cdadar)
  1326. (caddr caaddr . cdaddr)
  1327. (cdaar cadaar . cddaar)
  1328. (cdadr cadadr . cddadr)
  1329. (cddar caddar . cdddar)
  1330. (cdddr cadddr . cddddr)))
  1331. (setter
  1332. (lambda (e p)
  1333. (let ((mk-setter
  1334. (lambda (s) (symbol-append 'set- s '!))))
  1335. (cond ((not (pair? e))
  1336. (match:syntax-err p "unnested set! pattern"))
  1337. ((eq? (car e) 'vector-ref)
  1338. `(let ((x ,(cadr e)))
  1339. (lambda (y) (vector-set! x ,(caddr e) y))))
  1340. ((eq? (car e) 'unbox)
  1341. `(let ((x ,(cadr e))) (lambda (y) (set-box! x y))))
  1342. ((eq? (car e) 'car)
  1343. `(let ((x ,(cadr e))) (lambda (y) (set-car! x y))))
  1344. ((eq? (car e) 'cdr)
  1345. `(let ((x ,(cadr e))) (lambda (y) (set-cdr! x y))))
  1346. ((let ((a (assq (car e) get-c---rs)))
  1347. (and a
  1348. `(let ((x (,(cadr a) ,(cadr e))))
  1349. (lambda (y) (,(mk-setter (cddr a)) x y))))))
  1350. (else
  1351. `(let ((x ,(cadr e)))
  1352. (lambda (y) (,(mk-setter (car e)) x y))))))))
  1353. (getter
  1354. (lambda (e p)
  1355. (cond ((not (pair? e))
  1356. (match:syntax-err p "unnested get! pattern"))
  1357. ((eq? (car e) 'vector-ref)
  1358. `(let ((x ,(cadr e)))
  1359. (lambda () (vector-ref x ,(caddr e)))))
  1360. ((eq? (car e) 'unbox)
  1361. `(let ((x ,(cadr e))) (lambda () (unbox x))))
  1362. ((eq? (car e) 'car)
  1363. `(let ((x ,(cadr e))) (lambda () (car x))))
  1364. ((eq? (car e) 'cdr)
  1365. `(let ((x ,(cadr e))) (lambda () (cdr x))))
  1366. ((let ((a (assq (car e) get-c---rs)))
  1367. (and a
  1368. `(let ((x (,(cadr a) ,(cadr e))))
  1369. (lambda () (,(cddr a) x))))))
  1370. (else
  1371. `(let ((x ,(cadr e))) (lambda () (,(car e) x)))))))
  1372. (get-c---rs
  1373. '((caar car . car)
  1374. (cadr cdr . car)
  1375. (cdar car . cdr)
  1376. (cddr cdr . cdr)
  1377. (caaar caar . car)
  1378. (caadr cadr . car)
  1379. (cadar cdar . car)
  1380. (caddr cddr . car)
  1381. (cdaar caar . cdr)
  1382. (cdadr cadr . cdr)
  1383. (cddar cdar . cdr)
  1384. (cdddr cddr . cdr)
  1385. (caaaar caaar . car)
  1386. (caaadr caadr . car)
  1387. (caadar cadar . car)
  1388. (caaddr caddr . car)
  1389. (cadaar cdaar . car)
  1390. (cadadr cdadr . car)
  1391. (caddar cddar . car)
  1392. (cadddr cdddr . car)
  1393. (cdaaar caaar . cdr)
  1394. (cdaadr caadr . cdr)
  1395. (cdadar cadar . cdr)
  1396. (cdaddr caddr . cdr)
  1397. (cddaar cdaar . cdr)
  1398. (cddadr cdadr . cdr)
  1399. (cdddar cddar . cdr)
  1400. (cddddr cdddr . cdr)))
  1401. (symbol-append
  1402. (lambda l
  1403. (string->symbol
  1404. (apply string-append
  1405. (map (lambda (x)
  1406. (cond ((symbol? x) (symbol->string x))
  1407. ((number? x) (number->string x))
  1408. (else x)))
  1409. l)))))
  1410. (rac (lambda (l)
  1411. (if (null? (cdr l)) (car l) (rac (cdr l)))))
  1412. (rdc (lambda (l)
  1413. (if (null? (cdr l))
  1414. '()
  1415. (cons (car l) (rdc (cdr l)))))))
  1416. (list genmatch genletrec gendefine pattern-var?)))
  1417. (defmacro
  1418. match
  1419. args
  1420. (cond ((and (list? args)
  1421. (<= 1 (length args))
  1422. (match:andmap
  1423. (lambda (y) (and (list? y) (<= 2 (length y))))
  1424. (cdr args)))
  1425. (let* ((exp (car args))
  1426. (clauses (cdr args))
  1427. (e (if (symbol? exp) exp (gentemp))))
  1428. (if (symbol? exp)
  1429. ((car match:expanders) e clauses `(match ,@args))
  1430. `(let ((,e ,exp))
  1431. ,((car match:expanders) e clauses `(match ,@args))))))
  1432. (else
  1433. (match:syntax-err
  1434. `(match ,@args)
  1435. "syntax error in"))))
  1436. (defmacro
  1437. match-lambda
  1438. args
  1439. (if (and (list? args)
  1440. (match:andmap
  1441. (lambda (g126)
  1442. (if (and (pair? g126) (list? (cdr g126)))
  1443. (pair? (cdr g126))
  1444. #f))
  1445. args))
  1446. ((lambda ()
  1447. (let ((e (gentemp)))
  1448. `(lambda (,e) (match ,e ,@args)))))
  1449. ((lambda ()
  1450. (match:syntax-err
  1451. `(match-lambda ,@args)
  1452. "syntax error in")))))
  1453. (defmacro
  1454. match-lambda*
  1455. args
  1456. (if (and (list? args)
  1457. (match:andmap
  1458. (lambda (g134)
  1459. (if (and (pair? g134) (list? (cdr g134)))
  1460. (pair? (cdr g134))
  1461. #f))
  1462. args))
  1463. ((lambda ()
  1464. (let ((e (gentemp)))
  1465. `(lambda ,e (match ,e ,@args)))))
  1466. ((lambda ()
  1467. (match:syntax-err
  1468. `(match-lambda* ,@args)
  1469. "syntax error in")))))
  1470. (defmacro
  1471. match-let
  1472. args
  1473. (let ((g158 (lambda (pat exp body)
  1474. `(match ,exp (,pat ,@body))))
  1475. (g154 (lambda (pat exp body)
  1476. (let ((g (map (lambda (x) (gentemp)) pat))
  1477. (vpattern (list->vector pat)))
  1478. `(let ,(map list g exp)
  1479. (match (vector ,@g) (,vpattern ,@body))))))
  1480. (g146 (lambda ()
  1481. (match:syntax-err
  1482. `(match-let ,@args)
  1483. "syntax error in")))
  1484. (g145 (lambda (p1 e1 p2 e2 body)
  1485. (let ((g1 (gentemp)) (g2 (gentemp)))
  1486. `(let ((,g1 ,e1) (,g2 ,e2))
  1487. (match (cons ,g1 ,g2) ((,p1 unquote p2) ,@body))))))
  1488. (g136 (cadddr match:expanders)))
  1489. (if (pair? args)
  1490. (if (symbol? (car args))
  1491. (if (and (pair? (cdr args)) (list? (cadr args)))
  1492. (let g161 ((g162 (cadr args)) (g160 '()) (g159 '()))
  1493. (if (null? g162)
  1494. (if (and (list? (cddr args)) (pair? (cddr args)))
  1495. ((lambda (name pat exp body)
  1496. (if (match:andmap (cadddr match:expanders) pat)
  1497. `(let ,@args)
  1498. `(letrec ((,name (match-lambda* (,pat ,@body))))
  1499. (,name ,@exp))))
  1500. (car args)
  1501. (reverse g159)
  1502. (reverse g160)
  1503. (cddr args))
  1504. (g146))
  1505. (if (and (pair? (car g162))
  1506. (pair? (cdar g162))
  1507. (null? (cddar g162)))
  1508. (g161 (cdr g162)
  1509. (cons (cadar g162) g160)
  1510. (cons (caar g162) g159))
  1511. (g146))))
  1512. (g146))
  1513. (if (list? (car args))
  1514. (if (match:andmap
  1515. (lambda (g167)
  1516. (if (and (pair? g167)
  1517. (g136 (car g167))
  1518. (pair? (cdr g167)))
  1519. (null? (cddr g167))
  1520. #f))
  1521. (car args))
  1522. (if (and (list? (cdr args)) (pair? (cdr args)))
  1523. ((lambda () `(let ,@args)))
  1524. (let g149 ((g150 (car args)) (g148 '()) (g147 '()))
  1525. (if (null? g150)
  1526. (g146)
  1527. (if (and (pair? (car g150))
  1528. (pair? (cdar g150))
  1529. (null? (cddar g150)))
  1530. (g149 (cdr g150)
  1531. (cons (cadar g150) g148)
  1532. (cons (caar g150) g147))
  1533. (g146)))))
  1534. (if (and (pair? (car args))
  1535. (pair? (caar args))
  1536. (pair? (cdaar args))
  1537. (null? (cddaar args)))
  1538. (if (null? (cdar args))
  1539. (if (and (list? (cdr args)) (pair? (cdr args)))
  1540. (g158 (caaar args) (cadaar args) (cdr args))
  1541. (let g149 ((g150 (car args)) (g148 '()) (g147 '()))
  1542. (if (null? g150)
  1543. (g146)
  1544. (if (and (pair? (car g150))
  1545. (pair? (cdar g150))
  1546. (null? (cddar g150)))
  1547. (g149 (cdr g150)
  1548. (cons (cadar g150) g148)
  1549. (cons (caar g150) g147))
  1550. (g146)))))
  1551. (if (and (pair? (cdar args))
  1552. (pair? (cadar args))
  1553. (pair? (cdadar args))
  1554. (null? (cdr (cdadar args)))
  1555. (null? (cddar args)))
  1556. (if (and (list? (cdr args)) (pair? (cdr args)))
  1557. (g145 (caaar args)
  1558. (cadaar args)
  1559. (caadar args)
  1560. (car (cdadar args))
  1561. (cdr args))
  1562. (let g149 ((g150 (car args)) (g148 '()) (g147 '()))
  1563. (if (null? g150)
  1564. (g146)
  1565. (if (and (pair? (car g150))
  1566. (pair? (cdar g150))
  1567. (null? (cddar g150)))
  1568. (g149 (cdr g150)
  1569. (cons (cadar g150) g148)
  1570. (cons (caar g150) g147))
  1571. (g146)))))
  1572. (let g149 ((g150 (car args)) (g148 '()) (g147 '()))
  1573. (if (null? g150)
  1574. (if (and (list? (cdr args)) (pair? (cdr args)))
  1575. (g154 (reverse g147) (reverse g148) (cdr args))
  1576. (g146))
  1577. (if (and (pair? (car g150))
  1578. (pair? (cdar g150))
  1579. (null? (cddar g150)))
  1580. (g149 (cdr g150)
  1581. (cons (cadar g150) g148)
  1582. (cons (caar g150) g147))
  1583. (g146))))))
  1584. (let g149 ((g150 (car args)) (g148 '()) (g147 '()))
  1585. (if (null? g150)
  1586. (if (and (list? (cdr args)) (pair? (cdr args)))
  1587. (g154 (reverse g147) (reverse g148) (cdr args))
  1588. (g146))
  1589. (if (and (pair? (car g150))
  1590. (pair? (cdar g150))
  1591. (null? (cddar g150)))
  1592. (g149 (cdr g150)
  1593. (cons (cadar g150) g148)
  1594. (cons (caar g150) g147))
  1595. (g146))))))
  1596. (if (pair? (car args))
  1597. (if (and (pair? (caar args))
  1598. (pair? (cdaar args))
  1599. (null? (cddaar args)))
  1600. (if (null? (cdar args))
  1601. (if (and (list? (cdr args)) (pair? (cdr args)))
  1602. (g158 (caaar args) (cadaar args) (cdr args))
  1603. (let g149 ((g150 (car args)) (g148 '()) (g147 '()))
  1604. (if (null? g150)
  1605. (g146)
  1606. (if (and (pair? (car g150))
  1607. (pair? (cdar g150))
  1608. (null? (cddar g150)))
  1609. (g149 (cdr g150)
  1610. (cons (cadar g150) g148)
  1611. (cons (caar g150) g147))
  1612. (g146)))))
  1613. (if (and (pair? (cdar args))
  1614. (pair? (cadar args))
  1615. (pair? (cdadar args))
  1616. (null? (cdr (cdadar args)))
  1617. (null? (cddar args)))
  1618. (if (and (list? (cdr args)) (pair? (cdr args)))
  1619. (g145 (caaar args)
  1620. (cadaar args)
  1621. (caadar args)
  1622. (car (cdadar args))
  1623. (cdr args))
  1624. (let g149 ((g150 (car args)) (g148 '()) (g147 '()))
  1625. (if (null? g150)
  1626. (g146)
  1627. (if (and (pair? (car g150))
  1628. (pair? (cdar g150))
  1629. (null? (cddar g150)))
  1630. (g149 (cdr g150)
  1631. (cons (cadar g150) g148)
  1632. (cons (caar g150) g147))
  1633. (g146)))))
  1634. (let g149 ((g150 (car args)) (g148 '()) (g147 '()))
  1635. (if (null? g150)
  1636. (if (and (list? (cdr args)) (pair? (cdr args)))
  1637. (g154 (reverse g147) (reverse g148) (cdr args))
  1638. (g146))
  1639. (if (and (pair? (car g150))
  1640. (pair? (cdar g150))
  1641. (null? (cddar g150)))
  1642. (g149 (cdr g150)
  1643. (cons (cadar g150) g148)
  1644. (cons (caar g150) g147))
  1645. (g146))))))
  1646. (let g149 ((g150 (car args)) (g148 '()) (g147 '()))
  1647. (if (null? g150)
  1648. (if (and (list? (cdr args)) (pair? (cdr args)))
  1649. (g154 (reverse g147) (reverse g148) (cdr args))
  1650. (g146))
  1651. (if (and (pair? (car g150))
  1652. (pair? (cdar g150))
  1653. (null? (cddar g150)))
  1654. (g149 (cdr g150)
  1655. (cons (cadar g150) g148)
  1656. (cons (caar g150) g147))
  1657. (g146)))))
  1658. (g146))))
  1659. (g146))))
  1660. (defmacro
  1661. match-let*
  1662. args
  1663. (let ((g176 (lambda ()
  1664. (match:syntax-err
  1665. `(match-let* ,@args)
  1666. "syntax error in"))))
  1667. (if (pair? args)
  1668. (if (null? (car args))
  1669. (if (and (list? (cdr args)) (pair? (cdr args)))
  1670. ((lambda (body) `(let* ,@args)) (cdr args))
  1671. (g176))
  1672. (if (and (pair? (car args))
  1673. (pair? (caar args))
  1674. (pair? (cdaar args))
  1675. (null? (cddaar args))
  1676. (list? (cdar args))
  1677. (list? (cdr args))
  1678. (pair? (cdr args)))
  1679. ((lambda (pat exp rest body)
  1680. (if ((cadddr match:expanders) pat)
  1681. `(let ((,pat ,exp)) (match-let* ,rest ,@body))
  1682. `(match ,exp (,pat (match-let* ,rest ,@body)))))
  1683. (caaar args)
  1684. (cadaar args)
  1685. (cdar args)
  1686. (cdr args))
  1687. (g176)))
  1688. (g176))))
  1689. (defmacro
  1690. match-letrec
  1691. args
  1692. (let ((g200 (cadddr match:expanders))
  1693. (g199 (lambda (p1 e1 p2 e2 body)
  1694. `(match-letrec
  1695. (((,p1 unquote p2) (cons ,e1 ,e2)))
  1696. ,@body)))
  1697. (g195 (lambda ()
  1698. (match:syntax-err
  1699. `(match-letrec ,@args)
  1700. "syntax error in")))
  1701. (g194 (lambda (pat exp body)
  1702. `(match-letrec
  1703. ((,(list->vector pat) (vector ,@exp)))
  1704. ,@body)))
  1705. (g186 (lambda (pat exp body)
  1706. ((cadr match:expanders)
  1707. pat
  1708. exp
  1709. body
  1710. `(match-letrec ((,pat ,exp)) ,@body)))))
  1711. (if (pair? args)
  1712. (if (list? (car args))
  1713. (if (match:andmap
  1714. (lambda (g206)
  1715. (if (and (pair? g206)
  1716. (g200 (car g206))
  1717. (pair? (cdr g206)))
  1718. (null? (cddr g206))
  1719. #f))
  1720. (car args))
  1721. (if (and (list? (cdr args)) (pair? (cdr args)))
  1722. ((lambda () `(letrec ,@args)))
  1723. (let g189 ((g190 (car args)) (g188 '()) (g187 '()))
  1724. (if (null? g190)
  1725. (g195)
  1726. (if (and (pair? (car g190))
  1727. (pair? (cdar g190))
  1728. (null? (cddar g190)))
  1729. (g189 (cdr g190)
  1730. (cons (cadar g190) g188)
  1731. (cons (caar g190) g187))
  1732. (g195)))))
  1733. (if (and (pair? (car args))
  1734. (pair? (caar args))
  1735. (pair? (cdaar args))
  1736. (null? (cddaar args)))
  1737. (if (null? (cdar args))
  1738. (if (and (list? (cdr args)) (pair? (cdr args)))
  1739. (g186 (caaar args) (cadaar args) (cdr args))
  1740. (let g189 ((g190 (car args)) (g188 '()) (g187 '()))
  1741. (if (null? g190)
  1742. (g195)
  1743. (if (and (pair? (car g190))
  1744. (pair? (cdar g190))
  1745. (null? (cddar g190)))
  1746. (g189 (cdr g190)
  1747. (cons (cadar g190) g188)
  1748. (cons (caar g190) g187))
  1749. (g195)))))
  1750. (if (and (pair? (cdar args))
  1751. (pair? (cadar args))
  1752. (pair? (cdadar args))
  1753. (null? (cdr (cdadar args)))
  1754. (null? (cddar args)))
  1755. (if (and (list? (cdr args)) (pair? (cdr args)))
  1756. (g199 (caaar args)
  1757. (cadaar args)
  1758. (caadar args)
  1759. (car (cdadar args))
  1760. (cdr args))
  1761. (let g189 ((g190 (car args)) (g188 '()) (g187 '()))
  1762. (if (null? g190)
  1763. (g195)
  1764. (if (and (pair? (car g190))
  1765. (pair? (cdar g190))
  1766. (null? (cddar g190)))
  1767. (g189 (cdr g190)
  1768. (cons (cadar g190) g188)
  1769. (cons (caar g190) g187))
  1770. (g195)))))
  1771. (let g189 ((g190 (car args)) (g188 '()) (g187 '()))
  1772. (if (null? g190)
  1773. (if (and (list? (cdr args)) (pair? (cdr args)))
  1774. (g194 (reverse g187) (reverse g188) (cdr args))
  1775. (g195))
  1776. (if (and (pair? (car g190))
  1777. (pair? (cdar g190))
  1778. (null? (cddar g190)))
  1779. (g189 (cdr g190)
  1780. (cons (cadar g190) g188)
  1781. (cons (caar g190) g187))
  1782. (g195))))))
  1783. (let g189 ((g190 (car args)) (g188 '()) (g187 '()))
  1784. (if (null? g190)
  1785. (if (and (list? (cdr args)) (pair? (cdr args)))
  1786. (g194 (reverse g187) (reverse g188) (cdr args))
  1787. (g195))
  1788. (if (and (pair? (car g190))
  1789. (pair? (cdar g190))
  1790. (null? (cddar g190)))
  1791. (g189 (cdr g190)
  1792. (cons (cadar g190) g188)
  1793. (cons (caar g190) g187))
  1794. (g195))))))
  1795. (if (pair? (car args))
  1796. (if (and (pair? (caar args))
  1797. (pair? (cdaar args))
  1798. (null? (cddaar args)))
  1799. (if (null? (cdar args))
  1800. (if (and (list? (cdr args)) (pair? (cdr args)))
  1801. (g186 (caaar args) (cadaar args) (cdr args))
  1802. (let g189 ((g190 (car args)) (g188 '()) (g187 '()))
  1803. (if (null? g190)
  1804. (g195)
  1805. (if (and (pair? (car g190))
  1806. (pair? (cdar g190))
  1807. (null? (cddar g190)))
  1808. (g189 (cdr g190)
  1809. (cons (cadar g190) g188)
  1810. (cons (caar g190) g187))
  1811. (g195)))))
  1812. (if (and (pair? (cdar args))
  1813. (pair? (cadar args))
  1814. (pair? (cdadar args))
  1815. (null? (cdr (cdadar args)))
  1816. (null? (cddar args)))
  1817. (if (and (list? (cdr args)) (pair? (cdr args)))
  1818. (g199 (caaar args)
  1819. (cadaar args)
  1820. (caadar args)
  1821. (car (cdadar args))
  1822. (cdr args))
  1823. (let g189 ((g190 (car args)) (g188 '()) (g187 '()))
  1824. (if (null? g190)
  1825. (g195)
  1826. (if (and (pair? (car g190))
  1827. (pair? (cdar g190))
  1828. (null? (cddar g190)))
  1829. (g189 (cdr g190)
  1830. (cons (cadar g190) g188)
  1831. (cons (caar g190) g187))
  1832. (g195)))))
  1833. (let g189 ((g190 (car args)) (g188 '()) (g187 '()))
  1834. (if (null? g190)
  1835. (if (and (list? (cdr args)) (pair? (cdr args)))
  1836. (g194 (reverse g187) (reverse g188) (cdr args))
  1837. (g195))
  1838. (if (and (pair? (car g190))
  1839. (pair? (cdar g190))
  1840. (null? (cddar g190)))
  1841. (g189 (cdr g190)
  1842. (cons (cadar g190) g188)
  1843. (cons (caar g190) g187))
  1844. (g195))))))
  1845. (let g189 ((g190 (car args)) (g188 '()) (g187 '()))
  1846. (if (null? g190)
  1847. (if (and (list? (cdr args)) (pair? (cdr args)))
  1848. (g194 (reverse g187) (reverse g188) (cdr args))
  1849. (g195))
  1850. (if (and (pair? (car g190))
  1851. (pair? (cdar g190))
  1852. (null? (cddar g190)))
  1853. (g189 (cdr g190)
  1854. (cons (cadar g190) g188)
  1855. (cons (caar g190) g187))
  1856. (g195)))))
  1857. (g195)))
  1858. (g195))))
  1859. (defmacro
  1860. match-define
  1861. args
  1862. (let ((g210 (cadddr match:expanders))
  1863. (g209 (lambda ()
  1864. (match:syntax-err
  1865. `(match-define ,@args)
  1866. "syntax error in"))))
  1867. (if (pair? args)
  1868. (if (g210 (car args))
  1869. (if (and (pair? (cdr args)) (null? (cddr args)))
  1870. ((lambda () `(begin (define ,@args))))
  1871. (g209))
  1872. (if (and (pair? (cdr args)) (null? (cddr args)))
  1873. ((lambda (pat exp)
  1874. ((caddr match:expanders)
  1875. pat
  1876. exp
  1877. `(match-define ,@args)))
  1878. (car args)
  1879. (cadr args))
  1880. (g209)))
  1881. (g209))))
  1882. (define match:runtime-structures #f)
  1883. (define match:set-runtime-structures
  1884. (lambda (v) (set! match:runtime-structures v)))
  1885. (define match:primitive-vector? vector?)
  1886. (defmacro
  1887. defstruct
  1888. args
  1889. (let ((field?
  1890. (lambda (x)
  1891. (if (symbol? x)
  1892. ((lambda () #t))
  1893. (if (and (pair? x)
  1894. (symbol? (car x))
  1895. (pair? (cdr x))
  1896. (symbol? (cadr x))
  1897. (null? (cddr x)))
  1898. ((lambda () #t))
  1899. ((lambda () #f))))))
  1900. (selector-name
  1901. (lambda (x)
  1902. (if (symbol? x)
  1903. ((lambda () x))
  1904. (if (and (pair? x)
  1905. (symbol? (car x))
  1906. (pair? (cdr x))
  1907. (null? (cddr x)))
  1908. ((lambda (s) s) (car x))
  1909. (match:error x)))))
  1910. (mutator-name
  1911. (lambda (x)
  1912. (if (symbol? x)
  1913. ((lambda () #f))
  1914. (if (and (pair? x)
  1915. (pair? (cdr x))
  1916. (symbol? (cadr x))
  1917. (null? (cddr x)))
  1918. ((lambda (s) s) (cadr x))
  1919. (match:error x)))))
  1920. (filter-map-with-index
  1921. (lambda (f l)
  1922. (letrec ((mapi (lambda (l i)
  1923. (cond ((null? l) '())
  1924. ((f (car l) i)
  1925. =>
  1926. (lambda (x)
  1927. (cons x (mapi (cdr l) (+ 1 i)))))
  1928. (else (mapi (cdr l) (+ 1 i)))))))
  1929. (mapi l 1)))))
  1930. (let ((g227 (lambda ()
  1931. (match:syntax-err
  1932. `(defstruct ,@args)
  1933. "syntax error in"))))
  1934. (if (and (pair? args)
  1935. (symbol? (car args))
  1936. (pair? (cdr args))
  1937. (symbol? (cadr args))
  1938. (pair? (cddr args))
  1939. (symbol? (caddr args))
  1940. (list? (cdddr args)))
  1941. (let g229 ((g230 (cdddr args)) (g228 '()))
  1942. (if (null? g230)
  1943. ((lambda (name constructor predicate fields)
  1944. (let* ((selectors (map selector-name fields))
  1945. (mutators (map mutator-name fields))
  1946. (tag (if match:runtime-structures
  1947. (gentemp)
  1948. `',(match:make-structure-tag name)))
  1949. (vectorp
  1950. (cond ((eq? match:structure-control 'disjoint)
  1951. 'match:primitive-vector?)
  1952. ((eq? match:structure-control 'vector)
  1953. 'vector?))))
  1954. (cond ((eq? match:structure-control 'disjoint)
  1955. (if (eq? vector? match:primitive-vector?)
  1956. (set! vector?
  1957. (lambda (v)
  1958. (and (match:primitive-vector? v)
  1959. (or (zero? (vector-length v))
  1960. (not (symbol? (vector-ref v 0)))
  1961. (not (match:structure?
  1962. (vector-ref v 0))))))))
  1963. (if (not (memq predicate match:disjoint-predicates))
  1964. (set! match:disjoint-predicates
  1965. (cons predicate match:disjoint-predicates))))
  1966. ((eq? match:structure-control 'vector)
  1967. (if (not (memq predicate match:vector-structures))
  1968. (set! match:vector-structures
  1969. (cons predicate match:vector-structures))))
  1970. (else
  1971. (match:syntax-err
  1972. '(vector disjoint)
  1973. "invalid value for match:structure-control, legal values are")))
  1974. `(begin
  1975. ,@(if match:runtime-structures
  1976. `((define ,tag (match:make-structure-tag ',name)))
  1977. '())
  1978. (define ,constructor
  1979. (lambda ,selectors (vector ,tag ,@selectors)))
  1980. (define ,predicate
  1981. (lambda (obj)
  1982. (and (,vectorp obj)
  1983. (= (vector-length obj) ,(+ 1 (length selectors)))
  1984. (eq? (vector-ref obj 0) ,tag))))
  1985. ,@(filter-map-with-index
  1986. (lambda (n i)
  1987. `(define ,n (lambda (obj) (vector-ref obj ,i))))
  1988. selectors)
  1989. ,@(filter-map-with-index
  1990. (lambda (n i)
  1991. (and n
  1992. `(define ,n
  1993. (lambda (obj newval)
  1994. (vector-set! obj ,i newval)))))
  1995. mutators))))
  1996. (car args)
  1997. (cadr args)
  1998. (caddr args)
  1999. (reverse g228))
  2000. (if (field? (car g230))
  2001. (g229 (cdr g230) (cons (car g230) g228))
  2002. (g227))))
  2003. (g227)))))
  2004. (defmacro
  2005. define-structure
  2006. args
  2007. (let ((g242 (lambda ()
  2008. (match:syntax-err
  2009. `(define-structure ,@args)
  2010. "syntax error in"))))
  2011. (if (and (pair? args)
  2012. (pair? (car args))
  2013. (list? (cdar args)))
  2014. (if (null? (cdr args))
  2015. ((lambda (name id1)
  2016. `(define-structure (,name ,@id1) ()))
  2017. (caar args)
  2018. (cdar args))
  2019. (if (and (pair? (cdr args)) (list? (cadr args)))
  2020. (let g239 ((g240 (cadr args)) (g238 '()) (g237 '()))
  2021. (if (null? g240)
  2022. (if (null? (cddr args))
  2023. ((lambda (name id1 id2 val)
  2024. (let ((mk-id (lambda (id)
  2025. (if (and (pair? id)
  2026. (equal? (car id) '@)
  2027. (pair? (cdr id))
  2028. (symbol? (cadr id))
  2029. (null? (cddr id)))
  2030. ((lambda (x) x) (cadr id))
  2031. ((lambda () `(! ,id)))))))
  2032. `(define-const-structure
  2033. (,name ,@(map mk-id id1))
  2034. ,(map (lambda (id v) `(,(mk-id id) ,v)) id2 val))))
  2035. (caar args)
  2036. (cdar args)
  2037. (reverse g237)
  2038. (reverse g238))
  2039. (g242))
  2040. (if (and (pair? (car g240))
  2041. (pair? (cdar g240))
  2042. (null? (cddar g240)))
  2043. (g239 (cdr g240)
  2044. (cons (cadar g240) g238)
  2045. (cons (caar g240) g237))
  2046. (g242))))
  2047. (g242)))
  2048. (g242))))
  2049. (defmacro
  2050. define-const-structure
  2051. args
  2052. (let ((field?
  2053. (lambda (id)
  2054. (if (symbol? id)
  2055. ((lambda () #t))
  2056. (if (and (pair? id)
  2057. (equal? (car id) '!)
  2058. (pair? (cdr id))
  2059. (symbol? (cadr id))
  2060. (null? (cddr id)))
  2061. ((lambda () #t))
  2062. ((lambda () #f))))))
  2063. (field-name
  2064. (lambda (x) (if (symbol? x) x (cadr x))))
  2065. (has-mutator? (lambda (x) (not (symbol? x))))
  2066. (filter-map-with-index
  2067. (lambda (f l)
  2068. (letrec ((mapi (lambda (l i)
  2069. (cond ((null? l) '())
  2070. ((f (car l) i)
  2071. =>
  2072. (lambda (x)
  2073. (cons x (mapi (cdr l) (+ 1 i)))))
  2074. (else (mapi (cdr l) (+ 1 i)))))))
  2075. (mapi l 1))))
  2076. (symbol-append
  2077. (lambda l
  2078. (string->symbol
  2079. (apply string-append
  2080. (map (lambda (x)
  2081. (cond ((symbol? x) (symbol->string x))
  2082. ((number? x) (number->string x))
  2083. (else x)))
  2084. l))))))
  2085. (let ((g266 (lambda ()
  2086. (match:syntax-err
  2087. `(define-const-structure ,@args)
  2088. "syntax error in"))))
  2089. (if (and (pair? args)
  2090. (pair? (car args))
  2091. (list? (cdar args)))
  2092. (if (null? (cdr args))
  2093. ((lambda (name id1)
  2094. `(define-const-structure (,name ,@id1) ()))
  2095. (caar args)
  2096. (cdar args))
  2097. (if (symbol? (caar args))
  2098. (let g259 ((g260 (cdar args)) (g258 '()))
  2099. (if (null? g260)
  2100. (if (and (pair? (cdr args)) (list? (cadr args)))
  2101. (let g263 ((g264 (cadr args)) (g262 '()) (g261 '()))
  2102. (if (null? g264)
  2103. (if (null? (cddr args))
  2104. ((lambda (name id1 id2 val)
  2105. (let* ((id1id2 (append id1 id2))
  2106. (raw-constructor
  2107. (symbol-append 'make-raw- name))
  2108. (constructor (symbol-append 'make- name))
  2109. (predicate (symbol-append name '?)))
  2110. `(begin
  2111. (defstruct
  2112. ,name
  2113. ,raw-constructor
  2114. ,predicate
  2115. ,@(filter-map-with-index
  2116. (lambda (arg i)
  2117. (if (has-mutator? arg)
  2118. `(,(symbol-append name '- i)
  2119. ,(symbol-append
  2120. 'set-
  2121. name
  2122. '-
  2123. i
  2124. '!))
  2125. (symbol-append name '- i)))
  2126. id1id2))
  2127. ,(let* ((make-fresh
  2128. (lambda (x)
  2129. (if (eq? '_ x) (gentemp) x)))
  2130. (names1
  2131. (map make-fresh
  2132. (map field-name id1)))
  2133. (names2
  2134. (map make-fresh
  2135. (map field-name id2))))
  2136. `(define ,constructor
  2137. (lambda ,names1
  2138. (let* ,(map list names2 val)
  2139. (,raw-constructor
  2140. ,@names1
  2141. ,@names2)))))
  2142. ,@(filter-map-with-index
  2143. (lambda (field i)
  2144. (if (eq? (field-name field) '_)
  2145. #f
  2146. `(define (unquote
  2147. (symbol-append
  2148. name
  2149. '-
  2150. (field-name field)))
  2151. ,(symbol-append name '- i))))
  2152. id1id2)
  2153. ,@(filter-map-with-index
  2154. (lambda (field i)
  2155. (if (or (eq? (field-name field) '_)
  2156. (not (has-mutator? field)))
  2157. #f
  2158. `(define (unquote
  2159. (symbol-append
  2160. 'set-
  2161. name
  2162. '-
  2163. (field-name field)
  2164. '!))
  2165. ,(symbol-append
  2166. 'set-
  2167. name
  2168. '-
  2169. i
  2170. '!))))
  2171. id1id2))))
  2172. (caar args)
  2173. (reverse g258)
  2174. (reverse g261)
  2175. (reverse g262))
  2176. (g266))
  2177. (if (and (pair? (car g264))
  2178. (field? (caar g264))
  2179. (pair? (cdar g264))
  2180. (null? (cddar g264)))
  2181. (g263 (cdr g264)
  2182. (cons (cadar g264) g262)
  2183. (cons (caar g264) g261))
  2184. (g266))))
  2185. (g266))
  2186. (if (field? (car g260))
  2187. (g259 (cdr g260) (cons (car g260) g258))
  2188. (g266))))
  2189. (g266)))
  2190. (g266)))))
  2191. (define home-directory
  2192. (or (getenv "HOME")
  2193. (error "environment variable HOME is not defined")))
  2194. (defmacro recur args `(let ,@args))
  2195. (defmacro
  2196. rec
  2197. args
  2198. (match args
  2199. (((? symbol? x) v) `(letrec ((,x ,v)) ,x))))
  2200. (defmacro
  2201. parameterize
  2202. args
  2203. (match args ((bindings exp ...) `(begin ,@exp))))
  2204. (define gensym gentemp)
  2205. (define expand-once macroexpand-1)
  2206. (defmacro check-increment-counter args #f)
  2207. (define symbol-append
  2208. (lambda l
  2209. (string->symbol
  2210. (apply string-append
  2211. (map (lambda (x) (format #f "~a" x)) l)))))
  2212. (define gensym gentemp)
  2213. (define andmap
  2214. (lambda (f . lists)
  2215. (cond ((null? (car lists)) (and))
  2216. ((null? (cdr (car lists)))
  2217. (apply f (map car lists)))
  2218. (else
  2219. (and (apply f (map car lists))
  2220. (apply andmap f (map cdr lists)))))))
  2221. (define true-object? (lambda (x) (eq? #t x)))
  2222. (define false-object? (lambda (x) (eq? #f x)))
  2223. (define void (lambda () (cond (#f #f))))
  2224. (defmacro
  2225. when
  2226. args
  2227. (match args
  2228. ((tst body __1)
  2229. `(if ,tst (begin ,@body (void)) (void)))))
  2230. (defmacro
  2231. unless
  2232. args
  2233. (match args
  2234. ((tst body __1)
  2235. `(if ,tst (void) (begin ,@body (void))))))
  2236. (define should-never-reach
  2237. (lambda (form)
  2238. (slib:error "fell off end of " form)))
  2239. (define make-cvector make-vector)
  2240. (define cvector vector)
  2241. (define cvector-length vector-length)
  2242. (define cvector-ref vector-ref)
  2243. (define cvector->list vector->list)
  2244. (define list->cvector list->vector)
  2245. (define-const-structure (record _))
  2246. (defmacro
  2247. record
  2248. args
  2249. (match args
  2250. ((((? symbol? id) exp) ...)
  2251. `(make-record
  2252. (list ,@(map (lambda (i x) `(cons ',i ,x)) id exp))))
  2253. (_ (slib:error "syntax error at " `(record ,@args)))))
  2254. (defmacro
  2255. field
  2256. args
  2257. (match args
  2258. (((? symbol? id) exp)
  2259. `(match ,exp
  2260. (($ record x)
  2261. (match (assq ',id x)
  2262. (#f
  2263. (slib:error
  2264. "no field "
  2265. ,id
  2266. 'in
  2267. (cons 'record (map car x))))
  2268. ((_ . x) x)))
  2269. (_ (slib:error "not a record: " '(field ,id _)))))
  2270. (_ (slib:error "syntax error at " `(field ,@args)))))
  2271. (define-const-structure (module _))
  2272. (defmacro
  2273. module
  2274. args
  2275. (match args
  2276. (((i ...) defs ...)
  2277. `(let ()
  2278. ,@defs
  2279. (make-module
  2280. (record ,@(map (lambda (x) (list x x)) i)))))
  2281. (_ (slib:error "syntax error at " `(module ,@args)))))
  2282. (defmacro
  2283. import
  2284. args
  2285. (match args
  2286. ((((mod defs ...) ...) body __1)
  2287. (let* ((m (map (lambda (_) (gentemp)) mod))
  2288. (newdefs
  2289. (let loop ((mod-names m) (l-defs defs))
  2290. (if (null? mod-names)
  2291. '()
  2292. (append
  2293. (let ((m (car mod-names)))
  2294. (map (match-lambda
  2295. ((? symbol? x) `(,x (field ,x ,m)))
  2296. (((? symbol? i) (? symbol? e))
  2297. `(,i (field ,e ,m)))
  2298. (x (slib:error "ill-formed definition: " x)))
  2299. (car l-defs)))
  2300. (loop (cdr mod-names) (cdr l-defs)))))))
  2301. `(let (unquote
  2302. (map (lambda (m mod)
  2303. `(,m (match ,mod (($ module x) x))))
  2304. m
  2305. mod))
  2306. (let ,newdefs body ...))))))
  2307. (define raise
  2308. (lambda vals
  2309. (slib:error "Unhandled exception " vals)))
  2310. (defmacro
  2311. fluid-let
  2312. args
  2313. (match args
  2314. ((((x val) ...) body __1)
  2315. (let ((old-x (map (lambda (_) (gentemp)) x))
  2316. (swap-x (map (lambda (_) (gentemp)) x))
  2317. (swap (gentemp)))
  2318. `(let ,(map list old-x val)
  2319. (let ((,swap
  2320. (lambda ()
  2321. (let ,(map list swap-x old-x)
  2322. ,@(map (lambda (old x) `(set! ,old ,x)) old-x x)
  2323. ,@(map (lambda (x swap) `(set! ,x ,swap))
  2324. x
  2325. swap-x)))))
  2326. (dynamic-wind ,swap (lambda () ,@body) ,swap)))))
  2327. (_ (slib:error
  2328. "syntax error at "
  2329. `(fluid-let ,@args)))))
  2330. (defmacro
  2331. handle
  2332. args
  2333. (match args
  2334. ((e h)
  2335. (let ((k (gentemp)) (exn (gentemp)))
  2336. `((call-with-current-continuation
  2337. (lambda (k)
  2338. (fluid-let
  2339. ((raise (lambda ,exn (k (lambda () (apply ,h ,exn))))))
  2340. (let ((v ,e)) (lambda () v))))))))
  2341. (_ (slib:error "syntax error in " `(handle ,@args)))))
  2342. (defmacro
  2343. :
  2344. args
  2345. (match args ((typeexp exp) exp)))
  2346. (defmacro
  2347. module:
  2348. args
  2349. (match args
  2350. ((((i type) ...) defs ...)
  2351. `(let ()
  2352. ,@defs
  2353. (make-module
  2354. (record
  2355. ,@(map (lambda (i type) `(,i (: ,type ,i))) i type)))))))
  2356. (defmacro
  2357. define:
  2358. args
  2359. (match args
  2360. ((name type exp) `(define ,name (: ,type ,exp)))))
  2361. (define st:failure
  2362. (lambda (chk fmt . args)
  2363. (slib:error
  2364. (apply format
  2365. #f
  2366. (string-append "~a : " fmt)
  2367. chk
  2368. args))))
  2369. (defmacro
  2370. check-bound
  2371. args
  2372. (match args
  2373. ((var) var)
  2374. (x (st:failure `(check-bound ,@x) "syntax-error"))))
  2375. (defmacro
  2376. clash
  2377. args
  2378. (match args
  2379. ((name info ...) name)
  2380. (x (st:failure `(clash ,@x) "syntax error"))))
  2381. (defmacro
  2382. check-lambda
  2383. args
  2384. (match args
  2385. (((id info ...) (? symbol? args) body __1)
  2386. `(lambda ,args
  2387. (check-increment-counter ,id)
  2388. ,@body))
  2389. (((id info ...) args body __1)
  2390. (let* ((n 0)
  2391. (chk (let loop ((a args) (nargs 0))
  2392. (cond ((pair? a) (loop (cdr a) (+ 1 nargs)))
  2393. ((null? a)
  2394. (set! n nargs)
  2395. `(= ,nargs (length args)))
  2396. (else
  2397. (set! n nargs)
  2398. `(<= ,nargs (length args))))))
  2399. (incr (if (number? id)
  2400. `(check-increment-counter ,id)
  2401. #f)))
  2402. `(let ((lam (lambda ,args ,@body)))
  2403. (lambda args
  2404. ,incr
  2405. (if ,chk
  2406. (apply lam args)
  2407. ,(if (eq? '= (car chk))
  2408. `(st:failure
  2409. '(check-lambda ,id ,@info)
  2410. "requires ~a arguments, passed: ~a"
  2411. ,n
  2412. args)
  2413. `(st:failure
  2414. '(check-lambda ,id ,@info)
  2415. "requires >= ~a arguments, passed: ~a"
  2416. ,n
  2417. args)))))))
  2418. (x (st:failure `(check-lambda ,@x) "syntax error"))))
  2419. (defmacro
  2420. check-ap
  2421. args
  2422. (match args
  2423. (((id info ...) (? symbol? f) args ...)
  2424. `(begin
  2425. (check-increment-counter ,id)
  2426. (if (procedure? ,f)
  2427. (,f ,@args)
  2428. (st:failure
  2429. '(check-ap ,id ,@info)
  2430. "not a procedure: ~a"
  2431. ,f))))
  2432. (((id info ...) f args ...)
  2433. `((lambda (proc . args)
  2434. (check-increment-counter ,id)
  2435. (if (procedure? proc)
  2436. (apply proc args)
  2437. (st:failure
  2438. '(check-ap ,id ,@info)
  2439. "not a procedure: ~a"
  2440. proc)))
  2441. ,f
  2442. ,@args))
  2443. (x (st:failure `(check-ap ,@x) "syntax error"))))
  2444. (defmacro
  2445. check-field
  2446. args
  2447. (match args
  2448. (((id info ...) (? symbol? f) exp)
  2449. `(match ,exp
  2450. (($ record x)
  2451. (match (assq ',f x)
  2452. (#f
  2453. (st:failure
  2454. '(check-field ,id ,@info)
  2455. "no ~a field in (record ~a)"
  2456. ',f
  2457. (map car x)))
  2458. ((_ . x) x)))
  2459. (v (st:failure
  2460. '(check-field ,id ,@info)
  2461. "not a record: ~a"
  2462. v))))
  2463. (x (st:failure `(check-field ,@x) "syntax error"))))
  2464. (defmacro
  2465. check-match
  2466. args
  2467. (match args
  2468. (((id info ...) exp (and clause (pat _ __1)) ...)
  2469. (letrec ((last (lambda (pl)
  2470. (if (null? (cdr pl)) (car pl) (last (cdr pl))))))
  2471. (if (match (last pat)
  2472. ((? symbol?) #t)
  2473. (('and subp ...) (andmap symbol? subp))
  2474. (_ #f))
  2475. `(begin
  2476. (check-increment-counter ,id)
  2477. (match ,exp ,@clause))
  2478. `(begin
  2479. (check-increment-counter ,id)
  2480. (match ,exp
  2481. ,@clause
  2482. (x (st:failure
  2483. '(check-match ,id ,@info)
  2484. "no matching clause for ~a"
  2485. x)))))))
  2486. (x (st:failure `(check-match ,@x) "syntax error"))))
  2487. (defmacro
  2488. check-:
  2489. args
  2490. (match args
  2491. (((id info ...) typeexp exp)
  2492. `(st:failure
  2493. '(check-: ,id ,@info)
  2494. "static type annotation reached"))
  2495. (x (st:failure `(check-: ,@x) "syntax error"))))
  2496. (defmacro
  2497. make-check-typed
  2498. args
  2499. (match args
  2500. ((prim)
  2501. (let ((chkprim (symbol-append 'check- prim)))
  2502. (list 'defmacro
  2503. chkprim
  2504. 'id
  2505. (list 'quasiquote
  2506. `(lambda a
  2507. (check-increment-counter (,'unquote (car id)))
  2508. (if (null? a)
  2509. (,prim)
  2510. (st:failure
  2511. (cons ',chkprim '(,'unquote id))
  2512. "invalid arguments: ~a"
  2513. a)))))))
  2514. ((prim '_)
  2515. (let ((chkprim (symbol-append 'check- prim)))
  2516. (list 'defmacro
  2517. chkprim
  2518. 'id
  2519. (list 'quasiquote
  2520. `(lambda a
  2521. (check-increment-counter (,'unquote (car id)))
  2522. (if (= 1 (length a))
  2523. (,prim (car a))
  2524. (st:failure
  2525. (cons ',chkprim '(,'unquote id))
  2526. "invalid arguments: ~a"
  2527. a)))))))
  2528. ((prim type1)
  2529. (let ((chkprim (symbol-append 'check- prim)))
  2530. (list 'defmacro
  2531. chkprim
  2532. 'id
  2533. (list 'quasiquote
  2534. `(lambda a
  2535. (check-increment-counter (,'unquote (car id)))
  2536. (if (and (= 1 (length a)) (,type1 (car a)))
  2537. (,prim (car a))
  2538. (st:failure
  2539. (cons ',chkprim '(,'unquote id))
  2540. "invalid arguments: ~a"
  2541. a)))))))
  2542. ((prim '_ '_)
  2543. (let ((chkprim (symbol-append 'check- prim)))
  2544. (list 'defmacro
  2545. chkprim
  2546. 'id
  2547. (list 'quasiquote
  2548. `(lambda a
  2549. (check-increment-counter (,'unquote (car id)))
  2550. (if (= 2 (length a))
  2551. (,prim (car a) (cadr a))
  2552. (st:failure
  2553. (cons ',chkprim '(,'unquote id))
  2554. "invalid arguments: ~a"
  2555. a)))))))
  2556. ((prim '_ type2)
  2557. (let ((chkprim (symbol-append 'check- prim)))
  2558. (list 'defmacro
  2559. chkprim
  2560. 'id
  2561. (list 'quasiquote
  2562. `(lambda a
  2563. (check-increment-counter (,'unquote (car id)))
  2564. (if (and (= 2 (length a)) (,type2 (cadr a)))
  2565. (,prim (car a) (cadr a))
  2566. (st:failure
  2567. (cons ',chkprim '(,'unquote id))
  2568. "invalid arguments: ~a"
  2569. a)))))))
  2570. ((prim type1 '_)
  2571. (let ((chkprim (symbol-append 'check- prim)))
  2572. (list 'defmacro
  2573. chkprim
  2574. 'id
  2575. (list 'quasiquote
  2576. `(lambda a
  2577. (check-increment-counter (,'unquote (car id)))
  2578. (if (and (= 2 (length a)) (,type1 (car a)))
  2579. (,prim (car a) (cadr a))
  2580. (st:failure
  2581. (cons ',chkprim '(,'unquote id))
  2582. "invalid arguments: ~a"
  2583. a)))))))
  2584. ((prim type1 type2)
  2585. (let ((chkprim (symbol-append 'check- prim)))
  2586. (list 'defmacro
  2587. chkprim
  2588. 'id
  2589. (list 'quasiquote
  2590. `(lambda a
  2591. (check-increment-counter (,'unquote (car id)))
  2592. (if (and (= 2 (length a))
  2593. (,type1 (car a))
  2594. (,type2 (cadr a)))
  2595. (,prim (car a) (cadr a))
  2596. (st:failure
  2597. (cons ',chkprim '(,'unquote id))
  2598. "invalid arguments: ~a"
  2599. a)))))))
  2600. ((prim types ...)
  2601. (let ((nargs (length types))
  2602. (chkprim (symbol-append 'check- prim))
  2603. (types (map (match-lambda ('_ '(lambda (_) #t)) (x x))
  2604. types)))
  2605. (list 'defmacro
  2606. chkprim
  2607. 'id
  2608. (list 'quasiquote
  2609. `(lambda a
  2610. (check-increment-counter (,'unquote (car id)))
  2611. (if (and (= ,nargs (length a))
  2612. (andmap
  2613. (lambda (f a) (f a))
  2614. (list ,@types)
  2615. a))
  2616. (apply ,prim a)
  2617. (st:failure
  2618. (cons ',chkprim '(,'unquote id))
  2619. "invalid arguments: ~a"
  2620. a)))))))))
  2621. (defmacro
  2622. make-check-selector
  2623. args
  2624. (match args
  2625. ((prim pat)
  2626. (let ((chkprim (symbol-append 'check- prim)))
  2627. (list 'defmacro
  2628. chkprim
  2629. 'id
  2630. (list 'quasiquote
  2631. `(lambda a
  2632. (check-increment-counter (,'unquote (car id)))
  2633. (match a
  2634. ((,pat) x)
  2635. (_ (st:failure
  2636. (cons ',chkprim '(,'unquote id))
  2637. "invalid arguments: ~a"
  2638. a))))))))))
  2639. (make-check-typed number? _)
  2640. (make-check-typed null? _)
  2641. (make-check-typed char? _)
  2642. (make-check-typed symbol? _)
  2643. (make-check-typed string? _)
  2644. (make-check-typed vector? _)
  2645. (make-check-typed box? _)
  2646. (make-check-typed pair? _)
  2647. (make-check-typed procedure? _)
  2648. (make-check-typed eof-object? _)
  2649. (make-check-typed input-port? _)
  2650. (make-check-typed output-port? _)
  2651. (make-check-typed true-object? _)
  2652. (make-check-typed false-object? _)
  2653. (make-check-typed boolean? _)
  2654. (make-check-typed list? _)
  2655. (make-check-typed not _)
  2656. (make-check-typed eqv? _ _)
  2657. (make-check-typed eq? _ _)
  2658. (make-check-typed equal? _ _)
  2659. (make-check-typed cons _ _)
  2660. (make-check-selector car (x . _))
  2661. (make-check-selector cdr (_ . x))
  2662. (make-check-selector caar ((x . _) . _))
  2663. (make-check-selector cadr (_ x . _))
  2664. (make-check-selector cdar ((_ . x) . _))
  2665. (make-check-selector cddr (_ _ . x))
  2666. (make-check-selector caaar (((x . _) . _) . _))
  2667. (make-check-selector caadr (_ (x . _) . _))
  2668. (make-check-selector cadar ((_ x . _) . _))
  2669. (make-check-selector caddr (_ _ x . _))
  2670. (make-check-selector cdaar (((_ . x) . _) . _))
  2671. (make-check-selector cdadr (_ (_ . x) . _))
  2672. (make-check-selector cddar ((_ _ . x) . _))
  2673. (make-check-selector cdddr (_ _ _ . x))
  2674. (make-check-selector
  2675. caaaar
  2676. ((((x . _) . _) . _) . _))
  2677. (make-check-selector
  2678. caaadr
  2679. (_ ((x . _) . _) . _))
  2680. (make-check-selector
  2681. caadar
  2682. ((_ (x . _) . _) . _))
  2683. (make-check-selector caaddr (_ _ (x . _) . _))
  2684. (make-check-selector
  2685. cadaar
  2686. (((_ x . _) . _) . _))
  2687. (make-check-selector cadadr (_ (_ x . _) . _))
  2688. (make-check-selector caddar ((_ _ x . _) . _))
  2689. (make-check-selector cadddr (_ _ _ x . _))
  2690. (make-check-selector
  2691. cdaaar
  2692. ((((_ . x) . _) . _) . _))
  2693. (make-check-selector
  2694. cdaadr
  2695. (_ ((_ . x) . _) . _))
  2696. (make-check-selector
  2697. cdadar
  2698. ((_ (_ . x) . _) . _))
  2699. (make-check-selector cdaddr (_ _ (_ . x) . _))
  2700. (make-check-selector
  2701. cddaar
  2702. (((_ _ . x) . _) . _))
  2703. (make-check-selector cddadr (_ (_ _ . x) . _))
  2704. (make-check-selector cdddar ((_ _ _ . x) . _))
  2705. (make-check-selector cddddr (_ _ _ _ . x))
  2706. (make-check-typed set-car! pair? _)
  2707. (make-check-typed set-cdr! pair? _)
  2708. (defmacro
  2709. check-list
  2710. id
  2711. `(lambda a
  2712. (check-increment-counter ,(car id))
  2713. (apply list a)))
  2714. (make-check-typed length list?)
  2715. (defmacro
  2716. check-append
  2717. id
  2718. `(lambda a
  2719. (check-increment-counter ,(car id))
  2720. (let loop ((b a))
  2721. (match b
  2722. (() #t)
  2723. ((l) #t)
  2724. (((? list?) . y) (loop y))
  2725. (_ (st:failure
  2726. (cons 'check-append ',id)
  2727. "invalid arguments: ~a"
  2728. a))))
  2729. (apply append a)))
  2730. (make-check-typed reverse list?)
  2731. (make-check-typed list-tail list? number?)
  2732. (make-check-typed list-ref list? number?)
  2733. (make-check-typed memq _ list?)
  2734. (make-check-typed memv _ list?)
  2735. (make-check-typed member _ list?)
  2736. (defmacro
  2737. check-assq
  2738. id
  2739. `(lambda a
  2740. (check-increment-counter ,(car id))
  2741. (if (and (= 2 (length a))
  2742. (list? (cadr a))
  2743. (andmap pair? (cadr a)))
  2744. (assq (car a) (cadr a))
  2745. (st:failure
  2746. (cons 'check-assq ',id)
  2747. "invalid arguments: ~a"
  2748. a))))
  2749. (defmacro
  2750. check-assv
  2751. id
  2752. `(lambda a
  2753. (check-increment-counter ,(car id))
  2754. (if (and (= 2 (length a))
  2755. (list? (cadr a))
  2756. (andmap pair? (cadr a)))
  2757. (assv (car a) (cadr a))
  2758. (st:failure
  2759. (cons 'check-assv ',id)
  2760. "invalid arguments: ~a"
  2761. a))))
  2762. (defmacro
  2763. check-assoc
  2764. id
  2765. `(lambda a
  2766. (check-increment-counter ,(car id))
  2767. (if (and (= 2 (length a))
  2768. (list? (cadr a))
  2769. (andmap pair? (cadr a)))
  2770. (assoc (car a) (cadr a))
  2771. (st:failure
  2772. (cons 'check-assoc ',id)
  2773. "invalid arguments: ~a"
  2774. a))))
  2775. (make-check-typed symbol->string symbol?)
  2776. (make-check-typed string->symbol string?)
  2777. (make-check-typed complex? _)
  2778. (make-check-typed real? _)
  2779. (make-check-typed rational? _)
  2780. (make-check-typed integer? _)
  2781. (make-check-typed exact? number?)
  2782. (make-check-typed inexact? number?)
  2783. (defmacro
  2784. check-=
  2785. id
  2786. `(lambda a
  2787. (check-increment-counter ,(car id))
  2788. (if (and (<= 2 (length a)) (andmap number? a))
  2789. (apply = a)
  2790. (st:failure
  2791. (cons 'check-= ',id)
  2792. "invalid arguments: ~a"
  2793. a))))
  2794. (defmacro
  2795. check-<
  2796. id
  2797. `(lambda a
  2798. (check-increment-counter ,(car id))
  2799. (if (and (<= 2 (length a)) (andmap number? a))
  2800. (apply < a)
  2801. (st:failure
  2802. (cons 'check-< ',id)
  2803. "invalid arguments: ~a"
  2804. a))))
  2805. (defmacro
  2806. check->
  2807. id
  2808. `(lambda a
  2809. (check-increment-counter ,(car id))
  2810. (if (and (<= 2 (length a)) (andmap number? a))
  2811. (apply > a)
  2812. (st:failure
  2813. (cons 'check-> ',id)
  2814. "invalid arguments: ~a"
  2815. a))))
  2816. (defmacro
  2817. check-<=
  2818. id
  2819. `(lambda a
  2820. (check-increment-counter ,(car id))
  2821. (if (and (<= 2 (length a)) (andmap number? a))
  2822. (apply <= a)
  2823. (st:failure
  2824. (cons 'check-<= ',id)
  2825. "invalid arguments: ~a"
  2826. a))))
  2827. (defmacro
  2828. check->=
  2829. id
  2830. `(lambda a
  2831. (check-increment-counter ,(car id))
  2832. (if (and (<= 2 (length a)) (andmap number? a))
  2833. (apply >= a)
  2834. (st:failure
  2835. (cons 'check->= ',id)
  2836. "invalid arguments: ~a"
  2837. a))))
  2838. (make-check-typed zero? number?)
  2839. (make-check-typed positive? number?)
  2840. (make-check-typed negative? number?)
  2841. (make-check-typed odd? number?)
  2842. (make-check-typed even? number?)
  2843. (defmacro
  2844. check-max
  2845. id
  2846. `(lambda a
  2847. (check-increment-counter ,(car id))
  2848. (if (and (<= 1 (length a)) (andmap number? a))
  2849. (apply max a)
  2850. (st:failure
  2851. (cons 'check-max ',id)
  2852. "invalid arguments: ~a"
  2853. a))))
  2854. (defmacro
  2855. check-min
  2856. id
  2857. `(lambda a
  2858. (check-increment-counter ,(car id))
  2859. (if (and (<= 1 (length a)) (andmap number? a))
  2860. (apply min a)
  2861. (st:failure
  2862. (cons 'check-min ',id)
  2863. "invalid arguments: ~a"
  2864. a))))
  2865. (defmacro
  2866. check-+
  2867. id
  2868. `(lambda a
  2869. (check-increment-counter ,(car id))
  2870. (if (andmap number? a)
  2871. (apply + a)
  2872. (st:failure
  2873. (cons 'check-+ ',id)
  2874. "invalid arguments: ~a"
  2875. a))))
  2876. (defmacro
  2877. check-*
  2878. id
  2879. `(lambda a
  2880. (check-increment-counter ,(car id))
  2881. (if (andmap number? a)
  2882. (apply * a)
  2883. (st:failure
  2884. (cons 'check-* ',id)
  2885. "invalid arguments: ~a"
  2886. a))))
  2887. (defmacro
  2888. check--
  2889. id
  2890. `(lambda a
  2891. (check-increment-counter ,(car id))
  2892. (if (and (<= 1 (length a)) (andmap number? a))
  2893. (apply - a)
  2894. (st:failure
  2895. (cons 'check-- ',id)
  2896. "invalid arguments: ~a"
  2897. a))))
  2898. (defmacro
  2899. check-/
  2900. id
  2901. `(lambda a
  2902. (check-increment-counter ,(car id))
  2903. (if (and (<= 1 (length a)) (andmap number? a))
  2904. (apply / a)
  2905. (st:failure
  2906. (cons 'check-/ ',id)
  2907. "invalid arguments: ~a"
  2908. a))))
  2909. (make-check-typed abs number?)
  2910. (make-check-typed quotient number? number?)
  2911. (make-check-typed remainder number? number?)
  2912. (make-check-typed modulo number? number?)
  2913. (defmacro
  2914. check-gcd
  2915. id
  2916. `(lambda a
  2917. (check-increment-counter ,(car id))
  2918. (if (andmap number? a)
  2919. (apply gcd a)
  2920. (st:failure
  2921. (cons 'check-gcd ',id)
  2922. "invalid arguments: ~a"
  2923. a))))
  2924. (defmacro
  2925. check-lcm
  2926. id
  2927. `(lambda a
  2928. (check-increment-counter ,(car id))
  2929. (if (andmap number? a)
  2930. (apply lcm a)
  2931. (st:failure
  2932. (cons 'check-lcm ',id)
  2933. "invalid arguments: ~a"
  2934. a))))
  2935. (make-check-typed numerator number?)
  2936. (make-check-typed denominator number?)
  2937. (make-check-typed floor number?)
  2938. (make-check-typed ceiling number?)
  2939. (make-check-typed truncate number?)
  2940. (make-check-typed round number?)
  2941. (make-check-typed rationalize number? number?)
  2942. (make-check-typed exp number?)
  2943. (make-check-typed log number?)
  2944. (make-check-typed sin number?)
  2945. (make-check-typed cos number?)
  2946. (make-check-typed tan number?)
  2947. (make-check-typed asin number?)
  2948. (make-check-typed acos number?)
  2949. (defmacro
  2950. check-atan
  2951. id
  2952. `(lambda a
  2953. (check-increment-counter ,(car id))
  2954. (if (and (andmap number? a)
  2955. (pair? a)
  2956. (>= 2 (length a)))
  2957. (apply atan a)
  2958. (st:failure
  2959. (cons 'check-atan ',id)
  2960. "invalid arguments: ~a"
  2961. a))))
  2962. (make-check-typed sqrt number?)
  2963. (make-check-typed expt number? number?)
  2964. (make-check-typed
  2965. make-rectangular
  2966. number?
  2967. number?)
  2968. (make-check-typed make-polar number? number?)
  2969. (make-check-typed real-part number?)
  2970. (make-check-typed imag-part number?)
  2971. (make-check-typed magnitude number?)
  2972. (make-check-typed angle number?)
  2973. (make-check-typed exact->inexact number?)
  2974. (make-check-typed inexact->exact number?)
  2975. (defmacro
  2976. check-number->string
  2977. id
  2978. `(lambda a
  2979. (check-increment-counter ,(car id))
  2980. (if (and (andmap number? a)
  2981. (pair? a)
  2982. (>= 2 (length a)))
  2983. (apply number->string a)
  2984. (st:failure
  2985. (cons 'check-number->string ',id)
  2986. "invalid arguments: ~a"
  2987. a))))
  2988. (defmacro
  2989. check-string->number
  2990. id
  2991. `(lambda a
  2992. (check-increment-counter ,(car id))
  2993. (if (and (pair? a)
  2994. (string? (car a))
  2995. (>= 2 (length a))
  2996. (or (null? (cdr a)) (number? (cadr a))))
  2997. (apply string->number a)
  2998. (st:failure
  2999. (cons 'check-string->number ',id)
  3000. "invalid arguments: ~a"
  3001. a))))
  3002. (make-check-typed char=? char? char?)
  3003. (make-check-typed char<? char? char?)
  3004. (make-check-typed char>? char? char?)
  3005. (make-check-typed char<=? char? char?)
  3006. (make-check-typed char>=? char? char?)
  3007. (make-check-typed char-ci=? char? char?)
  3008. (make-check-typed char-ci<? char? char?)
  3009. (make-check-typed char-ci>? char? char?)
  3010. (make-check-typed char-ci<=? char? char?)
  3011. (make-check-typed char-ci>=? char? char?)
  3012. (make-check-typed char-alphabetic? char?)
  3013. (make-check-typed char-numeric? char?)
  3014. (make-check-typed char-whitespace? char?)
  3015. (make-check-typed char-upper-case? char?)
  3016. (make-check-typed char-lower-case? char?)
  3017. (make-check-typed char->integer char?)
  3018. (make-check-typed integer->char number?)
  3019. (make-check-typed char-upcase char?)
  3020. (make-check-typed char-downcase char?)
  3021. (defmacro
  3022. check-make-string
  3023. id
  3024. `(lambda a
  3025. (check-increment-counter ,(car id))
  3026. (if (and (pair? a)
  3027. (number? (car a))
  3028. (>= 2 (length a))
  3029. (or (null? (cdr a)) (char? (cadr a))))
  3030. (apply make-string a)
  3031. (st:failure
  3032. (cons 'check-make-string ',id)
  3033. "invalid arguments: ~a"
  3034. a))))
  3035. (defmacro
  3036. check-string
  3037. id
  3038. `(lambda a
  3039. (check-increment-counter ,(car id))
  3040. (if (andmap char? a)
  3041. (apply string a)
  3042. (st:failure
  3043. (cons 'check-string ',id)
  3044. "invalid arguments: ~a"
  3045. a))))
  3046. (make-check-typed string-length string?)
  3047. (make-check-typed string-ref string? number?)
  3048. (make-check-typed
  3049. string-set!
  3050. string?
  3051. number?
  3052. char?)
  3053. (make-check-typed string=? string? string?)
  3054. (make-check-typed string<? string? string?)
  3055. (make-check-typed string>? string? string?)
  3056. (make-check-typed string<=? string? string?)
  3057. (make-check-typed string>=? string? string?)
  3058. (make-check-typed string-ci=? string? string?)
  3059. (make-check-typed string-ci<? string? string?)
  3060. (make-check-typed string-ci>? string? string?)
  3061. (make-check-typed string-ci<=? string? string?)
  3062. (make-check-typed string-ci>=? string? string?)
  3063. (make-check-typed
  3064. substring
  3065. string?
  3066. number?
  3067. number?)
  3068. (defmacro
  3069. check-string-append
  3070. id
  3071. `(lambda a
  3072. (check-increment-counter ,(car id))
  3073. (if (andmap string? a)
  3074. (apply string-append a)
  3075. (st:failure
  3076. (cons 'check-string-append ',id)
  3077. "invalid arguments: ~a"
  3078. a))))
  3079. (make-check-typed string->list string?)
  3080. (defmacro
  3081. check-list->string
  3082. id
  3083. `(lambda a
  3084. (check-increment-counter ,(car id))
  3085. (if (and (= 1 (length a))
  3086. (list? (car a))
  3087. (andmap char? (car a)))
  3088. (list->string (car a))
  3089. (st:failure
  3090. (cons 'check-list->string ',id)
  3091. "invalid arguments: ~a"
  3092. a))))
  3093. (make-check-typed string-copy string?)
  3094. (make-check-typed string-fill! string? char?)
  3095. (make-check-typed make-vector number? _)
  3096. (defmacro
  3097. check-vector
  3098. id
  3099. `(lambda a
  3100. (check-increment-counter ,(car id))
  3101. (apply vector a)))
  3102. (make-check-typed vector-length vector?)
  3103. (make-check-typed vector-ref vector? number?)
  3104. (make-check-typed vector-set! vector? number? _)
  3105. (make-check-typed vector->list vector?)
  3106. (make-check-typed list->vector list?)
  3107. (make-check-typed vector-fill! vector? _)
  3108. (defmacro
  3109. check-apply
  3110. id
  3111. `(lambda a
  3112. (check-increment-counter ,(car id))
  3113. (if (pair? a)
  3114. (let loop ((arg (cdr a)))
  3115. (match arg
  3116. (((? list?)) (apply apply a))
  3117. ((_ . y) (loop y))
  3118. (_ (st:failure
  3119. (cons 'check-apply ',id)
  3120. "invalid arguments: ~a"
  3121. a))))
  3122. (st:failure
  3123. `(check-apply ,@id)
  3124. "invalid arguments: ~a"
  3125. a))))
  3126. (defmacro
  3127. check-map
  3128. id
  3129. `(lambda a
  3130. (check-increment-counter ,(car id))
  3131. (if (and (<= 2 (length a))
  3132. (procedure? (car a))
  3133. (andmap list? (cdr a)))
  3134. (apply map a)
  3135. (st:failure
  3136. (cons 'check-map ',id)
  3137. "invalid arguments: ~a"
  3138. a))))
  3139. (defmacro
  3140. check-for-each
  3141. id
  3142. `(lambda a
  3143. (check-increment-counter ,(car id))
  3144. (if (and (<= 2 (length a))
  3145. (procedure? (car a))
  3146. (andmap list? (cdr a)))
  3147. (apply for-each a)
  3148. (st:failure
  3149. (cons 'check-for-each ',id)
  3150. "invalid arguments: ~a"
  3151. a))))
  3152. (make-check-typed force procedure?)
  3153. (defmacro
  3154. check-call-with-current-continuation
  3155. id
  3156. `(lambda a
  3157. (check-increment-counter ,(car id))
  3158. (if (and (= 1 (length a)) (procedure? (car a)))
  3159. (call-with-current-continuation
  3160. (lambda (k)
  3161. ((car a) (check-lambda (continuation) (x) (k x)))))
  3162. (st:failure
  3163. (cons 'check-call-with-current-continuation ',id)
  3164. "invalid arguments: ~a"
  3165. a))))
  3166. (make-check-typed
  3167. call-with-input-file
  3168. string?
  3169. procedure?)
  3170. (make-check-typed
  3171. call-with-output-file
  3172. string?
  3173. procedure?)
  3174. (make-check-typed input-port? _)
  3175. (make-check-typed output-port? _)
  3176. (make-check-typed current-input-port)
  3177. (make-check-typed current-output-port)
  3178. (make-check-typed
  3179. with-input-from-file
  3180. string?
  3181. procedure?)
  3182. (make-check-typed
  3183. with-output-to-file
  3184. string?
  3185. procedure?)
  3186. (make-check-typed open-input-file string?)
  3187. (make-check-typed open-output-file string?)
  3188. (make-check-typed close-input-port input-port?)
  3189. (make-check-typed close-output-port output-port?)
  3190. (defmacro
  3191. check-read
  3192. id
  3193. `(lambda a
  3194. (check-increment-counter ,(car id))
  3195. (if (or (null? a)
  3196. (and (= 1 (length a)) (input-port? (car a))))
  3197. (apply read a)
  3198. (st:failure
  3199. (cons 'check-read ',id)
  3200. "invalid arguments: ~a"
  3201. a))))
  3202. (defmacro
  3203. check-read-char
  3204. id
  3205. `(lambda a
  3206. (check-increment-counter ,(car id))
  3207. (if (or (null? a)
  3208. (and (= 1 (length a)) (input-port? (car a))))
  3209. (apply read-char a)
  3210. (st:failure
  3211. (cons 'check-read-char ',id)
  3212. "invalid arguments: ~a"
  3213. a))))
  3214. (defmacro
  3215. check-peek-char
  3216. id
  3217. `(lambda a
  3218. (check-increment-counter ,(car id))
  3219. (if (or (null? a)
  3220. (and (= 1 (length a)) (input-port? (car a))))
  3221. (apply peek-char a)
  3222. (st:failure
  3223. (cons 'check-peek-char ',id)
  3224. "invalid arguments: ~a"
  3225. a))))
  3226. (defmacro
  3227. check-char-ready?
  3228. id
  3229. `(lambda a
  3230. (check-increment-counter ,(car id))
  3231. (if (or (null? a)
  3232. (and (= 1 (length a)) (input-port? (car a))))
  3233. (apply char-ready? a)
  3234. (st:failure
  3235. (cons 'check-char-ready? ',id)
  3236. "invalid arguments: ~a"
  3237. a))))
  3238. (defmacro
  3239. check-write
  3240. id
  3241. `(lambda a
  3242. (check-increment-counter ,(car id))
  3243. (if (and (pair? a)
  3244. (or (null? (cdr a)) (output-port? (cadr a))))
  3245. (apply write a)
  3246. (st:failure
  3247. (cons 'check-write ',id)
  3248. "invalid arguments: ~a"
  3249. a))))
  3250. (defmacro
  3251. check-display
  3252. id
  3253. `(lambda a
  3254. (check-increment-counter ,(car id))
  3255. (if (and (pair? a)
  3256. (or (null? (cdr a)) (output-port? (cadr a))))
  3257. (apply display a)
  3258. (st:failure
  3259. (cons 'check-display ',id)
  3260. "invalid arguments: ~a"
  3261. a))))
  3262. (defmacro
  3263. check-newline
  3264. id
  3265. `(lambda a
  3266. (check-increment-counter ,(car id))
  3267. (if (or (null? a) (output-port? (car a)))
  3268. (apply newline a)
  3269. (st:failure
  3270. (cons 'check-newline ',id)
  3271. "invalid arguments: ~a"
  3272. a))))
  3273. (defmacro
  3274. check-write-char
  3275. id
  3276. `(lambda a
  3277. (check-increment-counter ,(car id))
  3278. (if (and (pair? a)
  3279. (char? (car a))
  3280. (or (null? (cdr a)) (output-port? (cadr a))))
  3281. (apply write-char a)
  3282. (st:failure
  3283. (cons 'check-write-char ',id)
  3284. "invalid arguments: ~a"
  3285. a))))
  3286. (make-check-typed load string?)
  3287. (make-check-typed transcript-on string?)
  3288. (make-check-typed transcript-off)
  3289. (defmacro
  3290. check-symbol-append
  3291. id
  3292. `(lambda a
  3293. (check-increment-counter ,(car id))
  3294. (apply symbol-append a)))
  3295. (make-check-typed box _)
  3296. (make-check-typed unbox box?)
  3297. (make-check-typed set-box! box? _)
  3298. (make-check-typed void)
  3299. (make-check-typed make-module _)
  3300. (defmacro
  3301. check-match:error
  3302. id
  3303. `(lambda a
  3304. (check-increment-counter ,(car id))
  3305. (if (pair? a)
  3306. (apply match:error a)
  3307. (st:failure
  3308. (cons 'check-match:error ',id)
  3309. "invalid arguments: ~a"
  3310. a))))
  3311. (make-check-typed should-never-reach symbol?)
  3312. (defmacro
  3313. check-make-cvector
  3314. id
  3315. `(lambda a
  3316. (check-increment-counter ,(car id))
  3317. (if (and (pair? a)
  3318. (number? (car a))
  3319. (= 2 (length a)))
  3320. (apply make-cvector a)
  3321. (st:failure
  3322. (cons 'check-make-cvector ',id)
  3323. "invalid arguments: ~a"
  3324. a))))
  3325. (defmacro
  3326. check-cvector
  3327. id
  3328. `(lambda a
  3329. (check-increment-counter ,(car id))
  3330. (apply cvector a)))
  3331. (make-check-typed cvector-length cvector?)
  3332. (make-check-typed cvector-ref cvector? number?)
  3333. (make-check-typed cvector->list cvector?)
  3334. (make-check-typed list->cvector list?)
  3335. (defmacro
  3336. check-define-const-structure
  3337. args
  3338. (let ((field?
  3339. (lambda (x)
  3340. (or (symbol? x)
  3341. (and (pair? x)
  3342. (equal? (car x) '!)
  3343. (pair? (cdr x))
  3344. (symbol? (cadr x))
  3345. (null? (cddr x))))))
  3346. (arg-name
  3347. (lambda (x) (if (symbol? x) x (cadr x))))
  3348. (with-mutator? (lambda (x) (not (symbol? x)))))
  3349. (match args
  3350. ((((? symbol? name) (? field? id1) ...))
  3351. (let ((constructor (symbol-append 'make- name))
  3352. (check-constructor
  3353. (symbol-append 'check-make- name))
  3354. (predicate (symbol-append name '?))
  3355. (access
  3356. (let loop ((l id1))
  3357. (cond ((null? l) '())
  3358. ((eq? '_ (arg-name (car l))) (loop (cdr l)))
  3359. (else
  3360. (cons (symbol-append name '- (arg-name (car l)))
  3361. (loop (cdr l)))))))
  3362. (assign
  3363. (let loop ((l id1))
  3364. (cond ((null? l) '())
  3365. ((eq? '_ (arg-name (car l))) (loop (cdr l)))
  3366. ((not (with-mutator? (car l))) (loop (cdr l)))
  3367. (else
  3368. (cons (symbol-append
  3369. 'set-
  3370. name
  3371. '-
  3372. (arg-name (car l))
  3373. '!)
  3374. (loop (cdr l)))))))
  3375. (nargs (length id1)))
  3376. `(begin
  3377. (define-const-structure (,name ,@id1) ())
  3378. (defmacro
  3379. ,check-constructor
  3380. id
  3381. (lambda a
  3382. (check-increment-counter (,'unquote (car id)))
  3383. (if (= ,nargs (length a))
  3384. (apply ,constructor a)
  3385. (st:failure
  3386. (cons ',check-constructor '(,'unquote id))
  3387. "invalid arguments: ~a"
  3388. a))))
  3389. (make-check-typed ,predicate _)
  3390. ,@(map (lambda (a) `(make-check-typed ,a ,predicate))
  3391. access)
  3392. ,@(map (lambda (a) `(make-check-typed ,a ,predicate _))
  3393. assign))))
  3394. (x (st:failure
  3395. `(check-define-const-structure ,@x)
  3396. "syntax error")))))
  3397. (if (equal? '(match 1) (macroexpand-1 '(match 1)))
  3398. (load "/home/wright/scheme/match/match-slib.scm"))
  3399. (define sprintf
  3400. (lambda args (apply format #f args)))
  3401. (define printf
  3402. (lambda args (apply format #t args)))
  3403. (define disaster
  3404. (lambda (context fmt . args)
  3405. (slib:error
  3406. (apply sprintf
  3407. (string-append "in ~a: " fmt)
  3408. context
  3409. args))))
  3410. (define use-error
  3411. (lambda (fmt . args)
  3412. (slib:error (apply sprintf fmt args))))
  3413. (define syntax-err
  3414. (lambda (context fmt . args)
  3415. (newline)
  3416. (if context (pretty-print context))
  3417. (slib:error
  3418. (apply sprintf
  3419. (string-append "in syntax: " fmt)
  3420. args))))
  3421. (define flush-output force-output)
  3422. (define print-context
  3423. (lambda (obj depth)
  3424. (pretty-print
  3425. (recur loop
  3426. ((obj obj) (n 0))
  3427. (if (pair? obj)
  3428. (if (< n depth)
  3429. (cons (loop (car obj) (+ 1 n))
  3430. (loop (cdr obj) n))
  3431. '(...))
  3432. obj)))))
  3433. (define *box-tag* (gensym))
  3434. (define box (lambda (a) (cons *box-tag* a)))
  3435. (define box?
  3436. (lambda (b)
  3437. (and (pair? b) (eq? (car b) *box-tag*))))
  3438. (define unbox cdr)
  3439. (define box-1 cdr)
  3440. (define set-box! set-cdr!)
  3441. (define sort-list sort)
  3442. (define expand-once-if-macro
  3443. (lambda (e)
  3444. (and (macro? (car e)) (macroexpand-1 e))))
  3445. (define ormap
  3446. (lambda (f . lists)
  3447. (if (null? (car lists))
  3448. (or)
  3449. (or (apply f (map car lists))
  3450. (apply ormap f (map cdr lists))))))
  3451. (define call/cc call-with-current-continuation)
  3452. (define (cpu-time) 0)
  3453. (define (pretty-print x) (display x) (newline))
  3454. (define clock-granularity 1.0e-3)
  3455. (define set-vector! vector-set!)
  3456. (define set-string! string-set!)
  3457. (define maplr
  3458. (lambda (f l)
  3459. (match l
  3460. (() '())
  3461. ((x . y) (let ((v (f x))) (cons v (maplr f y)))))))
  3462. (define maprl
  3463. (lambda (f l)
  3464. (match l
  3465. (() '())
  3466. ((x . y) (let ((v (maprl f y))) (cons (f x) v))))))
  3467. (define foldl
  3468. (lambda (f i l)
  3469. (recur loop
  3470. ((l l) (acc i))
  3471. (match l (() acc) ((x . y) (loop y (f x acc)))))))
  3472. (define foldr
  3473. (lambda (f i l)
  3474. (recur loop
  3475. ((l l))
  3476. (match l (() i) ((x . y) (f x (loop y)))))))
  3477. (define filter
  3478. (lambda (p l)
  3479. (match l
  3480. (() '())
  3481. ((x . y)
  3482. (if (p x) (cons x (filter p y)) (filter p y))))))
  3483. (define filter-map
  3484. (lambda (p l)
  3485. (match l
  3486. (() '())
  3487. ((x . y)
  3488. (match (p x)
  3489. (#f (filter-map p y))
  3490. (x (cons x (filter-map p y))))))))
  3491. (define rac
  3492. (lambda (l)
  3493. (match l ((last) last) ((_ . rest) (rac rest)))))
  3494. (define rdc
  3495. (lambda (l)
  3496. (match l
  3497. ((_) '())
  3498. ((x . rest) (cons x (rdc rest))))))
  3499. (define map-with-n
  3500. (lambda (f l)
  3501. (recur loop
  3502. ((l l) (n 0))
  3503. (match l
  3504. (() '())
  3505. ((x . y)
  3506. (let ((v (f x n))) (cons v (loop y (+ 1 n)))))))))
  3507. (define readfile
  3508. (lambda (f)
  3509. (with-input-from-file
  3510. f
  3511. (letrec ((rf (lambda ()
  3512. (match (read)
  3513. ((? eof-object?) '())
  3514. (sexp (cons sexp (rf)))))))
  3515. rf))))
  3516. (define map2
  3517. (lambda (f a b)
  3518. (match (cons a b)
  3519. ((()) '())
  3520. (((ax . ay) bx . by)
  3521. (let ((v (f ax bx))) (cons v (map2 f ay by))))
  3522. (else (error 'map2 "lists differ in length")))))
  3523. (define for-each2
  3524. (lambda (f a b)
  3525. (match (cons a b)
  3526. ((()) (void))
  3527. (((ax . ay) bx . by)
  3528. (f ax bx)
  3529. (for-each2 f ay by))
  3530. (else (error 'for-each2 "lists differ in length")))))
  3531. (define andmap2
  3532. (lambda (f a b)
  3533. (match (cons a b)
  3534. ((()) (and))
  3535. (((ax) bx) (f ax bx))
  3536. (((ax . ay) bx . by)
  3537. (and (f ax bx) (andmap2 f ay by)))
  3538. (else (error 'andmap2 "lists differ in length")))))
  3539. (define ormap2
  3540. (lambda (f a b)
  3541. (match (cons a b)
  3542. ((()) (or))
  3543. (((ax) bx) (f ax bx))
  3544. (((ax . ay) bx . by)
  3545. (or (f ax bx) (ormap2 f ay by)))
  3546. (else (error 'ormap2 "lists differ in length")))))
  3547. (define empty-set '())
  3548. (define empty-set? null?)
  3549. (define set (lambda l (list->set l)))
  3550. (define list->set
  3551. (match-lambda
  3552. (() '())
  3553. ((x . y)
  3554. (if (memq x y)
  3555. (list->set y)
  3556. (cons x (list->set y))))))
  3557. (define element-of?
  3558. (lambda (x set) (and (memq x set) #t)))
  3559. (define cardinality length)
  3560. (define set<=
  3561. (lambda (a b)
  3562. (foldr (lambda (a-elt acc) (and acc (memq a-elt b) #t))
  3563. (and)
  3564. a)))
  3565. (define set-eq?
  3566. (lambda (a b)
  3567. (and (= (cardinality a) (cardinality b))
  3568. (set<= a b))))
  3569. (define union2
  3570. (lambda (a b)
  3571. (if (null? b)
  3572. a
  3573. (foldr (lambda (x b) (if (memq x b) b (cons x b)))
  3574. b
  3575. a))))
  3576. (define union (lambda l (foldr union2 '() l)))
  3577. (define setdiff2
  3578. (lambda (a b)
  3579. (if (null? b)
  3580. a
  3581. (foldr (lambda (x c) (if (memq x b) c (cons x c)))
  3582. '()
  3583. a))))
  3584. (define setdiff
  3585. (lambda l
  3586. (if (null? l)
  3587. '()
  3588. (setdiff2 (car l) (foldr union2 '() (cdr l))))))
  3589. (define intersect2
  3590. (lambda (a b)
  3591. (if (null? b)
  3592. a
  3593. (foldr (lambda (x c) (if (memq x b) (cons x c) c))
  3594. '()
  3595. a))))
  3596. (define intersect
  3597. (lambda l
  3598. (if (null? l) '() (foldl intersect2 (car l) l))))
  3599. (define-const-structure (some _))
  3600. (define-const-structure (none))
  3601. (define none (make-none))
  3602. (define some make-some)
  3603. (define-const-structure (and exps))
  3604. (define-const-structure (app exp exps))
  3605. (define-const-structure (begin exps))
  3606. (define-const-structure (const val pred))
  3607. (define-const-structure (if exp1 exp2 exp3))
  3608. (define-const-structure (lam names body))
  3609. (define-const-structure (let binds body))
  3610. (define-const-structure (let* binds body))
  3611. (define-const-structure (letr binds body))
  3612. (define-const-structure (or exps))
  3613. (define-const-structure (prim name))
  3614. (define-const-structure (delay exp))
  3615. (define-const-structure (set! (! name) exp))
  3616. (define-const-structure (var (! name)))
  3617. (define-const-structure (vlam names name body))
  3618. (define-const-structure (match exp mclauses))
  3619. (define-const-structure (record binds))
  3620. (define-const-structure (field name exp))
  3621. (define-const-structure (cast type exp))
  3622. (define-const-structure (body defs exps))
  3623. (define-const-structure (bind name exp))
  3624. (define-const-structure (mclause pat body fail))
  3625. (define-const-structure (pvar name))
  3626. (define-const-structure (pany))
  3627. (define-const-structure (pelse))
  3628. (define-const-structure (pconst name pred))
  3629. (define-const-structure (pobj name pats))
  3630. (define-const-structure (ppred name))
  3631. (define-const-structure (pand pats))
  3632. (define-const-structure (pnot pat))
  3633. (define-const-structure (define name (! exp)))
  3634. (define-const-structure
  3635. (defstruct
  3636. tag
  3637. args
  3638. make
  3639. pred
  3640. get
  3641. set
  3642. getn
  3643. setn
  3644. mutable))
  3645. (define-const-structure (datatype _))
  3646. (define-const-structure
  3647. (variant con pred arg-types))
  3648. (define-structure
  3649. (name name
  3650. ty
  3651. timestamp
  3652. occ
  3653. mutated
  3654. gdef
  3655. primitive
  3656. struct
  3657. pure
  3658. predicate
  3659. variant
  3660. selector))
  3661. (define-structure (type ty exp))
  3662. (define-const-structure (shape _ _))
  3663. (define-const-structure (check _ _))
  3664. (define parse-def
  3665. (lambda (def)
  3666. (let ((parse-name
  3667. (match-lambda
  3668. ((? symbol? s)
  3669. (if (keyword? s)
  3670. (syntax-err def "invalid use of keyword ~a" s)
  3671. s))
  3672. (n (syntax-err def "invalid variable at ~a" n)))))
  3673. (match def
  3674. (('extend-syntax ((? symbol? name) . _) . _)
  3675. (printf
  3676. "Note: installing but _not_ checking (extend-syntax (~a) ...)~%"
  3677. name)
  3678. (eval def)
  3679. '())
  3680. (('extend-syntax . _)
  3681. (syntax-err def "invalid syntax"))
  3682. (('defmacro (? symbol? name) . _)
  3683. (printf
  3684. "Note: installing but _not_ checking (defmacro ~a ...)~%"
  3685. name)
  3686. (eval def)
  3687. '())
  3688. (('defmacro . _)
  3689. (syntax-err def "invalid syntax"))
  3690. (('define (? symbol? n) e)
  3691. (list (make-define (parse-name n) (parse-exp e))))
  3692. (('define (n . args) . body)
  3693. (list (make-define
  3694. (parse-name n)
  3695. (parse-exp `(lambda ,args ,@body)))))
  3696. (('define . _) (syntax-err def "at define"))
  3697. (('begin . defs)
  3698. (foldr append '() (smap parse-def defs)))
  3699. (('define-structure (n . args))
  3700. (parse-def `(define-structure (,n ,@args) ())))
  3701. (('define-structure (n . args) inits)
  3702. (let ((m-args (smap (lambda (x) `(! ,x)) args))
  3703. (m-inits
  3704. (smap (match-lambda
  3705. ((x e) `((! ,x) ,e))
  3706. (_ (syntax-err
  3707. def
  3708. "invalid structure initializer")))
  3709. inits)))
  3710. (parse-def
  3711. `(define-const-structure (,n ,@m-args) ,m-inits))))
  3712. (('define-const-structure ((? symbol? n) . args))
  3713. (parse-def
  3714. `(define-const-structure (,n ,@args) ())))
  3715. (('define-const-structure
  3716. ((? symbol? n) . args)
  3717. ())
  3718. (letrec ((smap-with-n
  3719. (lambda (f l)
  3720. (recur loop
  3721. ((l l) (n 0))
  3722. (match l
  3723. (() '())
  3724. ((x . y)
  3725. (let ((v (f x n)))
  3726. (cons v (loop y (+ 1 n)))))
  3727. (_ (syntax-err l "invalid list"))))))
  3728. (parse-arg
  3729. (lambda (a index)
  3730. (match a
  3731. (('! '_)
  3732. (list none
  3733. none
  3734. (some (symbol-append
  3735. n
  3736. '-
  3737. (+ index 1)))
  3738. (some (symbol-append
  3739. 'set-
  3740. n
  3741. '-
  3742. (+ index 1)
  3743. '!))
  3744. #t))
  3745. (('! a)
  3746. (let ((a (parse-name a)))
  3747. (list (some (symbol-append n '- a))
  3748. (some (symbol-append
  3749. 'set-
  3750. n
  3751. '-
  3752. a
  3753. '!))
  3754. (some (symbol-append
  3755. n
  3756. '-
  3757. (+ index 1)))
  3758. (some (symbol-append
  3759. 'set-
  3760. n
  3761. '-
  3762. (+ index 1)
  3763. '!))
  3764. #t)))
  3765. ('_
  3766. (list none
  3767. none
  3768. (some (symbol-append
  3769. n
  3770. '-
  3771. (+ index 1)))
  3772. none
  3773. #f))
  3774. (a (let ((a (parse-name a)))
  3775. (list (some (symbol-append n '- a))
  3776. none
  3777. (some (symbol-append
  3778. n
  3779. '-
  3780. (+ index 1)))
  3781. none
  3782. #f)))))))
  3783. (let* ((arg-info (smap-with-n parse-arg args))
  3784. (get (map car arg-info))
  3785. (set (map cadr arg-info))
  3786. (getn (map caddr arg-info))
  3787. (setn (map cadddr arg-info))
  3788. (mutable
  3789. (map (lambda (x) (car (cddddr x))) arg-info)))
  3790. (list (make-defstruct
  3791. n
  3792. (cons n args)
  3793. (symbol-append 'make- n)
  3794. (symbol-append n '?)
  3795. get
  3796. set
  3797. getn
  3798. setn
  3799. mutable)))))
  3800. (('define-const-structure
  3801. ((? symbol? n) . args)
  3802. inits)
  3803. (syntax-err
  3804. def
  3805. "sorry, structure initializers are not supported"))
  3806. (('datatype . d)
  3807. (let* ((parse-variant
  3808. (match-lambda
  3809. (((? symbol? con) ? list? args)
  3810. (let ((n (parse-name con)))
  3811. (make-variant
  3812. (symbol-append 'make- n)
  3813. (symbol-append n '?)
  3814. (cons con args))))
  3815. (_ (syntax-err def "invalid datatype syntax"))))
  3816. (parse-dt
  3817. (match-lambda
  3818. (((? symbol? type) . variants)
  3819. (cons (list (parse-name type))
  3820. (smap parse-variant variants)))
  3821. ((((? symbol? type) ? list? targs) . variants)
  3822. (cons (cons (parse-name type)
  3823. (smap parse-name targs))
  3824. (smap parse-variant variants)))
  3825. (_ (syntax-err def "invalid datatype syntax")))))
  3826. (list (make-datatype (smap parse-dt d)))))
  3827. (((? symbol? k) . _)
  3828. (cond ((and (not (keyword? k))
  3829. (expand-once-if-macro def))
  3830. =>
  3831. parse-def)
  3832. (else (list (make-define #f (parse-exp def))))))
  3833. (_ (list (make-define #f (parse-exp def))))))))
  3834. (define keep-match #t)
  3835. (define parse-exp
  3836. (lambda (expression)
  3837. (letrec ((n-primitive (string->symbol "#primitive"))
  3838. (parse-exp
  3839. (match-lambda
  3840. (('quote (? symbol? s)) (make-const s 'symbol?))
  3841. ((and m ('quote _)) (parse-exp (quote-tf m)))
  3842. ((and m ('quasiquote _))
  3843. (parse-exp (quasiquote-tf m)))
  3844. ((and m (? box?)) (parse-exp (quote-tf m)))
  3845. ((and m (? vector?)) (parse-exp (quote-tf m)))
  3846. ((and m ('cond . _)) (parse-exp (cond-tf m)))
  3847. ((and m ('case . _)) (parse-exp (case-tf m)))
  3848. ((and m ('do . _)) (parse-exp (do-tf m)))
  3849. ((? symbol? s) (make-var (parse-name s)))
  3850. (#t (make-const #t 'true-object?))
  3851. (#f (make-const #f 'false-object?))
  3852. ((? null? c) (make-const c 'null?))
  3853. ((? number? c) (make-const c 'number?))
  3854. ((? char? c) (make-const c 'char?))
  3855. ((? string? c) (make-const c 'string?))
  3856. ((': ty e1) (make-cast ty (parse-exp e1)))
  3857. ((and exp ('record . bind))
  3858. (let ((bindings (smap parse-bind bind)))
  3859. (no-repeats (map bind-name bindings) exp)
  3860. (make-record bindings)))
  3861. ((and exp ('field name e1))
  3862. (make-field (parse-name name) (parse-exp e1)))
  3863. ((and exp ('match e clause0 . clauses))
  3864. (=> fail)
  3865. (if keep-match
  3866. (let* ((e2 (parse-exp e))
  3867. (parse-clause
  3868. (match-lambda
  3869. ((p ('=> (? symbol? failsym)) . body)
  3870. (make-mclause
  3871. (parse-pat p expression)
  3872. (parse-body
  3873. `((let ((,failsym (lambda () (,failsym))))
  3874. ,@body)))
  3875. failsym))
  3876. ((p . body)
  3877. (make-mclause
  3878. (parse-pat p expression)
  3879. (parse-body body)
  3880. #f))
  3881. (_ (syntax-err exp "invalid match clause")))))
  3882. (make-match
  3883. e2
  3884. (smap parse-clause (cons clause0 clauses))))
  3885. (fail)))
  3886. ((and exp ('lambda bind . body))
  3887. (recur loop
  3888. ((b bind) (names '()))
  3889. (match b
  3890. ((? symbol? n)
  3891. (let ((rest (parse-name n)))
  3892. (no-repeats (cons rest names) exp)
  3893. (make-vlam
  3894. (reverse names)
  3895. rest
  3896. (parse-body body))))
  3897. (()
  3898. (no-repeats names exp)
  3899. (make-lam (reverse names) (parse-body body)))
  3900. ((n . x) (loop x (cons (parse-name n) names)))
  3901. (_ (syntax-err
  3902. exp
  3903. "invalid lambda expression")))))
  3904. (('if e1 e2 e3)
  3905. (make-if
  3906. (parse-exp e1)
  3907. (parse-exp e2)
  3908. (parse-exp e3)))
  3909. ((and if-expr ('if e1 e2))
  3910. (printf "Note: one-armed if: ")
  3911. (print-context if-expr 2)
  3912. (make-if
  3913. (parse-exp e1)
  3914. (parse-exp e2)
  3915. (parse-exp '(void))))
  3916. (('delay e) (make-delay (parse-exp e)))
  3917. (('set! n e)
  3918. (make-set! (parse-name n) (parse-exp e)))
  3919. (('and . args) (make-and (smap parse-exp args)))
  3920. (('or . args) (make-or (smap parse-exp args)))
  3921. ((and exp ('let (? symbol? n) bind . body))
  3922. (let* ((nb (parse-name n))
  3923. (bindings (smap parse-bind bind)))
  3924. (no-repeats (map bind-name bindings) exp)
  3925. (make-app
  3926. (make-letr
  3927. (list (make-bind
  3928. nb
  3929. (make-lam
  3930. (map bind-name bindings)
  3931. (parse-body body))))
  3932. (make-body '() (list (make-var nb))))
  3933. (map bind-exp bindings))))
  3934. ((and exp ('let bind . body))
  3935. (let ((bindings (smap parse-bind bind)))
  3936. (no-repeats (map bind-name bindings) exp)
  3937. (make-let bindings (parse-body body))))
  3938. (('let* bind . body)
  3939. (make-let*
  3940. (smap parse-bind bind)
  3941. (parse-body body)))
  3942. ((and exp ('letrec bind . body))
  3943. (let ((bindings (smap parse-bind bind)))
  3944. (no-repeats (map bind-name bindings) exp)
  3945. (make-letr bindings (parse-body body))))
  3946. (('begin e1 . rest)
  3947. (make-begin (smap parse-exp (cons e1 rest))))
  3948. (('define . _)
  3949. (syntax-err
  3950. expression
  3951. "invalid context for internal define"))
  3952. (('define-structure . _)
  3953. (syntax-err
  3954. expression
  3955. "invalid context for internal define-structure"))
  3956. (('define-const-structure . _)
  3957. (syntax-err
  3958. expression
  3959. "invalid context for internal define-const-structure"))
  3960. ((and m (f . args))
  3961. (cond ((and (eq? f n-primitive)
  3962. (match args
  3963. (((? symbol? p)) (make-prim p))
  3964. (_ #f))))
  3965. ((and (symbol? f)
  3966. (not (keyword? f))
  3967. (expand-once-if-macro m))
  3968. =>
  3969. parse-exp)
  3970. (else
  3971. (make-app (parse-exp f) (smap parse-exp args)))))
  3972. (x (syntax-err
  3973. expression
  3974. "invalid expression at ~a"
  3975. x))))
  3976. (parse-name
  3977. (match-lambda
  3978. ((? symbol? s)
  3979. (when (keyword? s)
  3980. (syntax-err
  3981. expression
  3982. "invalid use of keyword ~a"
  3983. s))
  3984. s)
  3985. (n (syntax-err
  3986. expression
  3987. "invalid variable at ~a"
  3988. n))))
  3989. (parse-bind
  3990. (match-lambda
  3991. ((x e) (make-bind (parse-name x) (parse-exp e)))
  3992. (b (syntax-err expression "invalid binding at ~a" b))))
  3993. (parse-body
  3994. (lambda (body)
  3995. (recur loop
  3996. ((b body) (defs '()))
  3997. (match b
  3998. (((and d ('define . _)) . rest)
  3999. (loop rest (append defs (parse-def d))))
  4000. (((and d ('define-structure . _)) . rest)
  4001. (loop rest (append defs (parse-def d))))
  4002. (((and d ('define-const-structure . _)) . rest)
  4003. (loop rest (append defs (parse-def d))))
  4004. ((('begin) . rest) (loop rest defs))
  4005. (((and beg ('begin ('define . _) . _)) . rest)
  4006. (loop rest (append defs (parse-def beg))))
  4007. (((and beg ('begin ('define-structure . _) . _))
  4008. .
  4009. rest)
  4010. (loop rest (append defs (parse-def beg))))
  4011. (((and beg
  4012. ('begin
  4013. ('define-const-structure . _)
  4014. .
  4015. _))
  4016. .
  4017. rest)
  4018. (loop rest (append defs (parse-def beg))))
  4019. ((_ . _) (make-body defs (smap parse-exp b)))
  4020. (_ (syntax-err
  4021. expression
  4022. "invalid body at ~a"
  4023. b))))))
  4024. (no-repeats
  4025. (lambda (l exp)
  4026. (match l
  4027. (() #f)
  4028. ((_) #f)
  4029. ((x . l)
  4030. (if (memq x l)
  4031. (syntax-err exp "name ~a repeated" x)
  4032. (no-repeats l exp)))))))
  4033. (parse-exp expression))))
  4034. (define parse-pat
  4035. (lambda (pat expression)
  4036. (letrec ((parse-pat
  4037. (match-lambda
  4038. (#f (make-ppred 'false-object?))
  4039. (#t (make-ppred 'true-object?))
  4040. (() (make-ppred 'null?))
  4041. ((? number? c) (make-pconst c 'number?))
  4042. ((? char? c) (make-pconst c 'char?))
  4043. ((? string? c) (make-pconst c 'string?))
  4044. (('quote x) (parse-quote x))
  4045. ('_ (make-pany))
  4046. ('else (make-pelse))
  4047. ((? symbol? n) (make-pvar (parse-pname n)))
  4048. (('not . pats)
  4049. (syntax-err
  4050. expression
  4051. "not patterns are not supported"))
  4052. (('or . pats)
  4053. (syntax-err
  4054. expression
  4055. "or patterns are not supported"))
  4056. (('get! . pats)
  4057. (syntax-err
  4058. expression
  4059. "get! patterns are not supported"))
  4060. (('set! . pats)
  4061. (syntax-err
  4062. expression
  4063. "set! patterns are not supported"))
  4064. (('and . pats)
  4065. (let* ((pats (smap parse-pat pats))
  4066. (p (make-flat-pand pats))
  4067. (non-var?
  4068. (match-lambda
  4069. ((? pvar?) #f)
  4070. ((? pany?) #f)
  4071. (_ #t))))
  4072. (match p
  4073. (($ pand pats)
  4074. (when (< 1 (length (filter non-var? pats)))
  4075. (syntax-err
  4076. expression
  4077. "~a has conflicting subpatterns"
  4078. (ppat p))))
  4079. (_ #f))
  4080. p))
  4081. (('? (? symbol? pred) p)
  4082. (parse-pat `(and (? ,pred) ,p)))
  4083. (('? (? symbol? pred))
  4084. (if (keyword? pred)
  4085. (syntax-err
  4086. expression
  4087. "invalid use of keyword ~a"
  4088. pred)
  4089. (make-ppred pred)))
  4090. (('$ (? symbol? c) . args)
  4091. (if (memq c '(? _ $))
  4092. (syntax-err
  4093. expression
  4094. "invalid use of pattern keyword ~a"
  4095. c)
  4096. (make-pobj
  4097. (symbol-append c '?)
  4098. (smap parse-pat args))))
  4099. ((? box? cb)
  4100. (make-pobj 'box? (list (parse-pat (unbox cb)))))
  4101. ((x . y)
  4102. (make-pobj
  4103. 'pair?
  4104. (list (parse-pat x) (parse-pat y))))
  4105. ((? vector? v)
  4106. (make-pobj
  4107. 'vector?
  4108. (map parse-pat (vector->list v))))
  4109. (m (syntax-err expression "invalid pattern at ~a" m))))
  4110. (parse-quote
  4111. (match-lambda
  4112. (#f (make-pobj 'false-object? '()))
  4113. (#t (make-pobj 'true-object? '()))
  4114. (() (make-pobj 'null? '()))
  4115. ((? number? c) (make-pconst c 'number?))
  4116. ((? char? c) (make-pconst c 'char?))
  4117. ((? string? c) (make-pconst c 'string?))
  4118. ((? symbol? s) (make-pconst s 'symbol?))
  4119. ((? box? cb)
  4120. (make-pobj 'box? (list (parse-quote (unbox cb)))))
  4121. ((x . y)
  4122. (make-pobj
  4123. 'pair?
  4124. (list (parse-quote x) (parse-quote y))))
  4125. ((? vector? v)
  4126. (make-pobj
  4127. 'vector?
  4128. (map parse-quote (vector->list v))))
  4129. (m (syntax-err expression "invalid pattern at ~a" m))))
  4130. (parse-pname
  4131. (match-lambda
  4132. ((? symbol? s)
  4133. (cond ((keyword? s)
  4134. (syntax-err
  4135. expression
  4136. "invalid use of keyword ~a"
  4137. s))
  4138. ((memq s '(? _ else $ and or not set! get! ...))
  4139. (syntax-err
  4140. expression
  4141. "invalid use of pattern keyword ~a"
  4142. s))
  4143. (else s)))
  4144. (n (syntax-err
  4145. expression
  4146. "invalid pattern variable at ~a"
  4147. n)))))
  4148. (parse-pat pat))))
  4149. (define smap
  4150. (lambda (f l)
  4151. (match l
  4152. (() '())
  4153. ((x . r) (let ((v (f x))) (cons v (smap f r))))
  4154. (_ (syntax-err l "invalid list")))))
  4155. (define primitive
  4156. (lambda (p)
  4157. (list (string->symbol "#primitive") p)))
  4158. (define keyword?
  4159. (lambda (s)
  4160. (or (memq s
  4161. '(=> and
  4162. begin
  4163. case
  4164. cond
  4165. do
  4166. define
  4167. delay
  4168. if
  4169. lambda
  4170. let
  4171. let*
  4172. letrec
  4173. or
  4174. quasiquote
  4175. quote
  4176. set!
  4177. unquote
  4178. unquote-splicing
  4179. define-structure
  4180. define-const-structure
  4181. record
  4182. field
  4183. :
  4184. datatype))
  4185. (and keep-match (eq? s 'match)))))
  4186. (define make-flat-pand
  4187. (lambda (pats)
  4188. (let* ((l (foldr (lambda (p plist)
  4189. (match p
  4190. (($ pand pats) (append pats plist))
  4191. (_ (cons p plist))))
  4192. '()
  4193. pats))
  4194. (concrete?
  4195. (match-lambda
  4196. ((? pconst?) #t)
  4197. ((? pobj?) #t)
  4198. ((? ppred?) #t)
  4199. (_ #f)))
  4200. (sorted
  4201. (append
  4202. (filter concrete? l)
  4203. (filter (lambda (x) (not (concrete? x))) l))))
  4204. (match sorted ((p) p) (_ (make-pand sorted))))))
  4205. (define never-counter 0)
  4206. (define reinit-macros!
  4207. (lambda () (set! never-counter 0)))
  4208. (define cond-tf
  4209. (lambda (cond-expr)
  4210. (recur loop
  4211. ((e (cdr cond-expr)))
  4212. (match e
  4213. (()
  4214. (begin
  4215. (set! never-counter (+ 1 never-counter))
  4216. `(,(primitive 'should-never-reach)
  4217. '(cond ,never-counter))))
  4218. ((('else b1 . body)) `(begin ,b1 ,@body))
  4219. ((('else . _) . _)
  4220. (syntax-err cond-expr "invalid cond expression"))
  4221. (((test '=> proc) . rest)
  4222. (let ((g (gensym)))
  4223. `(let ((,g ,test))
  4224. (if ,g (,proc ,g) ,(loop rest)))))
  4225. (((#t b1 . body)) `(begin ,b1 ,@body))
  4226. (((test) . rest) `(or ,test ,(loop rest)))
  4227. (((test . body) . rest)
  4228. `(if ,test (begin ,@body) ,(loop rest)))
  4229. (_ (syntax-err cond-expr "invalid cond expression"))))))
  4230. (define scheme-cond-tf
  4231. (lambda (cond-expr)
  4232. (recur loop
  4233. ((e (cdr cond-expr)))
  4234. (match e
  4235. (() `(,(primitive 'void)))
  4236. ((('else b1 . body)) `(begin ,b1 ,@body))
  4237. ((('else . _) . _)
  4238. (syntax-err cond-expr "invalid cond expression"))
  4239. (((test '=> proc) . rest)
  4240. (let ((g (gensym)))
  4241. `(let ((,g ,test))
  4242. (if ,g (,proc ,g) ,(loop rest)))))
  4243. (((#t b1 . body)) `(begin ,b1 ,@body))
  4244. (((test) . rest) `(or ,test ,(loop rest)))
  4245. (((test . body) . rest)
  4246. `(if ,test (begin ,@body) ,(loop rest)))
  4247. (_ (syntax-err cond-expr "invalid cond expression"))))))
  4248. (define case-tf
  4249. (lambda (case-expr)
  4250. (recur loop
  4251. ((e (cdr case-expr)))
  4252. (match e
  4253. ((exp) `(begin ,exp (,(primitive 'void))))
  4254. ((exp ('else b1 . body)) `(begin ,b1 ,@body))
  4255. ((exp ('else . _) . _)
  4256. (syntax-err case-expr "invalid case expression"))
  4257. (((? symbol? exp)
  4258. ((? list? test) b1 . body)
  4259. .
  4260. rest)
  4261. `(if (,(primitive 'memv) ,exp ',test)
  4262. (begin ,b1 ,@body)
  4263. ,(loop (cons exp rest))))
  4264. (((? symbol? exp) (test b1 . body) . rest)
  4265. `(if (,(primitive 'memv) ,exp '(,test))
  4266. (begin ,b1 ,@body)
  4267. ,(loop (cons exp rest))))
  4268. ((exp . rest)
  4269. (if (not (symbol? exp))
  4270. (let ((g (gensym)))
  4271. `(let ((,g ,exp)) ,(loop (cons g rest))))
  4272. (syntax-err case-expr "invalid case expression")))
  4273. (_ (syntax-err case-expr "invalid case expression"))))))
  4274. (define conslimit 8)
  4275. (define quote-tf
  4276. (lambda (exp)
  4277. (letrec ((qloop (match-lambda
  4278. ((? box? q)
  4279. `(,(primitive qbox) ,(qloop (unbox q))))
  4280. ((? symbol? q) `',q)
  4281. ((? null? q) q)
  4282. ((? list? q)
  4283. (if (< (length q) conslimit)
  4284. `(,(primitive qcons)
  4285. ,(qloop (car q))
  4286. ,(qloop (cdr q)))
  4287. `(,(primitive qlist) ,@(map qloop q))))
  4288. ((x . y)
  4289. `(,(primitive qcons) ,(qloop x) ,(qloop y)))
  4290. ((? vector? q)
  4291. `(,(primitive qvector)
  4292. ,@(map qloop (vector->list q))))
  4293. ((? boolean? q) q)
  4294. ((? number? q) q)
  4295. ((? char? q) q)
  4296. ((? string? q) q)
  4297. (q (syntax-err
  4298. exp
  4299. "invalid quote expression at ~a"
  4300. q)))))
  4301. (match exp
  4302. (('quote q) (qloop q))
  4303. ((? vector? q) (qloop q))
  4304. ((? box? q) (qloop q))))))
  4305. (define quasiquote-tf
  4306. (lambda (exp)
  4307. (letrec ((make-cons
  4308. (lambda (x y)
  4309. (cond ((null? y) `(,(primitive 'list) ,x))
  4310. ((and (pair? y)
  4311. (equal? (car y) (primitive 'list)))
  4312. (cons (car y) (cons x (cdr y))))
  4313. (else `(,(primitive 'cons) ,x ,y)))))
  4314. (qloop (lambda (e n)
  4315. (match e
  4316. (('quasiquote e)
  4317. (make-cons 'quasiquote (qloop `(,e) (+ 1 n))))
  4318. (('unquote e)
  4319. (if (zero? n)
  4320. e
  4321. (make-cons 'unquote (qloop `(,e) (- n 1)))))
  4322. (('unquote-splicing e)
  4323. (if (zero? n)
  4324. e
  4325. (make-cons
  4326. 'unquote-splicing
  4327. (qloop `(,e) (- n 1)))))
  4328. ((('unquote-splicing e) . y)
  4329. (=> fail)
  4330. (if (zero? n)
  4331. (if (null? y)
  4332. e
  4333. `(,(primitive 'append) ,e ,(qloop y n)))
  4334. (fail)))
  4335. ((? box? q)
  4336. `(,(primitive 'box) ,(qloop (unbox q) n)))
  4337. ((? symbol? q)
  4338. (if (memq q
  4339. '(quasiquote unquote unquote-splicing))
  4340. (syntax-err
  4341. exp
  4342. "invalid use of ~a inside quasiquote"
  4343. q)
  4344. `',q))
  4345. ((? null? q) q)
  4346. ((x . y) (make-cons (qloop x n) (qloop y n)))
  4347. ((? vector? q)
  4348. `(,(primitive 'vector)
  4349. ,@(map (lambda (z) (qloop z n))
  4350. (vector->list q))))
  4351. ((? boolean? q) q)
  4352. ((? number? q) q)
  4353. ((? char? q) q)
  4354. ((? string? q) q)
  4355. (q (syntax-err
  4356. exp
  4357. "invalid quasiquote expression at ~a"
  4358. q))))))
  4359. (match exp (('quasiquote q) (qloop q 0))))))
  4360. (define do-tf
  4361. (lambda (do-expr)
  4362. (recur loop
  4363. ((e (cdr do-expr)))
  4364. (match e
  4365. (((? list? vis) (e0 ? list? e1) ? list? c)
  4366. (if (andmap (match-lambda ((_ _ . _) #t) (_ #f)) vis)
  4367. (let* ((var (map car vis))
  4368. (init (map cadr vis))
  4369. (step (map cddr vis))
  4370. (step (map (lambda (v s)
  4371. (match s
  4372. (() v)
  4373. ((e) e)
  4374. (_ (syntax-err
  4375. do-expr
  4376. "invalid do expression"))))
  4377. var
  4378. step)))
  4379. (let ((doloop (gensym)))
  4380. (match e1
  4381. (()
  4382. `(let ,doloop
  4383. ,(map list var init)
  4384. (if (not ,e0)
  4385. (begin ,@c (,doloop ,@step) (void))
  4386. (void))))
  4387. ((body0 ? list? body)
  4388. `(let ,doloop
  4389. ,(map list var init)
  4390. (if ,e0
  4391. (begin ,body0 ,@body)
  4392. (begin ,@c (,doloop ,@step)))))
  4393. (_ (syntax-err
  4394. do-expr
  4395. "invalid do expression")))))
  4396. (syntax-err do-expr "invalid do expression")))
  4397. (_ (syntax-err do-expr "invalid do expression"))))))
  4398. (define empty-env '())
  4399. (define lookup
  4400. (lambda (env x)
  4401. (match (assq x env)
  4402. (#f (disaster 'lookup "no binding for ~a" x))
  4403. ((_ . b) b))))
  4404. (define lookup?
  4405. (lambda (env x)
  4406. (match (assq x env) (#f #f) ((_ . b) b))))
  4407. (define bound?
  4408. (lambda (env x)
  4409. (match (assq x env) (#f #f) (_ #t))))
  4410. (define extend-env
  4411. (lambda (env x v) (cons (cons x v) env)))
  4412. (define extend-env*
  4413. (lambda (env xs vs)
  4414. (append (map2 cons xs vs) env)))
  4415. (define join-env
  4416. (lambda (env newenv) (append newenv env)))
  4417. (define populated #t)
  4418. (define pseudo #f)
  4419. (define global-error #f)
  4420. (define share #f)
  4421. (define matchst #f)
  4422. (define fullsharing #t)
  4423. (define dump-depths #f)
  4424. (define flags #t)
  4425. (define-structure
  4426. (c depth kind fsym pres args next))
  4427. (define-structure
  4428. (v depth kind name vis split inst))
  4429. (define-structure (ts type n-gen))
  4430. (define-structure (k name order args))
  4431. (define top (box 'top))
  4432. (define bot (box 'bot))
  4433. (define generic? (lambda (d) (< d 0)))
  4434. (define new-type
  4435. (lambda (s d)
  4436. (let ((t (box s)))
  4437. (vector-set!
  4438. types
  4439. d
  4440. (cons t (vector-ref types d)))
  4441. t)))
  4442. (define generate-counter
  4443. (lambda ()
  4444. (let ((n 0)) (lambda () (set! n (+ 1 n)) n))))
  4445. (define var-counter (generate-counter))
  4446. (define make-raw-tvar
  4447. (lambda (d k) (make-v d k var-counter #t #f #f)))
  4448. (define make-tvar
  4449. (lambda (d k) (new-type (make-raw-tvar d k) d)))
  4450. (define ord? (lambda (k) (eq? 'ord k)))
  4451. (define abs? (lambda (k) (eq? 'abs k)))
  4452. (define pre? (lambda (k) (eq? 'pre k)))
  4453. (define ord-depth 2)
  4454. (define depth ord-depth)
  4455. (define types (make-vector 16 '()))
  4456. (define reset-types!
  4457. (lambda ()
  4458. (set! depth ord-depth)
  4459. (set! types (make-vector 16 '()))))
  4460. (define push-level
  4461. (lambda ()
  4462. (set! depth (+ depth 1))
  4463. (when (< (vector-length types) (+ 1 depth))
  4464. (set! types
  4465. (let ((l (vector->list types)))
  4466. (list->vector
  4467. (append l (map (lambda (_) '()) l))))))))
  4468. (define pop-level
  4469. (lambda ()
  4470. (vector-set! types depth '())
  4471. (set! depth (- depth 1))))
  4472. (define v-ord (lambda () (make-tvar depth 'ord)))
  4473. (define v-abs (lambda () (make-tvar depth 'abs)))
  4474. (define v-pre (lambda () (make-tvar depth 'pre)))
  4475. (define tvar v-ord)
  4476. (define out1tvar
  4477. (lambda () (make-tvar (- depth 1) 'ord)))
  4478. (define monotvar
  4479. (lambda () (make-tvar ord-depth 'ord)))
  4480. (define pvar
  4481. (match-lambda
  4482. (($ box (and x ($ v d k _ vis _ _)))
  4483. (unless
  4484. (number? (v-name x))
  4485. (set-v-name! x ((v-name x))))
  4486. (string->symbol
  4487. (sprintf
  4488. "~a~a~a"
  4489. (match k
  4490. ('ord
  4491. (if (generic? d)
  4492. (if vis "X" "x")
  4493. (if vis "Z" "z")))
  4494. ('abs (if vis "A" "a"))
  4495. ('pre (if vis "P" "p")))
  4496. (v-name x)
  4497. (if dump-depths (sprintf ".~a" d) ""))))))
  4498. (define make-tvar-like
  4499. (match-lambda
  4500. (($ box ($ v d k _ _ _ _)) (make-tvar d k))))
  4501. (define ind*
  4502. (lambda (t)
  4503. (match (unbox t)
  4504. ((? box? u)
  4505. (let ((v (ind* u))) (set-box! t v) v))
  4506. (_ t))))
  4507. (define type-check?
  4508. (match-lambda
  4509. ((abs def inexhaust once _)
  4510. (cond (((if once check-abs1? check-abs?) abs)
  4511. (if (and def (definite? def)) 'def #t))
  4512. (inexhaust 'inexhaust)
  4513. (else #f)))))
  4514. (define type-check1?
  4515. (match-lambda
  4516. ((abs def inexhaust _ _)
  4517. (cond ((check-abs1? abs)
  4518. (if (and def (definite? def)) 'def #t))
  4519. (inexhaust 'inexhaust)
  4520. (else #f)))))
  4521. (define check-abs?
  4522. (lambda (vlist)
  4523. (letrec ((seen '())
  4524. (labs? (lambda (t)
  4525. (match t
  4526. (($ box ($ v _ _ _ _ _ inst))
  4527. (and inst
  4528. (not (memq t seen))
  4529. (begin
  4530. (set! seen (cons t seen))
  4531. (ormap (match-lambda ((t . _) (labs? t)))
  4532. inst))))
  4533. (($ box ($ c _ _ _ p _ n))
  4534. (or (labs? p) (labs? n)))
  4535. (($ box (? symbol?)) #t)
  4536. (($ box i) (labs? i))))))
  4537. (ormap labs? vlist))))
  4538. (define check-abs1?
  4539. (lambda (vlist)
  4540. (letrec ((labs1?
  4541. (lambda (t)
  4542. (match t
  4543. (($ box (? v?)) #f)
  4544. (($ box ($ c _ _ _ p _ n))
  4545. (or (labs1? p) (labs1? n)))
  4546. (($ box (? symbol?)) #t)
  4547. (($ box i) (labs1? i))))))
  4548. (ormap labs1? vlist))))
  4549. (define check-sources
  4550. (lambda (info)
  4551. (letrec ((seen '())
  4552. (lsrcs (lambda (t source)
  4553. (match t
  4554. (($ box ($ v _ k _ _ _ inst))
  4555. (union (if (and inst (not (memq t seen)))
  4556. (begin
  4557. (set! seen (cons t seen))
  4558. (foldr union
  4559. empty-set
  4560. (map (match-lambda
  4561. ((t . s) (lsrcs t s)))
  4562. inst)))
  4563. empty-set)))
  4564. (($ box ($ c _ _ _ p _ n))
  4565. (union (lsrcs p source) (lsrcs n source)))
  4566. (($ box (? symbol?))
  4567. (if source (set source) empty-set))
  4568. (($ box i) (lsrcs i source))))))
  4569. (match-let
  4570. (((abs _ _ _ _) info))
  4571. (if (eq? #t abs)
  4572. empty-set
  4573. (foldr union
  4574. empty-set
  4575. (map (lambda (t) (lsrcs t #f)) abs)))))))
  4576. (define check-local-sources
  4577. (match-lambda ((_ _ _ _ component) component)))
  4578. (define mk-definite-prim
  4579. (match-lambda
  4580. (($ box ($ c _ _ x p a n))
  4581. (if (eq? (k-name x) '?->)
  4582. (let ((seen '()))
  4583. (recur lprim
  4584. ((t (car a)))
  4585. (match t
  4586. (($ box ($ c _ _ x p a n))
  4587. (if (memq t seen)
  4588. '()
  4589. (begin
  4590. (set! seen (cons t seen))
  4591. (match (k-name x)
  4592. ('noarg (cons p (lprim n)))
  4593. ('arg
  4594. (let ((args (recur argloop
  4595. ((a (car a)))
  4596. (match a
  4597. (($ box
  4598. ($ c
  4599. _
  4600. _
  4601. _
  4602. p
  4603. _
  4604. n))
  4605. (cons p
  4606. (argloop
  4607. n)))
  4608. (($ box
  4609. ($ v
  4610. _
  4611. k
  4612. _
  4613. _
  4614. _
  4615. _))
  4616. (if (ord? k)
  4617. (list a)
  4618. '()))
  4619. (($ box
  4620. (? symbol?))
  4621. '())
  4622. (($ box i)
  4623. (argloop i))))))
  4624. (cons (list p args (lprim (cadr a)))
  4625. (lprim n))))))))
  4626. (($ box ($ v _ k _ _ _ _))
  4627. (if (ord? k) (list t) '()))
  4628. (($ box (? symbol?)) '())
  4629. (($ box i) (lprim i)))))
  4630. (mk-definite-prim n)))
  4631. (($ box (? v?)) '())
  4632. (($ box (? symbol?)) '())
  4633. (($ box i) (mk-definite-prim i))))
  4634. (define mk-definite-app
  4635. (match-lambda
  4636. (($ box ($ c _ _ _ p _ _)) (list p))))
  4637. (define mk-definite-lam
  4638. (match-lambda
  4639. (($ box ($ c _ _ x p a n))
  4640. (if (eq? (k-name x) '?->)
  4641. (let ((seen '()))
  4642. (recur llam
  4643. ((t (car a)))
  4644. (match t
  4645. (($ box ($ c _ _ x p a n))
  4646. (if (memq t seen)
  4647. '()
  4648. (begin
  4649. (set! seen (cons t seen))
  4650. (match (k-name x)
  4651. ('noarg (cons p (llam n)))
  4652. ('arg
  4653. (let ((args (list top)))
  4654. (cons (list p args (llam (cadr a)))
  4655. (llam n))))))))
  4656. (($ box ($ v _ k _ _ _ _))
  4657. (if (ord? k) (list t) '()))
  4658. (($ box (? symbol?)) '())
  4659. (($ box i) (llam i)))))
  4660. (mk-definite-lam n)))
  4661. (($ box (? v?)) '())
  4662. (($ box (? symbol?)) '())
  4663. (($ box i) (mk-definite-lam i))))
  4664. (define definite?
  4665. (lambda (def-info)
  4666. (letrec ((non-empty?
  4667. (lambda (t)
  4668. (let ((seen '()))
  4669. (recur ldef
  4670. ((t t))
  4671. (match t
  4672. (($ box ($ c _ _ _ p _ n))
  4673. (or (ldef p) (ldef n)))
  4674. (($ box ($ v d k _ _ _ inst))
  4675. (if (or global-error (abs? k))
  4676. (and inst
  4677. (generic? d)
  4678. (not (memq t seen))
  4679. (begin
  4680. (set! seen (cons t seen))
  4681. (ormap (match-lambda
  4682. ((t . _) (ldef t)))
  4683. inst)))
  4684. (generic? d)))
  4685. (($ box 'top) #t)
  4686. (($ box 'bot) #f)
  4687. (($ box i) (ldef i)))))))
  4688. (ok (lambda (l)
  4689. (ormap (match-lambda
  4690. ((? box? t) (non-empty? t))
  4691. ((p arg rest)
  4692. (and (non-empty? p)
  4693. (ormap non-empty? arg)
  4694. (ok rest))))
  4695. l))))
  4696. (not (ok def-info)))))
  4697. (define close
  4698. (lambda (t-list) (close-type t-list #f)))
  4699. (define closeall
  4700. (lambda (t) (car (close-type (list t) #t))))
  4701. (define for
  4702. (lambda (from to f)
  4703. (cond ((= from to) (f from))
  4704. ((< from to)
  4705. (begin (f from) (for (+ from 1) to f)))
  4706. (else #f))))
  4707. (define close-type
  4708. (lambda (t-list all?)
  4709. (let* ((sorted (make-vector (+ depth 1) '()))
  4710. (sort (lambda (t)
  4711. (match t
  4712. (($ box ($ c d _ _ _ _ _))
  4713. (vector-set!
  4714. sorted
  4715. d
  4716. (cons t (vector-ref sorted d))))
  4717. (($ box ($ v d _ _ _ _ _))
  4718. (vector-set!
  4719. sorted
  4720. d
  4721. (cons t (vector-ref sorted d))))
  4722. (_ #f))))
  4723. (prop-d
  4724. (lambda (down)
  4725. (letrec ((pr (match-lambda
  4726. (($ box (and x ($ v d _ _ _ _ _)))
  4727. (when (< down d) (set-v-depth! x down)))
  4728. (($ box (and x ($ c d _ _ p a n)))
  4729. (when (< down d)
  4730. (set-c-depth! x down)
  4731. (pr p)
  4732. (for-each pr a)
  4733. (pr n)))
  4734. (($ box (? symbol?)) #f)
  4735. (z (pr (ind* z))))))
  4736. (match-lambda
  4737. (($ box (and x ($ c d _ _ p a n)))
  4738. (when (<= down d) (pr p) (for-each pr a) (pr n)))
  4739. (_ #f)))))
  4740. (prop-k
  4741. (lambda (t)
  4742. (let ((pk (lambda (kind)
  4743. (rec pr
  4744. (match-lambda
  4745. (($ box (and x ($ v _ k _ _ _ _)))
  4746. (when (kind< kind k) (set-v-kind! x kind)))
  4747. (($ box (and x ($ c _ k _ p a n)))
  4748. (when (kind< kind k)
  4749. (set-c-kind! x kind)
  4750. (pr p)
  4751. (unless populated (for-each pr a))
  4752. (pr n)))
  4753. (($ box (? symbol?)) #f)
  4754. (z (pr (ind* z))))))))
  4755. (match t
  4756. (($ box (and x ($ c _ k _ p a n)))
  4757. (when (not (ord? k))
  4758. (let ((prop (pk k)))
  4759. (prop p)
  4760. (unless populated (for-each prop a))
  4761. (prop n))))
  4762. (_ #f)))))
  4763. (might-be-generalized?
  4764. (match-lambda
  4765. (($ box ($ v d k _ _ _ _))
  4766. (and (<= depth d) (or populated (ord? k) all?)))
  4767. (($ box ($ c d k _ _ _ _))
  4768. (and (<= depth d) (or populated (ord? k) all?)))
  4769. (($ box (? symbol?)) #f)))
  4770. (leaves '())
  4771. (depth-of
  4772. (match-lambda
  4773. (($ box ($ v d _ _ _ _ _)) d)
  4774. (($ box ($ c d _ _ _ _ _)) d)))
  4775. (vector-grow
  4776. (lambda (v)
  4777. (let* ((n (vector-length v))
  4778. (v2 (make-vector (* n 2) '())))
  4779. (recur loop
  4780. ((i 0))
  4781. (when (< i n)
  4782. (vector-set! v2 i (vector-ref v i))
  4783. (loop (+ 1 i))))
  4784. v2)))
  4785. (parents (make-vector 64 '()))
  4786. (parent-index 0)
  4787. (parents-of
  4788. (lambda (t)
  4789. (let ((d (depth-of t)))
  4790. (if (< depth d)
  4791. (vector-ref parents (- (- d depth) 1))
  4792. '()))))
  4793. (xtnd-parents!
  4794. (lambda (t parent)
  4795. (match t
  4796. (($ box (and x ($ v d _ _ _ _ _)))
  4797. (when (= d depth)
  4798. (set! parent-index (+ 1 parent-index))
  4799. (set-v-depth! x (+ depth parent-index))
  4800. (when (< (vector-length parents) parent-index)
  4801. (set! parents (vector-grow parents)))
  4802. (set! d (+ depth parent-index)))
  4803. (vector-set!
  4804. parents
  4805. (- (- d depth) 1)
  4806. (cons parent
  4807. (vector-ref parents (- (- d depth) 1)))))
  4808. (($ box (and x ($ c d _ _ _ _ _)))
  4809. (when (= d depth)
  4810. (set! parent-index (+ 1 parent-index))
  4811. (set-c-depth! x (+ depth parent-index))
  4812. (when (< (vector-length parents) parent-index)
  4813. (set! parents (vector-grow parents)))
  4814. (set! d (+ depth parent-index)))
  4815. (vector-set!
  4816. parents
  4817. (- (- d depth) 1)
  4818. (cons parent
  4819. (vector-ref parents (- (- d depth) 1))))))))
  4820. (needs-cleanup '())
  4821. (revtype
  4822. (rec revtype
  4823. (lambda (parent t)
  4824. (let ((t (ind* t)))
  4825. (cond ((not (might-be-generalized? t)) #f)
  4826. ((null? (parents-of t))
  4827. (xtnd-parents! t parent)
  4828. (set! needs-cleanup (cons t needs-cleanup))
  4829. (match t
  4830. (($ box (? v?))
  4831. (set! leaves (cons t leaves)))
  4832. (($ box ($ c _ _ _ p a n))
  4833. (let ((rev (lambda (q) (revtype t q))))
  4834. (rev p)
  4835. (for-each rev a)
  4836. (rev n)))))
  4837. ((not (memq parent (parents-of t)))
  4838. (xtnd-parents! t parent))
  4839. (else #f))))))
  4840. (generic-index 0)
  4841. (gen (rec gen
  4842. (lambda (t)
  4843. (let ((t (ind* t)))
  4844. (when (might-be-generalized? t)
  4845. (set! generic-index (- generic-index 1))
  4846. (let ((parents (parents-of t)))
  4847. (match t
  4848. (($ box (and x ($ v _ k _ _ _ _)))
  4849. (set-v-depth! x generic-index)
  4850. (when (and populated
  4851. (or global-error
  4852. (abs? k)
  4853. (pre? k))
  4854. (not all?))
  4855. (set-v-inst! x '())))
  4856. (($ box (? c? x))
  4857. (set-c-depth! x generic-index)))
  4858. (for-each gen parents)))))))
  4859. (cleanup
  4860. (match-lambda
  4861. (($ box (and x ($ v d _ _ _ _ _)))
  4862. (unless (< d 0) (set-v-depth! x (- depth 1))))
  4863. (($ box (and x ($ c d _ _ _ _ _)))
  4864. (unless (< d 0) (set-c-depth! x (- depth 1))))))
  4865. (gen2 (rec gen
  4866. (lambda (t)
  4867. (let ((t (ind* t)))
  4868. (when (might-be-generalized? t)
  4869. (set! generic-index (- generic-index 1))
  4870. (match t
  4871. (($ box (and x ($ v _ k _ _ _ _)))
  4872. (set-v-depth! x generic-index)
  4873. (when (and populated
  4874. (or global-error
  4875. (abs? k)
  4876. (pre? k))
  4877. (not all?))
  4878. (set-v-inst! x '())))
  4879. (($ box (and x ($ c _ _ _ p a n)))
  4880. (set-c-depth! x generic-index)
  4881. (gen p)
  4882. (for-each gen a)
  4883. (gen n))))))))
  4884. (upd (lambda (t)
  4885. (let ((d (depth-of t)))
  4886. (when (< 0 d)
  4887. (vector-set!
  4888. types
  4889. d
  4890. (cons t (vector-ref types d))))))))
  4891. (for-each sort (vector-ref types depth))
  4892. (for 0
  4893. (- depth 1)
  4894. (lambda (i)
  4895. (for-each (prop-d i) (vector-ref sorted i))))
  4896. (for-each prop-k (vector-ref types depth))
  4897. (vector-set! types depth '())
  4898. (if fullsharing
  4899. (begin
  4900. (for-each (lambda (t) (revtype t t)) t-list)
  4901. (for-each gen leaves)
  4902. (for-each cleanup needs-cleanup))
  4903. (for-each gen2 t-list))
  4904. (for 0
  4905. depth
  4906. (lambda (i) (for-each upd (vector-ref sorted i))))
  4907. (if (null? t-list)
  4908. '()
  4909. (match-let*
  4910. ((n-gen (- generic-index))
  4911. ((t-list n-gen)
  4912. (if (and pseudo flags (not all?))
  4913. (pseudo t-list n-gen)
  4914. (list t-list n-gen))))
  4915. (visible t-list n-gen)
  4916. (map (lambda (t) (make-ts t n-gen)) t-list))))))
  4917. (define visible-time 0)
  4918. (define visible
  4919. (lambda (t-list n-gen)
  4920. (let* ((before (cpu-time))
  4921. (valences (make-vector n-gen '()))
  4922. (namer (generate-counter))
  4923. (lvis (rec lvis
  4924. (lambda (t pos rcd)
  4925. (match t
  4926. (($ box ($ c d _ x p a n))
  4927. (when (and (generic? d)
  4928. (not (element-of?
  4929. pos
  4930. (vector-ref
  4931. valences
  4932. (- (- d) 1)))))
  4933. (let ((u (union (vector-ref
  4934. valences
  4935. (- (- d) 1))
  4936. (set pos))))
  4937. (vector-set! valences (- (- d) 1) u))
  4938. (lvis p pos rcd)
  4939. (match (k-name x)
  4940. ('?->
  4941. (lvis (car a) (not pos) #f)
  4942. (lvis (cadr a) pos #f))
  4943. ('record (lvis (car a) pos #t))
  4944. (_ (for-each
  4945. (lambda (x) (lvis x pos #f))
  4946. a)))
  4947. (lvis n pos rcd)))
  4948. (($ box (and x ($ v d k _ _ _ _)))
  4949. (when (and (generic? d)
  4950. (not (element-of?
  4951. pos
  4952. (vector-ref
  4953. valences
  4954. (- (- d) 1)))))
  4955. (let ((u (union (vector-ref
  4956. valences
  4957. (- (- d) 1))
  4958. (set pos))))
  4959. (vector-set! valences (- (- d) 1) u)
  4960. (set-v-name! x namer)
  4961. (cond ((abs? k) #f)
  4962. ((= 2 (cardinality u))
  4963. (set-v-split! x #t)
  4964. (set-v-vis! x #t))
  4965. ((eq? pos rcd) (set-v-vis! x #t))
  4966. (else (set-v-vis! x #f))))))
  4967. (($ box (? symbol?)) #f)
  4968. (($ box i) (lvis i pos rcd)))))))
  4969. (for-each (lambda (t) (lvis t #t #f)) t-list)
  4970. (set! visible-time
  4971. (+ visible-time (- (cpu-time) before))))))
  4972. (define visible?
  4973. (match-lambda
  4974. (($ box ($ v _ k _ vis _ _))
  4975. (or (pre? k) (and vis (not (abs? k)))))
  4976. (($ box 'top) #t)
  4977. (($ box 'bot) #f)
  4978. (($ box i) (visible? i))))
  4979. (define instantiate
  4980. (lambda (ts syntax)
  4981. (match ts
  4982. (($ ts t n-gen)
  4983. (let* ((absv '())
  4984. (seen (make-vector n-gen #f))
  4985. (t2 (recur linst
  4986. ((t t))
  4987. (match t
  4988. (($ box (and y ($ v d k _ _ _ inst)))
  4989. (cond ((not (generic? d)) t)
  4990. ((vector-ref seen (- (- d) 1)))
  4991. (else
  4992. (let ((u (make-tvar depth k)))
  4993. (vector-set! seen (- (- d) 1) u)
  4994. (when inst
  4995. (set-v-inst!
  4996. y
  4997. (cons (cons u syntax)
  4998. inst)))
  4999. (when (or (abs? k) (pre? k))
  5000. (set! absv (cons u absv)))
  5001. u))))
  5002. (($ box ($ c d _ x p a n))
  5003. (cond ((not (generic? d)) t)
  5004. ((vector-ref seen (- (- d) 1)))
  5005. (else
  5006. (let ((u (new-type
  5007. '**fix**
  5008. depth)))
  5009. (vector-set! seen (- (- d) 1) u)
  5010. (set-box!
  5011. u
  5012. (make-c
  5013. depth
  5014. 'ord
  5015. x
  5016. (if flags (linst p) top)
  5017. (map linst a)
  5018. (linst n)))
  5019. u))))
  5020. (($ box (? symbol?)) t)
  5021. (($ box i) (linst i))))))
  5022. (list t2 absv))))))
  5023. (define pseudo-subtype
  5024. (lambda (t-list n-gen)
  5025. (let* ((valences (make-vector n-gen '()))
  5026. (valence-of
  5027. (lambda (d) (vector-ref valences (- (- d) 1))))
  5028. (set-valence
  5029. (lambda (d v)
  5030. (vector-set! valences (- (- d) 1) v)))
  5031. (find (rec find
  5032. (lambda (t pos mutable)
  5033. (match t
  5034. (($ box ($ v d _ _ _ _ _))
  5035. (when (generic? d)
  5036. (cond (mutable
  5037. (set-valence d (set #t #f)))
  5038. ((not (element-of?
  5039. pos
  5040. (valence-of d)))
  5041. (set-valence
  5042. d
  5043. (union (valence-of d)
  5044. (set pos))))
  5045. (else #f))))
  5046. (($ box ($ c d _ x p a n))
  5047. (when (generic? d)
  5048. (cond ((= 2 (cardinality (valence-of d)))
  5049. #f)
  5050. (mutable
  5051. (set-valence d (set #t #f))
  5052. (for-each2
  5053. (lambda (t m)
  5054. (find t pos mutable))
  5055. a
  5056. (k-args x))
  5057. (find n pos mutable))
  5058. ((not (element-of?
  5059. pos
  5060. (valence-of d)))
  5061. (set-valence
  5062. d
  5063. (union (valence-of d)
  5064. (set pos)))
  5065. (if (eq? '?-> (k-name x))
  5066. (begin
  5067. (find (car a)
  5068. (not pos)
  5069. mutable)
  5070. (find (cadr a) pos mutable))
  5071. (for-each2
  5072. (lambda (t m)
  5073. (find t pos (or m mutable)))
  5074. a
  5075. (k-args x)))
  5076. (find n pos mutable))
  5077. (else #f))))
  5078. (($ box (? symbol?)) #f)
  5079. (($ box i) (find i pos mutable))))))
  5080. (seen (make-vector n-gen #f))
  5081. (new-generic-var
  5082. (lambda ()
  5083. (set! n-gen (+ 1 n-gen))
  5084. (box (make-raw-tvar (- n-gen) 'ord))))
  5085. (copy (rec copy
  5086. (lambda (t)
  5087. (match t
  5088. (($ box ($ v d k _ _ _ _))
  5089. (if (generic? d)
  5090. (or (vector-ref seen (- (- d) 1))
  5091. (let ((u (if (and (abs? k)
  5092. (equal?
  5093. (valence-of d)
  5094. '(#t)))
  5095. (new-generic-var)
  5096. t)))
  5097. (vector-set! seen (- (- d) 1) u)
  5098. u))
  5099. t))
  5100. (($ box ($ c d k x p a n))
  5101. (if (generic? d)
  5102. (or (vector-ref seen (- (- d) 1))
  5103. (let* ((u (box '**fix**))
  5104. (_ (vector-set!
  5105. seen
  5106. (- (- d) 1)
  5107. u))
  5108. (new-p (if (and (eq? (ind* p) top)
  5109. (equal?
  5110. (valence-of d)
  5111. '(#f)))
  5112. (new-generic-var)
  5113. (copy p)))
  5114. (new-a (map copy a))
  5115. (new-n (copy n)))
  5116. (set-box!
  5117. u
  5118. (make-c d 'ord x new-p new-a new-n))
  5119. u))
  5120. t))
  5121. (($ box (? symbol?)) t)
  5122. (($ box i) (copy i))))))
  5123. (t-list
  5124. (map (lambda (t) (find t #t #f) (copy t)) t-list)))
  5125. (list t-list n-gen))))
  5126. (set! pseudo pseudo-subtype)
  5127. (define unify
  5128. (letrec ((uni (lambda (u v)
  5129. (unless
  5130. (eq? u v)
  5131. (match (cons u v)
  5132. ((($ box (and us ($ c ud uk ux up ua un)))
  5133. $
  5134. box
  5135. (and vs ($ c vd vk vx vp va vn)))
  5136. (if (eq? ux vx)
  5137. (begin
  5138. (if (< ud vd)
  5139. (begin
  5140. (set-box! v u)
  5141. (when (kind< vk uk) (set-c-kind! us vk)))
  5142. (begin
  5143. (set-box! u v)
  5144. (when (kind< uk vk) (set-c-kind! vs uk))))
  5145. (uni un vn)
  5146. (for-each2 uni ua va)
  5147. (uni up vp))
  5148. (let* ((next (tvar))
  5149. (k (if (kind< uk vk) uk vk)))
  5150. (if (< ud vd)
  5151. (begin
  5152. (when (< vd ud) (set-c-depth! us vd))
  5153. (when (kind< vk uk) (set-c-kind! us vk))
  5154. (set-box! v u))
  5155. (begin
  5156. (when (< ud vd) (set-c-depth! vs ud))
  5157. (when (kind< uk vk) (set-c-kind! vs uk))
  5158. (set-box! u v)))
  5159. (uni (new-type
  5160. (make-c depth k ux up ua next)
  5161. depth)
  5162. vn)
  5163. (uni un
  5164. (new-type
  5165. (make-c depth k vx vp va next)
  5166. depth)))))
  5167. ((($ box (and x ($ v ud uk _ _ _ _)))
  5168. $
  5169. box
  5170. ($ v vd vk _ _ _ _))
  5171. (set-v-depth! x (min ud vd))
  5172. (set-v-kind! x (if (kind< uk vk) uk vk))
  5173. (set-box! v u))
  5174. ((($ box ($ v ud uk _ _ _ _))
  5175. $
  5176. box
  5177. (and x ($ c vd vk _ _ _ _)))
  5178. (when (< ud vd) (set-c-depth! x ud))
  5179. (when (kind< uk vk) (set-c-kind! x uk))
  5180. (set-box! u v))
  5181. ((($ box (and x ($ c ud uk _ _ _ _)))
  5182. $
  5183. box
  5184. ($ v vd vk _ _ _ _))
  5185. (when (< vd ud) (set-c-depth! x vd))
  5186. (when (kind< vk uk) (set-c-kind! x vk))
  5187. (set-box! v u))
  5188. ((($ box ($ v _ _ _ _ _ _)) $ box (? symbol?))
  5189. (set-box! u v))
  5190. ((($ box (? symbol?)) $ box ($ v _ _ _ _ _ _))
  5191. (set-box! v u))
  5192. ((($ box 'bot) $ box ($ c _ _ _ p _ n))
  5193. (set-box! v u)
  5194. (uni u p)
  5195. (uni u n))
  5196. ((($ box ($ c _ _ _ p _ n)) $ box 'bot)
  5197. (set-box! u v)
  5198. (uni v p)
  5199. (uni v n))
  5200. (_ (uni (ind* u) (ind* v))))))))
  5201. uni))
  5202. (define kind<
  5203. (lambda (k1 k2) (and (ord? k2) (not (ord? k1)))))
  5204. (define r+-
  5205. (lambda (flag+ flag- tail+- absent- pos env type)
  5206. (letrec ((absent+ v-ord)
  5207. (tvars '())
  5208. (fvars '())
  5209. (absv '())
  5210. (make-flag
  5211. (lambda (pos)
  5212. (cond ((not flags) top)
  5213. (pos (flag+))
  5214. (else (flag-)))))
  5215. (typevar?
  5216. (lambda (v)
  5217. (and (symbol? v)
  5218. (not (bound? env v))
  5219. (not (memq v
  5220. '(_ bool
  5221. mu
  5222. list
  5223. &list
  5224. &optional
  5225. &rest
  5226. arglist
  5227. +
  5228. not
  5229. rec
  5230. *tidy))))))
  5231. (parse-type
  5232. (lambda (t pos)
  5233. (match t
  5234. (('mu a t)
  5235. (unless
  5236. (typevar? a)
  5237. (raise 'type "invalid type syntax at ~a" t))
  5238. (when (assq a tvars)
  5239. (raise 'type "~a is defined more than once" a))
  5240. (let* ((fix (new-type '**fix** depth))
  5241. (_ (set! tvars (cons (list a fix '()) tvars)))
  5242. (t (parse-type t pos)))
  5243. (when (eq? t fix)
  5244. (raise 'type
  5245. "recursive type is not contractive"))
  5246. (set-box! fix t)
  5247. (ind* t)))
  5248. (('rec (? list? bind) t2)
  5249. (for-each
  5250. (match-lambda
  5251. ((a _)
  5252. (unless
  5253. (typevar? a)
  5254. (raise 'type "invalid type syntax at ~a" t))
  5255. (when (assq a tvars)
  5256. (raise 'type
  5257. "~a is defined more than once"
  5258. a))
  5259. (set! tvars
  5260. (cons (list a (new-type '**fix** depth) '())
  5261. tvars)))
  5262. (_ (raise 'type "invalid type syntax at ~a" t)))
  5263. bind)
  5264. (for-each
  5265. (match-lambda
  5266. ((a t)
  5267. (match (assq a tvars)
  5268. ((_ fix _)
  5269. (let ((t (parse-type t '?)))
  5270. (when (eq? t fix)
  5271. (raise 'type
  5272. "type is not contractive"))
  5273. (set-box! fix t))))))
  5274. bind)
  5275. (parse-type t2 pos))
  5276. ('bool (parse-type '(+ false true) pos))
  5277. ('s-exp
  5278. (let ((v (gensym)))
  5279. (parse-type
  5280. `(mu ,v
  5281. (+ num
  5282. nil
  5283. false
  5284. true
  5285. char
  5286. sym
  5287. str
  5288. (vec ,v)
  5289. (box ,v)
  5290. (cons ,v ,v)))
  5291. pos)))
  5292. (('list t)
  5293. (let ((u (gensym)))
  5294. (parse-type `(mu ,u (+ nil (cons ,t ,u))) pos)))
  5295. (('arglist t)
  5296. (let ((u (gensym)))
  5297. (parse-type `(mu ,u (+ noarg (arg ,t ,u))) pos)))
  5298. (('+ ? list? union) (parse-union union pos))
  5299. (t (parse-union (list t) pos)))))
  5300. (parse-union
  5301. (lambda (t pos)
  5302. (letrec ((sort-cs
  5303. (lambda (cs)
  5304. (sort-list
  5305. cs
  5306. (lambda (x y) (k< (c-fsym x) (c-fsym y))))))
  5307. (link (lambda (c t)
  5308. (set-c-next! c t)
  5309. (new-type c depth))))
  5310. (recur loop
  5311. ((t t) (cs '()))
  5312. (match t
  5313. (()
  5314. (foldr link
  5315. (if pos
  5316. (absent+)
  5317. (let ((v (absent-)))
  5318. (set! absv (cons v absv))
  5319. v))
  5320. (sort-cs cs)))
  5321. (((? box? t)) (foldr link t (sort-cs cs)))
  5322. (('_) (foldr link (tail+-) (sort-cs cs)))
  5323. (((? symbol? a))
  5324. (=> fail)
  5325. (unless (typevar? a) (fail))
  5326. (let* ((cs (sort-cs cs))
  5327. (ks (map c-fsym cs)))
  5328. (foldr link
  5329. (match (assq a tvars)
  5330. ((_ f aks)
  5331. (unless
  5332. (equal? ks aks)
  5333. (raise 'type
  5334. "variable ~a is not tidy"
  5335. a))
  5336. f)
  5337. (#f
  5338. (let ((v (tail+-)))
  5339. (set! tvars
  5340. (cons (list a v ks)
  5341. tvars))
  5342. v)))
  5343. cs)))
  5344. ((k . rest)
  5345. (loop rest (cons (parse-k k pos) cs))))))))
  5346. (parse-k
  5347. (lambda (k pos)
  5348. (cond ((and (list? k)
  5349. (let ((n (length k)))
  5350. (and (<= 2 n) (eq? '-> (list-ref k (- n 2))))))
  5351. (let* ((rk (reverse k))
  5352. (arg (reverse (cddr rk)))
  5353. (res (car rk)))
  5354. (letrec ((mkargs
  5355. (match-lambda
  5356. (() 'noarg)
  5357. ((('&rest x)) x)
  5358. ((('&list x))
  5359. (let ((u (gensym)))
  5360. `(mu ,u (+ noarg (arg ,x ,u)))))
  5361. ((('&optional x))
  5362. `(+ noarg (arg ,x noarg)))
  5363. ((x . y) `(arg ,x ,(mkargs y)))
  5364. (_ (raise 'type
  5365. "invalid type syntax")))))
  5366. (make-c
  5367. depth
  5368. 'ord
  5369. (lookup env '?->)
  5370. (make-flag pos)
  5371. (let ((a (parse-type (mkargs arg) (flip pos)))
  5372. (r (parse-type res pos)))
  5373. (list a r))
  5374. '**fix**))))
  5375. (else
  5376. (match k
  5377. ((arg '?-> res)
  5378. (make-c
  5379. depth
  5380. 'ord
  5381. (lookup env '?->)
  5382. (make-flag pos)
  5383. (let ((a (parse-type arg (flip pos)))
  5384. (r (parse-type res pos)))
  5385. (list a r))
  5386. '**fix**))
  5387. (('record ? list? fields)
  5388. (make-c
  5389. depth
  5390. 'ord
  5391. (lookup env 'record)
  5392. (make-flag pos)
  5393. (list (recur loop
  5394. ((fields fields))
  5395. (match fields
  5396. (() (if pos bot (v-ord)))
  5397. ((((? symbol? f) ftype)
  5398. .
  5399. rest)
  5400. (new-type
  5401. (make-c
  5402. depth
  5403. 'ord
  5404. (new-field! f)
  5405. (if pos
  5406. (v-ord)
  5407. (let ((v (v-pre)))
  5408. (set! absv
  5409. (cons v absv))
  5410. v))
  5411. (list (parse-type
  5412. ftype
  5413. pos))
  5414. (loop rest))
  5415. depth)))))
  5416. '**fix**))
  5417. (('not (? k? k))
  5418. (make-c
  5419. depth
  5420. 'ord
  5421. k
  5422. (if pos
  5423. (absent+)
  5424. (let ((v (absent-)))
  5425. (set! absv (cons v absv))
  5426. v))
  5427. (map (lambda (x) (tail+-)) (k-args k))
  5428. '**fix**))
  5429. (('not c)
  5430. (unless
  5431. (bound? env c)
  5432. (raise 'type "invalid type syntax at ~a" k))
  5433. (let ((k (lookup env c)))
  5434. (make-c
  5435. depth
  5436. 'ord
  5437. k
  5438. (if pos
  5439. (absent+)
  5440. (let ((v (absent-)))
  5441. (set! absv (cons v absv))
  5442. v))
  5443. (map (lambda (x) (tail+-)) (k-args k))
  5444. '**fix**)))
  5445. (('*tidy c (? symbol? f))
  5446. (unless
  5447. (bound? env c)
  5448. (raise 'type "invalid type syntax at ~a" k))
  5449. (let ((k (lookup env c)))
  5450. (make-c
  5451. depth
  5452. 'ord
  5453. k
  5454. (match (assq f fvars)
  5455. ((_ . f) f)
  5456. (#f
  5457. (let ((v (tail+-)))
  5458. (set! fvars
  5459. (cons (cons f v) fvars))
  5460. v)))
  5461. (map (lambda (x) (parse-type '(+) pos))
  5462. (k-args k))
  5463. '**fix**)))
  5464. (((? k? k) ? list? arg)
  5465. (unless
  5466. (= (length arg) (length (k-args k)))
  5467. (raise 'type
  5468. "~a requires ~a arguments"
  5469. (k-name k)
  5470. (length (k-args k))))
  5471. (make-c
  5472. depth
  5473. 'ord
  5474. k
  5475. (make-flag pos)
  5476. (smap (lambda (x) (parse-type x pos)) arg)
  5477. '**fix**))
  5478. ((c ? list? arg)
  5479. (unless
  5480. (bound? env c)
  5481. (raise 'type "invalid type syntax at ~a" k))
  5482. (let ((k (lookup env c)))
  5483. (unless
  5484. (= (length arg) (length (k-args k)))
  5485. (raise 'type
  5486. "~a requires ~a arguments"
  5487. c
  5488. (length (k-args k))))
  5489. (make-c
  5490. depth
  5491. 'ord
  5492. k
  5493. (make-flag pos)
  5494. (smap (lambda (x) (parse-type x pos)) arg)
  5495. '**fix**)))
  5496. (c (unless
  5497. (bound? env c)
  5498. (raise 'type
  5499. "invalid type syntax at ~a"
  5500. k))
  5501. (let ((k (lookup env c)))
  5502. (unless
  5503. (= 0 (length (k-args k)))
  5504. (raise 'type
  5505. "~a requires ~a arguments"
  5506. c
  5507. (length (k-args k))))
  5508. (make-c
  5509. depth
  5510. 'ord
  5511. k
  5512. (make-flag pos)
  5513. '()
  5514. '**fix**))))))))
  5515. (flip (match-lambda ('? '?) (#t #f) (#f #t))))
  5516. (let ((t (parse-type type pos))) (list t absv)))))
  5517. (define v-top (lambda () top))
  5518. (define r+
  5519. (lambda (env t)
  5520. (car (r+- v-top v-ord v-ord v-abs #t env t))))
  5521. (define r-
  5522. (lambda (env t)
  5523. (car (r+- v-top v-ord v-ord v-abs #f env t))))
  5524. (define r++
  5525. (lambda (env t)
  5526. (car (r+- v-top v-ord v-ord v-ord #t env t))))
  5527. (define r+collect
  5528. (lambda (env t)
  5529. (r+- v-top v-ord v-ord v-abs #t env t)))
  5530. (define r-collect
  5531. (lambda (env t)
  5532. (r+- v-top v-ord v-ord v-abs #f env t)))
  5533. (define r (lambda (t) (r+ initial-type-env t)))
  5534. (define r-match
  5535. (lambda (t)
  5536. (close '())
  5537. '(pretty-print `(fixing ,(ptype t)))
  5538. (fix-pat-abs! t)
  5539. (list t (collect-abs t))))
  5540. (define collect-abs
  5541. (lambda (t)
  5542. (let ((seen '()))
  5543. (recur loop
  5544. ((t t))
  5545. (match t
  5546. (($ box ($ v _ k _ _ _ _))
  5547. (if (abs? k) (set t) empty-set))
  5548. (($ box ($ c _ _ _ p a n))
  5549. (if (memq t seen)
  5550. empty-set
  5551. (begin
  5552. (set! seen (cons t seen))
  5553. (foldr union
  5554. (union (loop p) (loop n))
  5555. (map loop a)))))
  5556. (($ box (? symbol?)) empty-set)
  5557. (($ box i) (loop i)))))))
  5558. (define fix-pat-abs!
  5559. (lambda (t)
  5560. (let ((seen '()))
  5561. (recur loop
  5562. ((t t))
  5563. (match t
  5564. (($ box (and x ($ v d _ _ _ _ _)))
  5565. (when (= d depth) (set-v-kind! x 'abs)))
  5566. (($ box (and c ($ c _ _ _ p a n)))
  5567. (unless
  5568. (memq t seen)
  5569. (set! seen (cons t seen))
  5570. (loop p)
  5571. (when (and matchst flags (eq? (ind* p) top))
  5572. (set-c-pres! c (v-ord)))
  5573. (for-each loop a)
  5574. (loop n)))
  5575. (($ box (? symbol?)) t)
  5576. (($ box i) (loop i)))))))
  5577. (define pat-var-bind
  5578. (lambda (t)
  5579. (let ((seen '()))
  5580. (recur loop
  5581. ((t t))
  5582. (match t
  5583. (($ box ($ v d _ _ _ _ _))
  5584. (if (< d depth)
  5585. t
  5586. (match (assq t seen)
  5587. ((_ . new) new)
  5588. (#f
  5589. (let* ((new (v-ord)))
  5590. (set! seen (cons (cons t new) seen))
  5591. new)))))
  5592. (($ box ($ c d k x p a n))
  5593. (match (assq t seen)
  5594. ((_ . new) new)
  5595. (#f
  5596. (let* ((fix (new-type '**fix** depth))
  5597. (fixbox (box fix))
  5598. (_ (set! seen (cons (cons t fixbox) seen)))
  5599. (new-p (if flags (loop p) top))
  5600. (new-a (map2 (lambda (mutable a)
  5601. (if mutable a (loop a)))
  5602. (k-args x)
  5603. a))
  5604. (new-n (loop n)))
  5605. (if (and (eq? new-p p)
  5606. (eq? new-n n)
  5607. (andmap eq? new-a a))
  5608. (begin (set-box! fixbox t) t)
  5609. (begin
  5610. (set-box!
  5611. fix
  5612. (make-c d k x new-p new-a new-n))
  5613. fix))))))
  5614. (($ box (? symbol?)) t)
  5615. (($ box i) (loop i)))))))
  5616. (define fields '())
  5617. (define new-field!
  5618. (lambda (x)
  5619. (match (assq x fields)
  5620. (#f
  5621. (let ((k (make-k x (+ 1 (length fields)) '(#f))))
  5622. (set! fields (cons (cons x k) fields))
  5623. k))
  5624. ((_ . k) k))))
  5625. (define k<
  5626. (lambda (x y) (< (k-order x) (k-order y))))
  5627. (define k-counter 0)
  5628. (define bind-tycon
  5629. (lambda (x args covers fail-thunk)
  5630. (when (memq x
  5631. '(_ bool
  5632. mu
  5633. list
  5634. &list
  5635. &optional
  5636. &rest
  5637. arglist
  5638. +
  5639. not
  5640. rec
  5641. *tidy))
  5642. (fail-thunk "invalid type constructor ~a" x))
  5643. (set! k-counter (+ 1 k-counter))
  5644. (make-k
  5645. (if covers
  5646. (symbol-append x "." (- k-counter 100))
  5647. x)
  5648. k-counter
  5649. args)))
  5650. (define initial-type-env '())
  5651. (define init-types!
  5652. (lambda ()
  5653. (set! k-counter 0)
  5654. (set! var-counter (generate-counter))
  5655. (set! initial-type-env
  5656. (foldl (lambda (l env)
  5657. (extend-env
  5658. env
  5659. (car l)
  5660. (bind-tycon
  5661. (car l)
  5662. (cdr l)
  5663. #f
  5664. (lambda x (apply disaster 'init x)))))
  5665. empty-env
  5666. initial-type-info))
  5667. (set! k-counter 100)
  5668. (reset-types!)))
  5669. (define reinit-types!
  5670. (lambda ()
  5671. (set! var-counter (generate-counter))
  5672. (set! k-counter 100)
  5673. (set! fields '())
  5674. (set-cons-mutability! #t)
  5675. (reset-types!)))
  5676. (define deftype
  5677. (lambda (tag mutability)
  5678. (set! initial-type-env
  5679. (extend-env
  5680. initial-type-env
  5681. tag
  5682. (make-k
  5683. tag
  5684. (+ 1 (length initial-type-env))
  5685. mutability)))))
  5686. (define initial-type-info
  5687. '((?-> #f #f)
  5688. (arg #f #f)
  5689. (noarg)
  5690. (num)
  5691. (nil)
  5692. (false)
  5693. (true)
  5694. (char)
  5695. (sym)
  5696. (str)
  5697. (void)
  5698. (iport)
  5699. (oport)
  5700. (eof)
  5701. (vec #t)
  5702. (box #t)
  5703. (cons #t #t)
  5704. (cvec #f)
  5705. (promise #t)
  5706. (record #f)
  5707. (module #f)))
  5708. (define cons-is-mutable #f)
  5709. (define set-cons-mutability!
  5710. (lambda (m)
  5711. (set! cons-is-mutable m)
  5712. (set-k-args!
  5713. (lookup initial-type-env 'cons)
  5714. (list m m))))
  5715. (define tidy?
  5716. (lambda (t)
  5717. (let ((seen '()))
  5718. (recur loop
  5719. ((t t) (label '()))
  5720. (match t
  5721. (($ box (? v?))
  5722. (match (assq t seen)
  5723. (#f (set! seen (cons (cons t label) seen)) #t)
  5724. ((_ . l2) (equal? label l2))))
  5725. (($ box ($ c _ _ x _ a n))
  5726. (match (assq t seen)
  5727. ((_ . l2) (equal? label l2))
  5728. (#f
  5729. (set! seen (cons (cons t label) seen))
  5730. (and (loop n (sort-list (cons x label) k<))
  5731. (andmap (lambda (t) (loop t '())) a)))))
  5732. (($ box (? symbol?)) #t)
  5733. (($ box i) (loop i label)))))))
  5734. (define tidy
  5735. (match-lambda
  5736. (($ ts t _)
  5737. (tidy-print t print-union assemble-union #f))
  5738. (t (tidy-print t print-union assemble-union #f))))
  5739. (define ptype
  5740. (match-lambda
  5741. (($ ts t _)
  5742. (tidy-print
  5743. t
  5744. print-raw-union
  5745. assemble-raw-union
  5746. #t))
  5747. (t (tidy-print
  5748. t
  5749. print-raw-union
  5750. assemble-raw-union
  5751. #t))))
  5752. (define tidy-print
  5753. (lambda (t print assemble top)
  5754. (let* ((share (shared-unions t top))
  5755. (bindings
  5756. (map-with-n
  5757. (lambda (t n)
  5758. (list t
  5759. (box #f)
  5760. (box #f)
  5761. (symbol-append "Y" (+ 1 n))))
  5762. share))
  5763. (body (print t (print-binding bindings)))
  5764. (let-bindings
  5765. (filter-map
  5766. (match-lambda
  5767. ((_ _ ($ box #f) _) #f)
  5768. ((_ ($ box t) ($ box x) _) (list x t)))
  5769. bindings)))
  5770. (assemble let-bindings body))))
  5771. (define print-binding
  5772. (lambda (bindings)
  5773. (lambda (ty share-wrapper var-wrapper render)
  5774. (match (assq ty bindings)
  5775. (#f (render))
  5776. ((_ box-tprint box-name nprint)
  5777. (var-wrapper
  5778. (or (unbox box-name)
  5779. (begin
  5780. (set-box! box-name nprint)
  5781. (set-box! box-tprint (share-wrapper (render)))
  5782. nprint))))))))
  5783. (define shared-unions
  5784. (lambda (t all)
  5785. (let ((seen '()))
  5786. (recur loop
  5787. ((t t) (top #t))
  5788. (match t
  5789. (($ box (? v?)) #f)
  5790. (($ box ($ c _ _ _ _ a n))
  5791. (match (and top (assq t seen))
  5792. (#f
  5793. (set! seen (cons (cons t (box 1)) seen))
  5794. (for-each (lambda (x) (loop x #t)) a)
  5795. (loop n all))
  5796. ((_ . b) (set-box! b (+ 1 (unbox b))))))
  5797. (($ box (? symbol?)) #f)
  5798. (($ box i) (loop i top))))
  5799. (reverse
  5800. (filter-map
  5801. (match-lambda ((_ $ box 1) #f) ((t . _) t))
  5802. seen)))))
  5803. (define print-raw-union
  5804. (lambda (t print-share)
  5805. (recur loop
  5806. ((t t))
  5807. (match t
  5808. (($ box ($ v _ _ _ _ split _))
  5809. (if (and share split)
  5810. (string->symbol (sprintf "~a#" (pvar t)))
  5811. (pvar t)))
  5812. (($ box ($ c d k x p a n))
  5813. (print-share
  5814. t
  5815. (lambda (x) x)
  5816. (lambda (x) x)
  5817. (lambda ()
  5818. (let* ((name (if (abs? k)
  5819. (symbol-append '~ (k-name x))
  5820. (k-name x)))
  5821. (name (if dump-depths
  5822. (symbol-append d '! name)
  5823. name))
  5824. (pr-x `(,name ,@(maplr loop (cons p a)))))
  5825. (cons pr-x (loop n))))))
  5826. (($ box 'top) '+)
  5827. (($ box 'bot) '-)
  5828. (($ box i) (loop i))))))
  5829. (define assemble-raw-union
  5830. (lambda (bindings body)
  5831. (if (null? bindings) body `(rec ,bindings ,body))))
  5832. (define print-union
  5833. (lambda (t print-share)
  5834. (add-+ (recur loop
  5835. ((t t) (tailvis (visible? (tailvar t))))
  5836. (match t
  5837. (($ box (? v?))
  5838. (if (visible? t) (list (pvar t)) '()))
  5839. (($ box ($ c _ _ x p a n))
  5840. (print-share
  5841. t
  5842. add-+
  5843. list
  5844. (lambda ()
  5845. (cond ((visible? p)
  5846. (let* ((split-flag
  5847. (and share
  5848. (match (ind* p)
  5849. (($ box
  5850. ($ v
  5851. _
  5852. _
  5853. _
  5854. _
  5855. split
  5856. _))
  5857. split)
  5858. (_ #f))))
  5859. (kname (if split-flag
  5860. (string->symbol
  5861. (sprintf
  5862. "~a#~a"
  5863. (k-name x)
  5864. (pvar p)))
  5865. (k-name x))))
  5866. (cons (cond ((null? a) kname)
  5867. ((eq? '?-> (k-name x))
  5868. (let ((arg (add-+ (loop (car a)
  5869. (visible?
  5870. (tailvar
  5871. (car a))))))
  5872. (res (add-+ (loop (cadr a)
  5873. (visible?
  5874. (tailvar
  5875. (cadr a)))))))
  5876. (decode-arrow
  5877. kname
  5878. (lambda ()
  5879. (if split-flag
  5880. (string->symbol
  5881. (sprintf
  5882. "->#~a"
  5883. (pvar p)))
  5884. '->))
  5885. arg
  5886. res)))
  5887. ((eq? 'record (k-name x))
  5888. `(,kname
  5889. ,@(loop (car a) #f)))
  5890. (else
  5891. `(,kname
  5892. ,@(maplr (lambda (x)
  5893. (add-+ (loop x
  5894. (visible?
  5895. (tailvar
  5896. x)))))
  5897. a))))
  5898. (loop n tailvis))))
  5899. ((not tailvis) (loop n tailvis))
  5900. (else
  5901. (cons `(not ,(k-name x))
  5902. (loop n tailvis)))))))
  5903. (($ box 'bot) '())
  5904. (($ box i) (loop i tailvis)))))))
  5905. (define assemble-union
  5906. (lambda (bindings body)
  5907. (subst-small-type
  5908. (map clean-binding bindings)
  5909. body)))
  5910. (define add-+
  5911. (match-lambda
  5912. (() 'empty)
  5913. ((t) t)
  5914. (x (cons '+ x))))
  5915. (define tailvar
  5916. (lambda (t)
  5917. (match t
  5918. (($ box (? v?)) t)
  5919. (($ box ($ c _ _ _ _ _ n)) (tailvar n))
  5920. (($ box 'bot) t)
  5921. (($ box i) (tailvar i)))))
  5922. (define decode-arrow
  5923. (lambda (kname thunk-> arg res)
  5924. (let ((args (recur loop
  5925. ((l arg))
  5926. (match l
  5927. ('noarg '())
  5928. (('arg a b) `(,a ,@(loop b)))
  5929. (('+ ('arg a b) 'noarg . _)
  5930. `((&optional ,a) ,@(loop b)))
  5931. (('+ 'noarg ('arg a b) . _)
  5932. `((&optional ,a) ,@(loop b)))
  5933. ((? symbol? z)
  5934. (if (rectypevar? z) `(,z) `((&rest ,z))))
  5935. (('+ 'noarg z) (loop z))
  5936. (('+ ('arg a b) z)
  5937. (loop `(+ (arg ,a ,b) noarg ,z)))))))
  5938. `(,@args ,(thunk->) ,res))))
  5939. (define rectypevar?
  5940. (lambda (s)
  5941. (memq (string-ref (symbol->string s) 0) '(#\Y))))
  5942. (define typevar?
  5943. (lambda (s)
  5944. (memq (string-ref (symbol->string s) 0)
  5945. '(#\X #\Z))))
  5946. (define clean-binding
  5947. (lambda (binding)
  5948. (match binding
  5949. ((u ('+ 'nil ('cons a v)))
  5950. (if (and (equal? u v) (not (memq* u a)))
  5951. (list u `(list ,a))
  5952. binding))
  5953. ((u ('+ ('cons a v) 'nil))
  5954. (if (and (equal? u v) (not (memq* u a)))
  5955. (list u `(list ,a))
  5956. binding))
  5957. ((u ('+ 'nil ('cons a v) (? symbol? z)))
  5958. (if (and (equal? u v) (not (memq* u a)) (typevar? z))
  5959. (list u `(list* ,a ,z))
  5960. binding))
  5961. ((u ('+ ('cons a v) 'nil (? symbol? z)))
  5962. (if (and (equal? u v) (not (memq* u a)) (typevar? z))
  5963. (list u `(list* ,a ,z))
  5964. binding))
  5965. ((u ('+ 'noarg ('arg a v)))
  5966. (if (and (equal? u v) (not (memq* u a)))
  5967. (list u `(&list ,a))
  5968. binding))
  5969. ((u ('+ ('arg a v) 'noarg))
  5970. (if (and (equal? u v) (not (memq* u a)))
  5971. (list u `(&list ,a))
  5972. binding))
  5973. (x x))))
  5974. (define memq*
  5975. (lambda (v t)
  5976. (recur loop
  5977. ((t t))
  5978. (match t
  5979. ((x . y) (or (loop x) (loop y)))
  5980. (_ (eq? v t))))))
  5981. (define subst-type
  5982. (lambda (new old t)
  5983. (match new
  5984. (('list elem) (subst-list elem old t))
  5985. (_ (subst* new old t)))))
  5986. (define subst-list
  5987. (lambda (elem old t)
  5988. (match t
  5989. ((? symbol?) (if (eq? old t) `(list ,elem) t))
  5990. (('+ 'nil ('cons a (? symbol? b)))
  5991. (if (and (eq? b old) (equal? elem a))
  5992. `(list ,elem)
  5993. `(+ nil (cons ,(subst-list elem old a) ,b))))
  5994. (('+ ('cons a (? symbol? b)) 'nil)
  5995. (if (and (eq? b old) (equal? elem a))
  5996. `(list ,elem)
  5997. `(+ nil (cons ,(subst-list elem old a) ,b))))
  5998. ((a . b)
  5999. (cons (subst-list elem old a)
  6000. (subst-list elem old b)))
  6001. (z z))))
  6002. (define subst*
  6003. (lambda (new old t)
  6004. (cond ((eq? old t) new)
  6005. ((pair? t)
  6006. (cons (subst* new old (car t))
  6007. (subst* new old (cdr t))))
  6008. (else t))))
  6009. (define subst-small-type
  6010. (lambda (bindings body)
  6011. (recur loop
  6012. ((bindings bindings) (newb '()) (body body))
  6013. (match bindings
  6014. (()
  6015. (let ((newb (filter
  6016. (match-lambda
  6017. ((name type) (not (equal? name type))))
  6018. newb)))
  6019. (if (null? newb)
  6020. body
  6021. `(rec ,(reverse newb) ,body))))
  6022. (((and b (name type)) . rest)
  6023. (if (and (not (memq* name type)) (small-type? type))
  6024. (loop (subst-type type name rest)
  6025. (subst-type type name newb)
  6026. (subst-type type name body))
  6027. (loop rest (cons b newb) body)))))))
  6028. (define small-type?
  6029. (lambda (t)
  6030. (>= 8
  6031. (recur loop
  6032. ((t t))
  6033. (match t
  6034. ('+ 0)
  6035. ((? symbol? s) 1)
  6036. ((? number? n) 0)
  6037. ((x . y) (+ (loop x) (loop y)))
  6038. (() 0))))))
  6039. (define qop
  6040. (lambda (s)
  6041. (string->symbol (string-append "# " s))))
  6042. (define qcons (qop "cons"))
  6043. (define qbox (qop "box"))
  6044. (define qlist (qop "list"))
  6045. (define qvector (qop "vector"))
  6046. (define initial-info
  6047. `((not (a -> bool))
  6048. (eqv? (a a -> bool))
  6049. (eq? (a a -> bool))
  6050. (equal? (a a -> bool))
  6051. (cons (a b -> (cons a b)) (ic))
  6052. (car ((cons a b) -> a) (s (x . _)))
  6053. (cdr ((cons b a) -> a) (s (_ . x)))
  6054. (caar ((cons (cons a b) c) -> a)
  6055. (s ((x . _) . _)))
  6056. (cadr ((cons c (cons a b)) -> a) (s (_ x . _)))
  6057. (cdar ((cons (cons b a) c) -> a)
  6058. (s ((_ . x) . _)))
  6059. (cddr ((cons c (cons b a)) -> a) (s (_ _ . x)))
  6060. (caaar ((cons (cons (cons a b) c) d) -> a)
  6061. (s (((x . _) . _) . _)))
  6062. (caadr ((cons d (cons (cons a b) c)) -> a)
  6063. (s (_ (x . _) . _)))
  6064. (cadar ((cons (cons c (cons a b)) d) -> a)
  6065. (s ((_ x . _) . _)))
  6066. (caddr ((cons d (cons c (cons a b))) -> a)
  6067. (s (_ _ x . _)))
  6068. (cdaar ((cons (cons (cons b a) c) d) -> a)
  6069. (s (((_ . x) . _) . _)))
  6070. (cdadr ((cons d (cons (cons b a) c)) -> a)
  6071. (s (_ (_ . x) . _)))
  6072. (cddar ((cons (cons c (cons b a)) d) -> a)
  6073. (s ((_ _ . x) . _)))
  6074. (cdddr ((cons d (cons c (cons b a))) -> a)
  6075. (s (_ _ _ . x)))
  6076. (caaaar
  6077. ((cons (cons (cons (cons a b) c) d) e) -> a)
  6078. (s ((((x . _) . _) . _) . _)))
  6079. (caaadr
  6080. ((cons e (cons (cons (cons a b) c) d)) -> a)
  6081. (s (_ ((x . _) . _) . _)))
  6082. (caadar
  6083. ((cons (cons d (cons (cons a b) c)) e) -> a)
  6084. (s ((_ (x . _) . _) . _)))
  6085. (caaddr
  6086. ((cons e (cons d (cons (cons a b) c))) -> a)
  6087. (s (_ _ (x . _) . _)))
  6088. (cadaar
  6089. ((cons (cons (cons c (cons a b)) d) e) -> a)
  6090. (s (((_ x . _) . _) . _)))
  6091. (cadadr
  6092. ((cons e (cons (cons c (cons a b)) d)) -> a)
  6093. (s (_ (_ x . _) . _)))
  6094. (caddar
  6095. ((cons (cons d (cons c (cons a b))) e) -> a)
  6096. (s ((_ _ x . _) . _)))
  6097. (cadddr
  6098. ((cons e (cons d (cons c (cons a b)))) -> a)
  6099. (s (_ _ _ x . _)))
  6100. (cdaaar
  6101. ((cons (cons (cons (cons b a) c) d) e) -> a)
  6102. (s ((((_ . x) . _) . _) . _)))
  6103. (cdaadr
  6104. ((cons e (cons (cons (cons b a) c) d)) -> a)
  6105. (s (_ ((_ . x) . _) . _)))
  6106. (cdadar
  6107. ((cons (cons d (cons (cons b a) c)) e) -> a)
  6108. (s ((_ (_ . x) . _) . _)))
  6109. (cdaddr
  6110. ((cons e (cons d (cons (cons b a) c))) -> a)
  6111. (s (_ _ (_ . x) . _)))
  6112. (cddaar
  6113. ((cons (cons (cons c (cons b a)) d) e) -> a)
  6114. (s (((_ _ . x) . _) . _)))
  6115. (cddadr
  6116. ((cons e (cons (cons c (cons b a)) d)) -> a)
  6117. (s (_ (_ _ . x) . _)))
  6118. (cdddar
  6119. ((cons (cons d (cons c (cons b a))) e) -> a)
  6120. (s ((_ _ _ . x) . _)))
  6121. (cddddr
  6122. ((cons e (cons d (cons c (cons b a)))) -> a)
  6123. (s (_ _ _ _ . x)))
  6124. (set-car! ((cons a b) a -> void))
  6125. (set-cdr! ((cons a b) b -> void))
  6126. (list ((&list a) -> (list a)) (ic))
  6127. (length ((list a) -> num))
  6128. (append ((&list (list a)) -> (list a)) (ic) (d))
  6129. (reverse ((list a) -> (list a)) (ic))
  6130. (list-tail ((list a) num -> (list a)) (c))
  6131. (list-ref ((list a) num -> a) (c))
  6132. (memq (a (list a) -> (+ false (cons a (list a)))))
  6133. (memv (a (list a) -> (+ false (cons a (list a)))))
  6134. (member
  6135. (a (list a) -> (+ false (cons a (list a)))))
  6136. (assq (a (list (cons a c)) -> (+ false (cons a c))))
  6137. (assv (a (list (cons a c)) -> (+ false (cons a c))))
  6138. (assoc (a (list (cons a c)) -> (+ false (cons a c))))
  6139. (symbol->string (sym -> str))
  6140. (string->symbol (str -> sym))
  6141. (complex? (a -> bool))
  6142. (real? (a -> bool))
  6143. (rational? (a -> bool))
  6144. (integer? (a -> bool))
  6145. (exact? (num -> bool))
  6146. (inexact? (num -> bool))
  6147. (= (num num (&list num) -> bool))
  6148. (< (num num (&list num) -> bool))
  6149. (> (num num (&list num) -> bool))
  6150. (<= (num num (&list num) -> bool))
  6151. (>= (num num (&list num) -> bool))
  6152. (zero? (num -> bool))
  6153. (positive? (num -> bool))
  6154. (negative? (num -> bool))
  6155. (odd? (num -> bool))
  6156. (even? (num -> bool))
  6157. (max (num (&list num) -> num))
  6158. (min (num (&list num) -> num))
  6159. (+ ((&list num) -> num))
  6160. (* ((&list num) -> num))
  6161. (- (num (&list num) -> num))
  6162. (/ (num (&list num) -> num))
  6163. (abs (num -> num))
  6164. (quotient (num num -> num))
  6165. (remainder (num num -> num))
  6166. (modulo (num num -> num))
  6167. (gcd ((&list num) -> num))
  6168. (lcm ((&list num) -> num))
  6169. (numerator (num -> num))
  6170. (denominator (num -> num))
  6171. (floor (num -> num))
  6172. (ceiling (num -> num))
  6173. (truncate (num -> num))
  6174. (round (num -> num))
  6175. (rationalize (num num -> num))
  6176. (exp (num -> num))
  6177. (log (num -> num))
  6178. (sin (num -> num))
  6179. (cos (num -> num))
  6180. (tan (num -> num))
  6181. (asin (num -> num))
  6182. (acos (num -> num))
  6183. (atan (num (&optional num) -> num))
  6184. (sqrt (num -> num))
  6185. (expt (num num -> num))
  6186. (make-rectangular (num num -> num))
  6187. (make-polar (num num -> num))
  6188. (real-part (num -> num))
  6189. (imag-part (num -> num))
  6190. (magnitude (num -> num))
  6191. (angle (num -> num))
  6192. (exact->inexact (num -> num))
  6193. (inexact->exact (num -> num))
  6194. (number->string (num (&optional num) -> str))
  6195. (string->number (str (&optional num) -> num))
  6196. (char=? (char char -> bool))
  6197. (char<? (char char -> bool))
  6198. (char>? (char char -> bool))
  6199. (char<=? (char char -> bool))
  6200. (char>=? (char char -> bool))
  6201. (char-ci=? (char char -> bool))
  6202. (char-ci<? (char char -> bool))
  6203. (char-ci>? (char char -> bool))
  6204. (char-ci<=? (char char -> bool))
  6205. (char-ci>=? (char char -> bool))
  6206. (char-alphabetic? (char -> bool))
  6207. (char-numeric? (char -> bool))
  6208. (char-whitespace? (char -> bool))
  6209. (char-upper-case? (char -> bool))
  6210. (char-lower-case? (char -> bool))
  6211. (char->integer (char -> num))
  6212. (integer->char (num -> char))
  6213. (char-upcase (char -> char))
  6214. (char-downcase (char -> char))
  6215. (make-string (num (&optional char) -> str))
  6216. (string ((&list char) -> str))
  6217. (string-length (str -> num))
  6218. (string-ref (str num -> char))
  6219. (string-set! (str num char -> void))
  6220. (string=? (str str -> bool))
  6221. (string<? (str str -> bool))
  6222. (string>? (str str -> bool))
  6223. (string<=? (str str -> bool))
  6224. (string>=? (str str -> bool))
  6225. (string-ci=? (str str -> bool))
  6226. (string-ci<? (str str -> bool))
  6227. (string-ci>? (str str -> bool))
  6228. (string-ci<=? (str str -> bool))
  6229. (string-ci>=? (str str -> bool))
  6230. (substring (str num num -> str))
  6231. (string-append ((&list str) -> str))
  6232. (string->list (str -> (list char)) (ic))
  6233. (list->string ((list char) -> str))
  6234. (string-copy (str -> str))
  6235. (string-fill! (str char -> void))
  6236. (make-vector (num a -> (vec a)) (i))
  6237. (vector ((&list a) -> (vec a)) (i))
  6238. (vector-length ((vec a) -> num))
  6239. (vector-ref ((vec a) num -> a))
  6240. (vector-set! ((vec a) num a -> void))
  6241. (vector->list ((vec a) -> (list a)) (ic))
  6242. (list->vector ((list a) -> (vec a)) (i))
  6243. (vector-fill! ((vec a) a -> void))
  6244. (apply (((&list a) -> b) (list a) -> b) (i) (d))
  6245. (map ((a -> b) (list a) -> (list b)) (i) (d))
  6246. (for-each ((a -> b) (list a) -> void) (i) (d))
  6247. (force ((promise a) -> a) (i))
  6248. (call-with-current-continuation
  6249. (((a -> b) -> a) -> a)
  6250. (i))
  6251. (call-with-input-file
  6252. (str (iport -> a) -> a)
  6253. (i))
  6254. (call-with-output-file
  6255. (str (oport -> a) -> a)
  6256. (i))
  6257. (input-port? (a -> bool))
  6258. (output-port? (a -> bool))
  6259. (current-input-port (-> iport))
  6260. (current-output-port (-> oport))
  6261. (with-input-from-file (str (-> a) -> a) (i))
  6262. (with-output-to-file (str (-> a) -> a) (i))
  6263. (open-input-file (str -> iport))
  6264. (open-output-file (str -> oport))
  6265. (close-input-port (iport -> void))
  6266. (close-output-port (oport -> void))
  6267. (read ((&optional iport)
  6268. ->
  6269. (+ eof
  6270. num
  6271. nil
  6272. false
  6273. true
  6274. char
  6275. sym
  6276. str
  6277. (box (mu sexp
  6278. (+ num
  6279. nil
  6280. false
  6281. true
  6282. char
  6283. sym
  6284. str
  6285. (vec sexp)
  6286. (cons sexp sexp)
  6287. (box sexp))))
  6288. (cons sexp sexp)
  6289. (vec sexp)))
  6290. (i))
  6291. (read-char
  6292. ((&optional iport) -> (+ char eof))
  6293. (i))
  6294. (peek-char
  6295. ((&optional iport) -> (+ char eof))
  6296. (i))
  6297. (char-ready? ((&optional iport) -> bool) (i))
  6298. (write (a (&optional oport) -> void) (i))
  6299. (display (a (&optional oport) -> void) (i))
  6300. (newline ((&optional oport) -> void) (i))
  6301. (write-char (char (&optional oport) -> void) (i))
  6302. (load (str -> void))
  6303. (transcript-on (str -> void))
  6304. (transcript-off (-> void))
  6305. (symbol-append ((&rest a) -> sym))
  6306. (box (a -> (box a)) (i))
  6307. (unbox ((box a) -> a) (s boxx))
  6308. (set-box! ((box a) a -> void))
  6309. (void (-> void))
  6310. (make-module (a -> (module a)))
  6311. (raise ((&rest a) -> b))
  6312. (match:error (a (&rest b) -> c))
  6313. (should-never-reach (a -> b))
  6314. (make-cvector (num a -> (cvec a)))
  6315. (cvector ((&list a) -> (cvec a)))
  6316. (cvector-length ((cvec a) -> num))
  6317. (cvector-ref ((cvec a) num -> a))
  6318. (cvector->list ((cvec a) -> (list a)) (ic))
  6319. (list->cvector ((list a) -> (cvec a)))
  6320. (,qcons (a b -> (cons a b)) (ic) (n))
  6321. (,qvector ((&list a) -> (vec a)) (i) (n))
  6322. (,qbox (a -> (box a)) (i) (n))
  6323. (,qlist ((&list a) -> (list a)) (ic) (n))
  6324. (number? ((+ num x) -> bool) (p (num)))
  6325. (null? ((+ nil x) -> bool) (p (nil)))
  6326. (char? ((+ char x) -> bool) (p (char)))
  6327. (symbol? ((+ sym x) -> bool) (p (sym)))
  6328. (string? ((+ str x) -> bool) (p (str)))
  6329. (vector? ((+ (vec a) x) -> bool) (p (vec a)))
  6330. (cvector? ((+ (cvec a) x) -> bool) (p (cvec a)))
  6331. (box? ((+ (box a) x) -> bool) (p (box a)))
  6332. (pair? ((+ (cons a b) x) -> bool) (p (cons a b)))
  6333. (procedure?
  6334. ((+ ((&rest a) -> b) x) -> bool)
  6335. (p (?-> a b)))
  6336. (eof-object? ((+ eof x) -> bool) (p (eof)))
  6337. (input-port? ((+ iport x) -> bool) (p (iport)))
  6338. (output-port? ((+ oport x) -> bool) (p (oport)))
  6339. (true-object? ((+ true x) -> bool) (p (true)))
  6340. (false-object? ((+ false x) -> bool) (p (false)))
  6341. (module?
  6342. ((+ (module a) x) -> bool)
  6343. (p (module a)))
  6344. (boolean? ((+ true false x) -> bool) (p #t))
  6345. (list? ((mu u (+ nil (cons y u) x)) -> bool)
  6346. (p #t))))
  6347. (define initial-env '())
  6348. (define init-env!
  6349. (lambda ()
  6350. (set! initial-env
  6351. (foldr init-prim empty-env initial-info))))
  6352. (define init-prim
  6353. (lambda (l env)
  6354. (letrec ((build-selector
  6355. (match-lambda
  6356. ('x (lambda (x) x))
  6357. ('_ (lambda (x) (make-pany)))
  6358. ('boxx
  6359. (let ((c (lookup env 'box?)))
  6360. (lambda (x) (make-pobj c (list x)))))
  6361. ((x . y)
  6362. (let ((c (lookup env 'pair?))
  6363. (lx (build-selector x))
  6364. (ly (build-selector y)))
  6365. (lambda (x) (make-pobj c (list (lx x) (ly x)))))))))
  6366. (match l
  6367. ((name type . attr)
  6368. (let* ((pure (cond ((assq 'i attr) #f)
  6369. ((assq 'ic attr) 'cons)
  6370. (else #t)))
  6371. (def (assq 'd attr))
  6372. (check (assq 'c attr))
  6373. (nocheck (assq 'n attr))
  6374. (pred (match (assq 'p attr)
  6375. (#f #f)
  6376. ((_ #t) #t)
  6377. ((_ (tag . args))
  6378. (cons (lookup initial-type-env tag) args))))
  6379. (sel (match (assq 's attr)
  6380. (#f #f)
  6381. ((_ s) (build-selector s))))
  6382. (env1 (extend-env
  6383. env
  6384. name
  6385. (make-name
  6386. name
  6387. (closeall (r+ initial-type-env type))
  6388. #f
  6389. 0
  6390. #f
  6391. #f
  6392. (cond (nocheck 'nocheck)
  6393. (check 'check)
  6394. (def 'imprecise)
  6395. (else #t))
  6396. #f
  6397. pure
  6398. pred
  6399. #f
  6400. sel)))
  6401. (env2 (extend-env
  6402. env1
  6403. (symbol-append 'check- name)
  6404. (make-name
  6405. (symbol-append 'check- name)
  6406. (closeall (r++ initial-type-env type))
  6407. #f
  6408. 0
  6409. #f
  6410. #f
  6411. #t
  6412. #f
  6413. pure
  6414. pred
  6415. #f
  6416. sel))))
  6417. env2))))))
  6418. (define defprim
  6419. (lambda (name type mode)
  6420. (handle
  6421. (r+ initial-type-env type)
  6422. (match-lambda*
  6423. (('type . args) (apply syntax-err type args))
  6424. (x (apply raise x))))
  6425. (let* ((attr (match mode
  6426. ('impure '((i)))
  6427. ('pure '())
  6428. ('pure-if-cons-is '((ic)))
  6429. ('mutates-cons
  6430. (set! cons-mutators (cons name cons-mutators))
  6431. '())
  6432. (x (use-error
  6433. "invalid attribute ~a for st:defprim"
  6434. x))))
  6435. (info `(,name ,type ,@attr)))
  6436. (unless
  6437. (equal? info (assq name initial-info))
  6438. (set! initial-info (cons info initial-info))
  6439. (set! initial-env (init-prim info initial-env))))))
  6440. (init-types!)
  6441. (init-env!)
  6442. (define %not (lookup initial-env 'not))
  6443. (define %list (lookup initial-env 'list))
  6444. (define %cons (lookup initial-env 'cons))
  6445. (define %should-never-reach
  6446. (lookup initial-env 'should-never-reach))
  6447. (define %false-object?
  6448. (lookup initial-env 'false-object?))
  6449. (define %eq? (lookup initial-env 'eq?))
  6450. (define %eqv? (lookup initial-env 'eqv?))
  6451. (define %equal? (lookup initial-env 'equal?))
  6452. (define %null? (lookup initial-env 'null?))
  6453. (define %vector? (lookup initial-env 'vector?))
  6454. (define %cvector? (lookup initial-env 'cvector?))
  6455. (define %list? (lookup initial-env 'list?))
  6456. (define %boolean? (lookup initial-env 'boolean?))
  6457. (define %procedure?
  6458. (lookup initial-env 'procedure?))
  6459. (define n-unbound 0)
  6460. (define bind-defs
  6461. (lambda (defs env0 tenv0 old-unbound timestamp)
  6462. (letrec ((cons-mutable #f)
  6463. (unbound '())
  6464. (use-var
  6465. (lambda (x env context mk-node)
  6466. (match (lookup? env x)
  6467. (#f
  6468. (let* ((b (bind-var x)) (n (mk-node b)))
  6469. (set-name-timestamp! b context)
  6470. (set! unbound (cons n unbound))
  6471. n))
  6472. (b (when (and (name-primitive b)
  6473. (memq x cons-mutators))
  6474. (set! cons-mutable #t))
  6475. (set-name-occ! b (+ 1 (name-occ b)))
  6476. (mk-node b)))))
  6477. (bind-var
  6478. (lambda (x)
  6479. (make-name
  6480. x
  6481. #f
  6482. timestamp
  6483. 0
  6484. #f
  6485. #f
  6486. #f
  6487. #f
  6488. #f
  6489. #f
  6490. #f
  6491. #f)))
  6492. (bind (lambda (e env tenv context)
  6493. (let ((bind-cur (lambda (x) (bind x env tenv context))))
  6494. (match e
  6495. (($ var x) (use-var x env context make-var))
  6496. (($ prim x)
  6497. (use-var x initial-env context make-var))
  6498. (($ const c pred)
  6499. (use-var
  6500. pred
  6501. initial-env
  6502. context
  6503. (lambda (p) (make-const c p))))
  6504. (($ lam args e2)
  6505. (let* ((b-args (map bind-var args))
  6506. (newenv (extend-env* env args b-args)))
  6507. (make-lam
  6508. b-args
  6509. (bind e2 newenv tenv context))))
  6510. (($ vlam args rest e2)
  6511. (let* ((b-args (map bind-var args))
  6512. (b-rest (bind-var rest))
  6513. (newenv
  6514. (extend-env*
  6515. env
  6516. (cons rest args)
  6517. (cons b-rest b-args))))
  6518. (make-vlam
  6519. b-args
  6520. b-rest
  6521. (bind e2 newenv tenv context))))
  6522. (($ match e1 clauses)
  6523. (make-match
  6524. (bind-cur e1)
  6525. (map (lambda (x)
  6526. (bind-mclause x env tenv context))
  6527. clauses)))
  6528. (($ app e1 args)
  6529. (make-app (bind-cur e1) (map bind-cur args)))
  6530. (($ begin exps) (make-begin (map bind-cur exps)))
  6531. (($ and exps) (make-and (map bind-cur exps)))
  6532. (($ or exps) (make-or (map bind-cur exps)))
  6533. (($ if test then els)
  6534. (make-if
  6535. (bind-cur test)
  6536. (bind-cur then)
  6537. (bind-cur els)))
  6538. (($ delay e2) (make-delay (bind-cur e2)))
  6539. (($ set! x e2)
  6540. (use-var
  6541. x
  6542. env
  6543. context
  6544. (lambda (b)
  6545. (when (name-struct b)
  6546. (syntax-err
  6547. (pexpr e)
  6548. "define-structure identifier ~a may not be assigned"
  6549. x))
  6550. (when (name-primitive b)
  6551. (syntax-err
  6552. (pexpr e)
  6553. "(set! ~a ...) requires (define ~a ...)"
  6554. x
  6555. x))
  6556. (when (and (not (name-mutated b))
  6557. (not (= (name-timestamp b)
  6558. timestamp)))
  6559. (syntax-err
  6560. (pexpr e)
  6561. "(set! ~a ...) missing from compilation unit defining ~a"
  6562. x
  6563. x))
  6564. (set-name-mutated! b #t)
  6565. (make-set! b (bind-cur e2)))))
  6566. (($ let args e2)
  6567. (let* ((b-args
  6568. (map (match-lambda
  6569. (($ bind x e)
  6570. (make-bind
  6571. (bind-var x)
  6572. (bind-cur e))))
  6573. args))
  6574. (newenv
  6575. (extend-env*
  6576. env
  6577. (map bind-name args)
  6578. (map bind-name b-args))))
  6579. (make-let
  6580. b-args
  6581. (bind e2 newenv tenv context))))
  6582. (($ let* args e2)
  6583. (recur loop
  6584. ((args args) (b-args '()) (env env))
  6585. (match args
  6586. ((($ bind x e) . rest)
  6587. (let ((b (bind-var x)))
  6588. (loop rest
  6589. (cons (make-bind
  6590. b
  6591. (bind e
  6592. env
  6593. tenv
  6594. context))
  6595. b-args)
  6596. (extend-env env x b))))
  6597. (()
  6598. (make-let*
  6599. (reverse b-args)
  6600. (bind e2 env tenv context))))))
  6601. (($ letr args e2)
  6602. (let* ((b-args
  6603. (map (match-lambda
  6604. (($ bind x e)
  6605. (make-bind (bind-var x) e)))
  6606. args))
  6607. (newenv
  6608. (extend-env*
  6609. env
  6610. (map bind-name args)
  6611. (map bind-name b-args)))
  6612. (b-args
  6613. (map (match-lambda
  6614. (($ bind b e)
  6615. (let* ((n (name-occ b))
  6616. (e2 (bind e
  6617. newenv
  6618. tenv
  6619. context)))
  6620. (set-name-occ! b n)
  6621. (make-bind b e2))))
  6622. b-args)))
  6623. (make-letr
  6624. b-args
  6625. (bind e2 newenv tenv context))))
  6626. (($ body defs exps)
  6627. (match-let*
  6628. (((defs newenv newtenv)
  6629. (bind-defn defs env tenv #f)))
  6630. (make-body
  6631. defs
  6632. (map (lambda (x)
  6633. (bind x newenv newtenv context))
  6634. exps))))
  6635. (($ record args)
  6636. (make-record
  6637. (map (match-lambda
  6638. (($ bind x e)
  6639. (new-field! x)
  6640. (make-bind x (bind-cur e))))
  6641. args)))
  6642. (($ field x e2)
  6643. (new-field! x)
  6644. (make-field x (bind-cur e2)))
  6645. (($ cast ty e2)
  6646. (match-let
  6647. (((t absv)
  6648. (handle
  6649. (r+collect
  6650. tenv
  6651. (match ty
  6652. (('rec bind ty2)
  6653. `(rec ,bind (,ty2 -> ,ty2)))
  6654. (_ `(,ty -> ,ty))))
  6655. (match-lambda*
  6656. (('type . args)
  6657. (apply syntax-err ty args))
  6658. (x (apply raise x))))))
  6659. (make-cast
  6660. (list ty t absv)
  6661. (bind-cur e2))))))))
  6662. (bind-mclause
  6663. (lambda (clause env tenv context)
  6664. (match-let*
  6665. ((($ mclause pattern body failsym) clause)
  6666. (patenv empty-env)
  6667. (bp (recur loop
  6668. ((p pattern))
  6669. (match p
  6670. (($ pvar x)
  6671. (when (bound? patenv x)
  6672. (syntax-err
  6673. (ppat pattern)
  6674. "pattern variable ~a repeated"
  6675. x))
  6676. (let ((b (bind-var x)))
  6677. (set! patenv (extend-env patenv x b))
  6678. (make-pvar b)))
  6679. (($ pobj c args)
  6680. (use-var
  6681. c
  6682. env
  6683. context
  6684. (lambda (b)
  6685. (cond ((boolean? (name-predicate b))
  6686. (syntax-err
  6687. (ppat pattern)
  6688. "~a is not a predicate"
  6689. c))
  6690. ((and (not (eq? b %vector?))
  6691. (not (eq? b %cvector?))
  6692. (not (= (length
  6693. (cdr (name-predicate
  6694. b)))
  6695. (length args))))
  6696. (syntax-err
  6697. (ppat pattern)
  6698. "~a requires ~a sub-patterns"
  6699. c
  6700. (length
  6701. (cdr (name-predicate
  6702. b)))))
  6703. (else
  6704. (make-pobj
  6705. b
  6706. (map loop args)))))))
  6707. (($ pand pats)
  6708. (make-pand (map loop pats)))
  6709. (($ pnot pat) (make-pnot (loop pat)))
  6710. (($ ppred pred)
  6711. (use-var
  6712. pred
  6713. env
  6714. context
  6715. (lambda (b)
  6716. (unless
  6717. (name-predicate b)
  6718. (syntax-err
  6719. (ppat pattern)
  6720. "~a is not a predicate"
  6721. pred))
  6722. (make-ppred b))))
  6723. (($ pany) p)
  6724. (($ pelse) p)
  6725. (($ pconst c pred)
  6726. (use-var
  6727. pred
  6728. initial-env
  6729. context
  6730. (lambda (p) (make-pconst c p))))))))
  6731. (if failsym
  6732. (let ((b (bind-var failsym)))
  6733. (when (bound? patenv failsym)
  6734. (syntax-err
  6735. (ppat pattern)
  6736. "fail symbol ~a repeated"
  6737. failsym))
  6738. (set! patenv (extend-env patenv failsym b))
  6739. (make-mclause
  6740. bp
  6741. (bind body (join-env env patenv) tenv context)
  6742. b))
  6743. (make-mclause
  6744. bp
  6745. (bind body (join-env env patenv) tenv context)
  6746. #f)))))
  6747. (bind-defn
  6748. (lambda (defs env tenv glob)
  6749. (let* ((newenv empty-env)
  6750. (newtenv empty-env)
  6751. (struct-def
  6752. (lambda (x pure)
  6753. (when (or (bound? newenv x)
  6754. (and glob (bound? initial-env x)))
  6755. (syntax-err
  6756. #f
  6757. "~a defined more than once"
  6758. x))
  6759. (let ((b (bind-var x)))
  6760. (set-name-primitive! b #t)
  6761. (set-name-struct! b #t)
  6762. (set-name-pure! b pure)
  6763. (set! newenv (extend-env newenv x b))
  6764. b)))
  6765. (bind1 (match-lambda
  6766. ((and z ($ define x e))
  6767. (cond ((not x) z)
  6768. ((bound? newenv x)
  6769. (if glob
  6770. (make-define #f (make-set! x e))
  6771. (syntax-err
  6772. #f
  6773. "~a defined more than once"
  6774. x)))
  6775. (else
  6776. (let ((b (bind-var x)))
  6777. (set-name-gdef! b glob)
  6778. (set! newenv
  6779. (extend-env newenv x b))
  6780. (make-define b e)))))
  6781. ((and d
  6782. ($ defstruct
  6783. tag
  6784. args
  6785. make
  6786. pred
  6787. get
  6788. set
  6789. getn
  6790. setn
  6791. mutable))
  6792. (let* ((make (struct-def
  6793. make
  6794. (map not mutable)))
  6795. (pred (struct-def pred #t))
  6796. (bind-get
  6797. (lambda (name n)
  6798. (match name
  6799. (($ some x)
  6800. (let ((b (struct-def
  6801. x
  6802. #t)))
  6803. (set-name-selector!
  6804. b
  6805. (lambda (x)
  6806. (make-pobj
  6807. pred
  6808. (map-with-n
  6809. (lambda (_ m)
  6810. (if (= m n)
  6811. x
  6812. (make-pany)))
  6813. get))))
  6814. (some b)))
  6815. (none none))))
  6816. (bind-set
  6817. (match-lambda
  6818. (($ some x)
  6819. (some (struct-def x #t)))
  6820. (none none)))
  6821. (get (map-with-n bind-get get))
  6822. (getn (map-with-n bind-get getn))
  6823. (set (map bind-set set))
  6824. (setn (map bind-set setn))
  6825. (_ (when (bound? newtenv tag)
  6826. (syntax-err
  6827. (pdef d)
  6828. "type constructor ~a defined more than once"
  6829. tag)))
  6830. (tc (bind-tycon
  6831. tag
  6832. mutable
  6833. (bound? tenv tag)
  6834. (lambda args
  6835. (apply syntax-err
  6836. (cons (pdef d)
  6837. args))))))
  6838. (set! newtenv (extend-env newtenv tag tc))
  6839. (set-name-predicate!
  6840. pred
  6841. `(,tc ,@(map (lambda (_) (gensym)) get)))
  6842. (make-defstruct
  6843. tc
  6844. args
  6845. make
  6846. pred
  6847. get
  6848. set
  6849. getn
  6850. setn
  6851. mutable)))
  6852. ((and d ($ datatype dt))
  6853. (make-datatype
  6854. (maplr (match-lambda
  6855. (((tag . args) . bindings)
  6856. (when (bound? newtenv tag)
  6857. (syntax-err
  6858. (pdef d)
  6859. "type constructor ~a defined more than once"
  6860. tag))
  6861. (let ((tc (bind-tycon
  6862. tag
  6863. (map (lambda (_) #f)
  6864. args)
  6865. (bound? tenv tag)
  6866. (lambda args
  6867. (apply syntax-err
  6868. (cons (pdef d)
  6869. args))))))
  6870. (set! newtenv
  6871. (extend-env newtenv tag tc))
  6872. (cons (cons tc args)
  6873. (maplr (match-lambda
  6874. (($ variant
  6875. con
  6876. pred
  6877. arg-types)
  6878. (let ((make (struct-def
  6879. con
  6880. #t))
  6881. (pred (struct-def
  6882. pred
  6883. #t)))
  6884. (set-name-predicate!
  6885. pred
  6886. (cons tc
  6887. args))
  6888. (set-name-variant!
  6889. pred
  6890. arg-types)
  6891. (make-variant
  6892. make
  6893. pred
  6894. arg-types))))
  6895. bindings)))))
  6896. dt)))))
  6897. (defs2 (maplr bind1 defs))
  6898. (newenv2 (join-env env newenv))
  6899. (newtenv2 (join-env tenv newtenv))
  6900. (bind2 (match-lambda
  6901. ((and ($ define (? name? x) ($ var y)))
  6902. (=> fail)
  6903. (if (eq? (name-name x) y)
  6904. (if (bound? initial-env y)
  6905. (make-define
  6906. x
  6907. (make-var (lookup initial-env y)))
  6908. (begin
  6909. (printf
  6910. "Warning: (define ~a ~a) but ~a is not a primitive~%"
  6911. y
  6912. y
  6913. y)
  6914. (fail)))
  6915. (fail)))
  6916. ((and ($ define x e2) context)
  6917. (when (and glob
  6918. (name? x)
  6919. (bound?
  6920. initial-env
  6921. (name-name x)))
  6922. (printf
  6923. "Note: (define ~a ...) hides primitive ~a~%"
  6924. (name-name x)
  6925. (name-name x)))
  6926. (make-define
  6927. (or x
  6928. (let ((b (bind-var x)))
  6929. (set-name-gdef! b glob)
  6930. b))
  6931. (bind e2 newenv2 newtenv2 context)))
  6932. (d d))))
  6933. (list (maplr bind2 defs2) newenv2 newtenv2))))
  6934. (bind-old
  6935. (lambda (e env)
  6936. (match e
  6937. (($ var x)
  6938. (match (lookup? env (name-name x))
  6939. (#f (set! unbound (cons e unbound)))
  6940. (b (when (and (name-primitive b)
  6941. (memq x cons-mutators))
  6942. (set! cons-mutable #t))
  6943. (set-name-occ! b (+ 1 (name-occ b)))
  6944. (set-var-name! e b))))
  6945. (($ set! x _)
  6946. (match (lookup? env (name-name x))
  6947. (#f (set! unbound (cons e unbound)))
  6948. (b (when (name-struct b)
  6949. (syntax-err
  6950. (pexpr e)
  6951. "define-structure identifier ~a may not be assigned"
  6952. x))
  6953. (when (name-primitive b)
  6954. (syntax-err
  6955. (pexpr e)
  6956. "(set! ~a ...) requires (define ~a ...)"
  6957. x
  6958. x))
  6959. (when (and (not (name-mutated b))
  6960. (not (= (name-timestamp b)
  6961. timestamp)))
  6962. (syntax-err
  6963. (pexpr e)
  6964. "(set! ~a ...) missing from compilation unit defining ~a"
  6965. x
  6966. x))
  6967. (set-name-mutated! b #t)
  6968. (set-name-occ! b (+ 1 (name-occ b)))
  6969. (set-set!-name! e b))))))))
  6970. (match-let
  6971. (((defs env tenv) (bind-defn defs env0 tenv0 #t)))
  6972. (for-each
  6973. (lambda (x) (bind-old x env))
  6974. old-unbound)
  6975. (set-cons-mutability! cons-mutable)
  6976. (set! n-unbound (length unbound))
  6977. (list defs env tenv unbound)))))
  6978. (define rebind-var
  6979. (lambda (b)
  6980. (make-name
  6981. (name-name b)
  6982. (name-ty b)
  6983. (name-timestamp b)
  6984. (name-occ b)
  6985. (name-mutated b)
  6986. #f
  6987. #f
  6988. #f
  6989. #f
  6990. #f
  6991. #f
  6992. #f)))
  6993. (define warn-unbound
  6994. (lambda (l)
  6995. (let* ((names '())
  6996. (node->name
  6997. (match-lambda
  6998. (($ var x) x)
  6999. (($ set! x _) x)
  7000. (($ pobj x _) x)
  7001. (($ ppred x) x)))
  7002. (warn (lambda (b)
  7003. (unless
  7004. (memq (name-name b) names)
  7005. (set! names (cons (name-name b) names))
  7006. (printf
  7007. "Warning: ~a is unbound in "
  7008. (name-name b))
  7009. (print-context (pexpr (name-timestamp b)) 2)))))
  7010. (for-each (lambda (x) (warn (node->name x))) l))))
  7011. (define name-unbound?
  7012. (lambda (x) (not (number? (name-timestamp x)))))
  7013. (define improve-defs
  7014. (lambda (defs)
  7015. (map (match-lambda
  7016. (($ define x e2) (make-define x (improve e2)))
  7017. (x x))
  7018. defs)))
  7019. (define improve
  7020. (match-lambda
  7021. (($ match e clauses) (improve-match e clauses))
  7022. (($ if tst thn els) (improve-if tst thn els))
  7023. ((? var? e) e)
  7024. ((? const? e) e)
  7025. (($ lam args e2) (make-lam args (improve e2)))
  7026. (($ vlam args rest e2)
  7027. (make-vlam args rest (improve e2)))
  7028. (($ app (and e1 ($ var x)) args)
  7029. (let ((args (map improve args)))
  7030. (if (and (eq? x %list) (< (length args) conslimit))
  7031. (foldr (lambda (a rest)
  7032. (make-app (make-var %cons) (list a rest)))
  7033. (make-const '() %null?)
  7034. args)
  7035. (make-app e1 args))))
  7036. (($ app e1 args)
  7037. (make-app (improve e1) (map improve args)))
  7038. (($ begin exps) (make-begin (map improve exps)))
  7039. (($ and exps) (make-and (map improve exps)))
  7040. (($ or exps) (make-or (map improve exps)))
  7041. (($ delay e2) (make-delay (improve e2)))
  7042. (($ set! x e2) (make-set! x (improve e2)))
  7043. (($ let args e2)
  7044. (let ((args (map (match-lambda
  7045. (($ bind x e) (make-bind x (improve e))))
  7046. args)))
  7047. (make-let args (improve e2))))
  7048. (($ let* args e2)
  7049. (let ((args (map (match-lambda
  7050. (($ bind x e) (make-bind x (improve e))))
  7051. args)))
  7052. (make-let* args (improve e2))))
  7053. (($ letr args e2)
  7054. (let ((args (map (match-lambda
  7055. (($ bind x e) (make-bind x (improve e))))
  7056. args)))
  7057. (make-letr args (improve e2))))
  7058. (($ body defs exps)
  7059. (let ((defs (improve-defs defs)))
  7060. (make-body defs (map improve exps))))
  7061. (($ record args)
  7062. (make-record
  7063. (map (match-lambda
  7064. (($ bind x e) (make-bind x (improve e))))
  7065. args)))
  7066. (($ field x e2) (make-field x (improve e2)))
  7067. (($ cast ty e2) (make-cast ty (improve e2)))))
  7068. (define improve-if
  7069. (lambda (tst thn els)
  7070. (let ((if->match
  7071. (lambda (x p mk-s thn els)
  7072. (let ((else-pat
  7073. (match els
  7074. (($ app ($ var q) _)
  7075. (if (eq? q %should-never-reach)
  7076. (make-pelse)
  7077. (make-pany)))
  7078. (_ (make-pany)))))
  7079. (make-match
  7080. (make-var x)
  7081. (list (make-mclause
  7082. (mk-s (make-ppred p))
  7083. (make-body '() (list thn))
  7084. #f)
  7085. (make-mclause
  7086. (mk-s else-pat)
  7087. (make-body '() (list els))
  7088. #f)))))))
  7089. (match tst
  7090. (($ app ($ var v) (e))
  7091. (=> fail)
  7092. (if (eq? v %not) (improve-if e els thn) (fail)))
  7093. (($ app ($ var eq) (($ const #f _) val))
  7094. (=> fail)
  7095. (if (or (eq? eq %eq?)
  7096. (eq? eq %eqv?)
  7097. (eq? eq %equal?))
  7098. (improve-if val els thn)
  7099. (fail)))
  7100. (($ app ($ var eq) (val ($ const #f _)))
  7101. (=> fail)
  7102. (if (or (eq? eq %eq?)
  7103. (eq? eq %eqv?)
  7104. (eq? eq %equal?))
  7105. (improve-if val els thn)
  7106. (fail)))
  7107. (($ app ($ var v) (($ var x)))
  7108. (=> fail)
  7109. (if (and (name-predicate v) (not (name-mutated x)))
  7110. (improve (if->match x v (lambda (x) x) thn els))
  7111. (fail)))
  7112. (($ app ($ var v) (($ app ($ var s) (($ var x)))))
  7113. (=> fail)
  7114. (if (and (name-predicate v)
  7115. (name-selector s)
  7116. (not (name-mutated x)))
  7117. (improve
  7118. (if->match x v (name-selector s) thn els))
  7119. (fail)))
  7120. (($ app ($ var v) (($ var x)))
  7121. (=> fail)
  7122. (if (and (name-selector v) (not (name-mutated x)))
  7123. (improve
  7124. (if->match
  7125. x
  7126. %false-object?
  7127. (name-selector v)
  7128. els
  7129. thn))
  7130. (fail)))
  7131. (($ var v)
  7132. (=> fail)
  7133. (if (not (name-mutated v))
  7134. (improve
  7135. (if->match
  7136. v
  7137. %false-object?
  7138. (lambda (x) x)
  7139. els
  7140. thn))
  7141. (fail)))
  7142. (_ (make-if
  7143. (improve tst)
  7144. (improve thn)
  7145. (improve els)))))))
  7146. (define improve-match
  7147. (lambda (e clauses)
  7148. (let ((clauses
  7149. (map (match-lambda
  7150. (($ mclause p body fail)
  7151. (make-mclause p (improve body) fail)))
  7152. clauses)))
  7153. (match e
  7154. (($ var x)
  7155. (if (not (name-mutated x))
  7156. (let ((fix-clause
  7157. (match-lambda
  7158. ((and c ($ mclause p e fail))
  7159. (if (not (uses-x? e x))
  7160. c
  7161. (let ((y (rebind-var x)))
  7162. (make-mclause
  7163. (make-flat-pand (list p (make-pvar y)))
  7164. (sub e x y)
  7165. fail)))))))
  7166. (make-match e (map fix-clause clauses)))
  7167. (make-match e clauses)))
  7168. (_ (make-match (improve e) clauses))))))
  7169. (define uses-x?
  7170. (lambda (e x)
  7171. (recur loop
  7172. ((e e))
  7173. (match e
  7174. (($ and exps) (ormap loop exps))
  7175. (($ app fun args)
  7176. (or (loop fun) (ormap loop args)))
  7177. (($ begin exps) (ormap loop exps))
  7178. (($ if e1 e2 e3)
  7179. (or (loop e1) (loop e2) (loop e3)))
  7180. (($ lam names body) (loop body))
  7181. (($ let bindings body)
  7182. (or (ormap (match-lambda (($ bind _ b) (loop b)))
  7183. bindings)
  7184. (loop body)))
  7185. (($ let* bindings body)
  7186. (or (ormap (match-lambda (($ bind _ b) (loop b)))
  7187. bindings)
  7188. (loop body)))
  7189. (($ letr bindings body)
  7190. (or (ormap (match-lambda (($ bind _ b) (loop b)))
  7191. bindings)
  7192. (loop body)))
  7193. (($ or exps) (ormap loop exps))
  7194. (($ delay e2) (loop e2))
  7195. (($ set! name exp) (or (eq? x name) (loop exp)))
  7196. (($ var name) (eq? x name))
  7197. (($ vlam names name body) (loop body))
  7198. (($ match exp clauses)
  7199. (or (loop exp)
  7200. (ormap (match-lambda
  7201. (($ mclause p b _) (or (loop p) (loop b))))
  7202. clauses)))
  7203. (($ body defs exps)
  7204. (or (ormap loop defs) (ormap loop exps)))
  7205. (($ record bindings)
  7206. (ormap (match-lambda (($ bind _ b) (loop b)))
  7207. bindings))
  7208. (($ field _ e) (loop e))
  7209. (($ cast _ e) (loop e))
  7210. (($ define _ e) (loop e))
  7211. ((? defstruct?) #f)
  7212. ((? datatype?) #f)
  7213. (($ pand pats) (ormap loop pats))
  7214. (($ pnot pat) (loop pat))
  7215. (($ pobj c args) (ormap loop args))
  7216. (($ ppred pred) (eq? x pred))
  7217. (_ #f)))))
  7218. (define sub
  7219. (lambda (e x to)
  7220. (let ((dos (lambda (y) (if (eq? x y) to y))))
  7221. (recur sub
  7222. ((e e))
  7223. (match e
  7224. (($ define x e) (make-define x (sub e)))
  7225. ((? defstruct?) e)
  7226. ((? datatype?) e)
  7227. (($ match e clauses)
  7228. (let ((clauses
  7229. (map (match-lambda
  7230. (($ mclause p e fail)
  7231. (make-mclause p (sub e) fail)))
  7232. clauses)))
  7233. (make-match (sub e) clauses)))
  7234. (($ if tst thn els)
  7235. (make-if (sub tst) (sub thn) (sub els)))
  7236. (($ var x) (make-var (dos x)))
  7237. ((? const? e) e)
  7238. (($ lam args e2) (make-lam args (sub e2)))
  7239. (($ vlam args rest e2)
  7240. (make-vlam args rest (sub e2)))
  7241. (($ app e1 args)
  7242. (make-app (sub e1) (map sub args)))
  7243. (($ begin exps) (make-begin (map sub exps)))
  7244. (($ and exps) (make-and (map sub exps)))
  7245. (($ or exps) (make-or (map sub exps)))
  7246. (($ delay e2) (make-delay (sub e2)))
  7247. (($ set! x e2) (make-set! (dos x) (sub e2)))
  7248. (($ let args e2)
  7249. (let ((args (map (match-lambda
  7250. (($ bind x e) (make-bind x (sub e))))
  7251. args)))
  7252. (make-let args (sub e2))))
  7253. (($ let* args e2)
  7254. (let ((args (map (match-lambda
  7255. (($ bind x e) (make-bind x (sub e))))
  7256. args)))
  7257. (make-let* args (sub e2))))
  7258. (($ letr args e2)
  7259. (let ((args (map (match-lambda
  7260. (($ bind x e) (make-bind x (sub e))))
  7261. args)))
  7262. (make-letr args (sub e2))))
  7263. (($ body defs exps)
  7264. (make-body (map sub defs) (map sub exps)))
  7265. (($ record args)
  7266. (make-record
  7267. (map (match-lambda
  7268. (($ bind x e) (make-bind x (sub e))))
  7269. args)))
  7270. (($ field x e) (make-field x (sub e)))
  7271. (($ cast ty e) (make-cast ty (sub e))))))))
  7272. (define improve-clauses
  7273. (lambda (clauses)
  7274. (recur loop
  7275. ((clauses clauses))
  7276. (match clauses
  7277. (() '())
  7278. ((_) clauses)
  7279. (((and m1 ($ mclause p _ fail)) . rest)
  7280. (cons m1
  7281. (if fail
  7282. (loop rest)
  7283. (recur loop2
  7284. ((clauses (loop rest)))
  7285. (match clauses
  7286. (() '())
  7287. (((and m ($ mclause p2 body2 fail2))
  7288. .
  7289. r)
  7290. (match (improve-by-pattern p2 p)
  7291. (('stop . p)
  7292. (cons (make-mclause
  7293. p
  7294. body2
  7295. fail2)
  7296. r))
  7297. (('redundant . p)
  7298. (unless
  7299. (null? r)
  7300. (printf
  7301. "Warning: redundant pattern ~a~%"
  7302. (ppat p2)))
  7303. (cons (make-mclause
  7304. p
  7305. body2
  7306. fail2)
  7307. r))
  7308. (('continue . p)
  7309. (cons (make-mclause
  7310. p
  7311. body2
  7312. fail2)
  7313. (loop2 r))))))))))))))
  7314. (define improve-by-pattern
  7315. (lambda (p2 p1)
  7316. (call-with-current-continuation
  7317. (lambda (k)
  7318. (let* ((reject (lambda () (k (cons 'continue p2))))
  7319. (p1covers #t)
  7320. (p2covers #t)
  7321. (p3 (recur m
  7322. ((p1 p1) (p2 p2))
  7323. '(printf "(M ~a ~a)~%" (ppat p1) (ppat p2))
  7324. (match (cons p1 p2)
  7325. ((($ pand (a . _)) . p2) (m a p2))
  7326. ((p1 $ pand (a . b))
  7327. (make-flat-pand (cons (m p1 a) b)))
  7328. ((($ pvar _) . _)
  7329. (unless
  7330. (or (pvar? p2) (pany? p2))
  7331. (set! p2covers #f))
  7332. p2)
  7333. ((($ pany) . _)
  7334. (unless
  7335. (or (pvar? p2) (pany? p2))
  7336. (set! p2covers #f))
  7337. p2)
  7338. ((($ pelse) . _)
  7339. '(unless
  7340. (or (pvar? p2) (pany? p2))
  7341. (set! p2covers #f))
  7342. p2)
  7343. ((_ $ pvar _)
  7344. (unless p1covers (reject))
  7345. (set! p1covers #f)
  7346. (make-flat-pand (list p2 (make-pnot p1))))
  7347. ((_ $ pany)
  7348. (unless p1covers (reject))
  7349. (set! p1covers #f)
  7350. (make-flat-pand (list p2 (make-pnot p1))))
  7351. ((_ $ pelse)
  7352. (unless p1covers (reject))
  7353. (set! p1covers #f)
  7354. (make-flat-pand (list p2 (make-pnot p1))))
  7355. ((($ pconst a _) $ pconst b _)
  7356. (unless (equal? a b) (reject))
  7357. p2)
  7358. ((($ pobj tag1 a) $ pobj tag2 b)
  7359. (unless (eq? tag1 tag2) (reject))
  7360. (make-pobj tag1 (map2 m a b)))
  7361. ((($ ppred tag1) $ ppred tag2)
  7362. (unless (eq? tag1 tag2) (reject))
  7363. p2)
  7364. ((($ ppred tag1) $ pobj tag2 _)
  7365. (unless (eq? tag1 tag2) (reject))
  7366. (set! p2covers #f)
  7367. p2)
  7368. ((($ ppred tag1) $ pconst c tag2)
  7369. (unless (eq? tag1 tag2) (reject))
  7370. (set! p2covers #f)
  7371. p2)
  7372. (_ (reject))))))
  7373. (cond (p1covers (cons 'redundant p2))
  7374. (p2covers (cons 'stop p3))
  7375. (else (cons 'continue p3))))))))
  7376. (define improve-by-noisily
  7377. (lambda (p2 p1)
  7378. (let ((r (improve-by-pattern p2 p1)))
  7379. (printf
  7380. "~a by ~a returns ~a ~a~%"
  7381. (ppat p2)
  7382. (ppat p1)
  7383. (car r)
  7384. (ppat (cdr r))))))
  7385. (define make-components
  7386. (lambda (d)
  7387. (let* ((structs
  7388. (filter-map
  7389. (match-lambda ((? define?) #f) (x x))
  7390. d))
  7391. (defs (filter-map
  7392. (match-lambda ((? define? x) x) (_ #f))
  7393. d))
  7394. (name-of (match-lambda (($ define x _) x)))
  7395. (ref-of
  7396. (match-lambda
  7397. (($ define _ e) (references e name-gdef))))
  7398. (comp (top-sort defs name-of ref-of)))
  7399. (when #f
  7400. (printf "Components:~%")
  7401. (pretty-print
  7402. (map (lambda (c)
  7403. (map (match-lambda
  7404. (($ define x _) (and x (name-name x))))
  7405. c))
  7406. comp)))
  7407. (append structs comp))))
  7408. (define make-body-components
  7409. (lambda (d)
  7410. (let* ((structs
  7411. (filter-map
  7412. (match-lambda ((? define?) #f) (x x))
  7413. d))
  7414. (defs (filter-map
  7415. (match-lambda ((? define? x) x) (_ #f))
  7416. d))
  7417. (name-of (match-lambda (($ define x _) x)))
  7418. (bound (map name-of defs))
  7419. (ref-of
  7420. (match-lambda
  7421. (($ define _ e)
  7422. (references e (lambda (x) (memq x bound))))))
  7423. (comp (top-sort defs name-of ref-of)))
  7424. (when #f
  7425. (printf "Components:~%")
  7426. (pretty-print
  7427. (map (lambda (c)
  7428. (map (match-lambda
  7429. (($ define x _) (and x (name-name x))))
  7430. c))
  7431. comp)))
  7432. (append structs comp))))
  7433. (define make-letrec-components
  7434. (lambda (bindings)
  7435. (let* ((name-of bind-name)
  7436. (bound (map name-of bindings))
  7437. (ref-of
  7438. (match-lambda
  7439. (($ bind _ e)
  7440. (references e (lambda (x) (memq x bound))))))
  7441. (comp (top-sort bindings name-of ref-of)))
  7442. (when #f
  7443. (printf "Letrec Components:~%")
  7444. (pretty-print
  7445. (map (lambda (c)
  7446. (map (match-lambda (($ bind x _) (pname x))) c))
  7447. comp)))
  7448. comp)))
  7449. (define references
  7450. (lambda (e ref?)
  7451. (recur loop
  7452. ((e e))
  7453. (match e
  7454. (($ define x e)
  7455. (if (and x (name-mutated x))
  7456. (union (set x) (loop e))
  7457. (loop e)))
  7458. ((? defstruct?) empty-set)
  7459. ((? datatype?) empty-set)
  7460. ((? const?) empty-set)
  7461. (($ var x) (if (ref? x) (set x) empty-set))
  7462. (($ lam _ e1) (loop e1))
  7463. (($ vlam _ _ e1) (loop e1))
  7464. (($ app e0 args)
  7465. (foldr union2 (loop e0) (map loop args)))
  7466. (($ let b e2)
  7467. (let ((do-bind (match-lambda (($ bind _ e) (loop e)))))
  7468. (foldr union2 (loop e2) (map do-bind b))))
  7469. (($ let* b e2)
  7470. (let ((do-bind (match-lambda (($ bind _ e) (loop e)))))
  7471. (foldr union2 (loop e2) (map do-bind b))))
  7472. (($ letr b e2)
  7473. (let ((do-bind (match-lambda (($ bind _ e) (loop e)))))
  7474. (foldr union2 (loop e2) (map do-bind b))))
  7475. (($ body defs exps)
  7476. (foldr union2
  7477. empty-set
  7478. (map loop (append defs exps))))
  7479. (($ record b)
  7480. (let ((do-bind (match-lambda (($ bind _ e) (loop e)))))
  7481. (foldr union2 empty-set (map do-bind b))))
  7482. (($ field _ e) (loop e))
  7483. (($ cast _ e) (loop e))
  7484. (($ and exps)
  7485. (foldr union2 empty-set (map loop exps)))
  7486. (($ or exps)
  7487. (foldr union2 empty-set (map loop exps)))
  7488. (($ begin exps)
  7489. (foldr union2 empty-set (map loop exps)))
  7490. (($ if test then els)
  7491. (union (loop test) (loop then) (loop els)))
  7492. (($ delay e) (loop e))
  7493. (($ set! x body)
  7494. (union (if (ref? x) (set x) empty-set)
  7495. (loop body)))
  7496. (($ match exp clauses)
  7497. (foldr union2
  7498. (loop exp)
  7499. (map (match-lambda (($ mclause _ exp _) (loop exp)))
  7500. clauses)))))))
  7501. (define top-sort
  7502. (lambda (graph name-of references-of)
  7503. (let* ((adj assq)
  7504. (g (map (lambda (x)
  7505. (list (name-of x)
  7506. (box (references-of x))
  7507. (box #f)
  7508. x))
  7509. graph))
  7510. (gt (let ((gt (map (match-lambda
  7511. ((n _ _ name)
  7512. (list n (box empty-set) (box #f) n)))
  7513. g)))
  7514. (for-each
  7515. (match-lambda
  7516. ((n nay _ _)
  7517. (for-each
  7518. (lambda (v)
  7519. (match (adj v gt)
  7520. (#f #f)
  7521. ((_ b _ _) (set-box! b (cons n (unbox b))))))
  7522. (unbox nay))))
  7523. g)
  7524. gt))
  7525. (visit (lambda (vg)
  7526. (letrec ((visit (lambda (g l)
  7527. (match g
  7528. (#f l)
  7529. ((n nay mark name)
  7530. (if (unbox mark)
  7531. l
  7532. (begin
  7533. (set-box! mark #t)
  7534. (cons name
  7535. (foldr (lambda (v l)
  7536. (visit (adj v
  7537. vg)
  7538. l))
  7539. l
  7540. (unbox nay))))))))))
  7541. visit)))
  7542. (visit-gt (visit gt))
  7543. (visit-g (visit g))
  7544. (post (foldr visit-gt '() gt))
  7545. (pre (foldl (lambda (gg l)
  7546. (match (visit-g (adj gg g) '())
  7547. (() l)
  7548. (c (cons c l))))
  7549. '()
  7550. post)))
  7551. (reverse pre))))
  7552. (define genlet #t)
  7553. (define genmatch #t)
  7554. (define letonce #f)
  7555. (define type-defs
  7556. (lambda (d)
  7557. (for-each
  7558. (match-lambda
  7559. ((? defstruct? b) (type-structure b))
  7560. ((? datatype? b) (type-structure b))
  7561. (c (type-component c #t)))
  7562. (make-components d))
  7563. (close '())))
  7564. (define type-structure
  7565. (match-lambda
  7566. (($ defstruct
  7567. x
  7568. _
  7569. make
  7570. pred
  7571. get
  7572. set
  7573. getn
  7574. setn
  7575. mutable)
  7576. (let* ((vars (map (lambda (_) (gensym)) get))
  7577. (make-get-type
  7578. (lambda (getter v)
  7579. (match getter
  7580. (($ some b)
  7581. (set-name-ty!
  7582. b
  7583. (closeall
  7584. (r+ initial-type-env `((,x ,@vars) -> ,v)))))
  7585. (_ #f))))
  7586. (make-set-type
  7587. (lambda (setter v)
  7588. (match setter
  7589. (($ some b)
  7590. (set-name-ty!
  7591. b
  7592. (closeall
  7593. (r+ initial-type-env `((,x ,@vars) ,v -> void)))))
  7594. (_ #f)))))
  7595. (set-name-ty!
  7596. make
  7597. (closeall
  7598. (r+ initial-type-env `(,@vars -> (,x ,@vars)))))
  7599. (set-name-ty!
  7600. pred
  7601. (closeall
  7602. (r+ initial-type-env
  7603. `((+ (,x ,@vars) y) -> bool))))
  7604. (for-each2 make-get-type get vars)
  7605. (for-each2 make-set-type set vars)
  7606. (for-each2 make-get-type getn vars)
  7607. (for-each2 make-set-type setn vars)))
  7608. (($ datatype dt)
  7609. (for-each
  7610. (match-lambda
  7611. ((type . variants)
  7612. (for-each
  7613. (match-lambda
  7614. (($ variant con pred arg-types)
  7615. (set-name-ty!
  7616. con
  7617. (closeall
  7618. (r+ initial-type-env
  7619. `(,@(cdr arg-types) -> ,type))))
  7620. (set-name-ty!
  7621. pred
  7622. (closeall
  7623. (r+ initial-type-env
  7624. `((+ ,(name-predicate pred) x) -> bool))))))
  7625. variants)))
  7626. dt))))
  7627. (define type-component
  7628. (lambda (component top)
  7629. (when verbose
  7630. (let ((cnames
  7631. (filter-map
  7632. (match-lambda (($ define b _) (name-name b)))
  7633. component)))
  7634. (unless
  7635. (null? cnames)
  7636. (printf "Typing ~a~%" cnames))))
  7637. (let* ((f (match-lambda (($ define b e) (make-bind b e))))
  7638. (bindings (map f component))
  7639. (names (map (match-lambda (($ define b _) (pname b)))
  7640. component))
  7641. (f1 (match-lambda
  7642. (($ define b _) (set-name-ty! b (tvar)))))
  7643. (f2 (match-lambda
  7644. ((and d ($ define b e))
  7645. (set-define-exp! d (w e names)))))
  7646. (f3 (match-lambda
  7647. (($ define b e) (unify (name-ty b) (typeof e)))))
  7648. (f4 (match-lambda (($ define b _) (name-ty b))))
  7649. (f5 (lambda (d ts)
  7650. (match d (($ define b _) (set-name-ty! b ts))))))
  7651. (push-level)
  7652. (for-each f1 component)
  7653. (for-each f2 component)
  7654. (for-each f3 component)
  7655. (for-each limit-expansive component)
  7656. (for-each
  7657. f5
  7658. component
  7659. (close (map f4 component)))
  7660. (pop-level))))
  7661. (define w
  7662. (lambda (e component)
  7663. (match e
  7664. (($ const _ pred)
  7665. (make-type
  7666. (r+ initial-type-env (name-predicate pred))
  7667. e))
  7668. (($ var x)
  7669. (unless
  7670. (name-ty x)
  7671. (set-name-ty!
  7672. x
  7673. (if (name-mutated x)
  7674. (monotvar)
  7675. (let* ((_1 (push-level))
  7676. (t (closeall (tvar)))
  7677. (_2 (pop-level)))
  7678. t))))
  7679. (if (ts? (name-ty x))
  7680. (match-let*
  7681. ((tynode (make-type #f #f))
  7682. ((t absv) (instantiate (name-ty x) tynode)))
  7683. (set-type-ty! tynode t)
  7684. (set-type-exp!
  7685. tynode
  7686. (match (name-primitive x)
  7687. ('imprecise
  7688. (make-check (list absv #f #f #f component) e))
  7689. ('check
  7690. (make-check
  7691. (list (cons top absv) #f #f #f component)
  7692. e))
  7693. ('nocheck e)
  7694. (#t
  7695. (make-check
  7696. (list absv (mk-definite-prim t) #f #f component)
  7697. e))
  7698. (#f
  7699. (make-check (list absv #f #f #t component) e))))
  7700. tynode)
  7701. e))
  7702. (($ lam x e1)
  7703. (for-each (lambda (b) (set-name-ty! b (tvar))) x)
  7704. (match-let*
  7705. ((body (w e1 component))
  7706. ((t absv)
  7707. (r+collect
  7708. initial-type-env
  7709. `(,@(map name-ty x) -> ,(typeof body)))))
  7710. (make-type
  7711. t
  7712. (make-check
  7713. (list absv (mk-definite-lam t) #f #f component)
  7714. (make-lam x body)))))
  7715. (($ vlam x rest e1)
  7716. (for-each (lambda (b) (set-name-ty! b (tvar))) x)
  7717. (match-let*
  7718. ((z (tvar))
  7719. (_ (set-name-ty!
  7720. rest
  7721. (r+ initial-type-env `(list ,z))))
  7722. (body (w e1 component))
  7723. ((t absv)
  7724. (r+collect
  7725. initial-type-env
  7726. `(,@(map name-ty x) (&list ,z) -> ,(typeof body)))))
  7727. (make-type
  7728. t
  7729. (make-check
  7730. (list absv (mk-definite-lam t) #f #f component)
  7731. (make-vlam x rest body)))))
  7732. (($ app e0 args)
  7733. (match-let*
  7734. ((t0 (w e0 component))
  7735. (targs (maplr (lambda (e) (w e component)) args))
  7736. (a* (map (lambda (_) (tvar)) args))
  7737. (b (tvar))
  7738. ((t absv)
  7739. (r-collect initial-type-env `(,@a* -> ,b)))
  7740. (definf (mk-definite-app t)))
  7741. (unify (typeof t0) t)
  7742. (for-each2 unify (map typeof targs) a*)
  7743. (if (syntactically-a-procedure? t0)
  7744. (make-type b (make-app t0 targs))
  7745. (make-type
  7746. b
  7747. (make-check
  7748. (list absv definf #f #f component)
  7749. (make-app t0 targs))))))
  7750. (($ let b e2)
  7751. (let* ((do-bind
  7752. (match-lambda
  7753. (($ bind b e)
  7754. (if genlet
  7755. (let* ((_ (push-level))
  7756. (e (w e (list (pname b))))
  7757. (bind (make-bind b e)))
  7758. (limit-expansive bind)
  7759. (set-name-ty! b (car (close (list (typeof e)))))
  7760. (pop-level)
  7761. bind)
  7762. (let ((e (w e component)))
  7763. (set-name-ty! b (typeof e))
  7764. (make-bind b e))))))
  7765. (tb (map do-bind b))
  7766. (body (w e2 component)))
  7767. (make-let tb body)))
  7768. (($ let* b e2)
  7769. (let* ((do-bind
  7770. (match-lambda
  7771. (($ bind b e)
  7772. (if genlet
  7773. (let* ((_ (push-level))
  7774. (e (w e (list (pname b))))
  7775. (bind (make-bind b e)))
  7776. (limit-expansive bind)
  7777. (set-name-ty! b (car (close (list (typeof e)))))
  7778. (pop-level)
  7779. bind)
  7780. (let ((e (w e component)))
  7781. (set-name-ty! b (typeof e))
  7782. (make-bind b e))))))
  7783. (tb (maplr do-bind b))
  7784. (body (w e2 component)))
  7785. (make-let* tb body)))
  7786. (($ letr b e2)
  7787. (let* ((do-comp
  7788. (lambda (b)
  7789. (if genlet
  7790. (let* ((f1 (match-lambda
  7791. (($ bind b _) (set-name-ty! b (tvar)))))
  7792. (names (map (match-lambda
  7793. (($ bind b _) (pname b)))
  7794. b))
  7795. (f2 (match-lambda
  7796. (($ bind b e)
  7797. (make-bind b (w e names)))))
  7798. (f3 (match-lambda
  7799. (($ bind b e)
  7800. (unify (name-ty b) (typeof e))
  7801. (name-ty b))))
  7802. (f4 (lambda (bind ts)
  7803. (match bind
  7804. (($ bind b _)
  7805. (set-name-ty! b ts)))))
  7806. (_1 (push-level))
  7807. (_2 (for-each f1 b))
  7808. (tb (maplr f2 b))
  7809. (_3 (for-each limit-expansive tb))
  7810. (ts-list (close (maplr f3 tb))))
  7811. (pop-level)
  7812. (for-each2 f4 tb ts-list)
  7813. tb)
  7814. (let* ((f1 (match-lambda
  7815. (($ bind b _) (set-name-ty! b (tvar)))))
  7816. (f2 (match-lambda
  7817. (($ bind b e)
  7818. (make-bind b (w e component)))))
  7819. (f3 (match-lambda
  7820. (($ bind b e)
  7821. (unify (name-ty b) (typeof e)))))
  7822. (_1 (for-each f1 b))
  7823. (tb (maplr f2 b)))
  7824. (for-each f3 tb)
  7825. tb))))
  7826. (comps (make-letrec-components b))
  7827. (tb (foldr append '() (maplr do-comp comps))))
  7828. (make-letr tb (w e2 component))))
  7829. (($ body defs exps)
  7830. (for-each
  7831. (match-lambda
  7832. ((? defstruct? b) (type-structure b))
  7833. ((? datatype? b) (type-structure b))
  7834. (c (type-component c #f)))
  7835. (make-body-components defs))
  7836. (let ((texps (maplr (lambda (x) (w x component)) exps)))
  7837. (make-body defs texps)))
  7838. (($ and exps)
  7839. (let* ((texps (maplr (lambda (x) (w x component)) exps))
  7840. (t (match texps
  7841. (() (r+ initial-type-env 'true))
  7842. ((e) (typeof e))
  7843. (_ (let ((a (r+ initial-type-env 'false)))
  7844. (unify (typeof (rac texps)) a)
  7845. a)))))
  7846. (make-type t (make-and texps))))
  7847. (($ or exps)
  7848. (let* ((texps (maplr (lambda (x) (w x component)) exps))
  7849. (t (match texps
  7850. (() (r+ initial-type-env 'false))
  7851. ((e) (typeof e))
  7852. (_ (let* ((t-last (typeof (rac texps)))
  7853. (but-last (rdc texps))
  7854. (a (tvar)))
  7855. (for-each
  7856. (lambda (e)
  7857. (unify (typeof e)
  7858. (r+ initial-type-env
  7859. `(+ (not false) ,a))))
  7860. but-last)
  7861. (unify t-last
  7862. (r+ initial-type-env
  7863. `(+ (not false) ,a)))
  7864. t-last)))))
  7865. (make-type t (make-or texps))))
  7866. (($ begin exps)
  7867. (let ((texps (maplr (lambda (x) (w x component)) exps)))
  7868. (make-begin texps)))
  7869. (($ if test then els)
  7870. (let ((ttest (w test component))
  7871. (tthen (w then component))
  7872. (tels (w els component))
  7873. (a (tvar)))
  7874. (unify (typeof tthen) a)
  7875. (unify (typeof tels) a)
  7876. (make-type a (make-if ttest tthen tels))))
  7877. (($ delay e2)
  7878. (let ((texp (w e2 component)))
  7879. (make-type
  7880. (r+ initial-type-env `(promise ,(typeof texp)))
  7881. (make-delay texp))))
  7882. (($ set! x body)
  7883. (unless (name-ty x) (set-name-ty! x (monotvar)))
  7884. (let* ((body (w body component))
  7885. (t (if (ts? (name-ty x))
  7886. (car (instantiate (name-ty x) #f))
  7887. (name-ty x))))
  7888. (unify t (typeof body))
  7889. (make-type
  7890. (r+ initial-type-env 'void)
  7891. (make-set! x body))))
  7892. (($ record bind)
  7893. (let* ((tbind (map (match-lambda
  7894. (($ bind name exp)
  7895. (make-bind name (w exp component))))
  7896. bind))
  7897. (t (r+ initial-type-env
  7898. `(record
  7899. ,@(map (match-lambda
  7900. (($ bind name exp)
  7901. (list name (typeof exp))))
  7902. tbind)))))
  7903. (make-type t (make-record tbind))))
  7904. (($ field name exp)
  7905. (match-let*
  7906. ((texp (w exp component))
  7907. (a (tvar))
  7908. ((t absv)
  7909. (r-collect initial-type-env `(record (,name ,a)))))
  7910. (unify (typeof texp) t)
  7911. (make-type
  7912. a
  7913. (make-check
  7914. (list absv #f #f #f component)
  7915. (make-field name texp)))))
  7916. (($ cast (ty t absv) exp)
  7917. (let ((texp (w exp component)) (a (tvar)))
  7918. (unify (r+ initial-type-env `(,(typeof texp) -> ,a))
  7919. t)
  7920. (make-type
  7921. a
  7922. (make-check
  7923. (list absv #f #f #f component)
  7924. (make-cast (list ty t absv) texp)))))
  7925. (($ match exp clauses)
  7926. (for-each
  7927. (match-lambda
  7928. (($ mclause p _ (? name? fail))
  7929. (set-name-ty!
  7930. fail
  7931. (r+ initial-type-env '(a ?-> b))))
  7932. (_ #f))
  7933. clauses)
  7934. (match-let*
  7935. ((iclauses
  7936. (improve-clauses
  7937. (append
  7938. clauses
  7939. (list (make-mclause (make-pelse) #f #f)))))
  7940. ((tmatch absv precise)
  7941. (w-match (rdc iclauses) (rac iclauses)))
  7942. (texp (w exp component))
  7943. (_ (unify (typeof texp) tmatch))
  7944. (tclauses
  7945. (maplr (match-lambda
  7946. (($ mclause p e fail)
  7947. (make-mclause p (w e component) fail)))
  7948. clauses))
  7949. (a (tvar)))
  7950. (for-each
  7951. (match-lambda
  7952. (($ mclause _ e _) (unify (typeof e) a)))
  7953. tclauses)
  7954. (make-type
  7955. a
  7956. (make-check
  7957. (list absv #f (not precise) #f component)
  7958. (make-match texp tclauses))))))))
  7959. (define w-match
  7960. (lambda (clauses last)
  7961. (letrec ((bindings '())
  7962. (encode
  7963. (match-lambda
  7964. (($ pand pats) (encode* pats))
  7965. (x (encode* (list x)))))
  7966. (encode*
  7967. (lambda (pats)
  7968. (let* ((concrete?
  7969. (lambda (p)
  7970. (or (pconst? p) (pobj? p) (ppred? p) (pelse? p))))
  7971. (var? (lambda (p) (or (pvar? p) (pany? p))))
  7972. (not-var?
  7973. (lambda (p)
  7974. (and (not (pvar? p)) (not (pany? p)))))
  7975. (t (match (filter concrete? pats)
  7976. ((p)
  7977. (r+ initial-type-env
  7978. (match (template p)
  7979. ((x) x)
  7980. (x `(+ ,@x)))))
  7981. (()
  7982. (r+ initial-type-env
  7983. `(+ ,@(apply append
  7984. (map template
  7985. (filter
  7986. not-var?
  7987. pats)))
  7988. ,@(if (null? (filter var? pats))
  7989. '()
  7990. (list (out1tvar)))))))))
  7991. (for-each
  7992. (match-lambda
  7993. (($ pvar b)
  7994. (set! bindings (cons b bindings))
  7995. (set-name-ty! b (pat-var-bind t))))
  7996. (filter pvar? pats))
  7997. t)))
  7998. (template
  7999. (match-lambda
  8000. ((? pelse?) '())
  8001. (($ pconst _ pred) (list (name-predicate pred)))
  8002. ((and pat ($ pobj c args))
  8003. (list (cond ((or (eq? %vector? c) (eq? %cvector? c))
  8004. (cons (if (eq? %vector? c) 'vec 'cvec)
  8005. (match (maplr encode args)
  8006. (() (list (out1tvar)))
  8007. ((first . rest)
  8008. (list (foldr (lambda (x y)
  8009. (unify x y)
  8010. y)
  8011. first
  8012. rest))))))
  8013. (else
  8014. (cons (car (name-predicate c))
  8015. (maplr encode args))))))
  8016. (($ ppred pred)
  8017. (cond ((eq? pred %boolean?) (list 'true 'false))
  8018. ((eq? pred %list?) (list `(list ,(out1tvar))))
  8019. (else
  8020. (list (cons (car (name-predicate pred))
  8021. (maplr (lambda (_) (out1tvar))
  8022. (cdr (name-predicate pred))))))))
  8023. (($ pnot (? pconst?)) '())
  8024. (($ pnot ($ ppred pred))
  8025. (cond ((eq? pred %boolean?) '((not true) (not false)))
  8026. ((eq? pred %procedure?) '((not ?->)))
  8027. ((eq? pred %list?) '())
  8028. (else `((not ,(car (name-predicate pred)))))))
  8029. (($ pnot ($ pobj pred pats))
  8030. (let ((m (foldr + 0 (map non-triv pats))))
  8031. (case m
  8032. ((0) `((not ,(car (name-predicate pred)))))
  8033. ((1)
  8034. `((,(car (name-predicate pred))
  8035. ,@(map (match-lambda
  8036. (($ pobj pred _)
  8037. `(+ (not ,(car (name-predicate pred)))
  8038. ,(out1tvar)))
  8039. (($ ppred pred)
  8040. `(+ (not ,(car (name-predicate pred)))
  8041. ,(out1tvar)))
  8042. (_ (out1tvar)))
  8043. pats))))
  8044. (else '()))))))
  8045. (non-triv
  8046. (match-lambda
  8047. ((? pvar?) 0)
  8048. ((? pany?) 0)
  8049. ((? pelse?) 0)
  8050. ((? pconst?) 2)
  8051. (($ pobj _ pats) (foldr + 1 (map non-triv pats)))
  8052. (_ 1)))
  8053. (precise
  8054. (match-lambda
  8055. ((? pconst?) #f)
  8056. (($ pand pats) (andmap precise pats))
  8057. (($ pnot pat) (precise pat))
  8058. (($ pobj pred pats)
  8059. (let ((m (foldr + 0 (map non-triv pats))))
  8060. (case m
  8061. ((0) #t)
  8062. ((1) (andmap precise pats))
  8063. (else #f))))
  8064. (($ ppred pred) (not (eq? pred %list?)))
  8065. (_ #t))))
  8066. (push-level)
  8067. (match-let*
  8068. ((precise-match
  8069. (and (andmap
  8070. (match-lambda (($ mclause _ _ fail) (not fail)))
  8071. clauses)
  8072. (match last (($ mclause p _ _) (precise p)))))
  8073. (types (maplr (match-lambda (($ mclause p _ _) (encode p)))
  8074. clauses))
  8075. ((t absv)
  8076. (r-match
  8077. (foldr (lambda (x y) (unify x y) y) (tvar) types))))
  8078. (unify (out1tvar) t)
  8079. (for-each limit-name bindings)
  8080. (for-each2
  8081. set-name-ty!
  8082. bindings
  8083. (close (map name-ty bindings)))
  8084. (pop-level)
  8085. '(pretty-print
  8086. `(match-input
  8087. ,@(map (match-lambda (($ mclause p _ _) (ppat p)))
  8088. clauses)))
  8089. '(pretty-print
  8090. `(match-type
  8091. ,(ptype t)
  8092. ,@(map (lambda (b) (list (pname b) (ptype (name-ty b))))
  8093. bindings)))
  8094. (list t absv precise-match)))))
  8095. (define syntactically-a-procedure?
  8096. (match-lambda
  8097. (($ type _ e) (syntactically-a-procedure? e))
  8098. (($ check _ e) (syntactically-a-procedure? e))
  8099. (($ var x) (name-primitive x))
  8100. ((? lam?) #t)
  8101. ((? vlam?) #t)
  8102. (($ let _ body)
  8103. (syntactically-a-procedure? body))
  8104. (($ let* _ body)
  8105. (syntactically-a-procedure? body))
  8106. (($ letr _ body)
  8107. (syntactically-a-procedure? body))
  8108. (($ if _ e2 e3)
  8109. (and (syntactically-a-procedure? e2)
  8110. (syntactically-a-procedure? e3)))
  8111. (($ begin exps)
  8112. (syntactically-a-procedure? (rac exps)))
  8113. (($ body _ exps)
  8114. (syntactically-a-procedure? (rac exps)))
  8115. (_ #f)))
  8116. (define typeof
  8117. (match-lambda
  8118. (($ type t _) t)
  8119. (($ check _ e) (typeof e))
  8120. (($ let _ body) (typeof body))
  8121. (($ let* _ body) (typeof body))
  8122. (($ letr _ body) (typeof body))
  8123. (($ body _ exps) (typeof (rac exps)))
  8124. (($ begin exps) (typeof (rac exps)))
  8125. (($ var x) (name-ty x))))
  8126. (define limit-name
  8127. (lambda (n)
  8128. (when (name-mutated n)
  8129. (unify (name-ty n) (out1tvar)))))
  8130. (define limit-expansive
  8131. (letrec ((limit! (lambda (t) (unify t (out1tvar))))
  8132. (expansive-pattern?
  8133. (match-lambda
  8134. ((? pconst?) #f)
  8135. (($ pvar x) (name-mutated x))
  8136. (($ pobj _ pats) (ormap expansive-pattern? pats))
  8137. ((? pany?) #f)
  8138. ((? pelse?) #f)
  8139. (($ pand pats) (ormap expansive-pattern? pats))
  8140. (($ ppred x) (name-mutated x))
  8141. (($ pnot pat) (expansive-pattern? pat))))
  8142. (limit-expr
  8143. (match-lambda
  8144. (($ bind b e)
  8145. (if (name-mutated b)
  8146. (limit! (typeof e))
  8147. (limit-expr e)))
  8148. ((? defstruct?) #f)
  8149. ((? datatype?) #f)
  8150. (($ define x e)
  8151. (if (and x (name-mutated x))
  8152. (limit! (typeof e))
  8153. (limit-expr e)))
  8154. (($ type
  8155. t
  8156. ($ app ($ type _ ($ check _ ($ var x))) exps))
  8157. (cond ((list? (name-pure x))
  8158. (if (= (length (name-pure x)) (length exps))
  8159. (for-each2
  8160. (lambda (pure e)
  8161. (if pure (limit-expr e) (limit! (typeof e))))
  8162. (name-pure x)
  8163. exps)
  8164. (limit! t)))
  8165. ((or (eq? #t (name-pure x))
  8166. (and (eq? 'cons (name-pure x))
  8167. (not cons-is-mutable)))
  8168. (for-each limit-expr exps))
  8169. (else (limit! t))))
  8170. (($ type t ($ app _ _)) (limit! t))
  8171. (($ type t ($ check _ ($ app _ _))) (limit! t))
  8172. (($ delay _) #f)
  8173. (($ type t ($ set! _ _)) (limit! t))
  8174. (($ var _) #f)
  8175. ((? const?) #f)
  8176. (($ lam _ _) #f)
  8177. (($ vlam _ _ _) #f)
  8178. (($ let bind body)
  8179. (limit-expr body)
  8180. (for-each limit-expr bind))
  8181. (($ let* bind body)
  8182. (limit-expr body)
  8183. (for-each limit-expr bind))
  8184. (($ letr bind body)
  8185. (limit-expr body)
  8186. (for-each limit-expr bind))
  8187. (($ body defs exps)
  8188. (for-each limit-expr defs)
  8189. (for-each limit-expr exps))
  8190. (($ and exps) (for-each limit-expr exps))
  8191. (($ or exps) (for-each limit-expr exps))
  8192. (($ begin exps) (for-each limit-expr exps))
  8193. (($ if e1 e2 e3)
  8194. (limit-expr e1)
  8195. (limit-expr e2)
  8196. (limit-expr e3))
  8197. (($ record bind)
  8198. (for-each
  8199. (match-lambda (($ bind _ e) (limit-expr e)))
  8200. bind))
  8201. (($ field _ exp) (limit-expr exp))
  8202. (($ cast _ exp) (limit-expr exp))
  8203. (($ match exp clauses)
  8204. (limit-expr exp)
  8205. (for-each
  8206. (match-lambda
  8207. (($ mclause pat body fail)
  8208. (if (or (and fail (name-mutated fail))
  8209. (expansive-pattern? pat))
  8210. (limit! (typeof body))
  8211. (limit-expr body))))
  8212. clauses))
  8213. (($ type _ e1) (limit-expr e1))
  8214. (($ check _ e1) (limit-expr e1)))))
  8215. limit-expr))
  8216. (define unparse
  8217. (lambda (e check-action)
  8218. (letrec ((pbind (match-lambda
  8219. (($ bind n e) (list (pname n) (pexpr e)))))
  8220. (pexpr (match-lambda
  8221. ((and x ($ type _ (? check?)))
  8222. (check-action x pexpr))
  8223. (($ type _ exp) (pexpr exp))
  8224. (($ shape t exp) (pexpr exp))
  8225. (($ define x e)
  8226. (if (or (not x) (and (name? x) (not (name-name x))))
  8227. (pexpr e)
  8228. `(define ,(pname x) ,(pexpr e))))
  8229. (($ defstruct _ args _ _ _ _ _ _ _)
  8230. `(check-define-const-structure ,args))
  8231. (($ datatype d)
  8232. `(datatype
  8233. ,@(map (match-lambda
  8234. (((tag . args) . bindings)
  8235. (cons (cons (ptag tag) args)
  8236. (map (match-lambda
  8237. (($ variant _ _ types) types))
  8238. bindings))))
  8239. d)))
  8240. (($ and exps) `(and ,@(maplr pexpr exps)))
  8241. (($ or exps) `(or ,@(maplr pexpr exps)))
  8242. (($ begin exps) `(begin ,@(maplr pexpr exps)))
  8243. (($ var x) (pname x))
  8244. (($ prim x) (pname x))
  8245. (($ const x _) (pconst x))
  8246. (($ lam x e1)
  8247. `(lambda ,(maplr pname x) ,@(pexpr e1)))
  8248. (($ vlam x rest e1)
  8249. `(lambda ,(append (maplr pname x) (pname rest))
  8250. ,@(pexpr e1)))
  8251. (($ match e1 clauses)
  8252. (let* ((pclause
  8253. (match-lambda
  8254. (($ mclause p #f #f)
  8255. `(,(ppat p) <last clause>))
  8256. (($ mclause p exp fail)
  8257. (if fail
  8258. `(,(ppat p)
  8259. (=> ,(pname fail))
  8260. ,@(pexpr exp))
  8261. `(,(ppat p) ,@(pexpr exp))))))
  8262. (p1 (pexpr e1)))
  8263. `(match ,p1 ,@(maplr pclause clauses))))
  8264. (($ app e1 args)
  8265. (let* ((p1 (pexpr e1))
  8266. (pargs (maplr pexpr args))
  8267. (unkwote
  8268. (match-lambda
  8269. (('quote x) x)
  8270. ((? boolean? x) x)
  8271. ((? number? x) x)
  8272. ((? char? x) x)
  8273. ((? string? x) x)
  8274. ((? null? x) x)
  8275. ((? box? x) x)
  8276. ((? vector? x) x))))
  8277. (cond ((eq? p1 qlist) `',(maplr unkwote pargs))
  8278. ((eq? p1 qcons)
  8279. (let ((unq (maplr unkwote pargs)))
  8280. `',(cons (car unq) (cadr unq))))
  8281. ((eq? p1 qbox) (box (unkwote (car pargs))))
  8282. ((eq? p1 qvector)
  8283. (list->vector (maplr unkwote pargs)))
  8284. (else (cons p1 pargs)))))
  8285. (($ let b e2)
  8286. (let ((pb (maplr pbind b)))
  8287. `(let ,pb ,@(pexpr e2))))
  8288. (($ let* b e2)
  8289. (let ((pb (maplr pbind b)))
  8290. `(let* ,pb ,@(pexpr e2))))
  8291. (($ letr b e2)
  8292. (let ((pb (maplr pbind b)))
  8293. `(letrec ,pb ,@(pexpr e2))))
  8294. (($ body defs exps)
  8295. (let ((pdefs (maplr pexpr defs)))
  8296. (append pdefs (maplr pexpr exps))))
  8297. (($ if e1 e2 e3)
  8298. (let* ((p1 (pexpr e1)) (p2 (pexpr e2)) (p3 (pexpr e3)))
  8299. `(if ,p1 ,p2 ,p3)))
  8300. (($ record bindings)
  8301. `(record ,@(maplr pbind bindings)))
  8302. (($ field x e2) `(field ,x ,(pexpr e2)))
  8303. (($ cast (ty . _) e2) `(: ,ty ,(pexpr e2)))
  8304. (($ delay e) `(delay ,(pexpr e)))
  8305. (($ set! x e) `(set! ,(pname x) ,(pexpr e))))))
  8306. (pexpr e))))
  8307. (define pexpr
  8308. (lambda (ex)
  8309. (unparse
  8310. ex
  8311. (lambda (e pexpr)
  8312. (match e
  8313. (($ type _ ($ check _ exp)) (pexpr exp)))))))
  8314. (define pdef pexpr)
  8315. (define ppat
  8316. (match-lambda
  8317. (($ pconst x _) (pconst x))
  8318. (($ pvar x) (pname x))
  8319. (($ pany) '_)
  8320. (($ pelse) 'else)
  8321. (($ pnot pat) `(not ,(ppat pat)))
  8322. (($ pand pats) `(and ,@(maplr ppat pats)))
  8323. (($ ppred pred)
  8324. (match (pname pred)
  8325. ('false-object? #f)
  8326. ('true-object? #t)
  8327. ('null? '())
  8328. (x `(? ,x))))
  8329. (($ pobj tag args)
  8330. (match (cons (pname tag) args)
  8331. (('box? x) (box (ppat x)))
  8332. (('pair? x y) (cons (ppat x) (ppat y)))
  8333. (('vector? . x) (list->vector (maplr ppat x)))
  8334. ((tg . _) `($ ,(strip-? tg) ,@(maplr ppat args)))))))
  8335. (define strip-?
  8336. (lambda (s)
  8337. (let* ((str (symbol->string s))
  8338. (n (string-length str)))
  8339. (if (or (zero? n)
  8340. (not (char=? #\? (string-ref str (- n 1)))))
  8341. s
  8342. (string->symbol (substring str 0 (- n 1)))))))
  8343. (define pname
  8344. (match-lambda
  8345. ((? name? x) (or (name-name x) '<expr>))
  8346. ((? symbol? x) x)))
  8347. (define ptag
  8348. (match-lambda
  8349. ((? k? k) (k-name k))
  8350. ((? symbol? x) x)))
  8351. (define pconst
  8352. (match-lambda
  8353. ((? symbol? x) `',x)
  8354. ((? boolean? x) x)
  8355. ((? number? x) x)
  8356. ((? char? x) x)
  8357. ((? string? x) x)
  8358. ((? null? x) `',x)))
  8359. (define check
  8360. (lambda (file)
  8361. (output-checked file '() type-check?)))
  8362. (define profcheck
  8363. (lambda (file)
  8364. (output-checked #f '() type-check?)
  8365. (output-checked
  8366. #f
  8367. (make-counters total-possible)
  8368. type-check?)))
  8369. (define fullcheck
  8370. (lambda (file)
  8371. (let ((check? (lambda (_) #t)))
  8372. (output-checked #f '() check?)
  8373. (output-checked
  8374. #f
  8375. (make-counters total-possible)
  8376. check?))))
  8377. (define make-counters
  8378. (lambda (n)
  8379. (let* ((init `(define check-counters (make-vector ,n 0)))
  8380. (sum '(define check-total
  8381. (lambda ()
  8382. (let ((foldr (lambda (f i l)
  8383. (recur loop
  8384. ((l l))
  8385. (match l
  8386. (() i)
  8387. ((x . y) (f x (loop y))))))))
  8388. (foldr + 0 (vector->list check-counters))))))
  8389. (incr '(extend-syntax
  8390. (check-increment-counter)
  8391. ((check-increment-counter c)
  8392. (vector-set!
  8393. check-counters
  8394. c
  8395. (+ 1 (vector-ref check-counters c)))))))
  8396. (list init sum incr))))
  8397. (define output-checked
  8398. (lambda (file header check-test)
  8399. (set! summary '())
  8400. (set! total-possible 0)
  8401. (set! total-cast 0)
  8402. (set! total-err 0)
  8403. (set! total-any 0)
  8404. (let ((doit (lambda ()
  8405. (when (string? file)
  8406. (printf
  8407. ";; Generated by Soft Scheme ~a~%"
  8408. st:version)
  8409. (printf ";; (st:control")
  8410. (for-each
  8411. (lambda (x) (printf " '~a" x))
  8412. (show-controls))
  8413. (printf ")~%")
  8414. (unless
  8415. (= 0 n-unbound)
  8416. (printf
  8417. ";; CAUTION: ~a unbound references, this code is not safe~%"
  8418. n-unbound))
  8419. (printf "~%")
  8420. (for-each pretty-print header))
  8421. (for-each
  8422. (lambda (exp)
  8423. (match exp
  8424. (($ define x _)
  8425. (set! n-possible 0)
  8426. (set! n-clash 0)
  8427. (set! n-err 0)
  8428. (set! n-match 0)
  8429. (set! n-inexhaust 0)
  8430. (set! n-prim 0)
  8431. (set! n-lam 0)
  8432. (set! n-app 0)
  8433. (set! n-field 0)
  8434. (set! n-cast 0)
  8435. (if file
  8436. (pretty-print (pcheck exp check-test))
  8437. (pcheck exp check-test))
  8438. (make-summary-line x)
  8439. (set! total-possible
  8440. (+ total-possible n-possible))
  8441. (set! total-cast (+ total-cast n-cast))
  8442. (set! total-err (+ total-err n-err))
  8443. (set! total-any
  8444. (+ total-any
  8445. n-match
  8446. n-inexhaust
  8447. n-prim
  8448. n-lam
  8449. n-app
  8450. n-field
  8451. n-cast)))
  8452. (_ (when file
  8453. (pretty-print
  8454. (pcheck exp check-test))))))
  8455. tree)
  8456. (when (string? file)
  8457. (newline)
  8458. (newline)
  8459. (print-summary "; ")))))
  8460. (if (string? file)
  8461. (begin
  8462. (delete-file file)
  8463. (with-output-to-file file doit))
  8464. (doit)))))
  8465. (define total-possible 0)
  8466. (define total-err 0)
  8467. (define total-cast 0)
  8468. (define total-any 0)
  8469. (define n-possible 0)
  8470. (define n-clash 0)
  8471. (define n-err 0)
  8472. (define n-match 0)
  8473. (define n-inexhaust 0)
  8474. (define n-prim 0)
  8475. (define n-lam 0)
  8476. (define n-app 0)
  8477. (define n-field 0)
  8478. (define n-cast 0)
  8479. (define summary '())
  8480. (define make-summary-line
  8481. (lambda (x)
  8482. (let ((total (+ n-match
  8483. n-inexhaust
  8484. n-prim
  8485. n-lam
  8486. n-app
  8487. n-field
  8488. n-cast)))
  8489. (unless
  8490. (= 0 total)
  8491. (let* ((s (sprintf
  8492. "~a~a "
  8493. (padr (pname x) 16)
  8494. (padl total 2)))
  8495. (s (cond ((< 0 n-inexhaust)
  8496. (sprintf
  8497. "~a (~a match ~a inexhaust)"
  8498. s
  8499. n-match
  8500. n-inexhaust))
  8501. ((< 0 n-match)
  8502. (sprintf "~a (~a match)" s n-match))
  8503. (else s)))
  8504. (s (if (< 0 n-prim)
  8505. (sprintf "~a (~a prim)" s n-prim)
  8506. s))
  8507. (s (if (< 0 n-field)
  8508. (sprintf "~a (~a field)" s n-field)
  8509. s))
  8510. (s (if (< 0 n-lam)
  8511. (sprintf "~a (~a lambda)" s n-lam)
  8512. s))
  8513. (s (if (< 0 n-app) (sprintf "~a (~a ap)" s n-app) s))
  8514. (s (if (< 0 n-err)
  8515. (sprintf "~a (~a ERROR)" s n-err)
  8516. s))
  8517. (s (if (< 0 n-cast)
  8518. (sprintf "~a (~a TYPE)" s n-cast)
  8519. s)))
  8520. (set! summary (cons s summary)))))))
  8521. (define print-summary
  8522. (lambda (hdr)
  8523. (for-each
  8524. (lambda (s) (printf "~a~a~%" hdr s))
  8525. (reverse summary))
  8526. (printf
  8527. "~a~a~a "
  8528. hdr
  8529. (padr "TOTAL CHECKS" 16)
  8530. (padl total-any 2))
  8531. (printf
  8532. " (of ~s is ~s%)"
  8533. total-possible
  8534. (if (= 0 total-possible)
  8535. 0
  8536. (string->number
  8537. (chop-number
  8538. (exact->inexact
  8539. (* (/ total-any total-possible) 100))
  8540. 4))))
  8541. (when (< 0 total-err)
  8542. (printf " (~s ERROR)" total-err))
  8543. (when (< 0 total-cast)
  8544. (printf " (~s TYPE)" total-cast))
  8545. (printf "~%")))
  8546. (define padl
  8547. (lambda (arg n)
  8548. (let ((s (sprintf "~a" arg)))
  8549. (recur loop
  8550. ((s s))
  8551. (if (< (string-length s) n)
  8552. (loop (string-append " " s))
  8553. s)))))
  8554. (define padr
  8555. (lambda (arg n)
  8556. (let ((s (sprintf "~a" arg)))
  8557. (recur loop
  8558. ((s s))
  8559. (if (< (string-length s) n)
  8560. (loop (string-append s " "))
  8561. s)))))
  8562. (define chop-number
  8563. (lambda (x n)
  8564. (substring
  8565. (sprintf "~s00000000000000000000" x)
  8566. 0
  8567. (- n 1))))
  8568. (define pcheck
  8569. (lambda (ex check-test)
  8570. (unparse
  8571. ex
  8572. (lambda (e pexpr)
  8573. (match e
  8574. ((and z ($ type _ ($ check inf ($ var x))))
  8575. (cond ((name-primitive x)
  8576. (set! n-possible (+ 1 n-possible))
  8577. (match (check-test inf)
  8578. (#f (pname x))
  8579. ('def
  8580. (set! n-err (+ 1 n-err))
  8581. (set! n-prim (+ 1 n-prim))
  8582. `(,(symbol-append "CHECK-" (pname x))
  8583. ,(tree-index z)
  8584. ',(string->symbol "ERROR")))
  8585. (_ (set! n-prim (+ 1 n-prim))
  8586. `(,(symbol-append "CHECK-" (pname x))
  8587. ,(tree-index z)))))
  8588. ((name-unbound? x) `(check-bound ,(pname x)))
  8589. (else
  8590. (if (check-test inf)
  8591. (begin
  8592. (set! n-clash (+ 1 n-clash))
  8593. `(,(string->symbol "CLASH")
  8594. ,(pname x)
  8595. ,(tree-index z)))
  8596. (pname x)))))
  8597. ((and z
  8598. ($ type _ ($ check inf (and m ($ lam x e1)))))
  8599. (set! n-possible (+ 1 n-possible))
  8600. (match (check-test inf)
  8601. (#f (pexpr m))
  8602. ('def
  8603. (set! n-err (+ 1 n-err))
  8604. (set! n-lam (+ 1 n-lam))
  8605. `(,(string->symbol "CHECK-lambda")
  8606. (,(tree-index z) ',(string->symbol "ERROR"))
  8607. ,(map pname x)
  8608. ,@(pexpr e1)))
  8609. (_ (set! n-lam (+ 1 n-lam))
  8610. `(,(string->symbol "CHECK-lambda")
  8611. (,(tree-index z))
  8612. ,(map pname x)
  8613. ,@(pexpr e1)))))
  8614. ((and z
  8615. ($ type
  8616. _
  8617. ($ check inf (and m ($ vlam x rest e1)))))
  8618. (set! n-possible (+ 1 n-possible))
  8619. (match (check-test inf)
  8620. (#f (pexpr m))
  8621. ('def
  8622. (set! n-err (+ 1 n-err))
  8623. (set! n-lam (+ 1 n-lam))
  8624. `(,(string->symbol "CHECK-lambda")
  8625. (,(tree-index z) ',(string->symbol "ERROR"))
  8626. ,(append (map pname x) (pname rest))
  8627. ,@(pexpr e1)))
  8628. (_ (set! n-lam (+ 1 n-lam))
  8629. `(,(string->symbol "CHECK-lambda")
  8630. (,(tree-index z))
  8631. ,(append (map pname x) (pname rest))
  8632. ,@(pexpr e1)))))
  8633. ((and z
  8634. ($ type _ ($ check inf (and m ($ app e1 args)))))
  8635. (set! n-possible (+ 1 n-possible))
  8636. (match (check-test inf)
  8637. (#f (pexpr m))
  8638. ('def
  8639. (set! n-err (+ 1 n-err))
  8640. (set! n-app (+ 1 n-app))
  8641. `(,(string->symbol "CHECK-ap")
  8642. (,(tree-index z) ',(string->symbol "ERROR"))
  8643. ,(pexpr e1)
  8644. ,@(map pexpr args)))
  8645. (_ (set! n-app (+ 1 n-app))
  8646. (let ((p1 (pexpr e1)))
  8647. `(,(string->symbol "CHECK-ap")
  8648. (,(tree-index z))
  8649. ,p1
  8650. ,@(map pexpr args))))))
  8651. ((and z
  8652. ($ type _ ($ check inf (and m ($ field x e1)))))
  8653. (set! n-possible (+ 1 n-possible))
  8654. (match (check-test inf)
  8655. (#f (pexpr m))
  8656. ('def
  8657. (set! n-err (+ 1 n-err))
  8658. (set! n-field (+ 1 n-field))
  8659. `(,(string->symbol "CHECK-field")
  8660. (,(tree-index z) ',(string->symbol "ERROR"))
  8661. ,x
  8662. ,(pexpr e1)))
  8663. (_ (set! n-field (+ 1 n-field))
  8664. `(,(string->symbol "CHECK-field")
  8665. (,(tree-index z))
  8666. ,x
  8667. ,(pexpr e1)))))
  8668. ((and z
  8669. ($ type
  8670. _
  8671. ($ check inf (and m ($ cast (x . _) e1)))))
  8672. (set! n-possible (+ 1 n-possible))
  8673. (match (check-test inf)
  8674. (#f (pexpr m))
  8675. (_ (set! n-cast (+ 1 n-cast))
  8676. `(,(string->symbol "CHECK-:")
  8677. (,(tree-index z))
  8678. ,x
  8679. ,(pexpr e1)))))
  8680. ((and z
  8681. ($ type
  8682. _
  8683. ($ check inf (and m ($ match e1 clauses)))))
  8684. (set! n-possible (+ 1 n-possible))
  8685. (match (check-test inf)
  8686. (#f (pexpr m))
  8687. (inx (let* ((pclause
  8688. (match-lambda
  8689. (($ mclause p exp fail)
  8690. (if fail
  8691. `(,(ppat p)
  8692. (=> ,(pname fail))
  8693. ,@(pexpr exp))
  8694. `(,(ppat p) ,@(pexpr exp))))))
  8695. (p1 (pexpr e1)))
  8696. (if (eq? 'inexhaust inx)
  8697. (begin
  8698. (set! n-inexhaust (+ 1 n-inexhaust))
  8699. `(,(string->symbol "CHECK-match")
  8700. (,(tree-index z)
  8701. ,(string->symbol "INEXHAUST"))
  8702. ,p1
  8703. ,@(maplr pclause clauses)))
  8704. (begin
  8705. (set! n-match (+ 1 n-match))
  8706. `(,(string->symbol "CHECK-match")
  8707. (,(tree-index z))
  8708. ,p1
  8709. ,@(maplr pclause clauses)))))))))))))
  8710. (define tree-index-list '())
  8711. (define reinit-output!
  8712. (lambda () (set! tree-index-list '())))
  8713. (define tree-index
  8714. (lambda (syntax)
  8715. (match (assq syntax tree-index-list)
  8716. (#f
  8717. (let ((n (length tree-index-list)))
  8718. (set! tree-index-list
  8719. (cons (cons syntax n) tree-index-list))
  8720. n))
  8721. ((_ . n) n))))
  8722. (define tree-unindex
  8723. (lambda (n)
  8724. (let ((max (length tree-index-list)))
  8725. (when (<= max n)
  8726. (use-error "Invalid CHECK number ~a" n))
  8727. (car (list-ref tree-index-list (- (- max 1) n))))))
  8728. (define cause
  8729. (lambda ()
  8730. (for-each
  8731. (lambda (def)
  8732. (for-each pretty-print (exp-cause def)))
  8733. tree)))
  8734. (define cause*
  8735. (lambda names
  8736. (if (null? names)
  8737. (for-each
  8738. (lambda (def)
  8739. (for-each pretty-print (exp-cause def)))
  8740. tree)
  8741. (for-each
  8742. (match-lambda
  8743. ((? symbol? dname)
  8744. (for-each
  8745. pretty-print
  8746. (exp-cause (find-global dname)))))
  8747. names))))
  8748. (define exp-cause
  8749. (let ((sum (lambda (exps)
  8750. (foldr (lambda (x y) (append (exp-cause x) y))
  8751. '()
  8752. exps)))
  8753. (src (lambda (inf)
  8754. (let ((nonlocal (map tree-index (check-sources inf))))
  8755. (if (type-check1? inf)
  8756. (cons (check-local-sources inf) nonlocal)
  8757. nonlocal)))))
  8758. (match-lambda
  8759. ((and z ($ type ty ($ check inf ($ var x))))
  8760. (if (name-primitive x)
  8761. (if (type-check? inf)
  8762. (list `((,(symbol-append 'check- (pname x))
  8763. ,(tree-index z))
  8764. ,@(src inf)))
  8765. '())
  8766. (if (type-check1? inf)
  8767. (list `((clash ,(pname x) ,(tree-index z)) ,@(src inf)))
  8768. '())))
  8769. ((and z ($ type ty ($ check inf ($ lam x e1))))
  8770. (append
  8771. (if (type-check? inf)
  8772. (list `((check-lambda ,(tree-index z) ,(map pname x) ...)
  8773. ,@(src inf)))
  8774. '())
  8775. (exp-cause e1)))
  8776. ((and z
  8777. ($ type ty ($ check inf ($ vlam x rest e1))))
  8778. (append
  8779. (if (type-check? inf)
  8780. (list `((check-lambda
  8781. ,(tree-index z)
  8782. ,(append (map pname x) (pname rest))
  8783. ...)
  8784. ,@(src inf)))
  8785. '())
  8786. (exp-cause e1)))
  8787. ((and z ($ type _ ($ check inf ($ app e1 args))))
  8788. (append
  8789. (if (type-check? inf)
  8790. (list `((check-ap ,(tree-index z)) ,@(src inf)))
  8791. '())
  8792. (exp-cause e1)
  8793. (sum args)))
  8794. ((and z ($ type _ ($ check inf ($ field x e1))))
  8795. (append
  8796. (if (type-check? inf)
  8797. (list `((check-field ,(tree-index z) ,x ...)
  8798. ,@(src inf)))
  8799. '())
  8800. (exp-cause e1)))
  8801. ((and z
  8802. ($ type _ ($ check inf ($ cast (x . _) e1))))
  8803. (append
  8804. (if (type-check? inf)
  8805. (list `((check-: ,(tree-index z) ,x ...) ,@(src inf)))
  8806. '())
  8807. (exp-cause e1)))
  8808. ((and z
  8809. ($ type
  8810. _
  8811. ($ check inf (and m ($ match e1 clauses)))))
  8812. (append
  8813. (if (type-check? inf)
  8814. (list `((check-match ,(tree-index z) ...) ,@(src inf)))
  8815. '())
  8816. (exp-cause m)))
  8817. (($ define _ e) (exp-cause e))
  8818. ((? defstruct?) '())
  8819. ((? datatype?) '())
  8820. (($ app e1 args) (sum (cons e1 args)))
  8821. (($ match exp clauses)
  8822. (foldr (lambda (x y)
  8823. (append
  8824. (match x (($ mclause _ e _) (exp-cause e)))
  8825. y))
  8826. (exp-cause exp)
  8827. clauses))
  8828. (($ var _) '())
  8829. (($ and exps) (sum exps))
  8830. (($ begin exps) (sum exps))
  8831. ((? const?) '())
  8832. (($ if test then els)
  8833. (append
  8834. (exp-cause test)
  8835. (exp-cause then)
  8836. (exp-cause els)))
  8837. (($ let bindings body)
  8838. (foldr (lambda (x y)
  8839. (append (match x (($ bind _ e) (exp-cause e))) y))
  8840. (exp-cause body)
  8841. bindings))
  8842. (($ let* bindings body)
  8843. (foldr (lambda (x y)
  8844. (append (match x (($ bind _ e) (exp-cause e))) y))
  8845. (exp-cause body)
  8846. bindings))
  8847. (($ letr bindings body)
  8848. (foldr (lambda (x y)
  8849. (append (match x (($ bind _ e) (exp-cause e))) y))
  8850. (exp-cause body)
  8851. bindings))
  8852. (($ body defs exps) (sum (append defs exps)))
  8853. (($ or exps) (sum exps))
  8854. (($ delay e) (exp-cause e))
  8855. (($ set! var body) (exp-cause body))
  8856. (($ record bindings)
  8857. (foldr (lambda (x y)
  8858. (append (match x (($ bind _ e) (exp-cause e))) y))
  8859. '()
  8860. bindings))
  8861. (($ type _ exp) (exp-cause exp)))))
  8862. (define display-type tidy)
  8863. (define type
  8864. (lambda names
  8865. (if (null? names)
  8866. (for-each globaldef tree)
  8867. (for-each
  8868. (match-lambda
  8869. ((? symbol? x)
  8870. (match (lookup? global-env x)
  8871. (#f (use-error "~a is not defined" x))
  8872. (ty (pretty-print
  8873. `(,x : ,(display-type (name-ty ty)))))))
  8874. ((? number? n)
  8875. (let* ((ty (check-type (tree-unindex n)))
  8876. (type (display-type ty)))
  8877. (pretty-print `(,n : ,type))))
  8878. (_ (use-error
  8879. "arguments must be identifiers or CHECK numbers")))
  8880. names))))
  8881. (define localtype
  8882. (lambda names
  8883. (if (null? names)
  8884. (for-each localdef tree)
  8885. (for-each
  8886. (lambda (x) (localdef (find-global x)))
  8887. names))))
  8888. (define find-global
  8889. (lambda (name)
  8890. (let ((d (ormap (match-lambda
  8891. ((and d ($ define x _))
  8892. (and (eq? name (name-name x)) d))
  8893. (_ #f))
  8894. tree)))
  8895. (unless d (use-error "~a is not defined" name))
  8896. d)))
  8897. (define globaldef
  8898. (lambda (e)
  8899. (match e
  8900. (($ define x _)
  8901. (let ((type (display-type (name-ty x))))
  8902. (pretty-print `(,(pname x) : ,type))))
  8903. (_ #f))))
  8904. (define localdef
  8905. (lambda (e) (pretty-print (expdef e))))
  8906. (define expdef
  8907. (let* ((show (lambda (x)
  8908. `(,(pname x) : ,(display-type (name-ty x)))))
  8909. (pbind (match-lambda
  8910. (($ bind x e) `(,(show x) ,(expdef e))))))
  8911. (match-lambda
  8912. (($ define x e)
  8913. (if (or (not x) (and (name? x) (not (name-name x))))
  8914. (expdef e)
  8915. `(define ,(show x) ,(expdef e))))
  8916. ((? defstruct? d) (pdef d))
  8917. ((? datatype? d) (pdef d))
  8918. (($ and exps) `(and ,@(maplr expdef exps)))
  8919. (($ app fun args)
  8920. `(,(expdef fun) ,@(maplr expdef args)))
  8921. (($ begin exps) `(begin ,@(maplr expdef exps)))
  8922. (($ const c _) (pconst c))
  8923. (($ if test then els)
  8924. `(if ,(expdef test) ,(expdef then) ,(expdef els)))
  8925. (($ lam params body)
  8926. `(lambda ,(map show params) ,@(expdef body)))
  8927. (($ vlam params rest body)
  8928. `(lambda ,(append (map show params) (show rest))
  8929. ,@(expdef body)))
  8930. (($ let bindings body)
  8931. `(let ,(map pbind bindings) ,@(expdef body)))
  8932. (($ let* bindings body)
  8933. `(let* ,(map pbind bindings) ,@(expdef body)))
  8934. (($ letr bindings body)
  8935. `(letrec ,(map pbind bindings) ,@(expdef body)))
  8936. (($ body defs exps)
  8937. (let ((pdefs (maplr expdef defs)))
  8938. (append pdefs (maplr expdef exps))))
  8939. (($ record bindings)
  8940. `(record ,@(maplr pbind bindings)))
  8941. (($ field x e) `(field ,x ,(expdef e)))
  8942. (($ cast (ty . _) e) `(: ,ty ,(expdef e)))
  8943. (($ or exps) `(or ,@(maplr expdef exps)))
  8944. (($ delay e) `(delay ,(expdef e)))
  8945. (($ set! x body)
  8946. `(set! ,(pname x) ,(expdef body)))
  8947. (($ var x) (pname x))
  8948. (($ match e1 clauses)
  8949. (let* ((pclause
  8950. (match-lambda
  8951. (($ mclause p exp fail)
  8952. (if fail
  8953. `(,(expdef p) (=> ,(pname fail)) ,@(expdef exp))
  8954. `(,(expdef p) ,@(expdef exp))))))
  8955. (p1 (expdef e1)))
  8956. `(match ,p1 ,@(maplr pclause clauses))))
  8957. (($ pconst x _) (pconst x))
  8958. (($ pvar x) (show x))
  8959. (($ pany) '_)
  8960. (($ pelse) 'else)
  8961. (($ pnot pat) `(not ,(expdef pat)))
  8962. (($ pand pats) `(and ,@(maplr expdef pats)))
  8963. (($ ppred pred)
  8964. (match (pname pred)
  8965. ('false-object? #f)
  8966. ('true-object? #t)
  8967. ('null? '())
  8968. (x `(? ,x))))
  8969. (($ pobj tag args)
  8970. (match (cons (pname tag) args)
  8971. (('pair? x y) (cons (expdef x) (expdef y)))
  8972. (('box? x) (box (expdef x)))
  8973. (('vector? . x) (list->vector (maplr expdef x)))
  8974. ((tg . _)
  8975. `($ ,(strip-? tg) ,@(maplr expdef args)))))
  8976. (($ type _ exp) (expdef exp))
  8977. (($ check _ exp) (expdef exp)))))
  8978. (define check-type
  8979. (match-lambda
  8980. (($ type ty ($ check inf ($ var x))) ty)
  8981. (($ type ty ($ check inf ($ lam x e1))) ty)
  8982. (($ type ty ($ check inf ($ vlam x rest e1))) ty)
  8983. (($ type _ ($ check inf ($ app e1 args)))
  8984. (typeof e1))
  8985. (($ type _ ($ check inf ($ field x e1)))
  8986. (typeof e1))
  8987. (($ type _ ($ check inf ($ cast (x . _) e1)))
  8988. (typeof e1))
  8989. (($ type _ ($ check inf ($ match e1 clauses)))
  8990. (typeof e1))))
  8991. (define tree '())
  8992. (define global-env empty-env)
  8993. (define verbose #f)
  8994. (define times #t)
  8995. (define benchmarking #f)
  8996. (define cons-mutators '(set-car! set-cdr!))
  8997. (define st:check
  8998. (lambda args
  8999. (parameterize
  9000. ((print-level #f)
  9001. (print-length #f)
  9002. (pretty-maximum-lines #f))
  9003. (let ((output (apply do-soft args)))
  9004. (when output
  9005. (printf
  9006. "Typed program written to file ~a~%"
  9007. output))))))
  9008. (define st:run
  9009. (lambda (file)
  9010. (parameterize
  9011. ((optimize-level 3))
  9012. (when benchmarking
  9013. (printf "Reloading slow CHECKs...~%")
  9014. (load (string-append
  9015. installation-directory
  9016. "checklib.scm"))
  9017. (set! benchmarking #f))
  9018. (load file))))
  9019. (define st:bench
  9020. (lambda (file)
  9021. (parameterize
  9022. ((optimize-level 3))
  9023. (unless
  9024. benchmarking
  9025. (unless
  9026. fastlibrary-file
  9027. (use-error
  9028. "No benchmarking mode in this version"))
  9029. (printf "Reloading fast CHECKs...~%")
  9030. (load (string-append
  9031. installation-directory
  9032. fastlibrary-file))
  9033. (set! benchmarking #t))
  9034. (load file))))
  9035. (define st:
  9036. (lambda args
  9037. (parameterize
  9038. ((print-level #f)
  9039. (print-length #f)
  9040. (pretty-maximum-lines #f))
  9041. (let ((output (apply do-soft args)))
  9042. (cond ((not output)
  9043. (use-error "Output file name required to run"))
  9044. ((= 0 n-unbound)
  9045. (printf
  9046. "Typed program written to file ~a, executing ...~%"
  9047. output)
  9048. (flush-output)
  9049. (st:run output))
  9050. (else
  9051. (printf
  9052. "Typed program written to file ~a, not executing (unbound refs)~%"
  9053. output)))))))
  9054. (define do-soft
  9055. (match-lambda*
  9056. ((input (? string? output))
  9057. (when (strip-suffix output)
  9058. (use-error
  9059. "output file name cannot end in .ss or .scm"))
  9060. (cond ((string? input)
  9061. (soft-files (list input) output)
  9062. output)
  9063. ((and (list? input) (andmap string? input))
  9064. (soft-files input output)
  9065. output)
  9066. (else (soft-def input output) output)))
  9067. ((input #f)
  9068. (cond ((string? input) (soft-files (list input) #f) #f)
  9069. ((and (list? input) (andmap string? input))
  9070. (soft-files input #f)
  9071. #f)
  9072. (else (soft-def input #f) #f)))
  9073. ((input)
  9074. (cond ((string? input)
  9075. (let ((o (string-append
  9076. (or (strip-suffix input) input)
  9077. ".soft")))
  9078. (soft-files (list input) o)
  9079. o))
  9080. ((and (list? input) (andmap string? input))
  9081. (use-error "Output file name required"))
  9082. (else (soft-def input #t) #f)))
  9083. (else (use-error
  9084. "Input must be a file name or list of file names"))))
  9085. (define rawmode #f)
  9086. (define st:control
  9087. (lambda args
  9088. (let ((dbg (match-lambda
  9089. ('raw
  9090. (set! display-type ptype)
  9091. (set! rawmode #t))
  9092. ('!raw
  9093. (set! display-type tidy)
  9094. (set! rawmode #f))
  9095. ('verbose (set! verbose #t))
  9096. ('!verbose (set! verbose #f))
  9097. ('times (set! times #t))
  9098. ('!times (set! times #f))
  9099. ('partial (set! fullsharing #f))
  9100. ('!partial (set! fullsharing #t))
  9101. ('pseudo (set! pseudo pseudo-subtype))
  9102. ('!pseudo (set! pseudo #f))
  9103. ('populated (set! populated #t))
  9104. ('!populated (set! populated #f))
  9105. ('matchst (set! matchst #t))
  9106. ('!matchst (set! matchst #f))
  9107. ('genmatch (set! genmatch #t))
  9108. ('!genmatch (set! genmatch #f))
  9109. ('letonce (set! letonce #t))
  9110. ('!letonce (set! letonce #f))
  9111. ('global-error (set! global-error #t))
  9112. ('!global-error (set! global-error #f))
  9113. ('share (set! share #t))
  9114. ('!share (set! share #f))
  9115. ('flags (set! flags #t))
  9116. ('!flags (set! flags #f))
  9117. ('depths (set! dump-depths #t))
  9118. ('!depths (set! dump-depths #f))
  9119. ('match (set! keep-match #t))
  9120. ('!match (set! keep-match #f))
  9121. (x (printf "Error: unknown debug switch ~a~%" x)
  9122. (st:control)))))
  9123. (if (null? args)
  9124. (begin
  9125. (printf "Current values:")
  9126. (for-each
  9127. (lambda (x) (printf " ~a" x))
  9128. (show-controls))
  9129. (printf "~%"))
  9130. (for-each dbg args)))))
  9131. (define show-controls
  9132. (lambda ()
  9133. (list (if rawmode 'raw '!raw)
  9134. (if verbose 'verbose '!verbose)
  9135. (if times 'times '!times)
  9136. (if share 'share '!share)
  9137. (if flags 'flags '!flags)
  9138. (if dump-depths 'depths '!depths)
  9139. (if fullsharing '!partial 'partial)
  9140. (if pseudo 'pseudo '!pseudo)
  9141. (if populated 'populated '!populated)
  9142. (if letonce 'letonce '!letonce)
  9143. (if matchst 'matchst '!matchst)
  9144. (if genmatch 'genmatch '!genmatch)
  9145. (if global-error 'global-error '!global-error)
  9146. (if keep-match 'match '!match))))
  9147. (define soft-def
  9148. (lambda (exp output)
  9149. (reinit-macros!)
  9150. (reinit-types!)
  9151. (reinit-output!)
  9152. (set! visible-time 0)
  9153. (match-let*
  9154. ((before-parse (cpu-time))
  9155. (defs (parse-def exp))
  9156. (before-bind (cpu-time))
  9157. ((defs env tenv unbound)
  9158. (bind-defs
  9159. defs
  9160. initial-env
  9161. initial-type-env
  9162. '()
  9163. 0))
  9164. (_ (warn-unbound unbound))
  9165. (_ (if cons-is-mutable
  9166. (printf
  9167. "Note: use of ~a, treating cons as MUTABLE~%"
  9168. cons-mutators)
  9169. (printf
  9170. "Note: no use of ~a, treating cons as immutable~%"
  9171. cons-mutators)))
  9172. (before-improve (cpu-time))
  9173. (defs (improve-defs defs))
  9174. (before-typecheck (cpu-time))
  9175. (_ (type-check defs))
  9176. (_ (set! global-env env))
  9177. (before-output (cpu-time))
  9178. (_ (check output))
  9179. (_ (print-summary ""))
  9180. (before-end (cpu-time)))
  9181. (when times
  9182. (printf
  9183. "~a seconds parsing,~%"
  9184. (exact->inexact
  9185. (* (- before-bind before-parse)
  9186. clock-granularity)))
  9187. (printf
  9188. "~a seconds binding,~%"
  9189. (exact->inexact
  9190. (* (- before-improve before-bind)
  9191. clock-granularity)))
  9192. (printf
  9193. "~a seconds improving,~%"
  9194. (exact->inexact
  9195. (* (- before-typecheck before-improve)
  9196. clock-granularity)))
  9197. (printf
  9198. "~a seconds type checking,~%"
  9199. (exact->inexact
  9200. (* (- (- before-output before-typecheck)
  9201. visible-time)
  9202. clock-granularity)))
  9203. (printf
  9204. "~a seconds setting visibility,~%"
  9205. (exact->inexact
  9206. (* visible-time clock-granularity)))
  9207. (printf
  9208. "~a seconds writing output,~%"
  9209. (exact->inexact
  9210. (* (- before-end before-output)
  9211. clock-granularity)))
  9212. (printf
  9213. "~a seconds in total.~%"
  9214. (exact->inexact
  9215. (* (- before-end before-parse) clock-granularity)))))))
  9216. (define type-check
  9217. (lambda (defs)
  9218. (set! tree defs)
  9219. (type-defs defs)
  9220. defs))
  9221. (define soft-files
  9222. (lambda (files output)
  9223. (let ((contents
  9224. (map (lambda (f) `(begin ,@(readfile f))) files)))
  9225. (soft-def `(begin ,@contents) output))))
  9226. (define strip-suffix
  9227. (lambda (name)
  9228. (let ((n (string-length name)))
  9229. (or (and (<= 3 n)
  9230. (equal? ".ss" (substring name (- n 3) n))
  9231. (substring name 0 (- n 3)))
  9232. (and (<= 4 n)
  9233. (equal? ".scm" (substring name (- n 4) n))
  9234. (substring name 0 (- n 4)))))))
  9235. (define st:deftype
  9236. (match-lambda*
  9237. (((? symbol? x) ? list? mutability)
  9238. (=> fail)
  9239. (if (andmap boolean? mutability)
  9240. (deftype x mutability)
  9241. (fail)))
  9242. (args (use-error
  9243. "Invalid command ~a"
  9244. `(st:deftype ,@args)))))
  9245. (define st:defprim
  9246. (match-lambda*
  9247. (((? symbol? x) type) (defprim x type 'impure))
  9248. (((? symbol? x) type (? symbol? mode))
  9249. (defprim x type mode))
  9250. (args (use-error
  9251. "Invalid command ~a"
  9252. `(st:defprim ,@args)))))
  9253. (define st:help
  9254. (lambda ()
  9255. (printf
  9256. "Commands for Soft Scheme (~a)~%"
  9257. st:version)
  9258. (printf
  9259. " (st: file (output)) type check file and execute~%")
  9260. (printf
  9261. " (st:type (name)) print types of global defs~%")
  9262. (printf
  9263. " (st:check file (output)) type check file~%")
  9264. (printf
  9265. " (st:run file) execute type checked file~%")
  9266. (printf
  9267. " (st:bench file) execute type checked file fast~%")
  9268. (printf
  9269. " (st:ltype (name)) print types of local defs~%")
  9270. (printf
  9271. " (st:cause) print cause of CHECKs~%")
  9272. (printf
  9273. " (st:summary) print summary of CHECKs~%")
  9274. (printf
  9275. " (st:help) prints this message~%")
  9276. (printf
  9277. " (st:defprim name type (mode)) define a new primitive~%")
  9278. (printf
  9279. " (st:deftype name bool ...) define a new type constructor~%")
  9280. (printf
  9281. " (st:control flag ...) set internal flags~%")
  9282. (printf
  9283. "For more info, see ftp://ftp.nj.nec.com/pub/wright/ssmanual/softscheme.html~%")
  9284. (printf
  9285. "Copyright (c) 1993, 1994, 1995 by Andrew K. Wright under the~%")
  9286. (printf
  9287. "terms of the Gnu Public License. No warranties of any kind apply.~%")))
  9288. (define st:type type)
  9289. (define st:ltype localtype)
  9290. (define st:cause cause)
  9291. (define st:summary
  9292. (lambda () (print-summary "")))
  9293. (define init!
  9294. (lambda ()
  9295. (when customization-file
  9296. (load (string-append
  9297. installation-directory
  9298. customization-file)))
  9299. (let ((softrc
  9300. (string-append home-directory "/.softschemerc")))
  9301. (when (file-exists? softrc) (load softrc)))
  9302. (set! global-env initial-env)
  9303. (st:help)))
  9304. (init!)