realizzare un interprete in ocamlpages.di.unipi.it/ferrari/corsi/pr2/lezioni17-18/p3.pdf · 2017....
TRANSCRIPT
RealizzareuninterpreteinOCaml
1
Lastru(ura
Programma(text-file)letx=3inx+x;;
RappresentazioneIntermedia(IR)
Let(“x”,Num3,B_op(Plus,Var“x”,Var“x”))
Num6
Parsing
Esecuzione(Interprete)
SemanJcadellinguaggioguidaladefinizionediIRedell’esecuzione6
Pre(yPrinJng
Lastru(uranelde(aglio
OCAMLTypeperdescriverelarappresentazioneintermedia
typevariable=stringtypeop=Plus|Minus|Times|…typeexp=
|Int_eofint|Op_eofexp*op*exp|Var_eofvariable|Let_eofvariable*exp*exp
typevalue=exp
Lastru(uranelde(aglio
typevariable=stringtypeop=Plus|Minus|Times|…typeexp=
|Int_eofint|Op_eofexp*op*exp|Var_eofvariable|Let_eofvariable*exp*exp
typevalue=exp
lete1=Int_e3lete2=Int_e17lete3=Op_e(e1,Plus,e2)
Rappresentazionedi“3+17”
letx=30inlety=(letz=3inz*4)iny+y;;
Let_e(“x”,Int_e30,Let_e(“y”,Let_e(“z”,Int_e3,
Op_e(Var_e“z”,Times,Int_e4)),Op_e(Var_e“y”,Plus,Var_e“y”)
ProgrammaOCaml
Exp
Making These Ideas Precise
6
Notice how this reflects the “tree”:Let_e(“x”,Int_e 30,
Let_e(“y”,Let_e(“z”,Int_e 3,Op_e(Var_e “z”, Times, Int_e 4)),
Op_e(Var_e “y”, Plus, Var_e “y”)
let
x 30 let
y let +
z 3 *y y
z 4
AST
Variabili:dichiarazioneeuso
typevariable=stringtypeexp=
|Int_eofint|Op_eofexp*op*exp|Var_eofvariable|Let_eofvariable*exp*exp
RunJme:operazionedisupporto
letis_value(e:exp):bool=matchewith |Int_e_->true |(Op_e_ |Let_e_ |Var_e_)->false;;
eval_op:value->op->value->valuesubsPtute:value->variable->exp->exp
Variabili:dichiarazioneeuso
Typevariable=stringtypeexp=
|Int_eofint|Op_eofexp*op*exp|Var_eofvariable|Let_eofvariable*exp*exp
Usodiuna
variabile
Variabili:dichiarazioneeuso
Typevariable=stringtypeexp=
|Int_eofint|Op_eofexp*op*exp|Var_eofvariable|Let_eofvariable*exp*exp
Usodiuna
variabile
Dichiarazionedivariable
L’interprete
is_value:exp->booleval_op:value->op->value->valuesubsPtute:value->variable->exp->expletreceval(e:exp):exp=
matchewith|Int_ei->|Op_e(e1,op,e2)->|Let_e(x,e1,e2)->
RTS
L’interprete
is_value:exp->booleval_op:value->op->value->valuesubsPtute:value->variable->exp->expletreceval(e:exp):exp=
matchewith|Int_ei->Int_ei|Op_e(e1,op,e2)->|Let_e(x,e1,e2)->
RTS
L’interprete
is_value:exp->booleval_op:value->op->value->valuesubsPtute:value->variable->exp->expletreceval(e:exp):exp=
matchewith|Int_ei->Int_ei|Op_e(e1,op,e2)->letv1=evale1in letv2=evale2in eval_opv1opv2|Let_e(x,e1,e2)->
RTS
L’interprete
is_value:exp->booleval_op:value->op->value->valuesubsPtute:value->variable->exp->expletreceval(e:exp):exp=
matchewith|Int_ei->Int_ei|Op_e(e1,op,e2)->letv1=evale1in letv2=evale2in eval_opv1opv2|Let_e(x,e1,e2)->letv1=evale1in lete2’=subsPtutev1xe2in
evale2’
RTS
L’interprete
is_value:exp->booleval_op:value->op->value->valuesubsPtute:value->variable->exp->expletreceval(e:exp):exp=
matchewith|Int_ei->Int_ei|Op_e(e1,op,e2)->eval_opevale1opevale2|Let_e(x,e1,e2)->letv1=evale1in lete2’=subsPtutev1xe2in
evale2’
RTS
L’interprete
is_value:exp->booleval_op:value->op->value->valuesubsPtute:value->variable->exp->expletreceval(e:exp):exp=
matchewith|Int_ei->Int_ei|Op_e(e1,op,e2)->eval_opevale1opevale2|Let_e(x,e1,e2)->letv1=evale1in lete2’=subsPtutev1xe2in
evale2’
RTS
Comeavvienelavalutazione?
Usareletperdefinirnel’ordine
L’interprete
is_value:exp->booleval_op:value->op->value->valuesubsPtute:value->variable->exp->expletreceval(e:exp):exp=
matchewith|Int_ei->Int_ei|Op_e(e1,op,e2)->eval_opevale1opevale2|Let_e(x,e1,e2)->letv1=evale1in lete2’=subsPtutev1xe2in
evale2’|Var_ex->???
RTS
Nondovremmoincontrareunavariabile–l’avremmogiàdovutasosJtuireconunvalore!!
QuestoèunerrorediPpo
L’interprete
is_value:exp->booleval_op:value->op->value->valuesubsPtute:value->variable->exp->expletreceval(e:exp):expopPon=
matchewith|Int_ei->Some(Int_ei)|Op_e(e1,op,e2)->eval_opevale1opevale2|Let_e(x,e1,e2)->letv1=evale1in lete2’=subsPtutev1xe2in
evale2’|Var_ex->None
RTS
Questocomplical’interprete:matchingsuirisultaJdellechiamatericorsivedieval
L’interprete
is_value:exp->booleval_op:value->op->value->valuesubsPtute:value->variable->exp->expletreceval(e:exp):exp=
matchewith|Int_ei->Int_ei|Op_e(e1,op,e2)->eval_opevale1opevale2|Let_e(x,e1,e2)->letv1=evale1in lete2’=subsPtutev1xe2in
evale2’|Var_ex->raise(UnboundVariablex)
RTS
TalieccezionifannopartedelRTS
RTS:eval_op
leteval_op(v1:exp)(op:operand)(v2:exp):exp=matchv1,op,v2with |Int_ei,Plus,Int_ej->Int_e(i+j) |Int_ei,Minus,Int_ej->Int_e(i-j) |Int_ei,Times,Int_ej->Int_e(i*j) |_,(Plus|Minus|Times),_-> ifis_valuev1&&is_valuev2
thenraiseTypeError elseraiseNotValue
RTS:subsJtuJon
letsubsJtute(v:value)(x:variable)(e:exp):exp=letrecsubst(e:exp):exp= matchewith |Int_e_-> |Op_e(e1,op,e2)-> |Var_ey->...usex... |Let_e(y,e1,e2)->...usex...
insubste
RTS:subsJtuJon
letsubsJtute(v:value)(x:variable)(e:exp):exp=letrecsubst(e:exp):exp= matchewith |Int_e_->e |Op_e(e1,op,e2)-> |Var_ey->...usex... |Let_e(y,e1,e2)->...usex...
insubste
RTS:subsJtuJon
letsubsJtute(v:value)(x:variable)(e:exp):exp=letrecsubst(e:exp):exp= matchewith |Int_e_->e |Op_e(e1,op,e2)->Op_e(subste1,op,subste2) |Var_ey->...usex... |Let_e(y,e1,e2)->...usex...
insubstex,vimplici:
RTS:subsJtuJon
letsubsJtute(v:value)(x:variable)(e:exp):exp=letrecsubst(e:exp):exp= matchewith |Int_e_->e |Op_e(e1,op,e2)->Op_e(subste1,op,subste2) |Var_ey->ifx=ythenvelsee |Let_e(y,e1,e2)->...usex...
insubste
RTS:subsJtuJon
letsubsJtute(v:value)(x:variable)(e:exp):exp=letrecsubst(e:exp):exp= matchewith |Int_e_->e |Op_e(e1,op,e2)->Op_e(subste1,op,subste2) |Var_ey->ifx=ythenvelsee |Let_e(y,e1,e2)->Let_e(y,subste1,subste2)
insubste
RTS:subsJtuJon
letsubsJtute(v:value)(x:variable)(e:exp):exp=letrecsubst(e:exp):exp= matchewith |Int_e_->e |Op_e(e1,op,e2)->Op_e(subste1,op,subste2) |Var_ey->ifx=ythenvelsee |Let_e(y,e1,e2)->Let_e(y,subste1,subste2)
insubste ERRORE
Sey=x
RTS:subsJtuJon
letsubsJtute(v:value)(x:variable)(e:exp):exp=letrecsubst(e:exp):exp= matchewith |Int_e_->e |Op_e(e1,op,e2)->Op_e(subste1,op,subste2) |Var_ey->ifx=ythenvelsee |Let_e(y,e1,e2)->Let_e(y,subste1, ifx=ythene2elsesubste2)insubste Ifx=y…
shadowscope!!
Funzioni
Sintassi
typeexp=Int_eofint|Op_eofexp*op*exp|Var_eofvariable|Let_eofvariable*exp*exp|Fun_eofvariable*exp|FunCall_eofexp*exp
Sintassi
typeexp=Int_eofint|Op_eofexp*op*exp|Var_eofvariable|Let_eofvariable*exp*exp|Fun_eofvariable*exp|FunCall_eofexp*exp
LasintassiOCamlfunxevienerappresentatacomeFun_e(x,e)
Lachiamatafact3vienerappresentatacomeFunCall_e(Var_e“fact”,Int_e3)
EstendiamoilRTS
letis_value(e:exp):bool=matchewith |Int_e_->true |Fun_e(_,_)->true |(Op_e(_,_,_) |Let_e(_,_,_) |Var_e_ |FunCall_e(_,_))->false
Lefunzionisonovalori!!
Interprete++
is_value:exp->booleval_op:value->op->value->valuesubsPtute:value->variable->exp->expletreceval(e:exp):exp=
matchewith|:|Var_ex->raise(UnboundVariablex)|Fun_e(x,e)->Fun_e(x,e)|FunCall_e(e1,e2)-> matchevale1,evale2with |Fun_e(x,e),v2->eval(subsPtutev2xe) |_->raise(TypeError)
Funzioniricorsive
letg=recfx->f(x+1)ing3Let_e(“g”,
Rec_e(“f”,“x”, FunCall_e(Var_e“f”,Op_e(Var_e“x”,Plus,Int_e1))
),FunCall(Var_e“g”,Int_e3))
typeexp=Int_eofint|Op_eofexp*op*exp|Var_eofvariable|Let_eofvariable*exp*exp|Fun_eofvariable*exp|FunCall_eofexp*exp|Rec_eofvariable*variable*exp
Lefunzioniricorsivesonovalori
letis_value(e:exp):bool=matchewith |Int_e_->true |Fun_e(_,_)->true |Rec_eof(_,_,_)->true |(Op_e(_,_,_) |Let_e(_,_,_) |Var_e_ |FunCall_e(_,_))->false
LanozionedisosJtuzione
• SosJtuireilvalorevalpostodellavariabilexnellaespressionee:e[v/x]
• (x+y)[7/y]diventa(x+7)• (letx=30inlety=40inx+y)[7/y]diventa(letx=30inlety=40inx+y)
• (lety=yinlety=yiny+y)[7/y]diventa(lety=7inlety=yiny+y)
Comevalutarelefunzioniricorsive
IDEA(recfx=body)arg-->body[arg/x][recfx=body/f]
Passaggioparametri
JITcompilaPondelbody
letg=recfx->ifx<=0thenx elsex+f(x-1)
ing3
g3[recfx-> ifx<=0thenx elsex+f(x-1)/g]
(recfx->ifx<=0thenxelsex+f(x-1))
3
Ladichiarazione
LasosJtuzione
Risultatofinale
(ifx<=0thenxelsex+f(x-1))[3/x][recfx-> ifx<=0thenx elsex+f(x-1)/f]
(if3<=0then3else3+(recfx-> ifx<=0thenx elsex+f(x-1))(3-1))
Interprete
is_value:exp->booleval_op:value->op->value->valuesubsPtute:value->variable->exp->expletreceval(e:exp):exp=
matchewith|:|FunCall_e(e1,e2)-> matchevale1with |Fun_e(x,e)->letv=evale2insubsJtuteexv |(Rec_e(f,x,e))asg->letv=evale2in
subsJtute(subsJtuteexv)fg
Cosaabbiamoimparato?
• OCamlpuòessereusatocomelinguaggioperlasimulazionedellasemanJcaoperazionalediunlinguaggio(inclusosestesso!)
• Vantaggio:simulazionedell’implementazione• Svantaggio:complicatoperleoperazionidaeffe(uareconiJpidiOcaml– Op_e(e1,Plus,e2)rispe(oa“e1+e2”