CS 363 – Chapter 7 Chapter 7 – type systems Types that are supported

Slides:



Advertisements
Similar presentations
1 C++ Syntax and Semantics The Development Process.
Advertisements

Chapter 7:: Data Types Programming Language Pragmatics
1 Introduction to Data Types (Section 7.1) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael.
CSCI 330: Programming Language Concepts Instructor: Pranava K. Jha
COEN Expressions and Assignment
Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
ISBN Chapter 6 Data Types: Structured types.
G. Levine Chapter 6 Chapter 6 Encapsulation –Why do we want encapsulation? Programmer friendliness- programmer need not know about these details –Easier.
Mark Hennessy CS351 Dept Computer Science NUI Maynooth 1 Types CS351 – Programming Paradigms.
Copyright © 1995 by Addison-Wesley Publishing Co. 1 Names - Design issues: - Maximum length? - Are connector characters allowed? - Are names case sensitive?
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
Elementary Data Types Scalar Data Types Numerical Data Types Other
Primitive Data Types: Numbers Strings Ordinal Types Pointers
Type Checking  Legality checks  Operator determination  Overload resolution.
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.
Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
1 Adapted from slides for COMP 144 Programming Language Concepts Spring 2002 by Felix Hernandez-Campos The University of North Carolina at Chapel Hill.
ISBN 0-321— Chapter 6 sections 1-4, 9 Primitive Data Types Numbers Strings Ordinal Types Pointers.
1 CS Programming Languages Class 07 September 14, 2000.
Names Variables Type Checking Strong Typing Type Compatibility 1.
March 12, ICE 1341 – Programming Languages (Lecture #6) In-Young Ko Programming Languages (ICE 1341) Lecture #6 Programming Languages (ICE 1341)
CPS120: Introduction to Computer Science
CISC105 – General Computer Science Class 9 – 07/03/2006.
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.
ISBN Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types.
Types in programming languages1 What are types, and why do we need them?
Data Types Declarations Expressions Data storage C++ Basics.
ISBN Chapter 6 Data Types. Copyright © 2006 Addison-Wesley. All rights reserved. 6-2 Chapter 6 Topics Introduction Primitive Data Types.
CSI 3125, Preliminaries, page 1 Data Type, Variables.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Variables reference, coding, visibility. Rules for making names  permitted character set  maximum length, significant length  case sensitivity  special.
CS 330 Programming Languages 11 / 01 / 2007 Instructor: Michael Eckmann.
1 Structure of Compilers Lexical Analyzer (scanner) Modified Source Program Parser Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator.
CPS120: Introduction to Computer Science Variables and Constants.
Names, Scope, and Bindings Programming Languages and Paradigms.
 Data Type is a basic classification which identifies different types of data.  Data Types helps in: › Determining the possible values of a variable.
Data Types (1) 1 Programming Languages – Principles and Practice by Kenneth C Louden.
Chapter 1: Preliminaries Lecture # 2. Chapter 1: Preliminaries Reasons for Studying Concepts of Programming Languages Programming Domains Language Evaluation.
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
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.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
5.2 Names - We discuss all user-defined names here
CSC 533: Programming Languages Spring 2016
Data Types In Text: Chapter 6.
Chapter 6 – Data Types CSCE 343.
CSC 533: Programming Languages Spring 2015
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
Chapter 6: Data Types Lectures # 10.
Lecture 16: Introduction to Data Types
CS 326 Programming Languages, Concepts and Implementation
Instructor : Ahmed Alalawi Slides from Chung –Ta King
C Language VIVA Questions with Answers
Type Conversion, Constants, and the String Object
Data Types.
Introduction to Abstract Data Types
College of Computer Science and Engineering
CS 3304 Comparative Languages
Type Systems CSE 340 – Principles of Programming Languages Fall 2016
Introduction C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell.
Chapter 7 Expressions and Assignment Statements.
Java’s Central Casting
Types and Related Issues
Variables and Constants
Lecture 7: Types (Revised based on the Tucker’s slides) 10/4/2019
Presentation transcript:

CS 363 – Chapter 7 Chapter 7 – type systems Types that are supported scalar composite user-defined Some issues with types equivalence, compatibility

Why types? What do these Java expressions mean: a + b Math.sqrt(fileMenu) Some languages are typeless How does it work? Better or worse to have types?

Strong typing Languages (compilers) vary in how strong their typing rules are. Strong typing means: whenever you use a expression, its type must be appropriate for the context x : integer; y : real; x := y; (* should we allow this? *)

Thinking about types 3 ways to view types Denotational – a set of values Making sure that integer division stays closed Constructive – a type is represented as a built-in type or a composite of built-in types Abstract – focus on the operations we want to perform (In OO design, we do a lot of constructive and abstract thinking.)

Numeric types Integral types Real number types Integers Boolean signed, unsigned, BCD Most languages don’t specify size  Boolean Character Real number types Floating point: universal standard Fixed point: no exponent; rare

Enum type Our own type with names for each value Ex. days of the week, months of year type weekday = (mon, tue, wed, thur, fri) ; Internally represented as integer Pascal: pred() and succ() functions compatible with integers ? Pascal: no C: yes, can use a cast

Subranges If you have a variable, and you know it will only take on a small subset of values type test_score = 0..100; type year = 1900..2027; type coef is digits 7 range -1e10..+1e10; Usually not compatible with superset type. Advantages?

Base vs. composite types Base types Integers Characters Boolean Real numbers Enum subranges Composite types record union array pointer list set file Some of these composite (derived) types are “collections” that we’ve already seen

Type stuff Restrictions and freedom Compatible types

Limits on types Some languages are more limited in type choices. Algol: int, real, boolean, with arrays. How do you program without records/classes? What if array is the only way to aggregate?

Freedom Orthogonality = being able to do what you want regardless of type. For example: declare variables in any order give any name I want to variable can type a constant value of any type function can return value of any type can print any type can store any type in an array Languages usually have some limitations regarding orthogonality. Ex. “++”

Compatible types According to name According to structure When is it legal to assign a := b or pass as matching parameter? According to name Both variables must be declared to be literally the same type easy for compiler to check According to structure Both types have the same “structure” Ex. arrays of 100 integers

Structure examples type fahrenheit = float; type celsius = float; type array = [0..9] of int; type vector= [0..9] of int; // anonymous types: a : [0..9] of celsius; b : [1..10] of celsius; c : [1..10] of float; According to structure equivalence, these types should match.

Which is better? Usually, languages go with name equivalence, and allow loopholes: coercion casting identically declared types the same: “loose name equivalence” subranges and integers If programmer defined a distinct type, there’s probably a reason! Structural equivalence would require recursive algorithm for record/classes 

Expression type When an expr combines variables of diff types, type of overall expr is usually: Promotion according to a hierarchy Ex. double, float, unsigned long, long, unsigned, int, short The base type, if different subtypes Ex. (0..20) + (1900..2027)  int