Lecture 7: Types (Revised based on the Tucker’s slides) 10/4/2019

Slides:



Advertisements
Similar presentations
Programming Languages and Paradigms
Advertisements

Chapter 7:: Data Types Programming Language Pragmatics
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;
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.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;
PZ04A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ04A - Scalar and composite data Programming Language.
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;
ISBN Lecture 07 Expressions and Assignment Statements.
Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
Types(2). 2 Recursive Problems  One or more simple cases of the problem have a straightforward, nonrecusive solution  The other cases can be redefined.
1 CS 2104 Prog. Lang. Concepts Lecture 3 Dr. Abhik Roychoudhury School of Computing.
1-1 University of Hail College of Computer Science and Engineering Department of computer Science and Software Engineering Course: ICS313: Fundamentals.
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
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.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
ISBN Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.
Ch. 5 Ch. 51 jcmt Summer 2003Programming Languages CSE3302 Programming Languages (more notes) Summer 2003 Dr. Carter Tiernan.
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
Names, Scope, and Bindings Programming Languages and Paradigms.
1 CS Programming Languages Class 10 September 26, 2000.
 Data Type is a basic classification which identifies different types of data.  Data Types helps in: › Determining the possible values of a variable.
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
7-1/27 Chapter 7 Expressions and Assignment Statements Introduction Arithmetic Expressions Overloaded Operators Type Conversions Relational and Boolean.
5.2 Names - We discuss all user-defined names here
Expressions and Assignment Statements
Types Type Errors Static and Dynamic Typing Basic Types NonBasic Types
Expressions and Assignment Statements
7.2 Arithmetic Expressions
The C++ Data Types Fundamental Data Types
Data Types In Text: Chapter 6.
Chapter 7: Expressions and Assignment Statements
Chapter 6 – Data Types CSCE 343.
Chapter 6 Data Types.
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
Expressions and Assignment Statements
Expressions and Assignment
Chapter 6: Data Types Lectures # 10.
Lecture 16: Introduction to Data Types
Object Oriented Programming
CS 326 Programming Languages, Concepts and Implementation
Instructor : Ahmed Alalawi Slides from Chung –Ta King
Chapter 7: Expressions and Assignment Statements
Records Design Issues: 1. What is the form of references?
CSE 374 Programming Concepts & Tools
Expressions and Assignment Statements
C Basics.
Type Conversion, Constants, and the String Object
Expressions and Assignment Statements
Complex Data Types One very important measure of the “goodness” of a PL is the capability of its data types to model the problem space variables Design.
Data.
Introduction to Abstract Data Types
Expressions and Assignment Statements
Beginning C Lecture 2 Lecturer: Dr. Zhao Qinpei
College of Computer Science and Engineering
PZ04A - Scalar and composite data
Chapter 6 Data Types.
Chapter 7 Expressions and Assignment Statements.
Java Programming Language
Java’s Central Casting
PRESENTED BY ADNAN M. UZAIR NOMAN
Expressions and Assignment Statements
Presentation transcript:

Lecture 7: Types (Revised based on the Tucker’s slides) 10/4/2019 CS485, Lecture7 Type

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 , , . 10/4/2019 CS485, Lecture7 Type

5.1 Type Errors 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 is a precise definition of the bindings btw the type of a variable, its values, and the possible operations by a program 10/4/2019 CS485, Lecture7 Type

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. 10/4/2019 CS485, Lecture7 Type

Definitions 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? 10/4/2019 CS485, Lecture7 Type

Languages 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. 10/4/2019 CS485, Lecture7 Type

5.3 Basic Types Basic types usually correspond to the data types that are available on contemporary machines. 10/4/2019 CS485, Lecture7 Type

In most languages, the numeric types are finite in size. So a + b may overflow the finite range. Also in C-like languages, the equality and relational operators produce an int, not a Boolean 10/4/2019 CS485, Lecture7 Type

Otherwise it is termed a widening conversion. 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? 10/4/2019 CS485, Lecture7 Type

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); 10/4/2019 CS485, Lecture7 Type

Pointers C, C++, Ada, Pascal Java??? Value is a memory address Indirect referencing Operator in C: * 10/4/2019 CS485, Lecture7 Type

Bane of reliable software development Error-prone Buffer overflow, memory leaks Particularly troublesome in C E1[E2] = *(E1 + E2) 10/4/2019 CS485, Lecture7 Type

Comparison 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; 10/4/2019 CS485, Lecture7 Type

void strcpy(char *p, char *q) { while (*p++ = *q++) ; } 10/4/2019 CS485, Lecture7 Type

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 10/4/2019 CS485, Lecture7 Type

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 10/4/2019 CS485, Lecture7 Type

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) = i[a] 10/4/2019 CS485, Lecture7 Type

Strings Now so fundamental, directly supported. In C, a string is a 1D array with the string value terminated by a NUL character (value = 0). In Java, Perl, Python, a string variable can hold an unbounded number of characters. Libraries of string operations and functions. 10/4/2019 CS485, Lecture7 Type

Structures Analogous to a tuple in mathematics Collection of elements of different types Used first in Cobol, PL/I Absent from Fortran, Algol 60 Common to Pascal-like, C-like languages Omitted from Java as redundant 10/4/2019 CS485, Lecture7 Type

struct employeeType employee; ... employee.age = 45; int id; char name[25]; int age; float salary; char dept; }; struct employeeType employee; ... employee.age = 45; 10/4/2019 CS485, Lecture7 Type

Unions C: union Pascal: case-variant record Logically: multiple views of same storage Useful in some systems applications 10/4/2019 CS485, Lecture7 Type

begin tagged := (b => false, r => 3.375); type union = record case b : boolean of true : (i : integer); false : (r : real); end; var tagged : union; begin tagged := (b => false, r => 3.375); put(tagged.i); -- error 10/4/2019 CS485, Lecture7 Type

5.6 Functions as Types First Class Object: an entity that can be passed as a parameter, returned from a subroutine, or assigned to a value 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. 10/4/2019 CS485, Lecture7 Type

public interface RootSolvable { double valueAt(double x); } public double Newton(double a, double b, RootSolvable f); 10/4/2019 CS485, Lecture7 Type

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. Nowhere does it define identical type. 10/4/2019 CS485, Lecture7 Type

// which are equivalent types? struct complex { float re, im; }; struct polar { float x, y; struct { } a, b; struct complex c, d; struct polar e; int f[5], g[10]; // which are equivalent types? 10/4/2019 CS485, Lecture7 Type

Type Equivalence Name equivalence: two types have the same name Structure equivalence: two types have the same structure. Same number of fields Same order of fields For each field: same name and type 10/4/2019 CS485, Lecture7 Type