The Programming Language Co re The Programming Language Co re W olfgang Schreiner Resea rch Institute fo r Symb olic Computation (RISC-Linz) Johannes Kepler Universit y , A-4040 Linz, Austria W olfgang.Schreiner@risc.un i-linz.ac.at http://www.risc.uni-linz.ac.at/p eople/schrein e W olfgang Schreiner RISC-Linz
The Programming Language Co re The Programming Language Co re � \Co re" of values and op erations establish fundamental capabilities of a language. { Numerical computation: numeric values. { T ext editing: string values. { General purp ose: co re fo r many applications. � Sta rting p oint fo r language design. � Design p rograms and study their computa- tional p o w ers. � Later extend co re b y conveniences. { Sub routines, mo dules, . . . Let's study the nature of a p rogramming lan- guage co re! W olfgang Schreiner 1
The Programming Language Co re A Co re Imp erative Language A while lo op language � Syntax domains. � Syntax rules. C 2 Command E 2 Exp ression L 2 Lo cation N 2 Numeral C ::= L:=E j C ;C j if E then C else C � 1 2 1 2 j while E do C o d j skip E ::= N j @L j E +E j : E j E =E 1 2 1 2 L ::= lo c , if i > 0 i N ::= n , if n 2 Integer Example lo c := 0; while @ lo c =0 do lo c :=@ lo c +1 o d 1 1 2 1 W olfgang Schreiner 2
The Programming Language Co re Abstract Syntax � Non-terminal symb ols. { C, E, L, N. { V a riables over syntax trees. � T erminal symb ols. { @, +, :=, skip { Lab els of syntax trees. � Inductive de�nition of syntax trees. Abstract syntax de�nes syntax trees! W olfgang Schreiner 3
The Programming Language Co re Example lo c := 0; while @ lo c =0 do lo c :=@ lo c +1 o d 1 1 2 1 C C C ; L := E while E do C od loc_1 N E = E L := E Semantics gives meaning to syntax trees! 0 loc_2 W olfgang Schreiner 4 @L N E + E loc_1 0 @L N loc_1 1
The Programming Language Co re T yping Rules � Abstruct syntax do es not de�ne w ell- fo rmed p rograms only . { Phrase \(0=1)+2" allo w ed. { Cannot add b o olean to integer. � Re�ne abstract syntax de�nition. { Integer and b o olean exp ressions. { De�ne t w o distinct syntax domains? { Better: add t yping annotations! � A ttributed syntax trees { T yp e attributes to all phrase fo rms. { Syntax tree is w ell t yp ed if t yp e attributes can b e attached to all of its nonterminals. Inference rules used fo r describing t yp e struc- tures. W olfgang Schreiner 5
The Programming Language Co re Example C: comm C: comm C: comm ; L: intloc := E: intexp while E: boolexp do C: comm od loc_1 Each subtree is annotated with its t yp e! N: int E: intexp = E: intexp L: intloc := E: intexp 0 loc_2 @ L: intloc N: int E: intexp + E: intexp loc_1 0 @ L: intloc N: loc_1 1 W olfgang Schreiner 6
The Programming Language Co re T yping Rules Command L: intlo c E: intexp C : comm C : comm 1 2 L:=E: comm C ;C : comm 1 2 E: b o olexp C : comm C : comm 1 2 if E then C else C � : comm 1 2 E: b o olexp C: comm skip : comm while E do C o d : comm Exp ression N: int L: intlo c N: intexp @L: intexp E : intexp E : intexp E: b o olexp 1 2 E +E : intexp : E: b o olexp 1 2 E : � exp E : � exp 1 2 if � 2 f int , b o ol g E =E : b o olexp 1 2 Lo cation Numeral lo c : intlo c , if i > 0 n : int , if n 2 Integer i W olfgang Schreiner 7
The Programming Language Co re T yping Rules � One t yping rule fo r each construction of each syntax rule. � Conditions under which constructions a re w ell t yp ed. � Linea r Notation (full t yp e annotation): intlo c int intexp { (( lo c ) :=((0) ) 1 ( while intlo c intexp int intexp b o olexp ((@( lo c ) ) =((0) ) ) 1 intlo c ( do ( lo c ) := 2 intlo c intexp ((@( lo c ) ) + 1 int intexp intexp comm comm comm ((1) ) ) ) ) ) . � Abb reviation (ro ot t yp e annotation): { lo c := 0; 1 while @ lo c =0 do lo c :=@ lo c +1 o d : comm 1 2 1 W olfgang Schreiner 8
The Programming Language Co re T yping Rules � Logic assertion U: � . { T ree U is w ell t yp ed with t yp e � . � Static t yping fo r language. { T yp e attributes can b e calculated without evaluating the p rogram. � Strongly t yp ed language. { No run-time incompatibilit y erro rs. � Unicit y of t yping. { Can a syntax tree b e t yp ed in multiple w a ys? � Soundness of t yping rules. { Are the t yping rules sensible in their assignment of t yp e attributes to phrases? Questions will b e addressed later. W olfgang Schreiner 9
The Programming Language Co re Induction and Recursion Syntax rule E ::= true j : E j E &E 1 2 � Inductive de�nition: { true is in Exp ression. { If E is in Exp ression, then so is : E. { If E and E a re in Exp ression, then so is E &E . 1 2 1 2 { No other trees a re in exp ression. � Exp ression = set of trees! � Generate all trees in stages. { stage = fg . 0 { stage = stage [ f : E j E 2 stage g [ f E &E j E , i +1 i i 1 2 1 E 2 stage g . 2 i { Exp ression = [ stage . i � 0 i � Any tree in Exp ression is constructed in a �nite numb er of stages. W olfgang Schreiner 10
The Programming Language Co re Structural Induction � Pro of technique fo r syntax trees. { Goal: p rove P ( t ) fo r all trees t in a a language. { Inductive base: Prove that P holds fo r all trees in stage . 1 { Inductive hyp othesis: Assume that P holds fo r all trees in stages stage with j � i . j { Inductive step: Prove that P holds fo r all trees in stage . i � Prove P ( t ) fo r all trees t in Exp ression. { P ( true ) holds. { P ( : E) holds assuming that P (E) holds (fo r a rbitra ry E). { P (E &E ) holds assuming that P (E ) holds and P (E ) 1 2 1 2 holds (fo r a rbitra ry E , E ). 1 2 Syntax rules guide the p ro of ! W olfgang Schreiner 11
The Programming Language Co re Unicit y of T yping Can a syntax tree b e t yp ed in multiple w a ys? � Unicit y of t yping p rop ert y . { Every syntax tree has as most one assignment of t yping attributes to its no des. { If P : � holds, then � is unique. � Unicit y of T yping holds fo r Numeral. { By single t yping rule, if N : � holds, then � = int (fo r all N 2 Numeral). � Unicit y of T yping holds fo r Lo cation. { By single t yping rule, if L : � holds, then � = intlo c (fo r all L 2 Lo cation). W olfgang Schreiner 12
The Programming Language Co re Unicit y of T yping � Unicit y of t yping holds fo r Exp ression: { Case N. N: int holds. By single t yping rule, N: intexp holds. { Case E +E . By inductive hyp othesis, E : � and E : � 1 2 1 1 2 2 hold fo r unique � and � . By single t yping rule, 1 2 E : intexp and E : intexp must hold. If � = � = intexp , 1 2 1 2 then E +E : intexp . Otherwise, E +E has no t yping. 1 2 1 2 { . . . � Unicit y of t yping holds fo r Command: { Case L := E. L: intlo c holds. E: � holds fo r unique � . By 1 1 single t yping rule, � must b e intexp to have L:=E: comm . 1 Otherwise, L:=E has no t yping. { . . . Unicit y of t yping holds fo r all four syntax do- mains. W olfgang Schreiner 13
The Programming Language Co re T yping Rules De�ne a Language � T yping rules (not abstract syntax) de�ne language. { Only w ell-fo rmed p rograms a re of value. { Programs a re w ell-t yp ed trees. � Signi�cance of unicit y of t yping: { Linea r rep resentation without t yp e annotations rep resents (at most) one p rogram. { Example: 0+1. � Without unicit y of t yping: { Coherence : di�erent tree derivations of a linea r rep resen- tation should have same meaning. E: intexp E : realexp E : realexp 1 2 { E: realexp E +E : realexp 1 2 int int intexp realexp { (((0) +(1) ) ) . int intexp realexp { ((((0) ) ) + int intexp realexp realexp (((1) ) ) ) W olfgang Schreiner 14
Recommend
More recommend