Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L05-1 September 21, 2006http://www.csg.csail.mit.edu/6.827 Types and Simple Type.

Slides:



Advertisements
Similar presentations
Types and Programming Languages Lecture 13 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Advertisements

Types and Programming Languages Lecture 15 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Types and Programming Languages Lecture 7 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Functional Programming Lecture 10 - type checking.
Kathleen Fisher cs242 Reading: “Concepts in Programming Languages”, Chapter 6.
Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
Semantics Static semantics Dynamic semantics attribute grammars
Computational Models The exam. Models of computation. –The Turing machine. –The Von Neumann machine. –The calculus. –The predicate calculus. Turing.
1 Scheme and Functional Programming Aaron Bloomfield CS 415 Fall 2005.
Cs776 (Prasad)L4Poly1 Polymorphic Type System. cs776 (Prasad)L4Poly2 Goals Allow expression of “for all types T” fun I x = x I : ’a -> ’a Allow expression.
- Vasvi Kakkad.  Formal -  Tool for mathematical analysis of language  Method for precisely designing language  Well formed model for describing and.
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
1 PROPERTIES OF A TYPE ABSTRACT INTERPRETATER. 2 MOTIVATION OF THE EXPERIMENT § a well understood case l type inference in functional programming à la.
1 Mooly Sagiv and Greta Yorsh School of Computer Science Tel-Aviv University Modern Compiler Design.
Elements of Lambda Calculus Functional Programming Academic Year Alessandro Cimatti
Type checking © Marcelo d’Amorim 2010.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Principles of programming languages 4: Parameter passing, Scope rules Department of Information Science and Engineering Isao Sasano.
INF 212 ANALYSIS OF PROG. LANGS Type Systems Instructors: Crista Lopes Copyright © Instructors.
CSE 341, Winter Type Systems Terms to learn about types: –Type –Type system –Statically typed language –Dynamically typed language –Type error –Strongly.
Type Checking.
Compiler Construction
7. Fixed Points. © O. Nierstrasz PS — Fixed Points 7.2 Roadmap  Representing Numbers  Recursion and the Fixed-Point Combinator  The typed lambda calculus.
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.
Catriel Beeri Pls/Winter 2004/5 type reconstruction 1 Type Reconstruction & Parametric Polymorphism  Introduction  Unification and type reconstruction.
Type Checking in Cool Alex Aiken (Modified by Mooly Sagiv)
Catriel Beeri Pls/Winter 2004/05 types 65  A type-checking algorithm The task: (since we start with empty H, why is the goal not just E?) The rule set.
0 PROGRAMMING IN HASKELL Chapter 3 - Types and Classes.
Type Inference: CIS Seminar, 11/3/2009 Type inference: Inside the Type Checker. A presentation by: Daniel Tuck.
Functional Programming Element of Functional Programming.
Type Equivalence Rules Ada –Strict name equivalence except for almost everything Unique array constructors give rise to unique types Subtypes can create.
Types for Programs and Proofs Lecture 1. What are types? int, float, char, …, arrays types of procedures, functions, references, records, objects,...
Gary MarsdenSlide 1University of Cape Town Principles of programming language design Gary Marsden Semester 2 – 2001.
Programming Languages and Design Lecture 7 Subroutines and Control Abstraction Instructor: Li Ma Department of Computer Science Texas Southern University,
1 Names, Scopes and Bindings Aaron Bloomfield CS 415 Fall
CS321 Functional Programming 2 © JAS Type Checking Polymorphism in Haskell is implicit. ie the system can derive the types of all objects. This.
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L06-1 September 26, 2006http:// Type Inference September.
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.
Data Types (3) 1 Programming Languages – Principles and Practice by Kenneth C Louden.
COMP 412, FALL Type Systems II C OMP 412 Rice University Houston, Texas Fall 2000 Copyright 2000, Robert Cartwright, all rights reserved. Students.
Types and Programming Languages Lecture 14 Simon Gay Department of Computing Science University of Glasgow 2006/07.
C H A P T E R E I G H T Functional Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L03-1 September 14, 2006http:// -calculus: A Basis for.
COMP 412, FALL Type Systems C OMP 412 Rice University Houston, Texas Fall 2000 Copyright 2000, Robert Cartwright, all rights reserved. Students.
Dr. M. Al-Mulhem Introduction 1 Chapter 6 Type Systems.
1 Announcements Exam 2 graded Rainbow grades: HW1-5, Exam 1-2, Quiz 1-6 Part 1: Problem 1, Problem 2, will go over 2 Part 2: Problem 1, Problem 2 Part.
Lesson 10 Type Reconstruction
CS5205: Foundation in Programming Languages Type Reconstruction
CS5205: Foundations in Programming Languages
Functional Programming
Type Checking and Type Inference
Programming Languages and Compilers (CS 421)
Types CSCE 314 Spring 2016.
ML: a quasi-functional language with strong typing
Haskell Chapter 2.
Principles of programming languages 4: Parameter passing, Scope rules
Programming Languages and Compilers (CS 421)
CSC 533: Programming Languages Spring 2015
Language-based Security
Semantic Analysis Chapter 6.
Compiler Construction
CSC 533: Programming Languages Spring 2018
Compiler Construction
CSC 533: Programming Languages Spring 2019
Programming Languages and Compilers (CS 421)
Presentation transcript:

Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L05-1 September 21, 2006http:// Types and Simple Type Inference September 21, 2006

L05-2 September 21, 2006http:// Outline General issues Type instances Type Unification Type Inference rules for a simple non- polymorphic type system Type Inference rules for a polymorphic type system Overloading next time...

L05-3 September 21, 2006http:// What are Types? A method of classifying objects (values) in a language x ::  says object x has type  or object x belongs to a type   denotes a set of values. This notion of types is different from types in languages like C, where a type is a storage class specifier.

L05-4 September 21, 2006http:// Type Correctness If x ::  then only those operations that are appropriate to set may be performed on x. A program is type correct if it never performs a wrong operation on an object. - Add an Int and a Bool - Head of an Int - Square root of a list

L05-5 September 21, 2006http:// Type Safety A language is type safe if only type correct programs can be written in that language. Most languages are not type safe, i.e., have “holes” in their type systems. Fortran: Equivalence, Parameter passing Pascal: Variant records, files C, C++: Pointers, type casting However, Java, CLU, Ada, ML, Id, Haskell, pH etc. are type safe.

L05-6 September 21, 2006http:// Type Declaration vs Reconstruction Languages where the user must declare the types –CLU, Pascal, Ada, C, C++, Fortran, Java Languages where type declarations are not needed and the types are reconstructed at run time –Scheme, Lisp Languages where type declarations are generally not needed but allowed, and types are reconstructed at compile time –ML, Id, Haskell, pH A language is said to be statically typed if type-checking is done at compile time

L05-7 September 21, 2006http:// Polymorphism In a monomorphic language like Pascal, one defines a different length function for each type of list In a polymorphic language like ML, one defines a polymorphic type (list t), where t is a type variable, and a single function for computing the length pH and most modern functional languages have polymorphic objects and follow the Hindley-Milner type system.

L05-8 September 21, 2006http:// Type Instances The type of a variable can be instantiated differently within its lexical scope. let id = \x.x in ((id 1 5), (id 2 True)) id 1 :: ? id 2 :: ? Both id 1 and id 2 can be regarded as instances of type ? Int --> Int Bool --> Bool t --> t

L05-9 September 21, 2006http:// Type Instances: another example twice 1 ::? twice 2 :: ? ((I -> I) -> I -> I) -> (I -> I) -> (I -> I) let twice :: (t -> t) -> t -> t twice f x = f (f x) in twice 1 twice 2 (plus 3) 4 (I -> I) -> I -> I

L05-10 September 21, 2006http:// Type Instantiation: -bound vs Let-bound Variables Only let-bound identifiers can be instantiated differently. let twice f x = f (f x) in twice twice succ 4 vs. let twice f x = f (f x) foo g = (g g succ) 4 in foo twice foo is not type correct ! Generic vs. Non-generic type variables

L05-11 September 21, 2006http:// A mini Language (-calculus + let) to study Hindley-Milner Types There are no types in the syntax of the language! The type of each subexpression is derived by the Hindley-Milner type inference algorithm. but first a Simple Type System... Expressions E ::= c constant | x variable | x. E abstraction | (E 1 E 2 ) application | let x = E 1 in E 2 let-block

L05-12 September 21, 2006http:// A Simple Type System Types ::= base types | t type variables |   -->  2 Function types Type Environments TE ::= Identifiers --> Types

L05-13 September 21, 2006http:// Type Inference Issues What does it mean for two types  a   and  b to be equal? –Structural Equality Suppose a = 1 -->  2  b = 3  -->  4 Is  a = b ? iff  1  3 and  2  4 Can two types be made equal by choosing appropriate substitutions for their type variables? –Robinson’s unification algorithm Suppose  a t 1 --> Bool  b Int  --> t 2 Are  a and b  unifiable ? if t 1 = Int and t 2 = Bool Suppose  a t 1 --> Bool  b Int  --> Int Are  a and b unifiable ? No

L05-14 September 21, 2006http:// Simple Type Substitutions needed to define type unification A substitution is a map S : Type Variables --> Types S = [  / t 1,...,  n  / t n ]  ’ = S  ’ is a Substitution Instance of  Example: S = [(t --> Bool) / t 1 ] S( t 1 --> t 1 ) = ? Types ::= base types (Int, Bool..) | t type variables |     -->   Function types Substitutions can be composed, i.e., S 2 S 1 Example: S 1 = [(t --> Bool) / t 1 ] ; S 2 = [Int / t] S 2 S 1 ( t 1 --> t 1 ) = ? ( t --> Bool) --> ( t --> Bool) ( Int --> Bool) --> ( Int --> Bool)

L05-15 September 21, 2006http:// Unification An essential subroutine for type inference def Unify( 1,  2 ) = case ( 1,  2 ) of ( 1, t 2 )= [ 1 / t 2 ] provided t 2  FV( 1 ) (t 1,  2 )= [ 2 / t 1 ] provided t 1  FV( 2 ) ( 1,  2 )= if (eq?  1  2 ) then [ ] else fail ( 11 --> 12,  21 --> 22 ) = Unify( 1,  2 ) tries to unify  1 and  2 and returns a substitution if successful Does the order matter? letS 1 =Unify( 11,  21 ) S 2 =Unify(S 1 ( 12 ), S 1 ( 22 )) in S 2 S 1 otherwise= fail

L05-16 September 21, 2006http:// Type Inference Rules Typing: TE |--e :  Suppose we want to assert (prove) that given some type environment TE, the expression (e 1 e 2 ) has the type  ’. Then it must be the case that the same TE implies that e 1 has type --> ’ and e 2 has the type . Such an inference rule can be written as: (App) TE ├ e 1 : TE ├ e 2 :  TE ├ (e 1 e 2 ) :  ’ --> ’ 

L05-17 September 21, 2006http:// Simple Type Inference Rules Typing: TE ├ e :  (App) TE ├ e 1 : --> ’ TE ├ e 2 :   TE ├ (e 1 e 2 ) :  ’ (Abs) TE ├ x.e :--> ’ (Var) TE ├ x :  (Const) TE ├ c :  (Let) TE ├ (let x = e 1 in e 2 ) :  ’ TE + {x : } ├ e :  ’ (x : )TE typeof(c) =  TE+{x:} ├ e 1 : TE+{x:} ├ e 2 : ’

L05-18 September 21, 2006http:// Inference Algorithm W(TE, e) returns (S,) such that S (TE) |-- e :  The type environment TE records the most general type of each identifier while the substitution S records the changes in the type variables Def W(TE, e) = Case e of x =... x.e =... (e 1 e 2 ) =... let x = e 1 in e 2 =...

L05-19 September 21, 2006http:// Inference Algorithm (cont.) Def W(TE, e)= Case e of x = if (x   Dom(TE)) then Fail else let = TE(x); in ({},  ) x.e = let (S 1,  1 ) = W(TE + { x : u }, e); in (S 1, S 1 (u) -->  1 ) (e 1 e 2 ) = let x = e 1 in e 2 = u’s represent new type variables let(S 1,  1 ) = W(TE, e 1 ) (S 2,  2 ) = W(S 1 (TE), e 2 ) S 3 = Unify(S 2 ( 1 ),  2 --> u); in (S 3 S 2 S 1, S 3 (u)) let (S 1,  1 ) = W(TE + {x : u}, e 1 ); S 2 = Unify(S 1 (u),  1 ); (S 3,  2 ) = W(S 2 S 1 (TE) + {x :   }, e 2 ); in (S 3 S 2 S 1,  2 )

L05-20 September 21, 2006http:// Type Inference Let fact = n.if (n == 0) then 1 else n * fact (n-1) In fact

L05-21 September 21, 2006http:// Inferring Polymorphic Types let id = x. x in... (id True)... (id 1)...