CS 242 Types John Mitchell Reading: Chapter 6.

Slides:



Advertisements
Similar presentations
Kathleen Fisher cs242 Reading: “Concepts in Programming Languages”, Chapter 6.
Advertisements

Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
Names and Bindings.
Kathleen Fisher cs242 Reading: “Concepts in Programming Languages”, Chapter 6.
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.
Chapter 5: Elementary Data Types Properties of types and objects –Data objects, variables and constants –Data types –Declarations –Type checking –Assignment.
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;
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
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.
Runtime Environments Compiler Construction Chapter 7.
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.
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.
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.
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.
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
CS412/413 Introduction to Compilers Radu Rugina Lecture 13 : Static Semantics 18 Feb 02.
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.
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
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.
Functional Programming
Object Lifetime and Pointers
Data Types In Text: Chapter 6.
Type Checking and Type Inference
Programming Languages and Compilers (CS 421)
Zuse’s Plankalkül – 1945 Never implemented Problems Zuse Solved
Principles of programming languages 12: Functional programming
Types for Programs and Proofs
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
ML: a quasi-functional language with strong typing
Compiler Construction (CS-636)
CS 326 Programming Languages, Concepts and Implementation
CS 326 Programming Languages, Concepts and Implementation
Concepts of programming languages
Type Systems Terms to learn about types: Related concepts: Type
Semantic Analysis Chapter 6.
CSCI 3370: Principles of Programming Languages Chapter 9 Subprograms
Compiler Design 18. Object Oriented Semantic Analysis (Symbol Tables, Type Checking) Kanat Bolazar March 30, 2010.
Functional Programming
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
Chapter 6 Intermediate-Code Generation
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
CSC 533: Programming Languages Spring 2015
FP Foundations, Scheme In Text: Chapter 14.
Case Study: ML John Mitchell Reading: Chapter 5.
Semantic Analysis Chapter 6.
CSE-321 Programming Languages Introduction to Functional Programming
Polymorphism Programming Language Design and Implementation
CS 242 Types John Mitchell.
Polymorphism Programming Language Design and Implementation
Polymorphism Programming Language Design and Implementation
CSC 533: Programming Languages Spring 2018
CSC 533: Programming Languages Spring 2019
Types and Related Issues
Polymorphism Programming Language Design and Implementation
Presentation transcript:

CS 242 Types John Mitchell Reading: Chapter 6

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

Uses for types Program 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 Identify and prevent errors Compile-time or run-time checking can prevent meaningless computations such as 3 + true - “Bill” Support optimization Example: short integers require fewer bits Access record component by known offset

Type errors Hardware error Unintended semantics function call x() where x is not a function may cause jump to instruction that does not contain a legal op code Unintended semantics int_add(3, 4.5) not a hardware error, since bit pattern of float 4.5 can be interpreted as an integer just as much an error as x() above

General definition of type error A type error occurs when execution of program is not faithful to the intended semantics Do you like this definition? Store 4.5 in memory as a floating-point number Location contains a particular bit pattern To interpret bit pattern, we need to know the type If we pass bit pattern to integer addition function, the pattern will be interpreted as an integer pattern Type error if the pattern was intended to represent 4.5

Compile-time vs run-time checking Lisp uses run-time type checking (car x) check first to make sure x is list ML uses compile-time type checking f(x) must have f : A  B and x : A Basic tradeoff Both prevent type errors Run-time checking slows down execution Compile-time checking restricts program flexibility Lisp list: elements can have different types ML list: all elements must have same type

Expressiveness In Lisp, we can write function like (lambda (x) (cond ((less x 10) x) (T (car x)))) Some uses will produce type error, some will not Static typing always conservative if (big-hairy-boolean-expression) then ((lambda (x) … ) 5) else ((lambda (x) … ) 10) Cannot decide at compile time if run-time error will occur

Relative type-safety of languages Not safe: BCPL family, including C and C++ Casts, pointer arithmetic Almost 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 Safe: Lisp, ML, Smalltalk, and Java Lisp, Smalltalk: dynamically typed ML, Java: statically typed

Type checking and type inference Standard 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. Type inference Look at code without type information and figure out what types could have been declared. ML is designed to make type inference tractable.

Motivation Types and type checking Type inference Type systems have improved steadily since Algol 60 Important for modularity, compilation, reliability Type inference A cool algorithm Widely regarded as important language innovation ML type inference gives you some idea of how many other static analysis algorithms work

ML Type Inference Example How does this work? - fun f(x) = 2+x; > val it = fn : int  int How 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 Example How does this work? x + 2 - fun f(x) = 2+x; > val it = fn : int  int How does this work? Graph for x. ((plus 2) x) Propagate to internal nodes and generate constraints int (t = int) intint tint Solve by substitution = intint x  @ + 2 Assign types to leaves : t int  int  int real  realreal : int

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

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

Use of Polymorphic Function - fun f(g) = g(2); > val it = fn : (int  t)  t Possible 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 Function - fun f(g) = g(2); > val it = fn : (int  t)  t Incorrect 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 Function Definition - fun f(g,x) = g(g(x)); > val it = fn : (t  t)*t  t Type Inference Graph for g,x. g(g x) Solve by substitution = (vv)*vv Propagate to internal nodes and generate constraints v (s = uv) s*tv u (s = tu)  @ g x Assign types to leaves : t : s

Polymorphic Datatypes Datatype 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 Polymorphic function - fun length nil = 0 | length (cons(x,rest)) = 1 + length(rest) > length : ‘a list  int Type inference Infer separate type for each clause Combine by making two types equal (if necessary)

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

Main Points about Type Inference Compute type of expression Does not require type declarations for variables Find most general type by solving constraints Leads to polymorphism Static type checking without type specifications May lead to better error detection than ordinary type checking Type may indicate a programming error even if there is no type error (example following slide).

Information from type inference An interesting function on lists fun reverse (nil) = nil | reverse (x::lst) = reverse(lst); Most general type reverse : ‘a list  ‘b list What 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 Parametric 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, ... Overloading 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++ ML polymorphic function Declaration has no type information Type inference: type expression with variables Type inference: substitute for variables as needed C++ 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 ML fun swap(x,y) = let val z = !x in x := !y; y := z end; val swap = fn : 'a ref * 'a ref -> unit C++ template <typename T> void swap(T& , T& y){ T tmp = x; x=y; y=tmp; } Declarations look similar, but compiled is very differently

Implementation ML C++ Why the difference? Swap is compiled into one function Typechecker determines how function can be used C++ Swap is compiled into linkable format Linker duplicates code for each type of use Why 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 C++ polymorphic sort function template <typename T> 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]); } What parts of implementation depend on type? Indexing into array Meaning and implementation of <

ML Overloading Some predefined operators are overloaded User-defined functions must have unique type - fun plus(x,y) = x+y; This is compiled to int or real function, not both Why 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 Types are important in modern languages Type inference Program organization and documentation Prevent program errors Provide important information to compiler Type inference Determine best type for an expression, based on known information about symbols in the expression Polymorphism Single algorithm (function) can have many types Overloading Symbol with multiple meanings, resolved at compile time