CSE 341, Winter 2010 1 Type Systems Terms to learn about types: –Type –Type system –Statically typed language –Dynamically typed language –Type error –Strongly.

Slides:



Advertisements
Similar presentations
ISBN Chapter 7 Expressions and Assignment Statements.
Advertisements

CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
Chapter 7:: Data Types Programming Language Pragmatics
Chapter 5: Elementary Data Types Properties of types and objects –Data objects, variables and constants –Data types –Declarations –Type checking –Assignment.
Type Checking.
Compiler Construction
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.
ISBN Chapter 7 Expressions and Assignment Statements.
Approaches to Typing Programming Languages Robert Dewar.
CSE S. Tanimoto Syntax and Types 1 Representation, Syntax, Paradigms, Types Representation Formal Syntax Paradigms Data Types Type Inference.
C++ Training Datascope Lawrence D’Antonio Lecture 1 Quiz 1.
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.
MT311 Java Application Programming and Programming Languages Li Tak Sing ( 李德成 )
Copyright © 1998 by Addison -Wesley Longman, Inc. 1 Chapter 6 Arithmetic Expressions - Their evaluation was one of the motivations for the development.
1-1 University of Hail College of Computer Science and Engineering Department of computer Science and Software Engineering Course: ICS313: Fundamentals.
CS 363 Comparative Programming Languages Expressions and Assignment Statements.
VARIABLES AND TYPES CITS1001. Types in Java the eight primitive types the unlimited number of object types Values and References The Golden Rule Scope.
Names and Binding In procedural programming, you write instructions the manipulate the “state” of the process where the “state” is the collection of variables.
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.
March 12, ICE 1341 – Programming Languages (Lecture #6) In-Young Ko Programming Languages (ICE 1341) Lecture #6 Programming Languages (ICE 1341)
CSCE 121: Introduction to Program Design and Concepts, Honors Dr. J. Michael Moore Spring 2015 Set 3: Objects, Types, and Values 1 Based on slides.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
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
Expressions and Assignment Statements
Chapter 7 © 1998 by Addison -Wesley Longman, Inc Arithmetic Expressions - Their evaluation was one of the motivations for the development of the.
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.
ISBN Chapter 7 Expressions and Assignment Statements.
CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.
12/9/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1.
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
CPS120: Introduction to Computer Science Variables and Constants.
C H A P T E R T H R E E Type Systems and Semantics Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
CS 330 Programming Languages 10 / 23 / 2007 Instructor: Michael Eckmann.
1 CS Programming Languages Class 10 September 26, 2000.
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.
Expressions and Assignment Statements
Polymorphism in Methods
7.2 Arithmetic Expressions
Data Types In Text: Chapter 6.
Type Checking and Type Inference
Programming Languages and Compilers (CS 421)
Semantic Analysis Type Checking
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 4: Type Systems.
Programming Languages and Compilers (CS 421)
Representation, Syntax, Paradigms, Types
Type Systems Terms to learn about types: Related concepts: Type
Programming Languages and Compilers (CS 421)
Expressions and Assignment Statements
Arithmetic Expressions
Names, Binding, and Scope
College of Computer Science and Engineering
Representation, Syntax, Paradigms, Types
Representation, Syntax, Paradigms, Types
Expression and Asignment Statements
Names and Binding In Text: Chapter 5.
Chapter 7 Expressions and Assignment Statements.
Type Systems Terms to learn: Type Type system
Representation, Syntax, Paradigms, Types
Type Systems Terms to learn about types: Related concepts: Type
Subtype Substitution Principle
PRESENTED BY ADNAN M. UZAIR NOMAN
Presentation transcript:

CSE 341, Winter Type Systems Terms to learn about types: –Type –Type system –Statically typed language –Dynamically typed language –Type error –Strongly typed –Weakly typed –Type safe program –Type safe language Related concepts: –Polymorphism –Overloading –Coercion

CSE 341, Winter Type – Definition Type: a set of values and operations on those values Java examples of types: –int –values = {-2 31, …, -2, -1, 0, 1, 2, …, } (2 31 = 2,147,483,648) –operations = {+, -, *, …} –boolean –values = {false, true} –operations = {&&, ||, !, …} –String –values = {“”, “a”, “b”, …, “A”, …, “$”, … “ Σ ”, … “aa”, “ab”, …} –operations = {+, trim(), equals(Object x), clone(), …} –Applet –values = [all possible applets] –operations = {init(), paint(Graphics g), clone(), …}

CSE 341, Winter Type System A well-defined system of associating types with variables and expressions in the language

CSE 341, Winter Statically Typed Languages Statically typed. Statically typed means that the type of every expression can be determined at compile time. Java, ML, and Haskell are examples of statically typed languages. (Scheme and Ruby are not statically typed though.) Each variable has a single type throughout the lifetime of that variable at runtime. Note that “statically typed” is not the same as “type declarations are required”.

CSE 341, Winter Dynamically Typed Languages Dynamically typed. The types of expressions are not known until runtime. –Example languages: Scheme, Smalltalk, Python, Ruby. –Usually, the type of a variable can change dynamically during the execution of the program. (Some authors make this part of the definition, although we won’t for 341.) This is legal Scheme code: ;; don’t need to declare the type of x (define x 42) (set! x ′(1 2 squid))

CSE 341, Winter Type error A type error is a runtime error that occurs when we attempt an operation on a value for which that operation is not defined. –With a static type system, we can determine at compile time whether such a type error could occur -- thus avoiding a runtime type error. Examples: boolean b, c; b = c+1; int i; boolean b; i = b;

CSE 341, Winter Type Safety A program is type safe if it is known to be free of type errors. –However, the system is allowed to halt at runtime before performing an operation that would result in a type error. A language is type safe if all legal programs in that language are type safe. Java, Smalltalk, Scheme, Haskell, Ruby, and Ada are examples of type safe languages. Fortran and C are examples of languages that aren’t type safe. Some languages for systems programming, for example Mesa, have a safe subset, although the language as a whole is not type safe.

CSE 341, Winter Tradeoffs Generally we want languages to be type safe. An exception is a language used for some kinds of systems programming, for example writing a garbage collector. The “safe subset” approach is one way to deal with this problem. Advantages of static typing: –catch errors at compile time –machine-checkable documentation –potential for improved efficiency Advantages of dynamic typing: –Flexibility –rapid prototyping

CSE 341, Winter Strongly Typed Language We’ll define a strongly typed language to be the same as a type safe language. –However, you may want to avoid this term – or at least explain what you mean. (See the next slide.) Weakly typed means “not strongly typed”.

CSE 341, Winter Terminology about Types - Problems Unfortunately different authors use different definitions for the terms “statically typed” and “strongly typed”. Statically typed. We define “statically typed” to mean that the compiler can statically assign a correct type to every expression. –Others define statically typed to mean that the compiler can statically assign a type to every expression, but that type might be wrong. (By this alternate definition C and Fortran are statically typed.) Strongly typed. We’ll define strongly typed to mean the same as type safe. For others, strongly typed implies type safe and statically typed. (Is Scheme strongly typed?) To avoid misunderstanding, one can describe a language as e.g. “type safe and statically typed” rather than “strongly typed”. See the Wikipedia article on “Strongly typed” for a long list of what different people have meant by this term.

CSE 341, Winter Polymorphism, Overloading, and Coercion A polymorphic function is a single function that can be applied to several different types. The append function in Haskell is an example: it can be used on lists with any type of element. (++) :: [a] -> [a] -> [a] Note that there is just one definition of the function ++. An overloaded function is a function with several different definitions. The language implementation chooses the correct definition based on the types of the arguments (and for a few languages based on the type of the result -- generally not though). This can be done either at compile time or run time. In Haskell this done at compile time. Examples: +, the “show” function.

CSE 341, Winter Polymorphism, Overloading, and Coercion (continued) Coercion means that the language implementation converts one type to another to match the requirements of the function or operator. Typical example: coerce an integer to a float in Many languages support this (Fortran, Java, Smalltalk, ….) However, Haskell does not support coercion. Instead, the type of 3 (for example) is more general than Int or Float -- it is (Num t) => t Try this program: a = 3 :: Int b = 4 :: Float c = a + b