CSE 3302 Programming Languages

Slides:



Advertisements
Similar presentations
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Advertisements

Names and Bindings.
Chapter 6 - Data Types Programming Languages:
INF 212 ANALYSIS OF PROG. LANGS Type Systems Instructors: Crista Lopes Copyright © Instructors.
Various languages….  Could affect performance  Could affect reliability  Could affect language choice.
Kernighan/Ritchie: Kelley/Pohl:
CSE 341, Winter Type Systems Terms to learn about types: –Type –Type system –Statically typed language –Dynamically typed language –Type error –Strongly.
Chapter 5: Elementary Data Types Properties of types and objects –Data objects, variables and constants –Data types –Declarations –Type checking –Assignment.
Type Checking.
Compiler Construction
CS 355 – Programming Languages
1 Names, Scopes and Bindings. 2 Names Kinds of names Kinds of names Variables, functions, classes, types, labels, blocks, operators, tasks, etc. Variables,
CS 330 Programming Languages 10 / 24 / 2006 Instructor: Michael Eckmann.
Names Variables Type Checking Strong Typing Type Compatibility 1.
March 12, ICE 1341 – Programming Languages (Lecture #6) In-Young Ko Programming Languages (ICE 1341) Lecture #6 Programming Languages (ICE 1341)
CSE 3302 Programming Languages Chengkai Li, Weimin He Spring 2008 Data Types Lecture 7 – Data Types, Spring CSE3302 Programming Languages, UT-Arlington.
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.
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.
1 CS Programming Languages Class 09 September 21, 2000.
ISBN Chapter 6 Data Types Pointer Types Reference Types Memory Management.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Structure of Programming Languages Names, Bindings, Type Checking, and Scopes.
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
Data Types (3) 1 Programming Languages – Principles and Practice by Kenneth C Louden.
CSE 3302 Programming Languages
CS 330 Programming Languages 10 / 23 / 2007 Instructor: Michael Eckmann.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Names, Scope, and Bindings Programming Languages and Paradigms.
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
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 { --
Data Types Chapter 6: Data Types Lectures # 13. Topics Chapter 6: Data Types 2 Introduction Primitive Data Types Character String Types Array Types Associative.
Chapter 5 Names, Bindings, Type Checking CSCE 343.
Language-Based Security: Overview of Types Deepak Garg Foundations of Security and Privacy October 27, 2009.
Object Lifetime and Pointers
Dynamic Storage Allocation
Data Types In Text: Chapter 6.
Type Checking and Type Inference
Chapter 6 – Data Types CSCE 343.
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.
Type Checking, and Scopes
CSE 3302 Programming Languages
Lecture 4: Type Systems.
Lecture 16: Introduction to Data Types
Records Design Issues: 1. What is the form of references?
CSE 3302 Programming Languages
Concepts of programming languages
Type Systems Terms to learn about types: Related concepts: Type
Semantic Analysis Chapter 6.
CSE 3302 Programming Languages
CSE 3302 Programming Languages
CSE 3302 Programming Languages
Pointers.
Announcements Quiz 7 HW 9 is due on Friday Rainbow grades
CSE 3302 Programming Languages
Semantic Analysis Chapter 6.
CSE 3302 Programming Languages
Names and Binding In Text: Chapter 5.
Type Systems Terms to learn: Type Type system
Compiler Construction
Java Programming Language
CSE 3302 Programming Languages
Type Systems Terms to learn about types: Related concepts: Type
Compiler Construction
CSE 3302 Programming Languages
Presentation transcript:

CSE 3302 Programming Languages Data Types(cont.) Chengkai Li, Weimin He Spring 2008 Lecture 8 – Data Types, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

Function Type in C typedef int (*IntFunction)(int); int square(int x) {return x*x;} IntFunction f = square; int evaluate(IntFunction g, int value){ return g(value); } … printf(“%d\n”,evaluate(f,3)); Lecture 8 – Data Types, Spring 2008 2 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

Function Type in ML type IntFunction = int -> int; fun square(x: int) = x * x; val f = square; Fun evaluate(g:IntFunction, value:int) = g value; … evaluate(f,3); Lecture 8 – Data Types, Spring 2008 3 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

Vector, List Functional languages: Vectors: like arrays, more flexibility, especially dynamic resizability. Lists: like vectors, can only be accessed by counting down from the first element. Lecture 8 – Data Types, Spring 2008 4 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

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; Lecture 8 – Data Types, Spring 2008 5 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

Problems with Pointers 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); Lecture 8 – Data Types, Spring 2008 6 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

Problems with Pointers Dangling pointers (dangerous) int *a, *b; a = (int *) malloc(sizeof(int)); *a = 1; b = a; free(a); printf(“%d\n”, *b); Lecture 8 – Data Types, Spring 2008 7 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

Problems with Pointers Garbages (waste of memory) memory leakage int *a; a = (int *) malloc(sizeof(int)); *a=2; Lecture 8 – Data Types, Spring 2008 8 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

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 Lecture 8 – Data Types, Spring 2008 9 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

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 programmers, may often need to provide explicit type information. Lecture 8 – Data Types, Spring 2008 10 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

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. Lecture 8 – Data Types, Spring 2008 11 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

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. Lecture 8 – Data Types, Spring 2008 12 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

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 Lecture 8 – Data Types, Fall 2007 13 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

Safe vs. Legal Programs Legal programs Safe programs Lecture 8 – Data Types, Spring 2008 14 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

Type Equivalence How to decide if two types are the same? Structural Equivalence Types are sets of values Two types are equivalent if they contain the same values. Name Equivalence Lecture 8 – Data Types, Spring 2008 15 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

Structural Equivalence struct RecA { char x; int y; } struct RecB { struct RecC { char u; int v; struct RecD { Char X Int Int X Char Lecture 8 – Data Types, Spring 2008 16 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

But are they equivalent in these languages? In C: struct RecA { char x; int y; }; struct RecB { struct RecA a; struct RecB b; b=a; ( Error: incompatible types in assignment ) Lecture 8 – Data Types, Spring 2008 17 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

But are they equivalent in these languages? In C: struct RecA { char x; int y; }; struct RecB { struct RecA a; struct RecB* b; b=&a; ( Warning: incompatible types in assignment ) Lecture 8 – Data Types, Spring 2008 18 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

But are they equivalent in these languages? In C: struct RecA { char x; int y; }; struct RecB { struct RecA a; struct RecB* b; b=(struct RecB*)&a; ( OK, but does not mean they are equivalent ) Lecture 8 – Data Types, Spring 2008 19 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

But are they equivalent in these languages? In Java: class A { char x; int y; }; class B { A a = new B(); ? Lecture 8 – Data Types, Spring 2008 20 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

Equivalence Algorithm If structural equivalence is applied: struct RecA { char x; int y; }; struct RecB { char u; int v; struct RecA a; struct RecB b; b=a; Lecture 8 – Data Types, Spring 2008 21 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

Replacing the names by declarations typedef struct { char x; int y; } RecB; RecB b; struct { } c; Lecture 8 – Data Types, Spring 2008 22 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

Replacing the names by declarations typedef struct { char x; char y } SubRecA; typedef struct { char x; char y } SubRecB; struct RecA { int ID; SubRecA content; }; struct RecB { int ID; SubRecB content; Lecture 8 – Data Types, Spring 2008 23 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

Replacing the names by declarations? typedef struct CharListNode* CharList; typedef struct CharListNode2* CharList2; struct CharListNode { char data; CharList next; }; struct CharListNode2 { char data; CharList2 next; Lecture 8 – Data Types, Spring 2008 24 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

Cannot do that for recursive types typedef struct CharListNode* CharList; typedef struct CharListNode2* CharList2; struct CharListNode { char data; struct CharListNode* next; }; struct CharListNode2 { char data; struct CharListNode2* next; There are techniques for dealing with this Lecture 8 – Data Types, Spring 2008 25 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

Structural Equivalence Can be complicated when there are names, anonymous types, and recursive types Simpler, and more strict rules: name equivalence Lecture 8 – Data Types, Spring 2008 26 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

Name Equivalence struct RecA { char x; int y; }; typedef struct RecA RecB; struct RecA *a; RecB *b; struct RecA c; struct { char x; int y; } d; struct { char x; int y; } e,f; a=&c; a=&d; b=&d; a=b; e=d; ( ok ) (Warning: incompatible pointer type) (Warning: incompatible pointer type) ( ok. Typedef creates alias for existing name ) ( error: incompatible types in assignment ) Lecture 8 – Data Types, Spring 2008 27 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

Type Equivalence in C Name Equivalence: struct, union Structural Equivalence: everything else typedef doesn’t create a new type Lecture 8 – Data Types, Spring 2008 28 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

Example struct A { char x; int y; }; struct B { char x; int y; }; struct { char x; int y;}; typedef struct A C; typedef C* P; typedef struct A * R; typedef int S[10]; typedef int T[5]; typedef int Age; typedef int (*F)(int); typedef Age (*G)(Age); struct A and C struct A and B; B and C struct A and struct { char x; int y;}; P and R S and T int and Age F and G Lecture 8 – Data Types, Spring 2008 29 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

Type Equivalence in Java No typedef: so less complicated class/interface: new type (name equivalence, class/interface names) arrays: structural equivalence Lecture 8 – Data Types, Spring 2008 30 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

Type Checking Type Checking: Determine whether the program is correct in terms of data types. Type Inference: Types of expressions Type Equivalence: Are two types the same? Type Compatibility: Relaxing exact type equivalence under certain circumstances Lecture 8 – Data Types, Spring 2008 31 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

Example long y; float x; double c; x = y/2+c; y long,2 is int, so promoted to long, y/2 long. c is dobule, y/2 is long, so promoted to double, y/2+c is double. x Is float, y/2+c is double, what happens? C? Java? Lecture 8 – Data Types, Spring 2008 32 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

Example: C struct RecA {int i; double r;}; int p( struct {int i;double r;} x) { ... } int q( struct RecA x) struct RecA a; int b; b = p(a); b = q(a); Lecture 8 – Data Types, Spring 2008 33 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

Type Conversion Use code to designate conversion? No: automatic/implicit conversion Yes: manual/explicit conversion Data representation changed? No, just the type. Yes Lecture 8 – Data Types, Spring 2008 34 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

Example: Java Implicit conversion: Explicit conversion: Representation change (type promotion, e.g., int to double) No representation change (upcasting) Explicit conversion: Representation change (double x = 1.5; int y = (int)x) No representation change (downcasting) Lecture 8 – Data Types, Spring 2008 35 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

Casting in Java OK (upcating) OK (downcasting) compilation error class A {public int x;} class SubA extends A { public int y;} A a1 = new A( ); A a2 = new A( ); SubA suba = new SubA( ); a1 = suba; OK (upcating) suba = (SubA) a1; OK (downcasting) suba = a2; compilation error suba = (SubA) a2; compiles OK, runtime error a1.y; compilation error if (a1 instanceof SubA) { ((SubA) a1).y; } OK Lecture 8 – Data Types, Spring 2008 36 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008