PZ05B - Type equality Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 6.4 PZ05B.

Slides:



Advertisements
Similar presentations
PZ03D Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ03D - Program verification Programming Language Design.
Advertisements

PZ04B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ04B - Arrays and records Programming Language Design.
PZ09B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ09B - Parameter transmission Programming Language Design.
PZ09B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ09B - Parameter transmission Programming Language Design.
CSE 341, Winter Type Systems Terms to learn about types: –Type –Type system –Statically typed language –Dynamically typed language –Type error –Strongly.
PZ05A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ05A - Abstract data types Programming Language Design.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;
PZ10A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ10A - Heap storage Programming Language Design and.
PZ07B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ07B - Basic statements Programming Language Design.
PZ02A - Language translation
Encapsulation by Subprograms and Type Definitions
PZ04A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ04A - Scalar and composite data Programming Language.
PZ05B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ05B - Type equality Programming Language Design and.
PZ06C Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ06C - Polymorphism Programming Language Design and.
Polymorphism Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 7.3.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
PZ09A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ09A - Activation records Programming Language Design.
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
Code: BCA302 Data Structures with C Prof.(Dr.) Monalisa Banerjee By.
Abstract data types Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Chapter 5 Names, Bindings, Type Checking CSCE 343.
Language-Based Security: Overview of Types Deepak Garg Foundations of Security and Privacy October 27, 2009.
PZ11A Programming Language design and Implementation -4th Edition
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
Lecture 16: Introduction to Data Types
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Type Systems Terms to learn about types: Related concepts: Type
Inheritance Programming Language Design and Implementation
PZ06C - Polymorphism Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 7.3 PZ06C.
PZ05A - Abstract data types
Subprograms and Programmer Defined Data Type
PZ09A - Activation records
PZ04A - Scalar and composite data
Abstract data types Programming Language Design and Implementation
Inheritance Programming Language Design and Implementation
Type Systems Terms to learn: Type Type system
Compiler Construction
Polymorphism Programming Language Design and Implementation
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Arrays and records Programming Language Design and Implementation
Parameter transmission
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Arrays and records Programming Language Design and Implementation
Type Systems Terms to learn about types: Related concepts: Type
Polymorphism Programming Language Design and Implementation
PZ09B - Parameter transmission
Abstract data types Programming Language Design and Implementation
Arrays and records Programming Language Design and Implementation
Type equality Programming Language Design and Implementation
Type equality Programming Language Design and Implementation
Arrays and records Programming Language Design and Implementation
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Type equality Programming Language Design and Implementation
Parameter transmission
Polymorphism Programming Language Design and Implementation
PZ07A - Expressions Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section PZ07A.
Inheritance Programming Language Design and Implementation
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Type equality Programming Language Design and Implementation
Compiler Construction
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Inheritance Programming Language Design and Implementation
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Type equality Programming Language Design and Implementation
PZ07B - Basic statements Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Parameter transmission
Polymorphism Programming Language Design and Implementation
Lecture 7: Types (Revised based on the Tucker’s slides) 10/4/2019
Presentation transcript:

PZ05B - Type equality Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 6.4 PZ05B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000

Safe expressions Type checking: Want languages to only permit safe expressions: f: x  y. f is a safe expression if f(a) = b implies a  x. [That is, it is impossible to create an improper function call.] +: int x int  int 2+3 = int result - all arguments are safe But what about /:float x float  float? 3/0? PZ05B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000

Strong types A language is strongly typed if only safe expressions can be expressed in the language (i.e., results must be defined for that data type). E.g., char a, b, c; c = a+b;  Not safe since a+b can be > 255 But long a, b, c; c = a+b;  Safe since overflows will halt execution A[I]  Safe if dynamic checking of subscripts is performed A language is weakly typed if it accepts expressions that are not safe. PZ05B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000

Equality Two aspects of equality: When do 2 objects have the same value? When are two types the same? We will look at the first of these (equality of values) Define a new language: assign 2+1 to A Is A equal to 3? This is ambiguous as stated, although true for most languages studied so far. Need signature for assign and type for A If A is integer, then coersion forces 2+1 to be 3. If A is type code, then 2+1 is expression to be evaluated PZ05B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000

Equality of data objects For primitive data types equality is bit-level equality: int x; int y; float x; float y; x=y if they have same bit values But look at user types: type rational = record numerator: real; denomenator: real end; var A, B: rational; if A=B then ...  When is this true? True if: A.numerator=B.numerator and A.denomenator=B.denomenator or A.numerator/A.denomenator = B.numerator/B.denomenator There is no way for compiler to understand that rational means the usual mathematical concept of rational numbers. Equality must be defined, not coersed. PZ05B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000

Why a need for separate definition of equality Consider the following 4 data types and their representations: All can be defined as: struct { int size; int storage[100]; } PZ05B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000

Meaning of A=B What does A=B mean for each data type? stack: A.size = B.size For all i, A.storage[i]=B.storage[i] queue: A.size = B.size set: A.size = B.size All A.storage[i] differ For all i, some j, A.storage[i]=B.storage[j] (i.e., B.storage is a permutation of A.storage) bag: A.size = B.size B.storage is a permutation of A.storage So equality of user types is a complex issue that cannot be built into the operator by the language definition (effectively). PZ05B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000

Type equivalence Are X and Y the same type? type A = ...; type B = ...; A X; B Y; Name equivalence: X and Y are name equivalent if they are defined using the same name identifier: In Pascal: var X: array [1..10] of integer; Y: array [1..10] of integer; are not the same type, but: type arr = array [1..10] of integer; var U: arr; V: arr; are the same type. Structural equivalence: X and Y have the same storage representation: x: record a: real; b: integer end y: record m: real; n: integer end x and y have structural equivalence PZ05B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000

C type equivalence C uses name equivalence Explain the following: #define A struct {int M} typedef struct {int M} A; A X, Y; A X, Y; A Z; A Z; X=Y is legal X=Y is legal X=Z is an error X=Z is legal Although many C compilers will accept both definitions PZ05B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000