Listoflectures IntroductionandFunctionalProgramming ImperativeProgrammingandDataStructures 3 Environment 4 Evaluation 5 ObjectOrientedProgramming 6 Macrosand 7 VirtualMachinesandBytecode 8 GarbageCollectionandNativeCode 9 ParallelandDistributedComputing 10 DeclarativeandLogicProgrammingandStatic ProgramAnalysis 11 Summary 1 2 TDDA69DataandProgramStructure Summary CyrilleBerger 2/48 Lecturegoal Lecturecontent Provideasummaryofwhat hasbeenstudiedinthelecture Demonstratehowthe conceptsconnecttogether Theconscequencesofthedesign choiceinprogramminglanguages Choosinganappropriate ProgrammingLanguageParadigm Thedifferenttypesofinterpreter Thedynamic/staticbalance inImperativeProgramming Languages Final Whatisincluded? Examples 3/48 4/48 Doweneednewprogramminglanguages? NewConcepts(intheearly days,objectorientation) Newproblemsandnew infrastructure(multi-threading, distributedcomputing...) Developabetter MotivationforcreatingGo RobPike,Gocreator:‟Acoupleofyearsago,several ofusatGooglebecamealittlefrustratedwiththe softwaredevelopmentprocess,andparticularlyusing C++towritelargeserversoftware.Wefoundthatthe binariestendedtobemuchtoobig.Theytook toolongtocompile.Andthelanguageitself,which isprettymuchthemainsystemsoftwarelanguagein theworldrightnow,isaveryoldlanguage.Alotof theideasandchangesinhardwarethathave comeaboutinthelastcoupleofdecadeshaven’thad achancetoinfluenceC++.” 5/48 MotivationforcreatingRust 6/48 UsageofC++vsGovsRust GraydonHoare,Rustcreator:‟Alotofobviousgood ideas,knownandlovedinotherlanguages,haven't madeitintowidely-usedsystemslanguages,orare deployedinlanguagesthathaveverypoor(unsafe, concurrency-hostile)memorymodels.Therewerealotof goodcompetitorsinthelate70sandearly80sinthat space,andIwantedtorevivesomeoftheirideasand givethemanothergo,onthetheorythatcircumstances havechanged:theinternetishighlyconcurrentandhighly security-conscious,sothedesign-tradeoffsthatalways favorCandC++(forexample)havebeenshifting.” 7/48 8/48 Isiteasiertochangeandfixexistinglanguages? Backward-compatiblechanges Backward-incompatiblechanges PHPCriticism PHPwasnotdesigned,butdeveloped organically Inconsistentnamingoffunctionsandorderoftheir Somechangesaretoo IntroducingUnicodeinPythonand GarbagecollectorinC+ Somefunctionnameswerechosentoimprovethedistributionofhashvalues Ratherthanabortingwithanerror,PHPwilltrytoguess thedeveloperintent Problemswithweaktyping Thosechangesintroducelong developmenttimeandlong acceptancetime PHPcompilationoptions,serverconfigurations,applications configurationsandglobalstatescanaffectfunction behaviour Incoherentmixbetweenfunctionalandobjectorientedprogramming ... Youneedavisionandadesignwhendeveloping aprogramminglanguage! Python3.0wasintroducedin PHP6wasstartedin2006andnever 9/48 DesignConsiderationforaProgrammingLanguage Aprogramminglanguagemustbe: predictable Sourcecodeisreadmoreoftenthanwritten,ahumanmustbeableto understandwhatheread consistent Knowingpartofalanguageshouldhelplearnotherparts concise,simpleandgeneral reliable Programminglanguageareheretosolveproblem,nottointroducenewone debuggable Developerswillinevabilitywritebugs,theyneedallthehelptheycangetto findthem 10/48 DesignchoicesforaProgrammingLanguage ProgrammingParadigm DynamicvsStatic Low-levelvsHigh-level Directinterpretation,Virtual Machine,JIT,Native Compilation... implementable Thisreducethenumberofbugsinthelanguageimplementation 11/48 12/48 Whatisthepurposeofthenewlanguage? Firstquestionis,anew language,whatfor? Choosinganappropriate ProgrammingLanguageParadigm Queryingknowledge? Distributednumericalcomputation? WrittingdriversforanOperatingSystem? Writtingwebapplications? AnsweringtheUltimateQuestionofLife,the Universe,andEverything ... 13/48 Declarative ProgrammingParadigms Expresseslogicofcomputation withoutcontrolflow: Imperative declarative functional Whatshouldbecomputedandnothowit shouldbecomputed. Examples:XML/HTML,antlr4/yacc, make/ants... Logic Object-Oriented 15 16 Functional LogicProgramming Computationaretreated asmathematicalfunction BasedonFormallogic:expressing factsandrules Examples:Prolog withoutchanginganyinternalstate Examples:Lisp,Scheme, 17 Imperative 18 Object-Oriented Expresshowcomputationare executed Basedontheconceptofobjects, whicharedatastructures containingfieldsandmethods Describescomputationintermofstatements thatchangetheinternalstate Programsaredesignedbymakingobjects interactwitheachothers Examples:C/C++,Pascal, Java,Python,JavaScript... Examples:C++,Java,C#, Python,Ruby,JavaScript... 19 20 Conscequencesofthechoice Howtochooseaprogrammingparadigm? Conscequencefortheusers Thechoicedependsonthegoal Querying Expressivity,simplicity,readability Conscequenceonthe implementation ⇒Declarativeorlogic(reasoning) Distributednumerical ⇒Functional WrittingdriversforanOperating Differenttypesofinterpreters ⇒Imperative Writtingweb ⇒Object-Oriented Thechoicecanbeconsidered controversial! 21 22 Howisaprograminterpreted? Sourcecode Thedifferenttypesofinterpreter Parser Parser AbstractSyntaxTree Treevisitor Generator Sourcecode Bytecode VirtualMachine Assembler Assembly ... OperatingSystem CPU 24 InterpretingDeclarativeandLogic InterpretingFunctional Usingaqueryexecutor(forSQL) Usingareasoningengine(for Prolog) Functionsareevaluatedinorder ApplicativevsNormal Innormalorder,thetreeneedsto beexpanded,virtualmachines aremoredifficult 25 InterpretingImperative/Object-oriented Directlyfromtheabstract syntaxtree Usingvirtualmachines RunningontheCPU Themoreversatile,the morecommon,themost studied 26 Conscequencesofthechoiceofprogrammingparadigm Generality,expressivity andsimplicity Howtointerpret 27 28 Dynamicvsstatic Thedynamic/staticbalanceinImperativeProgrammingLanguages Dynamic/statictyping, classdefinition Strong/Weaktyping Abalancebetweeneaseof use,performanceand validation 30 ClassDefinition Type-Checking Static-typechecking Staticclass Dynamic-typechecking Dynamicclass Typesarecheckedatcompilationfromstatic analysis Usuallyvariableshaveasingletype Polymorphismallowsfordynamicity Supportfromdowncasting Classmembersandfunctionsaredefinedat compilation Eachobjectisassociatedtoaclassprototype Example:C++,Java Classmembersandfunctionscanbechanged duringruntime Example:JavaScript,Python Typesarecheckedatruntime Thetypeofavariablecanchange Unchecked Example:machinecode 31 32 Dynamicvsstatic Strong/Weaktyping Advantagesof Typecastingistheprocessofconvertingfrom onetypeofvaluetoanother Performance Checking Forinstnace,fromanintegertoastring Canbeimplicitorexplicit Astrongtypingrequiremoreexplicitconversion Forinstance,inRust,itisusedtocheckthreading issues ThestrongestisAda,itonlyallows C++,Java,Pythonallowreasonableimplicitconversion Advantagesofdynamic: 10+5.0->15.0 10+'5'->Error Easierfortheuser Aweaktypingrequirefewexplicitconversion Example:php 10+5.0->15.0 10+'5'->'105' 33 34 Conscequenceontheinterpreter With Checkingmustbeimplementedinthe compiler Finalexam With Checkingmustbeimplementedduring interpretation Itishardertocompileoptimizedcodefor runningonthehardware 35 Programmingparadigms Functionalprogramming(lec 1,3,4,sicp1) Imperativeprogramming(lec 2,3,4) Objectorientedprogramming (sicp3,lec5) Logicprogramming(sicp4.4,lec 10) Whatisincluded? 38 Modularity,state Metalinguisticabstraction assignmentandlocalstate(sicp 3.1,lab1) environmentmodelof evaluation(sicp3.2,lab1,2,lec 3) streams(sicp3.5) eventdrivensimulation(sicp3.3.4) constraintprogramming(sicp 3.3.5,lab1) metacircularevaluator(sicp lazyevaluation,parameterpassing withcall-by-value,call-by-nameand call-by-need(sicp4.2,lec4) macrosandspecialforms(lab4,lec6) non-deterministincprogrammingand continuations(sicp4.3) declarativeandproceduralprogramming (sicp4.4,lec10),patternmatchingand unification(lec10) 39 40 Virtualmachinesandcompilation explicitcontrolevaluator(sicp5.4) recursionanditeration(sicp5.1.4, lec1) compilerforJavaScript(lab 2,3,5,6) garbagecollection(sicp5.3,lec8) Examples 41 Environmentmodel Concepts Assumetheexpressionbelowisevaluatedintheorder itisgiven.Whatwilltheresultbe?Drawadiagram thatcaptureswhatisgoingonaccordingtothe environmentmodelofevaluation.Marktheimportant structuresandexplainwhy,andinwhatorder,they arecreatedand(canbe)eleminated.Usethediagram toshowtheresultoftheevaluation. functionf(x) { returng(x+3)(4,5); } functiong(y,z) { returnfunction(y,z){returnz+(y*x);} } f(3) 43 Explainthefollowingconcepts substitutionmodelofevaluation tailrecursion call-be-need unification constraint abstractsyntax continuations normalorder 44 Compilersandinterpreters ProgrammingLanguageDesign Explaintheadvantagesand disadvantagesthatcharacterize compilersversusinterpreters. Whenisitmoreappropriateto useacompiler?aninterpreter? Youaretaskedtowritea programminglanguagefor executingdistributedprograms. Whatparadigmdoyouchoose andwhy? 45 Logicprogramming 46 Conclusion Implementthepredicatemultiple inPrologorQLog.Itshould decidewhetheralistcontains thesameelementtwoormore (multiple(abdbc));;istrue times. (multiple(abcd));;isfalse Theneedfornewandchangeto existingprogramminglanguages Theneedforadesignandavision Theinfluenceonchoosinga paradigmanddynamicity/ staticforinterpretation Final Youcanusetheappend predicatewithoutdefiningit. 47 48/48