Lecturecontent TDDA69DataandProgramStructure Introduction CyrilleBerger CourseIntroduction IntroductiontothedifferentProgramming Paradigm Thedifferentprogrammingparadigms Whydifferentparadigms? IntroductiontoFunctionalProgramming Expressions Functions Control Recursion Advantagesandinconvenientsoffunctional programming 2/73 Coursegoals CourseIntroduction Describeaspectsofevaluationand executionindifferentlanguagemodels Explainanddemonstratehowdesign choicesaffecttheexpressivenessand efficacyofaprogramminglanguage Analyzeandvalueprogramminglanguages basedontheirevaluationand compilationstrategies Implementprogramminglanguagesinthe formofaninterpreterandacompiler 4 Programminglanguages Whydoyouneedtoknowhowprogramareinterpreted? Itwillhelpyouunderstandwhy programminglanguageworksacertain wayandwhatarethelimits Newprogramminglanguagesand interpretersareconstantlybeing developed Existinginterpretersareconstantly beingdeveloped Generalpurposes:C,C++,Java,Python... Specialpurposes:Prolog,Matlab,R, Agent0... Scripting:JavaScript,VBA... Historical:Fortran,Lisp... toimproveperformance,security,addnewfeatures... 5 6 Howisaprograminterpreted? Evolutionofprogramminglanguages Sourcecode Parser Parser 7 AbstractSyntaxTree Treevisitor Generator Sourcecode Bytecode VirtualMachine Assembler Assembly ... OperatingSystem CPU 8 Facebook'sworkonthePHPintepreter WhydidFacebookneedtodeveloptheirownPHPinterpreter? FacebookstartedwithPHPin2004 Backatthetime,PHPwas thegoldstandardforwebsite programmingandprototyping Butthisiscausingproblemsand forpracticalreasonsthey cannotchangeprogramming language www.govote.at493985 start 9 WhatdidFacebookdo? ThestandardPHPinterpreterisusing avirtualmachine(Zend) TheydevelopedatooltoconvertPHP toC++ Thentheydevelopedanew interpreterthatdoJust-In-Time (JIT)compilation,calledHHVM TheyintroducedHack,avariantofPHP withatypingsystem 11 10 Andotherexamples... GooglewithJava,Dalvik, PythonwithCPythonvs Qt'sJavaScript,switchingfrom ASTInterpretationtoJITandtoa mixofJITandASTInterpretation ... 12 Listoflectures Book(s) StructureandInterpretationof ComputerProgramsinPythonby HalAbelson,JerrySussman,Julie SussmanandJohnDenero StructureandInterpretationof ComputerProgramsbyHal Abelson,JerrySussmanandJulie Sussman 1IntroductionandFunctionalProgramming 2ImperativeProgrammingandDataStructures 3Parsing 4Evaluation 5ObjectOrientedProgrammingandtypessystem 6Macrosand 7VirtualMachinesandBytecode 8GarbageCollectionandNativeCode 9ConcurrentComputing 10DeclarativeProgramming 11Logic 12Summary 13 Listoflabs 14 Divisionoftime 1FunctionalProgramming 2Supportingclassforaninterpreter 3ECMAScriptInterpreter 4Macros 5Bytecode 6Garbagecollector 7SQpy 24hlectures(in12sessions) 40hlabs(in20sessions) 8htutorials(in4 88hhomework 15 16 Lastyearevaluationandimprovments Toomuchboilerplatting,Labsand lecturesarenotwellconnected Twonewlabs Quizzesarenicebutteacherwaitfortoo long IntroductiontothedifferentProgrammingParadigm Introduceatimeout Teacherisshy,slidesaremostlyusedby theteachertoknowwhattosay Introducemoreinterractivity Theexamisworthmorethantwocredits 17 Thedifferentprogrammingparadigms 20 Declarative ProgrammingParadigm(2/2) Expresseslogicofcomputation withoutcontrolflow: Imperative declarative functional Symbolic Whatshouldbecomputedandnothowit shouldbecomputed. Examples:XML/HTML,antlr4/yacc/ regularexpressions,make/ants, SQL,... Logic Object-Oriented 21 Declarative-Examples <b>Helloworld!</b> SELECTnameFROMstudent WHEREcourseeq'TDDA69' grammarHello; r:'hello'ID; ID:[a-z]+; WS:[''\t\r\n]+->skip; 22 Functional Computationaretreated asmathematicalfunction withoutchanginganyinternalstate Examples:Lisp,Scheme, 23 24 Functional-Examples Imperative (print"HelloWorld") (take25(squares-of Expresshowcomputationare executed ->(1491625364964...576625) Describescomputationintermofstatements thatchangetheinternalstate Examples:C/C++,Pascal, Java,Python,JavaScript... 25 26 Object-Oriented Imperative-Examples Basedontheconceptofobjects, whicharedatastructures containingfieldsandmethods for(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); else printf("%cisnotavowel.\n",ch); return0; } Programsaredesignedbymakingobjects interactwitheachothers Examples:C++,Java,C#, Python,Ruby,JavaScript... 27 28 Othersparadigmlanguages Object-Oriented-Programming Logic #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"; } } BasedonFormallogic:expressingfactsand rules Symbolic Aprogramcanmanipulateitsownformulas andcomponentsasiftheyaredata Example:prolog 29 Logicprogramming likes(mary,food). likes(mary,wine). likes(john,wine). likes(john,mary). |?likes(mary,food). yes. |?-likes(john,wine). yes. |?-likes(john,food). no. 30 Symbolicprogramming d(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 31 32 Canyoudoeverythinginimperativeprogramming? www.govote.at394839 start Whydifferentparadigms? 34 Isthereaparadigmtorulethemall? Intheoryyoucanprogram everythinginC/C++and imperativeprogramming,or functionalprogramming... Butisthat Andisthat FunctionalvsImperative Doubleallthenumbersinanarray varnumbers= Imperative: vardoubled=[] for(vari=0;i<numbers.length;i++){ varnewNumber=numbers[i]*2 doubled.push(newNumber) } Functional: vardoubled=numbers.map(function(n){ returnn*2 }) 35 36 DeclarativevsImperative FunctionalvsImperative Selectallthedogsthatbelongstoaspecific Declarative: SELECT*fromdogsINNERJOINowners WHEREdogs.owner_id= owners.id Imperative: vardogsWithOwners=[] vardog,owner Imperativelanguage(C/C++,Java...) for(vardogindogs){ for(varownerinowners){ if(owner&&dog.owner_id==owner.id){ dogsWithOwners.push({dog:dog,owner:owner }) } } } Functional Basicconstructsareimperativestatements Changeexistingvalues,states x=x+ y= while(x>0) ... Basicconstructsaredeclarative Declarenewvalues functionf(x){returnx+1; Computationsareprimarilydonebyevaluatingexpressions Pureifallconstructsaredeclarative 37 IntroductiontoFunctionalProgramming 38 Expressions Expressions(1/2) Expressions(2/2) Meansofcombiningsimple elementsintocoumpoundones Primitiveexpressionstocapturethe simplestelementswewantto numbers,arithmeticexpressions... describe max(min(pow(3,5),-4),min(1,-2)*2)+6 2 plus 'hello' number operator string Callexpressions: max ( 2 , 3 ) operator operand operand 41 Names Assignment Bindsnamesto a := 2 Nowahasthevalue2 a plus 2 evaluatesto4 Meansofabstractingelementsby namingandmanipulatingthem 2 a name max name plus name plus name ( 2 'hello' b name , 3 42 ) 43 44 Whatisafunction? Assignmentisasimplemeans ofabstraction:bindsnamesto values Functiondefinitionisamore powerfulmeansofabstraction: bindsnamestoexpressions Functions 46 Functiondefinition Pureandnon-purefunction Afunctiondefinitioncontains: Asignaturewhichdefineshowmany argumentsafunctiontakes Abodywhichdefinesthecomputation performedwhenthefunctioniscalled function <name> ( parameterslist return <returnexpression> ; Purefunctions:justreturnvalues: Math.abs(-2)-> Math.pow(2,100)-> 1267650600228229401496703205376 Non-purefunctions:haveside effects: ) print(-2)-> Butprint'-2'inthe Asideeffectisnotavalue,itisanythingthat happensasaconsequenceofcallinga 47 48 Whatisalambda? Closureandlambda www.govote.at956729 start Alambdaisafunctionwithnoname vardoubled=numbers.map(function(n) { returnn*2 }) Aclosureisafunctionwithcapturedvariables Thisseemsimple,butthisisactuallyrather powerful! functioncreate_function_multiplication(number) { returnfunction(x){returnx*number;} } vardoubled= numbers.map(create_function_multiplication(2)) 49 50 ClosureinPython Pythonhaslimitedsupportforlamdas: singlestatement: numbers.map(lambdav:v*2) Control Butsupportnestedfunctions (andclosure): defcreate_function_multiplication(number): deffunction_multiplication(x): returnx*number returnfunction_multiplication doubled= numbers.map(create_function_multiplication(2)) 51 Controlflow(1/2) Controlflow(2/2) Thefunctionalwayin imperativelanguages: Inimperativeprogramming,acontrolflow statementexecutionresultinachoice betweentwopaths InJavaScript,C++,Java,Ruby...: condition?result1:result2; InPython: conditionifresult1elseresult2 Exemple:if,while... Infunctionalprogramming,itisdoneusing aspecialfunction. Forinstance,inLisp: (COND (condition1result1) (condition2result2) ... (TresultN)) 53 54 Whataboutloops? Loopconstructsisimperative Howwouldyouimplementthe equivalentofaloopinfunctional? Recursion functionfactorial(n) { varr=1; for(vari=2;i<=n;++i) { r*=i; } returnr; } 55 Whatisrecursion? Factorial:theclassicalexample(1/2) Afunctioniscalledrecursiveifthe bodyofthatfunctioncallsitself, eitherdirectlyorindirectly. Factorialin factorial::Integral->Integral factorial0=1 factorialn=n*factorial(n-1) FactorialinCommonLISP: (define(factorialn) (cond((=n0)1) (t(*n(factorial(-n1)))))) 57 Factorial:theclassicalexample(1/2) Withaloop: Recursionvsloops while(expression) { do_something(); } functionloop_something(args...) { if(expression)return; else{do_something();loop_something(args...); } } (define(loop_somethingargs...) functionfactorial(n) { varr=1; for(vari=2;i<=n;++i) { r*=i; } returnr; } Witharecursive functionfactorial(n) { return(n===0)?1:n* factorial(n-1) } 58 (cond(expression)value) (t(do_something)(loop_somethingargs...)) 59 60 Whentouserecursionratherthaniteration?(1/4) Whentouserecursionratherthaniteration?(2/4) deffactorial(n): if(n==0): return1 else: returnn*factorial(n-1) Letstry: Inmostprogramminglanguage, thenumberoffunctioncallis limitedbythesizeofthestack sys.getrecursionlimit() sys.setrecursionlimit(1003) factorial(1000) Tail-calloptimisation factorial(10) factorial(1000) 61 Whentouserecursionratherthaniteration?(3/4) 62 Whentouserecursionratherthaniteration?(4/4) Itisamatterof Recursionisabitmoregeneral thanloops Callingafunctionisusuallymoreexpensive thanaloop deffactorial2(n): r=1 foriinrange(1,n+1): r=r*i returnr timeit.timeit("factorial(30)","from__main__ importfactorial") timeit.timeit("factorial2(30)","from__main__ importfactorial2") Whenwalkingthroughatree 63 64 Noside-effectspurefunctional Inpurefunctional,callingafunctiononlyreturn avalue Theimplicationisthatcallingafunctionwiththe sameargumentswillalwaysreturnthesame value Isthewithdrawfunctionpure Advantagesandinconvenientsoffunctionalprogramming (definebalance100) (define(withdrawamount) (if(>=balanceamount) (begin(set!balance(-balanceamount)) balance) "Insufficientfunds")) www.govote.atentercode5445 start 66 Verificationandproving Provingpropertiesinfunctionalprogramming Toproveaprogramcorrect,we mustconsidereverythinga programdependson Inpurefunctionalprograms, dependenceonanydata structureisexplicit (define(powerbn) (cond((=n0)1) (t(*(powerb(-n1)))))) Claim:foranyintegern≥0andany numberb,(powerbn)=bⁿ Proof: 1)Verifythebasecase:(powerb 2)Assumethat(powerb(-n1))iscorrect 3)Verifythat(powerbn)iscorrectassumingthat (powerb(-n1))iscorrect 67 68 Concurency Provingpropertiesinimperativeprogramming Concurencyisoneofthecurrenthot topicinprogramming Themainchallengeisdata-race Imperativeprogramsareverysensible todata-racebecauseofstates Thereisnodata-raceinpure functionallanguages functionpower(b,n){ intresult=1; for(inti=0;i<n;+ +i) { result*=b; } returnresult; } Devisealoopinvariant: (n≥i)⋀(result=bⁱ) Provethatitistrueforthefirstloopiteration Provethateachloopiterationpreservesit Assumethat(n≥i)⋀(result=bⁱ) Provethat(n≥j)⋀(result=bʲ)withj=i+ alldataisimmutable allfunctionsarepure,withoutside-effects 69 70 Thedownsideoffunctionalprogramming Summaryontheupsideoffunctionalprogramming Themainadvantageisnosideeffects Inpractice,thereisverylimited needforprovingaprogram Mostlyincriticalapplications:rocketcontrol, hospital... Andhowdoyouprovehardware? Verificationandproving Concurency Productivity? Performanceissues(rememberfunction callareexpensive) Verylimitedsupport Mostprogrammingtasksrequirestates Ericssonclaimsanincreaseinproductivity between9and25timeswhenusingtheir home-grownversionofErlang 71 72 MyKeymessageaboutprogrammingparadigms Bepragmatic,thereisno oneanswer! 73