Page 1
TDDA69DataandProgramStructureIntroductionCyrilleBerger
2/73
LecturecontentCourseIntroductionIntroductiontothedifferentProgrammingParadigmThedifferentprogrammingparadigmsWhydifferentparadigms?IntroductiontoFunctionalProgrammingExpressionsFunctionsControlRecursionAdvantagesandinconvenientsoffunctionalprogramming
CourseIntroduction
4
CoursegoalsDescribeaspectsofevaluationandexecutionindifferentlanguagemodelsExplainanddemonstratehowdesignchoicesaffecttheexpressivenessandefficacyofaprogramminglanguageAnalyzeandvalueprogramminglanguagesbasedontheirevaluationandcompilationstrategiesImplementprogramminglanguagesintheformofaninterpreterandacompiler
Page 2
5
Whydoyouneedtoknowhowprogramareinterpreted?
ItwillhelpyouunderstandwhyprogramminglanguageworksacertainwayandwhatarethelimitsNewprogramminglanguagesandinterpretersareconstantlybeingdevelopedExistinginterpretersareconstantlybeingdevelopedtoimproveperformance,security,addnewfeatures...
6
ProgramminglanguagesGeneralpurposes:C,C++,Java,Python...Specialpurposes:Prolog,Matlab,R,Agent0...Scripting:JavaScript,VBA...Historical:Fortran,Lisp...
7
Evolutionofprogramminglanguages
8
Howisaprograminterpreted?Sourcecode Parser
Parser
AbstractSyntaxTree Treevisitor
Generator Sourcecode ...
Bytecode VirtualMachine
Assembler Assembly OperatingSystem CPU
Page 3
9
Facebook'sworkonthePHPintepreter
FacebookstartedwithPHPin2004Backatthetime,PHPwasthegoldstandardforwebsiteprogrammingandprototypingButthisiscausingproblemsandforpracticalreasonstheycannotchangeprogramminglanguage
10
WhydidFacebookneedtodeveloptheirownPHPinterpreter?
www.govote.at493985start
11
WhatdidFacebookdo?ThestandardPHPinterpreterisusingavirtualmachine(Zend)TheydevelopedatooltoconvertPHPtoC++ThentheydevelopedanewinterpreterthatdoJust-In-Time(JIT)compilation,calledHHVMTheyintroducedHack,avariantofPHPwithatypingsystem
12
Andotherexamples...GooglewithJava,Dalvik,PythonwithCPythonvsQt'sJavaScript,switchingfromASTInterpretationtoJITandtoamixofJITandASTInterpretation...
Page 4
13
Listoflectures1IntroductionandFunctionalProgramming2ImperativeProgrammingandDataStructures3Parsing4Evaluation5ObjectOrientedProgrammingandtypessystem6Macrosand7VirtualMachinesandBytecode8GarbageCollectionandNativeCode9ConcurrentComputing10DeclarativeProgramming11Logic12Summary
14
Book(s)StructureandInterpretationofComputerProgramsinPythonbyHalAbelson,JerrySussman,JulieSussmanandJohnDeneroStructureandInterpretationofComputerProgramsbyHalAbelson,JerrySussmanandJulieSussman
15
Listoflabs1FunctionalProgramming2Supportingclassforaninterpreter3ECMAScriptInterpreter4Macros5Bytecode6Garbagecollector7SQpy
16
Divisionoftime24hlectures(in12sessions)40hlabs(in20sessions)8htutorials(in488hhomework
Page 5
17
Lastyearevaluationandimprovments
Toomuchboilerplatting,LabsandlecturesarenotwellconnectedTwonewlabsQuizzesarenicebutteacherwaitfortoolongIntroduceatimeoutTeacherisshy,slidesaremostlyusedbytheteachertoknowwhattosayIntroducemoreinterractivityTheexamisworthmorethantwocredits
IntroductiontothedifferentProgrammingParadigm
Thedifferentprogrammingparadigms
20
Page 6
21
ProgrammingParadigm(2/2)declarative
functional
Imperative
Object-Oriented
LogicSymbolic
22
DeclarativeExpresseslogicofcomputationwithoutcontrolflow:Whatshouldbecomputedandnothowitshouldbecomputed.Examples:XML/HTML,antlr4/yacc/regularexpressions,make/ants,SQL,...
23
Declarative-Examples<b>Helloworld!</b>SELECTnameFROMstudentWHEREcourseeq'TDDA69'grammarHello;r:'hello'ID;ID:[a-z]+;WS:[''\t\r\n]+->skip;
24
FunctionalComputationaretreatedasmathematicalfunctionwithoutchanginganyinternalstateExamples:Lisp,Scheme,
Page 7
25
Functional-Examples(print"HelloWorld")(take25(squares-of->(1491625364964...576625)
26
ImperativeExpresshowcomputationareexecutedDescribescomputationintermofstatementsthatchangetheinternalstateExamples:C/C++,Pascal,Java,Python,JavaScript...
27
Imperative-Examplesfor(vari=1;i<26;++i){varsq=i*i;console.log(sq)}#include<stdio.h>intmain(){charch;printf("Enteracharacter\n");scanf("%c",&ch);if(ch=='a'||ch=='A'||ch=='e'||ch=='E'||ch=='i'||ch=='I'||ch=='o'||ch=='O'||ch=='u'||ch=='U')printf("%cisavowel.\n",ch);elseprintf("%cisnotavowel.\n",ch);return0;}
28
Object-OrientedBasedontheconceptofobjects,whicharedatastructurescontainingfieldsandmethodsProgramsaredesignedbymakingobjectsinteractwitheachothersExamples:C++,Java,C#,Python,Ruby,JavaScript...
Page 8
29
Object-Oriented-Programming#include<iostream>classCharacter:publicSymbol{public:Character(char_c):m_c(_c){}boolisVowel()const{returnch=='a'||ch=='A'||ch=='e'||ch=='E'||ch=='i'||ch=='I'||ch=='o'||ch=='O'||ch=='u'||ch=='U';}private:charm_c;};intmain(){charc;std::cout<<"Enteracharacter:\n";std::cin>>c;Characterch(c);if(ch.isVowel()){std::cout<<c<<"isavowel.\n";}else{std::cout<<c<<"isnotavowel.\n";}}
30
OthersparadigmlanguagesLogicBasedonFormallogic:expressingfactsandrulesSymbolicAprogramcanmanipulateitsownformulasandcomponentsasiftheyaredataExample:prolog
31
Logicprogramminglikes(mary,food).likes(mary,wine).likes(john,wine).likes(john,mary).|?-likes(mary,food).yes.|?-likes(john,wine).yes.|?-likes(john,food).no.
32
Symbolicprogrammingd(X,X,1):-!./*d(X)w.r.t.Xis1*/d(C,X,0):-atomic(C)./*IfCisaconstant*//*thend(C)/dXis0*/d(U+V,X,R):-/*d(U+V)/dX=A+Bwhere*/d(U,X,A),/*A=d(U)/dXand*/d(V,X,B),R=A+B....d(sin(W),X,Z*cos(W)):-/*d(sin(W))/dX=Z*cos(W)*/d(W,X,Z)./*whereZ=d(W)/dX*/d(exp(W),X,Z*exp(W)):-/*d(exp(W))/dX=Z*exp(W)*/d(W,X,Z)./*whereZ=d(W)/dX*/...?-d(cos(2*X+1),X,what=2*sin(2*X
Page 9
Whydifferentparadigms?
34
Canyoudoeverythinginimperativeprogramming?
www.govote.at394839start
35
Isthereaparadigmtorulethemall?
IntheoryyoucanprogrameverythinginC/C++andimperativeprogramming,orfunctionalprogramming...ButisthatAndisthat
36
FunctionalvsImperativeDoubleallthenumbersinanarrayvarnumbers=Imperative:vardoubled=[]for(vari=0;i<numbers.length;i++){varnewNumber=numbers[i]*2doubled.push(newNumber)}Functional:vardoubled=numbers.map(function(n){returnn*2})
Page 10
37
DeclarativevsImperativeSelectallthedogsthatbelongstoaspecificDeclarative:SELECT*fromdogsINNERJOINownersWHEREdogs.owner_id=owners.idImperative:vardogsWithOwners=[]vardog,ownerfor(vardogindogs){for(varownerinowners){if(owner&&dog.owner_id==owner.id){dogsWithOwners.push({dog:dog,owner:owner})}}}
38
FunctionalvsImperativeImperativelanguage(C/C++,Java...)BasicconstructsareimperativestatementsChangeexistingvalues,statesx=x+y=while(x>0)...FunctionalBasicconstructsaredeclarativeDeclarenewvaluesfunctionf(x){returnx+1;ComputationsareprimarilydonebyevaluatingexpressionsPureifallconstructsaredeclarative
IntroductiontoFunctionalProgramming Expressions
Page 11
41
Expressions(1/2)Primitiveexpressionstocapturethesimplestelementswewanttodescribenumbers,arithmeticexpressions...2 plus 'hello'
number operator stringCallexpressions:max ( 2 , 3 )
operator operand operand42
Expressions(2/2)Meansofcombiningsimpleelementsintocoumpoundonesmax(min(pow(3,5),-4),min(1,-2)*2)+6
43
NamesMeansofabstractingelementsbynamingandmanipulatingthem2 plus 'hello'
namea plus b
name name namemax ( 2 , 3 )name
44
AssignmentBindsnamestoa := 2
Nowahasthevalue2a plus 2
evaluatesto4
Page 12
Functions
46
Whatisafunction?Assignmentisasimplemeansofabstraction:bindsnamestovaluesFunctiondefinitionisamorepowerfulmeansofabstraction:bindsnamestoexpressions
47
FunctiondefinitionAfunctiondefinitioncontains:AsignaturewhichdefineshowmanyargumentsafunctiontakesAbodywhichdefinesthecomputationperformedwhenthefunctioniscalledfunction <name> ( parameterslist )
return <returnexpression> ;
48
Pureandnon-purefunctionPurefunctions:justreturnvalues:Math.abs(-2)->Math.pow(2,100)->1267650600228229401496703205376Non-purefunctions:havesideeffects:print(-2)->Butprint'-2'intheAsideeffectisnotavalue,itisanythingthathappensasaconsequenceofcallinga
Page 13
49
Whatisalambda?www.govote.at956729start
50
ClosureandlambdaAlambdaisafunctionwithnonamevardoubled=numbers.map(function(n){returnn*2})AclosureisafunctionwithcapturedvariablesThisseemsimple,butthisisactuallyratherpowerful!functioncreate_function_multiplication(number){returnfunction(x){returnx*number;}}vardoubled=numbers.map(create_function_multiplication(2))
51
ClosureinPythonPythonhaslimitedsupportforlamdas:singlestatement:numbers.map(lambdav:v*2)Butsupportnestedfunctions(andclosure):defcreate_function_multiplication(number):deffunction_multiplication(x):returnx*numberreturnfunction_multiplicationdoubled=numbers.map(create_function_multiplication(2))
Control
Page 14
53
Controlflow(1/2)Inimperativeprogramming,acontrolflowstatementexecutionresultinachoicebetweentwopathsExemple:if,while...Infunctionalprogramming,itisdoneusingaspecialfunction.Forinstance,inLisp:(COND(condition1result1)(condition2result2)...(TresultN))
54
Controlflow(2/2)Thefunctionalwayinimperativelanguages:InJavaScript,C++,Java,Ruby...:condition?result1:result2;InPython:conditionifresult1elseresult2
55
Whataboutloops?LoopconstructsisimperativeHowwouldyouimplementtheequivalentofaloopinfunctional?functionfactorial(n){varr=1;for(vari=2;i<=n;++i){r*=i;}returnr;}
Recursion
Page 15
57
Whatisrecursion?Afunctioniscalledrecursiveifthebodyofthatfunctioncallsitself,eitherdirectlyorindirectly.
58
Factorial:theclassicalexample(1/2)
Factorialinfactorial::Integral->Integralfactorial0=1factorialn=n*factorial(n-1)FactorialinCommonLISP:(define(factorialn)(cond((=n0)1)(t(*n(factorial(-n1))))))
59
Factorial:theclassicalexample(1/2)
Withaloop:functionfactorial(n){varr=1;for(vari=2;i<=n;++i){r*=i;}returnr;}Witharecursivefunctionfactorial(n){return(n===0)?1:n*factorial(n-1)}
60
Recursionvsloopswhile(expression){do_something();}functionloop_something(args...){if(expression)return;else{do_something();loop_something(args...);}}(define(loop_somethingargs...)(cond(expression)value)(t(do_something)(loop_somethingargs...))
Page 16
61
Whentouserecursionratherthaniteration?(1/4)
deffactorial(n):if(n==0):return1else:returnn*factorial(n-1)Letstry:factorial(10)factorial(1000)
62
Whentouserecursionratherthaniteration?(2/4)
Inmostprogramminglanguage,thenumberoffunctioncallislimitedbythesizeofthestacksys.getrecursionlimit()sys.setrecursionlimit(1003)factorial(1000)Tail-calloptimisation
63
Whentouserecursionratherthaniteration?(3/4)
Callingafunctionisusuallymoreexpensivethanaloopdeffactorial2(n):r=1foriinrange(1,n+1):r=r*ireturnrtimeit.timeit("factorial(30)","from__main__importfactorial")timeit.timeit("factorial2(30)","from__main__importfactorial2")
64
Whentouserecursionratherthaniteration?(4/4)
ItisamatterofRecursionisabitmoregeneralthanloopsWhenwalkingthroughatree
Page 17
Advantagesandinconvenientsoffunctionalprogramming
66
Noside-effectspurefunctionalInpurefunctional,callingafunctiononlyreturnavalueTheimplicationisthatcallingafunctionwiththesameargumentswillalwaysreturnthesamevalueIsthewithdrawfunctionpure(definebalance100)(define(withdrawamount)(if(>=balanceamount)(begin(set!balance(-balanceamount))balance)"Insufficientfunds"))www.govote.atentercode5445start
67
VerificationandprovingToproveaprogramcorrect,wemustconsidereverythingaprogramdependsonInpurefunctionalprograms,dependenceonanydatastructureisexplicit
68
Provingpropertiesinfunctionalprogramming
(define(powerbn)(cond((=n0)1)(t(*(powerb(-n1))))))Claim:foranyintegern≥0andanynumberb,(powerbn)=bⁿProof:1)Verifythebasecase:(powerb2)Assumethat(powerb(-n1))iscorrect3)Verifythat(powerbn)iscorrectassumingthat(powerb(-n1))iscorrect
Page 18
69
Provingpropertiesinimperativeprogramming
functionpower(b,n){intresult=1;for(inti=0;i<n;++i){result*=b;}returnresult;}Devisealoopinvariant:(n≥i)⋀(result=bⁱ)ProvethatitistrueforthefirstloopiterationProvethateachloopiterationpreservesitAssumethat(n≥i)⋀(result=bⁱ)Provethat(n≥j)⋀(result=bʲ)withj=i+
70
ConcurencyConcurencyisoneofthecurrenthottopicinprogrammingThemainchallengeisdata-raceImperativeprogramsareverysensibletodata-racebecauseofstatesThereisnodata-raceinpurefunctionallanguagesalldataisimmutableallfunctionsarepure,withoutside-effects
71
Summaryontheupsideoffunctionalprogramming
Themainadvantageisnoside-effectsVerificationandprovingConcurencyProductivity?Ericssonclaimsanincreaseinproductivitybetween9and25timeswhenusingtheirhome-grownversionofErlang
72
Thedownsideoffunctionalprogramming
Inpractice,thereisverylimitedneedforprovingaprogramMostlyincriticalapplications:rocketcontrol,hospital...Andhowdoyouprovehardware?Performanceissues(rememberfunctioncallareexpensive)VerylimitedsupportMostprogrammingtasksrequirestates
Page 19
73
MyKeymessageaboutprogrammingparadigms
Bepragmatic,thereisnooneanswer!