Questions? Static Semantics Primitive types First exercise is - - PowerPoint PPT Presentation

questions static semantics
SMART_READER_LITE
LIVE PREVIEW

Questions? Static Semantics Primitive types First exercise is - - PowerPoint PPT Presentation

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


slide-1
SLIDE 1

Questions?

  • First exercise is online:

http://www.win.tue.nl/~mvdbrand/courses/GLT/1112/

Deadline 17th of October

  • Next week on Wednesday (5th of October) no

lectures!!! lectures!!!

/ Faculteit Wiskunde en Informatica

PAGE 0 28-9-2011

Static Semantics

  • 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 } − Integer = {…, -2, -1, 0, 1, 2, …}

/ Faculteit Wiskunde en Informatica

PAGE 1 28-9-2011

Static Semantics

  • 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 signature: enum(Name, Values) > Type

/ Faculteit Wiskunde en Informatica

PAGE 2 28-9-2011

Static Semantics

  • Composite types:

y

  • 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

PAGE 3 28-9-2011

slide-2
SLIDE 2

Static Semantics

  • Composite types

y

  • 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}

/ Faculteit Wiskunde en Informatica

PAGE 4 28-9-2011

Static Semantics

  • Functions implements mapping
  • A mapping S  T that takes a value of type S and maps it to

type T

bool isEven (int n) { bool isEven (int n) { return (n % 2 == 0); }

  • Functions with multiple parameters implement mappings S 
  • Functions with multiple parameters implement mappings S1 

S2  …  Sn  T

float power (float b, int n) { … }

  • Signature: function(Heading, Body) -> Function

/ Faculteit Wiskunde en Informatica

PAGE 5 28-9-2011

Static Semantics

  • Recursive types
  • A recursive type is defined in terms of itself
  • Lists:

− sequence of values: homogeneous or heterogeneous − operations:

− length ti t t − emptiness test − head selection − tail selection − concatenation − For example: data IntList = Nil | Cons Int IntList

/ Faculteit Wiskunde en Informatica

PAGE 6 28-9-2011

Static Semantics

  • Type systems

y y

  • A type system of a (programming) language groups values

into types

  • Prevents illegal operations, like multiplication of strings by

Prevents illegal operations, like multiplication of strings by booleans: type error

  • Statically typed language: each variable and expression has
  • 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
  • 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

PAGE 7 28-9-2011

slide-3
SLIDE 3

Static Semantics

  • Type equivalence

y

  • Determining the whether 2 composite types are the

same

  • T  T
  • T1  T2

− Structural equivalence − Name equivalence

/ Faculteit Wiskunde en Informatica

PAGE 8 28-9-2011

Static Semantics

  • Type equivalence: T1  T2
  • Structural equivalence:

− T1  T2 if and only if T1 and T2 have the same set of values

− if T1 and T2 are both primitive and identical, then T1  T2 if T A B d T A B − if T1 = A1  B1 and T2 = A2  B2, then T1  T2 if and only if A1  A2 and B1  B2 − if T1 = A1  B1 and T2 = A2  B2, then T1  T2 if and only if A1  A2 and B1  B2 − if T1 = A1 + B1 and T2 = A2 + B2, then T1  T2 if and only if A1  A2 and B1  B2 − otherwise T1 / T2

/ Faculteit Wiskunde en Informatica

PAGE 9 28-9-2011

Static Semantics

  • Type equivalence: T1  T2

y

1 2

  • Name equivalence:

− T1  T2 if and only if T1 and T2 are defined in the same place! struct Position {int x, y; }; { , y; }; struct Position pos; struct Date {int x, y; }; struct Date today; y; void show (struct Date d);

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

PAGE 10 28-9-2011

Static Semantics

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

  • Persistent variable’s lifetime is arbitrary and not restricted to

program’s run-time

/ Faculteit Wiskunde en Informatica

PAGE 11 28-9-2011

slide-4
SLIDE 4

Static Semantics

  • A global variable is a variable that can be used any

g y where in a program

  • A local variable is only available within the block

where it is declared 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

/ Faculteit Wiskunde en Informatica

PAGE 12 28-9-2011

Static Semantics

  • 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 elsewhere in the program

  • A binding is an association between an identifier and an

entity such as value variable or procedure entity such as value, variable or procedure

  • 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

PAGE 13 28-9-2011

Static Semantics

  • The scope of a declaration is the part of the program

g where the declaration is effective

  • A block is a language construct that delimits the

scope of declarations within it 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

/ Faculteit Wiskunde en Informatica

PAGE 14 28-9-2011

Static Semantics

  • 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

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

PAGE 15 28-9-2011

slide-5
SLIDE 5

Static Semantics

  • 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

pp made of the entity X to which I is bound

  • Each applied occurrence of I should correspond to exactly
  • ne binding occurrence of I

g

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

/ Faculteit Wiskunde en Informatica

PAGE 16 28-9-2011

Static Semantics

  • Static vs dynamic scoping

y g

  • 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 p g

  • A language is dynamically scoped if the body of a procedure

is executed in the environment of the procedure call p

− run-time binding of identifiers

  • Nearly all programming languages (C C++ Java etc ) are

Nearly all programming languages (C, C++, Java, etc.) are statically scoped

/ Faculteit Wiskunde en Informatica

PAGE 17 28-9-2011

Questions?

  • First exercise is online:

http://www.win.tue.nl/~mvdbrand/courses/GLT/1112/

Deadline 17th of October

  • Next week on Wednesday (5th of October) no

lectures!!! lectures!!!

/ Faculteit Wiskunde en Informatica

PAGE 18 28-9-2011

Static Semantics

  • Static vs dynamic scoping

y g

  • Consider:

const int s = 2; int f(int x) { return s * x;} 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)

Wh t i th l i t d t (1) d (2)?

  • What is the value printed at (1) and (2)?

/ Faculteit Wiskunde en Informatica

PAGE 19 28-9-2011

slide-6
SLIDE 6

Static Semantics

  • A declaration is a language construct to produce a

binding

  • Types of simple declarations:

Types of simple declarations:

  • type
  • constant
  • variable
  • variable
  • procedure

/ Faculteit Wiskunde en Informatica

PAGE 20 28-9-2011

Static Semantics

  • A procedure definition binds an identifier to a

procedure

  • function
  • procedure
  • procedure
  • A function definition:

2 bool even(int n) {return (n % 2 == 0);}

  • A procedure definition:

void double(int& n) { n *= 2; }

/ Faculteit Wiskunde en Informatica

PAGE 21 28-9-2011

Static Semantics

  • A function/procedure is an entity that embodies a

y computation

  • a function embodies an expression to be evaluated

a procedure embodies a command to be executed

  • a procedure embodies a command to be executed
  • Methods in OO languages are procedures but closely

g g p y related to classes

/ Faculteit Wiskunde en Informatica

PAGE 22 28-9-2011

Static Semantics

  • A function evaluates an expression and yields a

value as result

  • T I (FPD1, …, FPDn) B where:

− T is result type − I is function’s identifier − FPDi are formal parameter declarations − B is a block command (also called body), B t i t l t t t t f th f t E B contains at least one statement of the form return E

  • Call via I(AP1, …, APn) where:

AP are actual parameters − APi are actual parameters

/ Faculteit Wiskunde en Informatica

PAGE 23 28-9-2011

slide-7
SLIDE 7

Static Semantics

  • A procedure embodies a command to be executed

and will update variables

  • void I(FPD1, …, FPDn) B where:

− I is function’s identifier I is function s identifier − FPDi are formal parameter declarations − B is a block command (also called body)

  • Call via I(AP1, …, APn) where:

− APi are actual parameters

/ Faculteit Wiskunde en Informatica

PAGE 24 28-9-2011

Static Semantics

  • Parameters and arguments

g

  • An argument is a value or other entity that is passed to a

procedure

  • An actual parameter is an expression that yields an argument

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
  • Association between formal parameter and argument is

called parameter mechanism

− two basic concepts:

− copy parameter mechanism copy parameter mechanism − reference parameter mechanism

/ Faculteit Wiskunde en Informatica

PAGE 25 28-9-2011

Static Semantics

  • Copy parameter mechanism allows the transfer of

y values to and from a procedure

  • A formal parameter FP denotes a local variable of the

procedure procedure

  • A reference parameter allows for the formal

parameter FP to be bound directly to the argument

/ Faculteit Wiskunde en Informatica

PAGE 26 28-9-2011

Static Semantics

  • 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 functions are overloaded

  • “-” operator:

− integer negation (Integer  Integer) − floating-point negation (Float  Float) floating point negation (Float  Float) − integer subtraction (IntegerInteger  Integer) − floating-point subtraction (FloatFloat  Float)

/ Faculteit Wiskunde en Informatica

PAGE 27 28-9-2011

slide-8
SLIDE 8

Static Semantics

  • Identifier F denotes
  • function (f1) of type S1  T1
  • function (f2) of type S2  T2
  • context-independent overloading requires S1 and S2 are non-

equivalent

− if actual parameter E of F(E) is of type S then F denotes f − if actual parameter E of F(E) is of type S1 then F denotes f1 − if E is of type S2 then F denotes f2

  • with context-independent overloading the function can be

with context independent overloading the function can be uniquely identified by the type of the actual parameter

/ Faculteit Wiskunde en Informatica

PAGE 28 28-9-2011

Static Semantics

  • Identifier F denotes

f i ( ) f

  • function (f1) of type S1  T1
  • function (f2) of type S2  T2
  • context dependent overloading requires S and S are non
  • context-dependent overloading requires S1 and S2 are non-

equivalent or T1 and T2 are non-equivalent

− if S1 and S2 are non-equivalent, see previous slide − if S1 and S2 are equivalent, but T1 and T2 are non-equivalent the t t t b t k i t id ti context must be taken into consideration

− if the context F(E) is of type T1 then F denotes f1 − if the context is of type T2 then F denotes f2

with context dependent overloading it is possible to

  • with context-dependent overloading, it is possible to

formulate expressions which cannot be uniquely identified

/ Faculteit Wiskunde en Informatica

PAGE 29 28-9-2011

Static Semantics

  • Polymorphism

y

  • Monomorphic procedures can only operate on arguments of

a fixed type

  • Polymorphic procedures can operate uniformly on

Polymorphic procedures can operate uniformly on arguments of a family of types

  • A type variable is an identifier that stands for a family of

A type variable is an identifier that stands for a family of types

− monomorphic second(x : Int, y : Int) = y polymorphic − polymorphic second(x : δ, y : τ) = y

/ Faculteit Wiskunde en Informatica

PAGE 30 28-9-2011

Static Semantics

  • A polytype derives a family of similar types

δ  τ  τ includes − Integer  Boolean  Boolean String  String  String − String  String  String but not − Boolean  Integer  Boolean − Integer  Integer Integer  Integer

/ Faculteit Wiskunde en Informatica

PAGE 31 28-9-2011

slide-9
SLIDE 9

Static Semantics

  • 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

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

/ Faculteit Wiskunde en Informatica

PAGE 32 28-9-2011

Static Semantics

  • even n = (n ‘mod’ 2 = 0)

type of even is: Integer  Integer  Integer

  • id x = x

id x x

type of id is: τ  τ

f g \ > f(g( ))

  • f . g = \x -> f(g(x))
  • types of f and g are β  γ and α  β, respectively
  • variable x must be of type α

f

  • subexpression f(g(x)) is of type γ
  • expression \x -> f(g(x)) is of type α  γ
  • “.” is of type (β  γ)  (α  β)  (α  γ)

/ Faculteit Wiskunde en Informatica

PAGE 33 28-9-2011

Static Semantics

  • 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.), S ti d Al b i S ifi ti ( 207 226) B li Semantics and Algebraic Specification. (pp. 207-226) Berlin: Springer

  • Keiren, J.J.A. & Reniers, M.A. (2011). Type checking mCRL2.

Computer Science Report No. 11-11, Eindhoven: Technische Computer Science Report No. 11 11, Eindhoven: Technische Universiteit Eindhoven

/ Faculteit Wiskunde en Informatica

PAGE 34 28-9-2011