Type Equivalence Rules Ada –Strict name equivalence except for almost everything Unique array constructors give rise to unique types Subtypes can create.

Slides:



Advertisements
Similar presentations
Types in Ruby and other languages….  Classes and objects (vs prototypes)  Instance variables/encapsulation  Object creation  Object equality/comparison.
Advertisements

Overview of Previous Lesson(s) Over View  Front end analyzes a source program and creates an intermediate representation from which the back end generates.
Chapter 6 - Data Types Programming Languages:
CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
Chapter 7:: Data Types Programming Language Pragmatics
CSCI 330: Programming Language Concepts Instructor: Pranava K. Jha
Chapter 5: Elementary Data Types Properties of types and objects –Data objects, variables and constants –Data types –Declarations –Type checking –Assignment.
COEN Expressions and Assignment
Lecture # 20 Type Systems. 2 A type system defines a set of types and rules to assign types to programming language constructs Informal type system rules,
Type Checking.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
CS 330 Programming Languages 10 / 24 / 2006 Instructor: Michael Eckmann.
Type Checking  Legality checks  Operator determination  Overload resolution.
C++ Training Datascope Lawrence D’Antonio Lecture 1 Quiz 1.
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.
ISBN Lecture 07 Expressions and Assignment Statements.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
Names and Binding In procedural programming, you write instructions the manipulate the “state” of the process where the “state” is the collection of variables.
Names Variables Type Checking Strong Typing Type Compatibility 1.
5-1 Chapter 5: Names, Bindings, Type Checking, and Scopes Variables The Concept of Binding Type Checking Strong Typing Type Compatibility Scope and Lifetime.
CSE 425: Data Types II Survey of Common Types I Records –E.g., structs in C++ –If elements are named, a record is projected into its fields (e.g., via.
March 12, ICE 1341 – Programming Languages (Lecture #6) In-Young Ko Programming Languages (ICE 1341) Lecture #6 Programming Languages (ICE 1341)
Programming Language C++ Xulong Peng CSC415 Programming Languages.
Symbols and Type-Checking CPSC 388 Ellen Walker Hiram College.
1 Type Checking Type checking ensures that the operands and the operator are of compatible types Generalized to include subprograms and assignments Compatible.
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.
Arithmetic Expressions
ISBN Chapter 7 Expressions and Assignment Statements.
Programming Languages Third Edition Chapter 8 Data Types.
12/9/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
1 CS Programming Languages Class 09 September 21, 2000.
1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
Structure of Programming Languages Names, Bindings, Type Checking, and Scopes.
Static Checking and Type Systems Chapter 6. 2 Static versus Dynamic Checking Static checking: the compiler enforces programming language’s static semantics.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
March 31, ICE 1341 – Programming Languages (Lecture #11) In-Young Ko Programming Languages (ICE 1341) Lecture #11 Programming Languages (ICE 1341)
1 Structure of Compilers Lexical Analyzer (scanner) Modified Source Program Parser Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator.
Data Types (3) 1 Programming Languages – Principles and Practice by Kenneth C Louden.
CS 330 Programming Languages 10 / 23 / 2007 Instructor: Michael Eckmann.
Expressions Version Topics Arithmetic expressions Conversions Operator precedence String class 2.
CS 342: C++ Overloading Copyright © 2004 Dept. of Computer Science and Engineering, Washington University Overview of C++ Overloading Overloading occurs.
Chapter 1: Preliminaries Lecture # 2. Chapter 1: Preliminaries Reasons for Studying Concepts of Programming Languages Programming Domains Language Evaluation.
CSE 332: C++ Overloading Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both.
Dr. M. Al-Mulhem Introduction 1 Chapter 6 Type Systems.
7-1/27 Chapter 7 Expressions and Assignment Statements Introduction Arithmetic Expressions Overloaded Operators Type Conversions Relational and Boolean.
Expressions and Assignment Statements
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Expressions and Assignment Statements
Polymorphism in Methods
7.2 Arithmetic Expressions
Type Checking and Type Inference
Programming Languages and Compilers (CS 421)
Chapter 7: Expressions and Assignment Statements
Semantic Analysis Type Checking
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
Expressions and Assignment Statements
Java Primer 1: Types, Classes and Operators
Chapter 7: Expressions and Assignment Statements
Expressions and Assignment Statements
Operator Overloading CSCE 121 J. Michael Moore
Arithmetic Expressions
Expressions and Assignment Statements
CS 3304 Comparative Languages
PRESENTED BY ADNAN M. UZAIR NOMAN
Operator Overloading CSCE 121 Based on Slides created by Carlos Soto.
Presentation transcript:

Type Equivalence Rules Ada –Strict name equivalence except for almost everything Unique array constructors give rise to unique types Subtypes can create a new type or just an (equivalent) alias type Age1 is new integer; -- new and different type type Age2 is integer; -- type equivalent alias for integer

C –Name equivalence for structs and unions Struct and union constructors create new nonequivalent types. –Structural equivalence for everything else Any other type constructor (array, pointer) creates a type with the same name as any other same structure type.

Java –Class and interface declarations create new type names. Name equivalence is used for these types with the exception that a subclass type object may be assigned to a superclass variable. –Structural equivalence for scalars –Arrays must be structurally equivalent (ignoring size) with name equivalent component types

Type Compatibility General term type compatibility applies both to assignment and comparison with relational operators. Equivalent types are always compatible. C and Java: – all numeric types are compatible Ada –subranges of the same type are compatible

Assignment Compatibility Assignment compatibility is used to determine when a value of one type may be assigned or passed as a parameter to a variable of another type. We say that the assigned value type is compatible with the variable or parameter to which it will be assigned. C –All numeric types are assignment compatible Java –Any numeric type t 1 is assignment compatible with any other numeric type t 2 if conversion of t 1 to t 2 does not cause loss of information.

Implict Types If the type of a value or name is determined without a declaration, it is said to be implicit. –FORTRAN variables: starting letter determines type a-h,o-z: Real, i-n: integer –Old C: all undeclared variables and return types are int –4652: type is implicit. 4652L: type is explicit.

Type Conversion Coercion (implicit type conversion) is used to convert values to compatible types for use as arguments to operations. int x = 5; x = x / 3; x = (4.6 + x) / 3; Widening vs. Narrowing Explicit conversion (casts in C and Java) –static_cast (old-style cast, but checked for consistency and can’t cast away const) –reinterpret_cast (old-style cast, but can’t cast away const) –const_cast (casts away const) –dynamic_cast (casts a ptr/ref to a subclass or superclass ptr/ref) Ada attribute (tick) functions character’pos(‘a’) character’val(41) Union types

Polymorphic Type Checking Program code is said to be polymorphic (meaning many-formed or many-shaped) if it can be evaluated correctly when the variables it references are assigned different types. Usually the term is only applied to functions or subprograms that can be defined without giving explicit parameter type declarations. Conventional type checking starts with an AST for an expression attributed with the types of the leaf nodes. Rules are applied to determine what assignment of specific operators and functions to the interior nodes can be employed to evaluate the expression without violating type compatibility. If more than one such assignment is found, then the type rules of the language will either –assign a scalar quality value to each match and choose the best matching collection of assignments, or –declare that the expression has ambiguous overloadings and fail.

Typical Type Checking Example a i []i + int i; int a[10]; Requires declarations int →int int []:(int→int ) Χ int →int +:int Χ int → int Requires rules

Polymorphic Type Checking Requires us to use More Interesting Rules These are rules that ML uses to infer the type correctness of polymorphic code: 1.All occurrences of the same identifier in a given scope must have the same type. 2.In an if/then/else expression, the condition must be of type bool, and the then and else clauses must be of the same type. 3.A programmer-defined function has type 'a -> 'b where 'a is the type of the function’s parameter and 'b is the type of its result. (Functions have tuple arguments.) 4.When a function is applied, the type of the argument passed to the function must be the same as the parameter type in the function’s definition and the type of the application is the same as the type of the result in the function’s definition.

Polymorphic Type Checking (Same Example) a i []i + Requires declarations or functions [] : (int → σ) Χ int → σ Assign type variables {α,β,γ,δ,...} to leaf nodes (int → σ) Χ int → σ αβ β(by rule 1) int +:δ Χ δ → δ δ Χ δ → δ int int → σint → int int

Explicit Polymorphism Explicit parametric polymorphism allows one to constrain elements of a data structure or piece of code to have a type given by a parameter. C++ template types give this capability: template struct StackNode { T data; StackNode *next; }; template struct Stack { StackNode *theStack; }; ML declares this in a different way datatype ‘a Stack = EmptyStack | Stack of 'a * ('a Stack);

Example Problem 6.28