AGenericProgramforSequentialDecisionProcessesOegedeMoorProgrammingResearchGroup,OxfordUniversityComputingLaboratoryWolfsonBuilding,ParksRoad,OxfordOX13QD,UK1MotivationAgenericprogramforaclassofproblemsisaprogramwhichcanbeusedtosolveeachprobleminthatclassbysuitablyinstantiatingitsparameters.ThemostcelebratedexampleofagenericprogramisthealgorithmofAho,HopcroftandUllmanforthealgebraicpathproblem[1].Herewehaveasingleprogram,pa-rameterisedbyanumberofoperators,thatsolvesnumerousseeminglydisparateproblems.Furthermore,theapplicabilityofthegenericprogramiselegantlystatedastheconditionthattheparametersformaclosedsemi-ring.Examplesofsuchgenericalgorithmsareadmittedlyscarce,andonemightbeledtobelievethatthemajorityofprogramscannotbeexpressedinsuchagenericmanner.Ipersonallydonotsharethatview,forthefollowingtworeasons:Firstly,littlee orthasgoneintoattemptstoclassifyexistingalgorithms,re ectingthefactthatthecomputingcommunityasawholeplacesmorevalueontheinventionofnewalgorithmsthanontheorganisationofexistingknowledge.Incertainspecialisedareaswhereorganisationaltoolsareindispensable(forexampleinthedesignofarchitecture-independentparallelalgorithms),numerousgenericalgorithmshavebeendiscovered,andformthecoreofthesubject[13].Secondly,toexpressgenericalgorithmsonerequiresahigher-ordernotationinwhichbothprogramsandtypescanbeparameterstootherprograms.Suchhigher-ordernotationsencouragetheprogrammertoexploitgenericitywherepossible,andindeedmanyfunctionalprogrammersnowdosoasamatterofcourse.Moretraditionalprogrammingnotationsdonoto ersimilarsupportforwritinggenericprograms.Thispaperisanattempttopersuadeyouofmyviewpointbypresentinganovelgenericprogramforacertainclassofoptimisationproblems,namedsequen-tialdecisionprocesses.Thisclasswasoriginallyidenti edbyRichardBellmaninhispioneeringworkondynamicprogramming[4].Itisaperfectexampleofaclassofproblemswhichareverymuchalike,butwhichhasuntilnowescapedsolutionbyasingleprogram.ThosereaderswhohavefollowedsomeoftheworkthatRichardBirdandIhavebeendoingoverthelast veyears[6,7]willrecognisemanyindividualexamples:allofthesehavenowbeenuni ed.Thepointofthisobservationisthatevenwhenyouareonthelookoutforgenericprograms,itcantakearatherlongtimetodiscoverthem.Thepresentationbelowwillfollowthatearlierwork,byreferringtothecalculusofrelationsandtherelationaltheoryofdatatypes.Ishallhoweverattempttobelightontheformalism,asIdonotregarditasessentialtothemainthesisofthispaper.Undoubtedlythereareother(perhapsmoreconvenient)notationsinwhichthesameideascouldbedeveloped.ThispaperdoesassumesomedegreeoffamiliaritywithalazyfunctionalprogramminglanguagesuchasHaskell,Hope,Miranda1orGofer.Infact,theLaTEX leusedtoproducethispaperisitselfanexecutableGoferprogram.2SequentialDecisionProcessesManyoptimisationproblemscanbespeci edintheformspecrpgen=listminr.filterp.genHerethegeneratorgengeneratesalistofcandidatesolutions,filterpselectsthosesolutionswhicharefeasible(thosethatsatisfythepredicatep),and nallytheselectorlistminrpicksaminimumsolutionaccordingtotheorderrelationr.Sequentialdecisionprocessesaredistinguishedfromotheroptimisationprob-lemsinthewaythegeneratorisformulated.Thesequentialnatureofthegenera-toriscapturedbyexpressingitasaninstanceofthefunctionfold2.Informally,foldisde nedbytheequationfoldadde[a1,a2,...,an]=a1‘add‘(a2‘add‘...(an‘add‘e))Inwordsonemightsaythatfoldaddextraversesthelistxfromrighttoleft,startingwiththeseedvaluee,andsummingwiththeoperatoraddateachstep.Therecursivede nitionoffoldisfoldadde[]=efoldadde(a:x)=a‘add‘(foldaddex)Inasequentialdecisionprocess,wehavegen=foldfe1MirandaisatrademarkofResearchSoftwareLtd.2Thefunctionfoldisnormallycalledfoldr.BecauseIshalldeviatefromthecommonde nitionoffoldr1,Ihavedecidedtousefoldandfold1toavoidconfusion.andateachstep,theoperatorfo ersanondeterministicchoicebetweenanumberofalternatives.Itisthischoicethataccountsforthetermdecisionprocess.Thealternativesarepresentedasalistofoperators,andthespeci cationofasequentialdecisionprocessthereforereadssdp_specrpfsc=listminr.filterp.fold(choicefs)[c]choicefsaxs=[fax|f-fs,x-xs]Thatis,ateachstepwehavethechoiceofapplyinganyoftheoperatorsfromfstoanyoftheresultssofarproducedinxs.Cognoscentiwillrecognisearelationalfoldhere[2].Theabovecanbevariedslightlyaccordingtothekindoflistsoneworkswith.Forinstance,fornon-emptyliststhecounterpartoffoldisfold1fg[a]=gafold1fg(a:x)=fa(fold1fgx)Thatis,thefunctiongisappliedtothelastelementofthelist,andthenwesumfromrighttoleftusingthefunctionf.Modifyingthenotionofsequentialdecisionprocessaccordingly,oneobtainssdp1_specrpfsl=listminr.filterp.fold1(choicefs)l’wherel’a=[la]Notethatthisdi ersfromthede nitionofsdpspecinthebasecaseonly.Belowweshallalsohaveauseforlistswithatleasttwoelements.Againfoldcanbemodi edforthisparticulardatatype:fold2fg[a,b]=gabfold2fg(a:x)=fa(fold2fgx)Inwords,gisappliedtothelasttwoelementsofthelist,andthenwesum(asbefore)fromrighttoleft,usingthefunctionf.Thecorrespondingnotionofsequentialdecisionprocessisgivenbysdp2_specrpfsl=listminr.filterp.fold2(choicefs)l’wherel’ab=[lab]Th