1 Adapted from slides for COMP 144 Programming Language Concepts Spring 2002 by Felix Hernandez-Campos The University of North Carolina at Chapel Hill.

Slides:



Advertisements
Similar presentations
Programming Languages and Paradigms
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.
Elementary Data Types Prof. Alamdeep Singh. Scalar Data Types Scalar data types represent a single object, i.e. only one value can be derived. In general,
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.
Type Checking.
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
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.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes Names Variables The Concept of Binding Type Checking Strong Typing Type Compatibility.
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.
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
PZ04A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ04A - Scalar and composite data Programming Language.
Primitive Data Types: Numbers Strings Ordinal Types Pointers
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.
CS 355 – Programming Languages
MT311 Java Application Programming and Programming Languages Li Tak Sing ( 李德成 )
ISBN 0-321— Chapter 6 Data Types. Corrected and improved by Assoc. Prof. Zeki Bayram, EMU, North Cyprus. Original Copyright © 2007 Addison-Wesley.
ISBN 0-321— Chapter 6 sections 1-4, 9 Primitive Data Types Numbers Strings Ordinal Types Pointers.
Type Checking and Data Type Implementation (Sections )
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
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.
PRIMITIVE DATA TYPES -Integer -Floating Point -Decimal -Boolean -Character STRINGS -Character Array -Class -String Length -Static -Limited Dynamic -Dynamic.
1 COSC3306: Programming Paradigms Lecture 2: Data Types Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.
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.
1 Records Record aggregate of data elements –Possibly heterogeneous –Elements/slots are identified by names –Elements in same fixed order in all records.
Programming Languages and Paradigms Imperative Programming.
ISBN Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types.
ISBN Chapter 6 Data Types. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Chapter 6 Topics Introduction Primitive Data Types Character.
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.
CS 330 Programming Languages 10 / 30 / 2007 Instructor: Michael Eckmann.
Chapter 4 of Programming Languages by Ravi Sethi.
ISBN Chapter 6 Data Types. Copyright © 2006 Addison-Wesley. All rights reserved. 6-2 Chapter 6 Topics Introduction Primitive Data Types.
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
CS 330 Programming Languages 11 / 01 / 2007 Instructor: Michael Eckmann.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
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.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
ISBN Chapter 6 Data Types. Copyright © 2006 Addison-Wesley. All rights reserved.2 Primitive Data Types Almost all programming languages.
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.
Structure of Programming Language Data Types. 2 A data type defines a collection of data objects and a set of predefined operations on those objects An.
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.
Data Types Chapter 6: Data Types Lectures # 13. Topics Chapter 6: Data Types 2 Introduction Primitive Data Types Character String Types Array Types Associative.
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
Complex Data Types One very important measure of the “goodness” of a PL is the capability of its data types to model the problem space variables Design.
Introduction to Abstract Data Types
CS 3304 Comparative Languages
CS 363 – Chapter 7 Chapter 7 – type systems Types that are supported
Chapter 6 Data Types.
Lecture 7: Types (Revised based on the Tucker’s slides) 10/4/2019
Presentation transcript:

1 Adapted from slides for COMP 144 Programming Language Concepts Spring 2002 by Felix Hernandez-Campos The University of North Carolina at Chapel Hill

2 Data Types Computers manipulate sequences of bitsComputers manipulate sequences of bits But most programs manipulate more general dataBut most programs manipulate more general data –Numbers –String –Lists –…–…–…–… Programming languages provide data types that raise the level of abstraction from bits to dataProgramming languages provide data types that raise the level of abstraction from bits to data –But computer hardware only knows about bits!

3 Data Types The Purpose of Types Types provide implicit contextTypes provide implicit context –Compilers can infer more information, so programmers write less code –E.g. the expression a+b in Java may be adding two integer, two floats or two strings depending on the context Types provides a set of semantically valid operationsTypes provides a set of semantically valid operations –Compilers can detect semantic mistakes –E.g. Python’s lists support append() and pop(), but complex numbers do not.

4 Type Systems High-level languages have type systemsHigh-level languages have type systems –All objects and expressions have a type –E.g. void (*)(const int) is the type of a C++ function A type system consists ofA type system consists of –A mechanism for defining types and associating them with certain language constructs –A set of rules for type checking »Type equivalence »Type compatibility »Type inference

5 Type Systems Type Checking Type checking is the process of ensuring that a program obeys the language’s type compatibility rulesType checking is the process of ensuring that a program obeys the language’s type compatibility rules Strongly typed languages always detect types errorsStrongly typed languages always detect types errors –Weakly typed languages do not –All expressions and objects must have a type –All operations must be applied in appropriate type contexts Statically typed languages are strongly typed language in which all type checking occurs at compile typeStatically typed languages are strongly typed language in which all type checking occurs at compile type –Dynamically typed languages: some checking at run-time

6 What is a type? Three points of view:Three points of view: –Denotational: a set of values –Constructive: a type is built-in type or a composite type »Composite types are created using type constructors »E.g. In Java, boolean is a built-in type, while boolean[] is a composite type –Abstraction-based: a type is an interface that defines a set of consistent operations These points of view complement each otherThese points of view complement each other

7 Classification of Types Built-in Types Built-in/Primitive/Elementary typesBuilt-in/Primitive/Elementary types –Mimic hardware units –E.g. boolean, character, integer, real (float) Their implementation varies across languagesTheir implementation varies across languages Characters are traditionally one-byte quantities using the ASCII character setCharacters are traditionally one-byte quantities using the ASCII character set –Early computers had a different byte sizes »Byte = 8 bits standardized by Fred Brooks et al. thanks to the IBM System/360 –Other character sets have also been used

8 Built-in Types Unicode Characters Newer languages have built-in characters that support the Unicode character setNewer languages have built-in characters that support the Unicode character set – html htmlhttp:// html –Unicode is implemented using two-byte quantities –E.g. Java » –E.g. Python »u”¡Hola!” » http://

9 Built-in Types Numeric Types Most languages support integers and floatsMost languages support integers and floats –The range of value is implementation dependent Some languages support other numeric typesSome languages support other numeric types –Complex numbers (e.g. Fortran, Python) –Rational numbers (e.g. Scheme, Common Lisp) –Signed and unsigned integers (e.g. C, Modula-2) –Fixed point numbers (e.g. Ada) Some languages distinguish numeric types depending on their precisionSome languages distinguish numeric types depending on their precision –Single vs. double precision numbers »C’s int (4 bytes) and long (8 bytes)

10 Classification of Types Enumerations Enumeration improve program readability and error checkingEnumeration improve program readability and error checking They were first introduced in PascalThey were first introduced in Pascal –E.g. type weekday = (sun, mon, tue, wed, thu, fri, sat); –They define an order, so they can be used in enumeration- controlled loops –The same feature is available in C »enum weekday {sun, mon, tue, wed, thu, fri, sat}; –Other languages use constants to define enumeration –Pascal’s approach is more complete: integers and enumerations are not compatible

11 Classification of Types Subranges Subranges improve program readability and error checkingSubranges improve program readability and error checking They were first introduced in PascalThey were first introduced in Pascal –E.g. type test_score = ; workday = mon..fri; workday = mon..fri; –They define an order, so they can be used in enumeration- controlled loops

12 Classification of Types Composite Types Composite/Constructed types are created applying a constructor to one or more simpler typesComposite/Constructed types are created applying a constructor to one or more simpler types ExamplesExamples –Records –Variant Records –Arrays –Sets –Pointers –Lists –Files

13 Classification of Types Orthogonality Orthogonality is an important property in the design of type systemsOrthogonality is an important property in the design of type systems More orthogonal languages support have more flexible composite typesMore orthogonal languages support have more flexible composite types Scheme is a good exampleScheme is a good example

14 Records

15 Records Memory Layout Basic layout in 32-bit machinesBasic layout in 32-bit machines –There may be holes in the allocation of memory

16 Records Memory Layout Packed layoutPacked layout Rearranging record fieldRearranging record field

17 Records Implications of Memory Layout More memory efficient layouts have several drawbacksMore memory efficient layouts have several drawbacks –Assignments require multiple instructions »Masking and shifting operations –Access to record elements requires multiple instructions »Masking and shifting operations Holes complicate record equalityHoles complicate record equality –Requires field by field comparison or default values in holes –Some languages forbid record comparison »E.g. Pascal, C

18 Variant Records A variant record provides two or more alternative fields or collections of fields, but only one bit is valid at any given timeA variant record provides two or more alternative fields or collections of fields, but only one bit is valid at any given time –They are called unions in C/C++

19 Variant Records Memory Layout Some languages, like C, do not check whether variant records are properly accessedSome languages, like C, do not check whether variant records are properly accessed Other languages keep track of the value in an additional field, increasing safetyOther languages keep track of the value in an additional field, increasing safety

20 Arrays Memory Layout Arrays are usually stored in contiguous locationsArrays are usually stored in contiguous locations Two common orders for 2D arraysTwo common orders for 2D arrays

21 Arrays Memory Layout Contiguous allocation vs. row pointersContiguous allocation vs. row pointers

22 Arrays Address Calculations Code generation for a 3D arrayCode generation for a 3D array –A: array [L 1..U 1 ] of array [L 2..U 2 ] of array [L 3..U 3 ] of elem_type

23 Arrays Address Calculations Row-MajorOrderCompile-TimeConstantOptimization

24 Pointer Data Type Contains an address or NULLContains an address or NULL Provides indirect addressing and dynamic storage managementProvides indirect addressing and dynamic storage management Operations – assignment and dereferencingOperations – assignment and dereferencing Problems – dangling pointers, memory leaksProblems – dangling pointers, memory leaks Language question – does it provide solutions to these problems? (ex. C++)Language question – does it provide solutions to these problems? (ex. C++)

25 Reference Data Type Like a pointer but usually more limitedLike a pointer but usually more limited C++ reference type is constant – can’t be reassignedC++ reference type is constant – can’t be reassigned Java reference type is like a pointer except safer – why?Java reference type is like a pointer except safer – why?