Type equality Programming Language Design and Implementation

Slides:



Advertisements
Similar presentations
PZ04B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ04B - Arrays and records Programming Language Design.
Advertisements

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.
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.
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.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Static Checking and Type Systems Chapter 6. 2 Static versus Dynamic Checking Static checking: the compiler enforces programming language’s static semantics.
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
Functional Programming Lecture 1 - Introduction Professor Muffy Calder.
Abstract data types Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Data Types In Text: Chapter 6.
Type Checking and Type Inference
Types CSCE 314 Spring 2016.
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.
Lecture 16: Introduction to Data Types
CS 326 Programming Languages, Concepts and Implementation
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
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
PZ04A - Scalar and composite data
Abstract data types Programming Language Design and Implementation
Type Systems Terms to learn: Type Type system
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
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
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
Basic statements Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Basic statements Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
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
Basic statements Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Lecture 7: Types (Revised based on the Tucker’s slides) 10/4/2019
CSE 3302 Programming Languages
Presentation transcript:

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

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? Type equality

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. Type equality

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. Type equality

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. Type equality

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];} Type equality

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). Type equality

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 Type equality

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 Type equality

Type Definitions with Parameters Ada: type Section(MaxSize: Integer) is record Room: integer; Instructor: integer; ClassSize: integer range 0 .. Maxsize; ClassRoll: array(1 .. Maxsize) of Student_ID; end record; X: Section(100); Y: Section(25); ML: signature OrderedItem = sig type elem var lessthan elem*elem-> bool end; Type equality