Realizzare un interprete in OCamlpages.di.unipi.it/ferrari/CORSI/PR2/LEZIONI17-18/P3.pdf · 2017....

40
Realizzare un interprete in OCaml 1

Transcript of Realizzare un interprete in OCamlpages.di.unipi.it/ferrari/CORSI/PR2/LEZIONI17-18/P3.pdf · 2017....

Page 1: Realizzare un interprete in OCamlpages.di.unipi.it/ferrari/CORSI/PR2/LEZIONI17-18/P3.pdf · 2017. 11. 10. · La stru(ura nel de(aglio OCAML Type per descrivere la rappresentazione

RealizzareuninterpreteinOCaml

1

Page 2: Realizzare un interprete in OCamlpages.di.unipi.it/ferrari/CORSI/PR2/LEZIONI17-18/P3.pdf · 2017. 11. 10. · La stru(ura nel de(aglio OCAML Type per descrivere la rappresentazione

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

Page 3: Realizzare un interprete in OCamlpages.di.unipi.it/ferrari/CORSI/PR2/LEZIONI17-18/P3.pdf · 2017. 11. 10. · La stru(ura nel de(aglio OCAML Type per descrivere la rappresentazione

Lastru(uranelde(aglio

OCAMLTypeperdescriverelarappresentazioneintermedia

typevariable=stringtypeop=Plus|Minus|Times|…typeexp=

|Int_eofint|Op_eofexp*op*exp|Var_eofvariable|Let_eofvariable*exp*exp

typevalue=exp

Page 4: Realizzare un interprete in OCamlpages.di.unipi.it/ferrari/CORSI/PR2/LEZIONI17-18/P3.pdf · 2017. 11. 10. · La stru(ura nel de(aglio OCAML Type per descrivere la rappresentazione

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”

Page 5: Realizzare un interprete in OCamlpages.di.unipi.it/ferrari/CORSI/PR2/LEZIONI17-18/P3.pdf · 2017. 11. 10. · La stru(ura nel de(aglio OCAML Type per descrivere la rappresentazione

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

Page 6: Realizzare un interprete in OCamlpages.di.unipi.it/ferrari/CORSI/PR2/LEZIONI17-18/P3.pdf · 2017. 11. 10. · La stru(ura nel de(aglio OCAML Type per descrivere la rappresentazione

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

Page 7: Realizzare un interprete in OCamlpages.di.unipi.it/ferrari/CORSI/PR2/LEZIONI17-18/P3.pdf · 2017. 11. 10. · La stru(ura nel de(aglio OCAML Type per descrivere la rappresentazione

Variabili:dichiarazioneeuso

typevariable=stringtypeexp=

|Int_eofint|Op_eofexp*op*exp|Var_eofvariable|Let_eofvariable*exp*exp

Page 8: Realizzare un interprete in OCamlpages.di.unipi.it/ferrari/CORSI/PR2/LEZIONI17-18/P3.pdf · 2017. 11. 10. · La stru(ura nel de(aglio OCAML Type per descrivere la rappresentazione

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

Page 9: Realizzare un interprete in OCamlpages.di.unipi.it/ferrari/CORSI/PR2/LEZIONI17-18/P3.pdf · 2017. 11. 10. · La stru(ura nel de(aglio OCAML Type per descrivere la rappresentazione

Variabili:dichiarazioneeuso

Typevariable=stringtypeexp=

|Int_eofint|Op_eofexp*op*exp|Var_eofvariable|Let_eofvariable*exp*exp

Usodiuna

variabile

Page 10: Realizzare un interprete in OCamlpages.di.unipi.it/ferrari/CORSI/PR2/LEZIONI17-18/P3.pdf · 2017. 11. 10. · La stru(ura nel de(aglio OCAML Type per descrivere la rappresentazione

Variabili:dichiarazioneeuso

Typevariable=stringtypeexp=

|Int_eofint|Op_eofexp*op*exp|Var_eofvariable|Let_eofvariable*exp*exp

Usodiuna

variabile

Dichiarazionedivariable

Page 11: Realizzare un interprete in OCamlpages.di.unipi.it/ferrari/CORSI/PR2/LEZIONI17-18/P3.pdf · 2017. 11. 10. · La stru(ura nel de(aglio OCAML Type per descrivere la rappresentazione

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

Page 12: Realizzare un interprete in OCamlpages.di.unipi.it/ferrari/CORSI/PR2/LEZIONI17-18/P3.pdf · 2017. 11. 10. · La stru(ura nel de(aglio OCAML Type per descrivere la rappresentazione

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

Page 13: Realizzare un interprete in OCamlpages.di.unipi.it/ferrari/CORSI/PR2/LEZIONI17-18/P3.pdf · 2017. 11. 10. · La stru(ura nel de(aglio OCAML Type per descrivere la rappresentazione

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

Page 14: Realizzare un interprete in OCamlpages.di.unipi.it/ferrari/CORSI/PR2/LEZIONI17-18/P3.pdf · 2017. 11. 10. · La stru(ura nel de(aglio OCAML Type per descrivere la rappresentazione

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

Page 15: Realizzare un interprete in OCamlpages.di.unipi.it/ferrari/CORSI/PR2/LEZIONI17-18/P3.pdf · 2017. 11. 10. · La stru(ura nel de(aglio OCAML Type per descrivere la rappresentazione

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

Page 16: Realizzare un interprete in OCamlpages.di.unipi.it/ferrari/CORSI/PR2/LEZIONI17-18/P3.pdf · 2017. 11. 10. · La stru(ura nel de(aglio OCAML Type per descrivere la rappresentazione

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

Page 17: Realizzare un interprete in OCamlpages.di.unipi.it/ferrari/CORSI/PR2/LEZIONI17-18/P3.pdf · 2017. 11. 10. · La stru(ura nel de(aglio OCAML Type per descrivere la rappresentazione

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

Page 18: Realizzare un interprete in OCamlpages.di.unipi.it/ferrari/CORSI/PR2/LEZIONI17-18/P3.pdf · 2017. 11. 10. · La stru(ura nel de(aglio OCAML Type per descrivere la rappresentazione

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

Page 19: Realizzare un interprete in OCamlpages.di.unipi.it/ferrari/CORSI/PR2/LEZIONI17-18/P3.pdf · 2017. 11. 10. · La stru(ura nel de(aglio OCAML Type per descrivere la rappresentazione

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

Page 20: Realizzare un interprete in OCamlpages.di.unipi.it/ferrari/CORSI/PR2/LEZIONI17-18/P3.pdf · 2017. 11. 10. · La stru(ura nel de(aglio OCAML Type per descrivere la rappresentazione

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

Page 21: Realizzare un interprete in OCamlpages.di.unipi.it/ferrari/CORSI/PR2/LEZIONI17-18/P3.pdf · 2017. 11. 10. · La stru(ura nel de(aglio OCAML Type per descrivere la rappresentazione

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

Page 22: Realizzare un interprete in OCamlpages.di.unipi.it/ferrari/CORSI/PR2/LEZIONI17-18/P3.pdf · 2017. 11. 10. · La stru(ura nel de(aglio OCAML Type per descrivere la rappresentazione

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

Page 23: Realizzare un interprete in OCamlpages.di.unipi.it/ferrari/CORSI/PR2/LEZIONI17-18/P3.pdf · 2017. 11. 10. · La stru(ura nel de(aglio OCAML Type per descrivere la rappresentazione

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:

Page 24: Realizzare un interprete in OCamlpages.di.unipi.it/ferrari/CORSI/PR2/LEZIONI17-18/P3.pdf · 2017. 11. 10. · La stru(ura nel de(aglio OCAML Type per descrivere la rappresentazione

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

Page 25: Realizzare un interprete in OCamlpages.di.unipi.it/ferrari/CORSI/PR2/LEZIONI17-18/P3.pdf · 2017. 11. 10. · La stru(ura nel de(aglio OCAML Type per descrivere la rappresentazione

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

Page 26: Realizzare un interprete in OCamlpages.di.unipi.it/ferrari/CORSI/PR2/LEZIONI17-18/P3.pdf · 2017. 11. 10. · La stru(ura nel de(aglio OCAML Type per descrivere la rappresentazione

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

Page 27: Realizzare un interprete in OCamlpages.di.unipi.it/ferrari/CORSI/PR2/LEZIONI17-18/P3.pdf · 2017. 11. 10. · La stru(ura nel de(aglio OCAML Type per descrivere la rappresentazione

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!!

Page 28: Realizzare un interprete in OCamlpages.di.unipi.it/ferrari/CORSI/PR2/LEZIONI17-18/P3.pdf · 2017. 11. 10. · La stru(ura nel de(aglio OCAML Type per descrivere la rappresentazione

Funzioni

Page 29: Realizzare un interprete in OCamlpages.di.unipi.it/ferrari/CORSI/PR2/LEZIONI17-18/P3.pdf · 2017. 11. 10. · La stru(ura nel de(aglio OCAML Type per descrivere la rappresentazione

Sintassi

typeexp=Int_eofint|Op_eofexp*op*exp|Var_eofvariable|Let_eofvariable*exp*exp|Fun_eofvariable*exp|FunCall_eofexp*exp

Page 30: Realizzare un interprete in OCamlpages.di.unipi.it/ferrari/CORSI/PR2/LEZIONI17-18/P3.pdf · 2017. 11. 10. · La stru(ura nel de(aglio OCAML Type per descrivere la rappresentazione

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)

Page 31: Realizzare un interprete in OCamlpages.di.unipi.it/ferrari/CORSI/PR2/LEZIONI17-18/P3.pdf · 2017. 11. 10. · La stru(ura nel de(aglio OCAML Type per descrivere la rappresentazione

EstendiamoilRTS

letis_value(e:exp):bool=matchewith |Int_e_->true |Fun_e(_,_)->true |(Op_e(_,_,_) |Let_e(_,_,_) |Var_e_ |FunCall_e(_,_))->false

Lefunzionisonovalori!!

Page 32: Realizzare un interprete in OCamlpages.di.unipi.it/ferrari/CORSI/PR2/LEZIONI17-18/P3.pdf · 2017. 11. 10. · La stru(ura nel de(aglio OCAML Type per descrivere la rappresentazione

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)

Page 33: Realizzare un interprete in OCamlpages.di.unipi.it/ferrari/CORSI/PR2/LEZIONI17-18/P3.pdf · 2017. 11. 10. · La stru(ura nel de(aglio OCAML Type per descrivere la rappresentazione

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

Page 34: Realizzare un interprete in OCamlpages.di.unipi.it/ferrari/CORSI/PR2/LEZIONI17-18/P3.pdf · 2017. 11. 10. · La stru(ura nel de(aglio OCAML Type per descrivere la rappresentazione

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

Page 35: Realizzare un interprete in OCamlpages.di.unipi.it/ferrari/CORSI/PR2/LEZIONI17-18/P3.pdf · 2017. 11. 10. · La stru(ura nel de(aglio OCAML Type per descrivere la rappresentazione

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)

Page 36: Realizzare un interprete in OCamlpages.di.unipi.it/ferrari/CORSI/PR2/LEZIONI17-18/P3.pdf · 2017. 11. 10. · La stru(ura nel de(aglio OCAML Type per descrivere la rappresentazione

Comevalutarelefunzioniricorsive

IDEA(recfx=body)arg-->body[arg/x][recfx=body/f]

Passaggioparametri

JITcompilaPondelbody

Page 37: Realizzare un interprete in OCamlpages.di.unipi.it/ferrari/CORSI/PR2/LEZIONI17-18/P3.pdf · 2017. 11. 10. · La stru(ura nel de(aglio OCAML Type per descrivere la rappresentazione

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

Page 38: Realizzare un interprete in OCamlpages.di.unipi.it/ferrari/CORSI/PR2/LEZIONI17-18/P3.pdf · 2017. 11. 10. · La stru(ura nel de(aglio OCAML Type per descrivere la rappresentazione

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

Page 39: Realizzare un interprete in OCamlpages.di.unipi.it/ferrari/CORSI/PR2/LEZIONI17-18/P3.pdf · 2017. 11. 10. · La stru(ura nel de(aglio OCAML Type per descrivere la rappresentazione

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

Page 40: Realizzare un interprete in OCamlpages.di.unipi.it/ferrari/CORSI/PR2/LEZIONI17-18/P3.pdf · 2017. 11. 10. · La stru(ura nel de(aglio OCAML Type per descrivere la rappresentazione

Cosaabbiamoimparato?

•  OCamlpuòessereusatocomelinguaggioperlasimulazionedellasemanJcaoperazionalediunlinguaggio(inclusosestesso!)

•  Vantaggio:simulazionedell’implementazione•  Svantaggio:complicatoperleoperazionidaeffe(uareconiJpidiOcaml– Op_e(e1,Plus,e2)rispe(oa“e1+e2”