• Embed Doc
  • Readcast
  • Collections
  • 1
    CommentGo Back
Download
 
Erlang-Asurveyofthelanguageanditsindustrialapplications 
JoeArmstrong ComputerScienceLaboratorEricssonTelecommunicationsSystemsLaboratorBox150S-1252 Alvsj Swede joe@erix.ericsson.se September18,1996 
Abstrac
ThispaperdescribestheErlangprogramminglan- guageandsomeoftheapplicationswhichhavebeeprogrammedinErlang.Erlanghasmanylanguagefeaturesmorecommonlassociatedwithanoperatingsystemthanalanguage.Concurrentprocesses,scheduling,garbagecollection,etc.,areallperformedbytheErlangrun-timesysteandnotbytheunderlyingoperatingsystem.Thepaperstartswithadescriptionofsomeofthe requirementsthatwewantedtosatisfywhendesign- ingthelanguage.Thisisfollowedbyabriefintroductiontothelanguage.Finally,wediscusssomeofthe applicationswhichhavebeenprogrammedinErlang.Wehavechosentoconcentrateonthreeapplications whicharecommercialproducts.
1Introduction 
ErlangARM95a]isaparallelfunctionalprogramminglanguagedesignedforprogrammingreal-timcontrolsystems(examplesofsuchsystemsaretelephoneexchanges,AutomatedTellerMachinesetc.).Wewantedtomakealanguagewhichaddressedmanoftheproblemswhicharehandledinanoperatingsystemwhilemaintainingtheadvantagesofadeclarative programminglanguage.Ourproblemdomainrequirethatweaddressthefollowingproblems:
 
Real-tim
-Real-timeresponsetimesintheorderofmillisecondsarerequired.Erlangisdesignedforprogramming\soft"real-timesystemwherewedonothavetomeetalltimingdead- lines.
 
Verylargeprogram
-Controlsystemscan havemillionsoflinesofcode,andareprogrammedbylargeteamsofprogrammers.
 
Non-stopsystem
-Controlsystemscannobestoppedforsoftwaremaintenance.TheErlangabstractmachineallowsprogramcodetobe changedinarunningsystem.Oldcodecanbe phasedoutandreplacedbynewcode.During thetransition,botholdcodeandnewcodecan berunatthesametime.Thisenablesfaultstobe correctedandsoftwaretobeupgradedinsystemwithoutdisturbingtheiroperation.
 
Portability 
-Erlangcompilestoabstractma- chinecodewhichcanberunonanyofalargnumberofdierentoperatingsystems.Thisap- proachmakesthesystemsourceandobjectcode compatible.Thecostofemulationisnotalimitingfactorioursystems,experimentsHAU94]showthatErlangcanoftenbeexecutedasecientlyasun- optimisedCdespiteonelevelofmachineemula- tionbeinginvolved.Theadvantagesofhaving portableobjectcodeandofhavingtosupporonlyoneversionofthecompileroutweighsthe advantagesofnativecodecompilation.
 
Concurrency 
-Ourapplicationsarebestmod- eledbyaverylargenumberofconcurrentpro- cesses.Atanyinstantintimemostofthesepro- cessesareidle.Thenumberofprocessesantheirmemoryrequirementsvarywithtimeand areextremelydiculttopredictinadvance.Erlanghaslightweightprocesseswhosememoryrequirementsvarydynamically.Norequirementforconcurrencyareplacedonthehostoperating system.
 
InterProcessCommunication 
-TheErlang abstractmachinesupportscommunicationbetweenlight-weightprocesses.Communicationiperformedbyasynchronousmessagepassing.
 
Distributio
-Erlangisdesignedtoberunindistributedmulti-nodeenvironment.Everycom
 
putationinErlangisperformedwithinaprocess.Processeshavenosharedmemoryandcommunicatebyasynchronousmessagepassing.AnErlangsystemrunningononeprocessorcancreateaparallelprocessrunningonanothersyste(whichneednotevenbethesameprocessororop- eratingsystem)andthereaftercommunicatewitthisprocess.
 
Garbagecollection 
-Erlangisusedtoprogram real-timesystems.Longgarbagecollectiondelaysinsuchsystemsareunacceptable.Erlang implementationsarewrittenusingbounded-timgarbagetechniques,someofthesetechniquesardescribedinARM95b,VIR95].
 
Incrementalcodeloading 
-Userscancontrol indetailhowcodeisloaded.Inembeddedsystems,allcodeisusuallyloadedatboottime.Idevelopmentsystems,codeisloadedwhenitineeded.Iftestinguncoversbugs,onlythefaultcodeneedbereplaced.
 
Robustnes
-TheErlangabstractmachine hasthreeindependenterrordetectionprimitives whichcanbeusedtostructurefault-tolerantsystems.Oneofthesemechanismsallowsprocessetomonitortheactivitiesofotherprocesses,evenitheseprocessesareexecutingonotherprocessors.Wecangroupprocessestogetherindistributesystemsandusetheseasbuildingblocksindesigningdistributedtransactionorientedsystems.
 
Timing 
-Erlanghasmechanismsforallowing processestotimeoutwhilewaitingforeventsand toreadareal-timeclock.
 
ExternalInterface
-Erlanghasa"portmechanismwhichallowsprocessestocommunicatewiththeoutsideworldinamannerwhichisemanticallyequivalenttointernalmessagepassingbetweenErlangprocesses.Thismechanismiusedforcommunicationwiththehostoperating systemandforinteractionwithotherprocesse(perhapswritteninotherlanguages),whichrun onthehostoperatingsystem.Ifrequiredforrea- sonsofeciency,aspecialversionofthe\portconceptallowsothersoftwaretobedirectlylinked intotheabstractmachine.Examplesoftheusoftheportmechanismareinteractingwiththe hostlesystem,interfacingtoagraphicalinterfaceandalowlevelsocketinterface.Theexecutionmechanismslistedabovearepro- videdbytheErlangabstractmachine.ProgramwhichusethesemechanismsarecompletelyportablbetweenimplementationsofErlangrunningondierentoperatingsystemsandprocessors.ErlangstartedARM92a]asaPrologmetainterpretorwhichaddedanotionofconcurrencytProlog.HavingstartedasasetofextensionstoPro- log,Erlangslowlydriftedfromthelogictofunctional schoolofprogramming.Erlanghasmanyofthefea- turesfoundinamodernfunctionallanguage(higheorderfunctions,listcomprehensionsetc.).Itdierfrommostmodernfunctionallanguagesbyhavingdynamictypesystem(inheritedfromProlog)andan eagerevaluator.
2SequentialErlangprogram
Erlangprogramsarecomposedofmodulesinwhicfunctionsaredened.Functionsarewrittenassetsof recursionequations.Thefamiliarfactorialfunctioniwritten:
-module(math). -export(fac/0]). fac(N)whenN>0->N*fac(N-1) fac(0)->1
ThiscanbeevaluatedintheErlangshellasfollows:
>math:fac(25)15511210043330985984000000 
The
"symbolistheErlangshellprompt,the linefollowingthepromptisthevaluereturnedbythe Erlangevaluator.Theannotation 
-module(Name) 
denotesthemod- ulenamean
-export(Funs) 
isalistofthefunctionwhichthismoduleexports.Functionswhicharenot declaredintheexportlistcannotbecalledfrom 
outsid
themodule.
Inwhatfollowswewilloftenomitthemoduledeclarationswheretheyareimpliedbythetext.
Amorecomplexexamplemightbeafunctiontsearchforavalueinabinarytree:
lookup(Key,{Key,Val,_,_})-{ok,Val} lookup(Key,{Key1,Val,S,B})whenKey<Key1-> lookup(Key,S) lookup(Key,{Key1,Val,S,B})-> lookup(Key,B) lookup(Key,nil)-> not_found. 
Nodesinthetreeareareeithe
tuples 
ofthefor
{Key,Value,S,B} 
ortheatom 
ni
.
aretreeswitkeyssmallerthan 
Ke
an
aretreeswithkeysbigger orequalt
Ke
.ListsarewrittenasinProlog-asexamplesthe familia
append 
an
member 
functionsarewritten:
 
append(H|T],L)->H|append(T,L)] append(],L)->Lmember(H,H|_])->true member(H,_|T])->member(H,T) member(_,])->false
TheErlangprimitivedatatypesare:
 
atom
true,foo,bar'HelloJoe
 
Integers 
1231461426412645317156,42 
 
Floats 
3.1415926535 
 
Pids 
-processidentier
 
Ref
-guaranteeduniqueidentier
 
Funs 
-functionalobjectComplexdataobjectsarerepresentedas:
 
tuples 
-forstoringaxednumberofobjects,thus:
{a,1234
 
list
-forstoringavariablenumberofobjects,thus:
a,23,hi
Functionalobjectsareintroducedwiththesyntax:
fun(Arg1,..,ArgN)->...end. 
So,forexample,thefollowingsequenceofexpressions:
K=2F=fun(X)->X*Kend
createsafunctionalobjec
X*
whichisboundtthevariabl
.Inthisexpression 
isafreevariablan
isboundtotheinteger2.Wecanevaluat
withthesyntax 
F(Args) 
.Functions(funs)arerstclassobjects-theycan bepassedasargumentstootherfunctionsorcanbe returnedbyfunctions.Toillustratethissupposewedenethehigherorder function 
adder(C) 
asfollows:
adder(C)->fun(X)->X+Cend
Evaluating,forexample,
adder(10) 
returnsafunc- tionwhichadds10toitsargument:
>Add10=adder(10)#Fun >Add10(5)1
Suppose,also,thatwehavethefunction 
ma
denedasfollows:
map(F,H|T])->F(H)|map(T)] map(F,])->
Wecanusethisasfollows:
>map(Add10,1,2,3,4,5])11,12,13,14,15] 
Hereweusedthe 
Add10 
functionasanargumentt
ma
.Listcomprehensionsareintroducedbythesyntax 
Term||P1,P2,..,Pn] 
whereeachofthe 
P
'siseither ageneratorofthefor
Pattern<-Expression 
orpredicate.Usinglistcomprehensionswecanwritethefamiliar quick-sortroutineasfollows:
sort(X|Xs])-> sort(Y||Y<-Xs,Y<X])+X]+sort(Y||Y<-Xs,Y>=X]) sort(])->]
Wher
+
istheinxappendoperator.
3ConcurrentErlangProgram
Erlangis
concurrent 
programminglanguageparallelactivitiescanbeprogrammedinErlangitselanddonotmakeuseofanyconcurrencymechanisminthehostoperatingsystem.Erlanghasaprocessmodelofconcurrency.NeprocessarecreatedbyevaluatingtheErlangprimitive 
spawn 
send(writtenwiththeinxoperator 
)sends amessagetoaprocessand 
receive 
canbeusedtreceiveamessage.Theprimitive 
spawn(M,F,A1,A2,..,An]
cre- atesanewparallelprocess.Thisprocessevaluatesthe functio
M:F(A1,A2,..,An) 
.Whenthefunctionevaluationhascompletedtheprocessdies.Wecanwritea
echo 
processthus:
-module(echo). -export(start/0,loop/0]). start()-> spawn(echo,loop,]). loop()-> receive {From,Message}-From!Messageloop() end. spawn(echo,loop,]
causesthefunctionrep- resentedby 
echo:loop() 
tobeevaluate
inparallel 
withthecallingfunction.Thusevaluating:
of 00

Leave a Comment

You must be to leave a comment.
Submit
Characters: ...
02 / 24 / 2011This doucment made it onto the Rising List!
You must be to leave a comment.
Submit
Characters: ...