Chapter 7:: Data Types Programming Language Pragmatics

Slides:



Advertisements
Similar presentations
Names and Bindings.
Advertisements

CSCI 330: Programming Language Concepts Instructor: Pranava K. Jha Data Types-II: Composite Data Types.
Copyright © 2005 Elsevier Chapter 7:: Data Types Programming Language Pragmatics Michael L. Scott.
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
Chapter 5: Elementary Data Types Properties of types and objects –Data objects, variables and constants –Data types –Declarations –Type checking –Assignment.
Chapter Four Data Types Pratt 2 Data Objects A run-time grouping of one or more pieces of data in a virtual machine a container for data it can be –system.
CS 331, Principles of Programming Languages Chapter 4 Types: Data Representation.
Copyright © by Curt Hill Expressions and Assignment Statements Part 2.
Type Checking.
Compiler Construction
Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.
Names, Bindings, Type Checking, and Scopes
1 Names, Scopes and Bindings. 2 Names Kinds of names Kinds of names Variables, functions, classes, types, labels, blocks, operators, tasks, etc. Variables,
ISBN Chapter 6 Data Types: Structured types.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes Names Variables The Concept of Binding Type Checking Strong Typing Type Compatibility.
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?
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.
MT311 Java Application Programming and Programming Languages Li Tak Sing ( 李德成 )
1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
1 Adapted from slides for COMP 144 Programming Language Concepts Spring 2002 by Felix Hernandez-Campos The University of North Carolina at Chapel Hill.
1 CS 2104 Prog. Lang. Concepts Lecture 3 Dr. Abhik Roychoudhury School of Computing.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
Chapter 5 © 2002 by Addison Wesley Longman, Inc Names - We discuss all user-defined names here - Design issues for names: - Maximum length? - Are.
Type Checking and Data Type Implementation (Sections )
COMP4730/2003/lec5/H.Melikian Names, Bindings,Type Checking and Scopes (Chapter 5) - Design issues: - Maximum length? - Are connector characters allowed?
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
1 CS Programming Languages Class 07 September 14, 2000.
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.
March 12, ICE 1341 – Programming Languages (Lecture #6) In-Young Ko Programming Languages (ICE 1341) Lecture #6 Programming Languages (ICE 1341)
Introduction A variable can be characterized by a collection of properties, or attributes, the most important of which is type, a fundamental concept in.
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
Copyright © 2009 Elsevier Chapter 7:: Data Types Programming Language Pragmatics Michael L. Scott.
Arrays and Pointers Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 23.
Structure of Programming Languages Names, Bindings, Type Checking, and Scopes.
ISBN Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
Names and Binding In Text: Chapter 4.
1 Structure of Compilers Lexical Analyzer (scanner) Modified Source Program Parser Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
Names, Scope, and Bindings Programming Languages and Paradigms.
Names, Bindings, Type Checking and Scopes. Chapter 5 Topics Introduction Names Variables The Concept of Binding Type Checking Strong Typing Type Equivalence.
Copyright © 2009 Elsevier Chapter 7:: Data Types Programming Language Pragmatics Michael L. Scott.
Chapter 1: Preliminaries Lecture # 2. Chapter 1: Preliminaries Reasons for Studying Concepts of Programming Languages Programming Domains Language Evaluation.
Programming Language Theory 2011, 1 Chap. 7 :: Data Type Michael L. Scott.
CSI 3125, Data Types, page 1 Data types Outline Primitive data types Structured data types Strings Enumerated types Arrays Records Pointers Reading assignment.
Dr. M. Al-Mulhem Introduction 1 Chapter 6 Type Systems.
Chapter 5 Names, Bindings, Type Checking CSCE 343.
5.2 Names - We discuss all user-defined names here
Data Types In Text: Chapter 6.
Chapter 6 – Data Types CSCE 343.
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
Lecture 16: Introduction to Data Types
CS 326 Programming Languages, Concepts and Implementation
CS 3304 Comparative Languages
Chap. 7 :: Data Type Michael L. Scott.
Names and Binding In Text: Chapter 5.
Course Overview PART I: overview material PART II: inside a compiler
PRESENTED BY ADNAN M. UZAIR NOMAN
Types and Related Issues
Lecture 7: Types (Revised based on the Tucker’s slides) 10/4/2019
Presentation transcript:

Chapter 7:: Data Types Programming Language Pragmatics Michael L. Scott Adapted from Scott, 2006

Data Types We all have developed an intuitive notion of what types are; what's behind the intuition? collection of values from a "domain" (the denotational approach) internal structure or content of data, described down to the level of a small set of fundamental types (the structural approach) an interface providing a set of well defined operations (abstraction approach used by OO languages) Adapted from Scott, 2006

What are types good for? Data Types implicit context for many operations e.g., what operation should be used for (a + b)? checking of program semantics - making sure that certain meaningless operations do not occur type checking cannot prevent all meaningless operations can catch enough of them to be useful Adapted from Scott, 2006

STRONG TYPING has become a popular buzz-word Data Types STRONG TYPING has become a popular buzz-word like structured programming informally, it means that the language prevents you from applying an operation to data on which it is not appropriate This was Pascal's primary selling point in the 1980s STATIC TYPING means that the language is strongly typed, and that the compiler can do all the checking at compile time Adapted from Scott, 2006

Type Systems Examples C is weakly typed (and checks almost nothing at runtime) Common Lisp is strongly typed, but not statically typed Ada is statically typed Pascal is almost statically typed Except for variant records Java is strongly typed, with a non-trivial mix of things that can be checked statically and things that have to be checked dynamically Adapted from Scott, 2006

Common terms: Type Systems Scalar types - one-dimensional Discrete types (ordinal types) – countable integer boolean char real rational complex Adapted from Scott, 2006

Composite (nonscalar) types: Type Systems Composite (nonscalar) types: records – introduced by Cobol Called struct in C, C++ arrays indexed composite types strings sets – introduced by Pascal pointers – l-values reference to a base type lists files Adapted from Scott, 2006

A TYPE SYSTEM has rules for Type Checking A TYPE SYSTEM has rules for type equivalence (when are the types of two values the same?) type compatibility (when can a value of type A be used in a context that expects type B?) type inference (what is the type of an expression, given the types of the operands?) Adapted from Scott, 2006

Type compatibility / type equivalence Type Checking Type compatibility / type equivalence Compatibility is the more useful concept, because it tells you what you can DO The terms are often (incorrectly, but we do it too) used interchangeably. Adapted from Scott, 2006

Name equivalence is based on programmer's declarations Type Checking Two major approaches: structural equivalence and name equivalence Name equivalence is based on programmer's declarations Structural equivalence is based on the content of the types Name equivalence is more rigorous If the programmer makes the effort to name 2 types differently, the programmer probably wants them to be treated as different, even if their structures are identical. Adapted from Scott, 2006

Type Checking Structural equivalence depends on simple comparison of type descriptions substitute out all names expand all the way to built-in types Original types are equivalent if the expanded type descriptions are the same Adapted from Scott, 2006

Type Conversion Converting one type to another (casting) is required when: Types are structurally equivalent, but the language uses name equivalence. The conversion is only conceptual, not physical The types have different but intersecting sets of values (e.g., one is a subrange of the other) Runtime check tests the validity of the conversion Types are physically different, but values of one type correspond to values of the other e.g., all integers can be represented as reals Nonconverting cast Treat a variable of one type as another type, without changing the physical representation e.g., treating a char as an int in C Adapted from Scott, 2006

Type Checking Coercion Automatic, implicit type conversion When an expression of one type is used in a context where a different type is expected, one normally gets a type error But what about var a : integer; b, c : real; ... c := a + b; Adapted from Scott, 2006

Fortran has lots of coercion, all based on operand type Type Checking Coercion Many languages allow things like this, and COERCE an expression to be of the proper type Fortran has lots of coercion, all based on operand type C has lots of coercion, too, but with simpler rules: all floats in expressions become doubles short int and char become int in expressions Adapted from Scott, 2006

In effect, coercion rules are a relaxation of type checking Recent thought is that this is probably a bad idea Languages such as Modula-2 and Ada do not permit coercions C++, however, provides programmer-extensible coercion rules They're one of the hardest parts of the language to understand Adapted from Scott, 2006

Arrays are the most common and important composite data types Unlike records, which group related fields of disparate types, arrays are usually homogeneous Semantically, arrays can be thought of as a mapping from an index type to a component or element type Usually the only operations permitted are selection of an element and assignment, however Fortran 90 offers many array operations supporting matrix algebra Ada and Fortran 90 allow arrays to be compared for equality Adapted from Scott, 2006

Dimensions, Bounds, and Allocation Arrays Dimensions, Bounds, and Allocation global lifetime, static shape — If the shape of an array is known at compile time, and if the array can exist throughout the execution of the program, then the compiler can allocate space for the array in static global memory local lifetime, static shape — If the shape of the array is known at compile time, but the array should not exist throughout the execution of the program, then space can be allocated in the subroutine’s stack frame at run time. arbitrary lifetime, shape bound at elaboration time— In Java and C# an array is a reference to an object, whose space is allocated on the heap Adapted from Scott, 2006

Contiguous elements (see next slide) Arrays Contiguous elements (see next slide) column major - only in Fortran (and only due to a peculiarity of the IBM 704 computer, on which Fortran was first implemented) row major used by everybody else Makes a multidimensional array simply an array of subarrays Adapted from Scott, 2006

Arrays Adapted from Scott, 2006

Two layout strategies for arrays (see next slide): Contiguous elements Row pointers Row pointers – an array is an array of pointers to arrays an option in C allows rows to be put anywhere - nice for big arrays on machines with segmentation problems nice for matrices whose rows are of different lengths e.g. an array of strings avoids multiplication for offset calculation requires extra space for the pointers Adapted from Scott, 2006

Arrays Adapted from Scott, 2006

Strings are really just arrays of characters They are often special-cased, to give them flexibility (like dynamic sizing) that is not available for arrays in general It's easier to provide these things for strings than for arrays in general because strings are one-dimensional Adapted from Scott, 2006

Equality testing Shallow comparison Deep comparison Do the expressions refer to the same object? Address comparison l-value comparison Deep comparison Do the expressions refer to objects which are in some sense equal/equivalent? Value(s) comparison r-value comparison Adapted from Scott, 2006