Chapter 5 Types. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.5-2 Topics in this Chapter Values vs. Variables Types vs. Representations.

Slides:



Advertisements
Similar presentations
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 17 Templates.
Advertisements

Programming Languages and Paradigms
ISBN Chapter 7 Expressions and Assignment Statements.
Chapter 7:: Data Types Programming Language Pragmatics
Chapter 5 Types. 5-2 Topics in this Chapter Values vs. Variables Types vs. Representations Type Definition Operators Type Generators SQL Facilities.
Type Checking.
Compiler Construction
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 18 - C++ Operator Overloading Outline 18.1Introduction.
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.
Operator Overloading in C++ Systems Programming. Systems Programming: Operator Overloading 22   Fundamentals of Operator Overloading   Restrictions.
ISBN Chapter 7 Expressions and Assignment Statements.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes Names Variables The Concept of Binding Type Checking Strong Typing Type Compatibility.
ISBN Chapter 9 Subprograms. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Introduction Two fundamental abstraction facilities.
Encapsulation by Subprograms and Type Definitions
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.
Abstract Data Types and Encapsulation Concepts
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11 Object and Object- Relational Databases.
Operator Overloading in C++
Chapter 6 Relations. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-2 Topics in this Chapter Tuples Relation Types Relation Values Relation.
Chapter 6 Relations. Topics in this Chapter Tuples Relation Types Relation Values Relation Variables SQL Facilities.
Chapter 3 An Introduction to Relational Databases.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
1-1 University of Hail College of Computer Science and Engineering Department of computer Science and Software Engineering Course: ICS313: Fundamentals.
Type Equivalence Rules Ada –Strict name equivalence except for almost everything Unique array constructors give rise to unique types Subtypes can create.
Chapter 9 Integrity. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.9-2 Topics in this Chapter Predicates and Propositions Internal vs.
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.
Chapter 5 Types. Topics in this Chapter Values vs. Variables Types vs. Representations Type Definition Operators Type Generators SQL Facilities.
Ad Hoc Constraints Objectives of the Lecture : To consider Ad Hoc Constraints in principle; To consider Ad Hoc Constraints in SQL; To consider other aspects.
Chapter 8 Relational Calculus. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.8-2 Topics in this Chapter Tuple Calculus Calculus vs. Algebra.
The Data in a Relation To consider atomic data in relations; To consider data types in a relation; To consider missing data & NULLs in relations. Objectives.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Chapter 6 Programming Languages © 2007 Pearson Addison-Wesley. All rights reserved.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
Chapter 7 Relational Algebra. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.7-2 Topics in this Chapter Closure Revisited The Original Algebra:
1 Relational Algebra and Calculas Chapter 4, Part A.
Arithmetic Expressions
Expressions and Assignment Statements
ISBN Chapter 7 Expressions and Assignment Statements.
 2008 Pearson Education, Inc. All rights reserved Operator Overloading.
ISBN Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types.
Object-Oriented Programming Chapter Chapter
Chapter 4 An Introduction to SQL. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.4-2 Topics in this Chapter SQL: History and Overview The.
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
March 31, ICE 1341 – Programming Languages (Lecture #11) In-Young Ko Programming Languages (ICE 1341) Lecture #11 Programming Languages (ICE 1341)
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
Chapter 10 Views. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.10-2 Topics in this Chapter What are Views For? View Retrievals View Updates.
Relations Prof. Yin-Fu Huang CSIE, NYUST Chapter 6.
Types Prof. Yin-Fu Huang CSIE, NYUST Chapter 5. Advanced Database SystemYin-Fu Huang Relation, tuple, cardinality, attribute, degree, domain, primary.
CS 330 Programming Languages 10 / 23 / 2007 Instructor: Michael Eckmann.
Integrity Prof. Yin-Fu Huang CSIE, NYUST Chapter 9.
ISBN Chapter 12 Support for Object-Oriented Programming.
Chapter 7 Expressions and Assignment Statements. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 7 Topics Introduction Arithmetic Expressions.
Chapter 3 An Introduction to Relational Databases.
ISBN Chapter 7 Expressions and Assignments Statements.
7-1/27 Chapter 7 Expressions and Assignment Statements Introduction Arithmetic Expressions Overloaded Operators Type Conversions Relational and Boolean.
Chapter 7: Expressions and Assignment Statements
Type Checking, and Scopes
CS 326 Programming Languages, Concepts and Implementation
Chapter 7: Expressions and Assignment Statements
College of Computer Science and Engineering
Chapter 7 Expressions and Assignment Statements.
Compiler Construction
PRESENTED BY ADNAN M. UZAIR NOMAN
Compiler Construction
Presentation transcript:

Chapter 5 Types

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.5-2 Topics in this Chapter Values vs. Variables Types vs. Representations Type Definition Operators Type Generators SQL Facilities

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.5-3 Types A type is a set of values F/k/a (formerly known as) a domain Types can be system-defined or user-defined All types have associated operators Formally, this means that the operator can take the given type as a parameter For example integers can be passed to an addition operator but not a sub-string operator

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.5-4 Values vs. Variables - Values A value is an individual constant It has no location in time or space A value is represented by encoding, which generates its appearance, which is spatial and temporal Appearances can occur in different times and spaces: they are manifold A value cannot be updated, for then it would be some other value: a value is immutable

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.5-5 Values vs. Variables - Variables A variable is a holder for an appearance of a value It has location in time or space A variable can be updated, that is, it can hold another value A variable maintains its identity during the update: it is still the same variable

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.5-6 Values vs. Variables Values can be simple or complex –Simple: integer, char –Complex: an XML document, a relation A value per se can have multiple appearances An appearance can have multiple encodings When we refer to a “value,” we often mean “an appearance of an encoding of a value”

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.5-7 Values and Variables are Typed Every value has its immutable type Every variable has its immutable type, so that all its values will be of that type Every attribute of every relvar has its immutable type Operators have a type when operating, but this can be polymorphic in different contexts –e.g. = can operate on integers or characters, but not both at the same time

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.5-8 Types and their Representations A type per se is idealized, conceptual, a model A representation (appearance) of the type is its implementation Sometimes a type is called an ADT – Abstract Data Type, but this is inherently redundant This logical and physical distinction is an aspect of data independence

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.5-9 Types and their Representations - Scalar vs. Non-Scalar A scalar type is atomic and encapsulated –Integer, char, bool A non-scalar type is complex and user-visible –Name, address, employee, radiology image Values, variables, attributes, operators, parameters, expressions: all can be scalar or not, mutatis mutandis

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.5-10 Types and their Representations - Possible Representations Let T be a scalar type The physical representation is hidden from the user Values of type T must have at least one possible representation, which is not hidden from the user The possible representation of a scalar type may have components, and if so, then at least one set of these must be visible to the user

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.5-11 Types and their Representations - Possible Representations Each type has at least one POSSREP visible to the user in its declaration Each POSSREP includes two operators –Selector to specify a value for each representation –Ex.: QTY (100), QTY(N1 – N2) –THE_ to access each representation –Ex.: THE_QTY (Q), THE_QTY (Q1 – Q2), QTY cannot equal 100, because quantity is not an integer, if it has been declared as a type QTY can equal QTY(100)

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.5-12 Type Definition TYPE WEIGHT POSSREP { D DECIMAL (5,1) CONSTRAINT D > 0.0 AND D < }; TYPE WEIGHT POSSREP LBS { L DECIMAL (5,1) CONSTRAINT L > 0.0 AND L < }; POSSREP GMS { G DECIMAL (7,1) CONSTRAINT G > 0.0 AND G < AND MOD (G, 45.4) = 0.0 };

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.5-13 Operators OPERATOR ABS (Z RATIONAL) RETURNS RATIONAL; RETURN (CASE WHEN Z > 0.0 THEN +Z WHEN Z < 0.0 THEN –Z END CASE); END OPERATOR;

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.5-14 Operators OPERATOR REFLECT (P POINT) UPDATES P; BEGIN; THE_X (P) := - THE_X (P) ; THE_Y (P) := - THE_Y (P) ; RETURN; END; END OPERATOR; DROP OPERATOR REFLECT;

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.5-15 Type Conversions QTY(100) converts an integer to a quantity THE_QTY (Q1) converts a quantity to an integer P# = ‘P2’ violates the rule that both sides of an assignment must be of the same type Compiler uses the P# selector implicitly to convert ‘P2’ from Char to P# a/k/a “Coercion” Coercion is not permitted

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.5-16 Type Conversions Coercion is not permitted Explicit casting is permitted CAST_AS_CHAR (530.00) This is called strong typing: i.e., every value has a type, and the compiler checks to verify that operands are of the correct type for an operation –Can’t add weight to quantity, but can multiply them

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.5-17 Type and Domain All types are known to the system The types in a database are a closed set Assignments and comparisons, ditto In a database system, a domain is a type, and thereby is an object class Hence we can speak about relations and objects simultaneously

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.5-18 Type Generators a/k/a parameterized types, or templates ARRAY is a classic invocation of a type generator ARRAY can take in all sorts of types, and can return all sorts of other types VAR SALES ARRAY INTEGER [12]; ARRAY operators such as assignment, equality, THE_ work equally well with any valid type, i.e., a type known to the system

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.5-19 SQL Facilities Built-in operators such as CHAR, NUMERIC, BLOB, BOOL… Mostly strong typing, but SQL will coerce FLOAT to NUMERIC, for example Supports two kinds of user-defined types: distinct types and structured types SQL does not support POSSREP – only one representation per type SQL does not support CONSTRAINTs

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.5-20 SQL Facilities – Distinct Types CREATE TYPE WEIGHT AS DECIMAL (5,1) FINAL; For distinct types, SQL supports Selector and THE_ POSSREP not supported, so WEIGHT is always a DECIMAL Distinct types are strong, so you cannot use a comparison operator between the type and its underlying representation

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.5-21 SQL Facilities – Structured Types CREATE TYPE POINT AS (X FLOAT,Y FLOAT) NOT FINAL; Uses operators in place of Select and THE_ Observe and mutate methods Structured types can be ALTERed or DROPped Tuples and relations are structured types

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.5-22 SQL Facilities –Type Generators SQL includes three type generators: REF, ROW, ARRAY REFERENCE generates a reference ROW generates a set of fields ARRAY generates an array