Lecture content TDDA69 Data and Program Structure Introduction Course

advertisement
Lecturecontent
TDDA69DataandProgramStructure
Introduction
CyrilleBerger
Course
Introductiontothe
differentProgramming
Thedifferentprogrammingparadigm
Paradigm
Whydifferentparadigms?
IntroductiontoFunctional
Expressions
Functions
Control
Recursion
Advantagesandinconvenientoffunctionalprogramming
2/66
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
9
WhatdidFacebookdo?
ThestandardPHPinterpreterisusing
AbstractSyntaxTreeexecution
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
ComputerProgramsbyHal
Abelson,JerrySussmanandJulie
Sussman
StructureandInterpretationof
ComputerProgramsinPythonby
HalAbelson,JerrySussman,Julie
SussmanandJohnDenero
IntroductionandFunctionalProgramming
ImperativeProgrammingandDataStructures
3
Environment
4
Evaluation
5
ObjectOrientedProgramming
6
Macrosand
7
VirtualMachinesandBytecode
8
GarbageCollectionandNativeCode
9
DistributedComputing
10
Logic
11
Guestlecture
12
Summary
1
2
13
Listoflabs
14
Divisionoftime
24hlectures(in12sessions)
40hlabs(in20sessions)
8htutorials(in4
88hhomework
FunctionalProgramming
Supportingclassforaninterpreter
3 ECMAScriptInterpreter
4 Macros
5 SupportingclassforaVirtualMachine
6 BytecodeandVirtualMachine
ThelabsareentirelynewandusingPython
1
2
15
16
Thedifferentprogrammingparadigm
IntroductiontothedifferentProgrammingParadigm
ProgrammingParadigm(2/2)
Imperative
declarative
functional
Symbolic
Logic
Object-Oriented
19
20
Declarative
Imperative
Expresseslogicofcomputation
withoutcontrolflow:
Expresshowcomputationare
executed
Examples:XML/HTML,antlr4/yacc,
make/ants...
Examples:C/C++,Pascal,
Java,Python,JavaScript...
Whatshouldbecomputedandnothowit
shouldbecomputed.
Describescomputationintermofstatements
thatchangetheinternalstate
21
Functional
22
Object-Oriented
Computationaretreated
asmathematicalfunction
Basedontheconceptofobjects,
whicharedatastructures
containingfieldsandmethods
withoutchanginganyinternalstate
Examples:Lisp,Scheme,
Programsaredesignedbymakingobjects
interactwitheachothers
Examples:C++,Java,C#,
Python,Ruby,JavaScript...
23
24
Othersparadigmlanguages
Logic
BasedonFormallogic:expressingfactsand
rules
Whydifferentparadigms?
Symbolic
Aprogramcanmanipulateitsownformulas
andcomponentsasiftheyaredata
Example:prolog
25
Canyoudoeverythinginimperativeprogramming?
Isthereaparadigmtorulethemall?
Intheoryyoucanprogram
everythinginC/C++and
impertiveprogramming
Butisthat
Andisthat
27
28
FunctionalvsImperative
DeclarativevsImperative
Doubleallthenumbersinanarray
varnumbers=
Imperative:
vardoubled=[]
for(vari=0;i<numbers.length;i++){
varnewNumber=numbers[i]*2
doubled.push(newNumber)
}
Selectallthedogsthatbelongstoaspecific
Declarative:
SELECT*fromdogsINNERJOINowners
WHEREdogs.owner_id=
owners.id
Imperative:
vardogsWithOwners=[]
vardog,owner
for(vardogindogs){
for(varownerinowners){
if(owner&&dog.owner_id==owner.id){
dogsWithOwners.push({dog:dog,owner:owner
})
}
}
}
Functional:
vardoubled=numbers.map(function(n){
returnn*2
})
29
30
FunctionalvsImperative
Imperativelanguage(C/C++,
Basicconstructsareimperative
Changeexistingvalues,states
x=x+
IntroductiontoFunctionalProgramming
Functionallanguage
Basicconstructsare
Declarenewvalues
functionf(x){returnx+1;
Computationsareprimarilydonebyevaluatingexpressions
Pureifallconstructsaredeclarative
31
Expressions(1/2)
primitiveexpressionstocapture
thesimplestelementswewant
todescribe
meansofcombiningsimple
elementsintocoumpoundones
meansofabstractingelements
bynamingandmanipulating
them
Expressions
34
Expressions(2/2)
Names
Expressionscanbenamed
2
plus
'hello'
name
a
plus
b
name name name
max (
2
,
3
name
Primitiveexpression:numbers,
arithmeticexpressions...
2
number
Call
max
operator
plus
operator
(
2
'hello'
string
operand
,
3
operand
)
Compoundexpressions
max(min(pow(3,5),-4),min(1,-2)*2)+6
35
)
36
Assignment
Question
Bindsnamesto
a
:=
2
Nowahasthevalue2
a
plus
2
evaluatesto4
Whatisthevalueofthe
finalexpressioninthis
>>>f=min
sequence?
>>>g=max
>>>g,h=min,max
>>>max=g
>>>max(f(2,g(h(1,5),3)),4)
www.govote.atentercode421308
37
38
Whatisafunction?
Functions
Assignmentisasimplemeans
ofabstraction:bindsnamesto
values
Functiondefinitionisamore
powerfulmeansofabstraction:
bindsnamestoexpressions
40
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
41
42
Closure(orlambda)
Whatisaclosure/lambda?
Aclosureisafunctionwithno
vardoubled=numbers.map(function(n)
{
returnn*2
})
Thisseemsimple,butthisisactually
ratherpowerful!
functioncreate_function_multiplication(number)
{
returnfunction(x){returnx*number;}
}
vardoubled=
numbers.map(create_function_multiplication(2))
43
44
ClosureinPython
Pythonhaslimitedsupportfor
closures:
Control
singlestatement:
numbers.map(lambdav:v*2)
Butsupportnested
defcreate_function_multiplication(number):
deffunction_addition(x):
returnx*number
returnfunction_addition
doubled=
numbers.map(create_function_multiplication(2))
45
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))
47
48
Whataboutloops?
Loopconstructsisimperative
Howwouldyouimplementthe
equivalentofaloopinfunctional?
Recursion
functionfactorial(n)
{
varr=1;
for(vari=2;i<=n;++i)
{
r*=i;
}
returnr;
}
49
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))))))
51
52
Factorial:theclassicalexample(1/2)
Withaloop:
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)
}
Recursionvsloops
(cond(expression)value)
(t(do_something)(loop_somethingargs...))
53
Whentouserecursionratherthaniteration?(1/3)
54
Whentouserecursionratherthaniteration?(2/3)
Inmostprogramminglanguage,
thenumberoffunctioncallis
limitedbythesizeofthestack
deffactorial(n):
if(n==0):
return1
else:
returnn*factorial(n-1)
factorial(10)->3628800
factorial(1000)->RuntimeError:
maximumrecursiondepthexceeded
incomparison
sys.getrecursionlimit()->1000
sys.setrecursionlimit(1003)
factorial(1000)->4023872600...00000
Tail-calloptimisation
Callingafunctionisusuallymore
expensivethanaloop
55
56
Whentouserecursionratherthaniteration?(3/3)
Itisamatterof
Recursionisabitmoregeneral
thanloops
Advantagesandinconvenientoffunctionalprogramming
Whenwalkingthroughatree
57
Verificationandproving
Noside-effectspurefunctional
Toproveaprogramcorrect,must
considereverythingaprogram
dependson
Inpurefunctionalprograms,
depdenceonanydatastructure
isexplicit
Inpurefunctional,callingafunctiononlyreturn
avalue
Theimplicationisthatcallingafunctionwiththe
sameargumentswillalwaysreturnthesame
value
Isthewithdrawfunctionpurefunctional?
(definebalance100)
(define(withdrawamount)
(if(>=balanceamount)
(begin(set!balance(-balance
amount))
balance)
"Insufficientfunds"))
www.govote.atentercode315737
59
60
Provingpropertiesinfunctionalprogramming
Provingpropertiesinimperativeprogramming
(define(powerbn)
(cond((=n0)1)
(t(*(powerb(-n1))))))
Claim:foranyintegern≥0andany
numberb,(powerbn)=bⁿ
Proof:
functionpower(b,n){
intresult=1;
for(inti=0;i<n;+
+i)
{
result*=b;
}
returnresult;
}
Devisealoopinvariant:
1)Verifythebasecase:(powerb
2)Assumethat(powerb(-n1))iscorrect
3)Verifythat(powerbn)iscorrectassumingthat
(powerb(-n1))iscorrect
(n≥i)⋀(result=bⁱ)
Provethatitistrueforthefirstloopiteration
Provethateachloopiterationpreservesit
Assumethat(n≥i)⋀(result=bⁱ)
Provethat(n≥j)⋀(result=bʲ)withj=i+
61
Concurency
62
Summaryontheupsideoffunctionalprogramming
Themainadvantageisnosideeffects
Concurencyisoneofthecurrenthot
topicinprogramming
Themainchallengeisdata-race
Imperativeprogramsareverysensible
todata-racebecauseofstates
Thereisnodata-raceinpure
functionallanguages
Verificationandproving
Concurency
Productivity?
Ericssonclaimsanincreaseinproductivity
between9and25timeswhenusingtheir
home-grownversionofErlang
alldataisimmutable
allfunctionsarepure,withoutside-effects
63
64
Thedownsideoffunctionalprogramming
MyKeymessageaboutprogrammingparadigms
Bepragmatic,thereisno
oneanswer!
Inpractice,thereisverylimitedneedforprovinga
program
Mostlyincriticalapplications:rocketcontrol,hospital...
Andhowdoyouprovehardware?
Performanceissues(rememberfunctioncallare
expensive)
Verylimitedsupport
Mostprogrammingtasksrequire
65
66
Download