Data Types (cont.) Subset. subtype in Ada. Powerset. set of in Pascal. implementations. CSE 3302 Programming Languages 10/1/2007

Similar documents
Data Types (cont.) Administrative Issues. Academic Dishonesty. How do we detect plagiarism? Strongly Typed Languages. Type System

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

CPSC 3740 Programming Languages University of Lethbridge. Data Types

Organization of Programming Languages CS320/520N. Lecture 06. Razvan C. Bunescu School of Electrical Engineering and Computer Science

Chapter 6. Structured Data Types. Topics. Structured Data Types. Vectors and Arrays. Vectors. Vectors: subscripts

Introduction Primitive Data Types Character String Types User-Defined Ordinal Types Array Types. Record Types. Pointer and Reference Types

Data Types. Outline. In Text: Chapter 6. What is a type? Primitives Strings Ordinals Arrays Records Sets Pointers 5-1. Chapter 6: Data Types 2

Organization of Programming Languages CS3200 / 5200N. Lecture 06

Data Types In Text: Ch C apter 6 1

CSE 452: Programming Languages. Where are we? Types: Intuitive Perspective. Data Types. High-level Programming Languages.

CSE 307: Principles of Programming Languages

Semantics (cont.) Symbol Table. Static Scope. Static Scope. Static Scope. CSE 3302 Programming Languages. Static vs. Dynamic Scope

TYPES, VALUES AND DECLARATIONS

Types. What is a type?

CS 230 Programming Languages

Attributes, Bindings, and Semantic Functions Declarations, Blocks, Scope, and the Symbol Table Name Resolution and Overloading Allocation, Lifetimes,

G Programming Languages Spring 2010 Lecture 6. Robert Grimm, New York University

CS321 Languages and Compiler Design I Winter 2012 Lecture 13

Types II. Hwansoo Han

Chapter 6. Data Types ISBN

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

Storage. Outline. Variables and updating. Copy vs. Ref semantics Lifetime. Dangling References Garbage collection

Chapter 6. Data Types ISBN

COSC252: Programming Languages: Basic Semantics: Data Types. Jeremy Bolton, PhD Asst Teaching Professor

Programming Languages

Semantic Analysis. How to Ensure Type-Safety. What Are Types? Static vs. Dynamic Typing. Type Checking. Last time: CS412/CS413

CSE 3302 Programming Languages Lecture 5: Control

Storage. Outline. Variables and Updating. Composite Variables. Storables Lifetime : Programming Languages. Course slides - Storage

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Topic 9: Type Checking

Topic 9: Type Checking

Chapter 6 part 1. Data Types. (updated based on 11th edition) ISBN

Chapter 5. Names, Bindings, and Scopes

Types and Type Inference

Lecture Overview. [Scott, chapter 7] [Sebesta, chapter 6]

CS321 Languages and Compiler Design I. Fall 2013 Week 8: Types Andrew Tolmach Portland State University

Data Types. Data Types. Introduction. Data Types. Data Types. Data Types. Introduction

CS 430 Spring Mike Lam, Professor. Data Types and Type Checking

Heap, Variables, References, and Garbage. CS152. Chris Pollett. Oct. 13, 2008.

SE352b: Roadmap. SE352b Software Engineering Design Tools. W3: Programming Paradigms

Programming Languages

Types and Type Inference

Principles of Programming Languages

Chapter 5 Names, Binding, Type Checking and Scopes

Static Semantics. Winter /3/ Hal Perkins & UW CSE I-1

22c:111 Programming Language Concepts. Fall Types I

Data Types. CSE 307 Principles of Programming Languages Stony Brook University

Structuring the Data. Structuring the Data

Note 3. Types. Yunheung Paek. Associate Professor Software Optimizations and Restructuring Lab. Seoul National University

Programming Languages Third Edition. Chapter 10 Control II Procedures and Environments

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

Lecture 12: Data Types (and Some Leftover ML)

Class Information ANNOUCEMENTS

Types-2. Type Equivalence

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

Lecture 9: More Lambda Calculus / Types

- M ::= v (M M) λv. M - Impure versions add constants, but not necessary! - Turing-complete. - true = λ u. λ v. u. - false = λ u. λ v.

Programming Languages, Summary CSC419; Odelia Schwartz

Software II: Principles of Programming Languages

Programming Methodology

Programming Languages Third Edition. Chapter 7 Basic Semantics

For each of the following variables named x, specify whether they are static, stack-dynamic, or heapdynamic:

Chapter 6. Data Types. *modified by Stephanie Schwartz ISBN

G Programming Languages - Fall 2012

CS558 Programming Languages

22c:111 Programming Language Concepts. Fall Functions

Chapter 6. Data Types ISBN

CSCI-GA Scripting Languages

COMP 2355 Introduction to Systems Programming


MIDTERM EXAMINATION - CS130 - Spring 2005

CS558 Programming Languages

Chapter 8 :: Composite Types

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 17: Types and Type-Checking 25 Feb 08

Final-Term Papers Solved MCQS with Reference

CS558 Programming Languages Winter 2018 Lecture 4a. Andrew Tolmach Portland State University

CS558 Programming Languages

Subprograms. Bilkent University. CS315 Programming Languages Pinar Duygulu

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

Chapter 8 ( ) Control Abstraction. Subprograms Issues related to subprograms How is control transferred to & from the subprogram?

A data type defines a collection of data objects and a set of predefined operations on those objects

CS558 Programming Languages

Fundamentals of Programming Languages

INF 212/CS 253 Type Systems. Instructors: Harry Xu Crista Lopes

Compiler Construction

Dynamic Allocation in C

Informatica 3 Syntax and Semantics

Question No: 1 ( Marks: 1 ) - Please choose one One difference LISP and PROLOG is. AI Puzzle Game All f the given

Low-Level C Programming. Memory map Pointers Arrays Structures

CSc 520. Principles of Programming Languages 25: Types Introduction

Names, Bindings, Scopes

In Java we have the keyword null, which is the value of an uninitialized reference type

6. Names, Scopes, and Bindings

Concepts Introduced in Chapter 7

Weeks 6&7: Procedures and Parameter Passing

UEE1302 (1102) F10 Introduction to Computers and Programming (I)

Binding and Variables

NOTE: Answer ANY FOUR of the following 6 sections:

CS558 Programming Languages

Transcription:

CSE 3302 Programming Languages Data Types (cont.) Chengkai Li Fall 2007 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; 1 2 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; P(U) = { U U U } Example: Pascal set of <ordinal type> Powerset var S: set of 1.. 10; var S: 1.. 10; What s the difference? The order isn t significant though unordered, values are distinct 3 4 set of in Pascal implementations var S, T: set of 1.. 10; 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 ; type students = (Alice, Bob, Charlie, Dave, Eve,...); var CSE3302, CSE3310, whotookboth: set of students; CSE3302 := [ Alice, Bob, Dave ]; CSE3310 := [ Alice, Charlie, Dave, Eve ]; whotookboth := CSE3302 * CSE3310; Alice Bob Charlie Dave Eve CSE3302 1 1 0 1 0... CSE3310 1 0 1 1 1... Whotookboth 1 0 0 1 0... (*) (+) ( ) = (=) (<>) (>) (>=) (<) (<=) (in) 5 How about other operations? (membership, intersection, union, count, subsumption, ) 6 1

bit operations for set Implementation can be very efficient bit and (&), bit or ( ), bit negation (~), bit xor Hardware support Alternatives? Having Set type is convenient and efficient. If not present, can we use other types as alternatives? Encoding schemes 7 8 f: U V Array and Functions C/C++ Allocated on stack, size statically specified. index type component type typedef int TenIntArray [10]; typedef int IntArray []; [0, ] (C/C++/Java) Ordinal type (Ada/Pascal) 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 9 10 Java Ada 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); 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; 11 12 2

Four Categories of Arrays Based on subscript binding and location binding time Static range of subscripts (static), location (static) e.g. FORTRAN 77 Advantage: execution efficiency Fixed stack dynamic range of subscripts (static), location (dynamic). e.g. Pascal, C, C++ Four Categories of Arrays Stack dynamic range of subscripts (dynamic), location (dynamic), immutable once bound e.g. Ada Advantage: flexibility Heap dynamic range of subscript (dynamic), location (dynamic), can be changed e.g. FORTRAN 90 Advantage: maximum flexibility Advantage: space efficiency 13 14 Multi dimensional arrays Storage 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, -10..10) of integer; x(i,j); type Matric_Type is array(1..10) of array (-10..10) of integer; x(i)(j); 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][10], int size) Java int array_max(int [][][]a) 15 16 FORTRAN 90 INTEGER, ALLOCATABLE, ARRAY (:,:) :: MAT (Declares MAT to be a dynamic 2-dim array) ALLOCATE (MAT (10, NUMBER_OF_COLS)) (Allocates MAT to have 10 rows and NUMBER_OF_COLS columns) DEALLOCATE MAT (Deallocates MAT s storage) Array Operations Access individual elements The whole array Pascal: A := B FORTRAN: A+B Ada: Array concatenation A&B FORTRAN 90: matrix multiplication, transpose 17 18 3

Functions C: int incfn(int x) { return x+1; } typedef int (*IntFunc)(int); // why *? IntFunc inc = incfn; ML: type IntFunc = int -> int; val inc:intfunc = fn x => x+1; Vector, List, Sequence Functional languages: Vectors: like arrays, more flexibility, especially dynamic resizability. Lists: like vectors, can only be accessed by counting down from the first element. Sequences: typically (potentially) infinite. 19 20 Pointer A pointer type is a type in which the range of values consists of memory addresses and a special value, nil (or null) Advantages: Addressing flexibility (address arithmetic, explicit dereferencing and address of, domain type not fixed (void *)) Dynamic storage management Recursive data structures E.g., linked list struct CharListNode { char data; struct CharListNode* next; }; Typedef struct CharListNode* CharList; Alias (with side effect) int *a, *b; a=(int *) malloc(sizeof(int)); *a=2; b=(int *) malloc(sizeof(int)); *b=3; b=a; *b=4; printf( %d\n, *a); 21 22 Dangling pointers (dangerous) int *a, *b; a= (int *) malloc(sizeof(int)); b=a; free(a); *b=2; printf( %d\n, *b); Garbages (waste of memory) memory leakage int *a; a = (int *) malloc(sizeof(int)); *a=2; a = (int *) malloc(sizeof(int)); 23 24 4

Type System Type Constructors: Build new data types upon simple data types Type Checking: The translator checks if data types are used correctly. Type Inference: Infer the type of an expression, whose data type is not given explicitly. e.g., x/y Type Equivalence: Compare two types, decide if they are the same. e.g., x/y and z Type Compatibility: Can we use a value of type A in a place that expects type B? Nontrivial with user defined types and anonymous types Strongly Typed Languages Strongly typed: (Ada, ML, Haskell, Java, Pascal) Most data type errors detected at translation time A few checked during execution and runtime error reported (e.g., subscript out of array bounds). Pros: No data corrupting errors can occur during execution. (I.e., no unsafe program can cause data errors.) Efficiency (in translation and execution.) Security/reliability Cons: May reject safe programs (i.e., legal programs is a subset of safe programs.) Burden on programs, may often need to provide explicit type information. 25 26 Weakly typed and untyped languages Weakly typed: C/C++ e.g., interoperability of integers, pointers, arrays. Untyped (dynamically typed) languages: scheme, smalltalk, perl Doesn t necessarily result in data errors. All type checking performed at execution time. May produce runtime errors too frequently. Security vs. flexibility Strongly typed : No data errors caused by unsafe programs. Maximum restrictiveness, static type checking, illegal safe programs, large amount of type information supplied by programmers. Untyped: Runtime errors, no data corruptions. Legal unsafe programs. reduce the amount of type information the programmer must supply. 27 28 Security vs. flexibility Strongly typed : A type system tries to maximize both flexibility and security, where flexibility means: reduce the number of safe illegal programs & reduce the amount of type information the programmer must supply. Flexibility, no explicit typing or static type checking vs. Maximum restrictiveness, static type checking 29 5