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
Programming Languages and Paradigms
Advertisements

Programming Paradigms Introduction. 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1:
Names and Bindings.
Chapter 7:: Data Types Programming Language Pragmatics
Abstract Data Types Data abstraction, or abstract data types, is a programming methodology where one defines not only the data structure to be used, but.
Chapter 5: Elementary Data Types Properties of types and objects –Data objects, variables and constants –Data types –Declarations –Type checking –Assignment.
Compiler Construction
Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.
CSC321: Programming Languages5-1 CSC321: Programming Languages Chapter 5: Types 5.1Type Errors 5.2Static and Dynamic Typing 5.3Basic Types 5.4NonBasic.
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 6 Data Types: Structured types.
G. Levine Chapter 6 Chapter 6 Encapsulation –Why do we want encapsulation? Programmer friendliness- programmer need not know about these details –Easier.
ISBN Chapter 9 Subprograms. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Introduction Two fundamental abstraction facilities.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Chapter 5: Types Fall 2009 Marco Valtorta.
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;
C++ for Engineers and Scientists 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.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 9 Functions It is better to have 100 functions.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 12 Imperative Programming I really hate this.
Types(2). 2 Recursive Problems  One or more simple cases of the problem have a straightforward, nonrecusive solution  The other cases can be redefined.
MT311 Java Application Programming and Programming Languages Li Tak Sing ( 李德成 )
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Chapter 12: Adding Functionality to Your Classes.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
 2006 Pearson Education, Inc. All rights reserved Arrays.
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 II Survey of Common Types I Records –E.g., structs in C++ –If elements are named, a record is projected into its fields (e.g., via.
March 12, ICE 1341 – Programming Languages (Lecture #6) In-Young Ko Programming Languages (ICE 1341) Lecture #6 Programming Languages (ICE 1341)
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 11 Structured Data.
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.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. C H A P T E R F I V E Memory Management.
Copyright © Curt Hill Structured Data What this course is about.
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.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;
Session 24 Chapter 12: Polymorphism. Polymorphism polymorphism comes from the Greek root for “many forms” polymorphism is about how we can use different.
ISBN Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
Java Software Solutions Lewis and Loftus Chapter 6 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects for Organizing Data.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Names, Scope, and Bindings Programming Languages and Paradigms.
Chapter 1: Preliminaries Lecture # 2. Chapter 1: Preliminaries Reasons for Studying Concepts of Programming Languages Programming Domains Language Evaluation.
ISBN Chapter 12 Support for Object-Oriented Programming.
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.
Data Types In Text: Chapter 6.
Chapter 6 – Data Types CSCE 343.
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
Java Primer 1: Types, Classes and Operators
CSC 533: Programming Languages Spring 2015
7 Arrays.
Course Overview PART I: overview material PART II: inside a compiler
CSC 533: Programming Languages Spring 2019
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. Contents 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 Arrays and Lists Arrays: Sequence of values, accessible by index All array values are from the same type Design issues: –Number of dimensions/declaration syntax –Index type –Are array dimensions static or dynamic? –Are the boundaries of the index set determined by the language or the programmer? –Should the language enforce bounds checking?

Copyright © 2006 The McGraw-Hill Companies, Inc. Design Issues for Arrays Declaration syntax/number of dimensions –Technically, C only allows one dimension int C[4][3]; //array of 4 3-element rows –Compare to Pascal: TYPE Num = ARRAY[1..4, 1..3] OF Integer; VAR Num:C; and Fortran: integer C(4,3) early Fortran version : DIMENSION i(4,3) –Early languages set an upper limit on the number of dimensions

Copyright © 2006 The McGraw-Hill Companies, Inc. Array Design Issues Index type –Usually restricted to integers –Pascal allows any integral type: integers, char, enum

Copyright © 2006 The McGraw-Hill Companies, Inc. Array Design Issues Are the bounds of the index set fixed? C-like languages: (0, …, n-1) Fortran: defaults to (1, 2, …, n) but modifiable: – INTEGER Counts (-20:20) Pascal & Ada: programmer determined –Ada: a : array(-4..5) of INTEGER; –Pascal: TYPE nums = ARRAY[10…19] OF Integer nums: Numbers

Copyright © 2006 The McGraw-Hill Companies, Inc. Array Design Issues Are array dimensions static or dynamic? –Pascal: always static; array dimensions are part of the array type. –Java arrays are sized at runtime, don’t change thereafter –C/C++ permits runtime declaration of array bounds for dynamically declared arrays: int numbers[100]; // static array int* intPtr; cin >> n; intPtr = new int[n];//dynamic array

Copyright © 2006 The McGraw-Hill Companies, Inc. Design Issues Should the language enforce bounds checking? –Java and Ada do; Pascal provides it as an option Safety versus efficiency Overwriting array boundaries is one of the most common (and hardest to find) errors in a program –C & C++ generate a memory protection error if the reference is outside the boundaries of the program address space, otherwise no error message is given

Copyright © 2006 The McGraw-Hill Companies, Inc. Implementation Issues Dope Vector: array data needed at run time; e.g., –Element size & type –Index type/range for each dimension –Start address of array in memory Used to calculate memory addresses, & do index range checking if included in language Built during the semantic analysis phase of translation

Copyright © 2006 The McGraw-Hill Companies, Inc. Example/Java Consider the Java declaration int[] A = new int(n); At runtime, every reference A[i] must be checked: is 0 < i < n ? What memory address is referenced? Portions of the dope vector must be present at run- time for checking.

Copyright © 2006 The McGraw-Hill Companies, Inc. Dope Vectors For a one-dimensional array of integers, where integers take 4 bytes of storage For a two-dimensional array of characters, where characters take 2 bytes 4 = e, element size int = element type 10 = size (n) 2 = e, element size char = element type 4 = # of rows (m) 3 = # of col’s (n)

Copyright © 2006 The McGraw-Hill Companies, Inc. Address Computation To compute the address of a given element : addr(a[i]) = addr(a[0]) + e∙i (e = element size) addr(a[i][j]) = addr(a[0][0]) + e∙(ni + j) (n = number of columns) Two-dimensional array formula assumes row-major storage order; Fortran uses column-major. This formula assumes indexes start at 0; for languages that permit other bounds, modify the formula.

Copyright © 2006 The McGraw-Hill Companies, Inc. Strings Early languages – little or no direct support –Fortran, Algol60: none –COBOL: static strings, few operations –C: string is a 1D array terminated by a NUL character error prone In Java, Perl, Python, strings are built-in types; length is not restricted. C++ provides a string library – not a built in type.

Copyright © 2006 The McGraw-Hill Companies, Inc. Structures (Records) Collection of elements of different types –Elements commonly referred to as fields –Stored sequentially in memory, with some qualifications Used first in Cobol, PL/I Absent from Fortran, Algol 60 Common to Pascal-like, C-like languages How does a C++ struct differ from a class? Omitted from Java as redundant

Copyright © 2006 The McGraw-Hill Companies, Inc. struct employeeType { int id; char name[25]; int age; float salary; char dept; }; struct employeeType employee;... employee.age = 45;

Copyright © 2006 The McGraw-Hill Companies, Inc. Unions C/C++: union Pascal/Ada: variant record Logically: multiple views of same storage Infrequently used today, but might be useful in some systems applications Complicates issues of type safety since actual data type isn’t known at compile time.

Copyright © 2006 The McGraw-Hill Companies, Inc. union myUnion {// c/c++ example int i; float r; }; union myUnion u; // can only use one member at a time u.i = 10; // u is an integer u.r = 3.14; // u is a now a float Compiler reserves enough space to hold the largest member

Copyright © 2006 The McGraw-Hill Companies, Inc. 5.5 Recursive Data Type C/C++ linked lists are an example of a recursive data structure since the struct Node contained a field that was a pointer to a structure of the same type. In other words, recursion is allowed in the type definition. Other languages; e.g., Haskell, provide more general ways to define recursive data structures.

Copyright © 2006 The McGraw-Hill Companies, Inc. 5.6 Functions as Types Languages differ as to what kinds of entities can be assigned to a variable, or passed as an argument, or in other ways treated as a value of some type. Functions are an example. Pascal example: function newton(a, b: real; function f: real): real; Know that f returns a real value, but the arguments to f are unspecified.

Copyright © 2006 The McGraw-Hill Companies, Inc. Functions as Types Java interfaces can be used to achieve a similar effect. Not possible to construct functions and return them as function values. Should languages make functions types? Increases the generality of languages; see discussion of Scheme programming language.

Copyright © 2006 The McGraw-Hill Companies, Inc. Java Interface public interface RootSolvable { double valueAt(double x); } public double Newton(double a, double b, RootSolvable f);

Copyright © 2006 The McGraw-Hill Companies, Inc. 5.7 Type Equivalence Pascal Report: The assignment statement serves to replace the current value of a variable with a new value specified as an expression.... The variable (or the function) and the expression must be of identical type. What is an identical type? For example, struct or array definitions in C:

Copyright © 2006 The McGraw-Hill Companies, Inc. struct complex { float re, im; }; struct polar { float x, y; }; struct { /* anonymous type */ float re, im; } a, b; struct complex c, d; struct polar e; int f[5], g[10]; // which are equivalent types?

Copyright © 2006 The McGraw-Hill Companies, Inc. Equivalent Types Name Equivalence: two types are equivalent if they have the same name. In the example, c and d are equivalent because both are type complex. a & c aren’t. Java approach for classes, C/C++ for structs Structural Equivalence: types are equivalent if they have the same structure. For record types this means having the same number and order of fields, as well as name and type. a, b, c, and d are equivalent but d and e aren’t. C/C++ approach for arrays. f and g are the same type (1-d array of int s)

Copyright © 2006 The McGraw-Hill Companies, Inc. 5.8 Subtypes A subtype is a type that has certain constraints placed on its values or operations. Like type equivalence, subtypes are related to validity of assignments, passing parameters to functions, etc. A variable from a subtype can be passed as a parameter of the primary type, or assigned to a variable of the primary type, but not necessarily vice versa

Copyright © 2006 The McGraw-Hill Companies, Inc. Type Equivalence - Examples In Ada & Pascal subtypes can be directly specified. May be used to limit values to a certain range (Ada): subtype Weekend is Day range Sat..Sun ; Java’s class hierarchy is a kind of subtyping : if s is an object of class S and t is an object of class T and S is a subclass of T, then t = s is legal.

Copyright © 2006 The McGraw-Hill Companies, Inc. ADA Example subtype one_to_ten is Integer range 1..10; type Day is (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday); subtype Weekend is Day range Saturday.. Sunday; type Salary is delta 0.01 digits 9 range _999_999.99; subtype Author_Salary is Salary digits 5 range ;

Copyright © 2006 The McGraw-Hill Companies, Inc. JAVA Integer i = new Integer(3);... Number v = i;... Integer x = (Integer) v; //Integer is a subclass of Number, // and therefore a subtype In the first case i is assigned to v because an Integer is a Number, but v must be cast before assigning it to an Integer variable. Compare to widening and narrowing type conversions.

Copyright © 2006 The McGraw-Hill Companies, Inc. 5.8: Polymorphism and Generics From the Greek word meaning “having two or more forms” A function or operation is polymorphic if it can be applied to any one of several related types and achieve the same result. An advantage of polymorphism in a programming language is that it enables code reuse.

Copyright © 2006 The McGraw-Hill Companies, Inc. Polymorphism Java: overloaded methods: the number or type of parameters is used to distinguish between them Java: instance variable, method: name, name( ) Beyond overloading, an important feature of polymorphism is the ability to write functions that can be used for the same purpose on different data types.

Copyright © 2006 The McGraw-Hill Companies, Inc. Parametric Polymorphism A type of polymorphism in which code is written without any type-specific references. Later, the code can be used with any data type. Functions that are written in this way are called polymorphic functions. Compare to subtype polymorphism, which is based on class hierarchies and inheritance.

Copyright © 2006 The McGraw-Hill Companies, Inc. Ada generic functions – templates instantiated at compile time with concrete types and operators –parametric polymorphism –type binding delayed from programming time to compile time Example: In a simple bubble sort (page 128), the critical operations are –Looping through the data –Comparing two elements –Exchanging two elements Which operations are type dependent?

Copyright © 2006 The McGraw-Hill Companies, Inc. Parametric Polymorphism - Ada Generics Looping is independent of the data Assignment depends on the data type to the extent that the amount of storage assigned to the parameters is data-type dependent The compare operation is type dependent (compare integers differently than strings, for example) Basic logic does not change

Copyright © 2006 The McGraw-Hill Companies, Inc. generic type element is private; type list is array(natural range <>) of element; with function ">"(a,b:element)return boolean; package sort_pck is procedure sort (in out a : list); end sort_pck;

Copyright © 2006 The McGraw-Hill Companies, Inc. package sort_pck is procedure sort (in out a : list) is begin for i in a'first.. a'last - 1 loop for j in i+1.. a'last loop if a(i) > a(j) then declare t : element; begin t := a(i); a(i) := a(j); a(j) := t; end; end if;

Copyright © 2006 The McGraw-Hill Companies, Inc. Instantiation To specify an integer sort include the following statement: package integer_sort is new generic_sort( Integer, ">" ); Tells the compiler that integer_sort is a specialized version of generic_sort, with element type Integer and operation “ > ”, the usual math compare operator.

Copyright © 2006 The McGraw-Hill Companies, Inc. Generics/Templates Ada, C++ and Java all provide parametric polymorphism in the form of generics or templates One way to implement (C++ method) is by simple textual substitution of concrete parameters for generic parameters prior to compilation Object-oriented languages also provide another type of polymorphism through the inheritance mechanism.

Copyright © 2006 The McGraw-Hill Companies, Inc. Pure or SubTyping Polymorphism A function that specifies a parameter from type T will also accept an argument from type S, if S is a subtype of T. (based on inheritance) Example: Class – GeometricShape has method Draw. Subclasses Circle, Square, etc. each have their own Draw method. Type is bound at runtime, since the object passed to the method determines which version of Draw is called.

Copyright © 2006 The McGraw-Hill Companies, Inc Programmer-defined Types Recall the definition of a type: A set of values and a set of operations on those values. Structures allow a definition of a representation; problems: Representation is not hidden No new type operations can be defined Objects address this problem, but discussion will be deferred until later.