PZ05B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 1 PZ05B - Type equality Programming Language Design and.

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.
Arrays and records Programming Language Design and Implementation
PZ10B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ10B - Garbage collection 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.
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
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
PZ06A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ06A - Inheritance Programming Language Design and Implementation.
PZ04A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ04A - Scalar and composite data Programming Language.
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.
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.
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.
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 Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
PZ03BX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ03BX - Recursive descent parsing Programming Language.
PZ09A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ09A - Activation records Programming Language Design.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
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
Basic statements Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Abstract data types Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
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
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
Abstract data types Programming Language Design and Implementation
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
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.
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
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
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
PZ05B - Type equality Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 6.4 PZ05B.
Parameter transmission
Polymorphism Programming Language Design and Implementation
Lecture 7: Types (Revised based on the Tucker’s slides) 10/4/2019
Presentation transcript:

PZ05B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 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, 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, 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, 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 SNOBOL4 and Prolog operate this way. We will look at Prolog later.

PZ05B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 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, 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, 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 For all i, A.storage[i]=B.storage[i] 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, 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, C type equivalence C uses name equivalence Explain the following: #define A struct {int M} typedef struct {int M} A; A X, Y; A Z; X=Y is legal X=Z is an error X=Z is legal Although many C compilers will accept both definitions