CSE 3302 Programming Languages Chengkai Li, Weimin He Spring 2008 Data Types Lecture 7 – Data Types, Spring 2008 1 CSE3302 Programming Languages, UT-Arlington.

Slides:



Advertisements
Similar presentations
C Programming Lecture 23 Enumeration Types Structures.
Advertisements

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.
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Names and Bindings.
1 Chapter 6 Data Types What is a data type? A set of values versus A set of values + set of operations on those values.
1 Chapter 6 Data Types What is a data type? A set of values versus A set of values + set of operations on those values.
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.
Chapter Four Data Types Pratt 2 Data Objects A run-time grouping of one or more pieces of data in a virtual machine a container for data it can be –system.
CS 331, Principles of Programming Languages Chapter 4 Types: Data Representation.
Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
CS 330 Programming Languages 11 / 06 / 2007 Instructor: Michael Eckmann.
ISBN Chapter 6 Data Types: Structured types.
Elementary Data Types Scalar Data Types Numerical Data Types Other
Primitive Data Types: Numbers Strings Ordinal Types Pointers
Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
CS 355 – Programming Languages
Multidimensional Arrays C++ also allows an array to have more than one dimension. For example, a two-dimensional array consists of a certain number of.
MT311 Java Application Programming and Programming Languages Li Tak Sing ( 李德成 )
ISBN 0-321— Chapter 6 sections 1-4, 9 Primitive Data Types Numbers Strings Ordinal Types Pointers.
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.
PRIMITIVE DATA TYPES -Integer -Floating Point -Decimal -Boolean -Character STRINGS -Character Array -Class -String Length -Static -Limited Dynamic -Dynamic.
C Tokens Identifiers Keywords Constants Operators Special symbols.
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.
1 Records Record aggregate of data elements –Possibly heterogeneous –Elements/slots are identified by names –Elements in same fixed order in all records.
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.
C# C1 CSC 298 Elements of C# code (part 1). C# C2 Style for identifiers  Identifier: class, method, property (defined shortly) or variable names  class,
Programming Languages Third Edition Chapter 8 Data Types.
ENUMERATED DATATYPES. USER DEFINED DATA TYPES  Data Type Defined By Programmer  Allows Use Of More Complex Data  Typically Defined Globally So Variables.
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
Data Types simple and compound abstraction and implementation.
Chapter 4 of Programming Languages by Ravi Sethi.
ISBN Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.
ISBN Chapter 6 Data Types. Copyright © 2006 Addison-Wesley. All rights reserved. 6-2 Chapter 6 Topics Introduction Primitive Data Types.
Recitation 5 Enums and The Java Collections classes/interfaces 1.
Data Types W E E K F O U R. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Chapter 6 Topics Introduction Primitive Data Types Character String.
CSE 3302 Programming Languages Chengkai Li, Weimin He Spring 2008 Abstract Data Types and Modules Lecture 11 – ADT and Modules, Spring CSE3302 Programming.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Variables reference, coding, visibility. Rules for making names  permitted character set  maximum length, significant length  case sensitivity  special.
CS 330 Programming Languages 11 / 01 / 2007 Instructor: Michael Eckmann.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
CSE 3302 Programming Languages
Enumerated Types Mr. Dave Clausen La Cañada High School.
ISBN Chapter 6 Data Types. Copyright © 2006 Addison-Wesley. All rights reserved.2 Primitive Data Types Almost all programming languages.
Data Types (1) 1 Programming Languages – Principles and Practice by Kenneth C Louden.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Structure of Programming Language Data Types. 2 A data type defines a collection of data objects and a set of predefined operations on those objects An.
Data Types Chapter 6: Data Types Lectures # 12. Topics Chapter 6: Data Types 2 Introduction Primitive Data Types Character String Types Array Types Associative.
CSE 3302 Programming Languages Chengkai Li, Weimin He Spring 2008 Control I Expressions and Statements Lecture 9 – Control I, Spring CSE3302 Programming.
Java Programming Language Lecture27- An Introduction.
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 { --
CSC 533: Programming Languages Spring 2016
Data Types In Text: Chapter 6.
Structures, Unions, Enumerations
Chapter 6 – Data Types CSCE 343.
CSC 533: Programming Languages Spring 2015
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.
CSE 3302 Programming Languages
Lecture 16: Introduction to Data Types
CS 326 Programming Languages, Concepts and Implementation
Instructor : Ahmed Alalawi Slides from Chung –Ta King
CSE 3302 Programming Languages
Semantic Analysis Chapter 6.
CSE 3302 Programming Languages
CSE 3302 Programming Languages
Presentation transcript:

CSE 3302 Programming Languages Chengkai Li, Weimin He Spring 2008 Data Types Lecture 7 – Data Types, Spring CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

Data Types What is a data type? A name with certain attributes: – The values that can be stored, the internal representation, the operations, … A data type is a set of values e.g., int in Java: int x; x  Integers= [ , ] A data type is also a set of operations on the values Lecture 7 – Data Types, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He,

Why are data types important? Example: z = x / y; (Java) – int x, y; x=5; y=2; Integer division, x/y results in 2. int z: z = 2; double z: z=2.0; – double x, y; x=5; y=2; floating-point division, x/y results in 2.5 int z: wrong! double z: z=2.5; Lecture 7 – Data Types, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He,

Lecture 7 – Data Types, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, Type structure of Java

Lecture 7 – Data Types, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, Type structure of C

Simple Data Types No internal structure: e.g., integer, double, character, and boolean. Often directly supported in hardware. – machine dependency Most predefined types are simple types. – Exceptions: String in Java. Some simple types are not predefined Enumerated types Subrange types Lecture 7 – Data Types, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He,

Enumerated Types Ordered set, whose elements are named and listed explicitly. Examples: enum Color_Type {Red, Green, Blue}; ( C ) type Color_Type is (Red, Green, Blue); ( Ada ) datatype Color_Type = Red | Green | Blue; ( ML ) Operations: ? Successor and predecessor Lecture 7 – Data Types, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He,

Ada Example type Color_Type is (Red, Green, Blue); x : Color_Type := Green; x : Color_Type’Succ(x); x : Color_Type’Pred(x); put(x); -- prints GREEN No assumptions about the internal representation of values Print the value name itself Lecture 7 – Data Types, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Pascal Example type cardsuit = (club, diamond, heart, spade); card = record suit: cardsuit; value: ; end; var hand: array [ ] of card; Succ(diamond) = heart; Pred(spade) = heart; club < heart; is true. for acard := club to heart do Lecture 7 – Data Types, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

C Example #include enum Color {Red, Green, Blue}; enum Courses {CSE1111=1, CSE3302=3, CSE3310=3, CSE5555=4}; main() { enum Color x = Green; enum Courses c = CSE3302; x++; printf("%d\n“,x); printf("%d\n",Blue+1); printf("%d\n",c); return 0; } Enum in C is simply int Can customize the values Lecture 7 – Data Types, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Java Example public enum Planet { MERCURY (2.4397e6), EARTH ( e6); private final double radius; // in meters Planet(double radius) {this.radius = radius; } private double radius() { return radius; } public static void main(String[] args) { for (Planet p : Planet.values()) System.out.printf(“The radius of %s is %f\n", p, p.radius()); } java.util.Enumeration has different meaning for (Enumeration e = v.elements() ; e.hasMoreElements() ;) System.out.println(e.nextElement()); Lecture 7 – Data Types, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Evaluation of Enumeration Types Efficiency – e.g., compiler can select and use a compact efficient representation (e.g., small integers) Readability -- e.g. no need to code a color as a number Maintainability – e.g., adding a new color doesn’t require updating hard-coded constants. Reliability -- e.g. compiler can check operations and ranges of value. Lecture 7 – Data Types, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Courtesy of Charles Nicholas at UMBC

C Example for Maintainability enum Color {White, Green, Blue, Black}; enum Color {White, Yellow, Green, Blue, Black}; main(){ enum Color x = Black; int i = x; while (i >= White){ if (i < Green) printf(“this is a light color!\n”); i--; } } What if no enumeration? if (i < 1) printf(“this is a light color!\n”); Has to be changed to: if (i < 2) printf(“this is a light color!\n”); Lecture 7 – Data Types, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Ada Example for Reliability type DAY is (MON, TUE, WED, THU, FRI, SAT, SUN); type DIRECTION is (NORTH, EAST, SOUTH, WEST); GOAL : DIRECTION; TODAY : DAY; START : DAY; TODAY := MON; GOAL := WEST; START := TODAY; TODAY := WEST; -- Illegal: WEST is not a DAY value TODAY := 5; -- Illegal: 5 is not a DAY value TODAY := TODAY + START; -- Illegal: "+" is not defined for DAYS Lecture 7 – Data Types, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Subrange Types Contiguous subsets of simple types, with a least and greatest element. Example: type Digit_Type is range 0..9; ( Ada) Not available in C,C++,Java. Need to use something like: byte digit; // if (digit>9 || digit <0) throw new DigitException(); defined over ordinal types: – ordered, every value has a next/previous element E.g., integer, enumerations, and subrange itself Lecture 7 – Data Types, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He,

Lecture 7 – Data Types, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, Type constructors: Defining New Types Type constructors as set operations: – Cartesian product – Union – Subset – Functions (Arrays) Some type constructors do not correspond to set operations (e.g., pointers) Some set operators don’t have corresponding type constructors (e.g., intersection)

Cartesian Product Ordered Pairs of elements from U and V U  V = {(u, v) | u  U and v  V} Operations: – projection p 1 : U  V → U; p 2 : U  V → V p 1 ((u,v))=u; p 2 ((u,v))=v Lecture 7 – Data Types, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He,

Examples struct in C struct IntCharReal { int i; char c; double r; } int  char  double record in Ada type IntCharReal is record i: integer; c: character; r: float; end record; Lecture 7 – Data Types, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He,

The same type? struct IntCharReal { int i; char c; double r; } struct IntCharReal { char c; int i; double r; } Lecture 7 – Data Types, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He,

The same type? struct IntCharReal { int i; char c; double r; } struct IntCharReal { int j; char ch; double d; } Lecture 7 – Data Types, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He,

Record/structure are not exactly Cartesian products Component selector: projection by component names struct IntCharReal x; x.i; Most languages consider component names to be part of the type. Thus the previous two types can be considered different, even though they represent the same Cartesian product. Lecture 7 – Data Types, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

ML: Pure Cartesian Product type IntCharReal = int * char * real; (2, #"a", 3.14) #3(2, #"a", 3.14) = 3.14 Lecture 7 – Data Types, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He,

Union U  V = { x | x  U or x  V} – data items with different types are stored in overlapping region, reduce memory allocation. – Only one type of value is valid at one time. – E.g., union IntOrReal { int i; double r; } Different from records? Lecture 7 – Data Types, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He,

Undiscriminated Union in C union IntOrReal { int i; double r; } union IntOrReal x; x.i = 1; printf(“%f\n”, x.r); Can be unsafe Lecture 7 – Data Types, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Create Discriminated Union in C++ struct IntOrReal { bool isInt; union { int i; double r; }; IntOrReal x; x.isInt = true; x.i = 1;... if (x.isInt) printf(“%d\n”, x.i); else printf(“%f\n”, x.r); Safe now or not? Lecture 7 – Data Types, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Discriminated Union in Ada Variant record (with tag or discriminator) type Disc is (IsInt, IsReal); type IntOrReal (which: Disc) is record case which is when IsInt => i: integer; when IsReal => r: float; end case; end record;... x: IntOrReal := (IsReal, 2.3); put (x.i); -- generates ERROR Safe: programmers won’t be able to create inconsistent data Lecture 7 – Data Types, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He,

Discriminated Union in Pascal Variant record Can be unsafe: – First, the tag is optional – Second, the tag can be set inconsistently. Lecture 7 – Data Types, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Discriminated Union in ML datatype IntOrReal = IsInt of int | IsReal of real; val x = IsReal(2.3); Lecture 7 – Data Types, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

How about Java? Is there record or union in java? Why? Lecture 7 – Data Types, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Lecture 7 – Data Types, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, “Union” in Java public abstract class A {…}; public class B extends A {…}; public class C extends A {…}; Abstract class A : union of B and C. Discriminated union: instanceof

Subset U = { v | v satisfies certain conditions and v  V} Ada subtype Example 1 – type Digit_Type is range 0..9; – subtype IntDigit_Type is integer range 0..9; Lecture 7 – Data Types, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He,

subtype in Ada Example 2 type Disc is (IsInt, IsReal); type IntOrReal (which: Disc) is record case which is when IsInt => i: integer; when IsReal => r: float; end case; end record; subtype IRInt is IntOrReal(IsInt); subtype IRReal is IntOrReal(IsReal); x: IRReal := 2.3; Lecture 7 – Data Types, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He,

Powerset P(U) = { U’ | U’  U } Example: Pascal set of – var S: set of ; – var S: ; What’s the difference? The order isn’t significant though unordered, values are distinct Lecture 8 – Data Types, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

set of in Pascal var S, T: set of ; S := [1, 2, 3, 5, 7]; T := [1.. 6]; Set operations can be performed on the variables. What are these? T := S*T; If T = [1, 3, 5] then …; x = 3; if x in S then …; if S<=T then …;  (*)  (+)  (-)  (=)  (<>)  (>)  (>=)  (<)  (<=)  (in) Lecture 8 – Data Types, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Arrays and Functions f: U → V Lecture 7 – Data Types, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, index type [0, …] (C/C++/Java) Ordinal type (Ada/Pascal) component type

C/C++ Allocated on stack, size statically specified. typedef int TenIntArray [10]; typedef int IntArray []; TenIntArray x; int y[5]; int z[]={1,2,3,4}; IntArray w={1,2}; IntArray w; //illegal int n =... //from user input int a[n]; //illegal Lecture 7 – Data Types, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He,

Java Allocated on heap, size dynamically specified; Size can be obtained by.length int n =... //from user input int [] x = new int [n]; System.out.println(x.length); Lecture 7 – Data Types, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He,

Ada size dynamically specified; Set of subscripts type IntToInt is array(integer range <>) of integer; get(n); //from user input x: IntToInt(1..n); for i in x’range loop put(x(i)); end loop; Lecture 7 – Data Types, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He,

Multi-dimensional arrays C/C++ int x[10][20]; Java int [][] x = new int [10][20]; Ada These two are different type Matric_Type is array(1..10, ) of integer; x(i,j); type Matric_Type is array(1..10) of array ( ) of integer; x(i)(j); Lecture 7 – Data Types, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He,

Storage row-major form x[1,-10],x[1,-9],…,x[1,10],x[2,-10],…,x[2,10],x[3,-10],… column-major form x[1,-10],x[2,-10],…,x[10,-10],x[1,-9],…,x[10,-9],x[1,-8],… C/C++ int array_max(int a[][20], int size) Java int array_max(int [][][]a) Lecture 7 – Data Types, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He,