10/31/2015IT 3271 A Type is a Set 1. A set of values 2. A low-level representation 3. A collection of operations on those values int n; Chapter 6: Types.

Slides:



Advertisements
Similar presentations
Chapter 6 Type Checking. The compiler should report an error if an operator is applied to an incompatible operand. Type checking can be performed without.
Advertisements

Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Programming Languages and Paradigms
Names and Bindings.
Chapter 7:: Data Types Programming Language Pragmatics
1 Introduction to Data Types (Section 7.1) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael.
Elementary Data Types Prof. Alamdeep Singh. Scalar Data Types Scalar data types represent a single object, i.e. only one value can be derived. In general,
CSCI 330: Programming Language Concepts Instructor: Pranava K. Jha
Chapter 5: Elementary Data Types Properties of types and objects –Data objects, variables and constants –Data types –Declarations –Type checking –Assignment.
Type Checking.
Compiler Construction
Chapter 9 Imperative and object-oriented languages 1.
Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
ISBN Chapter 6 Data Types: Structured types.
1 Type Type system for a programming language = –set of types AND – rules that specify how a typed program is allowed to behave Why? –to generate better.
JavaScript, Third Edition
Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
Data Types.
Data Type. A data type defines a set of values that a variable can store along with a set of operations that can be performed on that variable. Common.
MT311 Java Application Programming and Programming Languages Li Tak Sing ( 李德成 )
1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
1 Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
Types Chapter SixModern Programming Languages, 2nd ed.1.
Names Variables Type Checking Strong Typing Type Compatibility 1.
5-1 Chapter 5: Names, Bindings, Type Checking, and Scopes Variables The Concept of Binding Type Checking Strong Typing Type Compatibility Scope and Lifetime.
March 12, ICE 1341 – Programming Languages (Lecture #6) In-Young Ko Programming Languages (ICE 1341) Lecture #6 Programming Languages (ICE 1341)
Lec 6 Data types. Variable: Its data object that is defined and named by the programmer explicitly in a program. Data Types: It’s a class of Dos together.
Chapter SixModern Programming Languages1 Types. Chapter SixModern Programming Languages2 A Type Is A Set n When you declare that a variable has a certain.
1 Type Checking Type checking ensures that the operands and the operator are of compatible types Generalized to include subprograms and assignments Compatible.
Chapter SixModern Programming Languages1 Types. Chapter SixModern Programming Languages2 A Type Is A Set Declaring a variable as a certain type restricts.
CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language.
TYPES Chapter Six Modern Programming Languages 1.
1 Records Record aggregate of data elements –Possibly heterogeneous –Elements/slots are identified by names –Elements in same fixed order in all records.
Arithmetic Expressions
ISBN Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types.
Types(1). Lecture 52 Type(1)  A type is a collection of values and operations on those values. Integer type  values..., -2, -1, 0, 1, 2,...  operations.
Homogeneous tuples What are they? –S 2 = S x S –S n = S x S x … x S Cardinalities –#(S 2 )= (#S) 2 –#(S n )= (#S) n –#(S 0 )= (#S) 0 =1 What is S 0 ? –It.
Structure of Programming Languages Names, Bindings, Type Checking, and Scopes.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 2 – August 23, 2001.
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
1 Structure of Compilers Lexical Analyzer (scanner) Modified Source Program Parser Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator.
Types Chapter SixModern Programming Languages 1. Elements of a Type Its set of values Its operations How its stored internally How its presented externally.
 Data Type is a basic classification which identifies different types of data.  Data Types helps in: › Determining the possible values of a variable.
Chapter 1: Preliminaries Lecture # 2. Chapter 1: Preliminaries Reasons for Studying Concepts of Programming Languages Programming Domains Language Evaluation.
Types Chapter SixModern Programming Languages 1. Elements of a Type Its set of values Its operations How its stored internally How its presented externally.
1 Scalar and composite data Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Scalar and composite data Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
CSI 3125, Data Types, page 1 Data types Outline Primitive data types Structured data types Strings Enumerated types Arrays Records Pointers Reading assignment.
Dr. M. Al-Mulhem Introduction 1 Chapter 6 Type Systems.
Records type city is record -- Ada Name: String (1..10); Country : String (1..20); Population: integer; Capital : Boolean; end record; struct city { --
Data Types In Text: Chapter 6.
Chapter 6 – Data Types CSCE 343.
CSE 3302 Programming Languages
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
Type Checking, and Scopes
Lecture 16: Introduction to Data Types
CS 326 Programming Languages, Concepts and Implementation
Instructor : Ahmed Alalawi Slides from Chung –Ta King
Basics of ‘C’.
CS 363 – Chapter 7 Chapter 7 – type systems Types that are supported
Chapter 2 Primitive Data Types and Operations
Types and Related Issues
Lecture 7: Types (Revised based on the Tucker’s slides) 10/4/2019
Presentation transcript:

10/31/2015IT 3271 A Type is a Set 1. A set of values 2. A low-level representation 3. A collection of operations on those values int n; Chapter 6: Types Bertrand Russell (1872 – 1970) (1910) Alfred N. Whitehead (1861 – 1947) Required for programming languages, so it is more restricted.

10/31/2015IT 3272 n Issues of types – Type annotations (how to declare?) – Type inference (how to determine?) – Type checking (when to check?) – Type equivalence issues (what is it?)

10/31/2015IT 3273 Primitive vs. Constructed Types n Primitive type: can be used but cannot be defined. ML: int, real, char n Constructed type: can be defined in a program ML: type intpair = int * int

10/31/2015IT 3274 Primitive Types n Every programming language defines its primitive types – Some define the primitive types precisely for implementation (e.g., Java) – Some leave it loose for implementation (e.g., C, ML)

10/31/2015IT 3275 Integral Types in different PL’s C: char unsigned char short int unsigned short int int unsigned int long int unsigned long int No standard implementation, but longer sizes must provide at least as much range as shorter sizes. Java: byte (1-byte signed) char (2-byte unsigned) short (2-byte signed) int (4-byte signed) long (8-byte signed) Scheme: integer Integers of unbounded range

10/31/2015IT 3276 Issues of primitive types n What sets of values? – Language specification; how much left up to the implementation? – If necessary, how can a program find out? ( INT_MAX in C, Int.maxInt in ML, etc.) n What operations are supported? – Detailed definitions: rounding, exceptions, etc. The choice of representation is a critical part in these decisions

10/31/2015IT 3277 Constructed Types 1.enumerations 2.tuples 3.arrays 4.strings 5.lists 6.unions 7.subtypes 8.function types Example: Each has a connection to a set operation in mathematics

10/31/2015IT 3278 Making Sets by Enumeration n We can construct sets by just listing all the elements: C: enum coin {penny, nickel, dime, quarter}; Ada: type GENDER is (MALE, FEMALE); Pascal: type primaryColors = (red, green, blue); ML: datatype day = M | Tu | W | Th | F | Sa | Su; n These define a new type (= set) n They also define a collection of named constants of that type (= elements)

10/31/2015IT 3279 Representing Enumeration Values n A common representation is to treat the values of an enumeration as small integers enum coin { penny = 1, nickel = 5, dime = 10, quarter = 25 }; enum escapes { BELL = '\a', BACKSPACE = '\b', TAB = '\t', NEWLINE = '\n', VTAB = '\v', RETURN = '\r' }; They are countable!!

10/31/2015IT Operations on Enumeration Values n Equality test: n If using integer to represent elements then elements are legitimate for all integer operations: fun isWeekend x = (x = Sa orelse x = Su); Pascal: for C := red to blue do P(C) C: int x = penny + nickel + dime;

10/31/2015IT Making Sets by Tupling n The Cartesian product of two or more sets defines tuples: n Some language like ML supports pure tuples: (i.e., fields without names) fun get1 (x : real * real) = #1 x;

10/31/2015IT Making Types by Tupling n Many PLs support record types: tuples with named fields C: struct complex { double rp; double ip; } x; ML: type complex = { rp:real, ip:real }; Operations on tuples: (selection) C: x.ip ML: #ip x fun getip (x : complex) = #ip x;

10/31/2015IT Representing Tuple Values n A common representation is to just place the elements side-by-side in memory n But there are lots of details: – In what order? – Are there “holes” between elements (e.g. on word boundaries) in memory? – Are these details visible to the programmer?

10/31/2015IT What does C say about tuples (structs)? Preface C is a general-purpose programming language which features economy of expression, modern control flow and data structures, and a rich set of operators. C is not a “very high level” language, nor a “big” one, and is not specialized to any particular area of application. But its absence of restrictions and its generality make it more convenient and effective for many tasks than supposedly more powerful languages. ….. The C Programming Language Brian W. Kernighan and Dennis M. Ritchie 1978 edition Let’s see what does C say about itself first.

10/31/2015IT ANSI C The members of a structure have addresses increasing in the order of their declarations. A non-field member of a structure is aligned at an addressing boundary depending on its type; therefore, there may be unnamed holes in a structure. struct { unsigned keyword : 1; unsigned external : 1; unsigned statics : 1; int value; unsigned ascii : 7; char eascii; } flag1, flag2; flag1 flag2 unnamed holes

10/31/2015IT Sets of Vectors n Fixed-size vectors: n Arbitrary-size vectors: n In PLs: arrays, strings, lists n Issues: – What are the index values? – Is the array size fixed at compile time? What is the difference between this and tuples?

10/31/2015IT Index values for array n Java, C, C++: – First element of an array a is a[0] – Indexes are always integers starting from 0 n Pascal is more flexible: – integers, characters, enumerations, subranges – Starting/Ending index chosen by the programmer – size is fixed at compile time type LetterCount = array['a'..'z'] of Integer; var Counts: LetterCount; begin Counts['a'] = 1 ………

10/31/2015IT n Array designers need to decide: 1.What are the index values? 2.Is array size fixed at compile time (static type)? 3.What operations are supported? 4.Are multiple dimensions allowed? 5.Is a higher-dimensional array the same as an array of arrays? 6.Is re-dimensioning possible at runtime? 7.What is the order of elements in memory? 8.Is there a separate type for strings (not just array of characters)? 9.Is there a separate type for lists?

10/31/2015IT Making Types by Union C: union element { int i; float f; }; ML: datatype element = I of int | F of real; Making Sets by Union:

10/31/2015IT Representing Union Values n Two (or more) representations overlap each other in memory. n This representation may or may not be exposed to the programmer union element { int i; char *p; } u; /* sizeof(u) is */ /* max(sizeof(u.i),sizeof(u.p)) */

10/31/2015IT Loosely Typed Unions n Some languages expose the details of union implementation (i.e., the programmer knows the implementation) union element { int i; float f; } e; No type casting is involved e f i float x; int n; e.i = 100; e.f = 1.0; x = e.f; n = e.i;

10/31/2015IT Strictly Typed Unions n In ML, an element type x is either I or F, thus, you have to say what to do with each type of value in the union: datatype element = I of int | F of real; Type casting is involved fun getReal (F x) = x | getReal (I x) = real x;

10/31/2015IT Variant Records n Union where specific type is dynamically determined by the value of a field (“discriminated union”) n Ada and Modula-2 C: I don’t care what is it. ML: I have to know what is it in advance Ada: Let’s see what is it.

10/31/2015IT Ada Variant Record Example type DEVICE is (PRINTER, DISK); type PERIPHERAL(Unit: DEVICE) is record HoursWorking: INTEGER; case Unit is when PRINTER => Line_count: INTEGER; when DISK => Cylinder: INTEGER; Track: INTEGER; end case; end record;

10/31/2015IT Subsets n We can define the subset selected by any predicate P: S X

10/31/2015IT Making Subtypes – Less general: Pascal subranges type digit = 0..9; – More general: Ada subtypes subtype DIGIT is INTEGER range 0..9; subtype WEEKDAY is DAY range MON..FRI; – Most general: Lisp types with predicates

10/31/2015IT Ada Subtypes type DEVICE is (PRINTER, DISK); type PERIPHERAL(Unit: DEVICE) is record HoursWorking: INTEGER; case Unit is when PRINTER => Line_count: INTEGER; when DISK => Cylinder: INTEGER; Track: INTEGER; end case; end record; subtype DISK_UNIT is PERIPHERAL(DISK);

10/31/2015IT Types with Predicates in Lisp (declare (type integer x)) (declare (type (or null cons) x)) (declare (type (and number (not integer)) x)) (declare (type (and integer (satisfies evenp)) x))

10/31/2015IT Representing Subtype Values n Usually, we just use the same representation for the subtype as for the supertype n Questions: – Do we want to shorten it if we can? I.e., does X: 1..9 take the same space as X: Integer ? – Do we enforce the subtyping? Is X := 10 legal? What about X := X + 1 ?

10/31/2015IT n Type is a key idea of OOP n A class is a type: data and operations on that data, bundled together n A subclass is a subtype: it includes a subset of the objects, but supports a superset of the operations

10/31/2015IT Making Sets of Functions n define a set of functions with given domain and range: A set can be a collection of functions

10/31/2015IT Making Types of Functions n Most languages have some notion of the type of a function: int f(char a, char b) { return a==b; } fun f(a:char, b:char) = (a = b); ML: C:

10/31/2015IT Operations on Function Values n call the function n bound to variables n passed as parameters n Treated as value Many languages support nothing beyond function call (What can C do?) ML supports many operations (all above and others)

10/31/2015IT Type Annotations The syntax for describing types n Some languages use naming conventions to declare the types of variables – BASIC: S$ is a string – Fortran: I is an integer n Like explicit annotations, these conventions provide static type information

10/31/2015IT Type Inference n Infers a static type for every expression and for every function, usually requires no annotations n Constants usually have static types – Java: 10 has type int, 10L has type long n Expressions may have static types inferred from operators and types of operands – Java: if a is double, a*0 is double ( 0.0 ) Simple Complicate Determining the types

10/31/2015IT Static Type Checking n Static type checking determines a type for everything before running the program: variables, functions, expressions, everything n Compile-time error messages when static types are not consistent – Operators: 1+"abc" – Functions: round("abc") – Statements: if "abc" then … n Most modern languages are statically typed

10/31/2015IT Dynamic Typing n In some languages, programs are not statically type-checked before being run. they are dynamically type-checked during the runtime. n At runtime, the language system checks that operands are of suitable types for operations

10/31/2015IT Example: Lisp It won’t work if a or b is not a number An improper call, like (f nil nil), is not caught at compile time n It is caught at runtime – that is dynamic typing (defun f (a b) (+ a b))

10/31/2015IT Explicit Runtime Type Tests n Some languages allow explicit runtime type tests: – Java: test object type with instanceof operator n Type information is known at runtime, even when the language is mostly statically typed

10/31/2015IT Strong Typing  Weak Typing The purpose of type-checking is to prevent the application of operations to incorrect types of operands n ML and Java, the type-checking is thorough enough to guarantee this—that’s strong typing n C has holes in the type system that add flexibility but weaken the guarantee

10/31/2015IT Type Equivalence What happens if apply f to an irpair2 ? – Name equivalence does not permit this: irpair2 and irpair1 are different names – Structural equivalence does permit this, since the types are constructed identically n ML uses Structural equivalence type irpair1 = int * real; type irpair2 = int * real; fun f(x:irpair1) = #1 x;