lecture content - idatdda69/lectures/2015/01_introduction.pdf · tdda69 data and program structure...

Post on 22-Sep-2019

2 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

TDDA69DataandProgramStructureIntroductionCyrilleBerger

2/66

LecturecontentCourseIntroductiontothedifferentProgrammingParadigmThedifferentprogrammingparadigmWhydifferentparadigms?IntroductiontoFunctionalExpressionsFunctionsControlRecursionAdvantagesandinconvenientoffunctionalprogramming

CourseIntroduction

4

CoursegoalsDescribeaspectsofevaluationandexecutionindifferentlanguagemodelsExplainanddemonstratehowdesignchoicesaffecttheexpressivenessandefficacyofaprogramminglanguageAnalyzeandvalueprogramminglanguagesbasedontheirevaluationandcompilationstrategiesImplementprogramminglanguagesintheformofaninterpreterandacompiler

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

9

Facebook'sworkonthePHPintepreter

FacebookstartedwithPHPin2004Backatthetime,PHPwasthegoldstandardforwebsiteprogrammingandprototypingButthisiscausingproblemsandforpracticalreasonstheycannotchangeprogramminglanguage

10

WhydidFacebookneedtodeveloptheirownPHPinterpreter?

11

WhatdidFacebookdo?ThestandardPHPinterpreterisusingAbstractSyntaxTreeexecutionTheydevelopedatooltoconvertPHPtoC++ThentheydevelopedanewinterpreterthatdoJust-In-Time(JIT)compilation,calledHHVMTheyintroducedHack,avariantofPHPwithatypingsystem

12

Andotherexamples...GooglewithJava,Dalvik,PythonwithCPythonvsQt'sJavaScript,switchingfromASTInterpretationtoJITandtoamixofJITandASTInterpretation...

13

Listoflectures1IntroductionandFunctionalProgramming2ImperativeProgrammingandDataStructures3Environment4Evaluation5ObjectOrientedProgramming6Macrosand7VirtualMachinesandBytecode8GarbageCollectionandNativeCode9DistributedComputing

10Logic11Guestlecture12Summary

14

Book(s)StructureandInterpretationofComputerProgramsbyHalAbelson,JerrySussmanandJulieSussmanStructureandInterpretationofComputerProgramsinPythonbyHalAbelson,JerrySussman,JulieSussmanandJohnDenero

15

Listoflabs1FunctionalProgramming2Supportingclassforaninterpreter3ECMAScriptInterpreter4Macros5SupportingclassforaVirtualMachine6BytecodeandVirtualMachineThelabsareentirelynewandusingPython

16

Divisionoftime24hlectures(in12sessions)40hlabs(in20sessions)8htutorials(in488hhomework

IntroductiontothedifferentProgrammingParadigm Thedifferentprogrammingparadigm

19 20

ProgrammingParadigm(2/2)declarative

functional

Imperative

Object-Oriented

LogicSymbolic

21

DeclarativeExpresseslogicofcomputationwithoutcontrolflow:Whatshouldbecomputedandnothowitshouldbecomputed.Examples:XML/HTML,antlr4/yacc,make/ants...

22

ImperativeExpresshowcomputationareexecutedDescribescomputationintermofstatementsthatchangetheinternalstateExamples:C/C++,Pascal,Java,Python,JavaScript...

23

FunctionalComputationaretreatedasmathematicalfunctionwithoutchanginganyinternalstateExamples:Lisp,Scheme,

24

Object-OrientedBasedontheconceptofobjects,whicharedatastructurescontainingfieldsandmethodsProgramsaredesignedbymakingobjectsinteractwitheachothersExamples:C++,Java,C#,Python,Ruby,JavaScript...

25

OthersparadigmlanguagesLogicBasedonFormallogic:expressingfactsandrulesSymbolicAprogramcanmanipulateitsownformulasandcomponentsasiftheyaredataExample:prolog

Whydifferentparadigms?

27

Canyoudoeverythinginimperativeprogramming?

28

Isthereaparadigmtorulethemall?

IntheoryyoucanprogrameverythinginC/C++andimpertiveprogrammingButisthatAndisthat

29

FunctionalvsImperativeDoubleallthenumbersinanarrayvarnumbers=Imperative:vardoubled=[]for(vari=0;i<numbers.length;i++){varnewNumber=numbers[i]*2doubled.push(newNumber)}Functional:vardoubled=numbers.map(function(n){returnn*2})

30

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})}}}

31

FunctionalvsImperativeImperativelanguage(C/C++,BasicconstructsareimperativeChangeexistingvalues,statesx=x+

FunctionallanguageBasicconstructsareDeclarenewvaluesfunctionf(x){returnx+1;ComputationsareprimarilydonebyevaluatingexpressionsPureifallconstructsaredeclarative

IntroductiontoFunctionalProgramming

Expressions

34

Expressions(1/2)primitiveexpressionstocapturethesimplestelementswewanttodescribemeansofcombiningsimpleelementsintocoumpoundonesmeansofabstractingelementsbynamingandmanipulatingthem

35

Expressions(2/2)Primitiveexpression:numbers,arithmeticexpressions...

2 plus 'hello'number operator stringCallmax ( 2 , 3 )

operator operand operandCompoundexpressionsmax(min(pow(3,5),-4),min(1,-2)*2)+6

36

NamesExpressionscanbenamed

2 plus 'hello'name

a plus bname name namemax ( 2 , 3 )name

37

AssignmentBindsnamesto

a := 2Nowahasthevalue2

a plus 2evaluatesto4

38

QuestionWhatisthevalueofthefinalexpressioninthissequence?>>>f=min>>>g=max>>>g,h=min,max>>>max=g>>>max(f(2,g(h(1,5),3)),4)

www.govote.atentercode421308

Functions

40

Whatisafunction?Assignmentisasimplemeansofabstraction:bindsnamestovaluesFunctiondefinitionisamorepowerfulmeansofabstraction:bindsnamestoexpressions

41

FunctiondefinitionAfunctiondefinitioncontains:AsignaturewhichdefineshowmanyargumentsafunctiontakesAbodywhichdefinesthecomputationperformedwhenthefunctioniscalledfunction <name> ( parameterslist )

return <returnexpression> ;

42

Pureandnon-purefunctionPurefunctions:justreturnvalues:Math.abs(-2)->Math.pow(2,100)->1267650600228229401496703205376Non-purefunctions:havesideeffects:print(-2)->Butprint'-2'intheAsideeffectisnotavalue,itisanythingthathappensasaconsequenceofcallinga

43

Whatisaclosure/lambda?

44

Closure(orlambda)Aclosureisafunctionwithnovardoubled=numbers.map(function(n){returnn*2})Thisseemsimple,butthisisactuallyratherpowerful!functioncreate_function_multiplication(number){returnfunction(x){returnx*number;}}vardoubled=numbers.map(create_function_multiplication(2))

45

ClosureinPythonPythonhaslimitedsupportforclosures:singlestatement:numbers.map(lambdav:v*2)Butsupportnesteddefcreate_function_multiplication(number):deffunction_addition(x):returnx*numberreturnfunction_additiondoubled=numbers.map(create_function_multiplication(2))

Control

47

Controlflow(1/2)Inimperativeprogramming,acontrolflowstatementexecutionresultinachoicebetweentwopathsExemple:if,while...Infunctionalprogramming,itisdoneusingaspecialfunction.Forinstance,inLisp:(COND(condition1result1)(condition2result2)...(TresultN))

48

Controlflow(2/2)Thefunctionalwayinimperativelanguages:InJavaScript,C++,Java,Ruby...):condition?result1:result2;InPython:conditionifresult1elseresult2

49

Whataboutloops?LoopconstructsisimperativeHowwouldyouimplementtheequivalentofaloopinfunctional?functionfactorial(n){varr=1;for(vari=2;i<=n;++i){r*=i;}returnr;}

Recursion

51

Whatisrecursion?Afunctioniscalledrecursiveifthebodyofthatfunctioncallsitself,eitherdirectlyorindirectly.

52

Factorial:theclassicalexample(1/2)

Factorialinfactorial::Integral->Integralfactorial0=1factorialn=n*factorial(n-1)FactorialinCommonLISP:(define(factorialn)(cond((=n0)1)(t(*n(factorial(-n1))))))

53

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)}

54

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...))

55

Whentouserecursionratherthaniteration?(1/3)

deffactorial(n):if(n==0):return1else:returnn*factorial(n-1)factorial(10)->3628800factorial(1000)->RuntimeError:maximumrecursiondepthexceededincomparison

56

Whentouserecursionratherthaniteration?(2/3)

Inmostprogramminglanguage,thenumberoffunctioncallislimitedbythesizeofthestacksys.getrecursionlimit()->1000sys.setrecursionlimit(1003)factorial(1000)->4023872600...00000Tail-calloptimisationCallingafunctionisusuallymoreexpensivethanaloop

57

Whentouserecursionratherthaniteration?(3/3)

ItisamatterofRecursionisabitmoregeneralthanloopsWhenwalkingthroughatree Advantagesandinconvenientoffunctionalprogramming

59

Noside-effectspurefunctionalInpurefunctional,callingafunctiononlyreturnavalueTheimplicationisthatcallingafunctionwiththesameargumentswillalwaysreturnthesamevalueIsthewithdrawfunctionpurefunctional?(definebalance100)(define(withdrawamount)(if(>=balanceamount)(begin(set!balance(-balanceamount))balance)"Insufficientfunds"))www.govote.atentercode315737

60

VerificationandprovingToproveaprogramcorrect,mustconsidereverythingaprogramdependsonInpurefunctionalprograms,depdenceonanydatastructureisexplicit

61

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

62

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+

63

ConcurencyConcurencyisoneofthecurrenthottopicinprogrammingThemainchallengeisdata-raceImperativeprogramsareverysensibletodata-racebecauseofstatesThereisnodata-raceinpurefunctionallanguagesalldataisimmutableallfunctionsarepure,withoutside-effects

64

Summaryontheupsideoffunctionalprogramming

Themainadvantageisnoside-effectsVerificationandprovingConcurencyProductivity?Ericssonclaimsanincreaseinproductivitybetween9and25timeswhenusingtheirhome-grownversionofErlang

65

Thedownsideoffunctionalprogramming

Inpractice,thereisverylimitedneedforprovingaprogramMostlyincriticalapplications:rocketcontrol,hospital...Andhowdoyouprovehardware?

Performanceissues(rememberfunctioncallareexpensive)VerylimitedsupportMostprogrammingtasksrequire

66

MyKeymessageaboutprogrammingparadigms

Bepragmatic,thereisnooneanswer!

top related