Questions? Static Semantics. Static Semantics. Static Semantics. Next week on Wednesday (5 th of October) no

Similar documents
CA341 - Comparative Programming Languages

Chapter 5: Procedural abstraction. Function procedures. Function procedures. Proper procedures and function procedures

4 Bindings and Scope. Bindings and environments. Scope, block structure, and visibility. Declarations. Blocks. 2004, D.A. Watt, University of Glasgow

Ch. 7: Generic abstraction. Generic units and instantiation. Generic units and instantiation. Generic units and instantiation

CSE 307: Principles of Programming Languages

Example: Haskell algebraic data types (1)

Lecture #23: Conversion and Type Inference

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = "Hello";

Lecture 16: Static Semantics Overview 1

CS558 Programming Languages. Winter 2013 Lecture 3

Storage. Outline. Variables and Updating. Composite Variables. Storables Lifetime : Programming Languages. Course slides - Storage

Principles of Programming Languages

Static Semantics. Lecture 15. (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1

Data Types. (with Examples In Haskell) COMP 524: Programming Languages Srinivas Krishnan March 22, 2011

Lecture Overview. [Scott, chapter 7] [Sebesta, chapter 6]

SMURF Language Reference Manual Serial MUsic Represented as Functions

CMSC 330: Organization of Programming Languages. Lets, Tuples, Records

Note 3. Types. Yunheung Paek. Associate Professor Software Optimizations and Restructuring Lab. Seoul National University

Properties of an identifier (and the object it represents) may be set at

CS558 Programming Languages

Short Notes of CS201

Lecture #13: Type Inference and Unification. Typing In the Language ML. Type Inference. Doing Type Inference

CS201 - Introduction to Programming Glossary By

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

Programmiersprachen (Programming Languages)

COMP 181. Agenda. Midterm topics. Today: type checking. Purpose of types. Type errors. Type checking

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

CS 430 Spring Mike Lam, Professor. Data Types and Type Checking

Types and Type Inference

Programming Languages, Summary CSC419; Odelia Schwartz

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

Chapter 5. Names, Bindings, and Scopes

CSCE 314 Programming Languages. Type System

CS321 Languages and Compiler Design I Winter 2012 Lecture 13

Fundamentals of Programming Languages

Concepts of programming languages

User-Defined Algebraic Data Types

Informal Semantics of Data. semantic specification names (identifiers) attributes binding declarations scope rules visibility

Lexical Considerations

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University

1 Terminology. 2 Environments and Static Scoping. P. N. Hilfinger. Fall Static Analysis: Scope and Types

Storage. Outline. Variables and updating. Copy vs. Ref semantics Lifetime. Dangling References Garbage collection

Haskell 98 in short! CPSC 449 Principles of Programming Languages

CPSC 3740 Programming Languages University of Lethbridge. Data Types

HANDLING NONLOCAL REFERENCES

G Programming Languages - Fall 2012

SE352b: Roadmap. SE352b Software Engineering Design Tools. W3: Programming Paradigms

Special Topics: Programming Languages

Functional Programming. Lecture 2: Algebra

Compilers. Type checking. Yannis Smaragdakis, U. Athens (original slides by Sam

Types. What is a type?

1 Lexical Considerations

Type Checking and Type Inference

A declaration may appear wherever a statement or expression is allowed. Limited scopes enhance readability.

22c:111 Programming Language Concepts. Fall Types I

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find


SML A F unctional Functional Language Language Lecture 19

Programming Languages Third Edition. Chapter 7 Basic Semantics

Types and Programming Languages. Lecture 5. Extensions of simple types

Organization of Programming Languages CS3200/5200N. Lecture 11

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

Pierce Ch. 3, 8, 11, 15. Type Systems

Informatica 3 Syntax and Semantics

CMSC 330: Organization of Programming Languages

G Programming Languages Spring 2010 Lecture 6. Robert Grimm, New York University

Type-indexed functions in Generic Haskell

Chapter 3 Linear Structures: Lists

CS558 Programming Languages

The Design of Core C++ (Notes)

Programming Languages and Compilers (CS 421)

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

G Programming Languages - Fall 2012

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

Lexical Considerations

CS558 Programming Languages

IEEE LANGUAGE REFERENCE MANUAL Std P1076a /D3

The PCAT Programming Language Reference Manual

Names, Scope, and Bindings

CSCI312 Principles of Programming Languages!

Functional Languages. CSE 307 Principles of Programming Languages Stony Brook University

Array. Prepared By - Rifat Shahriyar

Types and Type Inference

CS558 Programming Languages

Programming Language Concepts, cs2104 Lecture 04 ( )

Programming Languages and Techniques (CIS120)

Chapter 3 Linear Structures: Lists

CSC 533: Organization of Programming Languages. Spring 2005

CS558 Programming Languages

Types II. Hwansoo Han

Data Types. Data Types. Introduction. Data Types. Data Types. Data Types. Introduction

Organization of Programming Languages CS320/520N. Lecture 06. Razvan C. Bunescu School of Electrical Engineering and Computer Science

Index. object lifetimes, and ownership, use after change by an alias errors, use after drop errors, BTreeMap, 309

Names, Scope, and Bindings

CSCI-GA Scripting Languages

Attributes, Bindings, and Semantic Functions Declarations, Blocks, Scope, and the Symbol Table Name Resolution and Overloading Allocation, Lifetimes,

Programming Languages

Chapter 5 Names, Binding, Type Checking and Scopes

Review of the C Programming Language

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 17: Types and Type-Checking 25 Feb 08

Transcription:

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