avram.texinfo 502 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110
  1. \input texinfo @c -*-texinfo-*-
  2. @c %**start of header
  3. @setfilename avram.info
  4. @settitle avram - a virtual machine code interpreter
  5. @finalout
  6. @setchapternewpage odd
  7. @c %**end of header
  8. @set VERSION 0.13.0
  9. @ifinfo
  10. This file documents the @code{avram} command which is a virtual machine code interpreter
  11. Copyright (C) 2000, 2003, 2006-2010 Dennis Furey
  12. Permission is granted to make and distribute verbatim copies of
  13. this manual provided the copyright notice and this permission notice
  14. are preserved on all copies.
  15. @ignore
  16. Permission is granted to process this file through TeX and print the
  17. results, provided the printed document carries copying permission
  18. notice identical to this one except for the removal of this paragraph
  19. (this paragraph not being relevant to the printed manual).
  20. @end ignore
  21. Permission is granted to copy and distribute modified versions of this
  22. manual under the conditions for verbatim copying, provided that the entire
  23. resulting derived work is distributed under the terms of a permission
  24. notice identical to this one.
  25. Permission is granted to copy and distribute translations of this manual
  26. into another language, under the above conditions for modified versions,
  27. except that this permission notice may be stated in a translation approved
  28. by the Free Software Foundation.
  29. @end ifinfo
  30. @titlepage
  31. @title Avram
  32. @subtitle a virtual machine code interpreter
  33. @subtitle for avram Version @value{VERSION}
  34. @author by Dennis Furey
  35. @page
  36. @vskip 0pt plus 1filll
  37. Copyright @copyright{} 2006, 2007 Dennis Furey
  38. Permission is granted to make and distribute verbatim copies of
  39. this manual provided the copyright notice and this permission notice
  40. are preserved on all copies.
  41. Permission is granted to copy and distribute modified versions of this
  42. manual under the conditions for verbatim copying, provided that the entire
  43. resulting derived work is distributed under the terms of a permission
  44. notice identical to this one.
  45. Permission is granted to copy and distribute translations of this manual
  46. into another language, under the above conditions for modified versions,
  47. except that this permission notice may be stated in a translation approved
  48. by the Free Software Foundation.
  49. @end titlepage
  50. @shortcontents
  51. @contents
  52. @c All the nodes can be updated using the EMACS command
  53. @c texinfo-every-node-update, which is normally bound to C-c C-u C-e.
  54. @node Top, Preface, (dir), (dir)
  55. @ifinfo
  56. This file documents @code{avram} version @value{VERSION}, which is a virtual
  57. machine code interpreter.
  58. @end ifinfo
  59. @c All the menus can be updated with the EMACS command
  60. @c texinfo-all-menus-update, which is normally bound to C-c C-u C-a.
  61. @menu
  62. * Preface:: project aims and scope
  63. * User Manual:: command line options and usage
  64. * Virtual Machine Specification:: a guide for compiler writers
  65. * Library Reference:: how to reuse or enhance @code{avram}
  66. * Character Table:: representations for ASCII characters
  67. * Reference Implementations:: constructive computability proofs
  68. * Changes:: recent updates to the manual
  69. * External Libraries:: specifications and calling conventions
  70. * Copying:: license terms
  71. * Function Index:: for the shared library API
  72. * Concept Index::
  73. @end menu
  74. @node Preface, User Manual, Top, Top
  75. @unnumbered Preface
  76. @code{avram} is a virtual machine code interpreter. It reads an input
  77. file containing a user-supplied application expressed in virtual machine
  78. code, and executes it on the host machine. The name is a quasi-acronym
  79. for ``Applicative ViRtuAl Machine''. Notable features are
  80. @cindex functional programming
  81. @cindex environment
  82. @cindex Unix
  83. @itemize @bullet
  84. @item strong
  85. support for functional programming operations (e.g., list processing)
  86. @item
  87. interfaces to selected functions from mathematical libraries, such as
  88. @itemize @bullet
  89. @item
  90. @code{gsl} (numerical integration, differentiation, and series acceleration)
  91. @url{http://www.gnu.org/software/gsl/}
  92. @item
  93. @code{mpfr} (arbitrary precision arithmetic)
  94. @url{http://www.mpfr.org}
  95. @item
  96. @code{minpack} (non-linear optimization)
  97. @url{http://ftp.netlib.org/minpack}
  98. @item
  99. @code{lapack} (linear algebra)
  100. @url{http://ftp.netlib.org/lapack}
  101. @item
  102. @code{fftw} (fast fourier transforms)
  103. @url{http://www.fftw.org}
  104. @item
  105. @code{Rmath} (statistical and transcendental functions)
  106. @url{http://www.r-project.org}
  107. @item
  108. @code{ufsparse} (sparse matrices)
  109. @url{http://www.cise.ufl.edu/research/sparse/SuiteSparse/current/SuiteSparse/}
  110. @item
  111. @code{glpk} (linear programming by the simplex method)
  112. @url{http://tech.groups.yahoo.com/group/lp_solve/}
  113. @item
  114. @code{lpsolve} (mixed integer linear programming)
  115. @url{http://www.llnl.gov/CASC/sundials/}
  116. @item
  117. @code{kinsol} (constrained non-linear optimization)
  118. @url{http://www.llnl.gov/CASC/sundials/}
  119. @end itemize
  120. @item interoperability of virtual code applications with other console
  121. applications or shells through the @code{expect} library
  122. @item a simple high-level
  123. interface to files, environment variables and command line parameters
  124. @item support for various styles of stateless or persistent stream
  125. processors (a.k.a. Unix filters)
  126. @end itemize
  127. The reason for writing @code{avram} was that I wanted to do some work
  128. using a functional programming language, didn't like any functional
  129. programming languages that already existed, and felt that it would be
  130. less trouble to write a virtual machine emulator than the back end of
  131. a compiler. As of version 0.1.0, the first public release of
  132. @code{avram} as such in 2000, most of the code base had been in heavy
  133. use by me for about four years, running very reliably. At this writing
  134. some six years later, it has seen even more use with rarely any
  135. reliability issues, in some cases attacking large combinatorial
  136. problems for weeks or months at a time. These problems have involved
  137. both long running continuous execution, and batches of thousands of
  138. shorter jobs.
  139. Although the virtual machine is biased toward functional programming,
  140. it is officially language agnostic, so @code{avram} may be useful to
  141. anyone involved in the development of compilers for other programming,
  142. scripting, or special purpose languages. The crucial advantage of
  143. using it in your own project is that rather than troubling over
  144. address modes, register allocation, and other hassles inherent in
  145. generating native code, your compiler can just dump a fairly high
  146. level intermediate code representation of the source text to a file,
  147. and let the virtual machine emulator deal with the details. The
  148. tradeoff for using a presumably higher level interpreted language is that the
  149. performance is unlikely to be competitive with native code, but this
  150. issue is mitigated in the case of numerical applications whose heavy
  151. lifting is done by the external libraries mentioned above.
  152. Portability is an added bonus. The virtual code is binary compatible
  153. across all platforms. Versions of @code{avram} as of 0.1.0 and later are
  154. packaged using GNU autotools and should be possible to build on any
  155. platform supporting them. In particular, the package is known to have
  156. built successfully on MacOS, FreeBSD, Solaris (thanks to the compile
  157. farm at Sourceforge.net) Digital Unix, and Debian GNU/Linux for i386 and
  158. Alpha platforms, although it has not been extensively tested on all of
  159. them. Earlier versions were compiled and run successfully on Irix and
  160. even Windows-NT (with @command{gcc}).
  161. This document is divided into three main parts, with possibly three
  162. different audiences, but they all depend on a basic familiarity with
  163. @cindex Unix
  164. Unix or GNU/Linux systems.
  165. @table @asis
  166. @item @ref{User Manual}
  167. essentially reproduces the information found in
  168. the manpage that is distributed with @code{avram} with a few extra
  169. examples and longer explanations. Properly deployed,
  170. @code{avram} should be almost entirely hidden from end users by wrapper
  171. scripts, so the ``users'' to whom this part is relevant would be
  172. those involved in preparing these scripts (a matter of choosing the right
  173. command line options). Depending on the extent to which this task is
  174. automated by a compiler, that may include the compiler writer or the
  175. developers of applications.
  176. @item @ref{Virtual Machine Specification}
  177. documents much of what one would need to know in order to write a
  178. compiler that generates code executable by @code{avram}. That includes
  179. the complete virtual machine code semantics and file formats. It would
  180. also be possible to implement a compatible replacement for @code{avram}
  181. from scratch based on the information in this chapter, in case anyone
  182. has anything against C, my coding style, or the GPL. (A few patches to
  183. make it @command{lint} cleanly or a new implementation in good
  184. pedagogical Java without pointers would both be instructive
  185. exercises. ;-))
  186. @cindex pointers
  187. @cindex Java
  188. @item @ref{Library Reference}
  189. includes documentation on the application program interface and
  190. recommended entry points for the C library distributed with
  191. @code{avram}. This information would be of use to those wishing to
  192. develop applications incorporating similar features, or to reuse the
  193. code for unrelated purposes. It might also be useful to anyone wishing
  194. to develop C or C++ applications that read or write data files in the
  195. format used by @code{avram}.
  196. @end table
  197. @node User Manual, Virtual Machine Specification, Preface, Top
  198. @chapter User Manual
  199. This chapter provides the basic information on how to use @code{avram}
  200. to execute virtual machine code applications.
  201. @code{avram} is invoked
  202. by typing a command at a shell prompt in one of these three forms.
  203. @display
  204. @kbd{avram} [@emph{general options}]
  205. @kbd{avram} [@emph{filter mode options}] @var{codefile}[@kbd{.avm}]
  206. @kbd{avram} [@emph{parameter mode options}] @var{codefile}[@kbd{.avm}] [@emph{parameters}]
  207. @end display
  208. @noindent
  209. In the second case, @code{avram} reads from standard input, and may
  210. of course appear as part of commands such as
  211. @cindex standard input
  212. @display
  213. @kbd{avram} [@emph{filter mode options}] @var{codefile}[@kbd{.avm}] < @var{inputfile}
  214. @var{anothercommand} | @kbd{avram} [@emph{filter mode options}] @var{codefile}[@kbd{.avm}]
  215. @end display
  216. @noindent
  217. When @code{avram} is invoked with the name of an input file (with a
  218. default extension @kbd{.avm}), it reads virtual machine code from the
  219. file and executes it on the host machine.
  220. @cindex functional programming
  221. The virtual code format used by @code{avram} is designed
  222. to support the features of functional or applicative programming
  223. languages. Although this chapter documents only the usage of
  224. @code{avram} and not the internals, it will be helpful to keep in mind
  225. that the virtual machine code expresses a mathematical function rather
  226. than a program in the conventional sense. As such, it performs no action
  227. directly, but may be applied in a choice of ways by the user of
  228. @code{avram} according to the precise operation required.
  229. The following sections provide information in greater detail about
  230. usage and diagnostics.
  231. @menu
  232. * General Options:: getting help and version information
  233. * Modes of Operation:: stream processing or file oriented
  234. * Filter Mode Options:: how to run a stream processor
  235. * Parameter Mode Options:: how to have an application use files
  236. * Command Line Syntax:: application-independent conventions
  237. * Diagnostics:: explanation of error messages
  238. * Security:: running untrusted applications
  239. * Example Script:: how to unburden the end users
  240. * Files:: miscellaneous files used
  241. * Environment:: environment variables
  242. * Bugs:: hall of shame
  243. @end menu
  244. @node General Options, Modes of Operation, User Manual, User Manual
  245. @section General Options
  246. Regardless of whatever other command line parameters are given,
  247. @code{avram} accepts the following parameters:
  248. @cindex help
  249. @cindex emulation
  250. @cindex @code{help} command line option
  251. @cindex @code{emulation} command line option
  252. @table @code
  253. @item -h, --help
  254. Show a summary of options and exit.
  255. @item -V,-v, --version
  256. Show the version of program and a short copyleft message and exit.
  257. @item --emulation=@var{version}
  258. Be backward compatible with an older version of @code{avram}. This
  259. option should include a valid version number, for example
  260. @kbd{@value{VERSION}}, which is the version of @code{avram} to be
  261. emulated. It can make virtual code applications future proof, assuming
  262. that future versions of @code{avram} correctly support backward
  263. compatibility. It may be used in conjunction with any other option in
  264. any mode of operation.
  265. @cindex web page
  266. @cindex home page
  267. @cindex url
  268. This copy of the user manual has not been updated since version @value{VERSION} of
  269. @code{avram}, so it is unable to document incompatibilities with later
  270. versions. The latest version of the manual may be found at
  271. @url{http://www.lsbu.ac.uk/~fureyd/avram}.
  272. @item -e, --external-libraries
  273. @cindex @code{external-libraries}
  274. Show a list of libraries with which @code{avram} has been linked and
  275. whose functions therefore could be called from virtual machine
  276. programs. This growing list currently includes selected functions from
  277. @code{fftw}, @code{glpk}, @code{gsl}, @code{kinsol}, @code{lapack},
  278. @code{minpack}, @code{mpfr}, @code{lpsolve}, @code{Rmath} and @code{ufsparse}
  279. (see @ref{Preface}) which are documented further in @ref{External Libraries}.
  280. @item -j, --jail
  281. @cindex @code{jail}
  282. This option disables execution of shell commands by virtual code
  283. applications, which is normally possible by default even for nominally
  284. non-interactive applications (see @ref{Parameter Mode Options}). A
  285. virtual code application attempting to spawn a shell (using the
  286. @code{interact} combinator) when this option is selected will
  287. encounter an exception rather than successful completion of the
  288. operation. This option is provided as a security feature for running
  289. untrusted code (see @ref{Security}), and is incompatible with
  290. @option{-i}, @option{-t}, and @option{-s}.
  291. @item -f, --force-text-input
  292. @cindex @code{force-text-input} command line option
  293. Normally @code{avram} will try to guess by looking at a file whether it
  294. is an ordinary text file or one that has been written in the virtual
  295. code file format, and choose a different internal representation
  296. accordingly. An application may require one representation or the
  297. other. This option tells @code{avram} to treat all input files other
  298. than the virtual code file (named in the first command line parameter)
  299. as text files regardless of whether or not it would be possible to
  300. interpret them otherwise. This option may be used in combination with
  301. any other option.
  302. @end table
  303. @node Modes of Operation, Filter Mode Options, General Options, User Manual
  304. @section Modes of Operation
  305. Apart from to the capability to print brief help messages and exit,
  306. there are two main modes of operation, depending on which options
  307. are specified on the command line before the virtual code file name.
  308. @cindex modes
  309. @cindex modes
  310. For the purpose of choosing the mode of operation, the virtual
  311. code filename is taken to be the first command line argument not
  312. beginning with a dash. Other conventions relevant to application
  313. specific parameters are detailed in @ref{Command Line Syntax}.
  314. @menu
  315. * Filter Mode::
  316. * Parameter Mode::
  317. @end menu
  318. @node Filter Mode, Parameter Mode, Modes of Operation, Modes of Operation
  319. @subsection Filter Mode
  320. @cindex filter mode
  321. @cindex modes
  322. In filter mode, the argument to the function given by the virtual code
  323. is taken from standard input, and the result is written to standard
  324. output, except for error messages resulting from a failure to evaluate
  325. the function, which are written to standard error.
  326. @xref{Diagnostics}. Filter mode is indicated whenever these three
  327. conditions are all met.
  328. @itemize @bullet
  329. @item
  330. Either at least one of the filter mode options appears on the command
  331. line preceding the first filename parameter, or there are no options at
  332. all. @xref{Filter Mode Options}.
  333. @item
  334. Exactly one filename parameter appears on the command line,
  335. which is the name of the virtual machine code file.
  336. @item
  337. Either the filename comes last on the command line, or the
  338. @option{--unparameterized} option precedes it, causing
  339. everything following it to be ignored.
  340. @end itemize
  341. @noindent
  342. Examples:
  343. @table @kbd
  344. @item avram mynewapp < inputfilename
  345. @cindex standard input
  346. In this example, filter mode is recognized by default because there are
  347. no options or input files on the command line to indicate
  348. otherwise. (The input file redirected into standard input is not treated
  349. by the shell as a command line argument.)
  350. @item cat somefile | avram -r coolprog > outputfile
  351. In this example, the @option{-r} option gives it away, being one of the
  352. filter mode options, in addition to the fact that there are no input
  353. file parameters or application-specific options.
  354. @item avram -u devilmaycare.avm --bogusoption ignoredparameter
  355. In this case, filter mode is forced by the @option{-u}
  356. option despite indications to the contrary.
  357. @end table
  358. @node Parameter Mode, , Filter Mode, Modes of Operation
  359. @subsection Parameter Mode
  360. @cindex parameter mode
  361. In parameter mode, the argument to the function given by the virtual
  362. code is a data structure containing environment variables and command
  363. line parameters including files, application specific options, and
  364. possibly standard input. The result obtained by evaluating the
  365. function is either a data structure representing a set of files to be
  366. written, which may include standard output, or a sequence of shell
  367. commands to be executed, or a combination of both. Parameter mode is
  368. indicated whenever either of these conditions is met.
  369. @itemize @bullet
  370. @item
  371. Any of the parameter mode options appears on the
  372. command line preceding the first filename parameter.
  373. @ifinfo
  374. @*
  375. @end ifinfo
  376. @xref{Parameter Mode Options}.
  377. @item
  378. At least one additional filename parameter or option follows the first
  379. filename parameter, and the option @option{--unparameterized} does not
  380. precede it.
  381. @end itemize
  382. @noindent
  383. Examples:
  384. @table @kbd
  385. @item avram --map-to-each-file prettyprinter.avm *.c *.h --extra-pretty
  386. In this example, parameter mode is indicated both by the parameter
  387. mode option @option{--map-to-each-file} and by the presence of input
  388. file names and the @option{--extra-pretty} option. The latter is
  389. specific to the hypothetical @code{prettyprinter.avm} virtual code
  390. application, as indicated by its position on the command line, and is
  391. therefore passed to it by @code{avram}.
  392. @item cat ~/specfile | avram reportgenerator -v - /var/log/syslog
  393. In this example, a hypothetical parameter mode application
  394. @code{reportgenerator} is able to read @file{~/specfile} from standard
  395. input because of the @code{-} used as a parameter.
  396. @item avram --parameterized grepenv
  397. In this example, a hypothetical application that searches shell
  398. variables is invoked in parameter mode even with no input files or
  399. application specific options, because of the @option{--parameterized}
  400. option. Parameter mode invocation is required by the application to give
  401. it access to the environment.
  402. @item avram grepenv --search-targets=PATH,MANPATH
  403. This example shows an application specific option with both a keyword
  404. and a parameter list. They suffice to indicate parameter mode without
  405. an explicit @option{--parameterized} option.
  406. @end table
  407. @node Filter Mode Options, Parameter Mode Options, Modes of Operation, User Manual
  408. @section Filter Mode Options
  409. The options available in filter mode are listed below. Except as
  410. otherwise noted, all options are mutually exclusive. Ordinarily a
  411. given application will require certain fixed settings of these options
  412. and will not work properly if they are set inappropriately.
  413. @table @code
  414. @item -r, @code{--raw-output}
  415. @cindex raw-output command line option
  416. Normally the result obtained by evaluating the function in the virtual
  417. code file must be a list of character strings, which is written as
  418. such to standard output. However, if this option is selected, the form
  419. of the result is unconstrained, and it will be written in a data file
  420. format that is not human readable but can be used by other
  421. applications. This option is incompatible with any other options
  422. except @option{-u}.
  423. @item -c, --choice-of-output
  424. @cindex @code{choice-of-output} command line option
  425. When this option is used, the evaluation of the function given by the
  426. virtual machine code will be expected to yield a data structure from
  427. which @code{avram} will ascertain whether standard output should be
  428. written in text or raw data format. This option should be used only
  429. if application is aware of it. It is incompatible with any other options
  430. except @option{-u}.
  431. @item -l, --line-map
  432. @cindex @code{line-map} command line option
  433. Normally the entire contents of standard input up to @code{EOF} are
  434. loaded into memory and used as the argument to the function in the
  435. virtual code file. However, this option causes standard input to be read
  436. a line at a time, with the function applied individually to each line,
  437. and its result in each case written immediately to standard output. A
  438. given application either requires this option or does not, and will not
  439. work properly in the alternative. This option implies
  440. @option{--force-text-input} and is incompatible with any other option except
  441. @option{-u}.
  442. @item -b, --byte-transducer
  443. @cindex @code{byte-transducer} command line option
  444. This option causes standard input to be read one character at a time,
  445. evaluating the function given by the virtual code file each time. The
  446. function is used as a state transition function that takes a state and
  447. input to a next state and output. The output is written concurrently
  448. with the input operations. A given application will not work properly
  449. with an inappropriate setting of this option. This option implies
  450. @option{--force-text-input} and is incompatible with any other option
  451. except @option{-u}.
  452. @item -u, --unparameterized
  453. @cindex @code{unparameterized} command line option
  454. Normally @code{avram} guesses whether to use filter mode or parameter
  455. mode depending on whether there are any parameters. Selecting this
  456. option forces it to operate in filter mode regardless. Any parameters
  457. that may appear on the command line after the virtual code file name
  458. are ignored. This option may be used in conjunction with any other
  459. filter mode option.
  460. @end table
  461. @node Parameter Mode Options, Command Line Syntax, Filter Mode Options, User Manual
  462. @section Parameter Mode Options
  463. @cindex parameter mode
  464. The parameter mode options are listed below. Except as otherwise
  465. noted, any combination of parameter mode options may be selected
  466. together, and except as noted, the settings of these options can be
  467. varied without breaking the application.
  468. @table @code
  469. @item -q, --quiet
  470. @cindex @code{quiet} command line option
  471. @code{avram} normally informs the user when writing an output file with
  472. a short message to standard output. This option suppresses such
  473. messages. This option is compatible with any application and any other
  474. parameter mode option except @option{-a}.
  475. @item -a, --ask-to-overwrite
  476. @cindex @code{ask-to-overwrite} command line option
  477. Selecting this option will cause @code{avram} to ask permission
  478. interactively before overwriting an existing file, and to refrain from
  479. overwriting it without permission, in which case the contents that
  480. were to be written will be lost. This option overrides @option{-q} and is
  481. compatible with any other parameter mode option or application.
  482. @item -.EXT
  483. @cindex @code{EXT} command line option
  484. @cindex default file extensions
  485. @cindex extensions
  486. @cindex file name extensions
  487. @cindex file name suffixes
  488. An option beginning with a dash followed by a period specifies a
  489. default extension for input file names. If @code{avram} doesn't find a
  490. file named on the command line, and the filename doesn't already
  491. contain a period, @code{avram} will try to find a file having a similar
  492. name but with the default extension appended. The default extension given
  493. by this option takes precedence over the hard coded default extensions
  494. of @kbd{.fun} and @kbd{.avm}. At most one default extension can be supplied. This
  495. option is compatible with any other parameter mode option and
  496. compatible with any application.
  497. @item -d, --default-to-stdin
  498. @cindex @code{default-to-stdin} command line option
  499. @cindex standard input
  500. If no filename parameter appears on the command line (other than the
  501. name of the virtual code file), this option directs @code{avram} to read
  502. the contents of standard input as if it were specified as a command line
  503. parameter. (Standard input can also be specified explicitly as a
  504. dash. See @ref{Command Line Syntax}.) This option is compatible with any
  505. application and any other parameter mode option except @option{-m}.
  506. @item -m, --map-to-each-file
  507. @cindex @code{map-to-each-file} command line option
  508. Normally @code{avram} loads the entire contents of all files named on
  509. the command line into memory so as to evaluate the virtual machine
  510. code application on all of them together. This option can be used to
  511. save memory in the case of applications that operate on multiple files
  512. independently. It causes @code{avram} to load only one file at a time
  513. and to perform the relevant evaluation and output before loading the
  514. next one. Application specific options and standard input (if
  515. specified) are read only once and reused. This option is incompatible
  516. with @option{-d}, and not necessarily compatible with all applications,
  517. although some may work both with and without it.
  518. @item -i, --interactive
  519. @cindex @code{interactive} command line option
  520. @cindex interactive applications
  521. This option is used in the case of applications that interact with
  522. other programs through shell commands. An application that is meant to
  523. be invoked in this way requires this option and will not work without
  524. it, nor will applications that are not of this type work with it. This
  525. option is implied by @option{-t} and @option{-s}, and is compatible with any
  526. other parameter mode option.
  527. @item -s, --step
  528. @cindex @code{step} command line option
  529. This option is used in the case of applications that interact with
  530. other programs through shell commands, similarly to @option{-i}, and
  531. can substitute for it (see above). The option has the additional
  532. effect of causing shell commands issued by @code{avram} on behalf of
  533. the application to be written with their results to standard output,
  534. and to cause @code{avram} to pause after displaying each shell command
  535. until a key is pressed. This capability may be useful for debugging
  536. or auditing purposes but does not otherwise alter the effects of the
  537. application. This option is compatible with any other parameter mode
  538. option.
  539. @item -t, --trace
  540. @cindex @code{trace} command line option
  541. This option is used in the case of applications that interact with
  542. other programs through shell commands, but only by way of the
  543. @code{interact} combinator, for which it provides developers a means
  544. of low level debugging, particularly deadlock detection. When this
  545. option is selected, a verbose trace of all characters exchanged
  546. between the functional transducer and the external application are
  547. written to standard output, along with some additional control flow
  548. diagnostics. This option is compatible with any other parameter mode
  549. option.
  550. @item -p, --parameterized
  551. @cindex @code{parameterized} command line option
  552. Normally @code{avram} tries to guess whether to operate in filter mode
  553. or parameter mode based on the options used and the parameters. If
  554. there are no parameters and no options, it will default to filter
  555. mode, and try to read standard input. However, if this option is
  556. selected, it will use parameter mode (and therefore not try to read
  557. standard input unless required).
  558. @end table
  559. @node Command Line Syntax, Diagnostics, Parameter Mode Options, User Manual
  560. @section Command Line Syntax
  561. @cindex command line
  562. The command line parameters that follow the virtual code file name
  563. when @code{avram} is used in parameter mode (@ref{Parameter Mode}) are dependent on the
  564. specific application. However, all supported applications are
  565. constrained for implementation reasons to observe certain uniform
  566. conventions regarding their command line parameters, which are
  567. documented here to avoid needless duplication.
  568. @cindex shell
  569. @cindex file parameters
  570. @cindex input files
  571. The shell divides the command line into "arguments" separated by white
  572. space. Arguments containing white space or special characters used by
  573. the shell must be quoted or protected as usual. File names with wild
  574. cards in them are expanded by the shell before @code{avram} sees them.
  575. @code{avram} then extracts from the sequence of arguments a sequence of
  576. filenames and a sequence of options. Each option consists of a keyword and an
  577. optional parameter list. Filenames, keywords, and parameter lists are
  578. distinguished according to the following criteria.
  579. @enumerate
  580. @item
  581. An argument is treated as a keyword iff it meets these three
  582. conditions.
  583. @enumerate a
  584. @item
  585. It starts with a dash.
  586. @item
  587. It doesn't contain an equals sign.
  588. @item
  589. It doesn't consist solely of a dash.
  590. @end enumerate
  591. @item
  592. An argument is treated as a parameter list iff it meets these four
  593. conditions.
  594. @enumerate a
  595. @item
  596. It doesn't begin with a dash.
  597. @item
  598. It either begins with an equals sign or doesn't contain one.
  599. @item
  600. It immediately follows an argument beginning with a dash, not
  601. containing an equals sign and not consisting solely of a dash.
  602. @item
  603. At least one of the following is true.
  604. @enumerate
  605. @item
  606. It doesn't contain a period, tilde, or path separator.
  607. @cindex path separators
  608. @item
  609. It contains a comma.
  610. @item
  611. It can be interpreted as a C formatted floating point number.
  612. @end enumerate
  613. @end enumerate
  614. @item
  615. An argument is treated as an input file name iff it meets these four
  616. conditions.
  617. @enumerate a
  618. @item
  619. It doesn't begin with a dash.
  620. @item
  621. It doesn't contain an equals sign.
  622. @item
  623. It doesn't contain a comma.
  624. @item
  625. At least one of the following is true.
  626. @enumerate
  627. @item
  628. It contains a period, tilde, or path separator.
  629. @item
  630. It doesn't immediately follow an argument beginning with a dash, not
  631. consisting solely of a dash, and not containing an equals sign.
  632. @end enumerate
  633. @end enumerate
  634. @item
  635. If an argument contains an equals sign but doesn't begin with one,
  636. the part on the left of the first equals sign is treated as a
  637. keyword and the part on the right is treated as a parameter list.
  638. @item
  639. An argument consisting solely of a dash is taken to represent the
  640. standard input file.
  641. @item
  642. An argument not fitting any of the above classifications is an error.
  643. @end enumerate
  644. These conventions are needed for @code{avram} to detect input file names
  645. in a general, position independent way, so that it can preload the files
  646. on behalf of the application. Many standard Unix utilities follow these
  647. @cindex Unix
  648. conventions to a large extent, the exceptions being those that employ
  649. non-filename arguments without distinguishing syntax, and use positional
  650. or other ad hoc methods of command line interpretation. A drop-in
  651. replacement for such an application could nevertheless be implemented
  652. using @code{avram} with an appropriate wrapper script, similar to the
  653. approach recommended in @ref{Example Script}, but with suitable keywords
  654. inserted prior to the ambiguous arguments.
  655. @node Diagnostics, Security, Command Line Syntax, User Manual
  656. @section Diagnostics
  657. @cindex diagnostics
  658. @cindex error messages
  659. @cindex run time errors
  660. The means exists for virtual code applications to have run time error
  661. messages written to standard error on their behalf by
  662. @code{avram}. Any error messages not documented here originate with an
  663. application and should be documented by it.
  664. Most error messages originating from @code{avram} are prefaced by the
  665. application name (i.e., the name of the file containing the virtual
  666. machine code), but will be prefaced by @code{avram:} if the error is
  667. caused by a problem loading this file itself. Error messages originating
  668. from virtual code applications are the responsibility of their
  669. respective authors and might not be prefaced by the application name.
  670. The run time errors not specifically raised by the application can be
  671. classified as internal errors, i/o errors, overflow errors, file
  672. format errors, application programming errors, and configuration related
  673. errors.
  674. Some error messages include a code number. The number identifies
  675. the specific point in the source code where the condition was
  676. detected, for the benefit of the person maintaining it.
  677. @menu
  678. * Internal Errors::
  679. * i/o Errors::
  680. * Overflow Errors::
  681. * File Format Errors::
  682. * Application Programming Errors::
  683. * Configuration Related Errors::
  684. * Other Diagnostics and Warnings::
  685. @end menu
  686. @node Internal Errors, i/o Errors, Diagnostics, Diagnostics
  687. @subsection Internal Errors
  688. @cindex internal errors
  689. Internal errors should never occur unless the
  690. @code{avram} source code has been carelessly modified, except as noted
  691. in @ref{Bugs}. There are two kinds.
  692. @table @code
  693. @item @var{application-name}: virtual machine internal error (code @var{nn})
  694. Most internal errors would be reported by a message of this form
  695. if they were to occur. It indicates that some required invariant was
  696. not maintained. In such cases, the program terminates immediately, and
  697. any results already produced are suspect.
  698. @item @var{application-name}: @var{nn} unreclaimed @var{struct-names}
  699. A message of this form could be printed at the end of an otherwise successful
  700. run. @code{avram} maintains a count of the number of units allocated
  701. for various data structures, and checks that they are all reclaimed
  702. eventually as a safeguard against memory leaks. This message indicates
  703. that some memory remains unaccounted for.
  704. @end table
  705. @cindex bug reports
  706. @cindex email
  707. @cindex author
  708. If a repeatable internal error is discovered, please email a bug
  709. report and a small representative test case to the author at
  710. @email{avram-support@@basis.uklinux.net}. Include the version number of @code{avram},
  711. which you can get by running @kbd{avram --version}.
  712. @node i/o Errors, Overflow Errors, Internal Errors, Diagnostics
  713. @subsection i/o Errors
  714. @cindex i/o errors
  715. These error messages are prefaced with the name of the
  716. application. A further explanation as to the
  717. @cindex @code{strerror}
  718. reason, obtained from the standard @code{strerror()} utility, is
  719. appended to the messages below if possible.
  720. @table @code
  721. @item @var{application-name}: can't read @var{filename}
  722. @cindex file names
  723. @cindex @code{can't read}
  724. @cindex environment
  725. @cindex @code{AVMINPUTS}
  726. A file was not able to be opened for reading, typically because it was
  727. not found or because the user does not have permission. The file name is
  728. displayed with special characters expanded but without any default
  729. extensions or search paths that may have been tried. If you think a file
  730. exists and should have been found, there may be a problem with your
  731. @env{AVMINPUTS} environment variable (@ref{Environment}).
  732. @item @var{application-name}: can't write @var{filename}
  733. @cindex @code{can't write}
  734. A file was not able to be opened for writing.
  735. @item @var{application-name}: can't write to @var{filename}
  736. A file was successfully opened for writing but became impossible to
  737. write thereafter.
  738. @item @var{application-name}: can't spawn @var{command}
  739. @cindex shell
  740. @cindex expect
  741. @cindex libexpect
  742. @cindex @code{can't spawn}
  743. @cindex @code{exp_popen}
  744. @cindex spawning processes
  745. An attempt to execute a shell command on behalf of an interactive
  746. application failed during the @code{exp_popen()} call to the
  747. @code{libexpect} library.
  748. @item @var{application-name}: can't close @var{filename}
  749. @cindex @code{can't close}
  750. A call to the standard C procedure @code{fclose()} failed due to
  751. unforeseen circumstances. The error is non-fatal but the file should be
  752. checked for missing data.
  753. @end table
  754. @node Overflow Errors, File Format Errors, i/o Errors, Diagnostics
  755. @subsection Overflow Errors
  756. These errors are reported by the application name prefacing one of the
  757. following messages, except as noted below.
  758. @cindex overflow
  759. @cindex counter overflow
  760. @cindex memory overflow
  761. @table @code
  762. @item @var{application-name}: counter overflow (code @var{nn})
  763. An overflow occurred in an unsigned long integer being used as a reference
  764. counter or something similar. This situation is very unlikely.
  765. @item @var{application-name}: memory overflow (code @var{nn})
  766. There wasn't enough memory to build an internal data structure. The
  767. most likely cause is an attempt to operate on input
  768. files that are too large. Standard remedies apply.
  769. @end table
  770. The memory overflow or counter overflow messages can also be reported
  771. without the application name preface or a code number. In these cases,
  772. they arise in the course of evaluating the function given by the
  773. application, rather than by loading the input files.
  774. A counter overflow in this case is possible if the application
  775. attempts to compute the size of a very large, shared structure using
  776. native integer arithmetic.
  777. @cindex @command{ulimit}
  778. Memory overflows are possible due to insufficient memory for a valid
  779. purpose, but may also occur due to a non-terminating recursion in the
  780. virtual machine code. To prevent thrashing or other bad effects from
  781. runaway code, the @command{ulimit} shell command is your friend.
  782. @node File Format Errors, Application Programming Errors, Overflow Errors, Diagnostics
  783. @subsection File Format Errors
  784. Certain application crashes result from an application not adhering
  785. to the required conventions about data and file formats, or because the
  786. application was invoked in the wrong mode (@ref{Modes of Operation}). These
  787. are the following.
  788. @table @code
  789. @item @var{application-name}: invalid text format (code @var{nn})
  790. @cindex @code{invalid text format}
  791. An application that was expected to return a string of characters to
  792. be written to a text file returned data that did not correspond to any
  793. valid character representation.
  794. @item @var{application-name}: null character in prompt
  795. An interactive application (invoked rightly or wrongly with @option{-i},
  796. @option{-t}, or @option{-s}) is required to exchange strings of non-null
  797. characters internally with @code{avram}, and used a null.
  798. @item @var{application-name}: invalid file name (code @var{nn})
  799. The data structure representing a file obtained from an application
  800. has a name consisting of something other than character strings. This
  801. error could be the result of a filter mode application (@ref{Filter
  802. Mode}) being invoked in parameter mode.
  803. @ifinfo
  804. @*
  805. @end ifinfo
  806. (@ref{Parameter Mode})
  807. @item @var{application-name}: null character in file name
  808. @cindex @code{null character in file name}
  809. Similar to the above errors.
  810. @item @var{application-name}: bad character in file name
  811. @cindex @code{bad character in file name}
  812. Slashes, backslashes, and unprintable characters other than spaces are
  813. also prohibited in file names.
  814. @item @var{application-name}: invalid output preamble format
  815. @cindex @code{invalid output preamble format}
  816. According the format used by @code{avram} for data files, a data file
  817. may contain an optional text portion, known as the preamble. This
  818. error occurs when a data file obtained from an application can not be
  819. written because the preamble is something other than a list of
  820. character strings.
  821. @item @var{application-name}: invalid file specification
  822. @cindex @code{invalid file specification}
  823. This error occurs in situations where the data structure for a file
  824. obtained by evaluating the application is too broken to permit any
  825. more specific diagnosis.
  826. @item avram: invalid raw file format in @var{application-name}
  827. @cindex @code{invalid raw file format}
  828. The file containing the virtual machine code was not able to be
  829. loaded, because the code was not in a recognizable format. Either the
  830. file has become corrupted, the compiler that generated it has a
  831. bug in it, or the wrong file was used as a virtual code file.
  832. @end table
  833. @node Application Programming Errors, Configuration Related Errors, File Format Errors, Diagnostics
  834. @subsection Application Programming Errors
  835. A further class of application crashes results from miscellaneous bugs
  836. in the application. These require the application to be debugged and
  837. have no user level explanation or workaround, but are listed here for
  838. reference. These messages are not normally prefaced by the application
  839. name when reported unless the application elects to do so, except for
  840. the @code{invalid profile identifier} message.
  841. @itemize @bullet
  842. @item @code{invalid recursion}
  843. @cindex @code{invalid recursion}
  844. @item @code{invalid comparison}
  845. @cindex @code{invalid comparison}
  846. @item @code{invalid deconstruction}
  847. @cindex @code{invalid deconstruction}
  848. @item @code{invalid transpose}
  849. @cindex @code{invalid transpose}
  850. @item @code{invalid membership}
  851. @cindex @code{invalid membership}
  852. @item @code{invalid distribution}
  853. @cindex @code{invalid distribution}
  854. @item @code{invalid concatenation}
  855. @cindex @code{invalid concatenation}
  856. @item @code{invalid assignment}
  857. @cindex @code{invalid assignment}
  858. @item @code{unrecognized combinator (code @var{nn})}
  859. @cindex @code{unrecognized combinator}
  860. @item @code{@var{application-name}: invalid profile identifier}
  861. @cindex @code{invalid profile identifier}
  862. @item @code{unsupported hook}
  863. @cindex @code{unsupported hook}
  864. @end itemize
  865. @node Configuration Related Errors, Other Diagnostics and Warnings, Application Programming Errors, Diagnostics
  866. @subsection Configuration Related Errors
  867. The source code distribution of @code{avram} incorporates a flexible
  868. configuration script allowing it to be installed on a variety of
  869. platforms. Not all platforms allow support for all features. It is also
  870. anticipated that new features may be added to @code{avram} from time to
  871. time. Some problems may therefore occur due to features not being
  872. supported at your site for either of these reasons. The following error
  873. messages are relevant to these situations.
  874. @table @code
  875. @item unsupported hook
  876. @cindex @code{unsupported hook}
  877. If it's not simply due to an application programming error
  878. (@ref{Application Programming Errors}) this message may be the result of
  879. trying to use an application that requires a newer version of
  880. @code{avram} than the one installed, even though applications should
  881. avoid this problem by checking the version number at run time. If this
  882. is the reason, the solution would be to install the latest version.
  883. @item @var{application-name}: I need avram linked with @var{foo}, @var{bar} and @var{baz}.
  884. @cindex @code{I need avram linked with}
  885. A message of the this form indicates that a new installation may be
  886. needed. At this writing (11/11/1), @code{avram} may report this message
  887. with respect to @code{libexpect5.32}, @code{tcl8.3}, and @code{libutil}
  888. if any of the @option{-i}, @option{-t}, or @option{-s} options is used on a
  889. system where not all of these libraries were detected when @code{avram}
  890. was installed from a source distribution. (See @ref{Parameter Mode Options}.)
  891. Because @code{avram} is useful even without interactive applications,
  892. these libraries are not considered absolute prerequisites by the
  893. configuration script.
  894. @item avram: can't emulate version @var{version}
  895. @cindex @code{can't emulate version}
  896. @cindex @code{emulation} command line option
  897. @cindex versions
  898. @cindex backward compatibility
  899. The @option{--emulation=@var{version}} option obviously won't work if
  900. the requested version is newer than the installed version, or if it is
  901. not a valid version number (@ref{General Options}). When that happens,
  902. this message is printed instead and @code{avram} terminates.
  903. @item avram: multiple version specifications
  904. @cindex @code{multiple version specifications}
  905. The @option{--emulation=@var{version}} option can be used at most once
  906. on a command line. This message is printed if it is used more than
  907. once. If you only typed it once and got this message, check your aliases
  908. and wrapper scripts before reporting a bug.
  909. @item avram: unrecognized option: @var{option-name}
  910. @cindex @code{unrecognized option}
  911. may mean that a command line option has been misspelled, or may be another
  912. sign of an obsolete version of @code{avram}. This message will be followed
  913. by a usage summary similar to that of the @option{--help} option.
  914. (@ref{General Options}).
  915. @item @var{application-name}: warning: search paths not supported
  916. @cindex search paths
  917. @cindex paths
  918. @cindex environment
  919. @cindex @code{AVMINPUTS}
  920. @cindex @code{search paths not supported}
  921. @cindex @file{argz.h}
  922. If the @file{argz.h} header file was not detected during configuration,
  923. @code{avram} will not be able to support search paths in the
  924. @env{AVMINPUTS} environment variable (@ref{Environment}). This message
  925. is a warning that the environment variable is being ignored. If the
  926. warning is followed by an i/o error
  927. @ifinfo
  928. @*
  929. @end ifinfo
  930. (@ref{i/o Errors}), the latter may be due to a file being in a path that
  931. was not searched for this reason. A workaround is to specify the full
  932. path names of all input files outside the current working directory. If
  933. you don't need search paths, you can get rid of this message by
  934. undefining @env{AVMINPUTS}.
  935. @end table
  936. @node Other Diagnostics and Warnings, , Configuration Related Errors, Diagnostics
  937. @subsection Other Diagnostics and Warnings
  938. @table @asis
  939. @item @code{avram: multiple -.EXT options; all but last ignored}
  940. @cindex @code{multiple -.EXT options}
  941. @cindex extensions
  942. @cindex file extensions
  943. @cindex extensions
  944. @cindex file name extensions
  945. @cindex file name suffixes
  946. This message is written when more than one default extension is given as
  947. a command line parameter. At most one default extension is allowed. If
  948. more than one is given, only the last one is used. The error is
  949. non-fatal and @code{avram} will try to continue. If you need more
  950. than one default extension, consider using the hard coded default
  951. extensions of @file{.fun} and @file{.avm}, or hacking the shell script
  952. in which the @code{avram} command line appears.
  953. @item @code{@var{application name}: empty operator}
  954. This message probably means that the virtual code file is corrupt or
  955. invalid.
  956. @item usage summary
  957. @cindex @code{help} command line option
  958. For any errors in usage not covered by other diagnostics, such as
  959. incompatible combinations of options, @code{avram} prints a message to
  960. standard error giving a brief summary of options, similar to the output
  961. from @kbd{avram --help}. (See @ref{General Options}.)
  962. @end table
  963. @node Security, Example Script, Diagnostics, User Manual
  964. @section Security
  965. @cindex security
  966. A few obvious security considerations are relevant to running untrusted
  967. virtual code applications. These points are only as reliable as the
  968. assumption that the @code{avram} executable has not been modified to the
  969. contrary.
  970. @itemize @bullet
  971. @cindex filter mode
  972. @item The applications with the best protection from malicious code are
  973. those that run in filter mode, because they have no access to any
  974. information not presented to them in standard input, nor the ability to
  975. affect anything other than the contents of standard output (provided that
  976. the @code{--jail} command line option is used). The worst
  977. they can do is use up a lot of memory, which can be prevented with the
  978. @command{ulimit} command. Unfortunately, not all applications are usable
  979. in this mode.
  980. @item Parameter mode applications that do not involve the @option{-i},
  981. @cindex parameter mode
  982. @cindex standard input
  983. @option{-t} or @option{-s} options are almost as safe (also assuming
  984. @code{--jail}). They have (read-only) access to environment variables, and to the files that are
  985. indicated explicitly on the command line. If standard input is one of
  986. the files (as indicated by the use of @code{-} as a parameter), the
  987. virtual code application may infer the current date and time. However,
  988. a parameter mode application may write any file that the user has
  989. permission to write. The @option{--ask-to-overwrite} option should be
  990. used for better security, or at least the @option{--quiet} option should
  991. not be used. The virtual code can neither override nor detect the use
  992. of these options.
  993. @item Interactive parameter mode applications (those that use either the
  994. @cindex interactive applications
  995. @option{-i}, @option{-t} or @option{-s} options) are the least secure
  996. because they can execute arbitrary shell commands on behalf of the
  997. user. This statement also applies to filter mode and parameter mode
  998. applications where the @option{--jail} option is not used. Use of
  999. @option{--step} is preferable to @option{-i} for making an audit
  1000. trail of all commands executed, but the application could probably
  1001. subvert it. The @option{--step} option may be slightly better because
  1002. it can allow the user to inspect each command and interrupt it if
  1003. appropriate. However, in most cases a command will not be displayed
  1004. until it is already executed. Commands executed by non-interactive
  1005. applications normally will display no output to that effect. A
  1006. @command{chroot} environment may be the only secure way of running
  1007. untrusted interactive applications.
  1008. @end itemize
  1009. @node Example Script, Files, Security, User Manual
  1010. @section Example Script
  1011. @cindex script
  1012. @cindex shell script
  1013. It is recommended that the application developer (or the compiler)
  1014. package virtual machine code applications as shell scripts with the
  1015. @code{avram} command line embedded in them. This style relieves the user
  1016. of the need to remember the appropriate virtual machine options for
  1017. invoking the application, which are always the same for a given
  1018. application, or even to be aware of the virtual machine at all.
  1019. @cindex @code{cat}
  1020. @cindex @code{default-to-stdin} command line option
  1021. Here is a script that performs a similar operation to the standard
  1022. @cindex Unix
  1023. Unix @command{cat} utility.
  1024. @example
  1025. #!/bin/sh
  1026. #\
  1027. exec avram --force-text-input --default-to-stdin "$0" "$@@"
  1028. sKYQNTP\
  1029. @end example
  1030. @noindent
  1031. That is, it copies the contents of a file whose
  1032. name is given on the command line to standard output, or copies
  1033. standard input to standard output if no file name is given. This
  1034. script can be marked executable
  1035. @cindex executable files
  1036. (with @command{chmod}) and run by any user
  1037. @cindex @code{chmod}
  1038. @cindex paths
  1039. with the directory of the @code{avram} executable in his or her
  1040. @code{PATH} environment variable, even if @code{avram} had to be
  1041. installed in a non-standard directory such as
  1042. @cindex non-standard installation
  1043. @file{~/bin}.
  1044. The idea for this script is blatantly lifted from the @command{wish}
  1045. @cindex @code{wish}
  1046. manpage. The first line of the script invokes a shell to process
  1047. what follows. The shell treats the second line as a comment and
  1048. ignores it. Based on the third line, the shell invokes @code{avram}
  1049. with the indicated options, the script itself as the next argument, and
  1050. whatever command line parameters were initially supplied by the user
  1051. as the remaining arguments. The rest of the script after
  1052. that line is never processed by the shell.
  1053. When @code{avram} attempts to load the shell script as a virtual
  1054. machine code file, which happens as a result of it being executed by
  1055. the shell, it treats the first line as a comment and ignores it. It
  1056. also treats the second line as a comment, but takes heed of the
  1057. trailing backslash, which is interpreted as a comment continuation
  1058. character. It therefore also treats the third line as a comment and
  1059. ignores it. Starting with the fourth line, it reads the virtual code,
  1060. which is in a binary data format encoded with printable characters,
  1061. and evaluates it.
  1062. @node Files, Environment, Example Script, User Manual
  1063. @section Files
  1064. @table @code
  1065. @item ./profile.txt
  1066. @cindex @file{profile.txt}
  1067. This file is written automatically by @code{avram} on behalf of
  1068. applications that include profile annotations. It lists the number of
  1069. invocations for each annotated part of the application, the total
  1070. amount of time spent on it (in relative units), the average amount of
  1071. time for each invocation, and the percentage of time relative to the
  1072. remainder of the application. The exact format is undocumented and
  1073. subject to change.
  1074. @end table
  1075. @node Environment, Bugs, Files, User Manual
  1076. @section Environment
  1077. @cindex environment
  1078. @cindex @code{AVMINPUTS}
  1079. @cindex paths
  1080. An environment variable @env{AVMINPUTS} can be made to store a list of
  1081. directories (using the @command{set} or @command{export} commands) that
  1082. @code{avram} will search for input files. The directories should be
  1083. separated by colons, similarly to the @env{PATH} environment variable.
  1084. @cindex search paths
  1085. The search paths in @env{AVMINPUTS} apply only to the names of input
  1086. files given on the command line (@ref{Command Line Syntax}) when
  1087. @code{avram} is invoked in parameter mode (@ref{Parameter Mode}). They
  1088. do not apply to the name of the virtual code file, which is always
  1089. assumed to be either absolute or relative to the current working
  1090. directory (this assumption being preferable in the case of a script like
  1091. that of @ref{Example Script}).
  1092. @cindex shell script
  1093. Starting in the first directory in the list of @env{AVMINPUTS},
  1094. @code{avram} searches for a file exactly as its name appears on
  1095. the command line (subject to the expansion of special characters
  1096. by the shell). If it is not found and the name does not contain a
  1097. period, but a command line option of @option{-.EXT} has been used,
  1098. @code{avram} will then search for a file with that name combined with
  1099. the extension @code{.EXT}. If @option{-.EXT} has not been used or if no
  1100. @cindex @code{EXT} command line option
  1101. matching file is found with it, @code{avram} tries the extensions of
  1102. @kbd{.avm} and @kbd{.fun} in that order, provided the given file name
  1103. contained no periods. If no match is found for any of those cases,
  1104. @code{avram} proceeds to search the next directory in the list obtained
  1105. from @env{AVMINPUTS}, and so on. It stops searching when the first
  1106. match is found. For subsequent input files, the search begins again at
  1107. the first directory.
  1108. If @env{AVMINPUTS} is defined, the current working directory is not
  1109. searched for input files unless it is listed. If it is empty or not
  1110. @cindex search paths
  1111. defined, a default list of search paths is used, currently
  1112. @example
  1113. .:/usr/local/lib/avm:/usr/lib/avm:/lib/avm:/opt/avm:/opt/lib/avm\
  1114. :/usr/local/share/avm:/usr/share/avm:/share/avm:/opt/avm:/opt/share/avm
  1115. @end example
  1116. @noindent
  1117. These paths are defined in @code{avram.c} and can be changed
  1118. by recompiling it.
  1119. @node Bugs, , Environment, User Manual
  1120. @section Bugs
  1121. @cindex internal errors
  1122. @cindex bugs
  1123. @cindex exceptions
  1124. There are no known bugs outstanding, except for any that may be
  1125. inherent in the external library functions. However, @code{avram} has
  1126. been used most extensively on GNU/Linux systems, and the prospect
  1127. of portability issues with new or lesser used features on other
  1128. systems can't be excluded.
  1129. Though not observed in practice, it's theoretically possible to blow
  1130. the stack by passing enough functions as arguments to library
  1131. functions that pass more functions to library functions (e.g., by
  1132. using nested calls to the gsl integration functions meant for a single
  1133. variable to evaluate a very high dimensional multiple integral). In
  1134. all other cases only dynamic heap storage or a constant amount of
  1135. stack space is used. In particular, this issue is @emph{not} relevant
  1136. to virtual code applications that don't use external libraries, or
  1137. that don't pass functions to them as arguments.
  1138. @code{avram} is designed to recover gracefully from memory overflows
  1139. by always checking for @code{NULL} results from @code{malloc()} or
  1140. otherwise trapping functions that allocate memory. In the event of an
  1141. overflow, it conveys an appropriate error message to the virtual code
  1142. application to be handled by the usual exception handling
  1143. mechanisms. However, there is currently no way for a virtual code
  1144. application to detect in advance whether sufficient memory is
  1145. available, nor for it to resume normal operation once an exception
  1146. occurs. Furthermore, it has been observed on some systems including
  1147. Irix and 2.4 series Linux kernels that the @code{avram} process is
  1148. killed automatically for attempting to allocate too much memory rather
  1149. than given the chance to recover.
  1150. Please send bug reports to @email{avram-support@@basis.uklinux.net}.
  1151. @node Virtual Machine Specification, Library Reference, User Manual, Top
  1152. @chapter Virtual Machine Specification
  1153. This chapter contains a description of the virtual machine implemented
  1154. by @code{avram}, from the point of view of a person wishing to write a
  1155. compiler that generates code for it. Before reading this chapter,
  1156. readers should at least skim @ref{User Manual} in order to see the big
  1157. picture. Topics covered in this chapter include data representations,
  1158. virtual code semantics, and file formats. A toy programming language is
  1159. introduced for illustrative purposes. The sections in this chapter might
  1160. not make sense if read out of order the first time through.
  1161. @ifinfo
  1162. The last section, @ref{Virtual Code Semantics}, contains many equations
  1163. that may be difficult to read in the info or html renderings. The
  1164. printed version is recommended for anyone who really wants to comprehend
  1165. this material.
  1166. @end ifinfo
  1167. @menu
  1168. * Raw Material::
  1169. * Concrete Syntax::
  1170. * File Format::
  1171. * Representation of Numeric and Textual Data::
  1172. * Filter Mode Interface::
  1173. * Parameter Mode Interface::
  1174. * Virtual Code Semantics::
  1175. @end menu
  1176. @node Raw Material, Concrete Syntax, Virtual Machine Specification, Virtual Machine Specification
  1177. @section Raw Material
  1178. The purpose of this section is to instill some basic concepts about the
  1179. way information is stored or communicated by the virtual machine, which
  1180. may be necessary for an understanding of subsequent sections.
  1181. The virtual machine represents both programs and data as members of a
  1182. semantic domain that is straightforward to describe. Lisp users and
  1183. functional programmers may recognize familiar concepts of atoms and
  1184. @cindex lists
  1185. lists in this description. However, these terms are avoided for the
  1186. moment, in order to keep this presentation self contained and to prevent
  1187. knowledgeable readers from inferring any unintended meanings.
  1188. As a rule, it is preferable to avoid overspecifying any theoretical
  1189. artifact. In this spirit, the set of entities with which the virtual
  1190. machine is concerned can be defined purely in terms of the properties we
  1191. need it to have.
  1192. @table @emph
  1193. @item A distinguished element
  1194. A particular element of the set is designated, arbitrarily or otherwise,
  1195. as a distinguished element. Given any element of the set, it is
  1196. always possible to decide whether or not it is the distinguished
  1197. element. The set is non-empty and such an element exists.
  1198. @item A binary operator
  1199. A map from pairs of elements of the set to elements of the set exists
  1200. and meets these conditions.
  1201. @itemize @bullet
  1202. @item It associates a @emph{unique} element of the set with any given
  1203. ordered pair of elements from the set.
  1204. @item It does not associate the distinguished element with any pair of elements.
  1205. @end itemize
  1206. @end table
  1207. For the sake of concreteness, an additional constraint is needed:
  1208. @emph{the set has no proper subset satisfying the above conditions}. Any
  1209. number of constructions remain within these criteria, but there is no
  1210. need to restrict them further, because they are all equivalent for our
  1211. purposes.
  1212. To see that these properties provide all the structure we need for
  1213. general purpose computation, we may suppose some given set @code{S} and
  1214. an operator @code{cons} having them are fixed, and infer the following
  1215. points.
  1216. @itemize @bullet
  1217. @item @code{S} contains at least one element, the distinguished
  1218. element. Call it @code{nil}.
  1219. @cindex @code{nil}
  1220. @item The pair @code{(nil,nil)} is a pair of
  1221. elements of @code{S}, so there must be an element of @code{S} that
  1222. @code{cons} associates with it. We can denote this element
  1223. @code{cons(nil,nil)}.
  1224. @cindex @code{cons}
  1225. @item As no pair of elements is associated with the
  1226. distinguished element, @code{cons(nil,nil)} must differ from @code{nil}, so
  1227. @code{S} contains at least two distinct elements.
  1228. @item The pair @code{(nil,cons(nil,nil))} therefore differs from @code{(nil,nil)},
  1229. but because it is yet another pair of elements from @code{S}, there
  1230. must be an element associated with it by the operator. We can denote
  1231. this element as @code{cons(nil,cons(nil,nil))}.
  1232. @item Inasmuch as the operator
  1233. associates every pair of elements with a @emph{unique} element,
  1234. @code{cons(nil,cons(nil,nil))} must differ from the element associated
  1235. with any other pair of elements, so it must differ from
  1236. @code{cons(nil,nil)}, and we conclude that @code{nil},
  1237. @code{cons(nil,nil)} and @code{cons(nil,cons(nil,nil))} constitute three
  1238. distinct elements of the set @code{S}.
  1239. @item By defining @code{cons(cons(nil,nil),nil)}
  1240. and @code{cons(cons(nil,nil),cons(nil,nil))} analogously and following a
  1241. similar line of reasoning, one may establish the existence of two more
  1242. distinct elements of @code{S}.
  1243. @end itemize
  1244. It is not difficult to see that an argument in more general terms could
  1245. show that the inclusion of infinitely many elements in @code{S} is
  1246. mandated by the properties of the @code{cons} operator. Furthermore,
  1247. every element of @code{S} other than @code{nil} owes its inclusion to
  1248. being associated with some other pair of elements by @code{cons},
  1249. because if it were not, its exclusion would permit a proper subset of
  1250. @code{S} to meet all of the above conditions. We can conclude that
  1251. @code{S} contains exactly @code{nil} and the countable infinitude of
  1252. elements of the form @code{cons(x,y)}, where @code{x} and @code{y} are
  1253. either @code{nil} or something of the form @code{cons(@dots{})} themselves.
  1254. Some specific examples of sets and operators that have the required
  1255. properties are as follows.
  1256. @itemize @bullet
  1257. @item the set of natural numbers, with @code{0} as the distinguished element,
  1258. and the @code{cons} operator defined by @code{cons(@var{x},@var{y}) =
  1259. ((@var{x}+@var{y})(@var{x}+@var{y}+1))/2 + @var{y} + 1}
  1260. @item a set of balanced strings of parentheses, with @code{()} as the
  1261. distinguished element, and @code{cons} defined as string concatenation
  1262. followed by enclosure in parentheses
  1263. @item a set of ordered binary trees, with the empty tree as the distinguished
  1264. element, and the @code{cons} operator as that which takes an ordered
  1265. pair of trees to the tree having them as its descendents
  1266. @item a set containing only its own Cartesian product and an arbitrary
  1267. but fixed element @code{nil}, with @code{cons} being the identity
  1268. function
  1269. @end itemize
  1270. Each of these models may suggest a different implementation, some of which
  1271. are more practical than others. The remainder of this document is
  1272. phrased somewhat imprecisely in terms of a combination of the latter
  1273. two. The nature of the set in question is not considered further, and
  1274. elements of the set are described as ``trees'' or ``lists''. The
  1275. @cindex trees
  1276. @cindex lists
  1277. distinguished element is denoted by @code{nil} and the operator by
  1278. @code{cons}. Where no ambiguity results, @code{cons(x,y)} may be written
  1279. simply as @code{(x,y)}. These terms should not be seen as constraints
  1280. on the implementation.
  1281. @node Concrete Syntax, File Format, Raw Material, Virtual Machine Specification
  1282. @section Concrete Syntax
  1283. The previous section has developed a basic vocabulary for statements
  1284. such as ``the virtual machine code for the identity function is
  1285. @cindex identity function
  1286. @code{(nil,(nil,nil))}'', which are elaborated extensively in the
  1287. subsequent sections on code and data formats. However, a description in
  1288. this style would be inadequate without an explanation of how such an
  1289. entity as @code{(nil,(nil,nil))} is communicated to @code{avram} in a
  1290. virtual machine code file. The purpose of this section is to fill the
  1291. gap by explaining exactly how any given tree would be transformed to its
  1292. concrete representation.
  1293. The syntax is based on a conversion of the trees to bit strings,
  1294. @cindex bit strings
  1295. followed by grouping the bits into blocks of six, which are then encoded
  1296. by printable characters. Although anyone is free to modify @code{avram},
  1297. it is recommended that the concrete syntax described here be maintained
  1298. for the sake of portability of virtual machine code applications.
  1299. Building a tree by reading the data from a file requires a more
  1300. difficult algorithm than the one presented in this section, and is not
  1301. considered because it's not strictly necessary for a
  1302. compiler. Procedures for both reading and writing are available to C and
  1303. C++ users as part of the @code{avram} library, and are also easily
  1304. invoked on the virtual code level.
  1305. @menu
  1306. * Bit String Encoding::
  1307. * Blocking::
  1308. @end menu
  1309. @node Bit String Encoding, Blocking, Concrete Syntax, Concrete Syntax
  1310. @subsection Bit String Encoding
  1311. The conversion from trees to bit strings might have been done in several
  1312. @cindex trees
  1313. ways, perhaps the most obvious being based on a preorder traversal with
  1314. each vertex printed as it is traversed. By this method, the entire
  1315. encoding of the left descendent would precede that of the right in the
  1316. bit string. This alternative is therefore rejected because it imposes
  1317. unnecessary serialization on communication.
  1318. It is preferable for the encodings of both descendents of a tree to be
  1319. interleaved to allow concurrent transmission. Although there is
  1320. presently no distributed implementation of the virtual machine and hence
  1321. @cindex distributed implementation
  1322. none that takes advantage of this possibility, it is better to plan
  1323. ahead than to be faced with backward compatibility problems later.
  1324. The preferred algorithm for encoding a tree as a bit string employs a
  1325. queue. The queue contains trees and allows them to be processed in a
  1326. @cindex queues
  1327. first-in first-out order. Intuitively, the algorithm works by traversing
  1328. @cindex printing algorithm
  1329. the tree in level order. To print a tree @code{T} as a string of
  1330. @code{1}s and @code{0}s, it performs the following steps.
  1331. @display
  1332. Initialize the queue to contain only @code{T}
  1333. while the queue is not empty do
  1334. if the front element of the queue is @code{nil} then
  1335. print @code{0}
  1336. else if the front element of the queue is of the form @code{cons(x,y)} then
  1337. print @code{1}
  1338. append @code{x} to the back of the queue
  1339. append @code{y} to the back of the queue
  1340. end if
  1341. remove the front element of the queue
  1342. end while
  1343. @end display
  1344. This algorithm presupposes that any given tree
  1345. @cindex deconstruction
  1346. @code{cons(x,y)} can be ``deconstructed'' to obtain @code{x} and
  1347. @code{y}. The computability of such an operation is assured in theory by
  1348. the uniqueness property of the @code{cons} operator, regardless of the
  1349. representation chosen. If the trees are implemented with pointers in the
  1350. obvious way, their deconstruction is a trivial constant time operation.
  1351. As an example, running the following tree through the above algorithm
  1352. results in the bit string @code{111111101011110010001001100010100010100100100}.
  1353. @example
  1354. cons(
  1355. cons(
  1356. cons(nil,cons(nil,cons(nil,nil))),
  1357. cons(nil,cons(nil,nil))),
  1358. cons(
  1359. cons(
  1360. cons(nil,cons(nil,cons(nil,cons(nil,nil)))),
  1361. cons(nil,nil)),
  1362. cons(
  1363. cons(
  1364. cons(nil,cons(nil,cons(cons(nil,cons(nil,nil)),nil))),
  1365. cons(nil,nil)),
  1366. nil)))
  1367. @end example
  1368. @node Blocking, , Bit String Encoding, Concrete Syntax
  1369. @subsection Blocking
  1370. After the bit string is obtained as described above, it is grouped into
  1371. blocks of six. Continuing with the example, the string
  1372. @example
  1373. 111111101011110010001001100010100010100100100
  1374. @end example
  1375. @noindent
  1376. would be grouped as
  1377. @example
  1378. 111111 101011 110010 001001 100010 100010 100100 100
  1379. @end example
  1380. @noindent
  1381. Because the number of bits isn't a multiple of six, the last group has to
  1382. be padded with zeros, to give
  1383. @example
  1384. 111111 101011 110010 001001 100010 100010 100100 100000
  1385. @end example
  1386. @noindent
  1387. Each of these six bit substrings is then treated as a binary number,
  1388. with the most significant bit on the left. The numbers expressed in
  1389. decimal are
  1390. @example
  1391. 63 43 50 9 34 34 36 32
  1392. @end example
  1393. @noindent
  1394. @cindex character codes
  1395. The character codes for the characters to be written are obtained by
  1396. adding sixty to each of these numbers, so as to ensure that they will be
  1397. printable characters. The resulting character codes are
  1398. @example
  1399. 123 103 110 69 94 94 96 92
  1400. @end example
  1401. @noindent
  1402. which implies that the tree in the example could be written to a file as
  1403. @code{@{gnE^^`\}.
  1404. @node File Format, Representation of Numeric and Textual Data, Concrete Syntax, Virtual Machine Specification
  1405. @section File Format
  1406. @cindex file format
  1407. A virtual code file consists of an optional text preamble, followed by
  1408. the concrete representation for a tree. The latter uses the
  1409. syntax described in the previous section. The purpose of this section is
  1410. to specify the remaining details of the file format.
  1411. The format for virtual code files may also be used for other purposes
  1412. by virtual code applications, as it is automatically detected and parsed
  1413. by @code{avram} when used in an input file, and can be automatically
  1414. written to output files at the discretion of the application.
  1415. Other than virtual code files, input files not conforming to this format
  1416. are not an error as far as @code{avram} is concerned, because they are
  1417. @cindex text files
  1418. assumed to be text files. Applications can detect in virtual code
  1419. the assumption that is made and report an error if appropriate.
  1420. Although the data file format includes no checksums or other explicit
  1421. @cindex checksums
  1422. methods of error detection, the concrete syntax itself provides a
  1423. good measure of protection against undetected errors. The probability is
  1424. vanishingly small that a random alteration to any valid encoding leaves
  1425. it intact, because every bit in the sequence either mandates or
  1426. prohibits the occurrence of two more bits somewhere after it. Errors in
  1427. different parts of the file would have to be consistent with one another
  1428. to go unnoticed.
  1429. @menu
  1430. * Preamble Section::
  1431. * Data Section::
  1432. @end menu
  1433. @node Preamble Section, Data Section, File Format, File Format
  1434. @subsection Preamble Section
  1435. @cindex preamble
  1436. @itemize @bullet
  1437. @item A file may contain at most one preamble.
  1438. @item The preamble, if any, is a consecutive sequence of lines beginning
  1439. with the first line in the file.
  1440. @item The first line of the preamble must begin with a hash (@code{#})
  1441. character.
  1442. @item Subsequent lines of the preamble must either begin with a hash, or
  1443. immediately follow a line that ends with a backslash (@code{\})
  1444. character (or both).
  1445. @end itemize
  1446. @node Data Section, , Preamble Section, File Format
  1447. @subsection Data Section
  1448. @itemize @bullet
  1449. @item The data or virtual code section of the file begins on the first
  1450. line of the file that isn't part of the preamble.
  1451. @item The data section may not contain any hashes, white space, or other
  1452. extraneous characters other than line breaks.
  1453. @item If line breaks are ignored, the data section contains a sequence
  1454. of characters expressing a single tree in the concrete syntax described
  1455. in @ref{Concrete Syntax}.
  1456. @end itemize
  1457. @node Representation of Numeric and Textual Data, Filter Mode Interface, File Format, Virtual Machine Specification
  1458. @section Representation of Numeric and Textual Data
  1459. As noted already, virtual code applications are specified by functions
  1460. operating on elements of a set having the properties described in
  1461. @ref{Raw Material}, which are convenient to envision as ordered binary trees or
  1462. @cindex trees
  1463. @cindex @code{nil}
  1464. pairs of @code{nil}. However, virtual code applications normally deal
  1465. with numeric or textual data, for example when they refer to the
  1466. contents of a text file. It is therefore necessary for the application
  1467. and the virtual machine emulator to agree on a way of describing textual
  1468. or numeric data with these trees.
  1469. The purpose of this section is to explain the basic data structures used
  1470. in the exchange of information between @code{avram} and a virtual code
  1471. application. For example, an explanation is needed for statements like
  1472. ``an application invoked with the @option{--baz} option is expected to
  1473. return a pair @code{(@var{foo},@var{bar})}, where @code{@var{foo}} is a
  1474. @cindex strings
  1475. @cindex character strings
  1476. @cindex lists
  1477. list of character strings @dots{}'', that are made subsequently in this
  1478. document. Such statements should be understood as referring to the trees
  1479. representing the pairs, lists, character strings, etc., according to the
  1480. conventions explained below.
  1481. @table @emph
  1482. @item Characters
  1483. @cindex character codes
  1484. An arbitrarily chosen set of 256 trees is used to represent the
  1485. character set. They are listed in @ref{Character Table}. For example,
  1486. the letter @code{A} is represented by
  1487. @code{(nil,(((nil,(nil,(nil,nil))),nil),(nil,nil)))}. That means that
  1488. when an application wants the letter @code{A} written to a text file, it
  1489. returns something with this tree in it.
  1490. @item Booleans
  1491. @cindex booleans
  1492. The value of @code{false} is represented by @code{nil}, and the value of
  1493. @code{true} is represented by @code{(nil,nil)}.
  1494. @item Pairs
  1495. @cindex pairs
  1496. Given any two items of data @var{x1} and @var{x2}, having the respective
  1497. representations @var{r1} and @var{r2}, the pair @code{(@var{x1},@var{x2})} has the
  1498. representation @code{cons(@var{r1},@var{r2})}.
  1499. @item Lists
  1500. @cindex lists
  1501. A list of the items @var{x1}, @var{x2} @dots{} @var{xn} with respective
  1502. representations @var{r1} through @var{rn} is represented by the tree
  1503. @code{cons(@var{r1},cons(@var{r2}@dots{}cons(@var{rn},nil)@dots{}))}. In other words,
  1504. lists are represented as pairs whose left sides are the heads and whose
  1505. right sides are the tails. The empty list is identified with
  1506. @code{nil}. Lists of arbitrary finite length can be accommodated.
  1507. @item Naturals
  1508. @cindex naturals
  1509. A number of the form @code{@var{b0} + 2@var{b1} + 4@var{b2} + @dots{} +
  1510. 2^n @var{bn}}, where each @code{@var{b}i} is @code{0} or @code{1}, is
  1511. represented by a tree of the form
  1512. @code{cons(@var{t0},cons(@var{t1}@dots{}cons(@var{tn},nil)@dots{}))}
  1513. where each @code{@var{t}i} is @code{nil} if the corresponding
  1514. @code{@var{b}i} is @code{0}, and @code{(nil,nil)} otherwise. Note that
  1515. the numbers @code{@var{b}i} are exactly the bits written in the binary
  1516. expansion of the number, with @code{@var{b0}} being the least significant
  1517. bit.
  1518. @item Strings
  1519. @cindex strings
  1520. @cindex character strings
  1521. are represented as lists of characters.
  1522. @end table
  1523. @cindex types
  1524. @code{avram} imposes no more of a ``type discipline'' than necessary to
  1525. a workable interface between it and an application. This selection of
  1526. types and constructors should not be seen as constraining what a
  1527. compiler writer may wish to have in a source language.
  1528. @node Filter Mode Interface, Parameter Mode Interface, Representation of Numeric and Textual Data, Virtual Machine Specification
  1529. @section Filter Mode Interface
  1530. @cindex filter mode
  1531. @cindex parameter mode
  1532. @cindex modes
  1533. From the point of view of the application developer or compiler writer,
  1534. there are parameter mode applications, which are discussed in
  1535. @ref{Parameter Mode Interface}, and filter mode applications,
  1536. which are discussed in this section. Of the latter, there are mainly
  1537. three kinds: those that read one character at a time, those that read a
  1538. line at a time, and those that read the whole standard input file at
  1539. @cindex standard input
  1540. once. Each of them is invoked with different options and expected to
  1541. follow different calling conventions. This section summarizes these
  1542. conventions.
  1543. @menu
  1544. * Loading All of Standard Input at Once::
  1545. * Line Maps::
  1546. * Byte Transducers::
  1547. @end menu
  1548. @node Loading All of Standard Input at Once, Line Maps, Filter Mode Interface, Filter Mode Interface
  1549. @subsection Loading All of Standard Input at Once
  1550. Unless @option{--line-map} or @option{--byte-transducer} is used as a
  1551. @cindex @code{line-map} command line option
  1552. @cindex @code{byte-transducer} command line option
  1553. @cindex standard input
  1554. command line option when the application is invoked, the contents of
  1555. standard input are loaded entirely into memory by @code{avram} before
  1556. evaluation of the virtual code begins. This interface is obviously not
  1557. @cindex infinite streams
  1558. appropriate for infinite streams.
  1559. The virtual code application in this mode of operation is treated as a
  1560. single function taking the entire contents of standard input as its argument,
  1561. and returning the entire contents of standard output as its result.
  1562. Hence, this interface is one of the simplest available.
  1563. @menu
  1564. * Standard Input Representation::
  1565. * Standard Output Representation::
  1566. @end menu
  1567. @node Standard Input Representation, Standard Output Representation, Loading All of Standard Input at Once, Loading All of Standard Input at Once
  1568. @subsubsection Standard Input Representation
  1569. @cindex standard input
  1570. The representation for the standard input file used as the argument to
  1571. the function depends both on the file format and on the command line
  1572. options specified when the application is invoked. The
  1573. @cindex @code{unparameterized} command line option
  1574. @cindex @code{raw-mode} command line option
  1575. @option{--unparameterized} and @option{--raw-output} options make no
  1576. difference to the input representation, and the @option{--line-map}
  1577. and @option{--byte-transducer} options are not relevant to this mode of
  1578. operation. That leaves four possible combined settings of the
  1579. @cindex @code{choice-of-output} command line option
  1580. @cindex @code{force-text-input} command line option
  1581. @option{--choice-of-output} and @option{--force-text-input} options. If
  1582. standard input conforms to the data file format specification @ref{File Format},
  1583. the following effects are possible.
  1584. @itemize @bullet
  1585. @item If neither @option{--choice-of-output} nor
  1586. @option{--force-text-input} is used, the argument to the function will
  1587. be given directly by the tree encoded in the data section of the
  1588. file. The preamble of the file will be ignored.
  1589. @item If the @option{--choice-of-output} option is used
  1590. and the @option{--force-text-input} option is not used, the argument to
  1591. the function will be a pair @code{(@var{preamble},@var{contents})},
  1592. where @var{preamble} is a list of character strings taken from the
  1593. preamble of the file (with leading hashes stripped), and @var{contents}
  1594. is the tree represented in the data section of the file.
  1595. @item If the @option{--choice-of-output} option is not used
  1596. and the @option{--force-text-input} option is used, the argument to the
  1597. function will be the whole file as a list of character strings. I.e.,
  1598. both the preamble and the data sections are included, hashes are not
  1599. stripped from the preamble, and the data section is not converted to the
  1600. tree it represents.
  1601. @item If the @option{--choice-of-output} option is used
  1602. and the @option{--force-text-input} option is also used, the argument to the
  1603. the function will be a pair @code{(nil,@var{contents})}, where the
  1604. contents are the list of character strings as in the previous case.
  1605. @end itemize
  1606. @cindex file format
  1607. If standard input does not conform to the data file format specification
  1608. in @ref{File Format}, then it is assumed to be a text file. The
  1609. @option{--force-text-input} option makes no difference, and there are
  1610. only two possible effects, depending on whether
  1611. @option{--choice-of-output} is used. They correspond to the latter two
  1612. cases above, where @option{--force-text-input} is used.
  1613. @cindex preamble
  1614. @cindex text files
  1615. The idea of the @option{--choice-of-output} option is that it is used
  1616. only for applications that are smart enough to be aware of the
  1617. @code{(@var{preamble},@var{contents})} convention. A non-empty preamble
  1618. implies a data file whose contents could be any type, but an empty
  1619. preamble implies a text file whose contents can only be a list of
  1620. character strings. (In the case of a data file with no preamble, the
  1621. list of the empty string is used for the preamble to distinguish it from
  1622. a text file.)
  1623. Dumb applications that never want to deal with anything but text files
  1624. should be invoked with @option{--force-text-input}. Otherwise, they have
  1625. to be prepared for either text or data as arguments.
  1626. The use of both options at once is unproductive as far as the input
  1627. format is concerned, but may be justified when the output is to be a
  1628. data file and the input is text only.
  1629. @node Standard Output Representation, , Standard Input Representation, Loading All of Standard Input at Once
  1630. @subsubsection Standard Output Representation
  1631. @cindex standard output
  1632. @cindex @code{raw-output} command line option
  1633. @cindex @code{choice-of-output} command line option
  1634. As in the case of standard input, the representation for standard output
  1635. that the function is expected to return depends on the command line
  1636. options with which the application is invoked. The only relevant options
  1637. are @option{--raw-output} and @option{--choice-of-output}, which are
  1638. mutually exclusive.
  1639. @itemize @bullet
  1640. @item If neither option is selected, the result returned by the function must
  1641. be a list of character strings.
  1642. @item If @option{--raw-output} is used, the result returned by the
  1643. function is unconstrained, and it will be written as a data file with no
  1644. preamble, following the format specified in @ref{File Format}.
  1645. @item If @option{--choice-of-output} is used, the result returned by the
  1646. function must be a pair @code{(@var{preamble},@var{contents})}.
  1647. @end itemize
  1648. @cindex preamble
  1649. In the last case, the preamble determines how the file will be written.
  1650. If it is meant to be a text file, the preamble should be @code{nil}, and
  1651. the contents should be a list of character strings. If it is meant to be
  1652. a data file, the preamble should be a non-empty list of character
  1653. strings, and the format of the contents is unconstrained. To express a
  1654. data file with no preamble, the preamble should be the list containing
  1655. the empty string, rather than being empty.
  1656. In the result returned by the function, the preamble lines should not
  1657. include leading hash characters, because they are automatically added to
  1658. the output to enforce consistency with the data file format. However,
  1659. they should include trailing backslashes as continuation characters
  1660. where appropriate. The hashes that are automatically added will be
  1661. automatically stripped by @code{avram} on behalf of whatever application
  1662. uses the file.
  1663. @cindex character strings
  1664. @cindex printing algorithm
  1665. Any file can be written as a list of character strings, even ``text''
  1666. files that are full of unprintable characters, and even ``text'' files
  1667. that happen to conform to the format used for data files. However, if
  1668. the application intends to write a data file in the standard format used
  1669. by other virtual code applications, it can do so more quickly and easily
  1670. by having the virtual machine do the formatting automatically with the
  1671. @option{--choice-of-output} option than by implementing the algorithm in
  1672. @ref{Concrete Syntax}, from scratch in virtual code.
  1673. @node Line Maps, Byte Transducers, Loading All of Standard Input at Once, Filter Mode Interface
  1674. @subsection Line Maps
  1675. @cindex @code{line-map} command line option
  1676. Virtual code applications invoked with the @option{--line-map} option
  1677. (with or without the @option{--unparameterized} option) adhere to a very
  1678. simple interface.
  1679. @itemize @bullet
  1680. @item The argument to the function is a character string, and the result
  1681. must also be a character string.
  1682. @item The function is applied to each line of the standard input file
  1683. @cindex standard input
  1684. and the result in each case is written to standard output followed by a
  1685. @cindex standard output
  1686. line break.
  1687. @end itemize
  1688. @cindex infinite streams
  1689. This kind of application may be used on finite or infinite streams,
  1690. provided that the lengths of the lines are finite, but preserves no state
  1691. information from one line to the next.
  1692. @node Byte Transducers, , Line Maps, Filter Mode Interface
  1693. @subsection Byte Transducers
  1694. The interface used when the @code{--byte-transducer} option is selected
  1695. @cindex @code{byte-transducer} command line option
  1696. allows an application to serve as a persistent stream processor suitable
  1697. @cindex infinite streams
  1698. for finite or infinite streams. The interface can be summarized by the
  1699. following points.
  1700. @itemize @bullet
  1701. @item When it is first invoked, the function in the virtual code file is
  1702. applied to an argument of @code{nil}, and is expected to return a pair
  1703. @code{(@var{state},@var{output})}. The @var{state} format is
  1704. unconstrained. The @var{output} must be a character string that will
  1705. be written to standard output, but it may be the empty string.
  1706. @item For each byte read from standard input, @code{avram} applies the function
  1707. to the pair @code{(@var{state},@var{character})}, using the state
  1708. obtained from previous evaluation, and the character whose code is the
  1709. byte. The purpose of the @var{state} field is therefore to provide a
  1710. way for the application to remember something from one invocation to
  1711. the next.
  1712. @item The function is usually expected to return a pair
  1713. @code{(@var{state},@var{output})} for each input byte, so that the state
  1714. can be used on the next iteration, and the output can be written to standard
  1715. output as a character string.
  1716. @item If the function ever returns a value of @code{nil}, the computation
  1717. terminates.
  1718. @item If standard input comes to an end before the computation terminates,
  1719. the function will be applied to a pair of the form
  1720. @code{(@var{state},nil)} thereafter, but may continue to return
  1721. @code{(@var{state},@var{output})} pairs for arbitrarily many more iterations.
  1722. The @code{EOF} character is not explicitly passed to the function, but
  1723. the end is detectable insofar as @code{nil} is not a representation for
  1724. any character.
  1725. @end itemize
  1726. Unlike the situation with line maps, the output character strings
  1727. do not have line breaks automatically appended, and the application must
  1728. include them explicitly if required. The convention for
  1729. @cindex Unix
  1730. line breaks is system dependent. On Unix and GNU/Linux systems, character
  1731. code 10 indicates a line break, but other systems may use character code
  1732. 13 followed by character code 10. See @ref{Character Table} for the
  1733. @cindex character codes
  1734. representations of characters having these codes.
  1735. @node Parameter Mode Interface, Virtual Code Semantics, Filter Mode Interface, Virtual Machine Specification
  1736. @section Parameter Mode Interface
  1737. @cindex parameter mode
  1738. The virtual code file for a parameter mode application contains a tree
  1739. representing a single function, which takes as its argument a data
  1740. structure in the format described below. The format of the result
  1741. returned by the function depends on the virtual machine options used on
  1742. the command line, and the various alternatives are explained
  1743. subsequently.
  1744. @menu
  1745. * Input Data Structure::
  1746. * Input for Mapped Applications::
  1747. * Output From Non-interactive Applications::
  1748. * Output From Interactive Applications::
  1749. @end menu
  1750. @node Input Data Structure, Input for Mapped Applications, Parameter Mode Interface, Parameter Mode Interface
  1751. @subsection Input Data Structure
  1752. The data structure that is used as the argument to the parameter mode
  1753. application incorporates all the information about the command line and
  1754. @cindex environment
  1755. @cindex command line
  1756. the environment variables. It is in the form of a triple
  1757. @code{((@var{files},@var{options}),@var{environs})}. The fields have
  1758. these interpretations.
  1759. @table @var
  1760. @item files
  1761. is a list of quadruples
  1762. @code{((@var{date},@var{path}),(@var{preamble},@var{contents}))}, with
  1763. one quadruple for each input file named on the command line (but not the
  1764. virtual code file or the @code{avram} executable). The list will be in
  1765. the same order as the filenames on the command line, and is not affected
  1766. by options interspersed with them. The fields in each item have the
  1767. following interpretations.
  1768. @table @var
  1769. @item date
  1770. is the time stamp of the file in as a character string in the usual
  1771. @cindex time stamp
  1772. @cindex date
  1773. @cindex system time
  1774. @cindex current time
  1775. @cindex Unix
  1776. Unix format, for example, @code{Fri Jan 19 14:34:44 GMT 2001}. If the
  1777. file corresponds to standard input, the current system time and date are
  1778. used.
  1779. @item path
  1780. is the full path of the file, expressed as a list of strings. If the
  1781. @cindex file names
  1782. @cindex paths
  1783. @cindex absolute path
  1784. @cindex relative path
  1785. file corresponds to standard input, the list is empty. Otherwise, the
  1786. first string in the list is the file name. The next is the name of the
  1787. file's parent directory, if any. The next is the parent of the parent,
  1788. and so on. The root directory is indicated by the empty string, so that
  1789. any path ending with the empty string is an absolute path, while any path
  1790. ending with a non-empty string is relative to the current working
  1791. directory. Path separators (i.e., slashes) are omitted.
  1792. @item preamble
  1793. In the case of a text file, or any file not conforming to the format in
  1794. @cindex preamble
  1795. @ref{File Format}, this field is @code{nil}. Otherwise, this field contains
  1796. the preamble of the file expressed as a list of strings, or contains
  1797. just the empty string if the file has no preamble. Any leading hashes in
  1798. the preamble of the file are stripped.
  1799. @item contents
  1800. In the case of a text file (as indicated by the @var{preamble} field), this
  1801. @cindex text files
  1802. field will contain a list of character strings, with each line of the file
  1803. contained in a character string. Otherwise, it can contain data in any
  1804. format, which are obtained by converting the data section of the file
  1805. to a tree.
  1806. @end table
  1807. @item options
  1808. is a list of quadruples of the form
  1809. @code{((@var{position},@var{longform}),(@var{keyword},@var{params}))}
  1810. with one quadruple for each option appearing on the command line after
  1811. the name of the virtual code file.
  1812. @table @var
  1813. @item position
  1814. is a natural number indicating the position of the option on the command
  1815. @cindex naturals
  1816. @cindex command line
  1817. line. The position numbers of all the options will form an ascending
  1818. sequence, but not necessarily consecutive nor starting with zero. The
  1819. missing numbers in the sequence will correspond to the positions of the
  1820. file names on the command line, allowing their positions to be inferred by
  1821. applications for which the position matters.
  1822. @item longform
  1823. is a boolean value which is true if the option starts with two or more
  1824. @cindex booleans
  1825. dashes but false otherwise.
  1826. @item keyword
  1827. is the key word of the option expressed as a character string. For example
  1828. in the case of a command line option @kbd{--foo=bar,baz}, the keyword is
  1829. @code{foo}. Leading dashes are stripped.
  1830. @item params
  1831. is a list of character strings identifying the parameters for the
  1832. command line option in question. In the case of an option of the form
  1833. @kbd{--foo=bar,baz}, the first character string in the list will be
  1834. @code{bar} and the next will be @code{baz}. The same applies if the
  1835. option is written @kbd{--foo bar,baz} or @kbd{--foo =bar,baz}. If there
  1836. are no parameters associated with the option, the list is empty.
  1837. @end table
  1838. @item environs
  1839. is a list of pairs of character strings, with one pair in the list for
  1840. @cindex environment
  1841. each environment variable. The identifier is the left string in the
  1842. pair, and the value is the right. For example, if the environment
  1843. contains the definition @code{OSTYPE=linux-gnu}, there will be a pair in
  1844. the list whose left side is the string @code{OSTYPE} and whose
  1845. right side is the string @code{linux-gnu}.
  1846. @end table
  1847. @node Input for Mapped Applications, Output From Non-interactive Applications, Input Data Structure, Parameter Mode Interface
  1848. @subsection Input for Mapped Applications
  1849. Applications invoked using the @option{--map-to-each-file} option
  1850. @cindex @code{map-to-each-file} command line option
  1851. benefit from a slightly different interface than the one described
  1852. above. As the purpose of this option is to save memory by loading only
  1853. one file at a time, the application does not have access to all input
  1854. files named on the command line simultaneously within the same data
  1855. structure. Although the data structure is of the type already described,
  1856. the @var{files} field is not a list of arbitrary length. Instead, it is
  1857. a list containing exactly one item for an input file. If @kbd{-}
  1858. is used as a command line parameter, indicating standard input, then
  1859. @var{files} will have another item pertaining to standard
  1860. input. In no event will it have other than one or two items.
  1861. The mapped application is expected to work by being applied individually
  1862. to each of any number of separately constructed data structures, doing
  1863. the same in each case as it would if that case were the only one. To make
  1864. that possible, copies of the environment variables, the contents of standard
  1865. input, and the list of application specific options are contained in the
  1866. data structure used for every invocation.
  1867. @cindex command line
  1868. The position numbers in the options are adjusted for each invocation to
  1869. reflect the position of the particular input file associated with it.
  1870. For example, in the following command
  1871. @display
  1872. @kbd{avram --map-to-each-file mapster.avm fa.txt --data fb.dat --code fc.o}
  1873. @end display
  1874. @noindent
  1875. the function in the virtual code file @file{mapster.avm} would be
  1876. applied to each of three data structures, corresponding to the commands
  1877. @display
  1878. @kbd{avram mapster.avm fa.txt --data --code}
  1879. @kbd{avram mapster.avm --data fb.dat --code}
  1880. @kbd{avram mapster.avm --data --code fc.o}
  1881. @end display
  1882. @noindent
  1883. If the relative positions of the options and filenames were important to
  1884. the application, they could be reliably inferred from the position
  1885. numbers. In the first case, they would be 1 and 2, implying that the
  1886. file is in position 0. In the second case they would be 0 and 2,
  1887. implying that the file is in position 1, and in the third case they
  1888. would be 0 and 1, implying that the file is in position 2. (Of course,
  1889. nothing compels an application to concern itself with the positions of
  1890. its parameters, and the alternative might be preferable.)
  1891. For the most part, any application that can operate on one file at a
  1892. time without needing information from any others can be executed more
  1893. economically with the @option{--map-to-each-file} option and few if any
  1894. changes to the code. The effect will normally be analogous to the above
  1895. example, subject to a few possible differences.
  1896. @itemize @bullet
  1897. @item If an application is supposed to do something by default when
  1898. there are no file parameters or only standard input, it won't work as a
  1899. mapped application, because if there are no file parameters it won't be
  1900. executed at all.
  1901. @item If a mapped application causes any output files to be generated, they
  1902. may be written before other input files are read, possibly causing the
  1903. input files to be overwritten if they have the same names, and causing
  1904. subsequent invocations to use the overwritten versions. This behavior
  1905. differs from that of loading all input files at the outset, which
  1906. ensures the application seeing all of the original versions. The latter may be
  1907. more convenient for maintaining a group of files in some sort of
  1908. consistent state.
  1909. @item If an application causes standard output to be written along with
  1910. output files, normally standard output is written last as a security
  1911. measure against malicious code altering the @option{--ask-to-overwrite}
  1912. prompts by subtly clobbering the console. In a mapped application,
  1913. standard output isn't always last because there may be more invocations
  1914. to come.
  1915. @end itemize
  1916. @node Output From Non-interactive Applications, Output From Interactive Applications, Input for Mapped Applications, Parameter Mode Interface
  1917. @subsection Output From Non-interactive Applications
  1918. @cindex @code{interactive} command line option
  1919. @cindex @code{step} command line option
  1920. If a parameter mode application is not invoked with either of the
  1921. @option{--interactive} or @option{--step} options,
  1922. then it is deemed to be non-interactive, and therefore does not concern
  1923. itself with executing shell commands. Instead, it simply specifies a
  1924. list of output files to be created or updated on its behalf by @code{avram}.
  1925. The files are described by a list of quadruples
  1926. @code{((@var{overwrite},@var{path}),(@var{preamble},@var{contents}))},
  1927. with one quadruple for each file.
  1928. @cindex preamble
  1929. @cindex paths
  1930. @cindex standard input
  1931. @cindex standard output
  1932. In each quadruple, the @var{path}, @var{preamble}, and @var{contents}
  1933. fields have the same interpretations as in the list of files in the
  1934. input data structure described in @ref{Input Data Structure}, except that a
  1935. @code{nil} path refers to standard output rather than to standard input.
  1936. The @var{overwrite} field in each quadruple tells whether the file
  1937. @cindex appending to files
  1938. should be overwritten or appended. If the @var{overwrite} field is
  1939. @code{nil} (i.e., the representation for the Boolean value of
  1940. @code{false}) and a file already exists at the given path, the new
  1941. contents will be appended to it. If the @var{overwrite} field is
  1942. anything other than @code{nil} and/or no file exists with the given
  1943. path, a new file is written or the extant one is overwritten. Note that
  1944. the data file format specified in @ref{File Format} precludes appending
  1945. anything to it, but the format of existing output files is not checked
  1946. and nothing prevents data or text from being appended to one.
  1947. @node Output From Interactive Applications, , Output From Non-interactive Applications, Parameter Mode Interface
  1948. @subsection Output From Interactive Applications
  1949. @cindex @code{interactive} command line option
  1950. @cindex @code{step} command line option
  1951. Parameter mode applications invoked with either of the
  1952. @option{--interactive} or @option{--step} options are
  1953. required to take the data structure described in @ref{Input Data Structure} as an
  1954. argument but to return the virtual code for a function that will observe
  1955. @cindex shell
  1956. a certain protocol allowing shell commands to be executed on its
  1957. behalf. The intent is that the virtual code file doesn't contain the
  1958. real application @emph{per se}, but only something that builds the real
  1959. one on the fly using configuration information from the input files and
  1960. command line options.
  1961. The format of the result returned by an interactive application, being a
  1962. virtual code application itself, requires a full exposition of the
  1963. virtual machine code semantics. This subject is deferred to @ref{Virtual Code Semantics}.
  1964. The remainder of this section describes the protocol followed by the
  1965. function returned by the interactive application rather than the
  1966. application itself.
  1967. Similarly to the case of a byte transducer described in @ref{Byte Transducers},
  1968. the basic pattern of interaction between @code{avram} and the function
  1969. is a cycle of invocations. In general terms, the function is applied to
  1970. a @code{nil} argument initially, and expected to return an initial state
  1971. and initial output. Thereafter, the function is applied to a pair of the
  1972. state returned on the previous iteration, and the next installment of
  1973. input. The function returns further output and a new state, and the
  1974. cycle continues until the function returns a value of @code{nil}, at
  1975. which time the computation terminates.
  1976. @menu
  1977. * Line Oriented Interaction::
  1978. * Character Oriented Interaction::
  1979. * Mixed Modes of Interaction::
  1980. @end menu
  1981. @node Line Oriented Interaction, Character Oriented Interaction, Output From Interactive Applications, Output From Interactive Applications
  1982. @subsubsection Line Oriented Interaction
  1983. Within this general pattern, more specific styles of interaction are
  1984. possible. In the simplest one to explain first, the result returned by
  1985. the function is always a data structure of the form
  1986. @cindex command line
  1987. @code{(@var{state},(@var{command lines},@var{prompts}))}, wherein the
  1988. fields have these interpretations.
  1989. @table @var
  1990. @item state
  1991. is a tree incorporating any data in any format that the application
  1992. needs to remember from one invocation to the next.
  1993. @item command lines
  1994. is a list of character strings that are piped to the standard input
  1995. stream of a separately spawned process. The process may persist from one
  1996. invocation of the function to the next, or may be spawned each time.
  1997. @item prompts
  1998. is a non-empty list of character strings containing a suffix of the text
  1999. expected from the standard output stream of the process as a result of
  2000. sending the command lines to it.
  2001. @end table
  2002. On each iteration, @code{avram} sends the command line character strings
  2003. @cindex spawning processes
  2004. to a separately spawned process, with line breaks between them if there
  2005. are more than one command. If a process remains from the previous iteration that
  2006. has not terminated itself, the list of command lines is sent to the same
  2007. process. If no such process already exists, the first string in the list
  2008. of command lines is treated as a shell command and used to spawn the
  2009. @cindex @code{exp_popen}
  2010. process (using the @code{exp_popen} library function), and the remaining
  2011. strings are sent to the newly spawned process.
  2012. Normally processes spawned with commands that invoke interactive command
  2013. line interpreters of their own, such as @command{bash}, @command{ftp} or
  2014. @command{bc}, will persist indefinitely unless the command causing them
  2015. to exit is issued or some other event kills them. Processes spawned with
  2016. non-interactive commands, such as @command{ls} or @command{pwd}, will
  2017. terminate when the last of their initial output has been received.
  2018. In the case of processes that persist after being spawned, @code{avram}
  2019. needs some way of knowing when to stop waiting for more output from them
  2020. so that it doesn't get stuck waiting forever. This purpose is served by
  2021. the @var{prompts} field. This field could contain a single string
  2022. holding the last thing the process will send before becoming quiescent,
  2023. such as the strings @code{bash$ } or @code{ftp> } in the above
  2024. examples. Alternatively, a sequence of more than one prompt string can
  2025. be used to indicate that the corresponding sequence of lines must be
  2026. detected. An empty string followed by @code{ftp> } would indicate that
  2027. the @code{ftp> } prompt is expected to be immediately preceded by a line
  2028. @cindex prompts
  2029. break. There is also the option of using prompt strings to indicate a
  2030. pattern that does not necessarily imply quiescence, but is a more
  2031. convenient point at which to stop reading the output from the process.
  2032. For processes spawned with commands that do not start their own
  2033. interactive command line interpreters, such as @command{ls} or
  2034. @command{pwd}, it may be preferable to read all the output from them
  2035. until they terminate. To achieve this effect, the list of prompt strings
  2036. should contain only the single string containing only the single
  2037. @code{EOF} character (usually code 4) or any other character that is
  2038. certain not to occur in the output of the process. This technique is
  2039. based on the assumption that the process was spawned originally with the
  2040. command in question, not that such a command is sent to an existing
  2041. shell process.
  2042. In any case, when enough output has been received from the process, it
  2043. is collected into a list of received strings including the prompt
  2044. strings at the end (if they were received), and the function is applied
  2045. to the pair @code{(@var{state},@var{received strings})}. If the cycle is
  2046. to continue, the result returned by the function will include a new
  2047. state, a new list of command lines, and a new list of prompt strings. A
  2048. result of @code{nil} will cause the computation to terminate.
  2049. There are some unusual situations that could occur in the course of line
  2050. oriented interaction, and are summarized as follows.
  2051. @itemize @bullet
  2052. @item If the process terminates before any pattern matching the prompt
  2053. strings is received from it, all of the output from the process up to
  2054. the point where it terminated is collected into the @var{received
  2055. strings} list and passed to the function. This situation includes cases
  2056. where the process terminates immediately upon being spawned, but not
  2057. abnormal completion of the @code{exp_popen} library function, which is
  2058. a fatal error. This feature of the interface is what allows @code{EOF}
  2059. to be used for collecting all the output at once from a non-interactive
  2060. command.
  2061. @item If the list of @var{command lines} is empty, and no process
  2062. currently exists due to a previous iteration, the effect is the same as
  2063. if the process terminates unexpectedly before outputting anything. I.e.,
  2064. the function is applied to a pair containing an empty list of received
  2065. strings. There is no good reason for an application to get into this
  2066. situation.
  2067. @item If the list of @var{command lines} is empty but a process persists
  2068. from a previous iteration, no output is sent to it, but receiving from
  2069. it proceeds normally. This feature of the interface could be used
  2070. effectively by applications intended to process the received data in
  2071. @cindex deadlock
  2072. parts, but will cause deadlock if the process is already quiescent.
  2073. @item All character strings have to consist of lists of valid
  2074. representations of non-null characters according to @ref{Character
  2075. Table}, or else there will be some fatal error messages.
  2076. @item If the list of @var{prompt strings} contains only the empty
  2077. string, @code{avram} will not wait to receive anything from the process,
  2078. but will proceed with the next iteration immediately. If this effect is
  2079. intended, care must be taken not to confuse the empty list
  2080. of @var{prompt strings} with the list containing the empty string. The former
  2081. indicates character oriented interaction, which is explained next.
  2082. @end itemize
  2083. @node Character Oriented Interaction, Mixed Modes of Interaction, Line Oriented Interaction, Output From Interactive Applications
  2084. @subsubsection Character Oriented Interaction
  2085. A character oriented style of interaction involves the function always
  2086. returning a data structure of the form @code{(@var{state},(@var{command
  2087. lines},nil))}. The @var{state} and @var{command lines} fields serve
  2088. @cindex command line
  2089. exactly the same purposes respectively as they do in the case of line
  2090. oriented interaction. The field that would be occupied by the
  2091. @var{prompt strings} list in the case of line oriented interaction is
  2092. identically @code{nil} in this style.
  2093. When this style is used, @code{avram} spawns a process and/or sends
  2094. @cindex spawning processes
  2095. command lines to it as in the case of line oriented interaction, but
  2096. attempts to read only a single character from it per iteration. When the
  2097. character is received, @code{avram} applies the function to the pair
  2098. @code{(@var{state},@var{character})} in order to obtain the next state
  2099. and the next list of command lines. If the process has terminated, a
  2100. @code{nil} value is used in place of the character. If the process is
  2101. quiescent, deadlock ensues.
  2102. The character oriented style is a lower level protocol that shifts more
  2103. of the burden of analyzing the process's output to the virtual code
  2104. application. It can do anything line oriented interaction can do except
  2105. proceeding immediately without waiting to receive any output from the
  2106. process. It may also allow more general criteria (in effect) than the
  2107. matching of a fixed prompt string to delimit the received data, for
  2108. those pathological processes that may require such things.
  2109. Applications using character oriented interaction need to deal with line
  2110. @cindex line breaks
  2111. breaks explicitly among the received characters, unlike the case with
  2112. line oriented interaction, where the line breaks are implicit in the
  2113. @cindex Unix
  2114. list of received strings. Contrary to the convention for Unix text
  2115. files, line breaks in the output of a process are indicated by character
  2116. code 13 followed by character code 10.
  2117. @node Mixed Modes of Interaction, , Character Oriented Interaction, Output From Interactive Applications
  2118. @subsubsection Mixed Modes of Interaction
  2119. An application is not confined exclusively to line oriented or character
  2120. oriented interaction, but may switch from one style to the other between
  2121. iterations, and signal its choice simply by the format of the data
  2122. structure it returns. If the @var{prompt strings} field is non-empty,
  2123. the interaction is line oriented, and if the field is empty, the
  2124. interaction is character oriented. A function using both styles has to
  2125. be prepared for whichever type of data it indicates, either a character or
  2126. a list of character strings as the case may be.
  2127. Another alternative is possible if the function returns a data structure
  2128. in the form @code{(@var{files},nil)}. This structure includes neither a
  2129. list of command lines nor a list of prompt strings, empty or otherwise,
  2130. but does include a list of quadruples in the @var{files} field. The
  2131. quadruples are of the form
  2132. @code{((@var{overwrite},@var{path}),(@var{preamble},@var{contents}))}.
  2133. The fields have the same interpretations as in the output from a
  2134. non-interactive parameter mode application, as described in
  2135. @ref{Output From Non-interactive Applications}, and will cause a list of files to be written in the same way.
  2136. As an interactive application is able cause the execution of arbitrary
  2137. shell commands, it doesn't need @code{avram} to write files for it the
  2138. way a non-interactive application does, so this feature does not provide
  2139. any additional capabilities. However, it may be helpful as a matter of
  2140. convenience.
  2141. After the files are written, the function will be applied to the same
  2142. result it returned, @code{(@var{files},nil)}. There is no direct means
  2143. of preserving unconstrained state information from previous iterations
  2144. in this style of interaction. A likely scenario might therefore be that
  2145. the function returns a file list after finishing its other business, and
  2146. then returns @code{nil} on the next iteration to terminate.
  2147. @node Virtual Code Semantics, , Parameter Mode Interface, Virtual Machine Specification
  2148. @section Virtual Code Semantics
  2149. As the previous sections explain, virtual code applications are defined
  2150. in terms of mathematical functions. Up until this point, the discussion
  2151. has focused on the interface between the function and the virtual
  2152. machine interpreter, by detailing the arguments passed to the functions under
  2153. various circumstances and the results they are expected to return in
  2154. order to achieve various effects.
  2155. The purpose of this section is to complete the picture by explaining how
  2156. a given computable function can be expressed in virtual code,
  2157. considering only functions operating on the trees described in @ref{Raw Material}.
  2158. Functions manipulating trees of @code{nil} are undoubtedly a frivolous
  2159. and abstract subject in themselves. One is obliged to refer back to the
  2160. previous sections if in need of motivation.
  2161. @menu
  2162. * A New Operator::
  2163. * On Equality::
  2164. * A Minimal Set of Properties::
  2165. * A Simple Lisp Like Language::
  2166. * How @code{avram} Thinks::
  2167. * Variable Freedom::
  2168. * Metrics and Maintenance::
  2169. * Deconstruction::
  2170. * Recursion::
  2171. * Assignment::
  2172. * Predicates::
  2173. * Iteration::
  2174. * List Combinators::
  2175. * List Functions::
  2176. * Exception Handling::
  2177. * Interfaces to External Code::
  2178. * Vacant Address Space::
  2179. @end menu
  2180. @node A New Operator, On Equality, Virtual Code Semantics, Virtual Code Semantics
  2181. @subsection A New Operator
  2182. With regard to a set of trees as described in @ref{Raw Material}, we can define
  2183. a new binary operator. Unlike the @code{cons} operator, this one is not
  2184. required to associate an element of the set with every possible pair
  2185. of elements. For very many pairs of operands we will have nothing to
  2186. say about its result. In fact, we require nothing of it beyond
  2187. a few simple properties to be described presently.
  2188. Because this is the only other operator than @code{cons}, there is no
  2189. @cindex @code{cons}
  2190. need to have a special notation for it, so it will be denoted by empty
  2191. space. The tree associated by the operator with a pair of trees @code{@var{x}}
  2192. and @code{@var{y}}, if any, will be expressed in the infix notation
  2193. @code{@var{x} @var{y}}. For convenience, the operator is regarded as
  2194. being right associative, so that @code{@var{a} @var{b} @var{c}} can be
  2195. written for @code{@var{a} (@var{b} @var{c})}.
  2196. @node On Equality, A Minimal Set of Properties, A New Operator, Virtual Code Semantics
  2197. @subsection On Equality
  2198. @cindex equality
  2199. One example of a property this operator should have, for reasons that
  2200. will not be immediately clear, is that for any trees @code{@var{x}} and
  2201. @code{@var{k}}, the equality @code{cons(cons(nil,@code{@var{k}),nil) @var{x}} =
  2202. @code{@var{k}}} always holds.
  2203. Even though the present exposition opts for readability over formality,
  2204. statements like these demand clarification of the notion of
  2205. equality. Some of the more pedantic points in @ref{Raw Material} may be needed
  2206. for the following ideas to hold water.
  2207. @itemize @bullet
  2208. @item As originally stipulated, it is always possible to distinguish
  2209. @code{nil} from any member of the set. We can therefore decide on this
  2210. basis whether @code{@var{a} = @var{b}} whenever at least one of them is @code{nil}.
  2211. @item Where neither @code{@var{a}} nor @code{@var{b}} is @code{nil} in an expression
  2212. @code{@var{a} = @var{b}}, but rather something of the form
  2213. @code{cons(@var{x},@var{y})}, the equality holds if and only
  2214. if both pairs of corresponding subexpressions are equal. If at least one
  2215. member of each pair of corresponding subexpressions is @code{nil}, the
  2216. question is settled, but otherwise there is recourse to their respective
  2217. subexpressions, and so on. This condition follows from the uniqueness property
  2218. of the @code{cons} operator.
  2219. @item If one side of an equality is of the form
  2220. @code{@var{x} @var{y}}, or is defined in terms of such an expression,
  2221. but @code{(@var{x},@var{y})} is one of those pairs with which the operator
  2222. associates no result, then the equality holds if and only if the other
  2223. side is similarly ill defined.
  2224. @item Statements involving universal quantification (i.e.,
  2225. @cindex universal quantification
  2226. @cindex undefined expressions
  2227. beginning with words similar to ``for any tree @code{@var{x}} @dots{}'')
  2228. obviously do not apply to instances of a variable (@code{@var{x}}) outside
  2229. the indicated set (trees). Hence, they are not refuted by any
  2230. consequence of identifying a variable with an undefined expression.
  2231. @end itemize
  2232. Readers who are aware of such issues as pointer equality or intensional
  2233. @cindex pointer equality
  2234. @cindex pointers
  2235. versus extensional equality of functions are urged to forget all about
  2236. them in the context of this document, and abide only by what is
  2237. stated. Other readers should ignore this paragraph.
  2238. @node A Minimal Set of Properties, A Simple Lisp Like Language, On Equality, Virtual Code Semantics
  2239. @subsection A Minimal Set of Properties
  2240. For any trees @code{@var{x}}, @code{@var{y}}, and @code{@var{k}}, and any non-@code{nil}
  2241. trees @code{@var{p}}, @code{@var{f}}, and @code{@var{g}}, the new invisible operator satisfies these
  2242. conditions. In these expressions and hereafter, increasing abuse of
  2243. notation is perpetrated by not writing the @code{cons} in expressions of the form
  2244. @code{cons(@var{x},@var{y})}.
  2245. @table @emph
  2246. @item P0
  2247. @code{(nil,(nil,nil)) @var{x}} = @code{@var{x}}
  2248. @item P1
  2249. @code{(nil,((nil,nil),nil)) (@var{x},@var{y})} = @code{@var{x}}
  2250. @item P2
  2251. @code{(nil,(nil,(nil,nil))) (@var{x},@var{y})} = @code{@var{y}}
  2252. @item P3
  2253. @code{((nil,@var{k}),nil) @var{x}} = @code{@var{k}}
  2254. @item P4
  2255. @code{(((nil,(nil,nil)),nil),nil) (@var{f},@var{x})} = @code{@var{f} (@var{f},@var{x})}
  2256. @item P5
  2257. @code{((@var{f},@var{g}),nil) @var{x}} = @code{@var{f} @var{g} @var{x}}
  2258. @item P6
  2259. @code{((@var{f},nil),@var{g}) @var{x}} = @code{(@var{f} @var{x},@var{g} @var{x})}
  2260. @item P7
  2261. @code{((@var{p},@var{f}),@var{g}) @var{x}} = @code{@var{f} @var{x}} if
  2262. @code{@var{p} @var{x}} is a non-@code{nil} tree,
  2263. but @code{@var{g} @var{x}} if @code{@var{p} @var{x}} = @code{nil}
  2264. @end table
  2265. @cindex properties
  2266. @cindex operator properties
  2267. Although other properties remain to be described, it is worth pausing at
  2268. this point because there is ample food for thought in the ones already
  2269. given. An obvious question would be that of their origin. The short
  2270. answer is that they have been chosen arbitrarily to be true by
  2271. definition of the operator. At best, the completion of the construction
  2272. may lead to a more satisfactory answer based on aesthetic or engineering
  2273. grounds.
  2274. A more important question would be that of the relevance of the mystery
  2275. operator and its properties to the stated purpose of this section, which
  2276. is to specify the virtual machine code semantics. The answer lies in
  2277. that the operator induces a function for any given tree @code{@var{t}},
  2278. such that the value returned by the function when given an argument
  2279. @var{x} is @code{@var{t} @var{x}}. This function is the one that is
  2280. implemented by the virtual code @var{t}, which is to say the way an
  2281. application will behave if we put @var{t} in its virtual code file. An
  2282. equivalent way of looking at the situation is that the virtual machine
  2283. does nothing but compute the result of this operator, taking the tree in
  2284. the virtual code file as its left operand and the input data as the
  2285. right operand. By knowing what the operator will do with a given pair of
  2286. operands, we know what to put into the virtual code file to get the
  2287. function we want.
  2288. @cindex universality
  2289. @cindex Turing equivalence
  2290. @cindex exceptions
  2291. @cindex lists
  2292. It is worthwhile to note that properties @emph{P0} to @emph{P7} are
  2293. sufficient for universality in the sense of Turing equivalence. That
  2294. means that any computable function could be implemented by the suitable
  2295. choice of a tree @var{t} without recourse to any other properties of the
  2296. operator. A compiler writer who finds this material boring could
  2297. therefore stop reading at this point and carry out the task of targeting
  2298. any general purpose programming language to the virtual machine based on
  2299. the specifications already given. However, such an implementation would
  2300. not take advantage of the features for list processing, exception
  2301. handling, or profiling that are also built into the virtual
  2302. machine and have yet to be described.
  2303. @node A Simple Lisp Like Language, How @code{avram} Thinks, A Minimal Set of Properties, Virtual Code Semantics
  2304. @subsection A Simple Lisp Like Language
  2305. @cindex @code{silly}
  2306. With a universal computational model already at our disposal, it will be
  2307. easier to use the virtual machine to specify itself than to define all
  2308. of it from scratch. For this purpose, we use the @code{silly}
  2309. programming language, whose name is an acronym for SImple Lisp-like
  2310. Language (Yeah right). The language serves essentially as a thin layer
  2311. of symbolic names on top of the virtual machine code. Due to its poor
  2312. support for modularity and abstraction, @code{silly} is not recommended
  2313. for serious application development, but at least it has a shallow
  2314. learning curve.@footnote{Previous releases of @code{avram} included a
  2315. working @code{silly} compiler, but this has now been superseded
  2316. by the Ursala programming language. Ursala includes @code{silly} as a subset
  2317. for the most part, and the examples in this manual should compile and
  2318. execute with very little modification.}
  2319. @menu
  2320. * Syntax::
  2321. * Semantics::
  2322. * Standard Library::
  2323. @end menu
  2324. @node Syntax, Semantics, A Simple Lisp Like Language, A Simple Lisp Like Language
  2325. @subsubsection Syntax
  2326. @code{silly} has no reserved words, but it has equals signs, commas and
  2327. parentheses built in. A concise but ambiguous grammar for it can be given
  2328. as follows.
  2329. @cindex syntax
  2330. @cindex grammar
  2331. @display
  2332. @var{program} ::= @var{declaration}*
  2333. @var{declaration} ::= @var{identifier} @code{=} @var{expression}
  2334. @iftex
  2335. @var{expression} ::= @code{()} | @var{identifier} | @code{(@var{expression})} | @code{(@var{expression},@var{expression})}
  2336. | @var{expression} @var{expression} | @var{expression}@code{(}@var{expression}@code{)}
  2337. | @var{expression}@code{(}@var{expression}@code{,}@var{expression}@code{)}
  2338. @end iftex
  2339. @ifinfo
  2340. @var{expression} ::= () | @var{identifier}
  2341. | (@var{expression})
  2342. | (@var{expression},@var{expression})
  2343. | @var{expression} @var{expression}
  2344. | @var{expression}(@var{expression})
  2345. | @var{expression}(@var{expression},@var{expression})
  2346. @end ifinfo
  2347. @end display
  2348. @noindent
  2349. @cindex precedence
  2350. @cindex operator precedence
  2351. The real grammar is consistent with this one but enforces right
  2352. associativity for binary operations and higher precedence for juxtaposition
  2353. without intervening white space.
  2354. The declaration of any identifier must be unique and must precede its
  2355. occurrence in any expression. Hence, cyclic dependences between
  2356. declarations and ``recursive'' declarations are not allowed.
  2357. @node Semantics, Standard Library, Syntax, A Simple Lisp Like Language
  2358. @subsubsection Semantics
  2359. Declarations in @code{silly} should be understood in the obvious way as
  2360. preprocessor directives to perform parenthetic textual substitutions (similar to
  2361. @code{#define id (exp)} in C). All identifiers in expressions are thereby
  2362. eliminated during the preprocessing phase.
  2363. @cindex semantic function
  2364. The overall meaning of the program is the meaning of the expression in
  2365. the last declaration. A denotational semantics for expressions is given
  2366. by the following equations, where [[@code{@var{x}}]] should be read as
  2367. ``the meaning of @code{@var{x}}'', and @code{@var{x}}, @code{@var{y}} and @code{@var{z}}
  2368. are metavariables. (That is, they stand for any source code fragment
  2369. that could fit there subject to the constraint, informally speaking,
  2370. that it has to correspond to a connected subtree of the parse tree as enforced
  2371. by the unambiguous grammar in the context of the rest of the program.)
  2372. @display
  2373. [[@code{()}]] = @code{nil}
  2374. [[@code{(@var{x})}]] = [[@code{@var{x}}]]
  2375. [[@code{(@var{x},@var{y})}]] = @code{cons(}[[@code{@var{x}}]]@code{,}[[@code{@var{y}}]]@code{)}
  2376. [[@code{@var{x} @var{y}}]] = [[@code{@var{x}(@var{y})}]] = [[@code{@var{x}}]] [[@code{@var{y}}]]
  2377. [[@code{@var{x} (@var{y},@var{z})}]] = [[@code{@var{x}(@var{y},@var{z})}]] = [[@code{@var{x}}]] [[@code{(@var{y},@var{z})}]]
  2378. @end display
  2379. @noindent
  2380. Toy languages like this are among the few situations a
  2381. @cindex denotational semantics
  2382. denotational semantics stands a chance of clarifying more than it
  2383. obfuscates, so the reader is invited to take a moment to savor it.
  2384. @node Standard Library, , Semantics, A Simple Lisp Like Language
  2385. @subsubsection Standard Library
  2386. @code{silly} programs may be linked with library modules, which consist
  2387. of @code{silly} source text to be concatenated with the user
  2388. @cindex library modules
  2389. @cindex standard prelude
  2390. program prior to the preprocessing phase. Most @code{silly} programs are
  2391. linked with the standard @code{silly} prelude, which contains the
  2392. following declarations among others.
  2393. @cindex @code{nil}
  2394. @cindex @code{identity}
  2395. @cindex @code{left}
  2396. @cindex @code{right}
  2397. @cindex @code{meta}
  2398. @cindex @code{constant}
  2399. @cindex @code{couple}
  2400. @cindex @code{compose}
  2401. @cindex @code{conditional}
  2402. @example
  2403. nil = ()
  2404. identity = (nil,(nil,nil))
  2405. left = (nil,((nil,nil),nil))
  2406. right = (nil,(nil,(nil,nil)))
  2407. meta = (((nil,(nil,nil)),nil),nil)
  2408. constant_nil = ((nil,nil),nil)
  2409. couple = ((((left,nil),constant_nil),nil),right)
  2410. compose = couple(identity,constant_nil)
  2411. constant = couple(couple(constant_nil,identity),constant_nil)
  2412. conditional =
  2413. couple(couple(left,compose(left,right)),compose(right,right))
  2414. @end example
  2415. There is a close correspondence between these declarations and the
  2416. properties described in @ref{A Minimal Set of Properties}. A fitting analogy would be that
  2417. the properties of the operator specify the virtual machine instruction
  2418. set in a language independent way, and the @code{silly} library defines
  2419. the instruction mnemonics for a virtual assembly language. The
  2420. @cindex mnemonics
  2421. relationship of some mnemonics to their corresponding instructions may
  2422. be less clear than that of others, so they are all discussed next.
  2423. @node How @code{avram} Thinks, Variable Freedom, A Simple Lisp Like Language, Virtual Code Semantics
  2424. @subsection How @code{avram} Thinks
  2425. The definitions in the standard @code{silly} library pertaining to the
  2426. basic properties of the operator can provide a good intuitive
  2427. illustration of how computations are performed by @code{avram}. This
  2428. task is approached in the guise of a few trivial correctness proofs
  2429. about them. Conveniently, as an infeasibly small language, @code{silly}
  2430. is an ideal candidate for analysis by formal methods.
  2431. Technically the semantic function [[@dots{}]] has not been defined on
  2432. identifiers, but we can easily extend it to them by stipulating that the
  2433. meaning of an identifier @code{@var{x}} is the meaning of the program
  2434. @cindex identifiers
  2435. @code{@var{main} = @var{x}} when linked with a library containing the
  2436. declaration of @code{@var{x}}, where @code{@var{main}} is an identifier
  2437. not appearing elsewhere in the library.
  2438. With this idea in mind, the following ``theorems'' can be stated,
  2439. all of which have a similar proof. The variables @var{x} and @var{y}
  2440. stand for any tree, and the variable @var{f} stands for any tree other
  2441. than @code{nil}.
  2442. @table @emph
  2443. @item T0
  2444. [[@code{identity}]] @code{@var{x}} = @code{@var{x}}
  2445. @item T1
  2446. [[@code{left}]] @code{(@var{x},@var{y})} = @code{@var{x}}
  2447. @item T2
  2448. [[@code{right}]] @code{(@var{x},@var{y})} = @code{@var{y}}
  2449. @item T4
  2450. [[@code{meta}]] @code{(@var{f},@var{x})} = @code{@var{f} (@var{f},@var{x})}
  2451. @item T5
  2452. [[@code{constant_nil}]] @code{@var{x}} = @code{nil}
  2453. @end table
  2454. @noindent
  2455. Replacing each identifier with its defining expression directly
  2456. demonstrates a logical equivalence between the relevant theorem and one
  2457. of the basic operator properties postulated in @ref{A Minimal Set of Properties}.
  2458. For more of a challenge, it is possible to prove the next theorem.
  2459. @table @emph
  2460. @item T6
  2461. For non-@code{nil} @code{@var{f}} and @code{@var{g}},
  2462. ([[@code{couple}]] @code{(@var{f},@var{g})}) @code{@var{x}} =
  2463. @code{(@var{f} @var{x},@var{g} @var{x})}
  2464. @end table
  2465. @noindent
  2466. The proof is a routine calculation. Beware of the distinction between
  2467. the mathematical @code{nil} and the @code{silly} identifier @code{nil}.
  2468. @ifnotinfo
  2469. @format
  2470. ([[@code{couple}]] @code{(@var{f},@var{g})}) @code{@var{x}} = ([[@code{((((left,nil),constant_nil),nil),right)}]] @code{(@var{f},@var{g})}) @code{@var{x}}
  2471. by substitution of @code{couple} with its definition in the standard library
  2472. = (@code{((((}[[@code{left}]]@code{,}[[@code{nil}]]@code{),}[[@code{constant_nil}]]@code{),}[[@code{nil}]])@code{,}[[@code{right}]]@code{)} @code{(@var{f},@var{g})}) @code{@var{x}}
  2473. by definition of the semantic function [[@dots{}]] regarding pairs
  2474. = (@code{((((}[[@code{left}]]@code{,}[[@code{()}]]@code{),}[[@code{constant_nil}]]@code{),}[[@code{()}]])@code{,}[[@code{right}]]@code{)} @code{(@var{f},@var{g})}) @code{@var{x}}
  2475. by substitution of @code{nil} from its definition in the standard library
  2476. = (@code{((((}[[@code{left}]]@code{,}@code{nil}@code{),}[[@code{constant_nil}]]@code{),}@code{nil})@code{,}[[@code{right}]]@code{)} @code{(@var{f},@var{g})}) @code{@var{x}}
  2477. by definition of the semantic function in the case of [[@code{()}]]
  2478. = (@code{(}[[@code{left}]] @code{(@var{f},@var{g}),}[[@code{constant_nil}]] @code{(@var{f},@var{g})),}[[@code{right}]] @code{(@var{f},@var{g})}) @code{@var{x}}
  2479. by property @emph{P6} (twice)
  2480. = @code{((@var{f},nil),@var{g}) @var{x}}
  2481. by theorems @emph{T1}, @emph{T2}, and @emph{T5}
  2482. = @code{(@var{f} @var{x},@var{g} @var{x})}
  2483. by property @emph{P6} again.
  2484. @end format
  2485. @end ifnotinfo
  2486. @ifinfo
  2487. @ifnothtml
  2488. @format
  2489. ([[@code{couple}]] @code{(@var{f},@var{g})}) @code{@var{x}}
  2490. = ([[@code{((((left,nil),constant_nil),nil),right)}]] @code{(@var{f},@var{g})}) @code{@var{x}}
  2491. by substitution of @code{couple} with its definition in the standard library
  2492. = (
  2493. @code{(
  2494. (((}[[@code{left}]]@code{,}[[@code{nil}]]@code{),}[[@code{constant_nil}]]@code{),}[[@code{nil}]])@code{,}
  2495. [[@code{right}]]@code{)}
  2496. @code{(@var{f},@var{g})})
  2497. @code{@var{x}}
  2498. by definition of the semantic function [[@dots{}]] regarding pairs
  2499. = (
  2500. @code{(
  2501. (((}[[@code{left}]]@code{,}[[@code{()}]]@code{),}[[@code{constant_nil}]]@code{),}[[@code{()}]])@code{,}
  2502. [[@code{right}]]@code{)}
  2503. @code{(@var{f},@var{g})})
  2504. @code{@var{x}}
  2505. by substitution of @code{nil} from its definition in the standard library
  2506. = (
  2507. @code{(
  2508. (((}[[@code{left}]]@code{,}[[@code{nil}]]@code{),}[[@code{constant_nil}]]@code{),}[[@code{nil}]])@code{,}
  2509. [[@code{right}]]@code{)}
  2510. @code{(@var{f},@var{g})})
  2511. @code{@var{x}}
  2512. by definition of the semantic function in the case of [[@code{()}]]
  2513. = (
  2514. @code{(}
  2515. [[@code{left}]] @code{(@var{f},@var{g}),}[[@code{constant_nil}]] @code{(@var{f},@var{g})),}
  2516. [[@code{right}]] @code{(@var{f},@var{g})})
  2517. @code{@var{x}}
  2518. by property @emph{P6} (twice)
  2519. = @code{((@var{f},nil),@var{g}) @var{x}}
  2520. by theorems @emph{T1}, @emph{T2}, and @emph{T5}
  2521. = @code{(@var{f} @var{x},@var{g} @var{x})}
  2522. by property @emph{P6} again.
  2523. @end format
  2524. @end ifnothtml
  2525. @end ifinfo
  2526. Something to observe about this proof is that it might just as well have
  2527. been done automatically. Every step is either the substitution of an
  2528. identifier or a pattern match against existing theorems and properties
  2529. of the operator. Another thing to note is that the use of identifiers
  2530. and previously established theorems helps to make the proof human
  2531. readable, but is not a logical necessity. An equivalent proof could have
  2532. been expressed entirely in terms of the properties of the operator. If
  2533. one envisions a proof like this being performed blindly and
  2534. mechanically, without the running commentary or other amenities, that
  2535. would not be a bad way of thinking about what takes place when
  2536. @code{avram} executes virtual code.
  2537. Three more theorems have similar proofs. For non-@code{nil}
  2538. trees @code{@var{p}}, @code{@var{f}} and @code{@var{g}}, and any trees
  2539. @code{@var{x}} and @code{@var{k}}:
  2540. @cindex @code{compose}
  2541. @cindex @code{constant}
  2542. @cindex @code{conditional}
  2543. @table @emph
  2544. @item T7
  2545. ([[@code{compose}]] @code{(@var{f},@var{g})}) @var{x} = @var{f} @var{g} @var{x}
  2546. @item T8
  2547. ([[@code{constant}]] @code{@var{k}}) @var{x} = @var{k}
  2548. @item T9
  2549. ([[@code{conditional}]] @code{(@var{p},(@var{f},@var{g})}) @var{x} =
  2550. @code{@var{f} @var{x}} if
  2551. @code{@var{p} @var{x}} is non-@code{nil},
  2552. but @code{@var{g} @var{x}} if @code{@var{p} @var{x}} = @code{nil}
  2553. @end table
  2554. @noindent
  2555. The proofs of these theorems are routine calculations analogous to the
  2556. proof of @emph{T6}. Here is a proof of theorem @emph{T7} for good
  2557. measure.
  2558. @ifnotinfo
  2559. @format
  2560. ([[@code{compose}]] @code{(@var{f},@var{g})}) @code{@var{x}} = ([[@code{couple(identity,constant_nil)}]] @code{(@var{f},@var{g})}) @code{@var{x}}
  2561. @end format
  2562. @end ifnotinfo
  2563. @ifinfo
  2564. @ifnothtml
  2565. @format
  2566. ([[@code{compose}]] @code{(@var{f},@var{g})}) @code{@var{x}}
  2567. = ([[@code{couple(identity,constant_nil)}]] @code{(@var{f},@var{g})}) @code{@var{x}}
  2568. @end format
  2569. @end ifnothtml
  2570. @end ifinfo
  2571. @iftex
  2572. @display
  2573. @end display
  2574. @noindent
  2575. @end iftex
  2576. by substitution of @code{compose} with its definition in the standard library
  2577. @format
  2578. = (@code{(}[[@code{couple}]] @code{(}[[@code{identity}]]@code{,}[[@code{constant_nil}]]@code{))(@var{f},@var{g})}) @code{@var{x}}
  2579. by definition of the semantic function
  2580. = @code{(}[[@code{identity}]] @code{(@var{f},@var{g}),}[[@code{constant_nil}]]@code{ (@var{f},@var{g})) @var{x}}
  2581. by theorem @emph{T6}
  2582. = @code{((@var{f},@var{g}),nil) @var{x}}
  2583. by theorems @emph{T0} and @emph{T5}
  2584. = @code{@var{f} @var{g} @var{x}}
  2585. by property @emph{P5} of the operator.
  2586. @end format
  2587. @node Variable Freedom, Metrics and Maintenance, How @code{avram} Thinks, Virtual Code Semantics
  2588. @subsection Variable Freedom
  2589. The virtual code semantics is easier to specify using the
  2590. @code{silly} language than it would be without it, but still awkward in
  2591. some cases. An example is the following declaration from the standard
  2592. library,
  2593. @cindex @code{hired}
  2594. @example
  2595. hired = compose(
  2596. compose,
  2597. couple(
  2598. constant compose,
  2599. compose(couple,couple(constant,constant couple))))
  2600. @end example
  2601. @noindent
  2602. which is constructed in such a way as to imply the following theorem,
  2603. provable by routine computation.
  2604. @table @emph
  2605. @item T9
  2606. @code{(}[[@code{hired}]] @code{@var{h}) (@var{f},@var{g})} = [[@code{compose}]]@code{(@var{h},}[[@code{couple}]]@code{(@var{f},@var{g}))}
  2607. @end table
  2608. @noindent
  2609. Intuitively, @code{hired} represents a function that takes a given
  2610. function to a higher order function. For example, if @code{f} were a
  2611. function that adds two real numbers, @code{hired f} would be a function that
  2612. takes two real valued functions to their pointwise sum.
  2613. Apart from its cleverness, such an opaque way of defining a function has
  2614. little to recommend it. The statement of the theorem about the function
  2615. is more readable than the function definition itself, probably because
  2616. the theorem liberally employs mathematical variables, whereas the
  2617. @code{silly} language is variable free. On the other hand, it is not
  2618. worthwhile to linger over further enhancements to the language, such as
  2619. adding variables to it. The solution will be to indicate informally a
  2620. general method of inferring a variable free function definition from an
  2621. expression containing variables, and hereafter omit the more
  2622. cumbersome definitions.
  2623. @cindex @code{isolate}
  2624. @cindex variables
  2625. An algorithm called @code{isolate} does the job.
  2626. The input to @code{isolate} is a pair @code{(@var{e},@var{x})}, where
  2627. @code{@var{e}} is a syntactically correct @code{silly} expression in
  2628. which the identifier @code{@var{x}} may occur, but no other identifiers
  2629. dependent on @code{@var{x}} may occur (or else it's
  2630. garbage-in/garbage-out). Output is a syntactically correct @code{silly}
  2631. expression @code{@var{f}} in which the identifier @code{@var{x}} does
  2632. not occur, such that [[@code{@var{e}}]] = [[@code{@var{f} @var{x}}]].
  2633. The algorithm is as follows,
  2634. @display
  2635. if @code{@var{e}} = @code{@var{x}} then
  2636. return @code{identity}
  2637. else if @code{@var{e}} is of the form @code{(@var{u},@var{v})}
  2638. return @code{couple(isolate(@var{u},@var{x}),isolate(@var{v},@var{x}))}
  2639. else if @code{@var{e}} is of the form @code{@var{u} @var{v}}
  2640. return @code{(hired apply)(isolate(@var{u},@var{x}),isolate(@var{v},@var{x}))}
  2641. else
  2642. return @code{constant @var{e}}
  2643. @end display
  2644. @noindent
  2645. @cindex equality
  2646. where equality is by literal comparison of expressions, and the
  2647. definition of @code{apply} is
  2648. @cindex @code{apply}
  2649. @example
  2650. apply = (hired meta)((hired compose)(left,constant right),right)
  2651. @end example
  2652. @noindent
  2653. which represents a function that does the same thing as the invisible
  2654. operator.
  2655. @table @emph
  2656. @item T10
  2657. [[@code{apply}]] @code{(@var{f},@var{x})} = @code{@var{f} @var{x}}
  2658. @end table
  2659. The @code{isolate} algorithm can be generalized to functions of
  2660. arbitrarily many variables, but in this document we will need
  2661. only a unary and a binary version. The latter takes an expression
  2662. @code{@var{e}} and a pair of identifiers @code{(@var{x},@var{y})} as
  2663. input, and returns an expression @code{@var{f}} such that
  2664. [[@code{@var{e}}]] = [[@code{@var{f} (@var{x},@var{y})}]].
  2665. @display
  2666. if @code{@var{e}} = @code{@var{x}} then
  2667. return @code{left}
  2668. else if @code{@var{e}} = @code{@var{y}} then
  2669. return @code{right}
  2670. else if @code{@var{e}} is of the form @code{(@var{u},@var{v})}
  2671. return @code{couple(isolate(@var{u},(@var{x},@var{y})),isolate(@var{v},(@var{x},@var{y})))}
  2672. else if @code{@var{e}} is of the form @code{@var{u} @var{v}}
  2673. return @code{(hired apply)(isolate(@var{u},(@var{x},@var{y})),isolate(@var{v},(@var{x},@var{y})))}
  2674. else
  2675. return @code{constant @var{e}}
  2676. @end display
  2677. It might be noted in passing that something similar to these algorithms
  2678. would be needed in a compiler targeted to @code{avram} if the source
  2679. were a functional language with variables.
  2680. @node Metrics and Maintenance, Deconstruction, Variable Freedom, Virtual Code Semantics
  2681. @subsection Metrics and Maintenance
  2682. Certain features of the virtual machine pertain to software development
  2683. and maintenance more than to implementing any particular function. The
  2684. operations with the mnemonics @code{version}, @code{note},
  2685. @code{profile}, and @code{weight} are in this category.
  2686. @menu
  2687. * Version::
  2688. * Note::
  2689. * Profile::
  2690. * Weight::
  2691. @end menu
  2692. @node Version, Note, Metrics and Maintenance, Metrics and Maintenance
  2693. @subsubsection Version
  2694. A virtual code application with exactly the following definition
  2695. implements a function that returns a constant character string
  2696. regardless of its argument.
  2697. @cindex @code{version}
  2698. @example
  2699. version = ((nil,nil),((nil,nil),(nil,((nil,nil),nil))))
  2700. @end example
  2701. @noindent
  2702. The character string encodes the version number of the installed
  2703. @code{avram} executable, for example @code{@value{VERSION}}, using the standard
  2704. representation for characters.
  2705. Although such an application is useless by itself, the intended use for
  2706. this feature is to cope with the possibility that future versions of
  2707. @code{avram} may include enhancements. Ideally, the maintainer of
  2708. @code{avram} will update the version number when new enhancements are
  2709. added. Applications can then detect whether they are available in the
  2710. installed version by using this feature. If a needed enhancement is not
  2711. available, the application can either make allowances or at least
  2712. terminate gracefully.
  2713. @node Note, Profile, Version, Metrics and Maintenance
  2714. @subsubsection Note
  2715. This operation allows arbitrary information or comments to be embedded
  2716. in a virtual code application in such a way that it will be ignored by
  2717. @code{avram} when executing it. For the @code{silly} language, a
  2718. @code{note} function is defined in the standard prelude so as to imply
  2719. the following theorem.
  2720. @cindex @code{note}
  2721. @cindex annotations
  2722. @table @emph
  2723. @item T11
  2724. [[@code{note}]] @code{(@var{f},@var{c})} = @code{((nil,nil),((nil,nil),(nil,(nil,(@var{f},@var{c})))))}
  2725. @end table
  2726. @noindent
  2727. Intuitively, the argument @code{@var{f}} represents a function, and the
  2728. argument @code{c} represents the comment, annotation, or whatever, that
  2729. will be embedded but ignored in the virtual code.
  2730. Semantically, a function with a note attached is the same as the
  2731. function by itself, as the following property stipulates for
  2732. any non-@code{nil} @code{@var{f}}.
  2733. @table @emph
  2734. @item P8
  2735. ([[@code{note}]] @code{(@var{f},@var{c})}) @code{@var{x}} = @code{@var{f} @var{x}}
  2736. @end table
  2737. A possible reason for using this feature might be to support a language
  2738. that performs run-time type checking by hanging type tags on
  2739. @cindex type tags
  2740. everything. Another possible use would be to include symbolic
  2741. information needed by a debugger.
  2742. @node Profile, Weight, Note, Metrics and Maintenance
  2743. @subsubsection Profile
  2744. The virtual machine supports a profiling capability by way of this
  2745. @cindex @file{profile.txt}
  2746. feature. Profiling an application causes run time statistics about it to
  2747. be written to a file @file{./profile.txt}. Profiled applications are of
  2748. the form indicated in the following theorem
  2749. @table @emph
  2750. @item T12
  2751. [[@code{profile}]] @code{(@var{f},@var{s})} = @code{((nil,nil),((nil,nil),(nil,((@var{f},@var{s}),nil))))}
  2752. @end table
  2753. @noindent
  2754. where @code{@var{f}} stands for the virtual code of the application, and
  2755. @code{@var{s}} stands for the name of it to be written to the file.
  2756. The semantics of a profiled function is identical to the unprofiled
  2757. form for any non-@code{nil} @code{@var{f}}.
  2758. @table @emph
  2759. @item P9
  2760. ([[@code{profile}]] @code{(@var{f},@var{s})}) @code{@var{x}} = @code{@var{f} @var{x}}
  2761. @end table
  2762. Unlike the situation with @code{note}, the annotation @code{@var{s}} of
  2763. @cindex @code{note}
  2764. used in profiled code is not in an unrestricted format but must be a
  2765. character string in the standard representation (as in
  2766. @ref{Representation of Numeric and Textual Data}),
  2767. because this string needs to be written by @code{avram} to the file
  2768. @file{./profile.txt}. Ordinarily this string will be the source code
  2769. identifier of the function being profiled.
  2770. When profiles are used in many parts of an application, an informative
  2771. table is generated showing the time spent in each part.
  2772. @node Weight, , Profile, Metrics and Maintenance
  2773. @subsubsection Weight
  2774. The following virtual code implements a function that returns the weight
  2775. of its argument in the standard representation for natural numbers.
  2776. @cindex @code{weight}
  2777. @example
  2778. weight = ((nil,nil),((nil,nil),(nil,(nil,nil))))
  2779. @end example
  2780. @noindent
  2781. The weight of a tree is zero if the tree is @code{nil}, and otherwise
  2782. the sum of the weights of the two subtrees plus one.
  2783. An algorithm to compute the weight of a tree would be trivial to
  2784. implement without being built in to the virtual machine. The built in
  2785. capability could also be used for purposes unrelated to code
  2786. maintenance. Nevertheless, it is built in for the following reasons.
  2787. @itemize @bullet
  2788. @item Computing weights happened to be a bottleneck for a particular
  2789. aspect of code generation that was of interest to the author,
  2790. @cindex compression
  2791. namely the compression of generated code.
  2792. @item A built in implementation in C runs at least an order of magnitude
  2793. faster than the equivalent implementation in virtual code.
  2794. @item It runs even faster when repeated on the same data, by retrieving
  2795. previously calculated weights rather than recalculating them.
  2796. @end itemize
  2797. The only disadvantage of using this feature instead of implementing a
  2798. function in virtual code to compute weights is that it relies on native
  2799. @cindex native integer arithmetic
  2800. @cindex overflow
  2801. integer arithmetic and could overflow, causing a fatal error. It has
  2802. never occurred in practice, but is possible due to sharing, whereby the
  2803. nominal weight of a tree could be exponentially larger than the actual
  2804. amount of memory occupied by it.
  2805. @node Deconstruction, Recursion, Metrics and Maintenance, Virtual Code Semantics
  2806. @subsection Deconstruction
  2807. Much of the time required for evaluating a function is devoted to
  2808. @cindex deconstruction
  2809. performing deconstruction operations, e.g., taking the left side of a
  2810. pair, the tail of a list, the right side of the head of the tail, etc..
  2811. Because these operations are so frequent, there are some features of the
  2812. virtual machine to make them as efficient as possible.
  2813. @menu
  2814. * Field::
  2815. * Fan::
  2816. @end menu
  2817. @node Field, Fan, Deconstruction, Deconstruction
  2818. @subsubsection Field
  2819. The virtual machine supports a generalization of the @code{left} and
  2820. @cindex @code{left}
  2821. @cindex @code{right}
  2822. @code{right} deconstruction operations that is applicable to deeply nested
  2823. structures. Use of this feature is conducive to code that is faster and
  2824. more compact than is possible by relying on the primitive deconstructors
  2825. alone. It may also be easier for a code optimizer to recognize and
  2826. transform.
  2827. The general form of a virtual code application to perform deconstruction
  2828. is that it is a pair with a @code{nil} left side, and a non-@code{nil}
  2829. right side. The right side indicates the nature of the deconstruction to
  2830. be performed when the function is evaluated on an argument.
  2831. To make the expression of deconstruction functions more readable in
  2832. @code{silly}, the standard library contains the declaration
  2833. @example
  2834. field = couple(constant nil,identity)
  2835. @end example
  2836. @noindent
  2837. which implies the following theorem.
  2838. @table @emph
  2839. @item T13
  2840. [[@code{field}]] @code{@var{w}} = @code{(nil,@var{w})}
  2841. @end table
  2842. @cindex @code{field}
  2843. The virtual machine recognizes an application in this form and evaluates
  2844. it according to the following properties, where @code{@var{u}} and
  2845. @code{@var{v}} are other than @code{nil}, but @code{@var{x}},
  2846. @code{@var{y}}, and @code{@var{z}} are unrestricted.
  2847. @table @emph
  2848. @item P10
  2849. ([[@code{field}]] @code{(@var{u},nil)}) @code{(@var{x},@var{y})} = ([[@code{field}]] @code{@var{u}}) @code{@var{x}}
  2850. @item P11
  2851. ([[@code{field}]] @code{(nil,@var{v})}) @code{(@var{x},@var{y})} = ([[@code{field}]] @code{@var{v}}) @code{@var{y}}
  2852. @item P12
  2853. ([[@code{field}]] @code{(@var{u},@code{v})}) @code{@var{z}} = @code{((}[[@code{field}]] @code{@var{u}) @var{z},(}[[@code{field}]] @code{@var{v}) @var{z})}
  2854. @end table
  2855. @noindent
  2856. One might also add that ([[@code{field}]] @code{(nil,nil)})
  2857. @code{@var{z}} = @code{@var{z}}, but this statement would be equivalent to
  2858. @emph{P0}.
  2859. A suitable choice of the @code{field} operand permits the implementation
  2860. of any deconstruction function expressible in terms of @code{compose},
  2861. @code{couple}, @code{identity}, @code{left} and @code{right}. For
  2862. example, the application @code{couple(compose(right,right),left)} has an
  2863. equivalent representation in
  2864. @code{field((nil,(nil,(nil,nil))),((nil,nil),nil))}. The latter looks
  2865. longer in @code{silly} but is smaller and faster in virtual code.
  2866. @node Fan, , Field, Deconstruction
  2867. @subsubsection Fan
  2868. @cindex @code{fan}
  2869. In cases where a deconstructions would be needed to apply the same
  2870. function to both sides of a pair, the overhead can be avoided by means
  2871. of a property of the virtual machine intended for that purpose.
  2872. A @code{silly} definition of @code{fan} implying the following theorem is
  2873. helpful in expressing such an application.
  2874. @table @emph
  2875. @item T14
  2876. [[@code{fan}]] @code{@var{f}} = @code{((nil,nil),((nil,@var{f}),(nil,nil)))}
  2877. @end table
  2878. @noindent
  2879. The virtual machine recognizes when an application has the form shown
  2880. above, and uses @code{@var{f}} as a function to be applied to both sides
  2881. of the argument.
  2882. @table @emph
  2883. @item P13
  2884. ([[@code{fan}]] @code{@var{f}}) @code{(@var{x},@var{y})} = @code{(@var{f} @var{x},@var{f} @var{y})}
  2885. @end table
  2886. @node Recursion, Assignment, Deconstruction, Virtual Code Semantics
  2887. @subsection Recursion
  2888. @cindex recursion
  2889. Defining functions or programs self referentially is sometimes
  2890. informally known as recursion. In functional languages, the clever use
  2891. @cindex combinators
  2892. @cindex functional programming
  2893. of ``combinators'' is often preferred to this practice, and is in fact well
  2894. supported by the virtual machine. However, some computations may be
  2895. inexpressible without an explicitly ``recursive'' formulation, so there
  2896. is some support for that as well.
  2897. @menu
  2898. * Recur::
  2899. * Refer::
  2900. @end menu
  2901. @node Recur, Refer, Recursion, Recursion
  2902. @subsubsection Recur
  2903. @cindex @code{meta}
  2904. @cindex @code{recur}
  2905. A generalization of the form denoted by @code{meta} in @code{silly} is
  2906. recognized by the virtual machine and allows a slightly more efficient
  2907. encoding of recursive applications. An expression @code{recur @var{p}}
  2908. has the representation indicated by this theorem,
  2909. @table @emph
  2910. @item T15
  2911. [[@code{recur}]] @code{@var{p}} = @code{(((nil,@var{p}),nil),nil)}
  2912. @end table
  2913. @noindent
  2914. which implies that [[@code{meta}]] = [[@code{recur}]] @code{(nil,nil)}.
  2915. If @code{@var{p}} is non-@code{nil}, a tree of the form [[@code{recur}]]
  2916. @code{@var{p}} is interpreted as follows. Note that @emph{P4} is
  2917. equivalent to the special case of this property for which @code{@var{p}}
  2918. is @code{(nil,nil)}.
  2919. @table @emph
  2920. @item P14
  2921. ([[@code{recur}]] @code{@var{p}}) @code{@var{x}} = [[@code{meta}]] ([[@code{field}]] @code{@var{p}}) @code{@var{x}}
  2922. @end table
  2923. The rationale is that @code{meta} would very frequently be composed with
  2924. a deconstruction @code{field @var{p}}, so the virtual machine saves some
  2925. time and space by allowing the two of them to be encoded in a smaller
  2926. tree with the combined meaning.
  2927. @node Refer, , Recur, Recursion
  2928. @subsubsection Refer
  2929. @cindex @code{refer}
  2930. In the style of recursive programming compelled by the available
  2931. @code{meta} primitive, a function effectively requires a copy of its own
  2932. machine code as its left argument. Bringing about that state of
  2933. affairs is an interesting party trick.
  2934. @cindex @code{bu}
  2935. If we had a definition of @code{bu} in the standard library implying
  2936. @table @emph
  2937. @item T16
  2938. ([[@code{bu}]] @code{(@var{f},@var{k})}) @code{@var{x}} = @code{@var{f}(@var{k},@var{x})}
  2939. @end table
  2940. @noindent
  2941. which for the sake of concreteness can be done like this,
  2942. @example
  2943. bu = (hired compose)(
  2944. left,
  2945. (hired couple)(compose(constant,right),constant identity))
  2946. @end example
  2947. @noindent
  2948. then a definition of @code{refer} as
  2949. @example
  2950. refer = (hired bu)(identity,identity)
  2951. @end example
  2952. @noindent
  2953. would be consistent with the following property of the operator.
  2954. @table @emph
  2955. @item P15
  2956. ([[@code{refer}]] @code{@var{f}}) @code{@var{x}} = @code{@var{f} (@var{f},@var{x})}
  2957. @end table
  2958. @noindent
  2959. The proof, as always, is a matter of routine calculation in the manner
  2960. of the section on how @code{avram} thinks.
  2961. However, this pattern would occur so frequently in recursively defined
  2962. functions as to be a significant waste of space and time. Therefore,
  2963. rather than requiring it to be defined in terms of other operations, the
  2964. virtual machine specification recognizes a pattern of the form below with
  2965. respect to property @emph{P15},
  2966. @table @emph
  2967. @item T17
  2968. [[@code{refer}]] @code{@var{f}} = @code{(((@var{f},nil),nil),nil)}
  2969. @end table
  2970. @noindent
  2971. and takes the property to be true by definition of the operator. A
  2972. definition of @code{refer} consistent with @emph{T17} is therefore to
  2973. @cindex standard library
  2974. be found in the standard library, not the definition proposed above.
  2975. @node Assignment, Predicates, Recursion, Virtual Code Semantics
  2976. @subsection Assignment
  2977. @cindex assignment
  2978. @cindex imperative programming
  2979. In an imperative programming paradigm, a machine consists partly of an
  2980. ensemble of addressable storage locations, whose contents are changed
  2981. over time by assignment statements. An assignment statement includes
  2982. some computable function of the global machine state, and the address of
  2983. the location whose contents will be overwritten with the value computed
  2984. from the function when it is evaluated.
  2985. Compiling a language containing assignment statements into virtual
  2986. machine code suitable for @code{avram} might be facilitated by
  2987. exploiting the following property.
  2988. @table @emph
  2989. @item P16
  2990. ([[@code{assign}]] @code{(@var{p},@var{f})}) @code{@var{x}} = [[@code{replace}]] @code{((@var{p},@var{f} @var{x}),@var{x})}
  2991. @end table
  2992. @noindent
  2993. The identifier @code{assign} is used in @code{silly} to express a
  2994. virtual code fragment having the form shown below, and @code{replace}
  2995. corresponds to a further operation to be explained presently.
  2996. @cindex @code{assign}
  2997. @table @emph
  2998. @item T18
  2999. [[@code{assign}]] @code{(@var{p},@var{f})} = @code{(((@var{p},@var{f}),nil),nil)}
  3000. @end table
  3001. This feature simulates assignment statements in the following way. The
  3002. variable @code{@var{x}} in @emph{P16} corresponds intuitively to the set
  3003. of addressable locations in the machine. The variable @code{@var{f}}
  3004. corresponds to the function whose value will be stored in the location
  3005. addressed by @code{@var{p}}. The result of a function expressed using
  3006. @code{assign} is a new store similar to the argument @code{@var{x}}, but
  3007. with the part of it in location @code{@var{p}} replaced by @code{@var{f}
  3008. @var{x}}. A source text with a sequence of assignment statements could
  3009. therefore be translated directly into a functional composition of trees
  3010. in this form.
  3011. @cindex storage locations
  3012. The way storage locations are modeled in virtual code using this feature
  3013. would be as nested pairs, and the address @code{@var{p}} of a location
  3014. is a tree interpreted similarly to the trees used as operands to the
  3015. @code{field} operator described in @ref{Field}, to specify
  3016. deconstructions. In fact, @code{replace} can be defined as a minimal
  3017. solution to the following equation.
  3018. @cindex @code{replace}
  3019. @table @emph
  3020. @item E0
  3021. ([[@code{field}]] @code{@var{p}}) [[@code{replace}]] @code{((@var{p},@var{y}),@var{x})} = @code{@var{y}}
  3022. @end table
  3023. This equation regrettably does
  3024. not lend itself to inferring the @code{silly} source for @code{replace}
  3025. @cindex @code{isolate}
  3026. using the @code{isolate} algorithm in @ref{Variable Freedom}, so an explicit
  3027. construction is given in @ref{Replace}. This construction need not concern a
  3028. reader who considers the equation a sufficiently precise specification
  3029. in itself.
  3030. In view of the way addresses for deconstruction are represented as
  3031. trees, it would be entirely correct to infer from this equation that a
  3032. tuple of values computed together can be assigned to a tuple of
  3033. locations. The locations don't even have to be ``contiguous'', but could
  3034. be anywhere in the tree representing the store, and the function is
  3035. computed from the contents of all of them prior to the update. Hence,
  3036. this simulation of assignment fails to capture the full inconvenience of
  3037. imperative programming except in the special case of a single value
  3038. assigned to a single location, but fortunately this case is the only one
  3039. most languages allow.
  3040. There is another benefit to this feature besides running languages with
  3041. assignment statements in them, which is the support of abstract or
  3042. opaque data structures. A function that takes an abstract data structure
  3043. as an argument and returns something of the same type can be coded in a
  3044. way that is independent of the fields it doesn't use. For example, a
  3045. data structure with three fields having the field identifiers
  3046. @code{foo}, @code{bar}, and @code{baz} in some source language might be
  3047. represented as a tuple @code{((@var{foo contents},@var{bar
  3048. contents}),@var{baz contents})} on the virtual code level. Compile time
  3049. constants like @code{bar = ((nil,(nil,nil)),nil)} could be defined in an
  3050. effort to hide the details of the representation, so that the virtual
  3051. code @code{field bar} is used instead of @code{compose(right,left)}.
  3052. Using field identifiers appropriately, a function that transforms such a
  3053. structure by operating on the @code{bar} field could have the virtual
  3054. @cindex @code{field}
  3055. code @code{couple(couple(field foo,compose(f,field bar)),field
  3056. baz)}. However, this code does not avoid depending on the representation
  3057. of the data structure, because it relies on the assumption of the @code{foo}
  3058. field being on the left of the left, and the @code{baz} field being on
  3059. the right. On the other hand, the code @code{assign(bar,compose(f,field
  3060. bar))} does the same job without depending on anything but the position
  3061. of the @code{bar} field. Furthermore, if this position were to change
  3062. relative to the others, the code maintenance would be limited to a
  3063. recompilation.
  3064. @node Predicates, Iteration, Assignment, Virtual Code Semantics
  3065. @subsection Predicates
  3066. @cindex predicates
  3067. A couple of operations are built into the virtual machine for performing
  3068. tests efficiently. These functions return either @code{nil} for false or
  3069. @code{(nil,nil)} for true, and are useful for example as a predicate
  3070. @code{@var{p}} in programs of the form
  3071. @code{conditional(@var{p},(@var{f},@var{g}))} among other things. In
  3072. this example, the predicate is applied to the argument, a result of
  3073. @code{(nil,nil)} causes @code{@var{f}} to be applied to it, and a result
  3074. of @code{nil} causes @code{@var{g}} to be applied to it.
  3075. @menu
  3076. * Compare::
  3077. * Member::
  3078. @end menu
  3079. @node Compare, Member, Predicates, Predicates
  3080. @subsubsection Compare
  3081. @cindex @code{compare}
  3082. A function that performs comparison has a the following very simple
  3083. virtual code representation.
  3084. @table @emph
  3085. @item T19
  3086. [[@code{compare}]] = @code{(nil,nil)}
  3087. @end table
  3088. @noindent
  3089. The proof of theorem @emph{T19} is that the standard @code{silly} prelude
  3090. contains the declaration @code{compare = (nil,nil)}. Code in this form
  3091. has the following semantics.
  3092. @table @emph
  3093. @item P17
  3094. For distinct trees @code{@var{x}} and @code{@var{y}}, [[@code{compare}]] @code{(@var{x},@var{y})} = @code{nil}
  3095. @item P18
  3096. [[@code{compare}]] @code{(@var{x},@var{x})} = @code{(nil,nil)}
  3097. @end table
  3098. @noindent
  3099. @cindex equality
  3100. In other words, the virtual code @code{(nil,nil)} implements a function
  3101. that takes a pair of trees and returns true if and only if they are
  3102. equal.
  3103. It would be fairly simple to write an equivalent virtual code
  3104. application that implements this function if it were not realizable in
  3105. this form by definition of the operator. However, this method is
  3106. preferable because it saves space in virtual code and has a highly
  3107. optimized implementation in C.
  3108. @node Member, , Compare, Predicates
  3109. @subsubsection Member
  3110. Another built in predicate function has the virtual code shown below.
  3111. @cindex @code{member}
  3112. @table @emph
  3113. @item T20
  3114. [[@code{member}]] = @code{((nil,nil),((nil,nil),nil))}
  3115. @end table
  3116. @noindent
  3117. As the mnemonic suggests, the function implemented by this code detects
  3118. whether a given item is a member of a list. The left side of its
  3119. argument is the item to be detected, and the right side is the list that
  3120. may or may not contain it. Lists are represented as explained in
  3121. @ref{Representation of Numeric and Textual Data}.
  3122. The virtual code semantics can be specified by these three properties of
  3123. the operator.
  3124. @table @emph
  3125. @item P19
  3126. [[@code{member}]] @code{(@var{x},nil)} = @code{nil}
  3127. @item P20
  3128. [[@code{member}]] @code{(@var{x},(@var{x},@var{y}))} = @code{(nil,nil)}
  3129. @item P21
  3130. For distinct trees @code{@var{x}} and @code{@var{y}}, [[@code{member}]] @code{(@var{x},(@var{y},@var{z}))} =
  3131. [[@code{member}]] @code{(@var{x},@code{z})}
  3132. @end table
  3133. As in the case of @code{compare}, the implementation of @code{member} is
  3134. well optimized in C, so this form is to be preferred over an ad hoc
  3135. construction of a membership testing function in virtual code.
  3136. @node Iteration, List Combinators, Predicates, Virtual Code Semantics
  3137. @subsection Iteration
  3138. @cindex recursion
  3139. @cindex @code{iterate}
  3140. One of many alternatives to recursion provided by the virtual machine is
  3141. iteration, which allows an operation to be repeated until a condition is
  3142. met. If the source language is imperative, this feature provides an easy
  3143. means of translating loop statements to virtual code. In languages that allow
  3144. functions to be treated as data, iteration can be regarded as a function
  3145. that takes a predicate and a function as arguments, and returns a
  3146. function that applies the given function repeatedly to its argument
  3147. until the predicate is refuted.
  3148. Iterative applications are expressed in virtual code by the pattern shown below.
  3149. @table @emph
  3150. @item T21
  3151. [[@code{iterate}]] @code{(@var{p},@var{f})} = @code{((nil,nil),(nil,(@var{p},@var{f})))}
  3152. @end table
  3153. @noindent
  3154. In the @code{silly} language, the @code{iterate} mnemonic plays the role
  3155. of the function that takes the virtual code for a predicate
  3156. @code{@var{p}} and a function @code{@var{f}} as arguments, and returns
  3157. the virtual code for an iterating function.
  3158. The code for an iterating function is recognized as such by the virtual
  3159. machine emulator only if the subtrees @code{@var{f}} and @code{@var{p}} are other
  3160. than @code{nil}. The resulting function tests the argument
  3161. @code{@var{x}} with @code{@var{p}} and returns @code{@var{x}} if the
  3162. predicate is false.
  3163. @table @emph
  3164. @item P22
  3165. ([[@code{iterate}]] @code{(@var{p},@var{f})}) @code{@var{x}} = @code{@var{x}} if @code{@var{p} @var{x}} = @code{nil}
  3166. @end table
  3167. @noindent
  3168. If the predicate turns out to be true, @code{@var{f}} is applied to
  3169. @code{@var{x}}, and then another iteration is performed.
  3170. @table @emph
  3171. @item P23
  3172. ([[@code{iterate}]] @code{(@var{p},@var{f})}) @code{@var{x}} =
  3173. ([[@code{iterate}]] @code{(@var{p},@var{f})}) @code{@var{f} @var{x}} if @code{@var{p} @var{x}} is a non-@code{nil} tree
  3174. @end table
  3175. @node List Combinators, List Functions, Iteration, Virtual Code Semantics
  3176. @subsection List Combinators
  3177. @cindex lists
  3178. @cindex imperative programming
  3179. @cindex functional programming
  3180. There is extensive support for operations on lists in the virtual code
  3181. format. Use of these features is encouraged because they are conducive
  3182. to tight code with explicit concurrency. Within an imperative
  3183. programming paradigm, these features might perhaps have to be understood
  3184. as design patterns or algorithmic skeletons. The present exposition
  3185. takes a functional view, describing them in terms of operators that take
  3186. functions as their arguments and return functions as their result.
  3187. @menu
  3188. * Map::
  3189. * Filter::
  3190. * Reduce::
  3191. * Sort::
  3192. * Transfer::
  3193. * Mapcur::
  3194. @end menu
  3195. @node Map, Filter, List Combinators, List Combinators
  3196. @subsubsection Map
  3197. A virtual code application in the following form causes a function with
  3198. non-@code{nil} virtual code @code{@var{f}} to be applied to every item
  3199. in a list.
  3200. @table @emph
  3201. @item T22
  3202. [[@code{map}]] @code{@var{f}} = @code{((nil,nil),((nil,@var{f}),nil))}
  3203. @end table
  3204. @noindent
  3205. @cindex @code{map}
  3206. The @code{map} mnemonic is used in @code{silly} to express applications
  3207. in this form as @code{map @var{f}}. This operation is also well known to
  3208. lisp users and functional programmers. The semantics is determined by
  3209. these two operator properties (for non-@code{nil} @code{@var{f}}).
  3210. @table @emph
  3211. @item P24
  3212. ([[@code{map}]] @code{@var{f}}) @code{nil} = @code{nil}
  3213. @item P25
  3214. ([[@code{map}]] @code{@var{f}}) @code{(@var{x},@var{y})} = @code{(@var{f} @var{x},(}[[@code{map}]] @code{@var{f}) @var{y})}
  3215. @end table
  3216. @noindent
  3217. Note that the representation of lists described in
  3218. @ref{Representation of Numeric and Textual Data}, is assumed.
  3219. @node Filter, Reduce, Map, List Combinators
  3220. @subsubsection Filter
  3221. @cindex @code{filter}
  3222. Another well known list operation is that which applies a predicate to
  3223. every item of a list, and deletes those for which the predicate is
  3224. false. For a predicate with virtual code @code{@var{p}}, such an
  3225. application can be coded conveniently in this form,
  3226. @table @emph
  3227. @item T23
  3228. [[@code{filter}]] @code{@var{p}} = @code{((nil,nil),(nil,(@var{p},nil)))}
  3229. @end table
  3230. @noindent
  3231. which is to say that writing @code{((nil,nil),(nil,(@var{p},nil)))} in
  3232. @code{silly} is the same as writing @code{filter @var{p}}.
  3233. The virtual machine detects code of this form provided that
  3234. @code{@var{p}} is other than @code{nil}, and evaluates it consistently
  3235. with the following properties, causing it to have the meaning that it
  3236. does.
  3237. @table @emph
  3238. @item P26
  3239. ([[@code{filter}]] @code{@var{p}}) @code{nil} = @code{nil}
  3240. @item P27
  3241. ([[@code{filter}]] @code{@var{p}}) @code{(@var{x},@var{y})} =
  3242. ([[@code{filter}]] @code{@var{p}}) @code{@var{y}} if @code{@var{p} @code{@var{x}}} = @code{nil}
  3243. @item P28
  3244. ([[@code{filter}]] @code{@var{p}}) @code{(@var{x},@var{y})} =
  3245. @code{(@var{x},}([[@code{filter}]] @code{@var{p}}) @code{@var{y})} if @code{@var{p} @var{x}} is a non-@code{nil} tree
  3246. @end table
  3247. @node Reduce, Sort, Filter, List Combinators
  3248. @subsubsection Reduce
  3249. @cindex @code{reduce}
  3250. In the virtual code fragment shown below, @code{@var{f}} should be
  3251. regarded as the virtual code for a binary operator, and @code{@var{k}}
  3252. is a constant.
  3253. @table @emph
  3254. @item T24
  3255. [[@code{reduce}]] @code{(@var{f},@var{k})} = @code{((nil,nil),((@var{f},@var{k}),nil))}
  3256. @end table
  3257. @noindent
  3258. By constructing a tree in the form shown, the @code{silly}
  3259. mnemonic @code{reduce} effectively constructs the code for a function
  3260. operating on lists in a particular way.
  3261. The effect of evaluating an application in this form with an argument
  3262. representing a list can be broken down into several cases.
  3263. @itemize @bullet
  3264. @item If the list is empty, then the value of @code{@var{k}} is the
  3265. result.
  3266. @item If the list has only one item, then that item is the result.
  3267. @item if the list has exactly two items, the first being @code{@var{x}} and the
  3268. second being @code{@var{y}}, then the result is @code{@var{f}
  3269. (@var{x},@var{y})}.
  3270. @item If the list has more than two items and an even number of them, the
  3271. result is that of evaluating the application with the list of values
  3272. obtained by partitioning the list into pairs of adjacent items, and
  3273. evaluating @code{@var{f}} with each pair.
  3274. @item If the list has more than two items and an odd number of them, the
  3275. result is that of evaluating the application with the list of values
  3276. obtained by partitioning the list into pairs of adjacent items excluding
  3277. the last one, evaluating @code{@var{f}} with each pair, and then
  3278. appending the last item to the list of values.
  3279. @end itemize
  3280. @noindent
  3281. In the last two cases, evaluation of the application is not necessarily
  3282. finished after just one traversal of the list, because it has to carry
  3283. on until the list is reduced to a single item.
  3284. Some care has been taken to describe this operation in detail because it
  3285. differs from comparable operations common to functional programming
  3286. @cindex fold
  3287. languages, variously known as fold or reduce. All of these operations
  3288. could be used, for example, to compute the summation of a list of
  3289. numbers. The crucial differences are as follows.
  3290. @itemize @bullet
  3291. @item Whereas a fold or a reduce is conventionally either of the left or
  3292. right variety, this @code{reduce} is neither.
  3293. @item The vacuous case result @code{@var{k}} is never used at all unless
  3294. the argument is the empty list.
  3295. @item This @code{reduce} is not very useful if the operator
  3296. @code{@var{f}} is not associative.
  3297. @end itemize
  3298. The reason for defining the semantics of @code{reduce} in this way
  3299. instead of the normal way is that a distributed implementation of this
  3300. @cindex distributed implementation
  3301. one could work in logarithmic time, so it's worth making it easy for a
  3302. language processor to detect the pattern in case the virtual code is
  3303. ever going to be targeted to such an implementation. Of course, nothing
  3304. prevents the conventional left or right reduction semantics from being
  3305. translated to virtual code by explicit recursion.
  3306. @cindex recursion
  3307. The precise semantics of this operation are as follows, where
  3308. @code{@var{f}} is not @code{nil}, @code{@var{k}} is unconstrained, and
  3309. @code{pairwise} represents a function to be explained presently.
  3310. @cindex @code{iterate}
  3311. @cindex @code{pairwise}
  3312. @cindex @code{bu}
  3313. @cindex @code{right}
  3314. @table @emph
  3315. @item P29
  3316. ([[@code{reduce}]] @code{(@var{f},@var{k})}) @code{nil} = @code{@var{k}}
  3317. @item P30
  3318. ([[@code{reduce}]] @code{(@var{f},@var{k})}) @code{(@var{x},@var{y})} = @*
  3319. @w{ }@w{ }@w{ }
  3320. [[@code{left}]] ([[@code{bu(iterate,right)}]] [[@code{pairwise}]] @code{@var{f}}) @code{(@var{x},@var{y})}
  3321. @end table
  3322. @noindent
  3323. The latter property leverages off some virtual machine features and
  3324. @code{silly} code that has been defined already. The function
  3325. implemented by [[@code{pairwise}]] @code{@var{f}} is the one that
  3326. partitions its argument into pairs and evaluates @code{@var{f}} with
  3327. each pair as described above. The combination of that with
  3328. @code{bu(iterate,right)} results in an application that repeatedly
  3329. performs [[@code{pairwise}]] @code{@var{f}} while the resulting list
  3330. still has a tail (i.e., a @code{right} side), and stops when the list
  3331. has only a single item (i.e., when @code{right} is false). The
  3332. @code{left} operation then extracts the item.
  3333. For the sake of completeness, it is tedious but straightforward to
  3334. give an exact definition for @code{pairwise}. The short version is that
  3335. it can be anything that satisfies these three equations.
  3336. @table @emph
  3337. @item E1
  3338. ([[@code{pairwise}]] @code{@var{f}}) @code{nil} = @code{nil}
  3339. @item E2
  3340. ([[@code{pairwise}]] @code{@var{f}}) @code{(@var{x},nil)} = @code{(@var{x},nil)}
  3341. @item E3
  3342. ([[@code{pairwise}]] @code{@var{f}}) @code{(@var{x},(@var{y},@var{z}))} =
  3343. @code{(@var{f} (@var{x},@var{y}),}([[@code{pairwise}]] @code{@var{f}}) @code{@var{z})}
  3344. @end table
  3345. @noindent
  3346. For the long version, see @ref{Pairwise}.
  3347. @node Sort, Transfer, Reduce, List Combinators
  3348. @subsubsection Sort
  3349. @cindex @code{sort}
  3350. Sorting is a frequently used operation that has the following standard
  3351. representation in virtual code.
  3352. @table @emph
  3353. @item T25
  3354. [[@code{sort}]] @code{@var{p}} = @code{((nil,nil),((@var{p},nil),(nil,nil)))}
  3355. @end table
  3356. @noindent
  3357. When an application in this form is evaluated with an operand
  3358. representing a list, the result is a sorted version of the list.
  3359. The way a list is sorted depends on what order is of interest. For
  3360. example, numbers could be sorted in ascending or descending order,
  3361. character strings could be sorted lexically or by length, etc.. The
  3362. value of @code{@var{p}} is therefore needed in sorting applications to
  3363. specify the order. It contains the virtual code for a partial order
  3364. relational operator. This operator can be evaluated with any pair of
  3365. items selected from a list, and should have a value of true if the left
  3366. one should precede the right under the ordering. For example, if numbers
  3367. were to be sorted in ascending order, then @code{@var{p}} would compute
  3368. the ``less or equal'' relation, returning true if its operand were a
  3369. pair of numbers in which the left is less or equal to the right.
  3370. The virtual code semantics for sorting applications is given by these
  3371. two properties, wherein @code{@var{p}} is a non-@code{nil} tree, and
  3372. @code{insert} is a @code{silly} mnemonic to be defined next.
  3373. @cindex @code{insert}
  3374. @table @emph
  3375. @item P31
  3376. ([[@code{sort}]] @code{@var{p}}) @code{nil} = @code{nil}
  3377. @item P32
  3378. ([[@code{sort}]] @code{@var{p}}) @code{(@var{x},@var{y})} = ([[@code{insert}]] @code{@var{p}})
  3379. @code{(@var{x},}([[@code{sort}]] @code{@var{p}}) @code{@var{y})}
  3380. @end table
  3381. @noindent
  3382. These properties say that the empty list is already sorted, and
  3383. a non-empty list is sorted if its tail is sorted and the head is then
  3384. inserted in the proper place. This specification of sorting has nothing
  3385. to do with the sorting algorithm that @code{avram} really uses.
  3386. The meaning of insertion is convenient to specify in virtual code
  3387. itself. It should satisfy these three equations.
  3388. @table @emph
  3389. @item E4
  3390. ([[@code{insert}]] @code{@var{p}}) @code{(@var{x},nil)} = @code{(@var{x},nil)}
  3391. @item E5
  3392. ([[@code{insert}]] @code{@var{p}}) @code{(@var{x},(@var{y},@var{z}))} =
  3393. @code{(@var{y},}([[@code{insert}]] @code{@var{p}}) @code{(@var{x},@var{z}))}
  3394. if @code{@var{p}(@var{x},@var{y})} = @code{nil}
  3395. @item E6
  3396. ([[@code{insert}]] @code{@var{p}}) @code{(@var{x},(@var{y},@var{z})}) =
  3397. @code{(@var{x},(@var{y},@var{z}))} if @code{@var{p}(@var{x},@var{y})} is a non-@code{nil} tree
  3398. @end table
  3399. @noindent
  3400. Intuitively, the right argument, whether @code{nil} or
  3401. @code{(@var{y},@var{z})}, represents a list that is already sorted. The
  3402. left argument @code{@var{x}} therefore only needs to be compared to the
  3403. head element, @code{@var{y}} to ascertain whether or not it belongs at
  3404. the beginning. If not, it should be inserted into the tail.
  3405. A possible implementation of @code{insert} in @code{silly} is given in
  3406. @ref{Insert}.
  3407. @node Transfer, Mapcur, Sort, List Combinators
  3408. @subsubsection Transfer
  3409. @cindex @code{transfer}
  3410. A particular interpretation is given to virtual code in the following form.
  3411. @table @emph
  3412. @item T26
  3413. [[@code{transfer}]] @code{@var{f}} = @code{((nil,nil),(nil,(nil,@var{f})))}
  3414. @end table
  3415. @noindent
  3416. When code in this form is evaluated with an argument, the tree
  3417. @cindex state transition function
  3418. @code{@var{f}} is used as a state transition function, and the argument
  3419. is used as a list to be traversed. The traversal begins with
  3420. @code{@var{f}} being evaluated on @code{nil} to get the initial state
  3421. and the initial output. Thereafter, each item of the list is paired with
  3422. the current state to be evaluated with @code{@var{f}}, resulting in a
  3423. list of output and the next state. The output resulting from the entire
  3424. application is the cumulative concatenation of all outputs obtained in
  3425. the course of evaluating @code{@var{f}}. The computation
  3426. terminates when @code{@var{f}} yields a @code{nil} result. If the list
  3427. of inputs runs out before the computation terminates, @code{nil} values
  3428. are used as inputs.
  3429. This behavior is specified more precisely in the following property
  3430. of the operator, which applies in the case of non-@code{nil} @code{@var{f}}.
  3431. @cindex @code{transition}
  3432. @table @emph
  3433. @item P33
  3434. ([[@code{transfer}]] @code{@var{f}}) @code{@var{x}} =
  3435. ([[@code{transition}]] @code{@var{f}}) @code{(nil,(@var{f} nil,@var{x}))}
  3436. @end table
  3437. Much of the @code{transfer} semantics is implicit in the meaning of
  3438. @code{transition}. For any given application @code{@var{f}},
  3439. [[@code{transition}]] @code{@var{f}} is the virtual code for a function
  3440. that takes the list traversal from one configuration to the next.
  3441. A configuration is represented as a tuple, usually in the form
  3442. @code{(@var{previous outputs},((@var{state},@var{output}),(@var{next input},@var{subsequent
  3443. inputs})))}. A terminal configuration has the form
  3444. @code{(@var{previous outputs},(nil,(@var{next input},@var{subsequent
  3445. inputs})))}. A configuration may also have @code{nil} in place of the
  3446. pair @code{(@var{next input},@var{subsequent inputs})} if no more input
  3447. remains.
  3448. In the non-degenerate case, the meaning of [[@code{transition}]]
  3449. @code{@var{f}} is consistent with the following equation.
  3450. @table @emph
  3451. @item E7
  3452. ([[@code{transition}]] @code{@var{f}}) @code{(@var{y},((@var{s},@var{o}),(@var{i},@var{x})))} =@*
  3453. @w{ }@w{ }@w{ }@w{ }([[@code{transition}]] @code{@var{f}}) @code{((@var{o},@var{y}),(@var{f} (@var{s},@var{i}),@var{x}))}
  3454. @end table
  3455. @noindent
  3456. That is, the current output @code{@var{o}} is stored with previous outputs @code{@var{y}}, the next
  3457. input @code{@var{i}} is removed from the configuration, and the next state and output
  3458. are obtained from the evaluation of @code{@var{f}} with the state @code{@var{s}} and
  3459. the next input @code{@var{i}}.
  3460. In the case where no input remains, the transition function is
  3461. consistent with the following equation.
  3462. @table @emph
  3463. @item E8
  3464. ([[@code{transition}]] @code{@var{f}}) @code{(@var{y},((@var{s},@var{o}),nil))} = @*
  3465. @w{ }@w{ }@w{ }@w{ }([[@code{transition}]] @code{@var{f}}) @code{((@var{o},@var{y}),(@var{f} (@var{s},nil),nil))}
  3466. @end table
  3467. @noindent
  3468. This case is similar to the previous one except that the @code{nil}
  3469. value is used in place of the next input. Note that in either case,
  3470. nothing about @code{@var{f}} depends on the particular way
  3471. configurations are represented, except that it should have a state as
  3472. its left argument and an input as its right argument.
  3473. Finally, in the case of a terminal configuration, the transition
  3474. function returns the cumulative output.
  3475. @table @emph
  3476. @item E9
  3477. ([[@code{transition}]] @code{@var{f}}) @code{(@var{y},(nil,@var{x}))} =
  3478. [[@code{reduce(cat,nil)}]] [[@code{reverse}]] @code{@var{y}}
  3479. @end table
  3480. @noindent
  3481. The @code{silly} code @code{reduce(cat,nil)} has the effect of
  3482. @cindex @code{cat}
  3483. @cindex concatenation
  3484. flattening a list of lists into one long list, which is necessary
  3485. insofar as the transition function will have generated not necessarily a
  3486. single output but a list of outputs on each iteration. The @code{cat}
  3487. mnemonic stands for list concatenation and is explained in @ref{Cat}.
  3488. The reversal is necessary to cause the first generated output to be at
  3489. the head of the list. List reversal is a built in operation of the
  3490. virtual machine and is described in @ref{Reverse}.
  3491. If such a function as @code{transition} seems implausible, its
  3492. implementation in @code{silly} can be found in @ref{Transition}.
  3493. It is usually more awkward to express a function in terms of
  3494. a @code{transfer} than to code it directly using recursion or other list
  3495. operations. However, this feature is provided by the virtual machine for
  3496. several reasons.
  3497. @itemize @bullet
  3498. @item Functions in this form may be an easier translation target if the
  3499. source is an imperative language.
  3500. @item Translating from virtual code to asynchronous circuits or process
  3501. @cindex asynchronous circuits
  3502. networks has been a research interest of the author, and code in this
  3503. @cindex author
  3504. form lends itself to easy recognition and mapping onto discrete components.
  3505. @item The @option{--byte-transducer} and @option{--interactive} command
  3506. line options to @code{avram} cause an application to be invoked in a
  3507. @cindex state transition function
  3508. similar manner to the transition function in a @code{transfer}
  3509. function, so this feature allows for easy simulation and troubleshooting
  3510. of these applications without actually deploying them.
  3511. @end itemize
  3512. @node Mapcur, , Transfer, List Combinators
  3513. @subsubsection Mapcur
  3514. An alternative form of recursive definition is the following.
  3515. @cindex @code{mapcur}
  3516. @table @emph
  3517. @item T27
  3518. [[@code{mapcur}]] @code{@var{p}} = @code{((nil,nil),((nil,nil),(@var{p},nil)))}
  3519. @end table
  3520. @noindent
  3521. This form is convenient for applications that cause themselves to be
  3522. @cindex recursion
  3523. applied recursively to a list of arguments. It has this semantics.
  3524. @table @emph
  3525. @item P34
  3526. ([[@code{mapcur}]] @code{@var{p}}) @code{@var{x}} =
  3527. [[@code{map meta}]] [[@code{distribute}]] ([[@code{field}]] @code{@var{p}}) @code{@var{x}}
  3528. @end table
  3529. @node List Functions, Exception Handling, List Combinators, Virtual Code Semantics
  3530. @subsection List Functions
  3531. In addition to the foregoing list operations, the virtual machine
  3532. @cindex lists
  3533. provides a number of canned functions operating on lists, namely
  3534. concatenation, reversal, distribution, and transposition. These
  3535. functions could be coded by other means if they were not built in, but
  3536. the built in versions are faster and smaller.
  3537. @menu
  3538. * Cat::
  3539. * Reverse::
  3540. * Distribute::
  3541. * Transpose::
  3542. @end menu
  3543. @node Cat, Reverse, List Functions, List Functions
  3544. @subsubsection Cat
  3545. The list concatenation operation has this representation in virtual code.
  3546. @cindex @code{cat}
  3547. @cindex concatenation
  3548. @table @emph
  3549. @item T28
  3550. [[@code{cat}]] = @code{((nil,nil),(nil,nil))}
  3551. @end table
  3552. @noindent
  3553. This function takes a pair of lists as an argument, an returns the list
  3554. obtained by appending the right one to the left. The semantics of
  3555. concatenation is what one would expect.
  3556. @table @emph
  3557. @item P35
  3558. [[@code{cat}]] @code{(nil,@var{z})} = @code{@var{z}}
  3559. @item P36
  3560. [[@code{cat}]] @code{((@var{x},@var{y}),@var{z})} = @code{(@var{x},}[[@code{cat}]] @code{(@var{y},@code{z}))}
  3561. @end table
  3562. @node Reverse, Distribute, Cat, List Functions
  3563. @subsubsection Reverse
  3564. @cindex @code{reverse}
  3565. The function that reverses a list has the following representation in
  3566. virtual code.
  3567. @table @emph
  3568. @item T29
  3569. [[@code{reverse}]] = @code{((nil,nil),(nil,(nil,nil)))}
  3570. @end table
  3571. @noindent
  3572. This function takes a list as an argument, and returns a the list
  3573. consisting of the same items in the reverse order. The semantics is
  3574. given by the following properties.
  3575. @table @emph
  3576. @item P37
  3577. [[@code{reverse}]] @code{nil} = @code{nil}
  3578. @item P38
  3579. [[@code{reverse}]] @code{(@var{x},@var{y})} = [[@code{cat}]] ([[@code{reverse}]] @code{@var{y},(@var{x},nil)})
  3580. @end table
  3581. @node Distribute, Transpose, Reverse, List Functions
  3582. @subsubsection Distribute
  3583. The function with the following virtual code representation is
  3584. frequently useful for manipulating lists.
  3585. @cindex @code{distribute}
  3586. @table @emph
  3587. @item T30
  3588. @code{distribute} = @code{(((nil,nil),nil),nil)}
  3589. @end table
  3590. @noindent
  3591. This function takes a pair whose right side represents a list, and
  3592. returns a list of pairs, with one pair for each item in the list. The
  3593. left side of each pair is the left side of the original argument, and
  3594. the right side is the corresponding item of the list. A semantics for
  3595. this operation is specified by the following properties.
  3596. @table @emph
  3597. @item P39
  3598. [[@code{distribute}]] @code{(@var{x},nil)} = @code{nil}
  3599. @item P40
  3600. [[@code{distribute}]] @code{(@var{x},(@var{y},@var{z}))} =
  3601. @code{((@var{x},@var{y}),}[[@code{distribute}]] @code{(@var{x},@var{z}))}
  3602. @end table
  3603. @node Transpose, , Distribute, List Functions
  3604. @subsubsection Transpose
  3605. The @code{transpose} operation has the following representation in
  3606. virtual code.
  3607. @table @emph
  3608. @item T31
  3609. [[@code{transpose}]] = @code{((nil,nil),((nil,nil),(nil,nil)))}
  3610. @end table
  3611. @noindent
  3612. @cindex @code{transpose}
  3613. This function takes a list of equal length lists as an argument, and
  3614. returns a list of lists as a result. In the resulting list, the first
  3615. item is the list of all first items of lists in the argument. The next
  3616. item is the list of all second items, and so on.
  3617. In the specification of the semantics, the @code{silly} mnemonic
  3618. @cindex @code{flat}
  3619. @code{flat} is defined by @code{flat = reduce(cat,nil)} in the standard
  3620. @code{silly} prelude, which means that it flattens a list of lists into
  3621. one long list.
  3622. @table @emph
  3623. @item P41
  3624. [[@code{transpose}]] @code{@var{x}} = @code{nil} if [[@code{flat}]] @code{@var{x}} = @code{nil}
  3625. @item P42
  3626. [[@code{transpose}]] @code{@var{x}} =
  3627. @code{(}[[@code{map left}]] @code{@var{x},}[[@code{transpose}]] [[@code{map right}]] @code{@var{x})}@*
  3628. @w{ }@w{ }@w{ } if [[@code{flat}]] @code{@var{x}} is a non-@code{nil} tree
  3629. @end table
  3630. @node Exception Handling, Interfaces to External Code, List Functions, Virtual Code Semantics
  3631. @subsection Exception Handling
  3632. @cindex exceptions
  3633. In quite a few cases, the properties given for the operator up to this
  3634. point do not imply any particular result. A good example would be an
  3635. expression such as [[@code{left}]] @code{nil}, which appears to
  3636. represent the left side of an empty pair. It can be argued that
  3637. expressions like this have no sensible interpretation and should never
  3638. be used, so it would be appropriate to leave them undefined. On the
  3639. other hand, attempts to evaluate such expressions occur frequently by
  3640. mistake, and in any case, the virtual machine emulator should be
  3641. designed to do something reasonable about them if only for the sake of
  3642. reporting the error. The chosen remedy for this situation addresses the
  3643. need for error reporting, and also turns out to be useful in other ways.
  3644. @menu
  3645. * A Hierarchy of Sets::
  3646. * Operator Generalization::
  3647. * Error Messages::
  3648. * Expedient Error Messages::
  3649. * Computable Error Messages::
  3650. * Exception Handler Usage::
  3651. @end menu
  3652. @node A Hierarchy of Sets, Operator Generalization, Exception Handling, Exception Handling
  3653. @subsubsection A Hierarchy of Sets
  3654. As indicated already, the virtual machine represents all functions and
  3655. data as members of a set satisfying the properties in @ref{Raw Material},
  3656. namely a @code{nil} element and a @code{cons} operator for constructing
  3657. trees or nested pairs of @code{nil}. However, it will be necessary to
  3658. distinguish the results of computations that go wrong for exceptional
  3659. reasons from normal results. Because any tree in the set could conceivably
  3660. represent a normal result, we need to go outside the set to find an
  3661. unambiguous representation of exceptional results.
  3662. Because there may be many possible exceptional conditions, it will be helpful
  3663. to have a large set of possible ways to encode them, and in fact there
  3664. is no need to refrain from choosing a countably infinite
  3665. set. Furthermore, it will be useful to distinguish between different
  3666. levels of severity among exceptional conditions, so for this purpose a
  3667. countably infinite hierarchy of mutually disjoint sets is used.
  3668. In order to build on the theory already developed, the set that has been
  3669. used up to this point will form the bottom level of the hierarchy, and
  3670. its members will represent normal computational results. The members of
  3671. sets on the higher levels in the hierarchy represent exceptional
  3672. results. To avoid ambiguity, the term ``trees'' is reserved for members
  3673. @cindex trees
  3674. of the bottom set, as in ``for any tree @code{@var{x}} @dots{}''.
  3675. Unless otherwise stated, variables like @code{@var{x}} and
  3676. @code{@var{y}} are universally quantified over the bottom set only.
  3677. @cindex universal quantification
  3678. Because each set in the hierarchy is countably infinite, it is
  3679. isomorphic to the bottom set. With respect to an arbitrary but fixed
  3680. bijection between them, let @code{@var{x}_@var{n}} denote the image in
  3681. the @code{@var{n}}th level set of a tree @code{@var{x}} in the bottom
  3682. set. The level numbers in this notation start with zero, and we take
  3683. @code{@var{x}_0} to be synonymous with @code{@var{x}}. For good measure,
  3684. let @code{(@var{x}_@var{n})_@var{m}} = @code{@var{x}_(@var{n}+@var{m})}.
  3685. @node Operator Generalization, Error Messages, A Hierarchy of Sets, Exception Handling
  3686. @subsubsection Operator Generalization
  3687. Each set in the hierarchy induces a structure preserving @code{cons}
  3688. @cindex @code{cons}
  3689. operator, denoted @code{cons_@var{n}} for the @code{@var{n}}th level
  3690. set, and satisfying this equation.
  3691. @table @emph
  3692. @item E10
  3693. @code{cons_@var{n}(@var{x}_@var{n},@var{y}_@var{n})} =
  3694. @code{(cons(@var{x},@var{y}))_@var{n}}
  3695. @end table
  3696. @noindent
  3697. It will be convenient to generalize all of these @code{cons} operators to be
  3698. defined on members of other sets than their own.
  3699. @table @emph
  3700. @item E11
  3701. For @code{@var{m}} greater than @code{@var{n}},
  3702. @w{ } @w{ } @w{ } @code{cons_@var{n}(@var{x}_@var{m},@var{y}_@var{p})} =
  3703. @code{@var{x}_@var{m}}
  3704. @end table
  3705. @noindent
  3706. In this equation, @code{@var{p}} is unrestricted. The intuition is that
  3707. if the left operand of a @code{cons} is the result of a computation that
  3708. went wrong due to an exceptional condition (more exceptional than
  3709. @code{@var{n}}, the level already in effect), then the exceptional
  3710. result becomes the whole result.
  3711. It is tempting to hazard a slightly stronger statement, which is that
  3712. this equation holds even if @code{@var{y}_@var{p}} is equal to some
  3713. expression @code{@var{f} @var{x}} that is undefined according to the
  3714. operator semantics. This stipulation would correspond to an
  3715. implementation in which the right operand isn't evaluated after an error
  3716. is detected in the left, but there are two problems with it.
  3717. @itemize @bullet
  3718. @item This semantics might unreasonably complicate a concurrent
  3719. implementation of the virtual machine. If evaluation leads to
  3720. non-termination in some cases where the result is undefined (as it certainly
  3721. would in any possible implementation consistent with cases where it's
  3722. defined), then the mechanism that evaluates the right side of a pair
  3723. must be interruptible in case an exception is detected in the left.
  3724. @item It is beyond the expressive power of the present mathematical
  3725. framework to make such a statement, because it entails universal
  3726. quantification over non-members of the constructed sets, which includes
  3727. @cindex universal quantification
  3728. almost everything.
  3729. @end itemize
  3730. @noindent
  3731. Nevertheless, the implementation in @code{avram} is sequential and does
  3732. indeed behave as proposed, with no practical difficulty. As for any
  3733. deficiency in the theory, it could be banished by recasting the
  3734. semantics in terms of a calculus of expressions with formal rules of
  3735. manipulation. An operand to the @code{cons} operator would be identified
  3736. not with a member of a semantic domain, but with the expression used to
  3737. write it down, and then even ``undefinedness'' could be
  3738. @cindex undefined expressions
  3739. defined. However, the present author's preference in computing as in
  3740. @cindex author
  3741. life is to let some things remain a mystery rather than to abandon the
  3742. quest for meaning entirely.
  3743. A comparable condition applies in cases where the right side of a pair
  3744. represents an exceptional result.
  3745. @table @emph
  3746. @item E12
  3747. For @code{@var{m}} greater than @code{@var{n}},
  3748. @w{ } @w{ } @code{cons_@var{n}(@var{x}_@var{n},@var{y}_@var{m})} =
  3749. @code{@var{y}_@var{m}}
  3750. @end table
  3751. Whereas an infinitude of @code{cons} operators has been needed, it will
  3752. @cindex @code{cons}
  3753. be possible to get by with only one invisible operator, as before, by
  3754. generalizing it in the following way to operands on any level of the
  3755. hierarchy.
  3756. @table @emph
  3757. @item P43
  3758. @code{@var{f}_@var{n} @var{x}_@var{n}} = @code{(@var{f} @var{x})_@var{n}}
  3759. @item P44
  3760. For distinct @code{@var{n}} and @code{@var{m}}, @w{ }@code{@var{f}_@var{n} @var{x}_@var{m}} = @code{@var{x}_@var{m}}
  3761. @end table
  3762. @noindent
  3763. That is, the result of evaluating two operands on the same level is the
  3764. image relative to that level of the result of their respective images on
  3765. the bottom level, but the result of evaluating two operands on different
  3766. levels is the same as the right operand.
  3767. @node Error Messages, Expedient Error Messages, Operator Generalization, Exception Handling
  3768. @subsubsection Error Messages
  3769. The basic strategy for representing the results of exceptional
  3770. conditions arising from the evaluation of operands on a given level of
  3771. the hierarchy will be to use an error message corresponding to the image
  3772. of a list of character strings on the level above.
  3773. Unfortunately, the official @code{silly} standard does not
  3774. define character constants, but they are available as a vendor specific
  3775. extension in @code{silly-me} (millennium edition), where character strings
  3776. @cindex @code{silly-me}
  3777. @cindex strings
  3778. @cindex character strings
  3779. may be enclosed in single quotes. The value of the semantic
  3780. @cindex semantic function
  3781. function [[@dots{}]] in the case of a character string is the list
  3782. of representations of the characters, based on @ref{Character Table}
  3783. and @ref{Representation of Numeric and Textual Data}.
  3784. For the sake of consistency, each standard error message is a list of
  3785. character strings, even though the list has only one string in it. If
  3786. any exceptional condition is the result of a computation, it is written
  3787. to standard error by @code{avram} as the list of character strings it
  3788. represents.
  3789. @table @emph
  3790. @item P45
  3791. ([[@code{compare}]] @code{nil})@code{_@var{n}} = [[@code{('invalid comparison',nil)}]]@code{_(@var{n}+1)}
  3792. @item P46
  3793. ([[@code{left}]] @code{nil})@code{_@var{n}} = [[@code{('invalid deconstruction',nil)}]]@code{_(@var{n}+1)}
  3794. @item P47
  3795. ([[@code{right}]] @code{nil})@code{_@var{n}} = [[@code{('invalid deconstruction',nil)}]]@code{_(@var{n}+1)}
  3796. @item P48
  3797. (([[@code{fan}]] @code{@var{f}})
  3798. @code{nil})@code{_@var{n}} = [[@code{('invalid deconstruction',nil)}]]@code{_(@var{n}+1)}
  3799. @item P49
  3800. ([[@code{member}]] @code{nil})@code{_@var{n}} = [[@code{('invalid membership',nil)}]]@code{_(@var{n}+1)}
  3801. @item P50
  3802. ([[@code{distribute}]] @code{nil})@code{_@var{n}} = [[@code{('invalid distribution',nil)}]]@code{_(@var{n}+1)}
  3803. @item P51
  3804. ([[@code{cat}]] @code{nil})@code{_@var{n}} = [[@code{('invalid concatenation',nil)}]]@code{_(@var{n}+1)}
  3805. @item P52
  3806. ([[@code{meta}]] @code{nil})@code{_@var{n}} = [[@code{('invalid recursion',nil)}]]@code{_(@var{n}+1)}
  3807. @end table
  3808. Note that by virtue of property @emph{P44}, there is no need for an
  3809. application to make explicit checks for exceptional results at any
  3810. point, because the exceptional result propagates through to the output
  3811. of any function composed with the one that incurred it. For example, an
  3812. application of the form @code{h = compose(f,right)}, which will cause an
  3813. invalid deconstruction error if applied in filter mode to an empty file,
  3814. imposes no requirement that @code{f} be written to accommodate that
  3815. possibility (i.e., by checking for it) in order for the error to be
  3816. reported properly. The following proof demonstrates that the meaning of @code{f}
  3817. is irrelevant to the result.
  3818. @format
  3819. [[@code{compose(f,right)}]]@code{_0} @code{nil_0}
  3820. = [[@code{f}]]@code{_0} [[@code{right}]]@code{_0} @code{nil}@code{_0}
  3821. = [[@code{f}]]@code{_0} [[@code{('invalid deconstruction',nil)}]]@code{_1}
  3822. = [[@code{('invalid deconstruction',nil)}]]@code{_1}
  3823. @end format
  3824. @noindent
  3825. In an application @code{h = compose(f,g)}, the input
  3826. validation therefore may be confined to the ``front @w{end'', @code{g}.}
  3827. It will be recalled from the discussions of @code{recur} (@ref{Recur})
  3828. @cindex @code{recur}
  3829. @cindex @code{transpose}
  3830. and @code{transpose} (@ref{Transpose}) that the semantics of
  3831. virtual code involving these forms is defined in terms of the
  3832. @code{field} format for deconstruction functions (@ref{Field}),
  3833. @cindex @code{field}
  3834. which depends implicitly on the semantics of @code{left} and
  3835. @code{right}, being a generalization of them. An invalid deconstruction
  3836. @cindex @code{left}
  3837. @cindex @code{right}
  3838. message could therefore result from applications incorporating any of
  3839. the forms of @code{recur}, @code{transpose}, or @code{field}. Invalid
  3840. deconstructions could also arise from the @code{replace} operation
  3841. @cindex @code{replace}
  3842. @cindex assignment
  3843. (@ref{Replace}), which is used for assignment (@ref{Assignment}), because
  3844. @code{replace} is defined by virtual code, except as noted next.
  3845. @node Expedient Error Messages, Computable Error Messages, Error Messages, Exception Handling
  3846. @subsubsection Expedient Error Messages
  3847. @cindex error messages
  3848. Because there are so many ways to cause an invalid deconstruction, this
  3849. message is the most common in practice and therefore the least
  3850. informative. As a matter of convenience, @code{avram} takes the liberty
  3851. of a slight departure from the virtual machine specification as written
  3852. hitherto, and employs the following messages when invalid
  3853. deconstructions occur respectively in the cases of recursion,
  3854. transposition, and assignment.
  3855. @itemize @bullet
  3856. @item @code{invalid recursion}
  3857. @item @code{invalid transpose}
  3858. @item @code{invalid assignment}
  3859. @end itemize
  3860. @noindent
  3861. That is, this section contradicts and supersedes what is stated at the
  3862. end of @ref{Error Messages} and implied by the operator properties
  3863. @emph{P14}, @emph{P16}, and @emph{P42}. It is also possible that user
  3864. applications may modify the error messages by methods described in
  3865. @ref{Computable Error Messages}.
  3866. Whereas these three cases constitute an expedient variation on the
  3867. semantics, there is another sense in which no possible implementation
  3868. could conform faithfully to the specification. When an evaluation can
  3869. not be carried out because of insufficient space on the host machine,
  3870. one of the following error messages may be the result.
  3871. @itemize @bullet
  3872. @item @code{memory overflow}
  3873. @item @code{counter overflow}
  3874. @end itemize
  3875. @noindent
  3876. These messages are treated in the same way as those that are caused by
  3877. programming errors, and propagate to the final result written to
  3878. standard error without any specific consideration by the application
  3879. developer. The latter occurs only in connection with the built in weight
  3880. function (@ref{Weight}). Other messages listed in
  3881. @ref{Application Programming Errors} are also of this ilk.
  3882. @node Computable Error Messages, Exception Handler Usage, Expedient Error Messages, Exception Handling
  3883. @subsubsection Computable Error Messages
  3884. The automatic generation and reporting of error messages provides a
  3885. reasonable default behavior for applications that do not consider
  3886. exceptional conditions. All applications and their input data are
  3887. ordinarily members of the bottom level set in the hierarchy
  3888. (@ref{A Hierarchy of Sets}). The error messages caused by invalid
  3889. operations on this level are on the first level above the bottom, which
  3890. are recognized as such and written to standard error without
  3891. intervention from the application. However, there are two drawbacks to
  3892. this style of dealing with exceptions.
  3893. @cindex exceptions
  3894. @itemize @bullet
  3895. @item
  3896. An application developer may wish to translate error messages
  3897. into terms that are meaningful to the user, not only by literally
  3898. translating them from English to the local vernacular, but perhaps by
  3899. relating the particular exceptional condition to application specific
  3900. causes. While it is convenient for the ``back end'' code not to be
  3901. required to intervene in the error reporting, it would be most
  3902. inconvenient for it not to be able to do so.
  3903. @item
  3904. Some application specific errors might not correspond directly to any of
  3905. the particular conditions detected automatically due to invalid
  3906. operations, for example a semantic error in a syntactically correct
  3907. input file. It might be convenient in such cases for an application to
  3908. be able to define its own error messages but still have them reported
  3909. automatically like the built in messages.
  3910. @end itemize
  3911. These situations suggest a need for some ability on the part of an
  3912. application to operate on error messages themselves. Based on the
  3913. operator semantics given so far, such an application is inexpressible,
  3914. because for any application @code{@var{f}_0} and error message
  3915. @w{@code{@var{x}_1}}, property @emph{P44} stipulates @code{@var{f}_0 @var{x}_1} =
  3916. @code{@var{x}_1}, meaning that the resulting error message is
  3917. unchanged. Therefore, we need to define another basic property of the
  3918. operator.
  3919. The following form of virtual code is used in applications that may need
  3920. to operate on error messages.
  3921. @cindex @code{guard}
  3922. @table @emph
  3923. @item T32
  3924. [[@code{guard}]] @code{(@var{f},@var{g})} = @code{((nil,@var{f}),@var{g})}
  3925. @end table
  3926. @noindent
  3927. Code in this form has the following semantics.
  3928. @table @emph
  3929. @item P53
  3930. ([[@code{guard}]] @code{(@var{f},@var{g})})@code{_@var{n}}
  3931. @code{@var{x}_@var{p}} =
  3932. @code{@var{g}_(@var{n}+1) @var{f}_@var{n} @var{x}_@var{p}}
  3933. @end table
  3934. @noindent
  3935. The intuitive explanation is that @code{@var{f}} is the main part of the
  3936. application, and @code{@var{g}} is the part of the application that
  3937. operates on the error message that comes from @code{@var{f}} if an
  3938. exception occurs while it is being evaluated (i.e., the ``exception
  3939. handler''). Typically the exception handler code implements a function
  3940. that takes an error message as an argument and returns an error message
  3941. as a result.
  3942. Where there is no exception, the exception handler
  3943. @code{@var{g}_(@var{n}+1)} is never used, because its argument will be
  3944. on level @code{@var{n}}, and therefore unaffected by an application on
  3945. level @code{@var{n}+1}.
  3946. Exception handlers may have their own exception handlers, which will be
  3947. invoked if the evaluation of the exception handler causes a further
  3948. exception. Such an exception corresponds semantically to a value on the
  3949. next level of the hierarchy of sets.
  3950. @node Exception Handler Usage, , Computable Error Messages, Exception Handling
  3951. @subsubsection Exception Handler Usage
  3952. One way for this feature of the virtual machine to be used is to
  3953. intercept and translate error messages to a more meaningful form. An
  3954. application guarded as shown below causes messages of invalid deconstruction
  3955. to be changed to @code{'syntax error'}.
  3956. @display
  3957. @code{main = guard(
  3958. application,
  3959. conditional(
  3960. bu(compare,('invalid deconstruction',nil)),
  3961. (constant ('syntax error',nil),identity)))}
  3962. @end display
  3963. @noindent
  3964. The conditional compares its argument to the error message for an
  3965. @cindex deconstruction
  3966. invalid deconstruction, and if it matches, the syntax error message is
  3967. returned, but otherwise the original message is returned. Note that an
  3968. error message must be in the form of a list of character strings, so
  3969. that it can be printed. Although the message of @code{'syntax error'}
  3970. might not be very informative, at least it looks less like a crash.
  3971. A real application should of course strive to do better than that.
  3972. Exception handling features of the virtual machine can also be adapted
  3973. by applications to raise their own exceptions with customized messages.
  3974. @example
  3975. error_messenger =
  3976. guard(compose(compare,constant nil),constant ('syntax error',nil))
  3977. @end example
  3978. @noindent
  3979. This code fragment implements a function that causes a message of
  3980. @code{'syntax error'} to be reported for any possible input. This code
  3981. works by first causing an invalid comparison and then substituting its
  3982. own error message. A function that always causes an error is not useful
  3983. in itself, but might be used as part of an application in the following
  3984. form.
  3985. @example
  3986. main = conditional(validation,(application,error_messenger))
  3987. @end example
  3988. @noindent
  3989. In this case, the application checks the validity of the input with a
  3990. predicate, and invokes the error messenger if it is invalid.
  3991. Although the previous examples return a fixed error message for each
  3992. possible kind of error, it is also possible to have error messages
  3993. that depend on the input data, as the next example shows.
  3994. @cindex @code{bu}
  3995. @cindex @code{guard}
  3996. @cindex @code{identity}
  3997. @cindex @code{apply}
  3998. @cindex @code{hired}
  3999. @example
  4000. main = (hired apply)(
  4001. compose(
  4002. bu(guard,some_application),
  4003. (hired constant)(constant 'invalid input was:',identity)),
  4004. identity)
  4005. @end example
  4006. @noindent
  4007. If the application causes an exception for any reason, the error message
  4008. returned will include a complete listing of the input, prefaced by the
  4009. words @code{'invalid input was:'}. This particular example works only if
  4010. the input is a list of character strings, but could be adapted for other
  4011. types of data by substituting an appropriate formatting function for the
  4012. first identity. The formatting function would take the relevant data
  4013. type to a list of character strings. Another possible variation would be to
  4014. concatenate the invalid input listing with the error message that was
  4015. generated, rather than just replacing it.
  4016. As the last example may suggest, exception handlers turn out to be an
  4017. @cindex debugging
  4018. @cindex functional programming
  4019. @cindex imperative programming
  4020. essential debugging tool for functional programs, making them as easy to
  4021. debug as imperative programs if not more so. This example forms the
  4022. basis for a higher order function that wraps any given function with an
  4023. exception handler that prints the argument causing it to crash. For
  4024. arguments not causing a crash, the behavior is unchanged. Alternatively,
  4025. code implementing a function that unconditionally reports its argument
  4026. in an error message can be inserted at a strategic point in the
  4027. application code similarly to a print statement. Finally, inspired use
  4028. of exception handlers that concatenate their messages with previously
  4029. generated messages can show something like a parameter stack dump when a
  4030. recursively defined function crashes. These are all matters for a language
  4031. designer and are not pursued further in this document.
  4032. @node Interfaces to External Code, Vacant Address Space, Exception Handling, Virtual Code Semantics
  4033. @subsection Interfaces to External Code
  4034. A few other combinators have been incorporated into the virtual
  4035. machine as alternatives to the style of interactive applications
  4036. described in @ref{Output From Interactive Applications}. These make it
  4037. possible to interface with external libraries and applications either
  4038. by a simple function call, or by executing a run-time generated
  4039. transducer as described previously. In either case, there is no need
  4040. for any particular command line options to specify interactive
  4041. invocation, nor for the application to be designed that way from the
  4042. outset. Existing virtual code applications may therefore be enhanced
  4043. to make use of these features without radical changes.
  4044. To account for these additional capabilities, it is not entirely
  4045. adequate to continue defining the virtual machine semantics in terms
  4046. of a mathematical function, but it is done nevertheless due to the
  4047. lack of any appealing alternative. Although most library functions are
  4048. in fact functions in the sense that their outputs are determined by
  4049. their arguments, they defy a concise specification within the present
  4050. mathematical framework, especially insofar as they may involve finite
  4051. precision floating point numbers. More problematically, the effect of
  4052. interaction with a shell is neither well defined nor deterministic.
  4053. The descriptions that follow presuppose a computational procedure
  4054. associated with the following definitions but leave its exact nature
  4055. unspecified.
  4056. @menu
  4057. * Library combinator::
  4058. * Have combinator::
  4059. * Interaction combinator::
  4060. @end menu
  4061. @node Library combinator, Have combinator, Interfaces to External Code, Interfaces to External Code
  4062. @subsubsection Library combinator
  4063. The simplest and fastest method of interfacing to an external library
  4064. is by way of a virtual machine combinator called @code{library}. It
  4065. takes two non-empty character strings as arguments to a virtual code
  4066. program of the form implied by the following property.
  4067. @table @emph
  4068. @item T33
  4069. [[@code{library}]] (@code{@var{x}},@code{@var{y}}) = @code{((nil,nil),((@var{x},@var{y}),(nil,nil)))}
  4070. @end table
  4071. @noindent
  4072. Intuitively, @var{x} is the name of a library and @var{y} is the name
  4073. of a function within the library. For example, if @var{x} is
  4074. @code{'math'} and @var{y} is @code{'sqrt'}, then
  4075. @code{library}(@var{x},@var{y}) represents the function that computes
  4076. the square root of a floating point number as defined by the host
  4077. machine's native C implementation, normally in IEEE double precision
  4078. format. Different functions and libraries may involve other argument
  4079. and result types, such as complex numbers, arrays, sparse matrices, or
  4080. arbitrary precision numbers. A list of currently supported external
  4081. library names with their functions and calling conventions is given in
  4082. @ref{External Libraries}.
  4083. On the virtual code side, all function arguments and results
  4084. regardless of their types are encoded as nested pairs of @code{nil},
  4085. as always, and may be manipulated or stored as any other data,
  4086. including storage and retrieval from files in @file{.avm} virtual
  4087. code format (@ref{File Format}). However, on the C side,
  4088. various memory management and caching techniques are employed to
  4089. maintain this facade while allowing the libraries to operate on data
  4090. in their native format. The details are given more fully in the API
  4091. documentation, particularly in @ref{Type Conversions} and
  4092. @ref{External Library Maintenance}.
  4093. While this style is fast and convenient, it is limited either to
  4094. libraries that have already been built into the virtual machine, or to
  4095. those for which the user is prepared to implement a new interface
  4096. module in C as described in @ref{Implementing new library
  4097. functions}.
  4098. @node Have combinator, Interaction combinator, Library combinator, Interfaces to External Code
  4099. @subsubsection Have combinator
  4100. As virtual machine interfaces to external libraries accumulate faster
  4101. than they can be documented and may vary from one installation to
  4102. another, it is helpful to have a way of interrogating the virtual
  4103. machine for an up to date list of the installed libraries and
  4104. functions. A combinator called @code{have} can be used to test for the
  4105. availability of a library function. It takes the form
  4106. @table @emph
  4107. @item T34
  4108. [[@code{have}]] (@code{@var{x}},@code{@var{y}}) = @code{((nil,nil),((nil,@var{x}),(nil,@var{y})))}
  4109. @end table
  4110. @noindent
  4111. where @var{x} is the name of a library and @var{y} is the name of a
  4112. function within the library encoded as character strings. For example,
  4113. if @var{x} is @code{'mtwist'} and @var{y} is @code{'u_disc'} (for the
  4114. natural random number generator function in the Mersenne twistor
  4115. library) then @code{have(@var{x},@var{y})} is a function that returns
  4116. a non-empty value if an only if that library is installed and that
  4117. function is available within it. The actual argument to the function
  4118. is ignored as the result depends only on the installed virtual machine
  4119. configuration. In this sense, it acts like a @code{constant} combinator.
  4120. One way for this combinator to be used is in code of the form
  4121. @example
  4122. portable_rng =
  4123. conditional(
  4124. have('mtwist','u_disc'),
  4125. library('mtwist','u_disc'),
  4126. some_replacement_function)
  4127. @end example
  4128. @noindent
  4129. which will use the library function if available but otherwise use a
  4130. replacement function. Code in this form makes the decision at run
  4131. time, but it is also possible to express the function such that the
  4132. check for library presence is made at compile time, as the following
  4133. example shows, which will imply a slight improvement in performance.
  4134. @example
  4135. non_portable_rng =
  4136. apply(
  4137. conditional(
  4138. have('mtwist','u_disc'),
  4139. constant library('mtwist','u_disc'),
  4140. constant some_replacement_function),
  4141. 0)
  4142. @end example
  4143. @noindent
  4144. This program would be non-portable in the sense that it would need to
  4145. be recompiled for each installation if there were a chance that some
  4146. of them might have the @code{mtwist} library and some might not,
  4147. whereas the previous example would be binary compatible across all of
  4148. them. @footnote{In practice both examples are equally portable because
  4149. the @code{mtwist} source is distributed with @code{avram} so all
  4150. installations will have it. Most libraries are distributed
  4151. separately.}
  4152. The actual value returned by a function @code{have(foo,bar)} is the
  4153. list of pairs of strings @code{<(foo,bar)>} if the function is
  4154. available, or the empty list otherwise. A non-empty list is
  4155. represented as a pair @code{(head,tail)}, and an empty list as
  4156. @code{nil}. The angle bracket notation @code{<a,b,c...>} used here is
  4157. an abbreviation for @code{(a,(b,(c...nil)))}.
  4158. Either or both arguments to the @code{have} combinator can be a
  4159. wildcard, which is the string containing a single asterisk,
  4160. @cindex wild cards
  4161. @code{'*'}. In that case, the list of all available matching library
  4162. names and function names will be returned. This feature can be used to
  4163. find out what library functions are available without already knowing
  4164. their names.
  4165. If a library had a function named @code{'*'}, which clashes with
  4166. the wild card string, the interpretation as a wild card would take
  4167. precedence.
  4168. @node Interaction combinator, , Have combinator, Interfaces to External Code
  4169. @subsubsection Interaction combinator
  4170. A further combinator allows virtual code applications to interact
  4171. directly with any interactive console application using the
  4172. @code{expect} library. The mechanism is similar to that of
  4173. interactive applications documented in the @ref{Output From
  4174. Interactive Applications}, but attempts to be more convenient.
  4175. Instead of being designed as an interactive application, any virtual
  4176. code application may use this combinator to spawn a shell and interact
  4177. with it in order to compute some desired result.
  4178. The advantage of this combinator over the @code{library} combinator is
  4179. that it requires no modification of the virtual machine to support new
  4180. applications. It can also interact with applications that may reside
  4181. on remote servers, that are implemented languages other than C, or
  4182. @cindex GNU R
  4183. whose source code is unavailable. For example, the GNU R statistical
  4184. package provides an interactive command to evaluate multivariate
  4185. @cindex multivariate normal distrubution
  4186. normal distribution functions with an arbitrary covariance matrix, but
  4187. @cindex covariance matrix
  4188. the corresponding function is not provided by the @code{Rmath} C
  4189. library (or any other free library, to the author's knowledge) because
  4190. it is implemented in interpreted code. This combinator makes it
  4191. callable by an @code{avram} virtual code application nevertheless. The
  4192. disadvantage compared to the @code{library} combinator is that there
  4193. is more overhead in spawning a process than simply making a call to a
  4194. built in function, and the programming interface is more complicated.
  4195. The combinator takes the form
  4196. @table @emph
  4197. @item T35
  4198. [[@code{interact}]] @var{f} = @code{((nil,nil),(((nil,nil),nil),((nil,@var{f}),nil)))}
  4199. @end table
  4200. @noindent
  4201. where @var{f} is the virtual code for a function that
  4202. follows the same protocol described in @ref{Output From Interactive Applications},
  4203. except that it does not allow file output as described in
  4204. @ref{Mixed Modes of Interaction}. The argument @code{x} is ignored when the
  4205. expression @code{(interact f) x} is evaluated, similarly to the way the argument
  4206. is ignored in an expression like @code{(constant k) x}. The result returned
  4207. is a transcript of the dialogue that took place between @code{f} and the
  4208. externally spawned shell, represented as a list of lists of strings for
  4209. line oriented interaction, or a list of characters alternating with lists of
  4210. strings in the case of character oriented interaction.
  4211. The following example demonstrates a trivial use of the @code{interact}
  4212. combinator to spawn an @code{ftp} client, do an @code{ls} command, and then
  4213. @cindex ftp
  4214. terminate the session.
  4215. @example
  4216. eof = <(nil,(nil,(((nil,nil),nil),(nil,nil))))>
  4217. demo =
  4218. interact conditional(
  4219. conditional(identity,constant false,constant true),
  4220. constant(0,<'ftp'>,<'ftp> '>),
  4221. conditional(
  4222. conditional(left,constant false,constant true),
  4223. constant(1,<'ls',''>,<'','ftp> '>),
  4224. conditional(
  4225. compose(compare,couple(left,constant 1)),
  4226. constant(2,<'bye',''>,<eof>),
  4227. constant nil)))
  4228. @end example
  4229. @noindent
  4230. Some liberties are taken with @code{silly} syntax in this example, in
  4231. the way of using angle brackets to denote lists, and numbers to
  4232. represent states.
  4233. @itemize @bullet
  4234. @item
  4235. The interacting transducer works by checking whether its argument is
  4236. empty (via the @code{identity} function used as a predicate in the
  4237. @code{conditional}, which is then negated). In that case it returns
  4238. the triple containing the initial state of 0, the @code{ftp} shell
  4239. command to spawn the client, and the @code{'ftp> '} prompt expected
  4240. when the client has been spawned, both of the latter being lists of
  4241. strings.
  4242. @item
  4243. If the argument is non-empty, then next it checks whether it is in the
  4244. initial state of 0, (via the @code{left} function used as a predicate,
  4245. referring to the state variable expected on the left of any given
  4246. @code{(state,input)} pair, also negated). If so, it returns the triple
  4247. containing the next state of 1, the @code{ls} command followed by an
  4248. empty string to indicate a line break, and the expected prompt
  4249. preceded by an empty string to match it only at the beginning of a
  4250. line.
  4251. @item
  4252. Finally, it checks for state 1, in which case it issues the
  4253. @code{bye} command to close the session, @code{eof} rather than a
  4254. @cindex eof
  4255. prompt to wait for termination of the client, and a state of 2.
  4256. @item
  4257. In the remaining state of 2, which needn't be explicitly tested
  4258. because it is the only remaining possibility, the program returns a
  4259. @code{nil} value to indicate that the computation has
  4260. terminated.
  4261. @end itemize
  4262. Deadlock would be possible at any point if either party did not follow
  4263. @cindex deadlock
  4264. this protocol, but for this example it is not an issue. If an
  4265. expression of the form @code{demo x} were to be evaluated, then
  4266. regardless of the value of @code{x}, the value of the result would be
  4267. as shown below.
  4268. @example
  4269. <
  4270. <'ftp'>,
  4271. <'ftp> '>,
  4272. <'ls',''>,
  4273. <'ls','Not connected.','ftp> '>,
  4274. <'bye',''>,
  4275. <'bye',''>>
  4276. @end example
  4277. @noindent
  4278. That is, it would be a list of lists of strings, alternating between the
  4279. output of the interactor and the output of the @code{ftp} client. If
  4280. the spawned application had been something non-trivial such as a
  4281. computer algebra system or a command line web search utility,
  4282. then it is easy to see how functions using this combinator can leverage
  4283. off a wealth of available resources.
  4284. @node Vacant Address Space, , Interfaces to External Code, Virtual Code Semantics
  4285. @subsection Vacant Address Space
  4286. Not every possible pattern has been used by the virtual machine as a way
  4287. of encoding a function. The following patterns, where @code{@var{a}},
  4288. @code{@var{b}}, and @code{@var{c}} are non-@code{nil} trees, do not
  4289. represent anything useful.
  4290. @table @asis
  4291. @item unary forms
  4292. @code{((nil,nil),((nil,nil),(nil,((nil,@var{a}),nil))))}@*
  4293. @code{((nil,nil),((nil,nil),(nil,(nil,(nil,@var{a})))))}
  4294. @item binary forms
  4295. @code{((nil,nil),((nil,nil),(@var{a},@var{b})))}@*
  4296. @code{((nil,nil),((@var{a},nil),(@var{b},nil)))}@*
  4297. @code{((nil,nil),((@var{a},nil),(nil,@var{b})))}
  4298. @item ternary forms
  4299. @code{((nil,nil),((@var{a},@var{b}),(@var{c},nil)))}@*
  4300. @code{((nil,nil),((@var{a},@var{b}),(nil,@var{c})))}@*
  4301. @code{((nil,nil),((@var{a},nil),(@var{b},@var{c})))}@*
  4302. @code{((nil,nil),((nil,@var{a}),(@var{b},@var{c})))}
  4303. @end table
  4304. @noindent
  4305. These patterns are detected by the virtual machine simply to avoid blowing
  4306. it up, but they always cause an error message to be reported.
  4307. @cindex @code{unsupported hook}
  4308. @cindex @code{unrecognized combinator}
  4309. @table @emph
  4310. @item P55
  4311. For @code{@var{f}} matching any of the first three trees in the above list,@*
  4312. @w{ }@w{ }@w{ }@code{@var{f}_@var{n} @var{x}_@var{n}} =
  4313. [[@code{('unsupported hook',nil)}]]@code{_(@var{n}+1)}
  4314. @item P56
  4315. For the remaining trees @code{@var{f}} in the above list,@*
  4316. @w{ }@w{ }@w{ }@code{@var{f}_@var{n} @var{x}_@var{n}} =
  4317. [[@code{('unrecognized combinator (code @var{m})',nil)}]]@code{_(@var{n}+1)}
  4318. @end table
  4319. @noindent
  4320. Here, @code{@var{m}} is a numeric constant dependent on which tree
  4321. @code{@var{f}} was used. The unsupported hook message is meant to be
  4322. more informative than the unrecognized combinator message, suggesting
  4323. that a feature intended for future use is not yet available.
  4324. This list has been assembled for the benefit of readers considering the
  4325. addition of backward compatible extensions to the virtual code
  4326. semantics, who are undeterred by the facts that
  4327. @itemize @bullet
  4328. @item
  4329. the computational model
  4330. @cindex universality
  4331. is already universal
  4332. @item
  4333. virtual code applications are already
  4334. interoperable with all kinds of high performance software having a text
  4335. based or console interface by way of the @code{interact} combinator
  4336. @item
  4337. an unlimited number of built in library functions can be added
  4338. by way of the @code{library} combinator as described in
  4339. @ref{Implementing new library functions}
  4340. @item
  4341. the C code in @code{avram} makes fairly
  4342. @cindex pointers
  4343. intricate use of pointers with a careful policy of reference counting
  4344. and storage reclamation
  4345. @item
  4346. there is also a performance penalty incurred by
  4347. @cindex reference count
  4348. further extensions to the semantics, even for applications that don't
  4349. use them, because a pattern recognition algorithm in the interpreter has
  4350. more cases to consider.
  4351. @end itemize
  4352. Nevertheless, a new functional form combining a pair of functions to be
  4353. interpreted in a new way by the virtual machine could be defined using
  4354. any of the binary forms above, for example, with @code{@var{a}} as the
  4355. virtual code for one of the functions and @code{@var{b}} as that of the
  4356. other. Such a form would not conflict with any existing applications,
  4357. provided that both @code{@var{a}} and @code{@var{b}} are not @code{nil},
  4358. which is true of any valid representation for a function.
  4359. Virtual machine architects, take note. There are infinitely many trees
  4360. @cindex trees
  4361. fitting these patterns, but it would be possible to use them up by
  4362. assigning them without adequate foresight. For example, if
  4363. interpretations were assigned to the four ternary forms, the three binary
  4364. forms, and one of the remaining unary forms, then the only
  4365. unassigned pattern could be of the form
  4366. @display
  4367. @code{((nil,nil),((nil,nil),(nil,(nil,(nil,@var{a})))))}
  4368. @end display
  4369. @noindent
  4370. Assigning an interpretation to it would leave no further room for
  4371. backward compatible expansion. On the other hand, any tree of the
  4372. following form also fits the above pattern,
  4373. @display
  4374. @code{((nil,nil),((nil,nil),(nil,(nil,(nil,(@var{b},@var{c}))))))}
  4375. @end display
  4376. @noindent
  4377. with any values for @code{@var{b}} and @code{@var{c}}. Different
  4378. meanings could be chosen for the case where both are @code{nil}, both
  4379. are non-@code{nil}, or one is @code{nil} and the other non-@code{nil},
  4380. allowing two unary forms, one binary, and one constant. If at least one
  4381. of these patterns is reserved for future enhancements, then a
  4382. potentially inexhaustible supply of address space remains and there will
  4383. be no need for incompatible changes later.
  4384. @node Library Reference, Character Table, Virtual Machine Specification, Top
  4385. @chapter Library Reference
  4386. Much of the code developed for @code{avram} may be reusable in other
  4387. projects, so it has been packaged into a library and documented in this
  4388. chapter. For ease of reference, this chapter is organized with a
  4389. separate section for each source file. For the most part, each source
  4390. file encapsulates an abstract type and a number of related functions,
  4391. except for a few cases where C makes such a design awkward. An attempt
  4392. has been made to present the sections in a readable order as far as
  4393. possible.
  4394. The documentation in this chapter is confined to the application program
  4395. interface (API), and does not delve unnecessarily into any details of the
  4396. @cindex API
  4397. implementation. A reader wishing to extend, modify, or troubleshoot the
  4398. library itself can find additional information in the source code
  4399. comments. These are more likely to be in sync with the code than this
  4400. document may be, and are more readily accessible to someone working with
  4401. the code.
  4402. Some general points pertaining to the library are the following.
  4403. @itemize @bullet
  4404. @item Unlike the previous chapter, this chapter uses the word
  4405. ``function'' in the C sense rather than the mathematical sense of the word.
  4406. @item Internal errors are internal from the user's point of view, not
  4407. the developer's (@ref{Internal Errors}). Invoking these functions in
  4408. ways that are contrary to their specifications can certainly cause
  4409. internal errors (not to mention segfaults).
  4410. @item The library is definitely not thread safe, and thread safety is
  4411. @cindex threads
  4412. not a planned enhancement. The amount of locking required to make it
  4413. thread safe would probably incur an objectionable performance penalty
  4414. due to the complexity of the shared data structures involved, in
  4415. addition to being very difficult to get right. If you need these
  4416. facilities in a concurrent application, consider spawning a process for
  4417. @cindex spawning processes
  4418. each client of the library so as to keep their address spaces separate.
  4419. @item The library files are built from the standard source
  4420. distribution using GNU @command{libtool}. In the default directory
  4421. hierarchy, they will be found
  4422. either in @file{/usr/lib/libavram.*} or in @file{/usr/local/lib/libavram.*}.
  4423. These directories will differ in a non-standard installation.
  4424. @item The header files will probably be
  4425. located in either @file{/usr/include/avm/*.h} or
  4426. @file{/usr/local/include/avm/*.h} for a standard installation.
  4427. @item All exported functions, macros and constants are
  4428. preceded with @code{avm_}, so as to reduce the chance of name clashes
  4429. with other libraries. Not all type declarations or field identifiers
  4430. follow this convention, because that would be far too tedious.
  4431. @item The library header files are designed to be compatible with C++
  4432. @cindex C++
  4433. but have been tested only with C. Please refer to platform specific
  4434. documentation for further information on how to link library modules
  4435. with your own code.
  4436. @end itemize
  4437. @menu
  4438. * Lists::
  4439. * Characters and Strings::
  4440. * File Manipulation::
  4441. * Invocation::
  4442. * Version Management::
  4443. * Error Reporting::
  4444. * Profiling::
  4445. * Emulation Primitives::
  4446. * External Library Maintenance::
  4447. @end menu
  4448. @node Lists, Characters and Strings, Library Reference, Library Reference
  4449. @section Lists
  4450. The basic data structure used for representing virtual code and data in
  4451. the @code{avram} library is declared as a @code{list}.
  4452. @cindex lists
  4453. @cindex @code{head} field
  4454. @cindex @code{tail} field
  4455. The @code{list} type is a pointer to a structure having a @code{head}
  4456. field and a @code{tail} field, which are also lists. The empty tree,
  4457. @code{nil}, is represented by the C constant @code{NULL}. A tree of the
  4458. form @code{cons(@var{a},@var{b})} is represented in C as a list whose
  4459. @code{head} is the representation of @code{@var{a}} and whose
  4460. @code{tail} is the representation of @code{@var{b}}.
  4461. A number of other fields in the structure are maintained automatically
  4462. and should not be touched. For that matter, even the @code{head} and
  4463. @code{tail} fields should be considered read-only. Because of sharing,
  4464. it is almost never valid to modify a list ``in place'', except for cases
  4465. that are already covered by library functions.
  4466. @menu
  4467. * Simple Operations::
  4468. * Recoverable Operations::
  4469. * List Transformations::
  4470. * Type Conversions::
  4471. * Comparison::
  4472. * Deconstruction Functions::
  4473. * Indirection::
  4474. * The Universal Function::
  4475. @end menu
  4476. @node Simple Operations, Recoverable Operations, Lists, Lists
  4477. @subsection Simple Operations
  4478. These functions are declared in the header file @code{lists.h}, which
  4479. should be included in any C source file that uses them with a directive
  4480. such as @code{@w{#include <avm/lists.h>}}. All of these functions except
  4481. the first three have the potential cause a memory overflow. In that
  4482. @cindex overflow
  4483. event, a brief message is written to standard error and the process is
  4484. killed rather than returning to the caller. It is possible for client
  4485. programs requiring more robust behavior to do their own error handling
  4486. by using the alternative versions of these operations described in the
  4487. next section.
  4488. @deftypefun void avm_initialize_lists ()
  4489. The function @code{avm_initialize_lists} should be called before any of
  4490. the other ones in this section is called, because it sets up some
  4491. internal data structures. Otherwise, the behavior of the other functions
  4492. is undefined.
  4493. @end deftypefun
  4494. @deftypefun void avm_dispose (list @var{front})
  4495. This function deallocates the memory associated with a given list,
  4496. either by consigning it to a cache maintained internally by the library,
  4497. or by the standard @code{free} function if the cache is full. Shared
  4498. lists are taken into account and handled properly according to a
  4499. reference counting scheme. Lists should be freed only by this function,
  4500. not by using @code{free} directly.
  4501. @end deftypefun
  4502. @deftypefun void avm_count_lists ()
  4503. If a client program aims to do its own storage reclamation, this
  4504. function can be called optionally at the end of a run when it is
  4505. believed that all lists have been freed. If any allocated lists remain
  4506. at large, a warning will be printed to standard error. This function
  4507. therefore provides a useful check for memory leaks. Overhead is small
  4508. enough that it is not infeasible to leave this check in the production
  4509. code.
  4510. @end deftypefun
  4511. @deftypefun list avm_copied (list @var{operand})
  4512. A copy of the argument list is returned by this function. The copy
  4513. remains intact after the original is reclaimed. A typical use might be
  4514. for retaining part of a list after the rest of it is no longer
  4515. needed. In this example, a list @code{x} is traversed by a hypothetical
  4516. @code{visit} function to each item, which is then immediately reclaimed.
  4517. @example
  4518. while(x)@{
  4519. visit(x->head);
  4520. old_x = x;
  4521. x = avm_copied(x->tail); /* the right way */
  4522. avm_dispose(old_x);
  4523. @}
  4524. @end example
  4525. This example allows each item in the list to be visited even as
  4526. previously visited items are reclaimed, because @code{x} is copied at
  4527. each iteration. This example contrasts with the next one, which will
  4528. probably cause a segmentation fault.
  4529. @cindex segmentation fault
  4530. @example
  4531. while(x)@{
  4532. visit(x->head);
  4533. old_x = x;
  4534. x = x->tail; /* the wrong way */
  4535. avm_dispose(old_x);
  4536. @}
  4537. @end example
  4538. In the second example, a reference is made to a part of a list which no
  4539. longer exists because it has been deallocated.
  4540. In fact, the @code{avm_copied} function does nothing but increment a
  4541. reference count, so it is a fast, constant time operation that requires
  4542. @cindex reference count
  4543. no additional memory allocation. Semantically this action is equivalent
  4544. to creating a fresh copy of the list, because all list operations in the
  4545. library deal with reference counts properly.
  4546. @end deftypefun
  4547. @deftypefun list avm_join (list @var{left}, list @var{right})
  4548. This function takes a pair of lists to a list in which the left is the
  4549. head and the right is the tail. It may need to use @code{malloc} to
  4550. allocate additional memory. If there is insufficient memory, an error
  4551. message is written to standard error and the program exits.
  4552. When the list returned by @code{avm_join} is eventually deallocated, the
  4553. lists from which it was built are taken with it and must not be
  4554. referenced again. For example, the following code is an error.
  4555. @example
  4556. z = avm_join(x,y);
  4557. @dots{}
  4558. avm_dispose(z);
  4559. avm_print_list(x); /* error here */
  4560. @end example
  4561. To accomplish something similar to this without an error, a copy of
  4562. @code{x} should be made, as in the next example.
  4563. @example
  4564. z = avm_join(avm_copied(x),y);
  4565. @dots{}
  4566. avm_dispose(z);
  4567. avm_print_list(x); /* original x still intact */
  4568. @end example
  4569. @end deftypefun
  4570. @deftypefun void avm_enqueue (list *@var{front}, list *@var{back}, list @var{operand})
  4571. @cindex queues
  4572. A fast simple way of building a list head first is provided by the
  4573. @code{enqueue} function. The @code{front} is a pointer to the beginning
  4574. of the list being built, and the @code{back} is a pointer to the last
  4575. item. The recommended way to use it would be something like this.
  4576. @example
  4577. front = back = NULL;
  4578. avm_enqueue(&front,&back,item);
  4579. avm_enqueue(&front,&back,next_item);
  4580. avm_enqueue(&front,&back,another_item);
  4581. @dots{}
  4582. @end example
  4583. It might be more typical for the calls to @code{avm_enqueue} to appear
  4584. within a loop. In any case, after the above code is executed, the
  4585. following postconditions will hold.
  4586. @example
  4587. front->head == item
  4588. front->tail->head == next_item
  4589. front->tail->tail->head == another_item
  4590. back->head == another_item
  4591. back->tail == NULL
  4592. @end example
  4593. The @code{avm_enqueue} function must never be used on a shared list, because
  4594. it modifies its arguments in place. The only practical way to guarantee
  4595. that a list is not shared is to initialize the @code{front} and @code{back} to
  4596. @code{NULL} as shown before the first call to @code{avm_enqueue}, and to
  4597. make no copies of @code{front} or @code{back} until after the last call
  4598. to @code{avm_enqueue}.
  4599. Because a list built with @code{avm_enqueue} is not shared, it is one of the
  4600. few instances of a list that can have something harmlessly appended to
  4601. it in place. For example, if the next line of code were
  4602. @example
  4603. back->tail = rest_of_list;
  4604. @end example
  4605. that would be acceptable assuming @code{rest_of_list} is not shared and
  4606. does not conceal a dangling or cyclic reference, and if nothing further
  4607. were enqueued.
  4608. The items that are enqueued into a list are not copied and will be
  4609. deallocated when the list is deallocated, so they must not be referenced
  4610. thereafter. A non-obvious violation of this convention is implicit in
  4611. the following code.
  4612. @example
  4613. @dots{}
  4614. avm_enqueue(&front,&back,x->head);
  4615. @dots{}
  4616. avm_dispose(front);
  4617. avm_print_list(x); /* error here */
  4618. @end example
  4619. This code might cause a segmentation fault because of the reference to
  4620. @cindex segmentation fault
  4621. @code{x} after its head has been deallocated. The following code is
  4622. subject to the same problem,
  4623. @example
  4624. @dots{}
  4625. avm_enqueue(&front,&back,x->head);
  4626. @dots{}
  4627. avm_dispose(x);
  4628. avm_print_list(front); /* error here */
  4629. @end example
  4630. as is the following.
  4631. @example
  4632. @dots{}
  4633. avm_enqueue(&front,&back,x->head);
  4634. @dots{}
  4635. avm_dispose(x); /* front is now impossible to reclaim */
  4636. avm_dispose(front);
  4637. @end example
  4638. The problem with the last example is that it is not valid even to
  4639. dispose of the same list more than once, albeit indirectly.
  4640. If part of a list is intended to be enqueued temporarily or
  4641. independently of its parent, the list should be copied explicitly, as
  4642. the following code demonstrates.
  4643. @example
  4644. @dots{}
  4645. avm_enqueue(&front,&back,avm_copied(x->head)); /* correct */
  4646. @dots{}
  4647. avm_dispose(front);
  4648. avm_print_list(x);
  4649. @end example
  4650. @end deftypefun
  4651. @deftypefun counter avm_length (list @var{operand})
  4652. A @code{counter} is meant to be the longest unsigned integer available
  4653. @cindex @code{counter}
  4654. on the host machine, and is defined in @code{common.h}, which is
  4655. automatically included whenever @code{lists.h} is included. The
  4656. @code{avm_length} function returns the number of items in a list. If a
  4657. list is @code{NULL}, a value of zero is returned. There is a possibility
  4658. of a counter overflow error from this function (@ref{Overflow Errors}),
  4659. but only on a platform where the @code{counter} type is shorter than the
  4660. address length.
  4661. @end deftypefun
  4662. @deftypefun counter avm_area (list @var{operand})
  4663. This function is similar to @code{avm_length}, but it treats its
  4664. argument as a list of lists and returns the summation of their lengths.
  4665. @end deftypefun
  4666. @deftypefun list avm_natural (counter @var{number})
  4667. @cindex naturals
  4668. This function takes a @code{counter} to its representation as a list, as
  4669. described in @ref{Representation of Numeric and Textual Data}. That is,
  4670. the number is represented as a list of bits, least significant bit
  4671. first, with each zero bit represented by @code{NULL} and each one bit
  4672. represented by a list whose @code{head} and @code{tail} are @code{NULL}.
  4673. @end deftypefun
  4674. @deftypefun void avm_print_list (list @var{operand})
  4675. The @code{avm_print_list} function is not used in any production code
  4676. but retained in the library for debugging purposes. It prints a list to
  4677. @cindex standard output
  4678. standard output using an expression involving only commas and parentheses,
  4679. as per the @code{silly} syntax (@ref{A Simple Lisp Like Language}). The
  4680. results quickly become unintelligible for lists of any significant size.
  4681. The function is recursively defined and will crash in the event of a
  4682. stack overflow, which will occur in the case of very large or cyclic
  4683. lists.
  4684. @end deftypefun
  4685. @deftypefun list avm_position (list @var{key}, list @var{table}, int *@var{fault})
  4686. This function searches for a @var{key} in a short @var{table} where
  4687. each item is a possible key.
  4688. If it's not found, a @code{NULL} value is returned. If it's
  4689. found, a list representing a character encoding according to
  4690. @ref{Character Table} is returned.
  4691. The ascii code of the character corresponding to the returned list is
  4692. the position of the @var{key} in the @var{table}, assuming position
  4693. numbers start with 1.
  4694. The table should have a length of 255 or less. If it's longer and the
  4695. @var{key} is found beyond that range, the higher order bits of the
  4696. position number are ignored.
  4697. The integer referenced by @var{fault} is set to a non-zero value in
  4698. the event of a memory overflow, which could happen in the course of
  4699. the list comparisons necessary for the search.
  4700. @end deftypefun
  4701. @node Recoverable Operations, List Transformations, Simple Operations, Lists
  4702. @subsection Recoverable Operations
  4703. The functions in this section are similar to the ones in the previous
  4704. section except with regard to error handling. Whereas the other ones
  4705. cause an error message to be printed and the process to exit in the
  4706. event of an overflow, these return to the caller, whose responsibility
  4707. it is to take appropriate action. The functions in both sections are
  4708. declared in @file{lists.h}, and should be preceded by a call to
  4709. @code{avm_initialize_lists}.
  4710. @deftypefun list avm_recoverable_join (list @var{left}, list @var{right})
  4711. This function is similar to @code{avm_join}, but will return a
  4712. @code{NULL} pointer if memory that was needed can not be allocated. A
  4713. @code{NULL} pointer would never be the result of a join under normal
  4714. circumstances, so the overflow can be detected by the caller. Regardless
  4715. of whether overflow occurs, the arguments are deallocated by this function
  4716. and should not be referenced thereafter.
  4717. @end deftypefun
  4718. @deftypefun void avm_recoverable_enqueue (list *@var{front}, list *@var{back}, list @var{operand}, int *@var{fault})
  4719. This version of the enqueue function will dispose of the @code{@var{operand}} if there
  4720. isn't room to append another item and set @code{*@var{fault}} to a non-zero
  4721. value. Other than that, it does the same as @code{avm_enqueue}.
  4722. @end deftypefun
  4723. @deftypefun counter avm_recoverable_length (list @var{operand})
  4724. This function checks for arithmetic overflow when calculating the length
  4725. of a list, and returns a zero value if overflow occurs. The caller can
  4726. detect the error by noting that zero is not the length of any list other
  4727. than @code{NULL}. This kind of overflow is impossible unless the host
  4728. does not have long enough integers for its address space.
  4729. @end deftypefun
  4730. @deftypefun counter avm_recoverable_area (list @var{operand}, int *@var{fault})
  4731. This function is similar to @code{avm_area}, except that it reacts
  4732. differently to arithmetic overflow. The @code{fault} parameter should be
  4733. the address of an integer known to the caller, which will be set to a
  4734. non-zero value if overflow occurs. In that event, the value of zero will
  4735. also be returned for the area. Note that it is possible for non-empty
  4736. lists to have an area of zero, so this condition alone is not indicative
  4737. of an error.
  4738. @end deftypefun
  4739. @deftypefun list avm_recoverable_natural (counter @var{number})
  4740. This function returns the @code{list} representation of a native
  4741. unsigned long integer, provided that there is enough memory, similarly to the
  4742. @code{avm_natural} function. Unlike that function, this one will return
  4743. a value of @code{NULL} rather than exiting the program in the event of a
  4744. memory overflow. The overflow can be detected by the caller insofar as a
  4745. @code{NULL} @code{list} does not represent any number other than zero.
  4746. @end deftypefun
  4747. @node List Transformations, Type Conversions, Recoverable Operations, Lists
  4748. @subsection List Transformations
  4749. Some functions declared in @file{listfuns.h} are used to implement the
  4750. operations described in @ref{List Functions}. These functions are able
  4751. to report error messages in the event of overflow or other exceptional
  4752. @cindex overflow
  4753. @cindex exceptions
  4754. @cindex error messages
  4755. conditions, as described in @ref{Error Messages}. The error messages are
  4756. represented as lists and returned to the caller. The occurrence of an
  4757. error can be detected by the @code{*@var{fault}} flag being set to a
  4758. non-zero value. None of these functions ever causes a program exit except
  4759. in the event of an internal error.
  4760. @deftypefun void avm_initialize_listfuns ()
  4761. This has to be called before any of the other functions in this section
  4762. is called. It initializes the error message lists, among other things.
  4763. @end deftypefun
  4764. @deftypefun void avm_count_listfuns ()
  4765. At the end of a run, a call to this function can verify that no
  4766. unreclaimed storage attributable to these functions persists. If it
  4767. does, a warning is printed to standard error. If @code{avm_count_lists}
  4768. is also used, it must be called after this function.
  4769. @end deftypefun
  4770. @deftypefun list avm_reversal (list @var{operand}, int *@var{fault})
  4771. The reversal of the list is returned by this function if no overflow
  4772. occurs. A non-zero @code{*@var{fault}} and an error message are returned
  4773. otherwise. The original @code{@var{operand}} still exists in its original
  4774. order after this function is called. The amount of additional storage
  4775. allocated is proportional only to the length of the list, not the size
  4776. of its contents.
  4777. @end deftypefun
  4778. @deftypefun list avm_distribution (list @var{operand}, int *@var{fault})
  4779. This function performs the operation described in @ref{Distribute}. The
  4780. invalid distribution message is returned in the event of a @code{NULL}
  4781. operand. Otherwise, the returned value is the distributed list. In any event,
  4782. the @code{@var{operand}} is unaffected.
  4783. @end deftypefun
  4784. @deftypefun list avm_concatenation (list @var{operand}, int *@var{fault})
  4785. @cindex concatenation
  4786. The @code{@var{operand}} is treated as a pair of lists to be concatenated, with the
  4787. left one in the @code{head} field and the right one in the @code{tail}
  4788. field. The invalid concatenation message is returned in the event of a
  4789. @code{NULL} @code{@var{operand}}. The result returned otherwise is the concatenation
  4790. of the lists, but the given @code{@var{operand}} still exists unchanged.
  4791. @end deftypefun
  4792. @deftypefun list avm_transposition (list @var{operand}, int *@var{fault})
  4793. The operation performed by this function corresponds to that of
  4794. @ref{Transpose}. Unlike other functions in this section, the operand
  4795. passed to this function is deallocated, and must not be referenced
  4796. @cindex @code{transpose}
  4797. thereafter. The transposed list is accessible as the returned value of
  4798. this function. If the original @code{@var{operand}} is still needed
  4799. after a call to @code{avm_transposition}, only a copy of it should be
  4800. passed to it, obtained from @code{avm_copied}. The invalid transpose
  4801. error message is the result if the operand does not represent a list of equal
  4802. length lists.
  4803. @end deftypefun
  4804. @deftypefun list avm_membership (list @var{operand}, int *@var{fault})
  4805. This function computes the membership predicate described in
  4806. @cindex @code{member}
  4807. @ref{Member}. The operand is a list in which the @code{tail} field is
  4808. a list that will be searched for the item in the @code{head}. If the
  4809. item is not found, a @code{NULL} list is returned, but otherwise a list
  4810. with @code{NULL} @code{head} and @code{tail} fields is returned. If the
  4811. operand is @code{NULL}, an error message of invalid membership is
  4812. returned and @code{*@var{fault}} is set to a non-zero value.
  4813. The @code{avm_membership} function calls @code{avm_binary_comparison}
  4814. in order to compare lists, so the same efficiency and side-effect
  4815. considerations are relevant to both (@ref{Comparison}). It is not
  4816. necessary to @code{#include} the header file @code{compare.h} or to
  4817. call @code{avm_initialize_compare} in order to use
  4818. @code{avm_membership}, because they will be done automatically.
  4819. @end deftypefun
  4820. @deftypefun list avm_binary_membership (list @var{operand}, list @var{members}, int *@var{fault});
  4821. This function is the same as @code{avm_membership} except that it
  4822. allows the element and the set of members to be passed as separate
  4823. lists instead of being the head and the tail of the same list.
  4824. @end deftypefun
  4825. @deftypefun list avm_measurement (list @var{operand}, int *@var{fault})
  4826. This function implements the operation described in @ref{Weight}, which
  4827. pertains to the weight of a tree. The returned value of this function is
  4828. a list encoding the weight as a binary number, unless a counter overflow
  4829. occurs, in which case it's an error message. As noted previously, the
  4830. weight of a tree can easily be exponentially larger than the amount of
  4831. @cindex native integer arithmetic
  4832. memory it occupies, but this function uses native integer arithmetic for
  4833. performance reasons. Hence, a counter overflow is a real
  4834. possibility.
  4835. @end deftypefun
  4836. @node Type Conversions, Comparison, List Transformations, Lists
  4837. @subsection Type Conversions
  4838. External library functions accessed by the @code{library} combinator
  4839. as explained in @ref{Library combinator} may operate on data other
  4840. than the @code{list} type usually used by @code{avram}, such as
  4841. floating point numbers and arrays, but a virtual code application must
  4842. be able to represent the arguments and results of these functions in
  4843. order to use them. As a matter of convention, a data structure
  4844. occupying @var{size} bytes of contiguous storage on the host machine
  4845. appears as a list of length @var{size} to a virtual code application,
  4846. in which each item corresponds to a byte, and is represented according
  4847. to @ref{Character Table}.
  4848. In principle, a virtual code application invoking a library function
  4849. to operate on a contiguous block of data, such as an IEEE double
  4850. precision number, for example, would construct a list of eight
  4851. character representations (one for each byte in a double precision
  4852. number), and pass this list as an argument to the library
  4853. function. The virtual machine would transparently convert this
  4854. representation to the native floating point format, evaluate the
  4855. function, and convert the result back to a list. In practice, high
  4856. level language features beyond the scope of this document would
  4857. insulate the programmer from some of the details on the application
  4858. side as well.
  4859. To save the time of repeatedly converting between the list
  4860. representation and the contiguous native binary representation, the
  4861. structure referenced by a @code{list} pointer contains a @code{value}
  4862. @cindex value field
  4863. field which is a @code{void} pointer to a block of memory of
  4864. unspecified type, and serves as a persistent cache of the value
  4865. represented by the list. This field normally should be managed by the
  4866. API rather than being accessed directly by client modules, but see the
  4867. code in @file{mpfr.c} for an example of a situation in which it's
  4868. appropriate to break this rule. (Generally these situations involve
  4869. library functions operating on non-contiguous data.)
  4870. @menu
  4871. * Primitive types::
  4872. * One dimensional arrays::
  4873. * Two dimensional arrays::
  4874. * Related utility functions::
  4875. @end menu
  4876. @node Primitive types, One dimensional arrays, Type Conversions, Type Conversions
  4877. @subsubsection Primitive types
  4878. A pair of functions in support of this abstraction is prototyped in
  4879. @file{listfuns.h}. These functions will be of interest mainly to
  4880. developers wishing to implement an interface to a new library module
  4881. and make it accessible on the virtual side by way of the
  4882. @code{library} combinator (@ref{Library combinator}).
  4883. @deftypefun void *avm_value_of_list (list @var{operand}, list *@var{message}, int *@var{fault})
  4884. This function takes an @var{operand} representing a value used by a
  4885. library function in the format described above (@ref{Type
  4886. Conversions}) and returns a pointer to the value.
  4887. The @code{value} field in the @var{operand} normally will point to the
  4888. block of memory holding the value, and the @var{operand} itself will
  4889. be a list of character representations whose binary encodings spell
  4890. out the value as explained above.
  4891. The @code{value} field need not be initialized on entry but it will be
  4892. initialized as a side effect of being computed by this function. If it
  4893. has been initialized due to a previous call with the same
  4894. @var{operand}, this function is a fast constant time operation.
  4895. The caller should not free the pointer returned by this function
  4896. because a reference to its value will remain in the
  4897. @var{operand}. When the @var{operand} itself is freed by
  4898. @code{avm_dispose} (@ref{Simple Operations}), the value will go with it.
  4899. If an error occurs during the evaluation of this function, the integer
  4900. referenced by @var{fault} will be set to a non-zero value, and the
  4901. list referenced by @var{message} will be assigned a representation of
  4902. a list of strings describing the error. The @var{message} is freshly
  4903. created and should be freed by the caller with @code{avm_dispose}
  4904. when no longer needed.
  4905. Possible error messages are @code{<'missing value'>}, in the case of
  4906. @cindex missing value
  4907. an empty @var{operand}, @code{<'invalid value'>} in the case of an
  4908. @cindex invalid value
  4909. @var{operand} that is not a list of character representations, and
  4910. @code{<'memory overflow'>} if there was insufficient space to allocate
  4911. the result.
  4912. @end deftypefun
  4913. @deftypefun list avm_list_of_value (void *@var{contents}, size_t @var{size}, int *@var{fault})
  4914. This function performs the inverse operation of
  4915. @code{avm_value_of_list}, taking the address of an area of
  4916. contiguously stored data and its @var{size} in bytes to a list
  4917. representation. The length of the list returned is equal to the number
  4918. of bytes of data, @var{size}, and each item of the list is a character
  4919. representation for the corresponding byte as given by @ref{Character
  4920. Table}.
  4921. A copy of the memory area is made so that the original is no longer
  4922. needed and may be freed by the caller. A pointer to this copy is
  4923. returned by subsequent calls to @code{avm_value_of_list} when the
  4924. result returned by this function is used as the @var{operand}
  4925. parameter.
  4926. If there is insufficient memory to allocate the result, the integer
  4927. referenced by @var{fault} is set to a non-zero value, and a copy of
  4928. the message @code{<'memory overflow'>} represented as a list is
  4929. returned. This function could also cause a segmentation fault if it is
  4930. @cindex segmentation fault
  4931. passed an invalid pointer or a @var{size} that overruns the storage
  4932. area. However, it is acceptable to specify a @var{size} that is less
  4933. than the actual size of the given memory area to construct a list
  4934. representing only the first part of it. The @var{size} must always be
  4935. greater than zero.
  4936. @end deftypefun
  4937. @node One dimensional arrays, Two dimensional arrays, Primitive types, Type Conversions
  4938. @subsubsection One dimensional arrays
  4939. A couple of functions declared in @file{matcon.h} are concerned mainly
  4940. with one dimensional arrays or vectors. They have been used for
  4941. @cindex arrays
  4942. vectors of double precision and complex numbers, but are applicable to
  4943. @cindex vectors
  4944. any base type that is contiguous and of a fixed size.
  4945. The motivation for these functions is to enable a developer to present
  4946. an API to virtual code applications wherein external library functions
  4947. operating natively on one dimensional arrays of numbers are seen from
  4948. the virtual side to operate on lists of numbers. Lists are the
  4949. preferred container for interoperability with virtual code
  4950. applications.
  4951. @deftypefun void *avm_vector_of_list (list @var{operand}, size_t @var{item_size}, list *@var{message}, int *@var{fault})
  4952. This function calls @code{avm_value_of_list} (@ref{Primitive types})
  4953. for each item of the @var{operand} and puts all the values together
  4954. into one contiguous block, whose address is returned.
  4955. The given @var{item_size} is required to be the lengths of the items,
  4956. all necessarily equal, and is required only for validation. For
  4957. example, @var{item_size} is 8 for a list of double precision numbers,
  4958. because they occupy 8 bytes each and are represented as lists of length 8.
  4959. The total number of bytes allocated is the product of @var{item_size}
  4960. and the length of the @var{operand}. Unlike the case of
  4961. @code{avm_value_of_list} (@ref{Primitive types}), the result returned
  4962. by this function should be explicitly freed by the caller when no
  4963. longer needed.
  4964. Any errors such as insufficient memory cause the integer referenced by
  4965. @var{fault} to be assigned a non-zero value and the @var{message} to
  4966. be assigned an error message represented as a list of strings. An
  4967. error message of @code{<'bad vector specification'>} is possible in
  4968. the case of an empty @var{operand} or one whose item lengths don't
  4969. match the given @var{item_size}. Error messages caused by
  4970. @code{avm_value_of_list} can also be generated by this function. Any
  4971. non-empty error message should be reclaimed by the caller using
  4972. @code{avm_dispose} (@ref{Simple Operations}). If an error occurs, a
  4973. @code{NULL} pointer is returned.
  4974. @end deftypefun
  4975. @deftypefun list avm_list_of_vector (void *@var{vector}, int @var{num_items}, size_t @var{item_size}, int *@var{fault})
  4976. This function takes it on faith that an array of dimension
  4977. @var{num_items} in which each item occupies @var{item_size} bytes
  4978. begins at the address given by @var{vector}. A list representation of
  4979. each item in the array is constructed by the function
  4980. @code{avm_list_of_value} (@ref{Primitive types}), and a list of all of
  4981. the lists thus obtained in order of their position in the array is
  4982. returned.
  4983. In the event of any errors caused by @code{avm_list_of_value} or
  4984. errors due to insufficient memory, the error message is returned as
  4985. the function result, and the integer referenced by @var{fault} is
  4986. assigned a non-zero value. The error message is in the form of a list
  4987. of character string representations. A segmentation fault is possible
  4988. @cindex segmentation fault
  4989. if @var{vector} is not a valid pointer or if the array size implied by
  4990. misspecified values of @var{num_items} and @var{item_size}
  4991. exceeds its actual size.
  4992. @end deftypefun
  4993. @node Two dimensional arrays, Related utility functions, One dimensional arrays, Type Conversions
  4994. @subsubsection Two dimensional arrays
  4995. Several other functions in @file{matcon.h} are meant to support
  4996. conversions between matrices represented as lists of lists and arrays
  4997. in a variety of representations. Dense matrices either square or
  4998. @cindex matrices
  4999. rectangular are accommodated, and symmetric square matrices can be
  5000. stored with redundant entries omitted in either upper trangular or
  5001. lower triangular format.
  5002. Similarly to the vector operations (@ref{One dimensional arrays})
  5003. these functions are intended to allow a developer to present an
  5004. interface to external libraries based on lists rather than arrays.
  5005. The preferred convention for virtual code applications is to represent
  5006. a matrix as a list of lists of entities (typically numbers), with
  5007. one list for each row of the matrix. For example, a 3 by 3 matrix
  5008. containing a value of @code{aij} in the @code{i}-th row and the
  5009. @code{j}-th column would be represented by this list of three lists.
  5010. @example
  5011. <
  5012. <a11,a12,a13>,
  5013. <a21,a22,a23>,
  5014. <a31,a32,a33>>
  5015. @end example
  5016. @noindent
  5017. Such a representation is convenient for manipulation by
  5018. virtual machine combinators, for example @code{transpose}
  5019. (@ref{Transpose}), and is readily identified with the matrix
  5020. it represents.
  5021. If a matrix is symmetric (that is, with @code{aij} equal to
  5022. @code{aji} for all values of @code{i} and @code{j}), only the lower
  5023. triangular portion needs to be stored because the other entries are
  5024. @cindex triangular matrix
  5025. redundant. The list representatation would be something like this.
  5026. @example
  5027. <
  5028. <a11>,
  5029. <a21,a22>,
  5030. <a31,a32,a33>>
  5031. @end example
  5032. Another alternative for representing a symmetric matrix is to store only the
  5033. upper triangular portion. In this case, a list such as the following would be used.
  5034. @example
  5035. <
  5036. <a11,a12,a13>,
  5037. <a22,a23>,
  5038. <a33>>
  5039. @end example
  5040. @noindent
  5041. The upper and lower triangular representations are distinguishable by
  5042. whether or not the row lengths form an increasing sequence.
  5043. In addition to representing symmetric matrices, these upper and lower
  5044. @cindex symmetric matrix
  5045. triangular forms are also appropriate for representing matrices whose
  5046. remaining entries are zero, such as the factors in an LU
  5047. decomposition.
  5048. @cindex LU decomposition
  5049. @deftypefun void *avm_matrix_of_list (int @var{square}, int @var{upper_triangular}, int @var{lower_triangular}, int @var{column_major}, list @var{operand}, size_t @var{item_size}, list *@var{message}, int *@var{fault})
  5050. This function converts a matrix in one of the list representations
  5051. above to a contiguous array according to the given specifications.
  5052. The array can contain elements of any fixed sized type of size
  5053. @var{item_size}. The memory for it is allocated by this function and
  5054. it should be freed by the caller when no longer needed.
  5055. The input matrix is given by the list parameter, @var{operand}, and
  5056. its format is described by the integer parameters @var{square},
  5057. @var{upper_triangular}, and @var{lower_triangular}. The number of
  5058. bytes occupied by each entry is given by @var{item_size}.
  5059. To the extent these specifications are redundant, they are used for
  5060. validation. If any of the following conditions is not met, the integer
  5061. referenced by @var{fault} is assigned a non-zero value and a copy
  5062. of the message @code{<'bad matrix specification'>} represented as
  5063. a list is assigned to the list referenced by @var{message}. Errors
  5064. are also possible due to insufficient memory.
  5065. @itemize @bullet
  5066. @item
  5067. The @var{operand} must be a list of lists of lists such that
  5068. each item of each item is has a length of @var{item_size},
  5069. and its items consist of character representations as
  5070. required by @code{avm_value_of_list} (@ref{Primitive types}).
  5071. @item
  5072. If the lengths of the top level lists in the @var{operand} form an
  5073. increasing sequence, the lower triangular representation is assumed
  5074. and the @var{lower_triangular} parameter must have a non-zero value.
  5075. @item
  5076. If the lengths of the top level lists in the @var{operand} form a
  5077. decreasing sequence, the upper triangular representation is assumed
  5078. and the @var{upper_triangular} parameter must have a non-zero value.
  5079. @item
  5080. At least one of @var{upper_triangular} or @var{lower_triangular} must
  5081. be zero.
  5082. @item
  5083. If @var{square} has a non-zero value, then either all items of the
  5084. @var{operand} must have the same length as the operand, or if it's
  5085. triangular, then the longest one must have the same length as the
  5086. operand.
  5087. @item
  5088. If the @var{operand} is neither square nor a triangular form, all
  5089. items of it are required to have the same length.
  5090. @end itemize
  5091. The parameters @var{upper_triangular} or @var{lower_triangular} may be
  5092. set to non-zero values even if the @var{operand} is not in one of the
  5093. upper or lower triangular forms discussed above. In this case, the
  5094. @var{operand} must be square or rectangular (i.e., with all items the
  5095. same length), and the following interpretations apply.
  5096. @itemize @bullet
  5097. @item
  5098. If @var{upper_triangular} is non-zero, the diagonal elements and the
  5099. upper triangular portion of the input matrix are copied to the output.
  5100. The lower triangle of the input is ignored and the lower triangle of
  5101. the output is left uninitialized.
  5102. @item
  5103. If @var{lower_triangular} is non-zero, the diagonal elements and the
  5104. lower triangular portion of the input matrix are copied to the output.
  5105. The upper triangle of the input is ignored and the upper triangle of
  5106. the output is left uninitialized.
  5107. @end itemize
  5108. The @var{column_major} parameter affects the form of the output array.
  5109. If it is zero, then each row of the input matrix is stored in a
  5110. contiguous block of memory in the output array, and if it is non-zero,
  5111. each column is stored contiguously.
  5112. @cindex Fortran
  5113. The latter representation is also
  5114. known as Fortran order and may be required by library functions
  5115. written in Fortran.
  5116. In all cases when a triangular form is specified, part of the output
  5117. matrix is left uninitialized. The redundant entries may be assigned if
  5118. required by the @code{avm_reflect_matrix} function (@ref{Related
  5119. utility functions}).
  5120. @end deftypefun
  5121. @deftypefun list avm_list_of_matrix (void *@var{matrix}, int @var{rows}, int @var{cols}, size_t @var{item_size}, int *@var{fault})
  5122. This function performs an inverse operation to
  5123. @code{avm_matrix_of_list} by taking the address of a matrix stored as
  5124. a contiguous array in the parameter @var{matrix} and constructing the
  5125. list representation as discussed above. Only square and rectangular
  5126. matrices in row major order are supported, but see
  5127. @code{avm_matrix_transposition} for a way to convert between row major
  5128. @cindex column major order
  5129. and column major order (@ref{Related utility functions}).
  5130. The parameters @var{rows}, @var{cols}, and @var{item_size} describe
  5131. the form of the matrix. The list returned as a result will have a
  5132. length of @var{rows}, and each item will be a list of length
  5133. @var{cols}. Each item of the result corresponds to a row of the
  5134. matrix, and each item of the items represents the an entry of the
  5135. matrix as a list of length @var{item_size}. These items could be
  5136. passed to @code{avm_value_of_list}, for example, to obtain their
  5137. values (@ref{Primitive types}).
  5138. Memory is allocated by this function to create the list, which can be
  5139. reclaimed by @code{avm_dispose} (@ref{Simple Operations}). If there is
  5140. insufficient memory, the integer referenced by @var{fault} is assigned
  5141. a non-zero value and the result returned is a list representation of
  5142. the message @code{<'memory overflow'>}. The error message be reclaimed
  5143. by the caller as well using @code{avm_dispose}.
  5144. @end deftypefun
  5145. A packed storage representation for symmetric square matrices and
  5146. @cindex packed arrays
  5147. triangular matrices is of interest because it is used by some library
  5148. functions, notably those in @code{LAPACK}, to save memory and thereby
  5149. accommodate larger problems. In this representation, column major
  5150. @cindex column major order
  5151. order is assumed, and either the lower or the upper triangle of the
  5152. matrix is not explicitly stored. For example, a lower triangular
  5153. @cindex triangular matrix
  5154. matrix whose list representation corresponds to
  5155. @example
  5156. <
  5157. <a11>,
  5158. <a21,a22>,
  5159. <a31,a32,a33>,
  5160. <a41,a42,a43,a44>>
  5161. @end example
  5162. @noindent
  5163. would be stored according to the memory map
  5164. @cindex matrix memory map
  5165. @example
  5166. [a11 a21 a31 a41 a22 a32 a42 a33 a43 a44]
  5167. @end example
  5168. @noindent
  5169. with @code{a11} at the beginning address. An upper triangular matrix
  5170. @example
  5171. <
  5172. <a11,a12,a13,a14>,
  5173. <a22,a23,a24>,
  5174. <a33,a34>,
  5175. <a44>>
  5176. @end example
  5177. @noindent
  5178. would be stored according to the memory map
  5179. @example
  5180. [a11 a12 a22 a13 a23 a33 a14 a24 a34 a44].
  5181. @end example
  5182. A couple of functions converting between list representations and
  5183. packed array format are provided as described below.
  5184. @deftypefun void *avm_packed_matrix_of_list (int @var{upper_triangular}, list @var{operand}, int @var{n}, size_t @var{item_size}, list *@var{message}, int *@var{fault})
  5185. If the @var{operand} is a list in one of the triangular forms
  5186. explained above, then the @var{upper_triangular} parameter must be
  5187. consisitent with it, being non-zero if the @var{operand} is upper
  5188. triangular and zero otherwise.
  5189. If the @var{operand} is not in a triangular form, then each item of
  5190. the operand must be a list of length @var{n}. In this case, the
  5191. @var{upper_triangular} parameter indicates which triangle of the
  5192. operand should be copied to the result, and the other triangle is
  5193. ignored.
  5194. In either case, the operand must have a length of @var{n}, and the
  5195. items of its items must be lists of length @var{item_size} containing
  5196. character representations as required by @code{avm_value_of_list}
  5197. (@ref{Primitive types}).
  5198. If the input parameters are inconsistent or if there is insufficient
  5199. memory to allocate the result, the integer referenced by @var{fault}
  5200. is assigned a non-zero value, and the list referenced by @var{message}
  5201. is assigned a copy of the list representation of @code{<'bad matrix
  5202. specification'>} or @code{<'memory overflow'>}, respectively. A
  5203. non-empty message must be reclaimed by the caller using
  5204. @code{avm_dispose} (@ref{Simple Operations}).
  5205. If there are no errors, the result is a pointer to a packed array
  5206. representation of the @var{operand} as explained above. The memory for
  5207. this result is allocated by this function and should be freed by the
  5208. caller when no longer required. The number of bytes allocated will be
  5209. @var{item_size} * (@var{n} * (@var{n} + 1))/2.
  5210. @end deftypefun
  5211. @deftypefun list avm_list_of_packed_matrix (int @var{upper_trianguler},void *@var{operand}, int @var{n}, size_t @var{item_size}, int *@var{fault})
  5212. This function performs an inverse operation to that of
  5213. @code{avm_packed_matrix_of_list} given the address of a packed matrix
  5214. stored according to one of the memory maps discussed above. The
  5215. @var{operand} parameter holds the address, the parameter @var{n} gives
  5216. the number of rows, and the @var{upper_triangular} parameter specifies
  5217. which of the two possible memory maps to assume.
  5218. If there is sufficient memory, the result returned is a list in one of
  5219. the triangular forms described above, being upper triangular if the
  5220. @var{upper_triangular} parameter is non-zero, with values of length
  5221. @var{item_size} taken from the array.
  5222. In the event of a memory overflow, the integer referenced by
  5223. @var{fault} is assigned a non-zero value and the result is a copy of
  5224. the message @code{<'memory overflow'>} represented as a list. A
  5225. @cindex segmentation fault
  5226. segmentation fault is possible if this function is passed an invalid
  5227. pointer or dimension.
  5228. @end deftypefun
  5229. @node Related utility functions, , Two dimensional arrays, Type Conversions
  5230. @subsubsection Related utility functions
  5231. A small selection of additional functions that are likely to be of use
  5232. to developers concerned with matrix operations has been incorporated
  5233. into the API to save the trouble of reinventing them, although doing
  5234. so would be straightforward. They are described in this section
  5235. without further motivation.
  5236. @deftypefun void *avm_matrix_transposition (void *@var{matrix}, int @var{rows}, int @var{cols}, size_t @var{item_size})
  5237. This function takes the address of an arbitrary rectangular
  5238. @var{matrix} represented as a contiguous array (not a list) and
  5239. transposes it in place. That is, this function transforms
  5240. an @var{m} by @var{n} matrix to an @var{n} by @var{m} matrix
  5241. by exchanging the @var{i},@var{j}th element with the
  5242. @var{j},@var{i}th element for all values of @var{i} and @var{j}.
  5243. The numbers of rows and columns in the @var{matrix} are given by the
  5244. parameters @var{rows} and @var{cols}, respectively, and the size of
  5245. the entries in bytes is given by @var{item_size}.
  5246. The @var{matrix} is assumed to be in row major order, but this
  5247. function is applicable to matrices in column major order if the caller
  5248. @cindex column major order
  5249. passes the number of columns in @var{rows} and the number of rows in
  5250. @var{cols}.
  5251. Alternatively, this function can be seen as a conversion between the
  5252. row major and the column major representation of a matrix. An @var{m}
  5253. by @var{n} matrix in row major order will be transformed to the same
  5254. @var{m} by @var{n} matrix in column order, or from column order to row
  5255. order.
  5256. A notable feature of this function is that it allocates no memory so
  5257. there is no possibility of a memory overflow even for very large
  5258. matrices, unlike a naive implementation which would involve making a
  5259. temporary copy of the matrix. There is a possibility of a segmentation
  5260. @cindex segmentation fault
  5261. fault if invalid pointers or dimensions are given.
  5262. @end deftypefun
  5263. @deftypefun void *avm_matrix_reflection (int @var{upper_triangular}, void *@var{matrix}, int @var{n}, size_t @var{item_size})
  5264. This function takes a symmetric square @var{matrix} of dimension
  5265. @var{n} containing entries of @var{item_size} bytes each and fills in
  5266. the redundant entries.
  5267. If @var{upper_triangular} is non-zero, the
  5268. upper triangle of the @var{matrix} is copied to the lower triangle. If
  5269. @var{upper_triangular} is zero, the lower triangular entries are
  5270. copied to the upper triangle.
  5271. These conventions assume row major order. If the @var{matrix} is in
  5272. @cindex row major order
  5273. column major order, then the caller can either transpose it in place
  5274. @cindex column major order
  5275. before and after this function by @code{avm_matrix_transposition}, or
  5276. can complement the value of @var{upper_triangular}.
  5277. Note that this function may be unnecessary for @code{LAPACK} library
  5278. functions that ignore the redundant entries in a symmetric matrix,
  5279. because they can be left uninitialized, but it is included for the
  5280. sake of completeness.
  5281. @end deftypefun
  5282. @deftypefun list *avm_row_number_array (counter @var{m}, int *@var{fault})
  5283. A fast, memory efficient finite map from natural numbers to their list
  5284. representations can be obtained by using this function as an
  5285. alternative to @code{avm_natural} or @code{avm_recoverable_natural}
  5286. when repeated evaluations of numbers within a known range are
  5287. required (@ref{Simple Operations} and @ref{Recoverable Operations}).
  5288. Given a positive integer @var{m}, this function allocates and returns
  5289. an array of @var{m} lists whose @var{i}th entry is the list
  5290. representation of the number @var{i} as explained in
  5291. @ref{Representation of Numeric and Textual Data}.
  5292. An amount of memory proportional to @var{m} is used for the array and
  5293. its contents. If there is insufficient memory, a @code{NULL} value is
  5294. returned and the integer referenced by @var{fault} is set to a
  5295. non-zero value.
  5296. @end deftypefun
  5297. @deftypefun void avm_dispose_rows (counter @var{m}, list *@var{row_number})
  5298. This function reclaims an array @var{row_number} of size @var{m}
  5299. returned by @code{avm_row_number_array}, and its contents if any. A
  5300. @code{NULL} pointer is allowed as the @var{row_number} parameter and
  5301. will have no effect, but an uninitialized pointer will cause a
  5302. @cindex segmentation fault
  5303. segmentation fault.
  5304. @end deftypefun
  5305. @deftypefun void avm_initialize_matcon ();
  5306. This function initializes some static variables used by the functions
  5307. declared in @file{matcon.h} and should be called before any of them is
  5308. called or they might not perform according to specifications.
  5309. @end deftypefun
  5310. @deftypefun void avm_count_matcon ();
  5311. This function frees the static variables allocated by
  5312. @code{avm_initialize_matcon} and is used to verify the absence of
  5313. memory leaks. It should be called after the last call to any functions
  5314. in @file{matcon.h} but before @code{avm_count_lists} if the latter
  5315. is being used (@ref{Simple Operations}).
  5316. @end deftypefun
  5317. @node Comparison, Deconstruction Functions, Type Conversions, Lists
  5318. @subsection Comparison
  5319. The file @file{compare.h} contains a few function declarations
  5320. pertaining to the computation of the comparison predicate described in
  5321. @ref{Compare}. Some of the work is done by static functions in
  5322. @file{compare.c} that are not recommended entry points to the library.
  5323. @deftypefun void avm_initialize_compare ()
  5324. @cindex @code{compare}
  5325. This function should be called once before the first call to
  5326. @code{avm_comparison}, as it initializes some necessary internal data
  5327. structures.
  5328. @end deftypefun
  5329. @deftypefun void avm_count_compare ()
  5330. This function can be used to check for memory leaks, by detecting
  5331. unreclaimed storage at the end of a run. The data structures relevant to
  5332. comparison that could be reported as unreclaimed are known as ``decision''
  5333. nodes, but these should always be handled properly by the library
  5334. without intervention. If @code{avm_count_lists} is also being used, the
  5335. call to this function must precede it.
  5336. @end deftypefun
  5337. @deftypefun list avm_comparison (list @var{operand}, int *@var{fault})
  5338. This function takes a list operand representing a pair of trees and
  5339. returns a list representing the logical value of their equality. If the
  5340. operand is @code{NULL}, a message of invalid comparison is returned and
  5341. the @code{*@var{fault}} is set to a non-zero value. If the @code{head} of the
  5342. operand is unequal to the @code{tail}, a @code{NULL} value is
  5343. returned. If they are equal, a list is returned whose @code{head} and
  5344. @code{tail} are both @code{NULL}. The equality in question is structural
  5345. @cindex pointer equality
  5346. rather than pointer equality.
  5347. The list operand to this function may be modified by this function, but
  5348. not in a way that should make any difference to a client program. If two
  5349. lists are found to be equal, or if even two sublists are found to be
  5350. equal in the course of the comparison, one of them is deallocated and
  5351. made to point to the other. This action saves memory and may make
  5352. subsequent comparisons faster. However, it could disrupt client programs
  5353. @cindex pointers
  5354. that happen to be holding stale list pointers.
  5355. @cindex discontiguous field
  5356. As of @code{avram} version 0.6.0, a logical field called
  5357. @code{discontiguous} has been added to the @code{node} record type
  5358. declared in @code{lists.h}, which is checked by the comparison
  5359. function. If a list node has its @code{discontiguous} field set to a
  5360. non-zero value, and if it also has a non-null @code{value} field, then
  5361. it won't be deallocated in the course of comparison even if it is
  5362. found to be equal to something else. This feature can be used by
  5363. client modules to create lists in which value fields refer to data
  5364. structures that are meant to exist independently of them. See
  5365. @file{mpfr.c} for an example.
  5366. This function is likely to have better performance and memory usage than
  5367. a naive implementation of comparison, for the above reasons and also
  5368. because of optimizations pertaining to comparison of lists representing
  5369. characters. Moreover, it is not subject to stack overflow exceptions
  5370. @cindex recursion
  5371. because it is not written in a recursive style.
  5372. @end deftypefun
  5373. @deftypefun list avm_binary_comparison (list @var{left_operand}, list @var{right_operand}, int *@var{fault});
  5374. This function is the same as @code{avm_comparison} except that it
  5375. allows the left and right operands to be passed as separate lists
  5376. rather than taking them from the @code{head} and the @code{tail} of a
  5377. single list.
  5378. @end deftypefun
  5379. @node Deconstruction Functions, Indirection, Comparison, Lists
  5380. @subsection Deconstruction Functions
  5381. A fast native implementation of the deconstruction operation is provided
  5382. @cindex deconstruction
  5383. by the functions declared in @file{decons.h}.
  5384. @deftypefun void avm_initialize_decons ()
  5385. This should be called prior to the first call to
  5386. @code{avm_deconstruction}, so as to initialize some necessary internal
  5387. data structures. Results will be undefined if it is not.
  5388. @end deftypefun
  5389. @deftypefun void avm_count_decons ()
  5390. For ecologically sound memory management, this function should be called
  5391. at the end of a run to verify that there have been no leaks due to the
  5392. deconstruction functions, which there won't be unless the code in
  5393. @file{decons.c} has been ineptly modified. An error message to the
  5394. effect of unreclaimed ``points'' could be the result otherwise.
  5395. @end deftypefun
  5396. @deftypefun list avm_deconstruction (list @var{pointer}, list @var{operand}, int *@var{fault})
  5397. Deconstructions are performed by this function, as described in
  5398. @ref{Field}. In the @code{silly} program notation (@ref{A Simple Lisp
  5399. Like Language}), this function computes the value
  5400. of ([[@code{field}]] @code{@var{pointer}}) @code{@var{operand}}.
  5401. For example, using the fixed list @code{avm_join(NULL,NULL)} as the
  5402. @code{@var{pointer}} parameter will cause a copy of the operand itself
  5403. to be returned as the result. A @code{@var{pointer}} equal to
  5404. @code{avm_join(NULL,avm_join(NULL,NULL))} will cause a copy of
  5405. @code{operand->tail} to be returned, and so on. A @code{NULL}
  5406. @code{@var{pointer}} causes an internal error.
  5407. If the deconstruction is invalid, as in the case of the tail of an empty
  5408. list, the invalid deconstruction error message is returned as the
  5409. result, and the @code{*@var{fault}} parameter is set to a non-zero value.
  5410. The @code{*@var{fault}} parameter is also set to a non-zero value in the event
  5411. of a memory overflow, and the memory overflow message is returned.
  5412. @end deftypefun
  5413. @node Indirection, The Universal Function, Deconstruction Functions, Lists
  5414. @subsection Indirection
  5415. In some cases it is necessary to build a tree from the top down rather
  5416. @cindex pointers
  5417. than from the bottom up, when it is not known in advance what's on the
  5418. bottom. Although the @code{list} type is a pointer itself, these
  5419. situations call for a type of pointers to lists, which are declared as
  5420. the @code{branch} type in @file{branches.h}. For example, if @code{b} is
  5421. declared as a @code{branch} and @code{l} is declared as a @code{list},
  5422. it would be possible to write @code{b = &l}.
  5423. Facilities are also provided for maintaining queues of branches, which
  5424. @cindex queues
  5425. are declared as the @code{branch_queue} type. This type is a pointer to
  5426. a structure with two fields, @code{above} and @code{following}, where
  5427. @code{above} is a @code{branch} and @code{following} is a
  5428. @code{branch_queue}.
  5429. These functions are used internally elsewhere in the library and might
  5430. not be necessary for most client programs to use directly.
  5431. @deftypefun void avm_initialize_branches ()
  5432. This must be done once before any of the other branch related functions is
  5433. used, and creates some internal data structures. Results of the other
  5434. functions are undefined if this one isn't called first.
  5435. @end deftypefun
  5436. @deftypefun void avm_count_branches ()
  5437. This function can be used at the end of a run to detect unreclaimed
  5438. storage used for branches or branch queues. If any storage remains
  5439. unreclaimed, a message about unreclaimed branches is written to standard
  5440. error.
  5441. @end deftypefun
  5442. @deftypefun void avm_anticipate (branch_queue *@var{front}, branch_queue *@var{back}, branch @var{operand})
  5443. This function provides a simple queueing facility for
  5444. branches. Similarly to the case with @code{avm_enqueue}, @code{front}
  5445. and @code{back} should be initialized to @code{NULL} before the first
  5446. call. Each call to this function will enqueue one item to the back,
  5447. assuming enough memory is available, as the following example shows.
  5448. @example
  5449. front = NULL;
  5450. back = NULL;
  5451. l = avm_join(NULL,NULL);
  5452. anticipate(&front,&back,&(l->head));
  5453. anticipate(&front,&back,&(l->tail));
  5454. @end example
  5455. After the above code is executed, these postconditions will hold.
  5456. @example
  5457. front->above == &(l->head)
  5458. front->following->above == &(l->tail)
  5459. front->following == back
  5460. back->following == NULL
  5461. @end example
  5462. The name ``anticipate'' is used because ordinarily the queue contains
  5463. positions in a tree to be filled in later. As usual, only unshared trees should be
  5464. modified in place.
  5465. @end deftypefun
  5466. @deftypefun void avm_recoverable_anticipate (branch_queue *@var{front}, branch_queue *@var{back}, branch @var{operand}, int *@var{fault})
  5467. This function is similar to @code{avm_anticipate}, except that it will
  5468. not exit with an error message in the event of an overflow error, but
  5469. will simply set @code{*@var{fault}} to a non-zero value and return to the
  5470. caller. If an overflow occurs, nothing about the queue is changed.
  5471. @end deftypefun
  5472. @deftypefun void avm_enqueue_branch (branch_queue *@var{front}, branch_queue *@var{back}, int @var{received_bit})
  5473. A slightly higher level interface to the @code{avm_anticipate} function
  5474. is provided by this function, which is useful for building a tree from
  5475. @cindex trees
  5476. a string of input bits in a format similar to the one described in
  5477. @ref{Concrete Syntax}.
  5478. This function should be called the first time with @code{front} and
  5479. @code{back} having been initialized to represent a queue containing a
  5480. @cindex queues
  5481. single branch pointing to a list known to the caller. The list itself
  5482. need not be allocated or initialized. An easy way of doing so would be
  5483. the following.
  5484. @example
  5485. front = NULL;
  5486. back = NULL;
  5487. avm_anticipate(&front,&back,&my_list);
  5488. @end example
  5489. On each call to @code{avm_enqueue_branch}, the @code{@var{received_bit}}
  5490. parameter is examined. If it is zero, nothing will be added to the
  5491. queue, the list referenced by the front branch will be assigned
  5492. @code{NULL}, and the front branch will be removed from the queue. If
  5493. @code{@var{received_bit}} is a non-zero value, the list referenced by
  5494. the front branch will be assigned to point to a newly created unshared
  5495. list node, and two more branches will be appended to the queue. The
  5496. first branch to be appended will point to the head of the newly created
  5497. list node, and the second branch to be appended will point to the tail.
  5498. If the sequence of bits conforms to the required concrete syntax, this
  5499. function can be called for each of them in turn, and at the end of the
  5500. sequence, the queue will be empty and the list referenced by the initial
  5501. branch (i.e., @code{my_list}) will be the one specified by the bit
  5502. string. If the sequence of bits does not conform to the required
  5503. concrete syntax, the error can be detected insofar as the emptying of
  5504. the queue will not coincide exactly with the last bit.
  5505. The caller should check for the queue becoming prematurely empty due to
  5506. syntax errors, because no message is reported by
  5507. @code{avm_enqueue_branch} in that event, and subsequent attempts to
  5508. enqueue anything are ignored. However, in the event of a memory overflow,
  5509. an error message is reported and the process is terminated.
  5510. @end deftypefun
  5511. @deftypefun void avm_recoverable_enqueue_branch (branch_queue *@var{front}, branch_queue *@var{back}, int @var{received_bit}, int *@var{fault})
  5512. This function is similar to @code{avm_enqueue_branch} but will leave
  5513. error handling to the caller in the event of insufficient memory to
  5514. enqueue another branch. Instead of printing an error message and
  5515. exiting, it will dispose of the queue, set the @code{@var{fault}} flag
  5516. to a non-zero value, and return. Although the queue will be reclaimed,
  5517. the lists referenced by the branches in it will persist. The list nodes
  5518. themselves can be reclaimed by disposing of the list whose address was
  5519. stored originally in the front branch.
  5520. @end deftypefun
  5521. @deftypefun void avm_dispose_branch_queue (branch_queue @var{front})
  5522. This function deallocates a branch queue by chasing the @code{following}
  5523. fields in each one. It does nothing to the lists referenced by the
  5524. branches in the queue.
  5525. Rather than using @code{free} directly, client programs should use this
  5526. function for deallocating branch queues, because it allows better
  5527. performance by interacting with a local internal cache of free memory,
  5528. and because it performs necessary bookkeeping for
  5529. @code{avm_count_branches}.
  5530. @end deftypefun
  5531. @deftypefun void avm_dispose_branch (branch_queue @var{old})
  5532. This disposes of a single branch queue node rather than a whole queue.
  5533. Otherwise, the same comments as those above apply.
  5534. @end deftypefun
  5535. @node The Universal Function, , Indirection, Lists
  5536. @subsection The Universal Function
  5537. @cindex universal function
  5538. A function computing the result of the invisible operator used to
  5539. specify the virtual code semantics in @ref{Virtual Code Semantics}, is
  5540. easily available by way of a declaration in @file{apply.h}.
  5541. @deftypefun void avm_initialize_apply ()
  5542. This function should be called by the client program at least once prior
  5543. to the first call to @code{avm_apply} or
  5544. @code{avm_recoverable_apply}. It causes certain internal data structures
  5545. and error message texts to be initialized.
  5546. @end deftypefun
  5547. @deftypefun void avm_count_apply ()
  5548. This function should be used at the end of a run for the purpose of
  5549. detecting and reporting any unreclaimed storage associated with
  5550. functions in this section. If the function @code{avm_count_lists()} is
  5551. also being used, it should be called after this one.
  5552. @end deftypefun
  5553. @deftypefun list avm_apply (list @var{operator}, list @var{operand})
  5554. This is the function that evaluates the operator used to describe the
  5555. virtual code semantics. For example, the value of @code{@var{f} @var{x}}
  5556. can be obtained as the result returned by
  5557. @code{avm_apply(@var{f},@var{x})}.
  5558. Both parameters to this function are deallocated unconditionally and
  5559. should not be referenced again by the caller. If the parameters are
  5560. needed subsequently, then only copies of them should be passed to
  5561. @code{avm_apply} using @code{avm_copied}.
  5562. This function is not guaranteed to terminate, and may cause a memory
  5563. overflow error. In the event of an exceptional condition, the error
  5564. message is written to standard error and the program is halted. There is
  5565. no externally visible distinction between different levels of error
  5566. conditions.
  5567. @end deftypefun
  5568. @deftypefun list avm_recoverable_apply (list @var{operator}, list @var{operand}, int *@var{fault})
  5569. This function is similar to @code{avm_apply} but leaves the
  5570. responsibility of error handling with the caller. If any
  5571. overflow or exceptional condition occurs, the result returned is a list
  5572. representing the error message, and the @code{@var{fault}} flag is set to
  5573. a non-zero value. This behavior contrasts with that of @code{avm_apply},
  5574. which will display the message to standard error and kill the process.
  5575. @end deftypefun
  5576. @node Characters and Strings, File Manipulation, Lists, Library Reference
  5577. @section Characters and Strings
  5578. @cindex character strings
  5579. If a C program is to interact with a virtual code application by
  5580. exchanging text, it uses the representation for characters described in
  5581. @ref{Character Table}. This convention would be inconvenient without a
  5582. suitable API, so the functions in this section address the need. These
  5583. functions are declared in the header file @file{chrcodes.h}.
  5584. Some of these functions have two forms, with one of them having the
  5585. word @code{standard} as part of its name. The reason is to cope with
  5586. multiple character encodings. Versions of @code{avram} prior to 0.1.0
  5587. @cindex character encodings
  5588. @cindex multiple character encodings
  5589. used a different character encoding than the one documented in
  5590. @ref{Character Table}. The functions described in @ref{Version
  5591. Management} can be used to select backward compatible operation with
  5592. the older character encoding. The normal forms of the functions in
  5593. this section will use the older character set if a backward
  5594. compatibility mode is indicated, whereas the standard forms will use
  5595. the character encoding documented in @ref{Character Table} regardless.
  5596. Standard encodings should always be assumed for library and function
  5597. @cindex standard character encoding
  5598. names associated with the @code{library} combinator (@ref{Calling
  5599. existing library functions}), and for values of lists defined by
  5600. @code{avm_list_of_value} (@ref{Primitive types}), but version
  5601. dependent encodings should be used for all other purposes such as
  5602. error messages. Alternatively, the normal version dependent forms of
  5603. the functions below can be used safely in any case if backward
  5604. @cindex backward compatability
  5605. compatibility is not an issue. This distinction is viewed as a
  5606. transitional feature of the API that will be discontinued eventually
  5607. when support for the old character set is withdrawn and the
  5608. @code{standard} forms are be removed.
  5609. @deftypefun list avm_character_representation (int @var{character})
  5610. @end deftypefun
  5611. @deftypefun list avm_standard_character_representation (int @var{character})
  5612. This function takes an integer character code and returns a copy of
  5613. the list representing it, as per the table in @ref{Character
  5614. Table}. Because the copy is shared, no memory is allocated by this
  5615. function so there is no possibility of overflow. Nevertheless, it is
  5616. the responsibility of the caller dispose of the list when it is no
  5617. longer needed by @code{avm_dispose}, just as if the copy were not
  5618. shared (@ref{Simple Operations}). For performance reasons, this
  5619. function is implemented as a macro. If the argument is outside the
  5620. range of zero to 255, it is masked into that range.
  5621. @end deftypefun
  5622. @deftypefun int avm_character_code (list @var{operand})
  5623. @end deftypefun
  5624. @deftypefun int avm_standard_character_code (list @var{operand})
  5625. This function takes a list as an argument and returns the corresponding
  5626. character code, as per @ref{Character Table}. If the argument does not
  5627. represent any character, a value of @code{-1} is returned.
  5628. @end deftypefun
  5629. @deftypefun list avm_strung (char *@var{string})
  5630. @end deftypefun
  5631. @deftypefun list avm_standard_strung (char *@var{string})
  5632. This function takes a pointer to a null terminated character string and
  5633. returns the list obtained by translating each character into its list
  5634. representation and enqueuing them together. Memory needs to be allocated
  5635. for the result, and if there isn't enough available, an error message is
  5636. written to standard error and the process is terminated. This function
  5637. is useful to initialize lists from hard coded strings at the beginning
  5638. of a run, as in this example.
  5639. @example
  5640. hello_string = avm_strung("hello");
  5641. @end example
  5642. This form initializes a single string, but to initialize a one line
  5643. message suitable for writing to a file, it would have to be a list of
  5644. strings, as in this example.
  5645. @example
  5646. hello_message = avm_join(avm_strung("hello"),NULL);
  5647. @end example
  5648. The latter form is used internally by the library for initializing
  5649. most of the various error messages that can be returned by other functions.
  5650. @end deftypefun
  5651. @deftypefun list avm_recoverable_strung (char *@var{string}, int *@var{fault});
  5652. @end deftypefun
  5653. @deftypefun list avm_recoverable_standard_strung (char *@var{string}, int *@var{fault});
  5654. This function is like @code{avm_strung} except that if it runs out of memory
  5655. it sets the integer referenced by @var{fault} to a non-zero value and returns
  5656. instead of terminating the process.
  5657. @end deftypefun
  5658. @deftypefun char *avm_unstrung (list @var{string}, list *@var{message}, int *@var{fault})
  5659. @end deftypefun
  5660. @deftypefun char *avm_standard_unstrung (list @var{string}, list *@var{message}, int *@var{fault})
  5661. This function performs an inverse operation to
  5662. @code{avm_recoverable_strung}, taking a list representing a character
  5663. string to the character string in ASCII null terminated form as per
  5664. the standard C representation. Memory is allocated for the result by
  5665. this function which should be freed by the caller.
  5666. In the event of an exception, the integer referenced by @code{fault}
  5667. is assigned a non-zero value and an error message represented as a
  5668. list is assigned to the list referenced by @code{message}. The error
  5669. message should be reclaimed by the caller with @code{avm_dispose}
  5670. (@ref{Simple Operations} if it is non-empty. Possible error messages
  5671. are @code{<'memory overflow'>}, @code{<'counter overflow'>}, and
  5672. @code{<'invalid text format'>}.
  5673. @end deftypefun
  5674. @deftypefun list avm_scanned_list (char *@var{string})
  5675. An application that makes use of virtual code snippets or data that are
  5676. known at compile time can use this function to initialize them. The
  5677. argument is a string in the format described in @ref{Concrete Syntax},
  5678. and the result is the list representing it. For example, the program
  5679. discussed in @ref{Example Script} could be hard coded into a C program
  5680. by pasting the data from its virtual code file into an expression of
  5681. this form.
  5682. @example
  5683. cat_program = avm_scanned_list("sKYQNTP\\");
  5684. @end example
  5685. Note that the backslash character in the original data has to be
  5686. preceded by an extra backslash in the C source, because backslashes
  5687. usually mean something in C character constants.
  5688. The @code{avm_scanned_list} function needs to allocate memory. If there
  5689. isn't enough memory available, it writes a message to standard error and
  5690. causes the process to exit.
  5691. @end deftypefun
  5692. @deftypefun list avm_multiscanned (char **@var{strings})
  5693. Sometimes it may be useful to initialize very large lists from
  5694. strings, but some C compilers impose limitations on the maximum length
  5695. of a string constant, and the ISO standard for C requires only 512
  5696. bytes. This function serves a similar purpose to
  5697. @code{avm_scanned_list}, but allows the argument to be a pointer to a
  5698. null terminated array of strings instead of one long string, thereby
  5699. circumventing this limitation in the compiler.
  5700. @example
  5701. char *code[] = @{"sKYQ","NTP\\",NULL@};
  5702. ...
  5703. cat_program = avm_multiscanned(code);
  5704. @end example
  5705. If there is insufficient memory to allocate the list this function needs
  5706. to create, it causes an error message to be written to standard error,
  5707. and then kills the process.
  5708. @end deftypefun
  5709. @deftypefun char* avm_prompt (list @var{prompt_strings})
  5710. This function takes a list representing a list of character strings, and
  5711. returns its translation to a character string with the sequence 13 10
  5712. used as a separator. For example, given a tree of this form
  5713. @example
  5714. some_message = avm_join(
  5715. avm_strung("hay"),
  5716. avm_join(
  5717. avm_strung("you"),
  5718. NULL));
  5719. @end example
  5720. the result returned by @code{prompt_strings(some_message)} would be a
  5721. pointer to a null terminated character string equivalent to the C constant
  5722. @code{"hay\13\10you"}.
  5723. Error messages are printed and the process terminated in the event of
  5724. either a memory overflow or an invalid character representation.
  5725. This function is used by @code{avram} in the evaluation of interactive
  5726. @cindex interactive applications
  5727. virtual code applications, whose output has to be compared to the output
  5728. from a shell command in this format. The separator is chosen to be
  5729. compatible with the @code{expect} library.
  5730. @end deftypefun
  5731. @deftypefun char* avm_recoverable_prompt (list @var{prompt_strings}, list *@var{message}, int *@var{fault})
  5732. This function performs the same operation as @code{avm_prompt} but
  5733. allows the caller to handle exceptional conditions. If an exception
  5734. such as a memory overflow occurs, the integer referenced by
  5735. @code{fault} is assigned a non-zero value and a representation of the
  5736. error message as a list of strings is assigned to the list referenced
  5737. by @code{message}.
  5738. This function is used to by @code{avram} to evaluate the
  5739. @code{interact} combinator (@ref{Interaction combinator}), when
  5740. terminating in the event of an error would be inappropriate.
  5741. @end deftypefun
  5742. @deftypefun void avm_initialize_chrcodes ()
  5743. This function has to be called before any of the other character
  5744. conversion functions in this section, or else their results are
  5745. undefined. It performs the initialization of various internal data
  5746. structures.
  5747. @end deftypefun
  5748. @deftypefun void avm_count_chrcodes ()
  5749. This function can be called at the end of a run, after the last call to
  5750. any of the other functions in this section, but before
  5751. @code{avm_count_lists} if that function is also being used. The purpose
  5752. of this function is to detect and report memory leaks. If any memory
  5753. associated with any of these functions has not been reclaimed by the
  5754. client program, a message giving the number of unreclaimed lists will be
  5755. written to standard error.
  5756. @end deftypefun
  5757. @node File Manipulation, Invocation, Characters and Strings, Library Reference
  5758. @section File Manipulation
  5759. The functions described in this section provide an interface between
  5760. virtual code applications and the host file system by converting
  5761. between files or file names and their representations as lists. These
  5762. conversions are necessary when passing a file to a virtual code
  5763. application, or when writing a file received in the result of one.
  5764. @menu
  5765. * File Names::
  5766. * Raw Files::
  5767. * Formatted Input::
  5768. * Formatted Output::
  5769. @end menu
  5770. @node File Names, Raw Files, File Manipulation, File Manipulation
  5771. @subsection File Names
  5772. A standard representation is used by virtual code applications for the
  5773. @cindex file names
  5774. path names of files, following the description in @ref{Input Data
  5775. Structure}. The functions and constants declared in @code{fnames.h}
  5776. provide an API for operating on file names in this form.
  5777. @deftypefun list avm_path_representation (char *@var{path})
  5778. If a C program is to invoke a virtual code application and pass a path
  5779. name to it as a parameter, this function can be used to generate the
  5780. appropriate representation from a given character string.
  5781. @example
  5782. conf_path = avm_path_representation("/etc/resolve.conf");
  5783. @end example
  5784. In this example, @code{conf_path} is a @code{list}. For potentially
  5785. better portability, a C program can use the character constant
  5786. @code{avm_path_separator_character} in place of the slashes in hard
  5787. coded path names.
  5788. Other useful constants are @code{avm_current_directory_prefix} as a
  5789. @cindex @code{avm_path_separator_character}
  5790. @cindex @code{avm_path_separator}
  5791. @cindex @code{avm_current_directory_prefix}
  5792. @cindex @code{avm_parent_directory_prefix}
  5793. @cindex @code{avm_root_directory_prefix}
  5794. portable replacement for @code{"./"}, as well as
  5795. @code{avm_parent_directory_prefix} instead of @code{"../"}. There is
  5796. also @code{avm_root_directory_prefix} for @code{"/"}. These three
  5797. constants are null terminated strings, unlike
  5798. @code{avm_path_separator_character}, which is a character.
  5799. If a @code{NULL} pointer is passed as the @code{@var{path}}, a
  5800. @code{NULL} list is returned, which is the path representation for
  5801. standard input or standard output. If the address of an empty string is
  5802. passed to this function as the @code{@var{path}}, the list of the empty
  5803. string will be returned, which is the path representation for the root
  5804. directory. Trailing path separators are ignored, so @code{"/"} is the
  5805. same as the empty string.
  5806. Some memory needs to be allocated for the result of this function. If
  5807. the memory is not available, an error message is written to standard
  5808. error and the process is terminated.
  5809. @end deftypefun
  5810. @deftypefun list avm_date_representation (char *@var{path})
  5811. This function is essentially a wrapper around the standard
  5812. @code{ctime_r} function that not only gets the time stamp for a file at
  5813. a given path, but transforms it to a list representation according to
  5814. @ref{Character Table}. It needs to allocate memory for the result and
  5815. will cause the program to exit with an error message if there is not
  5816. enough memory available.
  5817. The time stamp will usually be in a format like @code{Sun Mar 4 10:56:40
  5818. GMT 2001}. If for some reason the time stamp can not be obtained, the
  5819. @cindex @code{unknown date}
  5820. result will be a representation of the string @code{unknown date}.
  5821. @end deftypefun
  5822. @deftypefun char* avm_path_name (list @var{path})
  5823. This function is the inverse of @code{avm_path_representation}, in that
  5824. it takes a list representing a path to the path name expressed as a character
  5825. string. This function can be used in C programs that invoke virtual code
  5826. applications returning paths as part of their results, so that the C
  5827. program can get the path into a character string in order to open the file.
  5828. If the @code{@var{path}} parameter is @code{NULL}, a @code{NULL} pointer
  5829. is returned as the result. The calling program should check for a
  5830. @cindex standard input
  5831. @cindex standard output
  5832. @code{NULL} result and interpret it as the path to standard input or
  5833. standard output.
  5834. The memory needed for the character string whose address is returned is
  5835. allocated by this function if possible. The given @code{@var{path}} is
  5836. not required to be consistent with the host file system, but it is
  5837. required to consist of representations of non-null printable characters
  5838. or spaces as lists per @ref{Character Table}. In the event of any error
  5839. or overflow, control does not return to the caller, but an error message
  5840. is printed and the program is aborted. The possible error messages from
  5841. this function are the following.
  5842. @cindex @code{counter overflow}
  5843. @cindex @code{memory overflow}
  5844. @cindex @code{null character in file name}
  5845. @cindex @code{bad character in file name}
  5846. @cindex @code{invalid file name}
  5847. @itemize @bullet
  5848. @item @code{@var{program-name}: counter overflow (code @var{nn})}
  5849. @item @code{@var{program-name}: memory overflow (code @var{nn})}
  5850. @item @code{@var{program-name}: null character in file name}
  5851. @item @code{@var{program-name}: bad character in file name}
  5852. @item @code{@var{program-name}: invalid file name (code @var{nn})}
  5853. @end itemize
  5854. @end deftypefun
  5855. @deftypefun void avm_initialize_fnames ()
  5856. A few housekeeping operations relevant to internal data structures are
  5857. performed by this function, making it necessary to be called by the
  5858. client program prior to using any of the other ones.
  5859. @end deftypefun
  5860. @deftypefun void avm_count_fnames ()
  5861. This function can be used after the the last call to any of the other
  5862. functions in this section during a run, and it will detect memory leaks
  5863. that may be attributable to code in these functions or misuse
  5864. thereof. If any unreclaimed storage remains when this function is
  5865. called, a warning message will be written to standard error. If the
  5866. function @code{avm_count_lists} is also being used by the client, it
  5867. should be called after this one.
  5868. @end deftypefun
  5869. @node Raw Files, Formatted Input, File Names, File Manipulation
  5870. @subsection Raw Files
  5871. Some low level operations involving lists and data files are provided by
  5872. these functions, which are declared in the header file @file{rawio.h}.
  5873. @deftypefun list avm_received_list (FILE *@var{object}, char *@var{filename})
  5874. This function is a convenient way of transferring data directly from a
  5875. raw format file into a list in memory. It might typically be used to
  5876. load the virtual code for an application that has been written to a file
  5877. by a compiler.
  5878. @table @code
  5879. @item @var{object}
  5880. is the address of a file which should
  5881. already be open for reading before this function is called, and will be
  5882. read from its current position.
  5883. @item @var{filename}
  5884. should be set by the caller to the address of a null terminated string
  5885. containing the name of the file, but is not used unless it needs to be
  5886. printed as part of an error message. If it is a null pointer, standard
  5887. input is assumed.
  5888. @end table
  5889. The result returned is a list containing data read from the file.
  5890. The file format is described in @ref{File Format}. The preamble section
  5891. of the file, if any, is ignored. If the file ends prematurely or
  5892. otherwise conflicts with the format, the program is aborted with a
  5893. message of
  5894. @cindex @code{invalid raw file format}
  5895. @display
  5896. @code{@var{program-name}: invalid raw file format in @var{filename}}
  5897. @end display
  5898. written to standard error. The program will also be aborted by this
  5899. function in the event of a memory overflow.
  5900. The file is left open when this function returns, and could therefore be
  5901. used to store other data after the end of the list. The end of a list is
  5902. detected automatically by this function, and it reads no further,
  5903. leaving the file position on the next character, if any.
  5904. @end deftypefun
  5905. @deftypefun void avm_send_list (FILE *@var{repository}, list @var{operand}, char *@var{filename})
  5906. This function can be used to transfer data from a list in memory to a
  5907. file, essentially by implementing the printing algorithm described in
  5908. @ref{Bit String Encoding}.
  5909. @table @code
  5910. @item @var{repository}
  5911. is the address of a file already open for writing, to which the data are
  5912. written starting from the current position.
  5913. @item @var{operand}
  5914. is the list containing the data to be written
  5915. @item @var{filename}
  5916. is the address of a null terminated string containing the name of the
  5917. file that will be reported in an error message if necessary.
  5918. @end table
  5919. No preamble section is written by this function, but one could be
  5920. @cindex preamble
  5921. written to the file by the caller prior to calling it. Error messages
  5922. are possible either because of i/o errors or because of insufficient
  5923. memory. I/o errors are not fatal and will result only in a warning
  5924. message being printed to standard error, but a memory overflow will
  5925. cause the process to abort. An i/o error message reported by this
  5926. function would be of the form
  5927. @cindex @code{can't write}
  5928. @display
  5929. @code{@var{program-name}: can't write to @var{filename}}
  5930. @end display
  5931. followed by the diagnostic obtained from the standard @code{strerror}
  5932. @cindex @code{strerror}
  5933. function if it exists on the host platform. The file is left open when
  5934. this function returns.
  5935. @end deftypefun
  5936. @deftypefun void avm_initialize_rawio ()
  5937. This function initializes some necessary data structures for the
  5938. functions in this section, and should be called prior to them at the
  5939. beginning of a run.
  5940. @end deftypefun
  5941. @deftypefun void avm_count_rawio ()
  5942. This function does nothing in the present version of the library, but
  5943. should be called after the last call to all of the other functions in
  5944. this section in order to maintain compatibility with future versions of
  5945. the library. Future versions may decide to use this function to do some
  5946. cleaning up of local data structures.
  5947. @end deftypefun
  5948. @node Formatted Input, Formatted Output, Raw Files, File Manipulation
  5949. @subsection Formatted Input
  5950. Some functions relating to the input of text files or data files with
  5951. preambles are declared in the header file @file{formin.h}. The usage of
  5952. these functions is as follows.
  5953. @deftypefun list avm_preamble_and_contents (FILE *@var{source}, char *@var{filename})
  5954. This function loads a file of either text or data format into memory.
  5955. @table @code
  5956. @item @var{source}
  5957. should be initialized by the caller as the address of a file
  5958. already open for reading that will be read from its current position.
  5959. @item @var{filename}
  5960. should be set by the caller to the address of a null terminated
  5961. character string giving the name of the file that will be used if an i/o
  5962. error message needs to be written about it. If it is a @code{NULL}
  5963. pointer, standard input is assumed.
  5964. @end table
  5965. The result returned by the function will be a list whose @code{head}
  5966. @cindex preamble
  5967. represents the preamble of the file and whose @code{tail} represents the
  5968. contents. As a side effect, the input file will be closed, unless the
  5969. @code{@var{filename}} parameter is @code{NULL}.
  5970. If the file conforms to the format described in @ref{File Format}, the
  5971. preamble is a list of character strings. In the result returned by the
  5972. function, the @code{head} field will be a list with one item for each
  5973. line in the file, and each item will be a list of character
  5974. representations as in @ref{Character Table}, but with the leading hashes
  5975. stripped. The @code{tail} will be the list specified by remainder of the
  5976. file according to @ref{Concrete Syntax}. If the file has an empty
  5977. preamble but is nevertheless a data file, the @code{head} will be a list
  5978. whose @code{head} and @code{tail} are both @code{NULL}.
  5979. If the file does not conform to the format in @ref{File Format}, then
  5980. the @code{head} of the result will be @code{NULL}, and the @code{tail}
  5981. will be a list of lists of character representations, with one for each
  5982. line.
  5983. Whether or not the file conforms to the format is determined on the fly,
  5984. so this function is useful for situations in which the format is not
  5985. known in advance. The conventions regarding the preamble and contents
  5986. maintained by this function are the same as those used by virtual code
  5987. applications as described in @ref{Standard Output Representation} and
  5988. @ref{Input Data Structure}.
  5989. The characters used for line breaks are not explicitly represented in
  5990. @cindex line breaks
  5991. the result. Depending on the host system, line breaks in text files may
  5992. be represented either by the character code 10, or by the sequence 13
  5993. 10. However, in order for the library to deal with binary files in a
  5994. portable way, a line break always corresponds to a 10 as far as this
  5995. function is concerned regardless of the host, and a 13 is treated like
  5996. any other character. Hence, if this function were used on binary files
  5997. that happened to have some 10s in them, the exact contents of a
  5998. file could be reconstructed easily by appending a 10 to all but the last
  5999. line and flattening the list.
  6000. A considerable amount of memory may need to be allocated by this
  6001. function in order to store the file as a list. If not enough memory is
  6002. available, the function prints an error message to standard error and
  6003. aborts rather than returning to the caller. However, i/o errors are not
  6004. fatal, and will cause the function to print a warning but attempt to
  6005. continue.
  6006. @end deftypefun
  6007. @deftypefun list avm_load (FILE *@var{source}, char *@var{filename}, int @var{raw})
  6008. Similarly to @code{avm_preamble_and_contents}, this function also loads
  6009. a file into memory, but the format is specified in advance.
  6010. @table @code
  6011. @item @var{source}
  6012. should be set by the caller to the address of an already open file for
  6013. reading, which will be read from its current position.
  6014. @item @var{filename}
  6015. should be initialized by the caller as a pointer to a null terminated
  6016. string containing the name of the file that will be reported to the user
  6017. in the event of an error reading from it. If it is a @code{NULL}
  6018. pointer, standard input is assumed.
  6019. @item @var{raw}
  6020. is set to a non-zero value by the caller to indicate that the file is
  6021. expected to conform to the format in @ref{File Format}. If the file is
  6022. an ordinary text file, then it should be set to zero.
  6023. @end table
  6024. In the case of a data file, which is when @code{@var{raw}} is non-zero,
  6025. the result returned by this function will be a list representing the
  6026. data section of the file and ignoring the preamble. In the case of a
  6027. text file, the result will be a list of lists of character
  6028. representations as per @ref{Character Table}, with one such list for
  6029. each line in the file. Similar comments about line breaks to those
  6030. mentioned under @code{avm_preamble_and_contents} are applicable.
  6031. As a side effect of this function, the @code{@var{source}} file will be
  6032. closed, unless the @code{@var{filename}} is a @code{NULL} pointer.
  6033. This function is useful when the type of file is known in advance. If a
  6034. data file is indicated by the @code{@var{raw}} parameter but the format
  6035. is incorrect, an error message is reported and the process
  6036. terminates. The error message will be of the form
  6037. @display
  6038. @code{@var{program-name}: invalid raw file format in @var{filename}}
  6039. @end display
  6040. Alternatively, if a text file is indicated by the
  6041. @cindex @code{invalid raw file format}
  6042. @code{@var{raw}} parameter, then no attempt is made to test whether it
  6043. could be interpreted as data, even if it could be. This behavior differs
  6044. from that of @code{avm_preamble_and_contents}, where a bad data
  6045. file format causes the file to be treated as text, and a valid data file
  6046. format, even in a ``text'' file, causes it to be treated as data.
  6047. Memory requirements for this function are significant and will cause the
  6048. process to abort with an error message in the event of insufficient free
  6049. memory. Messages pertaining to i/o errors are also possible and are not
  6050. fatal.
  6051. @end deftypefun
  6052. @deftypefun void avm_initialize_formin ()
  6053. This function should be called before either of the other functions in
  6054. this section is called, as it initializes some necessary static data
  6055. structures. Results of the other functions are undefined if this one is
  6056. not called first.
  6057. @end deftypefun
  6058. @deftypefun void avm_count_formin ()
  6059. This function should be called after the last call to any of the other
  6060. functions in this section, as it is necessary for cleaning up and
  6061. reclaiming some internal data. If any storage remains unreclaimed due to
  6062. memory leaks in these functions or to misuse of them, a warning message
  6063. is written to standard error. If the function @code{avm_count_lists} is
  6064. also being used by the client program, it should be called after this
  6065. one.
  6066. @end deftypefun
  6067. @node Formatted Output, , Formatted Input, File Manipulation
  6068. @subsection Formatted Output
  6069. The following functions pertaining to the output of text files or data files with
  6070. @cindex preamble
  6071. preambles are declared in the header file @file{formout.h}.
  6072. @deftypefun void avm_output (FILE *@var{repository}, char *@var{filename}, list @var{preamble}, list @var{contents}, int @var{trace_mode})
  6073. This function writes a either a text file or a data file in the format
  6074. described in @ref{File Format}. The parameters have these
  6075. interpretations.
  6076. @table @code
  6077. @item @var{repository}
  6078. is the address of a file opened for writing by the caller, that will be
  6079. written from its current position.
  6080. @item @var{filename}
  6081. is the address of a null terminated character string set
  6082. by the caller to be the name of the file that will
  6083. be reported to the user in the event of an i/o error.
  6084. @item @var{preamble}
  6085. is @code{NULL} in the case of a text file, but a list of character string
  6086. representations as per @ref{Character Table}, in the case of a data
  6087. file. If a data file has is to be written with an empty preamble, then
  6088. this list should have a @code{NULL} @code{head} and a @code{NULL}
  6089. @code{tail}.
  6090. @item @var{contents}
  6091. is either a list of character string representations in the case of a
  6092. text file, or is an unconstrained list in the case of a data file.
  6093. @item @var{trace_mode}
  6094. may be set to a non-zero value by the caller to request that everything
  6095. written to a text file should be echoed to standard output. It is
  6096. ignored in the case of a data file.
  6097. @end table
  6098. The effect of calling this function is to write the preamble and
  6099. contents to the file in the format indicated by the preamble. The file
  6100. is left open when this function returns.
  6101. Line breaks are always written as character code 10, not as 13 10,
  6102. @cindex line breaks
  6103. regardless of the convention on the host system, so that files written
  6104. by this function can be reliably read by other functions in the library.
  6105. Leading hashes are automatically added to the beginning of the lines in
  6106. the preamble, except where they are unnecessary due to a continuation
  6107. character on the previous line. This action enforces consistency with the
  6108. file format, ensuring that anything written as a data file can be read
  6109. back as one. The hashes are stripped automatically when the file is
  6110. read by @code{avm_preamble_and_contents}.
  6111. Another feature of this function is that it will mark any output file as
  6112. executable if it is a data format file with a prelude whose first
  6113. @cindex executable files
  6114. character in the first line is an exclamation point. This feature makes
  6115. it easier for a compiler implemented in virtual code to generate
  6116. executable shell scripts directly.
  6117. A fatal error is reported if any of the data required
  6118. to be a character representation is not listed in the @ref{Character
  6119. Table}. A fatal error can also be caused by a memory overflow. Possible
  6120. error messages are the following.
  6121. @cindex @code{invalid output preamble format}
  6122. @cindex @code{invalid text format}
  6123. @cindex @code{can't write}
  6124. @itemize @bullet
  6125. @item @code{@var{program-name}: invalid output preamble format}
  6126. @item @code{@var{program-name}: invalid text format}
  6127. @item @code{@var{program-name}: can't write to @var{filename}}
  6128. @end itemize
  6129. @cindex @code{strerror}
  6130. In the last case, the error message will be followed by an explanation
  6131. furnished by the standard @code{strerror} function if available.
  6132. @end deftypefun
  6133. @deftypefun void avm_output_as_directed (list @var{data}, int @var{ask_to_overwrite_mode}, int @var{verbose_mode})
  6134. This function writes an ensemble of files at specified paths in either
  6135. text or data format, optionally interacting with the user through
  6136. standard input and output. The parameters
  6137. have these interpretations.
  6138. @table @code
  6139. @item @var{data}
  6140. is a list in which each item specifies a file to be
  6141. written.
  6142. @item @var{ask_to_overwrite_mode}
  6143. may be set to a non-zero value by the calling program in order to
  6144. have this function ask the user for permission to overwrite existing files.
  6145. @item @var{verbose_mode}
  6146. may be set to a non-zero value by the calling program to have this
  6147. function print to standard output a list of the names of the files it
  6148. writes.
  6149. @end table
  6150. A high level interface between virtual code applications and the file
  6151. system is provided by this function. The @code{@var{data}} parameter
  6152. format is compatible with the the data structure returned by an
  6153. application complying with the conventions in @ref{Output From
  6154. Non-interactive Applications}.
  6155. Each item in the @code{@var{data}} list should be a non-empty list whose
  6156. @code{head} and @code{tail} are also non-empty. The fields in each item have
  6157. the following relevance to the file it specifies.
  6158. @itemize @bullet
  6159. @item The @code{head} of the @code{head} is @code{NULL} if the file is
  6160. to be opened for appending, and non-@code{NULL} if it is to be
  6161. overwritten.
  6162. @item The @code{tail} of the @code{head} represents a path as a list of
  6163. character string representations, in a form suitable as an argument to
  6164. @code{avm_path_name}.
  6165. @item The @code{head} of the @code{tail} represents the preamble of the
  6166. file, as either @code{NULL} for a text file or a non-empty list of
  6167. character string representations for a data file.
  6168. @item The @code{tail} of the @code{tail} represents the contents of the
  6169. file, either as a list of character string representations for a text
  6170. file or as a list in an unconstrained format for a data file.
  6171. @end itemize
  6172. For each item in the list, the function performs the following steps.
  6173. @enumerate
  6174. @item It decides whether to open a file
  6175. for overwriting or appending based on the @code{head} of the
  6176. @code{head}.
  6177. @item It uses the @code{tail} of the @code{head} to find out the
  6178. file name from @code{avm_path_name}, in order to open it.
  6179. @item If the
  6180. @code{@var{ask_to_overwrite_mode}} flag is set and the file is found to
  6181. exist already, the function will print one of the following messages to
  6182. standard output, depending on whether the file is to be overwritten or
  6183. appended.
  6184. @itemize @bullet
  6185. @item @code{@var{program-name}: overwrite @var{filename}? (y/n)}
  6186. @item @code{@var{program-name}: append to @var{filename}? (y/n)}
  6187. @end itemize
  6188. It will then insist on either @kbd{y} or @kbd{n} as an answer before continuing.
  6189. @item If the @code{@var{ask_to_overwrite}} flag has not been set, or the file did
  6190. not previously exist, or the answer of @kbd{y} was given, the preamble
  6191. and contents of the file are then written with @code{avm_output}.
  6192. @item If permission to write or append was denied, one of the following
  6193. messages is reported to standard output, and the data that were to be
  6194. written are lost.
  6195. @cindex @code{not writing} file name
  6196. @cindex @code{writing} file name
  6197. @itemize @bullet
  6198. @item @code{@var{program-name}: not writing @var{filename}}
  6199. @item @code{@var{program-name}: not appending @var{filename}}
  6200. @end itemize
  6201. @item If permission was granted to write or append to the file or the @code{@var{verbose_mode}} flag is
  6202. set, one of the messages
  6203. @itemize @bullet
  6204. @item @code{@var{program-name}: writing @var{filename}}
  6205. @item @code{@var{program-name}: appending @var{filename}}
  6206. @end itemize
  6207. is sent to standard output.
  6208. @end enumerate
  6209. @cindex standard output
  6210. If any files are to be written to standard output, which would be
  6211. indicated by a @code{NULL} path, they are not written until all other
  6212. files in the list are written. This feature is in the interest of
  6213. @cindex security
  6214. security, as it makes it more difficult for malicious or inept virtual
  6215. code to alter the appearance of the console through standard output until after
  6216. the interactive dialogue has taken place. Permission is not solicited
  6217. for writing to standard output, and it will not be closed.
  6218. Any of the fatal errors or i/o errors possible with @code{avm_output} or
  6219. @code{avm_path_name} are also possible with this function, as well as
  6220. the following additional ones.
  6221. @cindex @code{invalid file specification}
  6222. @cindex @code{can't close}
  6223. @cindex @code{can't write}
  6224. @itemize @bullet
  6225. @item @code{@var{program-name}: invalid file specification}
  6226. @item @code{@var{program-name}: can't close @var{filename}}
  6227. @item @code{@var{program-name}: can't write @var{filename}}
  6228. @end itemize
  6229. The last two are non-fatal i/o errors that will be accompanied by an
  6230. @cindex @code{strerror}
  6231. explanation from the @code{strerror} function if the host supports
  6232. it. The other message is the result of a badly formatted
  6233. @code{@var{data}} parameter.
  6234. @end deftypefun
  6235. @deftypefun void avm_put_bytes (list @var{bytes})
  6236. This function takes a list of character representations, converts them
  6237. to characters, and sends them to standard output. There is no chance of
  6238. a memory overflow, but the following other errors are possible.
  6239. @cindex @code{invalid text format}
  6240. @cindex @code{can't write}
  6241. @itemize @bullet
  6242. @item @code{@var{program-name}: invalid text format (code @var{nn})}
  6243. @item @code{@var{program-name}: can't write to standard output}
  6244. @end itemize
  6245. The latter is non-fatal, but the former causes the program to abort.
  6246. It is caused when any member of the list @code{@var{bytes}} is not a
  6247. character representation appearing in @ref{Character Table}.
  6248. @end deftypefun
  6249. @deftypefun void avm_initialize_formout ()
  6250. This function initializes some data structures used locally by the other
  6251. functions in this section, and should be called at the beginning of a
  6252. run before any of them is called.
  6253. @end deftypefun
  6254. @deftypefun void avm_count_formout ()
  6255. This function doesn't do anything in the current version of the library,
  6256. but should be called after the last call to any of the other functions
  6257. in this section. Future versions of the library might use this function
  6258. for cleaning up some internal data structures, and client programs that
  6259. call it will maintain compatibility with them.
  6260. @end deftypefun
  6261. @node Invocation, Version Management, File Manipulation, Library Reference
  6262. @section Invocation
  6263. The functions documented in this section can be used to incorporate the
  6264. capabilities of a virtual machine emulator into other C programs with
  6265. a minimal concern for the details of the required data structures
  6266. and virtual code invocation conventions.
  6267. @menu
  6268. * Command Line Parsing::
  6269. * Execution Modes::
  6270. @end menu
  6271. @node Command Line Parsing, Execution Modes, Invocation, Invocation
  6272. @subsection Command Line Parsing
  6273. @cindex command line
  6274. A couple of functions declared in @file{cmdline.h} can be used to do all
  6275. the necessary parsing of command lines and environment variables needed
  6276. by virtual code applications.
  6277. @deftypefun list avm_default_command_line (int @var{argc}, char *@var{argv}[], int @var{index}, char *@var{extension}, char *@var{paths}, int @var{default_to_stdin_mode}, int @var{force_text_input_mode}, int *@var{file_ordinal})
  6278. The purpose of this function is to build most of the data structure used
  6279. by parameter mode applications, as described in @ref{Input Data
  6280. Structure}, by parsing the command line according to @ref{Command Line
  6281. Syntax}. The parameters have these interpretations.
  6282. @table @code
  6283. @item @var{argc}
  6284. is the number elements in the array referenced by @code{@var{argv}}
  6285. @item @var{argv}
  6286. is the address of an array of pointers to null terminated character strings
  6287. holding command line arguments
  6288. @item @var{index}
  6289. is the position of the first element of @code{@var{argv}} to be
  6290. considered. Those preceding it are ignored.
  6291. @item @var{extension}
  6292. is the address of a string that will be appended to input file names
  6293. given in @code{@var{argv}} in an effort to find the associated files
  6294. @item @var{paths}
  6295. is the address of a null terminated character string containing a colon
  6296. separated list of directory names that will be searched for input files
  6297. @item @var{default_to_stdin_mode}
  6298. is set to a non-zero value by the caller if the contents of standard
  6299. input should be read in the absence of input files
  6300. @item @var{force_text_input_mode}
  6301. is set to a non-zero value by the caller to indicate that input files
  6302. should be read as text, using @code{avm_load} (rather than
  6303. @code{avm_preamble_and_contents}, which would allow them to be either
  6304. text or data). The @code{@var{preamble}} field of the returned file
  6305. specifications will always be empty when this flag is set.
  6306. @item @var{file_ordinal}
  6307. is set to a pointer to an integer by the caller if only one file is to
  6308. be loaded during each call. The value of the integer indicates the which
  6309. one it will be.
  6310. @end table
  6311. The result returned by this function is a list whose @code{head} is a list
  6312. of file specifications and whose @code{tail} is a list of command line options
  6313. intended for input to a virtual code application.
  6314. The list of file specifications returned in the @code{head} of the
  6315. result follows the same conventions as the @code{@var{data}} parameter
  6316. to the function @code{avm_output_as_directed}, except that the
  6317. @code{head} of the @code{head} of each item is a list representing the
  6318. time stamp of the file as given by @code{avm_date_representation}. If
  6319. the file is standard input, then it holds the current system date and
  6320. time.
  6321. If the @code{@var{file_ordinal}} parameter is @code{NULL}, then all
  6322. files on the command line are loaded, but if it points to an integer
  6323. @var{n}, then only the @var{n}th file is loaded, and @var{n} is
  6324. incremented. If there is no @var{n}th file, a @code{NULL} value is
  6325. returned as the entire result of the function. For a series of calls,
  6326. the integer should be initialized to zero by the caller before the first
  6327. call.
  6328. If standard input is indicated as one of the files on the command line
  6329. (by a dash), then it is also loaded regardless of the
  6330. @code{@var{file_ordinal}}, but a cached copy of it is used on subsequent
  6331. calls after the first, so that the function does not actually attempt to
  6332. reread it. If standard input is to be loaded, it must be finite for this
  6333. function to work properly.
  6334. The search strategy for files is described in @ref{Environment}, and
  6335. makes use of the @code{@var{extension}} and @code{@var{paths}} parameters.
  6336. In the list of command line options returned in the @code{tail} of the
  6337. result, each item is a list with a non-empty @code{head} and
  6338. @code{tail}, and is interpreted as follows.
  6339. @itemize @bullet
  6340. @item The @code{head} of the @code{head} is a list representing a
  6341. natural number, as given by @code{avm_natural}, indicating the position
  6342. of the option on the command line relative to the initial value of the
  6343. @code{@var{index}} parameter.
  6344. @item The @code{tail} of the @code{head} is a list which is @code{NULL}
  6345. in the case of a ``short form'' option, written with a single
  6346. dash on the command line, but is a list whose @code{head} and
  6347. @code{tail} are @code{NULL} in the case of a ``long form'' option,
  6348. written with two dashes.
  6349. @item The @code{head} of the @code{tail} is a list representing a
  6350. character string for the keyword of an option, for example @kbd{foo} in
  6351. the case of an option written @kbd{--foo=bar,baz}.
  6352. @item The @code{tail} of the @code{tail} is a list of lists representing
  6353. character strings, with one item for each parameter associated with the
  6354. option, for example, @kbd{bar} and @kbd{baz}.
  6355. @end itemize
  6356. If multiple calls to the function are made with differing values of
  6357. @code{*@var{file_ordinal}} but other parameters unchanged, the same list of
  6358. options will be returned each time, except insofar as the position
  6359. numbers in the @code{head} of the @code{head} of each item are adjusted
  6360. as explained in @ref{Input for Mapped Applications}.
  6361. Any of the i/o errors or fatal errors associated with other file
  6362. input operations are possible with this function as well. This non-fatal
  6363. warning message is also possible.
  6364. @cindex @code{search paths not supported}
  6365. @display
  6366. @code{@var{program-name}: warning: search paths not supported}
  6367. @end display
  6368. This error occurs if the library has been built on a platform that
  6369. @cindex @file{argz.h}
  6370. doesn't have the @file{argz.h} header file and the @code{@var{paths}}
  6371. parameter is non-@code{NULL}.
  6372. @end deftypefun
  6373. @deftypefun list avm_environment (char *@var{env}[])
  6374. @cindex environment
  6375. This function takes the address of a null terminated array of pointers
  6376. to null terminated character strings of the form
  6377. @code{"variable=value"}. The result returned is a list of lists, with
  6378. one item for each element of the array. The @code{head} of each item is
  6379. a representation of the left side of the corresponding string, and the
  6380. @code{tail} is a representation of the right.
  6381. This function is therefore useful along with
  6382. @code{avm_default_command_line} for building the remainder of the data
  6383. structure described in @ref{Parameter Mode Interface}. For example, a
  6384. virtual machine emulator for non-interactive parameter mode applications
  6385. with no bells and whistles could have the following form.
  6386. @example
  6387. int
  6388. main(argc,argv,env)
  6389. @dots{}
  6390. @{
  6391. FILE *virtual_code_file;
  6392. @dots{}
  6393. avm_initialize_lists();
  6394. avm_initialize_apply();
  6395. avm_initialize_rawio();
  6396. avm_initialize_formout();
  6397. avm_initialize_cmdline();
  6398. virtual_code_file = fopen(argv[1],"rb");
  6399. operator = avm_received_list(
  6400. virtual_code_file,argv[1]);
  6401. fclose(virtual_code_file);
  6402. command = avm_default_command_line(argc,
  6403. argv,2,NULL,NULL,0,0,NULL);
  6404. environs = avm_environment(env);
  6405. operand = avm_join(command,environs);
  6406. result = avm_apply(operator,operand);
  6407. avm_output_as_directed(result,0,0);
  6408. avm_dispose(result);
  6409. @dots{}
  6410. @}
  6411. @end example
  6412. The @code{avm_environment} function could cause the program to abort due
  6413. to a memory overflow. For security reasons, it will also abort with an
  6414. @cindex security
  6415. error message if any non-printing characters are detected in its
  6416. argument. (See @ref{Other Diagnostics and Warnings}.)
  6417. @end deftypefun
  6418. @deftypefun void avm_initialize_cmdline ()
  6419. This function initializes some local variables and should be called
  6420. before any of the other functions in this section is called, or else
  6421. their results are unpredictable.
  6422. @end deftypefun
  6423. @deftypefun void avm_count_cmdline ()
  6424. This function should be called after the last call to any of the other
  6425. functions in this section, as it reclaims some locally allocated
  6426. storage. If the @code{avm_count_lists} function is used, it should be
  6427. called after this one.
  6428. @end deftypefun
  6429. @node Execution Modes, , Command Line Parsing, Invocation
  6430. @subsection Execution Modes
  6431. Some functions declared in @file{exmodes.h} are useful for executing
  6432. interactive applications or filter mode transducers in a manner
  6433. consistent with the specifications described in the previous chapter.
  6434. @deftypefun void avm_interact (list @var{avm_interactor}, int @var{step_mode}, int @var{ask_to_overwrite_mode}, int @var{quiet_mode})
  6435. This function executes an interactive virtual code application. The
  6436. parameters have these interpretations.
  6437. @table @code
  6438. @item @var{avm_interactor}
  6439. is the virtual code for a function that performs as specified in
  6440. @ref{Output From Interactive Applications}.
  6441. @item @var{step_mode}
  6442. will cause all shell commands to be echoed if set to a non-zero
  6443. value, and will cause the program to pause after each shell command
  6444. until a key is pressed.
  6445. @item @var{ask_to_overwrite_mode}
  6446. can be set to a non-zero value by the caller to cause the program to ask
  6447. permission of the user to overwrite any existing files in cases where
  6448. the virtual code returns a file list as described in @ref{Mixed Modes of
  6449. Interaction}.
  6450. @item @var{quiet_mode}
  6451. can be set to a non-zero value to suppress console messages in the case
  6452. of file output per @ref{Mixed Modes of Interaction}.
  6453. @end table
  6454. The meaning of this function is accessible to any reader willing to slog
  6455. through @ref{Output From Interactive Applications}. The only subtle
  6456. point is that @code{@var{avm_interactor}} parameter in this function
  6457. does not correspond to the virtual code application that @code{avram}
  6458. reads from a virtual code file, but to the result computed when the
  6459. application read from the file is applied to the data structure
  6460. representing the command line and environment.
  6461. Any of the memory overflows or i/o errors possible with other functions
  6462. in the library are possible from this one as well, and will also cause
  6463. it to print an error message and halt the program. A badly designed
  6464. @cindex deadlock
  6465. virtual code application could cause a deadlock, which will not be
  6466. detected or reported
  6467. @end deftypefun
  6468. @deftypefun void avm_trace_interaction ()
  6469. This function enables diagnostic output for the @code{avm_recoverable_interact}
  6470. function.
  6471. @end deftypefun
  6472. @deftypefun void avm_disable_interaction ()
  6473. This function causes @code{avm_interact} and @code{avm_recoverable_interact}
  6474. to terminate with an error instead of executing, as required by the
  6475. @code{--jail} command line option.
  6476. @end deftypefun
  6477. @deftypefun list avm_recoverable_interact (list @var{interactor}, int @var{*fault})
  6478. This function is similar to @code{avm_interact} but always closes the
  6479. pipe and performs no file i/o, and will return an error message rather
  6480. than exiting. Otherwise it returns a transcript of the intereaction as
  6481. a list of lists of strings represented as lists of character
  6482. encodings. It implements the @var{interact} combinator with the
  6483. virtual code for the transducer function given as the parameter. A
  6484. prior call to @code{avm_trace_interaction} will cause diagnostic
  6485. information to be written to standard output when this function is
  6486. executed.
  6487. @end deftypefun
  6488. @deftypefun void avm_byte_transduce (list @var{operator})
  6489. This function executes a filter mode byte transducer application, which
  6490. behaves as described in @ref{Byte Transducers}. The argument is the
  6491. virtual code for the application, which would be found in a virtual code
  6492. file. There are limited opportunities for i/o errors, as only standard
  6493. input and standard output are involved with this function, but fatal
  6494. errors due to memory overflow are possible.
  6495. @end deftypefun
  6496. @deftypefun void avm_line_map (list @var{operator})
  6497. This function executes line mapped filter mode applications, which are
  6498. explained in @ref{Line Maps}. The argument is the virtual code for the
  6499. application. Similar comments to those above apply.
  6500. @end deftypefun
  6501. @deftypefun void avm_initialize_exmodes ()
  6502. This function should be called before any of the other functions in this
  6503. section in order to initialize some local variables. Results are
  6504. undefined if this function isn't called first.
  6505. @end deftypefun
  6506. @deftypefun void avm_count_exmodes ()
  6507. This function doesn't do anything in the present version of the library,
  6508. but should be called after the last call to any of the other functions
  6509. in this section in order to maintain compatibility with future versions,
  6510. which may use it for cleaning up local variables.
  6511. @end deftypefun
  6512. @node Version Management, Error Reporting, Invocation, Library Reference
  6513. @section Version Management
  6514. The @code{avram} library is designed to support any number of backward
  6515. @cindex versions
  6516. compatibility modes with itself, by way of some functions declared in
  6517. @file{vman.h}. The assumption is that the library will go through a
  6518. sequence of revisions during its life, each being identified by a unique
  6519. number. In the event of a fork in the project, each branch will
  6520. attempt to maintain compatibility at least with its own ancestors.
  6521. @deftypefun void avm_set_version (char *@var{number})
  6522. This function can be used to delay the demise of a client program that
  6523. uses the library but is not updated very often. The argument is a null
  6524. terminated string representing a version number, such as @code{"@value{VERSION}"}.
  6525. A call to this function requests that all library functions revert to
  6526. their behavior as of that version in any cases where the current
  6527. behavior is incompatible with it. It will also cause virtual code
  6528. applications evaluated with @code{avm_apply} to detect a version number
  6529. equal to the given one rather than the current one. (See @ref{Version}.)
  6530. The program will exit with an internal error message if any function in
  6531. the library has already interrogated the version number before this
  6532. function is called, or if it is passed a null pointer. This problem can
  6533. be avoided by calling it prior to any of the @code{avm_initialize}
  6534. functions with a valid address. The program will exit with the message
  6535. @display
  6536. @code{@var{program-name}: multiple version specifications}
  6537. @end display
  6538. if this function is called more than once, even with the same number.
  6539. If the number is not recognized as a present or past version, or is so
  6540. old that it is no longer supported, the program will exit with this
  6541. message.
  6542. @display
  6543. @code{avram: can't emulate version @var{number}}
  6544. @end display
  6545. Client programs that are built to last should allow the version number
  6546. to be specified as an option by the user, and let virtual code
  6547. applications that they execute take care of their own backward
  6548. compatibility problems. This strategy will at least guard against
  6549. changes in the virtual machine specification and other changes that do
  6550. not affect the library API.
  6551. @end deftypefun
  6552. @deftypefun int avm_prior_to_version (char *@var{number})
  6553. This function takes the address of a null terminated string representing
  6554. a version number as an argument, such as @code{"@value{VERSION}"}, and returns a
  6555. non-zero value if the version currently being emulated predates it.
  6556. If no call has been made to @code{avm_set_version} prior to the call to
  6557. this function, the current version is assumed, and subsequent calls to
  6558. @code{avm_set_version} will cause an internal error.
  6559. The intended use for this function would be by a maintainer of the
  6560. library introducing an enhancement that will not be backward compatible,
  6561. who doesn't wish to break existing client programs and virtual code
  6562. applications. For example, if a version @code{1.0} is developed at some
  6563. time in the distant future, and it incorporates a previously unexpected
  6564. way of doing something, code similar to the following could be used to
  6565. maintain backward compatibility.
  6566. @example
  6567. if (avm_prior_to_version("1.0"))
  6568. @{
  6569. /* do it the 0.x way */
  6570. @}
  6571. else
  6572. @{
  6573. /* do it the 1.0-and-later way */
  6574. @}
  6575. @end example
  6576. @noindent
  6577. This function will cause an internal error if the parameter does not
  6578. match any known past or present version number, or if it is a null pointer.
  6579. @end deftypefun
  6580. @deftypefun char* avm_version ()
  6581. This function returns the number of the version currently being emulated
  6582. as the address of a null terminated string. The string whose address is
  6583. returned should not be modified by the caller.
  6584. If no call has been made to @code{avm_set_version} prior to the call to
  6585. this function, the current version is assumed, and subsequent calls to
  6586. @code{avm_set_version} will cause an internal error.
  6587. @end deftypefun
  6588. @node Error Reporting, Profiling, Version Management, Library Reference
  6589. @section Error Reporting
  6590. @cindex error messages
  6591. Most of the error reporting by other functions in the library is done by
  6592. way of the functions declared in @file{error.h}. These function
  6593. communicate directly with the user through standard error. Client
  6594. programs should also use these functions where possible for the sake of
  6595. a uniform interface.
  6596. @deftypefun void avm_set_program_name (char *@var{argv0})
  6597. The argument to this function should be the address of a null terminated
  6598. string holding the name of the program to be reported in error messages
  6599. that begin with a program name. Typically this string will be the name
  6600. of the program as it was invoked on the command line, possibly with path
  6601. components stripped from it. An alternative would be to set it to the
  6602. name of a virtual code application being evaluated. If this function is
  6603. never called, the name @code{"avram"} is used by default. Space for a
  6604. copy of the program name is allocated by this function, and a fatal
  6605. memory overflow error is possible if there is insufficient space available.
  6606. @end deftypefun
  6607. @deftypefun char* avm_program_name ()
  6608. This function returns a pointer to a null terminated character string
  6609. holding the program name presently in use. It will be either the name
  6610. most recently set by @code{avm_set_program_name}, or the default name
  6611. @code{"avram"} if none has been set. The string whose address is
  6612. returned should not be modified by the caller.
  6613. @end deftypefun
  6614. @deftypefun void avm_warning (char *@var{message})
  6615. This function writes the null terminated string whose address is given
  6616. to standard error, prefaced by the program name and followed by a line
  6617. break.
  6618. @end deftypefun
  6619. @deftypefun void avm_error (char *@var{message})
  6620. This function writes the null terminated string whose address is given
  6621. to standard error, prefaced by the program name and followed by a line
  6622. break, as @code{avm_warning}, but it then terminates the process with an
  6623. exit code of 1.
  6624. @end deftypefun
  6625. @deftypefun void avm_fatal_io_error (char *@var{message}, char *@var{filename}, int @var{reason})
  6626. This function is useful for reporting errors caused in the course of
  6627. reading or writing files. The message is written to standard error
  6628. prefaced by the program name, and incorporating the name of the relevant
  6629. file. The @code{@var{reason}} should be the error code obtained from the
  6630. standard @code{errno} variable, which will be translated to an
  6631. @cindex @code{strerror}
  6632. informative message if possible by the standard @code{strerror} function
  6633. and appended to the message. After the message is written, the process
  6634. will terminate with an exit code of 1.
  6635. @end deftypefun
  6636. @deftypefun void avm_non_fatal_io_error (char *@var{message}, char *@var{filename}, int @var{reason})
  6637. This function does the same as @code{avm_fatal_io_error} except that it
  6638. doesn't exit the program, and allows control to return to the caller,
  6639. which should take appropriate action.
  6640. @end deftypefun
  6641. @deftypefun void avm_internal_error (int @var{code})
  6642. This function is used to report internal errors and halt the
  6643. program. The error message is written to standard error prefaced by the
  6644. program name and followed by a line break. The code should be a unique
  6645. integer constant (i.e., not one that's used for any other internal
  6646. error), that will be printed as part of the error message as an aid to
  6647. the maintainer.
  6648. This function should be used by client programs only in the event of
  6649. conditions that constitute some violation of a required invariant. It
  6650. indicates to the user that something has gone wrong with the program,
  6651. for which a bug report would be appropriate.
  6652. @end deftypefun
  6653. @deftypefun void avm_reclamation_failure (char *@var{entity}, counter @var{count})
  6654. This function is used only by the @code{avm_count} functions to report
  6655. unreclaimed storage. The @code{@var{count}} is the number of units of
  6656. storage left unreclaimed, and the @code{@var{entity}} is the address of
  6657. a null terminated string describing the type of unreclaimed entity, such
  6658. as @code{"lists"} or @code{"branches"}. The message is written to
  6659. standard error followed by a line break, but the program is not halted
  6660. and control returns to the caller.
  6661. @end deftypefun
  6662. @node Profiling, Emulation Primitives, Error Reporting, Library Reference
  6663. @section Profiling
  6664. @cindex @file{profile.h}
  6665. The functions declared in @file{profile.h} can be used for constructing
  6666. and writing tables of run time statistics such as those mentioned in
  6667. @ref{Files}, and @ref{Profile}. These functions maintain a database of
  6668. structures, each recording the statistics for a particular virtual code
  6669. fragment.
  6670. Each structure in the database is identified by a unique key, which must
  6671. be a list representing a character string. A pointer to such a structure
  6672. @cindex @code{score}
  6673. is declared to be of type @code{score}. For the most part, the data
  6674. structure should be regarded as opaque by a client program, except for a
  6675. field @code{reductions} of type @code{counter}, which may be modified
  6676. arbitrarily by the client.
  6677. The way these operations are used in the course of evaluating virtual
  6678. code applications containing profile annotations is to add a structure
  6679. to the database each time a new profiled code fragment is encountered,
  6680. using the annotation as its key, and to increment the @code{reductions}
  6681. @cindex annotations
  6682. of the structure each time any constituent of the code gets a quantum of
  6683. work done on it. Other ways of using these operations are left to the
  6684. developer's discretion.
  6685. @deftypefun score avm_entries (list @var{team}, list *@var{message}, int *@var{fault})
  6686. This function retrieves or creates a data base entry given its key. The
  6687. parameters have these interpretations.
  6688. @table @code
  6689. @item @var{team}
  6690. is a list representing a character string that uniquely identifies the
  6691. database entry to be retrieved or created.
  6692. @item @var{message}
  6693. is the address of a list known to the caller, which will be assigned a
  6694. list representing an error message if any error occurs in the course of
  6695. searching the database or creating a new entry.
  6696. @item @var{fault}
  6697. is the address of an integer that will be set to a non-zero value if any
  6698. error is caused by this function.
  6699. @end table
  6700. The pointer returned by this function is the address of the record whose
  6701. key is given by the @code{@var{team}} parameter. If such a record is
  6702. already in the database, its address is returned, but otherwise a new
  6703. one is created whose address is then returned. The @code{reductions}
  6704. field of a newly created entry will be zero.
  6705. In the course of searching the database, the @code{avm_compare}
  6706. function is used, so the associated lists may be modified as noted in
  6707. @ref{Comparison}. It is not necessary for a client to include the header
  6708. file @file{compare.h} or to call @code{avm_initialize_compare} in order
  6709. to use the profile operations, because they are done automatically.
  6710. If an error message is assigned to the list referenced by
  6711. @code{@var{message}}, the integer referenced by @code{@var{fault}} will
  6712. be set to a non-zero value. The form of the error message will be a list
  6713. in which each item is a list of character representations as per
  6714. @ref{Character Table}. It is the responsibility of the caller to dispose
  6715. of the error message. Currently the only possible error is a memory
  6716. overflow, which in this case is non-fatal.
  6717. @end deftypefun
  6718. @deftypefun void avm_tally (char *@var{filename})
  6719. This function makes a table of the results stored in the data base built
  6720. by the @code{avm_entries} function. The argument is the address of a
  6721. null terminated character string containing the name of the file in
  6722. which the results will be written. A file is opened and the table is
  6723. written in a self explanatory text format, with columns labeled
  6724. ``reductions'' and ``invocations'' among others. The latter contains the
  6725. number of times the associated key was accessed through
  6726. @code{avm_entries}.
  6727. The data written to the file should be taken with a grain of salt. It
  6728. is computed using native integer and floating point arithmetic, with no
  6729. checks made for overflow or roundoff error, and no guarantee of cross
  6730. @cindex reductions
  6731. platform portability. The number of ``reductions'' means whatever the
  6732. developer of the client program wants it to mean.
  6733. The following error messages are possible with this function, which will
  6734. be written to standard error. None of them is fatal.
  6735. @cindex @code{can't write}
  6736. @cindex @code{can't close}
  6737. @cindex @code{invalid profile identifier}
  6738. @itemize @bullet
  6739. @item @code{@var{program-name}: can't write @var{filename}}
  6740. @item @code{@var{program-name}: can't write to @var{filename}}
  6741. @item @code{@var{program-name}: can't close @var{filename}}
  6742. @item @code{@var{program-name}: invalid profile identifier}
  6743. @end itemize
  6744. The last message is reported if any record in the database has a key
  6745. that is not a list of valid character representations. The others are
  6746. @cindex @code{strerror}
  6747. accompanied by an explanation from the standard @code{strerror} function
  6748. if possible.
  6749. @end deftypefun
  6750. @deftypefun void avm_initialize_profile ()
  6751. This function should be called before any of the other functions in this
  6752. section in order to initialize the data base. Results are undefined if
  6753. it is not called first.
  6754. @end deftypefun
  6755. @deftypefun void avm_count_profile ()
  6756. This function can be called after the other functions in this section as
  6757. a way of detecting memory leaks. If any storage remains unreclaimed that
  6758. was created by the functions in this section, a warning message is
  6759. written to standard error. If the @code{avm_count_lists} function is
  6760. being used by the client program, it should be called after this one.
  6761. @end deftypefun
  6762. @node Emulation Primitives, External Library Maintenance, Profiling, Library Reference
  6763. @section Emulation Primitives
  6764. The functions documented in this section can be used to take very
  6765. specific control over the evaluation of virtual code applications. It is
  6766. unlikely that a client program will have any need for them unless it
  6767. aims to replace or extend the @code{avm_apply} function.
  6768. The virtual machine is somewhat removed from a conventional von Neumann
  6769. model of computation, so emulating it in C or any other imperative
  6770. language is less straightforward than one would prefer. An elaborate
  6771. system of interdependent data structures is used to represent partially
  6772. evaluated computations, which does not particularly lend itself to a
  6773. convenient, modular API. The abstraction provided by the functions in
  6774. this section is limited mainly to that of simple memory management and
  6775. stack operations. Consequently, a developer wishing to build on them
  6776. effectively would need to @emph{grok} the data structures involved,
  6777. which are described in some detail.
  6778. @menu
  6779. * Lists of Pairs of Ports::
  6780. * Ports and Packets::
  6781. * Instruction Stacks::
  6782. @end menu
  6783. @node Lists of Pairs of Ports, Ports and Packets, Emulation Primitives, Emulation Primitives
  6784. @subsection Lists of Pairs of Ports
  6785. @cindex @code{port}
  6786. A @code{port} is the name given to a type of pointer used in the library
  6787. as the address of a place where a computational result yet to be
  6788. evaluated will be sent. Ports are discussed further in @ref{Ports and Packets},
  6789. but are mentioned here because it is sometimes necessary to employ a
  6790. list of pairs of them. A pointer to such a list is declared as a
  6791. @code{portal} type. It refers to a structure of the form
  6792. @cindex @code{portal}
  6793. @cindex @code{port_pair}
  6794. @example
  6795. struct port_pair
  6796. @{
  6797. port left;
  6798. port right;
  6799. portal alters;
  6800. @}
  6801. @end example
  6802. A small selection of functions for @code{portal} memory management is
  6803. declared as follows in the header file @file{portals.h}. For reasons of
  6804. C-ness, the type declarations themselves are forced to be in
  6805. @file{lists.h}.
  6806. @deftypefun portal avm_new_portal (portal @var{alters})
  6807. This function is used to create storage for a new @code{port_pair}
  6808. structure, and returns a @code{portal} pointer to it if successful. If
  6809. the storage can't be allocated, a @code{NULL} pointer is returned.
  6810. The @code{alters} field of the result is initialized as the given
  6811. parameter supplied by the caller. All other fields are filled with zeros.
  6812. @end deftypefun
  6813. @deftypefun void avm_seal (portal @var{fate})
  6814. This function performs the reclamation of storage associated with
  6815. @code{portal} pointers, either by freeing them or by consigning them
  6816. temporarily to a local cache for performance reasons. Client programs
  6817. should use only this function for disposing of @code{portal} storage
  6818. rather than using @code{free} directly, so as to allow accurate record
  6819. keeping.
  6820. @end deftypefun
  6821. @deftypefun void avm_initialize_portals ()
  6822. This function should be called by a client program prior to calling
  6823. either of the above memory management functions in order to initialize
  6824. some local variables. Anomalous results are possible otherwise.
  6825. @end deftypefun
  6826. @deftypefun void avm_count_portals ()
  6827. This function should be called at the end of a run or after the last
  6828. call to any of the other functions in this section as a way of detecting
  6829. memory leaks associated with @code{portal} pointers. A warning message
  6830. will be written to standard error if any remains unreclaimed.
  6831. @end deftypefun
  6832. @node Ports and Packets, Instruction Stacks, Lists of Pairs of Ports, Emulation Primitives
  6833. @subsection Ports and Packets
  6834. A pointer type declared as a @code{port} points to a structure in the
  6835. following form, where a @code{flag} is an unsigned short integer type,
  6836. and a @code{counter} is an unsigned long integer.
  6837. @cindex @code{counter}
  6838. @cindex @code{flag}
  6839. @cindex @code{avm_packet}
  6840. @example
  6841. struct avm_packet
  6842. @{
  6843. port parent;
  6844. counter errors;
  6845. portal descendents;
  6846. list impetus, contents;
  6847. flag predicating;
  6848. @};
  6849. @end example
  6850. @noindent
  6851. For reasons that make sense to C, the @code{avm_packet} and @code{port}
  6852. types are declared in @code{lists.h}, but a few memory management
  6853. operations on them are available by way of functions declared in
  6854. @file{ports.h}. The intended meaning of this structure is described
  6855. presently, but first the memory management functions are as follows.
  6856. @deftypefun port avm_newport (counter @var{errors}, port @var{parent}, int @var{predicating})
  6857. This function attempts to allocate storage for a new packet structure
  6858. and returns its address if successful. If storage can not be allocated,
  6859. a @code{NULL} pointer is returned. The @code{errors}, @code{parent}, and
  6860. @code{predicating} fields are initialized with the parameters supplied
  6861. by the caller. The rest of the structure is filled with zeros. A local
  6862. memory cache is used for improved performance.
  6863. @end deftypefun
  6864. @deftypefun void avm_sever (port @var{appendage})
  6865. This function reclaims the storage associated with a @code{port}, either
  6866. freeing it entirely or holding it in a local cache. None of the
  6867. entities that may be referenced by pointers within the structure are
  6868. affected. Only this function should be used by client programs for
  6869. disposing of ports, not the @code{free} function directly, or some
  6870. internal bookkeeping will be disrupted. An internal error results if the
  6871. argument is a @code{NULL} pointer.
  6872. @end deftypefun
  6873. @deftypefun void avm_initialize_ports ()
  6874. This function must be called prior to calling either of the two above,
  6875. in order to initialize some static variables.
  6876. @end deftypefun
  6877. @deftypefun void avm_count_ports ()
  6878. This function may be called after the last call to any of the other
  6879. functions in this section in order to detect and report unreclaimed
  6880. storage associated with ports. A non-fatal warning will be written to
  6881. standard error if any is detected, but otherwise there is no effect.
  6882. @end deftypefun
  6883. The interesting aspect of this data structure is the role it plays in
  6884. capturing the state of a computation. For this purpose, it corresponds
  6885. to a single node in a partially computed result to be represented by a
  6886. @code{list} when it's finished. The nodes should be envisioned as a
  6887. doubly-linked binary tree, except that the pair of @code{descendents}
  6888. for each node is not yet known with certainty, so a list of alternatives
  6889. must be maintained.
  6890. Because the computation is not completed while this data structure
  6891. exists, there are always some empty fields in it. For example, the
  6892. @code{descendents} and the @code{contents} fields embody the same
  6893. information, the latter doing so in a compact as opposed to a more
  6894. expanded form. Hence, it would be redundant for both fields to be
  6895. non-empty at the same time. The data structure is built initially with
  6896. @code{descendents} and no @code{contents}, only to be transformed into
  6897. one with @code{contents} and no @code{descendents}.
  6898. The significance of each field in the structure can be summarized as
  6899. follows.
  6900. @table @code
  6901. @item contents
  6902. If the computational result destined for the @code{port} pointing to this packet
  6903. is not complete, then this field is @code{NULL} and the
  6904. @code{descendents} are being computed. Otherwise, it contains the result
  6905. of the computation.
  6906. @item descendents
  6907. This field points to a list of pairs of ports serving as the
  6908. destinations for an ensemble of concurrent computations.@footnote{Earlier
  6909. versions of @code{avram} included a bottom avoiding choice combinator
  6910. that required this feature, but which has been withdrawn. A single pair
  6911. of descendent ports would now suffice.} The @code{head}
  6912. and @code{tail} of the @code{contents} are to be identified respectively
  6913. with the @code{contents} of the @code{left} and @code{right} @code{port}
  6914. in the first pair to finish being computed.
  6915. @item parent
  6916. If this packet is addressed by the @code{left} or the @code{right} of
  6917. @code{port} in one of the @code{descendents} of some other packet, then
  6918. this field points to that packet.
  6919. @item errors
  6920. A non-zero value in this field indicates that the result destined for
  6921. the @code{contents} of this packet is expected to be an error
  6922. message. If the exact level of error severity incurred in the
  6923. computation of the @code{contents} matches this number, then the
  6924. contents can be assigned the result, but otherwise the result should
  6925. propagate to the @code{contents} of the @code{parent}.
  6926. @item predicating
  6927. A non-zero value in this field implies that the result destined for the
  6928. @code{contents} of this packet is being computed in order to decide
  6929. which arm of a conditional function should be chosen. I.e., a
  6930. @code{NULL} result calls for the one that is invoked when the predicate
  6931. is false.
  6932. @item impetus
  6933. If the result destined for the @code{contents} of this packet is being
  6934. computed in order to transform a virtual code fragment from its original
  6935. form to an equivalent representation capable of being evaluated more
  6936. directly, this field points to a @code{list} node at the root of the
  6937. virtual code in its original form.
  6938. @end table
  6939. One of the hitherto undocumented fields in a @code{list} node structure
  6940. @cindex @code{interpretation}
  6941. @cindex @code{impetus}
  6942. declared in @file{lists.h} is called the @code{interpretation}, and is
  6943. of type @code{list}. A client program delving into sufficient depth of
  6944. detail to be concerned with ports and packets may reasonably assign the
  6945. @code{interpretation} field of the @code{list} referenced by the
  6946. @code{impetus} field in a packet to be a copy of the @code{contents} of
  6947. the packet when they are eventually obtained. Doing so will save some
  6948. time by eliminating the need for it to be recomputed if the same virtual
  6949. code should be executed again.
  6950. If this course is taken, the @code{facilitator} field in a @code{list}
  6951. @cindex @code{facilitator}
  6952. node, also hitherto undocumented, should contain the address of the
  6953. packet referring to the list node as its @code{impetus}. The reason for
  6954. this additional link is so that it can be followed when the
  6955. @code{impetus} of the packet is be cleared by @code{avm_dispose} in the
  6956. event that the @code{list} node is freed before the computation
  6957. completes. This action is performed in order to preclude a dangling
  6958. pointer in the @code{impetus} field.
  6959. @node Instruction Stacks, , Ports and Packets, Emulation Primitives
  6960. @subsection Instruction Stacks
  6961. A header file named @file{instruct.h} declares a number of memory
  6962. management and stack operations on a data structure of the following
  6963. form.
  6964. @cindex @code{instruction_node}
  6965. @example
  6966. struct instruction_node
  6967. @{
  6968. port client;
  6969. score sheet;
  6970. struct avm_packet actor;
  6971. struct avm_packet datum;
  6972. instruction dependents;
  6973. @};
  6974. @end example
  6975. In this structure, an @code{instruction} is a pointer to an
  6976. @code{instruction_node}, a @code{score} is a pointer to a profile
  6977. database entry as discussed in @ref{Profiling}, and the @code{port} and
  6978. @code{avm_packet} types are as described in @ref{Ports and Packets}.
  6979. This data structure is appropriate for a simple virtual machine code
  6980. @cindex concurrency
  6981. evaluation strategy involving no concurrency. The strategy to evaluate an
  6982. expression @code{@var{f} @var{x}} would be based on a stack of these
  6983. nodes threaded through the @code{dependents} field, and would proceed
  6984. something like this.
  6985. @enumerate
  6986. @item
  6987. The stack is initialized to contain a single node having
  6988. @code{@var{f}} in its @code{actor.contents} field, and @code{@var{x}} in
  6989. its @code{datum.contents} field.
  6990. @item
  6991. The @code{client} in this node would refer to a static packet to whose
  6992. @code{contents} field the final result will be delivered.
  6993. @item
  6994. The evaluator examines the @code{actor.contents} field on the top of the
  6995. stack, detects by its form the operation it represents, and decides
  6996. whether it corresponds to one that can be evaluated immediately by way
  6997. of a canned function available in the library. List reversal,
  6998. transposition, and comparison would be examples of such operations.
  6999. @item
  7000. If the operation can be performed in this way, the result is computed
  7001. and assigned to the destination indicated by the @code{client} field.
  7002. @item
  7003. If the operation is not easy enough to perform immediately but is of a
  7004. form recognizable as a combination of simpler operations, it is
  7005. decomposed into the simpler operations, and each of them is
  7006. strategically positioned on the stack so as to effect the evaluation of
  7007. the combination. For example, if @code{@var{f}} were of the form
  7008. @code{compose(@var{g},@var{h})} (@code{silly} notation), the node with
  7009. @code{@var{f}} and @code{@var{x}} would be popped, but a node with
  7010. @code{@var{g}} as its @code{actor.contents} would be pushed, and then a
  7011. node with @code{@var{h}} as its @code{actor.contents} and @code{@var{x}}
  7012. as its @code{datum.contents} would be pushed. Furthermore, the
  7013. @code{client} field of the latter node would point to the
  7014. @code{datum.contents} of the one with @code{@var{g}}, and the
  7015. @code{client} field of the one with @code{@var{g}} would point
  7016. wherever the @code{client} of the popped node used to point.
  7017. @item
  7018. If the operation indicated by the top @code{actor.contents} is neither
  7019. implemented by a canned operation in the library nor easily decomposable
  7020. into some that are, the evaluator can either give up or use virtual code
  7021. to execute other virtual code. The latter trick is accomplished by
  7022. pushing a node with @code{@var{f}} as its @code{datum.contents}, and a
  7023. copy of a hard coded virtual code interpreter @code{@var{V}} as its
  7024. @code{actor.contents}. The @code{client} of this node will point to the
  7025. @code{@var{f}} in the original node so as to overwrite it when a
  7026. simplified version is subsequently computed. The implementation of
  7027. @code{@var{V}} is a straightforward exercise in @code{silly}
  7028. programming.
  7029. @item
  7030. In any case, the evaluator would continue working on the stack until
  7031. everything on it has been popped, at which time the result of the entire
  7032. computation will be found in the packet addressed by the @code{client}
  7033. in the original instruction node.
  7034. @end enumerate
  7035. What makes this strategy feasible to implement is the assumption of a
  7036. sequential language, wherein synchronization incurs no cost and is
  7037. automatic. The availability of any operand is implied by its position at
  7038. the top of the stack. If you are reading this section with a view to
  7039. @cindex threads
  7040. implementing a concurrent or multi-threaded evaluation strategy, it will
  7041. be apparent that further provisions would need to be made, such as that
  7042. of a @code{data_ready} flag added to the @code{avm_packet} structure.
  7043. The following functions support the use of stacks of instruction nodes
  7044. that would be needed in an evaluation strategy such as the one above.
  7045. @deftypefun int avm_scheduled (list @var{actor_contents}, counter @var{datum_errors}, list @var{datum_contents}, port @var{client}, instruction *@var{next}, score @var{sheet})
  7046. This function performs the memory allocation for instruction nodes. It
  7047. attempts to create one and to initialize the fields with the given
  7048. parameters, returning a pointer to it if successful. It returns a
  7049. @code{NULL} pointer if the storage could not be allocated.
  7050. Copies of the @code{list} parameters @code{actor_contents} and
  7051. @code{data_contents} are made by this function using @code{avm_copied},
  7052. so the originals still exist as far as the caller is concerned and will
  7053. have to be deallocated separately from this structure. The copies are
  7054. made only if the allocation succeeds.
  7055. Any fields other than those indicated by the parameters to this function
  7056. are filled with zeros in the result.
  7057. @end deftypefun
  7058. @deftypefun void avm_retire (instruction *@var{done})
  7059. This function performs the storage reclamation of instructions, taking
  7060. as its argument the instruction to be reclaimed. The @code{list} fields
  7061. in the structure corresponding to the @code{list} parameters used when
  7062. it was created are specifically reclaimed as well, using
  7063. @code{avm_dispose}.
  7064. The argument to this function is the address of an @code{instruction}
  7065. rather than just an @code{instruction} so that the @code{instruction}
  7066. whose address is given may be reassigned as the @code{dependents} field
  7067. of the deallocated node. In this way, the instructions can form a stack
  7068. that is popped by this function.
  7069. This function cooperates with @code{avm_scheduled} in the use of a local
  7070. cache of instruction nodes in the interest of better performance. Client
  7071. modules should not attempt to allocate or reclaim instructions directly
  7072. with @code{malloc} or @code{free}, but use only these functions.
  7073. It causes a fatal internal error to pass a @code{NULL} pointer to this
  7074. function.
  7075. @end deftypefun
  7076. @deftypefun void avm_reschedule (instruction *@var{next})
  7077. Given the address of an instruction pointer that may be regarded as the
  7078. top of a stack of instructions threaded through the @code{dependents}
  7079. field, this function exchanges the positions of the top instruction and
  7080. the one below it. A fatal internal error is caused if there are fewer
  7081. than two instructions in the stack.
  7082. A use for this function arises in the course of evaluating virtual code
  7083. applications of the form @code{conditional(@var{p},(@var{f},@var{g}))}
  7084. (in @code{silly} notation). The evaluation strategy would require
  7085. pushing nodes for all three constituents, but with @code{@var{p}} pushed
  7086. last (therefore evaluated first). The result of the evaluation of
  7087. @code{@var{p}} would require either the top one or the one below it to
  7088. be popped without being evaluated, depending on whether the result is
  7089. empty.
  7090. @end deftypefun
  7091. @deftypefun void avm_initialize_instruct ()
  7092. This function should be called before any of the instruction memory
  7093. management functions is called in order to initialize some local data
  7094. structures. Results are unpredictable without it.
  7095. @end deftypefun
  7096. @deftypefun void avm_count_instruct ()
  7097. This function should be called after the last call to any of the
  7098. other functions in this section in order to detect and report
  7099. unreclaimed storage associated with them. A warning message will be
  7100. written to standard error if any unreclaimed instructions remain. This
  7101. function relies on the assumption that the memory management has been
  7102. done only by way of the above functions.
  7103. @end deftypefun
  7104. @node External Library Maintenance, , Emulation Primitives, Library Reference
  7105. @section External Library Maintenance
  7106. External mathematical library functions such as those documented in
  7107. @ref{External Libraries} that are invoked from virtual code by the
  7108. @code{library} combinator (@ref{Library combinator}) are also
  7109. accessible from C by way of a uniform API implemented by the functions
  7110. declared in @code{libfuns.h}. This interface applies even to libraries
  7111. @cindex Fortran
  7112. implemented in Fortran such as @code{minpack}. This section briefly
  7113. documents the functions in @code{libfuns.h} and sets out some
  7114. recommeded guidelines for developers wishing to add support for other
  7115. external libraries.
  7116. @menu
  7117. * Calling existing library functions::
  7118. * Implementing new library functions::
  7119. * Working around library misfeatures::
  7120. @end menu
  7121. @node Calling existing library functions, Implementing new library functions, External Library Maintenance, External Library Maintenance
  7122. @subsection Calling existing library functions
  7123. Whatever data types a library function manipulates, its argument and
  7124. its result are each ultimately encoded each by a single list as
  7125. explained in @ref{Type Conversions}. This representation allows all
  7126. library functions to be invoked by a uniform calling convention as
  7127. detailed below.
  7128. @deftypefun list avm_library_call (list @var{library_name}, @var{list function_name}, list @var{argument}, int *@var{fault})
  7129. This function serves as an interpreter of external library functions
  7130. by taking a @var{library_name}, a @var{function_name}, and an
  7131. @var{argument} to the result returned by the corresponding library
  7132. function for the given @var{argument}.
  7133. The library and function names should be encoded as lists of character
  7134. representations, the same as the arguments that would be used with the
  7135. @code{library} combinator if it were being invoked by virtual code
  7136. @cindex backward compatability
  7137. (with attention to the backward compatibility issue explained in
  7138. @ref{Characters and Strings}).
  7139. If an error occurs in the course of evaluating a library function, the
  7140. integer referenced by @var{fault} will be assigned a non-zero value,
  7141. and the result will be a list of character string representations
  7142. explaining the error, such as @code{<'memory overflow'>}, for example.
  7143. Otherwise, the list returned will encode the result of the library
  7144. function in a way that depends on the particular function being evaluated.
  7145. @end deftypefun
  7146. @deftypefun list avm_have_library_call (list @var{library_name}, list @var{function_name}, int *@var{fault})
  7147. This function implements the @code{have} combinator described in
  7148. @ref{Have combinator}, which tests for the availability of a library
  7149. function. The @var{library_name} and @var{function_name} parameters
  7150. are as explained above for @code{avm_library_call}, and @code{fault}
  7151. could signal an error similarly for this function as well.
  7152. The result returned will be an error message in the event of an error,
  7153. or a list of pairs of strings otherwise. The list will be empty if the
  7154. library function is not available. If the library function is
  7155. available, the list will contain a single pair, as in
  7156. @example
  7157. <(library_name,function_name)>
  7158. @end example
  7159. In addition, the list representation of the character string
  7160. @code{'*'} can be specified as either the library name or the function
  7161. name or both. This string is interpreted as a wild card and will cause
  7162. all matching pairs of library and function names to be returned in the
  7163. list.
  7164. @end deftypefun
  7165. @deftypefun void avm_initialize_libfuns ()
  7166. This function initializes some static data structures used by the two
  7167. functions above. It may be called optionally before the first call to
  7168. either of them, but will be called automatically if not.
  7169. @end deftypefun
  7170. @deftypefun void avm_count_libfuns ()
  7171. This function can be used as an aid to detecting memory leaks. It
  7172. reclaims any data structures allocated by
  7173. @code{avm_initialize_libfuns} and should be called towards the end of
  7174. a run some time prior to @code{avm_count_lists} @ref{Simple
  7175. Operations}, if the latter is being used.
  7176. @end deftypefun
  7177. @node Implementing new library functions, Working around library misfeatures, Calling existing library functions, External Library Maintenance
  7178. @subsection Implementing new library functions
  7179. Adding more external libraries to @code{avram} is currently a manual
  7180. procedure requiring the attention of a developer conversant with C.
  7181. To support a new library called @code{foobar},
  7182. these steps need to be followed at a minimum.
  7183. @itemize @bullet
  7184. @item
  7185. Create a new file called @file{foobar.h} under the @file{avm/}
  7186. directory in the main source tree whose name doesn't clash with any
  7187. @cindex header file
  7188. @cindex library interface header file
  7189. existing file names and preferably doesn't induce any proper prefixes
  7190. among them. This file should contain at least these function
  7191. declarations.
  7192. @example
  7193. extern list avm_foobar_call (list function_name,list argument,
  7194. int *fault);
  7195. extern list avm_have_foobar_call (list function_name,int *fault);
  7196. extern void avm_initialize_foobar ();
  7197. extern void avm_count_foobar ();
  7198. @end example
  7199. There should also be the usual preprocessor directives for
  7200. @file{include} files. The naming convention shown should be followed in
  7201. anticipation of automated support for these operations in the future.
  7202. @item
  7203. Add @file{foobar.h} to the list of other header files in
  7204. @file{avm/Makefile.am}.
  7205. @item
  7206. Create a new file called @file{foobar.c} under the @file{src/}
  7207. directory whose name doesn't clash with any existing file names to
  7208. @cindex library interfac source file
  7209. store most of the library interface code. It can start out with
  7210. stubs for the functions declared in @file{foobar.h}.
  7211. @item
  7212. Add @file{foobar.c} to the list of other source files in
  7213. @file{src/Makefile.am}
  7214. @item
  7215. Execute the following command in the main @file{avram-x.x.x}
  7216. source directory where the file @file{configure.in} is found.
  7217. @example
  7218. aclocal \
  7219. && automake --gnu --add-missing \
  7220. && autoconf
  7221. @end example
  7222. This command requires having @code{automake} and
  7223. @cindex automake
  7224. @cindex autoconf
  7225. @code{autoconf} installed on your system.
  7226. @item
  7227. Make the following changes to @file{libfuns.c}.
  7228. @itemize @bullet
  7229. @item
  7230. Add the line @code{#include<avm/foobar.h>} after the
  7231. @cindex include directives
  7232. other @code{include} directives.
  7233. @item
  7234. Add the string @code{"foobar"} to the end of the array of
  7235. @code{libnames} in @code{avm_initialize_libfuns}.
  7236. @item
  7237. Add a call to @code{avm_initialize_foobar} to the body.
  7238. @item
  7239. Add a call to @code{avm_count_foobar} to the body of
  7240. @code{avm_count_libfuns}.
  7241. @item
  7242. Add a case of the form
  7243. @example
  7244. case nn:
  7245. return avm_foobar_call(function_name,argument,fault);
  7246. @end example
  7247. after the last case in @code{avm_library_call}, being
  7248. careful not to change the order, and using the same
  7249. name as above in the file @file{foobar.h}.
  7250. @item
  7251. Add a case of the form
  7252. @example
  7253. case nn:
  7254. looked_up = avm_have_foobar_call(function_name,fault);
  7255. break;
  7256. @end example
  7257. after the last case in @code{avm_have_library_call}, being
  7258. careful not to change the order, and using the same name
  7259. as above in the file @file{foobar.h}.
  7260. @end itemize
  7261. @item
  7262. Edit @file{foobar.c} and @file{foobar.h} to suit,
  7263. periodically compiling and testing by executing @code{make}.
  7264. @item
  7265. Package and install at will.
  7266. @end itemize
  7267. The functions shown above have the obvious interpretations, namely
  7268. that @code{avm_foobar_call} evaluates a library function from the
  7269. @code{foobar} library, and @code{avm_have_foobar_call} tests for a
  7270. function's availability. The latter should interpret wild cards as
  7271. explained in @ref{Calling existing library functions}, but should
  7272. return only a list of strings for the matching function names rather
  7273. than a list of pairs of strings, as the library name is redundant.
  7274. The remaining functions are for static initialization and reclamation.
  7275. These functions should consist mainly of boilerplate code similar to
  7276. the corresponding functions in any of the other library source files,
  7277. which should be consulted as examples. The real work would be done by
  7278. other functions called by them. These should be statically declared
  7279. within the @file{.c} source file and normally not listed in the
  7280. @file{.h} header file unless there is some reason to think they may be
  7281. of more general use. Any externally visible functions should have
  7282. names beginning with @code{avm_} to avoid name clashes.
  7283. Some helpful hints are reported below for what they may be worth.
  7284. @itemize @bullet
  7285. @item
  7286. The reason for doing this is to leverage off other people's
  7287. intelligence, so generally @code{foobar.c} should contain only glue
  7288. code for library routines developed elsewhere with great skill rather
  7289. than reinventing them in some home grown way.
  7290. @item
  7291. The best numerical software is often written by Fortran
  7292. @cindex Fortran
  7293. programmers. Linking to a Fortran library is no problem on GNU systems
  7294. provided that all variables are passed by reference and all arrays are
  7295. converted to column order (@ref{Type Conversions}).
  7296. @item
  7297. Most C++ programmers have yet to reach a comparable standard, but C++
  7298. @cindex C++
  7299. libraries can also be linked by running @code{nm} on the static
  7300. @cindex nm utility
  7301. library file to find out the real names of the functions and
  7302. @cindex c++filt utility
  7303. @code{c++filt} to find out which is which. However, there
  7304. is no obvious workaround for the use of so called derived classes
  7305. by C++ programmers to simulate passing functions as parameters.
  7306. @item
  7307. Anything worth using can probably be found in the Debian
  7308. @cindex Debian
  7309. archive.
  7310. @item
  7311. Not all libraries are sensible candidates for interfaces to
  7312. @code{avram}. Typical design flaws are
  7313. @itemize @bullet
  7314. @item
  7315. irrepressible debugging messages written to @code{stderr} or
  7316. @code{stdout} that are unfit for end user consumption
  7317. @item
  7318. deliberately crashing the application if @code{malloc} fails
  7319. @item
  7320. opaque data types with undocumented storage requirements
  7321. @item
  7322. opaque data types that would be useful to store persistently
  7323. but have platform specific binary representations
  7324. @item
  7325. heavily state dependent
  7326. @cindex state dependence
  7327. semantics
  7328. @item
  7329. identifiers with clashing names
  7330. @item
  7331. restrictive
  7332. @cindex licensing restrictions
  7333. licenses
  7334. @end itemize
  7335. Some of these misfeatures have workarounds as explained next in
  7336. @ref{Working around library misfeatures}, at least if there's
  7337. nothing else wrong with the library.
  7338. @end itemize
  7339. Those who support @code{avram} are always prepared to assist in the
  7340. dissemination of worthwhile contributed library modules under terms
  7341. compatible with @ref{Copying}, and under separate copyrights if
  7342. @cindex copyright
  7343. preferred. Contributed modules can be integrated into the official
  7344. source tree provided that they meet the following additional
  7345. @cindex coding standards
  7346. guidelines to those above.
  7347. @itemize @bullet
  7348. @item
  7349. source code documentation and indentation according to GNU coding
  7350. standards (@url{http://www.gnu.org/prep/standards})
  7351. @item
  7352. sufficient stability for a semi-annual release cycle
  7353. @item
  7354. no run-time or compile-time dependence on any non-free software,
  7355. although dynamic loading and client/server interaction are acceptable
  7356. @item
  7357. portable or at least unbreakable configuration by appropriate use of
  7358. @cindex autoconf
  7359. @code{autoconf} macros and conditional defines
  7360. @item
  7361. little or no state dependence at the level of the virtual code
  7362. @cindex state dependence
  7363. interface (i.e., pure functions or something like them, except for
  7364. @cindex random number generators
  7365. random number generators and related applications)
  7366. @item
  7367. adequate documentation for a section in @ref{External Libraries}
  7368. @end itemize
  7369. @node Working around library misfeatures, , Implementing new library functions, External Library Maintenance
  7370. @subsection Working around library misfeatures
  7371. As mentioned already (@ref{Implementing new library functions}), some
  7372. common problems with external libraries that are worthwhile in other
  7373. respects are that they may generate unwelcome console output while
  7374. running, they may follow ill defined memory management policies, and
  7375. they may handle exceptions just by crashing themselves along with the
  7376. client module.
  7377. An accumulation of techniques for coping with these issues (short of
  7378. modifying the library source) has been collected into the API and made
  7379. available by way of the header file @file{mwrap.h}. This section
  7380. briefly documents how they might be put to use.
  7381. @menu
  7382. * Inept excess verbiage::
  7383. * Memory leaks::
  7384. * Suicidal exception handling::
  7385. @end menu
  7386. @node Inept excess verbiage, Memory leaks, Working around library misfeatures, Working around library misfeatures
  7387. @subsubsection Inept excess verbiage
  7388. Although the author of a library function may take pride in putting
  7389. its activities on display, it should be assumed that virtual code
  7390. applications running on @code{avram} have other agendas for the
  7391. console, so the library interface module should prevent direct output
  7392. from the external library.
  7393. More thoughtful API's may have a verbosity setting, which should be
  7394. @cindex verbosity setting
  7395. used in preference to this workaround, but failing that, it is easy to
  7396. dispense with console output generated by calls to external library
  7397. functions by using some combination of the following functions.
  7398. @deftypefun void avm_turn_off_stdout ()
  7399. Calling this function will suppress all output to the standard output
  7400. stream until the next time @code{avm_turn_on_stdout} is called.
  7401. Additional calls to this function without intervening calls to
  7402. @code{avm_turn_on_stdout} may be made safely with no effect. The
  7403. standard output stream is flushed as a side effect of calling this
  7404. function.
  7405. @end deftypefun
  7406. @deftypefun void avm_turn_on_stdout ()
  7407. Calling this function will allow output to the standard output
  7408. stream to resume if it has been suppressed previously by a call to
  7409. @code{avm_turn_off_stdout}. If @code{avm_turn_off_stdout} has not been
  7410. previously called, this function has no effect. Any output that would
  7411. have been sent to @code{stdout} during the time it was turned off will
  7412. be lost.
  7413. @end deftypefun
  7414. @deftypefun void avm_turn_off_stderr ()
  7415. This function performs a similar service to that of
  7416. @code{avm_turn_off_stdout} but pertains to the standard error stream.
  7417. The standard error and the standard output streams are controlled
  7418. independently even if both of them are piped to the same console.
  7419. @end deftypefun
  7420. @deftypefun void avm_turn_on_stderr ()
  7421. This function performs a similar service to that of
  7422. @code{avm_turn_on_stdout} but pertains to the standard error stream.
  7423. @end deftypefun
  7424. As an example, the following code fragment will prevent any output to
  7425. standard output taking place as a side effect of @code{blather}, but
  7426. will allow error messages to standard error. Note that ouput should
  7427. not be left permanently turned off.
  7428. @example
  7429. ...
  7430. #include <avm/mwrap.h>
  7431. ...
  7432. x = y + z;
  7433. avm_turn_off_stdout ();
  7434. w = blather (foo, bar, baz);
  7435. avm_turn_on_stdout ();
  7436. return w;
  7437. ...
  7438. @end example
  7439. One possible issue with these functions is that they rely on a
  7440. feature of the GNU C library that might not be portable to non-GNU
  7441. @cindex portability
  7442. systems and has not been widely tested on other platforms.
  7443. Another issue is that a library function could be both careless enough
  7444. to clutter the console unconditionally and meticulous enough to check
  7445. for I/O errors after each attempted write. Writing while the output
  7446. stream is disabled will return an I/O error to the caller (i.e., to
  7447. the verbose library function) for appropriate action, which could
  7448. include terminating the process.
  7449. @node Memory leaks, Suicidal exception handling, Inept excess verbiage, Working around library misfeatures
  7450. @subsubsection Memory leaks
  7451. Incorrect memory management may undermine confidence in a library when
  7452. one wonders what else it gets wrong, but if the worst it does is leave
  7453. a few bytes unreclaimed, then help is at hand.
  7454. The first priority is to assess the seriousness of the situation.
  7455. Similarly to the way library functions are bracketed with calls to
  7456. those listed in @ref{Inept excess verbiage}, the following functions
  7457. are meant to be placed before and after a call to a library function
  7458. either for diagnostic purposes or production use.
  7459. @deftypefun void avm_manage_memory ()
  7460. After this function is called, all subsequent calls to the standard C
  7461. functions @code{malloc}, @code{free}, and @code{realloc} are
  7462. intercepted and logged until the next time
  7463. @code{avm_dont_manage_memory} is called. Furthermore, a complete
  7464. record is maintained of the addresses and sizes of all allocated areas
  7465. of memory during this time in a persistent data structure managed
  7466. internally.
  7467. @end deftypefun
  7468. @deftypefun void avm_dont_manage_memory ()
  7469. Calling this function suspends the storage monitoring activities
  7470. initiated by calling @code{avm_manage_memory}, but the record of
  7471. allocated memory areas is not erased.
  7472. @end deftypefun
  7473. @deftypefun void avm_debug_memory ()
  7474. After this function is called and @code{avm_manage_memory} is also
  7475. called, the standard output stream will display a running account of
  7476. the sizes and addresses of all memory allocations or deallocations as
  7477. they occur until the next call to either @code{avm_dont_debug_memory}
  7478. or @code{avm_dont_manage_memory}.
  7479. @end deftypefun
  7480. @deftypefun void avm_dont_debug_memory ()
  7481. This function stops the output being sent to @code{stdout} caused by
  7482. @code{avm_debug_memory}, if any, but has no effect on the logging of
  7483. memory management events preformed due to @code{avm_manage_memory}.
  7484. @end deftypefun
  7485. While the latter two are not useful in production code, they can help
  7486. to clarify an inadequately documented API during development by
  7487. experimentally identifying the functions that cause memory to be
  7488. allocated. They can also provide the answer to questions like whether
  7489. separate copies are made from arrays passed to functions (useful for
  7490. knowing when it's appropriate to free them).
  7491. Although the console output reveals everything there is to know about
  7492. memory management during the selected window, the question of
  7493. unreclaimed storage is more directly settled by the following
  7494. functions.
  7495. @deftypefun void avm_initialize_mwrap ()
  7496. This function has to be called before any other functions from
  7497. @file{mwrap.h} in order to clean the slate and prepare the static data
  7498. structures for use. This function might not have to be called
  7499. explicitly if the client module is part of @code{avram}, whose main
  7500. program would have already called it. There is no harm in calling it
  7501. repeatedly.
  7502. @end deftypefun
  7503. @deftypefun void avm_count_mwrap ()
  7504. This function should be called after the last call to any other
  7505. functions in @file{mwrap.h}, when it is expected that all storage that
  7506. was allocated while @code{avm_manage_memory} was in effect should have
  7507. been reclaimed.
  7508. If there is no unreclaimed storage allocated during an interval when
  7509. memory was being managed, this function returns uneventfully. However,
  7510. if any storage remains unreclaimed, a message stating the number of
  7511. bytes is written to @code{stderr}.
  7512. If @code{avm_debug_memory} is also in effect when this function
  7513. detects unreclaimed storage, an itemized list of the unreclaimed
  7514. memory addresses and their sizes is written to standard output.
  7515. @end deftypefun
  7516. Of course, in order for @code{avm_count_mwrap} to report meaningful
  7517. results, any memory that is allocated during the interval between
  7518. calls to @code{avm_manage_memory} and @code{avm_dont_manage_memory}
  7519. must have been given an opportunity to be reclaimed also while
  7520. this logging mechanism is in effect. However, there may be arbitrarily
  7521. many intervening intervals during which it is suspended.
  7522. On the other hand, any storage that is allocated when memory is not
  7523. being managed must not be freed at a time when it is (except for
  7524. freeing a @code{NULL} pointer, which is tolerated but not
  7525. encouraged). Doing so raises an internal error, causing termination
  7526. @cindex internal error
  7527. with extreme prejudice. This behavior is a precaution against library
  7528. functions freeing storage that they didn't allocate, which would mean
  7529. no memory is safe and it's better for @code{avram} not to continue.
  7530. If these investigations uncover no evidence of a memory leak, then
  7531. perhaps the relevant library functions are reliable enough to run
  7532. without supervisory memory management. Alternatively, when memory
  7533. leaks are indicated, the next function provides a simple remedy.
  7534. @deftypefun void avm_free_managed_memory ()
  7535. This function causes all storage to be reclaimed that was allocated
  7536. at any time while logging of memory allocation was in effect (i.e.,
  7537. whenever @code{avm_manage_memory} had been called more recently than
  7538. @code{avm_dont_manage_memory}). When the storage is freed, no further
  7539. record of it is maintained.
  7540. A side effect of this function is to call @code{avm_dont_manage_memory}
  7541. and therefore leave memory management turned off.
  7542. @end deftypefun
  7543. This last function when used in conjunction with the others is
  7544. therefore the workaround for library functions that don't clean up
  7545. after themselves. It may be important to do it for them if repeated
  7546. calls to the library function are expected, which would otherwise
  7547. cause unreclaimed storage to accumulate until it curtailed other
  7548. operations.
  7549. One small issue with this function is the assumption that unreclaimed
  7550. storage is really a leak and not internal library data that is
  7551. designed to persist between calls. If this assumption is not valid,
  7552. breakage will occur. However, libraries deliberately making use of
  7553. persistent data are likely to have initialization and destructor
  7554. functions as part of their API's, so this assumption is often
  7555. justified if they don't.
  7556. An example of using these functions is given below.
  7557. In this example, @code{allocated_library_object} is a hypothetical
  7558. function exported by an external library that causes storage to be
  7559. allocated, and @code{library_reclamation_routine} is provided by the
  7560. same library ostensibly to reclaim the storage thus
  7561. allocated. However, the latter is suspected of memory leaks.
  7562. The variable @code{my_data} is declared and used by an @code{avram}
  7563. developer who is presumably competent to reclaim it correctly, rather
  7564. than it being part of an external library. Memory management is
  7565. therefore enabled during the calls to the library routines but not at
  7566. other times.
  7567. The call to @code{avm_count_mwrap} is redundant immediately after a
  7568. call to @code{avm_free_managed_memory}, because with all managed
  7569. memory having been freed, no memory leak will ever be detected, but it
  7570. is included for illustrative purposes.
  7571. @example
  7572. #include <avm/mwrap.h>
  7573. ...
  7574. @{
  7575. void *behemoth;
  7576. char *my_data;
  7577. avm_initialize_mwrap ();
  7578. avm_manage_memory ();
  7579. behemoth = allocated_library_object (foo, bar);
  7580. avm_dont_manage_memory ();
  7581. my_data = (char *) malloc (100);
  7582. ...
  7583. free (my_data);
  7584. avm_manage_memory ();
  7585. library_reclamation_routine (&behemoth);
  7586. avm_free_managed_memory ();
  7587. avm_count_mwrap ();
  7588. return;
  7589. @}
  7590. @end example
  7591. It might be a cleaner solution in some sense to omit the call to
  7592. @code{library_reclamation_routine} entirely, because the storage
  7593. allocated during the call to @code{allocated_library_object} will be
  7594. reclaimed perfectly well by @code{avm_free_managed_memory} without
  7595. it. Doing so may also be the only option if the library reclamation
  7596. routine is either extremely unreliable or non-existent. However, the
  7597. style above is to be preferred for portability if possible. The memory
  7598. management functions rely on the availability of the system header
  7599. file @code{malloc.h}, and GNU C library features whose portability is
  7600. not assured. If the required features are not detected on the host
  7601. system at configuration time, conditional directives in the
  7602. @code{avram} source will make the @code{avm_}* memory management
  7603. functions perform no operations, and the responsibility for memory
  7604. management will devolve to the possibly less robust external library
  7605. implementation.
  7606. @node Suicidal exception handling, , Memory leaks, Working around library misfeatures
  7607. @subsubsection Suicidal exception handling
  7608. An inconvenient characteristic of some external library functions is
  7609. to terminate the program rather than returning an error status to the
  7610. caller for routine events such as a failure of memory allocation.
  7611. Although in many cases there is no simple workaround for this
  7612. behavior, memory allocation failures at least can be detected and
  7613. preventive action taken by using the functions described in this
  7614. section.
  7615. The general approach is to use memory management functions from
  7616. @file{mwrap.h} as described previously (@ref{Memory leaks}), while
  7617. additionally registering a return destination for a non-local jump to
  7618. @cindex non-local jumps
  7619. be taken in the event of a memory overflow. The jump is taken when an
  7620. external library function calls @code{malloc} or @code{realloc}
  7621. unsuccessfully. The jump avoids passing control back to the library
  7622. function, thereby denying it the opportunity to abort, but restores
  7623. the context to that of the jump destination almost as if the library
  7624. function and all of its intervening callers had returned normally.
  7625. The interface is similar to that of the standard @code{setjmp}
  7626. @cindex setjmp
  7627. function defined in the system header file @code{setjmp.h}, and in
  7628. fact is built on it, but differs in that the client module does not
  7629. explicitly refer to jump buffers. Instead, the @code{mwrap} module
  7630. internally maintains a stack of return destinations.
  7631. If a jump is taken, it always goes to the most recently registered
  7632. destination. It may revert to the previously registered destination
  7633. only when the current one is cleared. This organization provides the
  7634. necessary flexibility for multiple clients and recursion, but it
  7635. necessitates a protocol whereby each registration of a destination
  7636. must be explicitly cleared exactly once.
  7637. The following functions implement these two features.
  7638. @deftypefun int avm_setjmp ()
  7639. This function specifies the point to which control will pass by a
  7640. non-local jump if there is insufficient memory to complete a
  7641. subsequent @code{malloc} or @code{realloc} operation. Only the
  7642. operations that take place while memory is being managed due to
  7643. @code{avm_manage_memory} are affected (@ref{Memory leaks}).
  7644. The function returns zero when it is called normally and successfully
  7645. registers the return point.
  7646. It returns a non-zero value when it has been entered by a non-local
  7647. jump (i.e., when @code{malloc} or @code{realloc} has reported
  7648. insufficient memory while memory management is active), or when the
  7649. return point could not be successfully registered due to insufficient
  7650. memory. The client need not distinguish between these two cases,
  7651. because both correspond to memory overflows and the destination must
  7652. be cleared by @code{avm_clearjmp} regardless.
  7653. When a non-zero value is returned due to this function being reached
  7654. by a non-local jump, it has the side effects of reclaiming all managed
  7655. memory by calling @code{avm_free_managed_memory} and disabling memory
  7656. management by calling @code{avm_dont_manage_memory}.
  7657. @end deftypefun
  7658. @deftypefun void avm_clearjmp ()
  7659. This function cancels the effect of @code{avm_setjmp ()} by preventing
  7660. further non-local jumps to its destination if the destination was
  7661. successfully registered, or by acknowledging unsuccessful registration
  7662. otherwise. It should be called before exiting any function that calls
  7663. @code{avm_setjmp ()} or anomalous results may ensue.
  7664. @end deftypefun
  7665. The memory management functions @code{avm_manage_memory} and
  7666. @code{avm_dont_manage_memory} can be useful with or without
  7667. @code{avm_setjmp}, depending on how much of a workaround is needed for
  7668. a given library. If a library does not abort on memory overflows,
  7669. there is no need to use @code{avm_setjmp}, while it may still be
  7670. appropriate to use the other functions against memory leaks.
  7671. Calling @code{avm_clearjmp} is particularly important if a client
  7672. module with memory management that doesn't use @code{avm_setjmp} is
  7673. invoked subsequently to one that does, so that memory overflows in the
  7674. latter won't cause an attempted jump to a stale destination.
  7675. A further complication that arises from careful consideration of these
  7676. issues is the situation of a client module that does not intend to use
  7677. @code{avm_setjmp} but is called (perhaps indirectly) by one that
  7678. does. The latter will have registered a return destination that
  7679. remains active and valid even if the former refrains from doing so,
  7680. thereby allowing a branch to be taken that should have been prevented.
  7681. Although it is an unusual situation, it can be accommodated by the
  7682. following function.
  7683. @deftypefun void avm_setnonjump ()
  7684. This function temporarily inhibits non-local jumps to destinations
  7685. previously registered by @code{avm_setjmp} until the next time
  7686. @code{avm_clearjmp} is called. Thereafter, any previously registered
  7687. destinations are reinstated.
  7688. @end deftypefun
  7689. A sketch of how some of these functions might be used to cope with
  7690. library functions that would otherwise terminate the program in the
  7691. event of a memory overflow is shown below. The GNU @code{libc}
  7692. @cindex non-local jumps
  7693. reference manual contains a related discussion of non-local jumps.
  7694. @example
  7695. #include <avm/mwrap.h>
  7696. ...
  7697. int
  7698. function foobar (foo, bar)
  7699. ...
  7700. @{
  7701. char *my_data;
  7702. my_data = (char *) malloc (100);
  7703. if (avm_setjmp () != 0)
  7704. @{
  7705. avm_clearjmp ();
  7706. avm_turn_on_stdout (); /* reaching here */
  7707. free (my_data); /* means malloc */
  7708. return ABNORMAL_STATUS; /* failed below */
  7709. @}
  7710. avm_turn_off_stdout ();
  7711. avm_manage_memory ();
  7712. ...
  7713. call_library_functions (foo, bar); /* may jump */
  7714. ... /* to above */
  7715. avm_free_managed_memory ();
  7716. avm_turn_on_stdout ();
  7717. avm_clearjmp ();
  7718. free (my_data); /* reaching here means */
  7719. return OK_STATUS; /* jumping wasn't done */
  7720. @}
  7721. @end example
  7722. Portability issues with these functions are not well known at this
  7723. @cindex portability
  7724. writing. If the configuration script for @code{avram} fails to detect
  7725. the required features in @code{setjmp.h} on the host system,
  7726. conditional compilation directives will disable the functions
  7727. @code{avm_setjmp}, @code{avm_clearjmp}, and @code{avm_setnonjmp}.
  7728. However, it may still be possible for the other @code{avm_}* memory
  7729. management functions to be configured.
  7730. If @code{setjmp} is not configured, the @code{avm_setjmp} function
  7731. is still callable but will always return a value of zero, and will
  7732. provide no protection against external library functions aborting the
  7733. program. The other two will perform no operation and return.
  7734. @node Character Table, Reference Implementations, Library Reference, Top
  7735. @appendix Character Table
  7736. @cindex character representations
  7737. This table lists the representations used by @code{avram} for
  7738. characters. The left column shows the character code in decimal. For
  7739. printable characters, the middle column shows the character. The right
  7740. column shows the representation used. For example, the letter @code{A} has
  7741. character code 65, and the representation
  7742. @code{(nil,(((nil,(nil,(nil,nil))),nil),(nil,nil)))}.
  7743. These representations were generated automatically to meet various
  7744. helpful criteria, and are not expected to change in future releases. No
  7745. character representation coincides with the representations used for
  7746. boolean values, natural numbers, character strings, pairs of characters,
  7747. or certain other data types beyond the scope of this document. An easy
  7748. algorithm for lexical sorting is possible. Subject to these criteria,
  7749. the smallest possible trees were chosen.
  7750. @example
  7751. 0 (nil,(nil,(nil,((nil,nil),(nil,nil)))))
  7752. 1 (nil,(nil,((nil,nil),(nil,nil))))
  7753. 2 (nil,(nil,((nil,nil),(nil,(nil,nil)))))
  7754. 3 (nil,(nil,((nil,(nil,nil)),(nil,nil))))
  7755. 4 (nil,(nil,(((nil,nil),nil),(nil,nil))))
  7756. 5 (nil,(nil,(((nil,nil),(nil,nil)),nil)))
  7757. 6 (nil,(nil,((((nil,nil),(nil,nil)),nil),nil)))
  7758. 7 (nil,((nil,nil),(nil,nil)))
  7759. 8 (nil,((nil,nil),(nil,(nil,nil))))
  7760. 9 (nil,((nil,nil),(nil,(nil,(nil,nil)))))
  7761. 10 (nil,((nil,nil),(nil,(nil,(nil,(nil,nil))))))
  7762. 11 (nil,((nil,nil),(nil,((nil,nil),(nil,nil)))))
  7763. 12 (nil,((nil,nil),(nil,((nil,(nil,nil)),nil))))
  7764. 13 (nil,((nil,nil),(nil,(((nil,nil),nil),nil))))
  7765. 14 (nil,((nil,nil),((nil,nil),(nil,nil))))
  7766. 15 (nil,((nil,nil),((nil,nil),(nil,(nil,nil)))))
  7767. 16 (nil,((nil,nil),((nil,(nil,nil)),nil)))
  7768. 17 (nil,((nil,nil),((nil,(nil,nil)),(nil,nil))))
  7769. 18 (nil,((nil,nil),((nil,(nil,(nil,nil))),nil)))
  7770. 19 (nil,((nil,nil),(((nil,nil),nil),(nil,nil))))
  7771. 20 (nil,((nil,nil),(((nil,nil),(nil,nil)),nil)))
  7772. 21 (nil,((nil,(nil,nil)),(nil,nil)))
  7773. 22 (nil,((nil,(nil,nil)),(nil,(nil,nil))))
  7774. 23 (nil,((nil,(nil,nil)),(nil,(nil,(nil,nil)))))
  7775. 24 (nil,((nil,(nil,nil)),(nil,((nil,nil),nil))))
  7776. 25 (nil,((nil,(nil,nil)),((nil,nil),nil)))
  7777. 26 (nil,((nil,(nil,nil)),((nil,nil),(nil,nil))))
  7778. 27 (nil,((nil,(nil,nil)),((nil,(nil,nil)),nil)))
  7779. 28 (nil,((nil,(nil,nil)),(((nil,nil),nil),nil)))
  7780. 29 (nil,((nil,(nil,(nil,nil))),(nil,nil)))
  7781. 30 (nil,((nil,(nil,(nil,nil))),(nil,(nil,nil))))
  7782. 31 (nil,((nil,(nil,(nil,nil))),((nil,nil),nil)))
  7783. 32 (nil,((nil,(nil,(nil,(nil,nil)))),(nil,nil)))
  7784. 33 ! (nil,((nil,(nil,((nil,nil),nil))),(nil,nil)))
  7785. 34 " (nil,((nil,(nil,((nil,nil),(nil,nil)))),nil))
  7786. 35 # (nil,((nil,((nil,nil),nil)),(nil,nil)))
  7787. 36 $ (nil,((nil,((nil,nil),nil)),(nil,(nil,nil))))
  7788. 37 % (nil,((nil,((nil,nil),(nil,nil))),nil))
  7789. 38 & (nil,((nil,((nil,nil),(nil,nil))),(nil,nil)))
  7790. 39 ' (nil,((nil,((nil,nil),(nil,(nil,nil)))),nil))
  7791. 40 ( (nil,((nil,((nil,(nil,nil)),nil)),(nil,nil)))
  7792. 41 ) (nil,((nil,((nil,(nil,nil)),(nil,nil))),nil))
  7793. 42 * (nil,((nil,(((nil,nil),nil),nil)),(nil,nil)))
  7794. 43 + (nil,((nil,(((nil,nil),nil),(nil,nil))),nil))
  7795. 44 , (nil,((nil,(((nil,nil),(nil,nil)),nil)),nil))
  7796. 45 - (nil,(((nil,nil),nil),(nil,nil)))
  7797. 46 . (nil,(((nil,nil),nil),(nil,(nil,nil))))
  7798. 47 / (nil,(((nil,nil),nil),(nil,(nil,(nil,nil)))))
  7799. 48 0 (nil,(((nil,nil),nil),((nil,nil),(nil,nil))))
  7800. 49 1 (nil,(((nil,nil),nil),((nil,(nil,nil)),nil)))
  7801. 50 2 (nil,(((nil,nil),(nil,nil)),nil))
  7802. 51 3 (nil,(((nil,nil),(nil,nil)),(nil,nil)))
  7803. 52 4 (nil,(((nil,nil),(nil,nil)),(nil,(nil,nil))))
  7804. 53 5 (nil,(((nil,nil),(nil,nil)),((nil,nil),nil)))
  7805. 54 6 (nil,(((nil,nil),(nil,(nil,nil))),nil))
  7806. 55 7 (nil,(((nil,nil),(nil,(nil,nil))),(nil,nil)))
  7807. 56 8 (nil,(((nil,nil),(nil,(nil,(nil,nil)))),nil))
  7808. 57 9 (nil,(((nil,nil),((nil,nil),nil)),(nil,nil)))
  7809. 58 : (nil,(((nil,nil),((nil,nil),(nil,nil))),nil))
  7810. 59 ; (nil,(((nil,nil),((nil,(nil,nil)),nil)),nil))
  7811. 60 < (nil,(((nil,(nil,nil)),nil),(nil,nil)))
  7812. 61 = (nil,(((nil,(nil,nil)),nil),(nil,(nil,nil))))
  7813. 62 > (nil,(((nil,(nil,nil)),(nil,nil)),nil))
  7814. 63 ? (nil,(((nil,(nil,nil)),(nil,nil)),(nil,nil)))
  7815. 64 @@ (nil,(((nil,(nil,nil)),(nil,(nil,nil))),nil))
  7816. 65 A (nil,(((nil,(nil,(nil,nil))),nil),(nil,nil)))
  7817. 66 B (nil,(((nil,(nil,(nil,nil))),(nil,nil)),nil))
  7818. 67 C (nil,(((nil,((nil,nil),nil)),nil),(nil,nil)))
  7819. 68 D (nil,(((nil,((nil,nil),nil)),(nil,nil)),nil))
  7820. 69 E (nil,((((nil,nil),nil),nil),(nil,nil)))
  7821. 70 F (nil,((((nil,nil),nil),nil),(nil,(nil,nil))))
  7822. 71 G (nil,((((nil,nil),nil),(nil,nil)),nil))
  7823. 72 H (nil,((((nil,nil),nil),(nil,nil)),(nil,nil)))
  7824. 73 I (nil,((((nil,nil),nil),(nil,(nil,nil))),nil))
  7825. 74 J (nil,((((nil,nil),(nil,nil)),nil),(nil,nil)))
  7826. 75 K (nil,((((nil,nil),(nil,nil)),(nil,nil)),nil))
  7827. 76 L (nil,((((nil,(nil,nil)),nil),nil),(nil,nil)))
  7828. 77 M (nil,((((nil,(nil,nil)),nil),(nil,nil)),nil))
  7829. 78 N (nil,(((((nil,nil),nil),nil),nil),(nil,nil)))
  7830. 79 O (nil,(((((nil,nil),nil),nil),(nil,nil)),nil))
  7831. 80 P ((nil,nil),(nil,nil))
  7832. 81 Q ((nil,nil),(nil,(nil,nil)))
  7833. 82 R ((nil,nil),(nil,(nil,(nil,nil))))
  7834. 83 S ((nil,nil),(nil,(nil,(nil,(nil,nil)))))
  7835. 84 T ((nil,nil),(nil,(nil,(nil,(nil,(nil,nil))))))
  7836. 85 U ((nil,nil),(nil,(nil,((nil,(nil,nil)),nil))))
  7837. 86 V ((nil,nil),(nil,(nil,(((nil,nil),nil),nil))))
  7838. 87 W ((nil,nil),(nil,((nil,nil),(nil,nil))))
  7839. 88 X ((nil,nil),(nil,((nil,(nil,nil)),nil)))
  7840. 89 Y ((nil,nil),(nil,((nil,(nil,nil)),(nil,nil))))
  7841. 90 Z ((nil,nil),(nil,((nil,(nil,(nil,nil))),nil)))
  7842. 91 [ ((nil,nil),(nil,((nil,((nil,nil),nil)),nil)))
  7843. 92 \ ((nil,nil),(nil,(((nil,nil),nil),nil)))
  7844. 93 ] ((nil,nil),(nil,(((nil,nil),nil),(nil,nil))))
  7845. 94 ^ ((nil,nil),(nil,(((nil,nil),(nil,nil)),nil)))
  7846. 95 _ ((nil,nil),(nil,(((nil,(nil,nil)),nil),nil)))
  7847. 96 ` ((nil,nil),(nil,((((nil,nil),nil),nil),nil)))
  7848. 97 a ((nil,nil),((nil,nil),(nil,nil)))
  7849. 98 b ((nil,nil),((nil,nil),(nil,(nil,nil))))
  7850. 99 c ((nil,nil),((nil,nil),(nil,(nil,(nil,nil)))))
  7851. 100 d ((nil,nil),((nil,nil),((nil,nil),(nil,nil))))
  7852. 101 e ((nil,nil),((nil,nil),((nil,(nil,nil)),nil)))
  7853. 102 f ((nil,nil),((nil,(nil,nil)),nil))
  7854. 103 g ((nil,nil),((nil,(nil,nil)),(nil,nil)))
  7855. 104 h ((nil,nil),((nil,(nil,nil)),(nil,(nil,nil))))
  7856. 105 i ((nil,nil),((nil,(nil,nil)),((nil,nil),nil)))
  7857. 106 j ((nil,nil),((nil,(nil,(nil,nil))),nil))
  7858. 107 k ((nil,nil),((nil,(nil,(nil,nil))),(nil,nil)))
  7859. 108 l ((nil,nil),((nil,(nil,(nil,(nil,nil)))),nil))
  7860. 109 m ((nil,nil),((nil,((nil,nil),nil)),(nil,nil)))
  7861. 110 n ((nil,nil),((nil,((nil,nil),(nil,nil))),nil))
  7862. 111 o ((nil,nil),((nil,((nil,(nil,nil)),nil)),nil))
  7863. 112 p ((nil,nil),(((nil,nil),nil),(nil,nil)))
  7864. 113 q ((nil,nil),(((nil,nil),nil),(nil,(nil,nil))))
  7865. 114 r ((nil,nil),(((nil,nil),(nil,nil)),nil))
  7866. 115 s ((nil,nil),(((nil,nil),(nil,nil)),(nil,nil)))
  7867. 116 t ((nil,nil),(((nil,nil),(nil,(nil,nil))),nil))
  7868. 117 u ((nil,nil),(((nil,(nil,nil)),nil),(nil,nil)))
  7869. 118 v ((nil,nil),(((nil,(nil,nil)),(nil,nil)),nil))
  7870. 119 w ((nil,nil),((((nil,nil),nil),nil),(nil,nil)))
  7871. 120 x ((nil,nil),((((nil,nil),nil),(nil,nil)),nil))
  7872. 121 y ((nil,nil),(((((nil,nil),nil),nil),nil),nil))
  7873. 122 z ((nil,(nil,nil)),(nil,nil))
  7874. 123 @{ ((nil,(nil,nil)),(nil,(nil,(nil,nil))))
  7875. 124 | ((nil,(nil,nil)),(nil,(nil,(nil,(nil,nil)))))
  7876. 125 @} ((nil,(nil,nil)),(nil,((nil,nil),nil)))
  7877. 126 ~ ((nil,(nil,nil)),(nil,((nil,nil),(nil,nil))))
  7878. 127 ((nil,(nil,nil)),(nil,((nil,(nil,nil)),nil)))
  7879. 128 ((nil,(nil,nil)),((nil,nil),(nil,nil)))
  7880. 129 ((nil,(nil,nil)),((nil,nil),(nil,(nil,nil))))
  7881. 130 ((nil,(nil,nil)),((nil,(nil,nil)),nil))
  7882. 131 ((nil,(nil,nil)),((nil,(nil,nil)),(nil,nil)))
  7883. 132 ((nil,(nil,nil)),((nil,(nil,(nil,nil))),nil))
  7884. 133 ((nil,(nil,nil)),(((nil,nil),nil),(nil,nil)))
  7885. 134 ((nil,(nil,nil)),(((nil,nil),(nil,nil)),nil))
  7886. 135 ((nil,(nil,(nil,nil))),(nil,nil))
  7887. 136 ((nil,(nil,(nil,nil))),(nil,(nil,nil)))
  7888. 137 ((nil,(nil,(nil,nil))),(nil,(nil,(nil,nil))))
  7889. 138 ((nil,(nil,(nil,nil))),(nil,((nil,nil),nil)))
  7890. 139 ((nil,(nil,(nil,nil))),((nil,nil),(nil,nil)))
  7891. 140 ((nil,(nil,(nil,nil))),((nil,(nil,nil)),nil))
  7892. 141 ((nil,(nil,(nil,(nil,nil)))),(nil,nil))
  7893. 142 ((nil,(nil,(nil,(nil,nil)))),(nil,(nil,nil)))
  7894. 143 ((nil,(nil,(nil,(nil,nil)))),((nil,nil),nil))
  7895. 144 ((nil,(nil,(nil,(nil,(nil,nil))))),(nil,nil))
  7896. 145 ((nil,(nil,(nil,((nil,nil),nil)))),(nil,nil))
  7897. 146 ((nil,(nil,((nil,nil),nil))),(nil,nil))
  7898. 147 ((nil,(nil,((nil,nil),(nil,nil)))),(nil,nil))
  7899. 148 ((nil,(nil,((nil,(nil,nil)),nil))),(nil,nil))
  7900. 149 ((nil,(nil,(((nil,nil),nil),nil))),(nil,nil))
  7901. 150 ((nil,((nil,nil),nil)),(nil,nil))
  7902. 151 ((nil,((nil,nil),nil)),(nil,(nil,nil)))
  7903. 152 ((nil,((nil,nil),nil)),(nil,(nil,(nil,nil))))
  7904. 153 ((nil,((nil,nil),nil)),(nil,((nil,nil),nil)))
  7905. 154 ((nil,((nil,nil),nil)),((nil,nil),(nil,nil)))
  7906. 155 ((nil,((nil,nil),nil)),((nil,(nil,nil)),nil))
  7907. 156 ((nil,((nil,nil),(nil,nil))),(nil,nil))
  7908. 157 ((nil,((nil,nil),(nil,nil))),(nil,(nil,nil)))
  7909. 158 ((nil,((nil,nil),(nil,(nil,nil)))),(nil,nil))
  7910. 159 ((nil,((nil,nil),((nil,nil),nil))),(nil,nil))
  7911. 160 ((nil,((nil,(nil,nil)),nil)),(nil,nil))
  7912. 161 ((nil,((nil,(nil,nil)),nil)),(nil,(nil,nil)))
  7913. 162 ((nil,((nil,(nil,nil)),nil)),((nil,nil),nil))
  7914. 163 ((nil,((nil,(nil,nil)),(nil,nil))),(nil,nil))
  7915. 164 ((nil,((nil,(nil,(nil,nil))),nil)),(nil,nil))
  7916. 165 ((nil,((nil,((nil,nil),nil)),nil)),(nil,nil))
  7917. 166 ((nil,(((nil,nil),nil),nil)),(nil,nil))
  7918. 167 ((nil,(((nil,nil),nil),(nil,nil))),(nil,nil))
  7919. 168 ((nil,(((nil,nil),(nil,nil)),nil)),(nil,nil))
  7920. 169 ((nil,(((nil,(nil,nil)),nil),nil)),(nil,nil))
  7921. 170 ((nil,((((nil,nil),nil),nil),nil)),(nil,nil))
  7922. 171 (((nil,nil),nil),(nil,nil))
  7923. 172 (((nil,nil),nil),(nil,(nil,nil)))
  7924. 173 (((nil,nil),nil),(nil,(nil,(nil,nil))))
  7925. 174 (((nil,nil),nil),(nil,(nil,(nil,(nil,nil)))))
  7926. 175 (((nil,nil),nil),(nil,(nil,((nil,nil),nil))))
  7927. 176 (((nil,nil),nil),(nil,((nil,nil),nil)))
  7928. 177 (((nil,nil),nil),(nil,((nil,nil),(nil,nil))))
  7929. 178 (((nil,nil),nil),(nil,((nil,(nil,nil)),nil)))
  7930. 179 (((nil,nil),nil),(nil,(((nil,nil),nil),nil)))
  7931. 180 (((nil,nil),nil),((nil,nil),(nil,nil)))
  7932. 181 (((nil,nil),nil),((nil,nil),(nil,(nil,nil))))
  7933. 182 (((nil,nil),nil),((nil,(nil,nil)),nil))
  7934. 183 (((nil,nil),nil),((nil,(nil,nil)),(nil,nil)))
  7935. 184 (((nil,nil),nil),((nil,(nil,(nil,nil))),nil))
  7936. 185 (((nil,nil),nil),(((nil,nil),nil),(nil,nil)))
  7937. 186 (((nil,nil),nil),(((nil,nil),(nil,nil)),nil))
  7938. 187 (((nil,nil),(nil,nil)),(nil,nil))
  7939. 188 (((nil,nil),(nil,nil)),(nil,(nil,nil)))
  7940. 189 (((nil,nil),(nil,nil)),(nil,(nil,(nil,nil))))
  7941. 190 (((nil,nil),(nil,nil)),(nil,((nil,nil),nil)))
  7942. 191 (((nil,nil),(nil,nil)),((nil,(nil,nil)),nil))
  7943. 192 (((nil,nil),(nil,(nil,nil))),(nil,nil))
  7944. 193 (((nil,nil),(nil,(nil,nil))),(nil,(nil,nil)))
  7945. 194 (((nil,nil),(nil,(nil,(nil,nil)))),(nil,nil))
  7946. 195 (((nil,nil),(nil,((nil,nil),nil))),(nil,nil))
  7947. 196 (((nil,nil),((nil,nil),nil)),(nil,nil))
  7948. 197 (((nil,nil),((nil,nil),nil)),(nil,(nil,nil)))
  7949. 198 (((nil,nil),((nil,nil),(nil,nil))),(nil,nil))
  7950. 199 (((nil,nil),((nil,(nil,nil)),nil)),(nil,nil))
  7951. 200 (((nil,nil),(((nil,nil),nil),nil)),(nil,nil))
  7952. 201 (((nil,(nil,nil)),nil),(nil,nil))
  7953. 202 (((nil,(nil,nil)),nil),(nil,(nil,nil)))
  7954. 203 (((nil,(nil,nil)),nil),(nil,(nil,(nil,nil))))
  7955. 204 (((nil,(nil,nil)),nil),(nil,((nil,nil),nil)))
  7956. 205 (((nil,(nil,nil)),nil),((nil,nil),(nil,nil)))
  7957. 206 (((nil,(nil,nil)),nil),((nil,(nil,nil)),nil))
  7958. 207 (((nil,(nil,nil)),(nil,nil)),(nil,nil))
  7959. 208 (((nil,(nil,nil)),(nil,nil)),(nil,(nil,nil)))
  7960. 209 (((nil,(nil,nil)),(nil,(nil,nil))),(nil,nil))
  7961. 210 (((nil,(nil,nil)),((nil,nil),nil)),(nil,nil))
  7962. 211 (((nil,(nil,(nil,nil))),nil),(nil,nil))
  7963. 212 (((nil,(nil,(nil,nil))),nil),(nil,(nil,nil)))
  7964. 213 (((nil,(nil,(nil,nil))),nil),((nil,nil),nil))
  7965. 214 (((nil,(nil,(nil,nil))),(nil,nil)),(nil,nil))
  7966. 215 (((nil,(nil,(nil,(nil,nil)))),nil),(nil,nil))
  7967. 216 (((nil,(nil,((nil,nil),nil))),nil),(nil,nil))
  7968. 217 (((nil,((nil,nil),nil)),nil),(nil,nil))
  7969. 218 (((nil,((nil,nil),nil)),nil),(nil,(nil,nil)))
  7970. 219 (((nil,((nil,nil),nil)),nil),((nil,nil),nil))
  7971. 220 (((nil,((nil,nil),nil)),(nil,nil)),(nil,nil))
  7972. 221 (((nil,((nil,nil),(nil,nil))),nil),(nil,nil))
  7973. 222 (((nil,((nil,(nil,nil)),nil)),nil),(nil,nil))
  7974. 223 (((nil,(((nil,nil),nil),nil)),nil),(nil,nil))
  7975. 224 ((((nil,nil),nil),nil),(nil,nil))
  7976. 225 ((((nil,nil),nil),nil),(nil,(nil,nil)))
  7977. 226 ((((nil,nil),nil),nil),(nil,(nil,(nil,nil))))
  7978. 227 ((((nil,nil),nil),nil),(nil,((nil,nil),nil)))
  7979. 228 ((((nil,nil),nil),nil),((nil,nil),nil))
  7980. 229 ((((nil,nil),nil),nil),((nil,nil),(nil,nil)))
  7981. 230 ((((nil,nil),nil),nil),((nil,(nil,nil)),nil))
  7982. 231 ((((nil,nil),nil),nil),(((nil,nil),nil),nil))
  7983. 232 ((((nil,nil),nil),(nil,nil)),(nil,nil))
  7984. 233 ((((nil,nil),nil),(nil,nil)),(nil,(nil,nil)))
  7985. 234 ((((nil,nil),nil),(nil,(nil,nil))),(nil,nil))
  7986. 235 ((((nil,nil),nil),((nil,nil),nil)),(nil,nil))
  7987. 236 ((((nil,nil),(nil,nil)),nil),(nil,nil))
  7988. 237 ((((nil,nil),(nil,nil)),nil),(nil,(nil,nil)))
  7989. 238 ((((nil,nil),(nil,nil)),(nil,nil)),(nil,nil))
  7990. 239 ((((nil,nil),(nil,(nil,nil))),nil),(nil,nil))
  7991. 240 ((((nil,nil),((nil,nil),nil)),nil),(nil,nil))
  7992. 241 ((((nil,(nil,nil)),nil),nil),(nil,nil))
  7993. 242 ((((nil,(nil,nil)),nil),nil),(nil,(nil,nil)))
  7994. 243 ((((nil,(nil,nil)),nil),nil),((nil,nil),nil))
  7995. 244 ((((nil,(nil,nil)),nil),(nil,nil)),(nil,nil))
  7996. 245 ((((nil,(nil,nil)),(nil,nil)),nil),(nil,nil))
  7997. 246 ((((nil,(nil,(nil,nil))),nil),nil),(nil,nil))
  7998. 247 ((((nil,((nil,nil),nil)),nil),nil),(nil,nil))
  7999. 248 (((((nil,nil),nil),nil),nil),(nil,nil))
  8000. 249 (((((nil,nil),nil),nil),nil),(nil,(nil,nil)))
  8001. 250 (((((nil,nil),nil),nil),nil),((nil,nil),nil))
  8002. 251 (((((nil,nil),nil),nil),(nil,nil)),(nil,nil))
  8003. 252 (((((nil,nil),nil),(nil,nil)),nil),(nil,nil))
  8004. 253 (((((nil,nil),(nil,nil)),nil),nil),(nil,nil))
  8005. 254 (((((nil,(nil,nil)),nil),nil),nil),(nil,nil))
  8006. 255 ((((((nil,nil),nil),nil),nil),nil),(nil,nil))
  8007. @end example
  8008. @node Reference Implementations, Changes, Character Table, Top
  8009. @appendix Reference Implementations
  8010. This appendix contains some @code{silly} source code for several
  8011. functions that are mentioned in @ref{Virtual Code Semantics}, for specifying the virtual
  8012. machine code semantics, namely @code{pairwise}, @code{transition},
  8013. @code{insert} and @code{replace}.
  8014. The intention is to specify the virtual machine mathematically with a
  8015. minimum of hand waving, by using only simple equations and small
  8016. fragments of @code{silly} code, which has a straightforward semantics.
  8017. However, the @code{silly} code fragments are more significant in some
  8018. cases than what could fit into a few lines or be mechanically derived
  8019. from an equation.
  8020. The purpose of this appendix is therefore to avoid leaving any gaps in
  8021. the construction by demonstrating that everything mentioned can be done. None
  8022. of this code is needed for any practical purpose, because its
  8023. functionality is inherent in the virtual machine, but it shows how
  8024. certain operations would be specified if they were not built in.
  8025. @menu
  8026. * Pairwise::
  8027. * Insert::
  8028. * Replace::
  8029. * Transition::
  8030. @end menu
  8031. @node Pairwise, Insert, Reference Implementations, Reference Implementations
  8032. @section Pairwise
  8033. @cindex @code{pairwise}
  8034. This @code{silly} code fragment is mentioned in @ref{Reduce}, in the
  8035. discussion of @code{reduce}, and is provided as an example of a solution
  8036. to equations @emph{E1} to @emph{E3}. It is written in the style of a
  8037. higher order function, in that it takes a function @code{@var{f}} as an
  8038. argument and returns another function, [[@code{pairwise}]]
  8039. @code{@var{f}} as a result.
  8040. @example
  8041. self = left
  8042. argument = right
  8043. head = left
  8044. tail = right
  8045. pairwise =
  8046. compose(
  8047. refer,
  8048. compose(
  8049. bu(
  8050. conditional,
  8051. conditional(argument,compose(tail,argument),constant nil)),
  8052. couple(
  8053. (hired couple)(
  8054. (hired compose)(
  8055. identity,
  8056. constant (hired fan head)(
  8057. argument,
  8058. compose(tail,argument))),
  8059. constant (hired meta)(
  8060. self,
  8061. compose(tail,compose(tail,argument)))),
  8062. constant argument)))
  8063. @end example
  8064. @noindent
  8065. To see how this works, one should evaluate it symbolically with an
  8066. unknown @code{@var{f}}, which will result in some @code{silly}
  8067. pseudocode, and then evaluate that symbolically with some sample lists.
  8068. @node Insert, Replace, Pairwise, Reference Implementations
  8069. @section Insert
  8070. @cindex @code{insert}
  8071. This function is mentioned in @ref{Sort}, on sorting. It takes the
  8072. virtual code for a partial order relational operator and returns the
  8073. code for a function of two arguments. The left argument is a list item
  8074. and the right argument is a list of items of the same type, which is
  8075. already sorted with respect to the relational operator given as the
  8076. argument to @code{insert}. The result of the function returned by
  8077. @code{insert} is a list similar to its right argument but with the left
  8078. argument inserted in the proper position to maintain the order.
  8079. This code makes use of the @code{self}, @code{argument}, @code{head}
  8080. and @code{tail} declarations associated with @code{pairwise}.
  8081. @example
  8082. insert =
  8083. bu(compose,refer) (hired conditional)(
  8084. constant compose(right,argument),
  8085. couple(
  8086. (hired conditional)(
  8087. (hired compose)(
  8088. identity,
  8089. constant compose(
  8090. couple(left,compose(head,right)),
  8091. argument)),
  8092. constant (
  8093. argument,
  8094. couple(
  8095. compose(head,compose(right,argument)),
  8096. (hired meta)(
  8097. self,
  8098. couple(
  8099. compose(left,argument),
  8100. compose(tail,compose(right,argument))))))),
  8101. constant argument))
  8102. @end example
  8103. As with the other higher order functions in this appendix, the only
  8104. feasible ways to verify it would be either by formal proof or by some
  8105. form of symbolic interpretation.
  8106. @node Replace, Transition, Insert, Reference Implementations
  8107. @section Replace
  8108. @cindex @code{replace}
  8109. This code is needed in the discussion of assignment in @ref{Assignment}.
  8110. where it serves as a solution to equation @emph{E0}. The idea is that
  8111. the function takes an argument of the form
  8112. @code{((@var{locations},@var{values}),@var{store})} and returns the
  8113. store with the values stored at the locations indicated.
  8114. @example
  8115. locations = compose(left,compose(left,argument))
  8116. values = compose(right,compose(left,argument))
  8117. store = compose(right,argument)
  8118. replace =
  8119. refer conditional(
  8120. store,
  8121. (
  8122. conditional(
  8123. compose(left,locations),
  8124. (
  8125. conditional(
  8126. compose(right,locations),
  8127. (
  8128. (hired meta)(
  8129. self,
  8130. couple(
  8131. (hired fan right)(locations,values),
  8132. (hired meta)(
  8133. self,
  8134. couple(
  8135. (hired fan left)(locations,values),
  8136. store)))),
  8137. couple(
  8138. (hired meta)(
  8139. self,
  8140. couple(
  8141. couple(compose(left,locations),values),
  8142. compose(left,store))),
  8143. compose(right,store)))),
  8144. conditional(
  8145. compose(right,locations),
  8146. (
  8147. couple(
  8148. compose(left,store),
  8149. (hired meta)(
  8150. self,
  8151. couple(
  8152. couple(compose(right,locations),values),
  8153. compose(right,store)))),
  8154. values)))),
  8155. (hired meta)(
  8156. self,
  8157. couple(couple(locations,values),constant (nil,nil)))))
  8158. @end example
  8159. @node Transition, , Replace, Reference Implementations
  8160. @section Transition
  8161. This code is relevant to the discussion of @code{transfer} in @ref{Transfer},
  8162. where its specification is described in detail. When this code
  8163. is evaluated on a virtual code application @code{@var{f}}, the result is
  8164. the code for a transition function that takes one configuration to the
  8165. next in the course of evaluating a transfer function, as specified in
  8166. equations @emph{E7} to @emph{E9}.
  8167. @cindex @code{transition}
  8168. @example
  8169. output_buffer = compose(left,argument)
  8170. input_buffer = compose(right,compose(right,argument))
  8171. active = compose(left,compose(right,argument))
  8172. state = compose(left,active)
  8173. output = compose(right,active)
  8174. transition =
  8175. bu(compose,refer) (hired bu(conditional,active))(
  8176. (hired conditional)(
  8177. constant input_buffer,
  8178. bu(compose,(fan bu(hired meta,self))) (hired apply)(
  8179. constant fan bu(couple,couple(output,output_buffer)),
  8180. couple (fan bu(compose,couple))(
  8181. couple(
  8182. (hired apply)(
  8183. hired,
  8184. constant (state,compose(head,input_buffer))),
  8185. constant compose(tail,input_buffer)),
  8186. couple(
  8187. (hired apply)(hired,constant(state,constant nil)),
  8188. constant constant nil)))),
  8189. constant compose(flat,compose(reverse,output_buffer)))
  8190. @end example
  8191. @node Changes, External Libraries, Reference Implementations, Top
  8192. @appendix Changes
  8193. This section is reserved for brief updates due to changes in the
  8194. software that may be important enough to note temporarily until more
  8195. thorough revisions to the document can be made.
  8196. The lack of content here indicates that the current version is either
  8197. completely up to date or in such a sorry state of neglect that even
  8198. this section is obsolete.
  8199. @node External Libraries, Copying, Changes, Top
  8200. @appendix External Libraries
  8201. Various functions are callable from virtual code applications by way
  8202. of the @code{library} combinator as explained in @ref{Library
  8203. combinator}. An expression (shown in @code{silly} syntax) of the form
  8204. @code{library('foo','bar') x} applies a function named @code{'bar'}
  8205. from a library named @code{'foo'} to an argument @code{x}.
  8206. A brief overview of the libraries and functions can always be had by
  8207. executing
  8208. @example
  8209. $ avram --external-libraries
  8210. @end example
  8211. @noindent
  8212. The listing displayed by this command may show some that are not
  8213. included here if this version of the documentation is not current or
  8214. your installation has been locally enhanced. It may also lack some
  8215. that are documented here if your installation is not fully equipped.
  8216. Although the overview from the command line is adequate for a
  8217. reminder, it is not informative enough to explain how each function
  8218. should be used. The purpose of this section is to provide this
  8219. information in greater detail.
  8220. Some general comments are applicable to all libraries.
  8221. Each library documented in this section can generate error messages in
  8222. the event of exceptional conditions, that are documented individually.
  8223. In addition to those, it's also possible for any library function to
  8224. return error messages of
  8225. @cindex unrecognized library
  8226. @cindex unrecognized function name
  8227. @example
  8228. <'unrecognized library'>
  8229. <'unrecognized @var{xxxx} function name'>
  8230. @end example
  8231. @noindent
  8232. where @var{xxxx} is the name of a library. These indicate either that
  8233. the library name is invalid, or the library name is valid but the
  8234. function name is invalid, or that they're both valid but the library
  8235. wasn't detected on the host when @code{avram} was compiled. A virtual
  8236. code application can always avoid these errors by testing for the
  8237. availability of a function using the @code{have} combinator (@ref{Have
  8238. combinator}).
  8239. In addition, any library function that operates on numerical values or
  8240. lists thereof can return these messages in cases of invalid input.
  8241. @cindex missing value
  8242. @cindex invalid value
  8243. @cindex bad vector specification
  8244. @cindex bad matrix specification
  8245. @example
  8246. <'missing value'>
  8247. <'invalid value'>
  8248. <'bad vector specification'>
  8249. <'bad matrix specification'>
  8250. @end example
  8251. @noindent
  8252. These messages indicate that an input parameter that was required to
  8253. be a valid representation of a floating point number, a vector,
  8254. or a matrix was something other than that (@ref{Type Conversions}).
  8255. The last could also occur if a parameter that is required to be
  8256. a square matrix has unequal numbers of rows and columns.
  8257. @menu
  8258. * bes:: Bessel functions
  8259. * complex:: native complex arithmetic
  8260. * fftw:: fast Fourier transforms
  8261. * glpk:: simplex linear programming
  8262. * gsldif:: numerical differentiation
  8263. * gslevu:: series acceleration
  8264. * gslint:: numerical integration
  8265. * harminv:: harmonic inversion
  8266. * kinsol:: constrained non-linear optimization
  8267. * lapack:: linear algebra
  8268. * math:: native floating point arithmetic
  8269. * mtwist:: random number generation
  8270. * minpack:: non-linear optimization
  8271. * mpfr:: arbitrary precision arithmetic
  8272. * lpsolve:: mixed integer programming
  8273. * rmath:: statistical and special functions
  8274. * umf:: sparse matrices
  8275. @end menu
  8276. @node bes, complex, External Libraries, External Libraries
  8277. @section @code{bes}
  8278. An interface to the Bessel functions as defined in the GNU Scientific
  8279. Library (gsl) is available to virtual code applications by invoking a
  8280. function of the form
  8281. @example
  8282. library('bes',f)
  8283. @end example
  8284. @noindent
  8285. where f is a character string identifying the Bessel function family.
  8286. All functions in this library return a floating point number encoded
  8287. as in @ref{math}.
  8288. @menu
  8289. * Bessel function calling conventions::
  8290. * Bessel function errors::
  8291. @end menu
  8292. @node Bessel function calling conventions, Bessel function errors, bes, bes
  8293. @subsection Bessel function calling conventions
  8294. @cindex bessel functions
  8295. The virtual code interface simplifies the gsl C language API by
  8296. excluding the facilities for error estimates, omitting certain array
  8297. valued functions, and subsuming sets of related functions
  8298. within common ones where possible.
  8299. The functions with names in the following group take an argument of
  8300. the form @code{(n,x)}, where @code{n} identifies the member of the
  8301. function family, and @code{x} is the argument to the function.
  8302. @itemize @bullet
  8303. @item @code{J}
  8304. regular cylindrical Bessel functions
  8305. @item @code{Y}
  8306. irregular cylindrical Bessel functions
  8307. @item @code{I}
  8308. regular modified cylindrical Bessel functions
  8309. @item @code{K}
  8310. irregular modified cylindrical Bessel functions
  8311. @end itemize
  8312. For these functions, @code{n} can be either a natural number encoded
  8313. as in @ref{Representation of Numeric and Textual Data}, or a floating
  8314. point number encoded as in @ref{math}. The latter case specifies
  8315. functions of a fractional order. The relevant gsl function is called
  8316. based on the value and type of the parameter.
  8317. Two further related families of functions follow the same calling
  8318. convention.
  8319. @itemize @bullet
  8320. @item @code{Isc}
  8321. scaled regular modified cylindrical Bessel functions
  8322. @item @code{Ksc}
  8323. scaled irregular modified cylindrical Bessel functions
  8324. @end itemize
  8325. @noindent
  8326. The foregoing functions are related to those above by an exponential scale
  8327. factor as documented in the gsl reference manual.
  8328. Functions with names in the following group also take an argument of
  8329. the form @code{(n,x)}, but are not defined for fractional orders and
  8330. so require a natural number for @code{n}.
  8331. @itemize @bullet
  8332. @item @code{j}
  8333. regular spherical Bessel functions
  8334. @item @code{y}
  8335. irregular spherical Bessel functions
  8336. @item @code{isc}
  8337. regular modified spherical Bessel functions
  8338. @item @code{ksc}
  8339. irregular modified spherical Bessel functions
  8340. @end itemize
  8341. The functions in the remaining group follow idiosyncratic calling
  8342. conventions.
  8343. @itemize @bullet
  8344. @item @code{zJ0}, @code{zJ1}
  8345. These take a natural number @code{n} and return the @code{n}th root of
  8346. the regular cylindrical Bessel functions of order 0 or 1,
  8347. respectively.
  8348. @item @code{zJnu}
  8349. This takes a pair @code{(nu,n)} where @code{nu} is the (fractional)
  8350. order of a regular cylindrical Bessel function, @code{n} is a natural
  8351. number. It returns the @code{n}th zero of the function.
  8352. @item @code{lnKnu}
  8353. This takes a pair of floating point numbers @code{(nu,x)} where
  8354. @code{nu} is the (fractional) order of an irregular modified
  8355. cylindrical Bessel and @code{x} is the argument to the function,
  8356. and it returns the natural log of the function.
  8357. @end itemize
  8358. @node Bessel function errors, ,Bessel function calling conventions, bes
  8359. @subsection Bessel function errors
  8360. Memory overflows and unrecognized function names can happen as with
  8361. other library interfaces. A message of
  8362. @cindex bad bessel function call
  8363. @example
  8364. <'bad bessel function call'>
  8365. @end example
  8366. @noindent
  8367. means that invalid input parameters were given, such as a fractional
  8368. order to a function family that is defined only for natural orders.
  8369. @node complex, fftw, bes, External Libraries
  8370. @section @code{complex}
  8371. Complex numbers are represented according to the ISO C standard as
  8372. @cindex complex numbers
  8373. arrays of two IEEE double precision floating point numbers of 8 bytes
  8374. each, with the number representing the real part first.
  8375. A small selection of operations on complex numbers is available by
  8376. function calls of the form @code{library('complex',f)}. These
  8377. functions are implemented by the host system's C library.
  8378. One example is @code{library('complex','create')} which takes a pair
  8379. of floating point numbers @code{(@var{x},@var{y})} to a complex number
  8380. whose real part is @var{x} and whose imaginary part is @var{y}.
  8381. See @ref{math} for information about constructing floating point
  8382. numbers.
  8383. Other than that, the @code{complex} library functions @code{f} fall
  8384. into three main groups, which are the real valued unary operations,
  8385. the complex valued unary operations, and the complex valued binary
  8386. operations. All of these operations are designated by their standard C
  8387. names as documented elsewhere, such as the GNU @code{libc} reference
  8388. manual, except as noted.
  8389. @table @bullet
  8390. @item @asis{} real valued unary operations
  8391. @example
  8392. creal cimag cabs carg
  8393. @end example
  8394. @item @asis{} complex valued unary operations
  8395. @example
  8396. ccos cexp clog conj csin csqrt
  8397. ctan csinh ccosh ctanh casinh cacosh
  8398. catanh casin cacos catan
  8399. @end example
  8400. @item @asis{} complex valued binary operations
  8401. @example
  8402. cpow vid bus mul add sub div
  8403. @end example
  8404. @end table
  8405. The last four correspond to the C language operators @code{*},
  8406. @code{+}, @code{-}, and @code{/} for complex numbers. The functions
  8407. named @code{vid} and @code{bus} are similar to @code{div} and
  8408. @code{sub}, respectively, but with the operands interchanged. That is,
  8409. @example
  8410. library('complex','vid') (x,y)
  8411. @end example
  8412. @noindent
  8413. is equivalent to
  8414. @example
  8415. library('complex','div') (y,x)
  8416. @end example
  8417. All functions in this library taking complex numbers as input may also
  8418. operate on real numbers, and binary operators can have either or both
  8419. operands real. For real operands, a value of zero is inferred as the
  8420. imaginary part. The result type of the function is the same
  8421. regardless.
  8422. @node fftw, glpk, complex, External Libraries
  8423. @section @code{fftw}
  8424. Some functions in the @code{fftw} fast Fourier transform library are
  8425. @cindex Fourier transforms
  8426. @cindex Hartley transforms
  8427. callable by virtual code programs of the form
  8428. @code{library('fftw',f)}, where @code{f} can be one of the following
  8429. character strings.
  8430. @table @asis
  8431. @item @code{u_fw_dft}
  8432. (uni-dimensional forward Discrete Fourier transform)
  8433. @item @code{u_bw_dft}
  8434. (uni-dimensional backward Discrete Fourier transform)
  8435. @item @code{b_fw_dft}
  8436. (bi-dimensional forward Discrete Fourier transform)
  8437. @item @code{b_bw_dft}
  8438. (bi-dimensional backward Discrete Fourier transform)
  8439. @item @code{u_dht}
  8440. (uni-dimensional Discrete Hartley transform)
  8441. @item @code{b_dht}
  8442. (bi-dimensional Discrete Hartley transform)
  8443. @end table
  8444. These stand for the discrete Fourier transform, in one dimension and two
  8445. dimensions, either backward or forward, and the discrete Hartley
  8446. transform in one dimension and two dimensions. The @code{fftw} library
  8447. documentation (@url{http://www.fftw.org}) can give more information
  8448. about the meaning of these transformations.
  8449. The interface is somewhat simplified compared to the API for the @code{fftw}
  8450. C library because there are no considerations of memory management or
  8451. planning, nor any provision for dimensions higher than two.
  8452. Furthermore, from the virtual side of the interface, these
  8453. functions operate on lists rather than arrays. The one dimensional
  8454. Fourier transforms take a list of complex numbers to a list of complex
  8455. numbers (see @ref{complex}), and the one dimensional Hartley
  8456. transforms take a list of reals to a list of reals (see
  8457. @ref{math}). The two dimensional transforms are analogous but they
  8458. take a matrix represented as a list of lists. Error messages
  8459. pertaining to invalid input documented at the beginning of
  8460. this section (@ref{External Libraries}) are relevant.
  8461. Finally, unlike the native API for @code{fftw}, these transformations are
  8462. scaled so that the backward transformation is the inverse of the
  8463. forward, and the Hartley transformations are their own inverses
  8464. (subject to roundoff error).
  8465. @node glpk, gsldif, fftw, External Libraries
  8466. @section @code{glpk}
  8467. The @code{glpk} library (@url{ftp://ftp.gnu.org/pub/gnu/glpk/}) solves
  8468. linear programming problems by the either the simplex algorithm or
  8469. @cindex linear programming
  8470. an interior point method.
  8471. The API for C client programs involves a complicated protocol with
  8472. many optional settings, which is simplified for the virtual machine
  8473. interface. Specifically, the library gives a choice of only two
  8474. functions, which can be expressed in the following forms.
  8475. @example
  8476. library('glpk','simplex')
  8477. library('glpk','interior')
  8478. @end example
  8479. @noindent
  8480. These functions have the same calling convention and should return
  8481. generally the same output for identical inputs, but differences in
  8482. performance, precision, and maybe correctness can be expected.
  8483. The remainder of this section applies to both of them.
  8484. @menu
  8485. * glpk input parameters::
  8486. * glpk output::
  8487. * glpk errors::
  8488. * Additional glpk notes::
  8489. @end menu
  8490. @node glpk input parameters, glpk output, glpk, glpk
  8491. @subsection @code{glpk} input parameters
  8492. The argument must be a triple of the form,
  8493. @code{(@var{c},(@var{m},@var{y}))}, subject to the following specification.
  8494. @itemize @bullet
  8495. @item @var{c}
  8496. is a list of cost function coefficients as floating point numbers (see
  8497. @ref{math}). There should be one item of @var{c} for each variable in
  8498. the linear programming problem (Note that there is no additive
  8499. constant, which would require one extra).
  8500. The interpretation of @var{c} is that an assignment of non-negative
  8501. values to the variables @var{x} is sought to make the vector inner
  8502. product @var{c} @var{x} as small as possible.
  8503. @item @var{m}
  8504. is a sparse matrix represented as a list of triples in the form
  8505. @cindex sparse matrix
  8506. @example
  8507. <((@var{i},@var{j}),@var{a})...>
  8508. @end example
  8509. @noindent
  8510. where @var{i} and @var{j} are row and column indices as natural
  8511. numbers starting from 0 and @var{a} is a non-zero floating point
  8512. number. The presence of a triple @code{((@var{i},@var{j}),@var{a})} in
  8513. the list indicates that the @var{i},@var{j}-th entry in the matrix has
  8514. a value of @var{a}. Missing combinations of @var{i} and @var{j}
  8515. indicate that the corresponding entry is zero.
  8516. The interpretation of @var{m} is that together with @var{y} it
  8517. specifies a system of equations the variables in the solution @var{x}
  8518. must satisfy simultaneously, as explained below.
  8519. @item @var{y}
  8520. is a list of floating point numbers, with one number for each distinct value of
  8521. @var{i} in @var{m}, above, needed to complete the equations.
  8522. The interpretation of @var{y} is that in matrix notation, the
  8523. condition @var{m} @var{x} = @var{y} must be met by any acceptable
  8524. solution @var{x}.
  8525. To put it another way, for each distinct value of @var{i}, the @var{i}-th item
  8526. of @var{y} has to equal the sum over all @var{j} of @var{xj} @var{a},
  8527. where @var{a} is the real number appearing in the triple
  8528. @code{((@var{i},@var{j}),@var{a})} in @var{m}, if any, and @var{xj} is
  8529. the @var{j}-th variable of the solution.
  8530. @end itemize
  8531. @node glpk output, glpk errors, glpk input parameters, glpk
  8532. @subsection @code{glpk} output
  8533. If a solution meeting the constraints is found, it is returned as a
  8534. list of pairs of the form @code{<(@var{i},@var{x})...>}, where each
  8535. @var{i} is a natural number and each @var{x} is a floating point
  8536. number giving the value obtained for the @var{i}-th variable numbered
  8537. from zero. Any values of @var{i} that are omitted from the list
  8538. indicate that the corresponding variable has a value of zero.
  8539. If no solution is found due to infeasibility or because @code{glpk}
  8540. just didn't find one, an empty list is returned. The lack of a
  8541. solution is not treated as an exceptional condition.
  8542. @node glpk errors, Additional glpk notes, glpk output, glpk
  8543. @subsection @code{glpk} errors
  8544. Possible error messages are
  8545. @example
  8546. <'bad glpk specification'>
  8547. @end example
  8548. @noindent
  8549. which means that the input did not conform to the description given above,
  8550. and
  8551. @example
  8552. <'memory overflow'>
  8553. @end example
  8554. It is not considered an exceptional condition for no feasible solution
  8555. to exist, and in that case an empty list is returned.
  8556. The @code{glpk} documentation gives no assurance as to the correctness
  8557. of reported solutions, so the user should also take the possibility of
  8558. incorrect results into account.
  8559. @node Additional glpk notes, , glpk errors, glpk
  8560. @subsection Additional @code{glpk} notes
  8561. A sparse matrix representation of @var{m} is used because in practice
  8562. @cindex sparse matrix
  8563. most linear programming problems have very sparse systems of equations.
  8564. Only the constraint of non-negativity is admitted. Other
  8565. @cindex constraints
  8566. constraints such as upper bounds must be effected through a change of
  8567. variables if required.
  8568. The @code{glpk} library has a small memory leak, which @code{avram}
  8569. corrects by methods described in @ref{Memory leaks}.
  8570. @node gsldif, gslevu, glpk, External Libraries
  8571. @section @code{gsldif}
  8572. Numerical differentiation of a real valued function of a single real
  8573. @cindex numerical differentiation
  8574. variable can be done by a library function of the form
  8575. @example
  8576. library('gsldif',method)
  8577. @end example
  8578. @noindent
  8579. where @code{method} is one of
  8580. @itemize @bullet
  8581. @item @code{'backward'}
  8582. @item @code{'central'}
  8583. @item @code{'forward'}
  8584. @item @code{'t_backward'}
  8585. @item @code{'t_central'}
  8586. @item @code{'t_forward'}
  8587. @end itemize
  8588. @menu
  8589. * gsldif input parameters::
  8590. * gsldif output::
  8591. * gsldif exceptions::
  8592. * Additional gsldif notes::
  8593. @end menu
  8594. @node gsldif input parameters, gsldif output, gsldif, gsldif
  8595. @subsection @code{gsldif} input parameters
  8596. The argument to the functions with mnemonics of @code{backward},
  8597. @code{central} or @code{forward} is a pair @code{(@var{f},@var{x})},
  8598. where @var{f} is the virtual machine code for a real valued function
  8599. of a real variable, and @var{x} is the input to @var{f} where the
  8600. derivative is sought. Real numbers are represented according to
  8601. @ref{math}.
  8602. The argument to the functions with mnemonics of @code{t_backward},
  8603. @code{t_central} or @code{t_forward} is a pair @code{((@var{f},@var{t}),@var{x})},
  8604. where @var{f} and @var{x} are as above, and @var{t} is a tolerance represented
  8605. as a floating point number. The tolerance is passed through
  8606. to the GNU Scientific library (GSL) differentiation routines.
  8607. When no tolerance is specified, the default is @code{1.0e-8}.
  8608. @node gsldif output, gsldif exceptions, gsldif input parameters, gsldif
  8609. @subsection @code{gsldif} output
  8610. The result returned by @code{library('gsldif',method) (f,x)} or
  8611. @code{library('gsldif',method) ((f,t),x)}
  8612. is an approximation of the first derivative of @var{f} evaluated at
  8613. @var{x}.
  8614. The result is obtained by the one of the GNU Scientific Library (GSL)
  8615. @cindex GNU Scientific Library
  8616. functions for numerical differentiation that matches the virtual code
  8617. function name. These functions are documented in the GSL reference
  8618. manual. The three methods should have approximately the same results
  8619. but may differ in numerical properties.
  8620. @node gsldif exceptions, Additional gsldif notes, gsldif output, gsldif
  8621. @subsection @code{gsldif} exceptions
  8622. An error message of
  8623. @cindex bad derivative specification
  8624. @example
  8625. <'bad derivative specification'>
  8626. @end example
  8627. @noindent
  8628. will be returned if the either the whole argument, @var{f}, or @var{x} is
  8629. @code{nil}.
  8630. Any error message caused by the evaluation of @var{f} will
  8631. propagate to the result.
  8632. @node Additional gsldif notes, , gsldif exceptions, gsldif
  8633. @subsection Additional @code{gsldif} notes
  8634. The function @var{f} may be any expressible virtual machine code
  8635. function that takes a real argument to a real result, including one
  8636. that uses other library functions. However, if @var{f} passes
  8637. functions to other library functions as arguments, there is a constant
  8638. overhead in stack space for each level, and a remote possibility of a
  8639. @cindex segmentation fault
  8640. segmentation fault if they are very deeply nested.
  8641. Numerical instability is an issue for higher derivatives (i.e.,
  8642. differentiating a function that is obtained by differentiating another
  8643. function). Some experimentation with larger tolerances may be needed.
  8644. @node gslevu, gslint, gsldif, External Libraries
  8645. @section @code{gslevu}
  8646. This library exports a pair of functions of the form
  8647. @example
  8648. library('gslevu','accel')
  8649. library('gslevu','utrunc')
  8650. @end example
  8651. @noindent
  8652. that take a list of real numbers @var{x} to a pair of real numbers
  8653. @code{(@var{s},@var{e})}.
  8654. The idea is that @var{x} represents the first few terms of an infinite
  8655. @cindex infinite series
  8656. series whose sum converges, but only very slowly. The functions
  8657. @cindex convergence
  8658. extrapolate an estimate of the infinite summation by the Levin
  8659. @cindex Levin u-transform
  8660. u-transform as documented in the GNU Scientific Library reference
  8661. manual.
  8662. For well behaved series, considerably fewer terms are needed for an
  8663. accurate estimate than a direct summation would require.
  8664. @menu
  8665. * gslevu calling conventions::
  8666. * gslevu exceptions::
  8667. @end menu
  8668. @node gslevu calling conventions, gslevu exceptions, gslevu, gslevu
  8669. @subsection @code{gslevu} calling conventions
  8670. The input to either of these functions is a list of real numbers
  8671. represented as explained in @ref{math}.
  8672. The result is a pair @code{(@var{s},@var{e})} holding an estimate of
  8673. the sum, @var{s}, and an estimate of the error in the sum,
  8674. @var{e}, each being a real number.
  8675. Both functions compute the same sum, @var{s}, but the @code{utrunc} function
  8676. is faster and
  8677. @cindex infinite sum
  8678. more memory efficient, using a less trustworthy method of estimating
  8679. the error.
  8680. @node gslevu exceptions, , gslevu calling conventions, gslevu
  8681. @subsection @code{gslevu} exceptions
  8682. If an empty list is passed as a parameter to a function in this library,
  8683. an error message of @code{<'empty gslevu sequence'>} is returned.
  8684. If there is insufficient memory, an error message of @code{<'memory
  8685. overflow'>} is returned.
  8686. Other than that, no exceptional conditions are relevant other than the
  8687. general ones documented at the beginning of @ref{External Libraries}.
  8688. @node gslint, harminv, gslevu, External Libraries
  8689. @section @code{gslint}
  8690. An interface to a selection of numerical integration routines from the
  8691. @cindex numerical integration
  8692. GNU Scientific Library is provided by functions of the form
  8693. @example
  8694. library('gslint',q)
  8695. @end example
  8696. @noindent
  8697. where q can be one of
  8698. @code{'qng'}, @code{'qng_tol'},
  8699. @code{'qagx'}, @code{'qagx_tol'},
  8700. @code{'qagp'}, or @code{'qagp_tol'}.
  8701. @menu
  8702. * gslint input parameters::
  8703. * gslint output::
  8704. * gslint exceptions::
  8705. * Additional gslint notes::
  8706. @end menu
  8707. @node gslint input parameters, gslint output, gslint, gslint
  8708. @subsection @code{gslint} input parameters
  8709. The library functions @code{qng} and @code{qagx} take an
  8710. argument of the form @code{(@var{f},(@var{a},@var{b}))}, where @var{f}
  8711. is a function to be integrated, @var{a} is the lower limit, and
  8712. @var{b} is the upper limit, both limits being floating point numbers
  8713. as in @ref{math}.
  8714. The @code{qng_tol} and @code{qagx_tol} functions take an argument of
  8715. @cindex tolerance
  8716. the form @code{((@var{f},@var{t}),(@var{a},@var{b}))}, where @var{f},
  8717. @var{a}, and @var{b} are as above, and @var{t} is a specified
  8718. tolerance.
  8719. The @code{qagp} and @code{qagp_tol} functions take arguments of
  8720. the form @code{(@var{f},@var{p})} and
  8721. @code{((@var{f},@var{t}),@var{p})}, respectively, where @var{f} and
  8722. @var{t} are as above, and @var{p} is an ordered list of real numbers
  8723. specifying the limits of integration along with arbitrarily many
  8724. intervening breakpoints.
  8725. The integrand @var{f} is expressed in virtual machine code, and takes a
  8726. single real argument to a real result. The argument and result of
  8727. @var{f} are required to be floating point numbers as described in
  8728. @ref{math}. Any expressible function of this type is acceptable, even
  8729. one defined in terms of other integrals, so that a double or triple
  8730. integral can be expressed easily, albeit a costly computation.
  8731. However, a constant overhead in stack space is required for each
  8732. nested library function call, and there is currently no mechanism to
  8733. @cindex segmentation fault
  8734. prevent segmentation faults due to a stack overflow.
  8735. When no tolerance is specified, as with @code{qng}, @code{qagx}, and
  8736. @code{qagp}, the tightest attainable tolerance is chosen by default,
  8737. currently @code{2e-14}, in order find the most accurate result
  8738. possible. A selection of progressively looser tolerances is tried
  8739. automatically if the tightest one is not successful, stopping when
  8740. either a solution is found or ten orders of magnitude are covered.
  8741. If a tolerance is explicitly specified, as with @code{qng_tol},
  8742. @code{qagx_tol} or @code{qagp_tol}, only that tolerance is tried.
  8743. @node gslint output, gslint exceptions, gslint input parameters, gslint
  8744. @subsection @code{gslint} output
  8745. In all cases, if no exception occurs, the result returned is an
  8746. approximation of the integral of @var{f} over the interval from
  8747. @var{a} to @var{b} or from the first item of @var{p} to the last.
  8748. Results may differ in numerical properties depending on the
  8749. integration method and the tolerance used.
  8750. @itemize @bullet
  8751. @item
  8752. The @code{qagp}* and @code{qagx}* functions use an adaptive algorithm,
  8753. @cindex adaptive integration
  8754. @cindex non-adaptive integration
  8755. whereas the @code{qng}* functions use a faster non-adaptive algorithm
  8756. suitable only for smooth integrands.
  8757. @item
  8758. Faster and maybe more accurate results are obtained for discontinuous
  8759. or non-differentiable integrands by the @code{qagp}* integration
  8760. methods if the interior points in @var{p} are chosen to coincide with
  8761. the discontinuities or corners.
  8762. @item
  8763. Larger tolerances are conducive to faster but less accurate results in
  8764. most cases.
  8765. @end itemize
  8766. @node gslint exceptions, Additional gslint notes, gslint output, gslint
  8767. @subsection @code{gslint} exceptions
  8768. If an argument of an inappropriate form can be detected (such as an
  8769. empty pair or one without floating point numbers), it causes an error
  8770. message to be returned saying
  8771. @cindex bad integral specification
  8772. @example
  8773. <'bad integral specification'>
  8774. @end example
  8775. @noindent
  8776. Error messages signalled by the integrand @var{f} may also be
  8777. reported, as well as any message returned by @code{gsl_strerror}. A
  8778. typical cause for a @code{gsl_strerror} message would be an explicitly
  8779. specified tolerance that is too tight. An error message of
  8780. @cindex slow convergence
  8781. @example
  8782. <'slow convergence'>
  8783. @end example
  8784. @noindent
  8785. is returned in the event of excessively many function evaluations
  8786. (currently 3600 at each tolerance level).
  8787. @node Additional gslint notes, , gslint exceptions, gslint
  8788. @subsection Additional @code{gslint} notes
  8789. The @code{qagx}* functions subsume the GSL variants @code{qags},
  8790. @code{qagiu}, @code{qagil}, and @code{qagi} for finite, semi-infinite,
  8791. and infinite intervals, which are seleted as appropriate based on the
  8792. @cindex improper integrals
  8793. limits of integration @var{a} and @var{b}.
  8794. The @code{qagp} function reverts to the @code{qagx} function if there
  8795. are only two points given in @var{p}. Fewer than two will cause an
  8796. error.
  8797. The library interface code relies on the standard @code{setjmp}
  8798. @cindex setjmp
  8799. utility found in the system header file @code{setjmp.h} to break out
  8800. of integrals that don't converge after excessively many function
  8801. evaluations. Non-termination has been an issue in the past with GSL
  8802. integration routines for very badly behaved integrands, and the API
  8803. provides no documented means for the user supplied integrand function
  8804. to request a halt.
  8805. Although it is meant to be standard, a host without @code{setjmp} will
  8806. cause @code{avram} to be configured to abort the application with an
  8807. error message in the event of non-convergence. This behavior is
  8808. considered preferable to the alternative of non-termination. Usually
  8809. an effective workaround in such cases is to specify a sufficiently
  8810. loose tolerance explicitly by using one of the *@code{_tol} library
  8811. functions.
  8812. @node harminv, kinsol, gslint, External Libraries
  8813. @section @code{harminv}
  8814. The @code{harminv} library decomposes a complex valued function of a
  8815. @cindex harminv
  8816. discrete variable into a sum of decaying sinusoids given a finite
  8817. sample. It uses a method with better accuracy and convergence than
  8818. Fourier analysis or least squares curve fitting. More information
  8819. @cindex least squares
  8820. @cindex Fourier transforms
  8821. is available at @url{http://ab-initio.mit.edu/wiki/index.php/Harminv}.
  8822. @menu
  8823. * harminv input parameters::
  8824. * harminv output::
  8825. * harminv exceptions::
  8826. * Additional harminv notes::
  8827. @end menu
  8828. @node harminv input parameters, harminv output, harminv, harminv
  8829. @subsection @code{harminv} input parameters
  8830. The virtual machine interface to the @code{harminv} library provides
  8831. only a single function, callable as
  8832. @example
  8833. library('harminv','hsolve')
  8834. @end example
  8835. @noindent
  8836. The input to this function is an operand of the form
  8837. @example
  8838. (signal,(fmin,fmax),nf)
  8839. @end example
  8840. @noindent
  8841. where
  8842. @itemize @bullet
  8843. @item
  8844. @code{signal} is a list of complex numbers containing samples
  8845. of the function to be decomposed at equal time steps
  8846. (@ref{complex} and @ref{Representation of Numeric and Textual Data}).
  8847. @item
  8848. @code{fmin} and @code{fmax} are the band limits expressed in units
  8849. of inverse time steps as floating point numbers (@ref{math}).
  8850. @item
  8851. @code{nf} is the number of spectral basis functions expressed as a
  8852. natural (@ref{Representation of Numeric and Textual Data}).
  8853. @end itemize
  8854. @noindent
  8855. If a value of 0 is specified for @code{nf} a default value of
  8856. @example
  8857. min(300, (fmax - fmin) * n * 1.1)
  8858. @end example
  8859. @noindent
  8860. is used, where @code{n} is the length of @code{signal}. The
  8861. computation time increases cubically with @code{nf}.
  8862. @node harminv output, harminv exceptions, harminv input parameters, harminv
  8863. @subsection @code{harminv} output
  8864. The result returned by a call to
  8865. @example
  8866. library('harminv','hsolve')
  8867. @end example
  8868. @noindent
  8869. with valid input (@ref{harminv input parameters}) is a list of similar tuples of the form
  8870. @example
  8871. <(amplitude,frequency,decay,quality,error)...>
  8872. @end example
  8873. @noindent
  8874. with all members being real valued except for the amplitudes, which are
  8875. complex. Each tuple describes a function of the form
  8876. @example
  8877. f(t) = A * sin (frequency * t + P) * exp (-decay * t)
  8878. @end example
  8879. @noindent
  8880. such that the summation of these functions approximates the original
  8881. given signal (@ref{harminv input parameters}). The real amplitude
  8882. @code{A} and phase @code{P} are given by the modulus and argument of
  8883. the complex amplitude returned in the result,
  8884. @example
  8885. A = library('complex','cabs') amplitude
  8886. P = library('complex','carg') amplitude
  8887. @end example
  8888. @noindent
  8889. in terms of the complex library functions (@ref{complex}).
  8890. The error values are measures of the goodness of fit, and the quality
  8891. factors are defined as
  8892. @example
  8893. quality = (pi * |frequency| / decay)
  8894. @end example
  8895. @noindent
  8896. It may be useful in some applications to ignore components with
  8897. quality factors outside of a certain range.
  8898. @node harminv exceptions, Additional harminv notes, harminv output, harminv
  8899. @subsection @code{harminv} exceptions
  8900. Various exceptional conditions are possible with the @code{harminv}
  8901. library interface, and one of the following messages could be
  8902. returned. Each of them has the form of a list containing a single
  8903. character string.
  8904. @itemize @bullet
  8905. @item
  8906. @code{unrecognized harminv function name}
  8907. is reported in case of a function call of the form
  8908. @code{library('harminv',f)} where @code{f} is anything other than
  8909. the character string @code{'hsolve'}, this being the only function in
  8910. the library.
  8911. @item
  8912. @code{bad harminv function call}
  8913. is reported if the input parameters don't meet the specifications
  8914. described in @ref{harminv input parameters}, or if @code{fmin} is
  8915. greater than @code{fmax}.
  8916. @item
  8917. @code{bad vector specification}
  8918. could be the result of a list of real numbers rather than complex
  8919. numbers being passed as a @code{signal}. Real numbers can
  8920. be converted to complex numbers using the @code{create} function from
  8921. the @code{complex} library (@ref{complex}).
  8922. @item
  8923. @code{memory overflow}
  8924. can occur if @code{avram} is operating very close to the limit of host
  8925. memory, or perhaps if infeasibly large values are passed as @code{nf}
  8926. @item
  8927. @code{counter overflow}
  8928. is similar to a memory overflow
  8929. @end itemize
  8930. @node Additional harminv notes, , harminv exceptions, harminv
  8931. @subsection Additional @code{harminv} notes
  8932. The @code{harminv} library interface requires the @code{harminv} and
  8933. @code{lapack} libraries to be installed on the host system, and also
  8934. requires standard complex number support from the system's C library.
  8935. The author's installation of @code{avram} has been compiled against
  8936. the Debian @code{harminv} development library package, which at this
  8937. writing is unmaintained and is missing the necessary header file
  8938. @file{harminv-int.h}, without which compilation of files including
  8939. @file{harminv.h} fails. Some headers from @file{harminv.h} have been
  8940. copied directly into @file{avram-x.x.x/src/harminv.c} under the
  8941. @code{avram} source tree to avoid this dependence, so that
  8942. @code{avram} will compile correctly on a Debian system. These may need
  8943. to be updated if necessary to track the @code{harminv} source.
  8944. @node kinsol, lapack, harminv, External Libraries
  8945. @section @code{kinsol}
  8946. The @code{kinsol} library (@url{http://www.llnl.gov/CASC/sundials/})
  8947. contains sophisticated routines for non-linear optimization and
  8948. @cindex optimization
  8949. @cindex non-linear optimization
  8950. constrained non-linear optimization, some of which are available to
  8951. @cindex constrained non-linear optimization
  8952. virtual code applications by way of functions expressed as shown.
  8953. @example
  8954. library('kinsol',k)
  8955. @end example
  8956. @noindent
  8957. The function name @code{k} is a string of the form
  8958. @code{'@var{xy}_@var{zzzzz}'}. The field @var{zzzzz} specifies
  8959. the optimization algorithm, which can be one of @code{dense},
  8960. @code{gmres}, @code{bicgs}, or @code{tfqmr}, following the names used
  8961. by the API for @code{kinsol} in C. The field @var{y} determines
  8962. the way gradients are obtained, which is either @code{j} for a user
  8963. supplied Jacobian, or @code{d} for finite differences computed by
  8964. @code{kinsol}. The remaining field @var{x} is either @code{c} for
  8965. constrained optimization, or @code{u} for unconstrained. Hence, the
  8966. whole function name can be one of sixteen possible alternatives.
  8967. @example
  8968. cd_dense cd_gmres cd_bicgs cd_tfqmr
  8969. ud_dense ud_gmres ud_bicgs ud_tfqmr
  8970. cj_dense cj_gmres cj_bicgs cj_tfqmr
  8971. uj_dense uj_gmres uj_bicgs uj_tfqmr
  8972. @end example
  8973. More specific information about the optimization algorithms can be
  8974. found in the @code{kinsol} documentation at the above
  8975. address. Different algorithms may perform better on different
  8976. problems.
  8977. @menu
  8978. * kinsol input parameters::
  8979. * kinsol output::
  8980. * kinsol exceptions::
  8981. * Additional kinsol notes::
  8982. @end menu
  8983. @node kinsol input parameters, kinsol output, kinsol, kinsol
  8984. @subsection @code{kinsol} input parameters
  8985. Functions whose names are of the form @code{@var{x}d_@var{zzzzz}}
  8986. take an argument of the form
  8987. @code{(@var{f},(@var{i},@var{o}))},
  8988. and functions whose names are of the form @code{@var{x}j_@var{zzzzz}}
  8989. take an argument of the form
  8990. @code{((@var{f},@var{j}),(@var{i},@var{o}))}.
  8991. The parameters have these interpretations.
  8992. @itemize @bullet
  8993. @item
  8994. @var{f} is a function to be optimized, expressed in virtual machine
  8995. code. It takes a list of real numbers as input and returns a list of
  8996. real numbers as output. The numbers must be in floating point format
  8997. as described in @ref{math}.
  8998. @item
  8999. @var{j} is a function in virtual machine code that computes the
  9000. Jacobian or partial derivatives of @var{f} for a given list of input
  9001. @cindex Jacobian
  9002. numbers. The exact calling convention for @var{j} depends on the
  9003. optimization algorithm selected, as explained below.
  9004. @item
  9005. @var{i} is a list of real numbers suitable as an input for @var{f}.
  9006. The exact values of the numbers in @var{i} are not crucial but the
  9007. length of @var{i} is taken as an indication of the required length for
  9008. any input list to @var{f}. In the case of constrained optimization
  9009. problems (i.e., functions with names beginning with @code{c}), @var{i} must
  9010. consist entirely of non-negative numbers.
  9011. @item
  9012. @var{o} is a list numbers indicating the ``optimal'' output from
  9013. @var{f} in the sense described below (@ref{kinsol output}). Its length
  9014. is taken to indicate the usual length of an output returned by
  9015. @var{f}.
  9016. @end itemize
  9017. If the optimization problem is being solved by either the
  9018. @code{cj_dense} or the @code{uj_dense} method, the Jacobian parameter
  9019. @var{j} is expected to take a list @var{v} of real numbers the length
  9020. of @var{i} as input and return a list of lists of reals as output. The
  9021. numbers are represented as described in @ref{math}. The outer list in
  9022. the output from @var{j} is required to be the length of @var{o}, while
  9023. each inner list is required to be the length of @var{i}.
  9024. The output from @var{j} is interpreted as a matrix of the form
  9025. described in @ref{Two dimensional arrays}. The entry in row @var{m}
  9026. and column @var{n} is the partial derivative (evaluated at @var{v}) of
  9027. the @var{m}-th component of the output of @var{f} with respect to the
  9028. @var{n}-th item of the input list.
  9029. For optimization problems being solved by the methods of
  9030. @code{@var{x}j_gmres}, @code{@var{x}j_bicgs}, or
  9031. @code{@var{x}j_tfqmr}, (i.e., where @var{x} is either @code{c} or
  9032. @code{u}) the Jacobian function @var{j} follows a different convention
  9033. that is meant to be more memory efficient. Given an argument of the
  9034. form @code{(@var{m},@var{v})}, it returns only the @var{m}-th row of
  9035. the matrix described above instead of the whole thing. The parameter
  9036. @var{m} is a natural number less than the length of @var{o}, and
  9037. @var{v} is a list of real numbers the length of @var{i} the same as
  9038. above. The number @var{m} is encoded as described in
  9039. @ref{Representation of Numeric and Textual Data}.
  9040. @node kinsol output, kinsol exceptions, kinsol input parameters, kinsol
  9041. @subsection @code{kinsol} output
  9042. The @code{kinsol} functions attempt to search the domain of
  9043. @var{f} for a vector @var{v} the length of @var{i} to satisfy
  9044. @code{@var{f}(@var{v}) = @var{o}} as closely as possible. In the case
  9045. of constrained optimization, (i.e., functions whose names begin with
  9046. @code{c}), only non-negative numbers are acceptable in @var{v}. The
  9047. search for @var{v} will start in the vicinity of @var{i}. The value of
  9048. @var{i} will therefore determine a unique solution if multiple
  9049. solutions exist, and will save time if it is near a solution.
  9050. In some cases when a solution can't be found due to non-convergence,
  9051. @cindex non-convergence
  9052. an empty list is returned. Non-convergence is not considered an
  9053. exceptional condition. In all other cases where no exception occurs,
  9054. the output from a @code{kinsol} function will be the list @var{v} of
  9055. real numbers satisfying @code{@var{f}(@var{v}) = @var{o}} to the best
  9056. possible tolerance.
  9057. @cindex tolerance
  9058. @node kinsol exceptions, Additional kinsol notes, kinsol output, kinsol
  9059. @subsection @code{kinsol} exceptions
  9060. @itemize @bullet
  9061. @item
  9062. Any error messages that may be generated in the course of evaluating
  9063. the functions @var{f} and @var{j} will propagate to the result
  9064. returned by the @code{kinsol} library functions.
  9065. @item
  9066. If there is insufficient memory to complete any operation, the result
  9067. is a message of
  9068. @example
  9069. <'memory overflow'>
  9070. @end example
  9071. @item
  9072. If the argument to the library function (i.e.,
  9073. @code{(@var{f},(@var{i},@var{o}))} or
  9074. @code{((@var{f},@var{j}),(@var{i},@var{o}))}) fails to meet the
  9075. required specifications in a detectable way, the result will be a
  9076. message of
  9077. @cindex bad kinsol specification
  9078. @example
  9079. <'bad kinsol specification'>
  9080. @end example
  9081. @item
  9082. Any status returned by any @code{kinsol} API functions other than
  9083. success or one of several types of non-convergence results in a
  9084. message of
  9085. @example
  9086. <'kinsol error'>
  9087. @end example
  9088. @end itemize
  9089. @node Additional kinsol notes, , kinsol exceptions, kinsol
  9090. @subsection Additional @code{kinsol} notes
  9091. When a user supplied Jacobian function @var{j} is specified, the
  9092. @cindex Jacobian
  9093. solution is likely to be found faster and more accurately. The
  9094. Jacobian should be given if an analytical form for @var{f} is known,
  9095. from which the Jacobian can be obtained easily by partial
  9096. differentiation. If the Jacobian is unavailable, a finite difference
  9097. method implemented internally by @code{kinsol} is used as a substitute
  9098. and will usually yield acceptable results.
  9099. Tolerances are not explicitly specified on the virtual side of the
  9100. interface although the native @code{kinsol} API requires them. A range
  9101. of tolerances over ten orders of magnitude is automatically tried
  9102. before giving up.
  9103. Similarly to the @code{glpk} and @code{lpsolve} library interfaces
  9104. (@ref{glpk} and @ref{lpsolve}), the only expressible constraint through
  9105. @cindex constraints
  9106. the virtual code interface is that all variables are
  9107. non-negative. Arbitrary upper and lower bounds can be simulated by
  9108. appropriate variable substitutions in the formulation of the problem.
  9109. The @code{kinsol} library natively requires a system function @var{f}
  9110. with equally many inputs as outputs, and will search only for the
  9111. input associated with an output vector of all zeros, but the virtual
  9112. code interface relaxes these requirements by allowing a function that
  9113. transforms between lists of unequal lengths, and will search for the
  9114. input of @var{f} causing it to match any given ``optimal'' output
  9115. @var{o}. These effects are achieved by padding the shorter of the two
  9116. vectors transparently and subtracting the specified optimum from the
  9117. result.
  9118. The @code{kinsol} library can be configured to use single precision,
  9119. double precision, or extended precision arithmetic, but only a double
  9120. precision configuration is compatible with @code{avram}. This
  9121. condition is checked when @code{avram} is configured and it will not
  9122. interface with alternative @code{kinsol} configurations.
  9123. The @code{kinsol} library has some more advanced features to which
  9124. this interface doesn't do justice, such as preconditioning, scaling,
  9125. solution of systems with band limited Jacobians, and concurrent
  9126. computation.
  9127. @node lapack, math, kinsol, External Libraries
  9128. @section @code{lapack}
  9129. An arsenal of weapons grade linear algebra functions from the
  9130. @cindex Fortran
  9131. @code{LAPACK} Fortran library is accessible to virtual code
  9132. @cindex linear algebra
  9133. applications through library calls of the form
  9134. @example
  9135. library('lapack',f)
  9136. @end example
  9137. Each library function @code{f} invokes a @code{LAPACK} function of the
  9138. same name, but the calling conventions on the virtual side are an
  9139. artifact of the interface requiring their own documentation.
  9140. Some functions that are part of @code{LAPACK} are not described here
  9141. (mostly the so called computational and auxiliary routines, and
  9142. @cindex single precision
  9143. anything in single precision), because they are not accessible by
  9144. the virtual code interface.
  9145. @menu
  9146. * lapack calling conventions::
  9147. * lapack exceptions::
  9148. * Additional lapack notes::
  9149. @end menu
  9150. @node lapack calling conventions, lapack exceptions, lapack, lapack
  9151. @subsection @code{lapack} calling conventions
  9152. A table describing the inputs and outputs to the @code{lapack} library
  9153. functions listed by their function names is given in this
  9154. section. Some general points related to most of the functions are
  9155. mentioned first.
  9156. @itemize @bullet
  9157. @item
  9158. References to vectors, matrices, and packed matrices should be
  9159. understood as their list representations explained in @ref{Type
  9160. Conversions}. Although @code{LAPACK} internally uses column order
  9161. arrays, the virtual code library interface exhibits a matrix as a list
  9162. of lists with one inner list for each row.
  9163. @item
  9164. Some functions require a symmetric matrix as an input parameter. Any
  9165. @cindex symmetric matrices
  9166. input parameter that is required to be a symmetric matrix may be
  9167. specified optionally either in square form or in triangular form as
  9168. @cindex triangular matrices
  9169. described in @ref{Two dimensional arrays}. If a square matrix form is
  9170. used, symmetry is not checked and the lower triangular portion is
  9171. ignored.
  9172. @item
  9173. Some function names are listed in pairs differing only in the first
  9174. letter. Function names beginning with @code{d} pertain to vectors or
  9175. matrices of real numbers (@ref{math}), and function names beginning
  9176. with @code{z} pertain to complex numbers (@ref{complex}). The
  9177. specifications of similarly named functions are otherwise identical.
  9178. @end itemize
  9179. @table @asis
  9180. @item @code{dgesvx}
  9181. @item @code{zgesvx}
  9182. These library functions take a pair @code{(@var{a},@var{b})} where
  9183. @var{a} is an @var{n} by @var{n} matrix and @var{b} is a vector of
  9184. length @var{n}. If @var{a} is non-singular, they return a
  9185. vector @var{x} such that @code{@var{a} @var{x} = @var{b}}.
  9186. Otherwise they return an empty list.
  9187. @item @code{dgelsd}
  9188. @item @code{zgelsd}
  9189. These functions generalize those above by taking a pair
  9190. @code{(@var{a},@var{b})} where @var{a} is an @var{m} by @var{n} matrix
  9191. and @var{b} is a vector of length @var{m}, with @var{m} greater than
  9192. @var{n}. They return a vector @var{x} of length @var{n} to minimize
  9193. the magnitude of @code{@var{b} - @var{a} @var{x}}.
  9194. @item @code{dgesdd}
  9195. @item @code{zgesdd}
  9196. These functions take a list of @var{m} time series (i.e., vectors)
  9197. each of length @var{n} and return a list of basis vectors each of
  9198. length @var{n}. The basis vectors span the set of time series in the
  9199. @cindex singular value decomposition
  9200. given list according to the singular value decomposition (i.e., with
  9201. the basis vectors forming a series in order of decreasing
  9202. significance). The number of basis vectors is at most
  9203. @code{@var{min}(@var{m},@var{n})} but could be less if the input time
  9204. series aren't linearly independent. An empty list could be returned
  9205. due to lack of convergence.
  9206. @item @code{dgeevx}
  9207. @item @code{zgeevx}
  9208. These functions take a non-symmetric square matrix and
  9209. return a pair @code{(@var{e},@var{v})} where @var{e} is a list of
  9210. eigenvectors and @var{v} is a list of eigenvalues, both of which will
  9211. @cindex eigenvectors
  9212. contain only complex numbers. (N.B., both functions return complex
  9213. results even though @code{dgeevx} takes real input.) They could also
  9214. return @code{nil} due to a lack of convergence.
  9215. @item @code{dpptrf}
  9216. @item @code{zpptrf}
  9217. These functions take a symmetric square matrix and
  9218. return one of the Cholesky factors. The Cholesky factors are a pair
  9219. @cindex Cholesky decomposition
  9220. of triangular matrices, each equal to the transpose of the other,
  9221. whose product is the original matrix.
  9222. @itemize @bullet
  9223. @item
  9224. If the input matrix is specified in lower triangular form, the lower
  9225. triangular Cholesky factor is returned.
  9226. @item
  9227. If the input matrix is specified in square or upper triangular form,
  9228. the upper triangular Cholesky factor is returned.
  9229. @item
  9230. In either case, the result is returned in triangular form.
  9231. @end itemize
  9232. @item @code{dggglm}
  9233. @item @code{zggglm}
  9234. The input is a pair of matrices and a vector
  9235. @cindex generalized least squares
  9236. @cindex least squares
  9237. @code{((@var{A},@var{B}),@var{d})}. The output is a pair of vectors
  9238. @code{(@var{x},@var{y})} satisfying @code{@var{A}@var{x} +
  9239. @var{B}@var{y} = @var{d}} for which the magnitude of @var{y} is
  9240. minimal. The dimensions all have to be consistent, which means
  9241. the number of rows in @var{A} and @var{B} is the length of @var{d},
  9242. the number of columns in @var{A} is the length of @var{x}, and
  9243. the number of columns in @var{B} is the length of @var{y}.
  9244. @item @code{dgglse}
  9245. @item @code{zgglse}
  9246. The input is of the form @code{((@var{A},@var{c}),(@var{B},@var{d}))}
  9247. where @var{A} and @var{B} are matrices and @var{c} and @var{d} are
  9248. vectors. The output is a vector @var{x} to minimize the magnitude of
  9249. @code{@var{A}@var{x} - @var{c}} subject to the constraint that
  9250. @code{@var{B}@var{x} = @var{d}}. The dimensions have to be consistent,
  9251. which means @var{A} has @var{m} rows, @var{c} has length @var{m},
  9252. @var{B} has @var{p} rows, @var{d} has length @var{p}, both @var{A} and
  9253. @var{B} have @var{n} columns, and the output @var{x} has length
  9254. @var{n}. It is also a requirement that @code{@var{p} <= @var{n} <=
  9255. @var{m} + @var{p}}.
  9256. @item @code{dsyevr}
  9257. This function takes a symmetric real matrix and returns a pair
  9258. @code{(@var{e},@var{v})} where @var{e} is a list of eigenvectors and
  9259. @var{v} is a list of eigenvalues. Both contain only real numbers.
  9260. This function is fast and accurate but not as storage efficient as
  9261. possible. If there is insufficient memory, it automatically invokes
  9262. @code{dspev}.
  9263. @item @code{dspev}
  9264. This function takes a symmetric real matrix and returns a pair
  9265. @code{(@var{e},@var{v})} where @var{e} is a list of eigenvectors and
  9266. @var{v} is a list of eigenvalues. Both contain only real numbers. It
  9267. uses roughly half the memory of @code{dsyevr} but is not as fast or
  9268. accurate.
  9269. @item @code{zheevr}
  9270. This function takes a complex Hermitian matrix and returns a pair
  9271. @cindex Hermitian matrix
  9272. @code{(@var{e},@var{v})} where @var{e} is a list of eigenvectors and
  9273. @var{v} is a list of eigenvalues. The eigenvectors are complex but the
  9274. eigenvalues are real.
  9275. @itemize @bullet
  9276. @item
  9277. A Hermitian matrix has @var{Aij} equal to the complex conjugate of
  9278. @var{Aji}.
  9279. @item
  9280. Although not exactly symmetric, a Hermitian matrix may nevertheless
  9281. be given in either upper or lower triangular form.
  9282. @item
  9283. This function is faster but less storage efficient than @code{zhpev},
  9284. and calls it automatically if it runs out of memory.
  9285. @end itemize
  9286. @item @code{zhpev}
  9287. This function has the same inputs and approximate outputs as
  9288. @code{zheevr} but is slower and more memory efficient because it uses
  9289. only packed matrices.
  9290. @end table
  9291. @node lapack exceptions, Additional lapack notes, lapack calling conventions, lapack
  9292. @subsection @code{lapack} exceptions
  9293. @itemize @bullet
  9294. @item
  9295. Any of these functions can return a message of
  9296. @example
  9297. <'memory overflow'>
  9298. @end example
  9299. if it runs out of memory.
  9300. @item
  9301. If the input parameters don't meet the
  9302. specification, they can also return
  9303. @cindex bad lapack specification
  9304. @example
  9305. <'bad lapack specification'>
  9306. @end example
  9307. @item
  9308. Any unexpected behavior from the @code{LAPACK} Fortran functions or
  9309. irregular status returned by them is reported by the message
  9310. @cindex lapack error
  9311. @example
  9312. <'lapack error'>
  9313. @end example
  9314. @noindent
  9315. Getting to the bottom of it may require some debugging
  9316. of the @code{avram} source code in the file @file{lapack.c}.
  9317. @end itemize
  9318. @node Additional lapack notes, , lapack exceptions, lapack
  9319. @subsection Additional @code{lapack} notes
  9320. The functions @code{dgesdd} and @code{zgesdd} are an effective
  9321. dimensionality reduction technique for a large database of time
  9322. @cindex dimensionality reduction
  9323. series. A set of basis vectors can be computed once for the database,
  9324. and then any time series in the database can be expressed as a linear
  9325. combination thereof. To the extent that the data embody any redundant
  9326. information, an approximate reconstruction of an individual series
  9327. from the database will require fewer coefficients (maybe far fewer) in
  9328. terms of the basis than original length of the series.
  9329. The library functions @code{dgelsd} and @code{zgelsd} are good for
  9330. @cindex least squares
  9331. finding least squares fits to empirical data. If the matrix parameter
  9332. @var{a} is interpreted as a list of inputs and the vector parameter
  9333. @var{b} as the list of corresponding output data from some unknown
  9334. linear function of @var{n} variables @var{f}, then @var{x} is the list
  9335. of coefficients whereby @var{f} achieves the optimum fit to the data
  9336. in the least squares sense.
  9337. These functions solve a special case of the problem solved by
  9338. @cindex generalized least squares
  9339. @cindex least squares
  9340. @code{dggglm} and @code{zggglm} where the parameter @var{B} is the
  9341. identity matrix. For the latter functions, the output vector @var{y}
  9342. can be interpreted as a measure of the error, and @var{B} can be
  9343. chosen to express unequal costs for errors at different points in
  9344. the fitted function.
  9345. Cholesky decompositions obtained by @code{dpptrf} and @code{zpptrf}
  9346. @cindex Cholesky decomposition
  9347. are useful for generating correlated random numbers. A population of
  9348. vectors of uncorrelated standard normally distributed random numbers
  9349. can be made to exhibit any correlations to order by multiplying all of
  9350. @cindex correlation
  9351. the vectors by the lower Cholesky factor of the desired covariance
  9352. @cindex covariance matrix
  9353. matrix.
  9354. @node math, mtwist, lapack, External Libraries
  9355. @section @code{math}
  9356. The @code{math} library exports functions that operate on IEEE double
  9357. precision floating point numbers using the host system's C
  9358. library. The numbers are represented natively as contiguous blocks of
  9359. 8 bytes each, and on the virtual side as lists of eight character
  9360. representations. (More explanation is given in @ref{Type
  9361. Conversions}.) These functions take the form
  9362. @example
  9363. library('math',f)
  9364. @end example
  9365. @noindent
  9366. where @code{f} is a character string identifying the function in most
  9367. cases by its standard name in the C library.
  9368. @menu
  9369. * math library operators::
  9370. * math library predicates::
  9371. * math library conversion functions::
  9372. * math library exceptions::
  9373. * Additional math library notes::
  9374. @end menu
  9375. @node math library operators, math library predicates, math, math
  9376. @subsection @code{math} library operators
  9377. The unary operators take a single real number to a real result. They
  9378. @cindex trigonometric functions
  9379. include
  9380. @example
  9381. ceil floor round trunc
  9382. sin cos tan sinh cosh tanh
  9383. asin acos atan asinh acosh atanh
  9384. exp log sqrt cbrt expm1 log1p fabs
  9385. @end example
  9386. The binary operators take a pair of real numbers @code{(@var{x},@var{y})} to
  9387. a single real number output. They include
  9388. @example
  9389. pow hypot atan2 remainder bus vid add sub mul div
  9390. @end example
  9391. @noindent
  9392. where the last four correspond to the C language operators @code{+},
  9393. @code{-}, @code{*}, and @code{/}. The functions named @code{bus} and
  9394. @code{vid} are like the @code{sub} and @code{div} functions,
  9395. respectively, with the order of the operands reversed, as explained in
  9396. @ref{complex}.
  9397. The meanings of these operators are documented in the GNU @code{libc} reference
  9398. manual or other C language references. They follow IEEE standards including
  9399. proper handling of @code{nan} and infinity.
  9400. @node math library predicates, math library conversion functions, math library operators, math
  9401. @subsection @code{math} library predicates
  9402. There is one binary predicate, @code{islessequal}, and several unary
  9403. @cindex predicates
  9404. predicates, @code{isinfinite}, @code{isnan}, @code{isnormal}, @code{isubnormal}
  9405. and @code{iszero}.
  9406. The predicate @code{islessequal} takes a pair of floating point
  9407. numbers @code{(@var{x},@var{y})} as an argument, and returns
  9408. @code{nil} for a false result and @code{(nil,nil)} for a true result.
  9409. The unary predicates have the obvious interpretations as
  9410. classification functions, and should probably be used in preference to
  9411. comparison with constants in case the representations aren't unique.
  9412. @node math library conversion functions, math library exceptions, math library predicates, math
  9413. @subsection @code{math} library conversion functions
  9414. The conversion function @code{strtod} takes a string representing a
  9415. @cindex strtod
  9416. floating point number in C format to its representation. This function
  9417. is the primary means of creating or initializing floating point
  9418. numbers in virtual code. A value of floating point 0.0 is returned if
  9419. the string is not valid, but no exception is raised.
  9420. The conversion @code{asprintf} is similar to the one by that name in
  9421. @cindex asprintf
  9422. C, but requires a pair @code{(@var{f},@var{x})} as an argument. The
  9423. left side @var{f} is a character string containing a C style format
  9424. conversion for exactly one double precision floating point number,
  9425. such as @code{'%0.4e'}, and the parameter @var{x} is a floating point
  9426. number. The result returned will be a character string expressing the
  9427. number in the specified format.
  9428. @node math library exceptions, Additional math library notes, math library conversion functions, math
  9429. @subsection @code{math} library exceptions
  9430. The most likely cause of an exception is an attempt to apply a
  9431. @code{math} library function to @code{nil} or to an argument that
  9432. doesn't represent a floating point number. In these cases, an error
  9433. @cindex missing value
  9434. @cindex invalid value
  9435. message of @code{<'missing value'>} or @code{<'invalid value'>} will
  9436. be the result.
  9437. An error message of @code{<'invalid asprintf() specifier'>} is
  9438. @cindex invalid asprintf specifier
  9439. reported by the @code{asprintf} function if the format specifier
  9440. @cindex asprintf
  9441. pertains to a string, such as @code{'%s'}. This error is specifically
  9442. trapped because the alternative would be a segmentation
  9443. @cindex segmentation fault
  9444. fault. Otherwise, invalid format specifiers are not detected or
  9445. reported.
  9446. Error messages of @code{<'invalid text format'>} can be generated
  9447. @cindex invalid text format
  9448. by conversion functions if any parameters that are meant to be
  9449. character string representations are something else.
  9450. There is always a chance of a @code{<'memory overflow'>} error if
  9451. there is insufficient memory to allocate a result.
  9452. @node Additional math library notes, , math library exceptions, math
  9453. @subsection Additional @code{math} library notes
  9454. Floating point exceptions such as division by zero are not
  9455. specifically reported as exceptions, but invalid computations can be
  9456. @cindex nan
  9457. detected by the propagation of @code{nan} into the result, following
  9458. standard conventions.
  9459. The C function @code{feclearexcept (FE_ALL_EXCEPT)} is called before
  9460. @cindex feclearexcept
  9461. every floating point operation so that no lingering exception flags
  9462. can affect it.
  9463. There is no library predicate for exact comparison of floating point
  9464. numbers, but none is required because the virtual machine's
  9465. @code{compare} combinator will work on their representations as it
  9466. @cindex compare combinator
  9467. will on any other data. The usual caveats apply with regard to
  9468. comparing floating point numbers in the presence of roundoff error.
  9469. @node mtwist, minpack, math, External Libraries
  9470. @section @code{mtwist}
  9471. The @code{mtwist} library interfaces to a random number generator
  9472. @cindex random numbers
  9473. based on the Mersenne Twistor algorithm. The algorithm has good
  9474. properties but is not meant to be cryptographically secure. The
  9475. library functions are of the form
  9476. @example
  9477. library('mtwist',f)
  9478. @end example
  9479. @noindent
  9480. where @code{f} is one of the followng character strings.
  9481. @example
  9482. bern u_cont u_disc u_path u_enum w_disc w_enum
  9483. @end example
  9484. Formally they are not mathematical functions because their results
  9485. depend on a pseudo-random number that is not uniquely determined by
  9486. their arguments. The numbers are generated deterministically in a
  9487. sequence starting from a seed derived from the system clock at the
  9488. time @code{avram} is launched, and each call uses the next number
  9489. in the sequence. In so doing, it simulates a random draw from a
  9490. uniformly distributed population.
  9491. @menu
  9492. * mtwist calling conventions::
  9493. * mtwist exceptions::
  9494. * Additional mtwist notes::
  9495. @end menu
  9496. @node mtwist calling conventions, mtwist exceptions, mtwist, mtwist
  9497. @subsection @code{mtwist} calling conventions
  9498. All of the functions in this library simulate a random draw from a
  9499. distribution. There is a choice of distribution statistics depending
  9500. on the function used.
  9501. @table @asis
  9502. @item @code{bern}
  9503. takes a floating point number @var{p} between 0 and 1, encoded as in
  9504. @ref{math}, and returns a boolean value, either @code{(nil,nil)} for
  9505. true or @code{nil} for false. A true value is returned only if a
  9506. random draw from a uniform distribution ranging from 0 to 1 is less
  9507. than @var{p}. This function therefore simulates a draw from a
  9508. Bernoulli distribution. A @code{nil} value of @var{p} is treated as
  9509. 1/2.
  9510. @item @code{u_cont}
  9511. takes a floating point number @var{x} as an
  9512. argument, and returns a random draw from a continuous uniform
  9513. distribution ranging from 0 to @var{x}. A @code{nil} value of @var{x}
  9514. is treated as unity.
  9515. @item @code{u_disc}
  9516. simulates a draw from a uniform discrete distribution whose domain
  9517. is the set of natural numbers from 0 to @var{n} - 1. The number @var{n}
  9518. is given as a parameter to this function, and the retuned value
  9519. is the draw.
  9520. @itemize @bullet
  9521. @item
  9522. The returned value will have at most 64 bits regardless of @var{n}.
  9523. @item
  9524. Natural numbers are encoded as described in
  9525. @ref{Representation of Numeric and Textual Data}.
  9526. @item
  9527. If a value of 0 is passed for @var{n}, the full 64 bit range is
  9528. used.
  9529. @end itemize
  9530. @item @code{u_path}
  9531. takes a pair of natural numbers @code{(@var{n},@var{m})} and returns a
  9532. randomly chosen tree (@ref{Raw Material}) with 1 leaf and @var{n}
  9533. non-leaves each having either a left or a right descendent but not
  9534. both. The number @var{m} constrains the result to fall within the
  9535. first @var{m} - 1 trees of this form enumerated by exhausting all
  9536. possibilities at lower levels before admitting a right descendent at a
  9537. higher level. Within these criteria, all possible results are equally
  9538. probable. Both numbers are masked to 64 bits, but if @var{m} is zero,
  9539. it is treated as 2^@var{n}.
  9540. @item @code{u_enum}
  9541. simulates a random draw from a uniform discrete distribution whose
  9542. domain is enumerated. The argument to the function is a non-empty list,
  9543. and the result is an item selected from the list, with all choices
  9544. being equally probable.
  9545. @item @code{w_disc}
  9546. simulates a random draw from a non-uniform, or ``weighted'' discrete
  9547. distribution whose domain is a set of consecutive natural numbers
  9548. starting from zero. The argument to the function is a list giving the
  9549. probability of each outcome starting from zero as a floating point
  9550. number. Probabilities must be non-negative but needn't be normalized.
  9551. @item @code{w_enum}
  9552. simulates a random draw from a non-uniform, or ``weighted'' discrete
  9553. distribution with an arbitrary domain enumerated in the argument. The
  9554. argument is a list of pairs @code{<(@var{x},@var{p})..>}, where
  9555. @var{x} is a possible outcome and @var{p} is its probability. The
  9556. result returned is one of the values of @var{x} from the input list
  9557. chosen at random according to the associated
  9558. probability. Probabilities must be non-negative but needn't be
  9559. normalized.
  9560. @end table
  9561. @node mtwist exceptions, Additional mtwist notes, mtwist calling conventions, mtwist
  9562. @subsection @code{mtwist} exceptions
  9563. @itemize @bullet
  9564. @item
  9565. @code{<'memory overflow'>} can be returned if there is insufficient
  9566. memory to allocate a result.
  9567. @item
  9568. Messages of @code{<'missing value'>} and @code{<'invalid value'>} can
  9569. be returned if any floating point argument is @code{nil} or is not a
  9570. valid floating point number, unless there is a designated default
  9571. interpretation for @code{nil} as in @code{bern} and @code{u_cont}.
  9572. @item
  9573. A message of @code{<'bad mtwist specification'>} is returned if an
  9574. argument to the @code{bern} function is not in the range of 0 to 1, or
  9575. if any probability passed to the @code{w_}* functions is negative.
  9576. @item
  9577. A message of @code{<'draw from empty list'>} is returned if
  9578. an argument to the *@code{_enum} functions is @code{nil}
  9579. or if an argument to @code{w_enum} contains @code{nil}.
  9580. @end itemize
  9581. @node Additional mtwist notes, , mtwist exceptions, mtwist
  9582. @subsection Additional @code{mtwist} notes
  9583. Although the @code{mtwist} library is ``external'', it requires no
  9584. special configuration on the host because the uniform variate
  9585. generator in the form developed by its original authors is short and
  9586. elegant enough to be packaged easily within the @code{avram}
  9587. distribution. All further embellishments are home grown despite the
  9588. advice at the end of @ref{Implementing new library functions}.
  9589. The @code{u_path} function is intended to allow sampling from a large
  9590. population in logarithmic time when it is stored in a balanced tree. A
  9591. left-heavy tree should be constructed initially with the data items
  9592. all at the same level. Thereafter, a result returned by @code{u_path}
  9593. with the appropriate dimensions can be used as an index into the tree
  9594. for fast retrieval by the virtual machine's @code{field} combinator
  9595. (@ref{Field}).
  9596. The last three functions, @code{u_enum}, @code{w_disc}, and
  9597. @code{w_enum} use an inversion method with a binary search. The first
  9598. draw from a given list will take a time asymptotically proportional to
  9599. the length of the list, but subsequent draws from the same list are
  9600. considerably faster due to a persistent cache maintained transparently
  9601. by @code{avram}. For lists whose length is up to 2^16, the time
  9602. required for a subsequent draw consists mainly of constant overhead
  9603. with a small logarithmic component in the length of the list. For
  9604. longer lists, the time ramps up linearly by a small factor.
  9605. Information allowing fast draws from up to sixteen lists can be cached
  9606. simultaneously. If an application uses more than sixteen, the cached
  9607. data are replaced in first-in first-out order. The size of the cache
  9608. and the maximum list length for logarithmic time access can be
  9609. adjusted easily by redefining constants in @file{mtwist.c} under the
  9610. @code{avram} source tree, but will require recompilation.
  9611. @node minpack, mpfr, mtwist, External Libraries
  9612. @section @code{minpack}
  9613. The @code{minpack} library contains functions to solve non-linear
  9614. @cindex non-linear optimization
  9615. optimization and least squares problems. The functions can be
  9616. @cindex least squares
  9617. expressed as
  9618. @example
  9619. library('minpack',f)
  9620. @end example
  9621. @noindent
  9622. where @code{f} can be one of @code{'hybrd'}, @code{'hybrj'},
  9623. @code{'lmder'}, @code{'lmdif'}, or @code{'lmstr'}, following the names
  9624. of the underlying Fortran subroutines.
  9625. @menu
  9626. * minpack calling conventions::
  9627. * minpack exceptions::
  9628. * Additional minpack notes::
  9629. @end menu
  9630. @node minpack calling conventions, minpack exceptions, minpack, minpack
  9631. @subsection @code{minpack} calling conventions
  9632. The @code{minpack} library solves a similar problem to that of the
  9633. @code{kinsol} library (@ref{kinsol}), and the two libraries have
  9634. identical calling conventions at the level of the virtual code
  9635. interface.
  9636. The @code{hybrd} and @code{lmdif} functions take input arguments of
  9637. the form @code{(@var{f},(@var{i},@var{o}))}, whereas @code{hybrj},
  9638. @code{lmder}, and @code{lmstr} take arguments of the form
  9639. @code{((@var{f},@var{j}),(@var{i},@var{o}))}. The interpretations of
  9640. these parameters are explained in @ref{kinsol input parameters}.
  9641. For the @code{lmstr} function, the Jacobian function @var{j} takes
  9642. @cindex Jacobian
  9643. an argument @code{(@var{m},@var{v})} and returns only the @var{m}-th
  9644. row of the Jacobian matrix. For @code{lmder} and @code{hybrj}, the
  9645. Jacobian function takes only an input vector @var{v} and returns the
  9646. whole matrix. These specifications are also explained further in
  9647. relation to the @code{kinsol} library.
  9648. The output from any minpack function is a vector @var{v} satisfying
  9649. @code{@var{f}(@var{v}) = @var{o}} to the best possible tolerance if a
  9650. @cindex tolerance
  9651. solution is found. A range of tolerances over ten orders of magnitude
  9652. is sampled starting from @code{1e-15}. If no solution is found, an
  9653. empty list is returned.
  9654. @node minpack exceptions, Additional minpack notes, minpack calling conventions, minpack
  9655. @subsection @code{minpack} exceptions
  9656. @itemize @bullet
  9657. @item
  9658. A message of @code{<'memory overflow'>} is possible any time
  9659. @code{minpack} runs out of memory.
  9660. @item
  9661. A message of @code{<'bad minpack specification'>} will be returned
  9662. @cindex bad minpack specification
  9663. if an input argument recognizably violates the required specification.
  9664. @item
  9665. The @code{<'minpack error'>} message is returned in the event of any
  9666. @cindex minpack error
  9667. unexpected behavior or irregular status from the API.
  9668. @item
  9669. Any error messages reported by the system function @var{f} or the
  9670. Jacobian function @var{j} are propagated to the result.
  9671. @end itemize
  9672. @node Additional minpack notes, , minpack exceptions, minpack
  9673. @subsection Additional @code{minpack} notes
  9674. The @code{lm}* functions are better suited to problems in which the
  9675. system function @var{f} has more outputs than inputs, and the
  9676. @code{hybr}* functions are better suited to the alternative. If either
  9677. is called when the other is more appropriate, the job is handed off to
  9678. the other automatically.
  9679. The @code{lmstr} function is more memory efficient than the others
  9680. because it doesn't compute the whole Jacobian matrix at once. Any
  9681. @cindex Jacobian
  9682. of the @code{lm}* functions is more memory efficient than
  9683. the @code{kinsol} equivalent when the output list is sufficiently
  9684. longer than the input list.
  9685. Unlike @code{kinsol}, there is no provision in @code{minpack} for
  9686. @cindex constrained optimization
  9687. constrained optimization.
  9688. The @code{minpack} documentation doesn't state whether it's
  9689. @cindex re-entrancy
  9690. re-entrant, but the odds are against it unless it uses no storage
  9691. outside the user provided work areas. If it isn't re-entrant,
  9692. anomalous effects could occur when a virtual code function being
  9693. optimized calls another @code{minpack} function. A workaround would
  9694. be to use an equivalent @code{kinsol} function, which is re-entrant by
  9695. design.
  9696. The @code{avram} configuration script searches for a C header file
  9697. @cindex header file
  9698. @file{minpack.h} on the host system in order to build an interface to
  9699. this library. This file is specific to the Debian @code{minpack-dev}
  9700. package and is not part of the upstream Fortran source. Configuring
  9701. @code{avram} with an interface to the @code{minpack} library on a
  9702. @cindex Debian
  9703. non-Debian system may require the administrator to retrieve the header
  9704. file manually from the Debian archive and place it under
  9705. @file{/usr/include} before running the configuration script (in
  9706. addition to installing the @code{minpack} library itself, of course).
  9707. @node mpfr, lpsolve, minpack, External Libraries
  9708. @section @code{mpfr}
  9709. The @code{mpfr} library provides a rich assortment of floating point
  9710. operations on arbitrary precision numbers
  9711. (@url{http://www.mpfr.org}). These numbers are represented in a format
  9712. that is not binary compatible with the standard IEEE floating point
  9713. number format used by other libraries, but they offer superior
  9714. numerical stability suitable for many ill conditioned problems.
  9715. The virtual code interface to the @code{mpfr} library follows the
  9716. native API to the extent of using the same names for most operations,
  9717. but excludes features pertaining to i/o, mutable storage, and memory
  9718. management.
  9719. The @code{mpfr} library functions are invoked by an expression of the
  9720. form
  9721. @example
  9722. library('mpfr',f)
  9723. @end example
  9724. @noindent
  9725. Aside from a few exceptions as noted, @code{f} is a character string
  9726. derived from the name of the related function from the @code{mpfr} C
  9727. library as documented at the above address, but without the
  9728. @code{mpfr_} prefix.
  9729. The full complement of available functions is documented in the
  9730. remainder of this section.
  9731. @itemize @bullet
  9732. @item
  9733. References to natural numbers pertain to the list representation
  9734. described in @ref{Representation of Numeric and Textual Data}.
  9735. @item
  9736. All functions that perform rounding use a mode of @code{GMP_RNDN} for
  9737. @cindex rounding
  9738. rounding to nearest, which is not explicitly specified on the virtual
  9739. side.
  9740. @end itemize
  9741. @menu
  9742. * mpfr binary operators::
  9743. * mpfr unary operators::
  9744. * mpfr binary operators with a natural operand::
  9745. * mpfr binary predicates::
  9746. * mpfr unary predicates::
  9747. * mpfr constants::
  9748. * mpfr functions with miscellaneous calling conventions::
  9749. * mpfr conversion functions::
  9750. * mpfr exceptions::
  9751. * Additional mpfr notes::
  9752. @end menu
  9753. @node mpfr binary operators, mpfr unary operators, mpfr, mpfr
  9754. @subsection @code{mpfr} binary operators
  9755. Functions with these names take a pair of @code{mpfr} numbers
  9756. @code{(@var{x},@var{y})} and return an @code{mpfr} number as a result.
  9757. @itemize @bullet
  9758. @item @code{add}
  9759. @item @code{sub}
  9760. @item @code{mul}
  9761. @item @code{div}
  9762. @item @code{pow}
  9763. @item @code{atan2}
  9764. @item @code{hypot}
  9765. @item @code{min}
  9766. @item @code{max}
  9767. @item @code{vid}
  9768. @item @code{bus}
  9769. @end itemize
  9770. Their semantics are similar to those listed in the @code{mpfr}
  9771. documentation, with some minor qualifications.
  9772. @itemize @bullet
  9773. @item
  9774. Unlike the native API, there is no third argument to which the result
  9775. is assigned, because the result is the returned value.
  9776. @item
  9777. The precision of the result is the greater of the two precisions of
  9778. the input numbers @var{x} and @var{y}.
  9779. @item
  9780. The @code{vid} and @code{bus} functions are added features of the
  9781. virtual code interface, corresponding to division and subtraction with
  9782. the order of the operands reversed, as explained in @ref{complex}.
  9783. @end itemize
  9784. Mathematically it might make more sense for the precision of the
  9785. @cindex precision
  9786. result to be the lesser of the two input precisions, but this way is
  9787. more convenient for virtual code programs that perform binary
  9788. operations on their input with hard coded constants, because it makes
  9789. one size fit all.
  9790. @node mpfr unary operators, mpfr binary operators with a natural operand, mpfr binary operators, mpfr
  9791. @subsection @code{mpfr} unary operators
  9792. Functions with these names take a single @code{mpfr} number as an
  9793. argument and return a single @code{mpfr} number as a result.
  9794. @cindex gamma functions
  9795. @example
  9796. sqr sqrt cbrt neg abs log
  9797. log2 log10 exp exp2 exp10 cos
  9798. sin tan acos asin atan cosh
  9799. sinh tanh acosh asinh atanh lngamma
  9800. expm1 eint gamma erf log1p nextbelow
  9801. ceil floor round trunc frac nextabove
  9802. erfc
  9803. @end example
  9804. The semantics of these functions are similar to those of their
  9805. counterparts in the native API, with these provisions.
  9806. @itemize @bullet
  9807. @item
  9808. The precision of the result is the precision of the argument.
  9809. @item
  9810. There is no second argument for assigning the result.
  9811. @item
  9812. The @code{nextabove} and @code{nextbelow} functions do not modify
  9813. their arguments in place, but return a freshly allocated result
  9814. like all other functions.
  9815. @end itemize
  9816. @node mpfr binary operators with a natural operand, mpfr binary predicates, mpfr unary operators, mpfr
  9817. @subsection @code{mpfr} binary operators with a natural operand
  9818. Functions with these names take an argument of the form
  9819. @code{(@var{x},@var{n})}, where @var{x} is an @code{mpfr} number
  9820. and @var{n} is a natural number.
  9821. @itemize @bullet
  9822. @item @code{root}
  9823. @item @code{pow_ui}
  9824. @item @code{mul_2ui}
  9825. @item @code{div_2ui}
  9826. @item @code{grow}
  9827. @item @code{shrink}
  9828. @end itemize
  9829. The last two are specific to the virtual code interface, having no
  9830. counterpart in the native API of the @code{mpfr} library. The
  9831. @code{grow} function returns a copy of @var{x} with its precision
  9832. increased by @var{n} bits, and the @code{shrink} function returns a
  9833. copy of @var{x} with its precision reduced by @var{n} bits.
  9834. @itemize @bullet
  9835. @item
  9836. The precisions are silently capped at the maximum or floored at the
  9837. @cindex precision
  9838. minimum allowable precisions if necessary.
  9839. @item
  9840. Increasing the precision by the @code{grow} function does not directly
  9841. cause a more accurate result to be computed, but only pads an existing
  9842. number with zeros.
  9843. @item
  9844. Decreasing the precision by the @code{shrink} function does not prevent
  9845. valid bits from being discarded.
  9846. @end itemize
  9847. The appropriate way to use @code{grow} is to grow the precision of an
  9848. operand before applying an operator to it, which will cause the result
  9849. to be computed to the full precision. This capability is suitable for
  9850. algorithms that iterate over increasing precisions until a stopping
  9851. criterion is met.
  9852. @node mpfr binary predicates, mpfr unary predicates, mpfr binary operators with a natural operand, mpfr
  9853. @subsection @code{mpfr} binary predicates
  9854. These predicates take a pair of @code{mpfr} numbers
  9855. @code{(@var{x},@var{y})} as arguments and perform a logical operation.
  9856. If the result is true, they return @code{(nil,nil)}, and if it's
  9857. false, they return @code{nil}.
  9858. @itemize @bullet
  9859. @item @code{equal_p}
  9860. @item @code{unequal_abs}
  9861. @item @code{greater_p}
  9862. @item @code{greaterequal_p}
  9863. @item @code{less_p}
  9864. @item @code{lessequal_p}
  9865. @item @code{lessgreater_p}
  9866. @end itemize
  9867. The name of the function @code{unequal_abs}, for comparing absolute
  9868. values, has been changed from @code{mpfr_cmpabs} to avoid confusion
  9869. with the virtual machine's @code{compare} combinator. The
  9870. @code{compare} combinator returns a @code{(nil,nil)} result (i.e.,
  9871. true) if the
  9872. @cindex compare combinator
  9873. operands are equal and a @code{nil} result if they're unequal,
  9874. opposite from @code{unequal_abs}.
  9875. @node mpfr unary predicates, mpfr constants, mpfr binary predicates, mpfr
  9876. @subsection @code{mpfr} unary predicates
  9877. Each of these predicates takes an @code{mpfr} number as an argument
  9878. and performs a logical operation. If the result is true, it returns
  9879. @code{(nil,nil)}, and otherwise it returns @code{nil}.
  9880. @itemize @bullet
  9881. @item @code{nan_p}
  9882. @item @code{inf_p}
  9883. @item @code{number_p}
  9884. @item @code{zero_p}
  9885. @item @code{integer_p}
  9886. @end itemize
  9887. @node mpfr constants, mpfr functions with miscellaneous calling conventions, mpfr unary predicates, mpfr
  9888. @subsection @code{mpfr} constants
  9889. Each of these functions takes a natural number as an argument
  9890. specifying a precision, and returns a mathematical constant evaluated
  9891. to that precision.
  9892. @itemize @bullet
  9893. @item @code{const_log2}
  9894. @item @code{pi}
  9895. @item @code{const_catalan}
  9896. @item @code{inf}
  9897. @item @code{ninf}
  9898. @item @code{nan}
  9899. @end itemize
  9900. The name of the constant @code{pi} has been shortened from
  9901. @code{mpfr_const_pi}. The functions @code{inf} and @code{ninf} return
  9902. infinity and negative infinity, respectively.
  9903. The encoding of @code{nan}, used to represent the results of undefined
  9904. @cindex nan
  9905. computations such as division by zero, is not unique even for a fixed
  9906. precision. Applications should test for undefined results using
  9907. @code{nan_p} rather than by comparing a result to a hard coded
  9908. @code{nan} (@ref{mpfr unary predicates}).
  9909. @node mpfr functions with miscellaneous calling conventions, mpfr conversion functions, mpfr constants, mpfr
  9910. @subsection @code{mpfr} functions with miscellaneous calling conventions
  9911. Some functions listed below don't conform to any of the previously
  9912. mentioned calling conventions.
  9913. @table @asis
  9914. @item @code{eq}
  9915. This is a ternary operator taking a triple
  9916. @code{(@var{prec},(@var{x},@var{y}))}, where @var{prec} is a natural
  9917. number and @var{x} and @var{y} are @code{mpfr} numbers. It returns a
  9918. result of @code{(nil,nil)} (i.e., true) if the numbers agree up to the
  9919. specified precision measured in bits, and returns @code{nil}
  9920. otherwise.@footnote{a potentially useful tool for algorithms concerned
  9921. with numerical approximations despite its inexplicable malignment in
  9922. the @code{mpfr} documentation}
  9923. @item @code{urandomb}
  9924. This function takes a natural number specifying a precision and
  9925. @cindex random numbers
  9926. returns a uniformly distributed pseudo-random number of that precision
  9927. between 0 and 1.
  9928. @item @code{prec}
  9929. This function takes an @code{mpfr} number and returns a natural number
  9930. as a result, which is the precision of the argument in bits.
  9931. @item @code{sin_cos}
  9932. This function takes an @code{mpfr} number @var{z} as an argument and
  9933. returns a pair of @code{mpfr} numbers @code{(@var{x},@var{y})} as a
  9934. result, where @var{x} is the sine of @var{z} and @var{y} is the cosine. The
  9935. precisions of the results are the same as the precision of the
  9936. argument.
  9937. @end table
  9938. @node mpfr conversion functions, mpfr exceptions, mpfr functions with miscellaneous calling conventions, mpfr
  9939. @subsection @code{mpfr} conversion functions
  9940. The functions described in this section convert between @code{mpfr}
  9941. numbers and character strings, naturals, or standard IEEE floating
  9942. point format (in their list representations). Where these functions
  9943. have similar or equivalent counterparts in the @code{mpfr} library's
  9944. native API, the names have been changed for mnemonic reasons.
  9945. @table @asis
  9946. @item @code{dbl2mp}
  9947. The input is a standard floating point number as in @ref{math}. The
  9948. result is an @code{mpfr} number equal to the input with a
  9949. fixed precision, currently set to 160 bits.
  9950. @item @code{mp2dbl}
  9951. The input is an @code{mpfr} number, and the output is the best
  9952. possible approximation to it by a standard a double precision
  9953. number.
  9954. @item @code{str2mp}
  9955. The input is a pair @code{(@var{prec},@var{s})}, where @var{prec} is a
  9956. natural number specifying the precision, and @var{s} is a string
  9957. expressing a floating point number in C format. The output is an
  9958. @code{mpfr} number with the specified precision.
  9959. @item @code{mp2str}
  9960. The input is an @code{mpfr} number, and the output is a character
  9961. string expressing the number in exponential decimal notation.
  9962. Sufficiently many decimal digits are included in the string to express
  9963. the full precision.
  9964. @item @code{nat2mp}
  9965. The input is a natural number represented as described in
  9966. @ref{Representation of Numeric and Textual Data},
  9967. and the output is an @code{mpfr} number of sufficient precision to
  9968. express the natural number exactly.
  9969. @end table
  9970. The @code{mp2str} function enhances the native @code{mpfr_get_str}
  9971. function by properly formatting the output string rather than only
  9972. listing the digits of the mantissa.
  9973. The @code{nat2mp} function does not rely on the @code{mpfr} native
  9974. integer conversion functions, so natural numbers with any number of
  9975. bits up to @code{MP_PREC_MAX} can be used losslessly. There is
  9976. currently no conversion in the other direction.
  9977. @node mpfr exceptions, Additional mpfr notes, mpfr conversion functions, mpfr
  9978. @subsection @code{mpfr} exceptions
  9979. @itemize @bullet
  9980. @item
  9981. A message of @code{<'memory overflow'>} is possible any time
  9982. @code{mpfr} runs out of memory.
  9983. @item
  9984. A message of @code{<'bad mpfr specification'>} will be returned
  9985. @cindex bad mpfr specification
  9986. if an input argument recognizably violates the required specification.
  9987. @item
  9988. The @code{<'mpfr error'>} message is returned in the event of any
  9989. @cindex mpfr error
  9990. unexpected behavior or irregular status from the API.
  9991. @item
  9992. The message of @code{<'mpfr overflow'>} can be cause by the
  9993. @code{nat2mp} function if a natural number has too many bits to be
  9994. represented exactly as an @code{mpfr} number.
  9995. @end itemize
  9996. @node Additional mpfr notes, , mpfr exceptions, mpfr
  9997. @subsection Additional @code{mpfr} notes
  9998. The @code{eq} and @code{urandomb} functions depend not only on the
  9999. @code{mpfr} library but on the @code{gmp} library
  10000. @cindex gmp library
  10001. (@url{http://ftp.gnu.org/gnu/gmp}). It's possible for them to be
  10002. unavailable on a host without @code{gmp} even if the rest of the
  10003. @code{mpfr} library is properly configured.
  10004. The file @code{mpfr.c} in the @code{avram} source tree exports a
  10005. couple of functions that may be of use to C hackers interested in
  10006. further development of @code{avram} with @code{mpfr}. The functions
  10007. @code{avm_mpfr_of_list} and @code{avm_list_of_mpfr} convert between
  10008. the native representation for @code{mpfr} numbers and the caching list
  10009. representation used by @code{avram} (@ref{Type Conversions}). This
  10010. conversion is non-trivial because the numbers are not stored
  10011. contiguously.
  10012. @node lpsolve, rmath, mpfr, External Libraries
  10013. @section @code{lpsolve}
  10014. This library interface exports functions to solve linear programming
  10015. @cindex mixed integer programming
  10016. @cindex linear programming
  10017. and mixed integer programming problems using the @code{lpsolve}
  10018. package documented at
  10019. @url{http://lpsolve.sourceforge.net/5.5/}.
  10020. @noindent
  10021. Of the two linear
  10022. programming solvers currently interfaced with @code{avram}, this one
  10023. is believed to be the more robust.
  10024. @menu
  10025. * lpsolve calling conventions::
  10026. * lpsolve return values::
  10027. * lpsolve errors::
  10028. @end menu
  10029. @node lpsolve calling conventions, lpsolve return values, lpsolve, lpsolve
  10030. @subsection @code{lpsolve} calling conventions
  10031. The library is able to solve linear and mixed integer programming
  10032. problems, depending on which function is selected. The
  10033. @cindex linear programming
  10034. function to call the linear programming solver is of the form
  10035. @itemize @bullet
  10036. @item @code{library('lpsolve','stdform')}
  10037. @end itemize
  10038. @noindent
  10039. @cindex mixed integer programming
  10040. and the mixed integer programming functions are of the form
  10041. @itemize @bullet
  10042. @item @code{library('lpsolve','iform')}
  10043. @item @code{library('lpsolve','bform')}
  10044. @item @code{library('lpsolve','biform')}
  10045. @end itemize
  10046. @noindent
  10047. The argument to the @code{stdform} function represents a triple
  10048. @code{(@var{c},(@var{m},@var{y}))}, which has the same interpretation
  10049. described in @ref{glpk input parameters}. The arguments to the @code{iform},
  10050. @code{bform}, and @code{biform} functions are tuples
  10051. @code{(@var{i},(@var{c},(@var{m},@var{y})))}
  10052. @code{(@var{b},(@var{c},(@var{m},@var{y})))}, and
  10053. @code{((@var{b},@var{i}),(@var{c},(@var{m},@var{y})))}, respectively, where
  10054. @var{c}, @var{m}, and @var{y} are as above, and
  10055. @itemize @bullet
  10056. @item @var{b} is a list of binary variable column indices
  10057. @item @var{i} is a list of integer variable column indices
  10058. @end itemize
  10059. @noindent
  10060. where column indices pertain to the constraint matrix, and are
  10061. numbered from zero. Specifying some or all variables as integers
  10062. directs the solver to seek only solutions in which those variables
  10063. have integer values, and specifying any as binary directs the solver
  10064. to seek only solutions in which those variables have values of zero or
  10065. one. The IEEE floating point representation is used for all variables
  10066. regardless (@ref{math}).
  10067. @node lpsolve return values, lpsolve errors, lpsolve calling conventions, lpsolve
  10068. @subsection @code{lpsolve} return values
  10069. If a feasible and optimal solution is found, a list of values for the variables
  10070. is returned in the form @code{<(@var{i},@var{x})...>}, where @var{i} is a natural
  10071. number and @var{x} is a floating point number giving the value of the @var{i}-th
  10072. variable numbered from zero. Values of @var{x} equal to zero are omitted.
  10073. @node lpsolve errors, , lpsolve return values, lpsolve
  10074. @subsection @code{lpsolve} errors
  10075. If any calling conventions are not followed, an exception is raised and a diagnostic
  10076. message of @code{bad lpsolve problem specification} is reported. If no feasible solution
  10077. can be found, no exception is raised but an empty list is returned.
  10078. @node rmath, umf, lpsolve, External Libraries
  10079. @section @code{rmath}
  10080. A selection of mathematical and statistical functions from the GNU R
  10081. math library has a virtual code interface of the form
  10082. @example
  10083. library('rmath',f)
  10084. @end example
  10085. @noindent
  10086. where @code{f} is a character string derived from the name of a
  10087. function in the C language API described in the manual
  10088. @file{R-exts.pdf}, available at @url{http://www.r-project.org}.
  10089. Every function in the library returns a real result in the form of
  10090. @ref{math}, but functions differ in the argument types. The arguments
  10091. are tuples of real numbers and booleans that also closely follow the
  10092. native API as explained below.
  10093. @menu
  10094. * rmath statistical functions::
  10095. * rmath miscellaneous functions::
  10096. * rmath exceptions::
  10097. @end menu
  10098. @node rmath statistical functions, rmath miscellaneous functions, rmath, rmath
  10099. @subsection @code{rmath} statistical functions
  10100. Functions for evaluating random draws, density, cumulative probability
  10101. and inverse cumulative probability are provided for some of the more
  10102. frequently used probability distributions, which are chi-squared,
  10103. non-central chi-squared, exponential, lognormal, normal, poisson,
  10104. Student's t, and uniform.
  10105. Each distribution is known by an abbreviated name and specified by one
  10106. @cindex distributions
  10107. @cindex probability distributions
  10108. @cindex statistical distributions
  10109. or two real parameters as listed below. Names of distributions in this
  10110. table form the stem of a library function name. The names of the
  10111. parameters such as @var{mu} and @var{sigma} are not explicitly
  10112. mentioned when invoking the functions, but are listed here for
  10113. reference. The precise definitions of the distribution functions and
  10114. interpretations of these parameters can be found in standard texts on
  10115. probability and statistics.
  10116. @example
  10117. chisq @var{df}
  10118. nchisq @var{df}, @var{lambda}
  10119. exp @var{scale}
  10120. lnorm @var{logmean}, @var{logsd}
  10121. norm @var{mu}, @var{sigma}
  10122. pois @var{lambda}
  10123. t @var{n}
  10124. unif @var{a}, @var{b}
  10125. @end example
  10126. The virtual code interface follows a naming convention similar to the
  10127. native API, in that function names beginning with @code{r} represent
  10128. random draws from a distribution, with the argument to the function
  10129. being the parameters specifying the distribution. Functions in this
  10130. first group return a random draw from a distribution described by a
  10131. single real parameter.
  10132. @itemize @bullet
  10133. @item @code{rchisq}
  10134. @item @code{rexp}
  10135. @item @code{rpois}
  10136. @item @code{rt}
  10137. @end itemize
  10138. @noindent
  10139. These next functions return random draws from distributions specified
  10140. by a pair of parameters, @code{(@var{x},@var{y})}.
  10141. @itemize @bullet
  10142. @item @code{rnchisq}
  10143. @item @code{rlnorm}
  10144. @item @code{rnorm}
  10145. @item @code{runif}
  10146. @end itemize
  10147. Functions whose names begin with @code{d} evaluate the probability
  10148. density of a distribution at a given point. They require at least two
  10149. real arguments, the first being the point whose probability density is
  10150. sought, and the remaining ones being the parameters that specify the
  10151. distribution. A boolean operand, which is @code{nil} for false and
  10152. @code{(nil,nil)} for true, requests the logarithm of the density when
  10153. true.
  10154. Functions with names in the following group take a triple with two
  10155. real operands and a boolean, @code{(@var{x},(@var{y},@var{a}))}, and
  10156. return a probabiity density.
  10157. @itemize @bullet
  10158. @item @code{dchisq}
  10159. @item @code{dexp}
  10160. @item @code{dpois}
  10161. @item @code{dt}
  10162. @end itemize
  10163. @noindent
  10164. The next functions pertain to distributions requiring two paramters to
  10165. specify them, so they take a quadruple with three real operands and a
  10166. boolean, @code{(@var{x},(@var{y},(@var{z},@var{a})))}.
  10167. @itemize @bullet
  10168. @item @code{dnchisq}
  10169. @item @code{dlnorm}
  10170. @item @code{dnorm}
  10171. @item @code{dunif}
  10172. @end itemize
  10173. Functions whose names begin with @code{p} or @code{q} obtain
  10174. @cindex cumulative probability
  10175. cumulative probabilities or inverse cumulative probabilities
  10176. respectively for a specified distribution. They require one real
  10177. operand to identify the point whose probability or inverse probability
  10178. is sought, and other real operands to parameterize the distribution,
  10179. as above. There are also two boolean operands. The first is true in
  10180. order to request a probability or inverse probability with respect to
  10181. the lower tail as opposed to the upper, and the other is true to
  10182. indicate that probabilities are to be expressed logarithmically.
  10183. The argument to these functions is a quadruple with two real operands
  10184. and two booleans, @code{(@var{x},(@var{y},(@var{a},@var{b})))}.
  10185. @itemize @bullet
  10186. @item @code{pchisq}, @code{qchisq}
  10187. @item @code{pexp}, @code{qexp}
  10188. @item @code{ppois}, @code{qpois}
  10189. @item @code{pt}, @code{qt}
  10190. @end itemize
  10191. @noindent
  10192. The remaining functions pertain to distributions parameterized by two
  10193. real operands. These take a quintuple with three real operands and two
  10194. booleans, @code{(@var{x},(@var{y},(@var{z},(@var{a},@var{b}))))}.
  10195. @itemize @bullet
  10196. @item @code{pnchisq},
  10197. @code{qnchisq}
  10198. @item @code{plnorm},
  10199. @code{qlnorm}
  10200. @item @code{pnorm},
  10201. @code{qnorm}
  10202. @item @code{punif},
  10203. @code{qunif}
  10204. @end itemize
  10205. @node rmath miscellaneous functions, rmath exceptions, rmath statistical functions, rmath
  10206. @subsection @code{rmath} miscellaneous functions
  10207. Some less frequently used real valued mathematical functions are also
  10208. accessible by the @code{rmath} library interface. The functions with
  10209. @cindex gamma functions
  10210. names in this group take a single real operand.
  10211. @example
  10212. gammafn lgammafn digamma
  10213. trigamma tetragamma pentagamma
  10214. @end example
  10215. @noindent
  10216. The ones in this group take a pair of real operands
  10217. @cindex bessel functions
  10218. @code{(@var{x},@var{y})}.
  10219. @example
  10220. beta lbeta bessel_j bessel_y
  10221. @end example
  10222. @noindent
  10223. Those remaining take a triple of real operands
  10224. @code{(@var{x},(@var{y},@var{z}))}.
  10225. @example
  10226. bessel_i bessel_k
  10227. @end example
  10228. An alternative and better documented selection of Bessel functions is
  10229. provided by the @code{bes} library interface (@ref{bes}).
  10230. @node rmath exceptions, , rmath miscellaneous functions, rmath
  10231. @subsection @code{rmath} exceptions
  10232. The only exceptional condition specific to the @code{rmath} library
  10233. interface is associated with the message @code{<'bad rmath
  10234. specification'>}, which means that a tuple given as an argument
  10235. @cindex bad rmath specification
  10236. has the wrong number or types of operands.
  10237. @node umf, ,rmath, External Libraries
  10238. @section @code{umf}
  10239. Systems of equations described by sparse matrices (i.e., matrices
  10240. @cindex sparse matrices
  10241. containing mostly zeros) arise in certain practical problems. The
  10242. usual array representation in which zeros are explicitly stored would
  10243. be prohibitive for large matrices occurring in many problems of
  10244. interest. A more sophisticated approach is used by the @code{umf}
  10245. library to manage memory efficiently, which is documented at
  10246. @url{http://www.cise.ufl.edu/research/sparse/SuiteSparse/current/SuiteSparse/UMFPACK/Doc/}.
  10247. A virtual code interface to functions for solving sparse systems of
  10248. equations by these methods is afforded by library functions of the form
  10249. @example
  10250. library('umf',f)
  10251. @end example
  10252. @noindent
  10253. where the library function name, @code{f} is a character string
  10254. of the form @code{@var{tt}_@var{m}_@var{rrr}}.
  10255. @itemize @bullet
  10256. @item @var{tt}
  10257. can be either @code{di} for real matrices, or @code{zi} for complex.
  10258. @item @var{m}
  10259. can be one of @code{a}, @code{t}, or @code{c} for solving a system
  10260. given either by a matrix, its transpose, or its conjugate transpose,
  10261. respectively,
  10262. @cindex conjugate transpose
  10263. corresponding to mnemonics @code{A}, @code{Aat} and @code{At} used in
  10264. the C language API.
  10265. @item @var{rrr}
  10266. is either @code{trp} or @code{col}, to indicate a sparse matrix expressed
  10267. either as a list of triples, or in packed column form, as documented below.
  10268. @end itemize
  10269. The complete set of function names for this library interface is as
  10270. follows.
  10271. @example
  10272. di_a_trp di_a_col zi_a_trp zi_a_col
  10273. di_t_trp di_t_col zi_t_trp zi_t_col
  10274. zi_c_trp zi_c_col
  10275. @end example
  10276. @noindent
  10277. Not all combinations are represented, because the conjugate transpose
  10278. is relevant only to complex matrices.
  10279. @menu
  10280. * umf input parameters::
  10281. * umf output::
  10282. * umf exceptions::
  10283. * Additional umf notes::
  10284. @end menu
  10285. @node umf input parameters, umf output, umf, umf
  10286. @subsection @code{umf} input parameters
  10287. For a square matrix @var{A} and a column vector @var{b}, the
  10288. @code{umf} functions find the solution @var{x} to the matrix equation
  10289. @var{M} @var{x} = @var{b}, where @var{M} is either @var{A}, the
  10290. transpose of @var{A}, or its conjugate transpose. As noted above, the
  10291. choice is determined by whether the the function name is of the form
  10292. *@code{_a_}*, *@code{_t_}*, or *@code{_c_}* respectively.
  10293. The argument to any of these functions is a pair
  10294. @code{(@var{A},@var{b})}, where @var{A} represents the matrix
  10295. mentioned above and @var{b} represents the column vector.
  10296. The parameter @var{b} is required to be a list of numbers whose length
  10297. matches the number of rows in the matrix. The numbers are either real
  10298. numbers for the @code{di_}* functions (@ref{math}), or complex
  10299. for the @code{zi_}* functions (@ref{complex}).
  10300. There is a choice of representations for the parameter @var{A},
  10301. depending on whether the function being called is one of the *@code{_trp}
  10302. functions or one of the *@code{_col} functions.
  10303. For the *@code{_trp} functions, @var{A} is represented as a non-empty
  10304. list of triples @code{<((@var{i},@var{j}),@var{v})...>}, where each
  10305. item of the list corresponds to a non-zero entry in the matrix.
  10306. @itemize @bullet
  10307. @item
  10308. The parameters @var{i} and @var{j} are natural numbers as in
  10309. @ref{Representation of Numeric and Textual Data}.
  10310. @item
  10311. The value @var{v} is a real number for the @code{di_}*@code{_trp} functions or a
  10312. complex number for the @code{zi_}*@code{_trp} functions.
  10313. @item
  10314. The presence of a triple ((@var{i},@var{j}),@var{v}) in the list
  10315. signifies that the @var{i},@var{j}-th entry in the matrix @var{A}
  10316. (numbered from zero) has a value of @var{v}.
  10317. @end itemize
  10318. For the *@code{_col} functions, the representation of @var{A} is more
  10319. complicated but has a slight advantage in memory usage. It may also
  10320. have an advantage in speed unless more time is wasted on the virtual
  10321. side transforming a matrix to this representation than it saves.
  10322. In this case, @var{A} is represented by a triple of the form
  10323. @code{((@var{p},@var{i}),@var{v})}. The parameters @var{p} and @var{i}
  10324. are lists of natural numbers. The parameter @var{v} is a list of real
  10325. numbers for the @code{di_}*@code{_col} functions and complex numbers
  10326. for the @code{zi_}*@code{_col} functions. They have the following
  10327. interpretations.
  10328. @itemize @bullet
  10329. @item @var{v}
  10330. is the list of non-zero entries in the matrix in
  10331. @cindex column major order
  10332. column major order.
  10333. @item @var{i}
  10334. has the same length as @var{v}, and each item of @var{i} is the row
  10335. index of the corresponding item in @var{v}, numbered from zero.
  10336. @item @var{p}
  10337. has the length of the number of columns in the matrix, and each
  10338. item identifies the starting position of a column in @var{v} and
  10339. @var{i}, numbered from zero.
  10340. @end itemize
  10341. @noindent
  10342. The first item of @var{p} is always zero. Further explanation of this
  10343. format in terms of an array representation can be found in the file
  10344. @file{UMFPACK_UserGuide.pdf}, available from the @code{umf} library
  10345. home page at @url{http://www.cise.ufl.edu/research/sparse/SuiteSparse/current/SuiteSparse/}.
  10346. @node umf output, umf exceptions, umf input parameters, umf
  10347. @subsection @code{umf} output
  10348. If no exception occurs, the solution @var{x} to the matrix equation
  10349. @var{M} @var{x} = @var{b} noted previously will be returned if one
  10350. exists.
  10351. The solution is represented as either a list of real numbers as in
  10352. @ref{math}, or a list of complex numbers as in @ref{complex}. Real
  10353. numbers are returned by the @code{di_}* functions, and complex numbers
  10354. are returned by the @code{zi_}* functions.
  10355. If no solution exists due to a singular matrix, an empty list is
  10356. returned. The lack of a solution isn't treated as an exceptional
  10357. condition.
  10358. @node umf exceptions, Additional umf notes, umf output, umf
  10359. @subsection @code{umf} exceptions
  10360. If an exceptional condition arises from the use of this library, one
  10361. of the following lists of character strings may be returned as the
  10362. function result.
  10363. @itemize @bullet
  10364. @item
  10365. @code{<'memory overflow'>} means the library function ran out of
  10366. memory, most likely due to a matrix being too large.
  10367. @item
  10368. @code{<'bad umf specification'>} means an input parameter didn't
  10369. @cindex bad umf specification
  10370. conform to the appropriate format described above (@ref{umf input
  10371. parameters})
  10372. @item
  10373. @code{<'umf error'>} covers any unexpected behavior or abnormal
  10374. @cindex umf error
  10375. status returned by any function from the C language API.
  10376. @end itemize
  10377. For the *@code{_trp} functions. A non-square matrix will cause the
  10378. second exception above. For the *@code{_col} functions, a non-square
  10379. matrix will cause the third exception or cause an empty result
  10380. to be returned.
  10381. The exceptions noted at the beginning of this section (@ref{External
  10382. Libraries}) are also possible.
  10383. @node Additional umf notes, , umf exceptions, umf
  10384. @subsection Additional @code{umf} notes
  10385. The C language API to @code{umf} provides very many less frequently
  10386. used features that are not part of the virtual code interface, some of
  10387. which could be added by minor modifications to the file @file{umf.c}
  10388. in the @code{avram} source tree.
  10389. A set of @code{dl_}* and @code{zl_}* functions orthogonal to those
  10390. presently accessible would enable matrices having billions of rows or
  10391. columns by using long integers, but memory requirements on the virtual
  10392. code side for problems of that scale are probably prohibitive for the
  10393. foreseeable future.
  10394. @node Copying, Function Index, External Libraries, Top
  10395. @include gpl.texinfo
  10396. @node Function Index, Concept Index, Copying, Top
  10397. @unnumbered Function Index
  10398. @printindex fn
  10399. @node Concept Index, , Function Index, Top
  10400. @unnumbered Concept Index
  10401. @printindex cp
  10402. @bye
  10403. pcx has been ditched, but this section is left here
  10404. in case someone decides to put it back
  10405. @node pcx, lpsolve, mpfr, External Libraries
  10406. @section @code{pcx}
  10407. This library interface exports a function that uses the algorithm
  10408. documented at @url{http://www-fp.mcs.anl.gov/otc/Tools/PCx/} to solve
  10409. linear programming problems by an interior point method. The library
  10410. @cindex linear programming
  10411. function is of the form
  10412. @example
  10413. library('pcx','solution')
  10414. @end example
  10415. @noindent
  10416. This function has the same calling conventions as
  10417. @code{library('glpk','simplex')} and @code{library('glpk','interior')}
  10418. (@ref{glpk}).
  10419. @cindex internal errors
  10420. Error messages are also the same as @ref{glpk errors}, except that the
  10421. a message text of @code{<'bad pcx specification'>} is used. In
  10422. addition, @code{pcx} can cause a virtual machine internal error (code
  10423. 70), which is raised when @code{avram} detects access anomalies by an
  10424. external library, or in the worst case, a segmentation fault.
  10425. @cindex segmentation fault
  10426. A working configuration of this library interface requires a shared
  10427. library @file{libPCx.so} to be installed on the host, which is not a
  10428. feature of the standard installation distributed by the authors of
  10429. @code{PCx}. However, the shared library can be built from the source
  10430. package by any system administrator familiar with these
  10431. procedures. Alternatively, an unofficial patched version of the Debian
  10432. @cindex Debian
  10433. package of @code{pcx} that includes the shared library is available on
  10434. the @code{avram} home page. This patch has been submitted to the
  10435. Debian maintainer of @code{pcx} and may be expected in a future
  10436. release.
  10437. If @code{avram} has been built already on a system that has the
  10438. @code{pcx} header files but not the shared library, it should be
  10439. possible to get the @code{pcx} library interface to work just by
  10440. installing the shared library without recompiling @code{avram},
  10441. because @code{pcx} is dynamically loaded due to licensing
  10442. restrictions.