Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;

Slides:



Advertisements
Similar presentations
Names and Bindings.
Advertisements

ISBN Chapter 7 Expressions and Assignment Statements.
INF 212 ANALYSIS OF PROG. LANGS Type Systems Instructors: Crista Lopes Copyright © Instructors.
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,
ISBN Chapter 7 Expressions and Assignment Statements.
Copyright © by Curt Hill Expressions and Assignment Statements Part 2.
ISBN Chapter 7 Expressions and Assignment Statements.
CSC321: Programming Languages5-1 CSC321: Programming Languages Chapter 5: Types 5.1Type Errors 5.2Static and Dynamic Typing 5.3Basic Types 5.4NonBasic.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;
Elementary Data Types Scalar Data Types Numerical Data Types Other
Primitive Data Types: Numbers Strings Ordinal Types Pointers
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 6 Type Systems I was eventually persuaded.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Chapter 5: Types Fall 2009 Marco Valtorta.
ISBN Chapter 7 Expressions and Assignment Statements.
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.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;
ISBN Chapter 7 Expressions and Assignment Statements.
ISBN Lecture 07 Expressions and Assignment Statements.
Programming Languages 2nd edition Tucker and Noonan Chapter 6 Types I was eventually persuaded of the need to design programming notations so as to maximize.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 12 Imperative Programming I really hate this.
Expressions, Evaluation and Assignments
Types(2). 2 Recursive Problems  One or more simple cases of the problem have a straightforward, nonrecusive solution  The other cases can be redefined.
Copyright © 1998 by Addison -Wesley Longman, Inc. 1 Chapter 6 Arithmetic Expressions - Their evaluation was one of the motivations for the development.
1-1 University of Hail College of Computer Science and Engineering Department of computer Science and Software Engineering Course: ICS313: Fundamentals.
Chapter 7 Expressions and Assignment Statements. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Arithmetic Expressions Arithmetic evaluation.
C H A P T E R S E V E N Expressions and Assignment Statements.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Names and Binding In procedural programming, you write instructions the manipulate the “state” of the process where the “state” is the collection of variables.
Names Variables Type Checking Strong Typing Type Compatibility 1.
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.
Arithmetic Expressions
Expressions and Assignment Statements
Chapter 7 © 1998 by Addison -Wesley Longman, Inc Arithmetic Expressions - Their evaluation was one of the motivations for the development of the.
ISBN Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types.
Programming Languages 2nd edition Tucker and Noonan Chapter 6 Type Systems I was eventually persuaded of the need to design programming notations so as.
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.
ISBN Chapter 7 Expressions and Assignment Statements.
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 CS Programming Languages Class 09 September 21, 2000.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 1 Overview A good programming language is.
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
Pointers A pointer type variable holds the address of a data object or a function. A pointer can refer to an object of any one data type; it cannot refer.
Scalar and composite data Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
7-1/27 Chapter 7 Expressions and Assignment Statements Introduction Arithmetic Expressions Overloaded Operators Type Conversions Relational and Boolean.
Functional Programming Lecture 1 - Introduction Professor Muffy Calder.
Expressions and Assignment Statements
Types Type Errors Static and Dynamic Typing Basic Types NonBasic Types
Expressions and Assignment Statements
7.2 Arithmetic Expressions
Data Types In Text: Chapter 6.
Chapter 7: Expressions and Assignment Statements
Expressions and Assignment Statements
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
Expressions and Assignment Statements
Chapter 6: Data Types Lectures # 10.
Lecture 16: Introduction to Data Types
Chapter 7: Expressions and Assignment Statements
Expressions and Assignment Statements
Expressions and Assignment Statements
Introduction to Abstract Data Types
Expressions and Assignment Statements
College of Computer Science and Engineering
Chapter 7 Expressions and Assignment Statements.
B=15 b = a + fun(a); a=10 fun(a)=5.
PRESENTED BY ADNAN M. UZAIR NOMAN
Expressions and Assignment Statements
Lecture 7: Types (Revised based on the Tucker’s slides) 10/4/2019
Presentation transcript:

Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming; they make it digestible. Robin Milner

Copyright © 2006 The McGraw-Hill Companies, Inc. 5.1Type Errors 5.2Static and Dynamic Typing 5.3Basic Types 5.4NonBasic Types 5.5Recursive Data Types 5.6Functions as Types 5.7Type Equivalence 5.8Subtypes 5.9Polymorphism and Generics 5.10Programmer-Defined Types

Copyright © 2006 The McGraw-Hill Companies, Inc. A type is a collection of values and operations on those values. Example: Integer type has values..., -2, -1, 0, 1, 2,... and operations +, -, *, /, <,... The Boolean type has values true and false and operations , , .

Copyright © 2006 The McGraw-Hill Companies, Inc. Computer types have a finite number of values due to fixed size allocation; problematic for numeric types. Exceptions: Smalltalk uses unbounded fractions. Haskell type Integer represents unbounded integers. Floating point problems?

Copyright © 2006 The McGraw-Hill Companies, Inc. Even more problematic is fixed sized floating point numbers: 0.2 is not exact in binary. So 0.2 * 5 is not exactly 1.0 Floating point is inconsistent with real numbers in mathematics.

Copyright © 2006 The McGraw-Hill Companies, Inc. In the early languages, Fortran, Algol, Cobol, all of the types were built in. If needed a type color, could use integers; but what does it mean to multiply two colors. Purpose of types in programming languages is to provide ways of effectively modeling a problem solution.

Copyright © 2006 The McGraw-Hill Companies, Inc. 5.1 Type Errors Machine data carries no type information. Basically, just a sequence of bits. Example:

Copyright © 2006 The McGraw-Hill Companies, Inc The floating point number The 32-bit integer 1,079,508,992 Two 16-bit integers and 0 Four ASCII X NUL NUL

Copyright © 2006 The McGraw-Hill Companies, Inc. A type error is any error that arises because an operation is attempted on a data type for which it is undefined. Type errors are common in assembly language programming. High level languages reduce the number of type errors. A type system provides a basis for detecting type errors.

Copyright © 2006 The McGraw-Hill Companies, Inc. 5.2 Static and Dynamic Typing A type system imposes constraints such as the values used in an addition must be numeric. Cannot be expressed syntactically in EBNF. Some languages perform type checking at compile time (eg, C). Other languages (eg, Perl) perform type checking at run time. Still others (eg, Java) do both.

Copyright © 2006 The McGraw-Hill Companies, Inc. A language is statically typed if the types of all variables are fixed when they are declared at compile time. A language is dynamically typed if the type of a variable can vary at run time depending on the value assigned. Can you give examples of each?

Copyright © 2006 The McGraw-Hill Companies, Inc. A language is strongly typed if its type system allows all type errors in a program to be detected either at compile time or at run time. A strongly typed language can be either statically or dynamically typed. Union types are a hole in the type system of many languages. Most dynamically typed languages associate a type with each value.

Copyright © 2006 The McGraw-Hill Companies, Inc. 5.3 Basic Types Terminology in use with current 32-bit computers: Nibble: 4 bits Byte: 8 bits Half-word: 16 bits Word: 32 bits Double word: 64 bits Quad word: 128 bits

Copyright © 2006 The McGraw-Hill Companies, Inc. In most languages, the numeric types are finite in size. So a + b may overflow the finite range. Unlike mathematics: a + (b + c)  (a + b) + c Also in C-like languages, the equality and relational operators produce an int, not a Boolean

Copyright © 2006 The McGraw-Hill Companies, Inc. An operator or function is overloaded when its meaning varies depending on the types of its operands or arguments or result. Java: a + b (ignoring size) integer add floating point add string concatenation Mixed mode: one operand an int, the other floating point

Copyright © 2006 The McGraw-Hill Companies, Inc. A type conversion is a narrowing conversion if the result type permits fewer bits, thus potentially losing information. Otherwise it is termed a widening conversion. Should languages ban implicit narrowing conversions? Why?

Copyright © 2006 The McGraw-Hill Companies, Inc. 5.4 Nonbasic Types Enumeration: enum day {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday}; enum day myDay = Wednesday; In C/C++ the above values of this type are 0,..., 6. More powerful in Java: for (day d : day.values()) Sytem.out.println(d);

Copyright © 2006 The McGraw-Hill Companies, Inc. Pointers C, C++, Ada, Pascal Java??? Value is a memory address Indirect referencing Operator in C: *

Copyright © 2006 The McGraw-Hill Companies, Inc. Example struct Node { int key; struct Node* next; }; struct Node* head;

Copyright © 2006 The McGraw-Hill Companies, Inc. Fig 5.4: A Simple Linked List in C

Copyright © 2006 The McGraw-Hill Companies, Inc. Bane of reliable software development Error-prone Buffer overflow, memory leaks Particularly troublesome in C

Copyright © 2006 The McGraw-Hill Companies, Inc. float sum(float a[ ], int n) { int i; float s = 0.0; for (i = 0; i<n; i++) s += a[i]; return s; float sum(float *a, int n) { int i; float s = 0.0; for (i = 0; i<n; i++) s += *a++; return s;

Copyright © 2006 The McGraw-Hill Companies, Inc. void strcpy(char *p, char *q) { while (*p++ = *q++) ; } strcpy

Copyright © 2006 The McGraw-Hill Companies, Inc. Pointer Operations If T is a type and ref T is a pointer: & : T → ref T * : ref T → T For an arbitrary variable x: *(&x) = x

Copyright © 2006 The McGraw-Hill Companies, Inc. Arrays and Lists int a[10]; float x[3][5]; /* odd syntax vs. math */ char s[40]; /* indices: 0... n-1 */

Copyright © 2006 The McGraw-Hill Companies, Inc. Indexing Only operation for many languages Type signature [ ] : T[ ] x int → T Example float x[3] [5]; type of x: float[ ][ ] type of x[1]: float[ ] type of x[1][2]: float

Copyright © 2006 The McGraw-Hill Companies, Inc. Equivalence between arrays and pointers a = &a[0] If either e1 or e2 is type: ref T e1[e2] = *((e1) + (e2)) Example: a is float[ ] and i int a[i] = *(a + i)