22c:111 Programming Language Concepts. Fall Types I

Similar documents
CSCI312 Principles of Programming Languages!

HANDLING NONLOCAL REFERENCES

Names and Types. standard hue names. Dr. Philip Cannata 1

22c:111 Programming Language Concepts. Fall Functions

22c:111 Programming Language Concepts. Fall Syntax III

TYPES, VALUES AND DECLARATIONS

Types. What is a type?

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

G Programming Languages Spring 2010 Lecture 6. Robert Grimm, New York University

Lecture Overview. [Scott, chapter 7] [Sebesta, chapter 6]

COS 140: Foundations of Computer Science

Fundamentals of Programming Languages. Data Types Lecture 07 sl. dr. ing. Ciprian-Bogdan Chirila

Chapter 6. Structured Data Types. Topics. Structured Data Types. Vectors and Arrays. Vectors. Vectors: subscripts

CPSC 3740 Programming Languages University of Lethbridge. Data Types

Programming Languages

Programming Languages, Summary CSC419; Odelia Schwartz

Lecture 12: Data Types (and Some Leftover ML)

Introduction Primitive Data Types Character String Types User-Defined Ordinal Types Array Types. Record Types. Pointer and Reference Types

Chapter 7. Expressions and Assignment Statements ISBN

Chapter 7:: Data Types. Mid-Term Test. Mid-Term Test (cont.) Administrative Notes

Data Types. Outline. In Text: Chapter 6. What is a type? Primitives Strings Ordinals Arrays Records Sets Pointers 5-1. Chapter 6: Data Types 2

Organization of Programming Languages (CSE452) Why are there so many programming languages? What makes a language successful?

CS558 Programming Languages

COS 140: Foundations of Computer Science

Chapter 5 Names, Binding, Type Checking and Scopes

Note 3. Types. Yunheung Paek. Associate Professor Software Optimizations and Restructuring Lab. Seoul National University

Chapter 7. Expressions and Assignment Statements (updated edition 11) ISBN

COMP 181. Agenda. Midterm topics. Today: type checking. Purpose of types. Type errors. Type checking

Expressions & Assignment Statements

Data Types (cont.) Administrative Issues. Academic Dishonesty. How do we detect plagiarism? Strongly Typed Languages. Type System

Question No: 1 ( Marks: 1 ) - Please choose one One difference LISP and PROLOG is. AI Puzzle Game All f the given

Programming Languages

Data Types In Text: Ch C apter 6 1

Data Types. (with Examples In Haskell) COMP 524: Programming Languages Srinivas Krishnan March 22, 2011

Programming Languages 2nd edition Tucker and Noonan"

Names, Scopes, and Bindings II. Hwansoo Han

Fundamentals of Programming Languages

CS349/SE382 A1 C Programming Tutorial

Chapter 7. Expressions and Assignment Statements

Example: Haskell algebraic data types (1)

Organization of Programming Languages CS320/520N. Lecture 06. Razvan C. Bunescu School of Electrical Engineering and Computer Science

Expressions and Assignment Statements

Topic 9: Type Checking

Topic 9: Type Checking

CS 430 Spring Mike Lam, Professor. Data Types and Type Checking

Motivation for typed languages

Principles of Programming Languages

SE352b: Roadmap. SE352b Software Engineering Design Tools. W3: Programming Paradigms

CSCI-GA Scripting Languages

Types and Type Inference

Chapter 5. Names, Bindings, and Scopes

Structures and Pointers

Data Types. Data Types. Introduction. Data Types. Data Types. Data Types. Introduction

Pierce Ch. 3, 8, 11, 15. Type Systems

CSE 431S Type Checking. Washington University Spring 2013

Software II: Principles of Programming Languages. Why Expressions?

Preview from Notesale.co.uk Page 6 of 52

G Programming Languages - Fall 2012

CS321 Languages and Compiler Design I Winter 2012 Lecture 13

Intermediate Code Generation

Types and Type Inference

Chapter 5 Names, Bindings, Type Checking, and Scopes

9/7/17. Outline. Name, Scope and Binding. Names. Introduction. Names (continued) Names (continued) In Text: Chapter 5

Chapter 7 Expressions and Assignment statements

CSC2/454 Programming Languages Design and Implementation Records and Arrays

Storage. Outline. Variables and Updating. Composite Variables. Storables Lifetime : Programming Languages. Course slides - Storage

Introduction. Primitive Data Types: Integer. Primitive Data Types. ICOM 4036 Programming Languages

For each of the following variables named x, specify whether they are static, stack-dynamic, or heapdynamic:

Darshan Institute of Engineering & Technology for Diploma Studies Unit 5

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

St. MARTIN S ENGINEERING COLLEGE Dhulapally, Secunderabad

Expressions and Assignment

Programming Languages Third Edition. Chapter 7 Basic Semantics

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

Continuations provide a novel way to suspend and reexecute

Organization of Programming Languages CS3200 / 5200N. Lecture 06

Chapter 7. Expressions and Assignment Statements ISBN

Semantic Analysis. How to Ensure Type-Safety. What Are Types? Static vs. Dynamic Typing. Type Checking. Last time: CS412/CS413

Programming Languages Third Edition. Chapter 9 Control I Expressions and Statements

Organization of Programming Languages CS 3200/5200N. Lecture 09

Chapter 7. Expressions and Assignment Statements

LECTURE 17. Expressions and Assignment

Informatica 3 Syntax and Semantics

Types II. Hwansoo Han

CA341 - Comparative Programming Languages

INF 212/CS 253 Type Systems. Instructors: Harry Xu Crista Lopes

UNIT II Structuring the Data, Computations and Program. Kainjan Sanghavi

Intermediate Code Generation Part II

CS383 PROGRAMMING LANGUAGES. Kenny Q. Zhu Dept. of Computer Science Shanghai Jiao Tong University

Types. Chapter Six Modern Programming Languages, 2nd ed. 1

Short Notes of CS201

Review of the C Programming Language

INF 212 ANALYSIS OF PROG. LANGS Type Systems. Instructors: Crista Lopes Copyright Instructors.

Types, Type Inference and Unification

Programmiersprachen (Programming Languages)

CS201 - Introduction to Programming Glossary By

String Instructions In C Programming Language Using

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

Compilers CS S-05 Semantic Analysis

Primitive Data Types: Intro

Transcription:

22c:111 Programming Language Concepts Fall 2008 Types I Copyright 2007-08, The McGraw-Hill Company and Cesare Tinelli. These notes were originally developed by Allen Tucker, Robert Noonan and modified by Cesare Tinelli. They are copyrighted materials and may not be used in other course settings outside of the University of Iowa in their current form or modified form without the express written permission of one of the copyright holders. During this course, students are prohibited from selling notes to or being paid for taking notes by any person or commercial firm without the express written permission of one of the copyright holders.

5.1 Type Errors 5.2 Static and Dynamic Typing 5.3 Basic Types 5.4 NonBasic Types 5.5 Recursive Data Types 5.6 Functions as Types 5.7 Type Equivalence 5.8 Subtypes 5.9 Polymorphism and Generics 5.10 Programmer-Defined Types

Def: A type is a collection of values and operations on those values. Examples: The Integer type has values..., -2, -1, 0, 1, 2,... and operations +, -, *, /, <,... The Boolean type has values true and false and operations,,.

Computer types have a finite number of values due to fixed size allocation; problematic for numeric types. Exceptions: Smalltalk uses unbounded fractions. Haskell type Integer represents unbounded integers. Floating point problems?

Even more problematic is fixed sized floating point numbers: 0.2 is not exact in binary. So 0.2 * 5 is not exactly 1.0 Floating point is inconsistent with real numbers in mathematics.

In the early languages, Fortran, Algol, Cobol, all of the types were built in. If needed a type color, could use integers; but what does it mean to multiply two colors. Purpose of types in programming languages is to provide ways of effectively modeling a problem solution.

5.1 Type Errors Machine data carries no type information. Basically, just a sequence of bits. Example: 0100 0000 0101 1000 0000 0000 0000 0000

0100 0000 0101 1000 0000 0000 0000 0000 The floating point number 3.375 The 32-bit integer 1,079,508,992 Two 16-bit integers 16472 and 0 Four ASCII characters: @ X NUL NUL

Def: A type error is any error that arises because an operation is attempted on a data type for which it is undefined. Type errors are common in assembly language programming. High level languages reduce the number of type errors.

Def: A type system is a precise definition of the bindings between the types of a variable, its values, and the possible operations over those values A type system provides a basis for detecting type errors.

5.2 Static and Dynamic Typing A type system imposes constraints (such as the values used in an addition must be numeric). Cannot be expressed syntactically in EBNF. Some languages perform type checking at compile time (eg, C, C++, OCaml ). Other languages (eg, Perl,Scheme,Python) perform type checking at run time. Still others (eg, Java) do both.

Def: A language is statically typed if the types of all variables are fixed when they are declared at compile time. Def: A language is dynamically typed if the type of a variable can vary at run time depending on the value assigned. Can you give more examples of each?

Def: A language is strongly typed if its type system allows all type errors in a program to be detected either at compile time or at run time. Note: A strongly typed language can be either statically or dynamically typed. Union types are a hole in the type system of many languages (eg, C, C++). Most dynamically typed languages associate a type with each value.

5.3 Basic Types Terminology in use with current 32-bit computers: Nibble: 4 bits Byte: 8 bits Half-word: 16 bits Word: 32 bits Double word: 64 bits Quad word: 128 bits

In most languages, the numeric types are finite in size. So a + b may overflow the finite range. Unlike mathematics: a + (b + c) (a + b) + c Can you see why?

Also in C-like languages, the equality and relational operators produce an int, not a Boolean (2 < 4) evaluates to 0 (2 > 4) evaluates to 1 if 5 { } else { } is legal, and meaningful, code!

Def: An operator or function is overloaded when its meaning varies depending on the types of its operands or arguments or result. Java: a+b (ignoring size) integer add floating point add string concatenation Mixed mode: one operand an int, the other floating point

Languages that allow mix mode syntax introduce implicit type conversion between values (eg. 3.4 + 1 is treated as 3.4 + inttofloat(1)) Def: A type conversion is a narrowing conversion if the result type permits fewer bits, thus potentially losing information. Otherwise it is a widening conversion. Should languages ban implicit narrowing conversions? Why?

5.4 Nonbasic Types Enumerations enum day {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday}; enum day myday = Wednesday; In C/C++ these just define an int range [0..6] where Monday == 0, Tuesday == 1 and so on

Enumeration types are powerful in Java: for (day d : day.values()) Sytem.out.println(d); They are even more powerful in Ocaml, Haskell as they a special case of algebraic data types (more on them later)

Pointers C, C++, Ada, Pascal Java??? OCaml?? The values in a pointer type are memory addresses They are used for indirect referencing of data Operator in C: *

Example struct Node { int key; struct Node* next; }; struct Node* head;

Fig 5.4: A Simple Linked List in C

Pointers Bane of reliable software development Error-prone Buffer overflow, memory leaks Particularly troublesome in C

strcpy void strcpy(char *p, char *q) { while (*p++ = *q++) ; }

Pointer Operations If T is a type and ref T is a pointer: & : T ref T * : ref T T For an arbitrary variable x: *(&x) = x

Arrays int a[10]; float x[3][5]; /* odd syntax vs. math */ char s[40]; /* indices: 0... n-1 */

Array Indexing Only operation for many languages Type signature [ ] : T[ ] x int T Example float x[3] [5]; type of x: float[ ][ ] type of x[1]: float[ ] type of x[1][2]: float

Equivalence between arrays and pointers in C/C++ a = &a[0] If either e1 or e2 is type: ref T e1[e2] = *(e1 + e2) Example: a is float[ ] and i int a[i] = *(a + i)

float sum(float a[ ], int n) { int i; float s = 0.0; for (i = 0; i<n; i++) s += a[i]; return s; float sum(float *a, int n) { int i; float s = 0.0; for (i = 0; i<n; i++) s += *a++; return s;

Strings Now so fundamental, directly supported. In C, a string is a 1D array with the string value terminated by a NULL character (value = 0). In Java, Perl, Python, a string variable can hold an unbounded number of characters. Libraries of string operations and functions.

Structures (aka Records) Analogous to a tuple in mathematics Collection of elements of different types Used first in Cobol, PL/I Absent from Fortran, Algol 60 Common to Pascal-like, C-like, ML-like languages, Omitted from Java as redundant

struct employeetype { int id; char name[25]; int age; float salary; char dept; }; struct employeetype employee;... employee.age = 45;

Unions C: union Pascal: case-variant record Logically: multiple views of same storage Useful in some systems applications In functional languages, superseded by recursive data types (sometimes also called union types or algebraic data types)

(* Union type in Pascal *) type union = record case b : boolean of end; true : (i : integer); false : (r : real); var u : union, j: integer; begin u := (b => false, r => 3.375); j := tagged.i; (* will generate error *)

// simulated union type in Java class Value extends Expression { // Value = int intvalue boolean boolvalue Type t; int intvalue; boolean boolvalue; Value(int i) { intvalue = i; t = new Type(Type.INTEGER); } Value(boolean b) { boolvalue = b; t = new Type(Type.BOOLEAN); }