Questions? Static Semantics • Primitive types • First exercise is online: • Primitive value can not be decomposed into simpler http://www.win.tue.nl/~mvdbrand/courses/GLT/1112/ values Deadline 17 th of October • Primitive type is a type of which the values are primitive • Next week on Wednesday (5 th of October) no − Booleans = {‘true’, ‘false’} − Integer = { − Integer = {…, -2, -1, 0, 1, 2, …} 2 1 0 1 2 } lectures!!! lectures!!! / Faculteit Wiskunde en Informatica / Faculteit Wiskunde en Informatica 28-9-2011 PAGE 0 28-9-2011 PAGE 1 Static Semantics Static Semantics • Defining primitive types • Composite types: y • It is possible to define new types via enumeration of values, • Composite value is composed of simpler values so-called enumeration type • Composite type is a type of which the values are composite − example: type Month is (jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec); • Restricted number of structuring concepts: − Cartesian products (tuples, records) − signature: enum(Name signature: enum(Name, Values) > Type Values) -> Type − mappings (arrays, functions) − recursive types (lists, trees) / Faculteit Wiskunde en Informatica 28-9-2011 PAGE 2 / Faculteit Wiskunde en Informatica 28-9-2011 PAGE 3
Static Semantics Static Semantics • Composite types • Functions implements mapping y • A mapping S T that takes a value of type S and maps it to • Cartesian products type T − Values of different types are grouped into tuples, classes, or records bool isEven (int n) { bool isEven (int n) { − Basic operations: return (n % 2 == 0); − construction } − selection • Functions with multiple parameters implement mappings S • Functions with multiple parameters implement mappings S 1 S 2 … S n T • Mappings float power (float b, int n) { − Mapping from one set to another m : S T … − Formally S T = {m | x S m(x) T} } • Signature: function(Heading, Body) -> Function / Faculteit Wiskunde en Informatica / Faculteit Wiskunde en Informatica 28-9-2011 PAGE 4 28-9-2011 PAGE 5 Static Semantics Static Semantics • Recursive types • Type systems y y • A recursive type is defined in terms of itself • A type system of a (programming) language groups values into types • Lists: • Prevents illegal operations, like multiplication of strings by Prevents illegal operations, like multiplication of strings by − sequence of values: homogeneous or heterogeneous booleans: type error − operations: − length • Statically typed language: each variable and expression has • Statically typed language: each variable and expression has − emptiness test ti t t − head selection a fixed type − tail selection − all operands can be type-checked at compile-time − concatenation • Dynamically typed language: values have fixed type but • Dynamically typed language: values have fixed type, but variables and expressions have no fixed type. − For example: data IntList = Nil | Cons Int IntList − operands can be only type-checked at run-time / Faculteit Wiskunde en Informatica 28-9-2011 PAGE 6 / Faculteit Wiskunde en Informatica 28-9-2011 PAGE 7
Static Semantics Static Semantics • Type equivalence: T 1 T 2 • Type equivalence y • Structural equivalence: • Determining the whether 2 composite types are the − T 1 T 2 if and only if T 1 and T 2 have the same set of values same − if T 1 and T 2 are both primitive and identical, then T 1 T 2 • T 1 T 2 • T T − if T 1 = A 1 B 1 and T 2 = A 2 B 2 , if T A B d T A B then T 1 T 2 if and only if A 1 A 2 and B 1 B 2 − Structural equivalence − if T 1 = A 1 B 1 and T 2 = A 2 B 2 , − Name equivalence then T 1 T 2 if and only if A 1 A 2 and B 1 B 2 − if T 1 = A 1 + B 1 and T 2 = A 2 + B 2 , then T 1 T 2 if and only if A 1 A 2 and B 1 B 2 − otherwise T 1 / T 2 / Faculteit Wiskunde en Informatica / Faculteit Wiskunde en Informatica 28-9-2011 PAGE 8 28-9-2011 PAGE 9 Static Semantics Static Semantics • Type equivalence: T 1 T 2 • Lifetime of a variable is the time between creation y 1 2 (allocation) and destruction (deallocation) • Name equivalence: − T 1 T 2 if and only if T 1 and T 2 are defined in the same place! • Global variable’s lifetime is the program’s run-time struct Position { int x, y; }; { , y; }; • Local variable s lifetime is an activation of a block • Local variable’s lifetime is an activation of a block struct Position pos; • Heap variable’s lifetime is arbitrary, but maximum is struct Date { int x, y; }; program’s run-time struct Date today; y; • Persistent variable’s lifetime is arbitrary and not restricted to Persistent variable’s lifetime is arbitrary and not restricted to void show ( struct Date d); program’s run-time show(today); passes type checking using both show(today); passes type checking using both structural and name equivalence, show(pos); only passes using structural equivalence / Faculteit Wiskunde en Informatica 28-9-2011 PAGE 10 / Faculteit Wiskunde en Informatica 28-9-2011 PAGE 11
Static Semantics Static Semantics • Bindings and environments • A global variable is a variable that can be used any g y • If identifiers occur in an expression, such an expression where in a program cannot be understood in isolation: • A local variable is only available within the block − its meaning depends on the declarations of these identifiers where it is declared where it is declared elsewhere in the program elsewhere in the program • A block is a program construct that includes local • A binding is an association between an identifier and an declarations entity such as value variable or procedure entity such as value, variable or procedure • An activation of a block is the time interval that the block is executed • An environment (or name space ) is a set of bindings: − Consider the type environment in PICO Consider the type environment in PICO / Faculteit Wiskunde en Informatica / Faculteit Wiskunde en Informatica 28-9-2011 PAGE 12 28-9-2011 PAGE 13 Static Semantics Static Semantics • The scope of a declaration is the part of the program • Blocks: g where the declaration is effective • A block command is a form of command that contains a local declaration D and a subcommand C • A block is a language construct that delimits the − In C/C++ and Java: { D C } scope of declarations within it scope of declarations within it if (x > y) {int z = x; x = y; y = z;} • monolithic block structure, e.g. Cobol • flat block structure, e.g. Fortran • A block expression is a form of expression that contains a A block expression is a form of expression that contains a • nested block structure, e.g. Algol-like language, C, Java local declaration D and a subexpression E − Haskell provides block expressions: let D in E / Faculteit Wiskunde en Informatica 28-9-2011 PAGE 14 / Faculteit Wiskunde en Informatica 28-9-2011 PAGE 15
Static Semantics Static Semantics • Scope and visibility • Static vs dynamic scoping y g • A binding occurrence of identifier I is an occurrence where I • A language is statically scoped if the body of a procedure is is bound to some entity X executed in the environment of the procedure’s definition • An applied occurrence of I is an occurrence where use is − compile-time binding of identifiers pp p g made of the entity X to which I is bound • Each applied occurrence of I should correspond to exactly • A language is dynamically scoped if the body of a procedure one binding occurrence of I g is executed in the environment of the procedure call p − An identifier I may be defined in multiple blocks − run-time binding of identifiers • Nested blocks, some outer block contains a declaration of I : • Nearly all programming languages (C C++ Java etc ) are − If inner block does not contain a declaration of I then declaration If inner block does not contain a declaration of I , then declaration Nearly all programming languages (C, C++, Java, etc.) are statically scoped is visible throughout outer and inner blocks − If inner block contains a declaration of I , then the inner block declaration hides the outer block declaration / Faculteit Wiskunde en Informatica / Faculteit Wiskunde en Informatica 28-9-2011 PAGE 16 28-9-2011 PAGE 17 Questions? Static Semantics • First exercise is online: • Static vs dynamic scoping y g • Consider: http://www.win.tue.nl/~mvdbrand/courses/GLT/1112/ Deadline 17 th of October const int s = 2; int f(int x) { return s * x;} int f(int x) { return s * x;} • Next week on Wednesday (5 th of October) no void p(int y) { print(f(y));} (1) lectures!!! lectures!!! void q(int z) { const int s = 3; print(f(z));} (2) • What is the value printed at (1) and (2)? Wh t i th l i t d t (1) d (2)? / Faculteit Wiskunde en Informatica 28-9-2011 PAGE 18 / Faculteit Wiskunde en Informatica 28-9-2011 PAGE 19
Recommend
More recommend