12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679 |
- \input texinfo.tex @c -*-texinfo-*-
- @c %**start of header
- @setfilename kawa.info
- @documentencoding UTF-8
- @settitle The Kawa Scheme language
- @setchapternewpage off
- @syncodeindex fn cp
- @syncodeindex vr cp
- @syncodeindex pg cp
- @codequoteundirected on
- @codequotebacktick on
- @c version: %W% %G%
- @c %**end of header
- @macro false{}
- @code{#f}
- @end macro
- @macro true{}
- @code{#t}
- @end macro
- @macro func{NAME}
- @code{\NAME\}
- @end macro
- @macro stxdef{NAME}
- @findex @i{\NAME\}
- @anchor{meta-\NAME\}@var{\NAME\} @t{::=}
- @end macro
- @ifnotinfo
- @ifnottex
- @macro stxref{NAME}
- @ref{meta-\NAME\, @var{\NAME\}, @var{\NAME\}}
- @end macro
- @end ifnottex
- @end ifnotinfo
- @ifinfo
- @macro stxref{NAME}
- @var{\NAME\}
- @end macro
- @end ifinfo
- @iftex
- @macro stxref{NAME}
- @var{\NAME\}
- @end macro
- @end iftex
- @macro stxlit{TEXT}
- @code{@b{\TEXT\}}
- @end macro
- @macro stxlitlbrace{TEXT}
- @code{@b{@{}}
- @end macro
- @macro arbno{THING}
- \THING\@sup{*}
- @end macro
- @c maybe use \x2217
- @c \THING\ @var{...}
- @macro atleastone{THING}
- \THING\@sup{+}
- @end macro
- @macro meta{THING}
- @var{\THING\}
- @end macro
- @macro PerformanceNote
- @emph{Performance note:}
- @end macro
- @macro CompatibilityNote
- @emph{Compatibility:}
- @end macro
- @ifinfo
- @macro vari{THING}
- @var{\THING\1}
- @end macro
- @macro varii{THING}
- @var{\THING\2}
- @end macro
- @macro variii{THING}
- @var{\THING\3}
- @end macro
- @macro variv{THING}
- @var{\THING\4}
- @end macro
- @end ifinfo
- @ifnotinfo
- @macro vari{THING}
- @var{\THING\}@sub{1}
- @end macro
- @macro varii{THING}
- @var{\THING\}@sub{2}
- @end macro
- @macro variii{THING}
- @var{\THING\}@sub{3}
- @end macro
- @macro variv{THING}
- @var{\THING\}@sub{4}
- @end macro
- @end ifnotinfo
- @include version.texi
- @iftex
- @finalout
- @end iftex
- @titlepage
- @title The Kawa Scheme language
- @subtitle @value{UPDATED}
- @sp 1
- @author Per Bothner
- @page
- @end titlepage
- @contents
- @ifinfo
- @format
- START-INFO-DIR-ENTRY
- * kawa: (kawa). The Kawa Scheme language
- END-INFO-DIR-ENTRY
- @end format
- @end ifinfo
- @ifnottex
- @node Top, Installation, (dir), (dir)
- @top The Kawa Scheme language
- @end ifnottex
- @c Kawa is best known as an implementation of the
- @c @uref{http://www.schemers.org/,Scheme language} for the Java platform.
- @c It compiles Scheme to high-performance Java bytecodes.
- @c It is also a general framework for implementing dynamic languages,
- @c and includes a full implementation of XQuery and the beginnings of
- @c implementations of Common Lisp and Emacs Lisp (JEmacs).
- Kawa is a general-purpose programming language that runs on the Java platform.
- It aims to combine:
- @itemize
- @item
- the benefits of dynamic scripting languages
- (non-verbose code with less boiler-plate, fast and easy start-up,
- a @uref{http://en.wikipedia.org/wiki/Read-eval-print_loop,REPL},
- no required compilation step); with
- @item
- the benefits of traditional compiled languages (fast execution, static error detection,
- modularity, zero-overhead Java platform integration).
- @end itemize
- It is an extension of the long-established @uref{http://www.schemers.org/,Scheme}
- language, which is in the Lisp family of programming languages.
- Kawa has many @ref{Features,useful features}.
- Kawa is also a useful @ref{Framework,framework} for implementing
- other programming languages on the Java platform.
- It has many useful utility classes.
- This manual describes version @value{VERSION}, updated @value{UPDATED}.
- See the summary of @ref{News,recent changes}.
- @c Old versions of makeinfo don't support: @ifnotdocbook
- The Kawa home page (which is currently just an on-line
- version of this document) is @uref{http://www.gnu.org/software/kawa/}.
- @c (blank line needed above) @end ifnotdocbook
- The @ref{Tutorial,Kawa tutorial} is useful to get stated.
- While it is woefully incomplete, it does link to some other more in-depth
- (but not Kawa-specific) Scheme tutorials.
- For copyright information on the software and documentation,
- see @ref{License}.
- Various people and orgnizations @ref{Acknowledgements,have contributed to Kawa}.
- This package has nothing to do with the defunct Kawa commercial Java IDE.
- @menu
- * News:: News - Recent Changes
- * Features::
- * Community::
- * Installation:: Building and installing Kawa
- * Tutorial:: Kawa Scheme Tutorial
- * Running:: Invoking, Running, and Using Kawa
- * Syntax::
- * Program structure::
- * Control features::
- * Symbols and namespaces::
- * Procedures::
- * Numbers:: Quantities and Numbers
- * Characters and text::
- * Data structures::
- * Eval and Environments::
- * Debugging::
- * Input-Output:: Input, output, and file handling
- * Types::
- * Objects Classes and Modules::
- * XML tools:: XML, HTML, and the web
- * Miscellaneous::
- * FAQs:: Frequently Asked Questions
- * Framework:: The Kawa language framework
- * License::
- * Overall Index:: Index of functions, macros, concepts, and more.
- @end menu
- @node News
- @chapter News - Recent Changes
- @include news.texi
- @node Features
- @chapter Features
- Runs on the Java platform, with no native code needed.
- Extends the @uref{http://en.wikipedia.org/wiki/Scheme_%28programming_language%29,Scheme language}, following the @uref{http://r7rs.org/,R7RS} specification from 2013.
- Scheme has many implementations, and is much used in research and teaching.
- Programs @uref{http://per.bothner.com/blog/2010/Kawa-in-shootout/, run fast}
- - roughly as fast as Java programs,
- and much faster than other ``scripting languages''.
- This is due to a sophisticated compiler,
- compile-time transformations, type inference, and optional type declarations.
- Full, convenient, and efficient access to the huge set of Java libraries
- means you can access objects, methods, fields, and classes without run-time overhead.
- Start-up times are fast. You don't have to wait for a lot of
- initialization. Even if you start with source code, the parser
- and compiler are fast.
- @ref{Scripts} are simple Kawa source files
- that can run as an application or command. These are simple to write,
- start, and run efficiently, since they're automatically
- compiled before execution.
- Alternatively, you can embed Kawa as a @ref{Evaluating Scheme expressions from Java,
- scripting language for Java applications}.
- Deployment is easy and flexible. You just need the Kawa jar file.
- @ref{Macros} and @ref{Named quasi-literals,custom named literals} make it easy
- to extend the syntax and implement Domain-Specific Languages.
- Kawa provides the usual @ref{REPL Console,read-eval-print loop}, as well as batch modes.
- Kawa has builtin @ref{Pretty-printing,pretty-printer} support, and fancy formatting.
- Kawa supports class-definition facilities, and separately-compiled modules.
- You can @ref{Allocating objects,allocate and initialize objects}
- with a compact ``builder'' syntax. It works out-of-the-box
- (with no run-time overhead) on many classes and APIs,
- but can be customized if need be.
- A library for functional @ref{Composable pictures,composable pictures}
- lets you create ``picture'' objects,
- display them, transform them, combine them, convert to
- SVG or images, and more.
- This can be ``printed'' directly in the Kawa console
- (either the DomTerm console or the Swing one).
- @ref{Building JavaFX applications,JavaFX programming} is simpler.
- You can @ref{Building for Android,run Kawa programs on Android},
- and there is special handling to make @ref{Android view construction,constructing View objects} easier.
- Flexible shell-like functionality, including @ref{process literals}.
- @ref{Server-side scripts,Web page scripts} are easy to write and install
- with @ref{Self-configuring page scripts,self-configuring web servers},
- optionally using @ref{Servlets,servlets} and @ref{XML literals}.
- @ref{Arrays} and sequences have a lot of flexibility:
- Arrays can be multi-dimensional;
- you can use an array as an index (which generalizes slices and permutations);
- you can define a lazy array using a function that maps indexes to values;
- you can re-map the indexes to yield a transformed array.
- Many useful features for mathematics and numerics:
- @itemize
- @item
- The full ``numeric tower'' includes infinite-precision
- rational numbers and complex numbers.
- @item
- Compile-time optimization of arithmetic
- with the use of type declarations and inference.
- @item
- A @ref{Quantities,@dfn{quantity}} is a real number with a unit,
- such as @code{3cm}.
- @item
- @ref{Quaternions} are a 4-dimensional generalization of complex numbers.
- Unsigned primitive integer types (@code{ubyte}, @code{ushort},
- @code{uint}, @code{ulong}) are implemented efficiently without
- object allocation.
- @end itemize
- A @ref{Lazy evaluation,lazy value} wraps an expression which is evaluated
- only when it is needed.
- Kawa provides a @ref{Framework,framework} for implementing other programming languages,
- and comes with incomplete support for CommonLisp, Emacs Lisp, and
- EcmaScript, and
- @uref{http://www.gnu.org/software/qexo/,XQuery}.
- @menu
- * Implemented SRFIs::
- * Compatibility:: Compatibility with standards
- @end menu
- @node Implemented SRFIs
- @section Implemented SRFIs
- Kawa implements the following semi-standard SRFIs
- (@uref{http://srfi.schemers.org/,Scheme Request for Implementation}):
- @itemize
- @item
- @uref{http://srfi.schemers.org/srfi-0/srfi-0.html, SRFI 0}: Feature-based conditional expansion construct,
- using @code{cond-expand} - @pxref{Syntax and conditional compilation}.
- @item
- @uref{http://srfi.schemers.org/srfi-1/srfi-1.html, SRFI 1}: List Library, if @code{(require 'list-lib)} - @pxref{SRFI-1}.
- @item
- @uref{http://srfi.schemers.org/srfi-2/srfi-2.html, SRFI 2}: AND-LET*: an AND with local bindings, a guarded LET* special form.
- @item
- @uref{http://srfi.schemers.org/srfi-4/srfi-4.html, SRFI 4}: Homogeneous numeric vector datatypes - @pxref{Uniform vectors}.
- @item
- @uref{http://srfi.schemers.org/srfi-6/srfi-6.html, SRFI 6}: Basic String Ports - @pxref{Ports}.
- @item
- @uref{http://srfi.schemers.org/srfi-8/srfi-8.html, SRFI 8}: @code{receive}: Binding to multiple values - @pxref{Multiple values}.
- @item
- @uref{http://srfi.schemers.org/srfi-9/srfi-9.html, SRFI 9}: Defining Record Types, using @code{define-record-type}
- - @pxref{Record types}.
- @item
- @uref{http://srfi.schemers.org/srfi-10/srfi-10.html, SRFI 10}: @code{#,} external form for special named types.
- This is deprecated for various reasons, including that it conflicts
- with syntax-case @code{unsyntax}.
- Better to use srfi-108 @ref{Named quasi-literals}.
- @item
- @uref{http://srfi.schemers.org/srfi-11/srfi-11.html, SRFI 11}: Syntax for receiving multiple values,
- using @code{let-values} and @code{let*-value} - @pxref{Multiple values}.
- @item
- @uref{http://srfi.schemers.org/srfi-13/srfi-13.html, SRFI 13}: String Library.
- Needs some polishing.
- @item
- @uref{http://srfi.schemers.org/srfi-14/srfi-14.html, SRFI 14}: Character-set Library - @pxref{Character sets}.
- @item
- @uref{http://srfi.schemers.org/srfi-16/srfi-16.html, SRFI 16}: Syntax for procedures of variable arity,
- using @uref{http://srfi.schemers.org/srfi-16/srfi-16.html, @code{case-lambda}}.
- @item
- @uref{http://srfi.schemers.org/srfi-17/srfi-17.html, SRFI 17}: Generalized @code{set!} - @pxref{Locations}.
- @item
- @uref{http://srfi.schemers.org/srfi-23/srfi-23.html, SRFI 23}: Error reporting mechanism, using @code{error} - @pxref{Exceptions}.
- @item
- @uref{http://srfi.schemers.org/srfi-25/srfi-25.html, SRFI 25}: Multi-dimensional Array Primitives - @pxref{Arrays}.
- @item
- @uref{http://srfi.schemers.org/srfi-26/srfi-26.html, SRFI 26}: Notation for Specializing Parameters without Currying - @pxref{Procedures}.
- @item
- @uref{http://srfi.schemers.org/srfi-28/srfi-28.html, SRFI 28}: Basic Format Strings - @pxref{Format}.
- @item
- @uref{http://srfi.schemers.org/srfi-30/srfi-30.html, SRFI 30}: Nested Multi-line Comments.
- @item
- @uref{http://srfi.schemers.org/srfi-35/srfi-35.html, SRFI 35}: Conditions.
- @item
- @uref{http://srfi.schemers.org/srfi-37/srfi-37.html, SRFI 37}: @uref{http://srfi.schemers.org/srfi-37/srfi-37.html,@code{args-fold} - a program argument processor}, if @code{(require 'args-fold)}.
- @item
- @uref{http://srfi.schemers.org/srfi-38/srfi-38.html, SRFI 38}: External Representation for Data With Shared Structure.
- The @code{read-with-shared-structure} is missing, but subsumed by @code{read}.
- @item
- @uref{http://srfi.schemers.org/srfi-39/srfi-39.html, SRFI 39}:
- @xref{Parameter objects}.
- @item
- @uref{http://srfi.schemers.org/srfi-41/srfi-41.html, SRFI 41}: Streams - @pxref{Streams}.
- @item
- @uref{http://srfi.schemers.org/srfi-45/srfi-45.html, SRFI 45}: Primitives for Expressing Iterative Lazy Algorithms - @pxref{Lazy evaluation}.
- @item
- @uref{http://srfi.schemers.org/srfi-60/srfi-60.html, SRFI 60}: Integers as Bits. - @pxref{Logical Number Operations}.
- @item
- @uref{http://srfi.schemers.org/srfi-62/srfi-62.html, SRFI 62}: S-expression comments.
- @item
- @uref{http://srfi.schemers.org/srfi-64/srfi-64.html, SRFI 64}: A Scheme API for test suites.
- @item
- @uref{http://srfi.schemers.org/srfi-69/srfi-69.html, SRFI 69}: Basic hash tables - @pxref{Hash tables}.
- @item
- @uref{http://srfi.schemers.org/srfi-87/srfi-87.html, SRFI 87}: @code{=>} in @code{case} clauses.
- @item
- @uref{http://srfi.schemers.org/srfi-88/srfi-88.html, SRFI 88}: Keyword objects - @pxref{Keywords}.
- @item
- @uref{http://srfi.schemers.org/srfi-95/srfi-95.html, SRFI 95}: Sorting and Merging.
- @item
- @uref{http://srfi.schemers.org/srfi-97/srfi-97.html, SRFI 97}: Names for SRFI Libraries.
- @item
- @uref{http://srfi.schemers.org/srfi-98/srfi-98.html, SRFI 98}: An interface to access environment variables
- @item
- @uref{http://srfi.schemers.org/srfi-101/srfi-101.html, SRFI 101}: Purely Functional Random-Access Pairs and Lists - @pxref{SRFI-101}.
- @item
- @uref{http://srfi.schemers.org/srfi-107/,SRFI 107}: XML reader syntax - @pxref{XML literals}.
- @item
- @uref{http://srfi.schemers.org/srfi-108/,SRFI 108}: Named quasi-literal constructors - @pxref{Named quasi-literals}.
- @item
- @uref{http://srfi.schemers.org/srfi-109/srfi-109.html, SRFI-109}: Extended string quasi-literals - @pxref{string quasi-literals}.
- @item
- @uref{http://srfi.schemers.org/srfi-118/srfi-118.html, SRFI-118}: Simple adjustable-size strings (@code{string-append!} and @code{string-replace!}).
- @end itemize
- @node Compatibility
- @section Compatibility with standards
- Kawa implements all the required and optional features of R7RS,
- with the following exceptions.
- The entire ``numeric tower" is implemented.
- However, some transcendental functions only work on reals.
- Integral functions do not necessarily work on
- inexact (floating-point) integers.
- (The whole idea of ``inexact integer" in R5RS seems rather pointless ...)
- Also, @code{call-with-current-continuation} is only ``upwards" (?).
- I.e. once a continuation has been exited, it cannot be invoked.
- These restricted continuations can be used to implement catch/throw
- (such as the examples in R4RS), but not co-routines or backtracking.
- Kawa now does general tail-call elimination, but only if
- you use the flag @code{--full-tailcalls}. (Currently, the
- @code{eval} function itself is not fully tail-recursive, in violation
- of R5RS.) The @code{--full-tailcalls} flag is not on by default,
- partly because it is noticably slower (though I have not measured how
- much), and partly I think it is more useful for Kawa to be compatible
- with standard Java calling conventions and tools.
- Code compiled with @code{--full-tailcalls} can call code
- compiled without it and vice versa.
- Even without @code{--full-tailcalls}, if the
- compiler can prove that the procedure being called is the current
- function, then the tail call will be replaced by a jump.
- This includes must ``obvious'' cases of calls to the
- current function named using @code{define} or @code{letrec},
- and many cases of mutual tail-recursion (including
- state-machines using @code{letrec}).
- By default, symbols are case sensitive.
- Kawa implements most of the features of the expression language of DSSSL,
- the Scheme-derived ISO-standard Document Style Semantics and Specification
- Language for SGML. Of the core expression language, the only features
- missing are character properties, @code{external-procedure},
- the time-relationed procedures, and character name escapes in
- string literals.
- From the full expression language, Kawa additionally is missing
- @code{format-number}, @code{format-number-list}, and language objects.
- Quantities, keyword values, and the expanded @code{lambda} form
- (with optional and keyword parameters) are supported.
- @node Community
- @chapter The Kawa Community
- @menu
- * Reporting bugs:: Where to report bugs
- * Mailing lists:: Where to discuss changes, etc
- * Acknowledgements:: Acknowledgements and thanks
- * Support:: Technical support for Kawa
- * Projects:: Projects using Kawa
- * Ideas and tasks:: Ideas and tasks for contributing to Kawa
- @end menu
- @node Reporting bugs
- @section Reporting bugs
- To report a bug or a feature request
- use the @uref{https://gitlab.com/kashell/Kawa/issues,Issue Tracker}.
- This does require a @uref{https://gitlab.com/,GitLab} account;
- if this is a problem you can use the Savannah bug tracker.
- @subsubheading Older Savannah bug tracker
- The older bug tracker for Kawa on Savannah is still available,
- but we request you use
- the @uref{https://gitlab.com/kashell/Kawa/issues,GitLab Issue Tracker}
- for new issues.
- To report a bug or feature request for Kawa (including Qexo or JEmacs) through Savannah,
- use the
- @uref{http://savannah.gnu.org/bugs/?func=additem&group=kawa,bug-submission page}.
- You can browse and comment on existing bug reports
- using the @uref{http://savannah.gnu.org/bugs/?group=kawa, Kawa Bugzilla page}.
- When a bug report is created or modified, mail is automatically sent to the
- @email{bug-kawa@@gnu.org} list. You can subscribe, unsubscribe, or browse
- the archives through the
- @uref{http://mail.gnu.org/mailman/listinfo/bug-kawa,
- @code{bug-kawa} web interface}.
- @node Mailing lists
- @section General Kawa email and discussion
- The general Kawa email list is @email{kawa@@sourceware.org}.
- This mailing list is used for announcements, questions, patches,
- and general discussion relating to Kawa. If you wish to subscribe,
- send a blank message request to @email{kawa-subscribe@@sourceware.org}.
- To unsubscribe, send a blank message to
- @email{kawa-unsubscribe@@sourceware.org}.
- (If your mail is forwarded and you're not sure which email address you're
- subscribed as, send mail to the address following @code{mailto:} in the
- @code{List-Unsubscribe} line in the headers of the messages you get from
- the list.)
- You can browse the @uref{http://sourceware.org/ml/kawa/,
- archive of past messages}.
- There are separate mailing lists for
- @uref{http://mail.gnu.org/mailman/listinfo/qexo-general, Qexo}
- and @uref{http://lists.sourceforge.net/mailman/listinfo/jemacs-info,JEmacs}.
- @node Acknowledgements
- @section Acknowledgements and thanks
- The author and project leader of Kawa is
- @uref{http://per.bothner.com/,Per Bothner}
- @email{per@@bothner.com}.
- Kawa is a re-write of Kawa 0.2, which was a Scheme interpreter written by
- R. Alexander Milowski @email{alex@@milowski.com}.
- Thanks to Cygnus Solutions (now part of Red Hat) for sponsoring
- the initial development of Kawa, and then transferring
- their ownership interest to Per.
- @subsubheading Financial support
- Ean Schuessler and @uref{http://www.brainfood.com/,Brainfood}
- provided financial support and encouragement.
- Thanks to Chris Dean, Dean Ferreyra, and others
- at @uref{http://www.mercedsystems.com/,Merced Systems} for financial
- support and other contributions.
- @uref{http://google.com/,Google} through their
- @uref{http://code.google.com/soc/,Summer of Code} project
- sponsored Charles Turner during Summer 2011 and 2012,
- Andrea Bernardini Summer 2014 and 2015,
- and Tom Bousso Summer 2017.
- Thomas Kirk and AT&T provided financial support, and useful bug reports.
- @subsubheading Various contributions
- @uref{http://jcubic.pl/,Jakub Jankiewicz} contributed the Kawa logo.
- Helmut Eller provided SLIME support, syntaxutils.scm, and many bug reports.
- Daniel Bonniot for multiple small improvements
- to gnu.bytecode and gnu.expr.
- Jamison Hope for multiple contributions,
- including quaternion support, the SRFI-14 implementation,
- Ant improvements, and Google Summer of Code mentoring.
- Jim White for Ant support and other improvements.
- Bruce R. Lewis implemented @ref{KRL,,KRL} and made other contributions.
- Geoff Berry: Handle Exceptions attribute. Other improvements.
- Tom Bousso re-implemented @code{gnu.bytecode} to make use
- of @uref{asm.ow2.org,ASM}; plus other changes,
- Shad Gregory improved JEmacs.
- Al Petrofsky improved gnu.math printing and added some IntNum methods.
- Marco Vezzoli: SRFI-1 tailoring for Kawa.
- Albert Ting - old GuiConsole code.
- Christian Surlykke ported JEmacs to use SWT.
- Geoff Berry for various gnu.bytecode improvements.
- Ivelin Ivanov and Tom Reilly for servlet support.
- Anthony Green for Fedora packaging.
- Charles Turner for pretty-printer improvements,
- improvements in the Common Lips support, and other changes.
- Andrea Bernardini optimized the implementation of @code{case}, and
- implemented full continuation support (in experimental @code{callcc} branch.
- Julien Rousseau and Marius Kjeldahl contributed to Android support.
- Peter Lane for many documentation improvements.
- William D Clinger for test-cases.
- @subsubheading Small fixes and improvements
- Patrick Barta;
- Joseph Bowbeer;
- Dominique Boucher;
- Alexander Bunkenburg;
- Harold Carr;
- Emmanuel Castro;
- Álvaro Castro-Castilla;
- Sudarshan S Chawathe;
- Heather Downs;
- Francisco Vides Fernández;
- Nic Ferrier;
- Oliver Flasch;
- Weiqi Gao;
- Luke Gorrie;
- Mario Domenech Goulart;
- Zvi Har'E;
- Jeff Haynes;
- Ethan Herdrick;
- Joerg-Cyril Hoehle;
- Elliott Hughes;
- Mike Kenne;
- Brian Jones;
- Gerardo Jorvilleur;
- Simon Josefsson (JEmacs menu);
- Shiro Kawai;
- Thomas Kirk;
- Jay Krell;
- Timo Myyrä;
- Edouard Parmelan;
- Walter C. Pelissero;
- Rafael Jesus Alcantara Perez;
- Lynn Quam;
- Marcus Otto;
- Terje Pedersen (some XQuery functions);
- Matthias Radestock;
- Jim Rees;
- Ola Rinta-Koski;
- Andreas Schlapbach;
- Robert D. Skeels;
- Benny Tsai;
- Vladimir Tsichevski;
- Matthieu Vachon;
- Vasantha Ganesh;
- Phil Walker;
- Knut Wannheden;
- Chris Wegrzyn,
- Kay Zheng,
- @subsubheading Bug reports and test cases
- Seth Alves;
- Khairul Azhar;
- Bob Bane;
- Hans Boehm;
- Adrián Medraño Calvo;
- Brian D. Carlstrom;
- Luis Casillas;
- Sudarshan S Chawathe;
- Ken Dickey (format tests);
- Helge Dietert;
- Allan Erskine;
- Marc Feeley (polytype.scm);
- Margus Freudenthal;
- Weiqi Gao;
- Andrea Girotto;
- Norman Hard;
- Gerardo Horvilleur;
- Yaroslav Kavenchuk;
- Felix S Klock II;
- Francois Leygues;
- Mirko Luedde;
- Leonardo Valeri Manera;
- Kjetil S. Matheussen;
- Alex Mitchell;
- Alex Moiseenko;
- Marc Nieper-Wißkirchen;
- Okumura Yuki;
- Edouard Parmelan;
- Walter C. Pelissero;
- Stephen L. Peters;
- François Pinard;
- Bill Robinson;
- Dan Stanger (Eaton Vance);
- Hallvard Traetteberg;
- Taylor Venable;
- Alessandro Vernet;
- Tony White
- John Whittaker;
- Robert Yokota.
- @subsubheading Code ported from other packages
- Kawa includes Free Software originally written for other purposes,
- but incorporated into Kawa, perhaps with some porting. A partial list:
- Dorai Sitaram wrote pregexp.
- The @code{rationalize} algorithm is by Alan Bawden and Marc Feeley.
- Lars T Hansen wrote SRFI-11 (let-values, let*-values macros).
- Olin Shivers wrote the SRFI-1 list-processing library,
- and the SRFI-13 reference impementation.
- John David Stone wrote SRFI-8 (receive macro)
- Jussi Piitulainen wrote the SRFI-25 specification and tests.
- Richard Kelsey and Michael Sperber wrote SRFI-34.
- Anthony Carrico wrote the SRFI-37 reference implementation.
- Panu Kalliokoski wrote the SRFI-69 reference implementation.
- Donovan Kolbly wrote the srfi-64 ``meta'' testsuite.
- Alex Shinn improved SRFI-64 portability.
- Philip L. Bewig wrote the SRFI-41 (streams) specification and
- reference implementation.
- Simon Tatham wrote listsort.
- Aubrey Jaffer wrote much of SLIB, some of which has been
- imported into gnu.kawa.slib. He also wrote some tests we're using.
- @node Support
- @section Technical Support for Kawa
- If you have a project that depends on Kawa or one of its component
- packages, you might do well to get paid priority support from
- Kawa's author.
- The base price is $2400 for one year. This entitles you to basic
- support by email or phone. Per @email{per@@bothner.com} will answer techical
- questions about Kawa or its implementation, investigate bug reports, and
- suggest work-arounds. I may (at my discretion) provide fixes and
- enhancements (patches) for simple problems. Response for support
- requests received during the day (California time) will normally be
- within a few hours.
- All support requests must come through a single designated contact
- person. If Kawa is important to your business, you probably
- want at least two contact people, doubling the price.
- If the support contract is cancelled (by either party), remaining
- time will be prorated and refunded.
- Per is also available for development projects.
- @node Projects
- @section Projects using Kawa
- @uref{http://appinventor.mit.edu/,MIT App Inventor}
- for Android (formerly Google App Inventor)
- uses Kawa to translate its visual blocks language.
- The @uref{http://www.narrativeandplay.org/hypedyn/,HypeDyn} hypertext
- fiction authoring tool is written in Kawa. HypeDyn (pronounced "hyped
- in") is a procedural hypertext fiction authoring tool for people who
- want to create text-based interactive stories that adapt to reader
- choice. HypeDyn is free to download and open source, and runs on
- Linux, MacOS and Windows. This is a research project carried out at
- the Department of Communications and New Media, National University of
- Singapore.
- @uref{http://www.nuecho.com,Nü Echo} develops high-performance speech
- enabled applications. Nü Echo uses Kawa for the development of innovative
- speech application development tools, like a complete
- @uref{http://www.nuecho.com/en/services/grammar.shtml,grammar IDE}.
- @uref{http://www.mercedsystems.com/, Merced Systems@comma{} Inc.} uses Kawa
- extensively in their contact center performance management product
- Merced Peformance Suite. Kawa Scheme is used for all development
- and has allowed Merced to realize the large productivity gains
- that come with using Scheme while still maintaining tight
- integration with a large number of Java libraries.
- JEmacs is included in the Kawa distribution. It is a project to
- re-implement Emacs, allowing a mix of Java, Scheme, and Emacs Lisp.
- It has its own @uref{http://jemacs.sourceforge.net/,home-page}.
- BRL (``the Beautiful Report Language") is a database-oriented language
- to embed in HTML and other markup.
- @uref{http://brl.sourceforge.net/, BRL} allows you to embed Scheme in
- an HTML file on a web server.
- The @uref{http://schemeway.sourceforge.net,SchemeWay Project} is a set of
- @uref{http://www.eclipse.org,Eclipse} plug-ins for professional Scheme
- programming. The first plugin released, SchemeScript, is a fully-featured
- Scheme
- editor customizable in Scheme. It embeds the Kawa Scheme system and has
- many features that ease Kawa Scheme programming (like code completion on
- variable names,
- class and method names, namespaces, etc).
- The Health Media Research Laboratory, part of the Comprehensive Cancer
- Center at the University of Michigan, is using Kawa as an integral part of
- its core tailoring technologies. Java programs using Kawa libraries are used
- to administer customized web-based surveys, generate tailored feedback,
- validate data, and "characterize," or transform, data. Kawa code is embedded
- directly in XML-formatted surveys and data dictionaries. Performance and
- ease of implementation has far exceeded expectations. For more information
- contact Paul R. Potts, Technical Director, Health Media Research Lab,
- @code{<potts@@umich.edu>}.
- Mike Dillon (@code{mdillon@@gjt.org})
- did the preliminary work of creating a
- Kawa plugin for jEdit. It is called SchemeShell and provides a REPL inside
- of the jEdit console for executing expressions in Kawa (much as the BeanShell
- plugin does with the BeanShell scripting language).
- It is currently available only via CVS from:
- @example
- CVSROOT=:pserver:anonymous@@cvs.jedit.sourceforge.net:/cvsroot/jedit
- MODULE=plugins/SchemeShell
- @end example
- STMicroelectronics (@code{marco.vezzoli@@st.com})
- uses Kawa in a prototypal
- intranet 3tier information retrieval system as a communication protocol
- between server and clients, and to do server agents programming.
- @ignore
- The Nice Programming Language is a new open source language with a
- Java-like syntax. It features multiple dispatch, parametric types,
- higher-order functions, tuples, optional parameters, safe static typing
- of @code{null}, ..., and the new concept of "abstract interfaces".
- The Nice compiler (@code{nicec}) uses Kawa's @code{gnu.expr}
- and @code{gnu.bytecode}
- packages to generate Java bytecode.
- You can find more about Nice at @uref{http://nice.sourceforge.net}.
- For more information feel free to contact
- Daniel Bonniot @email{bonniot@@users.sf.net}).
- @end ignore
- @node Ideas and tasks
- @section Ideas and tasks for contributing to Kawa
- Kawa (like other Free Software projects) has no lack of tasks and projects
- to work on. Here are some ideas.
- The ones marked @i{(GSoC)} are probably most suitable for a Google
- Summer of Code project, in being a reasonable size, self-contained, and not
- depending on other tasks.
- @subsection Recusively initialized data structures
- @i{(GSoC)}
- Kawa has convenient syntax to @ref{Allocating objects, allocate and initialize objects},
- but it gets messier it you want to initialize multiple objects that
- reference each other.
- Likewise for a single object ``tree'' which contains links to the root.
- In this example, we will looks at two vectors, but the feature is more useful
- for tree structures. Assume:
- @example
- (define-constant list1 [1 2 list2])
- (define-constant list2 ['a 'b list1])
- @end example
- The compiler translates this to:
- @example
- (define-constant list1
- (let ((t (object[] length: 3))) ;; allocate native Java array
- (set! (t 0) 1)
- (set! (t 1) 2)
- (set! (t 2) list2)
- (FVector:makeConstant t)))
- (define-constant list2
- (let ((t (object[] length: 3))) ;; allocate native Java array
- (set! (t 0) 'a)
- (set! (t 1) 'b)
- (set! (t 2) list1)
- (FVector:makeConstant t)))
- @end example
- The problem is that @code{list2} has not been created when
- we evaluate the initializing expression for @code{list}.
- We can solve the problem by re-writing:
- @example
- (define-private tmp1 (object[] length: 3))
- (define-constant list1 (FVector:makeConstant tmp1)
- (define-private tmp2 (object[] length: 3))
- (define-constant list2 (FVector:makeConstant tmp2)
- (set! (tmp1 0) 1)
- (set! (tmp1 1) 2)
- (set! (tmp1 2) list2)
- (set! (tmp2 0) 1)
- (set! (tmp2 1) 2)
- (set! (tmp2 2) list1)
- @end example
- The complication is that the code for re-writing vector and object constructors
- is spread out (depending on the result type), and not where we
- deal with initializing the variables.
- One solution is to introduce an inlineable helper function @code{$build$} defined as:
- @example
- (define ($build$ raw-value create init)
- (let ((result (create raw-value))
- (init raw-value result)
- result))
- @end example
- Then we can re-write the above code to:
- @example
- (define-constant list1
- ($build$
- (object[] length: 3)
- (lambda (raw) (FVector:makeConstant raw))
- (lambda (raw result)
- ($init-raw-array$ raw 1 2 list2))))
- (define-constant list2
- ($build$
- (object[] length: 3)
- (lambda (raw) (FVector:makeConstant raw))
- (lambda (raw result)
- ($init-raw-array$ raw 'a 'b list1))))
- @end example
- Note that the call to @code{$build$}, as well as the generated @code{lambda}
- expressions, are all easily inlineable.
- Now assume if at the top-level @var{body} if there is a sequence of @code{define-constant}
- definitions initialized with calls to @code{$build$}.
- Now it is relatively easy to move all the @code{init} calls after all
- @code{alloc} and @code{create} expressions.
- The @code{$init-raw-array$} calls are expanded after the code has been re-ordered.
- The project includes both implementing the above framework,
- as well as updating type-specific (and default) object creation to use the framework.
- It would also be good to have compiler warnings if accessing an uninitialized object.
- @subsection Enhance texinfo-js documentation browser for Kawa documentation
- @i{(GSoC)}
- @subsection Run interactive process in separate Java Virtual Machine:
- @i{(GSoC)}
- When developing and testing it is useful for the REPL to support
- hot-swapping (replacing functions on-the-fly) and debugging.
- The main goal being able to smoothly reload changed modules
- (files or functions), and have other modules not break.
- Debugging (such as setting breakpoints) would not be a priority
- for this project, but could be a follow-on project.
- Skills: Should be experienced with Java, and interested in learning
- about @uref{https://docs.oracle.com/javase/8/docs/technotes/guides/jvmti/index.html,JVM TI} and similar low-level parts of the platform.
- Difficulty: Challenging, but you can study
- how @uref{https://en.wikipedia.org/wiki/Jshell,Java-9's new jshell}
- uses the JVM TI.
- @subsection Better dynamic reload
- @i{(GSoC - this is related to the previous item)}
- Kawa does a lot of optimizations and inlining. This conflicts
- with being able to ``reload'' a module into an already-running
- interactive environment.
- We could add an option to load a module in ``reloadable'' mode.
- Kawa already patches an old function object (a @code{ModuleMethod})
- so existing references to the function get automatically updated.
- However, there are problems if the ``signature'' of the function
- changes - for example if the return type (declared or inferred)
- becomes more general. In those cases the best thing is to
- re-compile any code that depends on the modified function.
- Reloading a module that defines a class is even trickier,
- at least if there are existing instances that should
- work as the updated class. We can handle the special case
- where only method bodies change: In reloadable mode, each
- method body is compiled to a separate function, the
- actual body indirects to the function. We must also
- recognize when compiling a new version of the same
- class, which requires a textual comparison between the
- old and new versions, or a structural comparison
- between the old class and the new code.
- When it comes to top-level variables, an issue is when
- to re-evaluate the initializing expression. It is reasonable
- to do so if and only if the expression is modified, which
- again requires a textual comparison.
- @subsection Easier Access to Native Libraries using JNA/JNR
- @i{(GSoC)}
- The traditional way to access native (C/C++) functions is using JNI,
- but it's very awkward.
- JNA and @uref{https://github.com/jnr,JNR} are @uref{http://www.oracle.com/technetwork/java/jvmls2013nutter-2013526.pdf,much easier to use}.
- This project would design and implement an easy-to-use Kawa wrapper for
- for JNR. You should study existing JNR wrappers, such as that for JRuby.
- Difficulty: Medium. Need to study existing wrappers and "foreign
- function interfaces" (in multiple languages) and design one suitable for Kawa.
- Some Scheme (Kawa) experience would be helpful.
- @subsection Types for units
- @i{(GSoC)}
- Kawa supports units (such as @code{cm^2} for square centimeters)
- and @ref{Quantities,quantities} (such as @code{4cm^2}).
- We would like to integrate these into the type system, both
- for performance and compile-time type checking.
- For syntax we can use a pseudo-parameterized type @code{quantity}. For example:
- @example
- (define a1 ::quantity[cm^2] 4cm^2)
- (* 2.0 a1) ;; @result{} 8cm^2
- (+ 2.0 a1) ;; @i{compile-time error}
- @end example
- The run-time type of the variable @code{a1} should be
- a primitive @code{double}, without object allocation.
- Of course when @code{a1} is converted to an object, we
- create a @code{Quantity}, not a @code{Double}.
- We can build on Kawa's existing framework for
- non-standard primitive types such as @code{character} and @code{ulong}.
- Skills: Need good Java experience, and somewhat familiar with the
- Java Virtual Machine.
- You will need to become comfortable reading @code{javap} output.
- Difficulty: Modest.
- @subsection Compiler should use class-file reading instead of reflection
- The Kawa compiler currently uses reflection to determine properties
- (such as exported function definitions) from referenced classes.
- It would be better to read class files.
- This should not be too difficult, since the @code{gnu.bytecode} library
- abstracts over class information read by reflection or class reading.
- @subsection Mutually dependent Java and Scheme modules
- @i{(GSoC - maybe)}
- We'd like a command for compiling a list of Java and Scheme
- source files that may have mutual dependencies. A good way
- to do this is to hook into @code{javac}, which is quite extensible
- and pluggable.
- One could do something like:
- @enumerate
- @item
- Read the ``header" of each Kawa source file, to determine the
- name of the generated main class.
- @item
- Enter these class names into the javac tables as ``uncompleted''
- classes.
- @item
- Start compiling the Java files. When this requires the members
- of the Kawa classes, switch to the Kawa files. From javac,
- treat these as pre-compiled .class files. I.e. we treat the Kawa
- compiler as a black box that produces Symbols in the same way as
- reading class files. At this point we should only need the
- initial ``scan'' phase on Kawa.
- @item
- If necessary, finish compiling remaining Kawa files.
- @end enumerate
- This approach may not immediately provide as robust mixed-language
- support as is ideal, but it is more amenable to incremental improvement
- than a standalone stub-generator.
- This project is good if you know or want to learn how @code{javac} works.
- @subsection Use Java-7 MethodHandles and invokedynamic
- Java 7 supports MethodHandles which are meant to provide better
- performance (ultimately) for dynamic languages.
- See @uref{http://jcp.org/en/jsr/detail?id=292,JSR 292}
- and the @uref{http://openjdk.java.net/projects/mlvm/,Da Vinci Machine Project}.
- Kawa makes limited use of MethodHandles, and no use of invokedynamic.
- There is more to be done. For example, we
- can start by optimizing arithmetic when the types are unknown
- at compile-time. They could make implementing
- generic functions (multimethods) more efficient.
- At some point we want to compile lambdas in the same way
- as Java 8 does. This can potentially
- be more efficient than Kawa's current mechanism.
- Remi Forax's @uref{https://github.com/forax/vmboiler,vmboiler} is
- a small library on top of ASM that generates optimistically typed bytecodes.
- It could be useful for ideas.
- @anchor{task-parameterized-types}
- @subsection Parameterized types
- @i{(GSoC)}
- Kawa has some limited support for parameterized types, but
- it's not used much. Improve type inferencing.
- Support definition of parameterized classes.
- Better use of parameterized types for sequence class.
- Support wildcards.
- (It might be better to have wild-carding be associated with
- declarations, as in Scala or @uref{http://openjdk.java.net/jeps/300,proposed for Java}, rather than uses.)
- See also @uref{http://openjdk.java.net/jeps/8043488}.
- @subsection Optimized function types and values using MethodHandles
- @i{(GSoC)}
- Kawa doesn't have true function types: Parameter and result types
- are only handled for ``known'' functions. The general case with
- optional and keyword parameter is complicated, but simple
- fixed-arity procedure types would be very useful.
- The following syntax is suggested:
- @example
- procedure[(@var{T1} .. @var{Tn}) @var{Tr}]
- @end example
- @var{T1} through @var{T1} are types of the parameters, and
- @var{Tr} is the type of the result.
- For example: @code{procedure[(vector int) string]}.
- We call this a typed-procedure type (in contrast to plain @code{procedure}).
- If a value has a typed-procedure type then its run-time representation
- is a just a @code{MethodHandle}. If such a procedure is called,
- the generated bytecode is to just call its @code{invokeExact} method.
- The argument expressions are converted (and type-checked) the same
- way as if we were calling a statically-known procedure.
- Note that passing an @code{int} argument
- of to @code{procedure[(vector int) string]} value does @emph{not}
- require allocating an object to ``box'' the @code{int};
- we can pass a plain @code{int} as-is.
- Thus using typed-procedure types can lead to major speed-up.
- For example the @code{lib-test.scm} should become much faster.
- Converting a known procedure to a typed-procedure type is usually
- just a matter of creating a @code{MethodHandle} that references the
- method implementing the procedure. Some glue code may be needed
- if the types aren't identical, or if the procedure is a closure.
- Converting a type-procedure value @code{p} to generic value (such
- as untyped @code{procedure} or @code{object}) can be though of as
- wrapping it in a @code{lambda}:
- @example
- ((lambda (arg1::vector arg2::int)::string (p arg1 arg2))
- @end example
- Coercing a generic value or an untyped procedure to a typed-procedure would
- need to generate a method whose signature matches the typed-procedure type,
- and in the body of the method use a generic apply.
- Coercing from one typed-procedure type to a different typed-procedure type
- is a combination of the above techniques (as if converting first to object and
- then to the target type), though some optimizations are worth doing.
- Adding varargs support can be done later.
- @c Later, we might consider merging @code{MHProcedure} and @code{PrimProcedure}.
- We need a fall-back mechanism for platforms (such as Android)
- that don't support @code{MethodHandle}s. The easiest is to
- just treat a typed-procedure type as plain @code{procedure} at run-time,
- though we still want the compile-time type-checking,
- @subsection Full continuations
- @emph{Currently being worked on.}
- Add support for full continuations, which is the major
- feature missing for Kawa to qualify as a ``true Scheme''.
- One way to implement continuations is to add a add that converts
- the abstract syntax tree to continuation-passing-style, and then
- expand the existing full-tail-call support to manage a stack.
- There are other ways to solve the problem.
- This may benefit from @ref{task-faster-tailcalls,Faster tailcalls}.
- @subsection Faster tailcalls
- @anchor{task-faster-tailcalls}
- Make @code{--full-tailcalls} run faster.
- This may depend on (or incorporate)
- @ref{task-TreeList-optimization,TreeList-optimization}.
- @anchor{task-TreeList-optimization}
- @subsection TreeList-optimization
- The @uref{http://www.gnu.org/software/kawa/api/gnu/lists/TreeList.html,TreeList} class is a data structure for ``flattened'' trees. It is used for
- XML-style nodes, for multiple values, and for the full-tail-call API.
- The basic concept is fine, but it could do with some re-thinking
- to make make random-access indexing fast. Also, support for updating
- is insufficient. (This needs someone into designing and hacking on
- low-level data-structures, along with lots of profiling and testing.)
- @subsection Asynchronous evaluation
- C# recently added @code{asynch} and @code{await} keywords
- for @uref{http://msdn.microsoft.com/en-us/vstudio/gg316360,asynchronous programming}. Kawa's recently improved support for lazy programming
- seems like a good framework for equivalent functionality:
- Instead of an @code{asynch} method that returns a @code{Task<T>},
- the Kawa programmer would write a function that returns a @code{lazy[T]}.
- This involves some design work, and modifying the compiler to
- rewrite the function body as needed.
- This is related to full continuations, as the re-writing is similar.
- @anchor{task-REPL-improvements}
- @subsection REPL console and other REPL improvement
- @emph{Currently being worked on.}
- Improvements to the read-eval-print console.
- In addition to a traditional Swing console,
- it would be useful to support using a web browser as a remote terminal,
- possibly using web-sockets.
- (This allows ``printing'' HTML-expressions, which can be a useful way
- to learn and experiment with web technologies.)
- See @uref{http://per.bothner.com/blog/2007/ReplPane/, here} for an article
- on the existing Swing REPL, along with some to-do items.
- Being able to hide and show different parts of the output might be nice.
- Being able to link from error messages to source might be nice.
- Better handling of redefinitions is discussed
- @uref{http://per.bothner.com/blog/2009/REPL-for-JavaFX/, here in the context of JavaXF Script}; this is a general REPL issue, mostly independent of the GUI for it.
- An interesting possibility is to use the @uref{http://ipython.org/,IPython}
- framework. There are existing ports for Scala: either
- @uref{https://github.com/mattpap/IScala,IScala}
- or @uref{https://github.com/Bridgewater/scala-notebook, Scala Notebook}.
- @subsection XQuery-3.0 functionality
- @i{(GSoC, for some subset)}
- It would be nice to update the XQuery (Qexo) support
- to some subset of @uref{http://www.w3.org/TR/xquery-30/,XQuery 3.0}.
- @subsection XQuery-updates
- It would be nice to support @uref{http://www.w3.org/TR/xquery-update-10/, XQuery updates}. This depends on @ref{task-TreeList-optimization,TreeList-optimization}.
- @anchor{task-common-lisp}
- @subsection Common Lisp support
- Kawa supports a small subset of the Common Lisp language, but it supports
- a much larger subset of core Common Lisp concepts and data structures, some
- designed with Common Lisp functionality in mind. Examples include
- packages, arrays, expanded function declarations, type specifications,
- and format. A lot could be done to improve the Common Lisp support
- with modest effort. Some Common Lisp features could also be useful
- for Scheme: Documentation strings (or markup) as Java annotations,
- better MOP-like introspection, and generic methods a la defmethod
- (i.e. with multiple definition statements, possibly in separate files,
- as opposed to the current make-procedure) all come to mind.
- Being able to run some existing Common Lisp code bases with
- at most modest changes should be the goal.
- One such package to start with might be an
- @uref{http://aperiodic.net/phil/archives/Geekery/notes-on-lisp-testing-frameworks.html,existing test framework}, perhaps
- @uref{http://common-lisp.net/project/bese/FiveAM.html, FivaAM}.
- Full Common Lisp compatibility is nice, but let's walk before we can run.
- @subsection JEmacs improvements
- @i{(GSoC, for some subset)}
- A lot of work is needed to make
- @uref{http://jemacs.sourceforge.net/,JEmacs} useful.
- One could try to import a useful package and see what works and what fails.
- Or one may look at basic editing primitives.
- Enhancements may be needed to core Emacs Lisp language primitives
- (enhancing @ref{task-common-lisp, Common Lisp support} may help),
- or to the display engine.
- Emacs now supports @uref{http://www.gnu.org/software/emacs/manual/html_node/elisp/Lexical-Binding.html,lexical bindings} - we should do the same.
- @subsection Improved IDE integration
- There is some Kawa support for Eclipse (Schemeway), and possibly
- other IDEs (NetBeans, IntelliJ). But many improvements are
- desirable.
- @ref{task-REPL-improvements, REPL improvements} may be a component of this.
- @subsubsection Plugin for NetBeans IDE
- Kawa-Scheme support for the NetBeans IDE would be useful.
- One could perhaps build on the Clojure plugin.
- @subsubsection Plugin for Eclipse IDE
- Kawa-Scheme support for the Eclipse IDE would be useful.
- Probably makes sense to
- enhance @uref{http://sourceforge.net/projects/schemeway/,SchemeWay}.
- It may also make sense to build on
- the @uref{http://www.eclipse.org/dltk/,Dynamic Languages Toolkit},
- possibly making use of @uref{http://schemeide.sourceforge.net/,Schemeide},
- though DLTk seems more oriented towards interpreted non-JVM-based languages.
- @subsubsection Improve Emacs integration
- @uref{http://en.wikipedia.org/wiki/SLIME, SLIME} is an Emacs mode
- that provides IDE-like functionality. It supports Kawa.
- @uref{http://jdee.sourceforge.net/,JDEE} is a Java development environment,
- so might have better hooks to the JVM and Java debugging architecture.
- @uref{http://cedet.sourceforge.net/,CEDET} is a more general
- framework of development tools.
- @subsection Hop-style web programming
- @uref{http://hop.inria.fr/,Hop} is an interesting design
- for integrating server-side and client-side programming using
- a Scheme dialect. These ideas seem like they would port
- quite well to Kawa.
- @subsection String localization
- @i{(GSoC)}
- Support localization by extending the
- @uref{http://srfi.schemers.org/srfi-109/srfi-109.html,SRFI_109}
- syntax, in the manner of (and compatible with)
- @uref{http://www.gnu.org/software/gettext/,GNU gettext}.
- I.e. optionally specify a localization key (to use as an index
- in the translation database); if there is no key specified,
- default to using the literal parts of the string.
- @subsection Data binding
- Implement a ``bind'' mechanism similar to that
- of @uref{http://docs.oracle.com/javafx/1.3/tutorials/core/dataBinding/,JavaFX Script}.
- The idea is that when you initialize a variable or field,
- instead of initializing it to a fixed value, you bind it to
- an expression depending on other variables.
- We install ``listeners'' on those variables, so when those variables
- change, we update the bound variable.
- This feature is useful in many applications, but the initial
- focus could be GUI programming and perhaps web programming.
- @c Note in general the result is a ``text'', which is a generalization
- @c of a string: A text is a sequence of characters *or* other values.
- @c If an embedded expression evaluates to a non-string, it is @emph{not}
- @c automatically converted to a string. Conversion to a string
- @c is done on demand, for example on printing.
- @c (This is similar to values in the XML data model.)
- @subsection Decimal arithmetic and repeated decimals
- @i{(GSoC. Possibly a bit light for a full Summer project,
- but can be extended or combined with other projects.)}
- Exact decimal arithmetic is a variation of exact rational arithmetic,
- but may be more user-friendly. In particular, printing using
- decimals is generally nicer than fractions.
- It is also sometimes useful to specify an explicit scale,
- so we can distinguish 0.2 from 0.20.
- We can use the Java @code{BigDecimal} class, but run into problems
- with division - for example @code{(/ 1.0 3.0)}.
- We should implement a subclass of @code{RatNum} that generalizes
- @code{BigDecimal} to also handle repeating decimals.
- We need a lexical syntax for repeating decimals.
- Possible ideas: @code{0._81_} or @code{0.#81}.
- If a Scheme number literal is specified as exact and has either
- a decimal point or an exponent (for example @code{#e1.25}), then it should
- read as an exact decimal, not a fraction.
- @subsection Optional strict typing along with an explicit @code{dynamic} type
- @i{(GSoC)}
- Kawa currently implements ``optimistic'' typing: The compiler only
- complains if an expression has no values in common with the target type
- - for example, if assigning a @code{string} expression to an @code{integer}
- variable.
- It would be interesting to experiment with a
- @code{--strict-typing} option (which would never be the default):
- Strict typing would only allow ``widening'' conversions - i.e.
- that the expression type be a subtype of the target type.
- For example it would complain if assigning a @code{number} to an @code{integer}
- unless you used an explicit cast.
- To make this easier to work with we'd make use
- of the @ref{dynamic-type,@code{dynamic} type}, similar to
- @uref{https://msdn.microsoft.com/en-us/library/dd264736.aspx,what
- @code{C#} does}: Any expression can be converted
- to or from @code{dynamic} without the compiler complaining.
- Similarly, if @code{x} is @code{dynamic} then @code{x:name}
- is allowed by the compiler regardless of @code{name}, with all checking
- being deferred to run-time. If a variable is declared without a type,
- it should default to @code{dynamic}. The @code{dynamic} type
- is represented in the VM as @code{object} but with an annotation
- (like we do with @code{character}).
- The type-checker might need some changes to better distinguish
- implicit conversions from explicit casts.
- @node Installation
- @chapter Getting and installing Kawa
- @menu
- * Getting Kawa::
- * Running Java:: Getting and running Java
- * Binary distribution:: Installing and using the binary distribution
- * Source distribution:: Installing and using the source distribution
- @end menu
- @node Getting Kawa, Running Java, , Installation
- @section Getting Kawa
- You can compile Kawa from the source distribution.
- Alternatively, you can install the pre-compiled binary distribution.
- You can get Kawa sources and binaries from the Kawa ftp site
- @uref{ftp://ftp.gnu.org/pub/gnu/kawa/},
- or from a @uref{http://www.gnu.org/order/ftp.html,mirror site}.
- The current release of the Kawa source code is
- @uref{ftp://ftp.gnu.org/pub/gnu/kawa/kawa-@value{VERSION}.tar.gz}.
- (To unpack @code{.tar.gz} files Windows users can use
- @uref{http://www.7-zip.org/,7-Zip}, which is Free Software.)
- The corresponding pre-compiled release
- is @uref{ftp://ftp.gnu.org/pub/gnu/kawa/kawa-@value{VERSION}.zip}.
- The most recent snapshot is
- @uref{ftp://ftp.gnu.org/pub/gnu/kawa/kawa-latest.zip}.
- Instructions for using either are @ref{Binary distribution,here}.
- @subsection Getting the development sources using Git
- The Kawa sources are managed using a
- @uref{https://gitlab.com/kashell/Kawa,git} repository.
- If you want the very latest version grab
- @uref{https://git-scm.com/downloads,a git client},
- and then check out the source using this command:
- @example
- git clone https://gitlab.com/kashell/Kawa.git
- @end example
- After a checkout you will need to run:
- @example
- ./autogen.sh
- @end example
- before proceding with instructions for @ref{Source distribution,building the source distribution}.
- Once you have it checked out, you can keep it up-to-date with @code{git pull}.
- You can also
- @uref{https://gitlab.com/kashell/Kawa/tree/master,browse the git archive} online.
- @node Running Java, Binary distribution, Getting Kawa, Installation
- @section Getting and running Java
- Before installing Kawa, you will need a working Java system.
- The released Kawa jar file assumes Java 8 or newer.
- You need to build Kawa from source if you have Java 5, Java 6,
- or are targeting Android.
- (Older versions of Kawa have been reported to
- work with JDK from 1.1, Kaffe, Symantec Cafe, J++, and GCJ,
- but these are no longer supported.)
- The discussion below assumes you are using the Java Developer's Kit
- (JDK) from Oracle. You can download free copies of
- @uref{http://www.oracle.com/technetwork/java/javase/downloads/index.html, JDK 8} for various platforms.
- @c If you want to run Kawa on a Macintosh, see
- @c @uref{http://rdsathene.org/scheme/mackawa.html}.
- The program @code{java} is the Java interpreter.
- The program @code{javac} is the Java compiler,
- and is needed if you want to compile the source release yourself.
- Both programs must be in your @code{PATH}.
- If you have the JDK in directory @code{$JAVA_HOME},
- and you are using a Bourne-shell compatible shell
- (/bin/sh, ksh, bash, and some others) you can set @code{PATH} thus:
- @example
- PATH=$JAVA_HOME/bin:$PATH
- export PATH
- @end example
- @node Binary distribution
- @section Installing and using the binary distribution
- The binary release comes as a @code{.zip} archive that
- includes Kawa itself (as a @code{.jar} file @code{kawa-@var{version}.jar}),
- some third-party helper libraries, @code{kawa} command scripts
- (for GNU/Linux/Unix/MacOS or Windows),
- and documentation (basically this manual).
- After downloading (see @ref{Getting Kawa}), extract the files
- from the @code{.zip} archive using a suitable @code{unzip} program,
- which will create a directory @code{kawa-@var{version}},
- with @code{lib}, @code{bin}, and @code{doc} sub-directories.
- In the following, we assume the environment variable @code{KAWA_HOME}
- refers to this directory:
- @example
- unzip ~/Downloads/kawa-@var{version}.zip
- export KAWA_HOME=`pwd`/kawa-@var{version}
- @end example
- The binary release requires Java 8 or later.
- If you have an older Java implementation, or build for a mobile
- environment like Android,
- then you will need to get the source distribution.
- If you want to use Kawa as part of some other application,
- you just need the @code{$KAWA_HOME/lib/kawa.jar}.
- @subsubheading Running the @code{kawa} command
- To run a Kawa script file or the Kawa read-eval-print-loop
- run the Kawa application. There are various way to do so.
- The recommended way is to execute the @code{$KAWA_HOME/bin/kawa} Bash
- shell script.
- This should work on most Unix-like platforms that have Bash installed,
- including GNU/Linux, BSD, MacOS, and Cygwin/MingW.
- (Please report if you have problems.)
- The script assumes that either a suitable @code{java} program is
- in your @code{PATH}; or the @code{JAVA} environment variable
- names a suitable @code{java} executable; or that @code{JAVA_HOME}
- is set so @code{$JAVA_HOME/bin/java} is suitable.
- If you want to put @code{kawa} in your search path you can of course do:
- @example
- PATH=$KAWA_HOME/bin:$PATH
- @end example
- Alternatively you can create a symbolic link in an already-searched directory.
- For example:
- @example
- cd /usr/local/bin
- ln -s $KAWA_HOME/bin/kawa kawa
- @end example
- The @code{bin/kawa.bat} script works on Windows.
- Both scripts add some helper libraries, including support for input editing.
- It is also possible to run Kawa using @code{java} directly:
- @example
- java -jar $KAWA_HOME/lib/kawa.jar
- @end example
- or:
- @example
- CLASSPATH=$KAWA_HOME/lib/kawa.jar
- export CLASSPATH
- java kawa.repl
- @end example
- On Windows:
- @example
- set classpath=%KAWA_HOME%\lib\kawa.jar
- @end example
- To run Kawa in a fresh window use the -w flag:
- @example
- kawa -w
- @end example
- or
- @example
- java kawa.repl -w
- @end example
- @subsubheading Reading the documentation
- The file @code{doc/kawa-manual.epub} contains the Kawa documention
- packaged as an electronic book, which is readable by most
- e-book readers. Plugins are also available for common browsers,
- for example @uref{http://www.epubread.com,EPUBReader} for @code{firefox}.
- Even easier is to invoke
- @ref{browse-manual-option,@code{bin/kawa --browse-manual}}
- (or on Windows: @code{bin\kawa.bat --browse-manual}).
- An @code{epub} is essentially a zip archive, which you can unzip:
- @example
- cd $KAWA_HOME/doc
- unzip kawa-manual.epub
- @end example
- Then you can use a plain browser
- with the URL @code{file:$KAWA_HOME/doc/OEBPS/index.xhtml}.
- @node Source distribution, , Binary distribution, Installation
- @section Installing and using the source distribution
- The Kawa release normally comes as a gzip-compressed tar file named
- @samp{kawa-@value{VERSION}.tar.gz}.
- @c The same sources are available as a zip file
- @c @samp{kawa-@value{VERSION}-src.zip}.
- Two methods are supporting for compiling the Kawa sources;
- choose whichever is most convenient for you.
- One method uses the traditional GNU @code{configure} script,
- followed by running @code{make}. This works well on Unix-like
- systems, such as GNU/Linux.
- You can also use this method on Microsoft Windows,
- with the help of tools from @uref{http://www.MinGW.org/, MinGW}
- or @uref{http://www.cygwin.org/, Cygwin}.
- The other method uses the @code{ant} command, a Java-based
- build system released by Apache's Jakarta project. This uses
- an @code{build.xml} file in place of @code{Makefile}s, and
- works on non-Unix systems such as Microsoft Windows. However,
- the @code{ant} method does not support all
- the features of the @code{configure}+@code{make} method.
- @subsection Build Kawa using @code{configure} and @code{make}
- (See @ref{building-on-Windows-with-make,below} for some notes for building
- on Microsoft Windows.)
- If you have a @code{tar.gz} file, first unpack that in your build directory:
- @example
- tar xzf kawa-@value{VERSION}.tar.gz
- cd kawa-@value{VERSION}
- @end example
- If you're building from the Git repository, you need to
- generate @code{configure} and some other files. This is
- easiest done with the @code{autogen.sh} script:
- @example
- ./autogen.sh
- @end example
- Then you must configure the sources. This you do in
- the same way you configure most other GNU software. Normally
- you can just run the configure script with no arguments:
- @example
- ./configure
- @end example
- The @code{configure} script takes a number of @ref{configure options,options}.
- If you have installed Kawa before, make sure your @code{CLASSPATH}
- does not include old versions of Kawa, or other classes that may
- conflict with the new ones.
- Then you need to compile all the .java source files.
- Just run make:
- @example
- make
- @end example
- This assumes that @samp{java} and @samp{javac} are the java interpreter
- and compiler, respectively.
- It has been reported that parallel make doesn't work,
- so don't use the @code{-j2} or above options.
- You can now test the system by running Kawa in place:
- @example
- java kawa.repl
- @end example
- or you can run the test suite:
- @example
- make check
- @end example
- or you can install the compiled files:
- @example
- make install
- @end example
- This will install your classes into @code{$PREFIX/share/java} (and its
- sub-directories). Here @code{$PREFIX} is the directory you specified
- to configure with the @code{--prefix} option, or @code{/usr/local} if you
- did not specify a @code{--prefix} option.
- To use the installed files, you need to set @code{CLASSPATH} so
- that @code{$PREFIX/share/java/kawa.jar} is in the path:
- @example
- CLASSPATH=$PREFIX/share/java/kawa.jar
- export CLASSPATH
- @end example
- This is done automatically if you use the @samp{kawa} script.
- @anchor{configure options}
- @cindex configure options
- @subsubsection Configure options
- The @code{configure} script takes a number of options.
- The @code{--help} switch gives you a list of options.
- The following are some of the more common or important ones.
- @table @asis
- @item @code{--prefix=@var{install-dir}}
- @itemx @code{--prefix @var{install-dir}}
- By default @code{make install} will install the compiled @code{.jar}
- files info @code{/usr/local/share/java},
- the @code{kawa} command into @code{/usr/local/bin},
- and so on in @code{/usr/local}.
- The @code{--prefix} option causes the files to be installed
- under @code{@var{install-dir}} instead of @code{/usr/local}.
- For example to install the @code{.jar} in @code{/opt/kawa/share/java}
- and otherwise use @code{/opt/kawa} do:
- @example
- ./configure --prefix=/opt/kawa
- @end example
- @item @code{--with-java-source=@var{version}}
- As distributed, the Kawa source code requires Java 8.
- If you only have Java 7, Java 6, or Java 5, use the @code{--with-java-source} option:
- @example
- ./configure --with-java-source=6
- @end example
- Kawa no longer supports older verisons of Java (JDK 1.4 or older).
- It might be possible to use a tool
- like @uref{http://retroweaver.sourceforge.net/, Retroweaver}
- on the Kawa @code{.jar} to fix up Java 5 dependencies.
- Contact the Kawa author if you want to be a tester for this.
- @item @code{--with-docbook-stylesheet[=@var{path}]}
- Build the documentation (this manual) as an electronic book
- (in ebook format) or a website, using
- the DocBook xslt stylesheets.
- (You can build the documentation without DocBook, but using
- it enables nicer-looking and more functional documentation.)
- The stylesheets are found using @var{path};
- the file @code{@var{path}/epub3/chunk.xsl} needs to exist.
- (For example, on Fedora 25 @var{path} can be @code{/usr/share/sgml/docbook/xsl-ns-stylesheets},
- while on Debian use @code{/usr/share/xml/docbook/stylesheet/docbook-xsl-ns}.)
- @item @code{--with-domterm}
- @itemx @code{--with-domterm=@var{domterm_home}}
- Compile with extra support for the @ref{Using DomTerm,DomTerm}
- terminal emulator library, where @code{@var{domterm_home}}
- is such that @code{@var{domterm_home}/lib/domterm.jar} exists.
- (Some DomTerm support is built-in regardless.)
- If you use this option along with @code{--with-javafx}
- then creating a new @ref{REPL Console,REPL} window
- will create a DomTerm window.
- As an optional convenience, you can use the @code{domterm.jar}
- in the Kawa binary distribution.
- @item @code{--with-jline3}
- @itemx @code{--with-jline3=@var{jline3.jar}}
- Build support for using @uref{https://github.com/jline/jline3,JLine 3},
- which is a library for handling console input, similar to GNU readline.
- If specified, the @var{jline3.jar} is added to the classpath of the
- generated @code{kawa.sh} or @code{kawa} shell program.
- An advantage of @code{--with-jline3} (compared to
- @code{--enable-kawa-frontend}) is that the former works without native code
- (on most Unix-like platforms), and it does not require a C wrapper program.
- As an optional convenience, you can use the @code{jline.jar}
- in the Kawa binary distribution.
- @item @code{--with-domterm}
- @itemx @code{--with-domterm=@var{domterm.jar}}
- Compile with extra support for the @ref{Using DomTerm,DomTerm}
- terminal emulator library. (Some DomTerm support is built-in regardless.)
- If you use this option along with @code{--with-javafx}
- then creating a new @ref{REPL Console,REPL} window
- will create a DomTerm window.
- As an optional convenience, you can use the @code{domterm.jar}
- in the Kawa binary distribution.
- @item @code{--with-servlet}
- @itemx @code{--with-servlet=@var{servlet-jar}}
- Build support for @ref{Servlets,servlets}, which are used in web servers.
- This requires the @code{servlet-api.jar} (available various places including
- @uref{http://tomcat.apache.org/,Tomcat} or
- @uref{https://glassfish.java.net/,Glassfish}),
- for @code{javax.servlet.Servlet} and related classes.
- If this class isn't in your classpath, specify its location
- as @code{@var{servlet-jar}}. For example:
- @example
- ./configure --with-servlet=/path/to/servlet-api.jar
- @end example
- @item @code{--enable-jemacs}
- Build JEmacs (enable Emacs-like text editor) and support (a subset of)
- the Emacs Lisp language. JEmacs is a proof of concept - not really
- usable or maintained.
- @item @code{--with-javafx}
- @itemx @code{--with-javafx=@var{javafx-jar}}
- @itemx @code{--with-javafx=@var{java-home}}
- Set this flag to enable the convenience features
- for @ref{Building JavaFX applications,JavaFX}.
- The JavaFX classes are included in JDK 8 (but not OpenJDK 8),
- and you don't need to specify @code{@var{javafx-jar}} or @code{@var{java-home}}.
- For JDK 7 you need to specify @code{@var{javafx-jar}}
- (the path to @code{javafx.rt}) or @code{@var{java-home}}
- (the value of the @code{$JAVA_HOME}).
- @item @code{--with-android=@var{android-jar}}
- Build for the Android platform.
- This requires @ref{Building for Android,special instructons}.
- @item @code{--enable-kawa-frontend}
- If you have the GNU @samp{readline} library installed, you might try
- adding the @samp{--enable-kawa-frontend} flag.
- This will build the
- @samp{kawa} front-end program, which provides input-line editing
- and an input history. You can get @samp{readline} from archives
- of GNU programs, including @uref{ftp://www.gnu.org/}.
- Note that using JLine, enabled by @code{--with-jline3},
- is now recommended instead of using the @code{readline} frontend.
- You may need to specify to @code{make} where to find
- the @code{readline} include files (with @code{READLINE_INCLUDE_PATH})
- and the library (with @code{READINE_LIB_PATH}).
- For example on OS/X you need to do:
- @example
- make READLINE_INCLUDE_PATH=-I/usr/local/unix/readline/include \
- READLINE_LIB_PATH=-L/usr/local/unix/readline/lib
- @end example
- @end table
- @anchor{building-on-Windows-with-make}
- @subsubsection Building on Windows using MinGW
- The Kawa @code{configure} and @code{make} process assumes Unix-like
- tools, which you can get from @uref{http://mingw.org, the MinGW project}.
- Download the MingGW Installation Manager, and use it to install
- at least @code{mingw-developer-toolkit}.
- (Also installing @code{msys-groff} avoids a minor problem
- building the documentation.)
- The @code{C:\MinGW\msys\1.0\msys.bat} script creates a command window
- with the @code{bash} shell and the @code{PATH} set up as needed.
- Alternatively, you can use the standard Windows command prompt
- if you set your @code{PATH} as described in @uref{http://mingw.org/wiki/Getting_Started, here}.
- @subsubsection Building on Windows using Cygwin
- The free @uref{http://sourceware.org/cygwin/,Cygwin}
- environment can be used for building Kawa: The Kawa configure script
- recognizes Cygwin, and modifies the classpath to use Windows-style
- path separators.
- Beyond the base packages, you probably want to install @code{autoconf},
- @code{automake}, @code{git}, @code{texinfo}, @code{groff},
- @code{make}, and @code{diffutils}.
- Cygwin (unlike MinGW) has a current version of @code{makeinfo}, but
- an undiagnosed bug still prevents building @code{kawa.info}.
- You can work around that problem with @code{touch doc/kawa.info}.
- @subsection Building the documentation
- @subsubsection Plain HTML documentation
- You can build a plain HTML version of the documentation
- (using @code{makeinfo} from the @code{texinfo} distribution):
- @example
- cd doc && make kawa-html/index.html
- @end example
- In this case, point your browser at
- @code{file:/@var{kawa_srcdir}/doc/kawa-html/index.html}.
- @subsubsection Fancier HTML documentation
- To build the documentation in a nicer form suitable for a web-site
- you need @code{makeinfo} @emph{and} the DocBook XSLT tools
- (and to have run @code{configure} with
- the @code{--with-docbook-stylesheet} option):
- @example
- cd doc && make web/index.html
- @end example
- You can then point your browser at @code{file:/@var{kawa_srcdir}/doc/web/index.html}.
- @subsubsection Using ebook readers or the --browse-manual option
- To build an @code{EPUB} file suitable for ebook readers,
- as well as enabling support for the
- @ref{browse-manual-option,@code{kawa --browse-manual} option}, do:
- @example
- cd doc && make kawa-manual.epub
- @end example
- This also requires the DocBook XSLT tools.
- @subsubsection Building a printable PDF file
- To build a @code{pdf} file suitable for printing or online viewing do:
- @example
- cd doc && make kawa.pdf
- @end example
- The resulting @code{kawa.pdf} is somewhat unsatisfactory - when viewed online,
- links aren't clickable. Furthermore, box drawing characters are missing.
- @subsection Build Kawa using @code{ant}
- Kawa now includes an Ant buildfile (@code{build.xml}).
- @uref{http://ant.apache.org, Ant} is a part of the Apache
- Jakarta project.
- If you don't hava Ant installed,
- get it from @uref{http://ant.apache.org/bindownload.cgi}.
- The build is entirely Java based and works equally well on *nix, Windows,
- and presumably most any other operating system.
- Once Ant has been installed and configured (you may need to set the
- @code{JAVA_HOME}, and @code{ANT_HOME} environment variables), you should
- be able to change to the directory containing the @code{build.xml} file,
- and invoke the @samp{ant} command. With the default settings, a
- successful build will result in a @code{kawa-@value{VERSION}.jar} in the
- current directory.
- There are a few Ant "targets" of interest (they can be supplied on the
- Ant command line):
- @table @code
- @item all
- This is the default, it does @code{classes} and @code{jar}.
- @item classes
- Compiles all the files into @code{*.class} files into the directory
- specified by the @code{build.dir} property.
- @item jar
- Builds a jar into into the directory
- specified by the @code{dist.dir} property.
- @item runw
- Run Kawa in a GUI window.
- @item clean
- Deletes all files generated by the build, including the jar.
- @end table
- There is not yet a @code{test} target for running the testsuite.
- There are various ``properties" that control what @code{ant} does. You can
- override these on the command line or by editing the
- @code{build.properties} file in the same directory as @code{build.xml}.
- For example, the @code{build.dir} property tells @code{ant} where to
- build temporary files, and where to leave the resulting @code{.jar}
- file. For example, to leave the generated files in the sub-directory
- named @code{BUILD} do:
- @example
- ant -Dbuild.dir=BUILD
- @end example
- A sample @code{build.properties} is provided and it contains
- comments explaining many of the options.
- Here are a few general properties that help to customize your build:
- @table @code
- @item build.dir
- Path to put the temporary files used for building.
- @item dist.dir
- Path to put the resulting jar file.
- @item version.local
- A suffix to add to the version label for your customized version.
- @item debug
- Whether (true/false) the Javac "-g" option is enabled.
- @item optimize
- Whether (true/false) the Javac "-O" option is enabled.
- @end table
- Here are some Kawa-specific ones (all @code{true}/@code{false}):
- @code{with-collections}, @code{with-references}, @code{with-awt},
- @code{with-swing}, @code{enable-jemacs}, and @code{enable-servlet}>
- See the sample @code{build.properties} for more information on these.
- If you change any of the build properties, you will generally want to do
- an @samp{ant clean} before building again as the build is often not able to
- notice that kind of change. In the case of changing a directory path,
- you would want to do the @code{clean} before changing the path.
- A special note for NetBeans users:
- For some reason the build-tools target which compiles an Ant task won't
- compile with the classpath provided by NetBeans.
- You may do @samp{ant build-tools} from the command line outside of NetBeans,
- in which case you will not want to use the @code{clean} target as that
- will delete the tool files as well.
- You can use the @code{clean-build} and/or @code{clean-dist}
- targets as appropriate. Alternatively you can add @code{ant.jar} to the
- @code{build-tools} classpath by copying or linking it into a @code{lib/ext}
- directory in Kawa's source directory (the one containing the @code{build.xml}
- file).
- @ignore
- @subsection Compiling Kawa to native code with GCJ
- @emph{Using GCJ is no longer supported. This section is for historial
- reference, or if someone enhances GCJ enough to support Kawa.}
- The GNU Compiler for the Java(tm) Programming Language
- (@uref{http://gcc.gnu.org/java/,GCJ}) is part of the
- GNU Compiler Collection (@uref{http://gcc.gnu.org/,GCC}).
- It can compile Java source or bytecode
- files into native code on supported systems.
- Version 4.1 or later of GCC is recommended,
- and only Intel x86-based Linux/GNU system have been tested with Kawa.
- First, get and install GCC. Set @code{PREFIX} to where
- you want to install GCJ, and configure it with these options:
- @example
- ./configure --enable-threads --enable-languages=c++,java --prefix $PREFIX
- make bootstrap
- make install
- @end example
- Make sure @code{gcj} is in your path and refers to the newly-installed
- version, and if needed, set @code{LD_LIBRARY_PATH} to point to the
- directory where @code{libgcj.so} was installed:
- @example
- PATH=$PREFIX/bin:$PATH
- LD_LIBRARY_PATH=$PREFIX/lib
- export LD_LIBRARY_PATH
- @end example
- To build Kawa, you need to specify @code{--with-gcj} to
- @code{configure} which tells it to use GCJ.
- @example
- ./configure --with-gcj --prefix $PREFIX
- @end example
- Then as before:
- @example
- make
- make install
- @end example
- Alternatively, you can use configure option
- @code{--with-gcj-dbtool}. This allows gcj to automatically
- find the kawa shared libraries from the @code{.jar} file.
- @end ignore
- @node Tutorial
- @chapter Kawa Scheme Tutorial
- @emph{This is obviously incomplete, but it may be useful,
- especially if you're starting with Kawa from scratch.}
- If you're new to Scheme you might also check out one of these tutorials:
- Takafumi Shido's
- @uref{http://www.shido.info/lisp/idx_scm_e.html,Yet Another Scheme Tutorial};
- @c dead link Greg Badross @uref{http://www.cs.washington.edu/education/courses/341/99suq/lectures/scheme/,lecture notes};
- Dorai Sitaram's @uref{http://www.ccs.neu.edu/home/dorai/t-y-scheme/t-y-scheme-Z-H-1.html,Teach Yourself Scheme in Fixnum Days}; or
- Paul Wilson's @uref{ftp://ftp.cs.utexas.edu/pub/garbage/cs345/schintro-v14/schintro_toc.html,An Introduction to Scheme and its Implementation}.
- @menu
- * Tutorial - Introduction:: Introduction
- * Tutorial - Booleans:: Booleans
- * Tutorial - Numbers:: Numbers
- * Tutorial - Functions:: Functions
- * Tutorial - Variables:: Variables
- * Tutorial - Pictures:: Pictures
- * Tutorial - Sequences:: Lists and sequences
- * Tutorial - Objects:: Creating and using objects
- * Tutorial - Types:: Types and declarations
- * Tutorial - Exceptions and errors::
- * Tutorial - Classes:: Classes
- * Tutorial - Other Java features::
- @end menu
- @node Tutorial - Introduction
- @section Introduction
- You've heard about all the hot scripting languages
- -- you might even be tired of hearing about them.
- But Kawa offers you something different than the
- scripting-language @i{du-jour} can.
- You may be interested in one that runs on the Java virtual machine,
- either because you have to interact with other Java tools,
- or because you like having access to all the Java packages out there.
- Or maybe you don't care about Java, but you care about performance.
- If so, let me tell you about Kawa, which is actually one of the
- very oldest language implementations running on the Java Virtual Machine,
- dating back to 1996.
- The Kawa language is a dialect/implementation of the Scheme language.
- (The Kawa project also supports other languages, including
- @uref{http://www.w3.org/XML/Query,XQuery}
- and @uref{http://jemacs.sourceforge.net,Emacs Lisp},
- as well as tools for implementing mew programming languages,
- but we won't cover that in this tutorial.)
- @uref{http://www.schemers.org/,Scheme}
- is an established language with many
- @uref{http://community.schemewiki.org/?scheme-faq-standards#implementations,implementations},
- a @uref{http://www.schemers.org/Documents/Standards/,standard} specification
- (the traditional @uref{http://www.schemers.org/Documents/Standards/R5RS/,R5RS},
- @uref{http://www.r6rs.org/,R6RS} which was ratified in 2007,
- and @uref{http://www.r7rs.org/,R7RS} which was ratified in 2013),
- and is used by universities for both teaching and research.
- Scheme also has a reputation for being difficult to learn,
- with a weird parenthesis-heavy syntax,
- and hard-to-understand concepts like @uref{http://en.wikipedia.org/wiki/Continuation,continuations}.
- Luckily, you don't need to understand continuations!
- (Kawa doesn't fully implement them anyway.)
- The following assumes that Kawa is already installed on your computer;
- if not see these @ref{Installation,installation instructions}.
- Running the @code{kawa} command in interactive mode
- is a good way start learning Kawa:
- @example
- $ @kbd{kawa}
- #|kawa:1|# @kbd{}
- @end example
- If you don't have @code{kawa} but you have a
- Kawa ``jar'' and you have Java installed you can instead do:
- @example
- $ @kbd{java -jar kawa-@var{version-number}.jar}
- #|kawa:1|# @kbd{}
- @end example
- The prompt string has the form of a Scheme comment,
- to make it easier to cut-and-paste.
- Kawa is expecting you type in an expression or command,
- which it will evaluate, and then print out the result.
- For example, a quoted string is a simple expression that evaluates to a
- string value, which will print as itself, before printing the next prompt:
- @example
- #|kawa:1|# @kbd{"Hello, world!"}
- Hello, world!
- #|kawa:2|# @kbd{}
- @end example
- @c Thus the Kawa equivalent of the traditional
- @c @uref{http://en.wikipedia.org/wiki/Hello_world,``hello world''} is trivial.
- The most noticable difference from most other programming languages
- is that Scheme uses ``prefix'' notation for function calls.
- For example Kawa has a function @code{max} which returns the
- largest value of the arguments.
- Instead of @code{max(5, 7, 3)}
- you write @code{(max 5 7 3)}:
- @example
- (max 5 7 3) @result{} 7
- @end example
- (We use the @code{@result{}} symbol above to indicate that
- the expression @code{(max 5 7 3)} evaluates to the
- value @code{7}.)
- The prefix notation may feel a bit weird, but you quickly
- get used to it, and it has some advantages.
- One is consistency: What are special infix operators in most languages
- are just regular functions in Scheme.
- For example, addition is just a regular function call,
- and @code{+} is just a regular function name:
- @example
- (+ 2.5 1.2) @result{} 3.7
- @end example
- The same prefix notation is used for special operations like assignments:
- @example
- #|kawa:1|# @kbd{(set! sqrt-of-2 (sqrt 2))}
- #|kawa:2|# @kbd{sqrt-of-2}
- 1.4142135623730951
- @end example
- @node Tutorial - Booleans
- @section Booleans
- Scheme uses the syntax @code{#t} and @code{#f}
- for Boolean true and false value, respectively. For example, the
- ``less-than'' function is named @code{<}.
- Its result is true if the first argument is less than the second (or, if
- there are more than two arguments, that they are in increasing order):
- @example
- (< 3 4) @result{} #t
- (< -3 -4) @result{} #f
- (< 2 3 5 7 11)) @result{} #t
- @end example
- The @code{if} special form takes two or three sub-expressions:
- It evaluates the first expression.
- If that is true it evaluates the second expression;
- otherwise it evaluates the third expression, if provided:
- @example
- (if (< 3 4) (+ 5 5) (+ 5 6)) @result{} 10
- @end example
- We call @code{if} a special form rather than a function,
- because for a function all the arguments are evaluated before the
- function is called, but in a special form that is not neceassarily the case.
- In addition to @code{#t} any value except @code{#f}
- (and the Kawa-specific @code{#!null})
- counts as ``true'' when evaluating the first expression of an @code{if}.
- Unlike C or JavaScript both (zero) and @code{""} (the empty string) are true:
- @example
- (if 0 (+ 5 5) (+ 5 6)) @result{} 10
- @end example
- You can use @code{and}, @code{or},
- and @code{not} to create complex boolean expressions.
- Of these @code{and} and @code{or}
- are special forms that only evaluate as many of the sub-expressions as needed.
- @example
- (if (not (and (>= i 0) (<= i 9)))
- (display "error"))
- @end example
- You can use the @code{cond} form as an alternative to
- @code{if}:
- @example
- (cond ((< 3 3) 'greater)
- ((> 3 3) 'less)
- (else ’equal)) @result{} equal
- @end example
- The null value (written as @code{#!null} in Kawa or @code{null} in Java)
- is also considered as false.
- @node Tutorial - Numbers
- @section Numbers
- @subheading Exact integers and fractions
- Kawa has the usual syntax for decimal integers.
- Addition, subtraction, and multiplication
- are written using the usual @code{+},
- @code{-}, and @code{*},
- but these are all prefix functions that take a variable number of arguments:
- @example
- (+ 1 2 3) @result{} 6
- (- 10 3 4) @result{} (- (- 10 3) 4) @result{} 3
- (* 2 -6) @result{} -12
- @end example
- Kawa has arbitrary-precision integers.
- Let us implement the @uref{http://en.wikipedia.org/wiki/Factorial,factorial} function.
- Type in the following (we'll look at the syntax shortly):
- @example
- #|kawa:1|# @kbd{(define (factorial x)}
- #|(---:2|# @kbd{ (if (< x 1) 1}
- #|(---:3|# @kbd{ (* x (factorial (- x 1)))))}
- @end example
- (The prompt changes to indicate a continuation line.)
- This binds the name @code{factorial}
- to a new function, with formal parameter @code{x}.
- This new function is immediately compiled to Java bytecodes,
- and later a JIT compiler may compile it to native code.
- A few tests:
- @example
- #|kawa:4|# @kbd{(list (factorial 3) (factorial 4))}
- (6 24)
- #|kawa:5|# @kbd{(factorial 30)}
- 265252859812191058636308480000000
- @end example
- @subheading Floating-point real numbers
- Given what was said above about being able to add, subtract and multiply integers,
- the following may be unexpected:
- @example
- #|kawa:1|# @kbd{(/ 2 3)}
- 2/3
- #|kawa:2|# @kbd{(+ (/ 1 3) (/ 2 3))}
- 1
- @end example
- In many languages, dividing two integers, as 2/3, would result in 0. At best,
- the result would be a floating point number, similar to 0.666667. Instead,
- Kawa has a @emph{rational} number type, which holds the results of divisions
- @emph{exactly}, as a proper fraction. Hence, adding one third to two thirds
- will always result in exactly one.
- Floating-point real numbers are known in Kawa as @emph{inexact} numbers, as
- they cannot be stored exactly. Consider:
- @example
- #|kawa:3|# @kbd{(exact? 2/3)}
- #t
- #|kawa:4|# @kbd{(exact? 0.33333333)}
- #f
- #|kawa:5|# @kbd{(exact->inexact 2/3)}
- 0.6666666666666666
- @end example
- The first two examples check numbers for being @code{exact?}; there is a
- corresponding @code{inexact?} test. The last shows how an exact number can be
- converted to an inexact form.
- Numbers are converted between exact and inexact versions when required
- within operations or procedures:
- @example
- #|kawa:6|# @kbd{(+ 0.33333333 2/3)}
- 0.9999999966666666
- #|kawa:7|# @kbd{(inexact? (+ 0.33333333 2/3))}
- #t
- #|kawa:8|# @kbd{(sin 2/3)}
- 0.618369803069737
- @end example
- @subheading Complex numbers
- A @emph{complex} number is made from two parts: a @emph{real} part and an
- @emph{imaginary} part. They are written @code{2+3i}. A complex number can
- be manipulated just like other numbers:
- @example
- #|kawa:9|# @kbd{(+ 2+3i 5+2i)}
- 7+5i
- #|kawa:10|# @kbd{(* 2+3i 4-3i)}
- 17+6i
- #|kawa:11|# @kbd{(integer? (+ 2+3i -3i))}
- #t
- @end example
- Notice how in the last example the result is an integer, which Kawa recognises.
- Kawa also includes @ref{Quaternions,quaternion} numbers.
- @subheading Units and dimensions
- In many applications, numbers have a @emph{unit}. For example, 5 might be
- a number of dollar bills, a weight on a scale, or a speed. Kawa enables us
- to represent numbers as @emph{quantities}: numbers along with their unit.
- For example, with weight, we might measure weight in pounds and ounces,
- where an ounce is 1/16 of a pound.
- Using Kawa, we can define units for our weight measurements, and specify the
- units along with numbers:
- @example
- #|kawa:12|# @kbd{(define-base-unit pound "Weight")}
- #|kawa:13|# @kbd{(define-unit ounce 0.0625pound)}
- #|kawa:14|# @kbd{3pound}
- 3.0pound
- #|kawa:15|# @kbd{(+ 1pound 5ounce)}
- 1.3125pound
- @end example
- In this example we define a base unit, the pound, and a unit based on it, the
- ounce, which is valued at 0.0625 pounds (one sixteenth). Numbers can then be
- written along with their unit (making them quantities). Arithmetic is possible
- with quantities, as shown in the last line, and Kawa will do the smart thing
- when combining units. In this case, 1 pound and 5 ounces is combined to make
- 1.3125 pounds.
- @node Tutorial - Functions
- @section Functions
- To declare a new function use @code{define},
- which has the following form:
- @example
- (define (@var{function-name} @var{parameter-names}) @var{body})
- @end example
- This creates a new function named @var{function-name},
- which takes @var{parameter-names} as parameters.
- When the function is called, the @var{parameter-names}
- are initialized with the actual arguments. Then @var{body}
- is evaluated, and its value becomes the result of the call.
- For example, in the @code{factorial} function we looked at recently,
- the @var{function-name} is @code{factorial},
- and the @var{parameter-names} is @code{x}:
- @example
- (define (factorial x)
- (if (< x 1) 1
- (* x (factorial (- x 1)))))
- @end example
- @subheading Anonymous functions
- @c If you just evaluate a function nameA function is an actual variable you can evaluate xxx
- An @emph{anonymous} function is simply a function which does not have a name.
- We define an anonymous function using a @dfn{lambda expression}, which has
- the following form:
- @example
- (lambda (@var{parameter-names}) @var{body})
- @end example
- The lambda expression has the @var{parameter-names} and @var{body} of a
- function, but it has no name. What is the point of this?
- An important example is creating a function to act on a list, perhaps using
- @code{map}. The @code{map} function takes two parameters: the first is a
- function which takes a value and returns a value; the second is a list. Here,
- we want to double every number in the list.
- The usual way of doing this is to create a named function, called
- @code{double}, and then apply it to a list:
- @example
- #|kawa:1|# @kbd{(define (double x)}
- #|.....2|# @kbd{ (* 2 x))}
- #|kawa:3|# @kbd{(map double (list 1 2 3 4 5))}
- (2 4 6 8 10)
- @end example
- Instead, anonymous functions make it easy to create a function to work on a
- list, without having to define it in advance:
- @example
- #|kawa:4|# @kbd{(map (lambda (x) (* 2 x)) (list 1 2 3 4 5))}
- (2 4 6 8 10)
- #|kawa:5|# @kbd{(define y 3)}
- #|kawa:6|# @kbd{(map (lambda (x) (* x y)) (list 1 2 3 4 5))}
- (3 6 9 12 15)
- @end example
- The first example shows the double example rewritten as an anonymous function.
- The second example shows how the anonymous function can be changed to
- fit the place in which it is used: here, the value of @var{y} determines the
- value by which the list values are multiplied.
- Notice that we can name our anonymous functions, in just the same way we
- name any value in Kawa, using @code{define}:
- @example
- (define double
- (lambda (n)
- (* 2 n)))
- @end example
- although more frequently we use the short-hand for defining functions, which we
- have already met:
- @example
- (define (double n)
- (* 2 n))
- @end example
- Anonymous functions are ``first-class values'' in Kawa, and can be passed to
- other functions as arguments (like we did with @code{map}), and they can even
- be created and returned by functions as results.
- @subheading Optional, rest and keyword parameters
- You can declare a function that takes optional arguments,
- or a variable number of arguments. You can also use keyword parameters.
- The following function illustrates the use of @emph{optional} arguments. The function
- identifies an optional argument @code{z}: if the function is called with 3 arguments, @code{z}
- will be bound to the third value, otherwise it will be @code{#f}.
- @example
- (define (addup x y #!optional z)
- (if z
- (+ x y z)
- (+ x y)))
- @end example
- The following examples show @code{addup} applied to 2, 3 and invalid arguments. It is an error to
- pass just one argument or more than three: @code{x} and @code{y} are compulsory, but @code{z} is
- optional.
- @example
- #|kawa:12|# @kbd{(addup 1 2)}
- 3
- #|kawa:13|# @kbd{(addup 1 2 3)}
- 6
- #|kawa:14|# @kbd{(addup 1)}
- /dev/stdin:14:1: call to 'addup' has too few arguments (1; min=2, max=3)
- #|kawa:15|# @kbd{(addup 1 2 3 4)}
- /dev/stdin:15:1: call to 'addup' has too many arguments (4; min=2, max=3)
- @end example
- In this example, a better way to define the function would be to include a
- default value for @code{z}, for when its value is not given by the caller.
- This is done as follows, with the same behavior as above:
- @example
- (define (addup x y #!optional (z 0))
- (+ x y z))
- @end example
- You can include as many optional parameters as you wish, after the @code{#!optional}.
- @emph{Rest} arguments are an alternative way to pass an undefined number of
- arguments to a function. Here is @code{addup} written with rest arguments,
- notice the variable name after the . (dot):
- @example
- (define (addup x y . args)
- (+ x y (apply + args)))
- @end example
- The @code{args} are simply a list of all remaining values. The following now all work, as the
- function only requires a minimum of two numbers:
- @example
- #|kawa:4|# @kbd{(addup 1 2)}
- 3
- #|kawa:5|# @kbd{(addup 1 2 3)}
- 6
- #|kawa:6|# @kbd{(addup 1 2 3 4 5 6 7 8)}
- 36
- @end example
- An alternative way to identify the rest args is with @code{#!rest}:
- @example
- (define (addup x y #!rest args)
- (+ x y (apply + args)))
- @end example
- Finally, it can be useful to identify parameters by name and, for this, Kawa
- provides @emph{keyword} arguments. Consider the following function:
- @example
- #|kawa:38|# @kbd{(define (vector-3d #!key x y z)}
- #|.....39|# @kbd{ (vector x y z))}
- #|kawa:40|# @kbd{(vector-3d #:x 2 #:z 3 #:y 4)}
- #(2 4 3)
- @end example
- @code{vector-3d} is defined with three keyword arguments: @code{x}, @code{y}, and @code{z}. When the
- function is called, we identify the name for each value by writing @code{#:} at the start of the name.
- This allows us to write the arguments in any order. Keyword parameters can also be given default
- values, as with optional parameters. Keyword parameters with no default value, and no value in the caller,
- will get the value @code{#f}.
- In the caller, keywords are symbols with @code{#:} at the front (or @code{:} at
- the end): @ref{Keywords,read more here}.
- All these extended types of arguments are available both for ``named'' and for ``anonymous'' functions.
- Optional, rest and keyword arguments can be mixed together, along with the usual arguments.
- For details @ref{Extended formals,read more here.}
- @node Tutorial - Variables
- @section Variables
- You can declare a variable using a @code{!} form.
- This takes a variable name, and an expression. It declares a new
- variable with the given name, and gives it the value of the expression.
- @example
- #|kawa:1|# @kbd{(! binary-kilo 1024)}
- #|kawa:2|# @kbd{(! binary-mega (* binary-kilo binary-kilo))}
- #|kawa:3|# @kbd{binary-mega}
- 1048576
- @end example
- If you prefer, you can use @code{define} instead of @code{!}:
- @example
- #|kawa:1|# @kbd{(define binary-kilo 1024)}
- #|kawa:2|# @kbd{(define binary-mega (* binary-kilo binary-kilo))}
- #|kawa:3|# @kbd{binary-mega}
- 1048576
- @end example
- The advantage of using @code{define} is that it is portable
- to other Scheme implementations.
- The advantages of using @code{!} is that it is shorter;
- it generalizes to patterns (see later);
- and it guards against accidentally ``shadowing'' a variable
- by a nested variable with the same name.
- A @code{!} (or @code{define}) typed into the command-line
- defines a top-level variable.
- You can also declare local variables, which are variables defined for
- a given block of code. For example, in the following code @code{let}
- is used to set up a local binding of @code{x} to 3: this does not affect
- the outer binding of @code{x} to 5:
- @example
- (define x 5)
- (let ((x 3))
- (display x)) @result{} 3
- (display x) @result{} 5
- @end example
- Alternative forms for defining local variables are
- @code{let}, @code{let*}, or @code{letrec}/@code{letrec*}.
- The differences are in the order in which definitions are made.
- @code{let} evaluates all its definitions in the environment holding
- at the start of the @code{let} statement. In the following example,
- the local variables are defined using values from the global variables:
- @example
- (define x 5)
- (define y 2)
- (let ((x (+ 2 y)) ; uses value of global y, i.e. 2
- (y (+ 3 x))) ; uses value of global x, i.e. 5
- (display (list x y))) @result {} (4 8)
- @end example
- @code{let*} instead evaluates each definition in the environment holding
- at the start of the @code{let*} statement, along with all @emph{previous}
- local definitions. In the following example, @code{y} is now defined with the
- @emph{local} value of @code{x}:
- @example
- (define x 5)
- (define y 2)
- (let* ((x (+ 2 y)) ; uses value of global y, i.e. 2
- (y (+ 3 x))) ; uses value of local x, i.e. 4
- (display (list x y))) @result {} (4 7)
- @end example
- @code{letrec/letrec*} are similar, but allow the definition of recursive
- functions:
- @example
- (letrec ((is-even? (lambda (n) (and (not (= 1 n))
- (or (zero? n)
- (is-odd? (- n 1))))))
- (is-odd? (lambda (n) (and (not (zero? n))
- (or (= 1 n)
- (is-even? (- n 1)))))))
- (display (is-even? 11))) @result {} #f
- @end example
- @node Tutorial - Pictures
- @section Composable pictures
- The @code{pictures} library lets you create geometric shapes
- and images, and combine them in interesting ways.
- You first need to import the library:
- @example
- (import (kawa pictures))
- @end example
- The easiest way to use and learn the library
- is with a suitable REPL, where you can type
- expressions that evaluate to pictures values,
- and view the resulting pictures directly on the console.
- The easiest way is to start the @code{kawa} command with the @code{-w}
- flag. Alternatively, you can use
- a @ref{Using DomTerm,DomTerm}-based terminal emulator
- such as @code{qtdomterm} (which is shown in the image below),
- and then the @code{kawa} command.
- @image{images/domterm-pictures-1}
- The above image shows two simple examples: a filled
- circle (radius 30 pixels, color magenta), and a non-filled rotated rectangle
- (color maroon 3-pixel wide strokes).
- See @ref{Composable pictures} for details and more examples.
- @subheading Shapes and coordinates
- A @dfn{shape} is a geometrical figure consisting of
- one or more curves and lines. One kind of shape is a circle;
- you can create one with the @code{circle} procedure,
- specifying the radius in ``pixels''.
- @example
- #|kawa:1|# @kbd{(import (kawa pictures))}
- #|kawa:2|# @kbd{(circle 30)}
- @image{images/fill-circ-1}
- @end example
- It you print a shape, it will show it as a thin black curve.
- A @dfn{point} has two real-numbered parts: the point's x-coordinate,
- and its y-coordinate.
- The x-coordinate increases as you move right along the page/screen,
- while the y-coordinate increases as you move @emph{down}.
- (Unlike traditional mathematics, where
- the y-coordinate increases as you go up.)
- The unit distance is one ``pixel'', which is defined as CSS or HTML.
- You can create a point with @code{&P} operator.
- For example:
- @example
- &P[30 20]
- @end example
- is a point 30 pixels right and 20 pixels down from the origin point.
- To create a circle centered on that point do @code{(center 30 &P[30 20])}.
- The expression @code{(rectangle &P[10 20] &P[50 40])} creates
- a rectangle whose upper left corner is (10,20) and whose
- lower right corner is (50,40).
- A @dfn{dimension} is a pair, a width and height,
- and is written:
- @example
- &D[@var{width} @var{height}]
- @end example
- In addition to being used for sizes,
- a dimension is also used for relative offsets.
- For example, the previous rectangle could also
- be written @code{(rectangle &P[10 20] &D[40 20])}.
- You can use @code{line} to create a line.
- More generally, if you specify @var{n} points you get a
- @dfn{polyline} of @var{n-1} line segments:
- @example
- #|kawa:3|# @kbd{(line &P[10 20] &P[50 60] &P[90 0])}
- @image{images/polyline-1}
- @end example
- The same line using dimensions for relative offsets:
- @example
- #|kawa:4|# @kbd{(line &P[10 20] &D[40 20] &D[40 -60])}
- @end example
- A @dfn{closed shape} is one whose end point is the same as its start point.
- The @code{polygon} function creates one using straight line segments
- @example
- #|kawa:5|# @kbd{(polygon &P[10 20] &P[50 60] &P[90 0])}
- @image{images/polygon-1}
- @end example
- @subheading Colors and filling
- You can override the default color (black) using the
- @code{with-paint} procedure, which takes a color and a picture
- to produce a new picture:
- @example
- #|kawa:6|# @kbd{(with-paint 'red (circle 32))}
- @end example
- The first argument can be either one of the standard CSS/HTML5 color
- names (such as @code{'red} or @code{'medium-slate-blue}),
- or an integer representing an sRGB color, usually written
- as a hex literal in the form @code{#xRRGGBB}:
- @example
- #|kawa:7|# @kbd{(with-paint #x0808FF (circle 32))}
- @end example
- The name @code{with-paint} is because the first argument
- can be not just a color, but a general ``paint'', such as
- a gradient or a background image. However, we won't go into that.
- If the shape is closed, you can ``fill'' its inside:
- @example
- (fill (circle 32))
- @end example
- You can change the color using @code{with-paint}:
- @example
- (with-paint 'goldenrod (fill (circle 32)))
- @end example
- or as an extra argument to @code{fill}:
- @example
- (fill 'goldenrod (circle 32))
- @end example
- draw TODO
- @subheading Images
- An image is a picture represented as a rectangular grid of color values.
- It may be a photograph from a camera, or be created by a painting
- program like Photoshop or gimp.
- You can use @code{image-read} to read an image from a file,
- typically a @code{.png} or @code{.jpg} file.
- @example
- #|kawa:10|# @kbd{(define img1 (image-read "http://pics.bothner.com/2013/Cats/06t.jpg"))}
- #|kawa:11|# @kbd{img1}
- @image{images/image-cat-1a}
- @end example
- @subheading Transforms TODO
- @example
- #|kawa:12|# @kbd{(scale 0.6 (rotate 30 img1))}
- @image{images/image-cat-1b}
- @end example
- @subheading Combining and adjusting pictures TODO
- @subheading Using and combining pictures TODO
- @node Tutorial - Sequences
- @section Lists and sequences
- A @dfn{sequence} is a generalized array or list:
- Zero or more values treated as a compound value.
- Sequences have certain common operations, including indexing and iteration.
- (@i{Technical note:} Sequences generally implement the @code{java.util.List}
- interface, but Kawa will also treat strings and native
- Java arrays as sequences.)
- @subheading Lists
- In traditional Lisp-family languages, the @dfn{list} is the
- most important kind of sequence.
- (Don't confuse Java's @code{List} interface with Kawa's use of the
- word @i{list}. They're related, in that a Kawa ``list'' implements
- the @code{List} interface, so any @i{list} is also @code{List},
- but not vice versa.)
- A list is implemented as a chain of linked @dfn{pairs}.
- You can create a constant list by quoting a parenthesized list:
- @example
- '(3 4 (10 20 30) "a string")
- @end example
- See @ref{Lists} for details and operations.
- @subheading Vectors
- A @dfn{vector} is a sequence that is implemented by storing the elements
- side-by-side in memory.
- A vector uses less space than a list of the same length,
- and is generally more efficient than a list.
- To create a vector you can use a bracketed list:
- @example
- (! vec1 ['A 'B 'C 'D 'E 'F])
- @end example
- This creates a vector of 6 symbols and binds it to @code{vec1}.
- To select an element you can use the traditional
- @code{vector-ref} procedure:
- @example
- (vector-ref vec1 3) @result{} 'D
- @end example
- Alternatively, in Kawa you can use function-call notation:
- @example
- (vec1 3) @result{} 'D
- @end example
- You can also create a vector using the traditional @code{vector} constructor:
- @example
- (! vec2 (vector 'A 'B 'C 'D 'E 'F))
- @end example
- There is one important difference between @code{vec1} and @code{vec2}:
- You can modify @code{vec2} by changing some or all of its elements.
- You can't do that for @code{vec1}.
- (We say that @code{vec1} is an @dfn{immutable} or @dfn{constant} vector,
- while @code{vec1} is a @dfn{mutable} or @dfn{modifiable} vector.)
- To change an element use either the traditional @code{vector-set!}
- procedure, or function-call notation:
- @example
- (vector-set! vec2 2 'Y)
- (set! (vec2 4) 'Z)
- vec2 @result{} ['A 'B 'Y 'D 'Z 'F]
- (vector-set! vec1 2 'Y) @result{} @i{throws exception}
- @end example
- See @ref{Vectors} for details and operations.
- @subheading Java arrays and primitive vectors
- See @ref{Array operations, Using Java arrays} for examples.
- @subheading Indexing of general sequences
- You can use function-call notation to index a generalized sequence,
- whether it is a list, vector, any @code{java.util.List},
- native Java array, or string:
- @example
- ((list 'A 'B 'C 'D) 2) @result{} 'C
- ("abcdef" 3) @result{} @result{}
- (! farr (float[] 1.5 3 4.5)) ;; native Java array
- (farr 2) @result{} 4.5
- @end example
- Note that indexing a list with an index @var{i} will be slow, since it
- has to step through the list @var{i} times. (So don't do that!)
- @subheading Ranges
- A @dfn{range} is a sequence of numbers in order,
- spaced uniformly apart. Usually, these are (exact) integers
- that increase by one. The usual notation is:
- @example
- [@var{start} <: @var{end}]
- @end example
- This is the sequence of integers starting with the integer @var{start}
- (inclusive) and ending with the integer @var{end} (exclusive).
- For example @code{[3 <: 7]} is the sequence @code{[3 4 5 6]}.
- The @samp{<:} is a keyword; the @code{<} is a mnemonic for the
- set of integers that are @code{<} the end value 6.
- You can also use @code{<=:} if you want to include the upper bound:
- @code{[4 <=: 8]} is @code{[4 5 6 7 8]}.
- You can use @code{>=:} or @code{>:} for a decreasing range.
- @code{[5 >=: 1]} or @code{[5 >: 0]} both evaluate to @code{[5 4 3 2 1]}.
- You can also specifify a step value: @code{[1 by: 2 <=: 9]},
- which evaluates to @code{[1 3 5 7 9]}.
- (@ref{Ranges,Details here}.)
- @subheading Using vector and ranges indexes
- If an index is a sequence of integers,
- the result is a new sequence (of the same type)
- selecting only the elements matching the index values.
- For example:
- @example
- #|kawa:2|# @kbd{(vec1 [3 5 2])}
- #(D F C)
- @end example
- In general, @code{((V1 V2) I)} is @code{(V1 (V2 I))}.
- You can use a range to create a slice - a contiguous subset of a list.
- @example
- #|kawa:3|# @kbd{(vec1 [2 <: 6])}
- #(C D E F)
- @end example
- A range is different from a vector integer in that you can use
- a range as the index in the LHS of a set!:
- @example
- #|kawa:4|# @kbd{(set! (vec1 [2 <: 4]) #(a b c d e))}
- #|kawa:5|# @kbd{vec1}
- #(A B a b c d e E F)
- @end example
- Notice how the number of replaced elements can be different
- then the number of elements in the replacement value.
- I.e. you can do insertion and deletion this way.
- @example
- #|kawa:7|# @kbd{(! str1 (string-copy "ABCDEF"))}
- #|kawa:8|# @kbd{(set! (str1 [2 <: 5]) "98")}
- AB98F
- @end example
- @node Tutorial - Objects
- @section Creating and using objects
- An @dfn{object} is a value that has the following features:
- @itemize
- @item
- class - each object is an instance of a specific class,
- making it part of the class hierarchy, which is an important
- aspect of the type system;
- @item
- properties - various fields and methods, depending on the class;
- @item
- identity - it is distinct from all other objects,
- even if all the properties are the same.
- @end itemize
- We later discuss @ref{Tutorial - Classes,how to write a new class}.
- Here we assume you're using an existing class, which
- could be written in Java or Scheme.
- @subheading Creating a new object
- To create a new object of class @code{T} you call @code{T} as if it
- were a function, passing it the various constructor arguments:
- @example
- (java.io.File "src" "build.xml")
- @end example
- If there are keyword arguments they are used to initialize
- the corresponding named properties:
- @example
- (! button1 (javax.swing.JButton text: "Do it!" tool-tip-text: "do it"))
- @end example
- This create a new @code{JButton} object (using @code{JButton}'s
- default constructor), and sets the @code{text} and @code{tool-tip-text}
- properties (by calling @code{JButton}'s @code{setText}
- and @code{setToolTipText} methods).
- If there are constructor arguments, they must come before the keywords.
- For objects that have components or elements, you
- can list these at the end. For example:
- @example
- (java.util.ArrayList 11 22 33)
- @end example
- This creates a fresh @code{java.util.ArrayList} (using the
- default constructor), and then calls the @code{add} method 3 times.
- If you prefer you can use the @code{make} procedure,
- but that only handle simple constructor calls:
- @example
- (make java.io.File "src" "build.xml")
- @end example
- See @ref{Allocating objects} for details.
- @subheading Calling instance methods
- Given an object @var{obj} of a class that has a method @var{meth},
- you can call it with argumens @var{v1} ... @var{v2} using @ref{Colon notation}:
- @example
- (@var{obj}:@var{meth} @var{v1} ... @var{v2})
- @end example
- For example:
- @example
- (button1:paintImmediately 10 10 30 20)
- @end example
- If you prefer, you can use the @code{invoke} procedure,
- normally with a quoted method name:
- @example
- (invoke button1 'paintImmediately 10 10 30 20)
- @end example
- You need to use @code{invoke} (rather than colon notation)
- if @var{obj} is a @code{Class} or a type expression, or its class
- implements @code{gnu.mapping.HasNamedParts}.
- See @ref{Method operations} for details.
- @subheading Accessing properties
- If @var{obj} has a field or property named @var{fld} you can also use colon
- notation:
- @example
- @var{obj}:@var{fld}
- @end example
- You use the same syntax whether @var{fld} is an actual field in the
- object, or a @dfn{property} (in the Java Beans sense). The
- latter is implemented using a getter/setter pair:
- Methods named @code{get@var{F}} and @code{set@var{F}}, respectively.
- For example:
- @example
- button1:tool-tip-text
- @end example
- is equivalent to:
- @example
- (button1:getToolTipText)
- @end example
- You can also change a field or property using colon notation:
- @example
- (set! @var{obj}:@var{fld} @var{value})
- @end example
- For example:
- @example
- (set! button1:tool-tip-text "really do it!")
- @end example
- This is equivalent to:
- @example
- (button1:setToolTipText "really do it!")
- @end example
- Instead of colon notation, you can use the @code{field} procedure.
- See @ref{Field operations} for details.
- @subheading Static fields and methods
- Kawa views static properties and methods as
- properties and methods of the class itself.
- To call a static method use the syntax:
- @example
- (@var{clas}:@var{meth} @var{v1} ... @var{vn})
- @end example
- For example:
- @example
- (java.math.BigDecimal:valueOf 12345 2) @result{} 123.45
- @end example
- To access a static field do @code{@var{clas}:@var{fld}}. For example:
- @example
- java.awt.Color:RED
- @end example
- You can also use the @code{static-field} and @code{invoke-static} procedures.
- @node Tutorial - Types
- @section Types and declarations
- A @dfn{type} is a named value for a set
- of objects with related properties.
- For example, @code{vector} is the type for standard Scheme vectors.
- You can use a type to specify that a variable can only have
- values of the specified type:
- @example
- #|kawa:5|# @kbd{(define v ::vector #(3 4 5))}
- #|kawa:6|# @kbd{v}
- #(3 4 5)
- #|kawa:7|# @kbd{(set! v 12)}
- /dev/stdin:7:1: warning - cannot convert literal (of type gnu.math.IntNum) to vector
- Value (12) for variable 'v' has wrong type (gnu.math.IntNum) (gnu.math.IntNum cannot be cast to gnu.lists.FVector)
- at atInteractiveLevel$7.run(stdin:7)
- at gnu.expr.ModuleExp.evalModule(ModuleExp.java:302)
- at kawa.Shell.run(Shell.java:275)
- at kawa.Shell.run(Shell.java:186)
- at kawa.Shell.run(Shell.java:167)
- at kawa.repl.main(repl.java:870)
- Caused by: java.lang.ClassCastException: gnu.math.IntNum cannot be cast to gnu.lists.FVector
- ... 6 more
- @end example
- Using a type specification catches errors, and makes your programs
- more readable. It can also allow the Kawa compiler to generate code
- that runs faster.
- You can use a type to check that a value is an instance of the type,
- using either the @code{instance?} function:
- @example
- (instance? #(3 4 5) vector) @result{} #t
- (instance? '(3 4 5) vector) @result{} #f
- @end example
- As a convenience, you can use a type-name followed by a ``@code{?}'':
- @example
- (@var{type}? @var{val}) == (instance? @var{val} @var{type})
- @end example
- You can ``call'' a type as if it were a function,
- which constructs a new instance of the type.
- The following example shows how to construct a normal Scheme vector,
- and a Java array of ints:
- @example
- #|kawa:1|# @kbd{(vector)}
- #()
- #|kawa:2|# @kbd{(instance? (vector) vector)}
- #t
- #|kawa:3|# @kbd{(define x (int[] 1 2 3))}
- #|kawa:4|# @kbd{x}
- [1 2 3]
- #|kawa:5|# @kbd{(instance? x int[])}
- #t
- @end example
- A fully-qualified Java class is a type name.
- So are the names of Java primitive types.
- So are Java array types, as shown above.
- e.g. a JFrame is constructed by using its class name as a function:
- @example
- #|kawa:6|# @kbd{(javax.swing.JFrame)}
- javax.swing.JFrame[frame0,0,25,0x0,invalid,hidden,layout=java.awt.BorderLayout,
- title=,resizable,normal,defaultCloseOperation=HIDE_ON_CLOSE,
- rootPane=javax.swing.JRootPane[,0,0,0x0,invalid,
- layout=javax.swing.JRootPane$RootLayout,alignmentX=0.0,alignmentY=0.0,border=,
- flags=16777673,maximumSize=,minimumSize=,preferredSize=],rootPaneCheckingEnabled=true]
- @end example
- A type is a true run-time value:
- @example
- (define mytypes (list vector list string))
- (instance? #(3 4 5) (car mytypes) @result{} #t
- @end example
- The @code{define-alias} form is useful for defining shorter names
- for types, like a generalization of Java's @code{import} statement:
- @example
- (define-alias jframe javax.swing.JFrame)
- @end example
- @node Tutorial - Exceptions and errors
- @section Exceptions and errors
- Kawa supports the exception framework and forms
- from R6RS and R7RS. See @ref{Exceptions} for details.
- @subheading Native exception handling
- You can also work with native Java exceptions at a low level.
- The @code{primitive-throw} procedure throws a @code{Throwable} value.
- It is implemented just like Java's @code{throw}.
- @example
- (primitive-throw (java.lang.IndexOutOfBoundsException "bad index"))
- @end example
- You can catch an exception with the @code{try-catch} syntax. For example:
- @example
- (try-catch
- (do-a-bunch-of-stuff)
- (ex java.lang.Throwable
- (format #f "caught ~a~%~!" ex)
- (exit)))
- @end example
- A @code{try-finally} does the obvious:
- @example
- (define (call-with-port port proc)
- (try-finally
- (proc port)
- (close-port port)))
- @end example
- Both @code{try-catch} and @code{try-finally} are
- expression forms that can return values, while the corresponding
- Java forms are statements that cannot return values.
- @node Tutorial - Classes
- @section Classes
- See @ref{Defining new classes} for the gory details; no tutorial yet.
- @node Tutorial - Other Java features
- @section Other Java features
- @subheading Import
- The @code{import} form can be used to avoid having to write
- fully-qualified class names. For example:
- @example
- (import (class java.util
- Map
- (HashMap HMap)))
- @end example
- This defines aliases for two classes in the @code{java.util} package,
- one with renaming:
- @code{Map} is an alias for @code{java.util.Map},
- and @code{HMap} is an alias for @code{java.util.HashMap}.
- The @code{class} keyword is needed because the @code{import}
- form is also used for Kawa's module system.
- See @ref{importing-class-names} and @ref{Importing} for details.
- @subheading Synchronized blocks
- You can use a @code{synchronized} expression:
- @example
- (synchronized obj form1 ... formn)
- @end example
- This waits until it can get an exclusive lock on @var{obj}
- and then evaluates @var{form1} through @var{formn}.
- Unlike Java, this is an expression and returns the value of @var{formn}.
- @subheading Annotations
- You can write annotation declarations - see @ref{Annotations} for details.
- Kawa does not yet support annotations on types,
- or declaring new annotation classes.
- @part Reference Documentation
- @node Running
- @chapter How to start up and run Kawa
- The easiest way to start up Kawa is to run the @samp{kawa} program.
- This finds your Java interpreter, and sets up @samp{CLASSPATH} correctly.
- If you have installed Kawa such that @code{$PREFIX/bin} is in your @code{$PATH},
- just do:
- @example
- kawa
- @end example
- However, @samp{kawa} only works if you have a Unix-like environment.
- On some platforms, @samp{kawa} is a program that uses the GNU
- @samp{readline} library to provide input line editing.
- To run Kawa manually, you must start a Java Virtual Machine.
- How you do this depends on the Java implementation.
- For Oracle's JDK, and some other implementations, you must have the
- Java evaluator (usually named @code{java}) in your @code{PATH}.
- You must also make sure that the @code{kawa/repl.class} file,
- the rest of the Kawa packages, and the standard Java
- packages can be found by searching CLASSPATH.
- @xref{Running Java}.
- Then you do:
- @example
- java kawa.repl
- @end example
- In either case, you will then get the @samp{#|kawa:1|#} prompt,
- which means you are
- in the Kawa read-eval-print-loop. If you type a Scheme
- expression, Kawa will evaluate it. Kawa will then print the
- result (if there is a non-"void" result).
- @menu
- * Options:: Command-line arguments
- * Scripts:: Running Command Scripts
- * REPL Console:: The REPL (read-eval-print-loop) console
- * Exiting:: Exiting Kawa
- * Compiling:: Compiling to byte-code
- @end menu
- @node Options, Scripts, Running, Running
- @section Command-line arguments
- @cindex options
- You can pass various flags to Kawa, for example:
- @example
- kawa -e '(display (+ 12 4))(newline)'
- @end example
- or:
- @example
- java kawa.repl -e '(display (+ 12 4))(newline)'
- @end example
- Either causes Kawa to print @samp{16}, and then exit.
- At startup, Kawa executes an init file from the user's home
- directory.
- The init file is named @code{.kawarc.scm} on Unix-like systems
- (those for which the file separator is @code{'/'}),
- and @code{kawarc.scm} on other systems.
- This is done before the read-eval-print loop
- or before the first @code{-f} or @code{-c} argument. (It is not run
- for a @code{-e} command, to allow you to set options to override
- the defaults.)
- @subsection Argument processing
- Kawa processes the command-line arguments in order.
- Options (which either start with @samp{-} or contain a @samp{=})
- may ``use up'' one or more command arguments.
- Some of the options (@samp{-c}, @samp{-e}, @samp{-f}, @samp{-s},
- @samp{-C}, @code{-w}, @samp{--}, @code{--browse-manual})
- are @dfn{action options}; others set various properties.
- When all the command-line arguments have been ``used up''
- and if no action options have been seen,
- then Kawa enters an interactive read-eval-print loop.
- (If an action option has been seen, we're done.)
- If the next command-line argument is not an option
- (does not start with @samp{-} nor contains a @samp{=})
- then we're done if we've seen an action option (and the
- last action option wasn't preceded by @code{--with-arg-count}).
- (Presumably any remaining arguments were command-line-arguments
- used by the action option.)
- Otherwise, the first remaining argument names either a
- file that is read and evaluated, or a compiled class.
- In the former case, the whole file is read and compiled as a module
- before being loaded (unlike the @code{-f} flag which reads and
- evaluates the file command by command.)
- If the argument is the fully-qualified name of a class,
- then the class is loaded, an instance allocated,
- and its @code{run} method invoked. If the class was compiled from
- a Kawa Scheme module, then invoking @code{run} has the
- effect of evaluating the module body.
- The @code{command-line-arguments} vector is set to any remaining
- arguments after the file/class name.
- (This can be overridden with the @code{--with-arg-count} option.
- Command-line processing continues if there are any further arguments.)
- @subsection General options
- @table @code
- @item -e @var{expr}
- Kawa evaluates @var{expr}, which contains one or more Scheme expressions.
- Does not cause the @code{~/.kawarc.scm} init file to be run.
- @item -c @var{expr}
- Same as @samp{-e @var{expr}}, except that it
- does cause the @code{~/.kawarc.scm} init file to be run.
- @item -f @var{filename-or-url}
- Kawa reads and evaluates expressions from the file named
- by @var{filename-or-url}. If the latter is @samp{-},
- standard input is read (with no prompting). Otherwise,
- it is equivalent to evaluating @samp{(load "@var{filename-or-url}")}.
- The @var{filename-or-url} is interpreted as a URL
- if it is absolute - it starts with a "URI scheme" like @code{http:}.
- @item -s
- @itemx --
- The remaining arguments (if any) are passed to @samp{command-line-arguments}
- and (the @code{cdr} of) @code{(command-line}),
- and an interactive read-eval-print loop is started.
- This uses the same "console" as where you started up Kawa;
- use @samp{-w} to get a new window.
- @item --script @var{filename-or-url}
- @itemx --script@var{N} @var{filename-or-url}
- The global variable @samp{command-line-arguments} is set to the remaining
- arguments (if any).
- Kawa reads and evaluates expressions from the file named
- by @var{filename-or-url}.
- If @code{script} is followed by an integer @var{N},
- then @var{N} lines are skipped first.
- Skipping some initial lines is useful if you want to have a non-Kawa
- preamble before the actual Kawa code.
- One use for this is for Kawa shell scripts (@pxref{Scripts}).
- @item -w
- @itemx -w@var{sub-option}
- Creates a new top-level window, and runs an interactive read-eval-print
- in the new window. See @ref{New-Window}.
- Same as @code{-e (scheme-window #t)}.
- You can specify multiple @samp{-w} options, and also use @samp{-s}.
- @item --help
- Prints out some help.
- @item --version
- Prints out the Kawa version number, and then exits.
- If Kawa was built with a @code{.git} repository present,
- also prints the result of @code{git describe}.
- @anchor{browse-manual-option}
- @item --browse-manual
- @itemx --browse-manual=@var{command}
- Browse a local copy of the documentation (this manual).
- This creates a mini web-server that reads
- from @code{doc/kawa-manual.epub}, which is
- included in the binary distributions, but not built by default from source.
- If no @var{command} is specified, creates a new window or tab
- in your default web browser.
- If @var{command} is a string containing @code{%U},
- then Kawa replaces @code{%U} with a URL that references itself,
- and then executes the resulting command.
- If @var{command} does not contain @code{%U}, then
- @var{command} becomes @var{command}@code{" %U"}.
- For example to use the Firefox browser to browse the manual do either of:
- @example
- kawa --browse-manual=firefox
- kawa --browse-manual="firefox %U"
- @end example
- @item --server @var{portnum}
- Start a server listening from connections on the specified @var{portnum}.
- Each connection using the Telnet protocol causes a new read-eval-print-loop
- to start. This option allows you to connect using any
- Telnet client program to a remote "Kawa server".
- @item --with-arg-count=@var{argc}
- This option is used before an action option (such as @code{-f}).
- The @var{argc} arguments after the action become the
- value of the @code{command-line-arguments} during the action.
- When the action is finished, command-line-processing resumes
- after skipping the @var{argc} arguments.
- For example:
- @example
- $ kawa -f a.scm -f b.scm x y
- @end example
- When evaluating @code{a.scm} the @code{command-line-arguments}
- by default is @emph{all} the remaining arguments: @code{["-f" "b.scm" "x" "y"]}.
- Then @code{b.scm} is evaluated with @code{command-line-arguments}
- set to @code{["x" "y"]}
- @example
- $ kawa --with-arg-count=0 -f a.scm -f b.scm x y
- @end example
- In this case @code{a.scm} is evaluated with @code{command-line-arguments}
- set to the empty vector @code{[]}, and then @code{b.scm} is evaluated with @code{command-line-arguments}
- set to @code{["x" "y"]}
- @example
- $ kawa --with-arg-count=4 -f a.scm -f b.scm x y
- @end example
- In this case @code{a.scm} is evaluated with @code{command-line-arguments}
- set to @code{["-f" "b.scm" "x" "y"]}. Since command-line processing
- skips the arguments specified by @code{--with-arg-count=4},
- in this case @code{b.scm} is not evaluated.
- @end table
- @subsection Options for language selection
- @table @code
- @item --scheme
- Set the default language to Scheme.
- (This is the default unless you select another language,
- or you name a file with a known extension on the command-line.)
- @item --r5rs
- @itemx --r6rs
- @itemx --r7rs
- Provide better compatibility with the specified Scheme standards.
- (This is a work-in-progress.)
- For example @code{--r6rs} aims to disable Kawa extensions
- that conflict with R6RS. It does not aim to disable all extensions,
- only incompatible extensions.
- These extensions disable the colon operator and keyword literals,
- as well as the use of initial @samp{@@} as a splicing operator.
- The ``@code{l}'' exponent suffix of a number literal creates a
- floating-point double, rather than a @code{BigInteger}.
- Selecting @code{--r5rs} makes symbols by default
- case-insensitive.
- @item --elisp
- @itemx --emacs
- @itemx --emacs-lisp
- Set the default language to Emacs Lisp.
- (The implementation is quite incomplete.)
- @item --lisp
- @itemx --clisp
- @itemx --clisp
- @itemx --commonlisp
- @itemx --common-lisp
- Set the default language to CommonLisp.
- (The implementation is @emph{very} incomplete.)
- @item --krl
- Set the default language to KRL. See @ref{KRL}.
- @item --brl
- Set the default language to KRL, in BRL-compatibility mode. See @ref{KRL}.
- @item --xquery
- Set the default language to the draft XML Query language.
- See the @uref{http://www.gnu.org/software/qexo/,Kawa-XQuery page}
- for more information.
- @item --xslt
- Set the default language to XSLT (XML Stylesheet Language Transformations).
- (The implementation is @emph{very} incomplete.)
- See the @uref{http://www.gnu.org/software/qexo/xslt.html,Kawa-XSLT page}
- for more information.
- @item --pedantic
- Try to follow the approprate language specification to the letter,
- even in corner cases, and even if it means giving up some
- Kawa convenience features. This flag so far only affects
- the XQuery parser, but that will hopefully change.
- @end table
- @subsection Options for warnings and errors
- @table @code
- @cindex @code{--warn-@var{whatever}}
- @cindex @code{--no-warn-@var{whatever}}
- @pindex @code{warn-undefined-variable}
- @item --warn-undefined-variable
- Emit a warning if the code references a variable which is neither in
- lexical scope nor in the compile-time dynamic (global) environment.
- This is useful for catching typos.
- (A @code{define-variable} form can be used to silence warnings.
- It declares to the compiler that a variable is to be resolved dynamically.)
- This defaults to on;
- to turn it off use the @code{--no-warn-undefined-variable} flag.
- @pindex warn-unknown-member
- @item --warn-unknown-member
- Emit a warning if the code references a named member (field or method)
- for which there is no match in the compile-time type of the receiver.
- This defaults to on;
- to turn it off use the @code{--no-warn-unknown-member} flag.
- @pindex warn-invoke-unknown-method
- @item --warn-invoke-unknown-method
- Emit a warning if the @code{invoke} function calls a named method
- for which there is no matching method in the compile-time type of the receiver.
- This defaults to the value of @code{--warn-unknown-member},
- to turn it off use the @code{--no-warn-invoke-unknown-method} flag.
- @pindex warn-unused
- @item --warn-unused
- Emit a warning if a variable is unused or code never executed. This defaults
- to on; to turn it off use the @code{--no-warn-unused} flag.
- @pindex warn-uninitialized
- @item --warn-uninitialized
- Warn if accessing an uninitialized variable.
- This defaults to on; to turn it off use the @code{--no-warn-uninitialized} flag.
- @cindex warn-unreachable
- @item --warn-unreachable
- Emit a warning if the code can never be executed. This defaults to on;
- to turn it off use the @code{--no-warn-unreachable} flag.
- @cindex warn-void-used
- @item --warn-void-used
- Emit a warning if an expression depends on an expression
- that is void (always has zero values), including call to @code{void}
- functions and method. Also warn if an expression depends on a
- conditional (@code{if}) that has no ``else'' clause.
- Examples include using the value of @code{set-car!} as
- an argument to a function, or to initialize a variable.
- This defaults to on;
- to turn it off use the @code{--no-warn-void-used} flag.
- @pindex warn-as-error
- @item --warn-as-error
- Treat a compilation warning as if it were an error and halt compilation.
- @item --max-errors=@var{value}
- Print no more than @var{value} errors or warnings (at a time).
- The value @code{-1} removes the limit.
- The initial default is 20. (A single error may so confuse Kawa
- that it prints very many useless error messages.)
- @end table
- An option can be followed by a value, as
- in @code{--warn-invoke-unknown-method=no}.
- For boolean options, the values @code{yes}, @code{true}, @code{on}, or @code{1}
- enable the option, while @code{no}, @code{false}, @code{off},
- or @code{0} disable it.
- You can also negate an option by prefixing it with @code{no-}:
- The option @code{--no-warn-unknown-member}
- is the same as @code{--warn-unknown-member=no}.
- These options can also be used in the module source, using
- @code{module-compile-options} or @code{with-compile-options}.
- (In that case they override the options on the command line.)
- @subsection Options for setting variables
- @table @code
- @item @var{name}=@var{value}
- Set the global variable with the specified @var{name} to the given @var{value}.
- The type of the @var{value} is currently unspecified; the plan is for it
- to be like XQuery's @dfn{untyped atomic} which can be coerced as needed.
- @item @{@var{namespace-uri}@}@var{local-name}=@var{value}
- Set the global variable with the specified namespace uri and
- namespace-local name to the given value.
- @end table
- These options are processed when invoking the @code{kawa}
- application (i.e. the @code{kawa.repl} application).
- If you want a Kawa application compiled with @code{--main}
- to process these these assignments, call the
- @code{process-command-line-assignments} utility function.
- @table @code
- @item -D@var{variable-name}=@var{variable-value}
- Sets the JVM property @var{variable-name} to @var{variable-value},
- using the @code{setProperty} method of @code{java.lang.System}.
- @end table
- @subsection Options for the REPL console
- @table @asis
- @item @code{--console}
- @itemx @code{--no-console}
- Usually Kawa can detect when the standard input port is a ``console''
- or ``terminal'', but these are useful for overriding that detection.
- The @code{--console} flag is useful when the standard input is a pipe,
- but you want to direct Kawa to treat it as an interactive terminal.
- The @code{--no-console} flag was useful for older pre-Java-6
- implementations that did not have the @code{java.lang.Console} class.
- @item @code{console:type=}@var{console-types}
- @itemx @code{console:use-jline=}[@code{yes}|@code{no}]
- @itemx @code{console:jline-mouse=}[@code{yes}|@code{no}]
- See the @ref{REPL Console} section.
- @item @code{console:prompt1=@var{prompt1}}
- @itemx @code{console:prompt2=@var{prompt2}}
- Initialize @ref{input-prompt1,@code{input-prompt1} and @code{input-prompt2}}, respectively.
- @end table
- See also the @code{--output-format} flag.
- @subsection Options for controlling output formatting
- @cindex @code{--output-format}
- @table @code
- @item --output-format @var{format}
- @itemx --format @var{format}
- Change the default output format to that specified by @var{format}.
- See @ref{Named output formats} for more information and a list.
- @end table
- @table @code
- @item out:base=@var{integer}
- The number base (radix) to use by default when printing rational numbers.
- Must be an integer between 2 and 36, and the default is of course 10.
- For example the option @code{out:base=16} produces hexadecimal output.
- Equivalent to setting the @code{*print-base*} variable.
- @item out:radix=no|yes
- If true, prints an indicator of the radix used when printing rational numbers.
- The default is @code{no}.
- Equivalent to setting the @code{*print-radix*} variable.
- @item out:doctype-system=@var{system-identifier}
- If @code{out:doctype-system} is specified then a @code{DOCTYPE} declaration
- is written before writing a top-level XML element, using
- the specified @var{system-identifier}.
- @item out:doctype-public=@var{public-identifier}
- Ignored unless @code{out:doctype-system} is also specified,
- in which case the @var{public-identifier} is written
- as the public identifiers of the @code{DOCTYPE} declaration.
- @item out:xml-indent=@var{kind}
- Controls whether extra line breaks and indentation are added
- when printing XML.
- If @var{kind} is @code{always} or @code{yes} then newlines and
- appropriate indentation are added before and after each element.
- If @var{kind} is @code{pretty} then the pretty-printer is used
- to only add new lines when an element otherwise won't fit on a single line.
- If @var{kind} is @code{no} (the default) then no extra line breaks
- or indentation are added.
- @item out:line-length=@var{columns}
- @itemx out:right-margin=@var{columns}
- Specifies the maximum number of number of columns in a line
- when the pretty-printer decides where to break a line.
- (The two options are equivalent.)
- @end table
- @subsection Options for compiling and optimizing
- @table @code
- @item --target @var{version}
- The @var{version} can be a JDK or Java specification version:
- @code{5}, @code{6}, or @code{7}.
- The JDK versions @code{1.5} and @code{1.6} are equivalent to @code{5}
- or @code{6}, respectively.
- Specify a JVM (classfile) version to target. This is useful
- if (for example) you use Java 6, but want to create @code{.class} files
- that can run on Java 5. In that case specify @code{--target 5}.
- @end table
- The following options control which calling conventions are used:
- @table @code
- @item --full-tailcalls
- Use a calling convention that supports proper tail recursion.
- @item --no-full-tailcalls
- Use a calling convention that does not support proper tail recursion.
- Self-tail-recursion (i.e. a recursive call to the current function)
- is still implemented correctly, assuming that the called function
- is known at compile time.
- @item --no-inline
- Disable inlining of known functions and methods.
- The generated code runs slower, but you can more reliably trace procedures.
- Normally Kawa will assume that a procedure @code{fn}
- declared using a @code{(define (fn args) body)} form is constant,
- assuming it isn't modified in the current module. However, it is
- possible some other module might modify the binding of @code{fn}.
- You can use the @code{--no-inline} to disable the assumption that @code{fn}
- is constant.
- @end table
- The default is currently @code{--no-full-tailcalls} because
- it is usually faster.
- It is also closer to the Java call model, so may be better for people
- primarily interested in using Kawa for scripting Java systems.
- Both calling conventions can co-exist: Code compiled
- with @code{--full-tailcalls} can call code compiled
- with @code{--no-full-tailcalls} and vice versa.
- These options can also be used in the module source, using
- @code{module-compile-options} or @code{with-compile-options}.
- (In that case they override the options on the command line.)
- The options @samp{-C}, @samp{-d}, @samp{-T}, @samp{-P}, @samp{--main}
- @samp{--applet}, and @code{--servlet} are used to compile a Scheme file;
- see @ref{Files compilation}.
- The options @samp{--module-static}, @code{--module-nonstatic},
- @code{--no-module-static}, and @code{--module-static-run}
- control how a module is mapped to a Java class; see @ref{static-or-non-modules}.
- The option @samp{--connect @var{portnum}} is only used by
- the @samp{kawa} front-end program.
- @subsection Options for debugging
- The following options are useful if you want to debug or understand
- how Kawa works.
- @table @code
- @item --debug-dump-zip
- Normally, when Kawa loads a source file, or evaluates a non-trivial expression,
- it generates new internal Java classes but does not write them out. This
- option asks it to write out generated classes in a @samp{.zip} archive
- whose name has the prefix @samp{kawa-zip-dump-}.
- @item --debug-print-expr
- Kawa translates source language forms into an internal @code{Expression}
- data structure. This option causes that data structure to be written out
- in a readable format to the standard output.
- @item --debug-print-final-expr
- Similar to the previous option, but prints out the @code{Expression} after
- various transformations and optimizations have been done, and just before
- code generation.
- @item --debug-syntax-pattern-match
- Prints logging information to standard error when a @code{syntax-rules}
- or @code{syntax-case} pattern matches.
- @item --debug-error-prints-stack-trace
- Prints a stack trace with any error found during compilation.
- @item --debug-warning-prints-stack-trace
- Prints a stack trace with any warning found during compilation.
- @end table
- @subsection Options for web servers
- JDK 6 (or later) includes a complete web server library.
- @table @code
- @item --http-auto-handler @var{context-path} @var{appdir}
- Register a web application handler that uses files
- in the directory @var{appdir} to handle HTTP (web) requests
- containing the given @var{context-path}. That is it handles
- requests that start with @code{http://localhost:@var{port}@var{context-path}}.
- (This assumes the @var{context-path} starts with a @code{/}.)
- @xref{Self-configuring page scripts}.
- @item --http-start @var{port}
- Start the web server, listing on the specified @var{port}.
- @end table
- @subsection Options for the JVM
- The @code{kawa} front-end can pass options to the @code{java} launcher,
- using @code{-J} or @code{-D} options.
- These must be given @emph{before} any other arguments.
- For example:
- @example
- kawa -J-Xms48m -Dkawa.command.name=foo foo.scm
- @end example
- is equivalent to (ignoring classpath issues):
- @example
- java -Xms48m -Dkawa.command.name=foo kawa.repl foo.scm
- @end example
- You can also pass a @code{-D} option (but not a @code{-J} option) after the
- class name, in which case it is processed by the Kawa command-line processor
- rather than the @code{java} launcher. The effect is normally the same.
- @table @code
- @item -J@var{jvm-option}
- Passes the @var{jvm-option} to the @code{java} command,
- before the class-name (@code{kawa.repl}) and Kawa options.
- @item -D@var{variable-name}=@var{variable-value}
- Sets the JVM property @var{variable-name} to @var{variable-value}.
- Equivalent to @code{-J-D@var{variable-name}=@var{variable-value}}.
- @end table
- @node Scripts
- @section Running Command Scripts
- If you write a Kawa application, it is convenient to be able
- to execute it directly (from the command line or clicking an icon, say),
- without have to explicitly run @code{kawa} or @code{java}.
- On Unix-like systems the easiest way to do this is to
- write a small shell script that runs your Kawa application.
- For modest-sized applications it is convenient if the shell script
- and the Kawa code can be in the same file.
- Unix-like systems support a mechanism where a @dfn{script} can
- specify a program that should execute it. The convention
- is that the first line of the file should start with the two characters
- @samp{#!} followed by the absolute path of the program that should
- process (interpret) the script.
- (Windows has @dfn{batch files}, which are similar.)
- This convention works well for script languages that use @samp{#}
- to indicate the start of a comment, since the interpreter will
- automatically ignore the line specifying the interpreter filename.
- Scheme, however, uses @samp{#} as a multi-purpose prefix,
- and Kawa specifically uses @samp{#!} as a prefix for
- various @ref{Special named constants} such as @code{#!optional}.
- Kawa does recognize the three-character sequence @samp{#!/} at the
- beginning of a file as special, and ignores it.
- Here is an example:
- @example
- #!/usr/local/bin/kawa
- (format #t "The command-line was:~@{ ~w~@}~%" (command-line))
- @end example
- If you copy this text to a file named @code{/home/me/bin/scm-echo},
- set the execute permission, and make sure it is in your @code{PATH},
- then you can execute it just by naming it on command line:
- @example
- $ chmod +x /home/me/bin/scm-echo
- $ PATH=/home/me/bin:$PATH
- $ scm-env a b
- The command-line was: "/home/me/bin/scm-echo" "a" "b"
- @end example
- The system kernel will automatically execute @code{kawa}, passing it the
- filename as an argument.
- Note that the full path-name of the @code{kawa} interpreter
- must be hard-wired into the script. This means you may have to edit
- the script depending on where Kawa is installed on your system.
- Another possible problem is that the interpreter must be an
- actual program, not a shell script. Depending on how you configure
- and install Kawa, @code{kawa} can be a real program or a script.
- You can avoid both problems by the @code{env} program, available on
- most modern Unix-like systems:
- @example
- #!/usr/bin/env kawa
- (format #t "The command-line was:~@{ ~w~@}~%" (command-line))
- @end example
- This works the same way, but assumes @code{kawa} is in the
- command @code{PATH}.
- @subsection Setting kawa options in the script
- If you need to specify extra arguments to @code{kawa},
- you can run arbitrary shell command inside Scheme block comments.
- Here is an example:
- @example
- #!/bin/sh
- #|
- exec kawa out:base=16 out:radix=yes "$0" "$*"
- |#
- (format #t "The command-line is:~@{ ~w~@}.~%" (command-line))
- (display "It has ")
- (display (apply + (map string-length (command-line))))
- (display " characters.")
- (newline)
- @end example
- The trick is to hide the shell code from Kawa inside
- a @code{#|...|#} block-comment. The start of the block comment
- is a line starting with a @code{#}, so it is treated as a comment by the shell.
- You can then invoke @code{kawa} (or @code{java} directly)
- as you prefer, setting up class-path and jars as needed,
- and passing whatever arguments you want.
- (The shell replaces the @code{"$0"} by the name of the script, and
- replaces the @code{"$@@"} by the remaining arguments passed to the script.)
- You need to make sure the shell finishes before it reaches
- the end of the block comment or the Scheme code, which would confuse it.
- The example uses @code{exec}, which tells the shell to @emph{replace}
- itself by @var{kawa};
- an alternative is to use the shell @code{exit} command.
- If you copy the above file to @code{/tmp/sch-echo} and make
- that file executable, you can run it directly:
- @example
- $ /tmp/scm-echo "a b" "c d"
- The command-line is: "/tmp/scm-echo" "a b c d".
- It has #x14 characters.
- @end example
- When the Kawa reader sees the initial @code{#/} it sets
- the command name to the file name, so it can be used by a future
- call to @code{(command-name)}. If you want to override
- this you can use the @code{-Dkawa.command.name=@var{name}} option.
- Using comments this way has the advantage that you have the
- option of running the script ``manually'' if you prefer:
- @example
- $ kawa /tmp/scm-echo out:base=8 "x y"
- The command-line is: "/tmp/scm-echo" "out:base=8" "x y".
- It has 26 characters.
- @end example
- @subsection Other ways to pass options using meta-arg or --script
- An argument consisting of just a @code{\} (backslash)
- causes Kawa to read the @emph{second} line looking for
- options. (Quotes and backslashes work like in the shell.)
- These replace the backslash in the command line.
- This is a less verbose mechanism, but it requires an
- absolute path to @code{kawa}, due to shell limitations.
- @example
- #!/usr/bin/kawa \
- --scheme --full-tailcalls
- (format #t "The command-line is:~@{ ~w~@}.~%" (command-line))
- @end example
- In this case the effective command line received by Kawa will
- be @code{--scheme}, @code{--full-tailcalls}, followed by the
- script filename, followed by other arguments specified when
- running the script.
- The backslash used this way originated in
- @uref{http://www.scsh.net, scsh} where it is called the @dfn{meta-arg}.
- (Unlike scsh, Kawa's @code{#!} is not a block comment,
- but a rest-of-line, though the backslash causes the following line
- to also be skipped.)
- An alternative method is to use the @code{--script2} option,
- which tells Kawa to execute the script after ignoring
- the initial two lines. For example:
- @example
- #!/bin/sh
- exec kawa --commonlisp out:base=16 --script2 "$0" "$@@"
- (setq xx 20) (display xx) (newline)
- @end example
- This is slightly more compact than using block-comments as shown earlier,
- but it has the disadvantage that you can't explicitly
- use @code{kawa} or @code{java} to run the script unless you
- make sure to pass it the @code{--script2} option.
- @subsection Scripts for compiled code
- If you compile your Kawa application to class files (or better:
- a @code{jar} file), you probably still want to write a small
- shell script to set things up. Here is one method:
- @example
- #!/bin/sh
- export CLASSPATH=/my/path
- exec kawa -Dkawa.command.name="$0" foo "$@@"
- @end example
- Using the @code{kawa} front-end is a convenience, since it automatically
- sets up the paths for the Kawa classes, and (if enabled) it
- provides readline support for the default input port.
- Setting the @code{kawa.command.name} property to @code{"$0"}
- (the filename used to invoke the script) enables
- @code{(command-line}) to use the script name as the command name.
- You can invoke @code{java} directly, which is necessary when
- running a @code{jar} file:
- @example
- #!/bin/sh
- exec java -cp /path/to/kawa -Dkawa.command.name="$0" foo.jar "$@@"
- @end example
- @ignore
- (It is in principle possible to compile a Kawa application to
- ``a native executable'', for example using @code{gcj}.
- However, this is no longer supported, as gcj is no longer
- being actively developed.)
- @end ignore
- @node REPL Console
- @section The REPL (read-eval-print-loop) console
- The read-eval-print-loop (REPL) console is a convenient way to
- do simple programming, test out things, and experiment.
- As the name implies, the REPL repeatedly (in a loop)
- prints out a prompt, reads an input command, evaluates it, then prints the result.
- The REPL is started when you invoke the @code{kawa} command
- with no arguments. For example:
- @example
- $ @kbd{kawa}
- #|kawa:1|# @kbd{(define pi (* 2 (asin 1)))}
- #|kawa:2|# @kbd{(list pi (sqrt pi))}
- (3.141592653589793 1.7724538509055159)
- #|kawa:3|# @kbd{}
- @end example
- The colors and styles used for the prompt and the user input
- depend on user preference and the capabilities of the console device.
- (If you read this on a color screen you should see pale green for the
- prompt and pale yellow for the user input;
- this matches the defaults for the DomTerm console.)
- You can @ref{Prompts,change the prompt string} if you want.
- The default format depends on the (programming) language used;
- the one shown above is used for Scheme.
- It has the form of a comment, which can be convenient for copying
- and pasting lines.
- You can @ref{Named output formats,change the output formatting} with
- the @code{--output-format} command-line option.
- The basic console has few frills, but should work in any enviroment
- where you have a console or terminal. It has no dependencies,
- except the kawa @code{.jar} file (and Java):
- @example
- $ @kbd{java kawa-@value{VERSION}.jar}
- #|kawa:2|# @kbd{}
- @end example
- On rare occason you may need to specify the @code{--console} flag.
- @subsection Input line editing and history
- When typing a command in a console it is helpful to go back
- and correct mistakes, repeat and edit previous commands, and so on.
- How well you can do this varies a lot depending on which tools you use.
- Kawa delegates input editing to an external tool.
- The recommended and default input-editing tool is
- the @uref{https://github.com/jline/jline3,JLine3 library},
- which is bundled with the Kawa binary distribution.
- JLine3 handles the normal editing comands, including arrow keys
- for moving around in the input, and deleting with backspace or delete.
- In general, JLine3 uses the same keybindings as GNU readline,
- which are based on Emacs key-bindings.
- You can use the up-arrow to move to previous commands in the
- input history and down-arrow to go forwards.
- Control-R (``reverse search'' searches backwards in the history
- for a previous command that contains the search string.
- Multi-line commands are treated as a unit by JLine3:
- If Kawa determines that input is ``incomplete'' it will
- ask for continuation lines - and you can go back and edit previous
- lines in the same command.
- You can explicitly create a multi-line command with Escape-Space.
- An entry in the command history may be multiple lines.
- Tab-completion works for Kawa-Scheme identifiers: If you type TAB
- after an identifier, Kawa will present a list of possible completions.
- There are multiple alternatives to using JLine3.
- You can use GNU readline (if you configured with @code{--enable-kawa-frontend}).
- You can use a front-end program like @code{rlfe} or @code{fep}.
- You can use Emacs shell or scheme mode.
- You can also use DomTerm in line-edit mode, where the browser handles
- the editing.
- @table @asis
- @item @code{console:use-jline=}[@code{yes}|@code{no}]
- Disable (with @code{no}) or enable (with @code{yes}, which is the default)
- input line editing with JLine.
- @item @code{console:console:jline-mouse=}[@code{yes}|@code{no}]
- Enable (with @code{yes}) mouse click reporting from
- most xterm-like terminals to JLine, which means you
- can move the input cursor with the mouse.
- This is disabled by default because it conflicts with other useful mouse
- actions (text selection using drag; middle-button paste; right-button
- context menu; and wheel mouse scrolling).
- If you enable mouse-reporting, on most terminals you can get the
- standard behavior when pressing the shift key. E.g. to enable selection,
- drag with the shift key pressed. (However, mouse-wheel scrolling
- may not work even with shift pressed.)
- @end table
- @anchor{New-Window}
- @subsection Running a Command Interpreter in a new Window
- Instead of using an existing terminal window for Kawa's REPL console,
- you can request a new window.
- The command-line options @code{-w} creates a new window.
- Kawa also creates a new window when it needs to create a REPL
- (for example if invoked with no options) and it is not running
- in a console.
- You have a number of options for how the window appears and
- what it supports, controlled by text following @code{-w}.
- All except @code{-wswing} (and @code{-wconsole}) use DomTerm,
- so they depend on some kind of web browser technology.
- All except @code{-wswing} by default use JLine3 input editing,
- if available.
- @table @asis
- @item @code{-w}
- Pick the default/preferred console implementation.
- You can specify your preference with the @code{console:type=} option,
- which is followed by one of the options below (without the @code{"-w"} prefix),
- It can also be list of options separated by semi-colons, in which
- case they are tried in order.
- The current default (it may change) is as if you specified:
- @example
- console:type="google-chrome;browser;javafx;swing;console"
- @end example
- @item @code{-wbrowser}
- Creates a Kawa window or tab in your preferred desktop browser.
- Kawa starts a builtin HTTP and WebSocket server to communicate with the browser.
- @item @code{-wbrowser=@var{command}}
- Uses @var{command} to display the Kawa REPL.
- The @var{command} should include the pattern @code{%U}, which Kawa replaces
- with a URL that it listens to.
- (Alternatively, it can use the pattern @code{%W}, which Kawa replaces
- with the port number of its WebSocket server. However, this feature may be removed.)
- If the is no @code{%} in the @var{command}, Kawa add @code{" %U"}.
- Thus @code{-wbrowser=firefox} is the same as @code{-wbrowser="firefox %U"}.
- @item @code{-wgoogle-chrome}
- Creates a new Google Chrome window in ``app mode'' - i.e. with no location or menu bar.
- This is the same as @code{-wbrowser="google-chrome --app=%U"}.
- @c @item @code{-wqtdomterm}
- @c Uses the @code{QtDomTerm} application.
- @c Same as @code{-wbrowser="qtdomterm --connect localhost:%W"}, where @code{%W}
- @c is the port of the WebSocke server that Kawa starts.
- @item @code{-wjavafx}
- Creates a new window using JavaFX WebView, which runs in the same JVM as Kawa.
- While this doesn't currently have much in the way of Kawa-specific menus
- or other features, it has the most potential for adding them in the future.
- However, it does require JavaFX, which is not always available,
- and which does not see a lot of love from Oracle. (It uses an old version of WebKit.)
- @item @code{-wswing}
- Create a console using the Swing toolkit.
- This is the old implementation of @code{-w}.
- It is deprecated because it only supports the builtin Swing line editing.
- (I.e. neither DomTerm or JLine3 features are available, though
- ``printing'' @ref{Composable pictures,pictures} does work.)
- @item @code{-wserve}
- @itemx @code{-wserve=}@var{port}
- Starts up an HTTP server (along with a WebSocket server),
- but does not automatically create any browser windows.
- Instead you can use any modern browser to load @code{http://localhost:@var{port}/}.
- If @var{port} is not specified, the systems selects it (and prints it out).
- @item @code{-wconsole}
- Same as @code{"--"} - i.e. it uses the existing console.
- @item @code{console:type=}@var{preference-list}
- Specify the behavior of plain @code{-w}.
- @end table
- @anchor{Using DomTerm}
- @subsection Using DomTerm
- @uref{http://domterm.org,DomTerm} is a family of terminal emulators that use
- the DomTerm JavaScript library.
- You can either have Kawa start DomTerm:
- @example
- $ kawa @var{options} -w
- @end example
- or start a DomTerm terminal emulator and have it start Kawa:
- @example
- $ domterm kawa @var{options} --
- @end example
- (You can also start a shell in a @code{domterm} window, and then start @code{kawa}.)
- Either approach works and both give you the benefits of DomTerm:
- @itemize
- @item
- A xterm/ansi-compatible terminal emulator,
- which means you can use (for example) JLine3 for input editing.
- @item
- You can ``print'' images, @ref{Composable pictures,pictures}, or HTML elements.
- @item
- Pretty-printing is handled by the terminal,
- which means line-breaking is re-computed when window width changes.
- @item
- Hide/show buttons allow you to temporarily hide/unhide the output from a specific command.
- @item
- You can save a session as an HTML file,
- which can be viewed later.
- (Still with dynamic line-breaking and pretty-printing, as well as working hide/show buttons.)
- The file is actually XHTML, so it can be processed with XML-reading tools.
- @item
- Distinct styles for prompts, input, error output and regular output,
- which can be customized with CSS.
- @end itemize
- For now it is recommended to use both DomTerm and JLine3.
- @deffn Procedure domterm-load-stylesheet stylesheet [name]
- The string @var{stylesheet} should be a literal CSS stylesheet
- which is downloaded into the current DomTerm console.
- The new stylesheet is given the attribute @code{name=@var{name}},
- where @var{name} defaults to @code{"Kawa"}. If there is an
- existing stylesheey whose @code{name} attribute is @var{name},
- it is replaced.
- In this example we change the background color to light gray:
- @example
- (domterm-load-stylesheet "div.domterm @{ background-color: lightgray@}")
- @end example
- @end deffn
- @node Exiting
- @section Exiting Kawa
- Kawa normally keeps running as long as there is an active
- read-eval-print loop still awaiting input or there is an unfinished
- other computation (such as requested by a @samp{-e} or @samp{-f} option).
- To close a read-eval-print-loop, you can type the special
- literal @code{#!eof} at top level. This is recognized as end-of-file.
- Typing an end-of-file character (normally ctrl-D under Unix)
- should also work, but that depends on your operating system
- and terminal interface.
- If the read-eval-print-loop
- is in a new window, you can select @samp{Close} from the @samp{File} menu.
- To exit the entire Kawa session, call the
- @ref{Exiting the current process,@code{exit} procedure} (with 0 or 1 integer arguments).
- @node Compiling, , , Running
- @section Compiling to byte-code
- All Scheme functions and source files are invisibly compiled
- into internal Java byte-codes.
- (A traditional interpreter is used for macro-expansion.
- Kawa used to also interpret ``simple'' expressions in interactive mode,
- but always compiling makes things more consistent, and allows for
- better stack traces on errors.)
- To save speed when loading large Scheme source files, you probably
- want to pre-compile them and save them on your local disk.
- There are two ways to do this.
- You can compile a Scheme source file to a single archive file.
- You do this using the @code{compile-file} function.
- The result is a single file that you can move around and @code{load}
- just like the @code{.scm} source file. You just specify the name
- of the archive file to the @code{load} procedure.
- Currently, the archive is a "zip" archive and has extension ".zip";
- a future release will probably use "Java Archive" (jar) files.
- The advantage of compiling to an archive is that it is simple
- and transparent.
- Alternatively, you can compile a Scheme source file to a
- collection of @samp{.class} files.
- You then use the standard Java class loading mechanism to load the code.
- The compiled class files do have to be installed somewhere
- in the @code{CLASSPATH}.
- @menu
- * Files compilation:: Compiling to a set of .class files
- * Archive compilation:: Compiling to an archive file
- * Compiling using Ant::
- * Application compilation:: Compiling to a standalone application
- * Applet compilation:: Compiling to an applet
- * Compiling to executable:: Compiling to a native executable
- @end menu
- @node Files compilation
- @subsection Compiling to a set of .class files
- Invoking @samp{kawa} (or @samp{java kawa.repl}) with
- the @samp{-C} flag will compile
- a @samp{.scm} source file into one or more @samp{.class} files:
- @example
- kawa --main -C myprog.scm
- @end example
- You run it as follows:
- @example
- kawa [-d @var{outdirectory}] [-P @var{prefix}] [-T @var{topname}] [--main | --applet | --servlet] -C @var{infile} ...
- @end example
- Note the @samp{-C} must come last, because @samp{Kawa} processes the
- arguments and options in order,
- Here:
- @table @code
- @item -C @var{infile} ...
- The Scheme source files we want to compile.
- @item -d @var{outdirectory}
- The directory under which the resulting @samp{.class} files will be.
- The default is the current directory.
- @item -P @var{prefix}
- A string to prepend to the generated class names.
- The default is the empty string.
- @item -T @var{topname}
- The name of the "top" class - i.e. the one that contains the code
- for the top-level expressions and definitions.
- The default is generated from the @var{infile} and @var{prefix}.
- @item --main
- Generate a @code{main} method so that the resulting "top" class can
- be used as a stand-alone application. @xref{Application compilation}.
- @item --applet
- The resulting class inherits from @code{java.applet.Applet},
- and can be used as an applet. @xref{Applet compilation}.
- @item --servlet
- The resulting class implements @code{javax.servlet.http.HttpServlet},
- and can be used as a servlet in a servlet container like Tomcat.
- @end table
- When you actually want to load the classes, the @var{outdirectory}
- must be in your @samp{CLASSPATH}.
- You can use the @code{require} syntax or the @code{load} function to load the code,
- by specifying the top-level class, either as a file name
- (relative to @var{outdirectory}) or as a class name.
- E.g. if you did:
- @example
- kawa -d /usr/local/share/java -P my.lib. -T foo -C foosrc.scm
- @end example
- you can use either:
- @example
- (require my.lib.foo)
- @end example
- or:
- @example
- (load "my.lib.foo")
- @end example
- Using @code{require} is preferred as it imports the definitions
- from @code{my.lib.foo} into the compile-time environment,
- while @code{load} only imports the definitions into the run-time environment.
- If you are compiling a Scheme source file (say @samp{foosrc.scm})
- that uses macros defined in some other file (say @samp{macs.scm}),
- you need to make sure the definitions are visible to the compiler.
- One way to do that is with the @samp{-f}:
- @example
- kawa -f macs.scm -C foosrc.scm
- @end example
- Many of the options @ref{Options,,described earlier} are
- relevant when compiling. Commonly used options include language selection,
- the @code{--warn-xxx} options, and @code{--full-tailcalls}.
- @node Archive compilation
- @subsection Compiling to an archive file
- @deffn Procedure compile-file source-file compiled-archive
- Compile the @var{source-file}, producing a @code{.zip} archive
- @var{compiled-file}.
- For example, to byte-compile a file @samp{foo.scm} do:
- @example
- (compile-file "foo.scm" "foo")
- @end example
- This will create @samp{foo.zip}, which contains
- byte-compiled JVM @code{.class} files.
- You can move this file around, without worrying about class paths.
- To load the compiled file, you can later @code{load} the
- named file, as in either @code{(load "foo")} or @code{(load "foo.zip")}.
- This should have the same effect as
- loading @samp{foo.scm}, except you will get the faster byte-compiled versions.
- @end deffn
- @node Compiling using Ant
- @subsection Compiling using Ant
- @cindex kawac
- Many Java projects use @uref{http://ant.apache.org, Ant}
- for building Java projects. Kawa includes a @code{<kawac>}
- Ant task that simplifies compiling Kawa source files to classes.
- See the @code{build.xml} in the Kawa source distribution for examples.
- See the @uref{ant-kawac.html, @code{kawac} task documentation} for details.
- @node Application compilation
- @subsection Compiling to a standalone application
- A Java application is a Java class with a special method
- (whose name is @code{main}). The application can be invoked directly
- by naming it in the Java command.
- If you want to generate an application from a Scheme program,
- create a Scheme source file with the definitions you need, plus
- the top-level actions that you want the application to execute.
- For example, assuming your Scheme file is
- @code{MyProgram.scm}, you have two ways at your disposal to
- compile this Scheme program to a standalone application:
- @enumerate
- @item
- Compile
- in the regular way described in the previous section, but add the
- @code{--main} option.
- @example
- kawa --main -C MyProgram.scm
- @end example
- The @code{--main} option will compile all Scheme programs
- received in arguments to standalone applications.
- @item
- Compile
- in the regular way decribed in the previous section, but add the
- @code{main: #t} module compile option to your module.
- @example
- ;; MyProgram.scm
- (module-name <myprogram>)
- (module-compile-options main: #t)
- @end example
- @example
- kawa -C MyProgram.scm
- @end example
- This way you can compile multiple Scheme programs at once, and
- still control which one(s) will compile to standalone application(s).
- @end enumerate
- Both methods will create a @code{MyProgram.class} which you can either
- @code{load} (as described in the previous section), or invoke as an application:
- @example
- java MyProgram [@var{args}]
- @end example
- Your Scheme program can access the command-line arguments @var{args}
- by using the global variable @samp{command-line-arguments},
- or the R6RS function @samp{command-line}.
- If there is no explicit @code{module-export} in a module compiled
- with @code{--main} then no names are exported. (The default
- otherwise is for all names to be exported.)
- @node Applet compilation, Compiling to executable, Application compilation, Compiling
- @subsection Compiling to an applet
- An applet is a Java class that inherits from @code{java.applet.Applet}.
- The applet can be downloaded and run in a Java-capable web-browser.
- To generate an applet from a Scheme program, write the Scheme
- program with appropriate definitions of the functions @samp{init},
- @samp{start}, @samp{stop} and @samp{destroy}. You must declare these
- as zero-argument functions with a @code{<void>} return-type.
- Here is an example, based on the scribble applet in Flanagan's
- "Java Examples in a Nutshell" (O'Reilly, 1997):
- @example
- (define-private last-x 0)
- (define-private last-y 0)
- (define (init) :: void
- (let ((applet (this)))
- (applet:addMouseListener
- (object (java.awt.event.MouseAdapter)
- ((mousePressed e)
- (set! last-x (e:getX))
- (set! last-y (e:getY)))))
- (applet:addMouseMotionListener
- (object (java.awt.event.MouseMotionAdapter)
- ((mouseDragged e)
- (let ((g (applet:getGraphics))
- (x (e:getX))
- (y (e:getY)))
- (g:drawLine last-x last-y x y)
- (set! last-x x)
- (set! last-y y)))))))
- (define (start) :: void (format #t "called start.~%~!"))
- (define (stop) :: void (format #t "called stop.~%~!"))
- (define (destroy) :: void (format #t "called destroy.~%~!"))
- @end example
- You compile the program with the @samp{--applet} flag in addition to the
- normal @samp{-C} flag:
- @example
- java kawa.repl --applet -C scribble.scm
- @end example
- You can then create a @samp{.jar} archive containing your applet:
- @example
- jar cf scribble.jar scribble*.class
- @end example
- Finally, you create an @samp{.html} page referencing your applet
- and its support @code{jar}s:
- @example
- <html><head><title>Scribble testapp</title></head>
- <body><h1>Scribble testapp</h1>
- You can scribble here:
- <br>
- <applet code="scribble.class" archive="scribble.jar, kawa-@value{VERSION}.jar" width=200 height=200>
- Sorry, Java is needed.</applet>
- </body></html>
- @end example
- The problem with using Kawa to write applets is that the Kawa @code{.jar}
- file is quite big, and may take a while to download over a network connection.
- Some possible solutions:
- @itemize
- @item
- Try to strip out of the Kawa @code{.jar} any classes your
- applet doesn't need.
- @item
- Java 2 provides a mechanism to install a @uref{http://java.sun.com/docs/books/tutorial/ext/basics/download.html,
- download extension}.
- @item
- Consider some alternative to applets, such as
- @uref{http://java.sun.com/products/javawebstart/,Java Web Start}.
- @end itemize
- @node Compiling to executable, , Applet compilation, Compiling
- @subsection Compiling to a native executable
- In the past it was possible to compile a Scheme program to native code using GCJ.
- However, using GCJ with Kawa is no longer supported,
- as GCJ is no longer being actively maintained.
- @ignore
- You can compile your Scheme program to native code using GCJ,
- as long as you have built Kawa using GCJ.
- First, you need to compile the Scheme code to a set of @code{.class} files;
- see @ref{Files compilation}.
- @example
- kawa --main -C myprog.scm
- @end example
- Then to create an executable @code{myprog} do:
- @example
- gckawa --main=myprog myprog*.class -o myprog
- @end example
- The @code{gckawa} is a simple shell script that calls @code{gcj}.
- The reason for the wildcard in @code{myprog*.class} is that sometimes
- Kawa will generate some helper classes in addition to @code{myprog.class}.
- The @code{--main} option tell @code{gcj} which class contains
- the @code{main} method it should use. The @code{-o} option names
- the resulting executable program. The @code{-lkawa} option tells
- the linker it should link with the kawa shared library, and
- the @code{-L$PREFIX/bin} option tells the linker where it can
- find that library.
- @end ignore
- @node Syntax
- @chapter Syntax
- @menu
- * Syntax notation::
- * Lexical and datum syntax::
- * Lexical syntax::
- * Datum syntax::
- * Hash-prefixed forms::
- * Primitive expression syntax::
- * Colon notation:: Property access using colon notation
- * Bodies::
- * Syntax and conditional compilation::
- * Macros::
- * Named quasi-literals::
- @end menu
- @node Syntax notation, Lexical and datum syntax, , Syntax
- @section Notation
- The formal syntax for Kawa Scheme is written in an extended @acronym{BNF}.
- Non--terminals are written @var{like-this}. Case is insignificant
- for non--terminal names.
- Literal text (terminals) are written @stxlit{like this}.
- All spaces in the grammar are for legibility.
- @c @meta{Empty} stands for the empty string.
- The following extensions to @acronym{BNF} are used to make the
- description more concise: @arbno{@meta{thing}} or @meta{thing}@code{...}
- both mean zero or more occurrences of @meta{thing},
- and @atleastone{@meta{thing}} means at least one @meta{thing}.
- Some non-terminal names refer to the Unicode scalar values of the same
- name: @meta{character-tabulation} (U+0009), @meta{linefeed} (U+000A),
- @meta{carriage-return} (U+000D), @meta{line-tabulation} (U+000B),
- @meta{form-feed} (U+000C), @meta{space} (U+0020), @meta{next-line}
- (U+0085), @meta{line-separator} (U+2028), and @meta{paragraph-separator}
- (U+2029).
- @node Lexical and datum syntax, Lexical syntax, Syntax notation, Syntax
- @section Lexical and datum syntax
- The syntax of Scheme code is organized in three levels:
- @enumerate
- @item
- the @emph{lexical syntax} that describes how a program text is split
- into a sequence of lexemes,
- @item
- the @emph{datum syntax}, formulated in terms of the lexical syntax, that
- structures the lexeme sequence as a sequence of @emph{syntactic data},
- where a syntactic datum is a recursively structured entity,
- @item
- the @emph{program syntax} formulated in terms of the datum syntax,
- imposing further structure and assigning meaning to syntactic data.
- @end enumerate
- Syntactic data (also called @emph{external representations}) double as a
- notation for objects, and the @func{read} and
- @func{write} procedures can be used for reading and writing syntactic data,
- converting between their textual representation and the corresponding
- objects. Each syntactic datum represents a corresponding
- @emph{datum value}. A syntactic datum can be used in a program to obtain the
- corresponding datum value using @code{quote}.
- @c FIXME (@ref{base expressions quotation}).
- Scheme source code consists of syntactic data and (non--significant)
- comments. Syntactic data in Scheme source code are called @emph{forms}.
- (A form nested inside another form is called a @emph{subform}.)
- Consequently, Scheme's syntax has the property that any sequence of
- characters that is a form is also a syntactic datum representing some
- object. This can lead to confusion, since it may not be obvious out of
- context whether a given sequence of characters is intended to be a
- representation of objects or the text of a program. It is also a source
- of power, since it facilitates writing programs such as interpreters or
- compilers that treat programs as objects (or vice versa).
- A datum value may have several different external representations. For
- example, both @code{#e28.000} and @code{#x1c} are syntactic data
- representing the exact integer object 28, and the syntactic data
- @code{(8 13)}, @code{( 08 13 )}, @code{(8 . (13 . ()))} all represent a
- list containing the exact integer objects 8 and 13. Syntactic data that
- represent equal objects (in the sense of @func{equal?})
- are always equivalent as forms of a program.
- Because of the close correspondence between syntactic data and datum
- values, we sometimes uses the term @emph{datum} for either a
- syntactic datum or a datum value when the exact meaning is apparent from
- the context.
- @c An implementation must not extend the lexical or datum syntax in any
- @c way, with one exception: it need not treat the syntax
- @c @code{#!<identifier>}, for any <identifier> (@ref{lex syntax
- @c identifiers}) that is not @code{r6rs}, as a syntax violation, and it may
- @c use specific @code{#!}--prefixed identifiers as flags indicating that
- @c subsequent input contains extensions to the standard lexical or datum
- @c syntax. The syntax @code{#!r6rs} may be used to signify that the input
- @c afterward is written with the lexical syntax and datum syntax described
- @c by this report. @code{#!r6rs} is otherwise treated as a comment;
- @c @ref{lex syntax whitespace and comments}.
- @node Lexical syntax, Datum syntax, Lexical and datum syntax, Syntax
- @section Lexical syntax
- The lexical syntax determines how a character sequence is split into a
- sequence of lexemes, omitting non--significant portions such as comments
- and whitespace. The character sequence is assumed to be text according
- to the @uref{http://unicode.org/,Unicode standard}.
- Some of the lexemes, such as
- identifiers, representations of number objects, strings etc., of the
- lexical syntax are syntactic data in the datum syntax, and thus
- represent objects. Besides the formal account of the syntax, this
- section also describes what datum values are represented by these
- syntactic data.
- The lexical syntax, in the description of comments, contains a forward
- reference to @meta{datum}, which is described as part of the datum
- syntax. Being comments, however, these @meta{datum}s do not play a
- significant role in the syntax.
- Case is significant except in representations of booleans, number
- objects, and in hexadecimal numbers specifying Unicode scalar values.
- For example, @code{#x1A} and @code{#X1a} are equivalent. The identifier
- @code{Foo} is, however, distinct from the identifier @code{FOO}.
- @subsection Formal account
- @noindent
- @var{Interlexeme-space} may occur on either side of any lexeme, but not
- within a lexeme.
- @meta{Identifier}s, @code{.}, @meta{number}s, @meta{character}s, and
- @meta{boolean}s, must be terminated by a @meta{delimiter} or by the end
- of the input.
- @c The following two characters are reserved for future extensions to the
- @c language: @code{@{ @}}
- @display
- @stxdef{lexeme} @stxref{identifier} | @var{boolean} | @stxref{number}
- | @var{character} | @stxref{string}
- | @stxlit{(} | @stxlit{)} | @stxlit{[} | @stxlit{]} | @stxlit{#(}
- | @stxlit{'} | @stxlit{`} | @stxlit{,} | @stxlit{,@@} | @stxlit{.}
- | @stxlit{#'} | @stxlit{#`} | @stxlit{#,} | @stxlit{#,@@}
- @stxdef{delimiter} @stxlit{(} | @stxlit{)} | @stxlit{[} | @stxlit{]} | @stxlit{"} | @stxlit{;} | @stxlit{#}
- | @stxref{whitespace}
- @end display
- ((UNFINISHED))
- @subsection Line endings
- Line endings are significant in Scheme in single--line comments
- and within string literals.
- In Scheme source code, any of the line endings in @meta{line-ending}
- marks the end of a line. Moreover, the two--character line endings
- @meta{carriage-return} @meta{linefeed} and @meta{carriage-return}
- @meta{next-line} each count as a single line ending.
- In a string literal, a @meta{line-ending} not preceded by a @code{\}
- stands for a linefeed character, which is the standard line--ending
- character of Scheme.
- @subsection Whitespace and comments
- @display
- @stxdef{intraline-whitespace} @var{space} | @var{character-tabulation}
- @stxdef{whitespace} @stxref{intraline-whitespace}
- | @var{linefeed} | @var{line-tabulation} | @var{form-feed}
- | @var{carriage-return} | @var{next-line}
- | @i{any character whose category is Zs, Zl, or Zp}
- @stxdef{line-ending} @var{linefeed} | @var{carriage return}
- | @var{carriage-return} @var{linefeed} | @var{next-line}
- | @var{carriage-return} @var{next-line} | @var{line-separator}
- @stxdef{comment} @stxlit{;} all subsequent characters up to a @var{line-ending}
- or @var{paragraph-separator}
- | @stxref{nested-comment}
- | @stxlit{#;} @stxref{interlexeme-space} @stxref{datum}
- | @stxref{shebang-comment}
- @stxdef{nested-comment} @stxlit{#|} @stxref{comment-text} @stxref{comment-cont}* @stxlit{|#}
- @stxdef{comment-text} character sequence not containing @stxlit{#|} or @stxlit{|#}
- @stxdef{comment-cont} @stxref{nested-comment} @stxref{comment-text}
- @stxdef{atmosphere} @stxref{whitespace} | @stxref{comment}
- @stxdef{interlexeme-space} @arbno{@var{atmosphere}}
- @end display
- As a special case the characters @stxlit{#!/} are treated as starting a comment,
- but only at the beginning of file. These characters are used on
- Unix systems as an @uref{http://en.wikipedia.org/wiki/Shebang_(Unix), Shebang interpreter directive}.
- The Kawa reader skips the entire line.
- If the last non-whitespace character is @stxlit{@backslashchar{}}
- (backslash) then the following line is also skipped, and so on.
- @display
- @stxdef{shebang-comment} @stxlit{#!} @var{absolute-filename} text up to non-escaped @var{line-ending}
- @end display
- @noindent
- @emph{Whitespace} characters are spaces, linefeeds, carriage returns,
- character tabulations, form feeds, line tabulations, and any other
- character whose category is Zs, Zl, or Zp. Whitespace is used for
- improved readability and as necessary to separate lexemes from each
- other. Whitespace may occur between any two lexemes, but not within a
- lexeme. Whitespace may also occur inside a string, where it is
- significant.
- The lexical syntax includes several comment forms. In all cases,
- comments are invisible to Scheme, except that they act as delimiters,
- so, for example, a comment cannot appear in the middle of an identifier
- or representation of a number object.
- A semicolon (@code{;}) indicates the start of a line comment. The
- comment continues to the end of the line on which the semicolon appears.
- Another way to indicate a comment is to prefix a @stxref{datum}
- with @code{#;}, possibly with
- @meta{interlexeme-space} before the @meta{datum}. The comment consists
- of the comment prefix @code{#;} and the @meta{datum} together. This
- notation is useful for ``commenting out'' sections of code.
- Block comments may be indicated with properly nested @code{#|} and
- @code{|#} pairs.
- @example
- #|
- The FACT procedure computes the factorial of a
- non-negative integer.
- |#
- (define fact
- (lambda (n)
- ;; base case
- (if (= n 0)
- #;(= n 1)
- 1 ; identity of *
- (* n (fact (- n 1))))))
- @end example
- @c The lexeme @code{#!r6rs}, which signifies that the program text that
- @c follows is written with the lexical and datum syntax described in this
- @c report, is also otherwise treated as a comment.
- @subsection Identifiers
- @display
- @stxdef{identifier} @stxref{initial} @stxref{subsequent}*
- | @stxref{peculiar-identifier}
- @stxdef{initial} @stxref{constituent} | @stxref{special-initial}
- | @stxref{inline-hex-escape}
- @stxdef{letter} @stxlit{a} | @stxlit{b} | @stxlit{c} | ... | @stxlit{z}
- | @stxlit{A} | @stxlit{B} | @stxlit{C} | ... | @stxlit{Z}
- @stxdef{constituent} @stxref{letter}
- | @i{any character whose Unicode scalar value is greater than
- 127, and whose category is Lu, Ll, Lt, Lm, Lo, Mn,
- Nl, No, Pd, Pc, Po, Sc, Sm, Sk, So, or Co}
- @stxdef{special-initial} @stxlit{!} | @stxlit{$} | @stxlit{%} | @stxlit{&} | @stxlit{*} | @stxlit{/} | @stxlit{<} | @stxlit{=}
- | @stxlit{>} | @stxlit{?} | @stxlit{^} | @stxlit{_} | @stxlit{~}
- @stxdef{subsequent} @stxref{initial} | @stxref{digit}
- | @i{any character whose category is Nd, Mc, or Me}
- | @stxref{special-subsequent}
- @stxdef{digit} @stxlit{0} | @stxlit{1} | @stxlit{2} | @stxlit{3} | @stxlit{4} | @stxlit{5} | @stxlit{6} | @stxlit{7} | @stxlit{8} | @stxlit{9}
- @stxdef{oct-digit} @stxlit{0} | @stxlit{1} | @stxlit{2} | @stxlit{3} | @stxlit{4} | @stxlit{5} | @stxlit{6} | @stxlit{7}
- @stxdef{hex-digit} @stxref{digit}
- | @stxlit{a} | @stxlit{A} | @stxlit{b} | @stxlit{B} | @stxlit{c} | @stxlit{C} | @stxlit{d} | @stxlit{D} | @stxlit{e} | @stxlit{E} | @stxlit{f} | @stxlit{F}
- @stxdef{special-subsequent} @stxlit{+} | @stxlit{-} | @stxlit{.} | @stxlit{@@}
- @stxdef{escape-sequence} @stxref{inline-hex-escape}
- | @stxlit{\\}@stxref{character-except-x}
- | @stxref{multi-escape-sequence}
- @stxdef{inline-hex-escape} @stxlit{\\x}@stxref{hex-scalar-value}@stxlit{;}
- @stxdef{hex-scalar-value} @stxref{hex-digit}+
- @stxdef{multi-escape-sequence} @stxlit{|}@arbno{@stxref{symbol-element}}@stxlit{|}
- @stxdef{symbol-element} @i{any character except} @stxlit{|} @i{or} @stxlit{@backslashchar{}}
- | @stxref{inline-hex-escape} | @stxref{mnemonic-escape} | @stxlit{@backslashchar{}|}
- @stxdef{character-except-x} @i{any character except @code{x}}
- @stxdef{peculiar-identifier} @stxlit{+} | @stxlit{-} | @stxlit{...} | @stxlit{->} @arbno{@var{subsequent}}
- @end display
- Most identifiers allowed by other programming languages are also
- acceptable to Scheme. In general, a sequence of letters, digits, and
- ``extended alphabetic characters'' is an identifier when it begins with
- a character that cannot begin a representation of a number object. In
- addition, @code{+}, @code{-}, and @code{...} are identifiers, as is a
- sequence of letters, digits, and extended alphabetic characters that
- begins with the two--character sequence @code{->}. Here are some
- examples of identifiers:
- @example
- lambda q soup
- list->vector + V17a
- <= a34kTMNs ->-
- the-word-recursion-has-many-meanings
- @end example
- Extended alphabetic characters may be used within identifiers as if they
- were letters. The following are extended alphabetic characters:
- @example
- ! $ % & * + - . / < = > ? @@ ^ _ ~
- @end example
- Moreover, all characters whose Unicode scalar values are greater than
- 127 and whose Unicode category is Lu, Ll, Lt, Lm, Lo, Mn, Mc, Me, Nd,
- Nl, No, Pd, Pc, Po, Sc, Sm, Sk, So, or Co can be used within
- identifiers. In addition, any character can be used within an
- identifier when specified using an @meta{escape-sequence}. For example,
- the identifier @code{H\x65;llo} is the same as the identifier
- @code{Hello}.
- Kawa supports two additional non-R6RS ways of making
- identifiers using special characters, both taken from Common Lisp:
- Any character (except @code{x}) following a backslash is treated
- as if it were a @var{letter};
- as is any character between a pair of vertical bars.
- @c , and the identifier @code{\x3BB;} is the same as the
- @c identifier $\lambda$.
- Identifiers have two uses within Scheme programs:
- @itemize
- @item
- Any identifier may be used as a @ref{variable-reference,,variable}
- or as a @ref{macro-reference,,syntactic keyword}.
- @item
- When an identifier appears as or with in @ref{literal-expression,,literal},
- it is being used to denote a @ref{Simple symbols,,symbol}.
- @end itemize
- In contrast with older versions of Scheme, the syntax distinguishes
- between upper and lower case in identifiers and in characters
- specified via their names, but not in numbers, nor in inline hex
- escapes used in the syntax of identifiers, characters, or strings.
- The following directives give explicit control over case folding.
- @deffn Syntax #!fold-case
- @deffnx Syntax #!no-fold-case
- These directives may appear anywhere comments are permitted and are
- treated as comments, except that they affect the reading of subsequent
- data. The @code{#!fold-case} directive causes the @code{read}
- procedure to case-fold (as if by @code{string-foldcase}) each
- identifier and character name subsequently read from the same
- port. The @code{#!no-fold-case} directive causes the @code{read}
- procedure to return to the default, non-folding behavior.
- @end deffn
- Note that colon @code{:} is treated specially for
- @ref{Colon notation, colon notation} in Kawa Scheme,
- though it is a @var{special-initial} in standard Scheme (R6RS).
- @subsection Numbers
- ((INCOMPLETE))
- @display
- @stxdef{number} ((TODO))
- | @stxref{quantity}
- @stxdef{decimal} @stxref{digit}+ @stxref{optional-exponent}
- | @stxlit{.} @stxref{digit}+ @stxref{optional-exponent}
- | @stxref{digit}+ @stxlit{.} @stxref{digit}+ @stxref{optional-exponent}
- @end display
- @display
- @stxdef{optional-exponent} @i{empty}
- | @stxref{exponent-marker} @stxref{optional-sign} @stxref{digit}+
- @stxdef{exponent-marker} @stxlit{e} | @stxlit{s} | @stxlit{f} | @stxlit{d} | @stxlit{l}
- @end display
- The letter used for the exponent in a floating-point literal determines
- its type:
- @table @asis
- @item @stxlit{e}
- Returns a @code{gnu.math.DFloat} - for example @code{12e2}.
- Note this matches the default when there is no @stxref{exponent-marker}.
- @item @stxlit{s} or @stxlit{f}
- Returns a primitive @code{float} (or @code{java.lang.Float}
- when boxed as an object) - for example @code{12s2} or @code{12f2}.
- @item @stxlit{d}
- Returns a primitive @code{double} (or @code{java.lang.Double} when boxed)
- - for example @code{12d2}.
- @item @stxlit{l}
- Returns a @code{java.math.BigDecimal} - for example @code{12l2}.
- @end table
- @display
- @stxdef{optional-sign} @i{empty} | @stxlit{+} | @stxlit{-}
- @stxdef{digit-2} @stxlit{0} | @stxlit{1}
- @stxdef{digit-8} @stxlit{0} | @stxlit{1} | @stxlit{2} | @stxlit{3} | @stxlit{4} | @stxlit{5} | @stxlit{6} | @stxlit{7}
- @stxdef{digit-10} @stxref{digit}
- @stxdef{digit-16} @stxref{digit-10} | @stxlit{a} | @stxlit{b} | @stxlit{c} | @stxlit{d} | @stxlit{e} | @stxlit{f}
- @end display
- @node Datum syntax, Hash-prefixed forms, Lexical syntax, Syntax
- @section Datum syntax
- The datum syntax describes the syntax of syntactic data in terms of a
- sequence of @meta{lexeme}s, as defined in the lexical syntax.
- The following grammar describes the syntax of syntactic data in terms of
- various kinds of lexemes defined in the grammar in section ``Lexical
- Syntax'':
- @display
- @stxdef{datum} @stxref{defining-datum}
- | @stxref{nondefining-datum}
- | @stxref{defined-datum}
- @stxdef{nondefining-datum} @stxref{lexeme-datum}
- | @stxref{compound-datum}
- @stxdef{lexeme-datum} @stxref{boolean} | @meta{number}
- | @stxref{character} | @stxref{string} | @stxref{symbol}
- @stxdef{symbol} @stxref{identifier}
- @stxdef{compound-datum} @stxref{list} | @stxref{vector} | @stxref{uniform-vector} | @stxref{array-literal} | @stxref{extended-string-literal} | @stxref{xml-literal}
- @stxdef{list} @stxlit{(}@stxref{datum}*@stxlit{)}
- | @stxlit{(}@atleastone{@stxref{datum}} @stxlit{.} @stxref{datum}@stxlit{)}
- | @stxref{abbreviation}
- @stxdef{vector} @stxlit{#(}@arbno{@stxref{datum}}@stxlit{)}
- @end display
- @c FIXME: add to abbrev-prefix: @stxlit{#,} | @stxlit{#,@@}
- @anchor{datum labels}
- @subsection Datum labels
- @display
- @stxdef{datum-label} @stxlit{#}@stxref{indexnum}@stxlit{=}
- @stxdef{defining-datum} @atleastone{@stxref{datum-label}}@stxref{nondefining-datum}
- @stxdef{defined-datum} @stxlit{#}@stxref{indexnum}@stxlit{#}
- @stxdef{indexnum} @atleastone{@stxref{digit}}
- @end display
- The lexical syntax @code{#@meta{n}=@meta{datum}} reads the
- same as @meta{datum}, but also results in @meta{datum} being
- labelled by @meta{n}, which must a sequence of digits.
- The lexical syntax @code{#@meta{n}#} serves as a reference to some
- object labelled by @code{#@meta{n}=}; the result is the same object
- (in the sense of @code{eq?}) as the @code{#@meta{n}=}.
- Together, these syntaxes permit the notation of structures
- with shared or circular substructure.
- @example
- (let ((x (list 'a 'b 'c)))
- (set-cdr! (cddr x) x)
- x) @result{} #0=(a b c . #0#)
- @end example
- The scope of a datum label is the portion of the outermost
- datum in which it appears that is to the right of the label.
- Consequently, a reference @code{#@meta{n}#} can occur
- only after a label @code{#@meta{n}=};
- it is an error to attempt a forward reference.
- In addition, it is an error if the reference appears as the labelled
- object itself (as in @code{#@meta{n}=#@var{n}#}), because the object
- labelled by @code{#@var{n}=} is not well defined in this case.
- @subsection Abbreviations
- @display
- @stxdef{abbreviation} @stxref{r6rs-abbreviation} | @stxref{kawa-abbreviation}
- @stxdef{r6rs-abbreviation} @stxref{abbrev-prefix} @stxref{datum}
- @stxdef{abbrev-prefix} @stxlit{'} | @stxlit{`} | @stxlit{,} | @stxlit{,@@}
- | @stxlit{#'} | @stxlit{#`}
- @stxdef{kawa-abbreviation} XXX
- @end display
- The following abbreviations are expanded at read-time:
- @table @asis
- @item @stxlit{'}@meta{datum}
- means @stxlit{(quote} @meta{datum}@stxlit{)}.
- @item @stxlit{`}@meta{datum}
- means @stxlit{(quasiquote} @meta{datum}@stxlit{)}.
- @item @stxlit{,}@meta{datum}
- means @stxlit{(unquote} @meta{datum}@stxlit{)}.
- @item @stxlit{,@@}@meta{datum}
- means @stxlit{(unquote-splicing} @meta{datum}@stxlit{)}.
- @item @stxlit{#'}@meta{datum}
- means @stxlit{(syntax} @meta{datum}@stxlit{)}.
- @item @stxlit{#`}@meta{datum}
- means @stxlit{(quasisyntax} @meta{datum}@stxlit{)}.
- @item @stxlit{#,}@meta{datum}
- means @stxlit{(unsyntax} @meta{datum}@stxlit{)}.
- This abbreviation is currently only recognized when nested inside an explicit
- @stxlit{#`}@meta{datum} form,
- because of a conflict with SRFI-10 named constructors.
- @item @stxlit{#,@@}@meta{datum}
- means @stxlit{(unsyntax-splicing} @meta{datum}@stxlit{)}.
- @item @meta{datum1}@stxlit{:}@meta{datum2}
- means @stxlit{($lookup$} @meta{datum1} @stxlit{(quasiquote} @meta{datum2}@stxlit{))}.
- @xref{Colon notation}.
- @item @stxlit{[}@meta{expression} ...@stxlit{]}
- means @stxlit{($bracket-list$} @meta{expression} ...@stxlit{)}.
- @item @meta{operator}@stxlit{[}@meta{expression} ...@stxlit{]}
- means @stxlit{($bracket-apply$} @meta{operator} @meta{expression} ...@stxlit{)}.
- @end table
- @node Hash-prefixed forms
- @section Hash-prefixed forms
- A number of different special forms are indicated by an
- initial hash (number) symbols (@code{#}).
- Here is a table summarizing them.
- Case is ignored for the character followed the @code{#}.
- Thus @code{#x} and @code{#X} are the same.
- @table @asis
- @item @stxlit{#:}@var{keyword}
- Guile-style @ref{Keywords,keyword} syntax.
- @item @stxlit{#\\}
- @ref{meta-character, Character literals, Character literals}.
- @item @stxlit{#!}
- @xref{Special named constants}.
- @item @stxlit{#`}@var{datum}
- Equivalent to @code{(quasisyntax @var{datum})}.
- Convenience syntax for syntax-case macros.
- @item @stxlit{#'}@var{datum}
- Equivalent to @code{(syntax @var{datum})}.
- Convenience syntax for syntax-case macros.
- @item @stxlit{#,}@var{datum}
- Equivalent to @code{(unsyntax @var{datum})}.
- Currently only recognized when inside a @code{#`@var{template}} form.
- Convenience syntax for syntax-case macros.
- @item @stxlit{#,(}@var{name} @var{datum} ...@stxlit{)}
- Special named constructors.
- This syntax is deprecated, because it conflicts with @code{unsyntax}.
- It is only recognized when @emph{not} in a @code{#`@var{template}} form.
- @item @stxlit{#,@@}@var{datum}
- Equivalent to @code{(unsyntax-splicing @var{datum})}.
- @item @stxlit{#(}
- A vector.
- @item @stxlit{#|}
- Start of nested-comment.
- @item @stxlit{#/}@var{regex}@stxlit{/}
- @xref{Regular expressions}.
- @item @stxlit{#<}
- @xref{XML literals}.
- @item @stxlit{#;}@var{datum}
- A datum comment - the @var{datum} is ignored.
- (An @var{interlexeme-space} may appear before the @var{datum}.)
- @item @stxlit{#}@var{number}@stxlit{=}@var{datum}
- A reference definition, allowing cyclic and shared structure.
- Equivalent to the @var{datum}, but also defines an association between
- the integer @var{number} and that @var{datum}, which can be
- used by a subsequent @code{#@var{number}#} form.
- @item @stxlit{#}@var{number}@stxlit{#}
- A back-reference, allowing cyclic and shared structure.
- @item @stxlit{#}@var{R}@stxlit{a}@var{datum}
- An @ref{array-literals,array literal},
- for a multi-dimensional array of rank @var{R}.
- @item @stxlit{#b}
- A binary (base-2) number.
- @item @stxlit{#d}
- A decimal (base-10) number.
- @item @stxlit{#e}
- A prefix to treat the following number as exact.
- @item @stxlit{#f}
- @itemx @stxlit{#false}
- The standard boolean false object.
- @item @stxlit{#f}@var{n}@stxlit{(}@var{number} ...@stxlit{)}
- A uniform vector of floating-point numbers.
- The parameter @var{n} is a precision, which can be 32 or 64.
- @xref{Uniform vectors}.
- @item @stxlit{#i}
- A prefix to treat the following number as inexact.
- @item @stxlit{#o}
- An octal (base-8) number.
- @item @stxlit{#}@var{base}@stxlit{r}
- A number in the specified @var{base} (radix).
- @item @stxlit{#s}@var{n}@stxlit{(}@var{number} ...@stxlit{)}
- A uniform vector of signed integers.
- The parameter @var{n} is a precision, which can be 8, 16, 32, or 64.
- @xref{Uniform vectors}.
- @item @stxlit{#t}
- @itemx @stxlit{#true}
- The standard boolean true object.
- @item @stxlit{#u}@var{n}@stxlit{(}@var{number} ...@stxlit{)}
- A uniform vector of unsigned integers.
- The parameter @var{n} is a precision, which can be 8, 16, 32, or 64.
- @xref{Uniform vectors}.
- @item @stxlit{#x}
- A hexadecimal (base-16) number.
- @end table
- The follow named constructor forms are supported:
- @table @asis
- @item @stxlit{#,(path} @var{path}@stxlit{)}
- @item @stxlit{#,(filepath} @var{path}@stxlit{)}
- @item @stxlit{#,(URI} @var{path}@stxlit{)}
- @item @stxlit{#,(symbol} @var{local-name} [@var{uri} [@var{prefix}]]@stxlit{)}
- @itemx @stxlit{#,(symbol} @var{local-name} @var{namespace}@stxlit{)}
- @item @stxlit{#,(namespace} @var{uri} [@var{prefix}]@stxlit{)}
- @item @stxlit{#,(duration} @var{duration}@stxlit{)}
- @end table
- @node Primitive expression syntax
- @section Primitive expression syntax
- @display
- @stxdef{expression} @stxref{literal-expression} | @stxref{variable-reference}
- | @stxref{procedure-call} | TODO
- @end display
- @anchor{literal-expression}
- @subsection Literal expressions
- @display
- @stxdef{literal-expression} @stxlit{(quote} @stxref{datum}@stxlit{)}
- | @stxlit{'} @stxref{datum}
- | @var{constant}
- @stxdef{constant} @var{number} | @meta{boolean} | @meta{character} | @meta{string}
- @end display
- @code{(quote @var{datum})} evaluates to @var{datum},
- which may be any external representation of a Scheme object.
- This notation is used to include literal constants in Scheme code.
- @example
- (quote a) @result{} a
- (quote #(a b c)) @result{} #(a b c)
- (quote (+ 1 2)) @result{} (+ 1 2)
- @end example
- @code{(quote @var{datum})} may be abbreviated as @code{'@var{datum}}.
- The two notations are equivalent in all respects.
- @example
- ’a @result{} a
- ’#(a b c) @result{} #(a b c)
- ’() @result{} ()
- ’(+ 1 2) @result{} (+ 1 2)
- ’(quote a) @result{} (quote a)
- ’’a @result{} (quote a)
- @end example
- Numerical constants, string constants, character constants,
- bytevector constants, and boolean constants evaluate to
- themselves; they need not be quoted.
- @example
- 145932 @result{} 145932
- #t @result{} #t
- "abc" @result{} "abc"
- @end example
- @c #vu8(2 24 123) @result{} #vu8(2 24 123)
- Note that @ref{Keywords,keywords} need to be quoted,
- unlike some other Lisp/Scheme dialect, including Common Lisp,
- and earlier versions of Kawa. (Kawa currently evaluates a non-quoted
- keyword as itself, but that will change.)
- @anchor{variable-reference}
- @subsection Variable references
- @display
- @stxdef{variable-reference} @stxref{identifier}
- @end display
- An expression consisting of a variable is a variable reference if it is
- not a macro use (see below). The value of the variable reference is the
- value stored in the location to which the variable is bound. It is a
- syntax violation to reference an unbound variable.
- The following example assumes the base library has been
- imported:
- @example
- (define x 28)
- x @result{} 28
- @end example
- @subsection Procedure calls
- @display
- @stxdef{procedure-call} @stxlit{(}@stxref{operator} @stxref{operand} @dots{})
- @stxdef{operator} @stxref{expression}
- @stxdef{operand} @stxref{expression}
- | @stxref{keyword} @stxref{expression}
- | @code{@@} @stxref{expression}
- | @code{@@:} @stxref{expression}
- @end display
- A procedure call consists of expressions for the procedure to be called
- and the arguments to be passed to it, with enclosing parentheses. A
- form in an expression context is a procedure call if @meta{operator} is
- not an identifier bound as a syntactic keyword.
- When a procedure call is evaluated, the operator and operand expressions
- are evaluated (in an unspecified order) and the resulting procedure is
- passed the resulting arguments.
- @example
- (+ 3 4) @result{} 7
- ((if #f + *) 3 4) @result{} 12
- @end example
- The syntax @stxref{keyword} @var{expression} is a @dfn{keyword argument}.
- This is a mechanism for specifying arguments using a name rather than position,
- and is especially useful for procedures with many optional paramaters.
- Note that @stxref{keyword} must be literal, and cannot be the
- result from evaluating a non-literal expression.
- (This is a change from previous versions of Kawa,
- and is different from Common Lisp and some other Scheme dialects.)
- An expression prefixed by @code{@@} or @code{@@:} is
- a splice argument. The following expression must evaluate to an
- ``argument list'' (see @ref{Application and Arguments Lists} for details);
- each element in the argument
- becomes a separate argument when call the @var{operator}.
- (This is very similar to the ``spread'' operator is EcmaScript 6.)
- @node Colon notation
- @section Property access using colon notation
- @cindex colon notation
- The @dfn{colon notation} accesses named parts (properties) of a value.
- It is used to get and set fields, call methods, construct compound symbols,
- and more.
- Evaluating the form @code{@var{owner}:@var{property}}
- evaluates the @code{@var{owner}} then it extracts the named @code{@var{property}} of the result.
- @display
- @stxdef{property-access-abbreviation} @stxref{property-owner-expression}@stxlit{:}@stxref{property-name}
- @stxdef{property-owner-expression} @stxref{expression}
- @stxdef{property-name} @stxref{identifier} | @stxlit{,}@stxref{expression}
- @end display
- The @var{property-name} is usually a literal name,
- but it can be an unquoted @var{expression} (i.e. following a @code{,}),
- in which case the name is evaluated at run-time.
- No separators are allowed on either side of the colon.
- The input syntax @code{@var{owner}:@var{part}} is translated by
- the Scheme reader to the internal representation @code{($lookup$ @var{owner} (quasiquote @var{part}))}.
- @subsection Part lookup rules
- Evaluation proceeds as follows.
- First @var{property-owner-expression} is
- evaluated to yield an @var{owner} object.
- Evaluating the @var{property-name} yields a @var{part} name,
- which is a simple symbol: Either
- the literal @var{identifier}, or the result of evaluating the
- property-name @var{expression}.
- If the @var{expression} evaluates to a string, it is converted to
- a symbol, as if using @code{string->symbol}.
- @itemize
- @item
- If the @var{owner} implements @code{gnu.mapping.HasNamedParts},
- then the result is that of invoking the @code{get} method of the @var{owner}
- with the @var{part} name as a parameter.
- As a special case of this rule, if @var{owner} is a
- @code{gnu.mapping.Namespace}, then the result is the
- @ref{Namespaces,compound symbol in that namespace}.
- @item
- If @var{owner} is a @code{java.lang.Class} or a @code{gnu.bytecode.ObjectType},
- the result is the static member named @var{part}
- (i.e. a static field, method, or member class).
- @item
- If @var{owner} is a @code{java.lang.Package} object, we get the member
- class or sub-package named @var{part}.
- @item
- Otherwise, we look for a named member (instance member or field).
- Note you can't use colon notation to invoke instance methods
- of a @code{Class}, because it will match a previous rule.
- For example if you want to invoke the @code{getDeclaredMethod}
- method of the @code{java.util.List} , you can't write @code{(java.util.List:getDeclaredMethod} because that will look for a static method in @code{java.util.List}.
- Instead, use the @code{invoke} or @code{invoke-sttic} method. For example:
- @code{(invoke java.util.List 'getDeclaredMethod)}.
- @end itemize
- If the colon form is on the left-hand-side of an assignment (@code{set!}),
- then the named part is modified as appropriate.
- @c We will look into examples and details below.
- @c @subsection The @code{HasNamedParts} case
- @c @subsection Static parts of classes and packages
- @c @subsection Instance parts
- @subsection Specific cases
- Some of these are deprecated;
- more compact and readable forms are usually preferred.
- @subsubsection Invoking methods
- @display
- @stxlit{(}@var{instance}@stxlit{:}@stxref{method-name} @var{arg} ...@stxlit{)}
- @stxlit{(}@var{class}@stxlit{:}@stxref{method-name} @var{instance} @var{arg} ...@stxlit{)}
- @stxlit{(}@var{class}@stxlit{:}@stxref{method-name} @var{arg} ...@stxlit{)}
- @stxlit{(*:}@stxref{method-name} @var{instance} @var{arg} ...@stxlit{)}
- @end display
- For details @pxref{Method operations}.
- @subsubsection Accessing fields
- @display
- @var{class}@stxlit{:}@stxref{field-name}
- @var{instance}@stxlit{:}@stxref{field-name}
- @stxlit{(}@var{prefix}@stxlit{:.}@stxref{field-name} @var{instance}@stxlit{)}
- @end display
- For details @pxref{Field operations}.
- @subsubsection Type literal
- @display
- @stxlit{(}@stxref{type}@stxlit{:<>)}
- @end display
- Returns the @var{type}.
- Deprecated; usually you can just write:
- @example
- @var{type}
- @end example
- @subsubsection Type cast
- @display
- @stxlit{(}@stxref{type}@stxlit{:}@stxlit{@atchar{}} @stxref{expression}@stxlit{)}
- @end display
- Performs a cast.
- Deprecated; usually you can just write:
- @example
- ->@var{type}
- @end example
- @subsubsection Type test
- @display
- @stxlit{(}@stxref{type}@stxlit{:instanceof?} @stxref{expression}@stxlit{)}
- @end display
- Deprecated; usually you can just write:
- @example
- (@var{type}? @var{expression})
- @end example
- @subsubsection New object construction
- @display
- @stxlit{(}@stxref{type}@stxlit{:new} @var{arg} ...@stxlit{)}
- @end display
- Deprecated; usually you can just write:
- @display
- @stxlit{(}@stxref{type} @var{arg} ...@stxlit{)}
- @end display
- @subsubsection Getting array length
- @display
- @stxref{expression}@stxlit{:length}
- @stxlit{(}@stxref{expression}@stxlit{:.length)}
- @end display
- @node Bodies
- @section Programs and Bodies
- @anchor{program units}
- @subheading Program units
- A @meta{program-unit} consists of a sequence of definitions and expressions.
- @display
- @stxdef{program-unit} @atleastone{@stxref{library-definition}} [@stxref{statements}]
- | @stxref{statements}
- @stxdef{statements} @atleastone{@stxref{statement}}
- @stxdef{statement} @var{definition} | @stxref{expression} | @stxlit{(begin} @arbno{@stxref{statement}} @stxlit{)}
- @end display
- Typically a @meta{program-unit} corresponds to a single source file
- (i.e.a named file in the file system). Evaluating a @meta{program-unit}
- first requires the Kawa processor to analyze
- the whole @meta{program-unit} to determine which names are defined by the
- definitions, and then evaluates each @meta{statement} in order in the context
- of the defined names. The value of an @meta{expression} is normally
- discarded, but may be printed out instead, depending on the evaluating context.
- The read-eval-print-loop (REPL) reads one or more lines until it gets
- a valid @meta{program-unit}, and evaluates it as above, except that the
- values of expressions are printed to the console (as if using the
- @code{display} function). Then the REPL reads and evaluates
- another @meta{program-unit}, and so on. A definition in an earlier
- @meta{program-unit} is remembered and is visible in a later @meta{program-unit}
- unles it is overridden.
- @cindex encoding specifier
- @cindex coding specifier
- A comment in the first 2 lines of a source file may contain an encoding
- specification. This can be used to tell the reader what kind of character
- set encoding is used for the file. This only works for a character
- encoding that is compatible with ASCII (in the sense that if the
- high-order bit is clear then it's an ASCII character),
- and that are no non-ASCI characters in the lines upto and including
- the encoding specification.
- A basic example is:
- @example
- ;; -*- coding: utf-8 -*-
- @end example
- In general any string that matches the following regular expression works:
- @example
- coding[:=]\s*([-a-zA-Z0-9]+)
- @end example
- @subheading Libraries
- @anchor{implicit library}
- A @meta{program-unit} may contain @meta{library-definitions}.
- In addition, any @meta{statements} in @meta{program-unit} comprise
- an @dfn{implicit library}, in that it can be given a name, and referenced
- from other libraries.
- Certain names defined in the @meta{program-unit} can be exported,
- and then they can be imported by other libraries.
- For more information @pxref{Module classes}.
- It is recommended but not required that:
- @itemize
- @item
- There should be at most one @meta{library-definition} in a @meta{program-unit}.
- @item
- The @meta{library-name} of the @meta{library-definition} should
- match the name of the source file. For example:
- @example
- (define-library (foo bar) ...)
- @end example
- should be in a file named @code{foo/bar.scm}.
- @item
- If there is a @meta{library-definition}, there should
- be no extra @meta{statements} - i.e no implicit library definition.
- (It is disallowed to @code{export} any definitions from the
- implicit library if there is also a @meta{library-definition}.)
- @end itemize
- Following these recommendations makes it easier to locate
- and organize libraries.
- However, having multiple libraries in a single @meta{program-unit}
- is occasionally useful for source distribution and for testing.
- @subheading Bodies
- The @meta{body} of a @func{lambda}, @func{let}, @func{let*},
- @func{let-values}, @func{let*-values}, @func{letrec}, or @func{letrec*}
- expression, or that of a definition with a body consists of zero or more
- definitions or expressions followed by a final expression.
- (Standard Scheme requires that all definitions precede all expressions.)
- @display
- @stxdef{body} @arbno{@stxref{statement}}
- @end display
- Each identifier defined by a definition is local to the @meta{body}.
- That is, the identifier is bound, and the region of the binding is the
- entire @meta{body}.
- Example:
- @example
- (let ((x 5))
- (define foo (lambda (y) (bar x y)))
- (define bar (lambda (a b) (+ (* a b) a)))
- (foo (+ x 3)))
- @result{} 45
- @end example
- When @func{begin}, @func{let-syntax}, or @func{letrec-syntax} forms
- occur in a body prior to the first expression, they are spliced into the
- body. Some or all of the body, including portions wrapped in
- @func{begin}, @func{let-syntax}, or @func{letrec-syntax} forms, may be
- specified by a macro use.
- An expanded @meta{body} containing variable definitions can be
- converted into an equivalent @func{letrec*} expression.
- (If there is a definition following expressions you may need to
- convert the expressions to dummy definitions.) For example,
- the @func{let} expression in the above example is equivalent to
- @example
- (let ((x 5))
- (letrec* ((foo (lambda (y) (bar x y)))
- (bar (lambda (a b) (+ (* a b) a))))
- (foo (+ x 3))))
- @end example
- @node Syntax and conditional compilation
- @section Syntax and conditional compilation
- @subheading Feature testing
- @deffn Syntax cond-expand @arbno{@stxref{cond-expand-clause}} [@stxlit{(else} command-or-definition*@stxlit{)}]
- @display
- @stxdef{cond-expand-clause} @stxlit{(}@var{feature-requirement} @var{command-or-definition}*@stxlit{)}
- @findex @i{fff-rec}
- @stxdef{feature-requirement} @stxref{feature-identifier}
- | @stxlit{(and} @arbno{@stxref{feature-requirement}}@stxlit{)}
- | @stxlit{(or} @arbno{@stxref{feature-requirement}}@stxlit{)}
- | @stxlit{(not} @stxref{feature-requirement}@stxlit{)}
- | @stxlit{(library} @stxref{library-name}@stxlit{)}
- @stxdef{feature-identifier} a symbol which is the name or alias of a SRFI
- @end display
- The @code{cond-expand} form tests for the existence of features at
- macro-expansion time. It either expands into the body of one of its
- clauses or signals an error during syntactic
- processing. @code{cond-expand} expands into the body of the first clause
- whose feature requirement is currently satisfied; the @code{else}
- clause, if present, is selected if none of the previous clauses is
- selected.
- The implementation has a set of
- feature identifiers which are ``present'', as well as a set
- of libraries which can be imported.
- The value of a
- @meta{feature-requirement} is determined by replacing each
- @meta{feature-identifier} by @code{#t} if it is present
- (and @code{#f} otherwise);
- replacing @code{(library @meta{library-name})}
- by @code{#t} if @meta{library-name} is importable (and @code{#f} otherwise);
- and then evaluating the resulting expression as a Scheme boolean expression
- under the normal interpretation of @code{and}, @code{or}, and @code{not}.
- Examples:
- @example
- (cond-expand
- ((and srfi-1 srfi-10)
- (write 1))
- ((or srfi-1 srfi-10)
- (write 2))
- (else))
- @end example
- @example
- (cond-expand
- (command-line
- (define (program-name) (car (argv)))))
- @end example
- The second example assumes that @code{command-line} is an alias for some
- feature which gives access to command line arguments. Note that an error
- will be signaled at macro-expansion time if this feature is not present.
- You can use @code{java-6}, @code{java-7}, @code{java-8},
- or @code{java-9} to check if the underlying Java
- is a specific version or newer.
- For example the name @code{java-7} matches for
- either Java 7, Java 8, or newer, as
- reported by @code{System} property @code{"java.version"}.
- You can use @code{class-exists:@var{ClassName}} to check
- if @code{@var{ClassName}} exists at compile-time.
- The identifier @code{class-exists:org.example.MyClass}
- is roughly equivalent to the test @code{(library (org example MyClass))}.
- (The latter has some special handling for @code{(srfi ...)} as well
- as builtin Kawa classes.)
- The feature @code{in-http-server} is defined in a
- @ref{Self-configuring page scripts,self-configuring web page scripts},
- and more specifically @code{in-servlet} in a @ref{Servlets,servlet container}.
- @end deffn
- @deffn Procedure features
- Returns a list of feature identifiers which @code{cond-expand}
- treats as true.
- This not a complete list - for example @code{class-exists:@var{ClassName}}
- feature identifiers are not included.
- It is an error to modify this list.
- Here is an example of what @code{features} might return:
- @example
- (features) @result{}
- (complex exact-complex full-unicode java-7 java-6 kawa
- ratios srfi-0 srfi-4 srfi-6 srfi-8 srfi-9 srfi-11
- srfi-16 srfi-17 srfi-23 srfi-25 srfi-26 srfi-28 srfi-30
- srfi-39 string-normalize-unicode threads)
- @end example
- @end deffn
- @subheading File inclusion
- @anchor{include}
- @anchor{include-relative}
- @deffn Syntax include @atleastone{path}
- @deffnx Syntax include-relative @atleastone{path}
- @deffnx Syntax include-ci @atleastone{path}
- These take one or more path names expressed as string literals,
- find corresponding files, read the contents of the files in the specified order
- as if by repeated applications of @code{read}, and effectively
- replace the @code{include} with a @code{begin} form
- containing what was read from the files.
- You can control the search path used for @code{include}
- by setting the @code{kawa.include.path} property. For example:
- @example
- $ kawa -Dkawa.include.path="|:/opt/kawa-includes"
- @end example
- The special @code{"|"} path element means to search
- relative to the directory containing the including source file.
- The default search path is @code{"|:."} which means to first
- search the directory containing the including source file,
- and then search the directory specified by @code{(current-path)}.
- The search path for @code{include-relative} prepends @code{"|"}
- before the search path used by @code{include}, so it always
- searches first the directory containing the including source file.
- Note that if the default search path is used then @code{include}
- and @code{include-relative} are equivalent; there is only a difference
- if the @code{kawa.include.path} property changes the default.
- Using @code{include-ci} is like @code{include}, except that it reads each
- file as if it began with the @code{#!fold-case} directive.
- @end deffn
- @node Macros
- @section Macros
- @anchor{macro-reference}
- Libraries and top--level programs can define and use new kinds of
- derived expressions and definitions called @emph{syntactic abstractions}
- or @emph{macros}. A syntactic abstraction is created by binding a
- keyword to a @emph{macro transformer} or, simply, @emph{transformer}.
- The transformer determines how a use of the macro (called a @emph{macro
- use}) is transcribed into a more primitive form.
- Most macro uses have the form:
- @example
- (@meta{keyword} @meta{datum} @dots{})
- @end example
- @noindent
- where @meta{keyword} is an identifier that uniquely determines the kind
- of form. This identifier is called the @emph{syntactic keyword}, or
- simply @emph{keyword}. The number of @meta{datum}s and the syntax of
- each depends on the syntactic abstraction.
- Macro uses can also take the form of improper lists, singleton
- identifiers, or @func{set!} forms, where the second subform of the
- @func{set!} is the keyword:
- @example
- (@meta{keyword} @meta{datum} @dots{} . @meta{datum})
- @meta{keyword}
- (set! @meta{keyword} @meta{datum})
- @end example
- The @func{define-syntax}, @func{let-syntax} and @func{letrec-syntax}
- forms create bindings for keywords, associate them with macro
- transformers, and control the scope within which they are visible.
- The @func{syntax-rules} and @func{identifier-syntax} forms create
- transformers via a pattern language. Moreover, the @func{syntax-case}
- form allows creating transformers via arbitrary Scheme code.
- Keywords occupy the same name space as variables. That is, within the
- same scope, an identifier can be bound as a variable or keyword, or
- neither, but not both, and local bindings of either kind may shadow
- other bindings of either kind.
- Macros defined using @func{syntax-rules} and @func{identifier-syntax}
- are ``hygienic'' and ``referentially transparent'' and thus preserve
- Scheme's lexical scoping.
- @itemize
- @item
- If a macro transformer inserts a binding for an identifier (variable or
- keyword) not appearing in the macro use, the identifier is in effect
- renamed throughout its scope to avoid conflicts with other identifiers.
- @item
- If a macro transformer inserts a free reference to an identifier, the
- reference refers to the binding that was visible where the transformer
- was specified, regardless of any local bindings that may surround the
- use of the macro.
- @end itemize
- Macros defined using the @func{syntax-case} facility are also hygienic
- unless @func{datum->syntax} is used.
- Kawa supports most of the @code{syntax-case} feature.
- Syntax definitions are valid wherever definitions are.
- They have the following form:
- @deffn Syntax define-syntax keyword @stxref{transformer-spec}
- The @var{keyword} is a identifier, and @var{transformer-spec}
- is a function that maps syntax forms to syntax forms,
- usually an instance of @code{syntax-rules}.
- If the @code{define-syntax} occurs at the top level, then the top-level
- syntactic environment is extended by binding the @var{keyword}
- to the specified transformer, but existing references to any top-level
- binding for @var{keyword} remain unchanged. Otherwise, it is an
- @dfn{internal syntax definition}, and is local to the @var{body}
- in which it is defined.
- @example
- (let ((x 1) (y 2))
- (define-syntax swap!
- (syntax-rules ()
- ((swap! a b)
- (let ((tmp a))
- (set! a b)
- (set! b tmp)))))
- (swap! x y)
- (list x y)) @result{} (2 1)
- @end example
- Macros can expand into definitions in any context that permits them.
- However, it is an error for a definition to define an identifier
- whose binding has to be known in order to determine the meaning
- of the definition itself, or of any preceding definition that belongs
- to the same group of internal definitions.
- @end deffn
- @deffn Syntax define-syntax-case name @stxlit{(}literals@stxlit{)} @stxlit{(}pattern expr@stxlit{)} ...
- A convenience macro to make it easy to define @code{syntax-case}-style macros.
- Defines a macro with the given @var{name} and list of @var{literals}.
- Each @var{pattern} has the form of a @code{syntax-rules}-style pattern,
- and it is matched against the macro invocation syntax form.
- When a match is found, the corresponding @var{expr} is evaluated.
- It must evaluate to a syntax form,
- which replaces the macro invocation.
- @example
- (define-syntax-case macro-name (literals)
- (pat1 result1)
- (pat2 result2))
- @end example
- is equivalent to:
- @example
- (define-syntax macro-name
- (lambda (form)
- (syntax-case form (literals)
- (pat1 result1)
- (pat2 result2))))
- @end example
- @end deffn
- @deffn Syntax define-macro @stxlit{(}name lambda-list@stxlit{)} form ...
- @emph{This form is deprecated.}
- Functionally equivalent to @code{defmacro}.
- @end deffn
- @deffn Syntax defmacro name lambda-list form ...
- @emph{This form is deprecated.}
- Instead of
- @example
- (defmacro (@var{name} ...)
- (let ... `(... ,@var{exp} ...)))
- @end example
- you should probably do:
- @example
- (define-syntax-case @var{name} ()
- ((_ ...) (let #`(... #,@var{exp} ...))))
- @end example
- and instead of
- @example
- (defmacro (@var{name} ... @var{var} ...) `(... @var{var} ...))
- @end example
- you should probably do:
- @example
- (define-syntax-case @var{name} ()
- ((_ ... @var{var} ...) #`(... @var{var} ...))
- @end example
- Defines an old-style macro a la Common Lisp,
- and installs @code{(lambda @var{lambda-list} @var{form} ...)}
- as the expansion function for @var{name}.
- When the translator sees an application of @var{name},
- the expansion function is called with the rest of the application
- as the actual arguments. The resulting object must be a Scheme
- source form that is futher processed (it may be repeatedly macro-expanded).
- @end deffn
- @deffn Procedure gentemp
- Returns a new (interned) symbol each time it is called.
- The symbol names are implementation-dependent.
- (This is not directly macro-related, but is often used in conjunction
- with @code{defmacro} to get a fresh unique identifier.)
- @end deffn
- @deffn Procedure expand form
- The result of evaluating @var{form} is treated as a Scheme expression,
- syntax-expanded to internal form, and then converted back to (roughly)
- the equivalent expanded Scheme form.
- This can be useful for debugging macros.
- To access this function, you must first @code{(require 'syntax-utils)}.
- @example
- (require 'syntax-utils)
- (expand '(cond ((> x y) 0) (else 1))) @result{} (if (> x y) 0 1)
- @end example
- @end deffn
- @subsection Pattern language
- A @meta{transformer-spec} is an expression that evaluates to a
- transformer procedure, which takes an input form and returns a
- resulting form. You can do general macro-time compilation with such a
- procedure, commonly using @code{syntax-case} (which is documented
- in the R6RS library specification).
- However, when possible it is better to use the simpler
- pattern language of @code{syntax-rules}:
- @display
- @stxdef{transformer-spec}
- @stxlit{(syntax-rules (} @arbno{@stxref{tr-literal}} @stxlit{)} @arbno{@stxref{syntax-rule}}@stxlit{)}
- | @stxlit{(syntax-rules} @stxref{ellipsis} @stxlit{(} @arbno{@stxref{tr-literal}} @stxlit{)} @arbno{@stxref{syntax-rule}}@stxlit{)}
- | @stxref{expression}
- @stxdef{syntax-rule} @stxlit{(}@stxref{list-pattern} @stxref{syntax-template}@stxlit{)}
- @stxdef{tr-literal} @stxref{identifier}
- @stxdef{ellipsis} @stxref{identifier}
- @end display
- An instance of @code{syntax-rules} produces a new
- macro transformer by specifying a sequence of hygienic
- rewrite rules. A use of a macro whose keyword is associated
- with a transformer specified by @code{syntax-rules} is matched
- against the patterns contained in the @meta{syntax-rule}s
- beginning with the leftmost syntax rule . When a match is
- found, the macro use is transcribed hygienically according
- to the template.
- The optional @meta{ellipsis} species a symbol used to indicate
- repetition; it defaults to @code{...} (3 periods).
- @display
- @stxdef{syntax-pattern}
- @stxref{identifier} | @stxref{constant} | @stxref{list-pattern} | @stxref{vector-pattern}
- @stxdef{list-pattern} @stxlit{(} @arbno{@stxref{syntax-pattern}} @stxlit{)}
- | @stxlit{(} @stxref{syntax-pattern} @arbno{@stxref{syntax-pattern}} @stxlit{.} @stxref{syntax-pattern} @stxlit{)}
- | @stxlit{(} @arbno{@stxref{syntax-pattern}} @stxref{syntax-pattern} @stxref{ellipsis} @arbno{@stxref{syntax-pattern}} @stxlit{)}
- | @stxlit{(} @arbno{@stxref{syntax-pattern}} @stxref{syntax-pattern} @stxref{ellipsis} @arbno{@stxref{syntax-pattern}} @stxlit{.} @stxref{syntax-pattern}@stxlit{)}
- @stxdef{vector-pattern} @stxlit{#(} @arbno{@stxref{syntax-pattern}} @stxlit{)}
- | @stxlit{#(} @arbno{@stxref{syntax-pattern}} @stxref{syntax-pattern} @stxref{ellipsis} @arbno{@stxref{syntax-pattern}} @stxlit{)}
- @end display
- An identifier appearing within a pattern can be an underscore
- (@code{_}), a literal identifier listed in the list of @meta{tr-literal}s,
- or the @meta{ellipsis}. All other identifiers appearing within a
- pattern are pattern variables.
- The outer @var{syntax-list} of the pattern in a @meta{syntax-rule}
- must start with an identifier. It is not involved in the matching and
- is considered neither a pattern variable nor a literal identifier.
- Pattern variables match arbitrary input elements and are
- used to refer to elements of the input in the template.
- It is an error for the same pattern variable to appear more
- than once in a @meta{syntax-pattern}.
- Underscores also match arbitrary input elements but are
- not pattern variables and so cannot be used to refer to
- those elements. If an underscore appears in the literals
- list, then that takes precedence and underscores in the
- pattern match as literals. Multiple underscores can
- appear in a @meta{syntax-pattern}.
- Identifiers that appear in @code{(@arbno{@meta{tr-literal}})} are interpreted
- as literal identifiers to be matched against corresponding
- elements of the input. An element in the input matches a
- literal identifier if and only if it is an identifier and either
- both its occurrence in the macro expression and its occurrence
- in the macro definition have the same lexical binding,
- or the two identifiers are the same and both have no lexical
- binding.
- A subpattern followed by ellipsis can match zero or
- more elements of the input, unless ellipsis appears in the
- literals, in which case it is matched as a literal.
- More formally, an input expression @var{E} matches a pattern @var{P}
- if and only if:
- @itemize @bullet
- @item
- @var{P} is an underscore (@stxlit{_}); or
- @item
- @var{P} is a non-literal identifier; or
- @item
- @var{P} is a literal identifier and @var{E} is an identifier with the
- same binding; or
- @item
- @var{P} is a list @stxlit{(}@var{P@sub{1}} ... @var{P@sub{n}}@stxlit{)} and
- @var{E} is a list of @var{n} elements
- that match @var{P@sub{1}} through @var{P@sub{n}}, respectively; or
- @item
- @var{P} is an improper list
- @stxlit{(}@var{P@sub{1}} ... @var{P@sub{n}} @stxlit{.} @var{P@sub{n+1}}@stxlit{)} and
- @var{E} is a list or improper list of @var{n} or more elements that
- match @var{P@sub{1}} through @var{P@sub{n}}, respectively,
- and whose @var{n}th tail matches @var{P@sub{n+1}}; or
- @item
- @var{P} is of the form
- @stxlit{(}@var{P@sub{1}} ... @var{P@sub{k}} @var{P@sub{e}} @var{ellipsis} @var{P@sub{k+1}} ... @var{P@sub{k+l}}@stxlit{)} where @var{E} is a proper list of @var{n} elements,
- the first @var{k} of which match @var{P@sub{1}} through @var{P@sub{k}},
- respectively, whose
- next @var{n-k-l} elements each match @var{P@sub{e}}, and whose remaining
- @var{l} elements match @var{P@sub{k+1}} through @var{P@sub{k+l}}; or
- @item
- @var{P} is of the form
- @stxlit{(}@var{P@sub{1}} ... @var{P@sub{k}} @var{P@sub{e}} @var{ellipsis} @var{P@sub{k+1}} ... @var{P@sub{k+l}} @stxlit{.} @var{P@sub{x}}@stxlit{)}
- where @var{E} is a list or improper list of @var{n} elements,
- the first @var{k} of which match @var{P@sub{1}} through @var{P@sub{k}},
- whose next @var{n-k-l} elements each match @var{P@sub{e}}, and whose
- remaining @var{l} elements match @var{P@sub{k+1}} through @var{P@sub{k+l}},
- and whose @var{n}th and final @code{cdr} matches @var{P@sub{x}}; or
- @item
- @var{P} is a vector of the form
- @stxlit{#(}@var{P@sub{1}} ... @var{P@sub{n}}@stxlit{)} and @var{E} is a
- vector of @var{n} elements that match @var{P@sub{1}} through @var{P@sub{n}}; or
- @item
- @var{P} is of the form
- @stxlit{#(}@var{P@sub{1}} ... @var{P@sub{k}} @var{P@sub{e}} @var{ellipsis} @var{P@sub{k+1}} ... @var{P@sub{k+l}}@stxlit{)}
- where @var{E} is a vector of @var{n} elements the first
- @var{k} of which match @var{P@sub{1}} through @var{P@sub{k}},
- whose next @var{n-k-l} elements each match @var{P@sub{e}},
- and whose remaining @var{l} elements match @var{P@sub{k+1}}
- through @var{P@sub{k+l}}; or
- @item
- @var{P} is a constant and E is equal to @var{P} in the sense of the
- @code{equal?} procedure.
- @end itemize
- It is an error to use a macro keyword, within the scope of
- its binding, in an expression that does not match any of
- the patterns.
- @display
- @stxdef{syntax-template} @stxref{identifier} | @stxref{constant}
- | @stxlit{(}@arbno{@stxref{template-element}}@stxlit{)}
- | @stxlit{(}@stxref{template-element} @arbno{@stxref{template-element}} @stxlit{.} @stxref{syntax-template} @stxlit{)}
- | @stxlit{(} @stxref{ellipsis} @stxref{syntax-template}@stxlit{)}
- @stxdef{template-element} @stxref{syntax-template} [@stxref{ellipsis}]
- @end display
- When a macro use is transcribed according to the template
- of the matching @meta{syntax-rule}, pattern variables that occur
- in the template are replaced by the elements they match in
- the input. Pattern variables that occur in subpatterns followed
- by one or more instances of the identifier @meta{ellipsis} are
- allowed only in subtemplates that are followed by as many
- instances of @meta{ellipsis} . They are replaced in the output by
- all of the elements they match in the input, distributed as
- indicated. It is an error if the output cannot be built up
- as specified.
- Identifiers that appear in the template but are not pattern
- variables or the identifier @meta{ellipsis} are inserted into the output
- as literal identifiers. If a literal identifier is inserted as a
- free identifier then it refers to the binding of that identifier
- within whose scope the instance of @code{syntax-rules} appears.
- If a literal identifier is inserted as a bound identifier then
- it is in effect renamed to prevent inadvertent captures of
- free identifiers.
- A template of the
- form @stxlit{(}@meta{ellipsis} @meta{template}@stxlit{)} is
- identical to @meta{template}, except that @meta{ellipses}
- within the template have no special meaning.
- That is, any @meta{ellipses} contained within
- @meta{template} are treated as ordinary identifiers. In particular,
- the template @stxlit{(}@meta{ellipsis} @meta{ellipsis}@stxlit{)}
- produces a single @meta{ellipsis}. This allows syntactic
- abstractions to expand into code containing ellipses.
- @example
- (define-syntax be-like-begin
- (syntax-rules ()
- ((be-like-begin name)
- (define-syntax name
- (syntax-rules ()
- ((name expr (... ...))
- (begin expr (... ...))))))))
- (be-like-begin sequence)
- (sequence 1 2 3 4) @result{} 4
- @end example
- @subsection Identifier predicates
- @deffn Procedure {identifier?} @var{obj}
- Return @true{} if @var{obj} is an identifier, i.e., a syntax object
- representing an identifier, and @false{} otherwise.
- The @func{identifier?} procedure is often used within a fender to verify
- that certain subforms of an input form are identifiers, as in the
- definition of @code{rec}, which creates self--contained recursive
- objects, below.
- @example
- (define-syntax rec
- (lambda (x)
- (syntax-case x ()
- ((_ x e)
- (identifier? #'x)
- #'(letrec ((x e)) x)))))
- (map (rec fact
- (lambda (n)
- (if (= n 0)
- 1
- (* n (fact (- n 1))))))
- '(1 2 3 4 5)) @result{} (1 2 6 24 120)
-
- (rec 5 (lambda (x) x)) @result{} exception
- @end example
- @end deffn
- The procedures @func{bound-identifier=?} and @func{free-identifier=?}
- each take two identifier arguments and return @true{} if their arguments
- are equivalent and @false{} otherwise. These predicates are used to
- compare identifiers according to their @emph{intended use} as free
- references or bound identifiers in a given context.
- @deffn Procedure {bound-identifier=?} @vari{id} @varii{id}
- @vari{id} and @varii{id} must be identifiers.
- The procedure @func{bound-identifier=?} returns @true{} if a binding for
- one would capture a reference to the other in the output of the
- transformer, assuming that the reference appears within the scope of the
- binding, and @false{} otherwise.
- In general, two identifiers are @func{bound-identifier=?} only if both
- are present in the original program or both are introduced by the same
- transformer application (perhaps implicitly, see @func{datum->syntax}).
- The @func{bound-identifier=?} procedure can be used for detecting
- duplicate identifiers in a binding construct or for other preprocessing
- of a binding construct that requires detecting instances of the bound
- identifiers.
- @end deffn
- @deffn Procedure {free-identifier=?} @vari{id} @varii{id}
- @vari{id} and @varii{id} must be identifiers.
- The @func{free-identifier=?} procedure returns @true{} if and only if
- the two identifiers would resolve to the same binding if both were to
- appear in the output of a transformer outside of any bindings inserted
- by the transformer. (If neither of two like--named identifiers resolves
- to a binding, i.e., both are unbound, they are considered to resolve to
- the same binding.)
- Operationally, two identifiers are considered equivalent by
- @func{free-identifier=?} if and only the topmost matching substitution
- for each maps to the same binding or the identifiers have the same name
- and no matching substitution.
- The @func{syntax-case} and @func{syntax-rules} forms internally use
- @func{free-identifier=?} to compare identifiers listed in the literals
- list against input identifiers.
- @example
- (let ((fred 17))
- (define-syntax a
- (lambda (x)
- (syntax-case x ()
- ((_ id) #'(b id fred)))))
- (define-syntax b
- (lambda (x)
- (syntax-case x ()
- ((_ id1 id2)
- #`(list
- #,(free-identifier=? #'id1 #'id2)
- #,(bound-identifier=? #'id1 #'id2))))))
- (a fred))
- @result{} (#t #f)
- @end example
- The following definition of unnamed @func{let} uses
- @func{bound-identifier=?} to detect duplicate identifiers.
- @example
- (define-syntax let
- (lambda (x)
- (define unique-ids?
- (lambda (ls)
- (or (null? ls)
- (and (let notmem? ((x (car ls)) (ls (cdr ls)))
- (or (null? ls)
- (and (not (bound-identifier=? x (car ls)))
- (notmem? x (cdr ls)))))
- (unique-ids? (cdr ls))))))
- (syntax-case x ()
- ((_ ((i v) ...) e1 e2 ...)
- (unique-ids? #'(i ...))
- #'((lambda (i ...) e1 e2 ...) v ...)))))
- @end example
- The argument @code{#'(i ...)} to @func{unique-ids?} is guaranteed to be
- a list by the rules given in the description of @func{syntax} above.
- With this definition of @func{let}:
- @example
- (let ((a 3) (a 4)) (+ a a)) @result{} @i{syntax error}
- @end example
- However,
- @example
- (let-syntax
- ((dolet (lambda (x)
- (syntax-case x ()
- ((_ b)
- #'(let ((a 3) (b 4)) (+ a b)))))))
- (dolet a))
- @result{} 7
- @end example
- @noindent
- since the identifier @code{a} introduced by @func{dolet} and the
- identifier @code{a} extracted from the input form are not
- @func{bound-identifier=?}.
- Rather than including @code{else} in the literals list as before, this
- version of @func{case} explicitly tests for @code{else} using
- @func{free-identifier=?}.
- @example
- (define-syntax case
- (lambda (x)
- (syntax-case x ()
- ((_ e0 ((k ...) e1 e2 ...) ...
- (else-key else-e1 else-e2 ...))
- (and (identifier? #'else-key)
- (free-identifier=? #'else-key #'else))
- #'(let ((t e0))
- (cond
- ((memv t '(k ...)) e1 e2 ...)
- ...
- (else else-e1 else-e2 ...))))
- ((_ e0 ((ka ...) e1a e2a ...)
- ((kb ...) e1b e2b ...) ...)
- #'(let ((t e0))
- (cond
- ((memv t '(ka ...)) e1a e2a ...)
- ((memv t '(kb ...)) e1b e2b ...)
- ...))))))
- @end example
- With either definition of @func{case}, @code{else} is not recognized as
- an auxiliary keyword if an enclosing lexical binding for @code{else}
- exists. For example,
- @example
- (let ((else @false{}))
- (case 0 (else (write "oops")))) @result{} @i{syntax error}
- @end example
- @noindent
- since @code{else} is bound lexically and is therefore not the same
- @code{else} that appears in the definition of @func{case}.
- @end deffn
- @subsection Syntax-object and datum conversions
- @deffn Procedure {syntax->datum} @var{syntax-object}
- @deffnx {Deprecated procedure} {syntax-object->datum} @var{syntax-object}
- Strip all syntactic information from a syntax object and returns the
- corresponding Scheme datum.
- Identifiers stripped in this manner are converted to their symbolic
- names, which can then be compared with @func{eq?}. Thus, a predicate
- @code{symbolic-identifier=?} might be defined as follows.
- @example
- (define symbolic-identifier=?
- (lambda (x y)
- (eq? (syntax->datum x)
- (syntax->datum y))))
- @end example
- @end deffn
- @deffn Procedure {datum->syntax} @var{template-id} @var{datum} [@var{srcloc}]
- @deffnx {Deprecated procedure} {datum->syntax-object} @var{template-id} @var{datum}
- @var{template-id} must be a template identifier and @var{datum} should
- be a datum value.
- The @func{datum->syntax} procedure returns a syntax-object
- representation of @var{datum} that contains the same contextual
- information as @var{template-id}, with the effect that the syntax object
- behaves as if it were introduced into the code when @var{template-id}
- was introduced.
- If @var{srcloc} is specified (and neither @code{#f} or @code{#!null}),
- it specifies the file position (including line number) for the result.
- In that case it should be a syntax object representing
- a list; otherwise it is currently ignored, though future extensions
- may support other ways of specifying the position.
- The @func{datum->syntax} procedure allows a transformer to ``bend''
- lexical scoping rules by creating @emph{implicit identifiers} that
- behave as if they were present in the input form, thus permitting the
- definition of macros that introduce visible bindings for or references
- to identifiers that do not appear explicitly in the input form. For
- example, the following defines a @func{loop} expression that uses this
- controlled form of identifier capture to bind the variable @code{break}
- to an escape procedure within the loop body. (The derived
- @func{with-syntax} form is like @func{let} but binds pattern variables.)
- @example
- (define-syntax loop
- (lambda (x)
- (syntax-case x ()
- ((k e ...)
- (with-syntax
- ((break (datum->syntax #'k 'break)))
- #'(call-with-current-continuation
- (lambda (break)
- (let f () e ... (f)))))))))
- (let ((n 3) (ls '()))
- (loop
- (if (= n 0) (break ls))
- (set! ls (cons 'a ls))
- (set! n (- n 1))))
- @result{} (a a a)
- @end example
- Were @code{loop} to be defined as:
- @example
- (define-syntax loop
- (lambda (x)
- (syntax-case x ()
- ((_ e ...)
- #'(call-with-current-continuation
- (lambda (break)
- (let f () e ... (f))))))))
- @end example
- @noindent
- the variable @code{break} would not be visible in @code{e ...}.
- The datum argument @var{datum} may also represent an arbitrary Scheme
- form, as demonstrated by the following definition of @func{include}.
- @example
- (define-syntax include
- (lambda (x)
- (define read-file
- (lambda (fn k)
- (let ((p (open-file-input-port fn)))
- (let f ((x (get-datum p)))
- (if (eof-object? x)
- (begin (close-port p) '())
- (cons (datum->syntax k x)
- (f (get-datum p))))))))
- (syntax-case x ()
- ((k filename)
- (let ((fn (syntax->datum #'filename)))
- (with-syntax (((exp ...)
- (read-file fn #'k)))
- #'(begin exp ...)))))))
- @end example
- @code{(include "filename")} expands into a @func{begin} expression
- containing the forms found in the file named by @code{"filename"}. For
- example, if the file @file{flib.ss} contains:
- @example
- (define f (lambda (x) (g (* x x))))
- @end example
- @noindent
- and the file @file{glib.ss} contains:
- @example
- (define g (lambda (x) (+ x x)))
- @end example
- @noindent
- the expression:
- @example
- (let ()
- (include "flib.ss")
- (include "glib.ss")
- (f 5))
- @end example
- @noindent
- evaluates to @code{50}.
- The definition of @func{include} uses @func{datum->syntax} to convert
- the objects read from the file into syntax objects in the proper lexical
- context, so that identifier references and definitions within those
- expressions are scoped where the @func{include} form appears.
- Using @func{datum->syntax}, it is even possible to break hygiene
- entirely and write macros in the style of old Lisp macros. The
- @func{lisp-transformer} procedure defined below creates a transformer
- that converts its input into a datum, calls the programmer's procedure
- on this datum, and converts the result back into a syntax object scoped
- where the original macro use appeared.
- @example
- (define lisp-transformer
- (lambda (p)
- (lambda (x)
- (syntax-case x ()
- ((kwd . rest)
- (datum->syntax #'kwd
- (p (syntax->datum x))))))))
- @end example
- @end deffn
- @subsection Signaling errors in macro transformers
- @deffn Syntax syntax-error message @arbno{args}
- The @meta{message} and @meta{args} are treated similary as for
- the @code{error} procedure. However, the error is reported
- when the @code{syntax-error} is expanded.
- This can be used as a @code{syntax-rules}
- template for a pattern that is an invalid use of the
- macro, which can provide more descriptive error messages.
- The @meta{message} should be a string literal, and the @var{args}
- arbitrary (non-evalualted) expressions providing additional information.
- @example
- (define-syntax simple-let
- (syntax-rules ()
- ((_ (head ... ((x . y) val) . tail)
- body1 body2 ...)
- (syntax-error "expected an identifier but got" (x . y)))
- ((_ ((name val) ...) body1 body2 ...)
- ((lambda (name ...) body1 body2 ...)
- val ...))))
- @end example
- @end deffn
- @deffn Procedure report-syntax-error location message
- This is a procedure that can be called at macro-expansion time
- by a syntax transformer function.
- (In contrast @code{syntax-error} is a syntax form used in the
- expansion result.)
- The @var{message} is reported as a compile-time error message.
- The @var{location} is used for the source location (file name and
- line/column numbers): In general it can be a @code{SourceLocator} value;
- most commonly it is a syntax object for a sub-list of the input form
- that is erroneous.
- The value returned by @code{report-syntax-error} is an
- instance of @code{ErrorExp}, which supresses further compilation.
- @example
- (define-syntax if
- (lambda (x)
- (syntax-case x ()
- ((_ test then)
- (make-if-exp #'test #'then #!null))
- ((_ test then else)
- (make-if-exp #'test #'then #'else))
- ((_ e1 e2 e3 . rest)
- (report-syntax-error #'rest
- "too many expressions for 'if'"))
- ((_ . rest)
- (report-syntax-error #'rest
- "too few expressions for 'if'")))))
- @end example
- In the above example, one could use the source form @code{x} for the
- location, but using @code{#'rest} is more accurate. Note that the following
- is incorrect, because @code{e1} might not be a pair, in which case
- we don't have location information for it (due to a Kawa limitation):
- @example
- (syntax-case x ()
- ...
- ((_ e1)
- (report-syntax-error
- #'e1 ;; @i{poor location specifier}
- "too few expressions for 'if'")))))
- @end example
- @end deffn
- @subsection Convenience forms
- @deffn Syntax with-syntax ((@var{pattern} @stxref{expression}) @dots{}) @stxref{body}
- The @func{with-syntax} form is used to bind pattern variables, just as
- @func{let} is used to bind variables. This allows a transformer to
- construct its output in separate pieces, then put the pieces together.
- Each @var{pattern} is identical in form to a @func{syntax-case}
- pattern. The value of each @var{expression} is computed and
- destructured according to the corresponding @var{pattern}, and pattern
- variables within the @var{pattern} are bound as with
- @func{syntax-case} to the corresponding portions of the value within
- @var{body}.
- The @func{with-syntax} form may be defined in terms of
- @func{syntax-case} as follows.
- @example
- (define-syntax with-syntax
- (lambda (x)
- (syntax-case x ()
- ((_ ((p e0) ...) e1 e2 ...)
- (syntax (syntax-case (list e0 ...) ()
- ((p ...) (let () e1 e2 ...))))))))
- @end example
- The following definition of @func{cond} demonstrates the use of
- @func{with-syntax} to support transformers that employ recursion
- internally to construct their output. It handles all @func{cond} clause
- variations and takes care to produce one-armed @func{if} expressions
- where appropriate.
- @example
- (define-syntax cond
- (lambda (x)
- (syntax-case x ()
- ((_ c1 c2 ...)
- (let f ((c1 #'c1) (c2* #'(c2 ...)))
- (syntax-case c2* ()
- (()
- (syntax-case c1 (else =>)
- (((else e1 e2 ...) #'(begin e1 e2 ...))
- ((e0) #'e0)
- ((e0 => e1)
- #'(let ((t e0)) (if t (e1 t))))
- ((e0 e1 e2 ...)
- #'(if e0 (begin e1 e2 ...)))))
- ((c2 c3 ...)
- (with-syntax ((rest (f #'c2 #'(c3 ...))))
- (syntax-case c1 (=>)
- ((e0) #'(let ((t e0)) (if t t rest)))
- ((e0 => e1)
- #'(let ((t e0)) (if t (e1 t) rest)))
- ((e0 e1 e2 ...)
- #'(if e0
- (begin e1 e2 ...)
- rest)))))))))))
- @end example
- @end deffn
- @deffn Syntax quasisyntax @var{template}
- @deffnx {Auxiliary Syntax} unsyntax
- @deffnx {Auxiliary Syntax} unsyntax-splicing
- The @func{quasisyntax} form is similar to @func{syntax}, but it allows
- parts of the quoted text to be evaluated, in a manner similar to the
- operation of @func{quasiquote}.
- Within a @func{quasisyntax} @var{template}, subforms of @func{unsyntax}
- and @func{unsyntax-splicing} forms are evaluated, and everything else is
- treated as ordinary template material, as with @func{syntax}.
- The value of each @func{unsyntax} subform is inserted into the output in
- place of the @func{unsyntax} form, while the value of each
- @func{unsyntax-splicing} subform is spliced into the surrounding list or
- vector structure. Uses of @func{unsyntax} and @func{unsyntax-splicing}
- are valid only within @func{quasisyntax} expressions.
- A @func{quasisyntax} expression may be nested, with each
- @func{quasisyntax} introducing a new level of syntax quotation and each
- @func{unsyntax} or @func{unsyntax-splicing} taking away a level of
- quotation. An expression nested within @emph{n} @func{quasisyntax}
- expressions must be within @emph{n} @emph{unsyntax} or
- @func{unsyntax-splicing} expressions to be evaluated.
- As noted in @stxref{abbreviation}, @code{#`@var{template}} is equivalent to
- @code{(quasisyntax @var{template})}, @code{#,@var{template}} is
- equivalent to @code{(unsyntax @var{template})}, and
- @code{#,@@@var{template}} is equivalent to @code{(unsyntax-splicing
- @var{template})}. @emph{Note} that for backwards compatibility,
- you should only use @code{#,@var{template}} inside a literal @code{#`@var{template}} form.
- The @func{quasisyntax} keyword can be used in place of
- @func{with-syntax} in many cases. For example, the definition of
- @func{case} shown under the description of @func{with-syntax} above can
- be rewritten using @func{quasisyntax} as follows.
- @example
- (define-syntax case
- (lambda (x)
- (syntax-case x ()
- ((_ e c1 c2 ...)
- #`(let ((t e))
- #,(let f ((c1 #'c1) (cmore #'(c2 ...)))
- (if (null? cmore)
- (syntax-case c1 (else)
- ((else e1 e2 ...)
- #'(begin e1 e2 ...))
- (((k ...) e1 e2 ...)
- #'(if (memv t '(k ...))
- (begin e1 e2 ...))])
- (syntax-case c1 ()
- (((k ...) e1 e2 ...)
- #`(if (memv t '(k ...))
- (begin e1 e2 ...)
- #,(f (car cmore)
- (cdr cmore))))))))))))
- @end example
-
- @c ((Unknown if this works))
- @c Uses of @func{unsyntax} and @func{unsyntax-splicing} with zero or more
- @c than one subform are valid only in splicing (list or vector) contexts.
- @c @code{(unsyntax @var{template} @dots{})} is equivalent to
- @c @code{(unsyntax @var{template}) ...}, and @code{(unsyntax-splicing
- @c @var{template} ...)} is equivalent to @code{(unsyntax-splicing
- @c @var{template}) ...}. These forms are primarily useful as intermediate
- @c forms in the output of the @func{quasisyntax} expander.
- @c
- @c @quotation
- @c @emph{Note:} Uses of @func{unsyntax} and @func{unsyntax-splicing} with
- @c zero or more than one subform enable certain idioms, such as
- @c @code{#,@@#,@@}, which has the effect of a doubly indirect splicing when
- @c used within a doubly nested and doubly evaluated @func{quasisyntax}
- @c expression.
- @c @end quotation
- @end deffn
- @quotation
- @emph{Note:} Any @func{syntax-rules} form can be expressed with
- @func{syntax-case} by making the @func{lambda} expression and
- @func{syntax} expressions explicit, and @func{syntax-rules} may be
- defined in terms of @func{syntax-case} as follows.
- @example
- (define-syntax syntax-rules
- (lambda (x)
- (syntax-case x ()
- ((_ (lit ...) ((k . p) t) ...)
- (for-all identifier? #'(lit ... k ...))
- #'(lambda (x)
- (syntax-case x (lit ...)
- ((_ . p) #'t) ...))))))
- @end example
- @end quotation
- @node Named quasi-literals
- @section Named quasi-literals
- Traditional Scheme has only a few kinds of values,
- and thus only a few builtin kinds of literals.
- Modern Scheme allows defining new types,
- so it is desirable to have a mechanism for defining literal values
- for the new types.
- Consider the @code{@ref{URI-type,,URI}} type.
- You can create a new instance of a @code{URI} using a
- constructor function:
- @example
- (URI "http://example.com/")
- @end example
- This isn't too bad, though the double-quote characters are an ugly distraction.
- However, if you need to construct the string it gets messy:
- @example
- (URI (string-append base-uri "icon.png"))
- @end example
- Instead use can write:
- @example
- &URI@{http://example.com/@}
- @end example
- or:
- @example
- &URI@{&[base-uri]icon.png@}
- @end example
- This syntax is translated by the Scheme reader
- to the more familiar but more verbose equivalent forms:
- @example
- ($construct$:URI "http://example.com/")
- ($construct$:URI $<<$ base-uri $>>$ "icon.png")
- @end example
- So for this to work there just needs to be a definition
- of @code{$construct$:URI}, usually a macro.
- Normal scope rules apply; typically you'd define @code{$construct$:URI} in
- a module.
- The names @code{$<<$} and @code{$>>$} are bound to unique zero-length strings.
- They are used to allow the implementation of @code{$construct$:URI}
- to determine which arguments are literal and which come from
- escaped expressions.
- If you want to define your own @code{$construct$:@var{tag}},
- or to read motivation and details, see the
- @uref{http://srfi.schemers.org/srfi-108/srfi-108.html, SRFI 108} specification.
- @display
- @stxdef{extended-datum-literal}
- @stxlit{&} @stxref{cname} @stxlit{@lbracechar{}} [@stxref{initial-ignored}] @arbno{@stxref{named-literal-part}} @stxlit{@rbracechar{}}
- | @stxlit{&} @stxref{cname} @stxlit{[} @arbno{@stxref{expression}} @stxlit{]@lbracechar{}} [@stxref{initial-ignored}] @arbno{@stxref{named-literal-part}} @stxlit{@rbracechar{}}
- @stxdef{cname} @stxref{identifier}
- @stxdef{named-literal-part}
- @i{any character except} @stxlit{&}@i{,} @stxlit{@lbracechar{}} @i{or} @stxlit{@rbracechar{}}
- | @stxlit{@lbracechar{}} @atleastone{@stxref{named-literal-part}} @stxlit{@rbracechar{}}
- | @stxref{char-ref}
- | @stxref{entity-ref}
- | @stxref{special-escape}
- | @stxref{enclosed-part}
- | @stxref{extended-datum-literal}
- @end display
- @c FIXME May rename: Control structure
- @node Program structure
- @chapter Program structure
- See @ref{program units} for some notes on
- structure of an entire source file.
- @menu
- * Boolean values::
- * Conditionals::
- * Variables and Patterns::
- * Definitions::
- * Local binding constructs::
- * Lazy evaluation::
- * Repeat forms:: Repeat patterns and expressions
- * Threads::
- * Exceptions:: Exception handling
- @end menu
- @node Boolean values
- @section Boolean values
- The standard boolean objects for true and false are written as @code{#t} and @code{#f}.
- Alternatively, they may be written @code{#true} and @code{#false},
- respectively.
- @display
- @stxdef{boolean} @stxlit{#t} | @stxlit{#f} | @stxlit{#true} | @stxlit{#false}
- @end display
- @display
- @stxdef{test-expression} @stxref{expression}
- @end display
- What really matters,
- though, are the objects that the Scheme conditional expressions (@code{if},
- @code{cond}, @code{and}, @code{or}, @code{when}, @code{unless}, @code{do})
- treat as true or
- false. The phrase ``a true value'' (or sometimes just ``true'')
- means any object treated as true by the conditional expressions, and the phrase ``a false value'' (or ``false'') means any
- object treated as false by the conditional expressions.
- In this document, @var{test-expression} is an expression that is evaluated,
- but we only care about whether the result is a true or a false value.
- Of all the standard Scheme values, only @code{#f} counts as false in
- conditional expressions. All other Scheme values, including @code{#t},
- count as true. A @var{test-expression} is an expression evaluated
- in this manner for whether it is true or false.
- In addition the null value @code{#!null} (in Java written as @code{null})
- is also considered false. Also, if you for some strange reason create a
- fresh @code{java.lang.Boolean} object whose @code{booleanValue()}
- returns @code{false}, that is also considered false.
- @emph{Note:} Unlike some other dialects of Lisp, Scheme distinguishes
- @code{#f} and the empty list from each other and from the symbol
- @code{nil}.
- Boolean constants evaluate to themselves, so they do not
- need to be quoted in programs.
- @example
- #t @result{} #t
- #true @result{} #t
- #f @result{} #f
- #false @result{} #f
- '#f @result{} #f
- @end example
- @deffn Type boolean
- The type of boolean values.
- As a type conversion, a true value is converted to @code{#t},
- while a false value is converted to @code{#f}.
- Represented as a primitive Java @code{boolean}
- or @code{kawa.lang.Boolean} when converted to an object.
- @end deffn
- @deffn Procedure boolean? obj
- The @code{boolean?} predicate returns @code{#t} if @var{obj}
- is either @code{#t} or @code{#f}, and returns @code{#f} otherwise.
- @example
- (boolean? #f) @result{} #t
- (boolean? 0) @result{} #f
- (boolean? '()) @result{} #f
- @end example
- @end deffn
- @deffn Procedure boolean=? boolean1 boolean2 boolean3 ...
- Returns @code{#t} if all the arguments are booleans and all are @code{#t}
- or all are @code{#f}.
- @end deffn
- @node Conditionals
- @section Conditionals
- Kawa Scheme has the usual conditional expression forms,
- such as @code{if}, @code{case}, @code{and}, and @code{or}:
- @example
- (if (> 3 2) 'yes 'no) @result{} yes
- @end example
- Kawa also allows you bind variables in the condition,
- using the @samp{?} operator.
- @example
- (if (and (? x ::integer (get-value)) (> x 0))
- (* x 10)
- 'invalid)
- @end example
- In the above, if @code{(get-value)} evaluates to an integer, that
- integer is bound to the variable @code{x}, which is visible
- in both following sub-expression of @code{and},
- as well case the true-part of the @code{if}.
- Specifically, the first sub-expression of an @code{if}
- is a @stxref{test-or-match}, which can be a @stxref{test-expression},
- or a @samp{?} match expression, or a combination using @code{and}:
- @display
- @stxdef{test-or-match} @stxref{test-expression}
- | @stxlit{(?} @stxref{pattern} @stxref{expression} @stxlit{)}
- | @stxlit{(and} @arbno{@stxref{test-or-match}}@stxlit{)}
- @end display
- A @var{test-or-match} is true if every nested @var{test-expression}
- is true, and every @samp{?} operation succeeds.
- It produces a set of variable bindings which is the union
- of the bindings produced by all the @var{pattern}s.
- In an @code{and} form, bindings produced by a @var{pattern} are visible to
- all subsequent @var{test-or-match} sub-expressions.
- @deffn Syntax ? @stxref{pattern} @stxref{expression}
- The form @code{(? @var{P} @var{V})} informally
- is true if the value of @var{V} matches the pattern @var{P}.
- Any variables bound in @var{P} are in scope in the ``true''
- path of the containing conditional.
- This has the form of an expression, but it can only be used
- in places where a @stxref{test-or-match} is required.
- For example it can be used as the first clause of an
- @code{if} expression, in which case the scope of the variables bound in
- the @code{pattern} includes the second (@stxref{consequent}) sub-expression.
- On the other hand, a @samp{?} form may not be used
- as an argument to a procedure application.
- @end deffn
- @deffn Syntax if @stxref{test-or-match} @stxref{consequent} @stxref{alternate}
- @deffnx Syntax if @stxref{test-or-match} @stxref{consequent}
- @display
- @stxdef{consequent} @stxref{expression}
- @stxdef{alternate} @stxref{expression}
- @end display
- An @func{if} expression is evaluated as follows:
- first, the @stxref{test-or-match} is
- evaluated. If it it true, then @meta{consequent} is
- evaluated and its values are returned. Otherwise @meta{alternate} is
- evaluated and its values are returned. If @meta{test} yields @false{}
- and no @meta{alternate} is specified, then the result of the expression
- is void.
- @example
- (if (> 2 3) 'yes 'no) @result{} no
- (if (> 3 2)
- (- 3 2)
- (+ 3 2)) @result{} 1
- (if #f #f) @result{} #!void
- (if (? x::integer 3)
- (+ x 1)
- 'invalid) @result{} 4
- (if (? x::integer 3.4)
- (+ x 1)
- 'invalid) @result{} 'invalid
- @end example
- The @meta{consequent} and @meta{alternate} expressions are in tail
- context if the @func{if} expression itself is.
- @end deffn
- @deffn Syntax cond @atleastone{@stxref{cond-clause}}
- @deffnx Syntax cond @arbno{@stxref{cond-clause}} @stxlit{(else} @meta{expression}@dots{}@stxlit{)}
- @display
- @stxdef{cond-clause} @stxlit{(}@stxref{test-or-match} @stxref{body}@stxlit{)}
- | @stxlit{(}@meta{test} @stxlit{=>} @meta{expression}@stxlit{)}
- @end display
- A @func{cond} expression is evaluated by evaluating the @meta{test-or-match}s
- of successive @meta{cond-clause}s in order until one of them
- evaluates to a true value. When a @meta{test-or-match} is true
- value, then the remaining @meta{expression}s in its @meta{cond-clause}
- are evaluated in order, and the results of the last @meta{expression} in
- the @meta{cond-clause} are returned as the results of the entire
- @func{cond} expression. Variables bound by the @var{test-or-match}
- are visible in @var{body}.
- If the selected @meta{cond-clause} contains only the @meta{test-or-match} and no
- @meta{expression}s, then the value of the last @meta{test-expression} is returned as the
- result. If the selected @meta{cond-clause} uses the @code{=>} alternate
- form, then the @meta{expression} is evaluated. Its value must be a
- procedure. This procedure should accept one argument; it is called on
- the value of the @meta{test-expression} and the values returned by this procedure
- are returned by the @func{cond} expression.
- If all @meta{test-or-match}s evaluate to @false{}, and there is no @code{else}
- clause, then the conditional expression returns unspecified values; if
- there is an @code{else} clause, then its @meta{expression}s are
- evaluated, and the values of the last one are returned.
- @example
- (cond ((> 3 2) 'greater)
- ((< 3 2) 'less)) @result{} greater
- (cond ((> 3 3) 'greater)
- ((< 3 3) 'less)
- (else 'equal)) @result{} equal
- (cond ('(1 2 3) => cadr)
- (else #f)) @result{} 2
- @end example
- For a @meta{cond-clause} of one of the following forms:
- @example
- (@meta{test} @arbno{@stxref{expression}})
- (else @stxref{expression} @arbno{@stxref{expression}})
- @end example
- @noindent
- the last @meta{expression} is in tail context if the @func{cond} form
- itself is. For a @meta{cond clause} of the form:
- @example
- (@meta{test} => @meta{expression})
- @end example
- @noindent
- the (implied) call to the procedure that results from the evaluation of
- @meta{expression} is in tail context if the @func{cond} form itself
- is.
- @end deffn
- @deffn Syntax case @stxref{case-key} @atleastone{@stxref{case-clause}}
- @deffnx Syntax case @stxref{case-key} @arbno{@stxref{case-clause}} @stxref{case-else-clause}
- @display
- @stxdef{case-key} @stxref{expression}
- @stxdef{case-clause} @stxlit{((}@arbno{@stxref{datum}}@stxlit{)} @atleastone{@stxref{expression}}@stxlit{)}
- | @stxlit{((}@arbno{@stxref{datum}}@stxlit{)} @stxlit{=>} @stxref{expression}@stxlit{)}
- @stxdef{case-else-clause} @stxlit{(else} @atleastone{@stxref{expression}}@stxlit{)}
- | @stxlit{(else =>} @stxref{expression}@stxlit{)}
- @end display
- Each @meta{datum} is an external representation of some object.
- Each @meta{datum} in the entire @code{case} expression should be distinct.
- A @func{case} expression is evaluated as follows.
- @enumerate
- @item
- The @meta{case-key} is evaluated and its result is compared using
- @func{eqv?} against the data represented by the @meta{datum}s of
- each @meta{case-clause} in turn, proceeding in order from left to
- right through the set of clauses.
- @item
- If the result of evaluating @meta{case-key} is equivalent to a datum of a
- @meta{case-clause}, the corresponding @meta{expression}s are evaluated
- from left to right and the results of the last expression in the
- @meta{case-clause} are returned as the results of the @func{case}
- expression. Otherwise, the comparison process continues.
- @item
- If the result of evaluating @meta{key} is different from every datum in
- each set, then if there is an @meta{case-else-clause} its expressions are
- evaluated and the results of the last are the results of the @func{case}
- expression; otherwise the result of @func{case} expression is unspecified.
- @end enumerate
- If the selected @meta{case-clause} or @meta{case-else-clause}
- uses the @code{=>} alternate
- form, then the @meta{expression} is evaluated. It is an error if
- its value is not a procedure accepting one argument. This
- procedure is then called on the value of the @meta{key} and the
- values returned by this procedure are returned by the @code{case}
- expression.
- @example
- (case (* 2 3)
- ((2 3 5 7) 'prime)
- ((1 4 6 8 9) 'composite)) @result{} composite
- (case (car '(c d))
- ((a) 'a)
- ((b) 'b)) @result{} unspecified
- (case (car '(c d))
- ((a e i o u) 'vowel)
- ((w y) 'semivowel)
- (else => (lambda (x) x))) @result{} c
- @end example
- The last @meta{expression} of a @meta{case clause} is in tail context if
- the @func{case} expression itself is.
- @end deffn
- @anchor{def-match}
- @deffn Syntax match @stxref{match-key} @atleastone{@stxref{match-clause}}
- The @code{match} form is a generalization of @code{case} using @stxref{pattern}s,
- @display
- @stxdef{match-key} @stxref{expression}
- @stxdef{match-clause}
- @stxlit{(} @stxref{pattern} [@stxref{guard}] @stxref{body} @stxlit{)}
- @end display
- The @var{match-key} is evaluated,
- Then the @var{match-clause}s are tried in order.
- The first @var{match-clause} whose @var{pattern} matches (and
- the @var{guard}, if any, is true), is selected,
- and the corresponding @var{body} evaluated.
- It is an error if no @var{match-clause} matches.
- @example
- (match value
- (0 (found-zero))
- (x #!if (> x 0) (found-positive x))
- (x #!if (< x 0) (found-negative x))
- (x::symbol (found-symbol x))
- (_ (found-other)))
- @end example
- One @code{case} feature is not (yet) directly supported by @code{match}:
- Matching against a list of values.
- However, this is easy to simulate using a guard using @code{memq},
- @code{memv}, or @code{member}:
- @example
- ;; compare similar example under case
- (match (car '(c d))
- (x #!if (memv x '(a e i o u)) ’vowel)
- (x #!if (memv x '(w y)) ’semivowel)
- (x x))
- @end example
- @end deffn
- @deffn Syntax and @arbno{@stxref{test-or-match}}
- If there are no @meta{test-or-match} forms, @true{} is returned.
- If the @code{and} is not in @stxref{test-or-match} context,
- then the last sub-expression (if any) must be a @stxref{test-expression},
- and not a @samp{?} form. In this case the
- @meta{test-or-match} expressions are evaluated from left to right until
- either one of them is false
- (a @meta{test-expression} is false or a @samp{?} match fails),
- or the last @meta{test-expression} is reached. In the
- former case, the @func{and} expression returns @false{} without
- evaluating the remaining expressions. In the latter case, the last
- expression is evaluated and its values are returned.
- If the @code{and} is in @stxref{test-or-match} context,
- then the last sub-form can be @samp{?} form.
- They are evaluated in order: If one of them is false,
- the entire @code{and} is false; otherwise the @code{and} is true.
- Regardless, any bindings made by earlier @samp{?} forms
- are visible in later @var{test-or-match} forms.
- @example
- (and (= 2 2) (> 2 1)) @result{} #t
- (and (= 2 2) (< 2 1)) @result{} #f
- (and 1 2 'c '(f g)) @result{} (f g)
- (and) @result{} #t
- (and (? x ::int 23) (> x 0)) @result{} #t
- @end example
- The @func{and} keyword could be defined in terms of @func{if} using
- @func{syntax-rules} as follows:
- @example
- (define-syntax and
- (syntax-rules ()
- ((and) #t)
- ((and test) test)
- ((and test1 test2 ...)
- (if test1 (and test2 ...) #t))))
- @end example
- The last @meta{test-expression} is in tail context if the @func{and}
- expression itself is.
- @end deffn
- @deffn Syntax or @stxref{test-expression} @dots{}
- If there are no @meta{test-expression}s, @false{} is returned. Otherwise, the
- @meta{test-expression}s are evaluated from left to right until a
- @meta{test-expression} returns a true value @var{val} or the last
- @meta{test-expression} is
- reached. In the former case, the @func{or} expression returns @var{val}
- without evaluating the remaining expressions. In the latter case, the
- last expression is evaluated and its values are returned.
- @example
- (or (= 2 2) (> 2 1)) @result{} #t
- (or (= 2 2) (< 2 1)) @result{} #t
- (or #f #f #f) @result{} #f
- (or '(b c) (/ 3 0)) @result{} (b c)
- @end example
- The @func{or} keyword could be defined in terms of @func{if} using
- @func{syntax-rules} as follows:
- @example
- (define-syntax or
- (syntax-rules ()
- ((or) #f)
- ((or test) test)
- ((or test1 test2 ...)
- (let ((x test1))
- (if x x (or test2 ...))))))
- @end example
- The last @meta{test-expression} is in tail context if the @func{or}
- expression itself is.
- @end deffn
- @deffn Procedure not test-expression
- The @code{not} procedure returns @code{#t} if @var{test-expression} is false,
- and returns @code{#f} otherwise.
- @example
- (not #t) @result{} #f
- (not 3) @result{} #f
- (not (list 3)) @result{} #f
- (not #f) @result{} #t
- (not ’()) @result{} #f
- (not (list)) @result{} #f
- (not ’nil) @result{} #f
- (not #!null) @result{} #t
- @end example
- @end deffn
- @deffn Syntax when @stxref{test-expression} form...
- If @var{test-expression} is true, evaluate each @var{form} in order,
- returning the value of the last one.
- @end deffn
- @deffn Syntax unless @stxref{test-expression} form...
- If @var{test-expression} is false, evaluate each @var{form} in order,
- returning the value of the last one.
- @end deffn
- @node Variables and Patterns
- @section Variables and Patterns
- An identifier can name either a type of syntax or a location
- where a value can be stored. An identifier that names a
- type of syntax is called a @dfn{syntactic keyword}
- (informally called a @dfn{macro}), and is said to be
- @dfn{bound} to a transformer for that syntax. An identifier that
- names a location is called a @dfn{variable} and is said to be @dfn{bound}
- to that location. The set of all visible bindings in effect at
- some point in a program is known as the @dfn{environment} in
- effect at that point. The value stored in the location to
- which a variable is bound is called the variable’s value.
- By abuse of terminology, the variable is sometimes said
- to name the value or to be bound to the value. This is
- not quite accurate, but confusion rarely results from this
- practice.
- Certain expression types are used to create new kinds of
- syntax and to bind syntactic keywords to those new syntaxes,
- while other expression types create new locations
- and bind variables to those locations. These expression
- types are called @dfn{binding constructs}.
- Those that bind syntactic keywords are discussed in @ref{Macros}.
- The most fundamental of the variable binding constructs is the
- @ref{meta-lambda-expression,@code{lambda} expression},
- because all other variable binding constructs
- can be explained in terms of @code{lambda} expressions.
- Other binding constructs include the @ref{Definitions,@code{define} family},
- and the @ref{Local binding constructs,@code{let} family}.
- Scheme is a language with block structure. To each place
- where an identifier is bound in a program there corresponds
- a @dfn{region} of the program text within which the binding is visible.
- The region is determined by the particular binding construct that
- establishes the binding; if the binding is
- established by a @code{lambda} expression, for example, then its
- region is the entire @code{lambda} expression. Every mention of
- an identifier refers to the binding of the identifier that established
- the innermost of the regions containing the use.
- If there is no binding of the identifier whose region contains the use,
- then the use refers to the binding for the
- variable in the global environment, if any;
- if there is no binding for the identifier, it is said to be @dfn{unbound}.
- @subsection Patterns
- The usual way to bind variables is to match an incoming
- value against a @dfn{pattern}. The pattern contains variables
- that are bound to some value derived from the value.
- @example
- (! [x::double y::double] (some-expression))
- @end example
- In the above example, the pattern @code{[x::double y::double]}
- is matched against the incoming value that results from
- evaluating @code{(some-expression)}.
- That value is required to be a two-element sequence.
- Then the sub-pattern @code{x::double} is matched against
- element 0 of the sequence, which means it is coerced to a @code{double}
- and then the coerced value is matched against the sub-pattern @code{x}
- (which trivially succeeds). Similarly, @code{y::double} is matched
- against element 1.
- The syntax of patterns is a work-in-progress. (The focus until now
- has been in designing and implementing how patterns work in general,
- rather than the details of the pattern syntax.)
- @display
- @stxdef{pattern} @stxref{identifier}
- | @stxlit{_}
- | @stxref{pattern-literal}
- | @stxlit{'}@stxref{datum}
- | @stxref{pattern} @stxlit{::} @stxref{type}
- | @stxlit{[} @arbno{@stxref{lpattern}} @stxlit{]}
- @stxdef{lpattern} @stxref{pattern}
- | @stxlit{@@} @stxref{pattern}
- | @stxref{pattern} @stxlit{...}
- | @stxref{guard}
- @stxdef{pattern-literal}
- @stxref{boolean} | number | @stxref{character} | @stxref{string}
- @stxdef{guard} @stxlit{#!if} @stxref{expression}
- @end display
- This is how the specific patterns work:
- @table @asis
- @item @stxref{identifier}
- This is the simplest and most common form of pattern.
- The @var{identifier} is bound to a new variable
- that is initialized to the incoming value.
- @item @stxlit{_}
- This pattern just discards the incoming value.
- It is equivalent to a unique otherwise-unused @var{identifier}.
- @item @stxref{pattern-literal}
- Matches if the value is @code{equal?} to the @var{pattern-literal}.
- @item @stxlit{'}@stxref{datum}
- Matches if the value is @code{equal?} to the quoted @var{datum}.
- @item @stxref{pattern} @stxlit{::} @stxref{type}
- The incoming value is coerced to a value of the specified @var{type},
- and then the coerced value is matched against the sub-@var{pattern}.
- Most commonly the sub-@var{pattern} is a plain @var{identifier},
- so the latter match is trivial.
- @item @stxlit{[} @arbno{@stxref{lpattern}} @stxlit{]}
- The incoming value must be a sequence (a list, vector or similar).
- In the case where each sub-pattern is a plain @var{pattern},
- then the number of sub-patterns must match the size of the sequence, and
- each sub-pattern is matched against the corresponding element of the sequence.
- More generally, each sub-pattern may match zero or more consequtive
- elements of the incoming sequence.
- @item @stxlit{#!if} @stxref{expression}
- No incoming value is used. Instead the @var{expression} is evaluated.
- If the result is true, matching succeeds (so far);
- otherwise the match fails.
- This form is called a @uref{https://en.wikipedia.org/wiki/Guard_(computer_science),@dfn{guard}}.
- @item @stxlit{@@} @stxref{pattern}
- A @dfn{splice pattern} may match multiple (zero or more) elements of a sequence.
- The @var{pattern} is matched against the resulting sub-sequence.
- @example
- (! [x @@r] [2 3 5 7 11])
- @end example
- This binds @code{x} to 2 and @code{r} to @code{[3 5 7 11]}.
- @item @stxref{pattern} @stxlit{...}
- Similar to @code{@@@var{pattern}} in that it matches
- multiple elements of a sequence.
- However, each individual element is matched
- against the @var{pattern}, rather than the elements as a sequence.
- This is a @ref{Repeat forms,repeat pattern}.
- @end table
- @node Definitions
- @section Definitions
- A variable definition binds one or more identifiers and specifies
- an initial value for each of them. The simplest kind of
- variable definition takes one of the following forms:
- @anchor{exclam-syntax}
- @deffn Syntax ! @stxref{pattern} @stxref{expression}
- Evaluate @stxref{expression}, and match the result against @var{pattern}.
- Defining variables in @stxref{pattern} becomes bound in the
- current (surrounding) scope.
- This is similar to @code{define-constant} except generalized to a @var{pattern}.
- @example
- (! [x y] (vector 3 4))
- (format "x is ~w and y is ~w" x y) @result{} "x is 3 and y is 4"
- @end example
- @end deffn
- @deffn Syntax define name [@stxlit{::} @stxref{type}] @stxref{expression}
- Evaluate the @var{expression}, optionally converting it to @var{type},
- and bind the @var{name} to the result.
- @end deffn
- @deffn Syntax define (name @stxref{formal-arguments}) @arbno{(@stxref{annotation} | @stxref{option-pair})} @stxref{opt-return-type} @stxref{body}
- @deffnx Syntax define (name @stxlit{.} @stxref{rest-arg}) @arbno{(@stxref{annotation} | @stxref{option-pair})} @stxref{opt-return-type} @stxref{body}
- Bind the @var{name} to a function definition. The form:
- @example
- (define (@var{name} @stxref{formal-arguments}) @arbno{@stxref{option-pair}} @stxref{opt-return-type} @stxref{body})
- @end example
- is equivalent to:
- @example
- (define @var{name} (lambda @stxref{formal-arguments}) name: @var{name} @arbno{@stxref{option-pair}} @stxref{opt-return-type} @stxref{body}))
- @end example
- while the form:
- @example
- (define (@var{name} . @stxref{rest-arg}) @arbno{@stxref{option-pair}} @stxref{opt-return-type} @stxref{body})
- @end example
- is equivalent to:
- @example
- (define @var{name} (lambda @stxref{rest-arg}) name: @var{name} @arbno{@stxref{option-pair}} @stxref{opt-return-type} @stxref{body}))
- @end example
- You can associate @ref{Annotations,annotations} with @var{name}.
- A field annotation will be associated with the generated field;
- a method annotation will be associated with the generated method(s).
- @end deffn
- In addition to @code{define} (which can take an optional type specifier),
- Kawa has some extra definition forms.
- @deffn Syntax define-private name [@stxlit{::} @stxref{type}] value
- @deffnx Syntax define-private (name formals) body
- Same as @code{define}, except that @code{name} is not exported.
- @end deffn
- @deffn Syntax define-constant name [@stxlit{::} @stxref{type}] value
- @deffnx Syntax define-early-constant name [:: type] value
- Defines @var{name} to have the given @var{value}.
- The value is readonly, and you cannot assign to it.
- (This is not fully enforced.)
- If @code{define-early-constant} is used
- @emph{or} the @var{value} is a compile-time constant,
- then the compiler will create a @code{final} field with
- the given name and type, and evaluate @var{value}
- in the module's class initializer (if the definition
- is static) or constructor (if the definition is non-static),
- before other definitions and expressions.
- Otherwise, the @var{value} is evaluated in the module body
- where it appears.
- If the @var{value} is a compile-time constant,
- then the definition defaults to being static.
- @end deffn
- @deffn Syntax define-variable name [@stxlit{::} @stxref{type}] [init]
- If @var{init} is specified and @var{name} does not have a global variable
- binding, then @var{init} is evaluated, and @var{name} bound to the result.
- Otherwise, the value bound to @var{name} does not change.
- (Note that @var{init} is not evaluated
- if @var{name} does have a global variable binding.)
- Also, declares to the compiler that @var{name} will be looked up
- in the per-thread dynamic environment. This can be useful for shutting up
- warnings from @code{--warn-undefined-variable}.
- This is similar to the Common Lisp @code{defvar} form.
- However, the Kawa version is (currently) only allowed at module level.
- @end deffn
- For @code{define-namespace} and @code{define-private-namespace}
- see @ref{Namespaces}.
- @node Local binding constructs
- @section Local binding constructs
- The binding constructs @code{let}, @code{let*}, @code{letrec},
- and @code{letrec*} give Scheme a block structure, like Algol 60.
- The syntax of these four constructs
- is identical, but they differ in the regions they establish
- for their variable bindings. In a @code{let} expression, the initial
- values are computed before any of the variables become
- bound; in a @code{let*} expression, the bindings and evaluations
- are performed sequentially; while in @code{letrec} and @code{letrec*}
- expressions, all the bindings are in effect while their initial
- values are being computed, thus allowing mutually recursive definitions.
- @c The let-values and let*-values con-
- @c structs are analogous to let and let* respectively, but
- @c are designed to handle multiple-valued expressions, bind-
- @c ing different identifiers to the returned values.
- @deffn Syntax let @stxlit{((}@stxref{pattern} @var{init}@stxlit{)} ...@stxlit{)} @stxref{body}
- Declare new local variables as found in the @meta{pattern}s.
- Each @var{pattern} is matched against the corresponding @var{init}.
- The @var{init}s are evaluated in the current environment (in left-to-right
- onder), the @var{variable}s in the @var{patterns}s are bound to fresh
- locations holding the matched results,
- the @var{body} is evaluated in the extended environment, and the values of
- the last expression of body are returned.
- Each binding of a variable has @var{body} as its region.
- @example
- (let ((x 2) (y 3))
- (* x y)) @result{} 6
- @end example
- @example
- (let ((x 2) (y 3))
- (let ((x 7)
- (z (+ x y)))
- (* z x))) @result{} 35
- @end example
- An example with a non-trivial pattern:
- @example
- (let (([a::double b::integer] (vector 4 5)))
- (cons b a)) @result{} (5 . 4.0)
- @end example
- @end deffn
- @deffn Syntax let* @stxlit{((}@stxref{pattern} init@stxlit{)} ...@stxlit{)} @stxref{body}
- The @code{let*} binding construct is similar to @code{let},
- but the bindings are performed sequentially from left to
- right, and the region of a @var{variable}s in a @var{pattern}
- is that part of the @code{let*} expression to the right of
- the @var{pattern}. Thus the second pattern is matched in an environment
- in which the bindings from the first pattern are visible, and so on.
- @example
- (let ((x 2) (y 3))
- (let* ((x 7)
- (z (+ x y)))
- (* z x))) @result{} 70
- @end example
- @end deffn
- @deffn Syntax letrec @stxlit{((}variable [@stxlit{::} @stxref{type}] init@stxlit{)} ...@stxlit{)} @stxref{body}
- @deffnx Syntax letrec* @stxlit{((}variable [@stxlit{::} @stxref{type}] init@stxlit{)} ...@stxlit{)} @stxref{body}
- The @var{variable}s are bound to fresh locations,
- each @var{variable} is assigned in left-to-right order
- to the result of the corresponding @var{init},
- the @var{body} is evaluated in the resulting environment,
- and the values of the last expression in body are returned.
- Despite the left-to-right evaluation and assignment order, each binding of a
- @var{variable} has the entire @code{letrec} or @code{letrec*}
- expression as its region,
- making it possible to define mutually recursive procedures.
- In Kawa @code{letrec} is defined as the same as @code{letrec*}.
- In standard Scheme the order of evaluation of the @var{init}s
- is undefined, as is the order of assignments.
- If the order matters, you should use @code{letrec*}.
- If it is not possible to evaluate each @var{init} without assigning
- or referring to the value of the corresponding @var{variable}
- or the variables that follow it, it is an error.
- @example
- (letrec ((even?
- (lambda (n)
- (if (zero? n)
- #t
- (odd? (- n 1)))))
- (odd?
- (lambda (n)
- (if (zero? n)
- #f
- (even? (- n 1))))))
- (even? 88))
- @result{} #t
- @end example
- @end deffn
- @node Lazy evaluation
- @section Lazy evaluation
- @dfn{Lazy evaluation} (or call-by-need) delays evaluating an expression until it
- is actually needed; when it is evaluated, the result is saved so
- repeated evaluation is not needed.
- @uref{http://en.wikipedia.org/wiki/Lazy_evaluation,Lazy evaluation}
- is a technique that can make some algorithms easier to express compactly
- or much more efficiently, or both. It is the normal evaluation mechanism
- for strict functional (side-effect-free) languages such as
- @uref{http://www.haskell.org,Haskell}.
- However, automatic lazy evaluation is awkward to combine with side-effects
- such as input-output. It can also be difficult to implement
- lazy evaluation efficiently, as it requires more book-keeping.
- Kawa, like other Schemes, uses ``eager evaluation'' - an expression
- is normally evaluated immediately, unless it is wrapped in a special form.
- Standard Scheme has some basic building blocks for ``manual''
- lazy evaluation, using an explicit @code{delay} operator to
- indicate that an expression is to be evaluated lazily,
- yielding a @dfn{promise},
- and a @code{force} function to force evaluation of a promise.
- This functionality is enhanced in
- @uref{http://srfi.schemers.org/srfi-45/srfi-45.html, SRFI 45},
- in R7RS-draft (based on SRFI 45),
- and @uref{http://srfi.schemers.org/srfi-41/srfi-41.html, SRFI 41} (lazy lists aka streams).
- Kawa makes lazy evaluation easier to use, by @dfn{implicit forcing}:
- The promise is automatically evaluated (forced) when used in a
- context that requires a normal value, such as arithmetic needing a number.
- Kawa enhances lazy evaluation in other ways, including
- support for safe multi-threaded programming.
- @subsection Delayed evaluation
- @deffn Syntax delay @stxref{expression}
- The @code{delay} construct is used together with the procedure
- @code{force} to implement @emph{lazy evaluation} or @emph{call by need}.
- The result of @code{(delay @var{expression})} is a
- @emph{promise} which at some point in the future may be asked (by the
- @func{force} procedure) to evaluate @var{expression}, and deliver the
- resulting value. The effect of @var{expression} returning multiple
- values is unspecified.
- @end deffn
- @deffn Syntax delay-force @stxref{expression}
- @deffnx Syntax lazy @stxref{expression}
- The @code{delay-force} construct is similar to @code{delay}, but it is expected
- that its argument evaluates to a promise.
- (Kawa treats a non-promise value as if it were a forced promise.)
- The returned
- promise, when forced, will evaluate to whatever the original
- promise would have evaluated to if it had been forced.
- The expression @code{(delay-force @meta{expression})} is
- conceptually similar to @code{(delay (force @meta{expression}))}, with
- the difference that forcing the result of @code{delay-force} will
- in effect result in a tail call to @code{(force @meta{expression})}, while
- forcing the result of @code{(delay (force @meta{expression}))} might
- not. Thus iterative lazy algorithms that might result in a
- long series of chains of @code{delay} and @code{force} can be rewritten
- using delay-force to prevent consuming unbounded space
- during evaluation.
- Using @code{delay-force} or @code{lazy} is equivalent.
- The name @code{delay-force} is from R7RS; the name @code{lazy}
- is from the older SRFI-45.
- @end deffn
- @deffn Procedure eager obj
- Returns a promise that when forced will return @var{obj}.
- It is similar to @code{delay}, but does not delay its argument;
- it is a procedure rather than syntax.
- The Kawa implementation just returns @var{obj} as-is.
- This is because Kawa treats as equivalent
- a value and forced promise evaluating to the value.
- @end deffn
- @deffn Procedure force promise
- The @func{force} procedure forces the value of @var{promise}.
- As a Kawa extension, if the @var{promise} is not a promise (a value that
- does not implement @code{gnu.mapping.Lazy}) then the argument is returned unchanged.
- If no value has been computed for the promise, then a value is computed and
- returned. The value of the promise is cached (or ``memoized'') so that
- if it is forced a second time, the previously computed value is
- returned.
- @example
- (force (delay (+ 1 2))) @result{} 3
- (let ((p (delay (+ 1 2))))
- (list (force p) (force p))) @result{} (3 3)
- (define integers
- (letrec ((next
- (lambda (n)
- (cons n (delay (next (+ n 1)))))))
- (next 0)))
- (define head
- (lambda (stream) (car (force stream))))
- (define tail
- (lambda (stream) (cdr (force stream))))
- (head (tail (tail integers))) @result{} 2
- @end example
- The following example is a mechanical transformation of
- a lazy stream-filtering algorithm into Scheme. Each call
- to a constructor is wrapped in @code{delay}, and each argument
- passed to a deconstructor is wrapped in @code{force}. The use
- of @code{(lazy ...)} instead of @code{(delay (force ...))} around
- the body of the procedure ensures that an ever-growing
- sequence of pending promises does not exhaust the heap.
- @example
- (define (stream-filter p? s)
- (lazy
- (if (null? (force s))
- (delay ’())
- (let ((h (car (force s)))
- (t (cdr (force s))))
- (if (p? h)
- (delay (cons h (stream-filter p? t)))
- (stream-filter p? t))))))
- (head (tail (tail (stream-filter odd? integers))))
- @result{} 5
- @end example
- @end deffn
- @deffn Procedure force* promise
- Does @code{force} as many times as necessary to produce a non-promise.
- (A non-promise is a value that does not implement @code{gnu.mapping.Lazy},
- or if it does implement @code{gnu.mapping.Lazy} then forcing the value
- using the @code{getValue} method yields the receiver.)
- The @code{force*} function is a Kawa extension.
- Kawa will add implicit calls to @code{force*}
- in most contexts that need it, but you can also call it explicitly.
- @end deffn
- The following examples are not intended to illustrate good
- programming style, as @code{delay}, @code{lazy}, and @code{force} are mainly
- intended for programs written in the functional style.
- However, they do illustrate the property that only one value is
- computed for a promise, no matter how many times it is
- forced.
- @example
- (define count 0)
- (define p
- (delay (begin (set! count (+ count 1))
- (if (> count x)
- count
- (force p)))))
- (define x 5)
- p @result{} @emph{a promise}
- (force p) @result{} 6
- p @result{} @emph{a promise, still}
- (begin (set! x 10)
- (force p)) @result{} 6
- @end example
- @subsection Implicit forcing
- If you pass a promise as an argument to a function like @code{sqrt}
- if must first be forced to a number. In general, Kawa does this
- automatically (implicitly) as needed, depending on the context.
- For example:
- @example
- (+ (delay (* 3 7)) 13) @result{} 34
- @end example
- Other functions,
- like @code{cons} have no problems with promises, and automatic forcing
- would be undesirable.
- Generally, implicit forcing happens for arguments that require a
- specific type, and does not happen for arguments that work on
- @emph{any} type (or @code{Object}).
- Implicit forcing happens for:
- @itemize
- @item
- arguments to arithmetic functions;
- @item
- the sequence and the index in indexing operations, like @code{string-ref};
- @item
- the operands to @code{eqv?} and @code{equal?} are forced,
- though the operands to @code{eq?} are not;
- @item
- port operands to port functions;
- @item
- the value to be emitted by a @code{display} but @emph{not} the
- value to be emitted by a @code{write};
- @item
- the function in an application.
- @end itemize
- Type membership tests, such as the @code{instance?} operation,
- generally do not force their values.
- The exact behavior for when implicit forcing happens is a work-in-progress:
- There are certainly places where implicit forcing doesn't happen while
- it should; there are also likely to be places where implicit forcing
- happens while it is undesirable.
- Most Scheme implementations are such that a forced promise behaves differently
- from its forced value, but some Scheme implementions are such that there is no
- means by which a promise can be operationally distinguished
- from its forced value.
- Kawa is a hybrid: Kawa tries to minimize the difference between
- a forced promise and its forced value, and may freely optimize
- and replace a forced promise with its value.
- @subsection Blank promises
- A @dfn{blank promise} is a promise that doesn't (yet) have
- a value @emph{or} a rule for calculating the value.
- Forcing a blank promise will wait forever, until some
- other thread makes the promise non-blank.
- Blank promises are useful as a synchronization mechanism -
- you can use it to safely pass data from one thread (the producer)
- to another thread (the consumer). Note that you can only
- pass one value for a given promise: To pass multiple values, you
- need multiple promises.
- @example
- (define p (promise))
- (future ;; Consumer thread
- (begin
- (do-stuff)
- (define v (force promise)) ; waits until promise-set-value!
- (do-stuff-with v)))
- ;; Producer thread
- ... do stuff ...
- (promise-set-value! p (calculate-value))
- @end example
- @deffn Constructor promise
- Calling @code{promise} as a zero-argument constructor
- creates a new blank promise.
- This calls the constructor for @code{gnu.mapping.Promise}.
- You can also create a non-blank promise, by setting one
- of the @code{value}, @code{alias}, @code{thunk}, or @code{exception} properties.
- Doing so is equivalent to calling @code{promise-set-value!},
- @code{promise-set-alias!}, @code{promise-set-thunk!}, or
- @code{promise-set-exception!} on the resulting promise.
- For example: @code{(delay exp)} is equivalent to:
- @example
- (promise thunk: (lambda() exp))
- @end example
- @end deffn
- The following four procedures require that their first arguments
- be blank promises. When the procedure returns, the promise is
- no longer blank, and cannot be changed. This is because a
- promise is conceptually a placeholder for a single ``not-yet-known'' value;
- it is not a location that can be assigned multiple times.
- The former enables clean and safe (``declarative") use of multiple threads;
- the latter is much trickier.
- @deffn Procedure promise-set-value! promise value
- Sets the value of the @var{promise} to @var{value},
- which makes the @var{promise} forced.
- @end deffn
- @deffn Procedure promise-set-exception! promise exception
- Associate @var{exception} with the @var{promise}.
- When the @var{promise} is forced the @var{exception} gets thrown.
- @end deffn
- @deffn Procedure promise-set-alias! promise other
- Bind the @var{promise} to be an alias of @var{other}.
- Forcing @var{promise} will cause @var{other} to be forced.
- @end deffn
- @deffn Procedure promise-set-thunk! promise thunk
- Associate @var{thunk} (a zero-argument procedure) with the @var{promise}.
- The first time the @var{promise} is forced will causes the
- @var{thunk} to be called, with the result (a value or an exception)
- saved for future calls.
- @end deffn
- @deffn Procedure make-promise obj
- The @code{make-promise} procedure returns a promise which,
- when forced, will return @var{obj}. It is similar to @code{delay}, but
- does not delay its argument: it is a procedure rather than
- syntax. If @var{obj} is already a promise, it is returned.
- Because of Kawa's implicit forcing, there is seldom a
- need to use @code{make-promise}, except for portability.
- @end deffn
- @subsection Lazy and eager types
- @deffn Type promise[T]
- This parameterized type is the type of promises that evaluate to
- an value of type @code{T}.
- It is equivalent to the Java interface @code{gnu.mapping.Lazy<T>}.
- The implementation class for promises is usually @code{gnu.mapping.Promise},
- though there are other classes that implement @code{Lazy},
- most notably @code{gnu.mapping.Future}, used for futures,
- which are promises evaluated in a separate thread.
- @end deffn
- Note the distinction between the types @code{integer}
- (the type of actual (eager) integer values), and @code{promise[integer]}
- (the type of (lazy) promises that evaluate to integer).
- The two are compatible: if a @code{promise[integer]} value is provided
- in a context requiring an @code{integer} then it is automatically
- evaluated (forced). If an @code{integer} value is provided
- in context requiring a @code{promise[integer]}, that conversion is basically
- a no-op (though the compiler may wrap the @code{integer}
- in a pre-forced promise).
- In a fully-lazy language there would be no distinction, or
- at least the promise type would be the default. However, Kawa is
- a mostly-eager language, so the eager type is the default.
- This makes efficient code-generation easier: If an expression
- has an eager type, then the compiler can generate code that
- works on its values directly, without having to check for laziness.
- @node Repeat forms
- @section Repeat patterns and expressions
- Many programming languages have some variant
- of @uref{https://en.wikipedia.org/wiki/List_comprehension,list comprehension syntax}.
- Kawa splits this into two separate forms,
- that can be in separate parts of the program:
- @itemize
- @item
- @cindex repeat pattern
- A @dfn{repeat pattern} as you might guess
- repeats a pattern by matching the pattern once for each element of a sequence.
- For example, assume @code{A} is a some sequence-valued expression.
- Then:
- @example
- #|kawa:3|# @kbd{(! [a::integer ...] A)}
- @end example
- Here @samp{a::integer ...} is a @var{repeat pattern} that
- matches all the elements pf @code{A}.
- We call @samp{a::integer} the @dfn{repeated pattern} - it matches an
- individual element of @code{A}.
- @cindex repeat variable
- Any variable defined in a repeated pattern is a @dfn{repeat variable}.
- In the example, that would be @code{a}.
- @item
- @cindex repeat expression
- A @dfn{repeat expression} creates a sequence by repeating
- an expression for each element of the result.
- @example
- #|kawa:4|# @kbd{[(* 2 a) ...]}
- [4 6 10 14 22]
- @end example
- In this case @samp{(* 2 a) ...} is the repeat expression.
- The @dfn{repeated expression} is @samp{(* 2 a)}.
- The repeated expression is evaluated once for each element
- of any contained repeat variable.
- If there is more than one repeat variable, they are
- repeated in parallel, as many times as the ``shortest'' repeat variable,
- similar to the @code{map} procedure.
- (If there is no repeat variable, the repeated expression is potentially
- evaluated infinitely many times, which is not allowed.
- A planned extension will allow it for lazy repeated expression.)
- @end itemize
- The use of @samp{...} for repeat patterns and expressions mirrors
- exactly their use in @code{syntax-rules} patterns and templates.
- It is an error to use a repeat variable outside of repeat context:
- @example
- #|kawa:5|# @kbd{a}
- /dev/stdin:2:1: using repeat variable 'a' while not in repeat context
- @end example
- The repeat form feature is not yet complete.
- It is missing functionality such as selecting only some elements
- from a repeat sequence, lazy sequences, and it could be optimized more.
- A repeat variable can be used multiple times in the
- same repeat expressions, or different repeat expressions:
- @example
- #|kawa:7|# @kbd{[a ... a ...]}
- [2 3 5 7 11 2 3 5 7 11]
- #|kawa:8|# @kbd{[(* a a) ...]}
- [4 9 25 49 121]
- @end example
- Repeat expressions are useful not just in sequence literals,
- but in the argument list of a procedure call, where the resulting sequence
- is spliced into the argument list. This is especially useful for functions
- that take a variable number of arguments, because that enables a
- convenient way to do @uref{https://en.wikipedia.org/wiki/Fold_(higher-order_function),fold/accumulate/reduce} operations. For example:
- @example
- #|kawa:9|# @kbd{(+ a ...)}
- 28
- @end example
- because 28 is the result of @code{(+ 2 3 5 7 11)}.
- An elegant way to implement @uref{https://en.wikipedia.org/wiki/Dot_product,dot product}:
- @example
- (define (dot-product [x ...] [y ...])
- (+ (* x y) ...))
- @end example
- When an ellipse expression references two or more distinct repeat variables
- then they are processed ``in parallel''.
- That does not (necessarily) imply muliple threads, but that the first element
- of the repeat result is evaluated using the first element of all the repeat sequences,
- the second element of the result uses the second element of all the repeat sequences, and so on.
- @subsubheading Sub-patterns in repeat patterns
- While the repeated pattern before the @samp{...}
- is commonly in identifier, it may be a more complex pattern.
- We showed earlier the repeated pattern with a type specifier,
- which applies to each element:
- @example
- #|kawa:11|# @kbd{(define (isum [x::integer ...]) (+ x ...))}
- #|kawa:12|# @kbd{(isum [4 5 6])}
- 15
- #|kawa:12|# @kbd{(isum [4 5.1 6])}
- Argument #1 (null) to 'isum' has wrong type
- at gnu.mapping.CallContext.matchError(CallContext.java:189)
- at atInteractiveLevel-6.isum$check(stdin:11)
- ...
- @end example
- (The stack trace line number @code{stdin:11} is that of the @code{isum} definition.)
- You can nest repeat patterns, allowing matching against sequences whose elements are sequences.
- @example
- #|kawa:31|# @kbd{(define (fun2 [[x ...] ...] [y ...])}
- #|.....32|# @kbd{ [[(+ x y) ...] ...])}
- #|kawa:33|# @kbd{(fun2 [[1 2 3] [10 11 12]] [100 200])}
- [[101 102 103] [210 211 212]]
- @end example
- Note that @code{x} is double-nested, while @code{y} is singly-nested.
- Here each element is constrained to be a pair (a -element sequence):
- @example
- #|kawa:1|# @kbd{(! [[x y] ...] [[11 12] [21 22] [31 32]])}
- #|kawa:2|# @kbd{[(+ x y) ...]}
- #(23 43 63)
- #|kawa:3|# @kbd{[[x ...] [y ...]]}
- #(#(11 21 31) #(12 22 32))
- @end example
- @node Threads
- @section Threads
- There is a very preliminary interface to create parallel threads.
- The interface is similar to the standard @code{delay}/@code{force},
- where a thread is basically the same as a promise, except that
- evaluation may be in parallel.
- @deffn Syntax future expression
- Creates a new thread that evaluates @var{expression}.
- (The result extends @code{java.lang.Thread} and
- implements @code{gnu.mapping.Lazy}.)
- @end deffn
- @deffn Procedure force thread
- The standard @code{force} function is generalized to also work
- on threads. It waits for the thread's @var{expression} to finish
- executing, and returns the result.
- @end deffn
- @deffn Procedure runnable function
- Creates a new @code{Runnable} instance from a function.
- Useful for passing to Java code that expects a @code{Runnable}.
- You can get the result (a value or a thrown exception) using the
- @code{getResult} method.
- @end deffn
- @deffn Syntax synchronized object form ...
- Synchronize on the given @var{object}. (This means getting an
- exclusive lock on the object, by acquiring its @dfn{monitor}.)
- Then execute the @var{form}s while holding the lock.
- When the @var{form}s finish (normally or abnormally by throwing
- an exception), the lock is released.
- Returns the result of the last @var{form}.
- Equivalent to the Java @code{synchronized} statement,
- except that it may return a result.
- @end deffn
- @node Exceptions
- @section Exception handling
- An @dfn{exception} is an object used to signal an error or
- other exceptional situation. The program or run-time system
- can @dfn{throw} the exception when an error is discovered.
- An exception handler is a program construct that registers
- an action to handle exceptions when the handler is active.
- If an exception is thrown and not handled then the
- read-eval-print-loop will print a stack trace, and bring
- you back to the top level prompt.
- When not running interactively, an unhandled exception
- will normally cause Kawa to be exited.
- In the Scheme exception model (as of R6RS and R7RS),
- exception handlers are one-argument procedures that determine
- the action the program takes when an exceptional situation is signaled.
- The system implicitly maintains a
- current exception handler in the dynamic environment.
- The program raises an exception by invoking the current
- exception handler, passing it an object encapsulating information
- about the exception. Any procedure accepting
- one argument can serve as an exception handler and any
- object can be used to represent an exception.
- The Scheme exception model is implemented on top of the Java VM's
- native exception model where the only objects that
- can be thrown are instances of @code{java.lang.Throwable}.
- Kawa also provides direct access to this native model,
- as well as older Scheme exception models.
- @deffn Procedure with-exception-handler handler thunk
- It is an error if @var{handler} does not accept one argument.
- It is also an error if @var{thunk} does not accept zero arguments.
- The @code{with-exception-handler} procedure returns the results
- of invoking @var{thunk}. The @var{handler} is installed as the current
- exception handler in the dynamic environment used for the
- invocation of @var{thunk}.
- @example
- (call-with-current-continuation
- (lambda (k)
- (with-exception-handler
- (lambda (x)
- (display "condition: ")
- (write x)
- (newline)
- (k 'exception))
- (lambda ()
- (+ 1 (raise ’an-error))))))
- @result{} exception
- @i{and prints} condition: an-error
- @end example
- @example
- (with-exception-handler
- (lambda (x)
- (display "something went wrong\n"))
- (lambda ()
- (+ 1 (raise ’an-error))))
- @i{prints} something went wrong
- @end example
- After printing, the second example then raises another exception.
- @PerformanceNote{} The @var{thunk} is inlined if it is a
- lambda expression. However, the @var{handler} cannot be inlined
- even if it is a lambda expression, because it could be called by
- @code{raise-continuable}. Using the @code{guard} form is
- usually more efficient.
- @end deffn
- @deffn Procedure raise obj
- Raises an exception by invoking the current exception handler on @var{obj}.
- The handler is called with the same dynamic
- environment as that of the call to raise, except that the
- current exception handler is the one that was in place when
- the handler being called was installed. If the handler returns,
- then @var{obj} is re-raised in the same dynamic environment as the handler.
- If @var{obj} is an instance of @code{java.lang.Throwable},
- then @code{raise} has the same effect as @code{primitive-throw}.
- @end deffn
- @deffn Procedure raise-continuable obj
- Raises an exception by invoking the current exception handler on @var{obj}.
- The handler is called with the same dynamic
- environment as the call to @code{raise-continuable}, except
- that: (1) the current exception handler is the one that was
- in place when the handler being called was installed, and
- (2) if the handler being called returns, then it will again
- become the current exception handler.
- If the handler returns, the values it returns become the values
- returned by the call to @code{raise-continuable}.
- @example
- (with-exception-handler
- (lambda (con)
- (cond
- ((string? con)
- (display con))
- (else
- (display "a warning has been issued")))
- 42)
- (lambda ()
- (+ (raise-continuable "should be a number")
- 23)))
- @i{prints:} should be a number
- @result{} 65
- @end example
- @end deffn
- @deffn Syntax guard @var{variable} @atleastone{@stxref{cond-clause}} @stxref{body}
- The @meta{body} is evaluated with an exception handler that binds
- the raised object to @var{variable} and, within the scope of that binding,
- evaluates the clauses as if they were the clauses of a @code{cond} expression.
- That implicit @code{cond} expression is evaluated with
- the continuation and dynamic environment of the @code{guard}
- expression. If every cond-clause’s test evaluates to @code{#f}
- and there is no @code{else} clause, then @code{raise-continuable} is
- invoked on the raised object within the dynamic environment of the
- original call to @code{raise} or @code{raise-continuable},
- except that the current exception handler is that of the
- @code{guard} expression.
- @example
- (guard (condition
- ((assq 'a condition) => cdr)
- ((assq 'b condition)))
- (raise (list (cons 'a 42))))
- @result{} 42
- @end example
- @example
- (guard (condition
- ((assq 'a condition) => cdr)
- ((assq 'b condition)))
- (raise (list (cons 'b 23))))
- @result{} (b . 23)
- @end example
- @PerformanceNote{} Using @code{guard} is moderately efficient:
- there is some overhead compared to using native exception handling,
- but both the @var{body} and the handlers in the @var{cond-clause}
- are inlined.
- @end deffn
- @deffn Procedure dynamic-wind in-guard thunk out-guard
- All three arguments must be 0-argument procedures.
- First calls @var{in-guard}, then @var{thunk}, then @var{out-guard}.
- The result of the expression is that of @var{thunk}.
- If @var{thunk} is exited abnormally (by throwing an exception or
- invoking a continuation), @var{out-guard} is called.
- If the continuation of the dynamic-wind is re-entered (which
- is not yet possible in Kawa), the @var{in-guard} is called again.
- This function was added in R5RS.
- @end deffn
- @deffn Procedure read-error? obj
- Returns #t if @var{obj} is an object raised by the @code{read} procedure.
- (That is if @var{obj} is a @code{gnu.text.SyntaxException}.)
- @end deffn
- @deffn Procedure file-error? obj
- Returns #t if @var{obj} is an object raised by inability to open an input
- or output port on a file.
- (This includes @code{java.io.FileNotFoundException} as well
- as certain other exceptions.)
- @end deffn
- @subsection Simple error objects
- @deffn Procedure error message obj ...
- Raises an exception as if by calling @code{raise}
- on a newly allocated @dfn{simple error object},
- which encapsulates the information provided by @var{message}
- (which should a string), as well as any @var{obj} arguments,
- known as the irritants.
- The string representation of a simple error object is as if calling
- @code{(format "#<ERROR ~a~@{ ~w~@}>" @var{message} @var{irritants})}.
- (That is the @var{message} is formatted as if with @code{display}
- while each irritant @var{obj} is formatted as if with @code{write}.)
- This procedure is part of SRFI-23, and R7RS.
- It differs from (and is incompatible with) R6RS's @code{error} procedure.
- @end deffn
- @deffn Procedure error-object? obj
- Returns @code{#t} if @var{obj} is a simple error object.
- Specifically, that @var{obj} is an instance of @code{kawa.lang.NamedException}.
- Otherwise, it returns @code{#f}.
- @end deffn
- @deffn Procedure error-object-message error-object
- Returns the message encapsulated by error-object,
- which must be a simple error object.
- @end deffn
- @deffn Procedure error-object-irritants error-object
- Returns a list of the irritants (other arguments)
- encapsulated by error-object, which must be a simple error object.
- @end deffn
- @subsection Named exceptions
- These functions associate a symbol with exceptions
- and handlers: A handler catches an exception if the symbol matches.
- @deffn Procedure catch key thunk handler
- Invoke @var{thunk} in the dynamic context of @var{handler} for
- exceptions matching @var{key}. If thunk throws to the symbol @var{key},
- then @var{handler} is invoked this way:
- @example
- (handler key args ...)
- @end example
- @var{key} may be a symbol. The @var{thunk} takes no
- arguments. If @var{thunk} returns normally, that is the return value of
- @code{catch}.
- Handler is invoked outside the scope of its own @code{catch}. If
- @var{handler} again throws to the same key, a new handler from further
- up the call chain is invoked.
- If the key is @code{#t}, then a throw to @emph{any} symbol will match
- this call to @code{catch}.
- @end deffn
- @deffn Procedure throw key arg ...
- Invoke the catch form matching @var{key}, passing the @var{arg}s to the
- current @var{handler}.
- If the key is a symbol it will match catches of the same
- symbol or of @code{#t}.
- If there is no handler at all, an error is signaled.
- @end deffn
- @subsection Native exception handling
- @deffn Procedure primitive-throw exception
- Throws the @var{exception}, which must be an instance of a sub-class
- of @code{java.lang.Throwable}.
- @end deffn
- @deffn Syntax try-finally body handler
- Evaluate @var{body}, and return its result.
- However, before it returns, evaluate @var{handler}.
- Even if @var{body} returns abnormally (by throwing an exception),
- @var{handler} is evaluated.
- (This is implemented just like Java's @code{try}-@code{finally}.
- However, the current implementation does not duplicate the @var{handler}.)
- @end deffn
- @deffn Syntax try-catch body handler ...
- Evaluate @var{body}, in the context of the given @var{handler} specifications.
- Each @var{handler} has the form:
- @example
- @var{var} @var{type} @var{exp} ...
- @end example
- If an exception is thrown in @var{body}, the first @var{handler}
- is selected such that the thrown exception is an instance of
- the @var{handler}'s @var{type}. If no @var{handler} is selected,
- the exception is propagated through the dynamic execution context
- until a matching @var{handler} is found. (If no matching @var{handler}
- is found, then an error message is printed, and the computation terminated.)
- Once a @var{handler} is selected,
- the @var{var} is bound to the thrown exception, and the @var{exp} in
- the @var{handler} are executed. The result of the @code{try-catch}
- is the result of @var{body} if no exception is thrown, or the
- value of the last @var{exp} in the selected @var{handler} if an
- exception is thrown.
- (This is implemented just like Java's @code{try}-@code{catch}.)
- @end deffn
- @node Control features
- @chapter Control features
- @menu
- * Mapping functions::
- * Multiple values::
- @end menu
- @node Mapping functions
- @section Mapping functions
- The procedures @code{string-for-each} and @code{string-map}
- are documented under @ref{Strings}.
- The procedure @code{string-cursor-for-each} is documented under @ref{String Cursor API}.
- @deffn Procedure map @var{proc} sequence@sub{1} sequence@sub{2} ...
- @deffnx Procedure for-each @var{proc} sequence@sub{1} sequence@sub{2} ...
- The @code{map} procedure applies @var{proc} element-wise to the elements
- of the @var{sequence}s and returns a list of the results, in order.
- The dynamic order in which @var{proc} is applied to
- the elements of the @var{sequence}s is unspecified.
- The @code{for-each} procedure does the same,
- but is executed for the side-effects of @var{proc}, whose result (if any)
- is discarded.
- Unlike @code{map}, @code{for-each} is guaranteed to call @var{proc}
- on the elements of the @var{sequences}s in order from the first element(s)
- to the last.
- The value returned by @code{for-each} is the void value.
- Each @var{sequence} must be a generalized sequence.
- (Traditionally, these arguments were restricted to lists,
- but Kawa allows sequences, including vectors, Java arrays, and strings.)
- If more than one @var{sequence} is given and not all
- @var{sequence}s have the same length, the procedure terminates when the
- shortest @var{sequence} runs out.
- The @var{sequence}s can be infinite (for example circular lists),
- but it is an error if all of them are infinite.
- The @var{proc} must be a procedure that accepts as many arguments
- as there are @var{sequence} arguments.
- It is an error for @var{proc} to mutate any of the @var{sequence}s.
- In the case of @code{map}, @var{proc} must return a single value.
- @example
- (map cadr '((a b) (d e) (g h)))
- @result{} (b e h)
- (map (lambda (n) (expt n n))
- '(1 2 3 4 5))
- @result{} (1 4 27 256 3125)
- (map + ’(1 2 3) ’(4 5 6 7)) @result{} (5 7 9)
- (let ((count 0))
- (map (lambda (ignored)
- (set! count (+ count 1))
- count)
- '(a b)))
- @result{} (1 2) @i{or} (2 1)
- @end example
- The result of @code{map} is a list, even if the arguments are non-lists:
- @example
- (map +
- #(3 4 5)
- (float[] 0.5 1.5))
- @result{} (3.5 5.5)
- @end example
- To get a vector result, use @code{vector-map}.
- @example
- (let ((v (make-vector 5)))
- (for-each (lambda (i)
- (vector-set! v i (* i i)))
- '(0 1 2 3 4))
- v)
- @result{} #(0 1 4 9 16)
- @end example
- A string is considered a sequence of @code{character} values
- (not 16-bit @code{char} values):
- @example
- (let ((v (make-vector 10 #\-)))
- (for-each (lambda (i ch)
- (vector-set! v i ch))
- [0 <: ]
- "Smile 😃!")
- v)
- @result{} #(#\S #\m #\i #\l #\e #\space #\x1f603 #\! #\- #\-)
- @end example
- @PerformanceNote{} These procedures are pretty well optimized.
- For each @var{sequence} the compiler will by default create an
- iterator.
- @c FUTURE (This can be done without object allocation XXXX)
- However, if the type of the @var{sequence} is known, the compiler will
- inline the iteration code.
- @end deffn
- @deffn Procedure vector-map @var{proc} @vari{sequence} @varii{sequence} @dots{}
- Same as the @code{map} procedure, except the result is a vector.
- (Traditionally, these arguments were restricted to vectors,
- but Kawa allows sequences, including lists, Java arrays, and strings.)
- @example
- (vector-map cadr '#((a b) (d e) (g h)))
- @result{} #(b e h)
- (vector-map (lambda (n) (expt n n))
- '#(1 2 3 4 5))
- @result{} #(1 4 27 256 3125)
- (vector-map + '#(1 2 3) ’#(4 5 6 7))
- @result{} #(5 7 9)
- (let ((count 0))
- (vector-map
- (lambda (ignored)
- (set! count (+ count 1))
- count)
- '#(a b)))
- @result{} #(1 2) @i{or} #(2 1)
- @end example
- @end deffn
- @deffn Procedure vector-for-each @var{proc} @vari{vector} @varii{vector} @dots{}
- Mostly the same as @code{for-each},
- however the arguments should be generalized vectors.
- Specifically, they should implement @code{java.util.List}
- (which both regular vectors and uniform vectors do).
- The @var{vectors} should also be efficiently indexable.
- (Traditionally, these arguments were restricted to vectors,
- but Kawa allows sequences, including lists, Java arrays, and strings.)
- @example
- (let ((v (make-list 5)))
- (vector-for-each
- (lambda (i) (list-set! v i (* i i)))
- '#(0 1 2 3 4))
- v)
- @result{} (0 1 4 9 16)
- @end example
- @end deffn
- @node Multiple values
- @section Multiple values
- The multiple-value feature was added in R5RS.
- @deffn Procedure values object ...
- Delivers all of its arguments to its continuation.
- @end deffn
- @deffn Procedure call-with-values producer consumer
- Calls its @var{producer} argument with no arguments and a
- continuation that, when passed some values, calls the
- @var{consumer} procedure with those values as arguments.
- @example
- (call-with-values (lambda () (values 4 5))
- (lambda (a b) b))
- @result{} 5
- (call-with-values * -) @result{} -1
- @end example
- @PerformanceNote{} If either the @var{producer} or @var{consumer} is a
- fixed-arity lambda expression, it is inlined.
- @end deffn
- @deffn Syntax define-values @stxref{formals} @stxref{expression}
- It is an error if a variable appears more than once in the
- set of @meta{formals}.
- The @meta{expression} is evaluated, and the @meta{formals}
- are bound to the return values in the same way that the @meta{formals} in
- a @code{lambda} expression are matched to the arguments in a procedure call.
- @example
- (define-values (x y) (integer-sqrt 17))
- (list x y) @result{} (4 1)
- (let ()
- (define-values (x y) (values 1 2))
- (+ x y))
- @result{} 3
- @end example
- @end deffn
- @deffn Syntax let-values @stxlit{((}@stxref{formals} @stxref{expression}@stxlit{)} ...@stxlit{)} @stxref{body}
- Each @var{formals} should be a formal arguments list, as for a @code{lambda}.
- The @var{expression}s are evaluated in the current environment, the
- variables of the @var{formals} are bound to fresh locations, the return
- values of the @var{expression}s are stored in the variables, the
- @var{body} is evaluated in the extended environment, and the values of
- the last expression of @var{body} are returned. The @var{body} is a
- "tail body", cf section 3.5 of the R5RS.
- The matching of each @var{formals} to values is as for the matching of
- @var{formals} to arguments in a @code{lambda} expression, and it is an
- error for an @var{expression} to return a number of values that does not
- match its corresponding @var{formals}.
- @example
- (let-values (((a b . c) (values 1 2 3 4)))
- (list a b c)) @result{} (1 2 (3 4))
- (let ((a 'a) (b 'b) (x 'x) (y 'y))
- (let-values (((a b) (values x y))
- ((x y) (values a b)))
- (list a b x y))) @result{} (x y a b)
- @end example
- @end deffn
- @deffn Syntax let*-values @stxlit{((}@stxref{formals} @stxref{expression}@stxlit{)} ...@stxlit{)} @stxref{body}
- Each @var{formals} should be a formal arguments list as for a
- @code{lambda} expression.
- @code{let*-values} is similar to @code{let-values}, but the bindings are
- performed sequentially from left to right, and the region of a binding
- indicated by (@var{formals} @var{expression}) is that part of the
- @code{let*-values} expression to the right of the binding. Thus the
- second binding is done in an environment in which the first binding is
- visible, and so on.
- @example
- (let ((a 'a) (b 'b) (x 'x) (y 'y))
- (let*-values (((a b) (values x y))
- ((x y) (values a b)))
- (list a b x y))) @result{} (x y x y)
- @end example
- @end deffn
- @deffn Syntax receive @stxref{formals} @stxref{expression} @stxref{body}
- This convenience
- form (from @uref{http://srfi.schemers.org/srfi-8/srfi-8.html, SRFI-8})
- is equivalent to:
- @example
- (let-values ((@var{formals} @var{expression})) @var{body})
- @end example
- For example:
- @example
- (receive a (values 1 2 3 4)
- (reverse a)) @result{} (4 3 2 1)
- (receive (a b . c) (values 1 2 3 4)
- (list a b c)) @result{} (1 2 (3 4))
- (let ((a 'a) (b 'b) (x 'x) (y 'y))
- (receive (a b) (values x y)
- (receive (x y) (values a b)
- (list a b x y)))) @result{} (x y x y)
- @end example
- @end deffn
- @deffn Procedure values-append arg1 ...
- The values resulting from evaluating each argument are appended
- together.
- @end deffn
- @node Symbols and namespaces
- @chapter Symbols and namespaces
- An identifier is a name that appears in a program.
- A symbol is an object representing a string that cannot be
- modified. This string is called the symbol's name. Unlike strings, two
- symbols whose names are spelled the same way are indistinguishable.
- A symbol is immutable (unmodifiable) and normally viewed as atomic.
- Symbols are useful for many applications; for instance, they may be
- used the way enumerated values are used in other languages.
- In addition to the simple symbols of standard Scheme, Kawa
- also has compound (two-part) symbols.
- @menu
- * Simple symbols::
- * Namespaces::
- * Keywords::
- * Special named constants::
- @end menu
- @node Simple symbols, Namespaces, , Symbols and namespaces
- @section Simple symbols
- Simple symbols have no properties other than their name, an immutable string.
- They have the useful property that two simple symbols
- are identical (in the sense of @func{eq?}, @func{eqv?} and
- @func{equal?}) if and only if their names are spelled the same way. A
- symbol literal is formed using @func{quote}.
- @deffn Procedure {symbol?} @var{obj}
- Return @true{} if @var{obj} is a symbol, @false{} otherwise.
- @example
- (symbol? 'foo) @result{} #t
- (symbol? (car '(a b))) @result{} #t
- (symbol? "bar") @result{} #f
- (symbol? 'nil) @result{} #t
- (symbol? '()) @result{} #f
- (symbol? #f) @result{} #f
- @end example
- @end deffn
- @deffn Procedure {symbol->string} @var{symbol}
- Return the name of @var{symbol} as an immutable string.
- @example
- (symbol->string 'flying-fish) @result{} "flying-fish"
- (symbol->string 'Martin) @result{} "Martin"
- (symbol->string (string->symbol "Malvina")) @result{} "Malvina"
- @end example
- @end deffn
- @deffn Procedure {string->symbol} @var{string}
- Return the symbol whose name is @var{string}.
- @example
- (eq? 'mISSISSIppi 'mississippi)
- @result{} #f
- (string->symbol "mISSISSIppi")
- @result{} the symbol with name "mISSISSIppi"
- (eq? 'bitBlt (string->symbol "bitBlt"))
- @result{} #t
- (eq? 'JollyWog (string->symbol (symbol->string 'JollyWog)))
- @result{} #t
- (string=? "K. Harper, M.D."
- (symbol->string (string->symbol "K. Harper, M.D.")))
- @result{} #t
- @end example
- @end deffn
- @node Namespaces, Keywords, Simple symbols, Symbols and namespaces
- @section Namespaces and compound symbols
- Different applications may want to use the same symbol
- to mean different things. To avoid such @dfn{name clashes}
- we can use @dfn{compound symbols}, which have two string parts:
- a @dfn{local name} and a @dfn{namespace URI}.
- The namespace-uri can be any string, but it is
- recommended that it have the form of an absolute
- @uref{http://en.wikipedia.org/wiki/Uniform_Resource_Identifier,URI}.
- It would be too verbose to write the full URI all the
- time, so one usually uses a @dfn{namespace prefix}
- (namespace alias) as a short local alias to refer to a
- namespace URI.
- Compound symbols are usually written using the infix colon operator:
- @example
- @var{prefix}@stxlit{:}@var{local-name}
- @end example
- where @var{prefix} is a namespace alias bound
- to some (lexically-known) namespace URI.
- Compound symbols are used for namespace-aware XML processing.
- @subsection Namespace objects
- A @dfn{namespace} is a mapping from strings to symbols.
- The string is the local-name of the resulting symbol.
- A namespace is similar to a Common Lisp @dfn{package}.
- A namespace has a namespace-uri, which a string;
- it is recommended that it have the form of an absolute URI.
- A namespace may optionally have a prefix, which is a string used
- when printing out symbols belonging to the namespace.
- (If you want ``equivalent symbols'' (i.e. those that have the same
- local-name and same uri) to be the identical symbol object, then
- you should use namespaces whose prefix is the empty string.)
- @deffn Constructor namespace name [prefix]
- Return a namespace with the given @var{name} and @var{prefix}.
- If no such namespace exists, create it.
- The @var{namespace-name} is commonly a URI, especially when working with XML,
- in which case it is called a @var{namespace-uri}. However, any non-empty
- string is allowed.
- The prefix can be a string or a simple symbol.
- (If a symbol is used, then the symbol's local-name is used.)
- The default for @var{prefix} is the empty string.
- Multiple calls with the same arguments will yield the same namespace object.
- @end deffn
- The reader macro @code{#,namespace} is equivalent to the
- @code{namespace} function, but it is invoked at read-time:
- @example
- #,(namespace "http://www.w3.org/1999/XSL/Transform" xsl)
- (eq? #,(namespace "foo") (namespace "foo")) @result{} #t
- @end example
- The form @code{(,#namespace "" "")} returns the default @dfn{empty namespace},
- which is used for simple symbols.
- @deffn Procedure namespace-uri namespace
- Return the namespace-uri of the argument @var{namespace}, as a string.
- @end deffn
- @deffn Procedure namespace-prefix namespace
- Return the namespace prefix of the argument @var{namespace}, as a string.
- @end deffn
- @subsection Compound symbols
- A compound symbol is one that belongs to a namespace other than the
- default empty namespace, and (normally) has a non-empty namespace uri.
- (It is possible for a symbol to belong to a non-default namespace
- and have an empty namespace uri, but that is not recommended.)
- @deffn Constructor symbol local-name namespace-spec
- @deffnx Constructor symbol local-name [uri [prefix]]
- Construct a symbol with the given @var{local-name} and namespace.
- If @var{namespace-spec} is a namespace object, then find (or, if needed,
- construct) a symbol with the given @var{local-name} belonging to the
- namespace. Multiple calls to @code{symbol} with the same namespace
- and @var{local-name} will yield the same symbol object.
- If uri is a string (optionally followed by a prefix),
- then:
- @example
- (symbol lname uri [prefix])
- @end example
- is equivalent to:
- @example
- (symbol lname (namespace uri [prefix]))
- @end example
- Using @code{#t} for the @var{namespace-spec} is equivalent to
- using the empty namespace @code{#,(namespace "")}.
- Using @code{#!null} or @code{#f} for the @var{namespace-spec}
- creates an @var{uninterned} symbol, which does not belong to
- any namespace.
- @end deffn
- @deffn Procedure symbol-local-name symbol
- Return the local name of the argument symbol, as an immutable string.
- (The string is interned, except in the case of an uninterned symbol.)
- @end deffn
- @deffn Procedure symbol-prefix symbol
- Return the prefix of the argument symbol, as an immutable
- (and interned) string.
- @end deffn
- @deffn Procedure symbol-namespace-uri symbol
- Return the namespace uri of the argument symbol, as an immutable
- (and interned) string.
- @end deffn
- @deffn Procedure symbol-namespace symbol
- Return the namespace object (if any) of the argument symbol.
- Returns @code{#!null} if the symbol is uninterned.
- @end deffn
- @deffn Procedure {symbol=?} @vari{symbol} @varii{symbol} @variii{symbol} @dots{}
- Return @true{} if the symbols are equivalent as symbols,
- i.e., if their local-names and namespace-uris are the same.
- They may have different values of @code{symbol-prefix} and @code{symbol-namespace}.
- If a symbol is uninterned (or is @code{#!null}) then @code{symbol=?}
- returns the same result as @code{eq?}.
- @end deffn
- Two symbols are @code{equal?} or @code{eqv?} if they're @code{symbol=?}.
- @subsection Namespace aliases
- A namespace is usually referenced using a shorter @dfn{namespace alias},
- which is is a lexical definition that binds a namespace prefix
- to a namespace object (and thus a namespace uri).
- This allows using compound symbols as identifiers in Scheme programs.
- @deffn Syntax define-namespace name namespace-name
- Defines @var{name} as a @dfn{namespace prefix} - a lexically scoped
- "nickname" for the namespace
- whose full name is @var{namespace-name}, which should be a non-empty
- string literal.
- It is customary for the string have syntactic form of
- an absolute @uref{http://en.wikipedia.org/wiki/Uniform_Resource_Identifier,URI},
- but any non-empty string is acceptable and is used without
- further interpretation.
- Any symbols in the scope of this definitions that contain a colon, and
- where the part before the colon matches the @var{name} will be
- treated as being in the package/namespace whose global unique name
- is the @var{namespace-name}.
- Has mostly the same effect as:
- @example
- (define-constant @var{name} #,(namespace @var{namespace-name})
- @end example
- However, using @code{define-namespace} (rather than @code{define-constant})
- is recommended if you want to use compound symbols as names of
- variables, especially local variables, or if you want to quote
- compound symbols.
- Note that the prefix is only visible lexically: it is not
- part of the namespace, or thus indirectly the symbols, and
- so is not available when printing the symbol.
- You might consider using @code{define-xml-namespace} as an alternative.
- A namespace is similar to a Common Lisp package,
- and the @var{namespace-name} is like the name of the package.
- However, a namespace alias belongs to the lexical scope,
- while a Common Lisp package nickname is global
- and belongs to the package itself.
- If the namespace-name starts with the string @code{"class:"}, then the
- @var{name} can be used for invoking Java methods
- (@pxref{Method operations}) and accessing fields (@pxref{Field operations}).
- You can use a namespace as an abbreviation or renaming of a
- class name, but as a matter of style @code{define-alias} is preferred.
- @end deffn
- @deffn Syntax define-private-namespace name namespace-name
- Same as @code{define-namespace}, but the prefix @var{name}
- is local to the current module.
- @end deffn
- For example, you might have a set of a geometry definitions
- defined under the namespace-uri @code{"http://foo.org/lib/geometry"}:
- @example
- (define-namespace geom "http://foo.org/lib/geometry")
- (define (geom:translate x y)
- (java.awt.geom.AffineTransform:getTranslateInstance x y))
- (define geom:zero (geom:translate 0 0))
- geom:zero
- @result{} AffineTransform[[1.0, 0.0, 0.0], [0.0, 1.0, 0.0]]
- @end example
- You could have some other definitions for complex math:
- @example
- (define-namespace complex "http://foo.org/lib/math/complex")
- (define complex:zero +0+0i)
- @end example
- You can use a namespace-value directly in a compound name:
- @example
- (namespace "http://foo.org/lib/geometry"):zero
- @result{} AffineTransform[[1.0, 0.0, 0.0], [0.0, 1.0, 0.0]]
- @end example
- The variation @code{define-xml-namespace} is used for @ref{Creating XML nodes}.
- @deffn Syntax define-xml-namespace prefix "namespace-uri"
- Defines a namespace with prefix @var{prefix} and URI @var{namespace-uri}.
- This is similar to @code{define-namespace} but with two important differences:
- @itemize
- @item
- Every symbol in the namespace automatically maps to
- an element-constructor-type, as with the @code{html} namespace.
- @item
- The @var{prefix} is a component of the namespace object, and
- hence indirectly of any symbols belongining to the namespace.
- @end itemize
- Thus the definition is roughly equivalent to:
- @example
- (define-constant @var{name} #,(namespace @var{namespace-name name})
- @end example
- along with an infinite set of definitions, for every possible @var{tag}:
- @example
- (define (name:@var{tag} . rest) (apply make-element 'name:@var{tag} rest))
- @end example
- @end deffn
- @example
- $ kawa --output-format xml
- #|kawa:1|# @kbd{(define-xml-namespace na "Namespace1")}
- #|kawa:2|# @kbd{(define-xml-namespace nb "Namespace1")}
- #|kawa:3|# @kbd{(define xa (na:em "Info"))}
- #|kawa:4|# @kbd{xa}
- <na:em xmlns:na="Namespace1">Info</na:em>
- #|kawa:5|# @kbd{(define xb (nb:em "Info"))}
- #|kawa:6|# @kbd{xa}
- <nb:em xmlns:nb="Namespace1">Info</nb:em>
- @end example
- Note that the prefix is part of the qualified name
- (it is actually part of the namespace object),
- and it is used when printing the tag.
- Two qualified names (symbols) that have the same
- local-name and the same namespace-name are considered
- equal, even if they have different prefix. You can think of
- the prefix as annotation used when printing, but not
- otherwise part of the ``meaning'' of a compound symbol.
- They are the same object if they also have the same prefix.
- This is an important difference from traditional Lisp/Scheme symbols,
- but it is how XML QNames work.
- @example
- #|kawa:7|# (instance? xb na:em)
- true
- #|kawa:8|# (eq? 'na:em 'nb:em)
- false
- #|kawa:9|# (equal? 'na:em 'nb:em)
- true
- #|kawa:10|# (eqv? 'na:em 'nb:em)
- true
- @end example
- (Note that @code{#t} is printed as @code{true} when using XML formatting.)
- The predefined @code{html} prefix could be defined thus:
- @example
- (define-xml-namespace html "http://www.w3.org/1999/xhtml")
- @end example
- @node Keywords, Special named constants, Namespaces, Symbols and namespaces
- @section Keywords
- Keywords are similar to symbols.
- They are used mainly for specifying keyword arguments.
- Historically keywords have been self-evaluating (you did not need to
- quote them). This has changed: you must quote a keyword if you
- want a literal keyword value, and not quote it if it is used
- as a keyword argument.
- @display
- @stxdef{keyword} @stxref{identifier}@stxlit{:}
- | @stxlit{#:}@stxref{identifier}
- @end display
- The two syntaxes have the same meaning: The former is nicer-looking;
- the latter is more portable (and required if you use the
- @code{--r7rs} command-line flag).
- @quotation
- @emph{Details:}
- In r7rs and other Scheme standards the colon character
- does not have any special meaning, so @code{foo:} or @code{foo:bar}
- are just regular identifiers. Therefore some other Scheme variants
- that have keywords (including Guile and Racket) use the @code{#:} syntax.
- Kawa has some hacks so that @emph{most}
- standard Scheme programs that have colons in identifiers will work.
- However, for best compatibility, use the @code{--r7rs} command-line flag
- (which turns colon into a regular character in a symbol), and the
- @code{#:} syntax.
- @end quotation
- A keyword is a single token; therefore no whitespace is allowed between
- the @var{identifier} and the colon or after the @code{#:};
- these characters are not considered part of the name of the keyword.
- @deffn Procedure keyword? obj
- Return @code{#t} if @var{obj} is a keyword, and otherwise returns @code{#f}.
- @end deffn
- @deffn Procedure keyword->string keyword
- Returns the name of @var{keyword} as a string.
- The name does not include the final @code{#\:}.
- @end deffn
- @deffn Procedure string->keyword string
- Returns the keyword whose name is @var{string}.
- (The @var{string} does not include a final @code{#\:}.)
- @end deffn
- @node Special named constants, , Keywords, Symbols and namespaces
- @section Special named constants
- @defvr Constant #!optional
- Special self-evaluating literal used in lambda parameter lists
- before optional parameters.
- @end defvr
- @defvr Constant #!rest
- Special self-evaluating literal used in lambda parameter lists
- before the rest parameter.
- @end defvr
- @defvr Constant #!key
- Special self-evaluating literal used in lambda parameter lists
- before keyword parameters.
- @end defvr
- @defvr Constant #!eof
- The end-of-file object.
- Note that if the Scheme reader sees this literal at top-level,
- it is returned literally. This is indistinguishable from
- coming to the end of the input file. If you do not want to end reading,
- but want the actual value of @code{#!eof}, you should quote it.
- @end defvr
- @defvr Constant #!void
- The void value. Same as @code{(values)}.
- If this is the value of an expression in a read-eval-print loop,
- nothing is printed.
- @end defvr
- @defvr Constant #!null
- The Java @code{null} value. This is not really a Scheme value,
- but is useful when interfacing to low-level Java code.
- @end defvr
- @node Procedures, Numbers, Symbols and namespaces, Top
- @chapter Procedures
- @menu
- * Application and Arguments Lists::
- * Extended formals::
- * Procedure properties::
- * Generic procedures::
- * Partial application::
- @end menu
- @node Application and Arguments Lists
- @section Application and Arguments Lists
- When a procedure is called, the actual argument expressions are evaluated,
- and the resulting values becomes the actual argument list.
- This is then matched against the formal parameter list
- in the procedure definition, and (assuming they match)
- the procedure body is called.
- @anchor{argument list}
- @subsection Arguments lists
- An argument list has three parts:
- @itemize @bullet
- @item
- Zero or more @dfn{prefix arguments}, each of which is a value.
- These typically get bound to named required or optional formal parameters,
- but can also get bound to patterns.
- @item
- Zero or more @dfn{keyword arguments}, each of which is a keyword
- (an identifier specified with keyword syntax) combined with a value.
- These are bound to either named keyword formal parameters, or
- bundled in with a rest parameter.
- @item
- Zero or more @dfn{postfix arguments}, each of which is a value.
- These are usually bound to a ``rest'' formal parameter, which
- receives any remaining arguments.
- If there are no keyword arguments, then it ambiguous where
- prefix arguments end and where postfix arguments start.
- This is normally not a problem: the called procedure can
- split them up however it wishes.
- @end itemize
- Note that all keyword arguments have to be grouped together:
- It is not allowed to have a keyword argument followed by a plain
- argument followed by a keyword argument.
- The argument list is constructed by evaluating each @stxref{operand}
- of the @stxref{procedure-call} in order:
- @table @asis
- @item @var{expression}
- The @var{expression} is evaluated, yielding a single value
- that becomes a prefix or postfix argument.
- @item @var{keyword} @var{expression}
- The @var{expression} is evaluated. The resulting value combined
- with the @var{keyword} becomes a keyword argument.
- @item @stxlit{@@}@var{expression}
- The @var{expression} is evaluated.
- The result must be a sequence - a list, vector, or primitive array.
- The values of the sequence are appended to the resulting argument list.
- Keyword arguments are not allowed.
- @item @stxlit{@@:}@var{expression}
- The @var{expression} is evaluted.
- The result can be a sequence;
- a hash table (viewed as a collection of (keyword,value) pairs);
- or an @dfn{explicit argument list} object, which is a sequence of values
- @emph{or} keyword arguments.
- The values and keyword arguments
- are appended to the resulting argument list, though subject to the restriction
- that keyword arguments must be adjacent in the resulting argument list.
- @end table
- @anchor{Explicit argument list objects}
- @subsection Explicit argument list objects
- Sometimes it is useful to create an argument list out of
- pieces, take argument lists apart, iterate over them,
- and generally treat an argument list as an actual first-class value.
- Explicit argument list objects can take multiple forms.
- The simplest is a sequence: a list, vector, or primitive array.
- Each element of the list becomes a value in the resulting argument list.
- @example
- (define v1 '(a b c))
- (define v2 (int[] 10 11 12 13))
- (list "X" @@v1 "Y" @@v2 "Z")
- @result{} ("X" a b c "Y" 10 11 12 13 "Z")
- @end example
- Things get more complicated once keywords are involved.
- An explicit argument list with keywords is only allowed
- when using the @code{@@:} splicing form,
- not the @code{@@} form. It can be either
- a hash table, or the types @code{arglist} or @code{argvector}.
- @quotation
- @emph{Design note:} An argument list with keywords is straightforward
- in Common Lisp and some Scheme implementations (including order versions of
- Kawa): It's just a list some of whose @code{car} cells are keyword objects.
- The problem with this model is neither a human or the compiler can
- reliably tell when an argument is a keyword, since any variable
- might have been assigned a keyword. This limits performance and
- error checking.
- @end quotation
- A hash table (anything the implements @code{java.util.Map})
- whose keys are strings or keyword objects is
- interpreted as a sequence of keyword arguments,
- using the hash-table keys and values.
- @deffn Type argvector
- @deffnx Constructor argvector @arbno{@stxref{operand}}
- List of arguments represented as an immutable vector.
- A keyword argument takes two elements in this vector:
- A keyword object, followed by the value.
- @example
- (define v1 (argvector 1 2 k1: 10 k2: 11 98 99))
- (v1 4) @result{} 'k2
- (v1 5) @result{} 11
- @end example
- When @code{v1} is viewed as a vector it
- is equivalent to @code{(vector 1 2 'k1: 10 'k2: 11 98 99)}.
- (Note in this case the keywords need to be quoted, since
- the @code{vector} constructor does not take keyword arguments.)
- However, the @code{argvector} ``knows'' which arguments
- are actually keyword arguments, and can be examined using the
- @code{(kawa arglist)} library discussed below:
- @example
- (arglist-key-count (argvector 1 x: 2 3)) @result{} 1
- (arglist-key-count (argvector 1 'x: 2 3)) @result{} 0
- (arglist-key-count (vector 1 'x: 2 3)) @result{} 0
- @end example
- In this case:
- @example
- (fun 'a @@:v1)
- @end example
- is equivalent to:
- @example
- (fun 'a 1 2 k1: 10 k2: 11 98 99)
- @end example
- @end deffn
- @deffn Type arglist
- @deffnx Constructor arglist @arbno{@stxref{operand}}
- Similar to @code{argvector}, but compatible with @code{list}.
- If there are no keyword arguments, returns a plain list.
- If there is at least one keyword argument creates a special
- @code{gnu.mapping.ArgListPair} object that implements the
- usual @code{list} properties but internally wraps a @code{argvector}.
- @end deffn
- @anchor{Argument list library}
- @subsection Argument list library
- @example
- (import (kawa arglist))
- @end example
- In the following, @var{args} is an @code{arglist} or @code{argvector}
- (or in general any object that implement @code{gnu.mapping.ArgList}).
- Also, @var{args} can be any sequence, in which case it
- behaves like an @code{argvector} that has no keyword arguments.
- @deffn Procedure arglist-walk args proc
- Call @var{proc} once, in order, for each argument in @var{args}.
- The @var{proc} is called with two arguments,
- corresponding to @code{(arglist-key-ref @var{args} @var{i})}
- and @code{(arglist-arg-ref @var{args} @var{i})} for each @var{i} from 0
- up to @code{(arglist-arg-count @var{args})} (exclusive).
- I.e. the first argument is either @code{#!null} or the keyword (as a string);
- the second argument is the corresponding argument value.
- @example
- (define (print-arguments args #!optional (out (current-output-port)))
- (arglist-walk args
- (lambda (key value)
- (if key (format out "key: ~a value: ~w~%" key value)
- (format out "value: ~w~%" value)))))
- @end example
- @end deffn
- @deffn Procedure arglist-key-count args
- Return the number of keyword arguments.
- @end deffn
- @deffn Procedure arglist-key-start args
- Number of prefix arguments, which is the number of arguments before
- the first keyword argument.
- @end deffn
- @deffn Procedure arglist-arg-count args
- Return the number of arguments.
- The count includes the number of keyword arguments, but not the actual keywords.
- @example
- (arglist-arg-count (arglist 10 11 k1: -1 19)) @result{} 4
- @end example
- @end deffn
- @deffn Procedure arglist-arg-ref args index
- Get the @var{index}'th argument value.
- The @var{index} counts keyword argument values, but not the keywords themselves.
- @example
- (arglist-arg-ref (arglist 10 11 k1: -1 19) 2) @result{} -1
- (arglist-arg-ref (arglist 10 11 k1: -1 19) 3) @result{} 19
- @end example
- @end deffn
- @deffn Procedure arglist-key-ref args index
- The @var{index} counts arguments like @code{arglist-arg-ref} does.
- If this is a keyword argument, return the corresponding keyword
- (as a string); otherwise, return @code{#!null} (which counts as false).
- @example
- (arglist-key-ref (argvector 10 11 k1: -1 k2: -2 19) 3) @result{} "k2"
- (arglist-key-ref (argvector 10 11 k1: -1 k2: -2 19) 4) @result{} #!null
- @end example
- @end deffn
- @deffn Procedure arglist-key-index args key
- Search for a keyword matching @var{key} (which must be an interned string).
- If there is no such keyword, return -1.
- Otherwise return the keyword's index as an argument to @code{arglist-key-ref}.
- @end deffn
- @deffn Procedure arglist-key-value args key default
- Search for a keyword matching @var{key} (which must be an interned string).
- If there is no such keyword, return the @var{default}.
- Otherwise return the corresponding keyword argument's value.
- @end deffn
- @subsection Apply procedures
- @deffn Procedure apply proc @arbno{argi} argrest
- @var{Argrest} must be a sequence (list, vector, or string) or a
- primitive Java array.
- (This is an extension over standard Scheme, which requires that
- @var{args} be a list.)
- Calls the @var{proc} (which must be a procedure), using as arguments
- the @var{argi}... values plus all the elements of @var{argrest}.
- Equivalent to: @code{(}@var{proc} @arbno{@var{argi}} @code{@@}@var{argrest}@code{)}.
- @end deffn
- @deffn Syntax constant-fold proc arg1 ...
- Same as @code{(@var{proc} @var{arg1} ...)}, unless @var{proc} and
- all the following arguments are compile-time constants.
- (That is: They are either constant, or symbols that have a global
- binding and no lexical binding.) In that case, @var{proc}
- is applied to the arguments at compile-time, and the result replaces
- the @code{constant-fold} form. If the application raises an exception,
- a compile-time error is reported.
- For example:
- @example
- (constant-fold vector 'a 'b 'c)
- @end example
- is equivalent to @code{(quote #(a b c))}, assuming @code{vector}
- has not been re-bound.
- @end deffn
- @c The node name 'Extended formals' is klunky, but kept for compatibility
- @node Extended formals
- @section Lambda Expressions and Formal Parameters
- A @code{lambda} expression evaluates to a procedure.
- The environment in effect when the @code{lambda} expression was
- evaluated is remembered as part of the procedure. When
- the procedure is later called with some actual arguments,
- the environment in which the @code{lambda} expression was evaluated
- will be extended by binding the variables in the formal
- argument list to fresh locations, and the corresponding actual
- argument values will be stored in those locations.
- (A @dfn{fresh location} is one that is distinct from every previously
- existing location.) Next, the expressions in the body of the
- lambda expression will be evaluated
- sequentially in the extended environment. The results of
- the last expression in the body will be returned as the results
- of the procedure call.
- @example
- (lambda (x) (+ x x)) @result{} @emph{a procedure}
- ((lambda (x) (+ x x)) 4) @result{} 8
- (define reverse-subtract
- (lambda (x y) (- y x)))
- (reverse-subtract 7 10) @result{} 3
- (define add4
- (let ((x 4))
- (lambda (y) (+ x y))))
- (add4 6) @result{} 10
- @end example
- The formal arguments list of a lambda expression has some
- extensions over standard Scheme:
- Kawa borrows the extended formal argument list of DSSSL,
- and allows you to declare the type of the parameter.
- More generally, you can use @ref{Variables and Patterns,patterns}.
- @display
- @stxdef{lambda-expression} @stxlit{(lambda} @stxref{formals} @arbno{@stxref{option-pair}} @stxref{opt-return-type} @stxref{body}@stxlit{)}
- @stxdef{opt-return-type} [@stxlit{::} @stxref{type}]
- @stxdef{formals} @stxlit{(}@stxref{formal-arguments}@stxlit{)} | @stxref{rest-arg}
- @end display
- An @var{opt-return-type} specifies the return type of the procedure:
- The result of evaluating the @var{body} is coerced to the specified @var{type}.
- @emph{Deprecated}: If the first form of the function body is an unbound
- identifier of the form @code{<TYPE>} (that is the first character
- is @samp{<} and the last is @samp{>}), then that is another way to specify the
- function's return type.
- See @ref{Procedure properties,properties} for
- how to set and use an @var{option-pair}.
- The @ref{Definitions,@code{define}} form has a short-hand that combines
- a lambda definition with binding the lambda to a variable:
- @example
- @b{(define (}@var{name} @stxref{formal-arguments}@b{)} @stxref{opt-return-type} @stxref{body}@b{)}
- @end example
- @display
- @stxdef{formal-arguments} @arbno{@stxref{required-or-guard}} [@stxlit{#!optional} @stxref{optional-arg} ...] @stxref{rest-key-args}
- @stxdef{rest-key-args} [@stxlit{#!rest} @stxref{rest-arg}] [@stxlit{#!key} @stxref{key-arg} ...] [@stxref{guard}]
- | [@stxlit{#!key} @stxref{key-arg} ...] [@stxref{rest-parameter}] [@stxref{guard}]
- | @stxlit{.} @stxref{rest-arg}
- @end display
- When the procedure is applied to an
- @ref{argument list}, the latter is matched against formal parameters.
- This may involve some complex rules and pattern matching.
- @subsubheading Required parameters
- @display
- @stxdef{required-or-guard} @stxref{required-arg} | @stxref{guard}
- @stxdef{required-arg} @stxref{pattern}
- | @stxlit{(} @stxref{pattern} @stxlit{::} @stxref{type}@stxlit{)}
- @end display
- The @var{required-arg}s are matched against the
- actual (pre-keyword) arguments in order,
- starting with the first actual argument.
- It is an error if there are fewer pre-keyword
- arguments then there are @var{required-arg}s.
- While a @var{pattern} is most commonly an identifier,
- more complicated patterns are possible, thus more (or fewer)
- variables may be bound than there are arguments.
- Note a @stxref{pattern} may include an @stxref{opt-type-specifier}.
- For example:
- @example
- (define (isquare x::integer)
- (* x x))
- @end example
- In this case the actual argument is coerced to an @code{integer}
- and then the result matched against the pattern @code{x}.
- This is how parameter types are specified.
- The @var{pattern} may be enclosed in parentheses for clarify
- (just like for optional parameters), but in that case the
- type specifier is required to avoid ambiguity.
- @subsubheading Optional parameters
- @display
- @stxdef{optional-arg} @var{variable} @stxref{opt-type-specifier}
- | @stxlit{(} @stxref{pattern} @stxref{opt-type-specifier} [@var{initializer} [@stxref{supplied-var}]]@stxlit{)}
- @stxdef{supplied-var} @var{variable}
- @end display
- Next the @var{optional-arg}s are bound to remaining pre-keyword arguments.
- If there are fewer remaining pre-keyword arguments than there are
- @var{optional-arg}s, then the remaining @var{variable}s are bound
- to the corresponding @var{initializer}.
- If no @var{initializer} was specified, it defaults to @code{#f}.
- (TODO: If a @var{type} is specified the default for @var{initializer}
- is the default value of the @var{type}.)
- The @var{initializer} is evaluated in an
- environment in which all the previous formal parameters have been bound.
- If a @var{supplied-var} is specified, it has type boolean,
- and is set to true if there was an actual corresponding argument,
- and false if the initializer was evaluated.
- @subsubheading Keyword parameters
- @display
- @stxdef{key-arg} @var{variable} @stxref{opt-type-specifier}
- | @stxlit{(} @var{variable} @stxref{opt-type-specifier} [@var{initializer} [@stxref{supplied-var}]] @stxlit{)}
- @end display
- Keyword parameters follow @code{#!key}.
- For each @var{variable}
- if there is an actual keyword parameter whose keyword matches @var{variable},
- then @var{variable} is bound to the corresponding value.
- If there is no matching artual argument, then the @var{initializer} is
- evaluated and bound to the argument.
- If @var{initializer} is not specified, it defaults to @code{#f}.
- The @var{initializer} is evaluated in an
- environment in which all the previous formal parameters have been bound.
- @example
- (define (fun x #!key (foo 1) (bar 2) (baz 3))
- (list x foo bar baz))
- (fun 9 baz: 10 foo: 11) @result{} (9 11 2 10)
- @end example
- The following cause a match failure, @emph{unless} there is a rest parameter:
- @itemize
- @item
- There may not be extra non-keyword arguments (prefix or postfix)
- beyond those matched by required and optional parameters.
- @item
- There may not be any duplicated keyword arguments.
- @item
- All keyowrds in the actual argument list must
- match one of the keyword formal parameters.
- @end itemize
- It is not recommended to use both keyword parameters
- and a rest parameter that can match keyword arguments.
- Currently, the rest parameter will include any arguments
- that match the explicit keyword parameters, as well any that don't,
- though this may change.
- On the other hand, it is fine to have both keyword parameters
- and a rest parameter does not accept keywords. In that case
- the rest parameter will match any ``postfix'' arguments:
- @example
- #|kawa:8|# (define (fun x #!key k1 k2 #!rest r)
- (format "x:~w k1:~w k2:~w r:~w" x k1 k2 r))
- (fun 3 k2: 12 100 101) @result{} x:3 k1:#f k2:12 r:(100 101)
- @end example
- The @var{supplied-var} argument is as for optional arguments.
- @PerformanceNote{} Keyword parameters are implemented very
- efficiently and compactly when explicit in the code.
- The parameters are sorted by the compiler, and the
- actual keyword arguemnts at the call state are also sorted at compile-time.
- So keyword matching just requires a fast linear scan comparing
- the two sorted lists. This implementation is also very compact,
- compared to say a hash table.
- If a @var{type} is specified, the corresponding actual argument (or
- the @var{initializer} default value) is coerced to the specified @var{type}.
- In the function body, the parameter has the specified type.
- @subsubheading Rest parameters
- A ``rest parameter'' matches any arguments not matched by other parameters.
- You can write it using any of the following ways:
- @display
- @stxdef{rest-parameter}
- @stxlit{#!rest} @stxref{rest-arg} [@stxlit{::} @stxref{type}]
- | @stxlit{@@}@stxref{rest-arg}
- | @stxlit{@@:}@stxref{rest-arg}
- @stxdef{rest-arg} @var{variable}
- @end display
- In addition, if @var{formals} is just a @var{rest-arg} identifier,
- or a @var{formal-arguments} ends with @code{. @var{rest-arg}}
- (i.e. is a dotted list) that is equivalent to using @code{#!rest}.
- These forms are similar but differ in the type of the @var{rest-arg}
- and whether keywords are allowed (as part of the @var{rest-arg}):
- @itemize
- @item
- If @stxlit{#!rest} @var{rest-arg} is used with no @var{type} specifier
- (or a @var{type} specifier of @code{list})
- then @var{rest-arg} is a list.
- Keywords are not allowed if @code{#!key} has been seen.
- (For backward compatibility, it is allowed to have extra keywords
- if @code{#!rest} is followed by @code{!key}.)
- If there are any keywords, then @var{rest-arg} is more specifically
- an @code{arglist}.
- @item
- If @stxlit{#!rest} @var{rest-arg} is used with @var{type} specifier
- that is a Java array (for example @code{#!rest r::string[]}
- then @var{rest-arg} has that type. Each argument must be compatible
- with the element type of the array.
- Keywords are not allowed (even if @var{type} is @code{object[]}).
- The generated method will be compiled like a Java varargs
- methods if possible (i.e. no non-trivial patterns or keyword paremeters).
- @item
- Using @stxlit{@@}@var{rest-arg} is equivalent to
- @code{#!rest @var{rest-arg}::object[]}:
- Keywords are not allowed; the type of @var{rest-arg} is a Java array;
- the method is compiled like a Java varargs method.
- @item
- For @stxlit{@@:}@var{rest-arg} then @var{rest-arg} is a vector,
- specifically an @code{argvector}. Keywords are allowed.
- @end itemize
- @subsubheading Guards (conditional expressions)
- A @stxref{guard} is evaluated when it appears in the formal
- parameter list.
- If it evaluates to false, then matching fails.
- Guards can appears before or after required arguments,
- or at the very end, after all other formal parameters.
- @node Procedure properties
- @section Procedure properties
- You can associate arbitrary @dfn{properties} with any procedure.
- Each property is a (@var{key}, @var{value})-pair. Usually the
- @var{key} is a symbol, but it can be any object.
- The preferred way to set a property is using an @stxref{option-pair}
- in a @stxref{lambda-expression}.
- For example,
- to set the @code{setter} property of a procedure
- to @code{my-set-car} do the following:
- @example
- (define my-car
- (lambda (arg) setter: my-set-car (primitive-car arg)))
- @end example
- The system uses certain internal properties:
- @code{'name} refers to the name used when a procedure is printed;
- @code{'emacs-interactive} is used to implement Emacs @code{interactive}
- specification;
- @code{'setter} is used to associate a @code{setter} procedure.
- @deffn Procedure procedure-property proc key [default]
- Get the property value corresponding to the given @var{key}.
- If @var{proc} has no property with the given @var{key},
- return @var{default} (which defaults to @code{#f}) instead.
- @end deffn
- @deffn Procedure set-procedure-property! proc key value
- Associate the given @var{value} with the @var{key} property of @var{proc}.
- @end deffn
- To change the print name of the standard @code{+} procedure (probably
- not a good idea!), you could do:
- @example
- (set-procedure-property! + 'name 'PLUS)
- @end example
- Note this @emph{only} changes the name property used for printing:
- @example
- + @result{} #<procedure PLUS>
- (+ 2 3) @result{} 5
- (PLUS 3 4) @result{} ERROR
- @end example
- As a matter of style, it is cleaner to use the @code{define-procedure}
- form, as it is a more declarative interface.
- @deffn Syntax define-procedure name [propname: propvalue] ... method ...
- Defines @var{name} as a compound procedure consisting of the
- specified @var{method}s, with the associated properties.
- Applying @var{name} select the "best" @var{method}, and applies that.
- See the following section on generic procedures.
- For example, the standard @code{vector-ref} procedure specifies
- one method, as well as the @code{setter} property:
- @example
- (define-procedure vector-ref
- setter: vector-set!
- (lambda (vector::vector k ::int)
- (invoke vector 'get k)))
- @end example
- @end deffn
- You can also specify properties in the lambda body:
- @example
- (define (vector-ref vector::vector k ::int)
- setter: vector-set!
- (invoke vector 'get k))
- @end example
- @subsection Standard properties
- @table @code
- @item name
- The name of a procedure (as a symbol), which is used
- when the procedure is printed.
- @item setter
- Set the setter procedure associated with the procedure.
- @item validate-apply
- @itemx validate-xapply
- Used during the validation phase of the compiler.
- @item compile-apply
- Used during the bytecode-generation phase of the compiler:
- If we see a call to a known function with this property,
- we can emit custom bytecode for the call.
- @end table
- @node Generic procedures
- @section Generic (dynamically overloaded) procedures
- A @dfn{generic procedure} is a collection of @dfn{method procedures}.
- (A "method procedure" is not the same as a Java method, but
- the terms are related.)
- You can call a generic procedure, which selects the "closest
- match" among the component method procedures: I.e. the most specific
- method procedure that is applicable given the actual arguments.
- @quotation
- @strong{Warning:} The current implementation of selecting the "best" method
- is not reliable if there is more than one method.
- It can select depending on argument count, and it can select between
- primitive Java methods. However, selecting between different Scheme
- procedures based on parameter types should be considered experimental.
- The main problem is we can't determine the most specific
- method, so Kawa just tries the methods in order.
- @end quotation
- @deffn Procedure make-procedure [keyword: value]... method...
- Create a generic procedure given the specific methods.
- You can also specify property values for the result.
- The @var{keyword}s specify how the arguments are used.
- A @code{method:} keyword is optional and specifies that the following
- argument is a method.
- A @code{name:} keyword specifies the name of the resulting procedure,
- when used for printing.
- Unrecognized keywords are used to set the procedure properties of the result.
- @example
- (define plus10 (make-procedure foo: 33 name: 'Plus10
- method: (lambda (x y) (+ x y 10))
- method: (lambda () 10)))
- @end example
- @end deffn
- @node Partial application
- @section Partial application
- @deffn Syntax cut slot-or-expr slot-or-expr* [@code{<...>}]
- where each @var{slot-or-expr} is either an @var{expression} or
- the literal symbol @code{<>}.
- It is frequently necessary to specialize some of the parameters of a
- multi-parameter procedure. For example, from the binary operation @code{cons}
- one might want to obtain the unary operation
- @code{(lambda (x) (cons 1 x))}.
- This specialization of parameters is also known
- as @dfn{partial application}, @dfn{operator section}, or @dfn{projection}.
- The macro @code{cut} specializes some of the parameters of its first
- argument. The parameters that are to show up as formal variables of the
- result are indicated by the symbol @code{<>}, pronouced as "slot".
- In addition, the symbol @code{<...>}, pronounced as "rest-slot", matches all
- residual arguments of a variable argument procedure.
- A @code{cut}-expression is transformed into
- a @var{lambda expression} with as many formal variables as there are
- slots in the list @var{slot-or-expr}*.
- The body of the resulting @var{lambda expression} calls
- the first @var{slot-or-expr} with arguments from the @var{slot-or-expr}* list
- in the order they appear. In case there is a rest-slot symbol, the resulting
- procedure is also of variable arity, and the body calls the first
- @var{slot-or-expr} with remaining arguments provided to the actual call of the
- specialized procedure.
- Here are some examples:
- @code{(cut cons (+ a 1) <>)} is the same as @w{@code{(lambda (x2) (cons (+ a 1) x2))}}
- @code{(cut list 1 <> 3 <> 5)} is the same as @w{@code{(lambda (x2 x4) (list 1 x2 3 x4 5))}}
- @code{(cut list)} is the same as @w{@code{(lambda () (list))}}
- @code{(cut list 1 <> 3 <...>)} is the same as @w{@code{(lambda (x2 . xs) (apply list 1 x2 3 xs))}}
- The first argument can also be a slot, as one should expect in Scheme:
- @code{(cut <> a b)} is the same as @w{@code{(lambda (f) (f a b))}}
- @end deffn
- @deffn Syntax cute slot-or-expr slot-or-expr* [@code{<...>}]
- The macro @code{cute} (a mnemonic for "cut with evaluated non-slots") is
- similar to @code{cut}, but it evaluates the non-slot expressions at the
- time the procedure is specialized, not at the time the specialized
- procedure is called.
- For example
- @code{(cute cons (+ a 1) <>)} is the same as
- @w{@code{(let ((a1 (+ a 1))) (lambda (x2) (cons a1 x2)))}}
- As you see from comparing this example with the first example above, the
- @code{cute}-variant will evaluate @code{(+ a 1)} once,
- while the @code{cut}-variant will
- evaluate it during every invocation of the resulting procedure.
- @end deffn
- @node Numbers, Characters and text, Procedures, Top
- @chapter Quantities and Numbers
- Kawa supports the full Scheme set of number operations with some extensions.
- Kawa converts between Scheme number types
- and Java number types as appropriate.
- @menu
- * Numerical types::
- * Arithmetic operations::
- * Numerical input and output::
- * Quaternions::
- * Quantities::
- * Logical Number Operations::
- * Performance of numeric operations::
- @end menu
- @node Numerical types
- @section Numerical types
- Mathematically, numbers are arranged into a tower of subtypes
- in which each level is a subset of the level before it:
- number; complex number; real number; rational number; integer.
- For example, @code{3} is an integer. Therefore @code{3} is also a rational,
- a real, and a complex number. The same is true of the
- Scheme numbers that model 3. For Scheme numbers, these
- types are defined by the predicates @code{number?}, @code{complex?},
- @code{real?}, @code{rational?}, and @code{integer?}.
- There is no simple relationship between a number’s type
- and its representation inside a computer. Although most
- implementations of Scheme will offer at least two different
- representations of 3, these different representations denote
- the same integer.
- Scheme’s numerical operations treat numbers as abstract
- data, as independent of their representation as possible.
- Although an implementation of Scheme may use multiple
- internal representations of numbers, this ought not to be
- apparent to a casual programmer writing simple programs.
- @deffn Type number
- The type of Scheme numbers.
- @end deffn
- @deffn Type quantity
- The type of quantities optionally with units.
- This is a sub-type of @code{number}.
- @end deffn
- @deffn Type complex
- The type of complex numbers.
- This is a sub-type of @code{quantity}.
- @end deffn
- @deffn Type real
- The type of real numbers.
- This is a sub-type of @code{complex}.
- @end deffn
- @deffn Type rational
- The type of exact rational numbers.
- This is a sub-type of @code{real}.
- @end deffn
- @deffn Type integer
- The type of exact Scheme integers.
- This is a sub-type of @code{rational}.
- @end deffn
- Kawa allows working with expressions of ``primitive'' types,
- which are supported by the JVM without object allocation,
- and using builtin arithmetic. Using these types may be much
- faster, assuming the compiler is able to infer
- that the variable or expression has primitive type.
- @deffn Type long
- @deffnx Type int
- @deffnx Type short
- @deffnx Type byte
- These are fixed-sized primitive signed exact integer types,
- of respectively 64, 32, 18, and 8 bits.
- If a value of one of these types needs to be converted to an
- object, the standard classes @code{java.lang.Long}, @code{java.lang.Integer},
- @code{java.lang.Short}, or @code{java.lang.Byte}, respectively, are used.
- @end deffn
- @deffn Type ulong
- @deffnx Type uint
- @deffnx Type ushort
- @deffnx Type ubyte
- These are fixed-sized primitive unsigned exact integer types,
- of respectively 64, 32, 18, and 8 bits.
- These are presented at runtime using the corresponding
- signed types (@code{long}, @code{int}, @code{short}, or @code{byte}).
- However, for arithmetic the Kawa compiler generates code to perform the
- ``mathematically correct'' result, truncated to an unsigned result
- rather than signed.
- If a value of one of these types needs to be converted to an
- object, the classes @code{gnu.math.ULong}, @code{gnu.math.UInt},
- @code{gnu.math.UShort}, or @code{gnu.math.UByte} is used.
- @end deffn
- @deffn Type double
- @deffnx Type float
- These are fixed-size primitive inexact floating-point real types,
- using the standard 64-bit or 32-bit IEEE representation.
- If a value of one of these types needs to be converted to an
- object, the standard classes @code{java.lang.Double},
- or @code{java.lang.Float} is used.
- @end deffn
- @subsection Exactness
- @cindex exactness
- It is useful to distinguish between numbers that are represented
- exactly and those that might not be. For example,
- indexes into data structures must be known exactly, as
- must some polynomial coefficients in a symbolic algebra
- system. On the other hand, the results of measurements
- are inherently inexact, and irrational numbers may be approximated
- by rational and therefore inexact approximations.
- In order to catch uses of inexact numbers where exact numbers
- are required, Scheme explicitly distinguishes
- exact from inexact numbers. This distinction is orthogonal
- to the dimension of type.
- @cindex exact complex number
- @cindex inexact complex number
- A Scheme number is @dfn{exact} if it was written as an exact
- constant or was derived from exact numbers using only exact operations.
- A number is @dfn{inexact} if it was written as
- an inexact constant, if it was derived using inexact ingredients,
- or if it was derived using inexact operations. Thus
- inexactness is a contagious property of a number.
- In particular, an @dfn{exact complex number} has an exact real part
- and an exact imaginary part; all other complex numbers
- are @dfn{inexact complex numbers}.
- If two implementations produce exact results for a computation
- that did not involve inexact intermediate results, the
- two ultimate results will be mathematically equal. This
- is generally not true of computations involving inexact
- numbers since approximate methods such as floating-point
- arithmetic may be used, but it is the duty of the implementation
- to make the result as close as practical to the
- mathematically ideal result.
- Rational operations such as @code{+} should always produce exact
- results when given exact arguments. If the operation
- is unable to produce an exact result, then it may either
- report the violation of an implementation restriction or it
- may silently coerce its result to an inexact value.
- Except for @code{exact}, the operations described in this section
- must generally return inexact results when given any inexact arguments.
- An operation may, however, return an
- exact result if it can prove that the value of the result is
- unaffected by the inexactness of its arguments. For example,
- multiplication of any number by an exact zero may
- produce an exact zero result, even if the other argument is inexact.
- Specifically, the expression @code{(* 0 +inf.0)} may return @code{0},
- or @code{+nan.0}, or report that inexact numbers are not supported,
- or report that non-rational real numbers are not supported,
- or fail silently or noisily in other implementation-specific ways.
- The procedures listed below will always return exact integer
- results provided all their arguments are exact integers
- and the mathematically expected results are representable
- as exact integers within the implementation:
- @code{-},
- @code{*},
- @code{+},
- @code{abs},
- @code{ceiling},
- @code{denominator},
- @code{exact-integer-sqrt},
- @code{expt},
- @code{floor},
- @code{floor/},
- @code{floor-quotient},
- @code{floor-remainder},
- @code{gcd},
- @code{lcm},
- @code{max},
- @code{min},
- @code{modulo},
- @code{numerator},
- @code{quotient},
- @code{rationalize},
- @code{remainder},
- @code{square},
- @code{truncate},
- @code{truncate/},
- @code{truncate-quotient},
- @code{truncate-remainder}.
- @subsection Numerical promotion and conversion
- When combining two values of different numeric types,
- the values are converted to the first line in the following
- that subsumes (follows) both types. The computation is done using
- values of that type, and so is the result.
- For example adding a @code{long} and a @code{float} converts the former
- to the latter, yielding a @code{float}.
- Note that @code{short}, @code{byte}, @code{ushort}, @code{ubyte}
- are converted to @code{int} regardless, even in the case of
- a single-operand operation, such as unary negation.
- Another exception is trancendental functions (such as @code{cos}),
- where integer operands are converted to @code{double}.
- @itemize
- @item
- @code{int} subsumes @code{short}, @code{byte}, @code{ushort}, @code{ubyte}.
- @item
- @code{uint}
- @item
- @code{long}
- @item
- @code{ulong}
- @item
- @code{java.lang.BigInteger}
- @item
- @code{integer} (i.e. @code{gnu.math.IntNum})
- @item
- @code{rational} (i.e. @code{gnu.math.RatNum})
- @item
- @code{float}
- @item
- @code{double}
- @item
- @code{gnu.math.FloNum}
- @item
- @code{real} (i.e. @code{gnu.math.RealNum})
- @item
- @code{number}
- @item
- @code{complex}
- @item
- @code{quantity}
- @end itemize
- When comparing a primitive signed integer value with a primitive unsigned
- integer (for example @code{<} applied to a @code{int} and a @code{ulong})
- the mathemically correct result is computed, as it converting both
- operands to @code{integer}.
- @node Arithmetic operations
- @section Arithmetic operations
- @deffn Procedure {real-valued?} @var{obj}
- @deffnx Procedure {rational-valued?} @var{obj}
- @deffnx Procedure {integer-valued?} @var{obj}
- These numerical type predicates can be applied to any kind of argument.
- The @func{real-valued?} procedure returns @true{} if the object is a
- number object and is equal in the sense of @code{=} to some real number
- object, or if the object is a NaN, or a complex number object whose real
- part is a NaN and whose imaginary part is zero in the sense of
- @func{zero?}. The @func{rational-valued?} and @func{integer-valued?}
- procedures return @true{} if the object is a number object and is equal
- in the sense of @code{=} to some object of the named type, and otherwise
- they return @false{}.
- @example
- (real-valued? +nan.0) @result{} #t
- (real-valued? +nan.0+0i) @result{} #t
- (real-valued? -inf.0) @result{} #t
- (real-valued? 3) @result{} #t
- (real-valued? -2.5+0.0i) @result{} #t
- (real-valued? -2.5+0i) @result{} #t
- (real-valued? -2.5) @result{} #t
- (real-valued? #e1e10) @result{} #t
- (rational-valued? +nan.0) @result{} #f
- (rational-valued? -inf.0) @result{} #f
- (rational-valued? 6/10) @result{} #t
- (rational-valued? 6/10+0.0i) @result{} #t
- (rational-valued? 6/10+0i) @result{} #t
- (rational-valued? 6/3) @result{} #t
- (integer-valued? 3+0i) @result{} #t
- (integer-valued? 3+0.0i) @result{} #t
- (integer-valued? 3.0) @result{} #t
- (integer-valued? 3.0+0.0i) @result{} #t
- (integer-valued? 8/4) @result{} #t
- @end example
- @quotation
- @emph{Note:} These procedures test whether a given number object can be
- coerced to the specified type without loss of numerical accuracy.
- Specifically, the behavior of these predicates differs from the behavior
- of @func{real?}, @func{rational?}, and @func{integer?} on complex number
- objects whose imaginary part is inexact zero.
- @end quotation
- @quotation
- @emph{Note:} The behavior of these type predicates on inexact number
- objects is unreliable, because any inaccuracy may affect the result.
- @end quotation
- @end deffn
- @deffn Procedure exact-integer? z
- Returns @code{#t} if @var{z} is both exact and an integer; otherwise
- returns @code{#f}.
- @example
- (exact-integer? 32) @result{} #t
- (exact-integer? 32.0) @result{} #t
- (exact-integer? 32/5) @result{} #f
- @end example
- @end deffn
- @deffn Procedure {finite?} @var{z}
- Returns @code{#t} if @var{z} is finite real number
- (i.e. an infinity and not a NaN),
- or if @var{z} is a complex number
- whose real and imaginary parts are both finite.
- @example
- (finite? 3) @result{} #t
- (finite? +inf.0) @result{} #f
- (finite? 3.0+inf.0i) @result{} #f
- @end example
- @end deffn
- @deffn Procedure {infinite?} @var{z}
- Return @code{#t} if @var{z} is
- an infinite real number (@code{+int.0} or @code{-inf.0}),
- or if @var{z} is a complex number where either
- real or imaginary parts or both are infinite.
- @example
- (infinite? 5.0) @result{} #f
- (infinite? +inf.0) @result{} #t
- (infinite? +nan.0) @result{} #f
- (infinite? 3.0+inf.0i) @result{} #t
- @end example
- @end deffn
- @deffn Procedure {nan?} @var{z}
- For a real numer returns whether its is a NaN;
- for a complex number if the real or imaginary parts or both is a NaN.
- @example
- (nan? +nan.0) @result{} #t
- (nan? 32) @result{} #f
- (nan? +nan.0+5.0i) @result{} #t
- (nan? 1+2i) @result{} #f
- @end example
- @end deffn
- @deffn Procedure + @var{z} @dots{}
- @deffnx Procedure * @var{z} @dots{}
- These procedures return the sum or product of their arguments.
- @example
- (+ 3 4) @result{} 7
- (+ 3) @result{} 3
- (+) @result{} 0
- (+ +inf.0 +inf.0) @result{} +inf.0
- (+ +inf.0 -inf.0) @result{} +nan.0
- (* 4) @result{} 4
- (*) @result{} 1
- (* 5 +inf.0) @result{} +inf.0
- (* -5 +inf.0) @result{} -inf.0
- (* +inf.0 +inf.0) @result{} +inf.0
- (* +inf.0 -inf.0) @result{} -inf.0
- (* 0 +inf.0) @result{} +nan.0
- (* 0 +nan.0) @result{} +nan.0
- (* 1.0 0) @result{} 0.0
- @end example
- For any real number object @var{x} that is neither infinite nor NaN:
- @example
- (+ +inf.0 @var{x}) @result{} +inf.0
- (+ -inf.0 @var{x}) @result{} -inf.0
- @end example
- For any real number object @var{x}:
- @example
- (+ +nan.0 @var{x}) @result{} +nan.0
- @end example
- For any real number object @var{x} that is not an exact 0:
- @example
- (* +nan.0 @var{x}) @result{} +nan.0
- @end example
- @c If any of these procedures are applied to mixed non--rational real and
- @c non--real complex arguments, they either raise an exception with
- @c condition type @code{&implementation-restriction} or return an
- @c unspecified number object.
- The behavior of @code{-0.0} is illustrated by the following examples:
- @example
- (+ 0.0 -0.0) @result{} 0.0
- (+ -0.0 0.0) @result{} 0.0
- (+ 0.0 0.0) @result{} 0.0
- (+ -0.0 -0.0) @result{} -0.0
- @end example
- @end deffn
- @deffn Procedure - @var{z}
- @deffnx Procedure - @vari{z} @varii{z} @variii{z} @dots{}
- With two or more arguments, this procedures returns the difference of
- its arguments, associating to the left. With one argument, however, it
- returns the negation (additive inverse) of its argument.
- @example
- (- 3 4) @result{} -1
- (- 3 4 5) @result{} -6
- (- 3) @result{} -3
- (- +inf.0 +inf.0) @result{} +nan.0
- @end example
- @c If this procedure is applied to mixed non--rational real and non--real
- @c complex arguments, it either raises an exception with condition type
- @c @code{&implementation-restriction} or returns an unspecified number
- @c object.
- The behavior of @code{-0.0} is illustrated by the following examples:
- @example
- (- 0.0) @result{} -0.0
- (- -0.0) @result{} 0.0
- (- 0.0 -0.0) @result{} 0.0
- (- -0.0 0.0) @result{} -0.0
- (- 0.0 0.0) @result{} 0.0
- (- -0.0 -0.0) @result{} 0.0
- @end example
- @end deffn
- @deffn Procedure / @var{z}
- @deffnx Procedure / @vari{z} @varii{z} @variii{z} @dots{}
- If all of the arguments are exact, then the divisors must all be
- nonzero. With two or more arguments, this procedure returns the
- quotient of its arguments, associating to the left. With one argument,
- however, it returns the multiplicative inverse of its argument.
- @example
- (/ 3 4 5) @result{} 3/20
- (/ 3) @result{} 1/3
- (/ 0.0) @result{} +inf.0
- (/ 1.0 0) @result{} +inf.0
- (/ -1 0.0) @result{} -inf.0
- (/ +inf.0) @result{} 0.0
- (/ 0 0) @result{} exception &assertion
- (/ 3 0) @result{} exception &assertion
- (/ 0 3.5) @result{} 0.0
- (/ 0 0.0) @result{} +nan.0
- (/ 0.0 0) @result{} +nan.0
- (/ 0.0 0.0) @result{} +nan.0
- @end example
- If this procedure is applied to mixed non--rational real and non--real
- complex arguments, it either raises an exception with condition type
- @code{&implementation-restriction} or returns an unspecified number
- object.
- @end deffn
- @deffn Procedure floor/ x y
- @deffnx Procedure truncate/ x y
- @deffnx Procedure div-and-mod x y
- @deffnx Procedure div0-and-mod0 x y
- These procedures implement number--theoretic integer division.
- They accept two real numbers @var{x} and @var{y} as operands,
- where @var{y} must be nonzero.
- In all cases the result is two values @var{q} (an integer) and @var{r} (a real)
- that satisfy the equations:
- @example
- @var{x} = @var{q} * @var{y} + @var{r}
- @var{q} = @var{rounding-op}(@var{x}/@var{y})
- @end example
- The result is inexact if either argument is inexact.
- For @code{floor/} the @var{rounding-op} is the @code{floor} function (below).
- @example
- (floor/ 123 10) @result{} 12 3
- (floor/ 123 -10) @result{} -13 -7
- (floor/ -123 10) @result{} -13 7
- (floor/ -123 -10) @result{} 12 -3
- @end example
- For @code{truncate/} the @var{rounding-op} is the @code{truncate} function.
- @example
- (truncate/ 123 10) @result{} 12 3
- (truncate/ 123 -10) @result{} -12 3
- (truncate/ -123 10) @result{} -12 -3
- (truncate/ -123 -10) @result{} 12 -3
- @end example
- For @code{div-and-mod} the @var{rounding-op} is either @code{floor}
- (if @var{y} is positive) or @code{ceiling} (if @var{y} is negative).
- We have:
- @example
- 0 <= @var{r} < |@var{y}|
- @end example
- @example
- (div-and-mod 123 10) @result{} 12 3
- (div-and-mod 123 -10) @result{} -12 3
- (div-and-mod -123 10) @result{} -13 7
- (div-and-mod -123 -10) @result{} 13 7
- @end example
- For @code{div0-and-mod0} the @var{rounding-op} is the @code{round} function,
- and @code{r} lies within a half--open interval centered on zero.
- @example
- -|@var{y}/2| <= @var{r} < |@var{y}/2|
- @end example
- @example
- (div0-and-mod0 123 10) @result{} 12 3
- (div0-and-mod0 123 -10) @result{} -12 3
- (div0-and-mod0 -123 10) @result{} -12 -3
- (div0-and-mod0 -123 -10) @result{} 12 -3
- (div0-and-mod0 127 10) @result{} 13 -3
- (div0-and-mod0 127 -10) @result{} -13 -3
- (div0-and-mod0 -127 10) @result{} -13 3
- (div0-and-mod0 -127 -10) @result{} 13 3
- @end example
- The inconsistent naming is for historical reasons: @code{div-and-mod}
- and @code{div0-and-mod0} are from R6RS, while @code{floor/} and
- @code{truncate/} are from R7RS.
- @end deffn
- @deffn Procedure floor-quotient x y
- @deffnx Procedure truncate-quotient x y
- @deffnx Procedure div x y
- @deffnx Procedure div0 x y
- These procedures return the quotient part (first value)
- of respectively @code{floor/}, @code{truncate/},
- @code{div-and-mod}, and @code{div0-and-mod0}.
- @end deffn
- @deffn Procedure floor-remainder x y
- @deffnx Procedure truncate-remainder x y
- @deffnx Procedure mod x y
- @deffnx Procedure mod0 x y
- These procedures return the remainder part (second value)
- of respectively @code{floor/}, @code{truncate/},
- @code{div-and-mod}, and @code{div0-and-mod0}.
- As a Kawa extension @var{y} may be zero, in which case the result is @var{x}:
- @example
- (mod 123 0) @result{} 123 ;; Kawa extension
- @end example
- @end deffn
- @deffn Procedure quotient x y
- @deffnx Procedure remainder x y
- @deffnx Procedure modulo x y
- These are equivalent to @code{truncate-quotient},
- @code{truncate-remainder}, and @code{floor-remainder}, respectively.
- These are provided for backward compatibility.
- @example
- (remainder 13 4) @result{} 1
- (remainder -13 4) @result{} -1
- (remainder 13 -4) @result{} 1
- (remainder -13 -4) @result{} -1
- (remainder -13 -4.0) @result{} -1.0
- (modulo 13 4) @result{} 1
- (modulo -13 4) @result{} 3
- (modulo 13 -4) @result{} -4
- (modulo -13 -4) @result{} -1
- @end example
- @end deffn
- @deffn Procedure abs @var{x}
- Returns the absolute value of its argument.
- @example
- (abs -7) @result{} 7
- (abs -inf.0) @result{} +inf.0
- @end example
- @end deffn
- @deffn Procedure gcd @vari{n} @dots{}
- @deffnx Procedure lcm @vari{n} @dots{}
- These procedures return the greatest common divisor or least common
- multiple of their arguments. The result is always non--negative.
- The arguments must be integers; if an argument is inexact, so is the result.
- @example
- (gcd 32 -36) @result{} 4
- (gcd) @result{} 0
- (lcm 32 -36) @result{} 288
- (lcm 32.0 -36) @result{} 288.0 ; inexact
- (lcm) @result{} 1
- @end example
- @end deffn
- @deffn Procedure numerator @var{q}
- @deffnx Procedure denominator @var{q}
- These procedures return the numerator or denominator of their argument;
- the result is computed as if the argument was represented as a fraction
- in lowest terms. The denominator is always positive. The denominator
- of @code{0} is defined to be @code{1}.
- The arguments must be integers; if an argument is inexact, so is the result.
- @example
- (numerator (/ 6 4)) @result{} 3
- (denominator (/ 6 4)) @result{} 2
- (denominator (inexact (/ 6 4))) @result{} 2.0
- @end example
- @end deffn
- @deffn Procedure floor @var{x}
- @deffnx Procedure ceiling @var{x}
- @deffnx Procedure truncate @var{x}
- @deffnx Procedure round @var{x}
- These procedures return inexact integer objects for inexact arguments
- that are not infinities or NaNs, and exact integer objects for exact
- rational arguments.
- @table @code
- @item floor
- Returns the largest integer object not larger than @var{x}.
- @item ceiling
- Returns the smallest integer object not smaller than @var{x}.
- @item truncate
- Returns the integer object closest to @var{x} whose absolute value is
- not larger than the absolute value of @var{x}.
- @item round
- Returns the closest integer object to @var{x}, rounding to even when
- @var{x} represents a number halfway between two integers.
- @end table
- If the argument to one of these procedures is inexact, then the result
- is also inexact. If an exact value is needed, the result should be
- passed to the @func{exact} procedure.
- Although infinities and NaNs are not integer objects, these procedures
- return an infinity when given an infinity as an argument, and a NaN when
- given a NaN.
- @example
- (floor -4.3) @result{} -5.0
- (ceiling -4.3) @result{} -4.0
- (truncate -4.3) @result{} -4.0
- (round -4.3) @result{} -4.0
- (floor 3.5) @result{} 3.0
- (ceiling 3.5) @result{} 4.0
- (truncate 3.5) @result{} 3.0
- (round 3.5) @result{} 4.0
- (round 7/2) @result{} 4
- (round 7) @result{} 7
- (floor +inf.0) @result{} +inf.0
- (ceiling -inf.0) @result{} -inf.0
- (round +nan.0) @result{} +nan.0
- @end example
- @end deffn
- @deffn Procedure rationalize @vari{x} @varii{x}
- The @func{rationalize} procedure returns a number object representing
- the @emph{simplest} rational number differing from @vari{x} by no more
- than @varii{x}.
- A rational number @emph{r_1} is @emph{simpler} than another rational
- number @emph{r_2} if @code{r_1 = p_1/q_1} and @code{r_2 = p_2/q_2} (in
- lowest terms) and @code{|p_1| <= |p_2|} and @code{|q_1| <= |q_2|}. Thus
- @code{3/5} is simpler than @code{4/7}.
- Although not all rationals are comparable in this ordering (consider
- @code{2/7} and @code{3/5}) any interval contains a rational number that
- is simpler than every other rational number in that interval (the
- simpler @code{2/5} lies between @code{2/7} and @code{3/5}).
- Note that @code{0 = 0/1} is the simplest rational of all.
- @example
- (rationalize (exact .3) 1/10) @result{} 1/3
- (rationalize .3 1/10) @result{} #i1/3 ; approximately
- (rationalize +inf.0 3) @result{} +inf.0
- (rationalize +inf.0 +inf.0) @result{} +nan.0
- @c (rationalize 3 +inf.0) @result{} 0.0
- @end example
- The first two examples hold only in implementations whose inexact real
- number objects have sufficient precision.
- @end deffn
- @deffn Procedure exp @var{z}
- @deffnx Procedure log @var{z}
- @deffnx Procedure log @vari{z} @varii{z}
- @deffnx Procedure sin @var{z}
- @deffnx Procedure cos @var{z}
- @deffnx Procedure tan @var{z}
- @deffnx Procedure asin @var{z}
- @deffnx Procedure acos @var{z}
- @deffnx Procedure atan @var{z}
- @deffnx Procedure atan @vari{x} @varii{x}
- These procedures compute the usual transcendental functions.
- The @func{exp} procedure computes the base--@var{e} exponential of
- @var{z}.
- The @func{log} procedure with a single argument computes the natural
- logarithm of @var{z} (@strong{not} the base--10 logarithm); @code{(log
- @vari{z} @varii{z})} computes the base--@varii{z} logarithm of @vari{z}.
- The @func{asin}, @func{acos}, and @func{atan} procedures compute
- arcsine, arccosine, and arctangent, respectively. The two--argument
- variant of @func{atan} computes:
- @example
- (angle (make-rectangular @varii{x} @vari{x}))
- @end example
- These procedures may return inexact results even when given exact
- arguments.
- @example
- (exp +inf.0) @result{} +inf.0
- (exp -inf.0) @result{} 0.0
- (log +inf.0) @result{} +inf.0
- (log 0.0) @result{} -inf.0
- (log 0) @result{} exception &assertion
- (log -inf.0) @result{} +inf.0+3.141592653589793i ; approximately
- (atan -inf.0) @result{} -1.5707963267948965 ; approximately
- (atan +inf.0) @result{} 1.5707963267948965 ; approximately
- (log -1.0+0.0i) @result{} 0.0+3.141592653589793i ; approximately
- (log -1.0-0.0i) @result{} 0.0-3.141592653589793i ; approximately
- ; if -0.0 is distinguished
- @end example
- @end deffn
- @deffn Procedure sinh z
- @deffnx Procedure cosh z
- @deffnx Procedure tanh z
- @deffnx Procedure asinh z
- @deffnx Procedure acosh z
- @deffnx Procedure atanh z
- The hyperbolic functions.
- @end deffn
- @deffn Procedure square z
- Returns the square of @var{z}.
- This is equivalent to @code{(* @var{z} @var{z})}.
- @example
- (square 42) @result{} 1764
- (square 2.0) @result{} 4.0
- @end example
- @end deffn
- @deffn Procedure sqrt @var{z}
- Returns the principal square root of @var{z}. For rational @var{z}, the
- result has either positive real part, or zero real part and
- non--negative imaginary part. The value of @code{(sqrt @var{z})} could be
- expressed as:
- @example
- e^((log z)/2)
- @end example
- The @func{sqrt} procedure may return an inexact result even when given
- an exact argument.
- @example
- (sqrt -5) @result{} 0.0+2.23606797749979i ; approximately
- (sqrt +inf.0) @result{} +inf.0
- (sqrt -inf.0) @result{} +inf.0i
- @end example
- Note that if the argument is a primitive number (such as @code{double}) or an
- instance of the corresponding boxed class (such as @code{java.lang.Double})
- then we use the real-number version of @code{sqrt}:
- @example
- (sqrt (->double -5)) @result{} NaN
- @end example
- That is, we get different a result for @code{java.lang.Double}
- and @code{gnu.math.DFloNum}, even for arguments that are numerically equal
- in the sense of @code{=}.
- This is so that the compiler can use the @code{java.lang.Math.sqrt}
- method without object allocation when the argument is a @code{double}
- (and because we want @code{double} and @code{java.lang.Double} to behave
- consistently).
- @end deffn
- @deffn Procedure exact-integer-sqrt @var{k}
- The @func{exact-integer-sqrt} procedure returns two non--negative exact
- integer objects @emph{s} and @emph{r} where @code{@var{k} = s^2 + r} and
- @code{@var{k} < (s+1)^2}.
- @example
- (exact-integer-sqrt 4) @result{} 2 0 ; two return values
- (exact-integer-sqrt 5) @result{} 2 1 ; two return values
- @end example
- @end deffn
- @deffn Procedure expt @vari{z} @varii{z}
- Returns @vari{z} raised to the power @varii{z}.
- For nonzero @vari{z}, this is
- @vari{z}@sup{@varii{z}} = @var{e}@sup{@varii{z} log @vari{z}}.
- The value of 0@sup{@var{z}} is 1 if @code{(zero? @var{z})},
- 0 if @code{(real-part @var{z})} is positive, and an error otherwise.
- Similarly for 0.0@sup{z}, with inexact results.
- @end deffn
- @node Numerical input and output
- @section Numerical input and output
- @deffn Procedure number->string z [radix]
- The procedure @code{number->string} takes a number and a
- radix and returns as a string an external representation
- of the given number in the given radix such that
- @example
- (let ((number number)
- (radix radix))
- (eqv? number
- (string->number (number->string number radix)
- radix)))
- @end example
- is true. It is an error if no possible result makes this expression true.
- If present, @var{radix} must be an exact integer
- in the range 2 to 36, inclusive.
- If omitted, @var{radix} defaults to 10.
- If @var{z} is inexact, the @var{radix} is 10, and the above expression
- can be satisfied by a result that contains a decimal point,
- then the result contains a decimal point and is expressed
- using the minimum number of digits (exclusive of exponent
- and trailing zeroes) needed to make the above expression;
- otherwise the format of the result is unspecified.
- The result returned by @code{number->string} never contains an
- explicit radix prefix.
- @emph{Note:}
- The error case can occur only when @var{z} is not a complex
- number or is a complex number with a non-rational real or
- imaginary part.
- @emph{Rationale:} If @var{z} is an inexact number and the @var{radix} is 10,
- then the above expression is normally satisfied by a result containing
- a decimal point. The unspecified case allows for infinities, NaNs,
- and unusual representations.
- @end deffn
- @deffn Procedure string->number string [radix]
- Returns a number of the maximally precise representation
- expressed by the given @var{string}. It is an error if @var{radix} is not
- an exact integer in the range 2 to 26, inclusive.
- If supplied, @var{radix} is a default radix that will be overridden
- if an explicit radix prefix is present in the string (e.g.
- @code{"#o177"}). If @var{radix} is not supplied, then the default @var{radix}
- is 10. If @var{string} is not a syntactically valid notation for a
- number, or would result in a number that the implementation cannot represent,
- then @code{string->number} returns @code{#f}.
- An error is never signaled due to the content of @var{string}.
- @example
- (string->number "100") @result{} 100
- (string->number "100" 16) @result{} 256
- (string->number "1e2") @result{} 100.0
- (string->number "#x100" 10) @result{} 256
- @end example
- @end deffn
- @node Quaternions
- @section Quaternions
- Kawa extends the Scheme numeric tower to include
- @uref{http://en.wikipedia.org/wiki/Quaternion,quaternions} as a proper
- superset of the complex numbers. Quaternions provide a convenient
- notation to represent
- @uref{http://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation,rotations in three-dimensional space},
- and are therefore commonly found in applications such as computer
- graphics, robotics, and spacecraft engineering. The Kawa quaternion API
- is modeled after
- @uref{http://www.ccs.neu.edu/home/dorai/squat/squat.html,this} with some
- additions.
- A quaternion is a number that can be expressed in the form
- @samp{w+xi+yj+zk}, where @code{w}, @code{x}, @code{y}, and @code{z} are
- real, and @code{i}, @code{j}, and @code{k} are imaginary units
- satisfying @math{i@sup{2} = j@sup{2} = k@sup{2} = ijk = -1}. The magnitude of a
- quaternion is defined to be its Euclidean norm when viewed as a point in
- @math{R@sup{4}}.
- The real--part of a quaternion is also called its @samp{scalar}, while
- the i--part, j--part, and k--part taken together are also called its
- @samp{vector}. A quaternion with zero j--part and k--part is an
- ordinary complex number. (If the i--part is also zero, then it is a
- real). A quaternion with zero real--part is called a
- @samp{vector quaternion}.
- The reader syntax for number literals has been extended to support both
- rectangular and polar (hyperspherical) notation for quaternions. The
- rectangular notation is as above, i.e. @code{w+xi+yj+zk}. The polar
- notation takes the form @code{r@@t%u&v}, where @code{r} is the
- magnitude, @code{t} is the first angle, and @code{u} and @code{v} are
- two other angles called the ``colatitude'' and ``longitude''.
- The rectangular coordinates and polar coordinates are related by the
- equations:
- @example
- @var{w} = @var{r} * cos @var{t}
- @var{x} = @var{r} * sin @var{t} * cos @var{u}
- @var{y} = @var{r} * sin @var{t} * sin @var{u} * cos @var{v}
- @var{z} = @var{r} * sin @var{t} * sin @var{u} * sin @var{v}
- @end example
- With either notation, zero elements may be omitted.
- @deffn Procedure make-rectangular @var{w} @var{x}
- @deffnx Procedure make-rectangular @var{w} @var{x} @var{y} @var{z}
- These procedures construct quaternions from Cartesian coordinates.
- @end deffn
- @deffn Procedure make-polar @var{r} @var{t}
- @deffnx Procedure make-polar @var{r} @var{t} @var{u} @var{v}
- These procedures construct quaternions from polar coordinates.
- @end deffn
- @deffn Procedure + @var{q} @dots{}
- @deffnx Procedure - @var{q} @dots{}
- @deffnx Procedure * @var{q} @dots{}
- @deffnx Procedure / @var{q}
- @deffnx Procedure / @vari{q} @varii{q} @variii{q} @dots{}
- @deffnx Procedure expt @vari{q} @varii{q}
- @deffnx Procedure exp @var{q}
- @deffnx Procedure log @var{q}
- @deffnx Procedure sqrt @var{q}
- @deffnx Procedure sin @var{q}
- @deffnx Procedure cos @var{q}
- @deffnx Procedure tan @var{q}
- @deffnx Procedure asin @var{q}
- @deffnx Procedure acos @var{q}
- @deffnx Procedure atan @var{q}
- All of the arithmetic and transcendental functions defined for complex
- arguments have been extended to support quaternions.
- Quaternion multiplication is not commutative, so there are two possible
- interpretations of @code{(/ q1 q2)} which would yield different results:
- either @code{(* q1 (/ q2))}, or @code{(* (/ q2) q1)}. Division in this
- implementation has been defined such that @code{(/ q1 q2 ...)} is
- equivalent to @code{(* q1 (/ q2) ...)}, but it is recommended to
- use reciprocals (unary @code{/}) and multiplication.
- @end deffn
- @deffn Procedure real-part @var{q}
- Return the real--part of @var{q}.
- @example
- (real-part 0) @result{} 0
- (real-part -i) @result{} 0
- (real-part 1+2i-3j+4k) @result{} 1
- @end example
- @end deffn
- @deffn Procedure imag-part @var{q}
- Return the i--part of @var{q}.
- @example
- (imag-part 0) @result{} 0
- (imag-part -i) @result{} -1
- (imag-part 1+2i-3j+4k) @result{} 2
- @end example
- @end deffn
- @deffn Procedure magnitude @var{q}
- Return the Euclidean norm of @var{q}. If @var{q} is @code{a+bi+cj+dk},
- then @code{(magnitude q)} is
- @code{(sqrt (apply + (map square (list a b c d))))}
- @end deffn
- @deffn Procedure angle @var{q}
- Return the angle of @var{q}.
- @end deffn
- @subsection The @code{(kawa quaternions)} module
- The following additional functionality is made available by doing one
- of:
- @example
- (require 'quaternions) ;; or
- (import (kawa quaternions))
- @end example
- @deffn Alias quaternion
- An alias for @code{gnu.math.Quaternion}, useful for type declarations.
- @end deffn
- @deffn Procedure {quaternion?} @var{x}
- Return @true{} if @var{x} is a quaternion, i.e. an ordinary number, and
- @false{} otherwise.
- @example
- (quaternion? 0) @result{} #t
- (quaternion? -i) @result{} #t
- (quaternion? 1+2i-3j+4k) @result{} #t
- (quaternion? 10.0m) @result{} #f
- (quaternion? "x") @result{} #f
- @end example
- @end deffn
- @deffn Procedure jmag-part @var{q}
- Return the j--part of @var{q}.
- @example
- (jmag-part 0) @result{} 0
- (jmag-part -i) @result{} 0
- (jmag-part 1+2i-3j+4k) @result{} -3
- @end example
- @end deffn
- @deffn Procedure kmag-part @var{q}
- @example
- (kmag-part 0) @result{} 0
- (kmag-part -i) @result{} 0
- (kmag-part 1+2i-3j+4k) @result{} 4
- @end example
- @end deffn
- @deffn Procedure complex-part @var{q}
- Return the projection of @var{q} into the complex plane:
- @code{(+ (real-part q) (* +i (imag-part q)))}
- @example
- (complex-part 0) @result{} 0
- (complex-part -i) @result{} -1i
- (complex-part 1+2i-3j+4k) @result{} 1+2i
- @end example
- @end deffn
- @deffn Procedure vector-part @var{q}
- Return the vector--part of @var{q}.
- @example
- (vector-part 0) @result{} 0
- (vector-part -i) @result{} -1i
- (vector-part 1+2i-3j+4k) @result{} +2i-3j+4k
- @end example
- @end deffn
- @deffn Procedure unit-quaternion @var{q}
- Return a quaternion of unit magnitude with the same direction as
- @var{q}. If @var{q} is zero, return zero. This is like a 4D version of
- a signum function.
- @example
- (unit-quaternion 0) @result{} 0
- (unit-quaternion -i) @result{} -1i
- (unit-quaternion 1+2i-3j+4k) @result{} 0.18257418583505536+0.3651483716701107i-0.5477225575051661j+0.7302967433402214k
- @end example
- @end deffn
- @deffn Procedure unit-vector @var{q}
- Return the vector--part of @var{q}, scaled to have magnitude 1. If the
- vector--part is zero, then return zero.
- @example
- (unit-vector 0) @result{} 0
- (unit-vector -i) @result{} -1i
- (unit-vector 1+2i-3j+4k) @result{} +0.3713906763541037i-0.5570860145311556j+0.7427813527082074k
- @end example
- @end deffn
- @deffn Procedure colatitude @var{q}
- Return the colatitude of @var{q}.
- @end deffn
- @deffn Procedure longitude @var{q}
- Return the longitude of @var{q}.
- @end deffn
- @deffn Procedure {vector-quaternion?} @var{obj}
- Return @true{} if @var{obj} is a vector quaternion, i.e. a quaternion
- with zero real--part.
- @end deffn
- @deffn Procedure make-vector-quaternion @var{x} @var{y} @var{z}
- Construct vector quaternion @code{xi+yj+zk}. This is equivalent to
- @code{(make-rectangular 0 x y z)}.
- @end deffn
- @deffn Procedure {vector-quaternion->list} @var{vq}
- Return a newly allocated list of the x, y, and z components of
- @var{vq}. This is equivalent to
- @code{(list (imag-part vq) (jmag-part vq) (kmag-part vq))}.
- @end deffn
- @deffn Procedure dot-product @vari{q} @varii{q}
- For two vector quaternions @vari{q} = @code{ai+bj+ck} and @varii{q} =
- @code{di+ej+fk}, return @code{ad + be + cf}. This is equal to the
- @math{R^3} dot product for vectors @math{(a,b,c)} and @math{(d,e,f)},
- and is also equal to @code{(- (real-part (* q1 q2)))}. It is an error
- if either @vari{q} or @varii{q} has a non-zero real--part.
- @end deffn
- @deffn Procedure cross-product @vari{q} @varii{q}
- For two vector quaternions @vari{q} = @code{ai+bj+ck} and @varii{q} =
- @code{di+ej+fk}, return the @math{R^3} cross product for vectors
- @math{(a,b,c)} and @math{(d,e,f)}, which is equal to
- @code{(vector-part (* q1 q2))}. It is an error
- if either @vari{q} or @varii{q} has a non-zero real--part.
- @end deffn
- @deffn Procedure conjugate @var{q}
- Return @code{(+ (real-part q) (* -1 (vector-part q)))}.
- @example
- (conjugate 0) @result{} 0
- (conjugate -i) @result{} +1i
- (conjugate 1+2i-3j+4k) @result{} 1-2i+3j-4k
- @end example
- @end deffn
- @subsection The @code{(kawa rotations)} module
- The @code{(kawa rotations)} library provides a set of functions which
- use unit quaternions to represent 3D spatial rotations. To use these
- functions, the library must be imported:
- @example
- (import (kawa rotations))
- @end example
- These functions normalize their quaternion inputs as needed to be of
- length 1.
- @subsubsection Rotation Representation Conversions
- Conversions to and from several alternate representations of rotations
- are supported.
- The set of unit quaternions provides a double covering of all
- possible 3D rotations: @code{q} and @code{-q} represent the same
- rotation. Most other representations also have multiple numerical
- values which map to the same rotation (for example, the rotation about
- @code{axis-vec} by @code{angle} is the same as the rotation about
- @code{-axis-vec} by @code{-angle+2pi}). Therefore, these functions do
- not necessarily act as inverses in the sense of @func{equal?}.
- Furthermore, rotations involve trigonometric functions, so there will
- typically be some floating point error: @code{(acos (cos 0.1))} returns
- 0.09999999999999945, which is very close to 0.1 but not exact.
- @subsubheading Rotation Matrices
- @deffn Procedure {quaternion->rotation-matrix} @var{q}
- @deffnx Procedure {rotation-matrix->quaternion} @var{m}
- The @func{quaternion->rotation-matrix} procedure returns a 3x3 rotation
- matrix representing the same rotation as @var{q}. The rotation matrix
- is instantiated as a @ref{Arrays,SRFI-25 multi-dimensional array} backed
- by an @ref{Uniform vectors,f64vector}.
- The @func{rotation-matrix->quaternion} procedure performs the reverse
- operation, producing an equivalent unit quaternion for the rotation
- matrix (multi-dimensional array) @var{m}.
- @example
- (rotation-matrix->quaternion (quaternion->rotation-matrix -1)) @result{} 1.0
- @end example
- @end deffn
- @subsubheading Axis-Angle Representation
- @deffn Procedure rotation-axis @var{q}
- @deffnx Procedure rotation-angle @var{q}
- @deffnx Procedure rotation-axis/angle @var{q}
- The @func{rotation-axis} procedure returns the axis of rotation of the
- quaternion @var{q} as a unit-length vector quaternion. If the axis of
- rotation is not well-defined (the angle of rotation is 0), then
- @code{+i} is arbitrarily chosen as the axis.
- The @func{rotation-angle} procedure returns the corresponding angle of
- rotation. Note that this is not the same as the result of the
- @func{angle} procedure.
- The @func{rotation-axis/angle} procedure returns the rotation axis and
- angle as multiple values.
- @example
- (let* ((q 1/2+1/2i+1/2j+1/2k)
- (ar (rotation-angle q))
- (ad (java.lang.Math:toDegrees ar))
- (exact-ad (exact ad)))
- (rationalize exact-ad 1/10)) @result{} 120
- @end example
- @end deffn
- @deffn Procedure {make-axis/angle} @var{axis-vec} @var{angle}
- @deffnx Procedure {make-axis/angle} @var{axis-x} @var{axis-y} @var{axis-z} @var{angle}
- The @func{make-axis/angle} procedure returns a quaternion representing
- the given axis/angle rotation. The axis is specified as either a single
- vector quaternion argument @var{axis-vec}, or as three reals
- @var{axis-x}, @var{axis-y}, and @var{axis-z}.
- @end deffn
- @deffn Procedure rotx @var{angle}
- @deffnx Procedure roty @var{angle}
- @deffnx Procedure rotz @var{angle}
- The procedures @func{rotx}, @func{roty}, and @func{rotz} return
- quaternions representing rotations about the X-, Y-, and Z-axes.
- @end deffn
- @subsubheading Intrinsic Angle Sets
- The intrinsic angle sets represent arbitrary rotations as a sequence of
- three rotations about coordinate frame axes attached to the rotating
- body (i.e. the axes rotate with the body).
- There are twelve possible angle sets which neatly divide into two groups
- of six. The six with same first and third axes are also known as
- ``Euler angles''. The six with different first and third axes are also
- known as ``Tait-Bryan angles''.
- @deffn Procedure intrinsic-xyx @var{q}
- @deffnx Procedure intrinsic-xzx @var{q}
- @deffnx Procedure intrinsic-yxy @var{q}
- @deffnx Procedure intrinsic-yzy @var{q}
- @deffnx Procedure intrinsic-zxz @var{q}
- @deffnx Procedure intrinsic-zyz @var{q}
- These functions decompose the rotation represented by @var{q} into Euler
- angles of the given set (XYX, XZX, YXY, YZY, ZXZ, or ZYZ) and returns
- the three angles as multiple values. The middle angle will be in the
- range [0,pi]. If it is on the edges of that range (within 1.0E-12 of 0
- or pi), such that the first and third axes are colinear, then the first
- angle will be set to 0.
- @example
- (intrinsic-zyz (* (rotz 0.3) (roty 0.8) (rotz -0.6))) @result{} 0.3000000000000001 0.7999999999999999 -0.5999999999999999
- @end example
- @end deffn
- @deffn Alias euler-xyx
- @deffnx Alias euler-xzx
- @deffnx Alias euler-yxy
- @deffnx Alias euler-yzy
- @deffnx Alias euler-zxz
- @deffnx Alias euler-zyz
- Aliases for the corresponding @code{intrinsic-} procedures.
- @end deffn
- @deffn Procedure intrinsic-xyz @var{q}
- @deffnx Procedure intrinsic-xzy @var{q}
- @deffnx Procedure intrinsic-yxz @var{q}
- @deffnx Procedure intrinsic-yzx @var{q}
- @deffnx Procedure intrinsic-zxy @var{q}
- @deffnx Procedure intrinsic-zyx @var{q}
- These functions decompose the rotation represented by @var{q} into
- Tait-Bryan angles of the given set (XYZ, XZY, YXZ, YZX, ZXY, or ZYX) and
- returns the three angles as multiple values. The middle angle will be
- in the range [-pi/2,pi/2]. If it is on the edges of that range, such
- that the first and third axes are colinear, then the first angle will be
- set to 0.
- @end deffn
- @deffn Alias tait-bryan-xyz
- @deffnx Alias tait-bryan-xzy
- @deffnx Alias tait-bryan-yxz
- @deffnx Alias tait-bryan-yzx
- @deffnx Alias tait-bryan-zxy
- @deffnx Alias tait-bryan-zyx
- Aliases for the corresponding @code{intrinsic-} procedures.
- @end deffn
- @deffn Procedure make-intrinsic-xyx @var{alpha} @var{beta} @var{gamma}
- @deffnx Procedure make-intrinsic-xzx @var{alpha} @var{beta} @var{gamma}
- @deffnx Procedure make-intrinsic-yxy @var{alpha} @var{beta} @var{gamma}
- @deffnx Procedure make-intrinsic-yzy @var{alpha} @var{beta} @var{gamma}
- @deffnx Procedure make-intrinsic-zxz @var{alpha} @var{beta} @var{gamma}
- @deffnx Procedure make-intrinsic-zyz @var{alpha} @var{beta} @var{gamma}
- These functions return quaternions representing the given Euler angle
- rotations.
- @end deffn
- @deffn Alias make-euler-xyx
- @deffnx Alias make-euler-xzx
- @deffnx Alias make-euler-yxy
- @deffnx Alias make-euler-yzy
- @deffnx Alias make-euler-zxz
- @deffnx Alias make-euler-zyz
- Aliases for the corresponding @code{make-intrinsic-} procedures.
- @example
- (let-values (((a b c) (euler-xyx (make-euler-xyx 1.0 0.0 2.0))))
- (list a b c)) @result{} (0.0 0.0 3.0)
- @end example
- @end deffn
- @deffn Procedure make-intrinsic-xyz @var{alpha} @var{beta} @var{gamma}
- @deffnx Procedure make-intrinsic-xzy @var{alpha} @var{beta} @var{gamma}
- @deffnx Procedure make-intrinsic-yxz @var{alpha} @var{beta} @var{gamma}
- @deffnx Procedure make-intrinsic-yzx @var{alpha} @var{beta} @var{gamma}
- @deffnx Procedure make-intrinsic-zxy @var{alpha} @var{beta} @var{gamma}
- @deffnx Procedure make-intrinsic-zyx @var{alpha} @var{beta} @var{gamma}
- These functions return quaternions representing the given Tait-Bryan
- angle rotations.
- @end deffn
- @deffn Alias make-tait-bryan-xyz
- @deffnx Alias make-tait-bryan-xzy
- @deffnx Alias make-tait-bryan-yxz
- @deffnx Alias make-tait-bryan-yzx
- @deffnx Alias make-tait-bryan-zxy
- @deffnx Alias make-tait-bryan-zyx
- Aliases for the corresponding @code{make-intrinsic-} procedures.
- @end deffn
- @subsubheading Extrinsic Angle Sets
- The extrinsic angle sets represent arbitrary rotations as a sequence of
- three rotations about fixed-frame axes (i.e. the axes do not rotate with
- the body).
- There are twelve possible extrinsic angle sets, and each is the dual of
- an intrinsic set. The extrinsic rotation about axes @code{A}, @code{B},
- and @code{C} by angles @code{a}, @code{b}, and @code{c} is the same as
- the intrinsic rotation about axes @code{C}, @code{B}, and @code{A} by
- angles @code{c}, @code{b}, and @code{a}, with the order of the three
- axes reversed.
- @deffn Procedure extrinsic-xyx @var{q}
- @deffnx Procedure extrinsic-xyz @var{q}
- @deffnx Procedure extrinsic-xzx @var{q}
- @deffnx Procedure extrinsic-zxy @var{q}
- @deffnx Procedure extrinsic-yxy @var{q}
- @deffnx Procedure extrinsic-yxz @var{q}
- @deffnx Procedure extrinsic-yzx @var{q}
- @deffnx Procedure extrinsic-yzy @var{q}
- @deffnx Procedure extrinsic-zxy @var{q}
- @deffnx Procedure extrinsic-zxz @var{q}
- @deffnx Procedure extrinsic-zyx @var{q}
- @deffnx Procedure extrinsic-zyz @var{q}
- These functions decompose the rotation represented by @var{q} into
- extrinsic angles of the given set and returns the three angles as
- multiple values.
- @end deffn
- @deffn Procedure make-extrinsic-xyx @var{gamma} @var{beta} @var{alpha}
- @deffnx Procedure make-extrinsic-xyz @var{gamma} @var{beta} @var{alpha}
- @deffnx Procedure make-extrinsic-xzx @var{gamma} @var{beta} @var{alpha}
- @deffnx Procedure make-extrinsic-xzy @var{gamma} @var{beta} @var{alpha}
- @deffnx Procedure make-extrinsic-yxy @var{gamma} @var{beta} @var{alpha}
- @deffnx Procedure make-extrinsic-yxz @var{gamma} @var{beta} @var{alpha}
- @deffnx Procedure make-extrinsic-yzx @var{gamma} @var{beta} @var{alpha}
- @deffnx Procedure make-extrinsic-yzy @var{gamma} @var{beta} @var{alpha}
- @deffnx Procedure make-extrinsic-zxy @var{gamma} @var{beta} @var{alpha}
- @deffnx Procedure make-extrinsic-zxz @var{gamma} @var{beta} @var{alpha}
- @deffnx Procedure make-extrinsic-zyx @var{gamma} @var{beta} @var{alpha}
- @deffnx Procedure make-extrinsic-zyz @var{gamma} @var{beta} @var{alpha}
- These functions return quaternions representing the given extrinsic
- angle rotations.
- @end deffn
- @deffn Alias rpy
- @deffnx Alias make-rpy
- Aliases for @func{extrinsic-xyz} and @func{make-extrinsic-xyz}.
- @example
- (let ((r (make-rpy 0.12 -0.23 0.34)))
- (let-values (((a b c) (tait-bryan-zyx r)))
- (list a b c))) @result{} (0.3400000000000001 -0.2300000000000001 0.12000000000000002)
- @end example
- @end deffn
- @subsubsection Rotation Operations
- @deffn Procedure rotate-vector @var{rq} @var{vq}
- Applies the rotation represented by quaternion @var{rq} to the vector
- represented by vector quaternion @var{vq}, and returns the rotated
- vector. This is equivalent to @code{(* rq vq (conjugate rq))} for
- normalized @var{rq}.
- @example
- (rotate-vector +k +2i) @result{} -2i
- (rotate-vector 1/2+1/2i+1/2j+1/2k +i+2j+3k) @result{} +3.0i+1.0j+2.0k
- @end example
- @end deffn
- @deffn Procedure make-rotation-procedure @var{rq}
- A partial application of @func{rotate-vector}. Returns a
- single-argument procedure which will take a vector quaternion argument
- and rotate it by @var{rq}. The returned procedure closes over both
- @var{rq} and its conjugate, so this will likely be more efficient than
- @func{rotate-vector} at rotating many vectors by the same rotation.
- @end deffn
- @node Quantities
- @section Quantities and Units
- As a super-class of numbers, Kawa also provides quantities.
- A @dfn{quantity} is a product of a @dfn{unit} and a pure number.
- The number part can be an arbitrary complex number.
- The unit is a product of integer powers of base units,
- such as meter or second.
- Quantity literals have the following syntax:
- @display
- @stxdef{quantity} @stxref{optional-sign} @stxref{decimal} @stxref{unit-term} [@stxlit{*} @stxref{unit-term}]... [@stxlit{/} @stxref{unit-term}]
- @stxdef{unit-term} @stxref{unit-name} [@stxlit{^} @stxref{digit}+]
- @stxdef{unit-name} @stxref{letter}+
- @end display
- Some examples are @code{10pt} (10 points), @code{5s} (5 seconds),
- and @code{4cm^2} (4 square centimeters).
- Note the @var{quantity} syntax is not recognized by the reader.
- Instead these are read as symbols.
- Assuming there is no lexical binding the for the symbol, it will be
- rewritten at compile-time into an expression. For example @code{4cm^2}
- is transformed into:
- @example
- (* 4.0 (expt unit:cm 2))
- @end example
- @deffn Procedure quantity? object
- True iff @var{object} is a quantity. Note that all numbers are
- quantities, but not the other way round.
- Currently, there are no quantities that are not numbers.
- To distinguish a plain unit-less number from a quantity,
- you can use @code{complex?}.
- @end deffn
- @c FIXME Using @code{complex?} as the test would erroneously classify
- @c @code{1+j} as non-plain. The right test would be @code{quaternion?},
- @c though that requires importing the (kawa quaternions) module.
- @deffn Procedure quantity->number q
- Returns the pure number part of the quantity @var{q}, relative to
- primitive (base) units.
- If @var{q} is a number, returns @var{q}.
- If @var{q} is a unit, yields the magitude of @var{q} relative to base units.
- @end deffn
- @deffn Procedure quantity->unit q
- Returns the unit of the quantity @var{q}.
- If @var{q} is a number, returns the empty unit.
- @end deffn
- @deffn Procedure make-quantity x unit
- Returns the product of @var{x} (a pure number) and @var{unit}.
- You can specify a string instead of @var{unit}, such as @code{"cm"}
- or @code{"s"} (seconds).
- @end deffn
- @deffn Syntax define-base-unit unit-name dimension
- Define @var{unit-name} as a base (primitive) unit,
- which is used to measure along the specified @var{dimension}.
- @example
- (define-base-unit dollar "Money")
- @end example
- @end deffn
- @deffn Syntax define-unit unit-name expression
- Define @var{unit-name} as a unit (that can be used in literals)
- equal to the quantity @var{expression}.
- @example
- (define-unit cent 0.01dollar)
- @end example
- The @var{unit-name} is declared in the @code{unit} namespace,
- so the above is equivalent to:
- @example
- (define-constant unit:cent (* 0.01 unit:dollar))
- @end example
- @end deffn
- @subheading Angles
- The following angle units are dimensionless, with no base unit.
- Some procedures treat a unit-less real number as if it were in radians
- (which mathematicians prefer);
- some procedures (such as @code{rotate}) treat a unit-less real number
- as if it were in degrees
- (which is common in Web and other standards).
- @deffn Unit rad
- A unit for angles specified in radians.
- A full circle is 2*pi radians.
- Note that @code{(= 1.5 1.5rad)} is true,
- while @code{(eqv? 1.5 1.5rad)} is false.
- @end deffn
- @deffn Unit deg
- A unit for angles specified in degrees.
- A full circle is 360 degrees.
- @end deffn
- @deffn Unit grad
- A unit for angles specified in gradians.
- A full circle is 400 gradians.
- @end deffn
- @node Logical Number Operations
- @section Logical Number Operations
- These functions operate on the 2's complement binary representation
- of an exact integer.
- @deffn Procedure bitwise-not i
- Returns the bit-wise logical inverse of the argument.
- More formally, returns the exact integer whose two's
- complement representation is the one's complement of the two's
- complement representation of @var{i}.
- @end deffn
- @deffn Procedure bitwise-and i ...
- @deffnx Procedure bitwise-ior i ...
- @deffnx Procedure bitwise-xor i ...
- These procedures return the exact integer that is the bit-wise
- ``and'', ``inclusive or'', or ``exclusive or'' of the two's
- complement representations of their arguments.
- If they are passed only one argument, they return that argument.
- If they are passed no arguments, they return the integer
- that acts as identity for the operation: -1, 0, or 0, respectively.
- @end deffn
- @deffn Procedure bitwise-if i1 i2 i3
- Returns the exact integer that is the bit-wise ``if'' of the twos
- complement representations of its arguments, i.e. for each bit, if it
- is 1 in i1, the corresponding bit in i2 becomes the value of the
- corresponding bit in the result, and if it is 0, the corresponding bit
- in i3 becomes the corresponding bit in the value of the result. This
- is equivaent to the following computation:
- @example
- (bitwise-ior (bitwise-and i1 i2)
- (bitwise-and (bitwise-not i1) i3))
- @end example
- @end deffn
- @deffn Procedure bitwise-bit-count i
- If i is non-negative, returns the number of 1 bits in the twos complement
- representation of i. Otherwise it returns the result of the following
- computation:
- @example
- (bitwise-not (bitwise-bit-count (bitwise-not i)))
- @end example
- @end deffn
- @deffn Procedure bitwise-length i
- Returns the number of bits needed to represent i if it is positive,
- and the number of bits needed to represent @code{(bitwise-not @var{i})}
- if it is negative, which is the exact integer that is the result of the
- following computation:
- @example
- (do ((result 0 (+ result 1))
- (bits (if (negative? i)
- (bitwise-not i)
- ei)
- (bitwise-arithmetic-shift bits -1)))
- ((zero? bits)
- result))
- @end example
- This is the number of bits needed to represent @var{i} in an unsigned field.
- @end deffn
- @deffn Procedure bitwise-first-bit-set i
- Returns the index of the least significant 1 bit in the twos complement
- representation of i. If i is 0, then - 1 is returned.
- @example
- (bitwise-first-bit-set 0) @result{} -1
- (bitwise-first-bit-set 1) @result{} 0
- (bitwise-first-bit-set -4) @result{} 2
- @end example
- @end deffn
- @deffn Procedure bitwise-bit-set? i1 i2
- Returns @code{#t} if the i2'th bit (where @var{i2} must be non-negative)
- is 1 in the two's complement representation of @var{i1}, and @code{#f}
- otherwise. This is the result of the following computation:
- @example
- (not (zero?
- (bitwise-and
- (bitwise-arithmetic-shift-left 1 i2)
- i1)))
- @end example
- @end deffn
- @deffn Procedure bitwise-copy-bit i bitno replacement-bit
- Returns the result of replacing the @var{bitno}'th bit of @var{i}
- by @var{replacement-bit}, where @var{bitno} must be non-negative,
- and @var{replacement-bit} must be either 0 or 1.
- This is the result of the following computation:
- @example
- (let* ((mask (bitwise-arithmetic-shift-left 1 bitno)))
- (bitwise-if mask
- (bitwise-arithmetic-shift-left replacement-bit bitno)
- i))
- @end example
- @end deffn
- @deffn Procedure bitwise-bit-field n start end
- Returns the integer formed from the (unsigned) bit-field
- starting at @var{start} and ending just before @var{end}.
- Same as:
- @example
- (let ((mask
- (bitwise-not
- (bitwise-arithmetic-shift-left -1 @var{end}))))
- (bitwise-arithmetic-shift-right
- (bitwise-and @var{n} mask)
- @var{start}))
- @end example
- @end deffn
- @deffn Procedure bitwise-copy-bit-field to start end from
- Returns the result of replacing in @var{to} the bits at positions from @var{start} (inclusive) to @var{end} (exclusive) by the bits in @var{from} from position 0 (inclusive) to position @var{end} - @var{start} (exclusive).
- Both @var{start} and @var{start} must be non-negative,
- and @var{start} must be less than or equal to @var{start}.
- This is the result of the following computation:
- @example
- (let* ((mask1
- (bitwise-arithmetic-shift-left -1 start))
- (mask2
- (bitwise-not
- (bitwise-arithmetic-shift-left -1 end)))
- (mask (bitwise-and mask1 mask2)))
- (bitwise-if mask
- (bitwise-arithmetic-shift-left from
- start)
- to))
- @end example
- @end deffn
- @deffn Procedure bitwise-arithmetic-shift i j
- Shifts @var{i} by @var{j}.
- It is a ``left'' shift if @code{@var{j}>0}, and
- a ``right'' shift if @code{@var{j}<0}.
- The result is equal to @code{(floor (* @var{i} (expt 2 @var{j})))}.
- Examples:
- @example
- (bitwise-arithmetic-shift -6 -1) @result{}-3
- (bitwise-arithmetic-shift -5 -1) @result{} -3
- (bitwise-arithmetic-shift -4 -1) @result{} -2
- (bitwise-arithmetic-shift -3 -1) @result{} -2
- (bitwise-arithmetic-shift -2 -1) @result{} -1
- (bitwise-arithmetic-shift -1 -1) @result{} -1
- @end example
- @end deffn
- @deffn Procedure bitwise-arithmetic-shift-left i amount
- @deffnx Procedure bitwise-arithmetic-shift-right i amount
- The @var{amount} must be non-negative
- The @code{bitwise-arithmetic-shift-left} procedure returns the same
- result as @code{bitwise-arithmetic-shift},
- and @code{(bitwise-arithmetic-shift-right @var{i} @var{amount})} returns the
- same result as @code{(bitwise-arithmetic-shift @var{i} (- @var{amount}))}.
- If @var{i} is a primitive integer type,
- then @var{amount} must be less than the number of bits in the
- promoted type of @var{i} (32 or 64).
- If the type is unsigned, an unsigned (logic) shift is
- done for @code{bitwise-arithmetic-shift-right},
- rather than a signed (arithmetic) shift.
- @end deffn
- @deffn Procedure bitwise-rotate-bit-field n start end count
- Returns the result of cyclically permuting in @var{n} the bits at positions
- from @var{start} (inclusive) to @var{end} (exclusive) by
- @var{count} bits towards the more significant bits,
- @var{start} and @var{end} must be non-negative,
- and @var{start} must be less than or equal to @var{end}.
- This is the result of the following computation:
- @example
- (let* ((n ei1)
- (width (- end start)))
- (if (positive? width)
- (let* ((count (mod count width))
- (field0
- (bitwise-bit-field n start end))
- (field1 (bitwise-arithmetic-shift-left
- field0 count))
- (field2 (bitwise-arithmetic-shift-right
- field0
- (- width count)))
- (field (bitwise-ior field1 field2)))
- (bitwise-copy-bit-field n start end field))
- n))
- @end example
- @end deffn
- @deffn Procedure bitwise-reverse-bit-field i start end
- Returns the result obtained from @var{i} by reversing the order of the bits at positions from @var{start} (inclusive) to @var{end} (exclusive),
- where @var{start} and @var{end} must be non-negative,
- and @var{start} must be less than or equal to @var{end}.
- @example
- (bitwise-reverse-bit-field #b1010010 1 4) @result{} 88 ; #b1011000
- @end example
- @end deffn
- @deffn Procedure logop op x y
- Perform one of the 16 bitwise operations of @var{x} and @var{y},
- depending on @var{op}.
- @end deffn
- @deffn Procedure logtest i j
- Returns true if the arguments have any bits in common.
- Same as @code{(not (zero? (bitwise-and @var{i} @var{j})))},
- but is more efficient.
- @end deffn
- @subsection SRFI-60 Logical Number Operations
- Kawa supports SRFI-60 ``Integers as Bits'' as well, although we
- generally recommend using the R6RS-compatible functions instead when
- possible. Unless noted as being a builtin function, to use these you
- must first @code{(require 'srfi-60)} or @code{(import (srfi :60))}
- (or @code{(import (srfi :60 integer-bits))}).
- @deffn Procedure logand i ...
- Equivalent to @code{(bitwise-and @var{i} ...)}. Builtin.
- @end deffn
- @deffn Procedure logior i ...
- Equivalent to @code{(bitwise-ior @var{i} ...)}. Builtin.
- @end deffn
- @deffn Procedure logxor i ...
- Equivalent to @code{(bitwise-xor @var{i} ...)}. Builtin.
- @end deffn
- @deffn Procedure lognot i
- Equivalent to @code{(bitwise-not @var{i})}. Builtin.
- @end deffn
- @deffn Procedure bitwise-merge mask i j
- Equivalent to @code{(bitwise-if @var{mask} @var{i} @var{j})}.
- @end deffn
- @deffn Procedure any-bits-set? i j
- Equivalent to @code{(logtest @var{i} @var{j})}.
- @end deffn
- @deffn Procedure logcount i
- @deffnx Procedure bit-count i
- Count the number of 1-bits in @var{i}, if it is non-negative.
- If @var{i} is negative, count number of 0-bits.
- Same as @code{(bitwise-bit-count @var{i})} if @var{i} is non-negative.
- Builtin as @func{logcount}.
- @end deffn
- @deffn Procedure integer-length i
- Equivalent to @code{(bitwise-length @var{i})}. Builtin.
- @end deffn
- @deffn Procedure log2-binary-factors i
- @deffnx Procedure first-set-bit i
- Equivalent to @code{(bitwise-first-bit-set @var{i})}.
- @end deffn
- @deffn Procedure logbit? pos i
- @deffnx Procedure bit-set? pos i
- Equivalent to @code{(bitwise-bit-set? @var{i} @var{pos})}.
- @end deffn
- @deffn Procedure copy-bit bitno i bool
- Equivalent to @code{(bitwise-copy-bit @var{i} @var{bitno} (if @var{bool} 1 0))}.
- @end deffn
- @deffn Procedure bit-field n start end
- Equivalent to @code{(bitwise-bit-field @var{n} @var{start} @var{end})}.
- @end deffn
- @deffn Procedure copy-bit-field to from start end
- Equivalent to @code{(bitwise-copy-bit-field @var{to} @var{start} @var{end} @var{from})}.
- @end deffn
- @deffn Procedure arithmetic-shift i j
- Equivalent to @code{(bitwise-arithmetic-shift @var{i} @var{j})}. Builtin.
- @end deffn
- @deffn Procedure ash i j
- Alias for @code{arithmetic-shift}. Builtin.
- @end deffn
- @deffn Procedure rotate-bit-field n count start end
- Equivalent to @code{(bitwise-rotate-bit-field @var{n} @var{start} @var{end} @var{count})}.
- @end deffn
- @deffn Procedure reverse-bit-field i start end
- Equivalent to @code{(bitwise-reverse-bit-field @var{i} @var{start} @var{end})}.
- @end deffn
- @deffn Procedure integer->list @var{k} [@var{length}]
- @deffnx Procedure list->integer @var{list}
- The @func{integer->list} procedure returns a list of @var{length}
- booleans corresponding to the bits of the non-negative integer @var{k},
- with @code{#t} for @code{1} and @code{#f} for @code{0}. @var{length}
- defaults to @code{(bitwise-length @var{k})}. The list will be in order
- from MSB to LSB, with the value of @code{(odd? @var{k})} in the last
- car.
- The @func{list->integer} procedure returns the integer corresponding to
- the booleans in the list @var{list}.
- The @func{integer->list} and @func{list->integer} procedures are
- inverses so far as @func{equal?} is concerned.
- @end deffn
- @deffn Procedure booleans->integer bool1 ...
- Returns the integer coded by the @var{bool1} ... arguments.
- Equivalent to @code{(list->integer (list @var{bool1} ...))}.
- @end deffn
- @subsection Deprecated Logical Number Operations
- This older function is still available, but we
- recommend using the R6RS-compatible function.
- @deffn Procedure bit-extract n start end
- Equivalent to @code{(bitwise-bit-field @var{n} @var{start} @var{end})}.
- @end deffn
- @node Performance of numeric operations
- @section Performance of numeric operations
- Kawa can generally do a pretty good job of generating
- efficient code for numeric operations, at least when
- it knows or can figure out the types of the operands.
- The basic operations @code{+}, @code{-}, and @code{*}
- are compiled to single-instruction bytecode if both
- operands are @code{int} or @code{long}.
- Likewise, if both operands are floating-point (or
- one is floating-point and the other is rational),
- then single-instruction @code{double} or @code{float}
- instructions are emitted.
- A binary operation involving an infinite-precision @code{integer}
- and a fixed-size @code{int} or @code{long} is normally
- evaluated by expanding the latter to @code{integer}
- and using @code{integer} arithmetic. An exception is
- an integer literal whose
- value fits in an @code{int} or @code{long} - in that case
- the operation is done using @code{int} or @code{long}
- arithmetic.
- In general, integer literals have amorphous type.
- When used to infer the type of a variable, they have @code{integer} type:
- @example
- (let ((v1 0))
- ... v1 has type integer ... )
- @end example
- However, a literal whose value fits in the @code{int} or @code{long} range
- is implicitly viewed @code{int} or @code{long} in certain contexts,
- primarily method overload resolution and binary arithmetic
- (as mentioned above).
- The comparison functions @code{<}, @code{<=}, @code{=},
- @code{>}, and @code{=>} are also optimized to single instriction
- operations if the operands have appropriate type.
- However, the functions @code{zero?}, @code{positive?}, and @code{negative?}
- have not yet been optimized.
- Instead of @code{(positive? x)} write @code{(> x 0)}.
- There are a number of integer division and modulo operations.
- If the operands are @code{int} or @code{long}, it is faster
- to use @code{quotient} and @code{remainder} rather
- than @code{div} and @code{mod} (or @code{modulo}).
- If you know the first operand is non-negative and the second is positive,
- then use @code{quotient} and @code{remainder}.
- (If an operand is an arbitrary-precision @code{integer},
- then it dosn't really matter.)
- The logical operations @code{bitwise-and}, @code{bitwise-ior},
- @code{bitwise-xor}, @code{bitwise-not}, @code{bitwise-arithmetic-shift-left},
- @code{bitwise-arithmetic-shift-right} are compiled
- to single bitcode instructions if the operands are @code{int} or @code{long}.
- Avoid @code{bitwise-arithmetic-shift} if the sign of the shift is known.
- If the operands are arbitrary-precision @code{integer},
- a library call is needed, but run-time type dispatch is avoided.
- @node Characters and text, Data structures, Numbers, Top
- @chapter Characters and text
- @menu
- * Characters::
- * Character sets::
- * Strings::
- * String literals::
- * Unicode:: Unicode character classes and conversions
- * Regular expressions::
- @end menu
- @node Characters
- @section Characters
- Characters are objects that represent human-readable characters
- such as letters and digits. More precisely, a character
- represents a @uref{http://www.unicode.org/glossary/#unicode_scalar_value,
- Unicode scalar value}. Each character has an integer value
- in the range @code{0} to @code{#x10FFFF}
- (excluding the range @code{#xD800} to @code{#xDFFF}
- used for @uref{http://www.unicode.org/glossary/#surrogate_code_point, Surrogate Code Points}).
- @quotation
- @emph{Note:}
- Unicode distinguishes
- between glyphs, which are printed for humans to read, and characters,
- which are abstract entities that map to glyphs (sometimes in a way
- that’s sensitive to surrounding characters). Furthermore, different
- sequences of scalar values sometimes correspond to the same
- character. The relationships among scalar, characters, and glyphs are
- subtle and complex.
- Despite this complexity, most things that a literate human would call
- a ``character'' can be represented by a single Unicode scalar value
- (although several sequences of Unicode scalar values may represent
- that same character). For example, Roman letters, Cyrillic letters,
- Hebrew consonants, and most Chinese characters fall into this
- category.
- Unicode scalar values exclude the range @code{#xD800} to @code{#xDFFF},
- which are part of the range of Unicode @dfn{code points}.
- However, the Unicode code points in this range, the so-called
- @dfn{surrogates}, are an artifact of the UTF-16 encoding, and can only
- appear in specific Unicode encodings, and even then only in pairs that
- encode scalar values. Consequently, all characters represent code
- points, but the surrogate code points do not have representations as
- characters.
- @end quotation
- @deffn Type character
- A Unicode code point - normally a Unicode scalar value,
- but could be a surrogate.
- This is implemented using a 32-bit @code{int}.
- When an object is needed (i.e. the @dfn{boxed} representation),
- it is implemented an instance of @code{gnu.text.Char}.
- @end deffn
- @deffn Type character-or-eof
- A @code{character} or the specical @code{#!eof} value (used to indicate
- end-of-file when reading from a port).
- This is implemented using a 32-bit @code{int},
- where the value -1 indicates end-of-file.
- When an object is needed, it is implemented an instance of
- @code{gnu.text.Char} or the special @code{#!eof} object.
- @end deffn
- @deffn Type char
- A UTF-16 code unit. Same as Java primitive @code{char} type.
- Considered to be a sub-type of @code{character}.
- When an object is needed, it is implemented as an instance
- of @code{java.lang.Character}. Note the unfortunate inconsistency
- (for historical reasons) of @code{char} boxed as @code{Character}
- vs @code{character} boxed as @code{Char}.
- @end deffn
- Characters are written using the notation
- @code{#\}@var{character} (which stands for the given @var{character};
- @code{#\x}@var{hex-scalar-value} (the character whose scalar value
- is the given hex integer);
- or @code{#\}@var{character-name} (a character with a given name):
- @c can't use @stxlit because of slash
- @display
- @stxdef{character} @code{@b{#\}}@var{any-character}
- | @code{@b{#\}} @meta{character-name}
- | @code{@b{#\x}} @stxref{hex-scalar-value}
- | @code{@b{#\X}} @stxref{hex-scalar-value}
- @end display
- The following @meta{character-name} forms are recognized:
- @table @code
- @item @b{#\alarm}
- @code{#\x0007} - the alarm (bell) character
- @item @b{#\backspace}
- @code{#\x0008}
- @item @b{#\delete}
- @item @b{#\del}
- @item @b{#\rubout}
- @code{#\x007f} - the delete or rubout character
- @item @b{#\escape}
- @item @b{#\esc}
- @code{#\x001b}
- @item @b{#\newline}
- @itemx @b{#\linefeed}
- @code{#\x001a} - the linefeed character
- @item @b{#\null}
- @itemx @b{#\nul}
- @code{#\x0000} - the null character
- @item @b{#\page}
- @code{#\000c} - the formfeed character
- @item @b{#\return}
- @code{#\000d} - the carriage return character
- @item @b{#\space}
- @code{#\x0020} - the preferred way to write a space
- @item @b{#\tab}
- @code{#\x0009} - the tab character
- @item @b{#\vtab}
- @code{#\x000b} - the vertical tabulation character
- @item @b{#\ignorable-char}
- A special @code{character} value, but it is not a Unicode code point.
- It is a special value returned when an index refers to the second
- @code{char} (code point) of a surrogate pair, and which should be ignored.
- (When writing a @code{character} to a string or file,
- it will be written as one or two @code{char} values.
- The exception is @code{#\ignorable-char}, for which zero
- @code{char} values are written.)
- @end table
- @deffn Procedure {char?} @var{obj}
- Return @true{} if @var{obj} is a character, @false{} otherwise.
- (The @var{obj} can be any character, not just a 16-bit @code{char}.)
- @end deffn
- @deffn Procedure {char->integer} @var{char}
- @deffnx Procedure {integer->char} @var{sv}
- @var{sv} should be a Unicode scalar value, i.e., a non--negative exact
- integer object in @code{[0, #xD7FF] union [#xE000, #x10FFFF]}.
- (Kawa also allows values in the surrogate range.)
- Given a character, @func{char->integer} returns its Unicode scalar value
- as an exact integer object. For a Unicode scalar value @var{sv},
- @func{integer->char} returns its associated character.
- @example
- (integer->char 32) @result{} #\space
- (char->integer (integer->char 5000)) @result{} 5000
- (integer->char #\xD800) @result{} throws ClassCastException
- @end example
- @PerformanceNote{} A call to @code{char->integer} is compiled as
- casting the argument to a @code{character}, and then re-interpreting
- that value as an @code{int}.
- A call to @code{integer->char} is compiled as
- casting the argument to an @code{int}, and then re-interpreting
- that value as an @code{character}.
- If the argument is the right type, no code is emitted: the value is
- just re-interpreted as the result type.
- @end deffn
- @deffn Procedure {char=?} @vari{char} @varii{char} @variii{char} @dots{}
- @deffnx Procedure {char<?} @vari{char} @varii{char} @variii{char} @dots{}
- @deffnx Procedure {char>?} @vari{char} @varii{char} @variii{char} @dots{}
- @deffnx Procedure {char<=?} @vari{char} @varii{char} @variii{char} @dots{}
- @deffnx Procedure {char>=?} @vari{char} @varii{char} @variii{char} @dots{}
- These procedures impose a total ordering on the set of characters
- according to their Unicode scalar values.
- @example
- (char<? #\z #\ß) @result{} #t
- (char<? #\z #\Z) @result{} #f
- @end example
- @PerformanceNote{} This is compiled as if converting each
- argument using @code{char->integer} (which requires no code)
- and the using the corresponing @code{int} comparison.
- @end deffn
- @deffn Procedure digit-value char
- This procedure returns the numeric value (0 to 9) of its
- argument if it is a numeric digit (that is, if @code{char-numeric?}
- returns @code{#t}), or @code{#f} on any other character.
- @example
- (digit-value #\3) @result{} 3
- (digit-value #\x0664) @result{} 4
- (digit-value #\x0AE6) @result{} 0
- (digit-value #\x0EA6) @result{} #f
- @end example
- @end deffn
- @node Character sets
- @section Character sets
- Sets of characters are useful for text-processing code,
- including parsing, lexing, and pattern-matching.
- @uref{http://srfi.schemers.org/srfi-14/srfi-14.html, SRFI 14} specifies
- a @code{char-set} type for such uses. Some examples:
- @example
- (import (srfi :14 char-sets))
- (define vowel (char-set #\a #\e #\i #\o #\u))
- (define vowely (char-set-adjoin vowel #\y))
- (char-set-contains? vowel #\y) @result{} #f
- (char-set-contains? vowely #\y) @result{} #t
- @end example
- See the @uref{http://srfi.schemers.org/srfi-14/srfi-14.html, SRFI 14 specification} for details.
- @deffn Type char-set
- The type of character sets.
- In Kawa @code{char-set} is a type that can be used in type specifiers:
- @example
- (define vowely ::char-set (char-set-adjoin vowel #\y))
- @end example
- @end deffn
- Kawa uses @uref{https://en.wikipedia.org/wiki/Inversion_list,inversion lists} for an efficient implementation, using Java @code{int} arrays
- to represents character ranges (inversions).
- The @code{char-set-contains?} function uses binary search,
- so it takes time proportional to the logarithm of the number of inversions.
- Other operations may take time proportional to the number of inversions.
- @node Strings
- @section Strings
- Strings are sequences of characters. The @emph{length} of a string is
- the number of characters that it contains, as an exact non-negative integer.
- The @emph{valid indices} of a string are the
- exact non-negative integers less than the length of the string.
- The first character of a
- string has index 0, the second has index 1, and so on.
- Strings are @emph{implemented} as a sequence of 16-bit @code{char} values,
- even though they're semantically a sequence of 32-bit Unicode code points.
- A character whose value is greater than @code{#xffff}
- is represented using two surrogate characters.
- The implementation allows for natural interoperability with Java APIs.
- However it does make certain operations (indexing or counting based on
- character counts) difficult to implement efficiently. Luckily one
- rarely needs to index or count based on character counts;
- alternatives are discussed below.
- @cindex istring
- @cindex mstring
- There are different kinds of strings:
- @itemize
- @item
- An @dfn{istring} is @emph{immutable}:
- It is fixed, and cannot be modified.
- On the other hand, indexing (e.g. @code{string-ref}) is efficient (constant-time),
- while indexing of other string implementations takes time proportional
- to the index.
- String literals are istrings, as are the return values of
- most of the procedures in this chapter.
- An @dfn{istring} is an instance of the @code{gnu.lists.IString} class.
- @item
- An @dfn{mstring} is @emph{mutable}:
- You can replace individual characters (using @code{string-set!}).
- You can also change the @var{mstring}'s length by inserting
- or removing characters (using @code{string-append!} or @code{string-replace!}).
- An @dfn{mstring} is an instance of the @code{gnu.lists.FString} class.
- @item
- Any other object that implements the @code{java.lang.CharSequence} interface
- is also a string.
- This includes standard Java @code{java.lang.String}
- and @code{java.lang.StringBuilder} objects.
- @end itemize
- Some of the procedures that operate on strings ignore the
- difference between upper and lower case. The names of
- the versions that ignore case end with “@code{-ci}” (for “case
- insensitive”).
- @CompatibilityNote{}
- Many of the following procedures (for example @code{string-append})
- return an immutable istring in Kawa,
- but return a ``freshly allocated'' mutable string in
- standard Scheme (include R7RS) as well as most Scheme implementations
- (including previous versions of Kawa).
- To get the ``compatibility mode'' versions of those procedures
- (which return mstrings),
- invoke Kawa with one the @code{--r5rs}, @code{--r6rs}, or @code{--r7rs}
- options, or you can @code{import} a standard library like @code{(scheme base)}.
- @deffn Type string
- The type of string objects.
- The underlying type is the interface @code{java.lang.CharSequence}.
- Immultable strings are @code{gnu.lists.IString} or @code{java.lang.String},
- while mutable strings are @code{gnu.lists.FString}.
- @end deffn
- @subsection Basic string procedures
- @deffn Procedure {string?} @var{obj}
- Return @true{} if @var{obj} is a string, @false{} otherwise.
- @end deffn
- @deffn Procedure {istring?} @var{obj}
- Return @true{} if @var{obj} is a istring (a immutable, constant-time-indexable string); @false{} otherwise.
- @end deffn
- @deffn Constructor string @var{char} @dots{}
- Return a string composed of the arguments.
- This is analogous to @var{list}.
- @CompatibilityNote{} The result is an istring,
- except in compatibility mode, when it is a new allocated mstring.
- @end deffn
- @deffn Procedure string-length @var{string}
- Return the number of characters in the given @var{string} as an exact
- integer object.
- @PerformanceNote{} If the @var{string} is not an istring,
- the calling @code{string-length} may take time proportional
- to the length of the @var{string},
- because of the need to scan for surrogate pairs.
- @end deffn
- @deffn Procedure string-ref @var{string} @var{k}
- @var{k} must be a valid index of @var{string}. The @func{string-ref}
- procedure returns character @var{k} of @var{string} using zero--origin
- indexing.
- @PerformanceNote{} If the @var{string} is not an istring,
- then calling @code{string-ref} may take time proportional
- to @var{k} because of the need to check for surrogate pairs.
- An alternative is to use @code{string-cursor-ref}.
- If iterating through a string, use @code{string-for-each}.
- @end deffn
- @deffn Procedure string-null? @var{string}
- Is @var{string} the empty string?
- Same result as @code{(= (string-length @var{string}) 0)} but
- executes in O(1) time.
- @end deffn
- @deffn Procedure string-every pred string [start end])
- @deffnx Procedure string-any pred string [start end])
- Checks to see if every/any character in @var{string} satisfies @var{pred},
- proceeding from left (index @var{start}) to right (index @var{end}). These
- procedures are short-circuiting: if @var{pred} returns false,
- @code{string-every} does not call @var{pred} on subsequent characters;
- if @var{pred} returns true, @code{string-any} does not call @var{pred}
- on subsequent characters. Both procedures are ``witness-generating'':
- @itemize
- @item
- If @code{string-every} is given an empty interval (with @var{start} = @var{end}),
- it returns @code{#t}.
- @item
- If @code{string-every} returns true for a non-empty interval
- (with @var{start} < @var{end}), the returned true value is the one returned by the final call to the predicate on
- @code{(string-ref @var{string} (- @var{end} 1))}.
- @item
- If @code{string-any} returns true, the returned true value is the one
- returned by the predicate.
- @end itemize
- @emph{Note:} The names of these procedures do not end with a question
- mark. This indicates a general value is returned instead of a simple
- boolean (@code{#t} or @code{#f}).
- @end deffn
- @subsection Immutable String Constructors
- @deffn Procedure string-tabulate proc len
- Constructs a string of size @var{len} by calling @var{proc} on
- each value from 0 (inclusive) to @var{len} (exclusive) to produce
- the corresponding element of the string.
- The procedure @var{proc} accepts an exact integer as its argument and returns a character.
- The order in which @var{proc} is called on those indexes is not specifified.
- @emph{Rationale:} Although @code{string-unfold} is more general,
- @code{string-tabulate} is likely to run faster for the common special
- case it implements.
- @end deffn
- @deffn Procedure string-unfold stop? mapper successor seed [base make-final]
- @deffnx Procedure string-unfold-right stop? mapper successor seed [base make-final]
- This is a fundamental and powerful constructor for strings.
- @itemize
- @item
- @var{successor} is used to generate a series of ``seed'' values from the initial seed:
- @var{seed}, @code{(}@var{successor} @var{seed}@code{)}, @code{(}@var{successor}@sup{2} @var{seed}@code{)}, @code{(}@var{successor}@sup{3} @var{seed}@code{)}, ...
- @item
- @var{stop?} tells us when to stop — when it returns true when applied to one of these seed values.
- @item
- @var{mapper} maps each seed value to the corresponding character(s) in the result string, which are assembled into that string in left-to-right order. It is an error for @var{mapper} to return anything other than a character or string.
- @item
- @var{base} is the optional initial/leftmost portion of the constructed string, which defaults to the empty string @code{""}.
- It is an error if @var{base} is anything other than a character or string.
- @item
- @var{make-final} is applied to the terminal seed value (on which @var{stop?} returns true) to produce the final/rightmost portion of the constructed string. It defaults to @code{(lambda (x) "")}.
- It is an error for @var{make-final} to return anything other than
- a character or string.
- @end itemize
- @code{string-unfold-right} is the same as @code{string-unfold} except the
- results of @var{mapper} are assembled into the string in right-to-left order,
- @var{base} is the optional rightmost portion of the constructed string, and
- @var{make-final} produces the leftmost portion of the constructed string.
- You can use it @code{string-unfold} to convert a list to a string,
- read a port into a string, reverse a string, copy a string, and so forth.
- Examples:
- @example
- (define (port->string p)
- (string-unfold eof-object? values
- (lambda (x) (read-char p))
- (read-char p)))
- (define (list->string lis)
- (string-unfold null? car cdr lis))
- (define (string-tabulate f size)
- (string-unfold (lambda (i) (= i size)) f add1 0))
- @end example
- To map @var{f} over a list @var{lis}, producing a string:
- @example
- (string-unfold null? (compose @var{f} car) cdr @var{lis})
- @end example
- Interested functional programmers may enjoy noting that @code{string-fold-right}
- and @code{string-unfold} are in some sense inverses.
- That is, given operations @var{knull?}, @var{kar}, @var{kdr},
- @var{kons}, and @var{knil} satisfying
- @example
- (@var{kons} (@var{kar} x) (@var{kdr} x)) = x and (@var{knull?} @var{knil}) = #t
- @end example
- then
- @example
- (string-fold-right @var{kons} @var{knil} (string-unfold @var{knull?} @var{kar} @var{kdr} @var{x})) = @var{x}
- @end example
- and
- @example
- (string-unfold @var{knull?} @var{kar} @var{kdr} (string-fold-right @var{kons} @var{knil} @var{string})) = @var{string}.
- @end example
- This combinator pattern is sometimes called an ``anamorphism.''
- @end deffn
- @subsection Selection
- @deffn Procedure substring @var{string} @var{start} @var{end}
- @var{string} must be a string, and @var{start} and @var{end} must be
- exact integer objects satisfying:
- @example
- 0 <= @var{start} <= @var{end} <= (string-length @var{string})
- @end example
- The @func{substring} procedure returns a newly allocated string formed
- from the characters of @var{string} beginning with index @var{start}
- (inclusive) and ending with index @var{end} (exclusive).
- @end deffn
- @deffn Procedure string-take string nchars
- @deffnx Procedure string-drop string nchars
- @deffnx Procedure string-take-right string nchars
- @deffnx Procedure string-drop-right string nchars
- @code{string-take} returns an immutable string containing the
- first @var{nchars} of @var{string};
- @code{string-drop} returns a string containing all but the first @var{nchars}
- of @var{string}.
- @code{string-take-right} returns a string containing the last @var{nchars}
- of @var{string}; @code{string-drop-right} returns a string containing all
- but the last @var{nchars} of @var{string}.
- @example
- (string-take "Pete Szilagyi" 6) @result{} "Pete S"
- (string-drop "Pete Szilagyi" 6) @result{} "zilagyi"
- (string-take-right "Beta rules" 5) @result{} "rules"
- (string-drop-right "Beta rules" 5) @result{} "Beta "
- @end example
- It is an error to take or drop more characters than are in the string:
- @example
- (string-take "foo" 37) @result{} @i{error}
- @end example
- @end deffn
- @deffn Procedure string-pad string len [char start end]
- @deffnx Procedure string-pad-right string len [char start end]
- Returns an istring of length @var{len} comprised of the characters
- drawn from the given subrange of @var{string},
- padded on the left (right) by as many occurrences of the
- character @var{char} as needed.
- If @var{string} has more than @var{len} chars, it is truncated on the
- left (right) to length @var{len}.
- The @var{char} defaults to @code{#\space}
- @example.
- (string-pad "325" 5) @result{} " 325"
- (string-pad "71325" 5) @result{} "71325"
- (string-pad "8871325" 5) @result{} "71325"
- @end example
- @end deffn
- @deffn Procedure string-trim string [pred start end]
- @deffnx Procedure string-trim-right string [pred start end]
- @deffnx Procedure string-trim-both string [pred start end]
- Returns an istring obtained from the given subrange of @var{string}
- by skipping over all characters on the left / on the right / on both sides that satisfy the second argument @var{pred}:
- @var{pred} defaults to @code{char-whitespace?}.
- @example
- (string-trim-both " The outlook wasn't brilliant, \n\r")
- @result{} "The outlook wasn't brilliant,"
- @end example
- @end deffn
- @subsection String Comparisons
- @deffn Procedure {string=?} @vari{string} @varii{string} @variii{string} @dots{}
- Return @true{} if the strings are the same length and contain the same
- characters in the same positions. Otherwise, the @func{string=?}
- procedure returns @false{}.
- @example
- (string=? "Straße" "Strasse") @result{} #f
- @end example
- @end deffn
- @deffn Procedure {string<?} @vari{string} @varii{string} @variii{string} @dots{}
- @deffnx Procedure {string>?} @vari{string} @varii{string} @variii{string} @dots{}
- @deffnx Procedure {string<=?} @vari{string} @varii{string} @variii{string} @dots{}
- @deffnx Procedure {string>=?} @vari{string} @varii{string} @variii{string} @dots{}
- These procedures return @code{#t} if their arguments are (respectively):
- monotonically increasing, monotonically decreasing,
- monotonically non-decreasing, or monotonically nonincreasing.
- These predicates are required to be transitive.
- These procedures are the lexicographic extensions to strings of the
- corresponding orderings on characters. For example, @func{string<?} is
- the lexicographic ordering on strings induced by the ordering
- @func{char<?} on characters. If two strings differ in length but are
- the same up to the length of the shorter string, the shorter string is
- considered to be lexicographically less than the longer string.
- @example
- (string<? "z" "ß") @result{} #t
- (string<? "z" "zz") @result{} #t
- (string<? "z" "Z") @result{} #f
- @end example
- @end deffn
- @deffn Procedure string-ci=? @vari{string} @varii{string} @variii{string} @dots{}
- @deffnx Procedure string-ci<? @vari{string} @varii{string} @variii{string} @dots{}
- @deffnx Procedure string-ci>? @vari{string} @varii{string} @variii{string} @dots{}
- @deffnx Procedure string-ci<=? @vari{string} @varii{string} @variii{string} @dots{}
- @deffnx Procedure string-ci>=? @vari{string} @varii{string} @variii{string} @dots{}
- These procedures are similar to @func{string=?}, etc.,
- but behave as if they applied @code{string-foldcase} to their arguments
- before invoking the corresponding procedures without @code{-ci}.
- @example
- (string-ci<? "z" "Z") @result{} #f
- (string-ci=? "z" "Z") @result{} #t
- (string-ci=? "Straße" "Strasse") @result{} #t
- (string-ci=? "Straße" "STRASSE") @result{} #t
- (string-ci=? "ΧΑΟΣ" "χαοσ") @result{} #t
- @end example
- @end deffn
- @subsection Conversions
- @deffn Procedure list->string @var{list}
- The @func{list->string} procedure returns an istring
- formed from the characters in @var{list}, in order.
- It is an error if any element of @var{list} is not a character.
- @CompatibilityNote{} The result is an istring,
- except in compatibility mode, when it is an mstring.
- @end deffn
- @deffn Procedure reverse-list->string @var{list}
- An efficient implementation of @code{(compose list->text reverse)}:
- @example
- (reverse-list->text '(#\a #\B #\c)) @result{} "cBa"
- @end example
- This is a common idiom in the epilogue of string-processing loops
- that accumulate their result using a list in reverse order.
- (See also @code{string-concatenate-reverse} for the ``chunked'' variant.)
- @end deffn
- @deffn Procedure string->list @var{string} [@var{start} [@var{end}]]
- The @func{string->list} procedure returns a newly allocated list of the
- characters of @var{string} between @var{start} and @var{end}, in order.
- The @func{string->list} and @func{list->string} procedures are inverses
- so far as @func{equal?} is concerned.
- @end deffn
- @deffn Procedure vector->string vector [start [end]]
- The @code{vector->string} procedure returns a newly allocated
- string of the objects contained in the elements of @var{vector}
- between @var{start} and @var{end}.
- It is an error if any element of @var{vector} between @var{start}
- and @var{end} is not a character, or is a character forbidden in strings.
- @example
- (vector->string #(#\1 #\2 #\3)) @result{} "123"
- (vector->string #(#\1 #\2 #\3 #\4 #\5) 2 4) @result{} "34"
- @end example
- @end deffn
- @deffn Procedure string->vector string [start [end]]
- The @code{string->vector} procedure
- returns a newly created vector initialized to the elements
- of the string @var{string} between @var{start} and @var{end}.
- @example
- (string->vector "ABC") @result{} #(#\A #\B #\C)
- (string->vector "ABCDE" 1 3) @result{} #(#\B #\C)
- @end example
- @end deffn
- @deffn Procedure string-upcase @var{string}
- @deffnx Procedure string-downcase @var{string}
- @deffnx Procedure string-titlecase @var{string}
- @deffnx Procedure string-foldcase @var{string}
- These procedures take a string argument and return a string result.
- They are defined in terms of Unicode's locale--independent case mappings
- from Unicode scalar--value sequences to scalar--value sequences. In
- particular, the length of the result string can be different from the
- length of the input string. When the specified result is equal in the
- sense of @func{string=?} to the argument, these procedures may return
- the argument instead of a newly allocated string.
- The @func{string-upcase} procedure converts a string to upper case;
- @func{string-downcase} converts a string to lower case. The
- @func{string-foldcase} procedure converts the string to its case--folded
- counterpart, using the full case--folding mapping, but without the
- special mappings for Turkic languages. The @func{string-titlecase}
- procedure converts the first cased character of each word, and downcases
- all other cased characters.
- @example
- (string-upcase "Hi") @result{} "HI"
- (string-downcase "Hi") @result{} "hi"
- (string-foldcase "Hi") @result{} "hi"
- (string-upcase "Straße") @result{} "STRASSE"
- (string-downcase "Straße") @result{} "straße"
- (string-foldcase "Straße") @result{} "strasse"
- (string-downcase "STRASSE") @result{} "strasse"
- (string-downcase "Σ") @result{} "σ"
- ; Chi Alpha Omicron Sigma:
- (string-upcase "ΧΑΟΣ") @result{} "ΧΑΟΣ"
- (string-downcase "ΧΑΟΣ") @result{} "χαος"
- (string-downcase "ΧΑΟΣΣ") @result{} "χαοσς"
- (string-downcase "ΧΑΟΣ Σ") @result{} "χαος σ"
- (string-foldcase "ΧΑΟΣΣ") @result{} "χαοσσ"
- (string-upcase "χαος") @result{} "ΧΑΟΣ"
- (string-upcase "χαοσ") @result{} "ΧΑΟΣ"
- (string-titlecase "kNock KNoCK") @result{} "Knock Knock"
- (string-titlecase "who's there?") @result{} "Who's There?"
- (string-titlecase "r6rs") @result{} "R6rs"
- (string-titlecase "R6RS") @result{} "R6rs"
- @end example
- Since these procedures are locale--independent, they may not be
- appropriate for some locales.
- @emph{Kawa Note:} The implementation of @func{string-titlecase}
- does not correctly handle the case where an initial character
- needs to be converted to multiple characters, such as
- ``LATIN SMALL LIGATURE FL'' which should be converted to
- the two letters @code{"Fl"}.
- @CompatibilityNote{} The result is an istring,
- except in compatibility mode, when it is an mstring.
- @end deffn
- @deffn Procedure string-normalize-nfd @var{string}
- @deffnx Procedure string-normalize-nfkd @var{string}
- @deffnx Procedure string-normalize-nfc @var{string}
- @deffnx Procedure string-normalize-nfkc @var{string}
- These procedures take a string argument and return a string result,
- which is the input string normalized to Unicode normalization form D,
- KD, C, or KC, respectively. When the specified result is equal in the
- sense of @func{string=?} to the argument, these procedures may return
- the argument instead of a newly allocated string.
- @example
- (string-normalize-nfd "\xE9;") @result{} "\x65;\x301;"
- (string-normalize-nfc "\xE9;") @result{} "\xE9;"
- (string-normalize-nfd "\x65;\x301;") @result{} "\x65;\x301;"
- (string-normalize-nfc "\x65;\x301;") @result{} "\xE9;"
- @end example
- @end deffn
- @subsection Searching and matching
- @deffn Procedure string-prefix-length string@sub{1} string@sub{2} [start@sub{1} end@sub{1} start@sub{2} end@sub{2}]
- @deffnx Procedure string-suffix-length string@sub{1} string@sub{2} [start@sub{1} end@sub{1} start@sub{2} end@sub{2}]
- Return the length of the longest common prefix/suffix of @var{string@sub{1}}
- and @var{string@sub{2}}.
- For prefixes, this is equivalent to their ``mismatch index''
- (relative to the start indexes).
- The optional @var{start}/@var{end} indexes restrict the comparison
- to the indicated substrings of @var{string@sub{1}} and @var{string@sub{2}}.
- @end deffn
- @deffn Procedure string-prefix? string@sub{1} string@sub{2} [start@sub{1} end@sub{1} start@sub{2} end@sub{2}]
- @deffnx Procedure string-suffix? string@sub{1} string@sub{2} [start@sub{1} end@sub{1} start@sub{2} end@sub{2}]
- Is @var{string@sub{1}} a prefix/suffix of @var{string@sub{2}}?
- The optional @var{start}/@var{end} indexes restrict the comparison
- to the indicated substrings of @var{string@sub{1}} and @var{string@sub{2}}.
- @end deffn
- @deffn Procedure string-index string pred [start end]
- @deffnx Procedure string-index-right string pred [start end]
- @deffnx Procedure string-skip string pred [start end]
- @deffnx Procedure string-skip-right string pred [start end]
- @code{string-index} searches through the given substring from the
- left, returning the index of the leftmost character satisfying the
- predicate @var{pred}.
- @code{string-index-right} searches from the right, returning
- the index of the rightmost character satisfying the predicate @var{pred}.
- If no match is found, these procedures return @code{#f}.
- The @var{start} and @var{end} arguments specify the beginning and end
- of the search; the valid indexes relevant to the search include @var{start}
- but exclude @var{end}. Beware of ``fencepost''" errors: when searching
- right-to-left, the first index considered is @code{(- @var{end} 1)},
- whereas when searching left-to-right, the first index considered is @var{start}.
- That is, the start/end indexes describe the same half-open interval
- @code{[@var{start},@var{end})}
- in these procedures that they do in other string procedures.
- The @code{-skip} functions are similar, but use the complement of the
- criterion: they search for the first char that @emph{doesn't} satisfy
- @var{pred}. To skip over initial whitespace, for example, say
- @example
- (substring string
- (or (string-skip string char-whitespace?)
- (string-length string))
- (string-length string))
- @end example
- These functions can be trivially composed with @code{string-take} and
- @code{string-drop} to produce @code{take-while}, @code{drop-while},
- @code{span}, and @code{break} procedures without loss of efficiency.
- @end deffn
- @c SRFI-13 and Guile generalizes pred to char_pred, which can be a
- @c predicate, a character, or a character set. That seems
- @c convenient.
- @deffn Procedure string-contains string@sub{1} string@sub{2} [start@sub{1} end@sub{1} start@sub{2} end@sub{2}]
- @deffnx Procedure string-contains-right string@sub{1} string@sub{2} [start@sub{1} end@sub{1} start@sub{2} end@sub{2}]
- Does the substring of @var{string@sub{1}} specified by @var{start@sub{1}} and @var{end@sub{1}} contain the sequence of characters given by the substring of @var{string@sub{2}} specified by @var{start@sub{2}} and @var{end@sub{2}}?
- Returns @code{#f} if there is no match.
- If @var{start@sub{2}} = @var{end@sub{2}}, @code{string-contains}
- returns @var{start@sub{1}} but @code{string-contains-right}
- returns @var{end@sub{1}}. Otherwise returns the index in @var{string@sub{1}} for the first character of the first/last match; that index lies within the half-open interval [@var{start@sub{1}},@var{end@sub{1}}), and the match lies entirely within the [@var{start@sub{1}},@var{end@sub{1}}) range of @var{string@sub{1}}.
- @example
- (string-contains "eek -- what a geek." "ee" 12 18) ; Searches "a geek"
- @result{} 15
- @end example
- Note: The names of these procedures do not end with a question mark. This indicates a useful value is returned when there is a match.
- @end deffn
- @subsection Concatenation and replacing
- @deffn Procedure string-append @var{string} @dots{}
- Returns a string whose characters form the concatenation
- of the given strings.
- @CompatibilityNote{} The result is an istring,
- except in compatibility mode, when it is an mstring.
- @end deffn
- @deffn Procedure string-concatenate string-list
- Concatenates the elements of @var{string-list} together into a single istring.
- @emph{Rationale:} Some implementations of Scheme limit the number of
- arguments that may be passed to an n-ary procedure, so the
- @code{(apply string-append @var{string-list})} idiom,
- which is otherwise equivalent to using this procedure, is not as portable.
- @end deffn
- @deffn Procedure string-concatenate-reverse string-list [final-string [end]])
- With no optional arguments, calling this procedure is equivalent to
- @code{(string-concatenate (reverse @var{string-list}))}.
- If the optional argument @var{final-string} is specified,
- it is effectively consed onto the beginning of @var{string-list}
- before performing the list-reverse and string-concatenate operations.
- If the optional argument @var{end} is given, only the characters up to but not including @var{end} in @var{final-string} are added to the result,
- thus producing
- @example
- (string-concatenate
- (reverse (cons (substring final-string 0 end)
- string-list)))
- @end example
- For example:
- @example
- (string-concatenate-reverse '(" must be" "Hello, I") " going.XXXX" 7)
- @result{} "Hello, I must be going."
- @end example
- @emph{Rationale:} This procedure is useful when constructing
- procedures that accumulate character data into lists of string
- buffers, and wish to convert the accumulated data into a single string
- when done. The optional end argument accommodates that use case when
- @var{final-string} is a bob-full mutable string,
- and is allowed (for uniformity) when
- @var{final-string} is an immutable string.
- @end deffn
- @deffn Procedure string-join string-list [delimiter [grammar]]
- This procedure is a simple unparser; it pastes strings together
- using the @var{delimiter} string, returning an istring.
- The @var{string-list} is a list of strings.
- The @var{delimiter} is the string used to delimit elements; it defaults to a single space @code{" "}.
- The @var{grammar} argument is a symbol that determines how the @var{delimiter}
- is used, and defaults to @code{'infix}.
- It is an error for @var{grammar} to be any symbol other than these four:
- @table @code
- @item 'infix
- An infix or separator grammar: insert the delimiter between list elements. An empty list will produce an empty string.
- @item 'strict-infix
- Means the same as @code{'infix} if the string-list is non-empty,
- but will signal an error if given an empty list.
- (This avoids an ambiguity shown in the examples below.)
- @item 'suffix
- Means a suffix or terminator grammar: insert the @var{delimiter}
- after every list element.
- @item 'prefix
- Means a prefix grammar: insert the @var{delimiter} before every list element.
- @end table
- @example
- (string-join '("foo" "bar" "baz"))
- @result{} "foo bar baz"
- (string-join '("foo" "bar" "baz") "")
- @result{} "foobarbaz"
- (string-join '("foo" "bar" "baz") ":")
- @result{} "foo:bar:baz"
- (string-join '("foo" "bar" "baz") ":" 'suffix)
- @result{} "foo:bar:baz:"
- ;; Infix grammar is ambiguous wrt empty list vs. empty string:
- (string-join '() ":") @result{} ""
- (string-join '("") ":") @result{} ""
- ;; Suffix and prefix grammars are not:
- (string-join '() ":" 'suffix)) @result{} ""
- (string-join '("") ":" 'suffix)) @result{} ":"
- @end example
- @end deffn
- @deffn Procedure string-replace string@sub{1} string@sub{2} start@sub{1} end@sub{1} [start@sub{2} end@sub{2}]
- Returns
- @example
- (string-append (substring @var{string@sub{1}} 0 @var{start@sub{1}})
- (substring @var{string@sub{2}} @var{start@sub{2}} @var{end@sub{2}})
- (substring @var{string@sub{1}} @var{end@sub{1}} (string-length @var{string@sub{1}})))
- @end example
- That is, the segment of characters in @var{string@sub{1}} from @var{start@sub{1}}
- to @var{end@sub{1}} is replaced by the segment of characters in @var{string@sub{2}} from @var{start@sub{2}} to @var{end@sub{2}}.
- If @var{start@sub{1}}=@var{end@sub{1}}, this simply splices the characters
- drawn from @var{string@sub{2}} into @var{string@sub{1}} at that position.
- Examples:
- @example
- (string-replace "The TCL programmer endured daily ridicule."
- "another miserable perl drone" 4 7 8 22)
- @result{} "The miserable perl programmer endured daily ridicule."
- (string-replace "It's easy to code it up in Scheme." "lots of fun" 5 9)
- @result{} "It's lots of fun to code it up in Scheme."
- (define (string-insert s i t) (string-replace s t i i))
- (string-insert "It's easy to code it up in Scheme." 5 "really ")
- @result{} "It's really easy to code it up in Scheme."
- (define (string-set s i c) (string-replace s (string c) i (+ i 1)))
- (string-set "String-ref runs in O(n) time." 19 #\1)
- @result{} "String-ref runs in O(1) time."
- @end example
- @end deffn
- Also see @code{string-append!} and @code{string-replace!}
- for destructive changes to a mutable string.
- @subsection Mapping and folding
- @deffn Procedure string-fold kons knil string [start end]
- @deffnx Procedure string-fold-right kons knil string [start end]
- These are the fundamental iterators for strings.
- The @code{string-fold} procedure maps the @var{kons} procedure across
- the given @var{string} from left to right:
- @example
- (... (@var{kons} @var{string}@sub{2} (@var{kons} @var{string}@sub{1} (@var{kons} @var{string}@sub{0} @var{knil}))))
- @end example
- In other words, string-fold obeys the (tail) recursion
- @example
- (string-fold @var{kons} @var{knil} @var{string} @var{start} @var{end})
- = (string-fold @var{kons} (@var{kons} @var{string}@sub{start} @var{knil}) @var{start+1} @var{end})
- @end example
- The @code{string-fold-right} procedure maps @var{kons} across the given
- string @var{string} from right to left:
- @example
- (@var{kons} @var{string}@sub{0}
- (... (@var{kons} @var{string}@sub{@var{end-3}}
- (@var{kons} @var{string}@sub{@var{end-2}}
- (@var{kons} @var{string}@sub{@var{end-1}}
- @var{knil})))))
- @end example
- obeying the (tail) recursion
- @example
- (string-fold-right @var{kons} @var{knil} @var{string} @var{start} @var{end})
- = (string-fold-right @var{kons} (@var{kons} @var{string}@sub{@var{end-1}} @var{knil}) @var{start} @var{end-1})
- @end example
- Examples:
- @example
- ;;; Convert a string or string to a list of chars.
- (string-fold-right cons '() string)
- ;;; Count the number of lower-case characters in a string or string.
- (string-fold (lambda (c count)
- (if (char-lower-case? c)
- (+ count 1)
- count))
- 0
- string)
- @end example
- The string-fold-right combinator is sometimes called a "catamorphism."
- @end deffn
- @deffn Procedure string-for-each @var{proc} @vari{string} @varii{string} @dots{}
- @deffnx Procedure string-for-each @var{proc} @vari{string} [start [end]]
- The @var{string}s must all have the same length. @var{proc} should
- accept as many arguments as there are @var{string}s.
- The @var{start}-@var{end} variant is provided for compatibility
- with the SRFI-13 version. (In that case @var{start} and @var{end}
- count code Unicode scalar values (@code{character} values),
- not Java 16-bit @code{char} values.)
- The @func{string-for-each} procedure applies @var{proc} element--wise to
- the characters of the @var{string}s for its side effects, in order from
- the first characters to the last. @var{proc} is always called in the
- same dynamic environment as @func{string-for-each} itself.
- @c The return values of @func{string-for-each} are unspecified.
- Analogous to @func{for-each}.
- @example
- (let ((v '()))
- (string-for-each
- (lambda (c) (set! v (cons (char->integer c) v)))
- "abcde")
- v)
- @result{} (101 100 99 98 97)
- @end example
- @PerformanceNote{} The compiler generates efficient code
- for @code{string-for-each}.
- If @var{proc} is a lambda expression, it is inlined.
- @end deffn
- @deffn Procedure string-map @var{proc} @vari{string} @varii{string} @dots{}
- The @code{string-map} procedure applies @var{proc} element-wise to
- the elements of the strings and returns a string of the results, in order.
- It is an error if @var{proc} does not accept as many arguments as there
- are strings, or return other than a single character or a string.
- If more than one string is given and not all strings have the same length,
- @code{string-map} terminates when the shortest string runs out.
- The dynamic order in
- which @var{proc} is applied to the elements of the strings is unspecified.
- @c If multiple returns occur from string-map, the
- @c values returned by earlier returns are not mutated.
- @example
- (string-map char-foldcase "AbdEgH") @result{} "abdegh"
- @end example
- @example
- (string-map
- (lambda (c) (integer->char (+ 1 (char->integer c))))
- "HAL")
- @result{} "IBM"
- @end example
- @example
- (string-map
- (lambda (c k)
- ((if (eqv? k #\u) char-upcase char-downcase) c))
- "studlycaps xxx"
- "ululululul")
- @result{} "StUdLyCaPs"
- @end example
- Traditionally the result of @var{proc} had to be a character,
- but Kawa (and SRFI-140) allows the result to be a string.
- @PerformanceNote{} The @code{string-map} procedure has not been
- optimized (mainly because it is not very useful):
- The characters are boxed, and the @var{proc} is not inlined even if
- it is a lambda expression.
- @end deffn
- @deffn Procedure string-map-index proc string [start end]
- Calls @var{proc} on each valid index of the specified substring, converts
- the results of those calls into strings, and returns the concatenation
- of those strings. It is an error for @var{proc} to return anything other
- than a character or string. The dynamic order in which proc is called
- on the indexes is unspecified, as is the dynamic order in which the
- coercions are performed. If any strings returned by @var{proc} are mutated
- after they have been returned and before the call to @code{string-map-index}
- has returned, then @code{string-map-index} returns a string with unspecified
- contents; the @var{string-map-index} procedure itself does not mutate those
- strings.
- @end deffn
- @deffn Procedure string-for-each-index proc string [start end]
- Calls @var{proc} on each valid index of the specified substring, in
- increasing order, discarding the results of those calls. This is
- simply a safe and correct way to loop over a substring.
- Example:
- @example
- (let ((txt (string->string "abcde"))
- (v '()))
- (string-for-each-index
- (lambda (cur) (set! v (cons (char->integer (string-ref txt cur)) v)))
- txt)
- v) @result{} (101 100 99 98 97)
- @end example
- @end deffn
- @deffn Procedure string-count string pred [start end]
- Returns a count of the number of characters in the specified substring
- of @var{string} that satisfy the predicate @var{pred}.
- @end deffn
- @deffn Procedure string-filter pred string [start end]
- @deffnx Procedure string-remove pred string [start end]
- Return an immutable string consisting of only selected characters, in order:
- @code{string-filter} selects only the characters that satisfy @var{pred};
- @code{string-remove} selects only the characters that @emph{not}
- satisfy @var{pred}
- @end deffn
- @subsection Replication & splitting
- @deffn Procedure string-repeat string-or-character len
- Create an istring by repeating the first argument @var{len} times.
- If the first argument is a character, it is as if it were wrapped with
- the @code{string} constructor.
- We can define string-repeat in terms of the more general @code{xsubstring}
- procedure:
- @example
- (define (string-repeat S N)
- (let ((T (if (char? S) (string S) S)))
- (xsubstring T 0 (* N (string-length T))))
- @end example
- @end deffn
- @deffn Procedure xsubstring string [from to [start end]]
- This is an extended substring procedure that implements replicated copying of a substring.
- The @var{string} is a string; @var{start} and @var{end} are optional arguments that specify a substring of @var{string},
- defaulting to 0 and the length of @var{string}.
- This substring is conceptually replicated both up and down the index space,
- in both the positive and negative directions.
- For example, if @var{string} is @code{"abcdefg"}, @var{start} is 3,
- and @var{end} is 6, then we have the conceptual bidirectionally-infinite string
- @example
- ... d e f d e f d e f d e f d e f d e f d ...
- -9 -8 -7 -6 -5 -4 -3 -2 -1 0 +1 +2 +3 +4 +5 +6 +7 +8 +9
- @end example
- @code{xsubstring} returns the substring of the @var{string} beginning
- at index @var{from}, and ending at @var{to}.
- It is an error if @var{from} is greater than @var{to}.
- If @var{from} and @var{to} are missing they default to 0 and
- @var{from}+(@var{end}-@var{start}), respectively.
- This variant is a generalization of using @code{substring},
- but unlike @code{substring} never shares substructures that would
- retain characters or sequences of characters that are substructures of
- its first argument or previously allocated objects.
- You can use @code{xsubstring} to perform a variety of tasks:
- @itemize
- @item
- To rotate a string left: @code{(xsubstring "abcdef" 2 8) @result{} "cdefab"}
- @item
- To rotate a string right: @code{(xsubstring "abcdef" -2 4) @result{} "efabcd"}
- @item
- To replicate a string: @code{(xsubstring "abc" 0 7) @result{} "abcabca"}
- @end itemize
- Note that
- @itemize
- @item
- The @var{from}/@var{to} arguments give a half-open range containing the
- characters from index @var{from} up to, but not including, index @var{to}.
- @item
- The @var{from}/@var{to} indexes are not expressed in the index space of
- @var{string}. They refer instead to the replicated index space of the
- substring defined by @var{string}, @var{start}, and @var{end}.
- @end itemize
- It is an error if @var{start}=@var{end}, unless @var{from}=@var{to},
- which is allowed as a special case.
- @end deffn
- @deffn Procedure string-split string delimiter [grammar limit start end]
- Returns a list of strings representing the words contained in the substring of @var{string} from @var{start} (inclusive) to @var{end} (exclusive).
- The @var{delimiter} is a string to be used as the word separator.
- This will often be a single character, but multiple characters are
- allowed for use cases such as splitting on @code{"\r\n"}.
- The returned list will have one more item than the number of non-overlapping
- occurrences of the @var{delimiter} in the string.
- If @var{delimiter} is an empty string, then the returned list contains a
- list of strings, each of which contains a single character.
- The @var{grammar} is a symbol with the same meaning as in
- the @code{string-join} procedure.
- If it is @code{infix}, which is the default, processing is done as
- described above, except an empty string produces the empty list;
- if grammar is @code{strict-infix}, then an empty string signals an error.
- The values @code{prefix} and @code{suffix} cause a leading/trailing empty string in the result to be suppressed.
- If @var{limit} is a non-negative exact integer, at most that many splits occur, and the remainder of string is returned as the final element of the list (so the result will have at most limit+1 elements). If limit is not specified or is #f, then as many splits as possible are made. It is an error if limit is any other value.
- To split on a regular expression, you can use SRFI 115's @code{regexp-split}
- procedure.
- @end deffn
- @subsection String mutation
- The following procedures create a mutable string,
- i.e. one that you can modify.
- @deffn Procedure make-string [@var{k} [@var{char}]]
- Return a newly allocated mstring of @var{k} characters,
- where @var{k} defaults to 0. If @var{char} is
- given, then all elements of the string are initialized to @var{char},
- otherwise the contents of the @var{string} are unspecified.
- The 1-argument version is deprecated as poor style, except when k is 0.
- @emph{Rationale:} In many languags the most common pattern for mutable strings
- is to allocate an empty string and incrementally append to it.
- It seems natural to initialize the string
- with @code{(make-string)}, rather than @code{(make-string 0)}.
- To return an immutable string that repeats @var{k} times a character
- @var{char} use @code{string-repeat}.
- This is as R7RS, except the result is variable-size and we allow
- leaving out @var{k} when it is zero.
- @end deffn
- @deffn Procedure string-copy @var{string} [@var{start} [@var{end}]]
- Returns a newly allocated mutable (mstring) copy of the part of the given
- @var{string} between @var{start} and @var{end}.
- @end deffn
- The following procedures modify a mutable string.
- @deffn Procedure string-set! string k char
- This procedure stores @var{char} in element @var{k} of @var{string}.
- @example
- (define s1 (make-string 3 #\*))
- (define s2 "***")
- (string-set! s1 0 #\?) @result{} @emph{void}
- s1 @result{} "?**"
- (string-set! s2 0 #\?) @result{} @emph{error}
- (string-set! (symbol->string 'immutable) 0 #\?) @result{} @emph{error}
- @end example
- @PerformanceNote{} Calling @code{string-set!} may take time proportional
- to the length of the string: First it must scan for the right position,
- like @code{string-ref} does. Then if the new character requires
- using a surrogate pair (and the old one doesn't) then we have to make room
- in the string, possibly re-allocating a new @code{char} array.
- Alternatively, if the old character requires using a surrogate pair
- (and the new one doesn't) then following characters need to be moved.
- The function @code{string-set!} is deprecated: It is inefficient,
- and it very seldom does the correct thing. Instead, you can
- construct a string with @code{string-append!}.
- @end deffn
- @deffn Procedure string-append! @var{string} @var{value} @dots{}
- The @var{string} must be a mutable string, such as one returned
- by @code{make-string} or @code{string-copy}.
- The @code{string-append!} procedure extends @var{string}
- by appending each @var{value} (in order) to the end of @var{string}.
- Each @code{value} should be a character or a string.
- @PerformanceNote{} The compiler converts a call with multiple @var{value}s
- to multiple @code{string-append!} calls.
- If a @var{value} is known to be a @code{character}, then
- no boxing (object-allocation) is needed.
- The following example shows how to efficiently process a string
- using @code{string-for-each} and incrementally ``build'' a result string
- using @code{string-append!}.
- @example
- (define (translate-space-to-newline str::string)::string
- (let ((result (make-string 0)))
- (string-for-each
- (lambda (ch)
- (string-append! result
- (if (char=? ch #\Space) #\Newline ch)))
- str)
- result))
- @end example
- @end deffn
- @deffn Procedure string-copy! @var{to} @var{at} @var{from} [@var{start} [@var{end}]]
- Copies the characters of the string @var{from} that are between
- @var{start} end @var{end} into the string @var{to},
- starting at index @var{at}. The order in which characters are copied
- is unspecified, except that if the source and destination overlap,
- copying takes place as if the source is first copied into a
- temporary string and then into the destination.
- (This is achieved without allocating storage by making sure to copy in
- the correct direction in such circumstances.)
- This is equivalent to (and implemented as):
- @example
- (string-replace! to at (+ at (- end start)) from start end))
- @end example
- @example
- (define a "12345")
- (define b (string-copy "abcde"))
- (string-copy! b 1 a 0 2)
- b @result{} "a12de"
- @end example
- @end deffn
- @deffn Procedure string-replace! @var{dst} @var{dst-start} @var{dst-end} @var{src} [@var{src-start} [@var{src-end}]]
- Replaces the characters of string @var{dst} (between @var{dst-start} and @var{dst-end}) with the characters of @var{src} (between @var{src-start} and @var{src-end}).
- The number of characters from @var{src} may be different than the
- number replaced in @var{dst}, so the string may grow or contract.
- The special case where @var{dst-start} is equal to @var{dst-end} corresponds to
- insertion; the case where @var{src-start} is equal to @var{src-end}
- corresponds to deletion.
- The order in which characters are copied
- is unspecified, except that if the source and destination overlap,
- copying takes place as if the source is first copied into a
- temporary string and then into the destination.
- (This is achieved without allocating storage by making sure to copy in
- the correct direction in such circumstances.)
- @end deffn
- @deffn Procedure string-fill! @var{string} @var{fill} [@var{start} [@var{end}]]
- The @code{string-fill!} procedure stores @var{fill} in the elements
- of @var{string} between @var{start} and @var{end}.
- It is an error if @var{fill} is not a character or is forbidden in strings.
- @end deffn
- @subsection Strings as sequences
- @subsubsection Indexing a string
- Using function-call syntax with strings is convenient
- and efficient. However, it has some ``gotchas''.
- We will use the following example string:
- @example
- (! str1 "Smile \x1f603;!")
- @end example
- or if you're brave:
- @example
- (! str1 "Smile 😃!")
- @end example
- This is @code{"Smile "} followed by an emoticon (``smiling face with
- open mouth'') followed by @code{"!"}.
- The emoticon has scalar value @code{\x1f603} - it is not
- in the 16-bit Basic Multi-language Plane,
- and so it must be encoded by a surrogate pair
- (@code{#\xd83d} followed by @code{#\xde03}).
- The number of scalar values (@code{character}s) is 8,
- while the number of 16-bits code units (@code{char}s) is 9.
- The @code{java.lang.CharSequence:length} method
- counts @code{char}s. Both the @code{length} and the
- @code{string-length} procedures count @code{character}s. Thus:
- @example
- (length str1) @result{} 8
- (string-length str1) @result{} 8
- (str1:length) @result{} 9
- @end example
- Counting @code{char}s is a constant-time operation (since it
- is stored in the data structure).
- Counting @code{character}s depends on the representation used:
- In geneeral it may take time proportional to the length of
- the string, since it has to subtract one for each surrogate pair;
- however the @var{istring} type (@code{gnu.lists.IString} class)
- uses a extra structure so it can count characters in constant-time.
- Similarly we can can index the string in 3 ways:
- @example
- (str1 1) @result{} #\m :: character
- (string-ref str1 1) @result{} #\m :: character
- (str1:charAt 1) @result{} #\m :: char
- @end example
- Using function-call syntax when the ``function'' is a string
- and a single integer argument is the same as using @code{string-ref}.
- Things become interesting when we reach the emoticon:
- @example
- (str1 6) @result{} #\😃 :: character
- (str1:charAt 6) @result{} #\d83d :: char
- @end example
- Both @code{string-ref} and the function-call syntax return the
- real character, while the @code{charAt} methods returns a partial character.
- @example
- (str1 7) @result{} #\! :: character
- (str1:charAt 7) @result{} #\de03 :: char
- (str1 8) @result{} @i{throws} StringIndexOutOfBoundsException
- (str1:charAt 8) @result{} #\! :: char
- @end example
- @subsubsection Indexing with a sequence
- You can index a string with a list of integer indexes,
- most commonly a range:
- @example
- (@var{str} [@var{i} ...])
- @end example
- is basically the same as:
- @example
- (string (@var{str} @var{i}) ...)
- @end example
- Generally when working with strings it is best to
- work with substrings rather than individual characters:
- @example
- (@var{str} [@var{start} <: @var{end}])
- @end example
- This is equivalent to invoking the @code{substring} procedure:
- @example
- (substring @var{str} @var{start} @var{end})
- @end example
- @c not implemented yet
- @c @subsubsection Assigning to a subsequence
- @anchor{String Cursor API}
- @subsection String Cursor API
- Indexing into a string (using for example @code{string-ref})
- is inefficient because of the possible presence of surrogate pairs.
- Hence given an index @var{i} access normally requires linearly
- scanning the string until we have seen @var{i} characters.
- The string-cursor API is defined in terms of abstract ``cursor values'',
- which point to a position in the string. This avoids the linear scan.
- Typical usage is:
- @example
- (let* ((str @var{whatever})
- (end (string-cursor-end str)))
- (do ((sc::string-cursor (string-cursor-start str)
- (string-cursor-next str sc)))
- ((string-cursor>=? sc end))
- (let ((ch (string-cursor-ref str sc)))
- (@var{do-something-with} ch))))
- @end example
- Alternatively, the following may be marginally faster:
- @example
- (let* ((str @var{whatever})
- (end (string-cursor-end str)))
- (do ((sc::string-cursor (string-cursor-start str)
- (string-cursor-next-quick sc)))
- ((string-cursor>=? sc end))
- (let ((ch (string-cursor-ref str sc)))
- (if (not (char=? ch #\ignorable-char))
- (@var{do-something-with} ch)))))
- @end example
- The API is non-standard, but is based on that in Chibi Scheme.
- @deffn Type string-cursor
- An abstract position (index) in a string.
- Implemented as a primitive @code{int} which counts the
- number of preceding code units (16-bit @code{char} values).
- @end deffn
- @deffn Procedure string-cursor-start str
- Returns a cursor for the start of the string.
- The result is always 0, cast to a @code{string-cursor}.
- @end deffn
- @deffn Procedure string-cursor-end str
- Returns a cursor for the end of the string - one past the last valid character.
- Implemented as @code{(as string-cursor (invoke @var{str} 'length))}.
- @end deffn
- @deffn Procedure string-cursor-ref str cursor
- Return the @code{character} at the @var{cursor}.
- If the @var{cursor} points to the second @code{char} of a surrogate pair,
- returns @code{#\ignorable-char}.
- @end deffn
- @deffn Procedure string-cursor-next string cursor [count]
- Return the cursor position @var{count} (default 1) character
- positions forwards beyond @var{cursor}.
- For each @var{count} this may add either 1 or 2
- (if pointing at a surrogate pair) to the @var{cursor}.
- @end deffn
- @deffn Procedure string-cursor-next-quiet cursor
- Increment cursor by one raw @code{char} position,
- even if @var{cursor} points to the start of a surrogate pair.
- (In that case the next @code{string-cursor-ref} will
- return @code{#\ignorable-char}.)
- Same as @code{(+ @var{cursor} 1)} but with the @code{string-cursor} type.
- @end deffn
- @deffn Procedure string-cursor-prev string cursor [count]
- Return the cursor position @var{count} (default 1) character
- positions backwards before @var{cursor}.
- @end deffn
- @deffn Procedure substring-cursor string [start [end]]
- Create a substring of the section of @var{string}
- between the cursors @var{start} and @var{end}.
- @end deffn
- @deffn Procedure string-cursor<? cursor1 cursor2
- @deffnx Procedure string-cursor<=? cursor1 cursor2
- @deffnx Procedure string-cursor=? cursor1 cursor2
- @deffnx Procedure string-cursor>=? cursor1 cursor2
- @deffnx Procedure string-cursor>? cursor1 cursor2
- Is the position of @var{cursor1} respectively before,
- before or same, same, after, or after or same, as @var{cursor2}.
- @PerformanceNote{} Implemented as the corresponding @code{int} comparison.
- @end deffn
- @c non-chibi
- @deffn Procedure string-cursor-for-each proc string [start [end]]
- Apply the procedure @var{proc} to each character position in
- @var{string} between the cursors @var{start} and @var{end}.
- @end deffn
- @node String literals
- @section String literals
- Kaw support two syntaxes of string literals:
- The traditional, portable, qdouble-quoted-delimited literals
- like @code{"this"};
- and extended SRFI-109 quasi-literals like @code{&@{this@}}.
- @subsection Simple string literals
- @display
- @stxdef{string} @stxlit{"}@arbno{@stxref{string-element}}@stxlit{"}
- @stxdef{string-element} @i{any character other than} @stxlit{"} @i{or} @stxlit{@backslashchar{}}
- | @stxref{mnemonic-escape} | @stxlit{@backslashchar{}"} | @stxlit{@backslashchar{}@backslashchar{}}
- | @stxlit{@backslashchar{}}@arbno{@stxref{intraline-whitespace}}@stxref{line-ending} @arbno{@stxref{intraline-whitespace}}
- | @stxref{inline-hex-escape}
- @stxdef{mnemonic-escape} @stxlit{@backslashchar{}a} | @stxlit{@backslashchar{}b} | @stxlit{@backslashchar{}t} | @stxlit{@backslashchar{}n} | @stxlit{@backslashchar{}r} | ... @i{(see below)}
- @end display
- A string is written as a sequence of characters enclosed
- within quotation marks (@stxlit{"}).
- Within a string literal, various escape sequence represent characters
- other than themselves.
- Escape sequences always start with a backslash (@stxlit{@backslashchar{}}):
- @table @asis
- @item @stxlit{@backslashchar{}a}
- Alarm (bell), @code{#\x0007}.
- @item @stxlit{@backslashchar{}b}
- Backspace, @code{#\x0008}.
- @item @stxlit{@backslashchar{}e}
- Escape, @code{#\x001B}.
- @item @stxlit{@backslashchar{}f}
- Form feed, @code{#\x000C}.
- @item @stxlit{@backslashchar{}n}
- Linefeed (newline), @code{#\x000A}.
- @item @stxlit{@backslashchar{}r}
- Return, @code{#\x000D}.
- @item @stxlit{@backslashchar{}t}
- Character tabulation, @code{#\x0009}.
- @item @stxlit{@backslashchar{}v}
- Vertical tab, @code{#\x000B}.
- @item @stxlit{@backslashchar{}C-}@meta{x}
- @itemx @stxlit{@backslashchar{}^}@meta{x}
- Returns the scalar value of @meta{x} masked (anded) with @code{#x9F}.
- An alternative way to write the Ascii control characters:
- For example @code{"\C-m"} or @code{"\^m"} is
- the same as @code{"#\x000D"} (which the same as @code{"\r"}).
- As a special case @code{\^?} is rubout (delete) (@code{\x7f;}).
- @item @stxlit{@backslashchar{}x} @stxref{hex-scalar-value}@stxlit{;}
- @itemx @stxlit{@backslashchar{}X} @stxref{hex-scalar-value}@stxlit{;}
- A hex encoding that gives the scalar value of a character.
- @item @stxlit{@backslashchar{}@backslashchar{}} @atleastone{@stxref{oct-digit}}
- At most three octal digits that give the scalar value of a character.
- (Historical, for C compatibility.)
- @item @stxlit{@backslashchar{}u} @atleastone{@stxref{hex-digit}}
- Exactly four hex digits that give the scalar value of a character.
- (Historical, for Java compatibility.)
- @item @stxlit{@backslashchar{}M-}@meta{x}
- (Historical, for Emacs Lisp.)
- Set the meta-bit (high-bit of single byte) of the following character @var{x}.
- @item @stxlit{@backslashchar{}|}
- Vertical line, @code{#\x007c}.
- (Not useful for string literals, but useful for symbols.)
- @item @stxlit{@backslashchar{}"}
- Double quote, @code{#\x0022}.
- @item @stxlit{@backslashchar{}@backslashchar{}}
- Backslah, @code{#\005C}.
- @item @stxlit{@backslashchar{}}@arbno{@stxref{intraline-whitespace}}@stxref{line-ending} @arbno{@stxref{intraline-whitespace}}
- Nothing (ignored). Allows you to split up a long string over multiple
- lines; ignoring initial whitespace on the continuation lines allows
- you to indent them.
- @end table
- Except for a line ending, any character outside of an escape
- sequence stands for itself in the string literal. A line ending
- which is preceded by @stxlit{@backslashchar{}}@arbno{@meta{intraline-whitespace}}
- expands to nothing (along with any trailing @meta{intraline-whitespace}),
- and can be used to indent strings for improved legibility.
- Any other line ending has the same effect as inserting a @stxlit{@backslashchar{}n}
- character into the string.
- Examples:
- @example
- "The word \"recursion\" has many meanings."
- "Another example:\ntwo lines of text"
- "Here’s text \
- containing just one line"
- "\x03B1; is named GREEK SMALL LETTER ALPHA."
- @end example
- @anchor{string quasi-literals}
- @subsection String templates
- The following syntax is a @dfn{string template} (also called
- a string quasi-literal or
- ``@uref{http://en.wikipedia.org/wiki/Here_document, here document}''):
- @example
- &@{Hello &[name]!@}
- @end example
- Assuming the variable @code{name} evaluates to @code{"John"}
- then the example evaluates to @code{"Hello John!"}.
- The Kawa reader converts the above example to:
- @example
- ($string$ "Hello " $<<$ name $>>$ "!")
- @end example
- See the @uref{http://srfi.schemers.org/srfi-109/srfi-109.html,SRFI-109}
- specification for details.
- @display
- @stxdef{extended-string-literal} @stxlit{&@lbracechar{}} [@stxref{initial-ignored}] @arbno{@stxref{string-literal-part}} @stxlit{@rbracechar{}}
- @stxdef{string-literal-part} @i{any character except} @stxlit{&}, @stxlit{@lbracechar{}} @i{or} @stxlit{@rbracechar{}}
- | @stxlit{@lbracechar{}} @arbno{@stxref{string-literal-part}} @stxlit{@rbracechar{}}
- | @stxref{char-ref}
- | @stxref{entity-ref}
- | @stxref{special-escape}
- | @stxref{enclosed-part}
- @end display
- You can use the plain @code{"@var{string}"} syntax for
- longer multiline strings, but @code{&@{@var{string}@}} has
- various advantages.
- The syntax is less error-prone because the start-delimiter is
- different from the end-delimiter. Also note that nested braces
- are allowed: a right brace @code{@rbracechar{}} is only an end-delimiter
- if it is unbalanced, so you would seldom need to escape it:
- @example
- &@{This has a @{braced@} section.@}
- @result{} "This has a @{braced@} section."
- @end example
- The escape character used for special characters is
- @stxlit{&}. This is compatible with XML syntax and @ref{XML literals}.
- @subsubsection Special characters
- @display
- @stxdef{char-ref}
- @stxlit{&#} @atleastone{@stxref{digit}} @stxlit{;}
- | @stxlit{&#x} @atleastone{@stxref{hex-digit}} @stxlit{;}
- @stxdef{entity-ref}
- @stxlit{&} @stxref{char-or-entity-name} @stxlit{;}
- @stxdef{char-or-entity-name} @meta{tagname}
- @end display
- You can the standard XML syntax for character references, using
- either decimal or hexadecimal values. The following string has two
- instances of the Ascii escape character, as either decimal 27 or hex 1B:
- @example
- &@{@} @result{} "\e\e"
- @end example
- You can also use the pre-defined XML entity names:
- @example
- &@{& < > " '@} @result{} "& < > \" '"
- @end example
- In addition, @code{{} @code{}} can be used for left and
- right curly brace, though you don't need them for balanced parentheses:
- @example
- &@{ }_{ / @{_@} @} @result{} " @}_@{ / @{_@} "
- @end example
- You can use the @uref{http://www.w3.org/2003/entities/2007/w3centities-f.ent,standard XML entity names}. For example:
- @example
- &@{Lærdalsøyri@}
- @result{} "Lærdalsøyri"
- @end example
- You can also use the standard R7RS character names @code{null},
- @code{alarm}, @code{backspace}, @code{tab}, @code{newline}, @code{return},
- @code{escape}, @code{space}, and @code{delete}.
- For example:
- @example
- &@{&escape;&space;@}
- @end example
- The syntax @code{&@var{name};} is actually syntactic sugar
- (specifically reader syntax) to the variable reference
- @code{$entity$:@var{name}}.
- Hence you can also define your own entity names:
- @example
- (define $entity$:crnl "\r\n")
- &@{&crnl;@} ⟹ "\r\n"
- @end example
- @subsubsection Multiline string literals
- @example
- @stxdef{initial-ignored}
- @arbno{@stxref{intraline-whitespace}} @stxref{line-ending} @arbno{@stxref{intraline-whitespace}} @stxlit{&|}
- @stxdef{special-escape}
- @arbno{@stxref{intraline-whitespace}} @stxlit{&|}
- | @stxlit{&} @stxref{nested-comment}
- | @stxlit{&-} @arbno{@stxref{intraline-whitespace}} @stxref{line-ending}
- @end example
- A line-ending directly in the text is becomes a newline,
- as in a simple string literal:
- @example
- (string-capitalize &@{one two three
- uno dos tres
- @}) @result{} "One Two Three\nUno Dos Tres\n"
- @end example
- However, you have extra control over layout.
- If the string is in a nested expression, it is confusing
- (and ugly) if the string cannot be indented to match
- the surrounding context. The indentation marker @stxlit{&|}
- is used to mark the end of insignificant initial whitespace.
- The @code{&|} characters and all the preceding whitespace are removed.
- In addition, it also suppresses an initial newline. Specifically,
- when the initial left-brace is followed by optional (invisible)
- intraline-whitespace, then a newline, then optional
- intraline-whitespace (the indentation), and finally the indentation
- marker @code{&|} - all of which is removed from the output.
- Otherwise the @code{&|} only removes initial intraline-whitespace
- on the same line (and itself).
- @example
- (write (string-capitalize &@{
- &|one two three
- &|uno dos tres
- @}) out)
- @result{} @i{prints} "One Two Three\nUno Dos Tres\n"
- @end example
- As a matter of style, all of the indentation lines should line up.
- It is an error if there are any non-whitespace characters between
- the previous newline and the indentation marker.
- It is also an error to write an indentation marker before the
- first newline in the literal.
- The line-continuation marker @stxlit{&-} is used to suppress a newline:
- @example
- &@{abc&-
- def@} @result{} "abc def"
- @end example
- You can write a @code{#|...|#}-style comment following a @code{&}.
- This could be useful for annotation, or line numbers:
- @example
- &@{&#|line 1|#one two
- &#|line 2|# three
- &#|line 3|#uno dos tres
- @} @result{} "one two\n three\nuno dos tres\n"
- @end example
- @subsubsection Embedded expressions
- @example
- @stxdef{enclosed-part}
- @stxlit{&} @arbno{@stxref{enclosed-modifier}} @stxlit{[} @arbno{@stxref{expression}} @stxlit{]}
- | @stxlit{&} @arbno{@stxref{enclosed-modifier}} @stxlit{(} @atleastone{@stxref{expression}} @stxlit{)}
- @end example
- An embedded expression has the form @code{&[@var{expression}]}.
- It is evaluated, the result converted to a string (as by @code{display}),
- and the result added in the result string.
- (If there are multiple expressions, they are all evaluated and
- the corresponding strings inserted in the result.)
- @example
- &@{Hello &[(string-capitalize name)]!@}
- @end example
- You can leave out the square brackets when the expression
- is a parenthesized expression:
- @example
- &@{Hello &(string-capitalize name)!@}
- @end example
- @subsubsection Formatting
- @example
- @stxdef{enclosed-modifier}
- @stxlit{~} @meta{format-specifier-after-tilde}
- @end example
- Using @ref{Format,@code{format}} allows finer-grained control over the
- output, but a problem is that the association between format
- specifiers and data expressions is positional, which is hard-to-read
- and error-prone. A better solution places the specifier adjacant to
- the data expression:
- @example
- &@{The response was &~,2f(* 100.0 (/ responses total))%.@}
- @end example
- The following escape forms are equivalent to the corresponding
- forms withput the @stxlit{~}@meta{fmt-spec}, except the
- expression(s) are formatted using @code{format}:
- @display
- @stxlit{&~}@meta{fmt-spec}@stxlit{[}@arbno{@meta{expression}}@stxlit{]}
- @end display
- Again using parentheses like this:
- @display
- @stxlit{&~}@meta{fmt-spec}@stxlit{(}@atleastone{@meta{expression}}@stxlit{)}
- @end display
- is equivalent to:
- @display
- @stxlit{&~}@meta{fmt-spec}@stxlit{[(}@atleastone{@meta{expression}}@stxlit{)]}
- @end display
- The syntax of @code{format} specifications is arcane, but it allows you
- to do some pretty neat things in a compact space.
- For example to include @code{"_"} between each element of
- the array @code{arr} you can use the @code{~@{...~@}} format speciers:
- @example
- (define arr [5 6 7])
- &@{&~@{&[arr]&~^_&~@}@} @result{} "5_6_7"
- @end example
- If no format is specified for an enclosed expression,
- the that is equivalent to a @code{~a} format specifier,
- so this is equivalent to:
- @example
- &@{&~@{&~a[arr]&~^_&~@}@} @result{} "5_6_7"
- @end example
- which is in turn equivalent to:
- @example
- (format #f "~@{~a~^_~@}" arr)
- @end example
- The fine print that makes this work:
- If there are multiple expressions in a @code{&[...]} with
- no format specifier then there is an implicit @code{~a} for
- each expression.
- On the other hand, if there is an explicit format specifier,
- it is not repeated for each enclosed expression: it appears
- exactly once in the effective format string, whether
- there are zero, one, or many expressions.
- @node Unicode
- @section Unicode character classes and conversions
- @ignore
- The procedures exported by the @rsixlibrary{unicode} library provide
- access to some aspects of the Unicode semantics for characters and
- strings: category information, case--independent comparisons, case
- mappings, and normalization.
- @end ignore
- Some of the procedures that operate on characters or strings ignore the
- difference between upper case and lower case. These procedures have
- @code{-ci} (for ``case insensitive'') embedded in their names.
- @subsection Characters
- @deffn Procedure char-upcase @var{char}
- @deffnx Procedure char-downcase @var{char}
- @deffnx Procedure char-titlecase @var{char}
- @deffnx Procedure char-foldcase @var{char}
- These procedures take a character argument and return a character
- result.
- If the argument is an upper--case or title--case character, and if there
- is a single character that is its lower--case form, then
- @func{char-downcase} returns that character.
- If the argument is a lower--case or title--case character, and there is
- a single character that is its upper--case form, then @func{char-upcase}
- returns that character.
- If the argument is a lower--case or upper--case character, and there is
- a single character that is its title--case form, then
- @func{char-titlecase} returns that character.
- If the argument is not a title--case character and there is no single
- character that is its title--case form, then @func{char-titlecase}
- returns the upper--case form of the argument.
- Finally, if the character has a case--folded character, then
- @func{char-foldcase} returns that character. Otherwise the character
- returned is the same as the argument.
- For Turkic characters @code{#\x130} and @code{#\x131},
- @func{char-foldcase} behaves as the identity function; otherwise
- @func{char-foldcase} is the same as @func{char-downcase} composed with
- @func{char-upcase}.
- @example
- (char-upcase #\i) @result{} #\I
- (char-downcase #\i) @result{} #\i
- (char-titlecase #\i) @result{} #\I
- (char-foldcase #\i) @result{} #\i
- (char-upcase #\ß) @result{} #\ß
- (char-downcase #\ß) @result{} #\ß
- (char-titlecase #\ß) @result{} #\ß
- (char-foldcase #\ß) @result{} #\ß
- (char-upcase #\Σ) @result{} #\Σ
- (char-downcase #\Σ) @result{} #\σ
- (char-titlecase #\Σ) @result{} #\Σ
- (char-foldcase #\Σ) @result{} #\σ
- (char-upcase #\ς) @result{} #\Σ
- (char-downcase #\ς) @result{} #\ς
- (char-titlecase #\ς) @result{} #\Σ
- (char-foldcase #\ς) @result{} #\σ
- @end example
- @quotation
- @emph{Note:} @func{char-titlecase} does not always return a title--case
- character.
- @end quotation
- @quotation
- @emph{Note:} These procedures are consistent with Unicode's
- locale--independent mappings from scalar values to scalar values for
- upcase, downcase, titlecase, and case--folding operations. These
- mappings can be extracted from @file{UnicodeData.txt} and
- @file{CaseFolding.txt} from the Unicode Consortium, ignoring Turkic
- mappings in the latter.
- Note that these character--based procedures are an incomplete
- approximation to case conversion, even ignoring the user's locale. In
- general, case mappings require the context of a string, both in
- arguments and in result. The @func{string-upcase},
- @func{string-downcase}, @func{string-titlecase}, and
- @func{string-foldcase} procedures perform more general case conversion.
- @end quotation
- @end deffn
- @deffn Procedure char-ci=? @vari{char} @varii{char} @variii{char} @dots{}
- @deffnx Procedure char-ci<? @vari{char} @varii{char} @variii{char} @dots{}
- @deffnx Procedure char-ci>? @vari{char} @varii{char} @variii{char} @dots{}
- @deffnx Procedure char-ci<=? @vari{char} @varii{char} @variii{char} @dots{}
- @deffnx Procedure char-ci>=? @vari{char} @varii{char} @variii{char} @dots{}
- These procedures are similar to @func{char=?}, etc., but operate on the
- case--folded versions of the characters.
- @example
- (char-ci<? #\z #\Z) @result{} #f
- (char-ci=? #\z #\Z) @result{} #f
- (char-ci=? #\ς #\σ) @result{} #t
- @end example
- @end deffn
- @deffn Procedure char-alphabetic? @var{char}
- @deffnx Procedure char-numeric? @var{char}
- @deffnx Procedure char-whitespace? @var{char}
- @deffnx Procedure char-upper-case? @var{char}
- @deffnx Procedure char-lower-case? @var{char}
- @deffnx Procedure char-title-case? @var{char}
- These procedures return @true{} if their arguments are alphabetic,
- numeric, whitespace, upper--case, lower--case, or title--case
- characters, respectively; otherwise they return @false{}.
- A character is alphabetic if it has the Unicode ``Alphabetic'' property.
- A character is numeric if it has the Unicode ``Numeric'' property. A
- character is whitespace if has the Unicode ``White_Space'' property. A
- character is upper case if it has the Unicode ``Uppercase'' property,
- lower case if it has the ``Lowercase'' property, and title case if it is
- in the Lt general category.
- @example
- (char-alphabetic? #\a) @result{} #t
- (char-numeric? #\1) @result{} #t
- (char-whitespace? #\space) @result{} #t
- (char-whitespace? #\x00A0) @result{} #t
- (char-upper-case? #\Σ) @result{} #t
- (char-lower-case? #\σ) @result{} #t
- (char-lower-case? #\x00AA) @result{} #t
- (char-title-case? #\I) @result{} #f
- (char-title-case? #\x01C5) @result{} #t
- @end example
- @end deffn
- @deffn Procedure char-general-category @var{char}
- Return a symbol representing the Unicode general category of
- @var{char}, one of @code{Lu}, @code{Ll}, @code{Lt}, @code{Lm},
- @code{Lo}, @code{Mn}, @code{Mc}, @code{Me}, @code{Nd}, @code{Nl},
- @code{No}, @code{Ps}, @code{Pe}, @code{Pi}, @code{Pf}, @code{Pd},
- @code{Pc}, @code{Po}, @code{Sc}, @code{Sm}, @code{Sk}, @code{So},
- @code{Zs}, @code{Zp}, @code{Zl}, @code{Cc}, @code{Cf}, @code{Cs},
- @code{Co}, or @code{Cn}.
- @example
- (char-general-category #\a) @result{} Ll
- (char-general-category #\space) @result{} Zs
- (char-general-category #\x10FFFF) @result{} Cn
- @end example
- @end deffn
- @subsection Deprecated in-place case modification
- The following functions are deprecated; they really don't
- and cannot do the right thing, because in some languages
- upper and lower case can use different number of characters.
- @deffn Procedure string-upcase! str
- @emph{Deprecated:} Destructively modify @var{str}, replacing the letters
- by their upper-case equivalents.
- @end deffn
- @deffn Procedure string-downcase! str
- @emph{Deprecated:} Destructively modify @var{str}, replacing the letters
- by their upper-lower equivalents.
- @end deffn
- @deffn Procedure string-capitalize! str
- @emph{Deprecated:} Destructively modify @var{str}, such that the letters that start a new word
- are replaced by their title-case equivalents, while non-initial letters
- are replaced by their lower-case equivalents.
- @end deffn
- @node Regular expressions, , Unicode, Characters and text
- @section Regular expressions
- Kawa provides @dfn{regular expressions}, which is a convenient
- mechanism for matching a string against a @dfn{pattern}
- and maybe replacing matching parts.
- A regexp is a string that describes a pattern. A regexp matcher tries
- to match this pattern against (a portion of) another string, which we
- will call the text string. The text string is treated as raw text and
- not as a pattern.
- Most of the characters in a regexp pattern are meant to match
- occurrences of themselves in the text string. Thus, the pattern ``@code{abc}''
- matches a string that contains the characters ``@code{a}'', ``@code{b}'',
- ``@code{c}'' in succession.
- In the regexp pattern, some characters act as @dfn{metacharacters},
- and some character sequences act as @dfn{metasequences}. That is, they
- specify something other than their literal selves. For example, in the
- pattern ``@code{a.c}'', the characters ``@code{a}'' and ``@code{c}'' do stand
- for themselves but the metacharacter ``@code{.}'' can match any character
- (other than newline). Therefore, the pattern ``@code{a.c}'' matches an
- ``@code{a}'', followed by any character, followed by a ``@code{c}''.
- If we needed to match the character ``@code{.}'' itself, we @dfn{escape}
- it, ie, precede it with a backslash ``@code{\}''. The character sequence
- ``@code{\.}'' is thus a metasequence, since it doesn’t match itself but
- rather just ``@code{.}''. So, to match ``@code{a}'' followed by a literal
- ``@code{.}'' followed by ``@code{c}'' we use the regexp pattern
- ``@code{a\.c}''. To write this as a Scheme string literal,
- you need to quote the backslash, so you need to write @code{"a\\.c"}.
- Kawa also allows the literal syntax @code{#/a\.c/},
- which avoids the need to double the backslashes.
- You can choose between two similar styles of regular expressions.
- The two differ slightly in terms of which characters act as metacharacters,
- and what those metacharacters mean:
- @itemize
- @item
- Functions starting with @code{regex-} are implemented using
- the @code{java.util.regex} package.
- This is likely to be more efficient, has better Unicode support and
- some other minor extra features, and literal syntax @code{#/a\.c/}
- mentioned above.
- @item
- Functions starting with @code{pregexp-} are implemented in pure Scheme
- using Dorai Sitaram's ``Portable Regular Expressions for Scheme'' library.
- These will be portable to more Scheme implementations, including BRL,
- and is available on older Java versions.
- @end itemize
- @subsection Java regular expressions
- The syntax for regular expressions is
- @uref{http://java.sun.com/javase/6/docs/api/java/util/regex/Pattern.html, documented here}.
- @deffn Type regex
- A compiled regular expression,
- implemented as @code{java.util.regex.Pattern}.
- @end deffn
- @deffn Constructor regex arg
- Given a regular expression pattern (as a string),
- compiles it to a @code{regex} object.
- @example
- (regex "a\\.c")
- @end example
- This compiles into a pattern that matches an
- ``@code{a}'', followed by any character, followed by a ``@code{c}''.
- @end deffn
- The Scheme reader recognizes ``@code{#/}'' as the start of a
- regular expression @dfn{pattern literal}, which ends with the next
- un-escaped ``@code{/}''.
- This has the big advantage that you don't need to double the backslashes:
- @example
- #/a\.c/
- @end example
- This is equivalent to @code{(regex "a\\.c")}, except it is
- compiled at read-time.
- If you need a literal ``@code{/}'' in a pattern, just escape it
- with a backslash: ``@code{#/a\/c/}'' matches a ``@code{a}'',
- followed by a ``@code{/}'', followed by a ``@code{c}''.
- You can add single-letter @emph{modifiers} following the pattern literal.
- The following modifiers are allowed:
- @table @code
- @item i
- The modifier ``@code{i}'' cause the matching to ignore case.
- For example the following pattern matches ``@code{a}'' or ``@code{A}''.
- @example
- #/a/i
- @end example
- @item m
- Enables ``metaline'' mode.
- Normally metacharacters ``@code{^}'' and ``@code{$}'
- match at the start end end of the entire input string.
- In metaline mode ``@code{^}'' and ``@code{$}'' also
- match just before or after a line terminator.
- Multiline mode can also be enabled by the metasequence ``@code{(?m)}''.
- @item s
- Enable ``singleline'' (aka ``dot-all'') mode.
- In this mode the matacharacter ``@code{.} matches any character,
- including a line breaks.
- This mode be enabled by the metasequence ``@code{(?s)}''.
- @end table
- The following functions accept a regex either as
- a pattern string or a compiled @code{regex} pattern.
- I.e. the following are all equivalent:
- @example
- (regex-match "b\\.c" "ab.cd")
- (regex-match #/b\.c/ "ab.cd")
- (regex-match (regex "b\\.c") "ab.cd")
- (regex-match (java.util.regex.Pattern:compile "b\\.c") "ab.cd")
- @end example
- These all evaluate to the list @code{("b.c")}.
- The following functions must be imported by doing one of:
- @example
- (require 'regex) ;; or
- (import (kawa regex))
- @end example
- @deffn Procedure regex-match-positions regex string [start [end]]
- The procedure @code{regex‑match‑position} takes pattern and a
- text @var{string}, and returns a match if the regex matches (some part of)
- the text string.
- Returns @code{#f} if the regexp did not match the string;
- and a list of index pairs if it did match.
- @example
- (regex-match-positions "brain" "bird") @result{} #f
- (regex-match-positions "needle" "hay needle stack")
- @result{} ((4 . 10))
- @end example
- In the second example, the integers 4 and 10 identify the substring
- that was matched. 4 is the starting (inclusive) index and 10 the
- ending (exclusive) index of the matching substring.
- @example
- (substring "hay needle stack" 4 10) @result{} "needle"
- @end example
- In this case the return list contains only one index
- pair, and that pair represents the entire substring matched by the
- regexp. When we discuss subpatterns later, we will see how a single
- match operation can yield a list of submatches.
- @code{regex‑match‑positions} takes optional third and fourth arguments
- that specify the indices of the text string within which the matching
- should take place.
- @example
- (regex-match-positions "needle"
- "his hay needle stack -- my hay needle stack -- her hay needle stack"
- 24 43)
- @result{} ((31 . 37))
- @end example
- Note that the returned indices are still reckoned relative to the full text string.
- @end deffn
- @deffn Procedure regex-match regex string [start [end]]
- The procedure @code{regex‑match} is called like @code{regex‑match‑positions}
- but instead of returning index pairs it returns the matching substrings:
- @example
- (regex-match "brain" "bird") @result{} #f
- (regex-match "needle" "hay needle stack")
- @result{} ("needle")
- @end example
- @code{regex‑match} also takes optional third and fourth arguments,
- with the same meaning as does @code{regex‑match‑positions}.
- @end deffn
- @deffn Procedure regex-split regex string
- Takes two arguments, a @var{regex} pattern and a text @var{string},
- and returns a list of substrings of the text string,
- where the pattern identifies the delimiter separating the substrings.
- @example
- (regex-split ":" "/bin:/usr/bin:/usr/bin/X11:/usr/local/bin")
- @result{} ("/bin" "/usr/bin" "/usr/bin/X11" "/usr/local/bin")
- (regex-split " " "pea soup")
- @result{} ("pea" "soup")
- @end example
- If the first argument can match an empty string, then the list of all
- the single-character substrings is returned, plus we get
- a empty strings at each end.
- @example
- (regex-split "" "smithereens")
- @result{} ("" "s" "m" "i" "t" "h" "e" "r" "e" "e" "n" "s" "")
- @end example
- (Note: This behavior is different from @code{pregexp-split}.)
- To identify one-or-more spaces as the delimiter, take care to use the regexp
- ``@code{ +}'', not ``@code{ *}''.
- @example
- (regex-split " +" "split pea soup")
- @result{} ("split" "pea" "soup")
- (regex-split " *" "split pea soup")
- @result{} ("" "s" "p" "l" "i" "t" "" "p" "e" "a" "" "s" "o" "u" "p" "")
- @end example
- @end deffn
- @deffn Procedure regex‑replace regex string replacement
- Replaces the matched portion of the text @var{string} by another a
- @var{replacdement} string.
- @example
- (regex-replace "te" "liberte" "ty")
- @result{} "liberty"
- @end example
- Submatches can be used in the replacement string argument.
- The replacement string can use ``@code{$@var{n}}''
- as a @dfn{backreference} to refer back to the @var{n}th
- submatch, ie, the substring that matched the @var{n}th
- subpattern. ``@code{$0}'' refers to the entire match.
- @example
- (regex-replace #/_(.+?)_/
- "the _nina_, the _pinta_, and the _santa maria_"
- "*$1*"))
- @result{} "the *nina*, the _pinta_, and the _santa maria_"
- @end example
- @end deffn
- @deffn Procedure regex‑replace* regex string replacement
- Replaces all matches in the text @var{string} by the @var{replacement} string:
- @example
- (regex-replace* "te" "liberte egalite fraternite" "ty")
- @result{} "liberty egality fratyrnity"
- (regex-replace* #/_(.+?)_/
- "the _nina_, the _pinta_, and the _santa maria_"
- "*$1*")
- @result{} "the *nina*, the *pinta*, and the *santa maria*"
- @end example
- @end deffn
- @deffn Procedure regex-quote pattern
- Takes an arbitrary string and returns a pattern string that precisely
- matches it. In particular, characters in the input string that could
- serve as regex metacharacters are escaped as needed.
- @example
- (regex-quote "cons")
- @result{} "\Qcons\E"
- @end example
- @code{regex‑quote} is useful when building a composite regex
- from a mix of regex strings and verbatim strings.
- @end deffn
- @subsection Portable Scheme regular expressions
- This provides the procedures @code{pregexp}, @code{pregexp‑match‑positions},
- @code{pregexp‑match}, @code{pregexp‑split}, @code{pregexp‑replace},
- @code{pregexp‑replace*}, and @code{pregexp‑quote}.
- Before using them, you must require them:
- @example
- (require 'pregexp)
- @end example
- These procedures have the same interface as the corresponding
- @code{regex-} versions, but take slightly different pattern syntax.
- The replace commands use ``@code{\}'' instead of ``@code{$}''
- to indicate substitutions.
- Also, @code{pregexp‑split} behaves differently from
- @code{regex‑split} if the pattern can match an empty string.
- See @uref{http://www.ccs.neu.edu/home/dorai/pregexp/index.html,here for details}.
- @node Data structures
- @chapter Data structures
- @menu
- * Sequences::
- * Lists::
- * Vectors::
- * Uniform vectors::
- * Bytevectors::
- * Ranges::
- * Streams:: Lazy lists.
- * Arrays:: Multi-dimensional Arrays
- * Hash tables::
- @end menu
- @node Sequences
- @section Sequences
- A @dfn{sequence} is a generalized list, consisting of zero or more values.
- You can choose between a number of different kinds of sequence implementations.
- Scheme traditionally has @ref{Lists,lists} and @ref{Vectors,vectors}.
- Any Java class that implements @code{java.util.List} is a sequence type.
- Raw Java arrays can also be viewerd as a sequence,
- and strings can be viewed a sequence (or vector) of characters.
- Kawa also provides @ref{Uniform vectors,uniform vectors}.
- Sequence types differ in their API, but given a sequence type @var{stype}
- you can construct instances of that type using the syntax:
- @example
- (@var{stype} @var{v0} @var{v1} .... @var{vn})
- @end example
- For example:
- @example
- (bytevector 9 8 7 6) @result{} #u8(9 8 7 6)
- @end example
- For a raw Java class name @var{jname} you may need to use
- the empty keyword @code{||:} to separate constructor parameters (if any)
- from sequence elements, as in:
- @example
- (gnu.lists.U8Vector ||: 9 8 7 6) @result{} #u8(9 8 7 6)
- @end example
- This syntax works with any type with a default constructor
- and a 1-argument @code{add} method;
- see @ref{Allocating objects} for details.
- You can use the same syntax for allocating arrays,
- though array creation supports @ref{Creating-new-Java-arrays,more options}.
- To extract an element from Scheme sequence of type @var{stype}
- there is usually a function @code{@var{stype}-ref}. For example:
- @example
- (define vec1 (vector 5 6 7 8))
- (vector-ref vec1 2) @result{} 7
- @end example
- More concisely, you can use (Kawa-specific) function call syntax:
- @example
- (vec1 3) @result{} 8
- @end example
- The index can be another sequence, which creates a new sequence
- of the selected indexes:
- @example
- (vec1 [3 0 2 1]) @result{} #(8 5 7 6)
- @end example
- It is convenient to use a @ref{Ranges,@dfn{range}} to select
- a sub-sequence:
- @example
- (vec1 [1 <=: 3]) @result{} #(6 7 8)
- (vec1 [2 <:]) @result{} #(7 8)
- @end example
- The same function call syntax also works for raw Java arrays
- (though the index is restricted to an integer, not a sequence or array):
- @example
- (define arr1 (long[] 4 5 6 7))
- (arr1 3) @result{} 7
- @end example
- To assign to (replace) an element from a sequence of Scheme type @var{stype}
- there is usually a function @code{@var{stype}-set!}:
- @example
- (vector-set! vec1 1 9)
- vec1 @result{} #(5 9 7 8)
- @end example
- Again, you can use the function call syntax:
- @example
- (set! (vec1 2) 'x)
- vec1 @result{} #(5 9 x 8)
- @end example
- @deffn Procedure length seq
- Returns the number of elements of the @var{seq}.
- @example
- (length '(a b c)) @result{} 3
- (length '(a (b) (c d e))) @result{} 3
- (length '()) @result{} 0
- (length [3 4 [] 12]) @result{} 4
- (length (vector)) @result{} 0
- (length (int[] 7 6)) @result{} 2
- @end example
- The length of a string is the number of characters (Unicode code points).
- In contrast, the @code{length} @emph{method} (of the @code{CharSequence} interface)
- returns the number of 16-bit code points:
- @example
- (length "Hello") @result{} 5
- (define str1 "Hello \x1f603;!")
- (invoke str1 'length) @result{} 9
- (length str1) @result{} 8 ; Used to return 9 in Kawa 2.x.
- (string-length str1) @result{} 8
- @end example
- @end deffn
- @node Lists
- @section Lists
- A pair (sometimes called a @dfn{dotted pair}) is a record structure
- with two fields called the car and cdr fields (for historical
- reasons). Pairs are created by the procedure @code{cons}. The
- car and cdr fields are accessed by the procedures @code{car} and
- @code{cdr}. The car and cdr fields are assigned by the procedures
- @code{set-car!} and @code{set-cdr!}.
- Pairs are used primarily to represent lists. A @dfn{list} can be
- defined recursively as either the empty list or a pair whose
- cdr is a list. More precisely, the set of lists is defined as
- the smallest set @var{X} such that:
- @itemize
- @item
- The empty list is in @var{X}.
- @item
- If @var{list} is in @var{X}, then any pair whose cdr field contains
- @var{list} is also in @var{X}.
- @end itemize
- The objects in the car fields of successive pairs of a list are
- the elements of the list. For example, a two-element list
- is a pair whose car is the first element and whose cdr is a
- pair whose car is the second element and whose cdr is the
- empty list. The length of a list is the number of elements,
- which is the same as the number of pairs.
- The empty list is a special object of its own type. It is not
- a pair, it has no elements, and its length is zero.
- @emph{Note:} The above definitions imply that all lists have finite
- length and are terminated by the empty list.
- The most general notation (external representation) for
- Scheme pairs is the “dotted” notation @code{(@var{c1} . @var{c2} )}
- where @var{c1} is the value of the car field and
- @var{c2} is the value of the cdr field.
- For example @code{(4 . 5)} is a pair whose car is 4 and
- whose cdr is 5. Note that @code{(4 . 5)} is the external representation
- of a pair, not an expression that evaluates to a pair.
- A more streamlined notation can be used for lists: the
- elements of the list are simply enclosed in parentheses and
- separated by spaces. The empty list is written @code{()}.
- For example,
- @example
- (a b c d e)
- @end example
- and
- @example
- (a . (b . (c . (d . (e . ())))))
- @end example
- are equivalent notations for a list of symbols.
- A chain of pairs not ending in the empty list is called an
- @dfn{improper list}. Note that an improper list is not a list.
- The list and dotted notations can be combined to represent
- improper lists:
- @example
- (a b c . d)
- @end example
- is equivalent to
- @example
- (a . (b . (c . d)))
- @end example
- @emph{Needs to finish merging from R7RS!}
- @deffn Procedure make-list k [fill]
- Returns a newly allocated list of @var{k} elements. If a second argument
- is given, the each element is initialized to @var{fill}.
- Otherwise the initial contents of each element is unspecified.
- @example
- (make-list 2 3) @result{} (3 3)
- @end example
- @end deffn
- @anchor{SRFI-1}
- @subsection SRFI-1 list library
- The @uref{http://srfi.schemers.org/srfi-1/srfi-1.html, SRFI-1 List Library}
- is available, though not enabled by default. To use its functions you
- must @code{(require 'list-lib)} or @code{(require 'srfi-1)}.
- @example
- (require 'list-lib)
- (iota 5 0 -0.5) @result{} (0.0 -0.5 -1.0 -1.5 -2.0)
- @end example
- @deffn Procedure reverse! list
- The result is a list consisting of the elements of @var{list} in reverse order.
- No new pairs are allocated, instead the pairs of @var{list} are re-used,
- with @code{cdr} cells of @var{list} reversed in place. Note that if @var{list}
- was pair, it becomes the last pair of the reversed result.
- @end deffn
- @anchor{SRFI-101}
- @subsection SRFI-101 Purely Functional Random-Access Pairs and Lists
- @uref{http://srfi.schemers.org/srfi-101/srfi-101.html, SRFI-101}
- specifies immutable (read-only) lists with fast (logarithmic) indexing
- and functional
- update (i.e. return a modified list).
- These are implemented by a @code{RAPair} class
- which extends the generic @code{pair} type, which means that most
- code that expects a standard list will work on these lists as well.
- @node Vectors
- @section Vectors
- Vectors are heterogeneous structures whose elements are indexed by
- integers. A vector typically occupies less space than a list of the
- same length, and the average time needed to access a randomly chosen
- element is typically less for the vector than for the list.
- The @emph{length} of a vector is the number of elements that it
- contains. This number is a non--negative integer that is fixed when the
- vector is created. The @emph{valid indices} of a vector are the exact
- non--negative integer objects less than the length of the vector. The
- first element in a vector is indexed by zero, and the last element is
- indexed by one less than the length of the vector.
- Vectors are written using the notation @code{#(@var{obj} ...)}.
- For example, a vector of length 3 3 containing the number zero
- in element 0, the list @code{(2 2 2 2)} in element 1, and the
- string @code{"Anna"} in element 2 can be written as following:
- @example
- #(0 (2 2 2 2) "Anna")
- @end example
- Note that this is the external representation of a vector.
- In Kawa, a vector datum is self-evaluating,
- but for style (and compatibility with R7RS) is is suggested
- you quote a vector constant:
- @example
- ’#(0 (2 2 2 2) "Anna") @result{} #(0 (2 2 2 2) "Anna")
- @end example
- Compare these different ways of creating a vector:
- @table @code
- @item (vector a b c)
- In this case @code{a}, @code{b}, and @code{c} are expressions evaluated at
- run-time and the results used to initialize a newly-allocated 3-element vector.
- @item [a b c]
- Same as using vector, but more concise, and results in an immutable
- (non-modifiable) vector.
- @item #(a b c)
- This is reader syntax and creates a vector literal,
- at read-time, early in compile-time.
- The symbols @code{a}, @code{b}, and @code{c} are not evaluated
- but instead used literally.
- @item `#(,a ,b ,c)
- This is reader-syntax, using quasi-quotation,
- so @code{a}, @code{b}, and @code{c} are expressions evaluated at run-time.
- This is equivalent to @code{[a b c]} in that it results in an immutable vector.
- @end table
- @deffn Type vector
- The type of vector objects.
- @end deffn
- @deffn Constructor vector @var{obj} @dots{}
- Return a newly allocated vector whose elements contain the given
- arguments. Analogous to @code{list}.
- @example
- (vector 'a 'b 'c) @result{} #(a b c)
- @end example
- Alternatively, you can use square-bracket syntax,
- which results in an immutable vector:
- @example
- ['a 'b 'c] @result{} #(a b c)
- @end example
- @end deffn
- @deffn Procedure make-vector @var{k}
- @deffnx Procedure make-vector @var{k} @var{fill}
- Return a newly allocated vector of @var{k} elements. If a second
- argument is given, then each element is initialized to @var{fill}.
- Otherwise the initial contents of each element is @code{#!null}.
- @end deffn
- @deffn Procedure vector? @var{obj}
- Return @true{} if @var{obj} is a vector, @false{} otherwise.
- @end deffn
- @deffn Procedure vector-length @var{vector}
- Return the number of elements in @var{vector} as an exact integer.
- @end deffn
- @deffn Procedure vector-ref @var{vector} @var{k}
- It is an error if @var{k} is not a valid index of @var{vector}.
- The @func{vector-ref}
- procedure returns the contents of element @var{k} of @var{vector}.
- @example
- (vector-ref '#(1 1 2 3 5 8 13 21) 5) @result{} 8
- (vector-ref '#(1 1 2 3 5 8 13 21)
- (inexact->exact (round (* 2 (acos -1)))))
- @result{} 13
- @end example
- @end deffn
- @deffn Procedure vector-set! @var{vector} @var{k} @var{obj}
- It is an error if @var{k} is not a valid index of @var{vector}.
- The @func{vector-set!}
- procedure stores @var{obj} in element @var{k} of @var{vector}, and
- returns no values.
- @example
- (let ((vec (vector 0 '(2 2 2 2) "Anna")))
- (vector-set! vec 1 '("Sue" "Sue"))
- vec)
- @result{} #(0 ("Sue" "Sue") "Anna")
- (vector-set! '#(0 1 2) 1 "doe")
- @result{} @i{error} ;; constant vector
- @end example
- @end deffn
- A concise alternative to @code{vector-ref} and @code{vector-set!}
- is to use function call syntax. For example:
- @example
- (let ((vec (vector 0 '(2 2 2 2) "Anna")))
- (set! (vec 1) '("Sue" "Sue"))
- (list (vec 2) (vec 1)))
- @result{} ("Anna" ("Sue" "Sue"))
- @end example
- @deffn Procedure vector->list @var{vector} [@var{start} [@var{end}]]
- The @func{vector->list} procedure returns a newly allocated list of the
- objects contained in the elements of @var{vector}
- between @var{start} and @var{end}.
- @example
- (vector->list '#(dah dah didah)) @result{} (dah dah didah)
- (vector->list '#(dah dah didah) 1 2) @result{} (dah)
- @end example
- @end deffn
- @deffn Procedure list->vector @var{list}
- The @func{list->vector} procedure returns a newly created vector
- initialized to the elements of the list @var{list}, in order.
- @example
- (list->vector '(dididit dah)) @result{} #(dididit dah)
- @end example
- @end deffn
- @deffn Procedure vector-copy vector [start [end]]
- Returns a newly allocated copy of the elements of the given
- @var{vector} between @var{start} and @var{end} . The elements of the new
- vector are the same (in the sense of @code{eqv?}) as the elements
- of the old.
- @example
- (define a #(1 8 2 8)) ; a may be immutable
- (define b (vector-copy a))
- (vector-set! b 0 3) ; b is mutable
- b @result{} #(3 8 2 8)
- (define c (vector-copy b 1 3))
- c @result{} #(8 2)
- @end example
- @end deffn
- @deffn Procedure vector-copy! to at from [start [end]]
- Copies the elements of vector from between start and end
- to vector to, starting at at. The order in which elements
- are copied is unspecified, except that if the source and
- destination overlap, copying takes place as if the source is first
- copied into a temporary vector and then into the destination.
- This can be achieved without allocating storage by making
- sure to copy in the correct direction in such circumstances.
- It is an error if @var{at} is less than zero or greater than the length
- of @var{to}.
- It is also an error if @code{(- (vector-length @var{to}) @var{at})} is less
- than @code{(- @var{end} @var{start})}.
- @example
- (define a (vector 1 2 3 4 5))
- (define b (vector 10 20 30 40 50))
- (vector-copy! b 1 a 0 2)
- b @result{} #(10 1 2 40 50)
- @end example
- @end deffn
- @deffn Procedure vector-append @var{arg}...
- Creates a newly allocated vector whose elements are the
- concatenation of the elements of the given arguments.
- Each @var{arg} may be a vector or a list.
- @example
- (vector-append #(a b c) #(d e f))
- @result{} #(a b c d e f)
- @end example
- @end deffn
- @deffn Procedure {vector-fill!} @var{vector fill} [@var{start} [@var{end}]]
- Stores @var{fill} in in the elements of @var{vector}
- between @var{start} and @var{end}.
- @example
- (define a (vector 1 2 3 4 5))
- (vector-fill! a 'smash 2 4)
- a @result{} #(1 2 smash smash 5)
- @end example
- @end deffn
- The procedures @code{vector-map} and @code{vector-for-each}
- are documented in @ref{Mapping functions}.
- @node Uniform vectors
- @section Uniform vectors
- Uniform vectors are vectors whose elements are of the same numeric type.
- The are defined by @uref{http://srfi.schemers.org/srfi-4/srfi-4.html,SRFI-4}.
- The type names (such as @code{s8vector}) are a Kawa extension.
- @display
- @stxdef{uniform-vector} @stxlit{#} @stxref{uniform-tag} @stxref{list}
- @stxdef{uniform-tag} @stxlit{f32} | @stxlit{f64}
- | @stxlit{s8} | @stxlit{s16} | @stxlit{s32} | @stxlit{s64}
- | @stxlit{u8} | @stxlit{u16} | @stxlit{u32} | @stxlit{u64}
- @end display
- This example is a literal for a 5-element vector of unsigned short (@code{ushort}) values:
- @example
- (define uvec1 #u16(64000 3200 160 8 0))
- @end example
- Since a uniform vector is a sequence, you can use function-call
- notation to index one. For example:
- @example
- (uvec1 1) @result{} 3200
- @end example
- In this case the result is a primitive unsigned short (@code{ushort}),
- which is converted to a @code{gnu.math.UShort} if an object is needed.
- @deffn Type s8vector
- The type of uniform vectors where each element can contain
- a signed 8-bit integer. Represented using an array of @code{byte}.
- @end deffn
- @deffn Type u8vector
- The type of uniform vectors where each element can contain
- an unsigned 8-bit integer. Represented using an array of @code{<byte>},
- but each element is treated as if unsigned.
- This type is a synonym for @code{bytevector},
- which has @ref{Bytevectors, extra functions}.
- @end deffn
- @deffn Type s16vector
- The type of uniform vectors where each element can contain
- a signed 16-bit integer. Represented using an array of @code{short}.
- @end deffn
- @deffn Type u16vector
- The type of uniform vectors where each element can contain
- an unsigned 16-bit integer. Represented using an array of @code{short},
- but each element is treated as if unsigned.
- @end deffn
- @deffn Type s32vector
- The type of uniform vectors where each element can contain
- a signed 32-bit integer. Represented using an array of @code{int}.
- @end deffn
- @deffn Type u32vector
- The type of uniform vectors where each element can contain
- an unsigned 32-bit integer. Represented using an array of @code{int},
- but each element is treated as if unsigned.
- @end deffn
- @deffn Type s64vector
- The type of uniform vectors where each element can contain
- a signed 64-bit integer. Represented using an array of @code{long}.
- @end deffn
- @deffn Type u64vector
- The type of uniform vectors where each element can contain
- an unsigned 64-bit integer. Represented using an array of @code{long},
- but each element is treated as if unsigned.
- @end deffn
- @deffn Type f32vector
- The type of uniform vectors where each element can contain
- a 32-bit floating-point real. Represented using an array of @code{float}.
- @end deffn
- @deffn Type f64vector
- The type of uniform vectors where each element can contain
- a 64-bit floating-point real. Represented using an array of @code{double}.
- @end deffn
- @deffn Procedure s8vector? value
- @deffnx Procedure u8vector? value
- @deffnx Procedure s16vector? value
- @deffnx Procedure u16vector? value
- @deffnx Procedure s32vector? value
- @deffnx Procedure u32vector? value
- @deffnx Procedure s64vector? value
- @deffnx Procedure u64vector? value
- @deffnx Procedure f32vector? value
- @deffnx Procedure f64vector? value
- Return true iff @var{value} is a uniform vector of the specified type.
- @end deffn
- @deffn Procedure make-s8vector n [value]
- @deffnx Procedure make-u8vector n [value]
- @deffnx Procedure make-s16vector n [value]
- @deffnx Procedure make-u16vector n [value]
- @deffnx Procedure make-s32vector n [value]
- @deffnx Procedure make-u32vector n [value]
- @deffnx Procedure make-s64vector n [value]
- @deffnx Procedure make-u64vector n [value]
- @deffnx Procedure make-f32vector n [value]
- @deffnx Procedure make-f64vector n [value]
- Create a new uniform vector of the specified type,
- having room for @var{n} elements.
- Initialize each element to @var{value} if it is specified; zero otherwise.
- @end deffn
- @deffn Constructor s8vector value ...
- @deffnx Constructor u8vector value ...
- @deffnx Constructor s16vector value ..
- @deffnx Constructor u16vector value ...
- @deffnx Constructor s32vector value ...
- @deffnx Constructor u32vector value ...
- @deffnx Constructor s64vector value ...
- @deffnx Constructor u64vector value ...
- @deffnx Constructor f32vector value ...
- @deffnx Constructor f64vector value ...
- Create a new uniform vector of the specified type,
- whose length is the number of @var{value}s specified,
- and initialize it using those @var{value}s.
- @end deffn
- @deffn Procedure s8vector-length v
- @deffnx Procedure u8vector-length v
- @deffnx Procedure s16vector-length v
- @deffnx Procedure u16vector-length v
- @deffnx Procedure s32vector-length v
- @deffnx Procedure u32vector-length v
- @deffnx Procedure s64vector-length v
- @deffnx Procedure u64vector-length v
- @deffnx Procedure f32vector-length v
- @deffnx Procedure f64vector-length v
- Return the length (in number of elements) of the uniform vector @var{v}.
- @end deffn
- @deffn Procedure s8vector-ref v i
- @deffnx Procedure u8vector-ref v i
- @deffnx Procedure s16vector-ref v i
- @deffnx Procedure u16vector-ref v i
- @deffnx Procedure s32vector-ref v i
- @deffnx Procedure u32vector-ref v i
- @deffnx Procedure s64vector-ref v i
- @deffnx Procedure u64vector-ref v i
- @deffnx Procedure f32vector-ref v i
- @deffnx Procedure f64vector-ref v i
- Return the element at index @var{i} of the uniform vector @var{v}.
- @end deffn
- @deffn Procedure s8vector-set! v i x
- @deffnx Procedure u8vector-set! v i x
- @deffnx Procedure s16vector-set! v i x
- @deffnx Procedure u16vector-set! v i x
- @deffnx Procedure s32vector-set! v i x
- @deffnx Procedure u32vector-set! v i x
- @deffnx Procedure s64vector-set! v i x
- @deffnx Procedure u64vector-set! v i x
- @deffnx Procedure f32vector-set! v i x
- @deffnx Procedure f64vector-set! v i x
- Set the element at index @var{i} of uniform vector @var{v}
- to the value @var{x}, which must be a number coercible
- to the appropriate type.
- @end deffn
- @deffn Procedure s8vector->list v
- @deffnx Procedure u8vector->list v
- @deffnx Procedure s16vector->list v
- @deffnx Procedure u16vector->list v
- @deffnx Procedure s32vector->list v
- @deffnx Procedure u32vector->list v
- @deffnx Procedure s64vector->list v
- @deffnx Procedure u64vector->list v
- @deffnx Procedure f32vector->list v
- @deffnx Procedure f64vector->list v
- Convert the uniform vetor @var{v} to a list containing the elments of @var{v}.
- @end deffn
- @deffn Procedure list->s8vector l
- @deffnx Procedure list->u8vector l
- @deffnx Procedure list->s16vector l
- @deffnx Procedure list->u16vector l
- @deffnx Procedure list->s32vector l
- @deffnx Procedure list->u32vector l
- @deffnx Procedure list->s64vector l
- @deffnx Procedure list->u64vector l
- @deffnx Procedure list->f32vector l
- @deffnx Procedure list->f64vector l
- Create a uniform vector of the appropriate type, initializing it
- with the elements of the list @var{l}. The elements of @var{l}
- must be numbers coercible the new vector's element type.
- @end deffn
- @subsection Relationship with Java arrays
- Each uniform array type is implemented as an @dfn{underlying Java array},
- and a length field.
- The underlying type is
- @code{byte[]} for @code{u8vector} or @code{s8vector};
- @code{short[]} for @code{u16vector} or @code{u16vector};
- @code{int[]} for @code{u32vector} or @code{s32vector};
- @code{long[]} for @code{u64vector} or @code{s64vector};
- @code{float[]} for @code{f32vector}; and
- @code{double[]} for @code{f32vector}.
- The length field allows a uniform array to only use the
- initial part of the underlying array. (This can be used
- to support Common Lisp's fill pointer feature.)
- This also allows resizing a uniform vector. There is no
- Scheme function for this, but you can use the @code{setSize} method:
- @example
- (invoke some-vector 'setSize 200)
- @end example
- If you have a Java array, you can create a uniform vector
- sharing with the Java array:
- @example
- (define arr :: byte[] ((primitive-array-new byte) 10))
- (define vec :: u8vector (make u8vector arr))
- @end example
- At this point @code{vec} uses @code{arr} for its underlying storage,
- so changes to one affect the other.
- It @code{vec} is re-sized so it needs a larger underlying array,
- then it will no longer use @code{arr}.
- @node Bytevectors
- @section Bytevectors
- @dfn{Bytevectors} represent blocks of binary data. They are
- fixed-length sequences of bytes, where a @var{byte} is an exact
- integer in the range [0, 255]. A bytevector is typically more
- space-efficient than a vector containing the same values.
- The length of a bytevector is the number of elements that
- it contains. This number is a non-negative integer that is
- fixed when the bytevector is created. The valid indexes of
- a bytevector are the exact non-negative integers less than
- the length of the bytevector, starting at index zero as with
- vectors.
- The @code{bytevector} type is equivalent to the @code{u8vector}
- @ref{Uniform vectors,uniform vector} type, but is specified
- by the R7RS standard.
- Bytevectors are written using the notation @code{#u8(byte . . . )}.
- For example, a bytevector of length 3 containing the byte
- 0 in element 0, the byte 10 in element 1, and the byte 5 in
- element 2 can be written as following:
- @example
- #u8(0 10 5)
- @end example
- Bytevector constants are self-evaluating, so they do not
- need to be quoted in programs.
- @deffn Type bytevector
- The type of bytevector objects.
- @end deffn
- @deffn Constructor bytevector @var{byte} @dots{}
- Return a newly allocated bytevector whose elements contain the given
- arguments. Analogous to @code{vector}.
- @example
- (bytevector 1 3 5 1 3 5) @result{} #u8(1 3 5 1 3 5)
- (bytevector) @result{} #u8()
- @end example
- @end deffn
- @deffn Procedure bytevector? @var{obj}
- Return @true{} if @var{obj} is a bytevector, @false{} otherwise.
- @end deffn
- @deffn Procedure make-bytevector k
- @deffnx Procedure make-bytevector k byte
- The @code{make-bytevector} procedure returns a newly allocated
- bytevector of length @var{k}. If @var{byte} is given, then all elements
- of the bytevector are initialized to @var{byte},
- otherwise the contents of each element are unspecified.
- @example
- (make-bytevector 2 12) @result{} #u8(12 12)
- @end example
- @end deffn
- @deffn Procedure bytevector-length bytevector
- Returns the length of @var{bytevector} in bytes
- as an exact integer.
- @end deffn
- @deffn Procedure bytevector-u8-ref bytevector k
- It is an error if @var{k} is not a valid index of @var{bytevector}.
- Returns the @var{k}th byte of @var{bytevector}.
- @example
- (bytevector-u8-ref ’#u8(1 1 2 3 5 8 13 21) 5)
- @result{} 8
- @end example
- @end deffn
- @deffn Procedure bytevector-u8-set! bytevector k byte
- It is an error if @var{k} is not a valid index of @var{bytevector}.
- Stores @var{byte} as the @var{k}th byte of @var{bytevector}.
- @example
- (let ((bv (bytevector 1 2 3 4)
- (bytevector-u8-set! bv 1 3)
- bv)
- @result{} #u8(1 3 3 4)
- @end example
- @end deffn
- @deffn Procedure bytevector-copy bytevector [start [end]]
- Returns a newly allocated bytevector containing the bytes
- in @var{bytevector} between @var{start} and @var{end}.
- @example
- (define a #u8(1 2 3 4 5))
- (bytevector-copy a 2 4))
- @result{} #u8(3 4)
- @end example
- @end deffn
- @deffn Procedure bytevector-copy! to at from [start [end]]
- Copies the bytes of bytevector@var{from} between @var{start} and @var{end}
- to bytevector @var{to}, starting at @var{at}. The order in which bytes
- are copied is unspecified, except that if the source and destination overlap,
- copying takes place as if the source is first
- copied into a temporary bytevector and then into the destination.
- This is achieved without allocating storage
- by making sure to copy in the correct direction in such
- circumstances.
- It is an error if @var{at} is less than zero or greater than the length
- of @var{to}.
- It is also an error if @code{(- (bytevector-length @var{to}) @var{at})}
- is less than @code{(- @var{end} @var{start})}.
- @example
- (define a (bytevector 1 2 3 4 5))
- (define b (bytevector 10 20 30 40 50))
- (bytevector-copy! b 1 a 0 2)
- b @result{} #u8(10 1 2 40 50)
- @end example
- @end deffn
- @deffn Procedure bytevector-append bytevector...
- Returns a newly allocated bytevector whose elements are
- the concatenation of the elements in the given bytevectors.
- @example
- (bytevector-append #u8(0 1 2) #u8(3 4 5))
- @result{} #u8(0 1 2 3 4 5)
- @end example
- @end deffn
- @subsection Converting to or from strings
- @deffn Procedure utf8->string bytevector [start [end]]
- This procedure decodes the bytes of a bytevector between @var{start}
- and @var{end}, interpreting as a UTF-8-encoded string,
- and returns the corresponding string.
- It is an error for @var{bytevector} to contain invalid UTF-8 byte sequences.
- @example
- (utf8->string #u8(#x41)) @result{} "A"
- @end example
- @end deffn
- @deffn Procedure utf16->string bytevector [start [end]]
- @deffnx Procedure utf16be->string bytevector [start [end]]
- @deffnx Procedure utf16le->string bytevector [start [end]]
- These procedures interpret their <var>bytevector</var> argument as
- a UTF-16 encoding of a sequence of characters,
- and return an istring containing that sequence.
- The bytevector subrange given to @code{utf16->string}
- may begin with a byte order mark (BOM); if so, that BOM
- determines whether the rest of the subrange is to be
- interpreted as big-endian or little-endian; in either case,
- the BOM will not become a character in the returned string.
- If the subrange does not begin with a BOM, it is decoded using
- the same implementation-dependent endianness used by
- @code{string->utf16}.
- The @code{utf16be->string} and @code{utf16le->string}
- procedures interpret their inputs as big-endian or little-endian,
- respectively. If a BOM is present, it is treated as a normal
- character and will become part of the result.
- It is an error if @code{(- @var{end} @var{start})} is odd,
- or if the bytevector subrange contains invalid UTF-16 byte sequences.
- @end deffn
- @deffn Procedure string->utf8 string [start [end]]
- This procedure encodes the characters of a string between
- @var{start} and @var{end} and returns the corresponding bytevector,
- in UTF-8 encoding.
- @example
- (string->utf8 "λ") @result{} " #u8(#xCE #xBB)
- @end example
- @end deffn
- @deffn Procedure string->utf16 string [start [end]]
- @deffnx Procedure string->utf16be string [start [end]]
- @deffnx Procedure string->utf16le string [start [end]]
- These procedures return a newly allocated (unless empty)
- bytevector containing a UTF-16 encoding of the given substring.
- The bytevectors returned by @code{string->utf16be}
- and @code{string->utf16le}
- do not contain a byte-order mark (BOM);
- @code{string->utf16be}> returns a big-endian encoding,
- while @code{string->utf16le} returns a little-endian encoding.
- The bytevectors returned by @code{string->utf16}
- begin with a BOM that declares an implementation-dependent
- endianness, and the bytevector elements following that BOM
- encode the given substring using that endianness.
- @emph{Rationale:}
- These procedures are consistent with the Unicode standard.
- Unicode suggests UTF-16 should default to big-endian, but
- Microsoft prefers little-endian.
- @end deffn
- @node Ranges
- @section Ranges
- A @dfn{range} is an immutable sequence of values
- that increase ``linearly'' - i.e. by a fixed amount.
- Most commonly it's a sequence of consequtive integers.
- An example of the syntax is @code{[3 <: 7]} which evaluates
- to the sequence @code{[3 4 5 6]}.
- You can specify an explicit increment with a @code{by:} option.
- There are multiple ways to specify when the sequence stops.
- For example @code{[3 by 2 <=: 7]} is the even numbers from
- 3 up to 7 (inclusive, because of the @code{<=}).
- Ranges are very useful for loop indexes, or selecting a sub-sequence.
- If you have a sequence @var{q} and a range @var{r}, and you
- use the syntax @code{(@var{q} @var{r})} to
- ``apply''@var{q} with the argument @var{r},
- is result is to select elements of @var{q} with indexes in @var{r}.
- @example
- ("ABCDEFG" [1 by: 2 <: 7]) @result{} "BDF"
- @end example
- A range can be @dfn{unbounded}, or non-finite, if you leave off
- the end value. For example @code{[3 by: 2]} is the odd integers starting at 3.
- @display
- @stxdef{unbounded-range}
- @stxlit{[} @var{start-expression} @stxlit{by:} @var{step-expression} @stxlit{]}
- | @stxlit{[} @var{start-expression} @stxlit{<:} @stxlit{]}
- @end display
- The expression @code{[@var{start} by: @var{step}]} evaluates to an
- infinite sequence of values, starting with @var{start}, and followed by
- @code{(+ @var{start} @var{step})},
- @code{(+ @var{start} (* 2 @var{step}))}, and so on.
- The syntax @code{[@var{start-expression} <:]}
- is shorthand for @code{[@var{start-expression} by: 1]}.
- @display
- @stxdef{bounded-range} @stxlit{[} @var{start-expression} [@stxlit{by:} @var{step-expression}] @stxref{range-end} @stxlit{]}
- @stxdef{range-end} @stxlit{<:} @var{end-expression}
- | @stxlit{<=:} @var{end-expression}
- | @stxlit{>:} @var{end-expression}
- | @stxlit{>=:} @var{end-expression}
- | @stxlit{size:} @var{size-expression}
- @end display
- A bounded range takes an initial subsequence of the unbounded
- range specified by the @var{start-expression} and optional @var{step-expression}.
- The different @var{end-expression} variants provide different
- ways to specify the initial subsequence.
- If @code{size: @var{size}} is specified, then the resulting range
- is the first @var{size} elements of unbounded sequence.
- In the @code{<: @var{end}} or @code{<=: @var{end}} cases then
- the sequence counts up: The @var{step} must be positive, and defaults to 1.
- The resulting values are those @var{x} such that @code{(< @var{x} @var{end})},
- or @code{(<= @var{x} @var{end})}, respectively.
- In the @code{>: @var{end}} or @code{>=: @var{end}} cases then
- the sequence counts down: The @var{step} must be negative, and defaults to -1.
- The resulting values are those @var{x} such that @code{(> @var{x} @var{end})},
- or @code{(>= @var{x} @var{end})}, respectively.
- The @var{start-expression}, @var{step-expression}, and @var{size-expression}
- must evaluate to real numbers, not necessarily integers.
- For example: @code{[1 by: 0.5 <=: 3.0]} is @code{[1.0 1.5 2.0 2.5 3.0]}.
- The two pseudo-ranges @code{[<:]} and @code{[>:]} are useful as
- array indexes. They mean ``all of the valid indexes'' of the array being indexed.
- For increasing index values use @code{[<:]}; for decreasing
- index values (i.e. reversing) use @code{[>:]}.
- @node Streams
- @section Streams - lazy lists
- Streams, sometimes called lazy lists, are a sequential data structure
- containing elements computed only on demand. A stream is either null
- or is a pair with a stream in its cdr. Since elements of a stream are
- computed only when accessed, streams can be infinite. Once computed,
- the value of a stream element is cached in case it is needed again.
- @emph{Note:} These are not the same as Java 8 streams.
- @example
- (require 'srfi-41)
- (define fibs
- (stream-cons 1
- (stream-cons 1
- (stream-map +
- fibs
- (stream-cdr fibs)))))
- (stream->list 8 fibs) @result{} (1 1 2 3 5 8 13 21)
- @end example
- See the @uref{http://srfi.schemers.org/srfi-41/srfi-41.html, SRFI 41 specification} for details.
- The Kawa implementations builds on @ref{Lazy evaluation,,promises}.
- The @code{stream-null} value is a promise that evaluates to the empty list.
- The result of @code{stream-cons} is an eager immutable pair whose
- @code{car} and @code{cdr} properties return promises.
- @node Arrays
- @section Multi-dimensional Arrays
- Arrays are heterogeneous data structures that generaize vectors to multiple
- indexes or dimensions. Instead of a single integer index,
- there are multiple indexes: An index is a vector of integers;
- the length of a valid index sequence
- is the rank or the number of dimensions of an array.
- @c @example
- @c (define arr1 (
- @c @end example
- Kawa multi-dimensional arrays follows the
- by @uref{http://srfi.schemers.org/srfi-25/srfi-25.html,SRFI-25 specification},
- with additions from Racket's
- @uref{https://docs.racket-lang.org/math/array.html, math.array} package
- and other sources.
- An array whose rank is 1, and where the (single) lower bound is 0
- is a sequence.
- Furthermore, if such an array is simple (not created by @code{share-array})
- it will be implemented using a @code{<vector>}.
- Uniform vectors and strings are also arrays in Kawa.
- A rank-0 array has a single value. It is essentially a box for that
- value. Functions that require arrays may treat non-arrays
- as a rank-0 array containing that value.
- An array of rank 2 is frequently called a @dfn{matrix}.
- Note that Kawa arrays are distinct from Java (native) arrays.
- The latter is a simpler one-dimensional vector-like data structure,
- which is used to implement Kawa arrays and vectors.
- @deffn Procedure array? obj
- Returns @code{#t} if @var{obj} is an array, otherwise returns @code{#f}.
- @end deffn
- @subsection Array shape
- The @dfn{shape} of an array consists of bounds for each index.
- @c a simple-array is either a SimpleVector or a GeneralArray
- @c a generic vector implements AVector
- @c a stored-array is a simple-array or a transform (view) of a simple-array
- The lower bound @var{b} and the upper bound @var{e} of a dimension are
- exact integers with @code{(<= @var{b} @var{e})}. A valid index along the
- dimension is an exact integer @var{i} that satisfies both
- @code{(<= @var{b} @var{i})} and @code{(< @var{i} @var{e})}.
- The length of the array along the dimension is the difference
- @code{(- @var{e} @var{b})}.
- The size of an array is the product of the lengths of its dimensions.
- A procedure that requires a @var{shape} accepts any of the following:
- @itemize
- @item
- A vector of simple @ref{Ranges,ranges}, one for each
- dimension, all of who are
- bounded (finite), consist of integer values,
- and have a @var{step} of 1.
- Each range, which is usually written as @code{[@var{b} <: @var{e}]},
- expresses the bounds of the corresponding dimension.
- For example @code{[[0 <: 5] [2 <=: 6]]} is the shape
- of a rank-2 array, where the first index can be from 0 to 5 (exclusive),
- while the second index can be from 2 to 6 (inclusive).
- @item
- A vector of simple integers.
- Each integer @var{e} is an upper bound,
- and is equivalent to the range @code{[0 <: @var{e}]}.
- @item
- A vector consisting of a mix of integers and ranges.
- @item
- A rank-2 array @var{S} whose own shape is @code{[@var{r} 2]}.
- For each dimension @var{k}
- (where @code{(<= @var{k} 0)} and @code{(< @var{k} @var{r})}),
- the lower bound @var{b@sub{k}} is @code{(S @var{k} 0)},
- and the upper bound @var{e@sub{k}} is @code{(S @var{k} 1)}.
- @end itemize
- @deffn Procedure shape bound ...
- Returns a shape. The sequence @var{bound} ... must consist of an even number
- of exact integers that are pairwise not decreasing. Each pair gives the
- lower and upper bound of a dimension.
- If the shape is used to specify the dimensions of an array
- and @var{bound} ... is the sequence @var{b0} @var{e0} ... @var{bk} @var{ek}
- ... of @var{n} pairs of bounds, then a valid index to the array is any
- sequence @var{j0} ... @var{jk} ... of @var{n} exact integers where
- each @var{jk} satisfies @code{(<= @var{bk} @var{jk})}
- and @code{(< @var{jk} @var{ek})}.
- The shape of a @var{d}-dimensional array is a @var{d} * 2 array
- where the element at @var{k 0} contains the lower bound for an index along
- dimension @var{k} and the element at @var{k 1} contains the
- corresponding upper bound, where @var{k} satisfies @code{(<= 0 @var{k})}
- and @code{(< @var{k} @var{d})}.
- @code{(shape @@@var{bounds})}
- is equivalent to:
- @code{(array [2 (/ (length @var{bounds}) 2)] @@@var{bounds})}
- @end deffn
- @deffn Procedure array-rank array
- Returns the number of dimensions of @var{array}.
- @example
- (array-rank
- (make-array (shape 1 2 3 4)))
- @end example
- Returns 2.
- @end deffn
- @deffn Procedure array-start array k
- Returns the lower bound (inclusive) for the index along dimension @var{k}.
- This is most commonly 0.
- @end deffn
- @deffn Procedure array-end array k
- Returns the upper bound for the index along dimension @var{k}.
- The bound is exclusive - i.e. the first integer higher
- than the last legal index.
- @end deffn
- @deffn Procedure array-size array
- Return the total number of elements of @var{array}.
- This is the product of @code{(- (array-end @var{array} @var{k}) (array-start @var{array} @var{k}))} for every valid @var{k}.
- @end deffn
- @subsection Array types
- @deffn Type array
- @deffnx Type array@var{N}
- @deffnx Type array[@var{etype}]
- @deffnx Type array@var{N}[@var{etype}]
- The type @code{array} matches all array values.
- The type @code{array@var{N}}, where @var{N} is an integer
- matches array of rank @var{N}.
- For example @code{array2} matches rank-2 array - i.e. matrixes.
- You can optionally specify the element type @var{etype}.
- This can be a primitive type.
- For example a @code{array2[double]} is a rank-2 array
- whose elements are @code{double} values.
- @end deffn
- @anchor{array-literals}
- @subsection Array literals and printing
- An array literal starts with @code{#} followed by its rank,
- followed by a tag that describes the underlying vector (by default @code{a}),
- optionally followed by information about its shape,
- and finally followed by the cells, organized into dimensions using parentheses.
- For example, @code{#2a((11 12 13) (21 22 23))} is a rank-2 array (a matrix)
- whose shape is @code{[2 3]} or equivalently @code{[[0 <: 2] [0 <: 3]]}.
- It is pretty-printed as:
- @example
- ╔#2a:2:3═╗
- ║11│12│13║
- ╟──┼──┼──╢
- ║21│22│23║
- ╚══╧══╧══╝
- @end example
- @display
- @stxdef{array-literal} @stxref{array-literal-header} @stxref{datum}
- @stxdef{array-literal-header} @stxlit{#} @var{rank} @stxref{vectag} @arbno{@stxref{array-bound}}
- @stxdef{array-bound} [@stxlit{@@}@var{lower}]@stxlit{:}@var{length} | @stxlit{@@}@var{lower}
- @stxdef{vectag} @stxlit{a} | @stxref{uniform-tag}
- @end display
- The @var{vectag} specifies the type of the elements of the array.
- Following the @var{vectag} you can optionally include information
- about the shape: For each dimension you can optionally specify
- the lower bounds (after the character @code{"@@"}),
- followed by the length of the dimension (after the character @code{":"}).
- The shape information is required if a lower bound is non-zero,
- or any length is zero.
- The @stxref{datum} contains the elements in a nested-list format:
- a rank-1 array (i.e. vector) uses a single list,
- a rank-2 array uses a list-of-lists, and so on.
- The elements are in lexicographic order.
- A uniform u32 array of rank 2 with index ranges 2..3 and 3..4:
- @example
- #2u32@@2@@3((1 2) (2 3))
- @end example
- This syntax follows Common Lisp with
- @uref{https://www.gnu.org/software/guile/manual/html_node/Array-Syntax.html,Guile
- extensions}. (Note that Guile prints rank-0 arrays with an extra
- set of parentheses. Kawa follows Common Lisp in not doing so.)
- When an array is printed with the @code{write} function,
- the result is an @code{array-literal}.
- Printing with @code{display} formats the array in a rectangular grid
- using the @code{format-array} procedure.
- (Note that @code{format-array} is only used when the output is in column 0,
- because Kawa has very limited support for printing rectangles.)
- @deffn Procedure format-array value [port] [element-format]
- Produce a nice ``pretty'' display for @var{value}, which is usually an array.
- If @var{port} is an output port, the formatted output is written into
- that port.
- Otherwise, @var{port} must be a boolean (one of @code{#t} or @code{#f}).
- If the port is @code{#t}, output is to the @code{(current-output-port)}.
- If the port is @code{#f} or no port is specified,
- the output is returned as a string.
- If the port is specified and is @code{#t} or an output-port,
- the result of the @code{format-array} procedure is unspecified.
- (This convention matches that of the @code{format} procedure.)
- The top line includes an @code{array-literal-header}.
- The lower bound are only printed if non-zero.
- The dimension lengths are printed if there is room, or if one of them is zero.
- @example
- #|kawa:34|# @kbd{(! arr (array [[1 <=: 2] [1 <=: 3]]}
- #|.....35|# @kbd{ #2a((1 2) (3 4)) 9 #2a((3 4) (5 6))}
- #|.....36|# @kbd{ [42 43] #2a:1:3((8 7 6)) #2a((90 91) (100 101))))}
- #|kawa:37|# @kbd{arr}
- ╔#2a@@1:2@@1:3════╤═════════╗
- ║#2a═╗ │ 9│#2a═╗ ║
- ║║1│2║ │ │║3│4║ ║
- ║╟─┼─╢ │ │╟─┼─╢ ║
- ║║3│4║ │ │║5│6║ ║
- ║╚═╧═╝ │ │╚═╧═╝ ║
- ╟───────┼───────┼─────────╢
- ║╔#1a:2╗│#2a:1:3│╔#2a:2:2╗║
- ║║42│43║│║8│7│6║│║ 90│ 91║║
- ║╚══╧══╝│╚═╧═╧═╝│╟───┼───╢║
- ║ │ │║100│101║║
- ║ │ │╚═══╧═══╝║
- ╚═══════╧═══════╧═════════╝
- @end example
- If @var{element-format} is specified, it is a format string used
- for format each non-array:
- @example
- #|kawa:38|# @kbd{(format-array arr "~4,2f")}
- ╔#2a@@1:2@@1:3══╤════════════════╤═══════════════╗
- ║╔#2a:2:2══╗ │ 9.00│╔#2a:2:2══╗ ║
- ║║1.00│2.00║ │ │║3.00│4.00║ ║
- ║╟────┼────╢ │ │╟────┼────╢ ║
- ║║3.00│4.00║ │ │║5.00│6.00║ ║
- ║╚════╧════╝ │ │╚════╧════╝ ║
- ╟─────────────┼────────────────┼───────────────╢
- ║╔#1a:2╤═════╗│╔#2a:1:3══╤════╗│╔#2a:2:2══════╗║
- ║║42.00│43.00║│║8.00│7.00│6.00║│║ 90.00│ 91.00║║
- ║╚═════╧═════╝│╚════╧════╧════╝│╟──────┼──────╢║
- ║ │ │║100.00│101.00║║
- ║ │ │╚══════╧══════╝║
- ╚═════════════╧════════════════╧═══════════════╝
- @end example
- If the rank is more than 2, then each ``layer''
- is printed separated by double lines.
- @example
- #|kawa:42|# @kbd{(array-reshape [1 <=: 24] [3 2 4])}
- ╔#3a:3:2:4══╗
- ║ 1│ 2│ 3│ 4║
- ╟──┼──┼──┼──╢
- ║ 5│ 6│ 7│ 8║
- ╠══╪══╪══╪══╣
- ║ 9│10│11│12║
- ╟──┼──┼──┼──╢
- ║13│14│15│16║
- ╠══╪══╪══╪══╣
- ║17│18│19│20║
- ╟──┼──┼──┼──╢
- ║21│22│23│24║
- ╚══╧══╧══╧══╝
- @end example
- @end deffn
- @subsection Array construction
- See also @code{array-reshape}
- @deffn Procedure array shape obj ...
- Returns a new array whose shape is given by @var{shape} and the initial
- contents of the elements are @var{obj} ... in row major order. The array does
- not retain a reference to @var{shape}.
- @end deffn
- @deffn Procedure make-array shape
- @deffnx Procedure make-array shape value...
- Returns a newly allocated array whose shape is given by @var{shape}.
- If @var{value} is provided, then each element is initialized to it.
- If there is more than one @var{value}, they are used in order, starting
- over when the @var{value}s are exhausted.
- If there is no @var{value}, the initial contents of each element is
- unspecified. (Actually, it is the @code{#!null}.)
- The array does not retain a reference to @var{shape}.
- @example
- #|kawa:16|# @kbd{(make-array [2 4] 1 2 3 4 5)}
- ╔#2a:2:4╗
- ║1│2│3│4║
- ╟─┼─┼─┼─╢
- ║5│1│2│3║
- ╚═╧═╧═╧═╝
- @end example
- @CompatibilityNote{} Guile has an incompatible @code{make-array} procedure.
- @end deffn
- @deffn Procedure build-array shape getter [setter[
- Construct a ``virtual array'' of the given @var{shape},
- which uses no storage for the elements.
- Instead, elements are calculated on-demand by calling @var{getter},
- which takes a single argument, an index vector.
- There is no caching or memoization.
- @example
- #|kawa:1|# @kbd{(build-array [[10 <: 12] 3]}
- #|....:2|# @kbd{ (lambda (ind)}
- #|....:3|# @kbd{ (let ((x (ind 0)) (y (ind 1)))}
- #|....:4|# @kbd{ (- x y))))}
- #2a@@10:2:3
- ║10│ 9│8║
- ╟──┼──┼─╢
- ║11│10│9║
- ╚══╧══╧═╝
- @end example
- The resulting array is mutable if a @var{setter} is provided.
- The @var{setter} takes two arguments:
- An index vector, and the new value for the specified element.
- Below is a simple and space-efficient (but slow) implementation
- of sparse arrays: Most element have a default initial value,
- but you can override specific elements.
- @example
- (define (make-sparse-array shape default-value)
- (let ((vals '())) ;; association list of (INDEX-VECTOR . VALUE)
- (build-array shape
- (lambda (I)
- (let ((v (assoc I vals)))
- (if v (cdr v)
- default-value)))
- (lambda (I newval)
- (let ((v (assoc I vals)))
- (if v
- (set-cdr! v newval)
- (set! vals (cons (cons I newval) vals))))))))
- @end example
- @end deffn
- @deffn Procedure index-array shape
- Return a new immutable array of the specified @var{shape}
- where each element is the corresponding row-major index.
- Same as @code{(array-reshape [0 <: @var{size}] @var{shape})}
- where @var{size} is the @code{array-size} of the resulting array.
- @example
- #|kawa:1|# @kbd{(index-array [[1 <: 3] [2 <: 6]])}
- #2a@@1:2@@2:4
- ║0│1│2│3║
- ╟─┼─┼─┼─╢
- ║4│5│6│7║
- ╚═╧═╧═╧═╝
- @end example
- @end deffn
- @subsection Array indexing
- If you ``call'' an array as it it were a function,
- it is equivalent to using @code{array-index-ref},
- which is generalization of @code{array-ref}.
- For example, given a rank-2 array @var{arr} with integer indexes @var{i}
- and @var{j}, the following all get the element of @var{arr}
- at index @code{[@var{i} @var{j}]}.
- @example
- (@var{arr} @var{i} @var{j})
- (array-index-ref @var{arr} @var{i} @var{j})
- (array-ref @var{arr} @var{i} @var{j})
- (array-ref @var{arr} [@var{i} @var{j}])
- @end example
- Using function-call notation or @code{array-index-ref}
- (but not plain @code{array-ref}) you can do generalized APL-style
- slicing and indirect indexing.
- See under @code{array-index-ref} for examples.
- @deffn Procedure array-ref array k ...
- @deffnx Procedure array-ref array index
- Returns the contents of the element of @var{array} at index @var{k} ....
- The sequence @var{k} ... must be a valid index to @var{array}.
- In the second form, @var{index} must be either a vector
- (a 0-based 1-dimensional array) containing @var{k} ....
- @example
- (array-ref (array [2 3]
- 'uno 'dos 'tres
- 'cuatro 'cinco 'seis)
- 1 0)
- @end example
- Returns @code{cuatro}.
- @example
- (let ((a (array (shape 4 7 1 2) 3 1 4)))
- (list (array-ref a 4 1)
- (array-ref a (vector 5 1))
- (array-ref a (array (shape 0 2)
- 6 1))))
- @end example
- Returns @code{(3 1 4)}.
- @end deffn
- @deffn Procedure array-index-ref array index ...
- Generalized APL-style array indexing, where each @var{index}
- can be either an array or an integer.
- If each @var{index} is an integer, then the result is the same as @code{array-ref}.
- Otherwise, the result is an immutable array whose rank is the sum of the ranks of
- each @var{index}. An integer is treated as rank-0 array.
- If @var{marr} is the result of @code{(array-index-ref @var{arr} @var{M@sub{1}} @var{M@sub{2}} ...)} then:
- @example
- (@var{marr} @var{i@sub{11}} @var{i@sub{12}} ... @var{i@sub{21}} @var{i@sub{22}} ...)
- @end example
- is defined as:
- @example
- (@var{arr} (@var{M@sub{1}} @var{i@sub{11}} @var{i@sub{12}} ...) (@var{M@sub{2}} @var{i@sub{21}} @var{i@sub{22}} ...) ...)
- @end example
- Each @var{M@sub{k}} gets as many indexes as its rank.
- If @var{M@sub{k}} is an integer, then it we use
- it directly without any indexing.
- Here are some examples, starting with simple indexing.
- @example
- #|kawa:1|# @kbd{(define arr (array #2a((1 4) (0 4))}
- #|.....2|# @kbd{ 10 11 12 13 20 21 22 23 30 31 32 33))}
- #|kawa:3|# @kbd{arr}
- ╔#2a@@1:3:4══╗
- ║10│11│12│13║
- ╟──┼──┼──┼──╢
- ║20│21│22│23║
- ╟──┼──┼──┼──╢
- ║30│31│32│33║
- ╚══╧══╧══╧══╝
- #|kawa:4|# @kbd{(arr 2 3)}
- 23
- @end example
- If one index is a vector and the rest are scalar integers,
- then the result is a vector:
- @example
- #|kawa:5|# @kbd{(arr 2 [3 1])}
- #(23 21)
- @end example
- You can select a ``sub-matrix'' when all indexes are vectors:
- @example
- #|kawa:6|# @kbd{(arr [2 1] [3 1 3])}
- ╔#2a:2:3═╗
- ║23│21│23║
- ╟──┼──┼──╢
- ║13│11│13║
- ╚══╧══╧══╝
- @end example
- Using ranges for index vectors selects a rectangular sub-matrix.
- @example
- #|kawa:7|# @kbd{(arr [1 <: 3] [1 <: 4])}
- ╔#2a:2:3═╗
- ║11│12│13║
- ╟──┼──┼──╢
- ║21│22│23║
- ╚══╧══╧══╝
- @end example
- You can add new dimensions:
- @example
- #|kawa:8|# @kbd{(arr [2 1] #2a((3 1) (3 2)))}
- #3a╤══╗
- ║23│21║
- ╟──┼──╢
- ║23│22║
- ╠══╪══╣
- ║13│11║
- ╟──┼──╢
- ║13│12║
- ╚══╧══╝
- @end example
- The pseudo-range @code{[<:]} can be used to select all the indexes
- along a dimension. To select row 2 (1-origin):
- @example
- #|kawa:9|# @kbd{(arr 2 [<:])}
- #(20 21 22 23)
- @end example
- To reverse the order use @code{[>:]}:
- @example
- #|kawa:10|# @kbd{(arr 2 [>:])}
- #(23 22 21 20)
- @end example
- To select column 3:
- @example
- #|kawa:11|# @kbd{(arr [<:] 3)}
- #(13 23 33)
- @end example
- If you actually want a column matrix (i.e. with shape @code{[3 1]}
- you can place the index in a single-element vector:
- @example
- #|kawa:12|# @kbd{(arr [<:] [3])}
- #2a╗
- ║13║
- ╟──╢
- ║23║
- ╟──╢
- ║33║
- ╚══╝
- @end example
- To expand that column to 5 colums you can repeat the column index:
- @example
- #|kawa:13|# @kbd{[3 by: 0 size: 5]}
- #(3 3 3 3 3)
- #|kawa:14|# @kbd{(arr [<:] [3 by: 0 size: 5])}
- ╔#2a:3:5═╤══╤══╗
- ║13│13│13│13│13║
- ╟──┼──┼──┼──┼──╢
- ║23│23│23│23│23║
- ╟──┼──┼──┼──┼──╢
- ║33│33│33│33│33║
- ╚══╧══╧══╧══╧══╝
- @end example
- @end deffn
- @subsection Modifying arrays
- You can use @code{set!} to modify one or multiple elements.
- To modify a single element:
- @example
- (set! (@var{arr} @var{index} ...) @var{new-value})
- @end example
- is equivalent to
- @example
- (array-set! @var{arr} @var{index} ... @var{new-value})
- @end example
- You can set a slice (or all of the elements).
- In that case:
- @example
- (set! (@var{arr} @var{index} ...) @var{new-array})
- @end example
- is equivalent to:
- @example
- (array-copy! (array-index-share @var{arr} @var{index} ...) @var{new-array})
- @end example
- @deffn Procedure array-set! array k ... obj
- @deffnx Procedure array-set! array index obj
- Stores @var{obj} in the element of @var{array} at index @var{k} ....
- Returns the void value.
- The sequence @var{k} ... must be a valid index to @var{array}.
- In the second form, @var{index} must be either a vector or a
- 0-based 1-dimensional array containing @var{k} ....
- @example
- (let ((a (make-array
- (shape 4 5 4 5 4 5))))
- (array-set! a 4 4 4 "huuhkaja")
- (array-ref a 4 4 4))
- @end example
- Returns @code{"huuhkaja"}.
- @CompatibilityNote{} SRFI-47, Guile and Scheme-48 have @code{array-set!} with a
- different argument order.
- @end deffn
- @deffn Procedure array-copy! dst src
- @CompatibilityNote{} Guile has a @code{array-copy!} with the reversed
- argument order.
- @end deffn
- @deffn Procedure array-fill! array value
- Set all the values @var{array} to @var{value}.
- @end deffn
- @subsection Transformations and views
- A view or transform of an array is an array @var{a@sub{2}}
- whose elements come from some other array @var{a@sub{1}},
- given some transform function @var{T} that maps @var{a@sub{2}} indexes
- to @var{a@sub{1}} indexes.
- Specifically @code{(array-ref @var{a@sub{2}} @var{indexes})}
- is @code{(array-ref @var{a@sub{1}} (@var{T} @var{indexes}))}.
- Modifying @var{a@sub{2}} causes @var{a@sub{1}} to be modified;
- modifying @var{a@sub{1}} may modify @var{a@sub{2}}
- (depending on the transform function).
- The shape of @var{a@sub{2}} is in generally different than that of @var{a@sub{1}}.
- @deffn Procedure array-transform array shape transform
- This is a general mechanism for creating a view.
- The result is a new array with the given @var{shape}.
- Accessing this new array is implemented by calling the @var{transform}
- function on the index vector, which must return a new index vector
- valid for indexing the original @var{array}.
- Here is an example (using the same @code{arr} as in
- the @code{array-index-ref} example):
- @example
- #|kawa:1|# @kbd{(define arr (array #2a((1 4) (0 4))}
- #|.....2|# @kbd{ 10 11 12 13 20 21 22 23 30 31 32 33))}
- #|kawa:14|# @kbd{(array-transform arr #2a((0 3) (1 3) (0 2))}
- #|.....15|# @kbd{ (lambda (ix) (let ((i (ix 0)) (j (ix 1)) (k (ix 2)))}
- #|.....16|# @kbd{ [(+ i 1)}
- #|.....17|# @kbd{ (+ (* 2 (- j 1)) k)])))}
- #3a:3@@1:2:2
- ║10│11║
- ╟──┼──╢
- ║12│13║
- ╠══╪══╣
- ║20│21║
- ╟──┼──╢
- ║22│23║
- ╠══╪══╣
- ║30│31║
- ╟──┼──╢
- ║32│33║
- ╚══╧══╝
- @end example
- The @code{array-transform} is generalization of @code{share-array},
- in that it does not require the @var{transform} to be affine.
- Also note the different calling conversions for the @var{tranform}:
- @code{array-transform} takes a single argument (a vector of indexes),
- and returns a single result (a vector of indexes);
- @code{share-array} takes one argument for each index, and returns
- one value for each index. The difference is historical.
- @end deffn
- @deffn Procedure array-index-share array index ...
- This does the same generalized APL-style indexing
- as @code{array-index-ref}. However, the resulting array
- is a modifiable view into the argument @var{array}.
- @end deffn
- @deffn Procedure array-reshape array shape
- Creates a new array @var{narray} of the given @var{shape},
- such that @code{(array->vector @var{array})} and
- @code{(array->vector @var{narray})} are equivalent.
- I.e. the @var{i}'th element in row-major-order of @var{narray}
- is the @var{i}'th element in row-major-order of @var{array}.
- Hence @code{(array-size @var{narray})} (as specied from the @var{shape})
- must be equal to @code{(array-size @var{array})}.
- The resulting @var{narray} is a view such that modifying @var{array}
- also modifies @var{narray} and vice versa.
- @end deffn
- @deffn Procedure share-array array shape proc
- Returns a new array of @var{shape} shape that shares elements of @var{array}
- through @var{proc}. The procedure @var{proc} must implement an affine
- function that returns indices of @var{array} when given indices of the
- array returned by @code{share-array}.
- The array does not retain a reference to @var{shape}.
- @example
- (define i_4
- (let* ((i (make-array
- (shape 0 4 0 4)
- 0))
- (d (share-array i
- (shape 0 4)
- (lambda (k)
- (values k k)))))
- (do ((k 0 (+ k 1)))
- ((= k 4))
- (array-set! d k 1))
- i))
- @end example
- Note: the affinity requirement for @var{proc} means that each value must
- be a sum of multiples of the arguments passed to @var{proc}, plus a constant.
- Implementation note: arrays have to maintain an internal index mapping
- from indices @var{k1} ... @var{kd} to a single index into a backing vector;
- the composition of this mapping and @var{proc} can be recognised
- as @code{(@var{+ n0} (* @var{n1} @var{k1}) ... (* @var{nd} @var{kd}))}
- by setting each index in turn to 1 and others to 0,
- and all to 0 for the constant term; the composition can then be compiled
- away, together with any complexity that the user introduced in their
- procedure.
- Here is an example where the @var{array} is a uniform vector:
- @example
- (share-array
- (f64vector 1.0 2.0 3.0 4.0 5.0 6.0)
- (shape 0 2 0 3)
- (lambda (i j) (+ (* 2 i) j)))
- @result{} #2f64((1.0 2.0 3.0) (4.0 5.0 6.0))
- @end example
- @end deffn
- @deffn Procedure array-flatten array
- @deffnx Procedure array->vector array
- Return a vector consisting of the elements of the @var{array}
- in row-major-order.
- The result of @code{array-flatten} is fresh (mutable) copy, not a view.
- The result of @code{array->vector} is a view: If @var{array} is mutable,
- then modifying @var{array} changes the flattened result and vice versa.
- If @var{array} is ``simple'', @code{array->vector} returns the original vector.
- Specifically, if @var{vec} is a vector then:
- @example
- (eq? @var{vec} (array->vector (array-reshape @var{vec} @var{shape})))
- @end example
- @end deffn
- @subsection Miscellaneous
- @deffn Procedure format-array value [element-format]
- @end deffn
- @node Hash tables
- @section Hash tables
- A @dfn{hashtable} is a data structure that
- associates keys with values.
- The hashtable has no intrinsic order for the (key, value) associations
- it contains, and
- supports in-place modification as the primary means of setting the contents
- of a hash table.
- Any object can be used as a key, provided a @dfn{hash function} and a suitable
- @dfn{equivalence function} is available.
- A hash function is a procedure that
- maps keys to exact integer objects.
- The hashtable provides key lookup and destructive update in amortised
- constant time, provided that a good hash function is used.
- A hash function @var{h} is acceptable for an equivalence predicate @var{e} iff
- @code{(@var{e} @var{obj1} @var{obj2})} implies
- @code{(= (@var{h} @var{obj1}) (@var{h} @var{obj2}))}.
- A hash function @var{h} is good for a equivalence predicate @var{e} if
- it distributes the resulting hash values for non-equal objects
- (by @var{e}) as uniformly as possible over the range of hash
- values, especially in the case when some (non-equal) objects resemble
- each other by e.g. having common subsequences. This definition is
- vague but should be enough to assert that e.g. a constant function is
- not a good hash function.
- Kawa provides two complete sets of functions for hashtables:
- @itemize
- @item
- The functions specified by R6RS have names starting with @code{hashtable-}
- @item
- The functions specified by the older
- @uref{http://srfi.schemers.org/srfi-69/srfi-69.html, SRFI-69} specifiation
- have names starting with @code{hash-table-}
- @end itemize
- Both interfaces use the same underlying datatype, so it is possible
- to mix and match from both sets.
- That datatype implements @code{java.util.Map}.
- @c The Kawa implementation has been optimized for performance and better
- @c Java integration. Specifically, the default hash function uses
- @c the standard Java @code{hashCode} method.
- Freshly-written code should probably use the R6RS functions.
- @subsection R6RS hash tables
- To use these hash table functions in your Kawa program you must first:
- @example
- (import (rnrs hashtables))
- @end example
- This section uses the @var{hashtable} parameter name for arguments that
- must be hashtables, and the @var{key} parameter name for arguments that
- must be hashtable keys.
- @deffn Procedure make-eq-hashtable
- @deffnx Procedure make-eq-hashtable @var{k}
- Return a newly allocated mutable hashtable that accepts arbitrary
- objects as keys, and compares those keys with @func{eq?}. If an
- argument is given, the initial capacity of the hashtable is set to
- approximately @var{k} elements.
- @end deffn
- @deffn Procedure make-eqv-hashtable
- @deffnx Procedure make-eqv-hashtable @var{k}
- Return a newly allocated mutable hashtable that accepts arbitrary
- objects as keys, and compares those keys with @func{eqv?}. If an
- argument is given, the initial capacity of the hashtable is set to
- approximately @var{k} elements.
- @end deffn
- @deffn Procedure make-hashtable @var{hash-function} @var{equiv}
- @deffnx Procedure make-hashtable @var{hash-function} @var{equiv} @var{k}
- @var{hash-function} and @var{equiv} must be procedures.
- @var{hash-function} should accept a key as an argument and should return
- a non--negative exact integer object. @var{equiv} should accept two
- keys as arguments and return a single value. Neither procedure should
- mutate the hashtable returned by @func{make-hashtable}.
- The @func{make-hashtable} procedure returns a newly allocated mutable
- hashtable using @var{hash-function} as the hash function and @var{equiv}
- as the equivalence function used to compare keys. If a third argument
- is given, the initial capacity of the hashtable is set to approximately
- @var{k} elements.
- Both @var{hash-function} and @var{equiv} should behave like pure
- functions on the domain of keys. For example, the @func{string-hash}
- and @func{string=?} procedures are permissible only if all keys are
- strings and the contents of those strings are never changed so long as
- any of them continues to serve as a key in the hashtable. Furthermore,
- any pair of keys for which @var{equiv} returns true should be hashed to
- the same exact integer objects by @var{hash-function}.
- @quotation
- @emph{Note:} Hashtables are allowed to cache the results of calling the
- hash function and equivalence function, so programs cannot rely on the
- hash function being called for every lookup or update. Furthermore any
- hashtable operation may call the hash function more than once.
- @end quotation
- @end deffn
- @subsubsection Procedures
- @deffn Procedure {hashtable?} @var{obj}
- Return @true{} if @var{obj} is a hashtable, @false{} otherwise.
- @end deffn
- @deffn Procedure hashtable-size @var{hashtable}
- Return the number of keys contained in @var{hashtable} as an exact
- integer object.
- @end deffn
- @deffn Procedure hashtable-ref @var{hashtable} @var{key} @var{default}
- Return the value in @var{hashtable} associated with @var{key}. If
- @var{hashtable} does not contain an association for @var{key},
- @var{default} is returned.
- @end deffn
- @deffn Procedure {hashtable-set!} @var{hashtable} @var{key} @var{obj}
- Change @var{hashtable} to associate @var{key} with @var{obj}, adding a
- new association or replacing any existing association for @var{key}, and
- returns unspecified values.
- @end deffn
- @deffn Procedure {hashtable-delete!} @var{hashtable} @var{key}
- Remove any association for @var{key} within @var{hashtable} and returns
- unspecified values.
- @end deffn
- @deffn Procedure {hashtable-contains?} @var{hashtable} @var{key}
- Return @true{} if @var{hashtable} contains an association for @var{key},
- @false{} otherwise.
- @end deffn
- @deffn Procedure {hashtable-update!} @var{hashtable} @var{key} @var{proc} @var{default}
- @var{proc} should accept one argument, should return a single value, and
- should not mutate @var{hashtable}.
- The @func{hashtable-update!} procedure applies @var{proc} to the value
- in @var{hashtable} associated with @var{key}, or to @var{default} if
- @var{hashtable} does not contain an association for @var{key}. The
- @var{hashtable} is then changed to associate @var{key} with the value
- returned by @var{proc}.
- The behavior of @func{hashtable-update!} is equivalent to the following
- code, but is may be (and is in Kawa) implemented more efficiently in cases
- where the implementation can avoid multiple lookups of the same key:
- @example
- (hashtable-set!
- hashtable key
- (proc (hashtable-ref
- hashtable key default)))
- @end example
- @end deffn
- @deffn Procedure hashtable-copy @var{hashtable}
- @deffnx Procedure hashtable-copy @var{hashtable} @var{mutable}
- Return a copy of @var{hashtable}. If the @var{mutable} argument is
- provided and is true, the returned hashtable is mutable; otherwise it is
- immutable.
- @end deffn
- @deffn Procedure {hashtable-clear!} @var{hashtable}
- @deffnx Procedure {hashtable-clear!} @var{hashtable} @var{k}
- Remove all associations from @var{hashtable} and returns unspecified
- values.
- If a second argument is given, the current capacity of the hashtable is
- reset to approximately @var{k} elements.
- @end deffn
- @deffn Procedure hashtable-keys @var{hashtable}
- Return a vector of all keys in @var{hashtable}. The order of the vector
- is unspecified.
- @end deffn
- @deffn Procedure hashtable-entries @var{hashtable}
- Return two values, a vector of the keys in @var{hashtable}, and a vector
- of the corresponding values.
- Example:
- @example
- (let ((h (make-eqv-hashtable)))
- (hashtable-set! h 1 'one)
- (hashtable-set! h 2 'two)
- (hashtable-set! h 3 'three)
- (hashtable-entries h))
- @result{} #(1 2 3) #(one two three) ; two return values
- @end example
- @noindent
- the order of the entries in the result vectors is not known.
- @end deffn
- @subsubsection Inspection
- @deffn Procedure hashtable-equivalence-function @var{hashtable}
- Return the equivalence function used by @var{hashtable} to compare keys.
- For hashtables created with @func{make-eq-hashtable} and
- @func{make-eqv-hashtable}, returns @func{eq?} and @func{eqv?}
- respectively.
- @end deffn
- @deffn Procedure hashtable-hash-function @var{hashtable}
- Return the hash function used by @var{hashtable}. For hashtables
- created by @func{make-eq-hashtable} or @func{make-eqv-hashtable},
- @false{} is returned.
- @end deffn
- @deffn Procedure {hashtable-mutable?} @var{hashtable}
- Return @true{} if @var{hashtable} is mutable, otherwise @false{}.
- @end deffn
- @subsubsection Hash functions
- The @func{equal-hash}, @func{string-hash}, and @func{string-ci-hash}
- procedures of this section are acceptable as the hash functions of a
- hashtable only if the keys on which they are called are not mutated
- while they remain in use as keys in the hashtable.
- @deffn Procedure equal-hash @var{obj}
- Return an integer hash value for @var{obj}, based on its structure and
- current contents. This hash function is suitable for use with
- @func{equal?} as an equivalence function.
- @quotation
- @emph{Note:} Like @func{equal?}, the @func{equal-hash} procedure must
- always terminate, even if its arguments contain cycles.
- @end quotation
- @end deffn
- @deffn Procedure string-hash @var{string}
- Return an integer hash value for @var{string}, based on its current
- contents. This hash function is suitable for use with @func{string=?}
- as an equivalence function.
- @end deffn
- @deffn Procedure string-ci-hash @var{string}
- Return an integer hash value for @var{string} based on its current
- contents, ignoring case. This hash function is suitable for use with
- @func{string-ci=?} as an equivalence function.
- @end deffn
- @deffn Procedure symbol-hash @var{symbol}
- Return an integer hash value for @var{symbol}.
- @end deffn
- @subsection SRFI-69 hash tables
- To use these hash table functions in your Kawa program you must first:
- @example
- (require 'srfi-69)
- @end example
- or
- @example
- (require 'hash-table)
- @end example
- or
- @example
- (import (srfi 69 basic-hash-tables))
- @end example
- @subsubsection Type constructors and predicate
- @deffn Procedure make-hash-table [ equal? [ hash [ size-hint]]] → hash-table
- Create a new hash table with no associations.
- The @var{equal?} parameter is a predicate
- that should accept two keys and return a boolean telling whether they
- denote the same key value; it defaults to the @code{equal?} function.
- The @var{hash} parameter is a hash function, and defaults to an
- appropriate hash function
- for the given @var{equal?} predicate (see the Hashing section).
- However, an
- acceptable default is not guaranteed to be given for any equivalence
- predicate coarser than @code{equal?}, except for @code{string-ci=?}.
- (The function @code{hash} is acceptable for @code{equal?}, so if you
- use coarser equivalence than @code{equal?} other than @code{string-ci=?},
- you must always provide the function hash yourself.)
- (An equivalence predicate @var{c1} is coarser than a equivalence
- predicate @var{c2} iff there exist values @var{x} and @var{y} such
- that @code{(and (@var{c1} @var{x} @var{y}) (not (@var{c2} @var{x} @var{y})))}.)
- The @var{size-hint} parameter can be used to suggested an approriate
- initial size. This option is not part of the SRFI-69 specification
- (though it is handled by the reference implementation), so specifying
- that option might be unportable.
- @end deffn
- @deffn Procedure hash-table? obj → boolean
- A predicate to test whether a given object @var{obj} is a hash table.
- @end deffn
- @deffn Procedure alist->hash-table alist [ equal? [ hash [ size-hint]]] → hash-table
- Takes an association list @var{alist} and creates a hash table
- @var{hash-table} which maps the @code{car} of every element in
- @var{alist} to the @code{cdr} of corresponding elements in
- @var{alist}. The @var{equal?}, @var{hash}, and @var{size-hint}
- parameters are interpreted as in @code{make-hash-table}. If some key
- occurs multiple times in @var{alist}, the value in the first
- association will take precedence over later ones. (Note: the choice of
- using @code{cdr} (instead of @code{cadr}) for values tries to strike
- balance between the two approaches: using @var{cadr} would render this
- procedure unusable for @code{cdr} alists, but not vice versa.)
- @end deffn
- @subsubsection Reflective queries
- @deffn Procedure hash-table-equivalence-function hash-table
- Returns the equivalence predicate used for keys of @var{hash-table}.
- @end deffn
- @deffn Procedure hash-table-hash-function hash-table
- Returns the hash function used for keys of @var{hash-table}.
- @end deffn
- @subsubsection Dealing with single elements
- @deffn Procedure hash-table-ref hash-table key [ thunk ] → value
- This procedure returns the value associated to @var{key} in
- @var{hash-table}. If no value is associated to @var{key} and
- @var{thunk} is given, it is called with no arguments and its value is
- returned; if @var{thunk} is not given, an error is signalled. Given a
- good hash function, this operation should have an (amortised) complexity
- of O(1) with respect to the number of associations in @var{hash-table}.
- @end deffn
- @deffn Procedure hash-table-ref/default hash-table key default → value
- Evaluates to the same value as @code{(hash-table-ref @var{hash-table}
- @var{key} (lambda () @var{default}))}. Given a good hash function, this
- operation should have an (amortised) complexity of O(1) with respect
- to the number of associations in hash-table.
- @end deffn
- @deffn Procedure hash-table-set! hash-table key value → void
- This procedure sets the value associated to @var{key} in
- @var{hash-table}. The previous association (if any) is removed. Given
- a good hash function, this operation should have an (amortised)
- complexity of O(1) with respect to the number of associations in
- hash-table.
- @end deffn
- @deffn Procedure hash-table-delete! hash-table key → void
- This procedure removes any association to @var{key} in
- @var{hash-table}. It is not an error if no association for the
- @var{key} exists; in this case, nothing is done. Given a good hash
- function, this operation should have an (amortised) complexity of O(1)
- with respect to the number of associations in hash-table.
- @end deffn
- @deffn Procedure hash-table-exists? hash-table key → boolean
- This predicate tells whether there is any association to @var{key} in
- @var{hash-table}. Given a good hash function, this operation should
- have an (amortised) complexity of O(1) with respect to the number of
- associations in hash-table.
- @end deffn
- @deffn Procedure hash-table-update! hash-table key function [ thunk ] → void
- Semantically equivalent to, but may be implemented more efficiently than,
- the following code:
- @example
- (hash-table-set! @var{hash-table key}
- (function (hash-table-ref @var{hash-table} @var{key} @var{thunk})))
- @end example
- @end deffn
- @deffn Procedure hash-table-update!/default hash-table key function default → void
- Behaves as if it evaluates to
- @code{(hash-table-update! @var{hash-table} @var{key} @var{function} (lambda () @var{default}))}.
- @end deffn
- @subsubsection Dealing with the whole contents
- @deffn Procedure hash-table-size hash-table → integer
- Returns the number of associations in @var{hash-table}. This operation takes
- constant time.
- @end deffn
- @deffn Procedure hash-table-keys hash-table → list
- Returns a list of keys in @var{hash-table}.
- The order of the keys is unspecified.
- @end deffn
- @deffn Procedure hash-table-values hash-table → list
- Returns a list of values in @var{hash-table}. The order of the values is
- unspecified, and is not guaranteed to match the order of keys in the
- result of @code{hash-table-keys}.
- @end deffn
- @deffn Procedure hash-table-walk hash-table proc → void
- @var{proc} should be a function taking two arguments, a key and a
- value. This procedure calls @var{proc} for each association in
- @var{hash-table}, giving the key of the association as key and the
- value of the association as value. The results of @var{proc} are
- discarded. The order in which @var{proc} is called for the different
- associations is unspecified.
- @end deffn
- @deffn Procedure hash-table-fold hash-table f init-value → final-value
- This procedure calls @var{f} for every association in @var{hash-table}
- with three arguments: the key of the association key, the value of the
- association value, and an accumulated value, @var{val}. The @var{val}
- is @var{init-value} for the first invocation of @var{f}, and for
- subsequent invocations of @var{f}, the return value of the previous
- invocation of @var{f}. The value @var{final-value} returned by
- @code{hash-table-fold} is the return value of the last invocation of
- @var{f}. The order in which @var{f} is called for different
- associations is unspecified.
- @end deffn
- @deffn Procedure hash-table->alist hash-table → alist
- Returns an association list such that the @code{car} of each element
- in @var{alist} is a key in @var{hash-table} and the corresponding
- @code{cdr} of each element in @var{alist} is the value associated to
- the key in @var{hash-table}. The order of the elements is unspecified.
- The following should always produce a hash table with the same mappings
- as a hash table @var{h}:
- @example
- (alist->hash-table (hash-table->alist @var{h})
- (hash-table-equivalence-function @var{h})
- (hash-table-hash-function @var{h}))
- @end example
- @end deffn
- @deffn Procedure hash-table-copy hash-table → hash-table
- Returns a new hash table with the same equivalence predicate, hash
- function and mappings as in @var{hash-table}.
- @end deffn
- @deffn Procedure hash-table-merge! hash-table1 hash-table2 → hash-table
- Adds all mappings in @var{hash-table2} into @var{hash-table1} and
- returns the resulting hash table. This function may modify
- @var{hash-table1} destructively.
- @end deffn
- @subsubsection Hash functions
- The Kawa implementation always calls these hash functions with a single
- parameter, and expects the result to be within the entire
- (32-bit signed) @code{int} range, for compatibility with
- standard @code{hashCode} methods.
- @deffn Procedure hash object [ bound ] → integer
- Produces a hash value for object in the range from 0 (inclusive) tp to
- @var{bound} (exclusive).
- If @var{bound} is not given, the Kawa implementation returns a value within
- the range @w{@code{(- (expt 2 32))}} (inclusive)
- to @w{@code{(- (expt 2 32) 1)}} (inclusive).
- It does this by calling the standard @code{hashCode} method,
- and returning the result as is.
- (If the @var{object} is the Java @code{null} value, 0 is returned.)
- This hash function is acceptable for @code{equal?}.
- @end deffn
- @deffn Procedure string-hash string [ bound ] → integer
- The same as @code{hash}, except that the argument string must be a string.
- (The Kawa implementation returns the same as the @code{hash} function.)
- @end deffn
- @deffn Procedure string-ci-hash string [ bound ] → integer
- The same as @code{string-hash}, except that the case of characters in
- string does not affect the hash value produced.
- (The Kawa implementation returns the same the @code{hash} function
- applied to the lower-cased @var{string}.)
- @end deffn
- @deffn Procedure hash-by-identity object [ bound ] → integer
- The same as @code{hash}, except that this function is only guaranteed
- to be acceptable for @code{eq?}.
- Kawa uses the @code{identityHashCode} method of @code{java.lang.System}.
- @end deffn
- @node Eval and Environments
- @chapter Eval and Environments
- @deffn Procedure environment @arbno{list}
- This procedure returns a specifier for the environment that
- results by starting with an empty environment and then
- importing each @var{list}, considered as an @stxref{import-set}, into it.
- The bindings of the environment represented by the specifier
- are immutable, as is the environment itself.
- See the @code{eval} function for examples.
- @end deffn
- @deffn Procedure null-environment version
- This procedure returns an environment that contains no variable bindings,
- but contains (syntactic) bindings for all the syntactic keywords.
- The effect of assigning to a variable in this environment (such
- as @code{let}) is undefined.
- @end deffn
- @deffn Procedure scheme-report-environment version
- The @var{version} must be an exact non-negative inetger corresponding to
- a version of one of the Revised@var{version} Reports on Scheme.
- The procedure returns an environment that contains exactly the set of
- bindings specified in the corresponding report.
- This implementation supports @var{version} that is 4 or 5.
- The effect of assigning to a variable in this environment (such
- as @code{car}) is undefined.
- @end deffn
- @deffn Procedure interaction-environment
- This procedure return an environment that contains implementation-defined
- bindings, as well as top-level user bindings.
- @end deffn
- @deffn Procedure environment-bound? environment symbol
- Return true @code{#t} if there is a binding for @var{symbol}
- in @var{environment}; otherwise returns @code{#f}.
- @end deffn
- @deffn Procedure environment-fold environment proc init
- Call @var{proc} for each key in the @var{environment},
- which may be any argument to @code{eval}, such as
- @code{(interaction-environment)} or a call to
- the @code{environment} procedure.
- The @var{proc} is called with two arguments:
- The binding's key, and an accumulator value.
- The @var{init} is the initial accumulator value;
- the result returned by @var{proc} is used for subsequent accumulator values.
- The value returned by @code{environment-fold} is the final
- acumulator value.
- A key is normally a symbol,
- but can also be a @code{KeyPair} object (a pair of a symbol and a
- property symbol used to implement Common Lisp-style property lists).
- @example
- (environment-fold (environment '(scheme write)) cons '())
- @result{} (display write-shared write write-simple)
- @end example
- To get all the predefined bindings use @code{(environment '(kawa base))}.
- @end deffn
- @deffn Syntax fluid-let ((variable init) ...) body ...
- Evaluate the @var{init} expressions.
- Then modify the dynamic bindings for the @var{variables} to the
- values of the @var{init} expressions, and evaluate the @var{body} expressions.
- Return the result of the last expression in @var{body}.
- Before returning, restore the original bindings.
- The temporary bindings are only visible in the current thread, and its
- descendent threads.
- @end deffn
- @deffn Procedure base-uri [node]
- If @var{node} is specified, returns the base-URI property
- of the @var{node}. If the @var{node} does not have the base-URI
- property, returns @code{#f}.
- (The XQuery version returns the empty sequence in that case.)
- In the zero-argument case, returns the "base URI" of the current context.
- By default the base URI is the current working directory (as a URL).
- While a source file is @code{load}ed, the base URI is temporarily
- set to the URL of the document.
- @end deffn
- @deffn Procedure eval expression [environment]
- This procedure evaluates @var{expression} in the environment indicated
- by @var{environment}.
- The default for @var{environment} is the result
- of @code{(interaction-environment)}.
- @example
- (eval ’(* 7 3) (environment '(scheme base)))
- @result{} 21
- (let ((f (eval '(lambda (f x) (f x x))
- (null-environment 5))))
- (f + 10))
- @result{} 20
- (eval '(define foo 32) (environment '(scheme base)))
- @result{} @i{error is signaled}
- @end example
- @end deffn
- @deffn Procedure load path [environment]
- @deffnx Procedure load-relative path [environment]
- The @var{path} can be an (absolute) URL or a filename
- of a source file, which is read and evaluated line-by-line.
- The @var{path} can also be a fully-qualified class name.
- (Mostly acts like the @code{-f} command-line option,
- but with different error handling.)
- Since @code{load} is a run-time function it doesn't know
- about the enclosing lexical environment, and the latter
- can't know about definitions introduced by @code{load}.
- For those reasons it is highly recommended that you use instead use
- @code{@ref{require, require}} or @code{@ref{include, include}}.
- Evaluation is done in the specified @var{environment},
- which defauls to result of @code{(interaction-environment)}.
- The @code{load-relative} procedure is like @code{load},
- except that @var{path} is a
- URI that is relative to the context's current base URI.
- @end deffn
- @menu
- * Locations::
- * Parameter objects::
- @end menu
- @node Locations, Parameter objects, , Eval and Environments
- @section Locations
- A @dfn{location} is a place where a value can be stored.
- An @dfn{lvalue} is an expression that refers to a location.
- (The name "lvalue" refers to the fact that the left operand
- of @code{set!} is an lvalue.)
- The only kind of lvalue in standard Scheme is a @dfn{variable}.
- Kawa also allows @dfn{computed lvalues}. These are procedure
- calls used in "lvalue context", such as the left operand of @code{set!}.
- You can only use procedures that have an associated @dfn{setter}.
- In that case, @code{(set! (f arg ...) value)}
- is equivalent to @code{((setter f) arg ... value)}
- Currently, only a few procedures have associated @code{setter}s,
- and only builtin procedures written in Java can have @code{setter}s.
- For example:
- @example
- (set! (car x) 10)
- @end example
- is equivalent to:
- @example
- ((setter car) x 10)
- @end example
- which is equivalent to:
- @example
- (set-car! x 10)
- @end example
- @deffn Procedure setter procedure
- Gets the "setter procedure" associated with a "getter procedure".
- Equivalent to @code{(procedure-property @var{procedure} 'setter)}.
- By convention, a setter procedure takes the same parameters as
- the "getter" procedure, plus an extra parameter that is the
- new value to be stored in the location specified by the parameters.
- The expectation is that following
- @code{((setter @var{proc}) @var{args} ... @var{value})} then
- the value of @code{(@var{proc} @var{args} ...)} will be @var{value}.
- The @code{setter} of @code{setter} can be used to set the
- @code{setter} property.
- For example the Scheme prologue effectively does the following:
- @example
- (set! (setter vector-set) vector-set!)
- @end example
- @end deffn
- Kawa also gives you access to locations as first-class values:
- @deffn Syntax location lvalue
- Returns a location object for the given @var{lvalue}.
- You can get its value (by applying it, as if it were a procedure),
- and you can set its value (by using @code{set!} on the application).
- The @var{lvalue} can be a local or global variable, or a procedure
- call using a procedure that has a @code{setter}.
- @example
- (define x 100)
- (define lx (location x))
- (set! (lx) (cons 1 2)) ;; set x to (1 . 2)
- (lx) ;; returns (1 . 2)
- (define lc (location (car x)))
- (set! (lc) (+ 10 (lc)))
- ;; x is now (11 . 2)
- @end example
- @end deffn
- @deffn Syntax define-alias variable lvalue
- Define @var{variable} as an alias for @var{lvalue}.
- In other words, makes it so that @code{(location @var{variable})}
- is equivalent to @code{(location @var{lvalue})}.
- This works both top-level and inside a function.
- @end deffn
- @deffn Syntax define-private-alias variable lvalue
- Same as @code{define-alias}, but the @var{variable}
- is local to the current module.
- @end deffn
- Some people might find it helpful to think of a location
- as a settable @dfn{thunk}. Others may find it useful to
- think of the @code{location} syntax as similar to the C @samp{&} operator;
- for the @samp{*} indirection operator, Kawa uses procedure application.
- You can use @code{define-alias} to define a shorter type synonym,
- similar to Java's @code{import TypeName} (single-type-import) declaration:
- @example
- (define-alias StrBuf java.lang.StringBuffer)
- @end example
- @node Parameter objects, , Locations, Eval and Environments
- @section Parameter objects
- A parameter object is a procedure that is bound to a location,
- and may optionally have a conversion procedure.
- The procedure accepts zero or one argument.
- When the procedure is called with zero arguments,
- the content of the location is returned.
- On a call with one argument the content of the location
- is updated with the result of applying the parameter object's conversion
- procedure to the argument.
- Parameter objects are created with the @code{make-parameter} procedure
- which takes one or two arguments. The second argument is a one
- argument conversion procedure. If only one argument is passed to
- make-parameter the identity function is used as a conversion
- procedure.
- A new location is created and asociated with the
- parameter object. The initial content of the location is the
- result of applying the conversion procedure to the first argument of
- make-parameter.
- Note that the conversion procedure can be used for guaranteeing the
- type of the parameter object's binding and/or to perform some
- conversion of the value.
- The @code{parameterize} special form, when given a parameter object
- and a value, binds the parameter
- object to a new location for the dynamic extent of its body.
- The initial content of the location is the result of
- applying the parameter object's conversion procedure to the value. The
- @code{parameterize} special form behaves analogously to @code{let}
- when binding more than one parameter object (that is the order of
- evaluation is unspecified and the new bindings are only visible in the
- body of the parameterize special form).
- When a new thread is created using @code{future} or @code{runnable}
- then the child thread inherits initial values from its parent.
- Once the child is running, changing the value in the child does not
- affect the value in the parent or vice versa.
- (In the past this was not the case: The child would share a location
- with the parent except within a @code{parameterize}.
- This was changed to avoid unsafe and inefficient coupling between threads.)
- Note that @code{parameterize} and @code{fluid-let} have similar
- binding and sharing behavior.
- The difference is that @code{fluid-let} modifies locations
- accessed by name, while @code{make-parameter} and @code{parameterize}
- create anonymous locations accessed by calling a parameter procedure.
- The R5RS procedures @code{current-input-port} and @code{current-output-port}
- are parameter objects.
- @deffn Procedure make-parameter init [converter]
- Returns a new parameter object which is bound in the global dynamic
- environment to a location containing the value returned by the call
- @code{(@var{converter} @var{init})}. If the conversion procedure
- converter is not specified the identity function is used instead.
- The parameter object is a procedure which accepts zero or one
- argument. When it is called with no argument, the content of the
- location bound to this parameter object in the current dynamic
- environment is returned. When it is called with one argument, the
- content of the location is set to the result of the call
- @code{(@var{converter} @var{arg})}, where @var{arg} is the argument
- passed to the parameter object, and an unspecified value is returned.
- @example
- (define radix
- (make-parameter 10))
- (define write-shared
- (make-parameter
- #f
- (lambda (x)
- (if (boolean? x)
- x
- (error "only booleans are accepted by write-shared")))))
- (radix) @result{} 10
- (radix 2)
- (radix) @result{} 2
- (write-shared 0) gives an error
- (define prompt
- (make-parameter
- 123
- (lambda (x)
- (if (string? x)
- x
- (with-output-to-string (lambda () (write x)))))))
- (prompt) @result{} "123"
- (prompt ">")
- (prompt) @result{} ">"
- @end example
- @end deffn
- @anchor{parameterize-syntax}
- @deffn Syntax parameterize ((expr1 expr2) ...) @stxref{body}
- The expressions @var{expr1} and @var{expr2} are evaluated in an
- unspecified order. The value of the @var{expr1} expressions must be
- parameter objects. For each @var{expr1} expression and in an
- unspecified order, the local dynamic environment is extended with a
- binding of the parameter object @var{expr1} to a new location whose
- content is the result of the call @code{(@var{converter} @var{val})},
- where @var{val} is the value of @var{expr2} and @var{converter} is the
- conversion procedure of the parameter object. The resulting dynamic
- environment is then used for the evaluation of @var{body} (which
- refers to the R5RS grammar nonterminal of that name). The result(s) of
- the parameterize form are the result(s) of the @var{body}.
- @example
- (radix) @result{} 2
- (parameterize ((radix 16)) (radix)) @result{} 16
- (radix) @result{} 2
- (define (f n) (number->string n (radix)))
- (f 10) @result{} "1010"
- (parameterize ((radix 8)) (f 10)) @result{} "12"
- (parameterize ((radix 8) (prompt (f 10))) (prompt)) @result{} "1010"
- @end example
- @end deffn
- @node Debugging
- @chapter Debugging
- @deffn Syntax trace procedure
- Cause @var{procedure} to be "traced", that is debugging output will
- be written to the standard error port every time @var{procedure}
- is called, with the parameters and return value.
- Note that Kawa will normally assume that a procedure defined with
- the procedure-defining variant of @code{define} is constant,
- and so it might be inlined:
- @example
- (define (ff x) (list x x))
- (trace ff) ;; probably won't work
- (ff 3) ;; not traced
- @end example
- It works if you specify the @code{--no-inline} flag to Kawa.
- Alternatively, you can use the variable-defining variant of @code{define}:
- @example
- #|kawa:1|# (define ff (lambda (x) name: 'ff (list x x)))
- #|kawa:2|# (trace ff) ;; works
- #|kawa:3|# (ff 3)
- call to ff (3)
- return from ff => (3 3)
- (3 3)
- @end example
- Note the use of the @code{name:} procedure property to give the
- anonymous @code{lambda} a name.
- @end deffn
- @deffn Syntax untrace procedure
- Turn off tracing (debugging output) of @var{procedure}.
- @end deffn
- @deffn Procedure disassemble procedure
- Returns a string representation of the disassembled bytecode
- for @var{procedure}, when known.
- @end deffn
- @node Input-Output
- @chapter Input, output, and file handling
- Kawa has a number of useful tools for controlling input and output:
- A programmable reader.
- A powerful pretty-printer.
- @menu
- * Named output formats::
- * Paths:: Paths - file name, URLs, and URIs
- * Files:: File System Interface
- * Reading and writing whole files::
- * Ports::
- * Format:: Formatted Output (Common-Lisp-style)
- * Pretty-printing::
- * Resources::
- @end menu
- @node Named output formats, Paths, , Input-Output
- @section Named output formats
- The @code{--output-format} (or @code{--format}) command-line switch
- can be used to override the default format for how values are
- printed on the standard output. This format is used for values printed
- by the read-eval-print interactive interface. It is also used to
- control how values are printed when Kawa evaluates a file named on the
- command line (using the @code{-f} flag or just a script name).
- (It also affects applications compiled with the @code{--main} flag.)
- It currently affects how values are printed by a @code{load},
- though that may change.
- The default format depends on the current programming language.
- For Scheme, the default is @code{scheme} for read-eval-print
- interaction, and @code{ignore} for files that are loaded.
- The formats currently supported include the following:
- @table @code
- @item scheme
- Values are printed in a format matching the Scheme programming language,
- as if using @code{display}. "Groups" or "elements" are written as lists.
- @item readable-scheme
- Like @code{scheme}, as if using @code{write}:
- Values are generally printed in a way that they can
- be read back by a Scheme reader. For example, strings have quotation marks,
- and character values are written like @samp{#\A}.
- @item elisp
- Values are printed in a format matching the Emacs Lisp programming language.
- Mostly the same as @code{scheme}.
- @item readable-elisp
- Like @code{elisp}, but values are generally printed in a way that they can
- be read back by an Emacs Lisp reader. For example, strings have quotation
- marks, and character values are written like @samp{?A}.
- @item clisp
- @itemx commonlisp
- Values are printed in a format matching the Common Lisp programming language,
- as if written by @code{princ}.
- Mostly the same as @code{scheme}.
- @item readable-clisp
- @itemx readable-commonlisp
- Like @code{clisp}, but as if written by @code{prin1}: values are generally
- printed in a way that they can be read back by a Common Lisp reader.
- For example, strings have quotation marks, and character values are
- written like @samp{#\A}.
- @item xml
- @itemx xhtml
- @itemx html
- Values are printed in XML, XHTML, or HTML format.
- This is discussed in more detail in @ref{Formatting XML}.
- @item cgi
- The output should follow the CGI standards. I.e. assume that this
- script is invoked by a web server as a CGI script/program, and that the
- output should start with some response header,
- followed by the actual response data.
- To generate the response headers, use the @code{response-header} function.
- If the @code{Content-type} response header has not been specified, and
- it is required by the CGI standard, Kawa will attempt
- to infer an appropriate @code{Content-type} depending on the following value.
- @item ignore
- Top-level values are ignored, instead of printed.
- @end table
- @node Paths
- @section Paths - file name, URLs, and URIs
- A @dfn{Path} is the name of a file or some other @dfn{resource}.
- The path mechanism provides a layer of abstraction, so you can
- use the same functions on either a filename or a URL/URI.
- Functions that in standard Scheme take a filename
- have been generalized to take a path or a path string,
- as if using the @code{path} function below. For example:
- @example
- (open-input-file "http://www.gnu.org/index.html")
- (open-input-file (URI "ftp://ftp.gnu.org/README"))
- @end example
- @deffn Type path
- A general path, which can be a @code{filename} or a @code{URI}.
- It can be either a @code{filename} or a @code{URI}.
- Represented using the abstract Java class @code{gnu.kawa.io.Path}.
- Coercing a value to a @code{Path} is equivalent to
- calling the @code{path} constructor documented below.
- @end deffn
- @deffn Constructor path arg
- Coerces the @var{arg} to a @code{path}.
- If @var{arg} is already a @code{path}, it is returned unchanged.
- If @var{arg} is a @code{java.net.URI}, or a @code{java.net.URL}
- then a @code{URI} value is returned.
- If @var{arg} is a @code{java.io.File}, a @code{filepath} value is returned.
- Otherwise, @var{arg} can be a string.
- A @code{URI} value is returned if the string starts with a URI scheme
- (such as @code{"http:"}),
- and a @code{filepath} value is returned otherwise.
- @end deffn
- @deffn Predicate path? arg
- True if @var{arg} is a @code{path} - i.e. an instance of a @code{gnu.kawa.io.Path}.
- @end deffn
- @deffn Procedure current-path [new-value]
- With no arguments, returns the default directory of the current thread
- as a @code{path}.
- This is used as the base directory for relative pathnames.
- The initial value is that of the @code{user.dir} property
- as returned by @code{(java.lang.System:getProperty "user.dir")}.
- If a @var{new-value} argument is given, sets the default directory:
- @example
- (current-path "/opt/myApp/")
- @end example
- A string value is automatically converted to a @code{path},
- normally a @code{filepath}.
- Alternatively, you can change the default using a setter:
- @example
- (set! (current-path) "/opt/myApp/")
- @end example
- Since @code{current-path} is a @ref{Parameter objects,parameter object}, you can
- locally change the value using @ref{parameterize-syntax,@code{parameterize}}.
- @end deffn
- @deffn Type filepath
- The name of a local file.
- Represented using the Java class @code{gnu.kawa.io.FilePath},
- which is a wrapper around @code{java.io.File}.
- @end deffn
- @deffn Predicate filepath? arg
- True if @var{arg} is a @code{filepath} - i.e. an instance of
- a @code{gnu.kawa.io.FilePath}.
- @end deffn
- @anchor{URI-type}
- @deffn Type URI
- A Uniform Resource Indicator, which is a generalization of
- the more familiar URL. The general format is specified by
- @uref{http://www.ietf.org/rfc/rfc2396.txt,
- RFC 2396: Uniform Resource Identifiers (URI): Generic Syntax}.
- Represented using the Java class @code{gnu.kawa.io.URIPath},
- which is a wrapper around @code{java.net.URI}.
- A URI can be a URL, or it be a relative URI.
- @end deffn
- @deffn Predicate URI? arg
- True if @var{arg} is a @code{URI} - i.e. an instance of
- a @code{gnu.kawa.io.URIPath}.
- @end deffn
- @deffn Type URL
- A Uniform Resource Locator - a subtype of @code{URI}.
- Represented using the Java class @code{gnu.kawa.io.URLPath},
- which is a wrapper around a @code{java.net.URL}, in
- addition to extending @code{gnu.kawa.io.URIPath}.
- @end deffn
- @subsection Extracting Path components
- @deffn Procedure path-scheme arg
- Returns the ``URI scheme'' of @var{arg} (coerced to a @code{path}) if it is
- defined, or @code{#f} otherwise. The URI scheme of a @code{filepath}
- is @code{"file"} if the @code{filepath} is absolute, and @code{#f} otherwise.
- @example
- (path-scheme "http://gnu.org/") @result{} "http"
- @end example
- @end deffn
- @deffn Procedure path-authority arg
- Returns the authority part of @var{arg} (coerced to a @code{path}) if it is
- defined, or @code{#f} otherwise.
- The ``authority'' is usually the hostname, but may also include user-info
- or a port-number.
- @example
- (path-authority "http://me@@localhost:8000/home") @result{} "me@@localhost:8000"
- @end example
- @end deffn
- @deffn Procedure path-host arg
- Returns the name name part of @var{arg} (coerced to a @code{path}) if it is
- defined, or @code{#f} otherwise.
- @example
- (path-host "http://me@@localhost:8000/home") @result{} "localhost"
- @end example
- @end deffn
- @deffn Procedure path-user-info arg
- Returns the ``user info'' of @var{arg} (coerced to a @code{path}) if it is
- specified, or @code{#f} otherwise.
- @example
- (path-host "http://me@@localhost:8000/home") @result{} "me"
- @end example
- @end deffn
- @deffn Procedure path-port arg
- Returns the port number of @var{arg} (coerced to a @code{path}) if it is
- specified, or @code{-1} otherwise. Even if there is a default port
- associated with a URI scheme (such as 80 for @code{http}), the value
- -1 is returned unless the port number is @emph{explictly} specified.
- @example
- (path-host "http://me@@localhost:8000/home") @result{} 8000
- (path-host "http://me@@localhost/home") @result{} -1
- @end example
- @end deffn
- @deffn Procedure path-file arg
- Returns the ``path component'' of the @var{arg}
- (coerced to a @code{path}).
- (The name @code{path-path} might be more logical,
- but it is obviously a bit awkward.)
- The path component of a file name is the file name itself.
- For a URI, it is the main hierarchical part of the URI,
- without schema, authority, query, or fragment.
- @example
- (path-file "http://gnu.org/home/me.html?add-bug#body") @result{} "/home/me.html"
- @end example
- @end deffn
- @deffn Procedure path-directory arg
- If @var{arg} (coerced to a @code{path}) is directory,
- return @var{arg}; otherwise return the ``parent'' path, without the
- final component.
- @example
- (path-directory "http://gnu.org/home/me/index.html#body")
- @result{} (path "http://gnu.org/home/me/")
- (path-directory "http://gnu.org/home/me/")
- @result{} (path "http://gnu.org/home/me/")
- @end example
- @code{(path-directory "./dir")} @code{@result{}} @code{(path "./dir")} if @code{dir} is a directory, and @code{(path ".")} otherwise.
- @end deffn
- @deffn Procedure path-parent arg
- Returns the ``parent directory'' of @var{arg} (coerced to a @code{path}).
- If @var{arg} is not a directory, same as @code{path-directory @var{arg}}.
- @example
- (path-parent "a/b/c") @result{} (path "a/b")
- (path-parent "file:/a/b/c") @result{} (path "file:/a/b/c")
- (path-parent "file:/a/b/c/") @result{} (path "file:/a/b/")
- @end example
- @end deffn
- @deffn Procedure path-last arg
- The last component of path component
- of @var{arg} (coerced to a @code{path}).
- Returns a substring of @code{(path-file @var{arg})}.
- If that string ends with @samp{/} or the path separator,
- that last character is ignored.
- Returns the tail of the path-string, following
- the last (non-final) @samp{/} or path separator.
- @example
- (path-last "http:/a/b/c") @result{} "c"
- (path-last "http:/a/b/c/") @result{} "c"
- (path-last "a/b/c") @result{} "c"
- @end example
- @end deffn
- @deffn Procedure path-extension arg
- Returns the ``extension'' of the @var{arg}
- (coerced to a @code{path}).
- @example
- (path-extension "http://gnu.org/home/me.html?add-bug#body") @result{} "html"
- (path-extension "/home/.init") @result{} #f
- @end example
- @end deffn
- @deffn Procedure path-query arg
- Returns the query part of @var{arg} (coerced to a @code{path}) if it is
- defined, or @code{#f} otherwise. The query part of a URI is the
- part after @samp{?}.
- @example
- (path-query "http://gnu.org/home?add-bug") @result{} "add-bug"
- @end example
- @end deffn
- @deffn Procedure path-fragment arg
- Returns the fragment part of @var{arg} (coerced to a @code{path}) if it is
- defined, or @code{#f} otherwise. The fragment of a URI is the
- part of after @samp{#}.
- @example
- (path-query "http://gnu.org/home#top") @result{} "top"
- @end example
- @end deffn
- @deffn Procedure resolve-uri uri base
- Returns a @var{uri} unchanged if it is an absolute URI.
- Otherwise resolves it against a base URI @var{base},
- which is normally (though not always) absolute.
- This uses the algorithm specifyed by RFC-3986 (assuming @var{base}
- is absolute), unlike the obsolete RFC-2396 algorithm used
- by @code{java.net.URI.resolve}.
- @end deffn
- @node Files
- @section File System Interface
- @deffn Procedure file-exists? filename
- Returns true iff the file named @var{filename} actually exists.
- This function is defined on arbitrary @code{path} values:
- for URI values we open a @code{URLConnection}
- and invoke @code{getLastModified()}.
- @end deffn
- @deffn Procedure file-directory? filename
- Returns true iff the file named @var{filename} actually exists
- and is a directory.
- This function is defined on arbitrary @code{path} values;
- the default implementation for non-file objects is to
- return @code{#t} iff the path string ends with the character @samp{/}.
- @end deffn
- @deffn Procedure file-readable? filename
- Returns true iff the file named @var{filename} actually exists
- and can be read from.
- @end deffn
- @deffn Procedure file-writable? filename
- Returns true iff the file named @var{filename} actually exists
- and can be writen to.
- (Undefined if the @var{filename} does not exist,
- but the file can be created in the directory.)
- @end deffn
- @deffn Procedure delete-file filename
- Delete the file named @var{filename}.
- On failure, throws an exception.
- @end deffn
- @deffn Procedure rename-file oldname newname
- Renames the file named @var{oldname} to @var{newname}.
- @end deffn
- @deffn Procedure copy-file oldname newname-from path-to
- Copy the file named @var{oldname} to @var{newname}.
- The return value is unspecified.
- @end deffn
- @deffn Procedure create-directory dirname
- Create a new directory named @var{dirname}.
- Unspecified what happens on error (such as exiting file with the same name).
- (Currently returns @code{#f} on error, but may change to be more compatible
- with scsh.)
- @end deffn
- @deffn Procedure system-tmpdir
- Return the name of the default directory for temporary files.
- @end deffn
- @deffn Procedure make-temporary-file [format]
- Return a file with a name that does not match any existing file.
- Use @var{format} (which defaults to @code{"kawa~d.tmp"}) to generate
- a unique filename in @code{(system-tmpdir)}.
- The implementation is safe from race conditions,
- and the returned filename will not be reused by this JVM.
- @end deffn
- @node Reading and writing whole files
- @section Reading and writing whole files
- The following procedures and syntax allow you to read and write
- the entire contents of a file, without iterating using a port.
- @subsection Reading a file
- For reading the contents of a file in a single operation,
- you can use the following syntax:
- @display
- @stxlit{&<@lbracechar{}}@stxref{named-literal-part}+@stxlit{@rbracechar{}}
- @end display
- This is equivalent to using the @code{path-data} function (defined below):
- @display
- @stxlit{(path-data} @stxlit{&@lbracechar{}}@stxref{named-literal-part}+@stxlit{@rbracechar{})}
- @end display
- For example:
- @example
- (define dir "/home/me/")
- (define help-message &<@lbracechar{}&[dir]HELP@rbracechar{})
- @end example
- This binds @code{help-message} to the contents of the file
- named @code{HELP} in the @code{dir} directory.
- @anchor{Blobs}
- @subsection Blobs
- The content of a file is, in general, a sequence of uninterpreted bytes.
- Often these bytes represent text in a locale-dependent encoding,
- but we don't always know this. Sometimes they're images, or videos,
- or word-processor documents. A filename extension or a ``magic number''
- in the file can give you hints, but not certainty as to the type of the data.
- A @dfn{@uref{http://en.wikipedia.org/wiki/Binary_large_object,blob}}
- is a raw uninterpreted sequence of bytes. It is a @code{bytevector}
- that can be automatically converted to other types as needed,
- specifically to a string or a bytevector.
- The @code{&<@lbracechar{}..@rbracechar{}} returns a blob. For example,
- assume the file @code{README} contains (bytes representing)
- the text @code{"Check doc directory.\n"}. Then:
- @example
- #|kawa:1|# (define readme &<@lbracechar{}README@rbracechar{}))
- |kawa:2|# readme:class
- class gnu.lists.Blob
- #|kawa:3|# (write (->string readme))
- "Check doc directory.\n"
- #|kawa:4|# (write (->bytevector readme))
- #u8(67 104 101 99 107 32 100 111 99 32 100 105 114 101 99 116 111 114 121 46 10)
- #|kawa:5|# (->bytevector readme):class
- class gnu.lists.U8Vector
- @end example
- @subsection Writing to a file
- The @code{&<@lbracechar{}..@rbracechar{}} syntax can be used with @code{set!}
- to replace the contents of a file:
- @example
- (set! &<@lbracechar{}README@rbracechar{} "Check example.com\n")
- @end example
- The new contents must be blob-compatible - i.e. a bytevector or a string.
- If you dislike using @code{<} as an output operator, you can instead using the @code{&>@lbracechar{}..@rbracechar{}} operation, which evaluates to a function whose single argument is the new value:
- @example
- (&>@lbracechar{}README@rbracechar{} "Check example.com\n")
- @end example
- In general:
- @example
- @stxlit{&>@lbracechar{}}@stxref{named-literal-part}+@stxlit{@rbracechar{}}
- @end example
- is equivalent to:
- @example
- (lambda (new-contents)
- (set! @stxlit{&<@lbracechar{}}@stxref{named-literal-part}+@stxlit{@rbracechar{}} new-contents))
- @end example
- You can use @code{&>>} to append more data to a file:
- @example
- (&>>@lbracechar{}README@rbracechar{} "or check example2.com\n")
- @end example
- @subsection Functions
- @deffn Procedure path-data path
- Reads the contents of the file specified by @var{path},
- where @var{path} can be a @ref{Paths,path} object, or anything that can
- be converted to a @code{Path}, including a filename string or a URL.
- returning the result as a blob.
- The result is a @emph{blob}, which is a kind of bytevector than can be
- auto-converted to a string or bytevecor as required.
- The function @code{path-data} has a setter, which replaces the contents
- with new contents:
- @example
- (set! &<@lbracechar{}file-name@rbracechar{} new-contents)
- @end example
- @end deffn
- @deffn Procedure path-bytes path
- Reads the contents of the file specified by @var{path}, as
- with the @code{path-data} function, but the result is a plain bytevector,
- rather than a blob. This functtion also has a setter, which you
- can use to replace the file-contents by new bytevector-valued data.
- @end deffn
- @node Ports
- @section Ports
- Ports represent input and output devices.
- An input port is a Scheme object that can deliver data upon
- command, while an output port is a Scheme object that can
- accept data.
- Different @dfn{port types} operate on different data:
- @itemize @bullet
- @item
- A @dfn{textual port} supports reading or writing of individual
- characters from or to a backing store containing characters
- using @code{read-char} and @code{write-char} below, and it supports
- operations defined in terms of characters, such as @code{read} and
- @code{write}.
- @item
- A @dfn{binary port} supports reading or writing of individual
- bytes from or to a backing store containing bytes using
- @code{read-u8} and @code{write-u8} below, as well as operations defined
- in terms of bytes (integers in the range 0 to 255).
- All Kawa binary ports created by procedures documented here
- are also textual ports. Thus you can either read/write
- bytes as described above, or read/write
- characters whose scalar value is in the range 0 to 255
- (i.e. the Latin-1 character set), using @code{read-char} and @code{write-char}.
- A native binary port is a @code{java.io.InputStream}
- or @code{java.io.OutputStream} instance. These are not textual ports.
- You can use methods @code{read-u8} and @code{write-u8},
- @c not true?
- but not @code{read-char} and @code{write-char} on native binary ports.
- (The functions @code{input-port?}, @code{output-port?}, @code{binary-port?},
- and @code{port?} all currently return false on native binary ports,
- but that may change.)
- @end itemize
- @deffn Procedure call-with-port port proc
- The @code{call-with-port} procedure calls @var{proc} with port as an
- argument. If @var{proc} returns, then the port is closed automatically
- and the values yielded by the proc are returned.
- If @var{proc} does not return, then the port must not be closed
- automatically unless it is possible to prove that the port
- will never again be used for a read or write operation.
- As a Kawa extension, @var{port} may be any object
- that implements @code{java.io.Closeable}.
- It is an error if @var{proc} does not accept one argument.
- @c @emph{Rationale}: Because Scheme’s escape procedures have unlimited
- @c extent, it is possible to escape from the current continuation
- @c but later to resume it. If implementations were permitted to
- @c close the port on any escape from the current continuation,
- @c then it would be impossible to write portable code using both
- @c call-with-current-continuation and call-with-port.
- @end deffn
- @deffn Procedure call-with-input-file path proc
- @deffnx Procedure call-with-output-file path proc
- These procedures obtain a textual port obtained by
- opening the named file for input or output as if by
- @code{open-input-file} or @code{open-output-file}. The port and
- @var{proc} are then passed to a procedure equivalent to
- @code{call-with-port}.
- It is an error if @var{proc} does not accept one argument.
- @end deffn
- @deffn Procedure input-port? obj
- @deffnx Procedure output-port? obj
- @deffnx Procedure textual-port? obj
- @deffnx Procedure binary-port? obj
- @deffnx Procedure port? obj
- These procedures return @code{#t} if obj is an input port, output port,
- textual port, binary port, or any kind of port,
- respectively. Otherwise they return @code{#f}.
- These procedures currently return @code{#f} on a native Java streams
- (@code{java.io.InputStream} or @code{java.io.OutputStream}),
- a native reader (a @code{java.io.Reader} that is not an
- @code{gnu.mapping.Inport}), or a native writer (a @code{java.io.Writer}
- that is not an @code{gnu.mapping.Outport}). This may change if
- conversions between native ports and Scheme ports becomes more seamless.
- @end deffn
- @deffn Procedure input-port-open? port
- @deffnx Procedure output-port-open? port
- Returns @code{#t} if @var{port} is still open and capable of performing
- input or output, respectively, and @code{#f} otherwise.
- (Not supported for native binary ports - i.e. @code{java.io.InputStteam}
- or @code{java.io.OutputStream}.)
- @end deffn
- @deffn Procedure current-input-port
- @deffnx Procedure current-output-port
- @deffnx Procedure current-error-port
- Returns the current default input port, output port, or
- error port (an output port), respectively.
- (The error port is the port to which errors and warnings should be sent
- - the @dfn{standard error} in Unix and C terminology.)
- These procedures are @ref{Parameter objects,parameter objects},
- which can be overridden with @ref{parameterize-syntax,@code{parameterize}}.
- The initial bindings for @code{(current-output-port)} and
- @code{(current-error-port)} are hybrid textual/binary
- ports that wrap the values of the corresponding @code{java.lang.System} fields
- @code{out}, and @code{err}. The latter, in turn are bound
- to the standard output and error streams of the JVM process.
- This means you can write binary data to standard output
- using @code{write-bytevector} and @code{write-u8}.
- The initial value @code{(current-input-port)} similarly is a textual port
- that wraps the @code{java.lang.System} field @code{in}, which is bound
- to the standard input stream of the JVM process.
- It is a @emph{hybrid} textual/binary port only if there
- is no console (as determined by @code{(java.lang.System:console)}
- returning @code{#!null}) - i.e. if standard input is not a tty.
- Here is an example that copies standard input to standard output:
- @example
- (let* ((in (current-input-port))
- (out (current-output-port))
- (blen ::int 2048)
- (buf (make-bytevector blen)))
- (let loop ()
- (define n (read-bytevector! buf in))
- (cond ((not (eof-object? n))
- (write-bytevector buf out 0 n)
- (loop)))))
- @end example
- @end deffn
- @deffn Procedure with-input-from-file path thunk
- @deffnx Procedure with-output-to-file path thunk
- The file is opened for input or output as if by
- @code{open-input-file} or @code{open-output-file}, and the new port
- is made to be the value returned by @code{current-input-port}
- or @code{current-output-port} (as used by @code{(read)},
- @code{(write @var{obj})}, and so forth). The thunk is then called with no
- arguments. When the @var{thunk} returns, the port is closed
- and the previous default is restored. It is an error if @var{thunk}
- does not accept zero arguments. Both procedures return
- the values yielded by @var{thunk}. If an escape procedure is used
- to escape from the continuation of these procedures, they
- behave exactly as if the current input or output port had
- been bound dynamically with @code{parameterize}.
- @end deffn
- @deffn Procedure open-input-file path
- @deffnx Procedure open-binary-input-file path
- Takes a @var{path} naming an existing file and returns a textual
- input port or binary input port that is capable of delivering
- data from the file.
- @c If the file does not exist or cannot be
- @c opened, an error that satisfies file-error? is signaled.
- The procedure @code{open-input-file} checks the fluid variable
- @ref{port-char-encoding,@code{port-char-encoding}} to determine how bytes are decoded
- into characters. The procedure @code{open-binary-input-file}
- is equivalent to calling @code{open-input-file} with
- @code{port-char-encoding} set to @code{#f}.
- @end deffn
- @deffn Procedure open-output-file path
- @deffnx Procedure open-binary-output-file path
- Takes a @var{path} naming an output file to be created and returns
- respectively a textual output port or binary output port that is
- capable of writing data to a new file by that name. If a
- file with the given name already exists, the effect is unspecified.
- @c If the file cannot be opened, an error that satisfies
- @c file-error? is signaled.
- The procedure @code{open-output-file} checks the fluid variable
- @ref{port-char-encoding,@code{port-char-encoding}} to determine how characters are
- encoded as bytes. The procedure @code{open-binary-output-file}
- is equivalent to calling @code{open-output-file} with
- @code{port-char-encoding} set to @code{#f}.
- @end deffn
- @deffn Procedure close-port port
- @deffnx Procedure close-input-port port
- @deffnx Procedure close-output-port port
- Closes the resource associated with @var{port}, rendering the port
- incapable of delivering or accepting data. It is an error to
- apply the last two procedures to a port which is not an
- input or output port, respectively.
- (Specifically, @code{close-input-port} requires a @code{java.io.Reader},
- while @code{close-output-port} requires a @code{java.io.Writer}.
- In contrast @code{close-port} accepts any object whose class
- implements @code{java.io.Closeable}.)
- @c Scheme implementations may provide ports which are simultaneously input
- @c and output ports, such as sockets; the close-input-port
- @c and close-output-port procedures can then be used to
- @c close the input and output sides of the port independently.
- These routines have no effect if the port has already been
- closed.
- @end deffn
- @subsection String and bytevector ports
- @deffn Procedure open-input-string string
- Takes a string and returns a text input port that delivers characters
- from the string. The port can be closed by @code{close-input-port},
- though its storage will be reclaimed by the
- garbage collector if it becomes inaccessible.
- @example
- (define p
- (open-input-string "(a . (b c . ())) 34"))
- (input-port? p) @result{} #t
- (read p) @result{} (a b c)
- (read p) @result{} 34
- (eof-object? (peek-char p)) @result{} #t
- @end example
- @end deffn
- @deffn Procedure open-output-string
- Returns an textual output port that will accumulate characters
- for retrieval by @code{get-output-string}.
- The port can be closed by the procedure @code{close-output-port},
- though its storage will be reclaimed by the garbage collector
- if it becomes inaccessible.
- @example
- (let ((q (open-output-string))
- (x '(a b c)))
- (write (car x) q)
- (write (cdr x) q)
- (get-output-string q)) @result{} "a(b c)"
- @end example
- @end deffn
- @deffn Procedure get-output-string output-port
- Given an output port created by @code{open-output-string},
- returns a string consisting of the characters that have been
- output to the port so far in the order they were output.
- If the result string is modified, the effect is unspecified.
- @example
- (parameterize
- ((current-output-port (open-output-string)))
- (display "piece")
- (display " by piece ")
- (display "by piece.")
- (newline)
- (get-output-string (current-output-port)))
- @result{} "piece by piece by piece.\n"
- @end example
- @end deffn
- @deffn Procedure call-with-input-string string proc
- Create an input port that gets its data from @var{string},
- call @var{proc} with that port as its one argument, and return
- the result from the call of @var{proc}
- @end deffn
- @deffn Procedure call-with-output-string proc
- Create an output port that writes its data to a @var{string},
- and call @var{proc} with that port as its one argument.
- Return a string consisting of the data written to the port.
- @end deffn
- @deffn Procedure open-input-bytevector bytevector
- Takes a bytevector and returns a binary input port that
- delivers bytes from the bytevector.
- @end deffn
- @deffn Procedure open-output-bytevector
- Returns a binary output port that will accumulate bytes
- for retrieval by @code{get-output-bytevector}.
- @end deffn
- @deffn Procedure get-output-bytevector port
- Returns a bytevector consisting of the bytes that have been
- output to the port so far in the order they were output.
- It is an error if @var{port} was not created with @code{open-output-bytevector}.
- @end deffn
- @subsection Input
- If @var{port} is omitted from any input procedure, it defaults
- to the value returned by @code{(current-input-port)}. It is an
- error to attempt an input operation on a closed port.
- @deffn Procedure read [port]
- The @code{read} procedure converts external representations of
- Scheme objects into the objects themselves. That is, it is
- a parser for the non-terminal @stxref{datum}.
- It returns the next object parsable from the
- given textual input port, updating port to point to the
- first character past the end of the external representation
- of the object.
- If an end of file is encountered in the input before any
- characters are found that can begin an object, then an
- end-of-file object is returned. The port remains open, and
- further attempts to read will also return an end-of-file object.
- If an end of file is encountered after the beginning of
- an object’s external representation, but the external representation
- is incomplete and therefore not parsable, an error
- that satisfies @code{read-error?} is signaled.
- @end deffn
- @deffn Procedure read-char [port]
- Returns the next character available from the textual input
- @var{port}, updating the port to point to the following character.
- If no more characters are available, an end-of-file value is
- returned.
- The result type is @code{character-or-eof}.
- @end deffn
- @deffn Procedure peek-char [port]
- Returns the next character available from the textual input @var{port},
- but @emph{without} updating the port to point to the
- following character. If no more characters are available, an
- end-of-file value is returned.
- The result type is @code{character-or-eof}.
- @emph{Note:} The value returned by a call to @code{peek-char} is the same as
- the value that would have been returned by a call to @code{read-char}
- with the same @var{port}. The only difference is that the very next call
- to @code{read-char} or @code{peek-char} on that @var{port} will return the
- value returned by the preceding call to @code{peek-char}. In particular, a
- call to @code{peek-char} on an interactive port will hang waiting for
- input whenever a call to @code{read-char} would have hung.
- @end deffn
- @deffn Procedure read-line [port [handle-newline]]
- Reads a line of input from the textual input @var{port}.
- The @var{handle-newline} parameter determines what is done with
- terminating end-of-line delimiter.
- The default, @code{'trim}, ignores the delimiter;
- @code{'peek} leaves the delimiter in the input stream;
- @code{'concat} appends the delimiter to the returned value;
- and @code{'split} returns the delimiter as a second value.
- You can use the last three options to tell if the string was
- terminated by end-or-line or by end-of-file.
- If an end of file is encountered before any end of
- line is read, but some characters have been read, a string
- containing those characters is returned.
- (In this case, @code{'trim}, @code{'peek}, and @code{'concat}
- have the same result and effect. The @code{'split} case returns two
- values: The characters read, and the delimiter is an empty string.)
- If an end of file is encountered before any characters are read,
- an end-of-file object is returned.
- For the purpose of this procedure, an
- end of line consists of either a linefeed character, a carriage
- return character, or a sequence of a carriage return character
- followed by a linefeed character.
- @end deffn
- @deffn Procedure eof-object? obj
- Returns @code{#t} if @var{obj} is an end-of-file object,
- otherwise returns @code{#f}.
- @code{Performance note}: If @var{obj} has type @code{character-or-eof},
- this is compiled as an @code{int} comparison with -1.
- @end deffn
- @deffn Procedure eof-object
- Returns an end-of-file object.
- @end deffn
- @deffn Procedure char-ready? [port]
- Returns @code{#t} if a character is ready on the textual input
- @var{port} and returns @code{#f} otherwise. If char-ready returns @code{#t}
- then the next @code{read-char} operation on the given @var{port} is
- guaranteed not to hang. If the port is at end of file then
- @code{char-ready?} returns @code{#t}.
- @emph{Rationale:} The @code{char-ready?} procedure exists to make it
- possible for a program to accept characters from interactive ports
- without getting stuck waiting for input. Any input editors as-
- sociated with such ports must ensure that characters whose
- existence has been asserted by @code{char-ready?} cannot be removed
- from the input. If @code{char-ready?} were to return @code{#f} at end of
- file, a port at end-of-file would be indistinguishable from an
- interactive port that has no ready characters.
- @end deffn
- @deffn Procedure read-string k [port]
- Reads the next @var{k} characters, or as many as are available
- before the end of file, from the textual input @var{port} into a
- newly allocated string in left-to-right order and returns the
- string. If no characters are available before the end of file,
- an end-of-file object is returned.
- @end deffn
- @deffn Procedure read-u8 [port]
- Returns the next byte available from the binary input @var{port},
- updating the @var{port} to point to the following byte. If no more
- bytes are available, an end-of-file object is returned.
- @end deffn
- @deffn Procedure peek-u8 [port]
- Returns the next byte available from the binary input @var{port},
- but @emph{without} updating the @var{port} to point to the following byte.
- If no more bytes are available, an end-of-file object is returned.
- @end deffn
- @deffn Procedure u8-ready? [port]
- Returns @code{#t} if a byte is ready on the binary input @var{port} and
- returns @code{#f} otherwise. If @code{u8-ready?} returns @code{#t} then the
- next @code{read-u8} operation on the given port is guaranteed
- not to hang. If the port is at end of file then @code{u8-ready?}
- returns @code{#t}.
- @end deffn
- @deffn Procedure read-bytevector k [port]
- Reads the next @var{k} bytes, or as many as are available before
- the end of file, from the binary input @var{port} into a newly
- allocated bytevector in left-to-right order and returns the
- bytevector. If no bytes are available before the end of file,
- an end-of-file object is returned.
- @end deffn
- @deffn Procedure read-bytevector! bytevector [port [start [end]]]
- Reads the next @var{end} − @var{start} bytes, or as many as are
- available before the end of file, from the binary input @var{port}
- into @var{bytevector} in left-to-right order beginning at the @var{start}
- position. If @var{end} is not supplied, reads until the end of
- @var{bytevector} has been reached. If @var{start} is not supplied, reads
- beginning at position 0. Returns the number of bytes read.
- If no bytes are available, an end-of-file object is returned.
- @end deffn
- @subsection Output
- If @var{port} is omitted from any output procedure, it defaults
- to the value returned by @code{(current-output-port)}. It is an
- error to attempt an output operation on a closed port.
- The return type of these methods is @code{void}.
- @deffn Procedure write obj [port]
- Writes a representation of @var{obj} to the given textual output
- @var{port}. Strings that appear in the written representation
- are enclosed in quotation marks, and within those strings
- backslash and quotation mark characters are escaped by
- backslashes.
- Symbols that contain non-ASCII characters
- are escaped with vertical lines.
- Character objects are written using the @code{#\} notation.
- If @var{obj} contains cycles which would cause an infinite loop
- using the normal written representation, then at least the
- objects that form part of the cycle must be represented
- using @ref{datum labels}. Datum
- labels must not be used if there are no cycles.
- @end deffn
- @deffn Procedure write-shared obj [port]
- The @code{write-shared} procedure is the same as @code{write}, except
- that shared structure must be represented using datum
- labels for all pairs and vectors that appear more than once
- in the output.
- @end deffn
- @deffn Procedure write-simple obj [port]
- The @code{write-simple} procedure is the same as @code{write}, except
- that shared structure is never represented using datum labels.
- This can cause write-simple not to terminate if @var{obj}
- contains circular structure.
- @end deffn
- @deffn Procedure display obj [port]
- Writes a representation of @var{obj} to the given textual output
- port. Strings that appear in the written representation
- are output as if by @code{write-string} instead of by @code{write}.
- Symbols are not escaped. Character objects appear in the
- representation as if written by @code{write-char} instead of by
- @code{write}.
- The @code{display} representation of other objects is unspecified.
- @c However, display must always terminate. Thus if the normal write
- @c representation is used, datum labels are needed
- @c to represent cycles as in write.
- @end deffn
- @deffn Procedure newline [port]
- Writes an end of line to textual output @var{port}.
- This is done using the @code{println} method
- of the Java class @code{java.io.PrintWriter}.
- @end deffn
- @deffn Procedure write-char char [port]
- Writes the character @var{char} (not an external representation
- of the character) to the given textual output @var{port}.
- @end deffn
- @deffn Procedure write-string string [port [start [end]]]
- Writes the characters of @var{string} from @var{start} to @var{end}
- in left-to-right order to the textual output @var{port}.
- @end deffn
- @deffn Procedure write-u8 byte [port]
- Writes the @var{byte} to the given binary output port.
- @end deffn
- @deffn Procedure write-bytevector bytevector [port [start [end]]]
- Writes the bytes of @var{bytevector} from @var{start} to @var{end}
- in left-to-right order to the binary output @var{port}.
- @end deffn
- @deffn Procedure flush-output-port [port]
- @deffnx Procedure force-output [port]
- Forces any pending output on @var{port} to be delivered to the output file or
- device and returns an unspecified value. If the @var{port} argument is
- omitted it defaults to the value returned by @code{(current-output-port)}.
- (The name @code{force-output} is older,
- while R6RS added @code{flush-output-port}. They have the same effect.)
- @end deffn
- @anchor{Prompts}
- @cindex prompts
- @subsection Prompts for interactive consoles (REPLs)
- When an interactive input port is used for a read-eval-print-loop (REPL
- or console) it is traditional for the REPL to print a short
- @dfn{prompt} string to signal that the user is expected to type
- an expression. These prompt strings can be customized.
- @anchor{input-prompt1}
- @defvar input-prompt1
- @defvarx input-prompt2
- These are fluid variable whose values are string templates
- with placeholders similar to @code{printf}-style format.
- The placeholders are expanded (depending on the current state),
- and the resulting string printed in front of the input line.
- The @code{input-prompt1} is used normally. For multi-line input commands
- (for example if the first line is incomplete), @code{input-prompt1}
- is used for the first line of each command, while @code{input-prompt2} is
- used for subsequent ``continuation'' lines.
- The following placeholders are handled:
- @table @asis
- @item @stxlit{%%}
- A literal @samp{%}.
- @item @stxlit{%N}
- The current line number. This is @code{(+ 1 (port-line @var{port}))}.
- @item @stxlit{%}@var{n}@stxlit{P}@var{c}
- Insert padding at this possion, repeating the following
- character @code{@var{c}} as needed to bring the total number of columns
- of the prompt to that specified by the digits @code{@var{n}}.
- @item @stxlit{%P}@var{c}
- Same as @code{%@var{n}P@var{c}}, but @code{@var{n}} defaults to the number
- of columns in the initial prompt from the expansion of @code{input-prompt1}.
- This is only meaningful when expanding @code{input-prompt2} for
- continuation lines.
- @item @stxlit{%}@stxlitlbrace{}@var{hidden}@stxlit{%@}}
- Same as @var{hidden}, but the characters of @var{hidden}
- are assumed to have zero visible width. Can be used for
- @uref{https://en.wikipedia.org/wiki/ANSI_escape_code,ANSI escape sequences}
- to change color or style:
- @example
- (set! input-prompt1 "%@{\e[48;5;51m%@}@{Kawa:%N@} %@{\e[0m%@}")
- @end example
- The above changes both the text and the background color
- (to a pale blue).
- @item @stxlit{%H}@var{c}@var{d}
- If running under DomTerm, use the characters @var{c} and @var{d} as a clickable
- mini-button to hide/show (fold) the command and its output.
- (When output is visible @var{c} is displayed; clicking on it hides the output.
- When output is hidden @var{d} is displayed; clicking on it shows the output.)
- Ignored if not running under DomTerm.
- @item @stxlit{%M}
- Insert a ``message'' string.
- Not normally used by Kawa, but supported by JLine.
- @end table
- These variables can be initialized by the command-line
- arguments @code{console:prompt1=@var{prompt1}} and
- @code{console:prompt2=@var{prompt2}}, respectively. If these are
- not specified, languages-specific defaults are used.
- For example for Scheme the default value of @code{input-prompt1}
- is @code{"#|%H▼▶kawa:%N|# "} and @code{input-prompt2} is @code{"#|%P.%N| "}.
- These have the form of Scheme comments, to make it easier to cut-and-paste.
- If @code{input-prompt1} (respectively @code{input-prompt2}) does not contain
- an escape sequence (either @code{"%@{} or the escape character @code{"\e"})
- then ANSI escape sequences are added to to highlight the prompt.
- (Under DomTerm this sets the @code{prompt} style, which can be customised
- with CSS but defaults to a light green background;
- if using JLine the background is set to light green.)
- @end defvar
- For greater flexibility, you can also set a prompter procedure.
- @deffn Procedure set-input-port-prompter! port prompter
- Set the prompt procedure associated with @var{port} to @var{prompter},
- which must be a one-argument procedure taking an input port,
- and returning a string.
- The procedure is called before reading the first line of a command;
- its return value is used as the first-line prompt.
- The prompt procedure can have side effects.
- In Bash shell terms: It combines the features of @code{PROMPT_COMMAND}
- and @code{PS1}.
- The initial @var{prompter} is @code{default-prompter},
- which returns the expansion of @code{input-prompt1}.
- @end deffn
- @deffn Procedure input-port-prompter port
- Get the prompt procedure associated with @var{port}.
- @end deffn
- @deffn Procedure default-prompter port
- The default prompt procedure.
- Normally (i.e. when @code{input-port-read-state} is a space)
- returns @code{input-prompt1} after expanding the @code{%}-placeholders.
- Can also expand @code{input-prompt2} when @code{input-port-read-state}
- is not whitespace.
- @end deffn
- @subsection Line numbers and other input port properties
- @deffn Function port-column input-port
- @deffnx Function port-line input-port
- Return the current column number or line number of @var{input-port},
- using the current input port if none is specified.
- If the number is unknown, the result is @code{#f}. Otherwise,
- the result is a 0-origin integer - i.e. the first character
- of the first line is line 0, column 0. (However, when you
- display a file position, for example in an error message,
- we recommend you add 1 to get 1-origin integers. This is
- because lines and column numbers traditionally start with
- 1, and that is what non-programmers will find most natural.)
- @end deffn
- @deffn Procedure set-port-line! port line
- Set (0-origin) line number of the current line of @var{port} to @var{num}.
- @end deffn
- @deffn Procedure input-port-line-number port
- Get the line number of the current line of @var{port},
- which must be a (non-binary) input port.
- The initial line is line 1.
- Deprecated; replaced by @code{(+ 1 (port-line @var{port}))}.
- @end deffn
- @deffn Procedure set-input-port-line-number! port num
- Set line number of the current line of @var{port} to @var{num}.
- Deprecated; replaced by @code{(set-port-line! @var{port} (- @var{num} 1))}.
- @end deffn
- @deffn Procedure input-port-column-number port
- Get the column number of the current line of @var{port},
- which must be a (non-binary) input port.
- The initial column is column 1.
- Deprecated; replaced by @code{(+ 1 (port-column @var{port}))}.
- @end deffn
- @deffn Procedure input-port-read-state port
- Returns a character indicating the current @code{read} state of the @var{port}.
- Returns @code{#\Return} if not current doing a @var{read},
- @code{#\"} if reading a string; @code{#\|} if reading a comment; @code{#\(}
- if inside a list; and @code{#\Space} when otherwise in a @code{read}.
- The result is intended for use by prompt prcedures, and is not necessarily
- correct except when reading a new-line.
- @end deffn
- @defvar symbol-read-case
- A symbol that controls how @code{read} handles letters when reading a symbol.
- If the first letter is @samp{U}, then letters in symbols are upper-cased.
- If the first letter is @samp{D} or @samp{L}, then letters
- in symbols are down-cased.
- If the first letter is @samp{I}, then the case of letters in symbols
- is inverted.
- Otherwise (the default), the letter is not changed.
- (Letters following a @samp{\} are always unchanged.)
- The value of @code{symbol-read-case} only checked
- when a reader is created, not each time a symbol is read.
- @end defvar
- @subsection Miscellaneous
- @anchor{port-char-encoding}
- @defvar port-char-encoding
- Controls how bytes in external files are converted to/from internal
- Unicode characters. Can be either a symbol or a boolean.
- If @code{port-char-encoding} is @code{#f}, the file is assumed
- to be a binary file and no conversion is done.
- Otherwise, the file is a text file. The default is @code{#t}, which
- uses a locale-dependent conversion. If @code{port-char-encoding}
- is a symbol, it must be the name of a character encoding known to Java.
- For all text files (that is if @code{port-char-encoding} is not @code{#f}),
- on input a @code{#\Return} character or
- a @code{#\Return} followed by @code{#\Newline}
- are converted into plain @code{#\Newline}.
- This variable is checked when the file is opened; not when actually
- reading or writing. Here is an example of how you can safely
- change the encoding temporarily:
- @example
- (define (open-binary-input-file name)
- (fluid-let ((port-char-encoding #f)) (open-input-file name)))
- @end example
- @end defvar
- @defvar *print-base*
- The number base (radix) to use by default when printing rational numbers.
- Must be an integer between 2 and 36, and the default is of course 10.
- For example setting @code{*print-base*} to 16 produces hexadecimal output.
- @end defvar
- @defvar *print-radix*
- If true, prints an indicator of the radix used when printing rational numbers.
- If @code{*print-base*} is respectively 2, 8, or 16, then
- @code{#b}, @code{#o} or @code{#x} is written before the number;
- otherwise @code{#@var{N}r} is written, where @code{@var{N}} is the base.
- An exception is when @code{*print-base*} is 10, in which case a period
- is written @emph{after} the number, to match Common Lisp; this may
- be inappropriate for Scheme, so is likely to change.
- @end defvar
- @defvar *print-right-margin*
- The right margin (or line width) to use when pretty-printing.
- @end defvar
- @defvar *print-miser-width*
- If this an integer, and the available width is less or equal to this value,
- then the pretty printer switch to the more @dfn{miser} compact style.
- @end defvar
- @defvar *print-xml-indent*
- When writing to XML, controls pretty-printing and indentation.
- If the value is @code{'always} or @code{'yes} force each element to
- start on a new suitably-indented line.
- If the value is @code{'pretty} only force new lines for elements that
- won't fit completely on a line.
- The the value is @code{'no} or unset, don't add extra whitespace.
- @end defvar
- @node Format, , Ports, Input-Output
- @section Formatted Output (Common-Lisp-style)
- @deffn Procedure format destination fmt . arguments
- An almost complete implementation of Common LISP format description
- according to the CL reference book @cite{Common LISP} from Guy L.
- Steele, Digital Press. Backward compatible to most of the available
- Scheme format implementations.
- Returns @code{#t}, @code{#f} or a string; has side effect of printing
- according to @var{fmt}. If @var{destination} is @code{#t},
- the output is to the current output port and @code{#!void} is returned. If
- @var{destination} is @code{#f}, a formatted string is returned as the
- result of the call. If @var{destination} is a string,
- @var{destination} is regarded as the format string; @var{fmt} is
- then the first argument and the output is returned as a string. If
- @var{destination} is a number, the output is to the current error port
- if available by the implementation. Otherwise @var{destination} must be
- an output port and @code{#!void} is returned.@refill
- @var{fmt} must be a string or an instance of @code{gnu.text.MessageFormat}
- or @code{java.text.MessageFormat}. If @var{fmt} is a string,
- it is parsed as if by @code{parse-format}.
- @end deffn
- @deffn Procedure parse-format format-string
- Parses @code{format-string}, which is a string of the form of a Common LISP
- format description. Returns an instance of @code{gnu.text.ReportFormat},
- which can be passed to the @code{format} function.
- @end deffn
- A format string passed to @code{format} or @code{parse-format}
- consists of format directives (that start with @samp{~}),
- and regular characters (that are written directly to the destination).
- Most of the Common Lisp (and Slib) format directives are implemented.
- Neither justification, nor pretty-printing are supported yet.
- Plus of course, we need documentation for @code{format}!
- @subsection Implemented CL Format Control Directives
- Documentation syntax: Uppercase characters represent the corresponding
- control directive characters. Lowercase characters represent control
- directive parameter descriptions.
- @table @asis
- @cindex ~a
- @item @code{~A}
- Any (print as @code{display} does).
- @table @asis
- @item @code{~@@A}
- left pad.
- @item @code{~@var{mincol},@var{colinc},@var{minpad},@var{padchar}A}
- full padding.
- @end table
- @cindex ~s
- @item @code{~S}
- S-expression (print as @code{write} does).
- @table @asis
- @item @code{~@@S}
- left pad.
- @item @code{~@var{mincol},@var{colinc},@var{minpad},@var{padchar}S}
- full padding.
- @end table
- @cindex ~c
- @item @code{~C}
- Character.
- @table @asis
- @item @code{~@@C}
- prints a character as the reader can understand it (i.e. @code{#\} prefixing).
- @item @code{~:C}
- prints a character as emacs does (eg. @code{^C} for ASCII 03).
- @end table
- @end table
- @subsection Formatting Integers
- @table @asis
- @cindex ~d
- @item @code{~D}
- Decimal.
- @table @asis
- @item @code{~@@D}
- print number sign always.
- @item @code{~:D}
- print comma separated.
- @item @code{~@var{mincol},@var{padchar},@var{commachar},@var{commawidth}D}
- padding.
- @end table
- @cindex ~x
- @item @code{~X}
- Hexadecimal.
- @table @asis
- @item @code{~@@X}
- print number sign always.
- @item @code{~:X}
- print comma separated.
- @item @code{~@var{mincol},@var{padchar},@var{commachar},@var{commawidth}X}
- padding.
- @end table
- @cindex ~o
- @item @code{~O}
- Octal.
- @table @asis
- @item @code{~@@O}
- print number sign always.
- @item @code{~:O}
- print comma separated.
- @item @code{~@var{mincol},@var{padchar},@var{commachar},@var{commawidth}O}
- padding.
- @end table
- @cindex ~b
- @item @code{~B}
- Binary.
- @table @asis
- @item @code{~@@B}
- print number sign always.
- @item @code{~:B}
- print comma separated.
- @item @code{~@var{mincol},@var{padchar},@var{commachar},@var{commawidth}B}
- padding.
- @end table
- @cindex ~r
- @item @code{~@var{n}R}
- Radix @var{n}.
- @table @asis
- @item @code{~@var{n},@var{mincol},@var{padchar},@var{commachar},@var{commawidth}R}
- padding.
- @end table
- @item @code{~@@R}
- print a number as a Roman numeral.
- @item @code{~:@@R}
- print a number as an ``old fashioned'' Roman numeral.
- @item @code{~:R}
- print a number as an ordinal English number.
- @item @code{~R}
- print a number as a cardinal English number.
- @cindex ~p
- @item @code{~P}
- Plural.
- @table @asis
- @item @code{~@@P}
- prints @code{y} and @code{ies}.
- @item @code{~:P}
- as @code{~P but jumps 1 argument backward.}
- @item @code{~:@@P}
- as @code{~@@P but jumps 1 argument backward.}
- @end table
- @end table
- @var{commawidth} is the number of characters between two comma characters.
- @subsection Formatting real numbers
- @table @asis
- @cindex ~f
- @item @code{~F}
- Fixed-format floating-point (prints a flonum like @var{mmm.nnn}).
- @table @asis
- @item @code{~@var{width},@var{digits},@var{scale},@var{overflowchar},@var{padchar}F}
- @item @code{~@@F}
- If the number is positive a plus sign is printed.
- @end table
- @end table
- @table @asis
- @cindex ~e
- @item @code{~E}
- Exponential floating-point (prints a flonum like @var{mmm.nnn}@code{E}@var{ee})
- @table @asis
- @item @code{~@var{width},@var{digits},@var{exponentdigits},@var{scale},@var{overflowchar},@var{padchar},@var{exponentchar}E}
- @item @code{~@@E}
- If the number is positive a plus sign is printed.
- @end table
- @end table
- @table @asis
- @cindex ~g
- @item @code{~G}
- General floating-point (prints a flonum either fixed or exponential).
- @table @asis
- @item @code{~@var{width},@var{digits},@var{exponentdigits},@var{scale},@var{overflowchar},@var{padchar},@var{exponentchar}G}
- @item @code{~@@G}
- If the number is positive a plus sign is printed.
- @end table
- A slight difference from Common Lisp: If the number is printed
- in fixed form and the fraction is zero,
- then a zero digit is printed for the fraction, if allowed by the @var{width}
- and @var{digits} is unspecified.
- @end table
- @table @asis
- @cindex ~$
- @item @code{~$}
- Dollars floating-point (prints a flonum in fixed with signs separated).
- @table @asis
- @item @code{~@var{digits},@var{scale},@var{width},@var{padchar}$}
- @item @code{~@@$}
- If the number is positive a plus sign is printed.
- @item @code{~:@@$}
- A sign is always printed and appears before the padding.
- @item @code{~:$}
- The sign appears before the padding.
- @end table
- @end table
- @subsection Miscellaneous formatting operators
- @table @asis
- @cindex ~%
- @item @code{~%}
- Newline.
- @table @asis
- @item @code{~@var{n}%}
- print @var{n} newlines.
- @end table
- @cindex ~&
- @item @code{~&}
- print newline if not at the beginning of the output line.
- @table @asis
- @item @code{~@var{n}&}
- prints @code{~&} and then @var{n-1} newlines.
- @end table
- @cindex ~|
- @item @code{~|}
- Page Separator.
- @table @asis
- @item @code{~@var{n}|}
- print @var{n} page separators.
- @end table
- @cindex ~~
- @item @code{~~}
- Tilde.
- @table @asis
- @item @code{~@var{n}~}
- print @var{n} tildes.
- @end table
- @cindex ~@i{newline}
- @item @code{~}<newline>
- Continuation Line.
- @table @asis
- @item @code{~:}<newline>
- newline is ignored, white space left.
- @item @code{~@@}<newline>
- newline is left, white space ignored.
- @end table
- @cindex ~t
- @item @code{~T}
- Tabulation.
- @table @asis
- @item @code{~@@T}
- relative tabulation.
- @item @code{~@var{colnum},@var{colinc}T}
- full tabulation.
- @end table
- @cindex ~?
- @item @code{~?}
- Indirection (expects indirect arguments as a list).
- @table @asis
- @item @code{~@@?}
- extracts indirect arguments from format arguments.
- @end table
- @cindex ~(
- @item @code{~(@var{str}~)}
- Case conversion (converts by @code{string-downcase}).
- @table @asis
- @item @code{~:(@var{str}~)}
- converts by @code{string-capitalize}.
- @item @code{~@@(@var{str}~)}
- converts by @code{string-capitalize-first}.
- @item @code{~:@@(@var{str}~)}
- converts by @code{string-upcase}.
- @end table
- @cindex ~*
- @item @code{~*}
- Argument Jumping (jumps 1 argument forward).
- @table @asis
- @item @code{~@var{n}*}
- jumps @var{n} arguments forward.
- @item @code{~:*}
- jumps 1 argument backward.
- @item @code{~@var{n}:*}
- jumps @var{n} arguments backward.
- @item @code{~@@*}
- jumps to the 0th argument.
- @item @code{~@var{n}@@*}
- jumps to the @var{n}th argument (beginning from 0)
- @end table
- @item @code{~[@var{str0}~;@var{str1}~;...~;@var{strn}~]}
- Conditional Expression (numerical clause conditional).
- @table @asis
- @cindex ~[
- @item @code{~@var{n}[}
- take argument from @var{n}.
- @item @code{~@@[}
- true test conditional.
- @item @code{~:[}
- if-else-then conditional.
- @cindex ~;
- @item @code{~;}
- clause separator.
- @item @code{~:;}
- default clause follows.
- @end table
- @item @code{~@{@var{str}~@}}
- Iteration (args come from the next argument (a list)).
- @table @asis
- @item @code{~@var{n}@{}
- at most @var{n} iterations.
- @cindex ~@{
- @item @code{~:@{}
- args from next arg (a list of lists).
- @item @code{~@@@{}
- args from the rest of arguments.
- @item @code{~:@@@{}
- args from the rest args (lists).
- @end table
- @cindex ~^
- @item @code{~^}
- Up and out.
- @table @asis
- @item @code{~@var{n}^}
- aborts if @var{n} = 0
- @item @code{~@var{n},@var{m}^}
- aborts if @var{n} = @var{m}
- @item @code{~@var{n},@var{m},@var{k}^}
- aborts if @var{n} <= @var{m} <= @var{k}
- @end table
- @end table
- @subsection Unimplemented CL Format Control Directives
- @table @asis
- @item @code{~:A}
- print @code{#f} as an empty list (see below).
- @item @code{~:S}
- print @code{#f} as an empty list (see below).
- @item @code{~<~>}
- Justification.
- @item @code{~:^}
- @end table
- @subsection Extended, Replaced and Additional Control Directives
- These are not necesasrily implemented in Kawa!
- @table @asis
- @item @code{~I}
- print a R4RS complex number as @code{~F~@@Fi} with passed parameters for
- @code{~F}.
- @item @code{~Y}
- Pretty print formatting of an argument for scheme code lists.
- @item @code{~K}
- Same as @code{~?.}
- @item @code{~!}
- Flushes the output if format @var{destination} is a port.
- @item @code{~_}
- Print a @code{#\space} character
- @table @asis
- @item @code{~@var{n}_}
- print @var{n} @code{#\space} characters.
- @end table
- @item @code{~@var{n}C}
- Takes @var{n} as an integer representation for a character. No arguments
- are consumed. @var{n} is converted to a character by
- @code{integer->char}. @var{n} must be a positive decimal number.@refill
- @item @code{~:S}
- Print out readproof. Prints out internal objects represented as
- @code{#<...>} as strings @code{"#<...>"} so that the format output can always
- be processed by @code{read}.
- @refill
- @item @code{~:A}
- Print out readproof. Prints out internal objects represented as
- @code{#<...>} as strings @code{"#<...>"} so that the format output can always
- be processed by @code{read}.
- @refill
- @item @code{~F, ~E, ~G, ~$}
- may also print number strings, i.e. passing a number as a string and
- format it accordingly.
- @end table
- @node Pretty-printing
- @section Pretty-printing
- Pretty-printing is displaying a data structure as text,
- by adding line-breaks and indenttaion so that the visual
- structure of the output corresponds to the logical structure of
- data structure. This makes it easier to read and understand.
- Pretty-printing takes into account the column width of the output
- so as to avoid using more lines than needed.
- Pretty-printing of standard sequences types such as lists and
- vectors is done by default. For example:
- @example
- #|kawa:11|# @kbd{(set! *print-right-margin* 50)}
- #|kawa:12|# @kbd{'(ABCDEF (aa bb cc dd) (x123456789}
- #|.....13|# @kbd{y123456789 z123456789) ABCDEFG HIJKL)}
- (ABCDEF (aa bb cc dd)
- (x123456789 y123456789 z123456789) ABCDEFG HIJK)
- @end example
- Setting @code{*print-right-margin*} to 50
- causes output to be limited to 50 columns.
- Notice the top-level list has to be split,
- but sub-lists @code{(aa bb cc dd)}
- and @code{(x123456789 y123456789 z123456789)}
- don't need to be split.
- When outputting to a DomTerm REPL,
- then @code{*print-right-margin*} is ignored,
- and the line-breaking is actually handled by DomTerm.
- If you change the window width, DomTerm will dynamically
- re-calculate the line-breaks of previous pretten output.
- This works even in the case of a session saved to an HTML
- file, as long as JavaScript is enabled.
- The concepts and terminology are
- based on those of @uref{https://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node253.html,Common Lisp}.
- @subsection Pretty-printing Scheme forms
- Scheme and Lisp code is traditionally pretty-printed
- slightly differently than plain lists.
- The @code{pprint} procedure assumes the argument
- is a Scheme form, and prints its accordingly.
- For example the special form @code{(let ...)} is printed
- differently from a regular function call @code{(list ...)}.
- @deffn Procedure pprint obj [out]
- Assume @var{obj} is a Scheme form, and pretty-print it
- in traditional Scheme format. For example:
- @example
- #|kawa:1|# @kbd{(import (kawa pprint))}
- #|kawa:2|# @kbd{(define fib-form}
- #|.....3|# @kbd{ '(define (fibonacci n)}
- #|.....4|# @kbd{ (let loop ((i0 0) (i1 1) (n n))}
- #|.....5|# @kbd{ (if (<= n 0) i0}
- #|.....6|# @kbd{ (loop i1 (+ i0 i1) (- n 1))))))}
- #|kawa:7|# @kbd{(set! *print-right-margin* 80)}
- #|kawa:8|# @kbd{(pprint fib-form)}
- (define (fibonacci n)
- (let loop ((i0 0) (i1 1) (n n)) (if (<= n 0) i0 (loop i1 (+ i0 i1) (- n 1)))))
- #|kawa:9|# @kbd{(set! *print-right-margin* 40)}
- #|kawa:10|# @kbd{(pprint fib-form)}
- (define (fibonacci n)
- (let loop ((i0 0) (i1 1) (n n))
- (if (<= n 0)
- i0
- (loop i1 (+ i0 i1) (- n 1)))))
- @end example
- The @code{pprint} special-cases forms that start with
- @code{define}, @code{if}, @code{lambda}, @code{let},
- and a few more, and formats them with ``traditional'' indentation.
- However, it is not as complete or polished as it should be.
- (It should also use a programmable dispatch table,
- rather than having these special cases hard-wired.
- That is an improvemet for another day.)
- @end deffn
- @subsection Generic pretty-printing functions
- The following procedures are used to indicate logical blocks,
- and optional newlines.
- To access them do:
- @example
- (import (kawa pprint))
- @end example
- In the following, @var{out} is the output port, which
- defaults to @code{(current-output-port)}.
- @deffn Syntax pprint-logical-block @var{options} @arbno{@stxref{statement}}
- Evaluate the @var{statement}s within the context of a new ``logical block''.
- The @var{options} are one or more of the following:
- @table @asis
- @item @stxlit{prefix:} @var{prefix}
- @itemx @stxlit{per-line:} @var{per-line-prefix}
- Emit @var{prefix} or @var{per-line-prefix} (only one of them can be specified) before the start of the logical block.
- If @var{per-line-prefix} is provided, it is also print for each
- line within the logical block, indented the same.
- These are strings and default to @code{""}.
- @item @stxlit{suffix:} @var{suffix}
- Emit @var{suffix} after the end of the logical block.
- @item @stxlit{out:} @var{out}
- The output file.
- @end table
- For example to print a list you might do:
- @example
- (pprint-logical-block prefix: "(" suffix: ")"
- @i{print contents of list})
- @end example
- This macro is equivalent to:
- @example
- (pprint-start-logical-block @var{prefix} @var{is-per-line} @var{suffix} @var{out})
- (try-finally
- (begin @arbno{@var{statement}})
- (pprint-end-logical-block @var{suffix} @var{out}))
- @end example
- @end deffn
- @deffn Procedure pprint-start-logical-block prefix is-per-line suffix out
- Start a logical block.
- The @var{is-per-line} argument is a boolean to specifiy
- of @var{prefix} is a per-line-prefix or a plain prefix.
- @end deffn
- @deffn Procedure pprint-end-logical-block suffix out
- End a logical block.
- @end deffn
- @deffn Procedure pprint-newline kind [out]
- Print a conditional newline, where @var{kind} is one of the
- symbols @code{'fill}, @code{'linear}, @code{'mandatory},
- or @code{'miser}.
- Usually follows printing of a space, as nothing is printed
- if the line is not broken here.
- @end deffn
- @deffn Procedure pprint-ident mode amount [out]
- Change how much following lines are indented
- (with the current logical block).
- The @var{amount} is the size of the indentation, in characters.
- The @var{mode} is either @code{'current} (if the
- @var{amount} is relative to the current position),
- or @code{'block} (if the @var{amount} is relative to the
- start (after any @var{prefix}) of the current logical block).
- @end deffn
- @node Resources
- @section Resources
- A resource is a file or other fixed data that an application may access.
- Resources are part of the application and are shipped with it, but are
- stored in external files. Examples are images, sounds,
- and translation (localization) of messages.
- In the Java world a resource is commonly bundled in the same jar file
- as the application itself.
- @deffn Syntax resource-url resource-name
- Returns a @code{URLPath} you can use as a @code{URL}, or
- you can pass to it @code{open-input-file} to read the resource data.
- The @var{resource-name} is a string which is passed to the
- @code{ClassLoader} of the containing module.
- If the module class is in a jar file, things will magically
- work if the resource is in the same jar file, and @var{resource-name}
- is a filename relative to the module class in the jar.
- If the module is immediately evaluated, the @var{resource-name} is resolved
- against the location of the module source file.
- @end deffn
- @deffn Syntax module-uri
- Evaluates to a special URI that can be used to access resources
- relative to the class of the containing module.
- The URI has the form @code{"class-resource://@var{CurrentClass}/"}
- in compiled code, to allow moving the classes/jars.
- The current @code{ClassLoader} is associated with the URI, so accessing
- resources using the URI will use that @code{ClassLoader}.
- Therefore you should not create a @code{"class-resource:"} URI
- except by using this function or @code{resolve-uri},
- since that might try to use the wrong @code{ClassLoader}.
- The macro @code{resource-url} works by using @code{module-uri}
- and resolving that to a normal @code{URL}.
- @end deffn
- @deffn Syntax module-class
- Evaluates to the containing module class, as a @code{java.lang.Class} instance.
- @end deffn
- @node Types, Objects Classes and Modules, Input-Output, Top
- @chapter Types
- A @dfn{type} is a set of values, plus an associated set of operations
- valid on those values.
- Types are useful for catching errors ("type-checking"), documenting
- the programmer's intent, and to help the compiler generate better code.
- Types in some languages (such as C) appear in programs,
- but do not exist at run-time. In such languages, all type-checking
- is done at compile-time. Other languages (such as standard Scheme)
- do not have types as such, but they have @dfn{predicates}, which
- allow you to check if a value is a member of certain sets; also,
- the primitive functions will check at run-time if the arguments
- are members of the allowed sets. Other languages, including Java
- and Common Lisp, provide a combination: Types may be used as specifiers
- to guide the compiler, but also exist as actual run-time values.
- In Java, for each class, there is a corresponding @code{java.lang.Class}
- run-time object, as well as an associated type (the set of values
- of that class, plus its sub-classes, plus @code{null}).
- Kawa, like Java, has first-class types, that is types exist as
- objects you can pass around at run-time. The most used types
- correspond to Java classes and primitive types,
- but Kawa also has other non-Java types.
- Type specifiers have a @dfn{type expressions}, and a type expression
- is conceptually an expression that is evaluated to yield a type value.
- The current Kawa compiler
- is rather simple-minded, and in many places only allows simple
- types that the compiler can evaluate at compile-time.
- More specifically, it only allows simple @dfn{type names}
- that map to primitive Java types or Java classes.
- @display
- @stxdef{type} @stxref{expression}
- @stxdef{opt-type-specifier} [@stxlit{::} @stxref{type}]
- @end display
- Various Kawa constructs require or allow a type to be specified.
- You can use a type specifier most places where you
- @ref{Variables and Patterns,define a variable or match a pattern}.
- Types specifiers can appear in other placess, such as procedure return
- type specifiers. For example in this procedure definition,
- @code{::vector} is an argument type specifier
- (and @code{vec::vector} is a pattern),
- while @code{::boolean} is a return type specifier.
- @example
- (define (vector-even? vec::vector)::boolean
- (not (odd? (vector-length vec))))
- (vector-even? #(3 4 5)) @result{} #f
- (vector-even? (list 3 4 5 6)) @result{} @i{error}
- @end example
- @menu
- * Standard Types::
- * Parameterized Types::
- * Type tests and conversions::
- @end menu
- @node Standard Types
- @section Standard Types
- These types are predefined with the following names.
- Instead of plain @code{@var{typename}} you can also use
- the syntax @code{<@var{typename}>} with angle brackets,
- but that syntax is no longer recommended, because it doesn't
- ``fit'' as well with some ways type names are used.
- To find which Java classes these types map into, look in
- @code{kawa/standard/Scheme.java}.
- Note that the value of these variables are instances
- of @code{gnu.bytecode.Type},
- not (as you might at first expect) @code{java.lang.Class}.
- The numeric types (@code{number}, @code{quantity},
- @code{complex}, @code{real}, @code{rational}, @code{integer},
- @code{long}, @code{int}, @code{short}, @code{byte}
- @code{ulong}, @code{uint}, @code{ushort}, @code{ubyte},
- @code{double}, @code{float})
- are discussed in @ref{Numerical types}.
- The types @code{character} and @code{char}
- are discussed in @ref{Characters}.
- @defvar Object
- An arbitrary Scheme value - and hence an arbitrary Java object.
- @end defvar
- @defvar symbol
- The type of Scheme symbols.
- (Implemented using the Java class @code{gnu.mapping.Symbol}.)
- (@CompatibilityNote{} Previous versions of Kawa implemented
- a simple Scheme symbol using an interned @code{java.lang.String}.)
- @end defvar
- @defvar keyword
- The type of keyword values. @xref{Keywords}.
- @end defvar
- @defvar list
- The type of Scheme lists (pure and impure, including the empty list).
- @end defvar
- @defvar pair
- The type of Scheme pairs. This is a sub-type of @code{list}.
- @end defvar
- @defvar string
- The type of Scheme strings.
- (Implemented using @code{gnu.lists.IString} or @code{java.lang.String}
- for immutable strings,
- and @code{gnu.lists.FString} for mutable strings.
- These all implement the interface @code{java.lang.CharSequence}.
- (@CompatibilityNote{} Previous versions of Kawa
- always used @code{gnu.lists.FString}.)
- @end defvar
- @defvar character
- The type of Scheme character values. This is a sub-type of
- @code{Object}, in contrast to type @code{char}, which is the
- primitive Java @code{char} type.
- @end defvar
- @defvar vector
- The type of Scheme vectors.
- @end defvar
- @defvar procedure
- The type of Scheme procedures.
- @end defvar
- @defvar input-port
- The type of Scheme input ports.
- @end defvar
- @defvar output-port
- The type of Scheme output ports.
- @end defvar
- @defvar String
- This type name is a special case. It specifies the class
- @code{java.lang.String}.
- However, coercing a value to @code{String} is done by
- invoking the @code{toString} method on the value to be coerced.
- Thus it "works" for all objects.
- It also works for @code{#!null}.
- When Scheme code invokes a Java method, any parameter
- whose type is @code{java.lang.String} is converted
- as if it was declared as a @code{String}.
- @end defvar
- @defvar parameter
- A parameter object, as created by @code{make-parameter}.
- This type can take a type parameter (sic):
- @example
- (define-constant client ::parameter[Client] (make-parameter #!null))
- @end example
- This lets Kawa know that reading the parameter (as in @code{(client)})
- returns a value of the specified type (in this case @code{Client}).
- @end defvar
- More will be added later.
- A type specifier can also be one of the primitive Java types.
- The numeric types @code{long}, @code{int}, @code{short},
- @code{byte}, @code{float}, and @code{double} are converted from the
- corresponding Scheme number classes. Similarly, @code{char}
- can be converted to and from Scheme characters. The type
- @code{boolean} matches any object, and the result is @code{false}
- if and only if the actual argument is @code{#f}.
- (The value @code{#f} is identical to @code{Boolean.FALSE},
- and @code{#t} is identical to @code{Boolean.TRUE}.)
- The return type @code{void} indicates that no value is returned.
- A type specifier can also be a fully-qualified Java class name
- (for example @code{java.lang.StringBuffer}). In that case,
- the actual argument is cast at run time to the named class.
- Also, @code{java.lang.StringBuffer[]} represents
- an array of references to @code{java.lang.StringBuffer} objects.
- @anchor{dynamic-type}
- @defvar dynamic
- Used to specify that the type is unknown, and is likely to change
- at run-time.
- Warnings about unknown member names are supressed
- (a run-time name lookup is formed).
- An expression of type @code{dynamic} is (statically) compatible with
- any type.
- @end defvar
- @node Parameterized Types
- @section Parameterized Types
- Kawa has some basic support for parameterized (generic) types.
- The syntax:
- @example
- Type[Arg1 Arg2 ... ArgN]
- @end example
- is more-or-less equivalent to Java's:
- @example
- Type<Arg1, Arg2, ..., ArgN>
- @end example
- This is a work-in-progress. You can use this syntax with
- fully-qualified class names, and also type aliases:
- @example
- (define v1 ::gnu.lists.FVector[gnu.math.IntNum] [4 5 6])
- (define-alias fv gnu.lists.FVector)
- (define v2 ::fv[integer] [5 6 7])
- (define-alias fvi fv[integer])
- (define v3 ::fvi [6 7 8])
- @end example
- @node Type tests and conversions
- @section Type tests and conversions
- Scheme defines a number of standard type testing predicates.
- For example @code{(vector? x)} is @code{#t} if and only if
- @code{x} is a vector.
- Kawa generalizes this to arbitrary type names:
- If @var{T} is a type-name (that is in scope at compile-time),
- then @code{@var{T}?} is a one-argument function that returns
- @code{#t} if the argument is an instance of the type @code{@var{T}},
- and @code{#f} otherwise:
- @example
- (gnu.lists.FVector? #(123)) @result{} #t
- (let ((iarr (int[] 10))) (int[]? iarr)) @result{} #t
- @end example
- To convert (coerce) the result of an expression @var{value} to a
- type @var{T} use the syntax: @code{(->@var{T} @var{value})}.
- @example
- (->float 12) @result{} 12.0f0
- @end example
- In general:
- @example
- (@var{T}? @var{x}) @result{} (instance? @var{x} @var{T})
- (->@var{T} @var{x}) @result{} (as @var{T} @var{x})
- @end example
- @deffn Procedure instance? value type
- Returns @code{#t} iff @var{value} is an instance of type @var{type}.
- (Undefined if @var{type} is a primitive type, such as @code{int}.)
- @end deffn
- @deffn Procedure as type value
- Converts or coerces @var{value} to a value of type @var{type}.
- Throws an exception if that cannot be done.
- Not supported for @var{type} to be a primitive type such as @code{int}.
- @end deffn
- @node Objects Classes and Modules, XML tools, Types, Top
- @chapter Object, Classes and Modules
- Kawa provides various ways to define, create, and access Java objects.
- Here are the currently supported features.
- The Kawa module system is based on the features of the Java class system.
- @menu
- * Defining new classes::
- * Anonymous classes::
- * Enumerations:: Enumeration types
- * Annotations::
- * Module classes:: Modules and how they are compiled to classes
- * Importing:: Importing from a library
- * Record types:: Defining Record Types
- * Dynamic records:: Creating New Record Types On-the-fly
- * Method operations:: Calling Java methods from Scheme
- * Allocating objects::
- * Field operations:: Accessing fields of Java objects
- * Mangling:: Mapping Scheme names to Java names
- * Scheme types in Java::
- * Array operations:: Using Java arrays
- * Loading Java functions into Scheme::
- * Evaluating Scheme expressions from Java::
- @end menu
- @deffn Syntax this
- Returns the "this object" - the current instance of the current class.
- The current implementation is incomplete, not robust, and not
- well defined. However, it will have to do for now.
- Note: "@code{this}" is a macro, not a variable, so you have to write
- it using parentheses: @samp{(this)}. A planned extension will
- allow an optional class specifier (needed for nested clases).
- @end deffn
- @node Defining new classes
- @section Defining new classes
- Kawa provides various mechanisms for defining new classes.
- The @code{define-class} and @code{define-simple-class} forms
- will usually be the preferred mechanisms. They have basically
- the same syntax, but have a couple of differences.
- @code{define-class} allows multiple inheritance as well as true nested
- (first-class) class objects. However, the implementation
- is more complex: code using it is slightly slower, and the mapping to
- Java classes is a little less obvious. (Each Scheme class is implemented
- as a pair of an interface and an implementation class.)
- A class defined by @code{define-simple-class} is slightly more
- efficient, and it is easier to access it from Java code.
- The syntax of @code{define-class} are mostly compatible with that
- in the Guile and Stk dialects of Scheme.
- @deffn Syntax define-class @stxref{class-name} @stxlit{(}supers ...@stxlit{)} @arbno{(@stxref{annotation}|@stxref{option-pair})} @stxref{field-or-method-decl} ...
- @deffnx Syntax define-simple-class @stxref{class-name} @stxlit{(}supers ...@stxlit{)} @arbno{(@stxref{annotation}|@stxref{option-pair})} @stxref{field-or-method-decl} ...
- Defines a new class named @var{class-name}. If @code{define-simple-class} is
- used, creates a normal Java class named @var{class-name} in the current package.
- (If @var{class-name} has the form @code{<xyz>} the Java implementation
- type is named @code{xyz}.) For @code{define-class} the implementation is
- unspecified. In most cases, the compiler creates a class pair,
- consisting of a Java interface and a Java implementation class.
- @end deffn
- @display
- @stxdef{class-name} @stxref{identifier}
- @stxdef{option-pair} @var{option-keyword} @var{option-value}
- @stxdef{field-or-method-decl} @stxref{field-decl} | @stxref{method-decl}
- @end display
- @subsection General class properties
- The class inherits from the classes and interfaces listed in @var{supers}.
- This is a list of names of classes that are in scope (perhaps imported
- using @code{require}), or names for existing classes or interfaces
- optionally surrounded by @code{<>}, such as @code{<gnu.lists.Sequence>}.
- If @code{define-simple-class} is used, at most one of these may be
- the name of a normal Java class or classes defined using
- @code{define-simple-class}; the rest must be interfaces or classes
- defined using @code{define-class}.
- If @code{define-class} is used, @emph{all} of the classes listed
- in @var{supers} should be interfaces or classes defined using
- @code{define-class}.
- @table @asis
- @item @stxlit{interface:} @var{make-interface}
- Specifies whether Kawa generates a Java class, interface, or both.
- If @var{make-interface} is @code{#t}, then a Java interface is generated.
- In that case all the supertypes must be interfaces, and
- all the declared methods must be abstract.
- If @var{make-interface} is @code{#f}, then a Java class is generated.
- If @code{interface:} is unspecified, the default is @code{#f}
- for @code{define-simple-class}. For @code{define-class} the default
- is to generate an interface, and in addition (if needed) a helper
- class that implements the interface. (In that case any non-abstract methods
- are compiled to static methods. The methods that implement the interface
- are just wrapper methods that call the real static methods. This
- allows Kawa to implement true multiple inheritance.)
- @item @stxlit{access:} @var{kind}
- Specifies the Java access permission on the class.
- Can be one of @code{'public} (which is the default in Kawa),
- @code{'package} (which the default "unnamed" permission in Java code),
- @code{'protected}, @code{'private},
- @code{'volatile}, or @code{'transient}.
- Can also be used to specify @code{final}, @code{abstract}, or @code{enum}, as in Java.
- (You don't need to explicitly specify the class is @code{abstract}
- if any @var{method-body} is @code{#!abstract},
- or you specify @code{interface: #t}.)
- The @var{kind} can also be a list, as for example:
- @example
- access: '(protected volatile)
- @end example
- @item @stxlit{class-name:} @code{"}@var{cname}@code{"}
- Specifies the Java name of the created class.
- The @var{name} specified after @code{define-class}
- or @code{define-simple-class} is the @emph{Scheme name},
- i.e. the name of a Scheme variable that is bound to the class.
- The Java name is by default derived from the Scheme name,
- but you can override the default with a @code{class-name:} specifier.
- If the @var{cname} has no periods, then it is a name in
- the package of the main (module) class.
- If the @var{cname} starts with a period,
- then you get a class nested within the module class.
- In this case the actual class name is @var{moduleClass}@code{$}@var{rname},
- where @var{rname} is @var{cname} without the initial period.
- To force a class in the top-level (unnamed) package (something
- not recommended) write a period at the end of the @var{cname}.
- @end table
- @subsection Declaring fields
- @display
- @stxdef{field-decl} @stxlit{(}@stxref{field-name} (@stxref{annotation} | @stxref{opt-type-specifier} | @stxref{field-option})*@stxlit{)}
- @stxdef{field-name} @stxref{identifier}
- @stxdef{field-option} @stxref{keyword} @stxref{expression}
- @end display
- As a matter of style the following order is suggested, though this not enforced:
- @display
- @stxlit{(}@stxref{field-name} @stxref{annotation}* @stxref{opt-type-specifier} @stxref{field-option}*@stxlit{)}
- @end display
- Each @var{field-decl} declares a instance "slot" (field)
- with the given @var{field-name}.
- By default it is publicly visible, but you can specify
- a different visiblity with the @code{access:} specifier.
- The following @var{field-option} @var{keyword}s are implemented:
- @table @asis
- @item @stxlit{type:} @stxref{type}
- Specifies that @var{type} is the type of (the values of) the field.
- Equivalent to @samp{:: @var{type}}.
- @item @stxlit{allocation:} @var{kind}
- If @var{kind} is @code{'class} or @code{'static} a single slot is shared
- between all instances of the class (and its sub-classes).
- Not yet implemented for @code{define-class},
- only for @code{define-simple-class}.
- In Java terms this is a @code{static} field.
- If @var{kind} is @code{'instance} then
- each instance has a separate value "slot", and they
- are not shared. In Java terms, this is a non-@code{static} field.
- This is the default.
- @c You can use a keyword like @code{class:} or a string like @code{"class"}
- @c if you prefer instead of a quoted symbol like @code{'class};
- @c the latter is recommended.
- @item @stxlit{access:} @var{kind}
- Specifies the Java access permission on the field.
- Can be one of @code{'private}, @code{'protected},
- @code{'public} (which is the default in Kawa),
- or @code{'package} (which the default "unnamed" permission
- in Java code).
- Can also be used to specify @code{volatile}, @code{transient},
- @code{enum}, or @code{final}, as in Java,
- or a quoted list with these symbols.
- @item @stxlit{init:} @var{expr}
- An expression used to initialize the slot.
- The expression is evaluated in a scope that includes the field and
- method names of the current class.
- @item @stxlit{init-form:} @var{expr}
- An expression used to initialize the slot.
- The lexical environment of the @var{expr} is that of the @code{define-class};
- it does @emph{not} include the field and method names of the current class.
- or @code{define-simple-class}.
- @item @stxlit{init-value:} @var{value}
- A value expression used to initialize the slot.
- For now this is synonymous with @var{init-form:}, but that may change
- (depending on what other implementation do), so to be safe only use
- @code{init-value:} with a literal.
- @item @stxlit{init-keyword:} @code{@var{name}:}
- A keyword that that can be used to initialize instance in @code{make} calls.
- For now, this is ignored, and @var{name} should be the same as the
- field's @var{field-name}.
- @end table
- The @var{field-name} can be left out. That indicates a "dummy slot",
- which is useful for initialization not tied to a specific field.
- In Java terms this is an instance or static initializer, i.e., a
- block of code executed when a new instance is created or the class is loaded.
- In this example, @code{x} is the only actual field. It is first
- initialized to 10, but if @code{(some-condition)} is true
- then its value is doubled.
- @example
- (define-simple-class <my-class> ()
- (allocation: 'class
- init: (perform-actions-when-the-class-is-initizalized))
- (x init: 10)
- (init: (if (some-condition) (set! x (* x 2)))))
- @end example
- @subsection Declaring methods
- @display
- @stxdef{method-decl} @stxlit{((}@stxref{method-name} @stxref{formal-arguments}@stxlit{)}
- @arbno{@stxref{method-option}} [@stxref{deprecated-return-specifier}] @stxref{method-body}@stxlit{)}
- @stxdef{method-name} @stxref{identifier}
- @stxdef{method-option} @stxref{annotation} | @stxref{opt-return-type} | @stxref{option-pair}
- @stxdef{method-body} @stxref{body} | @stxlit{#!abstract} | @stxlit{#!native}
- @stxdef{deprecated-return-specifier} @stxref{identifier}
- @end display
- Each @var{method-decl} declares a method,
- which is by default public and non-static, and whose name is @var{method-name}.
- (If @var{method-name} is not a valid
- Java method name, it is mapped to something reasonable.
- For example @code{foo-bar?} is mapped to @code{isFooBar}.)
- The types of the method arguments can be specified in the
- @var{formal-arguments}. The return type can be specified by
- a @var{opt-return-type}, @var{deprecated-return-specifier},
- or is otherwise the type of the @var{body}.
- Currently, the @var{formal-arguments} cannot contain optional, rest,
- or keyword parameters. (The plan is to allow optional parameters,
- implemented using multiple overloaded methods.)
- A @var{method-decl} in a @code{define-simple-class}
- can have the following @var{option-keyword}s:
- @table @asis
- @item @stxlit{access:} @var{kind}
- Specifies the Java access permission on the method.
- Can be one of @code{'private}, @code{'protected},
- @code{'public}, or @code{'package}.
- Can also be @code{'synchronized}, @code{'final}, @code{'strictfp},
- or a quoted list.
- @item @stxlit{allocation:} @var{kind}
- If @var{kind} is @code{'class} or @code{'static} creates a static method.
- @item @stxlit{throws:} ( @var{exception-class-name} ... )
- Specifies a list of checked exception that the method may throw.
- Equivalent to a @code{throws} specification in Java code.
- For example:
- @example
- (define-simple-class T
- (prefix)
- ((lookup name) throws: (java.io.FileNotFoundException)
- (make java.io.FileReader (string-append prefix name))))
- @end example
- @end table
- The scope of the @var{body} of a method includes the @var{field-decl}s
- and @var{method-decl}s of the class, including those inherited from
- superclasses and implemented interfaces.
- If the @var{method-body} is the special form @code{#!abstract},
- then the method is abstract. This means the method must
- be overridden in a subclass, and you're not allowed to
- create an instance of the enclosing class.
- @example
- (define-simple-class Searchable () interface: #t
- ((search value) :: boolean #!abstract))
- @end example
- If the @var{method-body} is the special form @code{#!native},
- then the method is native, implemented using @uref{http://en.wikipedia.org/wiki/Java_Native_Interface,JNI}.
- The special @var{method-name} @samp{*init*} can be used to name
- a non-default constructor (only if @var{make-interface} discussed above
- is @code{#f}).
- It can be used to initialize a freshly-allocated instance
- using passed-in parameters.
- You can call a superclass or a sibling constructor using
- the @code{invoke-special} special function.
- (This is general but admittedly a bit verbose; a more compact
- form may be added in the future.)
- See the example below.
- @subsection Example
- In the following example we define a simple class @code{2d-vector}
- and a class @code{3d-vector} that extends it. (This is for illustration
- only - defining 3-dimensional points as an extension
- of 2-dimensional points does not really make sense.)
- @example
- (define-simple-class 2d-vector ()
- (x ::double init-keyword: x:)
- ;; Alternative type-specification syntax.
- (y type: double init-keyword: y:)
- (zero-2d :: 2d-vector allocation: 'static
- init-value: (2d-vector 0))
- ;; An object initializer (constructor) method.
- ((*init* (x0 ::double) (y0 ::double))
- (set! x x0)
- (set! y y0))
- ((*init* (xy0 ::double))
- ;; Call above 2-argument constructor.
- (invoke-special 2d-vector (this) '*init* xy0 xy0))
- ;; Need a default constructor as well.
- ((*init*) #!void)
- ((add (other ::2d-vector)) ::2d-vector
- ;; Kawa compiles this using primitive Java types!
- (2d-vector
- x: (+ x other:x)
- y: (+ y other:y)))
- ((scale (factor ::double)) ::2d-vector
- (2d-vector x: (* factor x) y: (* factor y))))
- (define-simple-class 3d-vector (2d-vector)
- (z type: double init-value: 0.0 init-keyword: z:)
- ;; A constructor which calls the superclass constructor.
- ((*init* (x0 ::double) (y0 ::double) (z0 ::double))
- (invoke-special 2d-vector (this) '*init* x0 y0)
- (set! z z0))
- ;; Need a default constructor.
- ((*init*) #!void)
- ((scale (factor ::double)) ::2d-vector
- ;; Note we cannot override the return type to 3d-vector
- ;; because Kawa doesn't yet support covariant return types.
- (3d-vector
- x: (* factor x)
- y: (* factor (this):y) ;; Alternative syntax.
- z: (* factor z))))
- @end example
- Note we define both explicit non-default constructor methods,
- and we associate fields with keywords, so they can be named
- when allocating an object. Using keywords requires a default constructor,
- and since having non-default constructors suppresses
- the implicit default constructor we have to explicitly define it.
- Using both styles of constructors is rather redundant, though.
- @node Anonymous classes
- @section Anonymous classes
- @deffn Syntax object @stxlit{(}supers ...@stxlit{)} field-or-method-decl ...
- Returns a new instance of an anonymous (inner) class.
- The syntax is similar to @code{define-class}.
- @display
- @stxdef{object-field-or-method-decl} @stxref{object-field-decl} | @stxref{method-decl}
- @stxdef{object-field-decl} @stxlit{(}@stxref{field-name} (@stxref{annotation} | @stxref{opt-type-specifier} | @stxref{field-option})* [@stxref{object-init}] @stxlit{)}
- @stxdef{object-init} @stxref{expression}
- @end display
- Returns a new instance of a unique (anonymous) class.
- The class inherits from the list of @var{supers}, where at most one of the
- elements should be the base class being extended from, and the rest
- are interfaces.
- This is roughly equivalent to:
- @example
- (begin
- (define-simple-class @var{hname} (@var{supers} ...) @var{field-or-method-decl} ...)
- (make @var{hname}))
- @end example
- A @var{field-decl} is as for @code{define-class}, except
- that we also allow an abbreviated syntax.
- Each @var{field-decl} declares a public instance field.
- If @var{object-finit} is given, it is an expression whose value
- becomes the initial value of the field.
- The @var{object-init} is evaluated at the same time as the @code{object}
- expression is evaluated,
- in a scope where all the @var{field-name}s are visible.
- A @var{method-decl} is as for @code{define-class}.
- @end deffn
- @anchor{SAM-conversion}
- @subsection Lambda as shorthand for anonymous class
- An anonymous class is commonly used in the Java platform where a
- function language would use a lambda expression.
- Examples are call-back handlers, events handlers, and @code{run} methods.
- In these cases Kawa lets you use a lambda expression as a short-hand
- for an anonymous class. For example:
- @example
- (button:addActionListener
- (lambda (e) (do-something)))
- @end example
- is equivalent to:
- @example
- (button:addActionListener
- (object (java.awt.event.ActionListener)
- ((actionPerformed (e ::java.awt.event.ActionEvent))::void
- (do-something))))
- @end example
- This is possible when the required type is an interface or
- abstract class with a Single (exactly one) Abstract Methods.
- Such a class is sometimes called a @dfn{SAM-type}, and the
- conversion from a lambda expression to an anonymous class
- is sometimes called @dfn{SAM-conversion}.
- Note that Kawa can also infer the parameter and return types
- of a method that overrides a method in a super-class.
- @node Enumerations
- @section Enumeration types
- An enumeration type is a set of named atomic enumeration values
- that are distinct from other values. You define the type
- using @code{define-enum}, and you reference enumeration values
- using colon notation:
- @example
- (define-enum colors (red blue green))
- (define favorite-color colors:green)
- @end example
- Displaying an enum just prints the enum name,
- but readable output using @code{write} (or the @code{~s} @code{format}
- specifier) prepends the type name:
- @example
- (format "~a" favorite-color) @result{} "green"
- (format "~s" favorite-color) @result{} "colors:green"
- @end example
- The static @code{values} method returns a Java array of the enumeration
- values, in declaration order, while @code{ordinal} yields the index
- of an enumeration value:
- @example
- (colors:values) @result{} [red blue green]
- ((colors:values) 1) @result{} blue
- (favorite-color:ordinal) @result{} 2
- @end example
- If you invoke the enumeration type as a function,
- it will map the name (as a string) to the corresponding value.
- (This uses the @code{valueOf} method.)
- @example
- (colors "red") @result{} red
- (colors "RED") @result{} throws IllegalArgumentException
- (eq? favorite-color (colors:valueOf "green")) @result{} #t
- @end example
- Kawa enumerations are based on Java enumerations.
- Thus the above is similar to a Java5 @code{enum} declaration,
- and the type @code{colors} above extends @code{java.lang.Enum}.
- @deffn Syntax define-enum enum-type-name @var{option-pair}... @stxlit{(}enum-value-name ...@stxlit{)} @var{field-or-method-decl}...
- This declares a new enumeration type @var{enum-type-name},
- whose enumerations values are the @var{enum-value-name} list.
- You can specify extra options and members using
- @var{option-pair} and @var{field-or-method-decl},
- which are as in @code{define-simple-class}.
- (The @var{define-enum} syntax is similar to a
- @code{define-simple-class} that extends @code{java.lang.Enum}.)
- @end deffn
- (Note that R6RS has a separate Enumerations library @code{(rnrs enum)}.
- Unfortunately, this is not compatible with standard Java enums.
- R6RS enums are simple symbols, which means you cannot distinguish
- two enum values from different enumeration types if they have the
- same value, nor from a vanilla symbol. That makes them less useful.)
- @node Annotations
- @section Annotations of declarations
- The Java platform lets you associate with each declaration zero or more
- @uref{http://download.oracle.com/javase/1.5.0/docs/guide/language/annotations.html, annotations}.
- They provide an extensible mechanism to associate properties
- with declarations.
- Kawa support for annotations is not complete (the most important
- functionality missing is being able to declare annotation types),
- but is fairly functional.
- Here is a simple example illustrating use of
- @uref{http://jcp.org/en/jsr/detail?id=222,JAXB annotations}:
- an @code{XmlRootElement} annotation on a class,
- and an @code{XmlElement} annotation on a field:
- @example
- (import (class javax.xml.bind.annotation XmlRootElement XmlElement))
- (define-simple-class Bib ( ) (@@XmlRootElement name: "bib")
- (books (@@XmlElement name: "book" type: Book) ::java.util.ArrayList))
- (define-simple-class Book () ...)
- @end example
- @uref{http://per.bothner.com/blog/2011/Using-JAXB-annotations, This tutorial}
- explains the JAXB example in depth.
- Here is the syntax:
- @display
- @stxdef{annotation} @stxlit{(@@}@stxref{annotation-typename} @stxref{annotations-element-values}@stxlit{)}
- @stxdef{annotations-element-values} @stxref{annotation-element-value}
- | @stxref{annotation-element-pair} ...
- @stxdef{annotation-element-pair} @stxref{keyword} @stxref{annotation-element-value}
- @stxdef{annotation-element-value} @stxref{expression}
- @stxdef{annotation-typename} @stxref{expression}
- @end display
- An @var{annotations-element-values} consisting of just
- a single @var{annotation-element-value} is equivalent to an
- @var{annotation-element-pair} with a @code{value:} keyword.
- Each @var{keyword} must correspond to the name of
- an element (a zero-argument method) in the annotation type.
- The corresponding @var{annotation-element-value} must be compatible with the
- element type (return type of the method) of the annotation type.
- Allowed element types are of the following kinds:
- @itemize @bullet
- @item
- Primitive types, where the @var{annotation-element-value} must
- be number or boolean coercible to the element type.
- @item
- Strings, where the @var{annotation-element-value} is normally a string literal.
- @item
- Classes, where the @var{annotation-element-value} is normally
- a classname.
- @item
- Enumeration types. The value usually has the form @code{@var{ClassName}:@var{enumFieldname}}.
- @item
- Nested annotation types, where the @var{annotation-element-value} must
- be a compatible @var{annotation} value.
- @item
- An array of one of the allowable types.
- An array constructor expression works, but using the
- square bracket syntax is recommended.
- @end itemize
- Annotations are usually used in declarations,
- where they are required to be ``constant-folded'' to compile-time
- constant annotation values.
- This is so they can be written to class files.
- However, in other contexts an annotation can be used as an expression
- with general sub-expressions evaluated at run-time:
- @example
- (define bk-name "book")
- (define be (@@XmlElement name: bk-name type: Book))
- (be:name) @result{} "book"
- @end example
- (This may have limited usefulness: There are some bugs, including
- lack of support for default values for annotation elements.
- These bugs can be fixed if someone reports a need for
- runtime construction of annotation values.)
- @node Module classes
- @section Modules and how they are compiled to classes
- Modules provide a way to organize Scheme into
- reusable parts with explicitly defined interfaces to the rest
- of the program.
- A @dfn{module} is a set of definitions that the module @dfn{exports},
- as well as some @dfn{actions} (expressions evaluated for their side effect).
- The top-level forms in a Scheme source file compile a module;
- the source file is the @dfn{module source}.
- When Kawa compiles the module source, the result is the
- @dfn{module class}. Each exported definition is translated to
- a public field in the module class.
- @subsection Name visibility
- The definitions that a module exports are accessible to other modules.
- These are the "public" definitions, to use Java terminology.
- By default, all the identifiers declared at the top-level of a module
- are exported, except those defined using @code{define-private}.
- (If compiling with the @code{--main} flag,
- then by default no identifiers are exported.)
- However, a major purpose of using modules is to control the set of
- names exported. One reason is to reduce the chance of accidental
- name conflicts between separately developed modules. An even more
- important reason is to enforce an interface: Client modules should
- only use the names that are part of a documented interface, and should
- not use internal implementation procedures (since those may change).
- If there is a @code{module-export} (or @code{export})
- declaration in the module, then only those names listed are exported.
- There can be more than one @code{module-export}, and they can be
- anywhere in the Scheme file. The recommended style has
- a single @code{module-export} near the beginning of the file.
- @anchor{meta-export-declaration}
- @deffn Syntax module-export @arbno{@stxref{export-spec}}
- @deffnx Syntax export @arbno{@stxref{export-spec}}
- The forms @code{export} and @code{module-export} are equivalent.
- (The older Kawa name is @code{module-export};
- @code{export} comes from R7RS.)
- Either form specifies a list of identifiers which
- can be made visible to other libraries or programs.
- @display
- @stxdef{export-spec} @var{identifier}
- | @stxlit{(rename} @stxref{identifier}@sub{1} @stxref{identifier}@sub{2}@stxlit{)}
- @end display
- In the former variant, an @var{identifier} names a single binding
- defined within or imported into the library, where the
- external name for the export is the same as the name of
- the binding within the library.
- A @code{rename} spec exports the
- binding defined within or imported into the library and
- named by @var{identifier}@sub{1},
- using @var{identifier}@sub{2} as the external name.
- Note that it is an error if there is no definition for @var{identifier}
- (or @var{identifier}@sub{1})
- in the current module, or if it is defined using @code{define-private}.
- As a matter of style, @code{export} or @code{module-export} should
- appear after @code{module-name} but @emph{before} other commands
- (including @code{import} or @code{require}).
- (This is a requirement if there are any cycles.)
- @end deffn
- In this module, @code{fact} is public and @code{worker} is private:
- @example
- (module-export fact)
- (define (worker x) ...)
- (define (fact x) ...)
- @end example
- Alternatively, you can write:
- @example
- (define-private (worker x) ...)
- (define (fact x) ...)
- @end example
- @subsection R7RS explicit library modules
- A R7RS @code{define-library} form is another way to create a module.
- The R7RS term @dfn{library} is roughly the same as a Kawa module.
- In Kawa, each source file is a @ref{implicit library,@dfn{implicit module}},
- which may contain zero or more explicit sub-modules (in
- the form of @code{define-library}) optionally followed by
- the definitions and expressions of the implicit (file-level) module.
- @anchor{meta-library-definition}
- @findex @i{library-definition}
- @deffn Syntax define-library @stxref{library-name} @arbno{@stxref{library-declaration}}
- @end deffn
- @display
- @stxdef{library-name} @stxlit{(} @stxref{library-name-parts} @stxlit{)}
- @stxdef{library-name-parts} @atleastone{@var{identifier}}
- @end display
- A @meta{library-name} is a list whose members are identifiers and
- exact non-negative integers. It is used to identify the library
- uniquely when importing from other programs or
- libraries. Libraries whose first identifier is @code{scheme} are
- reserved for use by the R7RS report and future versions of that
- report. Libraries whose first identifier is @code{srfi} are reserved
- for libraries implementing @uref{http://srfi.schemer.org/,Scheme Requests for Implementation}.
- It is inadvisable, but not an error, for identifiers
- in library names to contain any of the characters @code{|} @code{\} @code{?}
- @code{*} @code{<} @code{"} @code{:} @code{>} @code{+} @code{[} @code{]}
- @code{/} @code {.} or control characters after escapes are
- expanded.
- See @ref{module-name} for how a @meta{library-name} is
- mapped to a class name.
- @display
- @stxdef{library-declaration}
- @stxref{export-declaration}
- | @stxref{import-declaration}
- | @stxlit{(begin} @arbno{@stxref{statement}} @stxlit{)}
- | @stxlit{(include} @atleastone{@meta{filename}}@stxlit{)}
- | @stxlit{(include-ci} @atleastone{@meta{filename}}@stxlit{)}
- | @stxlit{(include-library-declarations} @atleastone{@meta{filename}}@stxlit{)}
- | @stxlit{(cond-expand} @arbno{@stxref{cond-expand-clause}} [@stxlit{(else} command-or-definition*@stxlit{)}]@stxlit{)}
- | @stxref{statement}
- @end display
- The @code{begin}, @code{include}, and @code{include-ci} declarations are
- used to specify the body of the library. They have the
- same syntax and semantics as the corresponding expression types.
- This form of @code{begin} is analogous to, but not the
- same as regular @code{begin}.
- A plain @meta{statement} (which is allowed as a Kawa extension)
- is also part of the body of the library,
- as if it were wrapped in a @code{begin}).
- The @code{include-library-declarations} declaration is similar
- to @code{include} except that the contents of the file are
- spliced directly into the current library definition. This
- can be used, for example, to share the same @code{export} declaration
- among multiple libraries as a simple form of library interface.
- The @code{cond-expand} declaration has the same syntax and semantics
- as the @code{cond-expand} expression type, except that
- it expands to spliced-in library declarations rather than
- expressions enclosed in @code{begin}.
- When a library is loaded, its expressions are executed in
- textual order. If a library’s definitions are referenced in
- the expanded form of a program or library body, then that
- library must be loaded before the expanded program or
- library body is evaluated. This rule applies transitively. If
- a library is imported by more than one program or library,
- it may possibly be loaded additional times.
- Similarly, during the expansion of a library @code{(foo)}, if any
- syntax keywords imported from another library @code{(bar)} are
- needed to expand the library, then the library @code{(bar)} must
- be expanded and its syntax definitions evaluated before the
- expansion of @code{(foo)}.
- Regardless of the number of times that a library is loaded,
- each program or library that imports bindings from a library must
- do so from a single loading of that library, regardless
- of the number of import declarations in which it
- appears. That is, @code{(import (only (foo) a})) followed by
- @code{(import (only (foo) b))} has the same effect as
- @code{(import (only (foo) a b))}.
- @subsection How a module becomes a class
- If you want to just use a Scheme module as a module (i.e. @code{load}
- or @code{require} it), you don't care how it gets translated
- into a module class. However, Kawa gives you some control over how this
- is done, and you can use a Scheme module to define a class which
- you can use with other Java classes. This style of class definition
- is an alternative to @code{define-class},
- which lets you define classes and instances fairly conveniently.
- The default name of the module class is the main part of the
- filename of the Scheme source file (with directories and extensions
- stripped off). That can be overridden by the @code{-T} Kawa
- command-line flag. The package-prefix specified by the @code{-P}
- flag is prepended to give the fully-qualified class name.
- @anchor{module-name}
- @deffn Syntax module-name name
- @deffnx Syntax module-name <name>
- @deffnx Syntax module-name @stxref{library-name}
- Sets the name of the generated class, overriding the default.
- If there is no @samp{.} in the @var{name}, the package-prefix
- (specified by the @code{-P} Kawa command-line flag) is prepended.
- If the form @meta{library-name} is used,
- then the class name is the result of taking
- each @meta{identifier} in the @meta{library-name-parts},
- @ref{Mangling,mangling} if needed, and concatenating them
- separated by periods.
- For example @code{(org example doc-utils)} becomes
- @code{org.example.doc-utils}. (You can't reference the class name
- @code{doc-utils} directly in Java, but the JVM has no problems with it.
- In Java you can use reflection to access classes with such names.)
- As a matter of style, @code{module-name} should be the first
- command in a file (after possible comments). It must appear
- before a @code{require} or @code{import}, in case of cycles.
- @end deffn
- By default, the base class of the generated module class is unspecified;
- you cannot count on it being more specific than @code{Object}.
- However, you can override it with @code{module-extends}.
- @deffn Syntax module-extends class
- Specifies that the class generated from the immediately surrounding
- module should extend (be a sub-class of) the class @code{@var{class}}.
- @end deffn
- @deffn Syntax module-implements interface ...
- Specifies that the class generated from the immediately surrounding
- module should implement the interfaces listed.
- @end deffn
- Note that the compiler does @emph{not} currently check that all the
- abstract methods requires by the base class or implemented interfaces
- are actually provided, and have the correct signatures. This will
- hopefully be fixed, but for now, if you are forgot a method, you will
- probably get a verifier error
- For each top-level exported definition the compiler creates a
- corresponding public field with a similar (mangled) name.
- By default, there is some indirection: The value of the Scheme variable
- is not that of the field itself. Instead, the field is a
- @code{gnu.mapping.Location} object, and the value Scheme variable is
- defined to be the value stored in the @code{Location}.
- Howewer, if you specify an explicit type, then the field will
- have the specified type, instead of being a @code{Location}.
- The indirection using @code{Location} is also avoided if you use
- @code{define-constant}.
- If the Scheme definition defines a procedure (which is not re-assigned
- in the module), then the compiler assumes the variable as bound as a
- constant procedure. The compiler generates one or more methods
- corresponding to the body of the Scheme procedure. It also generates
- a public field with the same name; the value of the field is an
- instance of a subclass of @code{<gnu.mapping.Procedure>} which when
- applied will execute the correct method (depending on the actual arguments).
- The field is used when the procedure used as a value (such as being passed
- as an argument to @code{map}), but when the compiler is able to do so,
- it will generate code to call the correct method directly.
- You can control the signature of the generated method by declaring
- the parameter types and the return type of the method. See the
- applet (@pxref{Applet compilation}) example for how this can be done.
- If the procedures has optional parameters, then the compiler will
- generate multiple methods, one for each argument list length.
- (In rare cases the default expression may be such that this is
- not possible, in which case an "variable argument list" method
- is generated instead. This only happens when there is a nested
- scope @emph{inside} the default expression, which is very contrived.)
- If there are @code{#!keyword} or @code{#!rest} arguments, the compiler
- generate a "variable argument list" method. This is a method whose
- last parameter is either an array or a @code{<list>}, and whose
- name has @code{$V} appended to indicate the last parameter is a list.
- Top-leval macros (defined using either @code{define-syntax}
- or @code{defmacro}) create a field whose type is currently a sub-class of
- @code{kawa.lang.Syntax}; this allows importing modules to detect
- that the field is a macro and apply the macro at compile time.
- Unfortunately, the Java class verifier does not allow fields to have
- arbitrary names. Therefore, the name of a field that represents a
- Scheme variable is "mangled" (@pxref{Mangling}) into an acceptable Java name.
- The implementation can recover the original name of a field @code{X}
- as @code{((gnu.mapping.Named) X).getName()} because all the standard
- compiler-generated field types implement the @code{Named} interface.
- @anchor{dual-purpose-class}
- @subsection Same class for module and defined class
- You can declare a class using @code{define-simple-class}
- with the same name as the module class, for example the
- following in a file named @code{foo.scm}:
- @example
- (define-simple-class foo ...)
- @end example
- In this case the defined class will serve dual-purpose as the module class.
- To avoid confusion, in this case you must not specify
- @code{module-extends}, @code{module-implements}, or @code{(module-static #t)}.
- Also, the defined class should not have public static members.
- In that case it works out pretty well: public static members
- represent bindings exported by the module; other non-private members
- ``belong'' to the defined class.
- In this case @code{(module-static 'init-run)} is implied.
- @anchor{static-or-non-modules}
- @subsection Static vs non-static modules
- There are two kinds of module class:
- A @dfn{static module} is a class (or gets compiled to a class)
- all of whose public fields are static, and that does not have a
- public constructor. A JVM can only have a single global instance of
- a static module.
- An @dfn{instance module} has a public default constructor,
- and usually has at least one non-static public field.
- There can be multiple instances
- of an instance module; each instance is called a @dfn{module instance}.
- However, only a single instance of a module can be @dfn{registered}
- in an environment, so in most cases there is only a single
- instance of instance modules. Registering an instance in an environment
- means creating a binding mapping a magic name (derived from the class name)
- to the instance.
- In fact, any Java class class that has the properties of either
- an instance module or a static module, is a module, and can be
- loaded or imported as such; the class need not have written
- using Scheme.
- You can control whether a module is compiled to a static or
- a non-static class using either a command-line flag to the compiler,
- or using the @code{module-static} special form.
- @table @code
- @item --module-static
- Generate a static module
- (as if @code{(module-static #t)} were specified).
- This is (now) the default.
- @item --module-nonstatic
- @itemx --no-module-static
- Generate a non-static module
- (as if @code{(module-static #f)} were specified).
- This used to be the default.
- @item --module-static-run
- Generate a static module
- (as if @code{(module-static 'init-run)} were specified).
- @end table
- @deffn Syntax module-static name ...
- @deffnx Syntax module-static @code{#t}
- @deffnx Syntax module-static @code{#f}
- @deffnx Syntax module-static @code{'init-run}
- Control whether the generated fields and methods are static.
- If @code{#t} or @code{'init-run} is specified, then the module will be a
- static module, @emph{all} definitions will be static.
- If @code{'init-run} is specified, in addition the module body
- is evaluated in the class's static initializer.
- (Otherwise, it is run the first time it is @code{require}'d.)
- Otherwise, the module is an instance module. If there is a non-empty
- list of @var{name}s then the module is an instance module, but the @var{name}s
- that are explicitly listed will be compiled to static fields and methods.
- If @code{#f} is specified, then all exported names will
- be compiled to non-static (instance) fields and methods.
- By default, if no @code{module-static} is specified:
- @enumerate
- @item
- If there is a @code{module-extends} or @code{module-implements}
- declaration, or one of the @code{--applet} or @code{--servlet}
- command-line flags was specified, then @code{(module-static #f)} is implied.
- @item
- If one of the command-line flags
- @code{--no-module-static}, @code{--module-nonstatic},
- @code{--module-static}, or @code{--module-static-run} was specified,
- then the default is @code{#f}, @code{#f}, @code{#t}, or @code{'init-run},
- respectively.
- @item
- If the module class is @ref{dual-purpose-class,dual-purpose}
- then @code{(module-static 'init-run)} is implied.
- @item
- Otherwise the default is @code{(module-static #t)}.
- (It used to be @code{(module-static #f)} in older Kawa versions.)
- @end enumerate
- The default is @code{(module-static #t)}. It usually produces more efficient
- code, and is recommended if a module contains only procedure or macro
- definitions. However, a static module means that all environments in a JVM
- share the same bindings, which you may not want if you use
- multiple top-level environments.
- @end deffn
- The top-level actions of a module will get compiled to a @code{run}
- method. If there is an explicit @code{method-extends}, then the
- module class will also automatically implement @code{java.lang.Runnable}.
- (Otherwise, the class does not implement @code{Runnable}, since in that
- case the @code{run} method return an @code{Object} rather than @code{void}.
- This will likely change.)
- @subsection Module options
- Certain compilation options can be be specified @emph{either}
- on the command-line when compiling, or in the module itself.
- @deffn Syntax module-compile-options [key@stxlit{:} value] ...
- This sets the value of the @code{key} option to @code{value}
- for the current module (source file). It takes effect as
- soon it is seen during the first macro-expansion pass,
- and is active thereafter (unless overridden by @code{with-compile-options}).
- The @var{key:} is one of the supported option names
- (The ending colon makes it a Kawa keyword). Valid
- option keys are:
- @itemize @bullet
- @item
- @stxlit{main:} - Generate an application, with a main method.
- @end itemize
- @itemize @bullet
- @item
- @stxlit{full-tailcalls:} - Use a calling convention that supports proper tail recursion.
- @end itemize
- @itemize @bullet
- @item
- @stxlit{warn-undefined-variable:} - Warn if no compiler-visible binding for a variable.
- @item
- @stxlit{warn-unknown-member:} - Warn if referencing an unknown method or field.
- @item
- @stxlit{warn-invoke-unknown-method:} - Warn if invoke calls an unknown method (subsumed by warn-unknown-member).
- @item
- @stxlit{warn-unused:} - Warn if a variable is usused or code never executed.
- @item
- @stxlit{warn-uninitialized:} - Warn if accessing an uninitialized variable.
- @item
- @stxlit{warn-unreachable:} - Warn if this code can never be executed.
- @item
- @stxlit{warn-void-used:} - Warn if an expression depends on the value of a void sub-expression (one that never returns a value).
- @item
- @stxlit{warn-as-error:} - Treat a compilation warning as if it were an error.
- @end itemize
- The @var{value} must be a literal value: either a boolean
- (@code{#t} or @code{#f}), a number, or a string,
- depending on the @var{key}.
- (All the options so far are boolean options.)
- @example
- (module-compile-options warn-undefined-variable: #t)
- ;; This causes a warning message that y is unknown.
- (define (func x) (list x y))
- @end example
- @end deffn
- @deffn Syntax with-compile-options [key: value] ... body
- Similar to @code{module-compile-options}, but the option
- is only active within @var{body}.
- The module option key @code{main:} has no effect when applied
- to a particular body via the @code{with-compile-options} syntax.
- @example
- (define (func x)
- (with-compile-options warn-invoke-unknown-method: #f
- (invoke x 'size)))
- @end example
- @end deffn
- @node Importing
- @section Importing from a library
- @anchor{require}
- You can import a module into the current namespace with @code{import} or @code{require}. This adds the exported bindings (or a subset of them) to the
- current lexical scope. It follows that these bindings (which are said
- to be imported) are determined at compile-time.
- @anchor{meta-import-declaration}
- @deffn Syntax import @arbno{@stxref{import-set}}
- An @code{import} declaration provides a way to import identifiers
- exported by a library (module). Each @meta{import-set} names a set of
- bindings from a library and possibly specifies local names
- for the imported bindings.
- @display
- @stxdef{import-set}
- @meta{classname}
- | @stxref{library-reference}
- | @stxlit{(library} @stxref{library-reference} @stxlit{)}
- | @stxlit{(class} @var{class-prefix} @arbno{@var{import-only-name}}@stxlit{)}
- | @stxlit{(only} @stxref{import-set} @arbno{@var{import-only-name}}@stxlit{)}
- | @stxlit{(except} @stxref{import-set} @arbno{@var{identifier}}@stxlit{)}
- | @stxlit{(prefix} @stxref{import-set} @var{identifier} @stxlit{)}
- | @stxlit{(rename} @stxref{import-set} @arbno{@stxref{rename-pair}}@stxlit{)}
- @stxdef{library-reference} @stxlit{(} @stxref{library-name-parts} [@stxref{explicit-source-name}]@stxlit{)}
- @stxdef{import-only-name} @var{identifier}|@stxref{rename-pair}
- @stxdef{explicit-source-name} @stxref{string}
- @stxdef{rename-pair} @stxlit{(} @var{identifier}@sub{1} @var{identifier}@sub{2}@stxlit{)}
- @end display
- A @var{library-reference} is mapped to a class name by concatenating
- all the identifiers, separated by dots.
- For example:
- @example
- (import (gnu kawa slib srfi37))
- @end example
- is equivalent to:
- @example
- (import gnu.kawa.slib.srfi37)
- @end example
- as well as to:
- @example
- (require gnu.kawa.slib.srfi37)
- @end example
- By default, all of an imported library's exported bindings are made
- visible within an importing library using the names given to the
- bindings by the imported library. The precise set of bindings to be
- imported and the names of those bindings can be adjusted with the
- @code{only}, @code{except}, @code{prefix}, and @code{ rename} forms as
- described below.
- @itemize
- @item
- An @code{only} form produces a subset of the bindings from another
- @meta{import-set}, including only the listed @meta{identifier}s. The
- included @meta{identifier}s must be in the original @meta{import-set}.
- If a @var{rename-pair} is used, then the @code{@var{identifier}@sub{1}}
- must be in the original @meta{import-set},
- and is renamed to @code{@var{identifier}@sub{2}}. For example:
- @example
- (import (only (kawa example) A (B1 B2) C (D1 D2)))
- @end example
- is equivalent to:
- @example
- (import (rename (only (kawa example) A B1 C D1)
- (B1 B2) (D1 D2)))
- @end example
- The names @code{A}, @code{B1}, @code{C}, and @code{D1} must
- exist in the library @code{(kawa example)}. The bindings are
- accessible using the names @code{A}, @code{B2}, @code{C}, and @code{D2}.
- @item
- An @code{except} form produces a subset of the bindings from another
- @meta{import-set}, including all but the listed @meta{identifier}s. All
- of the excluded @meta{identifier}s must be in the original @meta{import-set}.
- @item
- A @code{prefix} form adds the @meta{identifier} prefix to each name from
- another @meta{import-set}.
- @item
- A @code{rename} form:
- @example
- (rename (@var{identifier}@sub{1} @var{identifier}@sub{2}) @dots{})
- @end example
- @noindent
- removes the bindings for @code{@var{identifier}@sub{1} @dots{}} to form an
- intermediate @meta{import-set}, then adds the bindings back for the
- corresponding @code{@var{identifier}@sub{2} @dots{}} to form the final
- @meta{import-set}. Each @code{@var{identifier}@sub{1}} must be in the original
- @meta{import-set}, each @var{identifier}@sub{2} must not be in the
- intermediate @meta{import-set}, and the @var{identifier}@sub{2}s must be
- distinct.
- @end itemize
- A @code{class} form is a convenient way to define abbreviations
- for class names; it may be more convenient than @code{define-alias}.
- The @var{class-prefix} is concatenated with each @meta{identifier}
- (with a period in between) to produce a classname.
- Each @meta{identifier} becomes an alias for the class.
- For example:
- @example
- (import (class java.util Map (HashMap HMap)))
- @end example
- This defines @code{Map} as an alias for @code{java.util.Map},
- and @code{HMap} as an alias for @code{java.util.HashMap}.
- (You can think of the @code{class} form as similar to a @code{only} form,
- where the @var{class-prefix} names a special kind of
- library represented of a Java package, and whose exported
- bindings are the classes in the package.)
- You can combine the @code{class} form with
- @code{only}, @code{except}, @code{rename}, and @code{prefix},
- though only @code{prefix} is likely to be useful. For example:
- @example
- (import (prefix (class java.lang Long Short) jl-))
- @end example
- is equivalent to
- @example
- (import (class java.lang (Long jl-Long) (Short jl-Short)))
- @end example
- which is equivalent to:
- @example
- (define-private-alias jl-Short java.lang.Short)
- (define-private-alias jl-Long java.lang.Long)
- @end example
- @end deffn
- @deffn Syntax require @stxlit{'}featureName
- @deffnx Syntax require classname [@stxref{explicit-source-name}]
- @deffnx Syntax require @stxref{explicit-source-name}]
- Search for a matching module (class), and add the names
- exported by that module to the current set of visible names.
- Normally, the module is specified using @var{classname}.
- The form @code{require} has similar functionality as @code{import},
- but with a different syntax, and without options like @code{rename}.
- If a @code{@stxlit{"}@var{sourcepath}@stxlit{"}} is specified then
- that is used to locate the source file for the module, and if necessary,
- compile it.
- If a @code{'@var{featurename}} is specified then the
- @var{featurename} is looked up (at compile time) in the "feature table"
- which yields the implementing @var{classname}.
- @end deffn
- @deffn Syntax provide @stxlit{'}featurename
- Declare that @code{'@var{featurename}} is available.
- A following @code{cond-expand} in this scope will match @var{featurename}.
- @end deffn
- Using @code{require} and @code{provide} with @var{featurename}s is
- similar to the same-named macros in SLib, Emacs, and Common Lisp.
- However, in Kawa these are not functions, but instead they
- are syntax forms that are processed at compile time. That is
- why only quoted @var{featurename}s are supported.
- This is consistent with Kawa emphasis on compilation and
- static binding.
- For some examples, you may want to look in the @code{gnu/kawa/slib}
- directory.
- @subsection Searching for modules
- When Kawa sees a @code{import} or @code{require} it searches for
- either a matching source file or a previously-compiled class with a
- matching name.
- For @code{import} we generate a classname by converting it in the same
- way @code{module-name} does: taking each identifier in the
- @meta{library-name-parts}, mangling if needed, and concatenating the parts
- separated by periods.
- If there is a matching module in any @meta{program-unit} that is
- in the process of being compiled, we use that. This may be
- a file requested to be compiled with the @code{-C} command-line switch,
- or an extra @meta{library-definition} in a file already parsed.
- Kawa will attempt to finish compiling the module and load the class,
- but if there are circular dependencies it will use the uncompiled definitions.
- Next Kawa looks for a matching class in the context classpath.
- (There is special handling if the library-name starts with @code{srfi},
- and certain builtin classes will have @code{kawa.lib.} prepended.)
- Kawa also searches for a matching source file, described below.
- It uses the implicit source name (formed by concatenating the
- library-name parts, separated by @code{"/"}), as well as
- any @meta{explicit-source-name}. The source file is parsed as
- a @stxref{program-unit}. It is an error if the @meta{program-unit}
- does not declare a library (explicit or implicit) with the
- matching name.
- If Kawa finds both a matching source file and a class, it will pick one
- based on which is newer.
- @subsection Searching for source files
- The Java property @code{kawa.import.path} controls how @code{import}
- and @code{require} search for a suitable source file. Example usage:
- @example
- $ kawa -Dkawa.import.path=".:<foo fo>/opt/fo-libs/*.scm:/usr/local/kawa"
- @end example
- The value of the @code{kawa.import.path} property is a list of
- path elements, separated by @code{":"}.
- Each path element is combined with either the explicit source name
- or the implicit source name to produce a filename.
- If a matching file exists, then we have found a source file.
- If a path element contains a @code{"*"} then the @code{"*"}
- is replaced by the implicit source name (without an extension).
- (Any explicit source name is ignored in this case.)
- For example, for @code{(import (foo bar))} or @code{(require foo.bar)}
- the implicit source name is @code{"foo/bar"}. If the path element is
- @code{"/opt/kawa/*.sc"} then the resulting filename is @code{"/opt/kawa/foo/bar.sc"}.
- If there is no @code{"*"} in the path element, and there is an
- explicit source, then it is appended to the path element
- (or replaces the path element if the explicit source is absolute).
- Otherwise we use the implicit source, followed by the default file extension.
- (The default file extension is that of the current source if that is a
- named file; otherwise the default for the current language, which
- is @code{".scm"} for Scheme.)
- A path element that starts with a selector of the
- form @code{"<@stxref{library-name-parts}>"} is only applicable if a prefix
- of the requested module name matches the @meta{library-name-parts}. If there
- is @code{"*"} in the path element, that is replaced by the corresponding rest
- of the implicit source name. For example if importing @code{(fee fo foo fum})
- and the path element is @code{"<fee fo>/opt/fo-libs/*.scm"} then the
- resulting filename is @code{"/opt/fo-libs/foo/fum.scm"}.
- If there is a selector but no @code{"*"}, then the rest of the path element
- following the selector is combined with the explicit or implicit source
- as if there were no selector (assuming of course that the selector matches).
- If the resulting filename is relative, then it is resolved
- relative to the @dfn{current root}. For example the source to
- a library with the name @code{(x y)} that compiles to
- a class @code{x.y} might be a file named @code{/a/b/x/y.scm}.
- Then the current root would be @code{/a/b/}
- - that is the directory that results from removing the library name
- suffix from the file name.
- More generally: assume the current module has @math{N} name components.
- For example the name @code{(x y)}
- (with the class name @code{x.y}) has 2 components.
- The current root is what you get when you take the current file name
- (say @code{"/a/b/c/d.scm"}), and remove everything after
- the @math{N}'th slash (@code{"/"}) from the end (say @code{"c/d.scm"};
- what remains (e.g. @code{"/a/b/"} is the current root.
- (If the current input source is not a named file,
- use the value of @code{(current-path)} with a @code{"/"} appended.)
- The default search path is @code{"."} - i.e. just search relative
- to the current root.
- @subsection Builtin libraries
- The following libraries are bundled with Kawa:
- @table @code
- @item (scheme base)
- @itemx (scheme case-lambda)
- @itemx (scheme char)
- @itemx (scheme complex)
- @itemx (scheme cxr)
- @itemx (scheme cxr)
- @itemx (scheme eval)
- @itemx (scheme inexact)
- @itemx (scheme lazy)
- @itemx (scheme load)
- @itemx (scheme process-context)
- @itemx (scheme read)
- @itemx (scheme repl)
- @itemx (scheme time)
- @itemx (scheme write)
- @itemx (scheme r5rs)
- The above are standard libraries as defined by R7RS.
- @item (rnrs arithmetic bitwise)
- @itemx (rnrs hashtables)
- @itemx (rnrs lists)
- @itemx (rnrs programs)
- @itemx (rnrs sorting)
- @itemx (rnrs unicode)
- The above are standard libraries as defined by R6RS.
- @item (kawa reflect)
- Defines procedures and syntax for acessing Java objects and members:
- @code{as}
- @code{field}
- @code{instance?}
- @code{invoke}
- @code{invoke-static}
- @code{invoke-special}
- @code{make}
- @code{primitive-throw}
- @code{set-field!}
- @code{set-static-field!}
- @code{static-field}
- @item (kawa expressions)
- @itemx (kawa hashtable)
- @itemx (kawa quaternions)
- @itemx (kawa rotations)
- @itemx (kawa regex)
- @itemx (kawa string-cursors)
- Various Kawa libraries @i{add details}.
- @item (kawa base)
- All the bindings by default available to the kawa top-level.
- @end table
- @subsection Importing a SRFI library
- Importing a supported SRFI numbered @var{N} is conventionally
- doing using a @code{(import (srfi @var{N}))}
- or the older R6RS syntax @code{(import (srfi :@var{N}))} (with a colon, for historical reasons). You can also give it
- a name, as specified by @uref{http://srfi.schemers.org/srfi-95/srfi-95.html,SRFI 95}. For example, any of these work:
- @example
- (import (srfi 95))
- (import (srfi 95 sorting-and-merging))
- (import (srfi :95))
- (import (srfi :95 sorting-and-merging))
- @end example
- You can also use @code{(require 'srfi-@var{N})}:
- @example
- (require 'srfi-95)
- @end example
- @subsection Importing from a plain class
- Note you can import from many classes, even if they weren't
- compiled from a library-definition. The set of @code{public} fields
- in a class are considered as the set of exported definitions,
- with the names demangled as needed.
- The module can be static module (all public fields must be static),
- or an instance module (it has a public default constructor).
- If an imported definition is a non-static field and if no module
- instance for that class
- has been registered in the current environment, then a new instance
- is created and registered (using a "magic" identifier).
- If the module class either inherits from @code{gnu.expr.ModuleBody}
- or implements @code{java.lang.Runnable} then the corresponding @code{run}
- method is executed. (This is done @emph{after} the instance is
- registered so that cycles can be handled.) These actions (creating,
- registering, and running the module instance) are done both at compile
- time and at run time, if necessary.
- All the imported fields of the module class are then incorporated
- in the current set of local visible names in the current module.
- (This is for both instance and static modules.)
- This is done at compile time - no new bindings are created at run-time
- (except for the magic binding used to register the module instance),
- and the imported bindings are private to the current module.
- References to the imported bindings will be compiled as field
- references, using the module instance (except for static fields).
- @node Record types
- @section Record types
- The @code{define-record-type} form can be used for creating new data
- types, called record types. A predicate, constructor, and field
- accessors and modifiers are defined for each record type.
- The @code{define-record-type} feature is specified
- by @uref{http://srfi.schemers.org/srfi-9/srfi-9.html,SRFI-9},
- which is implemented by many modern Scheme implementations.
- @deffn Syntax define-record-type @var{type-name} (@var{constructor-name} @var{field-tag} ...) @var{predicate-name} (@var{field-tag} @var{accessor-name} [@var{modifier-name}]) ...
- The form @code{define-record-type} is generative: each use creates a new
- record type that is distinct from all existing types, including other
- record types and Scheme's predefined types. Record-type definitions may
- only occur at top-level (there are two possible semantics for `internal'
- record-type definitions, generative and nongenerative, and no consensus
- as to which is better).
- An instance of @code{define-record-type} is equivalent to the following definitions:
- @itemize
- @item
- The @var{type-name} is bound to a representation of the record type
- itself.
- @item
- The @var{constructor-name} is bound to a procedure that takes
- as many arguments as there are @var{field-tag}s in the
- @code{(@var{constructor-name} ...)} subform and returns
- a new @var{type-name} record. Fields whose tags are listed with
- @var{constructor-name} have the corresponding argument as their initial
- value. The initial values of all other fields are unspecified.
- @item
- The @var{predicate-name} is a predicate that returns @code{#t}
- when given a value returned by @var{constructor-name}
- and @code{#f} for everything else.
- @item
- Each @var{accessor-name} is a procedure that takes a record of
- type @var{type-name} and returns the current value of the corresponding field.
- It is an error to pass an accessor a value which is not a record of the
- appropriate type.
- @item
- Each @var{modifier-name} is a procedure that takes a record of
- type @var{type-name} and a value which becomes the new value of
- the corresponding field.
- The result (in Kawa) is the empty value @code{#!void}.
- It is an error to pass a
- modifier a first argument which is not a record of the appropriate type.
- @end itemize
- Set!ing the value of any of these identifiers has no effect on the
- behavior of any of their original values.
- @end deffn
- Here is an example of how you can define a record type named @code{pare}
- with two fields @code{x} and @code{y}:
- @example
- (define-record-type pare
- (kons x y)
- pare?
- (x kar set-kar!)
- (y kdr))
- @end example
- The above defines @code{kons} to be a constructor,
- @code{kar} and @code{kdr} to be accessors,
- @code{set-kar!} to be a modifier,
- and @code{pare?} to be a predicate for @code{pare}s.
- @example
- (pare? (kons 1 2)) @result{} #t
- (pare? (cons 1 2)) @result{} #f
- (kar (kons 1 2)) @result{} 1
- (kdr (kons 1 2)) @result{} 2
- (let ((k (kons 1 2)))
- (set-kar! k 3)
- (kar k)) @result{} 3
- @end example
- Kawa compiles the record type into a nested class.
- If the @code{define-record-type} appears at module level,
- the result is a class that is a member of the module class.
- For example if the above @code{pare} class is define in a
- module @code{parelib}, then the result is a class
- named @code{pare} with the internal JVM name @code{parelib$pare}.
- The @code{define-record-type} can appear inside a procedure,
- in which case the result is an inner class.
- The nested class has a name derived from
- the @var{type-name}. If the @var{type-name} is valid Java class name,
- that becomes the name of the Java class. If the @var{type-name} has
- the form @code{<@var{name}>} (for example @code{<pare>}), then @var{name}
- is used, if possible, for the Java class name. Otherwise, the name
- of the Java class is derived by "mangling" the @var{type-name}.
- In any case, the package is the same as that of the surrounding module.
- Kawa generates efficient code for the resulting functions,
- without needing to use run-time reflection.
- @node Dynamic records, Method operations, Record types, Objects Classes and Modules
- @section Creating New Record Types On-the-fly
- Calling the @code{make-record-type} procedure creates a new record data
- type at run-time, without any compile-time support.
- It is primarily provided for compatibility; in most cases it is better
- to use the @code{define-record-type} form (@pxref{Record types}).
- @deffn Procedure make-record-type type-name field-names
- Returns a @dfn{record-type descriptor}, a value representing a new data
- type disjoint from all others. The @var{type-name} argument must be a
- string, but is only used for debugging purposes (such as the printed
- representation of a record of the new type). The @var{field-names}
- argument is a list of symbols naming the @dfn{fields} of a record of the
- new type. It is an error if the list contains any duplicates.
- @end deffn
- @c @deffn Procedure make-record-sub-type type-name field-names rtd
- @c Returns a @dfn{record-type descriptor}, a value representing a new data
- @c type, disjoint from all others. The @var{type-name} argument must be a
- @c string. The @var{field-names} argument is a list of symbols naming the
- @c additional @dfn{fields} to be appended to @var{field-names} of
- @c @var{rtd}. It is an error if the combinded list contains any
- @c duplicates.@refill
- @c
- @c Record-modifiers and record-accessors for @var{rtd} work for the new
- @c record-sub-type as well. But record-modifiers and record-accessors for
- @c the new record-sub-type will not neccessarily work for @var{rtd}.@refill
- @c @end deffn
- @deffn Procedure record-constructor rtd [field-names]
- Returns a procedure for constructing new members of the type represented
- by @var{rtd}. The returned procedure accepts exactly as many arguments
- as there are symbols in the given list, @var{field-names}; these are
- used, in order, as the initial values of those fields in a new record,
- which is returned by the constructor procedure. The values of any
- fields not named in that list are unspecified. The @var{field-names}
- argument defaults to the list of field names in the call to
- @code{make-record-type} that created the type represented by @var{rtd};
- if the @var{field-names} argument is provided, it is an error if it
- contains any duplicates or any symbols not in the default list.
- @c In Kawa, @var{rtd} may be any @code{Class} that has a public default
- @c constructor, as long as the @var{field-names} are public instance
- @c fields. (The fields should have type @code{Object} -- unless you
- @c know what you are doing!)
- @end deffn
- @deffn Procedure record-predicate rtd
- Returns a procedure for testing membership in the type represented by
- @var{rtd}. The returned procedure accepts exactly one argument and
- returns a true value if the argument is a member of the indicated record
- type; it returns a false value otherwise.
- @c In Kawa, the returned procedure checks if the argument is an instance
- @c of @var{rtd} or one of its sub-classes.
- @end deffn
- @c @deffn Procedure record-sub-predicate rtd
- @c Returns a procedure for testing membership in the type represented by
- @c @var{rtd} or its parents. The returned procedure accepts exactly one
- @c argument and returns a true value if the argument is a member of the
- @c indicated record type or its parents; it returns a false value
- @c otherwise.@refill
- @c @end deffn
- @deffn Procedure record-accessor rtd field-name
- Returns a procedure for reading the value of a particular field of a
- member of the type represented by @var{rtd}. The returned procedure
- accepts exactly one argument which must be a record of the appropriate
- type; it returns the current value of the field named by the symbol
- @var{field-name} in that record. The symbol @var{field-name} must be a
- member of the list of field-names in the call to @code{make-record-type}
- that created the type represented by @var{rtd}.
- @end deffn
- @deffn Procedure record-modifier rtd field-name
- Returns a procedure for writing the value of a particular field of a
- member of the type represented by @var{rtd}. The returned procedure
- accepts exactly two arguments: first, a record of the appropriate type,
- and second, an arbitrary Scheme value; it modifies the field named by
- the symbol @var{field-name} in that record to contain the given value.
- The returned value of the modifier procedure is unspecified. The symbol
- @var{field-name} must be a member of the list of field-names in the call
- to @code{make-record-type} that created the type represented by @var{rtd}.
- @end deffn
- @deffn Procedure record? obj
- Returns a true value if @var{obj} is a record of any type and a false
- value otherwise.
- @end deffn
- @deffn Procedure record-type-descriptor record
- Returns a record-type descriptor representing the type of the given
- record. That is, for example, if the returned descriptor were passed to
- @code{record-predicate}, the resulting predicate would return a true
- value when passed the given record.
- @end deffn
- @deffn Procedure record-type-name rtd
- Returns the type-name associated with the type represented by rtd. The
- returned value is @code{eqv?} to the @var{type-name} argument given in
- the call to @code{make-record-type} that created the type represented by
- @var{rtd}.@refill
- @end deffn
- @deffn Procedure record-type-field-names rtd
- Returns a list of the symbols naming the fields in members of the type
- represented by @var{rtd}. The returned value is @code{equal?} to the
- field-names argument given in the call to @code{make-record-type} that
- created the type represented by @var{rtd}.@refill
- @end deffn
- Records are extensions of the class @code{Record}.
- These procedures use the Java 1.1 reflection facility.
- @node Method operations, Allocating objects, Dynamic records, Objects Classes and Modules
- @section Calling Java methods from Scheme
- You can call a Java method as if it were a Scheme procedure
- using various mechanisms.
- @subsection Calling static methods using colon notation
- The easiest way to invoke a static method is to use
- @ref{Colon notation, colon notation}, specifically:
- @display
- @stxlit{(}@var{class-expression}@stxlit{:}@var{method-name} @var{argument} ...@stxlit{)}
- @end display
- The @var{class-expression} can be a class in the current lexical
- scope, such as a class defined using @code{define-simple-class}:
- @example
- (define-simple-class MyClass ()
- ((add2 x y) allocation: 'static (+ x y)))
- (MyClass:add2 3 4) @result{} 7
- @end example
- Often @var{class-expression} is a fully-qualified class name:
- @example
- (java.lang.Math:sqrt 9.0) @result{} 3.0
- @end example
- This is only allowed when the name is of a class that exists
- and is accessible both at compile-time and run-time,
- and the name is not otherwise lexically bound.
- You can also use a defined alias:
- @example
- (define-alias jlMath java.lang.Math)
- (jlMath:sqrt 16.0) @result{} 4.0
- @end example
- You can even evaluate @var{class-expression} at run-time
- (in which case Kawa may have to use slower reflection):
- @example
- (let ((math java.lang.Math)) (math:sqrt 9.0)) @result{} 3.0
- @end example
- Here @code{java.lang.Math} evaluates to a @code{java.lang.Class}
- instance for the named class (like Java's @code{java.lang.Class.class},
- again assuming the class exists and is accessible both at compile-time and
- run-time, and the name is not otherwise lexically bound.
- @subsection Calling instance methods using colon notation
- The syntax is:
- @display
- @stxlit{(}@var{instance}@stxlit{:}@var{method-name} @var{argument} ...@stxlit{)}
- @end display
- This invokes the method named @var{method-name}
- with the evaluated @var{instance} as the target object
- and the evaluated @var{argument}s as the method arguments.
- For example:
- @example
- ((list 9 8 7):toString) @result{} "(9 8 7)"
- ([5 6 7]:get 2) @result{} 7
- @end example
- This older syntax is also available:
- @display
- @stxlit{(*:}@var{method-name} @var{instance} @var{argument} ...@stxlit{)}
- @end display
- For example:
- @example
- (*:toString (list 9 8 7))
- @end example
- You can also name the class explicitly:
- @display
- @stxlit{(}@var{class-expression}@stxlit{:}@var{method-name} @var{instance} @var{argument} ...@stxlit{)}
- @end display
- For example:
- @example
- (java.util.List:get [5 6 7] 2) @result{} 7
- @end example
- Using an explicit class is like coercing the @var{instance}:
- @display
- @stxlit{(*:}@var{method-name} @stxlit{(as }@var{class-expression} @var{instance} @stxlit{)}@var{argument} ...@stxlit{)}
- @end display
- Note that for some special values,
- including @code{java.lang.Class} instances, you can't
- use the compact form of @ref{Colon notation, colon notation}
- where the @var{instance} is before the comma:
- @example
- (java.lang.Integer:getDeclaredField "MAX_VALUE") @result{} @i{error}
- @end example
- This is because in this case we look for a static member
- of @code{java.lang.Integer}
- (at least as currently defined and implemented),
- while we want an instance member of @code{java.lang.Class}.
- In those cases you can use one of
- these alternative forms, which all return the same
- @code{java.lang.reflect.Field} result:
- @example
- (*:getDeclaredField java.lang.Integer "MAX_VALUE")
- (java.lang.Class:getDeclaredField java.lang.Integer "MAX_VALUE")
- (invoke java.lang.Integer 'getDeclaredField "MAX_VALUE")
- @end example
- @subsection Method names
- The method to invoke is selected using the specified
- method name and argments. If specified name is not a Java name,
- it is "mangled" (@pxref{Mangling}) into a valid Java name.
- All accessible methods whose names match are considered.
- Methods that match after appending @code{$V} or @code{$X} or @code{$V$X}
- are also considered. A @code{$V} suffix matches a variable
- number of arguments: any excess arguments are collect into an
- @code{gnu.lists.LList} or a Java array (depending on the final parameter type).
- A @code{$X} specifies that the method expects an extra implicit
- @code{CallContext} parameter. In that case the method's result is written
- to the @code{CallContext}, so the method result type must be @code{void}.
- (Kawa may compile a procedure with a @code{#!rest} or keyword args
- whose name is @code{@var{fn}} to a method named @code{@var{fn}$V}.
- It adds an implicit parameter for the extra arguments.
- By default this extra extra parameter is a Scheme list.
- You can specify a Java array type instead, in which case the method is
- named @code{@var{fn}} without the @code{$V},
- and instead it is marked as a Java-5 varargs method.
- The array element type must be compatible with all the extra arguments.)
- @subsection Invoking a method with the @code{invoke} function
- If you prefer, you can instead use the following functions.
- (There is also an older deprecated lower-level interface
- (@pxref{Low-level Method invocation}.)
- @deffn Procedure invoke-static class name args ...
- The @var{class} can be a @code{java.lang.Class}, a
- @code{gnu.bytecode.ClassType}, or a @code{symbol} or @code{string}
- that names a Java class. The @var{name} can be @code{symbol} or
- @code{string} that names one or more methods in the Java class.
- Any accessible methods (static or instance) in the specified @var{class}
- (or its super-classes) that match "@var{name}" or "@var{name}$V" collectively
- form a generic procedure. When the procedure is applied to the argument list,
- the most specific applicable method is chosen depending on the
- argument list; that method is then
- called with the given arguments. Iff the method is an instance method,
- the first actual argument is used as the @code{this} argument. If there are
- no applicable methods (or no methods at all!), or there is no "best"
- method, @code{WrongType} is thrown.
- An example:
- @example
- (invoke-static java.lang.Thread 'sleep 100)
- @end example
- The behavior of interpreted code and compiled code is not
- identical, though you should get the same result either way
- unless you have designed the classes rather strangely. The
- details will be nailed down later, but the basic idea is that
- the compiler will "inline" the @code{invoke-static} call
- if it can pick a single "best" matching method.
- @end deffn
- @deffn Procedure invoke object name args ...
- The @var{name} can be @code{<symbol>} or
- @code{<string>} that names one or more methods in the Java class.
- Any accessible methods (static or instance) in the specified @var{class}
- (or its super-classes) that match "@var{name}" or "@var{name}$V" collectively
- form a generic procedure. When the procedure is applied to the argument list,
- the most specific applicable method is chosen depending on the
- argument list; that method is then
- called with the given arguments. Iff the method is an instance method,
- the @var{object} is used as the @code{this} argument;
- otherwise @var{object} is prepended to the @var{args} list. If there are
- no applicable methods (or no methods at all!), or there is no "best"
- method, @code{WrongType} is thrown.
- The behavior of interpreted code and compiled code is not
- indentical, though you should get the same result either way
- unless you have designed the classes rather strangely. The
- details will be nailed down later, but the basic idea is that
- the compiler will "inline" the @code{invoke-static} call
- if it can pick a single "best" matching method.
- If the compiler cannot determine the method to call (assuming
- the method name is constant), the compiler has to generate code
- at run-time to find the correct method. This is much slower,
- so the compiler will print a warning. To avoid a waning, you can
- use a type declaration, or insert a cast:
- @example
- (invoke (as java.util.Date my-date) 'setDate cur-date)
- @end example
- or
- @example
- (let ((my-date ::java.util.Date (calculate-date))
- (cur-date ::int (get-cur-date)))
- (invoke my-date 'setDate cur-date))
- @end example
- @end deffn
- @deffn Procedure invoke-special class receiver-object name arg ...
- The @var{class} can be a @code{java.lang.Class}, a
- @code{gnu.bytecode.ClassType}, or a @code{symbol} or @code{string}
- that names a Java class.
- The @var{name} can be @code{symbol} or
- @code{string} that names one or more methods in the Java class.
- This procedure is very similar to @code{invoke} and @code{invoke-static}
- and invokes the specified method, ignoring any methods in subclasses
- that might overide it. One interesting use is to invoke a method in
- your super-class like the Java language @code{super} keyword.
- Any methods in the specified @var{class} that match "@var{name}" or
- "@var{name}$V" collectively form a generic procedure. That generic
- procedure is then applied as in @code{invoke} using the
- @code{receiver-object} and the arguments (if any).
- The compiler must be able to inline this procedure (because you cannot
- force a specific method to be called using reflection). Therefore the
- @var{class} and @var{name} must resolve at compile-time to a specific
- method.
- @example
- (define-simple-class <MyClass> (<java.util.Date>)
- ((get-year) :: <int>
- (+ (invoke-special <java.util.Date> (this) 'get-year)) 1900)
- ((set-year (year :: <int>)) :: <void>
- (invoke-special <java.util.Date> (this) 'set-year (- year 1900))))
- @end example
- @end deffn
- @deffn Procedure class-methods class name
- Return a generic function containing those methods of @var{class}
- that match the name @var{name}, in the sense of @code{invoke-static}.
- Same as:
- @example
- (lambda args (apply invoke-static (cons class (cons name args))))
- @end example
- @end deffn
- Some examples using these functions are @samp{vectors.scm}
- and @samp{characters.scm} the directory @samp{kawa/lib} in
- the Kawa sources.
- @subsection Using a namespace prefix
- @emph{This way of invoking a method is deprecated.}
- You can use @code{define-namespace} to define an alias for a Java class:
- @example
- (define-namespace Int32 "class:java.lang.Integer")
- @end example
- In this example the name @code{Int32} is a @dfn{namespace alias}
- for the namespace whose full name is @code{"class:java.lang.Integer"}.
- The full name should be the 6 characters @code{"class:"} followed
- by the fully-qualified name of a Java class.
- Instead of a @var{vamespace-uri} you can use a variable that names
- a class, usually of the form @code{<@var{classname}>}.
- The following is equivalent to the above:
- @example
- (define-namespace Int32 <java.lang.Integer>)
- @end example
- However, there is one important difference: The @code{<@var{classname}>}
- is first searched in the lexical scope.
- It may resolve to a class defined in the current compilation unit
- (perhaps defined using @code{define-simple-class}),
- or imported from another module,
- or an alias (such as from @code{define-alias}).
- Only if @code{<@var{classname}>} is @emph{not} found in the current
- scope is it tried as the class name @var{classname}.
- You can name a method using a @dfn{qualified name} containing a colon.
- The part of the name before the colon is a namespace alias (in
- this case @code{Int32}), and the part of the name after the colon is the
- method name. For example:
- @example
- (Int32:toHexString 255) @result{} "ff"
- @end example
- This invokes the static method @code{toHexString} in the
- Java class @code{java.lang.Integer}, passing it the argument @code{255},
- and returning the String @code{"ff"}.
- The general syntax is
- @example
- (@var{prefix}:@var{method-name} @var{arg} ...)
- @end example
- This invokes the method named @var{method-name} in the class corresponding
- to @var{prefix}, and the @var{arg}s are the method arguments.
- You can use the method name @code{new} to construct new objects:
- @example
- (Int32:new '|255|)
- @end example
- This is equivalent to the Java expression @code{new Integer("255")}.
- You can also write:
- @example
- (Int32:new "255")
- @end example
- You can also call instance methods using a namespace prefix:
- @example
- (Int32:doubleValue (Int32:new "00255"))
- @end example
- This returns the @code{double} value @code{255.0}.
- As a shorthand, you can use the name of a Java class instead of a
- namespace alias:
- @example
- (java.lang.Integer:toHexString 255)
- (java.lang.Object:toString some-value)
- @end example
- If Kawa sees a qualified name with a prefix that is not defined @emph{and}
- that matches the name of a known class, then Kawa will automatically
- treat the prefix
- as a nickname for namespace uri like @code{class:java.lang.Integer}.
- Both conditions should be true at both compile-time and run-time.
- However, using an explicit @code{define-namespace} is recommended.
- As a final shorthand you can use an identifier in handle brackets,
- such as an existing type alias like @code{<list>}.
- The following are all equivalent:
- @example
- (<list>:list3 'a 'b 'c)
- @end example
- This is equivalent to:
- @example
- (define-namespace @var{prefix} <list>
- (@var{prefix}:list3 'a 'b 'c)
- @end example
- for some otherwise-unused @var{prefix}.
- @node Allocating objects, Field operations, Method operations, Objects Classes and Modules
- @section Allocating objects
- The recommended way to create an instance of a type @var{T}
- is to ``call'' @var{T} as if it were a function, with the
- arguments used to initialize the object.
- If @code{T} is a class and @code{T} has a matching constructor,
- then the arguments will used for constructor arguments:
- @example
- (java.util.StringTokenizer "this/is/a/test" "/")
- @end example
- (You can think of the type @var{T} as being
- coerced to an instance-constructor function.)
- If @code{T} is a container or collection type,
- then typically the arguments will be used to specify
- the child or component values.
- Many standard Scheme procedures fit this convention.
- For example in Kawa @code{list} and @code{vector} evaluate to
- types, rather than procedures as in standard Scheme,
- but because types can be used as constructor functions it just works:
- @example
- (list 'a (+ 3 4) 'c) @result{} (a 7 c)
- (vector 'a 'b 'c) @result{} #(a b c)
- @end example
- Any class @code{T} that has a default constructor
- and an @code{add} method can be initialized this way.
- Examples are @code{java.util} collection classes,
- and @code{jawa.awt} and @code{javax.swing} containers.
- @example
- (java.util.ArrayList 11 22 33) @result{} [11, 22, 333]
- @end example
- The above expression is equivalent to:
- @example
- (let ((tmp (java.util.ArrayList)))
- (tmp:add 11)
- (tmp:add 22)
- (tmp:add 33)
- tmp)
- @end example
- Allocating Java arrays (@pxref{Creating-new-Java-arrays}) uses a
- similar pattern:
- @example
- (int[] 2 3 5 7 11)
- @end example
- Sometimes you want to set some named property to an initial value.
- You can do that using a keyword argument. For example:
- @example
- (javax.swing.JButton text: "Do it!" tool-tip-text: "do it")
- @end example
- This is equivalent to using @dfn{setter methods}:
- @example
- (let ((tmp (javax.swing.JButton)))
- (tmp:setText "Do it!")
- (tmp:setToolTipText "do it")
- tmp)
- @end example
- A keyword argument @code{@var{key-name}}@stxlit{:} can
- can translated to either a @code{@stxlit{set}@var{KeyName}@stxlit{:}}
- or a @code{@stxlit{add}@var{KeyName}@stxlit{:}} method.
- The latter makes it convenient to add listeners:
- @example
- (javax.swing.JButton
- text: "Do it!"
- action-listener:
- (object (java.awt.event.ActionListener)
- ((actionPerformed e) (do-the-action))))
- @end example
- This is equivalent to:
- @example
- (let ((tmp (javax.swing.JButton)))
- (tmp:setText "Do it!")
- (tmp:addActionListener
- (object (java.awt.event.ActionListener)
- ((actionPerformed e) (do-the-action))))
- tmp)
- @end example
- Making use of so-called ``SAM-conversion'' (@pxref{SAM-conversion})
- makes it even more convenient:
- @example
- (javax.swing.JButton
- text: "Do it!"
- action-listener:
- (lambda (e) (do-the-action)))
- @end example
- The general case allows for a mix of
- constructor arguments, property keywords, and child values:
- @display
- @var{class-type} @stxref{constructor-value}... @stxref{property-initializer}... @stxref{child-value}...
- @stxdef{constructor-value} @stxref{expression}
- @stxdef{property-initializer} @stxref{keyword} @stxref{expression}
- @stxdef{child-value} @stxref{expression}
- @end display
- First an object is constructed with the @var{constructor-value} arguments
- (if any) passed to the object constructor;
- then named properties (if any) are used to initialize named properties;
- and then remaining arguments are used to add child values.
- There is an ambiguity if there is no @var{property-initializer} -
- we can't distinguish between a @var{constructor-value}
- and a @var{child-value}.
- In that case, if there is a matching constructor method, then all of the
- arguments are constructor arguments;
- otherwise, there must a default constructor, and all
- of the arguments are @var{child-value} arguments.
- There is a trick you can you if you need both
- @var{constructor-value} and @var{child-value} arguments:
- separate them with an ``empty keyword'' @code{||:}.
- This matches a method named @code{add}, which means that
- the next argument effectively a @var{child-value} - as do
- all the remaining arguments. Example:
- @example
- (let ((vec #(1 2 3)))
- (java.util.ArrayList vec ||: 4 5 6))
- @result{} [1, 2, 3, 4, 5, 6]
- @end example
- The compiler rewrites these allocations expression
- to generated efficient bytecode, assuming that the ``function''
- being applied is a type known by the compiler.
- Most of the above expressions also work if the type is applied
- at run-time, in which case Kawa has to use slower reflection:
- @example
- (define iarr int[])
- (apply iarr (list 3 4 5)) @result{} [3 4 5]
- @end example
- However @code{add@var{Xxx}} methods and SAM-conversion
- are currently only recognized in the case of a class known at compile-time,
- not at run-time.
- Here is a working Swing demo illustrating many of these techniques:
- @example
- (import (class javax.swing
- JButton Box JFrame))
- (define-simple-class HBox (Box)
- ((*init*) (invoke-special Box (this) '*init* 0)))
- (define value 0)
- (define txt
- (javax.swing.JLabel
- text: "0"))
- (define (set-value i)
- (set! value i)
- (set! txt:text (number->string i)))
- (define fr
- (JFrame
- title: "Hello!"
- (Box 1#|VERTICAL|# ||:
- (javax.swing.Box:createGlue)
- txt
- (javax.swing.Box:createGlue)
- (HBox
- (JButton ;; uses 1-argument constructor
- "Decrement" ;; constructor argument
- tool-tip-text: "decrement"
- action-listener: (lambda (e) (set-value (- value 1))))
- (javax.swing.Box:createGlue)
- (JButton ;; uses 0-argument constructor
- text: "Increment"
- tool-tip-text: "increment"
- action-listener: (lambda (e) (set-value (+ value 1))))))))
- (fr:setSize 200 100)
- (set! fr:visible #t)
- @end example
- If you prefer, you can use the older @code{make} special function:
- @deffn Procedure make type args ...
- Constructs a new object instance of the specified @var{type},
- which must be either a @code{java.lang.Class} or a
- @code{<gnu.bytecode.ClassType>}.
- Equivalent to:
- @example
- @var{type} @var{args} ...
- @end example
- @end deffn
- Another (semi-deprecated) function is to use the colon notation
- with the @code{new} pseudo-function.
- The following three are all equivalent:
- @example
- (java.awt.Point:new x: 4 y: 3)
- (make java.awt.Point: x: 4 y: 3)
- (java.awt.Point x: 4 y: 3)
- @end example
- @node Field operations, Mangling, Allocating objects, Objects Classes and Modules
- @section Accessing object fields
- @subsection Accessing static fields and properties
- The recommmended way to access fields
- uses the @ref{Colon notation, colon notation}.
- For static fields and properties the following is recommended:
- @display
- @var{class-expression}@stxlit{:}@var{field-name}
- @end display
- For example:
- @example
- java.lang.Integer:MAX_VALUE
- @end example
- A property with a @code{get} method is equivalent to a field.
- The following are all equivalent:
- @example
- java.util.Currency:available-currencies
- java.util.Currency:availableCurrencies
- (java.util.Currency:getAvailableCurrencies)
- @end example
- Just like for a method call, the @var{class-expression}
- can be a class in the current lexical scope,
- a fully-qualified class name, or more generally an
- expression that evaluates to a class.
- @subsection Accessing instance fields and properties
- The syntax is:
- @display
- @var{instance}@stxlit{:}@var{field-name}
- @end display
- The @var{field-name} can of course be the name of an actual
- object field, but it can also be the name of a property with
- a zero-argument @code{get} method.
- For example, if @code{cal} is a @code{java.util-Calendar} instance,
- then the following are all equivalent:
- @example
- cal:time-zone
- cal:timeZone
- (cal:getTimeZone)
- (cal:get-time-zone)
- @end example
- You can use colon notation to assign to a field:
- @example
- (set! cal:time-zone TimeZone:default)
- @end example
- which is equivalent to:
- @example
- (cal:setTimeZone (TimeZone:getDefault))
- @end example
- A Java array only has the @code{length} field, plus the @code{class} property:
- @example
- (int[] 4 5 6):length @result{} 3
- (int[] 4 5 6):class:name @result{} "int[]"
- @end example
- @subsection Using field and static-field methods
- The following methods are useful in cases where colon notation
- is ambiguous, for example where there are both fields and methods
- with the same name.
- You might also prefer as a matter of style, to
- emphasise that a field is being accessed.
- @deffn Procedure field object fieldname
- Get the instance field with the given @var{fieldname} from the given
- @var{Object}. Returns the value of the field, which must be accessible.
- This procedure has a @code{setter}, and so can be used as the first
- operand to @code{set!}.
- The field name is "mangled" (@pxref{Mangling}) into a valid Java name.
- If there is no accessible field whose name is @code{"@var{fieldname}"},
- we look for a no-argument method whose name is
- @code{"get@var{Fieldname}"} (or @code{"is@var{Fieldname}"} for a
- boolean property).
- If @var{object} is a primitive Java array, then @var{fieldname} can only
- be @code{'length}, and the result is the number of elements of the array.
- @end deffn
- @deffn Procedure static-field class fieldname
- Get the static field with the given @var{fieldname} from the given
- @var{class}. Returns the value of the field, which must be accessible.
- This procedure has a @code{setter}, and so can be used as the first
- operand to @code{set!}.
- If the @var{fieldname} is the special name @code{class},
- then it returns the @code{java.lang.Class} object corresponding to
- @var{class} (which is usually a @code{gnu.bytecode.ClassType} object).
- @end deffn
- Examples:
- @example
- (static-field java.lang.System 'err)
- ;; Copy the car field of b into a.
- (set! (field a 'car) (field b 'car))
- @end example
- @deffn Procedure slot-ref object fieldname
- A synonym for @code{(field @var{object} @var{fieldname})}.
- @end deffn
- @deffn Procedure slot-set! object fieldname value
- A synonym for @code{(set! (field @var{object} @var{fieldname}) @var{value})}.
- @end deffn
- @subsection Older colon-dot notation
- There is older syntax where following the colon
- there is field name a following the colon @emph{and} a period.
- To access an static field named @var{field-name} use this syntax
- @example
- (@var{prefix}:.@var{field-name} @var{instance})
- @end example
- The @var{prefix} can be as discussed in @xref{Method operations}.
- Here are 5 equivalent ways:
- @example
- (java.lang.Integer:.MAX_VALUE)
- (<java.lang.Integer>:.MAX_VALUE)
- (define-namespace Int32 <java.lang.Integer>)
- (Int32:.MAX_VALUE)
- (define-namespace Integer "class:java.lang.Integer")
- (Integer:.MAX_VALUE)
- (define-alias j.l.Integer java.lang.Integer)
- (j.l.Integer:.MAX_VALUE)
- @end example
- You can set a static field using this syntax:
- @example
- (set! (@var{prefix}:.@var{field-name}) @var{new-value})
- @end example
- The special field name @code{class} can be used to extract the
- @code{java.lang.Class} object for a class-type. For example:
- @example
- (java.util.Vector:.class) @result{} class java.util.Vector
- @end example
- To access a instance field named @var{field-name} use the following syntax.
- Note the period before the @var{field-name}.
- @example
- (*:.@var{field-name} @var{instance})
- @end example
- This syntax works with @code{set!} - to set the field use this syntax:
- @example
- (set! (*:.@var{field-name} @var{instance}) @var{new-value})
- @end example
- Here is an example:
- @example
- (define p (list 3 4 5))
- (*:.cdr p) @result{} (4 5)
- (set! (*:.cdr p) (list 6 7))
- p @result{} (3 6 7)
- @end example
- You can specify an explicit class:
- @example
- (@var{prefix}:.@var{field-name} @var{instance})
- @end example
- If @var{prefix} is bound to @code{<@var{class}>}, then the above
- is equivalent to:
- @example
- (*:.@var{field-name} (as <@var{class}> @var{instance}))
- @end example
- @node Mangling, Scheme types in Java, Field operations, Objects Classes and Modules
- @section Mapping Scheme names to Java names
- Programs use "names" to refer to various values and procedures.
- The definition of what is a "name" is different in different
- programming languages. A name in Scheme (and other Lisp-like
- languages) can in principle contain any character (if using a
- suitable quoting convention), but typically names consist of
- "words" (one or more letters) separated by hyphens, such
- as @samp{make-temporary-file}. Digits
- and some special symbols are also used. Traditionally, Scheme is
- case-insensitive; this means that the names @samp{loop},
- @samp{Loop}, and @samp{LOOP} are all the same name. Kawa
- is by default case-sensitive, but we recommend that you
- avoid using upper-case letters as a general rule.
- The Java language and the Java virtual machine uses names for
- classes, variables, fields and methods.
- Names in the Java language can contain upper- and lower-case letters,
- digits, and the special symbols @samp{_} and @samp{$}.
- The Java virtual machine (JVM) allows most characters, but still
- has some limitations.
- Kawa translates class names, package names, field names, and local variable
- names using the
- @uref{https://blogs.oracle.com/jrose/entry/symbolic_freedom_in_the_vm,''symbolic'' convention}, so most characters are unchanged.
- For example the Scheme function @samp{file-exists?}
- becomes the field @samp{file-exists?}, but @code{dotted.name}
- becomes @samp{dotted\,name}.
- Such names may not be valid Java name, so to access them from a
- Java program you might have to use reflection.
- When translating procedure names to method names,
- Kawa uses a different translation, in order to achieve
- more ``Java-like'' names. This means translating a
- Scheme-style name like @samp{make-temporary-file} to
- "mixed-case" words, such as @samp{makeTemporaryFile}.
- The basic rule is simple: Hyphens are dropped, and
- a letter that follows a hyphen is translated to its
- upper-case (actually "title-case") equivalent. Otherwise,
- letters are translated as is.
- Some special characters are handled specially. A final @samp{?}
- is replaced by an @emph{initial} @samp{is}, with the following
- letter converted to titlecase. Thus @samp{number?} is
- converted to @samp{isNumber} (which fits with Java conventions),
- and @samp{file-exists?} is converted to @samp{isFileExists}
- (which doesn't really).
- The pair @samp{->} is translated to @samp{$To$}.
- For example @samp{list->string} is translated to @samp{list$To$string}.
- Some symbols are mapped to a mnemonic sequence, starting with a dollar-sign,
- followed by a two-character abbreviation. For example, the less-than
- symbol @samp{<} is mangled as @samp{$Ls}.
- See the source code to the @code{mangleName} method in the
- @code{gnu.expr.Mangling} class for the full list.
- Characters that do not have a mnemonic abbreviation are
- mangled as @samp{$} followed by a four-hex-digit unicode value.
- For example @samp{Tamil vowel sign ai} is mangled as @samp{$0bc8}.
- Note that this mapping may map different Scheme names to the
- same Java name. For example @samp{string?}, @samp{String?},
- @samp{is-string}, @samp{is-String},
- and @samp{isString} are all mapped to the same Java identifier
- @samp{isString}. Code that uses such "Java-clashing" names
- is @emph{not} supported. There is very partial support for
- renaming names in the case of a clash, and there may be better
- support in the future. However, some of the nice features of
- Kawa depend on being able to map Scheme name to Java names
- naturally, so we urge you to @emph{not} write code that
- "mixes" naming conventions by using (say) the names @samp{open-file}
- and @samp{openFile} to name two different objects.
- @node Scheme types in Java, Array operations, Mangling, Objects Classes and Modules
- @section Scheme types in Java
- All Scheme values are implemented by sub-classes of @samp{java.lang.Object}.
- Scheme symbols are implemented using @code{java.lang.String}.
- (Don't be confused by the fact the Scheme sybols are represented
- using Java Strings, while Scheme strings are represented by
- @code{gnu.lists.FString}. It is just that the semantics of Java strings
- match Scheme symbols, but do not match mutable Scheme strings.)
- Interned symbols are presented as interned Strings.
- (Note that with JDK 1.1 string literals are automatically interned.)
- Scheme integers are implemented by @code{gnu.math.IntNum}.
- Use the make static function to create a new IntNum from an int or a long.
- Use the intValue or longValue methods to get the int or long value of
- an IntNum.
- A Scheme "flonum" is implemented by @code{gnu.math.DFloNum}.
- A Scheme pair is implemented by @code{gnu.lists.Pair}.
- A Scheme vector is implemented by @code{gnu.lists.FVectror}.
- Scheme characters are implemented using @code{gnu.text.Char}.
- Scheme strings are implemented using @code{gnu.lists.FString}.
- Scheme procedures are all sub-classes of @code{gnu.mapping.Procedure}.
- The "action" of a @samp{Procedure} is invoked by using one of
- the @samp{apply*} methods: @samp{apply0}, @samp{apply1},
- @samp{apply2}, @samp{apply3}, @samp{apply4}, or @samp{applyN}.
- Various sub-class of @samp{Procedure} provide defaults
- for the various @samp{apply*} methods. For example,
- a @samp{Procedure2} is used by 2-argument procedures.
- The @samp{Procedure2} class provides implementations of all
- the @samp{apply*} methods @emph{except} @samp{apply2},
- which must be provided by any class that extends @code{Procedure2}.
- @node Array operations, Loading Java functions into Scheme, Scheme types in Java, Objects Classes and Modules
- @section Using Java Arrays
- @anchor{Creating-new-Java-arrays}
- @subsection Creating new Java arrays
- To allocate a Java array you can use the array type specifier
- as a constructor function. For example, to allocate an array with room for 10 elements
- each of each is a primitive @code{int}:
- @example
- (int[] length: 10)
- @end example
- You can specify the initial elements instead of the length:
- @example
- (object[] 31 32 33 34)
- @end example
- This creates a 4-length array, initialized to the given values.
- Note this is a variation of the generation object-allocation
- (@pxref{Allocating objects}) pattern. You can explicitly
- use the @code{make} function, if you prefer:
- @example
- (make object[] 31 32 33 34)
- @end example
- If you specify a length, you can also specify initial values for selected
- elements.
- If you specify an index, in the form of a literal integer-valued keyword,
- then following elements are placed starting at that position.
- @example
- (int[] length: 100 10 12 80: 15 16 50: 13 14)
- @end example
- This creates an array with 100 elements. Most of them are initialized
- to the default value of zero,
- but elements with indexes 0, 1, 50, 51, 80, 81 are initialized
- to the values 10, 12, 13, 14, 15, 16, respectively.
- @subsection Accessing Java array elements
- You can access the elements of a Java array by treating it as
- a one-argument function, where the argument is the index:
- @example
- (define primes (integer[] 2 3 5 7 11 13))
- (primes 0) @result{} 2
- (primes 5) @result{} 13
- @end example
- You can set an element by treating the array as a function
- with a @code{setter}:
- @example
- (set! (primes 0) -2)
- (set! (primes 3) -7)
- primes @result{} [-2 3 5 -7 11 13]
- @end example
- To get the number of elements of an array, you can treat
- it as having a @code{length} field:
- @example
- primes:length @result{} 6
- @end example
- Here is a longer example. This is the actual definition of the
- standard @code{gcd} function. Note the @code{args} variable
- receives all the arguments on the form of an @code{integer} array.
- (This uses the Java5 varargs feature.)
- @example
- (define (gcd #!rest (args ::integer[])) ::integer
- (let ((n ::int args:length))
- (if (= n 0)
- 0
- (let ((result ::integer (args 0)))
- (do ((i ::int 1 (+ i 1)))
- ((>= i n) result)
- (set! result (gnu.math.IntNum:gcd result (args i))))))))
- @end example
- The above example generates good code,
- thanks to judicious use of casts and type specifications.
- In general, if Kawa knows that
- a ``function'' is an array then it will generate efficient
- bytecode instructions for array operations.
- @subsection Old low-level array macros
- The deprecated @ref{Low-level array macros} are also supported.
- @node Loading Java functions into Scheme, Evaluating Scheme expressions from Java, Array operations, Objects Classes and Modules
- @section Loading Java functions into Scheme
- When @code{kawa -C} compiles (@pxref{Files compilation}) a Scheme module
- it creates a class that implements the @code{java.lang.Runnable} interface.
- (Usually it is a class that extends the @code{gnu.expr.ModuleBody}.)
- It is actually fairly easy to write similar "modules" by hand in Java,
- which is useful when you want to extend Kawa with new "primitive functions"
- written in Java. For each function you need to create an object that
- extends @code{gnu.mapping.Procedure}, and then bind it in the global
- environment. We will look at these two operations.
- There are multiple ways you can create a @code{Procedure} object. Below
- is a simple example, using the @code{Procedure1} class, which is class
- extending @code{Procedure} that can be useful for one-argument
- procedure. You can use other classes to write procedures. For example
- a @code{ProcedureN} takes a variable number of arguments, and you must
- define @code{applyN(Object[] args)} method instead of @code{apply1}.
- (You may notice that some builtin classes extend @code{CpsProcedure}.
- Doing so allows has certain advantages, including support for
- full tail-recursion, but it has some costs, and is a bit trickier.)
- @example
- import gnu.mapping.*;
- import gnu.math.*;
- public class MyFunc extends Procedure1
- @{
- // An "argument" that is part of each procedure instance.
- private Object arg0;
- public MyFunc(String name, Object arg0)
- @{
- super(name);
- this.arg0 = arg0;
- @}
- public Object apply1 (Object arg1)
- @{
- // Here you can so whatever you want. In this example,
- // we return a pair of the argument and arg0.
- return gnu.lists.Pair.make(arg0, arg1);
- @}
- @}
- @end example
- You can create a @code{MyFunc} instance and call it from Java:
- @example
- Procedure myfunc1 = new MyFunc("my-func-1", Boolean.FALSE);
- Object aresult = myfunc1.apply1(some_object);
- @end example
- The name @code{my-func-1} is used when @code{myfunc1} is printed
- or when @code{myfunc1.toString()} is called. However,
- the Scheme variable @code{my-func-1} is still not bound.
- To define the function to Scheme, we can create
- a "module", which is a class intended to be loaded
- into the top-level environment. The provides the definitions to be
- loaded, as well as any actions to be performed on loading
- @example
- public class MyModule
- @{
- // Define a function instance.
- public static final MyFunc myfunc1
- = new MyFunc("my-func-1", IntNum.make(1));
- @}
- @end example
- If you use Scheme you can use @code{require}:
- @example
- #|kawa:1|# (require <MyModule>)
- #|kawa:2|# (my-func-1 0)
- (1 0)
- @end example
- Note that @code{require} magically defines @code{my-func-1} without
- you telling it to. For each public final
- field, the name and value of the field are entered in the
- top-level environment when the class is loaded. (If there are
- non-static fields, or the class implements @code{Runnable}, then
- an instance of the object is created, if one isn't available.)
- If the field value is a @code{Procedure} (or implements @code{Named}),
- then the name bound to the procedure is used instead of the field name.
- That is why the variable that gets bound in the Scheme environment is
- @code{my-func-1}, not @code{myfunc1}.
- Instead of @code{(require <MyModule>)}, you can do @code{(load "MyModule")}
- or @code{(load "MyModule.class")}.
- If you're not using Scheme, you can use Kawa's @code{-f} option:
- @example
- $ kawa -f MyModule --xquery --
- #|kawa:1|# my-func-1(3+4)
- <list>1 7</list>
- @end example
- If you need to do some more complex calculations when a module is loaded,
- you can put them in a @code{run} method, and have the module
- implement @code{Runnable}:
- @example
- public class MyModule implements Runnable
- @{
- public void run ()
- @{
- Interpreter interp = Interpreter.getInterpreter();
- Object arg = Boolean.TRUE;
- interp.defineFunction (new MyFunc ("my-func-t", arg));
- System.err.println("MyModule loaded");
- @}
- @}
- @end example
- Loading @code{MyModule} causes @code{"MyModule loaded"} to be printed,
- and @code{my-func-t} to be defined. Using @code{Interpreter}'s
- @code{defineFunction} method is recommended because it does the righ
- things even for languages like Common Lisp that use separate
- "namespaces" for variables and functions.
- A final trick is that you can have a @code{Procedure} be its own module:
- @example
- import gnu.mapping.*;
- import gnu.math.*;
- public class MyFunc2 extends Procedure2
- @{
- public MyFunc(String name)
- @{
- super(name);
- @}
- public Object apply2 (Object arg1, arg2)
- @{
- return gnu.lists.Pair.make(arg1, arg2);
- @}
- public static final MyFunc myfunc1 = new MyFunc("my-func-2);
- @}
- @end example
- @node Evaluating Scheme expressions from Java, , Loading Java functions into Scheme, Objects Classes and Modules
- @section Evaluating Scheme expressions from Java
- The following methods are recommended if you need to evaluate a
- Scheme expression from a Java method.
- (Some details (such as the @samp{throws} lists) may change.)
- @deftypefn {Static method} void Scheme.registerEnvironment ()
- Initializes the Scheme environment. Maybe needed if you
- try to load a module compiled from a Scheme source file.
- @end deftypefn
- @deftypefn {Static method} Object Scheme.eval (InPort @var{port}, Environment @var{env})
- Read expressions from @var{port}, and evaluate them in the
- @var{env} environment, until end-of-file is reached.
- Return the value of the last expression,
- or @code{Interpreter.voidObject} if there is no expression.
- @end deftypefn
- @deftypefn {Static method} Object Scheme.eval (String @var{string}, Environment @var{env})
- Read expressions from @var{string}, and evaluate them in the
- @var{env} environment, until the end of the string is reached.
- Return the value of the last expression,
- or @code{Interpreter.voidObject} if there is no expression.
- @end deftypefn
- @deftypefn {Static method} Object Scheme.eval (Object @var{sexpr}, Environment @var{env})
- The @var{sexpr} is an S-expression (as may be returned by @code{read}).
- Evaluate it in the @var{env} environment, and return the result.
- @end deftypefn
- For the @code{Environment} in most cases you could use
- @samp{Environment.current()}. Before you start, you
- need to initialize the global environment,
- which you can do with
- @example
- Environment.setCurrent(new Scheme().getEnvironment());
- @end example
- Alternatively, rather than setting the global environment,
- you can use this style:
- @example
- Scheme scm = new Scheme();
- Object x = scm.eval("(+ 3 2)");
- System.out.println(x);
- @end example
- @subsection Using @code{javax.script} portable Java scripting
- Kawa also supports the standard
- @uref{http://docs.oracle.com/javase/8/docs/api/javax/script/package-summary.html,@code{javax.script}} API.
- The main advantage of this API is if you want your users to be able to choose
- between multiple scripting languages. That way you can support Kawa
- without Kawa-specific programming.
- For example the standard JDK tool @uref{http://docs.oracle.com/javase/8/docs/technotes/tools/unix/jrunscript.html,jrunscript} provides a
- read-eval-print-loop for any language that implements the @code{javax.script}
- API. It knows nothing about Kawa but can still use it:
- @example
- $ jrunscript -cp kawa.jar -l scheme
- scheme> (cadr '(3 4 5))
- 4
- @end example
- (Of course the @code{jrunscript} REPL isn't as nice as the one that
- Kawa provides. For example the latter can handle multi-line inputs.)
- @node XML tools
- @chapter Working with XML and HTML
- Kawa has a number of features for working with XML, HTML,
- and generated web pages.
- In Kawa you don't write XML or HTML directly.
- Instead you write expressions that evaluate to ``node objects''
- corresponding to elements, attributes, and text.
- You then write these node objects using either an XML or HTML format.
- Many web-page-generating tools require you to work directly
- with raw HTML, as for example:
- @example
- (display "<p>Don't use the <code><blink></code> tag.</p>")
- @end example
- In Kawa you would instead do:
- @example
- (display (html:p "Don't use the " (html:code "<blink>") " tag."))
- @end example
- The conversion from node objects to XML or HTML is handled by
- the formatter (or serializer).
- Some advantages of doing it this way are:
- @itemize
- @item
- You don't have to worry about quoting special characters.
- Missing or incorrect quoting is a common source of bugs
- and security problems on systems that work directly with text, such as PHP.
- @item
- Some errors, such as mismatched element tags, are automatically avoided.
- @item
- The generated XML can be validated as it is generated,
- or even using compile-time type-checking. (Kawa doesn't yet do either.)
- @item
- In an application that also reads XML,
- you can treat XML that is read in and XML that is generated using
- the same functions.
- @end itemize
- @menu
- * Formatting XML::
- * Creating HTML nodes::
- * Creating XML nodes::
- * XML literals::
- * Server-side scripts:: Writing web-server-side Kawa scripts
- * Self-configuring page scripts::
- * Servlets:: Installing Kawa programs as Servlets
- * CGI scripts:: Installing Kawa programs as CGI scripts
- * HTTP requests:: Functions for accessing HTTP requests
- * HTTP response:: Functions for generating HTTP response
- * XML beyond Scheme:: Using non-Scheme languages for XML/HTML
- @end menu
- @node Formatting XML, Creating HTML nodes, , XML tools
- @section Formatting XML
- The easiest way to generate HTML or XML output is to run Kawa
- with the appropriate @ref{Named output formats, , @code{--output-format} option}.
- The intentation is that these output modes should be compatible with
- @uref{http://www.w3.org/TR/2006/PR-xslt-xquery-serialization-20061121/,
- XSLT 2.0 and XQuery 1.0 Serialization}.
- (However, that specifies many options, most
- of which have not yet been implemented.
- @table @code
- @item xml
- Values are printed in XML format.
- "Groups" or "elements" are written as using xml element syntax.
- Plain characters (such as @samp{<}) are escaped (such as @samp{<}).
- @item xhtml
- Same as @code{xml}, but follows the xhtml compatibility guidelines.
- @item html
- Values are printed in HTML format.
- Mostly same as @code{xml} format, but certain elements without body,
- are written without a closing tag. For example @code{<img>} is written
- without @code{</img>}, which would be illegal for html, but required for xml.
- Plain characters (such as @samp{<}) are not escaped inside @code{<script>}
- or @code{<style>} elements.
- @end table
- To illustrate:
- @example
- $ kawa --output-format html
- #|kawa:1|# (html:img src:"img.jpg")
- <img src="img.jpg">
- @end example
- @example
- $ kawa --output-format xhtml
- #|kawa:1|# (html:img src:"img.jpg")
- <img xmlns="http://www.w3.org/1999/xhtml" src="img.jpg" />
- @end example
- @example
- $ kawa --output-format xml
- #|kawa:1|# (html:img src:"img.jpg")
- <img xmlns="http://www.w3.org/1999/xhtml" src="img.jpg"></img>
- @end example
- And here is the default @code{scheme} formatting:
- @example
- $ kawa
- #|kawa:1|# (html:img src:"img.jpg")
- (@{http://www.w3.org/1999/xhtml@}img src: img.jpg )
- @end example
- @deffn Procedure as-xml value
- Return a value (or multiple values) that when printed will
- print @var{value} in XML syntax.
- @example
- (require 'xml)
- (as-xml (make-element 'p "Some " (make-element 'em "text") "."))
- @end example
- prints @code{<p>Some <em>text</em>.</p>}.
- @end deffn
- @deffn Procedure unescaped-data data
- Creates a special value which causes @code{data} to be printed,
- as is, without normal escaping. For example, when the output format
- is XML, then printing @code{"<?xml?>"} prints as @samp{<?xml?>},
- but @code{(unescaped-data "<?xml?>")} prints as @samp{<?xml?>}.
- @end deffn
- @node Creating HTML nodes, Creating XML nodes, Formatting XML, XML tools
- @section Creating HTML nodes
- The @code{html} prefix names a special namespace
- (@pxref{Namespaces}) of functions to create HTML element nodes.
- For example, @code{html:em} is a constructor that
- when called creates a element node whose tag is @code{em}.
- If this element node is formatted as HTML, the
- result has an @code{<em>} tag.
- @deffn Syntax html:@var{tag} attributes ... content ...
- Creates an element node whose tag is @var{tag}.
- The parameters are first zero or more attributes, followed
- by zero of more child values.
- An attribute is either an attribute value (possibly
- created using @code{make-attribute}), or a pair of arguments:
- A keyword followed by the attribute value.
- Child values are usually either strings (text content),
- or nested element nodes, but can also be comment or processing-instruction
- nodes.
- @example
- (html:a href: "http://gnu.org/" "the "(html:i "GNU")" homepage")
- @end example
- @end deffn
- The compound identifier @code{html:@var{tag}} is actually
- a type: When you call it as a function you're using Kawa's
- standard coercion of a type to its constructor function.
- This means you can do type tests:
- @example
- (define some-node ...)
- (if (instance? some-node html:blink)
- (error "blinking not allowed!"))
- @end example
- Object identity is currently not fully specified. Specifically,
- it is undefined if a nested (child) element node is copied
- ``by value'' or ``by reference''. This is related to whether
- nodes have a parent reference. In the XPath/XQuery data model
- nodes do have a parent reference, and child nodes are conceptually
- copied. (In the actual implemention copying is commonly avoided.)
- Kawa/Scheme currently followed the XQuery copying semantics,
- which may not be the most appropriate for Scheme.
- @node Creating XML nodes, XML literals, Creating HTML nodes, XML tools
- @section Creating XML nodes
- The XML data model is similar to HTML, with one important addition:
- XML tags may be @dfn{qualified names}, which are similar
- to @ref{Namespaces, , compound symbols}.
- You must do this to use the following types and functions:
- @example
- (require 'xml)
- @end example
- The following types and functions assume:
- @example
- (require 'xml)
- @end example
- @deffn Procedure make-element tag [attribute ...] child ...
- Create a representation of a XML element, corresponding to
- @example
- <@var{tag} @var{attribute}...>@var{child}...</@var{tag}>
- @end example
- The result is a @code{TreeList}, though if the result context is a consumer
- the result is instead "written" to the consumer.
- Thus nested calls to @code{make-element} only result in a
- single @code{TreeList}.
- More generally, whether an @var{attribute} or @var{child} is includded
- by copying or by reference is (for now) undefined.
- The @var{tag} should currently be a symbol, though in the future it should
- be a qualified name.
- An @var{attribute} is typically a call to @code{make-attribute},
- but it can be any attribute-valued expression.
- @example
- (make-element 'p
- "The time is now: "
- (make-element 'code (make <java.util.Date>)))
- @end example
- @end deffn
- @deffn Procedure element-name element
- Returns the name (tag) of the element node, as a symbol (QName).
- @end deffn
- @deffn Procedure make-attribute name value...
- Create an "attribute", which is a name-value pair.
- For now, @var{name} should be a symbol.
- @end deffn
- @deffn Procedure attribute-name element
- Returns the name of the attribute node, as a symbol (QName).
- @end deffn
- @deffn Type comment
- Instances of this type represent comment values,
- specifically including comments in XML files.
- Comment nodes are currently ignored when printing using Scheme formatting,
- though that may change.
- @end deffn
- @deffn Constructor comment comment-text
- Create a comment object with the specified @var{comment-text}.
- @end deffn
- @deffn Type processing-instruction
- Instances of this type represent ``processing instructions'',
- such as may appear in XML files.
- Processing-instruction nodes are currently ignored when printing using
- Scheme formatting, though that may change.
- @end deffn
- @deffn Constructor processing-instruction target contents
- Crreate a processing-instruction object with the specified @var{target}
- (a simple symbol) and @var{contents} (a string).
- @end deffn
- @node XML literals, Server-side scripts, Creating XML nodes, XML tools
- @section XML literals
- You can write XML literals directly in Scheme code,
- following a @code{#}.
- Notice that the outermost element needs to be prefixed
- by @code{#}, but nested elements do not (and must not).
- @example
- #<p>The result is <b>final</b>!</p>
- @end example
- Actually, these are not really literals since they can contain
- enclosed expressions:
- @example
- #<em>The result is &@{result@}.</em>
- @end example
- The value of @var{result} is substituted into the output,
- in a similar way to quasi-quotation.
- (If you try to quote one of these ``XML literals'',
- what you get is unspecified and is subject to change.)
- An @var{xml-literal} is usually an element constructor,
- but there some rarely used forms (processing-instructions,
- comments, and CDATA section) we'll cover later.
- @display
- @stxdef{xml-literal} @stxlit{#}@stxref{xml-constructor}
- @stxdef{xml-constructor} @stxref{xml-element-constructor}
- | @stxref{xml-PI-constructor}
- | @stxref{xml-comment-constructor}
- | @stxref{xml-CDATA-constructor}
- @end display
- @subsection Element constructors
- @display
- @stxdef{xml-element-constructor}
- @stxlit{<}@stxref{QName} @stxref{xml-attribute}*@stxlit{>}@stxref{xml-element-datum}...@stxlit{</}@stxref{QName} @stxlit{>}
- | @stxlit{<}@stxref{xml-name-form} @stxref{xml-attribute}*@stxlit{>}@stxref{xml-element-datum}...@stxlit{</>}
- | @stxlit{<}@var{xml-name-form} @stxref{xml-attribute}*@stxlit{/>}
- @stxdef{xml-name-form} @stxref{QName}
- | @stxref{xml-enclosed-expression}
- @stxdef{xml-enclosed-expression}
- @stxlit{@lbracechar{}}@stxref{expression}@stxlit{@rbracechar{}}
- | @stxlit{(}@stxref{expression}...@stxlit{)}
- @end display
- The first @var{xml-element-constructor} variant uses a literal @var{QName},
- and looks like standard non-empty XML element, where the starting @var{QName}
- and the ending @var{QName} must match exactly:
- @example
- #<a href="next.html">Next</a>
- @end example
- As a convenience, you can leave out the ending tag(s):
- @example
- This is a paragraph in <emphasis>DocBook</> syntax.</>
- @end example
- You can use an expression to compute the element tag at runtime -
- in that case you @emph{must} leave out the ending tag:
- @example
- #<p>This is <(if be-bold 'strong 'em)>important</>!</p>
- @end example
- You can use arbitrary @var{expression} inside curly braces,
- as long as it evaluates to a symbol.
- You can leave out the curly braces
- if the @var{expression} is a simple parenthesised compound expression.
- The previous example is equivalent to:
- @example
- #<p>This is <@{(if be-bold 'strong 'em)@}>important</>!</p>
- @end example
- The third @var{xml-element-constructor} variant above is an XML
- ``empty element''; it is equivalent to the second variant
- when there are no @var{xml-element-datum} items.
- (Note that every well-formed XML element, as defined in the XML specifications,
- is a valid @var{xml-element-constructor}, but not vice versa.)
- @subsection Elements contents (children)
- The ``contents'' (children) of an element
- are a sequence of character (text) data, and nested nodes.
- The characters @code{&}, @code{<}, and @code{>} are special,
- and need to be escaped.
- @display
- @stxdef{xml-element-datum}
- any character except @code{&}, or @code{<}.
- | @stxref{xml-constructor}
- | @stxref{xml-escaped}
- @stxdef{xml-escaped}
- @stxlit{&}@stxref{xml-enclosed-expression}
- | @stxlit{&}@stxref{xml-entity-name}@stxlit{;}
- | @stxref{xml-character-reference}
- @stxdef{xml-character-reference}
- @stxlit{&#}@stxref{digit}+@stxlit{;}
- | @stxlit{&#x}@stxref{hex-digit}+@stxlit{;}
- @end display
- Here is an example shows both hex and decimal character references:
- @example
- #<p>ABCDE</p> @result{} <p>ABCDE</p>
- @end example
- @display
- @stxdef{xml-entity-name} @stxref{identifier}
- @end display
- Currently, the only supported values for @var{xml-entity-name}
- are the builtin XML names @code{lt}, @code{gt}, @code{amp},
- @code{quot}, and @code{apos}, which stand for the characters
- @code{<}, @code{>}, @code{&}, @code{"}, and @code{'}, respectively.
- The following two expressions are equivalent:
- @example
- #<p>< > & " '</p>
- #<p>&@{"< > & \" '"@}</p>
- @end example
- @subsection Attributes
- @display
- @stxdef{xml-attribute}
- @stxref{xml-name-form}@stxlit{=}@stxref{xml-attribute-value}
- @stxdef{xml-attribute-value}
- @stxlit{"}@stxref{quot-attribute-datum}*@stxlit{"}
- | @stxlit{'}@stxref{apos-attribute-datum}*@stxlit{'}
- @stxdef{quot-attribute-datum}
- any character except @code{"}, @code{&}, or @code{<}.
- | @stxref{xml-escaped}
- @stxdef{apos-attribute-datum}
- any character except @code{'}, @code{&}, or @code{<}.
- | @stxref{xml-escaped}
- @end display
- If the @var{xml-name-form} is either @code{xmlns} or
- a compound named with the prefix @code{xmlns}, then
- technically we have a namespace declaration, rather than
- an attribute.
- @subsection QNames and namespaces
- The names of elements and attributes are @dfn{qualified names}
- (QNames), which are represented using compound symbols (@pxref{Namespaces}).
- The lexical syntax for a QName is either a simple identifier,
- or a (prefix,local-name) pair:
- @display
- @stxdef{QName} @stxref{xml-local-part}
- | @stxref{xml-prefix}@stxlit{:}@stxref{xml-local-part}
- @stxdef{xml-local-part} @stxref{identifier}
- @stxdef{xml-prefix} @stxref{identifier}
- @end display
- An @var{xml-prefix} is an alias for a namespace-uri,
- and the mapping between them is defined by a namespace-declaration.
- You can either use a @code{define-namespace} form, or you
- can use a @dfn{namespace declaration attribute}:
- @display
- @stxdef{xml-namespace-declaration-attribute}
- @stxlit{xmlns:}@stxref{xml-prefix}@stxlit{=}@stxref{xml-attribute-value}
- | @stxlit{xmlns=}@stxref{xml-attribute-value}
- @end display
- The former declares @var{xml-prefix} as a namespace alias for
- the namespace-uri specified by @var{xml-attribute-value}
- (which must be a compile-time constant).
- The second declares that @var{xml-attribute-value} is the default
- namespace for simple (unprefixed) element tags.
- (A default namespace declaration is ignored for attribute names.)
- @example
- (let ((qn (element-name #<gnu:b xmlns:gnu="http://gnu.org/"/>)))
- (list (symbol-local-name qn)
- (symbol-prefix qn)
- (symbol-namespace-uri qn)))
- @result{} ("b" "gnu" "http://gnu.org/")
- @end example
- @subsection Other XML types
- @subsubsection Processing instructions
- An @var{xml-PI-constructor} can be used to create an XML
- @dfn{processing instruction}, which can be used to pass
- instructions or annotations to an XML processor (or tool).
- (Alternatively, you can use the @code{processing-instruction}
- type constructor.)
- @display
- @stxdef{xml-PI-constructor} @stxlit{<?}@stxref{xml-PI-target} @stxref{xml-PI-content}@stxlit{?>}
- @stxdef{xml-PI-target} @var{NCname} (i.e. a simple (non-compound) identifier)
- @stxdef{xml-PI-content} any characters, not containing @code{?>}.
- @end display
- For example, the DocBook XSLT stylesheets can use the @code{dbhtml}
- instructions to specify that a specific chapter should be
- written to a named HTML file:
- @example
- #<chapter><?dbhtml filename="intro.html" ?>
- <title>Introduction</title>
- ...
- </chapter>
- @end example
- @subsubsection XML comments
- You can cause XML comments to be emitted in the XML output document.
- Such comments can be useful for humans reading the XML document,
- but are usually ignored by programs.
- (Alternatively, you can use the @code{comment} type constructor.)
- @display
- @stxdef{xml-comment-constructor} @stxlit{<!--}@stxref{xml-comment-content}@stxlit{-->}
- @stxdef{xml-comment-content} any characters, not containing @code{--}.
- @end display
- @subsubsection CDATA sections
- A @code{CDATA} section can be used to avoid excessive use of
- @var{xml-entity-ref} such as @code{&} in element content.
- @display
- @stxdef{xml-CDATA-constructor} @stxlit{<![CDATA[}@stxref{xml-CDATA-content}@stxlit{]]>}
- @stxdef{xml-CDATA-content} any characters, not containing @code{]]>}.
- @end display
- The following are equivalent:
- @example
- #<p>Specal characters <![CDATA[< > & ' "]]> here.</p>
- #<p>Specal characters < > & " ' here.</p>
- @end example
- Kawa remembers that you used a @code{CDATA} section in
- the @var{xml-element-constructor} and will write it out
- using a @code{CDATA} constructor.
- @node Server-side scripts, Self-configuring page scripts, XML literals, XML tools
- @section Web page scripts
- A Kawa @dfn{web page script} is a Kawa program that is invoked
- by a web server because the server received an HTTP request.
- The result of evaluating the top-level expressions becomes
- the HTTP response that the servlet sends back to the client, usually a browser.
- A web page script may be as simple as:
- @example
- (format "The time is <~s>." (java.util.Date))
- @end example
- This returns a response of consisting of a formatted string
- giving the current time.
- The string would interpreted as @code{text/plain} content:
- The angle brackets are regular characters, and not
- HTML tag markers.
- The script can alternatively evaluate to XML/HTML node
- values, for example those created by @ref{XML literals}:
- @example
- #<p>Hello, <b>&(request-remote-host)</b>!</p>
- @end example
- In this case the response would be @code{text/html} or similar content:
- The angle brackets should be interpreted by the browser as HTML tag markers.
- The function @code{request-remote-host} is available (automatically)
- to web page scripts; it returns the host that made the HTTP request,
- which is then interpolated into the response.
- Following sections will go into more details about how
- to write web page scripts. You can do so in any supported
- Kawa language, including Scheme, BRL, KRL, or XQuery.
- A web server will use a URL mapping to map a request URL
- to a specific web page script. This can be done in a
- number of different ways:
- @itemize
- @item
- The easiest to manage is to use Kawa's mechanism for
- @ref{Self-configuring page scripts}. Ths is especially
- easy if you the web server built in to JDK 6, since no
- configuration files are needed.
- You can also use a ``servlet engine'' like Tomcat or Glassfish.
- @item
- You can explicitly compile the web page script to a servlet,
- in the same way Java servlets are compiled.
- This can then be installed ("deployed") in a servlet-supporting
- web server, such a Tomcat or Glassfish. @xref{Servlets}.
- @item
- You can run the servlet as a @ref{CGI scripts,CGI script}.
- @end itemize
- For details on how to extract information from the request
- see @ref{HTTP requests}.
- For details on how the response is created see @ref{HTTP response,Generating HTTP responses}.
- If the response is HTML or XML, you may want to
- read @ref{Creating HTML nodes}, or @ref{Creating XML nodes},
- or @ref{XML literals}.
- Here are some examples, starting with a simple @code{hello.scm}:
- @example
- (response-content-type 'text/html) ; Optional
- (html:p
- "The request URL was: " (request-url))
- (make-element 'p
- (let ((query (request-query-string)))
- (if query
- (values-append "The query string was: " query)
- "There was no query string.")))
- @end example
- This returns two @code{<p>} (paragraph) elements: One using
- @code{make-element} and one using the @code{html:p} constructor.
- Or you may prefer to use @ref{XML literals}.
- The same program using KRL:
- @example
- <p>The request URL was: [(request-url)]</p>,
- <p>[(let ((query (request-query-string)))
- (if query
- (begin ]The query string was: [query)
- ]There was no query string.[))]</p>
- @end example
- You can also use XQuery:
- @example
- <p>The request URL was: @{request-url()@}</p>
- <p>@{let $query := request-query-string() return
- if ($query)
- then ("The query string was: ",$query)
- else "There was no query string."@}</p>
- @end example
- The @code{+default+} script in the @code{doc} directory is
- useful for reading the Kawa documentation using a browser.
- The script uses the @code{jar:} URL scheme to automatically extract
- and uncompress the pages from @code{doc/kawa-manual.epub},
- which is in EPUB3 format. Read the script for usage instructions.
- @node Self-configuring page scripts, Servlets, Server-side scripts, XML tools
- @section Self-configuring web page scripts
- Kawa makes it easy to set up a web site without configuration
- files. Instead, the mapping from request URL to web page script
- matches the layout of files in the application directory.
- Many web servers make it easy to execute a script using a script
- processor which is selected depending on the extension of the
- requested URL. That is why you see lots of URLs that end in
- @code{.cgi}, @code{.php}, or @code{.jsp}. This is bad, because it
- exposes the server-side implementation to the user: Not only are such
- URLs ugly, but they make it difficult to change the server without
- breaking people's bookmarks and search engines. A server will usually
- provide a mechanism to use prettier URLs, but doing so requires extra
- effort, so many web-masters don't.
- If you want a script to be executed in response to a URL
- @code{http://host/app/foo/bar} you give the script the name
- @code{app/foo/bar}, in the appropriate server ``application''
- directory (as explained below). You get to pick the name @code{bar}.
- Or you can use the name @code{bar.html}, even though the file named
- @code{bar.html} isn't actually
- an html file - rather it produces html when evaluated. Or better: just use
- a name without an extension at all.
- Kawa figures
- out what kind of script it is based on the content of the file,
- rather than the file name. Once Kawa has
- found a script, it looks at the first line to see if it can recognize
- the kind (language) of the script. Normally this would be a comment
- that contains the name of a programming language that Kawa
- knows about. For example:
- @example
- ;; Hello world page script written in -*- scheme -*-
- #<p>Hello, <b>&(request-remote-host)</b>!</p>
- @end example
- (Using the funny-looking string @code{-*- scheme -*-} has the
- bonus is that it recognized by the Emacs text editor.)
- A script named @code{+default+} is run if there isn't a matching script.
- For example assume the following is a file named @code{+default}.
- @example
- ;; This is -*- scheme -*-
- (make-element 'p "servlet-path: " (request-servlet-path))
- @end example
- This becomes the default script for HTTP requests that aren't handled
- by a more specific script.
- The @code{request-servlet-path} function
- returns the "servlet path", which is the part of the requested URL
- that is relative to the current web application. Thus a request for
- @code{http://host:port/app/this/is/a/test} will return:
- @example
- servlet-path: /this/is/a/test
- @end example
- You can use the feature variable @code{in-http-server}
- in a @code{cond-expand} to test if the code is executing
- in a web server.
- @subsection Using the OpenJDK built-in web server
- The easiest way to run a Kawa web server is to
- use the web server built in to JDK 6 or later.
- @example
- kawa --http-auto-handler @var{context-path} @var{appdir} --http-start @var{port}
- @end example
- This starts a web server that listens on the given @var{port},
- using the files in directory @var{appdir} to handle
- requests that start with the given @var{context-path}.
- The @var{context-path} must start with a @code{"/"} (one is added if
- needed), and it is recommended that it also end with a @code{"/"}
- (otherwise you might get some surprising behavior).
- You can specify multiple @code{--http-auto-handler} options.
- For example use the files in the current directory to handle
- all requests on the standard port 80 do:
- @example
- kawa --http-auto-handler / . --http-start 80
- @end example
- There are some examples in the @code{testsuite/webtest} directory
- the Kawa source distribution. You can start the server thus:
- @example
- bin/kawa --http-auto-handler / testsuite/webtest/ --http-start 8888
- @end example
- and then for example browse to @code{http://localhost:8888/adder.scm}.
- For lots of information about the HTTP request, browse to
- @code{http://localhost:8888/info/@var{anything}}.
- @subsection Using a servlet container
- You can also can use a ``servlet container''
- such as Tomcat or Glassfish with self-configuring script.
- See @ref{Servlets} for information on how to install
- these servers, and the concept of web applications.
- Once you have these server installed, you create a
- web application with the following in the
- @code{@var{appdir}/WEB-INF/web.xml} configuration file:
- @example
- <web-app>
- <display-name>Kawa auto-servlet</display-name>
- <servlet>
- <servlet-name>KawaPageServlet</servlet-name>
- <servlet-class>gnu.kawa.servlet.KawaPageServlet</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>KawaPageServlet</servlet-name>
- <url-pattern>/*</url-pattern>
- </servlet-mapping>
- </web-app>
- @end example
- This creates a web application where all URLs
- are handled by the @code{gnu.kawa.servlet.KawaPageServlet} servlet class,
- which is included in the Kawa jar file.
- The @code{KawaPageServlet} class handles the searching
- and compiling described in this page.
- @subsection Finding a matching script
- When Kawa receives a request for:
- @example
- http://host:port/appname/a/b/anything
- @end example
- it will look for a file:
- @example
- @var{appdir}/a/b/anything
- @end example
- If such a file exists, the script will be executed, as described
- below. If not, it will look for a file name @code{+default+} in the same
- directory. If that desn't exist either, it will look for @code{+default+}
- in the parent
- directory, then the grand-parent directory, and so on until it gets to
- the appname web application root directory. So the default script is
- this: @code{@var{appdir}/+default}.
- If that doesn't exist then Kawa returns a 404 "page not found" error.
- @subsection Determining script language
- Once Kawa has found a script file corresponding to a request URL,
- it needs to determine if this is a data file or a web page script,
- and in the latter case, what language it is written in.
- Kawa recognizes the following "magic strings" in the first line of a script:
- @table @code
- @item kawa:scheme
- The Scheme language.
- @item kawa:xquery
- The XQuery language.
- @item kawa:@var{language}
- Some other language known to Kawa.
- @end table
- Kawa also recognizes Emacs-style "mode specifiers":
- @table @code
- @item -*- scheme -*-
- The Scheme language.
- @item -*- xquery -*-
- The XQuery language (though Emacs doesn't know about XQuery).
- @item -*- emacs-lisp -*-
- @itemx -*- elisp -*-
- The Emacs Lisp extension language.
- @item -*- common-lisp -*-
- @itemx -*- lisp -*-
- The Common Lisp language.
- @end table
- Also, it also recognizes comments in the first two columns of the line:
- @table @code
- @item ;;
- A Scheme or Lisp comment - assumed to be in the Scheme language.
- @item (:
- Start of an XQuery comment, so assumed to be in the XQuery language.
- @end table
- If Kawa doesn't recognize the language of a script (and it
- isn't named +default+) then it assumes the file is a data file. It
- asks the servlet engine to figure out the content type (using the
- getMimeType method of ServletContext), and just copies the file into
- the response.
- @subsection Compilation and caching
- Kawa automatically compiles a script into a class. The
- class is internal to the server, and is not written out to
- disk. (There is an unsupported option to write the compiled file to a
- class file, but there is no support to use previously-compiled
- classes.) The server then creates a module instance to handle the
- actual request, and runs the body (the @code{run} method)
- of the script class. On subsequence
- requests for the same script, the same class and instance are reused;
- only the @code{run} is re-executed.
- If the script is changed, then it is re-compiled and a new module
- instance created. This makes it very easy to develop and modify a
- script. (Kawa for performance reasons doesn't check more
- than once a second whether a script has been modified.)
- @node Servlets, CGI scripts, Self-configuring page scripts, XML tools
- @section Installing web page scripts as Servlets
- You can compile a Kawa program to a @uref{http://en.wikipedia.org/wiki/Java_Servlet,Servlet}, and run it
- in a servlet engine (a Servlet-aware web server).
- One or more servlets are installed together as a web application.
- This section includes specific information for
- the Tomcat and Glassfish web servers.
- @subsection Creating a web application
- A @dfn{web application} is a group of data, servlets, and
- configuration files to handle a related set of URLs.
- The @uref{http://jcp.org/aboutJava/communityprocess/final/jsr315/index.html,
- servlet specification}
- specifies the directory structure of a web application.
- Assume the web application is called @code{myapp}, and lives in a
- directory with the same name. The application normally handles
- requests for URLs that start with @code{http://example.com/myapp}.
- Most files in the application directory are used to handle
- requests with corresponding URL. For example,
- a file @code{myapp/list/help.html} would be the response
- to the request @code{http://example.com/myapp/list/help.html}.
- The directory @code{WEB-INF} is special. It contains configuration
- files, library code, and other server data.
- So to create the @code{myapp} application, start with:
- @example
- mkdir myapp
- cd myapp
- mkdir WEB-INF WEB-INF/lib WEB-INF/classes
- @end example
- Copy the Kawa jar from the @code{lib} direcory.
- (You can also use a ``hard'' link, but symbolic links may not
- work, for security systems.)
- @example
- cp @var{kawa-home}/kawa-@value{VERSION}.jar WEB-INF/lib/kawa.jar
- @end example
- If you build Kawa from source, you must specify the
- @code{--with-servlet} @ref{configure options,configure option}.
- You should also create the file @code{WEB-INF/web.xml}.
- For now, this is is just a place-holder:
- @example
- <web-app>
- <display-name>My Application</display-name>
- </web-app>
- @end example
- @subsection Compiling a web page script to a servlet
- Assume for simplicity that the source files
- are in the @code{WEB-INF/classes} directory, and make that the
- current directory:
- @example
- cd .../myapp/WEB-INF/classes
- @end example
- Depending on the source language, you compile your script
- sing the @code{--servlet} switch:
- @example
- kawa --servlet -C hello.scm
- @end example
- or:
- @example
- kawa --servlet --krl -C hello.krl
- @end example
- or:
- @example
- kawa --servlet --xquery -C hello.xql
- @end example
- This lets the web-application find the compiled servlets.
- Finally, you just need to add the new servlet to
- the @code{WEB-INF/web.xml} file:
- @example
- <web-app>
- <display-name>My Application</display-name>
- <servlet>
- <servlet-name>MyHello</servlet-name>
- <servlet-class>hello</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>MyHello</servlet-name>
- <url-pattern>/hello</url-pattern>
- </servlet-mapping>
- </web-app>
- @end example
- The @code{<servlet>} clause says that the servlet named
- @code{MyHello} is implemented by the Java class @code{hello}.
- The @code{<servlet-mapping>} clause says that a request
- URL @code{/hello} should be handled by the servlet named @code{MyHello}.
- The URL is relative to the application context path,
- so the actual URL would be @code{http://example.com/myapp/hello}.
- @subsection Installing a servlet under Tomcat
- @cindex Tomcat
- Apache's @uref{http://tomcat.apache.org/,Tomcat} is an open-source
- implementation of the servlet specifications.
- After you @uref{http://tomcat.apache.org/download-60.cgi, download it},
- uncompress it in some convenient location,
- which is commonly referred to as @code{$CATALINA_HOME}.
- To install your web application, copy/move its directory
- to be in the @code{$CATALINA_HOME/webapps} directory.
- Thus for the example above you would have
- a @code{$CATALINA_HOME/webapps/myapp} directory.
- To start or stop Tomcat use the scripts in @code{$CATALINA_HOME/bin}.
- For example to start Tomcat on a GNU/Linux system run
- @code{$CATALINA_HOME/bin/startup.sh}. This will start a web server
- that listens on the default port of 8080,
- so you can browse the above example at @code{http://localhost:8080/myapp/hello}.
- If you're running Fedora GNU/Linux, you can use the @code{tomcat6} package:
- @example
- # yum install tomcat6
- # export CATALINA_HOME=/usr/share/tomcat6
- @end example
- You can the manage Tomcat like other system services.
- You can install webapps under @code{$CATALINA_HOME/webapps}.
- @subsection Installing a servlet under Glassfish
- @cindex Glassfish
- @uref{https://glassfish.dev.java.net/,Glassfish} from Oracle/Sun
- is a open-source ``application server'' that implements
- Java EE 6, including the 3.0 servlet specification.
- After you @uref{https://glassfish.dev.java.net/downloads/3.0.1-final.html, download it}, uncompress it in some convenient location.
- This location is called @var{as-install-parent} in the
- @uref{http://docs.sun.com/app/docs/doc/820-7689/aboaa?a=view,Quick Start Guide}.
- The commands you will use is most in @code{@var{as-install}/bin},
- where @var{as-install} is @code{@var{as-install}/glassfish}.
- To start the server, do:
- @example
- @var{as-install}/bin/startserv
- @end example
- or under under Windows:
- @example
- @var{as-install}\bin\startserv.bat
- @end example
- The default post to listen to is @code{8080};
- you can the port (and lots of other properties)
- using the adminstration console at port @code{4848}.
- A web application does not need to be any particular
- location, instead you just install it with this command:
- @example
- @var{as-install}/bin/adadmin deploy @var{appdir}
- @end example
- where @var{appdir} is the application directory - @code{myapp} in the example.
- (Use @code{asadmin.bat} under Windows.)
- @subsection Servlet-specific script functions
- The following functions only work within a servlet container.
- To use these functions, first do:
- @example
- (require 'servlets)
- @end example
- You can conditionalize your code to check at compile-time for servlets,
- like this:
- @example
- (cond-expand
- (in-servlet
- (require 'servlets)
- (format "[servlet-context: ~s]" (current-servlet-context)))
- (else
- "[Not in a servlet]"))
- @end example
- For a run-time check you can test if @code{(current-servlet)} is
- non-@code{#!null}.
- @deffn Procedure current-servlet
- When called from a Kawa servlet handler, returns the
- actual @code{javax.servlet.http.HttpServlet} instance.
- Returns @code{#!null} if the current context is not that of
- @code{KawaServlet}.
- (Hence this function also returns @code{#!null} if you compile a servlet
- ``by hand'' rather that using the @code{--servet} option.)
- @end deffn
- @deffn Procedure current-servlet-context
- Returns the context of the currently executing servlet,
- as an instance of @code{javax.servlet.ServletContext}.
- @end deffn
- @deffn Procedure current-servlet-config
- Returns the @code{ServletConfig} of the currently executing servlet.
- @end deffn
- @deffn Procedure get-request
- Return the current servlet request, as an instance of
- @code{javax.servlet.http.HttpServletRequest}.
- @end deffn
- @deffn Procedure get-response
- Return the current servlet response, as an instance of
- @code{javax.servlet.http.HttpServletResponse}.
- @end deffn
- @deffn Procedure request-servlet-path
- Get the servlet path of the current request.
- Similar to @code{request-script-path}, but not always the same,
- depending on configuration, and does @emph{not} end with a @code{"/"}.
- @end deffn
- @deffn Procedure request-path-info
- Get the path info of the current request.
- Corresponds to the CGI variable @code{PATH_INFO}.
- @end deffn
- @deffn Procedure servlet-context-realpath [path]
- Returns the file path of the current servlet's "Web application".
- @end deffn
- @node CGI scripts, HTTP requests, Servlets, XML tools
- @section Installing Kawa programs as CGI scripts
- The recommended way to have a web-server run a Kawa program
- as a CGI script is to compile the Kawa program to a servlet
- (as explained in @ref{Server-side scripts}, and then use
- Kawa's supplied CGI-to-servlet bridge.
- First, compile your program to one or more class files
- as explained in @ref{Server-side scripts}. For example:
- @example
- kawa --servlet --xquery -C hello.xql
- @end example
- Then copy the resulting @code{.class} files to your server's
- CGI directory. On Red Hat GNU/Linux, you can do the following (as root):
- @example
- cp hello*.class /var/www/cgi-bin/
- @end example
- Next find the @code{cgi-servlet} program that Kawa builds and installs.
- If you installed Kawa in the default place, it will be in
- @code{/usr/local/bin/cgi-servlet}.
- (You'll have this if you installed Kawa from source, but not
- if you're just using Kawa @code{.jar} file.)
- Copy this program into the same CGI directory:
- @example
- cp /usr/local/bin/cgi-servlet /var/www/cgi-bin/
- @end example
- You can link instead of copying:
- @example
- ln -s /usr/local/bin/cgi-servlet /var/www/cgi-bin/
- @end example
- However, because of security issues this may not work, so it is
- safer to copy the file. However, if you already have a copy
- of @code{cgi-servlet} in the CGI-directory, it is safe to make
- a hard link instead of making an extra copy.
- Make sure the files have the correct permissions:
- @example
- chmod a+r /var/www/cgi-bin/hello*.class /var/www/cgi-bin/hello
- chmod a+x /var/www/cgi-bin/hello
- @end example
- Now you should be able to run the Kawa program,
- using the URL @url{http://localhost/cgi-bin/hello}.
- It may take a few seconds to get the reply, mainly because of the
- start-up time of the Java VM. That is why servlets are
- preferred. Using the CGI interface can still be useful
- for testing or when you can't run servlets.
- @node HTTP requests, HTTP response, CGI scripts, XML tools
- @section Functions for accessing HTTP requests
- The following functions are useful for accessing
- properties of a HTTP request, in a Kawa program that is
- run either as a servlet or a CGI script. These functions
- can be used from plain Scheme, from KRL (whether
- in BRL-compatible mode or not), and from XQuery.
- The examples below assume the request @code{http://example.com:8080/myapp/foo/bar?val1=xyz&val2=abc}, where @code{myapp} is the application context.
- We also assume that this is handled by a script @code{foo/+default+}.
- The file @code{testsuite/webtest/info/+default+} in the Kawa source distribution
- calls most of these functions.
- You can try it as described in @ref{Self-configuring page scripts}.
- @subsection Request URL components
- @deffn Procedure request-URI
- Returns the URI of the request, as a value of type @code{URI}.
- This excludes the server specification,
- but includes the query string.
- (It is the combination of CGI variables @code{SCRIPT_NAME},
- @code{PATH_INFO}, and @code{QUERY_STRING}.
- Using servlets terminology, it is the combination of
- Context Path, Servlet Path, PathInfo, and Query String.)
- @example
- (request-URI) @result{} "/myapp/foo/bar?val1=xyz&val2=abc"
- @end example
- @end deffn
- @deffn Procedure request-path
- Returns the URI of the request, as a value of type @code{URI}.
- This excludes the server specification and the query string.
- Equivalent to @code{(path-file (request-URI))}.
- (It is the combination of CGI variables @code{SCRIPT_NAME}, and
- @code{PATH_INFO}.
- Same as the concatenation of @code{(request-context-path)},
- @code{(request-script-path)}, and @code{(request-local-path)}.
- Using servlets terminology, it is the combination of
- Context Path, Servlet Path, and PathInfo.)
- @example
- (request-path) @result{} "/myapp/foo/bar"
- @end example
- @end deffn
- @deffn Procedure request-uri
- This function is deprecated, because of possible confusion
- with @code{request-URI}. Use @code{request-path} instead.
- @end deffn
- @deffn Procedure request-url
- Returns the complete URL of the request, except the query string.
- The result is a @code{java.lang.StringBuffer}.
- @example
- (request-url) @result{} "http://example.com:8080/myapp/foo/bar"
- @end example
- @end deffn
- @deffn Procedure request-context-path
- Returns the context path, relative to the server root.
- This is an initial substring of the @code{(request-path)}.
- Similar to the Context Path of a servlet request,
- except that it ends with a @code{"/"}.
- @example
- (request-context-path) @result{} "/myapp/"
- @end example
- @end deffn
- @deffn Procedure request-script-path
- Returns the path of the script, relative to the context.
- This is either an empty string, or a string that ends with @code{"/"},
- but does not start with one. (The reason for this is to produce URIs
- that work better with operations like @code{resolve-uri}.)
- This is conceptually similar to @code{request-servlet-path},
- though not always the same, and the @code{"/"} conventions differ.
- @example
- (request-script-path) @result{} "foo/"
- @end example
- @end deffn
- @deffn Procedure request-local-path
- Returns the remainder of the @code{request-path},
- relative to the @code{request-script-path}.
- @example
- (request-local-path) @result{} "bar"
- @end example
- @end deffn
- @deffn Procedure request-query-string
- Returns the query string from an HTTP request. The query string is
- the part of the request URL after a question mark.
- Returns false if there was no query string.
- Corresponds to the CGI variable @code{QUERY_STRING}.
- @example
- (request-query-string) @result{} "val1=xyz&val2=abc"
- @end example
- @end deffn
- @subsection Request parameters
- Request parameters are used for data returned from forms,
- and for other uses.
- They may be encoded in the query string or in the request body.
- @deffn Procedure request-parameter name [default]
- If there is a parameter with the given name (a string),
- return the (first) corresponding value, as a string.
- Otherwise, return the @var{default} value,
- or @code{#!null} if there is no @var{default}.
- @example
- (request-parameter "val1") @result{} "xyz"
- (request-parameter "val9" "(missing)") @result{} "(missing)"
- @end example
- @end deffn
- @deffn Procedure request-parameters name
- If there is are one or more parameter with the given name (a string),
- return them all (as multiple values).
- Otherwise, return no values (i.e. @code{(values)}).
- @example
- (request-parameters "val1") @result{} "xyz"
- (request-parameters "val9") @result{} #!void
- @end example
- @end deffn
- @deffn Procedure request-parameter-map
- Request a map of all the parameters.
- This is a map from strings to a sequence of strings.
- (Specifically, a @code{java.util.Map<String,java.util.List<String>>}.)
- @end deffn
- @subsection Request headers
- The request headers are a set of (keyword, string)-pairs
- transmitted as part of the HTTP request, before the request body.
- @deffn Procedure request-header name
- If there is a header with the given @var{name} (a string),
- return the corresponding value string.
- Otherwise, return @code{#!null}.
- @example
- (request-header "accept-language") @result{} "en-us,en;q=0.5"
- @end example
- @end deffn
- @deffn Procedure request-header-map
- Request a map of all the headers.
- This is a map from strings to a sequence of strings.
- (Specifically, a @code{java.util.Map<String,java.util.List<String>>}.)
- @end deffn
- @subsection Request body
- @deffn Procedure request-input-port
- Return a textual input port for reading the request body,
- as a sequence of characters.
- @end deffn
- @deffn Procedure request-input-stream
- Return a binary input stream for reading the request body,
- as a sequence of bytes.
- @end deffn
- @deffn Procedure request-body-string
- Return the entire request body as a string
- @end deffn
- @subsection Request IP addresses and ports
- Information about the interface and port on which the request was received.
- @deffn Procedure request-local-socket-address
- The local address on which the request was received.
- This is the combination of @code{(request-local-host)}
- and @code{(request-local-port)}, as an instance of
- @code{java.net.InetSocketAddress}.
- @end deffn
- @deffn Procedure request-local-host
- Get the IP address of the interface on which request was received,
- as an @code{java.net.InetAddress}.
- @end deffn
- @deffn Procedure request-local-IP-address
- Get the IP address of the interface on which request was received,
- a string in numeric form:
- @example
- (request-local-host) @result{} "127.0.0.1"
- @end example
- @end deffn
- @deffn Procedure request-local-port
- Get the port this request was received on.
- @example
- (request-local-port) @result{} 8080
- @end example
- @end deffn
- Information about the interface and port of the remote client that invoked the request.
- @deffn Procedure request-remote-socket-address
- The address of the remote client (usually a web browser)
- which invoked the request.
- This is the combination of @code{(request-remove-host)}
- and @code{(request-remote-port)}, as an instance of
- @code{java.net.InetSocketAddress}.
- @end deffn
- @deffn Procedure request-remote-host
- Get the IP address of the remote client which invoked the request,
- as an @code{java.net.InetAddress}.
- @end deffn
- @deffn Procedure request-remote-IP-address
- Get the IP address of the remote client which invoked the request,
- as a string in numeric form.
- @example
- (request-remote-host) @result{} "123.45.6.7"
- @end example
- @end deffn
- @deffn Procedure request-remote-port
- The port used by the remote client.
- @end deffn
- @subsection Miscellaneous request properties
- @deffn Procedure request-path-translated
- Map the request-path to a file name (a string)
- in the server application directory.
- Corresponds to the CGI variable @code{PATH_TRANSLATED}.
- @end deffn
- @deffn Procedure request-method
- Returns the method of the HTTP request, usually @code{"GET"}
- or @code{"POST"}. Corresponds to the CGI variable @code{REQUEST_METHOD}.
- @end deffn
- @deffn Procedure request-scheme
- Returns the scheme (protocol) of the request.
- Usually @code{"http"}, or @code{"https"}.
- @end deffn
- @node HTTP response, XML beyond Scheme, HTTP requests, XML tools
- @section Generating HTTP responses
- The result of evaluating the top-level expressions of a web page script
- becomes the HTTP response that the servlet sends back to the browser.
- The result is typically an HTML/XML element code object
- Kawa will automatically format the result as appropriate for the type.
- Before the main part of the response there may be
- special "response header values",
- as created by the @code{response-header} function.
- Kawa will use the response header values to set various
- required and optional fields of the HTTP response.
- Note that @code{response-header} does not actually do anything
- until it is "printed" to the standard output.
- Note also that a @code{"Content-Type"} response value is
- special since it controls the formatting of the following
- non-response-header values.
- @deffn Procedure response-header key value
- Create the response header @samp{@var{key}: @var{value}} in the HTTP
- response. The result is a "response header value" (of some unspecified
- type). It does not directly set or print a response header, but only
- does so when you actually "print" its value to the response output stream.
- @end deffn
- @deffn Procedure response-content-type type
- Species the content-type of the result - for example @code{"text/plain"}.
- Convenience function for @code{(response-header "Content-Type" @var{type})}.
- @end deffn
- @deffn Procedure error-response code [message]
- Creates a response-header with an error code of @var{code} and a
- response message of @var{message}.
- (For now this is the same as @code{response-status}.)
- Note this also returns a response-header value, which does not actually
- do anything unless it is returned as the result of executing a servlet body.
- @end deffn
- @deffn Procedure response-status code [message]
- Creates a response-header with an status code of @var{code} and a
- response message of @var{message}.
- (For now this is the same as @code{error-response}.)
- @end deffn
- @node XML beyond Scheme, , HTTP response, XML tools
- @section Using non-Scheme languages for XML/HTML
- @subsection XQuery language
- Bundled with Kawa is a fairly complete implementation of W3C's
- new @uref{http://www.w3c.org/XML/Query,XML Query language}.
- If you start Kawa with the @code{--xquery} it selects the "XQuery"
- source language; this also prints output using XML syntax.
- See the @uref{http://www.gnu.org/software/qexo/,Qexo (Kawa-XQuery) home page}
- for examples and more information.
- @subsection XSL transformations
- There is an experimental implementation of the XSLT (XML Stylesheet
- Language Transformations) language. Selecting @code{--xslt} at the
- Kawa command line will parse a source file according to the syntax
- on an XSLT stylesheet.
- See the @uref{http://www.gnu.org/software/qexo/xslt.html,Kawa-XSLT page}
- for more information.
- @menu
- * KRL:: KRL - The Kawa Report Language for generating XML/HTML
- @end menu
- @node KRL, , , XML beyond Scheme
- @subsection KRL - The Kawa Report Language for generating XML/HTML
- KRL (the "Kawa Report Language") is powerful Kawa dialect for embedding
- Scheme code in text files such as HTML or XML templates. You select
- the KRL language by specifying @code{--krl} on the Kawa command line.
- KRL is based on on @uref{http://brl.sourceforge.net/,BRL},
- Bruce Lewis's "Beautiful Report Language", and
- uses some of BRL's code, but there are some experimental differences,
- and the implementation core is different. You can run KRL in
- BRL-compatility-mode by specifying @code{--brl} instead of @code{--krl}.
- @subsection Differences between KRL and BRL
- This section summarizes the known differences between KRL and BRL.
- Unless otherwise specified, KRL in BRL-compatibility mode will
- act as BRL.
- @itemize
- @item
- In BRL a normal Scheme string @code{"mystring"} is the same
- as the inverted quote string @code{]mystring[}, and both are instances
- of the type @code{<string>}.
- In KRL @code{"mystring"} is a normal Scheme string of type @code{<string>},
- but @code{]mystring[} is special type that suppresses output escaping.
- (It is equivalent to @code{(unescaped-data "mystring")}.)
- @item
- When BRL writes out a string, it does not do any processing
- to escape special characters like @code{<}. However, KRL in its default
- mode does normally escape characters and strings. Thus @code{"<a>"}
- is written as @code{<a&gr;}.
- You can stop it from doing this by overriding the output format, for example
- by specifying @code{--output-format scheme} on the Kawa command line,
- or by using the @code{unescaped-data} function.
- @item
- Various Scheme syntax forms, including @code{lambda},
- take a @stxref{body}, which is a list of one or more declarations and
- expressions. In normal Scheme and in BRL the value of a @var{body}
- is the value of the last expression. In KRL the value of a @var{body}
- is the concatenation of all the values of the expressions,
- as if using @code{values-append}.
- @item
- In BRL a word starting with a colon is a keyword.
- In KRL a word starting with a colon is an identifier, which by
- default is bound to the @code{make-element} function specialized
- to take the rest of the word as the tag name (first argument).
- @item
- BRL has an extensive utility library. Most of this has not yet been ported
- to KRL, even in BRL-compatibility mode.
- @end itemize
- @node Miscellaneous
- @chapter Miscellaneous topics
- @deffn Procedure scheme-implementation-version
- Returns the Kawa version number as a string.
- @c (Compatible with slib.)
- @end deffn
- @deffn Procedure scheme-window [shared]
- Create a read-eval-print-loop in a new top-level window.
- If @var{shared} is true, it uses the same environment as the
- current @code{(interaction-environment)}; if not (the default),
- a new top-level environment is created.
- You can create multiple top-level window that can co-exist.
- They run in separate threads.
- @end deffn
- @menu
- * Composable pictures::
- * Building JavaFX applications::
- * Building for Android:: Building for Android devices
- * Android view construction::
- * System inquiry::
- * Processes::
- * Time-related functions::
- * Low-level functions:: Deprecated low-level functions
- @end menu
- @node Composable pictures
- @section Composable pictures
- The @code{(kawa pictures)} library lets you create geometric shapes
- and images, and combine them in interesting ways.
- The @ref{Tutorial - Pictures,tutorial} gives an introduction.
- The easiest way to use and learn the @code{pictures} library
- is with a suitable REPL. You can use the old Swing-based console
- or any @ref{Using DomTerm,DomTerm}-based terminal emulator.
- You can create a suitable window either by starting @code{kawa}
- with the @code{-w} flag, or by running the @code{kawa} command
- inside an existing DomTerm-based terminal emulator.
- The screenshot below is of the latter,
- using the @code{qtdomterm} terminal emulator.
- @image{images/domterm-pictures-1}
- After @code{(import (kawa swing))} you can use @code{show-picture}
- to display a picture in a Swing window.
- A @dfn{picture} is an object that can be displayed on a screen,
- web-page, or printed page, and combined with other pictures.
- A picture has a method printing itself in a graphical context.
- It also has various properties.
- An important property of a picture is its @dfn{bounding box}.
- This is a rectangle (with edges parallel to the axes) that
- surrounds the contents of the picture.
- Usually the entire visible part of the picture is inside the
- bounding box, but in some cases part of the picture may stick
- outside the bounding box.
- For example when a circle is drawn (stroked) with a ``pen'',
- the bounding box is that of the infinitely-thin mathematical circle, so
- ``ink'' from the pen that is outside the circle may be outside the bounding box.
- A picture has an origin point corresponding to the (0 0) cordinates.
- The origin is commonly but not always inside the bounding box.
- Certain operations (for example @code{hbox}) combine pictures by putting
- them ``next to'' each other, where ``next to'' is defined in
- terms of the bounding box and origin point.
- @c The Racket language has a @uref{https://docs.racket-lang.org/pict/,Pict}
- @c library, with a lot more functionality;
- @c it is used by the @uref{https://docs.racket-lang.org/quick/,Racket tutorial}.
- @c Racket also has a @uref{https://docs.racket-lang.org/teachpack/2htdpimage-guide.html,@code{2htdp/image} library} which should also be studied.
- @subsection Coordinates - points and dimensions
- The library works with a two-dimensional grid,
- where each position has an x cordinate and y coordinate.
- Normally, x values increase as you move right on the screen/page,
- while y values increase as you move @emph{down}.
- This convention matches that used by Java 2D, SVG, and many other
- graphics libraries. However, note that this is different from the traditional
- mathematical convention of y values increasing as you go up.
- By default, one unit is one ``pixel''. (More precisely,
- it is the @code{px} unit in the CSS specification.)
- @deffn Type Point
- A point is a pair consisting of an x and a y coordinate.
- @end deffn
- @deftypefn Literal {} &P@stxlit{[} @var{x} @var{y} @stxlit{]}
- Construct a point value with the specified @var{x} and @var{y} values.
- Both @var{x} and @var{y} are expressions that evaluate to real numbers:
- @example
- &P[(+ old-right 10) baseline]
- @end example
- @end deftypefn
- @deffn Type Dimension
- A dimension value is a pair of a width and a height.
- It is used for the size of pictures in the two dimensions.
- In a context that expects a point, a dimension is treated
- as an offset relative to some other point.
- @end deffn
- @deftypefn Literal {} &D@stxlit{[} @var{width} @var{height} @stxlit{]}
- Construct a dimension value with the specified @var{width}
- and @var{height} values, which are both expressions that
- evaluate to real numbers.
- @end deftypefn
- @subsection Shapes
- A shape is a collection of lines and curves.
- Examples include lines, circles, and polygons.
- A shape can be @dfn{stroked}, which you can think of as
- being drawn by a very fancy calligraphic pen that
- follows the lines and curves of the shape.
- A @dfn{closed shape} is a shape that is continuous and ends up where it
- started. This includes circles and polygons.
- A closed shape can be filled, which means the entire ``interior''
- is painted with some color or texture.
- A shape is represented by the Java @code{java.awt.Shape} interface.
- The @code{picture} library only provides relatively simple shapes,
- but you can use any methods that create a @code{java.awt.Shape} object.
- @dfn{Shape} is effectively a sub-type of @dfn{picture},
- though they're represented using using disjoint classes:
- If you use a shape where a picture is required,
- the shape is automatically converted to a picture,
- as if using the @code{draw} procedure.
- @deffn Procedure line p1 [p2 ...]
- In the simple case two points are specified, and the result
- is a line that goes from point @var{p1} to @var{p2}.
- If @var{n} points are specied, the result is a @dfn{polyline}:
- a path consisting of @var{n-1} line segments, connecting adjacent arguments.
- (If only a single point is specified, the result is degenerate
- single-point shape.)
- All of the points except the first can optionally be specified
- using a dimension, which is treated an an offset from the previous point:
- @example
- (line &P[30 40] &D[10 5] &D[10 -10])
- @end example
- is the same as:
- @example
- (line &P[30 40] &P[40 45] &P[50 35])
- @end example
- @end deffn
- @deffn Procedure polygon p1 [p2 ...]
- Constructs a closed shape from line segments.
- This is the same as calling @code{line} with the same arguments,
- with the addition of a final line segment from the last point
- back to the first point.
- @end deffn
- @deffn Procedure rectangle p1 [p2]
- A rectangle is closed polygon of 4 line segments that are
- alternatively parallel to the x-axis and the y-axis.
- I.e. if you rotate a rectangle it is no longer a rectangle by
- this definition, though of course it still has a rectangular shape.
- If @var{p2} is not specified, constructs a rectangle whose upper-left corner
- is @code{&P[0 0]} and whose lower-right corner is @var{p1}.
- If @var{p2} is specified, constructs a rectangle whose upper-left corner
- is @var{p1} and whose lower-right corner is @var{p2}.
- If @var{p2} is a dimension it is considered a relative offset from @var{p1},
- just like for @code{polygon}.
- @end deffn
- @deffn Procedure circle radius [center]
- Creates a circle with the specified @var{radius}.
- If the @var{center} is not specified, it defaults to @code{&P[0 0]}.
- @end deffn
- @subsection Colors and paints
- A @dfn{paint} is a color pattern used to fill part of the canvas.
- A paint can be a color, a texture (a replicated pattern), or a gradient
- (a color that gradually fades to some other color).
- A @dfn{color} is defined by red, green, and blue values.
- It may also have an alpha component, which specifies transparency.
- @deffn Procedure ->paint value
- Converts @var{value} to a color - or more general a paint.
- Specificlly, the return type is @code{java.awt.Paint}.
- The @var{value} can be any one of:
- @itemize
- @item
- A @code{java.awt.Paint}, commonly a @code{java.awt.Color}.
- @item
- A 24-bit integer value is converted to a color.
- Assume the integer is equal
- to a hexadecimal literal of the form @code{#xRRGGBB}.
- Then @code{RR} (bits 16-23) is the intensity of the red component;
- @code{GG} (bits 8-15) is the intensity of the green component; and
- @code{RR} (bits 0-7) is the intensity of the red component.
- @item
- One of the standard HTML/CSS/SVG color names, as a string or symbol.
- See the table in @code{gnu/kawa/models/StandardColor.java} source file.
- Case is ignored, and you can optionally use hyphens to separate words.
- For example @code{'hot-pink}, @code{'hotpink}, and @code{'hotPink}
- are all the same sRGB color @code{#xFF69B4}.
- @end itemize
- @end deffn
- @deffn Procedure with-paint paint picture
- Create a new picture that is the ``same'' as @var{picture}
- but use @var{paint} as the default paint.
- For @var{paint} you can use any valid argument to @code{->paint}.
- The default paint (which is the color black if none is specified)
- is used for both @code{fill} (paint interior) and @code{draw} (stroke outline).
- @example
- #|kawa:1|# @kbd{(! circ1 (circle 20 &P[20 20]))}
- #|kawa:2|# @kbd{(hbox (fill circ1) (draw circ1))}
- @image{images/paint-circ-1}
- #|kawa:3|# @kbd{(with-paint 'hot-pink (hbox (fill circ1) (draw circ1)))}
- @image{images/paint-circ-2}
- @end example
- Above we use @code{with-paint} to create a cloned picture, which is
- the same as the original @code{hbox}, except for the default paint,
- in this case the color @code{hot-pink}.
- @example
- #|kawa:4|# @kbd{(! circ2 (hbox (fill circ1) (with-paint 'hot-pink (fill circ1))))}
- #|kawa:5|# @kbd{circ2}
- @image{images/paint-circ-3}
- #|kawa:6|# @kbd{(with-paint 'lime-green circ2)}
- @image{images/paint-circ-4}
- @end example
- Here @code{circ2} is an @code{hbox} of two filled circles,
- one that has unspecified paint, and one that is @code{hot-pink}.
- Printing @code{circ2} directly uses black for the
- circle with unspecified color,
- but if we wrap @code{circ2} in another @code{with-paint}
- that provides a default that is used for the first circle.
- @end deffn
- @subsection Filling a shape with a color
- @deffn Procedure fill shape
- @deffnx Procedure fill paint shape
- Fill the ``inside'' of @var{shape}.
- If no @var{paint} is specified, uses the current default paint.
- Otherwise, @code{(fill @var{paint} @var{shape})}
- is the same @code{(with-paint @var{paint} (fill @var{shape}))}.
- @end deffn
- @subsection Stroking (outlining) a shape
- @deffn Procedure draw @arbno{option} @atleastone{shape}
- Returns a picture that draws the outline of the @var{shape}.
- This is called @dfn{stroking}.
- An @var{option} may be one of:
- @itemize
- @item
- A @code{Paint} or @code{Color} object, which is used to draw the shape.
- @item
- A standard color name, such as @code{'red} or @code{'medium-slate-blue}.
- This is mapped to a @code{Color}.
- @item
- A join-specifier, which is a symbol specifying how each curve of the shape is
- connected to the next one.
- The options are @code{'miter-join}, @code{'round-join}, and @code{'bevel-join}.
- The default if none is specified is @code{'miter-join}.
- @item
- A end-cap-specifier, which is a symbol specifying how each end of the
- shape is terminated.
- The options are @code{'square-cap}, @code{'round-cap}, or @code{'butt-cap}.
- The default is @code{'butt-cap}.
- (This follows SVG and HTML Canvas.
- The default in plain Java AWT is a square cap.)
- @item
- A real number specifies the thickness of the stroke.
- @item
- A @code{java.awt.Stroke} object.
- This combines join-specifier, end-cap-specifier, thickness, and more
- in a single object. The @code{BasicStroke} class can specify dashes,
- though that is not yet supported for SVG output; only AWT or image output.
- @end itemize
- Let us illustrate with a sample line @code{lin}
- and a helper macro @code{show-draw}, which adds a border around a shape,
- then draws the given shape with some options, and finally
- re-draws the shape in plain form.
- @example
- #|kawa:10|# @kbd{(define lin (line &P[0 0] &P[300 40] &P[200 100] &P[50 70]))}
- #|kawa:11|# @kbd{(define-syntax show-draw}
- #|....:12|# @kbd{ (syntax-rules ()}
- #|....:13|# @kbd{ ((_ options ... shape)}
- #|....:14|# @kbd{ (border 12 'bisque (zbox (draw options ... shape) shape)))))}
- #|....:15|# @kbd{(show-draw 8 'lime lin)}
- @image{images/show-draw-1}
- #|....:16|# @kbd{(show-draw 8 'lime 'round-cap 'round-join lin)}
- @image{images/show-draw-2}
- #|....:17|# @kbd{(show-draw 8 'lime 'square-cap 'bevel-join lin)}
- @image{images/show-draw-3}
- @end example
- @end deffn
- Notice how the different cap and join styles change the drawing.
- Also note how the stroke (color lime) extends beyond its bounding box,
- into the surrounding border (color bisque).
- @subsection Affine transforms
- A 2D affine transform is a linear mapping from coordinates to
- coordinates. It generalizes translation, scaling, flipping, shearing,
- and their composition. An affine transform maps straight parallel lines
- into other straight parallel lines, so it is only a subset of possible
- mappings - but a very useful subset.
- @deffn Procedure affine-transform xx xy yx yy x0 y0
- @deffnx Procedure affine-transform px py p0
- Creates a new affine transform.
- The result of applying @code{(affine-transform @var{x@sub{x}} @var{x@sub{y}} @var{y@sub{x}} @var{y@sub{y}} @var{x@sub{0}} @var{y@sub{0}})}
- to the point @code{&P[@var{x} @var{y}]}
- is the transformed point
- @example
- &P[(+ (* @var{x} @var{x@sub{x}}) (* @var{y} @var{y@sub{x}}) @var{x@sub{0}})
- (+ (* @var{x} @var{x@sub{y}}) (* @var{y} @var{y@sub{y}}) @var{y@sub{0}})]
- @end example
- If using point arguments,
- @code{(affine-transform &P[@var{x@sub{x}} @var{x@sub{y}}] &P[@var{y@sub{x}} @var{y@sub{y}}] &P[@var{x@sub{0}} @var{y@sub{0}}])}
- is equivalent to:
- @code{(affine-transform @var{x@sub{x}} @var{x@sub{y}} @var{y@sub{x}} @var{y@sub{y}} @var{x@sub{0}} @var{y@sub{0}})}.
- @end deffn
- @deffn Procedure with-transform transform picture
- @deffnx Procedure with-transform transform shape
- Creates a transformed picture.
- If the argument is a @var{shape}, then the result is also a shape.
- @end deffn
- @deffn Procedure with-transform transform point
- Apply a transform to a single point, yielding a new point.
- @end deffn
- @deffn Procedure with-transform transform1 transform2
- Combine two transforms, yielding the composed transform.
- @end deffn
- @deffn Procedure rotate angle
- @deffnx Procedure rotate angle picture
- The one-argument variant creates a new affine transform that rotates
- a picture about the origin by the specified @var{angle}.
- A positive @var{angle} yields a clockwise rotation.
- The @var{angle} can be either a quantity (with a unit of
- either @code{rad} radians, @code{deg} (degrees), or @code{grad} (gradians)),
- or it can be a unit-less real number (which is treated as degrees).
- The two-argument variant applies the resulting transform to
- the specified picture. It is equivalent to:
- @example
- (with-transform (rotate @var{angle}) @var{picture})
- @end example
- @end deffn
- @deffn Procedure scale factor
- @deffnx Procedure scale factor picture
- Scales the @var{picture} by the given @var{factor}.
- The @var{factor} can be a real number.
- The @var{factor} can also be a point or a dimension, in which case
- the two cordinates are scaled by a different amount.
- The two-argument variant applies the resulting transform to
- the specified picture. It is equivalent to:
- @example
- (with-transform (scale @var{factor}) @var{picture})
- @end example
- @end deffn
- @deffn Procedure translate offset
- @deffnx Procedure translate offset picture
- The single-argument variant creates a transform
- that adds the @var{offset} to each point.
- The @var{offset} can be either a point or a dimension (which
- are treated quivalently).
- The two-argument variant applies the resulting transform to
- the specified picture. It is equivalent to:
- @example
- (with-transform (translate @var{offset}) @var{picture})
- @end example
- @end deffn
- @subsection Combining pictures
- @deffn Procedure hbox [spacing] picture ...
- @deffnx Procedure vbox [spacing] picture ...
- @deffnx Procedure zbox picture ...
- Make a combined picture from multiple sub-pictures drawn
- either ``next to'' or ``on top of'' each other.
- The case of @code{zbox} is simplest: The sub-pictures are drawn
- in argument order at the same position (origin). The ``@code{z}'' refers to
- the idea that the pictures are stacked on top of each other along
- the ``Z-axis'' (the one perpendicular to the screen).
- The @code{hbox} and @code{vbox} instead place the sub-pictures
- next to each other, in a row or column.
- If @var{spacing} is specified, if must be a real number.
- That much extra spacing is added between each sub-picture.
- More precisely: @code{hbox} shifts each sub-picture except the first
- so its left-origin control-point
- (see discussion at @code{re-center}) has the same position
- as the right-origin control point of the previous picture
- @var{plus} the amount of @var{spacing}.
- Similarly, @code{vbox} shifts each sub-picture except the first
- so its top-origin control point has the same position
- as the bottom-origin point of the previous picture, plus @var{spacing}.
- The bounding box of the result is the smallest rectangle that
- includes the bounding boxes of the (shifted) sub-pictures.
- The origin of the result is that of the first picture.
- @end deffn
- @deffn Procedure border [size [paint]] picture
- Return a picture that combines @var{picture}
- with a rectangular border (frame) around @var{picture}'s bounding box.
- The @var{size} specifies the thickness of the border:
- it can be real number, in which it is the thickness on all four sides;
- it can be a Dimension, in which case the width is the left and right
- thickness, while the height is the top and bottom thickness;
- or it can be a Rectangle, in which case it is the new bounding box.
- If @var{paint} is specified it is used for the border;
- otherwise the default paint is used.
- The border is painted before (below) the @var{picture} painted.
- The bounding box of the result is that of the border, while
- the origin point is that of the original @var{picture}.
- @example
- #|kawa:2|# @kbd{(with-paint 'blue (border &D[8 5] (fill 'pink (circle 30))))}
- @image{images/border-1}
- @end example
- @end deffn
- @deffn Procedure padding width [background] picture
- This is similar to @code{border},
- but it just adds extra space around @var{picture},
- without painting it. The @var{size} is specified the same way.
- If @var{background} is specified,
- it becomes the background paint for the entire padded
- rectangle (both @var{picture} and the extra padding).
- @example
- #|kawa:3|# @kbd{(define circ1 (fill 'teal (circle 25)))}
- #|kawa:4|# @kbd{(zbox (line &P[-30 20] &P[150 20])}
- #|kawa:5|# @kbd{ (hbox circ1 (padding 6 'yellow circ1) (padding 6 circ1)))}
- @image{images/padding-1}
- @end example
- This shows a circle drawn three ways:
- as-is; with padding and a background color;
- with padding and a transparent background.
- A line is drawn before (below) the circles to contrast
- the yellow vs transparent backgrounds.
- @end deffn
- @deffn Procedure re-center [xpos] [ypos] picture
- Translate the @var{picture} such that the point specified by @var{xpos}
- and @var{ypos} is the new origin point, adjusting the bounding box to match.
- If the @var{picture} is a shape, so is the result.
- The @var{xpos} can have four possible values, all of which are symbols:
- @code{'left} (move the origin to the left edge of the bounding box);
- @code{'right} (move the origin to the right edge of the bounding box);
- @code{'center} (or @code{'centre}) (move the origin to halfway between the left and right edges);
- or @code{'origin} (don't change the location along the x-axis).
- The @var{ypos} can likewise have four possible values:
- @code{'top} (move the origin to the top edge of the bounding box);
- @code{'bottom} (move the origin to the bottom edge of the bounding box);
- @code{'center} (or @code{'centre}) (move the origin to halfway between the
- top and bottom edges);
- or @code{'origin} (don't change the location along the y-axis).
- A single @code{'center} argument
- is the same as specifying @code{'center} for both axis; this is the default.
- A single @code{'origin} argument
- is the same as specifying @code{'origin} for both axis;
- this is the same as just @var{picture}.
- The 16 control points are shown below, relative to a picture's
- bounding box and the X- and Y-axes.
- The abbreviations have the obvious values, for example
- @code{LC} means @code{'left 'center}.
- @example
- LT OT CT RT
- ┌────┬──────────┐
- │ │ │
- │ │ │
- LC│ OC C │RC
- LO├────O──CO──────┤RO
- │ │ │
- └────┴──────────┘
- LB OB CB RB
- @end example
- The result of (for example) @code{(re-center 'left 'center @var{P})}
- is @var{P} translated so the origin is at control point @code{LC}.
- @example
- #|kawa:1|# @kbd{(define D (fill 'light-steel-blue (polygon &P[-20 0] &P[0 -20] &P[60 0] &P[0 40])))}
- #|kawa:2|# @kbd{(zbox D (draw 'red (circle 5)))}
- @image{images/re-center-1}
- @end example
- Above we defined @code{D} as a vaguely diamond-shaped quadrilateral.
- A small red circle is added to show the origin point.
- Below we display 5 versions of @code{D} in a line (an @code{hbox}),
- starting with the original @code{D} and 4 calls to @code{re-center}.
- @example
- #|kawa:3|# @kbd{(zbox (hbox D (re-center 'top D) (re-center 'bottom D)}
- #|....:4|# @kbd{ (re-center 'center D) (re-center 'origin D))}
- #|....:5|# @kbd{ (line &P[0 0] &P[300 0]))}
- @image{images/re-center-2}
- @end example
- The line at @var{y=0} shows the effects of @code{re-center}.
- @end deffn
- @subsection Images
- An image is a picture represented as a rectangular grid of color values.
- An image file is some encoding (usually compressed) of an image,
- and mostly commonly has the extensions @code{png}, @code{gif},
- or @code{jpg}/@code{jpeg}.
- A ``native image'' is an instance of @code{java.awt.image.BufferedImage},
- while a ``picture image'' is an instance of @code{gnu.kawa.models.DrawImage}.
- (Both classes implement the @code{java.awt.image.RenderedImage} interface.)
- A @code{BufferedImage} is automatically converted to a @code{DrawImage}
- when needed.
- @deffn Procedure image bimage
- @deffnx Procedure image picture
- @deffnx Procedure image src: path
- Creates a picture image, using either an existing native image @var{bimage},
- or an image file specified by @var{path}.
- Writing @code{(image src: @var{path})} is roughly the same
- as @code{(image (read-image @var{path}))} except that the former
- has the @var{path} associated with the resulting picture image.
- This can make a difference when the image is used or displayed.
- If the argument is a @var{picture}, it is converted to an image
- as if by @code{->image}.
- @end deffn
- @deffn Procedure image-read path
- Read an image file from the specified @var{path},
- and returns a native image object (a @code{BufferedImage}).
- @example
- #|kawa:10|# @kbd{(define img1 (image-read "http://pics.bothner.com/2013/Cats/06t.jpg"))}
- #|kawa:11|# @kbd{img1}
- @image{images/image-cat-1a}
- #|kawa:12|# @kbd{(scale 0.6 (rotate 30 img1))}
- @image{images/image-cat-1b}
- @end example
- @end deffn
- Note that while @code{img1} above is a (native) image,
- the scaled rotated image is not an image object.
- It is a picture - a more complex value that @emph{contains} an image.
- @deffn Procedure image-write picture path
- The @var{picture} is converted to an image
- (as if by using @code{->image}) and then it is written
- to the specified @var{path}.
- The format used depends on (the lower-cased string value of) the path:
- A JPG file if the name ends with @code{".jpg"} or @code{".jpeg"};
- a GIF file if the name ends with @code{".gif"};
- a PNG file if the name ends with @code{".png"}.
- (Otherwise, the defalt is PNG, but that might change.)
- @end deffn
- @deffn Procedure image-width image
- @deffnx Procedure image-height image
- Return the width or height of the given @var{image}, in pixels.
- @end deffn
- @deffn Procedure ->image picture
- Convert @var{picture} to an image (a @code{RenderedImage}).
- If the @var{picture} is an image, return as-is.
- Otherwise, create an empty image (a @code{BufferedImage} whose size is the
- @var{picture}'s bounding box), and ``paint'' the @var{picture} into it.
- @example
- #|kawa:1|# @kbd{(define c (fill (circle 10)))}
- #|kawa:2|# @kbd{(scale 3 (hbox c (->image c)))}
- @image{images/image-scaled-1}
- @end example
- Here we take a circle @code{c}, and convert it to an image.
- Note how when the image is scaled, the pixel artifacts are very noticable.
- Also note how the origin of the image is the top-level corner,
- while the origin of the original circle is its center.
- @end deffn
- @subsection Compositing - Controlling how pictures are combined
- @deffn Procedure with-composite [[compose-op] picture] ...
- Limited support - SVG and DomTerm output has not been implemented.
- @end deffn
- @subsection Displaying and exporting pictures
- @subsubsection Export to SVG
- @deffn Procedure picture-write-svg picture path [headers]
- Writes the @var{picture} to the file specified by @var{path},
- in SVG (Structered Vector Graphics) format.
- If @var{headers} is true (which is the default)
- first write out the XML and DOCTYPE declarations
- that should be in a well-formed standaline SVG file.
- Otherwise, just write of the @code{<svg>} element.
- (Modern browers should be able to display a file
- consisting of just the @code{<svg>} element,
- as long as it has extension @code{.svg}, @code{.xml}, or @code{.html};
- the latter may add some extra padding.)
- @end deffn
- @deffn Procedure picture->svg-node picture
- Returns a SVG representation of @code{picture},
- in the form of an @code{<svg>} element,
- similar to those discussed in @ref{Creating XML nodes}.
- If you convert the @code{<svg>} element to a string,
- you get formatted XML;
- if you @code{write} the @code{<svg>} element
- you get an @ref{XML literals,XML literal} of form @code{"#<svg>...</svg>"}.
- If you @code{display} the @code{<svg>} element
- in a DomTerm terminal you get the picture (as a picture).
- This works because when you display an element in DomTerm
- it gets inserted into the display.
- @end deffn
- @subsubsection Display in Swing
- These procedures require @code{(import (kawa swing))} in addition to
- @code{(import (kawa pictures))}.
- The convenience function @code{show-picture} is useful
- for displaying a picture in a new (Swing) window.
- @deffn Procedure show-picture picture
- If this is the first call to @code{show-pictures},
- displays @var{picture} in a new top-level window (using the Swing toolkit).
- Sequenent calls to @code{show-picture} will reuse the window.
- @example
- #|kawa:1|# @kbd{(import (kawa swing) (kawa pictures))}
- #|kawa:2|# @kbd{(show-picture @var{some-picture})}
- #|kawa:3|# @kbd{(set-frame-size! &D[200 200])} ; Adjust window size
- #|kawa:4|# @kbd{(show-picture @var{some-other-picture})}
- @end example
- @end deffn
- @deffn Procedure picture->jpanel picture
- Return a @code{JPanel} that displays @var{picture}.
- You can change the displayed picture by:
- @example
- (set! @var{panel}:picture @var{some-other-picture})
- @end example
- @end deffn
- @deffn Procedure set-frame-size! size [frame]
- If @var{frame} is specified, set its size.
- Otherwise, remember the size for future @code{show-picture} calls;
- if there is already a @code{show-picture} window, adjust its size.
- @end deffn
- @subsubsection Convert to image
- You can convert a picture to an image using the @code{->image} procedure,
- or write it to a file using the @code{image-write} procedure.
- @node Building JavaFX applications
- @section Building JavaFX applications
- @cindex JavaFX
- Kawa makes it easy to build ``rich client'' (i.e. GUI) applications using
- @uref{http://www.oracle.com/technetwork/java/javafx/overview/index.html,JavaFX}.
- For example the following program will print up a window with a button;
- clicking on the button will print a message on the console output about the
- event.
- @example
- (require 'javafx-defs)
- (javafx-application)
- (javafx-scene
- title: "Hello Button"
- width: 600 height: 450
- (Button
- text: "Click Me"
- layout-x: 25
- layout-y: 40
- on-action: (lambda (e) (format #t "Event: ~s~%~!" e))))
- @end example
- JavaFX support is builtin to the pre-built @code{kawa-@value{VERSION}.jar}.
- It is easiest to use JDK 8; see below if you're using JDK 7.
- If you build Kawa from source, specify @code{--with-javafx} on the
- @code{configure} command line (assuming you're using JDK 8).
- Assume the above file is @code{HelloButton1.scm}, you can
- run it like this:
- @example
- $ kawa HelloButton1.scm
- @end example
- For more information and examples read this (slightly older)
- @uref{http://per.bothner.com/blog/2011/JavaFX-using-Kawa-intro/,introduction},
- and
- @uref{http://localhost/per/blog/2011/JavaFX-using-Kawa-animation/,this on animation}.
- The @code{browse-kawa-manual} script in the @code{doc} directory (source only)
- uses JavaFX WebView to create a window for browsing the Kawa documentation.
- @subsection Using JavaFX with JDK 7
- JDK 8 ships with JavaFX, and it is in the default @code{CLASSPATH}.
- JDK 7 update 9 or later does have JavaFX included, but it is a separate
- @code{jfxrt.jar} which not in the default @code{CLASSPATH}.
- Thus you have to explicitly add @code{jfxrt.jar}.
- To run the previous @code{HelloButton1.scm} you can do:
- @example
- java -cp $JAVA_HOME/lib/jfxrt.jar:$KAWA_HOME/kawa.jar HelloButton1.scm
- @end example
- If you build Kawa from source, do:
- @example
- $ ./configure --with-javafx=$JAVA_HOME --enable-kawa-frontend ...other-args...
- @end example
- The resulting Kawa binary sets up the path to @code{jfxrt.jar} so you just need to do:
- @example
- $ kawa HelloButton1.scm
- @end example
- @node Building for Android
- @section Building for Android
- Google's phone/tablet operating system
- @uref{https://developers.google.com/android/,Android}
- is based on a custom virtual machine on top of a Linux kernel.
- Even though Android isn't strictly (or legally) speaking Java,
- you can build Android applications using Kawa.
- Below is "Hello world" written in Kawa Scheme. A slightly
- more interesting example is in @ref{Android view construction,next section}.
- @example
- (require 'android-defs)
- (activity hello
- (on-create-view
- (android.widget.TextView (this)
- text: "Hello, Android from Kawa Scheme!")))
- @end example
- The following instructions have been tested on GNU/Linux,
- specifically Fedora 17.
- @uref{http://asieno.com/blog/index.php/post/2012/08/16/Setting-up-the-environment-Android-Kawa,This link} may be helpful if you're building on Windows.
- @subsection Downloading and setting up the Android SDK
- First @uref{http://code.google.com/android/download.html,download the Android SDK}. Unzip in a suitable location, which we'll refer to as @code{ANDROID_HOME}.
- @example
- export ANDROID_HOME=/path/to/android-sdk-linux
- PATH=$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools:$PATH
- @end example
- Next you have to get the appropriate platform SDK:
- @example
- $ android update sdk
- @end example
- You need to select an Android ``platform''.
- Platform (API) 16 corresponds to Android 4.1.2 (Jelly Bean).
- Select that or whatever you prefer, and click @code{Install}.
- (You can install multiple platforms, but each project
- is built for a specific platform.)
- @example
- ANDROID_PLATFORM=android-16
- @end example
- @subsection Building Kawa for Android
- Set @code{JAVA_HOME} to where your JDK tree is.
- You should use JDK 6; JDK 7 does not work at time of writing.
- @example
- $ export JAVA_HOME=/opt/jdk1.6
- @end example
- First @ref{Getting Kawa,get the Kawa source code}.
- If using Ant (as is recommended on Windows):
- @example
- $ ant -Denable-android=true
- @end example
- Alternatively, you can use @code{configure} and @code{make}:
- @example
- $ KAWA_DIR=path_to_Kawa_sources
- $ cd $KAWA_DIR
- $ ./configure --with-android=$ANDROID_HOME/platforms/$ANDROID_PLATFORM/android.jar --disable-xquery --disable-jemacs
- $ make
- @end example
- @subsection Creating the application
- Next, we need to create a project or ``activity''.
- This tutorial assumes you want to create the project
- in the target directory @code{KawaHello},
- with the main activity being a class named @code{hello} in a
- package @code{kawa.android}:
- @example
- PROJECT_DIR=KawaHello
- PROJECT_CLASS=hello
- PROJECT_PACKAGE=kawa.android
- PROJECT_PACKAGE_PATH=kawa/android
- @end example
- To create the project use the following command:
- @example
- $ android create project --target $ANDROID_PLATFORM --name $PROJECT_DIR --activity $PROJECT_CLASS --path ./$PROJECT_DIR --package $PROJECT_PACKAGE
- @end example
- Replace the skeleton @code{hello.java} by the Scheme code at the
- top of this note, placing in a file named @code{hello.scm}:
- @example
- $ cd $PROJECT_DIR
- $ HELLO_APP_DIR=`pwd`
- $ cd $HELLO_APP_DIR/src/$PROJECT_PACKAGE_PATH
- $ rm $PROJECT_CLASS.java
- $ @i{create} $PROJECT_CLASS.scm
- @end example
- We need to copy/link the Kawa jar file so the Android SDK can find it:
- @example
- $ cd $HELLO_APP_DIR
- $ ln -s $KAWA_DIR/kawa-@value{VERSION}.jar libs/kawa.jar
- @end example
- Optionally, you can use kawart-@value{VERSION}.jar, which is slightly smaller,
- but does not support eval, and does not get built by the Ant build:
- @example
- $ ln -s $KAWA_DIR/kawart-@value{VERSION}.jar libs/kawa.jar
- @end example
- Copy or link @code{custom_rules.xml} from the Kawa sources:
- @example
- ln -s $KAWA_DIR/gnu/kawa/android/custom_rules.xml .
- @end example
- Finally to build the application just do:
- @example
- $ ant debug
- @end example
- @subsection Running the application on the Android emulator
- First you need to create an @uref{http://developer.android.com/tools/devices,Android Virtual Device (avd)}. Start:
- @example
- android
- @end example
- Then from menu @code{Tools} select @code{Manage AVDs...}.
- In the new window click @code{New....}
- Pick a @code{Name} (we use @code{avd16} in the following),
- a @code{Target} (to match @code{$ANDROID_PLATFORM}),
- and optionally change the other properties, before clicking @code{Create AVD}.
- Now you can start up the Android emulator:
- @example
- $ emulator -avd avd16 &
- @end example
- Wait until Android has finished booting (you will see the Android home screen),
- click the menu and home buttons. Now install our new application:
- @example
- adb install bin/KawaHello-debug.apk
- @end example
- @subsection Running the application on your device
- If the emulator is running, kill it:
- @example
- $ kill %emulator
- @end example
- On your phone or other Android devude, enable USB debugging.
- (This is settable from the @code{Settings} application,
- under @code{Applications / Development}.)
- Connect the phone to your computer with the USB cable.
- Verify that the phone is accessible to @code{adb}:
- @example
- $ adb devices
- List of devices attached
- 0A3A560F0C015024 device
- @end example
- If you don't see a device listed, it may be permission problem. You can figure out which device corresponds to the phone by doing:
- @example
- $ ls -l /dev/bus/usb/*
- /dev/bus/usb/001:
- total 0
- ...
- crw-rw-rw- 1 root wheel 189, 5 2010-10-18 16:52 006
- ...
- @end example
- The timestamp corresponds to when you connected the phone.
- Make the USB connection readable:
- @example
- $ sudo chmod a+w /dev/bus/usb/001/006
- @end example
- Obviously if you spend time developing for an Androd phone you'll want to automate this process;
- @uref{https://sites.google.com/site/siteofhx/Home/android/drivers/udc,this link}
- or @uref{https://groups.google.com/forum/?fromgroups=#!topic/android-developers/nTfhhPktGfM,this link} may be helpful.
- Anyway, once @code{adb} can talk to the phone, you install in the same way as before:
- @example
- adb install bin/KawaHello-debug.apk
- @end example
- @subsection Some debugging notes
- You will find a copy of the SDK documentation in @code{$ANDROID_HOME/docs/index.html}.
- If the emulator complains that your application has stopped unexpectedly, do:
- @example
- $ adb logcat
- @end example
- This shows log messages, stack traces, output from the @code{Log.i} logging method, and other useful information.
- (You can alternatively start @code{ddms} (Dalvik Debug Monitor Service), click on the @code{kawa.android line} in the top-left sub-window to select it, then from the @code{Device} menu select @code{Run logcat....}).
- To uninstall your application, do:
- @example
- $ adb uninstall kawa.android
- @end example
- @subsection Other resources
- (A more interesting @uref{http://androidscheme.blogspot.com/2010/10/text-to-speech-app.html,text-to-speech} example app is on Santosh Rajan's @uref{http://androidscheme.blogspot.com/,Android-Scheme blog}.)
- @uref{https://github.com/ecraven/SchemeAndroidOGL}
- @node Android view construction
- @section Android view construction
- An Android user interface is constructed from @code{View} objects.
- The following is an example that illustrates some features of
- Kawa to help write views hierarchies,
- The example is self-contained, and can be built and run
- as described in @ref{Building for Android}.
- @example
- (require 'android-defs)
- (activity hello
- (on-create-view
- (define counter ::integer 0)
- (define counter-view
- (TextView text: "Not clicked yet."))
- (LinearLayout orientation: LinearLayout:VERTICAL
- (TextView text: "Hello, Android from Kawa Scheme!")
- (Button
- text: "Click here!"
- on-click-listener: (lambda (e)
- (set! counter (+ counter 1))
- (counter-view:setText
- (format "Clicked ~d times." counter))))
- counter-view)))
- @end example
- The first @code{import} form imports various useful definitions
- from the Kawa Android library. Using these is not required for
- writing a Kawa application, but makes it more convenient.
- The names @code{LinearLayout}, @code{TextView}, and @code{Button}
- are just aliases for standard Android @code{View} sub-classes.
- A few are prefined by @code{(require 'android-defs)}, or you
- can define them yourself using @code{define-alias}.
- An Android application consists of one or more @dfn{activities},
- each of which is an instance of the @code{android.app.Activity} class.
- You can use the @code{activity} macro to define your @code{Activity} class.
- The first macro argument (in this case @code{hello}) is the class name,
- and the others are members of the class, in the syntax of
- a @stxref{field-or-method-decl}. The sub-form @code{on-create-view}
- is an abbreviation for declaring an @code{onCreate} method
- (which is called when the @code{Activity} starts up
- @c LOOK THIS UP - also when re-started?
- followed by a @code{setContentView}:
- The body of the @code{on-create-view} is evaluated.
- The result should be a @code{View} expression,
- which is passed to @code{setContentView}.
- @deffn Procedure current-activity [new-value]
- With no arguments, returns the current @code{Activity}.
- If a @var{new-value} argument is given, sets the current activity.
- It is set automatically by the @code{on-create} and @code{on-create-view}
- methods of the @code{activity} macro.
- Since @code{current-activity} is a @ref{Parameter objects,parameter object},
- you can
- locally change the value using @ref{parameterize-syntax,@code{parameterize}}.
- @end deffn
- @subsection View object allocation
- To create an instance of a @code{View} class you ``call'' the
- class as if it were a function,
- as described in @ref{Allocating objects}.
- For example:
- @example
- (TextView (this) text: "Hello, Android from Kawa Scheme!")
- @end example
- If you @code{(require 'android-defs)} that defines
- some special handling for @code{View} classes.
- You can leave out the @code{(this)} argument,
- which refers to the enclosing @code{Activity}:
- @example
- (TextView text: "Hello, Android from Kawa Scheme!")
- @end example
- @subsection Event handlers
- You can register event listeners on Android @code{View} objects
- using methods typically named @code{setOn@var{EVENT}Listener}.
- For example @code{setOnClickListener}. When allocating
- an object you can leave out the @code{set}, and you can optionally
- use Scheme-style names: @code{on-click-listener}. The argument
- is an object of a special nested listener class,
- for example @code{View$OnClickListener}. These are
- single-method classes, so you can use a lambda expression
- and @ref{SAM-conversion} will automatically create the needed
- listener class.
- @node System inquiry
- @section System inquiry
- @defvar home-directory
- A string containing the home directory of the user.
- @end defvar
- @deffn Procedure command-line
- Returns a nonempty list of immutable strings. The first element is
- an implementation-specific name for the running top-level
- program.
- The remaining elements are the command-line arguments,
- as passed to the @code{main} method (except for those
- flags processed by Kawa itself).
- The first element will depend on how the Kawa module was invoked.
- Kawa uses the following rules to determine the command name:
- @enumerate
- @item
- If the property @code{kawa.command.name} is set, that is used.
- This variable can be set on the @code{kawa} command line,
- for example from a script:
- @example
- kawa -Dkawa.command.name="$0" foo "$@@"
- @end example
- This variable is also set implicitly by the meta-arg option. FIXME.
- @item
- If we're reading a source file that starts with the Unix command-file prefix
- @samp{#!/} then we use the name of the source file.
- The assumption is that such a file is an executable script.
- @item
- If the Java property @code{kawa.command.line} is set,
- then we use that (after stripping off text that duplicates
- the remaining arguments).
- The @code{kawa} program sets this property to the
- command line used to invoke it
- (specifically the contents of the entire @code{argv} array),
- before invoking the @code{java} program.
- @item
- If the Java property @code{sun.java.command} is set,
- then we use that (after stripping off text that duplicates
- the remaining arguments), and then prepending the string @code{"java "}.
- The OpenJDK @code{java} program sets this property.
- @item
- If all else fails, the command name is @code{"kawa"}.
- @end enumerate
- @end deffn
- @defvar command-line-arguments
- Any command-line arguments (following flags processed by Kawa itself)
- are assigned to the global variable @samp{command-line-arguments},
- which is a vector of strings.
- @end defvar
- @deffn Procedure process-command-line-assignments
- Process any initial command-line options that set variables.
- These have the form @code{@var{name}=@var{value}}.
- Any such command-line options (at the start of the command-line)
- are processed and removed from the command-line.
- @example
- $ java kawa.repl -- abc=123 def
- #|kawa:1|# (write (command-line))
- ("java kawa.repl --" "abc=123" "def")
- #|kawa:2|# (process-command-line-assignments)
- #|kawa:3|# (write (command-line))
- ("java kawa.repl -- abc=123" "def")
- #|kawa:4|# abc
- 123
- @end example
- This function is mostly useful for Kawa applications
- compiled with the @code{--main} option.
- (It is used to set XQuery @code{external} variables.)
- @end deffn
- @deffn Procedure get-environment-variable name
- Many operating systems provide each running process with
- an environment conisting of environment variables.
- (This environment is not to be confused with the
- Scheme environments that can be passed to @code{eval}.)
- Both the name and value of an environment variable are
- strings. The procedure @code{get-environment-variable}
- returns the value of the environment variable @var{name},
- or @code{#f} if the environment variable is not found.
- @c It may use locale information to encode the name and
- @c decode the value of the environment variable.
- (This uses the @code{java.lang.System:getenv} method.)
- It is an error to mutate the resulting string.
- @example
- (get-environment-variable "PATH")
- @result{} "/usr/local/bin:/usr/bin:/bin"
- @end example
- @end deffn
- @deffn Procedure get-environment-variables
- Returns the names and values of all the environment variables as an alist,
- where the car of each entry is the name of an environment variable,
- and the cdr is its value, both as strings.
- It is an error to mutate any of the strings or the alist itself.
- @example
- (get-environment-variables)
- @result{} (("USER" . "root") ("HOME" . "/"))
- @end example
- @end deffn
- @node Processes
- @section Processes
- A @dfn{process} is a native (operating-system-level) application or
- program that runs separately from the current virtual machine.
- Many programming languages have facilities to allow access to system
- processes (commands). (For example Java has @code{java.lang.Process}
- and @code{java.lang.ProcessBuilder}.)
- These facilities let you send data to the standard input, extract the
- resulting output, look at the return code, and sometimes even pipe
- commands together. However, this is rarely as easy as it is using
- the old Bourne shell; for example command substitution is awkward.
- Kawa's solution is based on these two ideas:
- @itemize
- @item
- A ``process expression'' (typically a function call) evaluates to a
- @code{LProcess} value, which provides access to a Unix-style
- (or Windows) process.
- @item
- In a context requiring a string (or a bytevector), an @code{LProcess} is
- automatically converted to a string (or bytevector)
- comprising the standard output from the process.
- @end itemize
- @subsection Creating a process
- The most flexible way to start a process is with either the
- @code{run-process} procedure or
- the @code{&`@lbracechar{}@meta{command}@rbracechar{}} syntax
- for @ref{process literals}.
- @deffn Procedure run-process @arbno{@stxref{process-keyword-argument}} @meta{command}
- Creates a process object, specifically a @code{gnu.kawa.functions.LProcess}
- object.
- A @var{process-keyword-argument} can be used to set various options,
- as discussed below.
- The @var{command} is the process command-line (name and arguments).
- It can be an array of strings, in which case those are used as the
- command arguments directly:
- @example
- (run-process ["ls" "-l"])
- @end example
- The @var{command} can also be a single string, which is split (tokenized)
- into command arguments separated by whitespace.
- Quotation groups words together just like traditional shells:
- @example
- (run-process "cmd a\"b 'c\"d k'l m\"n'o")
- @result{} (run-process ["cmd" "ab 'cd" "k'l m\"no"])
- @end example
- The syntax shorthand @code{&`@lbracechar{}@meta{command}@rbracechar{}}
- or @code{&sh@lbracechar{}@meta{command}@rbracechar{}} (discussed below)
- is usually more convenient.
- @end deffn
- @display
- @stxdef{process-keyword-argument}
- @stxref{process-redirect-argument}
- | @stxref{process-environment-argument}
- | @stxref{process-misc-argument}
- @end display
- We discuss @stxref{process-redirect-argument} and
- @stxref{process-environment-argument} later.
- @anchor{meta-process-misc-argument}
- The @var{process-misc-argument} options are just the following:
- @table @asis
- @item @stxlit{shell:} @var{shell}
- Currently, @var{shell} must be one of @code{#f} (which is ignored)
- or @code{#t}. The latter means to use an external shell to tokenize
- the @var{command}.
- I.e. the following are equivalent:
- @example
- (run-process shell: #t "@var{command}")
- (run-process ["/bin/sh" "-c" "@var{command}"])
- @end example
- @item @stxlit{directory:} @var{dir}
- Change the working directory of the new process to @var{dir}.
- @end table
- @anchor{process literals}
- @subsection Process literals
- A simple @dfn{process literal} is a kind of
- @ref{Named quasi-literals,named literal} that uses the backtick character
- (@code{`}) as the @stxref{cname}.
- For example:
- @example
- &`@lbracechar{}date --utc@rbracechar{}
- @end example
- This is equivalent to:
- @example
- (run-process "date --utc")
- @end example
- In general the following are
- roughly equivalent (using @ref{string quasi-literals}):
- @example
- &`[@var{args}...]@lbracechar{}@var{command}@rbracechar{}
- (run-process @var{args}... &@lbracechar{}@var{command}@rbracechar{})
- @end example
- The reason for the ``roughly'' is if @var{command} contains
- escaped sub-expressions; in that case @code{&`} may process
- the resulting values differently from plain string-substitution,
- as discussed below.
- If you use @code{&sh} instead of @code{&`} then a shell is used:
- @example
- &sh@lbracechar{}rm *.class@rbracechar{}
- @end example
- which is equivalent to:
- @example
- &`@lbracechar{}/bin/sh -c "rm *.class"@rbracechar{}
- @end example
- In general, the following are equivalent:
- @example
- &sh[@var{args}...]@lbracechar{}@var{command}@rbracechar{}
- &`[shell: #t @var{args}...]@lbracechar{}@var{command}@rbracechar{}
- @end example
- @subsection Process values and process output
- The value returned from a call to @code{run-process} or a process literal
- is an instance of @code{gnu.kawa.functions.LProcess}.
- This class extends @code{java.lang.Process}, so you can treat it as
- any other @code{Process} object.
- @example
- #|kawa:1|# (define p1 &`@lbracechar{}date --utc@rbracechar{})
- #|kawa:2|# (p1:toString)
- gnu.kawa.functions.LProcess@@377dca04
- #|kawa:3|# (write p1)
- gnu.kawa.functions.LProcess@@377dca04
- @end example
- What makes an @code{LProcess} interesting is that it is also
- a @ref{Blobs,blob}, which is automatically
- converted to a string (or bytevector) in a context that requires it.
- The contents of the blob comes from the standard output of the process.
- The blob is evaluated @ref{Lazy evaluation,lazily},
- so data it is only collected when requested.
- @example
- #|kawa:4|# (define s1 ::string p1)
- #|kawa:5|# (write s1)
- "Wed Jan 1 01:18:21 UTC 2014\n"
- #|kawa:6|# (define b1 ::bytevector p1)
- (write b1)
- #u8(87 101 100 32 74 97 110 ... 52 10)
- @end example
- The @code{display} procedure prints it in ``human'' form, as a string:
- @example
- #|kawa:7|# (display p1)
- Wed Jan 1 01:18:21 UTC 2014
- @end example
- This is also the default REPL formatting:
- @example
- #|kawa:8|# &`@lbracechar{}date --utc@rbracechar{}
- Wed Jan 1 01:18:22 UTC 2014
- @end example
- When you type a command to a shell, its output goes to the console,
- Similarly, in a REPL the output from the process
- is copied to the console output - which can sometimes by optimized
- by letting the process inherit its standard output from the Kawa process.
- @subsection Substitution and tokenization
- To substitute the variable or the result of an expression
- in the command line use the usual syntax for quasi literals:
- @example
- (define filename (make-temporary-file))
- &sh@lbracechar{}run-experiment >&[filename]@rbracechar{}
- @end example
- Since a process is convertible a string, we need no special
- syntax for command substitution:
- @example
- `@lbracechar{}echo The directory is: &[&`@lbracechar{}pwd@rbracechar{}]@rbracechar{}
- @end example
- or equivalently:
- @example
- `@lbracechar{}echo The directory is: &`@lbracechar{}pwd@rbracechar{}@rbracechar{}
- @end example
- @anchor{substitution-tokenization}
- Things get more interesting when considering the interaction between
- substitution and tokenization. This is not simple string
- interpolation. For example, if an interpolated value contains a quote
- character, we want to treat it as a literal quote, rather than a token
- delimiter. This matches the behavior of traditional shells. There are
- multiple cases, depending on whether the interpolation result is a
- string or a vector/list, and depending on whether the interpolation is
- inside quotes.
- @itemize
- @item
- If the value is a string, and we're not inside quotes, then all
- non-whitespace characters (including quotes) are literal, but
- whitespace still separates tokens:
- @example
- (define v1 "a b'c ")
- &`@lbracechar{}cmd x y&[v1]z@rbracechar{} @result{} (run-process ["cmd" "x" "ya" "b'c" "z"])
- @end example
- @item
- If the value is a string, and we are inside single quotes,
- all characters (including whitespace) are literal.
- @example
- &`@lbracechar{}cmd 'x y&[v1]z'@rbracechar{} @result{} (run-process ["cmd" "x ya b'c z"])
- @end example
- Double quotes work the same except that newline is an argument
- separator. This is useful when you have one filename per line, and the
- filenames may contain spaces, as in the output from @code{find}:
- @example
- &`@lbracechar{}ls -l "&`@lbracechar{}find . -name '*.pdf'@rbracechar{}"@rbracechar{}
- @end example
- This solves a problem that is quite painful with traditional shells.
- @item
- If the value is a vector or list (of strings), and we're not inside
- quotes, then each element of the array becomes its own argument, as-is:
- @example
- (define v2 ["a b" "c\"d"])
- &`@lbracechar{}cmd &[v2]@rbracechar{} @result{} (run-process ["cmd" "a b" "c\"d"])
- @end example
- However, if the enclosed expression is adjacent to non-space non-quote
- characters, those are prepended to the first element, or appended to
- the last element, respectively.
- @example
- &`@lbracechar{}cmd x&[v2]y@rbracechar{} @result{} (run-process ["cmd" "xa b" "c\"dy"])
- &`@lbracechar{}cmd x&[[]]y@rbracechar{} @result{} (run-process ["cmd" "xy"])
- @end example
- This behavior is similar to how shells handle @code{"$@@"}
- (or @code{"$@lbracechar{}name[@@]@rbracechar{}"} for general arrays), though in Kawa you would
- leave off the quotes.
- Note the equivalence:
- @example
- &`@lbracechar{}&[array]@rbracechar{} @result{} (run-process array)
- @end example
- @item
- If the value is a vector or list (of strings), and we @emph{are}
- inside quotes, it is equivalent to interpolating a single string
- resulting from concatenating the elements separated by a space:
- @example
- &`@lbracechar{}cmd "&[v2]"@rbracechar{}
- @result{} (run-process ["cmd" "a b c\"d"])
- @end example
- This behavior is similar to how shells handle @code{"$*"} (or
- @code{"$@lbracechar{}name[*]@rbracechar{}"} for general arrays).
- @item
- If the value is the result of a call to @code{unescaped-data} then it
- is parsed as if it were literal. For example a quote in the unescaped
- data may match a quote in the literal:
- @example
- (define vu (unescaped-data "b ' c d '"))
- &`@lbracechar{}cmd 'a &[vu]z'@rbracechar{} @result{} (run-process ["cmd" "a b " "c" "d" "z"])
- @end example
- @item
- If we're using a shell to tokenize the command, then we add quotes or
- backslashes as needed so that the shell will tokenize as described
- above:
- @example
- (define authors ["O'Conner" "de Beauvoir"])
- &sh@lbracechar{}list-books &[authors]@rbracechar{}
- @end example
- The command passed to the shell is:
- @example
- list-books 'O'\''Conner' 'de Beauvoir
- @end example
- Having quoting be handled by the @code{$construct$:sh}
- implementation automatically eliminates common code injection problems.
- @end itemize
- Smart tokenization only happens when using the quasi-literal forms such
- as @code{&`@lbracechar{}command@rbracechar{}}.
- You can of course use string templates with @code{run-process}:
- @example
- (run-process &@lbracechar{}echo The directory is: &`@lbracechar{}pwd@rbracechar{}@rbracechar{})
- @end example
- However, in that case there is no smart tokenization: The template is
- evaluated to a string, and then the resulting string is tokenized,
- with no knowledge of where expressions were substituted.
- @subsection Input/output redirection
- You can use various keyword arguments to specify standard input, output,
- and error streams. For example to lower-case the text in @code{in.txt},
- writing the result to @code{out.txt}, you can do:
- @example
- &`[in-from: "in.txt" out-to: "out.txt"]@lbracechar{}tr A-Z a-z@rbracechar{}
- @end example
- or:
- @example
- (run-process in-from: "in.txt" out-to: "out.txt" "tr A-Z a-z")
- @end example
- @anchor{meta-process-redirect-argument}
- A @var{process-redirect-argument} can be one of the following:
- @table @asis
- @item @stxlit{in:} @var{value}
- The @var{value} is evaluated, converted to a string (as if
- using @code{display}), and copied to the input file of the process.
- The following are equivalent:
- @example
- &`[in: "text\n"]@lbracechar{}command@rbracechar{}
- &`[in: &`@lbracechar{}echo "text"@rbracechar{}]@lbracechar{}command@rbracechar{}
- @end example
- You can pipe the output from @code{command1} to the input
- of @code{command2} as follows:
- @example
- &`[in: &`@lbracechar{}command1@rbracechar{}]@lbracechar{}command2@rbracechar{}
- @end example
- @item @stxlit{in-from:} @var{path}
- The process reads its input from the specified @var{path}, which
- can be any value coercible to a @code{filepath}.
- @item @stxlit{out-to:} @var{path}
- The process writes its output to the specified @var{path}.
- @item @stxlit{err-to:} @var{path}
- Similarly for the error stream.
- @item @stxlit{out-append-to:} @var{path}
- @item @stxlit{err-append-to:} @var{path}
- Similar to @code{out-to} and @code{err-to}, but append to the file
- specified by @var{path}, instead of replacing it.
- @item @stxlit{in-from: 'pipe}
- @item @stxlit{out-to: 'pipe}
- @item @stxlit{err-to: 'pipe}
- Does not set up redirection. Instead, the specified stream is available
- using the methods @code{getOutputStream}, @code{getInputStream},
- or @code{getErrorStream}, respectively, on the resulting @code{Process} object,
- just like Java's @code{ProcessBuilder.Redirect.PIPE}.
- @item @stxlit{in-from: 'inherit}
- @item @stxlit{out-to: 'inherit}
- @item @stxlit{err-to: 'inherit}
- Inherits the standard input, output, or error stream from the
- current JVM process.
- @item @stxlit{out-to:} @var{port}
- @item @stxlit{err-to:} @var{port}
- Redirects the standard output or error of the process to
- the specified @var{port}.
- @item @stxlit{out-to: 'current}
- @item @stxlit{err-to: 'current}
- Same as @code{out-to: (current-output-port)},
- or @code{err-to: (current-error-port)}, respectively.
- @item @stxlit{in-from:} @var{port}
- @item @stxlit{in-from: 'current}
- Re-directs standard input to read from the @var{port}
- (or @code{(current-input-port)}). It is unspecified how much is read from
- the @var{port}. (The implementation is to use a thread that reads from the
- port, and sends it to the process, so it might read to the end of the port,
- even if the process doesn't read it all.)
- @item @stxlit{err-to: 'out}
- Redirect the standard error of the process to be merged with the
- standard output.
- @end table
- The default for the error stream (if neither @code{err-to} or
- @code{err-append-to} is specified) is equivalent to @code{err-to: 'current}.
- @emph{Note:} Writing to a port is implemented by copying the output or error
- stream of the process. This is done in a thread, which means we don't have
- any guarantees when the copying is finished. (In the future we might
- change @code{process-exit-wait} (discussed later) wait for not only the
- process to finish, but also for these helper threads to finish.)
- A @uref{https://en.wikipedia.org/wiki/Here_document,here document} is
- a form a literal string, typically multi-line, and commonly used in
- shells for the standard input of a process. You can use string literals or
- @ref{string quasi-literals} for this.
- For example, this passes the string @code{"line1\nline2\nline3\n"} to
- the standard input of @code{command}:
- @example
- (run-process [in: &@lbracechar{}
- &|line1
- &|line2
- &|line3
- @rbracechar{}] "command")
- @end example
- Note the use of @code{&|} to mark the end of ignored indentation.
- @subsection Pipe-lines
- Piping the output of one process as the input of another
- is in principle easy - just use the @code{in:}
- process argument. However, writing a multi-stage pipe-line quickly gets ugly:
- @example
- &`[in: &`[in: "My text\n"]@lbracechar{}tr a-z A-Z@rbracechar{}]@lbracechar{}wc@rbracechar{}
- @end example
- The convenience macro @code{pipe-process} makes this much nicer:
- @example
- (pipe-process
- "My text\n"
- &`@lbracechar{}tr a-z A-Z@rbracechar{}
- &`@lbracechar{}wc@rbracechar{})
- @end example
- @deffn Syntax pipe-process input @arbno{process}
- All of the @var{process} expressions must be @code{run-process} forms,
- or equivalent @code{&`@lbracechar{}command@rbracechar{}} forms.
- The result of evaluating @var{input} becomes the input to the first
- @var{process}; the output from the first @var{process} becomes
- the input to the second @var{process}, and so on. The result of
- whole @code{pipe-process} expression is that of the last @var{process}.
- Copying the output of one process to the input of the next is
- optimized: it uses a copying loop in a separate thread. Thus you can
- safely pipe long-running processes that produce huge output. This
- isn't quite as efficient as using an operating system pipe, but is
- portable and works pretty well.
- @end deffn
- @subsection Setting the process environment
- @anchor{meta-process-environment-argument}
- By default the new process inherits the system environment of the current
- (JVM) process as returned by @code{System.getenv()}, but you can override it.
- A @var{process-environment-argument} can be one of the following:
- @table @asis
- @item @stxlit{env-}@var{name}@stxlit{:} @var{value}
- In the process environment, set the @code{"@var{name}"} to the
- specified @var{value}. For example:
- @example
- &`[env-CLASSPATH: ".:classes"]@lbracechar{}java MyClass@rbracechar{}
- @end example
- @item @var{NAME}@stxlit{:} @var{value}
- Same as using the @code{env-@var{NAME}} option above, but only if the
- @code{@var{NAME}} is uppercase (i.e. if uppercasing @code{@var{NAME}} yields
- the same string). For example the previous example could be written:
- @example
- &`[CLASSPATH: ".:classes"]@lbracechar{}java MyClass@rbracechar{}
- @end example
- @item @stxlit{environment:} @var{env}
- The @var{env} is evaluated and must yield a @code{HashMap}.
- This map is used as the system environment of the process.
- @end table
- @subsection Waiting for process exit
- When a process finishes, it returns an integer exit code.
- The code is traditionally 0 on successful completion,
- while a non-zero code indicates some kind of failure or error.
- @deffn Procedure process-exit-wait process
- The @var{process} expression must evaluate to a process
- (any @code{java.lang.Process} object).
- This procedure waits for the process to finish, and then returns the
- exit code as an @code{int}.
- @example
- (process-exit-wait (run-process "echo foo")) @result{} 0
- @end example
- @end deffn
- @deffn Procedure process-exit-ok? process
- Calls @code{process-exit-wait}, and then returns @code{#false}
- if the process exited it 0, and returns @code{#true} otherwise.
- This is useful for emulating the way traditional shell do
- logic control flow operations based on the exit code.
- For example in @code{sh} you might write:
- @example
- if grep Version Makefile >/dev/null
- then echo found Version
- else echo no Version
- fi
- @end example
- The equivalent in Kawa:
- @example
- (if (process-exit-ok? &`@lbracechar{}grep Version Makefile@rbracechar{})
- &`@lbracechar{}echo found@rbracechar{}
- &`@lbracechar{}echo not found@rbracechar{})
- @end example
- Strictly speaking these are not quite the same, since the Kawa
- version silently throws away the output from @code{grep}
- (because no-one has asked for it). To match the output from the @code{sh},
- you can use @code{out-to: 'inherit}:
- @example
- (if (process-exit-ok? &`[out-to: 'inherit]@lbracechar{}grep Version Makefile@rbracechar{})
- &`@lbracechar{}echo found@rbracechar{}
- &`@lbracechar{}echo not found@rbracechar{})
- @end example
- @end deffn
- @anchor{Exiting the current process}
- @subsection Exiting the current process
- @deffn Procedure exit [code]
- Exits the Kawa interpreter, and ends the Java session.
- Returns the value of @var{code} to the operating system:
- The @var{code} must be integer, or the special
- values @code{#f} (equivalent to -1), or
- @code{#t} (equivalent to 0).
- If @var{code} is not specified, zero is returned.
- The @var{code} is a status code; by convention a non-zero
- value indicates a non-standard (error) return.
- Before exiting, finally-handlers (as in @code{try-finally},
- or the @var{after} procedure of @code{dynamic-wind}) are
- executed, but only in the current thread, and only if
- the current thread was started normally. (Specifically
- if we're inside an @code{ExitCalled} block with non-zero
- nesting - see @code{gnu.kawa.util.ExitCalled}.)
- Also, JVM shutdown hooks are executed - which includes
- flushing buffers of output ports. (Specifically
- @code{Writer} objects registered with the @code{WriterManager}.)
- @end deffn
- @deffn Procedure emergency-exit [code]
- Exits the Kawa interpreter, and ends the Java session.
- Communicates an exit value in the same manner as @code{exit}.
- Unlike @code{exit}, neither finally-handlers nor
- shutdown hooks are executed.
- @end deffn
- @subsection Deprecated functions
- @deffn Procedure make-process command envp
- Creates a @code{<java.lang.Process>} object, using the specified
- @var{command} and @var{envp}.
- The @var{command} is converted to an array of Java strings
- (that is an object that has type @code{<java.lang.String[]>}.
- It can be a Scheme vector or list (whose elements should be
- Java strings or Scheme strings); a Java array of Java strings;
- or a Scheme string. In the latter case, the command is converted
- using @code{command-parse}.
- The @var{envp} is process environment; it should be either
- a Java array of Java strings, or the special @code{#!null} value.
- Except for the representation of @var{envp}, this is similar to:
- @example
- (run-process environment: @var{envp} @var{command})
- @end example
- @end deffn
- @deffn Procedure system command
- Runs the specified @var{command}, and waits for it to finish.
- Returns the return code from the command. The return code is an integer,
- where 0 conventionally means successful completion.
- The @var{command} can be any of the types handled by @code{make-process}.
- Equivalent to:
- @example
- (process-exit-wait (make-process @var{command} #!null))
- @end example
- @end deffn
- @defvar command-parse
- The value of this variable should be a one-argument procedure.
- It is used to convert a command from a Scheme string to a Java
- array of the constituent "words".
- The default binding, on Unix-like systems, returns a new command to
- invoke @code{"/bin/sh" "-c"} concatenated with the command string;
- on non-Unix-systems, it is bound to @code{tokenize-string-to-string-array}.
- @end defvar
- @deffn Procedure tokenize-string-to-string-array command
- Uses a @code{java.util.StringTokenizer} to parse the @var{command} string
- into an array of words. This splits the @var{command} using spaces
- to delimit words; there is no special processing for quotes or other
- special characters.
- (This is the same as what @code{java.lang.Runtime.exec(String)} does.)
- @end deffn
- @node Time-related functions
- @section Time-related functions
- @deffn Procedure current-second
- Returns an inexact number represent the current time on the
- @uref{http://en.wikipedia.org/wiki/International_Atomic_Time,International Atomic Time (TAI)} scale.
- The value 0.0 represents midnight on January 1, 1070 TAI (equivalent
- to 10 seconds before midnight Universal Time), and
- the value 1.0 represents on TAI second later.
- Neither high acuracy nor high precision are required; in particular
- returning Coordinated Universal Time plus a suitable
- constant might be the best an implementation cat do.
- The Kawa implementation just multiplies by 0.001 the result of calling
- the method @code{currentTimeMillis} in class @code{java.lang.System}.
- @end deffn
- @deffn Procedure current-jiffy
- Returns the number of @dfn{jiffies} as an exact integer that have
- elapses since an arbitrary implementation-defined epoch (instant).
- A jiffy is an implementation-defined fraction of a second which is
- defined by the return value of the @code{jiffies-per-second} procedure.
- The starting epoch (instant 0) is guaranteed to be constant during
- a run of the program, but may vary between runs.
- (At the time of writing, Kawa's jiffy is one nano-second.)
- @emph{Rationale:} Jiffies are allowed to be implementation-dependent
- so that @code{current-jiffy} can execute with minimal overhead.
- It should be very likely that a compactly represented integer will
- suffice as the return value. Any particular jiffy size will be
- inappropriate some some implementations: a microsecond is too long for
- a very fast machine, while a much smaller unit would force many
- implementations to return integers which have to allocated for most calls,
- rendering @code{current-jiffy} less useful for accurate timing measurements.
- @end deffn
- @deffn Procedure jiffies-per-second
- Returns an exact integer representing the number of jiffies
- per SI second. This value is an implementation-specified
- constant.
- (At the time of writing, the value in Kawa is 1,000,000,000.)
- @end deffn
- @deffn Procedure sleep time
- Suspends the current thread for the specified time.
- The @var{time} can be either a pure number (in secords),
- or a quantity whose unit is a time unit (such as @code{10s}).
- @end deffn
- @node Low-level functions
- @section Deprecated low-level functions
- These sections document older and less convenient ways
- to call Java methods, access Java fields, and use Java arrays.
- @anchor{Low-level Method invocation}
- @subsection Low-level Method invocation
- The following lower-level primitives require you to specify
- the parameter and return types explicitly.
- You should probably use the functions @code{invoke} and @code{invoke-static}
- (@pxref{Method operations}) instead.
- @deffn Syntax primitive-constructor class (argtype ...)
- Returns a new anonymous procedure, which when called will create
- a new object of the specified class, and will then call the
- constructor matching the specified argument types.
- @end deffn
- @deffn Syntax primitive-virtual-method class method rtype (argtype ...)
- Returns a new anonymous procedure, which when called will
- invoke the instance method whose name is the string @var{method}
- in the class whose name is @var{class}.
- @end deffn
- @deffn Syntax primitive-static-method class method rtype (argtype ...)
- Returns a new anonymous procedure, which when called will
- invoke the static method whose name is the string @var{method}
- in the class whose name is @var{class}.
- @end deffn
- @deffn Syntax primitive-interface-method interface method rtype (argtype ...)
- Returns a new anonymous procedure, which when called will
- invoke the matching method from the interface whose name is @var{interface}.
- @end deffn
- The macros return procedure values, just like @code{lambda}.
- If the macros are used directly as the procedure of a procedure call,
- then kawa can inline the correct bytecodes to call the specified methods.
- (Note also that neither macro
- checks that there really is a method that matches the specification.)
- Otherwise, the Java reflection facility is used.
- @subsection Low-level field operations
- The following macros evaluate to procedures that can be used to
- access or change the fields of objects or static fields.
- The compiler can inline each to a single bytecode instruction
- (not counting type conversion).
- These macros are deprecated.
- The @code{fields} and @code{static-field} functions
- (@pxref{Field operations}) are easier to use, more powerful, and
- just as efficient. However, the high-level functions currently do
- not provide access to non-public fields.
- @deffn Syntax primitive-get-field class fname ftype
- Use this to access a field named @var{fname} having type @var{type} in
- class @var{class}. Evaluates to a new one-argument procedure,
- whose argument is a reference to an object of the specified @var{class}.
- Calling that procedure returns the value of the specified field.
- @end deffn
- @deffn Syntax primitive-set-field class fname ftype
- Use this to change a field named @var{fname} having type @var{type} in
- class @var{class}. Evaluates to a new two-argument procedure,
- whose first argument is a reference to an object of the
- specified @var{class}, and the second argument is the new value.
- Calling that procedure sets the field to the specified value.
- (This macro's name does not end in a @samp{!}, because it does not actually
- set the field. Rather, it returns a function for setting the field.)
- @end deffn
- @deffn Syntax primitive-get-static class fname ftype
- Like @code{primitive-get-field}, but used to access static fields.
- Returns a zero-argument function, which when called returns
- the value of the static field.
- @end deffn
- @deffn Syntax primitive-set-static class fname ftype
- Like @code{primitive-set-field}, but used to modify static fields.
- Returns a one-argument function, which when called sets the
- value of the static field to the argument.
- @end deffn
- @anchor{Low-level array macros}
- @subsection Old low-level array macros
- The following macros evaluate to procedures that can be used to
- manipulate primitive Java array objects.
- The compiler can inline each to a single bytecode instruction
- (not counting type conversion).
- @deffn Syntax primitive-array-new element-type
- Evaluates to a one-argument procedure. Applying the resulting procedure to
- an integer count allocates a new Java array of the specified length,
- and whose elements have type @var{element-type}.
- @end deffn
- @deffn Syntax primitive-array-set element-type
- Evaluates to a three-argument procedure. The first argument of
- the resulting procedure must be an array whose elements have type
- @var{element-type}; the second argument is an index; and the third
- argument is a value (coercible to @var{element-type}) which replaces
- the value specified by the index in the given array.
- @end deffn
- @deffn Syntax primitive-array-get element-type
- Evaluates to a two-argument procedure. The first argument of
- the resulting procedure must be an array whose elements have type
- @var{element-type}; the second argument is an index.
- Applying the procedure returns the element at the specified index.
- @end deffn
- @deffn Syntax primitive-array-length element-type
- Evaluates to a one-argument procedure. The argument of
- the resulting procedure must be an array whose elements have type
- @var{element-type}.
- Applying the procedure returns the length of the array.
- (Alternatively, you can use @code{(field @var{array} 'length)}.)
- @end deffn
- @node FAQs
- @chapter Frequently Asked Questions
- @ignore
- @subsubheading What is the difference between load, require, import, and include?
- Handling @code{include} is all done at compile time. It's as if the text
- of the included file is copied and replaces the include command.
- If the include is inside a @code{let}, the included text can
- use the name defined in the @code{let}.
- In contrast @code{load} is a procedure, not syntax.
- It reads and evaluates each line in the file, one at a time,
- @end ignore
- @anchor{importing-class-names}
- @subsubheading What is the equivalent of Java import?
- To provide a short name for a class instead of the complete fully-qualified
- name use either @code{define-alias} (or @code{define-private-alias})
- or the @code{import}-@code{class} combination.
- For example, to be able to write @code{ArrayList} instead
- of @code{java.util.ArrayList} do either:
- @example
- (import (class java.util ArrayList))
- @end example
- or
- @example
- (define-alias ArrayList java.util.ArrayList)
- @end example
- Using @code{import} is recommended:
- It handles errors better,
- and it allows you to define multiple aliases conveniently:
- @example
- (import (class java.util Map HashMap))
- @end example
- Both forms allow renaming. For example if you want to refer
- to @code{java.lang.StringBuilder} as @code{StrBuf} do:
- @example
- (import (class java.lang (StringBuilder StrBuf)))
- @end example
- or:
- @example
- (define-alias StrBuf java.lang.StringBuilder)
- @end example
- The name(s) defined by @code{import} are by default private.
- A name defined using @code{define-alias} is by default exported;
- to avoid that use @code{define-private-alias} instead.
- You can also use @code{define-namespace} to introduce an abbreviation or
- renaming of a class name, but as a matter of style @code{define-alias}
- is preferred.
- There is no direct equivalent to Java's @code{import PackageOrTypeName.*}
- (type-import-on-demand) declaration, but you can alias a package:
- @example
- (define-alias jutil java.util)
- (define mylist :: jutil:List (jutil:ArrayList))
- @end example
- To import a static member, giving it a shortened name
- (like Java's static-import-on-demand declaration), you can use
- @code{define-alias}. For example:
- @example
- (define-alias console java.lang.System:console)
- @end example
- For static fields only (not methods or member classes) you can
- use an @code{import} form, either:
- @example
- (import (only (java lang System) out))
- @end example
- or:
- @example
- (import (only java.lang.System out))
- @end example
- This works because Kawa can treat any class as a ``library'';
- in which case it considers all public static fields as exported bindings.
- @subsubheading How do I refer to a Java member (nested) class?
- Consider the Java SE member class @code{javax.swing.text.AbstractDocument.Content}.
- Using the Java syntax doesn't work in Kawa.
- Inside you should use Kawa's colon operator:
- @example
- javax.swing.text.AbstractDocument:Content
- @end example
- Alternatively, you can use the internal JVM class name:
- @example
- javax.swing.text.AbstractDocument$Content
- @end example
- @subsubheading Why does Kawa's REPL use display rather than write?
- The read-eval-print-loop of most Scheme implementations prints the
- evaluation result using @code{write}, while Kawa uses @code{display} by default.
- First note that it is easy to override the default with the
- @code{--output-format} command-line option:
- @example
- $kawa --output-format readable-scheme
- #|kawa:1|# "abc"
- "abc"
- @end example
- The reason @code{display} is the default is because of a vision of the REPL
- console as more than just printing out Scheme objects in
- textual form for use by a programmer.
- Some examples:
- @itemize
- @item
- A math program can display equations and graphs as the
- output of an expression.
- @item
- An expression can evaluate to a``@ref{Composable pictures,picture}''
- which would be displayed inline.
- @item
- An HTML/XML obj can be insert into the output in visual
- form if the console understands HTML. (There is a prototype
- for this that works by using the JavaFX WebView as the display.)
- @item
- The plan for "Kawa-shell" functionality is to have expressions
- that evaluate to process objects, which would be lazy strings.
- This string would be the data from standard output. Thus the
- effect of displaying a process object would be to print out
- the standard output - just like a regular shell. Users would
- find it confusing/annoying if shell output used quotes.
- @end itemize
- This "repl-as-pad" model doesn't work as well if the repl
- uses @code{write} rather than @code{display}.
- @node Framework
- @chapter The Kawa language framework
- Kawa is a framework written in Java for implementing
- high-level and dynamic languages, compiling them into Java bytecodes.
- The Kawa distributions includes of other programming languages
- besides Scheme,
- including @uref{../qexo/index.html, XQuery (Qexo)}
- and @uref{http://JEmacs.sourceforge.net/,Emacs Lisp (JEmacs)}.
- For a technical overview of Kawa, see these
- @uref{http://www.gnu.org/software/kawa/internals/index.html}.
- Javadoc generated @uref{http://www.gnu.org/software/kawa/api/,documentation of the Kawa classes} is also available.
- The packages
- @uref{http://www.gnu.org/software/kawa/api/gnu/bytecode/package-summary.html,@code{gnu.bytecode}},
- @uref{http://www.gnu.org/software/kawa/api/gnu/math/package-summary.html,@code{gnu.math}},
- @uref{http://www.gnu.org/software/kawa/api/gnu/lists/package-summary.html,@code{gnu.lists}},
- @uref{http://www.gnu.org/software/kawa/api/gnu/xml/package-summary.html,@code{gnu.xml}},
- @uref{http://www.gnu.org/software/kawa/api/gnu/expr/package-summary.html,@code{gnu.expr}},
- @uref{http://www.gnu.org/software/kawa/api/gnu/mapping/package-summary.html,@code{gnu.mapping}},
- and
- @uref{http://www.gnu.org/software/kawa/api/gnu/text/package-summary.html,@code{gnu.text}},
- are used by Kawa, and distributed with it, but may be independently useful.
- This @uref{gnu.bytecode/compiling-regexps.html,article} explains how to
- use @code{gnu.bytecode} to compile regular expressions to bytecode.
- @node License
- @chapter License
- @menu
- * Software-License:: License for the Kawa software
- * Manual-License:: License for the Kawa manual
- @end menu
- @node Software-License, Manual-License, , License
- @section License for the Kawa software
- The license for the Kawa software
- (except the optional JEmacs and BRL features - see below) is the
- @uref{http://opensource.org/licenses/mit-license.php, X11/MIT license}
- which is quoted below.
- @example
- The software (with related files and documentation) in these packages
- are copyright (C) 1996-2009 Per Bothner.
- Permission is hereby granted, free of charge, to any person obtaining
- a copy of this software and associated documentation files (the
- "Software"), to deal in the Software without restriction, including
- without limitation the rights to use, copy, modify, merge, publish,
- distribute, sublicense, and/or sell copies of the Software, and to
- permit persons to whom the Software is furnished to do so, subject to
- the following conditions:
- The above copyright notice and this permission notice shall be
- included in all copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- @end example
- In the past the Kawa license was a ``modified GNU GPL (General
- Public License)".
- If you find any files that contain the old license or otherwise seem
- to contradict the new license, please report that as a bug.
- Some of the JEmacs files are based on Emacs and have a GPL license,
- which is incompatible with non-Free (proprietary) products. For that
- reason, the @code{gnu.jemacs.*} packages are not included any more in
- the standard @code{.jar}, or by default when building from source, to
- avoid surprises.
- To build JEmacs you have to specify the @code{configure}
- flag @code{--enable-jemacs} or the @code{ant} flag
- @code{-Denable-jemacs=true}.
- Some code in @code{gnu/brl} and @code{gnu/kawa/brl} is copyright
- Bruce R. Lewis and Eaton Vance Management,
- with a modified-GPL license: no restrictions if used
- unmodified, but otherwise the GPL applies.
- These packages are no longer included by default in Kawa builds,
- but have to be selected with the @code{configure}
- flag @code{--enable-brl} or the @code{ant} flag @code{-Denable-brl=true}.
- Kawa uses some math routines from fdlib's libf77,
- which have a AT&T Bell Laboratories and Bellcore copyright.
- See the source file @code{gnu/math/DComplex.java}.
- The sorting routine in @code{gnu.xquery.util.OrderedTuples}
- is a re-implementatiomn in Java of code copyrighted by
- Simon Tatham.
- Some of the Scheme code in @code{kawa/lib} and @code{gnu/kawa/slib}
- are copyright other parties, and may have slightly different
- license wording, but I believe none of then contradicts the
- main Kawa license or impose extra restrictions.
- Search for the word @code{copyright} in these directories.
- Some code has been converted from other languages, with permission.
- This includes the @code{rationalize} method
- in @code{gnu/math/RatNum.java}, based on an algorithm of Alan Bawden,
- as expressed by Marc Feeley in C-Gambit.
- The concepts and algorithm of @code{gnu/text/PrettyWriter.java}
- are converted from SBCL, which is in the public domain.
- @node Manual-License, , Software-License, License
- @section License for the Kawa manual
- Here is the copyright license for this manual:
- Copyright @copyright{} 1996, 1997, 1998, 1999, 2005 Per Bothner
- Permission is granted to make and distribute verbatim copies of
- this manual provided the copyright notice and this permission notice
- are preserved on all copies.
- Permission is granted to copy and distribute modified versions of this
- manual under the conditions for verbatim copying, provided that the entire
- resulting derived work is distributed under the terms of a permission
- notice identical to this one.
- Permission is granted to copy and distribute translations of this manual
- into another language, under the above conditions for modified versions,
- except that this permission notice may be stated in a translation approved
- by the author.
- Parts of this manual are copied from the @uref{http://www.r6rs.org/,R6RS}
- or @uref{http://www.r7rs.org/,R7RS}, which both state:
- @quotation
- We intend this report to belong to the entire Scheme community, and so
- we grant permission to copy it in whole or in part without fee. In
- particular, we encourage implementors of Scheme to use this report as
- a starting point for manuals and other documentation, modifying it as
- necessary.
- @end quotation
- Parts of this manual were derived from the SLIB manual,
- copyright @copyright{} 1993-1998 Todd R. Eigenschink and Aubrey Jaffer.
- Parts of this manual were derived from ISO/EIC 10179:1996(E)
- (Document Style and Specifical Language) - unknown copyright.
- This manual has quoted from SRFI-6 (Basic String Ports),
- which is Copyright (C) William D Clinger (1999). All Rights Reserved.
- This manual has quoted from SRFI-8 (receive: Binding to multiple values),
- which is Copyright (C) John David Stone (1999). All Rights Reserved.
- This manual has quoted from SRFI-9 (Defining Record Types)
- which is Copyright (C) Richard Kelsey (1999). All Rights Reserved.
- This manual has quoted from SRFI-11 (Syntax for receiving multiple values),
- which is Copyright (C) Lars T. Hansen (1999). All Rights Reserved.
- This manual has quoted from SRFI-25 (Multi-dimensional Array Primitives),
- which is Copyright (C) Jussi Piitulainen (2001). All Rights Reserved.
- This manual has quoted from SRFI-26 (Notation for Specializing
- Parameters without Currying),
- which is Copyright (C) Sebastian Egner (2002). All Rights Reserved.
- This manual has quoted from SRFI-39 (Parameter objects),
- which is Copyright (C) Marc Feeley (2002). All Rights Reserved.
- The following notice applies to SRFI-6, SRFI-8, SRFI-9, SRFI-11, SRFI-25,
- SRFI-26, and SRFI-39,
- which are quoted in this manual, but it does not apply to the manual as a whole:
- @quotation
- This document and translations of it may be copied and furnished to
- others, and derivative works that comment on or otherwise explain it or
- assist in its implementation may be prepared, copied, published and
- distributed, in whole or in part, without restriction of any kind,
- provided that the above copyright notice and this paragraph are included
- on all such copies and derivative works. However, this document itself
- may not be modified in any way, such as by removing the copyright notice
- or references to the Scheme Request For Implementation process or
- editors, except as needed for the purpose of developing SRFIs in which
- case the procedures for copyrights defined in the SRFI process must be
- followed, or as required to translate it into languages other than
- English.
- The limited permissions granted above are perpetual and will not be
- revoked by the authors or their successors or assigns.
- This document and the information contained herein is provided on an
- "AS IS" basis and THE AUTHOR AND THE SRFI EDITORS DISCLAIM ALL
- WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY
- WARRANTY THAT THE USE OF THE INFORMATION HEREIN WILL NOT INFRINGE ANY
- RIGHTS OR ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A
- PARTICULAR PURPOSE.
- @end quotation
- This manual has quoted from SRFI-69 (Basic hash tables),
- which is Copyright (C) Panu Kalliokoski (2005). All Rights Reserved.
- The following notice applies to SRFI-69,
- which is quoted in this manual, but it does not apply to the manual as a whole:
- @quotation
- Permission is hereby granted, free of charge, to any person obtaining
- a copy of this software and associated documentation files (the
- Software), to deal in the Software without restriction, including
- without limitation the rights to use, copy, modify, merge, publish,
- distribute, sublicense, and/or sell copies of the Software, and to
- permit persons to whom the Software is furnished to do so, subject to
- the following conditions:
- The above copyright notice and this permission notice shall be
- included in all copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS
- OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- @end quotation
- This manual has made use of text and examples from Dorai Sitaram's
- @code{pregexp} implementation. But not where the latter talks about
- @code{pregexp-xxx} functions; the manual also talks about
- the @code{regex-xxx} functions (which are similar but use a
- slightly different regular expression syntax).
- The @code{pregexp} distribution has the following @code{COPYING} file:
- @quotation
- Copyright (c) 1999-2005, Dorai Sitaram.
- All rights reserved.
- Permission to copy, modify, distribute, and use this work or
- a modified copy of this work, for any purpose, is hereby
- granted, provided that the copy includes this copyright
- notice, and in the case of a modified copy, also includes a
- notice of modification. This work is provided as is, with
- no warranty of any kind.
- @end quotation
- @c I'd prefer to call this node plain "Index", but that causes a clash
- @c with index.html when generating HTML on case-insenstive file systems.
- @node Overall Index, , License, Top
- @appendix Index
- @printindex cp
- @bye
|