Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;

Slides:



Advertisements
Similar presentations
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 13 Object-Oriented Programming I am surprised.
Advertisements

Copyright © 1998 by Addison Wesley Longman, Inc. 1 Chapter 4 Names - Design issues: - Maximum length? - Are connector characters allowed? - Are names case.
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Programming Languages and Paradigms
Programming Paradigms Introduction. 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1:
Abstract Data Types Data abstraction, or abstract data types, is a programming methodology where one defines not only the data structure to be used, but.
Chapter 5: Elementary Data Types Properties of types and objects –Data objects, variables and constants –Data types –Declarations –Type checking –Assignment.
Compiler Construction
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
CSC321: Programming Languages5-1 CSC321: Programming Languages Chapter 5: Types 5.1Type Errors 5.2Static and Dynamic Typing 5.3Basic Types 5.4NonBasic.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 12 Imperative Programming I really hate this.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 10 Function Implementation In theory, there.
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 2 Introductory Programs. 2 Getting started To create and run a Java program –Create a text file with a.java extension for the source code. For.
Generic programming Define software components with type parameters –A sorting algorithm has the same structure, regardless of the types being sorted –Stack.
Primitive Data Types: Numbers Strings Ordinal Types Pointers
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 6 Type Systems I was eventually persuaded.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Chapter 5: Types Fall 2009 Marco Valtorta.
Generic programming Define software components with type parameters –A sorting algorithm has the same structure, regardless of the types being sorted –Stack.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 13 Object-Oriented Programming I am surprised.
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.
Programming Languages Tucker and Noonan
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 4 Names The first step toward wisdom is calling.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 9 Functions It is better to have 100 functions.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 12 Imperative Programming I really hate this.
Types(2). 2 Recursive Problems  One or more simple cases of the problem have a straightforward, nonrecusive solution  The other cases can be redefined.
Names Variables Type Checking Strong Typing Type Compatibility 1.
Java Tutorial. Object-Oriented Programming Concepts Object –a representation of some item state  fields/members and should be encapsulated behavior 
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
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.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. C H A P T E R F I V E Memory Management.
Semantics. Semantics is a precise definition of the meaning of a syntactically and type-wise correct program. Ideas of meaning: –Operational Semantics.
Ch. 5 Ch. 51 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (more notes) Dr. Carter Tiernan.
Copyright © Curt Hill Structured Data What this course is about.
C++ Lecture 1 Friday, 4 July History of C++ l Built on top of C l C was developed in early 70s from B and BCPL l Object oriented programming paradigm.
ISBN Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types.
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.
Slide 1 Vitaly Shmatikov CS 345 Types and Parametric Polymorphism.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;
Copyright © 2006 The McGraw-Hill Companies, Inc. Basic Terminology Value-returning functions: –known as “non-void functions/methods” in C/C++/Java –called.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6: Transition to Java Programming with Alice and Java First Edition.
ISBN Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.
Ch. 5 Ch. 51 jcmt Summer 2003Programming Languages CSE3302 Programming Languages (more notes) Summer 2003 Dr. Carter Tiernan.
Static Checking and Type Systems Chapter 6. 2 Static versus Dynamic Checking Static checking: the compiler enforces programming language’s static semantics.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 1 Overview A good programming language is.
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
BUILDING JAVA PROGRAMS CHAPTER 7 Arrays days until the AP Computer Science test.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Pointers A pointer type variable holds the address of a data object or a function. A pointer can refer to an object of any one data type; it cannot refer.
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.
Chapter 1: Preliminaries Lecture # 2. Chapter 1: Preliminaries Reasons for Studying Concepts of Programming Languages Programming Domains Language Evaluation.
ISBN Chapter 12 Support for Object-Oriented Programming.
Scalar and composite data Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 8 Semantic Interpretation To understand a.
Types Type Errors Static and Dynamic Typing Basic Types NonBasic Types
Definition of the Programming Language CPRL
Data Types In Text: Chapter 6.
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.
Types of Programming Languages
Programming Languages 2nd edition Tucker and Noonan
Subtype Substitution Principle
Lecture 7: Types (Revised based on the Tucker’s slides) 10/4/2019
Presentation transcript:

Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming; they make it digestible. Robin Milner

Copyright © 2006 The McGraw-Hill Companies, Inc. Contents 5.1Type Errors 5.2Static and Dynamic Typing 5.3Basic Types 5.4NonBasic Types 5.5Recursive Data Types 5.6Functions as Types 5.7Type Equivalence 5.8Subtypes 5.9Polymorphism and Generics 5.10Programmer-Defined Types

Copyright © 2006 The McGraw-Hill Companies, Inc. Strings Now so fundamental, directly supported. In C, a string is a 1D array with the string value terminated by a NUL character (value = 0). In Java, Perl, Python, a string variable can hold an unbounded number of characters. Libraries of string operations and functions.

Copyright © 2006 The McGraw-Hill Companies, Inc. Structures 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 languages Omitted from Java as redundant

Copyright © 2006 The McGraw-Hill Companies, Inc. struct employeeType { int id; char name[25]; int age; float salary; char dept; }; struct employeeType employee;... employee.age = 45;

Copyright © 2006 The McGraw-Hill Companies, Inc. Unions C: union Pascal: case-variant record Logically: multiple views of same storage Useful in some systems applications

Copyright © 2006 The McGraw-Hill Companies, Inc. type union = record case b : boolean of true : (i : integer); false : (r : real); end; var tagged : union; begin tagged := (b => false, r => 3.375); put(tagged.i); -- error

Copyright © 2006 The McGraw-Hill Companies, Inc. // simulated union type class Value extends Expression { // Value = int intValue | boolean boolValue Type type; int intValue; boolean boolValue; Value(int i) { intValue = i; type = new Type(Type.INTEGER); } Value(boolean b) { boolValue = b; type = new Type(Type.BOOLEAN); }

Copyright © 2006 The McGraw-Hill Companies, Inc. 5.5 Recursive Data Type data Value = IntValue Integer | FloatValue Float | BoolValue Bool | CharValue Char deriving (Eq, Ord, Show) data Expression = Var Variable | Lit Value | Binary Op Expression Expression | Unary Op Expression deriving (Eq, Ord, Show) type Variable = String type Op = String type State = [(Variable, Value)]

Copyright © 2006 The McGraw-Hill Companies, Inc. 5.6 Functions as Types Pascal example: function newton(a, b: real; function f: real): real; Know that f returns a real value, but the arguments to f are unspecified.

Copyright © 2006 The McGraw-Hill Companies, Inc. public interface RootSolvable { double valueAt(double x); } public double Newton(double a, double b, RootSolvable f);

Copyright © 2006 The McGraw-Hill Companies, Inc. 5.7 Type Equivalence Pascal Report: The assignment statement serves to replace the current value of a variable with a new value specified as an expression.... The variable (or the function) and the expression must be of identical type. Nowhere does it define identical type.

Copyright © 2006 The McGraw-Hill Companies, Inc. struct complex { float re, im; }; struct polar { float x, y; }; struct { float re, im; } a, b; struct complex c, d; struct polar e; int f[5], g[10]; // which are equivalent types?

Copyright © 2006 The McGraw-Hill Companies, Inc. 5.8 Subtypes A subtype is a type that has certain constraints placed on its values or operations. In Ada subtypes can be directly specified.

Copyright © 2006 The McGraw-Hill Companies, Inc. subtype one_to_ten is Integer range ; type Day is (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday); subtype Weekend is Day range Saturday.. Sunday; type Salary is delta 0.01 digits 9 range _999_999.99; subtype Author_Salary is Salary digits 5 range ;

Copyright © 2006 The McGraw-Hill Companies, Inc. Integer i = new Integer(3);... Number v = i;... Integer x = (Integer) v; //Integer is a subclass of Number, // and therefore a subtype

Copyright © 2006 The McGraw-Hill Companies, Inc. Polymorphism and Generics A function or operation is polymorphic if it can be applied to any one of several related types and achieve the same result. An advantage of polymorphism is that it enables code reuse.

Copyright © 2006 The McGraw-Hill Companies, Inc. Polymorphism Comes from Greek Means: having many forms Example: overloaded built-in operators and functions + - * / == !=... Java: + also used for string concatenation Ada 83

Copyright © 2006 The McGraw-Hill Companies, Inc. Ada, C++: define for new types Java overloaded methods: number or type of parameters Example: class PrintStream print, println defined for: boolean, char, int, long, float, double, char[ ] String, Object

Copyright © 2006 The McGraw-Hill Companies, Inc. Java: instance variable, method –name, name( ) Ada generics: generic sort –parametric polymorphism –type binding delayed from code implementation to compile time –procedure sort is new generic_sort(integer);

Copyright © 2006 The McGraw-Hill Companies, Inc. generic type element is private; type list is array(natural range <>) of element; with function ">"(a, b : element) return boolean; package sort_pck is procedure sort (in out a : list); end sort_pck;

Copyright © 2006 The McGraw-Hill Companies, Inc. package sort_pck is procedure sort (in out a : list) is begin for i in a'first.. a'last - 1 loop for j in i+1.. a'last loop if a(i) > a(j) then declare t : element; begin t := a(i); a(i) := a(j); a(j) := t; end; end if;

Copyright © 2006 The McGraw-Hill Companies, Inc. Instantiation package integer_sort is new generic_sort( Integer, ">" );

Copyright © 2006 The McGraw-Hill Companies, Inc. Programmer-defined Types Recall the definition of a type: A set of values and a set of operations on those values. Structures allow a definition of a representation; problems: Representation is not hidden Type operations cannot be defined Defer further until Chapter 12.