Questions? First exercise is online: http://www.win.tue.nl/~mvdbrand/courses/glt/1112/ Deadline 17 th of October Next week on Wednesday (5 th of October) no lectures!!! Primitive types Primitive value can not be decomposed into simpler values Primitive type is a type of which the values are primitive Booleans = { true, false } Integer = {, -2, -1, 0, 1, 2, } / Faculteit Wiskunde en Informatica 28-9-2011 PAGE 0 / Faculteit Wiskunde en Informatica 28-9-2011 PAGE 1 Defining primitive types It is possible to define new types via enumeration of values, so-called enumeration type example: type Month is (jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec); signature: enum(name, Values) -> Type Composite types: Composite value is composed of simpler values Composite type is a type of which the values are composite Restricted number of structuring concepts: Cartesian products (tuples, records) 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
Composite types Cartesian products Values of different types are grouped into tuples, classes, or records Basic operations: construction selection Mappings Mapping from one set to another m : S T Formally S T = {m x S m(x) T} Functions implements mapping A mapping S T that takes a value of type S and maps it to type T bool iseven (int n) { return (n % 2 == 0); } Functions with multiple parameters implement mappings S 1 S 2 S n T float power (float b, int n) { } Signature: function(heading, Body) -> Function / Faculteit Wiskunde en Informatica 28-9-2011 PAGE 4 / Faculteit Wiskunde en Informatica 28-9-2011 PAGE 5 Recursive types A recursive type is defined in terms of itself Lists: sequence of values: homogeneous or heterogeneous operations: length emptiness test t head selection tail selection concatenation For example: data IntList = Nil Cons Int IntList Type systems A type system of a (programming) language groups values into types Prevents illegal operations, like multiplication of strings by booleans: type error Statically typed language: each variable and expression has a fixed type all operands can be type-checked at compile-time Dynamically typed language: values have fixed type, but variables and expressions have no fixed type. 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
Type equivalence Determining the whether 2 composite types are the same T 1 T 2 Structural equivalence Name equivalence Type equivalence: T 1 T 2 Structural equivalence: T 1 T 2 if and only if T 1 and T 2 have the same set of values if T 1 and T 2 are both primitive and identical, then T 1 T 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 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 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 28-9-2011 PAGE 8 / Faculteit Wiskunde en Informatica 28-9-2011 PAGE 9 Type equivalence: T 1 T 2 Name equivalence: T 1 T 2 if and only if T 1 and T 2 are defined in the same place! struct Position {int x, y; }; struct Position pos; struct Date {int x, y; }; struct Date today; void show (struct Date d); Lifetime of a variable is the time between creation (allocation) and destruction (deallocation) Global variable s lifetime is the program s run-time Local variable s lifetime is an activation of a block Heap variable s lifetime is arbitrary, but maximum is program s run-time Persistent variable s lifetime is arbitrary and not restricted to program s run-time 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
A global variable is a variable that can be used any where in a program A local variable is only available within the block where it is declared A block is a program construct that includes local declarations An activation of a block is the time interval that the block is executed Bindings and environments If identifiers occur in an expression, such an expression cannot be understood in isolation: its meaning depends on the declarations of these identifiers elsewhere in the program A binding is an association between an identifier and an entity such as value, variable or procedure An environment (or name space) is a set of bindings: Consider the type environment in PICO / Faculteit Wiskunde en Informatica 28-9-2011 PAGE 12 / Faculteit Wiskunde en Informatica 28-9-2011 PAGE 13 The scope of a declaration is the part of the program where the declaration is effective A block is a language construct that delimits the scope of declarations within it monolithic block structure, e.g. Cobol flat block structure, e.g. Fortran nested block structure, e.g. Algol-like language, C, Java Blocks: A block command is a form of command that contains a local declaration D and a subcommand C In C/C++ and Java: { D C } if (x > y) {int z = x; x = y; y = z;} A block expression is a form of expression that contains a 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
Scope and visibility A binding occurrence of identifier I is an occurrence where I is bound to some entity X An applied occurrence of I is an occurrence where use is made of the entity X to which I is bound Each applied occurrence of I should correspond to exactly one binding occurrence of I An identifier I may be defined in multiple blocks Nested blocks, some outer block contains a declaration of I: If inner block does not contain a declaration of I, then declaration 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 Static vs dynamic scoping A language is statically scoped if the body of a procedure is executed in the environment of the procedure s definition compile-time binding of identifiers A language is dynamically scoped if the body of a procedure is executed in the environment of the procedure call run-time binding of identifiers Nearly all programming languages (C, C++, Java, etc.) are statically scoped / Faculteit Wiskunde en Informatica 28-9-2011 PAGE 16 / Faculteit Wiskunde en Informatica 28-9-2011 PAGE 17 Questions? First exercise is online: http://www.win.tue.nl/~mvdbrand/courses/glt/1112/ Deadline 17 th of October Next week on Wednesday (5 th of October) no lectures!!! Static vs dynamic scoping Consider: const int s = 2; int f(int x) { return s * x;} void p(int y) { print(f(y));} (1) void q(int z) { const int s = 3; print(f(z));} (2) What tis the value printed at t(1) and d(2)? / Faculteit Wiskunde en Informatica 28-9-2011 PAGE 18 / Faculteit Wiskunde en Informatica 28-9-2011 PAGE 19
A declaration is a language construct to produce a binding Types of simple declarations: type constant variable procedure A procedure definition binds an identifier to a procedure function procedure A function definition: bool even(int n) {return (n % 2 == 0);} A procedure definition: void double(int& n) { n *= 2; } / Faculteit Wiskunde en Informatica 28-9-2011 PAGE 20 / Faculteit Wiskunde en Informatica 28-9-2011 PAGE 21 A function/procedure is an entity that embodies a computation a function embodies an expression to be evaluated a procedure embodies a command to be executed Methods in OO languages g are procedures but closely related to classes A function evaluates an expression and yields a value as result T I (FPD 1,, FPD n ) B where: T is result type I is function s identifier FPD i are formal parameter declarations B is a block command (also called body), B contains at tleast one statement t tof fthe form return E Call via I(AP 1,, AP n ) where: AP i are actual parameters / Faculteit Wiskunde en Informatica 28-9-2011 PAGE 22 / Faculteit Wiskunde en Informatica 28-9-2011 PAGE 23
A procedure embodies a command to be executed and will update variables void I(FPD 1,, FPD n ) B where: I is function s identifier FPD i are formal parameter declarations B is a block command (also called body) Call via I(AP 1,, AP n ) where: AP i are actual parameters Parameters and arguments An argument is a value or other entity that is passed to a procedure An actual parameter is an expression that yields an argument A formal parameter is an identifier through which a procedure can access an argument Association between formal parameter and argument is called parameter mechanism two basic concepts: copy parameter mechanism reference parameter mechanism / Faculteit Wiskunde en Informatica 28-9-2011 PAGE 24 / Faculteit Wiskunde en Informatica 28-9-2011 PAGE 25 Copy parameter mechanism allows the transfer of values to and from a procedure A formal parameter FP denotes a local variable of the procedure A reference parameter allows for the formal parameter FP to be bound directly to the argument An identifier is overloaded of it denotes two or more distinct procedures in the same scope In older programming languages operators for certain built-in functions are overloaded - operator: integer negation (Integer Integer) floating-point negation (Float Float) integer subtraction (Integer Integer Integer) floating-point subtraction (Float Float Float) / Faculteit Wiskunde en Informatica 28-9-2011 PAGE 26 / Faculteit Wiskunde en Informatica 28-9-2011 PAGE 27
Identifier F denotes function (f 1 ) of type S 1 T 1 function (f 2 ) of type S 2 T 2 context-independent overloading requires S 1 and S 2 are nonequivalent if actual parameter E of F(E) is of type S 1 then F denotes f 1 if E is of type S 2 then F denotes f 2 with context-independent overloading the function can be uniquely identified by the type of the actual parameter Identifier F denotes function (f 1 ) of type S 1 T 1 function (f 2 ) of type S 2 T 2 context-dependent overloading requires S 1 and S 2 are nonequivalent or T 1 and T 2 are non-equivalent if S 1 and S 2 are non-equivalent, see previous slide if S 1 and S 2 are equivalent, but T 1 and T 2 are non-equivalent the context t must be taken into consideration if the context F(E) is of type T 1 then F denotes f 1 if the context is of type T 2 then F denotes f 2 with context-dependent overloading, it is possible to formulate expressions which cannot be uniquely identified / Faculteit Wiskunde en Informatica 28-9-2011 PAGE 28 / Faculteit Wiskunde en Informatica 28-9-2011 PAGE 29 Polymorphism Monomorphic procedures can only operate on arguments of a fixed type Polymorphic procedures can operate uniformly on arguments of a family of types A type variable is an identifier that stands for a family of types monomorphic second(x : Int, y : Int) = y polymorphic second(x : δ, y : τ) = y A polytype derives a family of similar types δ τ τ includes Integer Boolean Boolean String String String but not Boolean Integer Boolean Integer Integer / Faculteit Wiskunde en Informatica 28-9-2011 PAGE 30 / Faculteit Wiskunde en Informatica 28-9-2011 PAGE 31
Statically type programming languages insist on explicit declaration of the type of entity in programs integer I := E In some languages we can write a definition I = E where type of I is not explicitly stated, but inferred from E Type inference is a process where the type of a declared entity is inferred instead of explicitly stated monotype, if strong clues, e.g., if a monomorphic operator is used polytype in case of polymorphic operators even n = (n mod 2 = 0) type of even is: Integer Integer Integer id x = x type of id is: τ τ f. g = \x -> f(g(x)) types of f and g are β γ and α β, respectively variable x must be of type α subexpression f(g(x)) is of type γ expression \x -> f(g(x)) is of type α γ. is of type (β γ) (α β) (α γ) / Faculteit Wiskunde en Informatica 28-9-2011 PAGE 32 / Faculteit Wiskunde en Informatica 28-9-2011 PAGE 33 Further reading Pierce, B.C. (2002), Types and Programming Languages. MIT Press Brand, M.G.J. van den, Meer, A.P. van der & Serebrenik, A. (2009). Type checking evolving languages with MSOS. In J. Palsberg (Ed.), Semantics and Algebraic Specification. (pp. 207-226) 226) Berlin: Springer Keiren, J.J.A. & Reniers, M.A. (2011). Type checking mcrl2. Computer Science Report No. 11-11, 11, Eindhoven: Technische Universiteit Eindhoven / Faculteit Wiskunde en Informatica 28-9-2011 PAGE 34