tailieunhanh - Lecture Programming languages (2/e): Chapter 8b - Tucker, Noonan

Chapter 8b - Semantics with dynamic typing. Here we consider the semantics of a dynamically typed language in order to examine the difference between statically and dynamically typed languages more fully. We use the same method of presentation as in section . | Programming Languages 2nd edition Tucker and Noonan Chapter 8 Semantic Interpretation To understand a program you must become both the machine and the program. A. Perlis Contents State Transformations and Partial Functions Semantics of Clite Semantics with Dynamic Typing A Formal Treatment of Semantics Dynamically Typed Languages Scripting: Perl, Python, PHP Object-oriented: Smalltalk, Ruby Functional: Scheme, ML, Haskel Logic: Prolog Our example: dynamically typed C++Lite int main( ) { n =3; i =1; f = ; while (i < n) { i = i + 1; f = f * float(i); } } Step Stmt n i f 1 3 -- -- -- 2 4 3 -- -- 3 5 3 1 -- 4 6 3 1 5 7 3 1 6 8 3 2 7 6 3 2 8 7 3 2 Step Stmt n i f 9 8 3 3 10 6 3 3 11 10 3 3 Perl vs. Python Perl: implicit conversions, distinct operators “2” < “10” : true – numeric comparison “2” lt “10” : false – string comparison 2 lt “10” : false – 2 converted to “2” Python: explicit conversions required “2” < “10” : false – string comparison 2 < “10” : error Meaning Rule The meaning of a Program is the meaning of its body when given an empty initial state. Variables declared as encountered Type of a variable is type of is value In factorial: i, n – int f – float C++Dynamic Statement = Skip | Block | Assignment | Conditional | Loop Skip, Block unchanged Conditional, Loop – check that test is bool Assignment add target variable to state, if needed no assignment compatibility check needed ??? Meaning Rule The meaning of an expression in the current state is a value defied as follows: If the expression is a value, then the value itself If the expression is a Variable: If the Variable occurs in the current state, then its associated value. Otherwise the program is meaningless Meaning Rule If the expression is a binary: Determine the value of term1, term2 in current state Apply Rule to the operator and values If the expression is a unary: Determine the value of term in current state Apply Rule to the | Programming Languages 2nd edition Tucker and Noonan Chapter 8 Semantic Interpretation To understand a program you must become both the machine and the program. A. Perlis Contents State Transformations and Partial Functions Semantics of Clite Semantics with Dynamic Typing A Formal Treatment of Semantics Dynamically Typed Languages Scripting: Perl, Python, PHP Object-oriented: Smalltalk, Ruby Functional: Scheme, ML, Haskel Logic: Prolog Our example: dynamically typed C++Lite int main( ) { n =3; i =1; f = ; while (i < n) { i = i + 1; f = f * float(i); } } Step Stmt n i f 1 3 -- -- -- 2 4 3 -- -- 3 5 3 1 -- 4 6 3 1 5 7 3 1 6 8 3 2 7 6 3 2 8 7 3 2 Step Stmt n i f 9 8 3 3 10 6 3 3 11 10 3 3 Perl vs. Python Perl: implicit conversions, distinct operators “2” < “10” : true – numeric comparison “2” lt “10” : false – string comparison 2 lt “10” : false – 2 converted to “2” Python: explicit conversions required “2” < “10” : false – string .

TỪ KHÓA LIÊN QUAN