Types John Mitchell CS 242 Reading: Chapter 6. Announcements uHomework 1 due today Turn in at the end of class, OR Turn in by 5PM to the homework drop.

Slides:



Advertisements
Similar presentations
Modern Programming Languages, 2nd ed.
Advertisements

Kathleen Fisher cs242 Reading: “Concepts in Programming Languages”, Chapter 6.
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
1 Mooly Sagiv and Greta Yorsh School of Computer Science Tel-Aviv University Modern Compiler Design.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
Overview of Previous Lesson(s) Over View  Front end analyzes a source program and creates an intermediate representation from which the back end generates.
Names and Bindings.
Kathleen Fisher cs242 Reading: “Concepts in Programming Languages”, Chapter 6.
Slide 1 Vitaly Shmatikov CS 345 Functions. slide 2 Reading Assignment uMitchell, Chapter 7 uC Reference Manual, Chapters 4 and 9.
INF 212 ANALYSIS OF PROG. LANGS Type Systems Instructors: Crista Lopes Copyright © Instructors.
Various languages….  Could affect performance  Could affect reliability  Could affect language choice.
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.
Type Checking.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Type Checking- Contd Compiler Design Lecture (03/02/98) Computer Science Rensselaer Polytechnic.
Cse321, Programming Languages and Compilers 1 6/19/2015 Lecture #18, March 14, 2007 Syntax directed translations, Meanings of programs, Rules for writing.
ISBN Chapter 9 Subprograms. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Introduction Two fundamental abstraction facilities.
CS 330 Programming Languages 10 / 24 / 2006 Instructor: Michael Eckmann.
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
Approaches to Typing Programming Languages Robert Dewar.
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.
Slide 1 Vitaly Shmatikov CS 345 Scope and Activation Records.
ICE1341 Programming Languages Spring 2005 Lecture #3 Lecture #3 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
© 2003 G. Drew Kessler and William M. Pottenger1 Subroutines (Part 1) CSE 262, Spring 2003.
1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
Chapter TwelveModern Programming Languages1 Memory Locations For Variables.
Types and Type Inference Mooly Sagiv Slides by Kathleen Fisher and John Mitchell Reading: “Concepts in Programming Languages”, Revised Chapter 6 - handout.
INF Polymorphism and Type Inference Fatemeh Kazemeyni Department of Informatics – University of Oslo Based on John C. Mitchell’s.
5-1 Chapter 5: Names, Bindings, Type Checking, and Scopes Variables The Concept of Binding Type Checking Strong Typing Type Compatibility Scope and Lifetime.
CSE 425: Data Types II Survey of Common Types I Records –E.g., structs in C++ –If elements are named, a record is projected into its fields (e.g., via.
Runtime Environments Compiler Construction Chapter 7.
March 12, ICE 1341 – Programming Languages (Lecture #6) In-Young Ko Programming Languages (ICE 1341) Lecture #6 Programming Languages (ICE 1341)
Types John Mitchell CS 242. Type A type is a collection of computable values that share some structural property. uExamples Integers Strings int  bool.
Basic Semantics Associating meaning with language entities.
Midterm Review John Mitchell CS 242. Midterm uThurs Nov 7, 7-9PM Terman Aud Closed book uClass schedule Thurs Oct 31, Tues Nov 5 – topics not on midterm.
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.
Functional Programming With examples in F#. Pure Functional Programming Functional programming involves evaluating expressions rather than executing commands.
PZ06C Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ06C - Polymorphism Programming Language Design and.
Polymorphism Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 7.3.
Slide 1 Vitaly Shmatikov CS 345 Types and Parametric Polymorphism.
12/9/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
Slide 1 Dr. Mohammad El-Ramly Fall 2010 Set 7- II - Functions Slides by Vitaly Shmatikov Cairo University Faculty of Computers and Information CS317 Concepts.
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.
1 Structure of Compilers Lexical Analyzer (scanner) Modified Source Program Parser Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator.
CS 330 Programming Languages 10 / 23 / 2007 Instructor: Michael Eckmann.
Types John Mitchell CS 242. Type A type is a collection of computable values that share some structural property. uExamples Integers Strings int  bool.
Chapter 1: Preliminaries Lecture # 2. Chapter 1: Preliminaries Reasons for Studying Concepts of Programming Languages Programming Domains Language Evaluation.
Dr. M. Al-Mulhem Introduction 1 Chapter 6 Type Systems.
Heath Carroll Bill Hanczaryk Rich Porter.  A Theory of Type Polymorphism in Programming ◦ Robin Milner (1977)  Milner credited with introducing the.
Object Lifetime and Pointers
Data Types In Text: Chapter 6.
Type Checking and Type Inference
Principles of programming languages 12: Functional programming
CSCI 3370: Principles of Programming Languages Chapter 9 Subprograms
Names, Binding, and Scope
Case Study: ML John Mitchell Reading: Chapter 5.
CSE-321 Programming Languages Introduction to Functional Programming
Polymorphism Programming Language Design and Implementation
CS 242 Types John Mitchell.
CS 242 Types John Mitchell Reading: Chapter 6.
Polymorphism Programming Language Design and Implementation
Modern Programming Languages
Polymorphism Programming Language Design and Implementation
Polymorphism Programming Language Design and Implementation
Presentation transcript:

Types John Mitchell CS 242 Reading: Chapter 6

Announcements uHomework 1 due today Turn in at the end of class, OR Turn in by 5PM to the homework drop box –First floor of Gates Bldg outside Gates 182 –Box is labeled CS 242 Homework uHomework late policy You may turn in up to 3 late homeworks this quarter Each must be turned in by 5PM on Thursday –Same drop box as Wednesday homework

Outline uGeneral discussion of types What is a type? Compile-time vs run-time checking Conservative program analysis uType inference Good example of static analysis algorithm Will study algorithm and examples uPolymorphism Polymorphism vs overloading Uniform vs non-uniform impl of polymorphism

Language goals and trade-offs uThoughts to keep in mind What features are convenient for programmer? What other features do they prevent? What are design tradeoffs? –Easy to write but harder to read? –Easy to write but poorer error messages? What are the implementation costs? Architect Compiler, Runtime environ- ment Programmer Tester Diagnostic Tools Programming Language

Type A type is a collection of computable values that share some structural property. uExamples Integers Strings int  bool (int  int)  bool u“Non-examples”  3, true, x.x  Even integers  f:int  int | if x>3 then f(x) > x*(x+1)  Distinction between sets that are types and sets that are not types is language dependent.

Uses for types uProgram organization and documentation Separate types for separate concepts –Represent concepts from problem domain Indicate intended use of declared identifiers –Types can be checked, unlike program comments uIdentify and prevent errors Compile-time or run-time checking can prevent meaningless computations such as 3 + true - “Bill” uSupport optimization Example: short integers require fewer bits Access record component by known offset

Compile-time vs run-time checking uJavaScript, Lisp use run-time type checking f(x) make sure f is a function before calling f uML uses compile-time type checking f(x) must have f : A  B and x : A uBasic tradeoff Both prevent type errors Run-time checking slows down execution Compile-time checking restricts program flexibility JavaScript array: elements can have different types ML list: all elements must have same type Which gives better programmer diagnostics?

Expressiveness uIn JavaScript, we can write function like function f(x) { return x < 10 ? x : x(); } Some uses will produce type error, some will not uStatic typing always conservative if (big-hairy-boolean-expression) then f(5); else f(10); Cannot decide at compile time if run-time error will occur

Relative type-safety of languages uNot safe: BCPL family, including C and C++ Casts, pointer arithmetic uAlmost safe: Algol family, Pascal, Ada. Dangling pointers. –Allocate a pointer p to an integer, deallocate the memory referenced by p, then later use the value pointed to by p –No language with explicit deallocation of memory is fully type-safe uSafe: Lisp, ML, Smalltalk, JavaScript, and Java Lisp, Smalltalk, JavaScript: dynamically typed ML, Java: statically typed

Type checking and type inference uStandard type checking int f(int x) { return x+1; }; int g(int y) { return f(y+1)*2;}; Look at body of each function and use declared types of identifies to check agreement. uType inference int f(int x) { return x+1; }; int g(int y) { return f(y+1)*2;}; Look at code without type information and figure out what types could have been declared. ML is designed to make type inference tractable.

Motivation uTypes and type checking Type systems have improved steadily since Algol 60 Important for modularity, compilation, reliability uType inference Widely regarded as important language innovation ML type inference is an illustrative example flow- insensitive static analysis algorithm

ML Type Inference uExample - fun f(x) = 2+x; > val it = fn : int  int uHow does this work? + has two types: int*int  int, real*real  real 2 : int has only one type This implies + : int*int  int From context, need x: int Therefore f(x:int) = 2+x has type int  int Overloaded + is unusual. Most ML symbols have unique type. In many cases, unique type may be polymorphic.

Another presentation uExample - fun f(x) = 2+x; > val it = fn : int  int uHow does this +2 Assign types to leaves : t int  int  int real  real  real : int Propagate to internal nodes and generate constraints int (t = int) int  int t  int Solve by substitution = int  int Graph for x. ((plus 2) x)

Application and Abstraction uApplication f must have function type domain  range domain of f must be type of argument x result type is range of f u Function expression Type is function type domain  range Domain is type of variable x Range is type of function body e fx e : t: s : t : r (s = t  r): s  t

Types with type variables uExample - fun f(g) = g(2); > val it = fn : (int  t)  t uHow does this work? g Assign types to leaves : int: s Propagate to internal nodes and generate constraints t (s = int  t) stst Solve by substitution = (int  t)  t Graph for g. (g 2)

Use of Polymorphic Function uFunction - fun f(g) = g(2); > val it = fn : (int  t)  t uPossible applications - fun add(x) = 2+x; > val it = fn : int  int - f(add); > val it = 4 : int - fun isEven(x) =...; > val it = fn : int  bool - f(isEven); > val it = true : bool

Recognizing type errors uFunction - fun f(g) = g(2); > val it = fn : (int  t)  t uIncorrect use - fun not(x) = if x then false else true; > val it = fn : bool  bool - f(not); Type error: cannot make bool  bool = int  t

Another Type Inference Example uFunction Definition - fun f(g,x) = g(g(x)); > val it = fn : (t  t)*t  t uType Inference Solve by substitution = (v  v)*v  g g Assign types to leaves : t : s Propagate to internal nodes and generate constraints v (s = u  v) s*t  v u (s = t  u) Graph for  g,x . g(g x)

Polymorphic Datatypes uDatatype with type variable ’a is syntax for “type variable a” - datatype ‘a list = nil | cons of ‘a*(‘a list) > nil : ‘a list > cons : ‘a*(‘a list)  ‘a list u Polymorphic function - fun length nil = 0 | length (cons(x,rest)) = 1 + length(rest) > length : ‘a list  int uType inference Infer separate type for each clause Combine by making two types equal (if necessary)

Type inference with recursion uSecond Clause length(cons(x,rest)) = 1 + length(rest) uType inference Assign types to leaves, including function name Proceed as usual Add constraint that type of function body = type of function name rest : t ‘a list  int = t : ‘a*‘a list  ‘a list We do not expect you to master this.

Main Points about Type Inference uCompute type of expression Does not require type declarations for variables Find most general type by solving constraints Leads to polymorphism uStatic type checking without type specifications Idea can be applied to other program properties uSometimes better error detection than type checking Type may indicate a programming error even if there is no type error (example following slide) uSome costs More difficult to identify program line that causes error ML requires different syntax for integer 3, real 3.0 Complications regarding assignment took years to work out

Information from type inference uAn interesting function on lists fun reverse (nil) = nil | reverse (x::lst) = reverse(lst); uMost general type reverse : ‘a list  ‘b list uWhat does this mean? Since reversing a list does not change its type, there must be an error in the definition of “reverse” See Koenig paper on “Reading” page of CS242 site

Polymorphism vs Overloading uParametric polymorphism Single algorithm may be given many types Type variable may be replaced by any type f : t  t => f : int  int, f : bool  bool,... uOverloading A single symbol may refer to more than one algorithm Each algorithm may have different type Choice of algorithm determined by type context Types of symbol may be arbitrarily different + has types int*int  int, real*real  real, no others

Parametric Polymorphism: ML vs C++ uML polymorphic function Declaration has no type information Type inference: type expression with variables Type inference: substitute for variables as needed uC++ function template Declaration gives type of function arg, result Place inside template to define type variables Function application: type checker does instantiation ML also has module system with explicit type parameters

Example: swap two values uML -fun swap(x,y) = let val z = !x in x := !y; y := z end; val swap = fn : 'a ref * 'a ref -> unit uC++ template void swap(T& x, T& y){ T tmp = x; x=y; y=tmp; } Declarations look similar, but compiled very differently

Implementation uML Swap is compiled into one function Typechecker determines how function can be used uC++ Swap is compiled into linkable format Linker duplicates code for each type of use uWhy the difference? ML ref cell is passed by pointer, local x is pointer to value on heap C++ arguments passed by reference (pointer), but local x is on stack, size depends on type

Another example uC++ polymorphic sort function template void sort( int count, T * A[count] ) { for (int i=0; i<count-1; i++) for (int j=i+1; j<count-1; j++) if (A[j] < A[i]) swap(A[i],A[j]); } uWhat parts of implementation depend on type? Indexing into array Meaning and implementation of <

ML Overloading uSome predefined operators are overloaded uUser-defined functions must have unique type - fun plus(x,y) = x+y; This is compiled to int or real function, not both uWhy is a unique type needed? Need to compile code  need to know which + Efficiency of type inference Aside: General overloading is NP-complete Two types, true and false Overloaded functions and : {true*true  true, false*true  false, …}

Summary uTypes are important in modern languages Program organization and documentation Prevent program errors Provide important information to compiler uType inference Determine best type for an expression, based on known information about symbols in the expression uPolymorphism Single algorithm (function) can have many types uOverloading Symbol with multiple meanings, resolved at compile time