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

Similar documents
Types. What is a type?

Data Types (cont.) Administrative Issues. Academic Dishonesty. How do we detect plagiarism? Strongly Typed Languages. Type System

Data Types (cont.) Subset. subtype in Ada. Powerset. set of in Pascal. implementations. CSE 3302 Programming Languages 10/1/2007

Fundamentals of Programming Languages

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

Lecture 12: Data Types (and Some Leftover ML)

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

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

Question No: 1 ( Marks: 1 ) - Please choose one One difference LISP and PROLOG is. AI Puzzle Game All f the given

Topic 9: Type Checking

Topic 9: Type Checking

Programming Languages, Summary CSC419; Odelia Schwartz

CSE 431S Type Checking. Washington University Spring 2013

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

Programming Languages

22c:111 Programming Language Concepts. Fall Types I

Programming Languages Third Edition. Chapter 7 Basic Semantics

TYPES, VALUES AND DECLARATIONS


Francesco Nidito. Programmazione Avanzata AA 2007/08

9/7/17. Outline. Name, Scope and Binding. Names. Introduction. Names (continued) Names (continued) In Text: Chapter 5

G Programming Languages - Fall 2012

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

CPSC 3740 Programming Languages University of Lethbridge. Data Types

CSCI312 Principles of Programming Languages!

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

CSCI-GA Scripting Languages

UNIT II Structuring the Data, Computations and Program. Kainjan Sanghavi

Introduction Primitive Data Types Character String Types User-Defined Ordinal Types Array Types. Record Types. Pointer and Reference Types

Programming Languages

Chapter 5 Names, Binding, Type Checking and Scopes

Lecture #23: Conversion and Type Inference

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

INF 212/CS 253 Type Systems. Instructors: Harry Xu Crista Lopes

Types and Type Inference

Introduction. Primitive Data Types: Integer. Primitive Data Types. ICOM 4036 Programming Languages

INF 212 ANALYSIS OF PROG. LANGS Type Systems. Instructors: Crista Lopes Copyright Instructors.

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

Chapter 5 Names, Bindings, Type Checking, and Scopes

Semantic Analysis. How to Ensure Type-Safety. What Are Types? Static vs. Dynamic Typing. Type Checking. Last time: CS412/CS413

Chapter 6. Data Types ISBN

Type Checking and Type Equality

Data Types. Outline. In Text: Chapter 6. What is a type? Primitives Strings Ordinals Arrays Records Sets Pointers 5-1. Chapter 6: Data Types 2

Type Bindings. Static Type Binding

Zheng-Liang Lu Java Programming 45 / 79

HANDLING NONLOCAL REFERENCES

Type Systems, Type Inference, and Polymorphism

Types. Chapter Six Modern Programming Languages, 2nd ed. 1

Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus

Chapter 5. Names, Bindings, and Scopes

Data Types In Text: Ch C apter 6 1

Type Checking and Type Inference

Motivation for typed languages

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

Types and Type Inference

Data types. Definitions Aggregate constructors User-defined type definitions Types and storage Types and subtypes Type systems and type checking

COS 140: Foundations of Computer Science

CS321 Languages and Compiler Design I Winter 2012 Lecture 13

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

Intermediate Code Generation Part II

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

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

CS558 Programming Languages

CSE 307: Principles of Programming Languages

A Type is a Set of Values. Here we declare n to be a variable of type int; what we mean, n can take on any value from the set of all integer values.

Short Notes of CS201

Chapter 6 part 1. Data Types. (updated based on 11th edition) ISBN

CS201 - Introduction to Programming Glossary By

An Introduction to Subtyping

Static Semantics. Winter /3/ Hal Perkins & UW CSE I-1

Principles of Programming Languages

Sub- PPL Unit-II Class-SE Comp

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

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

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

Names, Scopes, and Bindings II. Hwansoo Han

Data Types, Variables and Arrays. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Topics Covered Thus Far CMSC 330: Organization of Programming Languages

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

COSC252: Programming Languages: Basic Semantics: Data Types. Jeremy Bolton, PhD Asst Teaching Professor

COS 320. Compiling Techniques

Strict Inheritance. Object-Oriented Programming Winter

Strict Inheritance. Object-Oriented Programming Spring 2008

Chapter 5. Variables. Topics. Imperative Paradigm. Von Neumann Architecture

COS 140: Foundations of Computer Science

Haskell 98 in short! CPSC 449 Principles of Programming Languages

Structuring the Data. Structuring the Data

Chapter 7:: Data Types. Mid-Term Test. Mid-Term Test (cont.) Administrative Notes

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Programming Methodology

Static Checking and Type Systems

Type systems. Static typing

Organization of Programming Languages CS3200 / 5200N. Lecture 06

CSCE 314 Programming Languages. Type System

Inheritance & Polymorphism. Object-Oriented Programming Spring 2015

Course outline. CSE 341: Programming Languages. Why study programming languages? Course motivation and objectives. 1 lecture: Concepts

Midterm CSE 131B Spring 2005

Data Types. CSE 307 Principles of Programming Languages Stony Brook University

CS558 Programming Languages

Number Representation & Conversion

Transcription:

Every program uses data, either explicitly or implicitly to arrive at a result. Data in a program is collected into data structures, and is manipulated by algorithms. Algorithms + Data Structures = Programs 2

Most programming languages provide: A set of simple data entities such as Integer Real Boolean Char Mechanisms for constructing new data entities from these. 3

Finiteness: Mathematically, integer data is thought of as:, -3, -2, -1, 0, 1, 2, 3, In a computer s hardware, there is always an upper and lower bound for values of integers. This is due to a given representation size in bits of an integer on a specific machine. 4

Different language designs deal with data type information in different ways. Some languages mandate that type information be explicit in the programming language. This is used to verify program correctness prior to execution. Example: int x; float y; char z; 5

Scheme is a language with no explicit types or translation-time typing. Ada is a very strictly-typed language. It performs strict type checking. 6

Some form of static (translation-time) type checking is very useful to: Allocate memory efficiently. Reduce the amount of code to be compiled. Improves both writability and readability. Improves security and reliability. Removes ambiguities. Can be used to prove program correctness. 7

A Data Type is a Set of Values Insufficient Definition! 8

A Data Type is a Set of Values + A Set of Operations on Those Values having Certain Properties 9

Example: The values of an int range from a lower bound to an upper bound (e.g. MININT to MAXINT) Operations on int include: + - * / Etc. 10

Type Checking: Data Types Is the process a translator goes through to determine whether the type information in a program is consistent. 11

Type Checking: Data Types Example: x = y + z Can the type of the result of y + z be stored in x? The type of y + z depends on the type of y and z Determining the type of y + z is called type inference. 12

We can construct more complex user defined types from simple data types: Example: An array of ten integers in C/C++: int a[10] The same array in Ada: a: array (1..10) of integer 13

Type definitions: In some languages, we can give new types names. Example in C: typedef int Array_of_ten_integers[10]; Array_of_ten_integers a; A name given to an int array of ten elements Just like we declare int a, We can declare Array_of_ten_integers a 14

Type definitions: In some languages, we can give new types names. Example in C: typedef int Array_of_ten_integers[10]; Array_of_ten_integers a; A name given to an int array of ten elements Just like we declare int a, We can declare Array_of_ten_integers a 15

Type definitions: In some languages, we can give new types names. Example in Ada: type Array_of_ten_integers is array(1..10) of integer; a: Array_of_ten_integers; 16

Type definitions: With new type definitions, we have a problem! During type checking, a translator must often compare two types to determine if they are the same. Example: The following should be equivalent in type! int a[10] Array_of_ten_integers 17

Type definitions: Data Types Each language with type declarations has rules for checking equivalence. These are called rule equivalence algorithms. 18

Strongly Typed Languages: If a programming language definition specifies a complete type system that can be applied statically. Guarantees that all (unintentional) data-corrupting errors in a program will be detected at the earliest possible point. The language is said to be strongly typed. 19

Strongly Typed Languages: All type errors are detected at translation time. With the exception of a few errors that can only be checked during execution. E.g. array subscript bounds 20

Strongly Typed Languages: Strong typing ensures that: Most unsafe programs (programs with data corrupting errors) will be rejected at translation time. The unsafe programs that are not rejected at translation time will cause an execution error prior to any data corrupting actions. Strong typing can also be a burden on the programmer, having to explicitly specify types all the time. 21

Strongly Typed Languages (Examples): Ada is a strongly typed language. Strong ML and Haskell Pascal C (Considered weakly typed) Weak 22

Untyped Languages: Languages without static type checking. Also called dynamically typed languages. Examples: Scheme, Smalltalk, most scripting languages such as Perl. An untyped language does not necessarily allow programs to corrupt data, but rather all safety checking is performed at execution time: They will generate runtime errors. 23

Type Construction: Data Types How are new types constructed? We can think of construction as mathematical operations. 24

Type Construction: Data Types Cartesian product. Unions. Subsets. Arrays/Functions. Sequences/Lists. Recursive types. No Intersection: types should NOT overlap. 25

Type Construction: Cartesian product: Data Types Finite combinations of previously defined types. Example: C structs: struct IntCharReal { int i; char c; double r; }; A finite combination of those types 26

Type Construction: Unions: Values belong to ONE of a finite set of types. Example: C unions: union IntOrReal { }; int i; double r; Only one of those can be used. 27

Type Construction: Data Types Subsets: A new type may be a subrange of another type. Example in Ada: subtype IntDigit_Type is integer range 0..9 28

Type Construction: Data Types Subsets: A new type may be a subrange of another type. Example in Ada: subtype IntDigit_Type is integer range 0..9 29

Type Construction: Data Types Arrays and Functions: You can define arrays in most programming languages as a homogeneous collection of a given type. You can define functions also. 30

Type Construction: Data Types Sequences and Lists: Sequences are like arrays, except that they are potentially infinite. Example: Streams Lists are like arrays, but can only be accessed by counting down from the first element. Almost all functional languages have lists. 31

Type Construction: Data Types Recursive Types: Example: class node { int value; node next; } Recursive Definition 32

Example: Java Type Structure: Java Types Primitive Reference boolean Numeric Array class interface Integral char byte short int long Floating point float double 33

Example: C Type Structure: C Types Basic Derived void Numeric Pointer Array Function struct union Integral (signed) (unsigned) char int short int long int enum Floating float double long double 34

Type Checking: Dynamic type checking: If type information is maintained and checked at runtime. Interpreters by definition perform dynamic type checking. Compilers also generate code that maintain type attributes during runtime in table. Dynamic type checking is required when the types can only be determined at runtime. Static type checking: The types are determined from the text of the program. Checking is performed before execution. 35

Type Checking: A strongly typed language must report all type errors as compilation errors that prevent execution. A language definition may not specify whether static or dynamic type checking is to be used. 36

Type Compatibility: Data Types Two different types may be combined correctly in certain ways. They are called compatible. Example: int, short. What do we mean by combined correctly in a certain way? Ranges! 37

Implicit Types: Sometimes types are implicitly inferred. Example: 2 + 3, or x / y In C, the following is an implicit int variable X; Also, in C, if functions have no return type indicated, they implicitly return an int. 38

Overlapping Types: Data Types It is sometimes difficult to avoid overlapping of various types. Example: In Java, the following types overlap: int short long byte All overlap 39

Shared Operations: Data Types The + operator for example may be applied to any combination of int, short, byte, double, float, long. The + operator even has a different meaning in: System.out.println( My + Name + Is ); The plus operator is said to be overloaded. 40

Type Conversion: There is always a need to convert from one type to another. Conversion can either be implicit (automatic) or explicit (manual). 41

Type Conversion: Example of implicit type conversion in C/C++: int x = 3.3; Although x is an int, 3.3 is truncated to 3, and stored in x. Example of implicit type conversion in Java: double x = 2 + 3; The expression s type is automatically promoted to double to be stored in x. Implicit conversions are also called coercions. 42

Type Conversion: When we implicitly convert from int to double for example, the destination is large enough to accommodate any int. This is called a widening conversion. The opposite is called a narrowing conversion. 43

Polymorphic Type Checking Most statically typed languages require that explicit type information be given for all names in each declaration. It is possible to use an extension of type inference to determine the types of names in a declaration without explicitly giving those types. The translator can collect information on uses of a name, and infer from the set of all uses a probable type (the most general type) for which all uses are correct. The Hindley-Milner type checking provides this kind of inference. It is a major feature of strongly typed functional languages such as ML and Haskell. 44

Type Conversion: Explicit conversions involve the writing of directives to explicitly convert from one type to another in the code. Example: int x = 2; double y = 3.3; y = (double) x; (type casting) Type casting will temporarily change the type of the RHS to that of the cast. 45