Type Checking and Type Inference

Size: px
Start display at page:

Download "Type Checking and Type Inference"

Transcription

1 Type Checking and Type Inference Principles of Programming Languages CSE Types in Programming Languages 2 Static Type Checking 3 Polymorphic Type Inference Version: :20: /08/25 Compiled at 18:31 on 2014/12/01 Programming Languages Type Checking and Inference CSE / 41

2 Use of datatype information The set of possible values denoted by a name or kept in a cell in store is denoted by a data type. Common data types include integers, floating point or real numbers, Booleans, and structured data. The type information is used for many purposes: Data representation: Type determines how data is represented (e.g. fixed precision integers; floating point for reals) Operation selection: Type information is used to select which among a set of overloaded operators is applied. (e.g. in x+y, a.f(b)). Compile-time error detection: Type information is used to check for operations on inappropriate data. Programming Languages Type Checking and Inference CSE / 41

3 Languages and Datatypes (1) Dynamically-typed languages: The datatype information is known and tracked only at run-time. So, data representation, operation selection, and error detection can all be performed only at run-time. Languages such as Scheme, Prolog, Python, and Javascript are all dynamically typed. Statically-typed languages: The datatype information is tracked and checked at compile-time, before a program s execution. Data representation is determined at compile time. Much of operation selection and error detection can be performed at compile-time as well. Languages such as SML, C, and Pascal are statically typed. Java is considered statically-typed as well even though operation selection and some of error detection is performed at run-time. Programming Languages Type Checking and Inference CSE / 41

4 Languages and Datatypes (2) Strongly-typed languages: The datatype information is used to ensure error-free operations. Languages such as SML, Java, Scheme, Prolog, Python, and Ruby are all strongly typed. Languages such as C, C++, and Perl are not strongly typed (aka weakly typed). Common confusion: static typing vs. strong typing. Statically typed Dynamically typed Strongly typed SML, Java Scheme, Prolog, Python Weakly typed C, C++ Perl Static/dynamic only refers to when type information is known and tracked. Strong/weak refers to whether type information is always tracked and whether it is used for operation selection/error detection. Programming Languages Type Checking and Inference CSE / 41

5 Why is C weakly typed? Use of type casts on pointers (e.g. a pointer to void can be cast to be a pointer to int). Use of unions: overlapping memory areas used for different types of values. Lack of memory safety (array bounds, dangling references to automatic variables). Programming Languages Type Checking and Inference CSE / 41

6 Static Type Checking A procedure that, without running the program, determines the types of expressions and checks for improper operations. We generally use this terminology for checking datatypes without considering the control flow of the program (aka flow-insensitive). Type-checking techniques are used for more complex types, e.g. non-null references, which may use control flow information (e.g. type of a variable at a specific statement in the program). Static type checking can be (and is often) specified using inference rules. Programming Languages Type Checking and Inference CSE / 41

7 Type Checking Given an expression e and a type t, is the expression well-typed w.r.t. t? e.g. is x+y:int well typed?... depends on the types of x and y Type environment Γ: a function that associates a type with each name in the program. e.g. {x int, y int}. Type judgement Γ e : t Under type environment Γ the expression e has type t. Type inference: given an expression e, is there a type t such that e is well-typed w.r.t. t? Programming Languages Type Checking and Inference CSE / 41

8 Type Expressions We can construct expressions over types analogous to how we construct arithmetic and boolean expressions over numeric and boolean constants and variables. Type expressions can be: Type names (e.g. int, bool, float) t 1 t 2 where t 1, t 2 are type exprs. t 1 t 2 where t 1, t 2 are type exprs. list of t where t is a type expr. ref t where t is a type expr.... Type variables (e.g. α, α, β,...) Programming Languages Type Checking and Inference CSE / 41

9 Example Expression Language We will use a small language of expressions for formally defining a type system and the corresponding type checking and inference procedures. As usual, we will start with a trivial language and add language constructs as we proceed. Ultimately, this language will look (i.e. be syntactically) and behave (i.e. be semantically) similar to OCAML. Programming Languages Type Checking and Inference CSE / 41

10 Constants Boolean Constants: true, false, represented in abstract syntax as True and False, resp. Integer Constants: usual, represented in abstract syntax as Int(i) Floating Point Constants: usual, represented in abstract syntax as Float(x) Unit Constant: written in concrete syntax as () and represented in abstract syntax as Unit. Programming Languages Type Checking and Inference CSE / 41

11 Type Rules Constants Int Float Bool t Bool f Unit Γ Int(i) : int Γ Float(x) : float Γ True : bool Γ False : bool Γ Unit : unit Each type judgement of the form Γ e : t associates a type t with expression e in type environment Γ. The inference of types for constants, as specified above, is trivial. Unsurprising fact: the type environment is not used to infer types of constants. Programming Languages Type Checking and Inference CSE / 41

12 Boolean Operators And(e 1, e 2 ), Or(e 1, e 2 ), Not(e 1 ) where e 1 and e 2 are expressions. Note that concrete syntax may use an infix notation for these operators. If(e 1, e 2, e 3 ), where e 1, e 2 and e 3 are expressions. In concrete syntax, these expressions may be written as: if e 1 then e 2 else e 3 Programming Languages Type Checking and Inference CSE / 41

13 Type Rules Boolean Operators And Or Not If Γ e 1 : bool Γ e 2 : bool Γ And(e 1, e 2 ) : bool Γ e 1 : bool Γ e 2 : bool Γ Or(e 1, e 2 ) : bool Γ e 1 : bool Γ Not(e 1 ) : bool Γ e 1 : bool Γ e 2 : t Γ e 3 : t Γ If(e 1, e 2, e 3 ) : t Note the use of t in the If rule: The condition in if must be a bool The then- and else- expressions must be of same (but arbitrary) type t. The whole expression will then have type t. Programming Languages Type Checking and Inference CSE / 41

14 Arithmetic Operators Add(e 1, e 2 ), Sub(e 1, e 2 ),... where e 1 and e 2 are expressions. Comparison operations such as Equal(e 1, e 2 ), Less(e 1, e 2 ),... where e 1 and e 2 are expressions. Note that concrete syntax may use an infix notation for these operators. Programming Languages Type Checking and Inference CSE / 41

15 Type Rules Arithmetic Operators Add 1 Add 2 Add 3 Add 4 Γ e 1 : int Γ e 2 : int Γ Add(e 1, e 2 ) : int Γ e 1 : int Γ e 2 : float Γ Add(e 1, e 2 ) : float Γ e 1 : float Γ e 2 : int Γ Add(e 1, e 2 ) : float Γ e 1 : float Γ e 2 : float Γ Add(e 1, e 2 ) : float The type rules explicitly list the cases when the built-in operators are overloaded. Type rules for other arithmetic operators are similar. Programming Languages Type Checking and Inference CSE / 41

16 Let Expressions We now add the ability to give names to expressions and use them elsewhere, as in: let x = 2 in x+3 Concrete syntax: let v = e 1 in e 2 Abstract syntax: let(v, e 1, e 2 ), where v is an identifier, and e 1 and e 2 are expressions. Programming Languages Type Checking and Inference CSE / 41

17 Type Rules Let Expressions Var Let (x t) Γ Γ Id(x) : t Γ e 1 : t 1 (x t 1 ) Γ e 2 : t Γ Let(x, e 1, e 2 ) : t The Var rule uses the type environment to determine the type of an identifier. In the Let rule, the type of the entire expression is the same as the type of the inner expression e 2 since e 2 may have x free, its type environment needs to specify the type of x and that type is same as the type of e 1. Programming Languages Type Checking and Inference CSE / 41

18 Functions Following the lambda calculus: Abstraction for defining new functions. Concrete syntax: fun x -> e Abstract syntax: fun(x, e), where x is an identifier and e is an expression. Application for calling functions. Concrete syntax: (e 1 e 2 ) Abstract syntax: apply(e 1, e 2 ), where e 1 and e 2 are expressions. Programming Languages Type Checking and Inference CSE / 41

19 Type Rules Functions Fun App (x t 1 ) Γ e : t 2 Γ Fun(x, e) : t 1 t 2 Γ e 1 : t 2 t Γ e 2 : t 2 Γ Apply(e 1, e 2 ) : t The Fun rule augments the environment with the formal parameter s type; and uses this environment to determine the type of e. According to the App rule, the first expression must be a function (of type t 1 t 2 ) and the second expression s type must match that of the function s parameter. Programming Languages Type Checking and Inference CSE / 41

20 Type Rules Functions(2) Fun App (x t 1 ) Γ e : t 2 Γ Fun(x, e) : t 1 t 2 Γ e 1 : t 2 t Γ e 2 : t 2 Γ Apply(e 1, e 2 ) : t In Fun(x, e), the type of x is not specified, so t 1 can be any type Programming Languages Type Checking and Inference CSE / 41

21 Type Rules Functions(2) Fun App (x t 1 ) Γ e : t 2 Γ Fun(x, e) : t 1 t 2 Γ e 1 : t 2 t Γ e 2 : t 2 Γ Apply(e 1, e 2 ) : t In Fun(x, e), the type of x is not specified, so t 1 can be any type... as long as, for that t 1, e has a valid type. Programming Languages Type Checking and Inference CSE / 41

22 Type Rules Functions(2) Fun App (x t 1 ) Γ e : t 2 Γ Fun(x, e) : t 1 t 2 Γ e 1 : t 2 t Γ e 2 : t 2 Γ Apply(e 1, e 2 ) : t In Fun(x, e), the type of x is not specified, so t 1 can be any type... as long as, for that t 1, e has a valid type. As a consequence, Fun(x, e) may not have a unique type! Programming Languages Type Checking and Inference CSE / 41

23 Type Rules Functions(2) Fun App (x t 1 ) Γ e : t 2 Γ Fun(x, e) : t 1 t 2 Γ e 1 : t 2 t Γ e 2 : t 2 Γ Apply(e 1, e 2 ) : t In Fun(x, e), the type of x is not specified, so t 1 can be any type... as long as, for that t 1, e has a valid type. As a consequence, Fun(x, e) may not have a unique type! E.g., Fun(x, Id(x)) has type int int, float float, bool bool, int int int int,... Programming Languages Type Checking and Inference CSE / 41

24 Type Rules Functions(2) Fun App (x t 1 ) Γ e : t 2 Γ Fun(x, e) : t 1 t 2 Γ e 1 : t 2 t Γ e 2 : t 2 Γ Apply(e 1, e 2 ) : t In Fun(x, e), the type of x is not specified, so t 1 can be any type... as long as, for that t 1, e has a valid type. As a consequence, Fun(x, e) may not have a unique type! E.g., Fun(x, Id(x)) has type int int, float float, bool bool, int int int int,... In ML, we have variables ranging over types (aka type variables). This lets us define the most general type of an expression, called its principal type. Programming Languages Type Checking and Inference CSE / 41

25 Pairs As in C, pair datatypes are added with: Pair constructor (concrete: (e 1, e 2 ); abstract: Pair(e 1, e 2 )). Fst and Snd access operators (concrete: e.1, and e.2, respectively; abstract: Fst(e) and Snd(e)). Programming Languages Type Checking and Inference CSE / 41

26 Type Rules Datatypes: Pairs Pair Fst Snd Γ e 1 : t 1 Γ e 2 : t 2 Γ Pair(e 1, e 2 ) : t 1 t 2 Γ e : t 1 t 2 Γ Fst(e) : t 1 Γ e : t 1 t 2 Γ Snd(e) : t 2 The Pair rule judges the type of the constructed pair using the types of its components. The Fst and Snd rules apply only when the operand is of a pair type; the rules then specify which component of the pair type to extract. Programming Languages Type Checking and Inference CSE / 41

27 Lists Similar to pairs, lists can be added with: Cons and Nil constructors (concrete: e 1 ::e 2, and [], respectively; abstract: Cons(e 1, e 2 ), and Nil). Hd and Tl access operators (concrete: hd(e), and tl(e), respectively; abstract: Hd(e) and Tl(e)). isnil operator to test whether a list is nil or not. Programming Languages Type Checking and Inference CSE / 41

28 Type Rules Lists Cons Nil Hd Tl IsNil Γ e 1 : t Γ e 2 : list of t Γ Cons(e 1, e 2 ) : list of t Γ Nil : list of t Γ e : list of t Γ Hd(e) : t Γ e : list of t Γ Tl(e) : list of t Γ e : list of t Γ IsNil(e) : bool Programming Languages Type Checking and Inference CSE / 41

29 References Finally, we add OCAML-style references for writing programs with side effects. Ref(e) to create new references (initialized to the value of e) Deref(e) to dereference an existing reference (concrete syntax:!e) Assign(e 1, e 2 ) to assign the value of one expression (e 2 ) to the location supplied by another expression (e 1 ). Concrete syntax: e 1 := e 2 Programming Languages Type Checking and Inference CSE / 41

30 Type Rules References Ref Deref Assign Γ e 1 : t 1 Γ Ref(e 1 ) : ref t 1 Γ e 1 : ref t 1 Γ Deref(e 1 ) : t 1 Γ e 1 : ref t 1 Γ e 2 : t 1 Γ Assign(e 1, e 2 ) : unit The Ref rule judges the type of the constructed cell using the type of the stored value. The Deref rule does the converse: determines the type of a value in a cell based on the type of its reference. The Assign requires the l.h.s. to be a reference type, and r.h.s. to be a value of the same type. Programming Languages Type Checking and Inference CSE / 41

31 Type Inference Infer the type of an expression given the types of the subexpressions. Consider the expression fst(1,2): Int(1) Int(2) Pair Fst Programming Languages Type Checking and Inference CSE / 41

32 Type Inference Infer the type of an expression given the types of the subexpressions. Consider the expression fst(1,2): Int(1) :int Int(2) :int Pair Fst Type of Int(1) and Int(2) are int (from rules for constants) Programming Languages Type Checking and Inference CSE / 41

33 Type Inference Infer the type of an expression given the types of the subexpressions. Consider the expression fst(1,2): Int(1) :int Int(2) :int Pair :int * int Fst Type of Int(1) and Int(2) are int (from rules for constants) Type of Pair(e 1, e 2 ) is t 1 t 2 where t i is the type of e i (from Pair rule) Programming Languages Type Checking and Inference CSE / 41

34 Type Inference Infer the type of an expression given the types of the subexpressions. Consider the expression fst(1,2): Int(1) :int Int(2) :int Pair :int * int Fst :int Type of Int(1) and Int(2) are int (from rules for constants) Type of Pair(e 1, e 2 ) is t 1 t 2 where t i is the type of e i (from Pair rule) Type of Fst(e) is t 1 if e is of type t 1 t 2 (from Fst rule) Programming Languages Type Checking and Inference CSE / 41

35 Type Reconstruction Introduction Can we infer the types of the subexpressions (or at least, some constraints on their types) assuming the full expression is well-typed? Consider the expression fst(x,y): Id(x) Id(y) Pair Fst Programming Languages Type Checking and Inference CSE / 41

36 Type Reconstruction Introduction Can we infer the types of the subexpressions (or at least, some constraints on their types) assuming the full expression is well-typed? Consider the expression fst(x,y): Id(x) : α Id(y) : β Pair Fst Types of x and y are assumed to be α and β. (i.e., some unknown type, but not necessarily the same). Programming Languages Type Checking and Inference CSE / 41

37 Type Reconstruction Introduction Can we infer the types of the subexpressions (or at least, some constraints on their types) assuming the full expression is well-typed? Consider the expression fst(x,y): Id(x) : α Id(y) : β Pair : α β Fst Types of x and y are assumed to be α and β. (i.e., some unknown type, but not necessarily the same). Type of Pair(e 1, e 2 ) is t 1 t 2 where t i is the type of e i (from Pair rule) Programming Languages Type Checking and Inference CSE / 41

38 Type Reconstruction Introduction Can we infer the types of the subexpressions (or at least, some constraints on their types) assuming the full expression is well-typed? Consider the expression fst(x,y): Id(x) : α Id(y) : β Pair : α β Fst : α Types of x and y are assumed to be α and β. (i.e., some unknown type, but not necessarily the same). Type of Pair(e 1, e 2 ) is t 1 t 2 where t i is the type of e i (from Pair rule) Type of Fst(e) is t 1 if e is of type t 1 t 2 (from Fst rule) Programming Languages Type Checking and Inference CSE / 41

39 Type Reconstruction Example 2 Can we infer the types of the subexpressions (or at least, some constraints on their types) assuming the full expression is well-typed? Consider fun x -> fun y -> x: Id(x) y Fun x Fun Programming Languages Type Checking and Inference CSE / 41

40 Type Reconstruction Example 2 Can we infer the types of the subexpressions (or at least, some constraints on their types) assuming the full expression is well-typed? Consider fun x -> fun y -> x: Id(x) y Fun x : α Fun Types of x and y are assumed to be α and β. (i.e., some unknown type, but not necessarily the same). Programming Languages Type Checking and Inference CSE / 41

41 Type Reconstruction Example 2 Can we infer the types of the subexpressions (or at least, some constraints on their types) assuming the full expression is well-typed? Consider fun x -> fun y -> x: Id(x) y : β Fun x : α Fun Types of x and y are assumed to be α and β. (i.e., some unknown type, but not necessarily the same). Programming Languages Type Checking and Inference CSE / 41

42 Type Reconstruction Example 2 Can we infer the types of the subexpressions (or at least, some constraints on their types) assuming the full expression is well-typed? Consider fun x -> fun y -> x: Id(x) : α y : β Fun x : α Fun Types of x and y are assumed to be α and β. (i.e., some unknown type, but not necessarily the same). Type of innermost x is same as its assumption (Id rule). Programming Languages Type Checking and Inference CSE / 41

43 Type Reconstruction Example 2 Can we infer the types of the subexpressions (or at least, some constraints on their types) assuming the full expression is well-typed? Consider fun x -> fun y -> x: Id(x) : α y : β Fun : β α x : α Fun Types of x and y are assumed to be α and β. (i.e., some unknown type, but not necessarily the same). Type of innermost x is same as its assumption (Id rule). Type of (fun y ->e) is t 1 t 2 if y is of type t 2 (from Fun rule) Programming Languages Type Checking and Inference CSE / 41

44 Type Reconstruction Example 2 Can we infer the types of the subexpressions (or at least, some constraints on their types) assuming the full expression is well-typed? Consider fun x -> fun y -> x: Id(x) : α y : β Fun : β α x : α Fun : α β α Types of x and y are assumed to be α and β. (i.e., some unknown type, but not necessarily the same). Type of innermost x is same as its assumption (Id rule). Type of (fun y ->e) is t 1 t 2 if y is of type t 2 (from Fun rule) Type of (fun x ->e) is t 1 t 2 if x is of type t 2 (from Fun rule) Programming Languages Type Checking and Inference CSE / 41

45 Type Reconstruction Example 3 Consider fun x -> if fst(x) then 1 else snd(x) Since the entire expression is of the form fun x -> e, its type is α β, where x : α and e : β. Let s determine the type of e under the environment {x α}. Since e is an if expression, we have the following: 1 fst(x) : bool 2 1: β 3 snd(x): β From 1: β, we get β = int. From x:α and fst(x):bool, and from the rule Fst, we get α = bool γ. From x:α and snd(x):int, and from the rule Snd, we get, α = δ int. From the last two steps, we get α = bool int and hence the original expressions type is: bool int int Programming Languages Type Checking and Inference CSE / 41

46 Type Unification Motivation In the previous example, we initially assigned types denoted by type variables to certain identifiers. As type inference proceeded, we discovered constraints on those type variables, e.g. α = bool γ. The constraints are all equality constraints, hence if α 1 = α 2 and α 2 = α 3 then α 1 = α 3. From bool γ = δ int, we get γ = int and δ = bool. More formally, the constraints are solved using unification, defined next. Programming Languages Type Checking and Inference CSE / 41

47 Type Unification Two type expressions t 1, t 2 are said to unify iff there is some substitution θ to the type variables in t 1 and t 2 such that t 1 θ = t 2 θ. Examples: A type variable α unifies with any type expression t that does not contain α. α and list of β unify, setting α = list of β. list of α and list of β unify, setting α = β. int and list of β do not unify. α 1 α 2 and list of β do not unify. α and list of α do not unify. Type unification is the core of Hindley-Milner type inference. Programming Languages Type Checking and Inference CSE / 41

48 Polymorphic Types The type rules can be used to find out the constraints on Γ and t such that Γ e : t is derivable from the empty type judgement. We will illustrate the use of these rules informally: Find the type of fun x -> x Programming Languages Type Checking and Inference CSE / 41

49 Polymorphic Types The type rules can be used to find out the constraints on Γ and t such that Γ e : t is derivable from the empty type judgement. We will illustrate the use of these rules informally: Find the type of fun x -> x Since it is a function definition, we represent the type of its formal parameter to be some type variable, say α. Programming Languages Type Checking and Inference CSE / 41

50 Polymorphic Types The type rules can be used to find out the constraints on Γ and t such that Γ e : t is derivable from the empty type judgement. We will illustrate the use of these rules informally: Find the type of fun x -> x Since it is a function definition, we represent the type of its formal parameter to be some type variable, say α. We evaluate the type of the function s body assuming x is of type α. Programming Languages Type Checking and Inference CSE / 41

51 Polymorphic Types The type rules can be used to find out the constraints on Γ and t such that Γ e : t is derivable from the empty type judgement. We will illustrate the use of these rules informally: Find the type of fun x -> x Since it is a function definition, we represent the type of its formal parameter to be some type variable, say α. We evaluate the type of the function s body assuming x is of type α. The type of its body is then determined to be α as well. Programming Languages Type Checking and Inference CSE / 41

52 Polymorphic Types The type rules can be used to find out the constraints on Γ and t such that Γ e : t is derivable from the empty type judgement. We will illustrate the use of these rules informally: Find the type of fun x -> x Since it is a function definition, we represent the type of its formal parameter to be some type variable, say α. We evaluate the type of the function s body assuming x is of type α. The type of its body is then determined to be α as well. Som the function s type is α α. Programming Languages Type Checking and Inference CSE / 41

53 Polymorphic Types (contd.) A function is said to be polymorphic if it has more than one type. Since fun x -> x is of type α α for any type α, the function is polymorphic. This form of polymorphism is called parametric polymorphism: f is polymorphic w.r.t. type parameter α. The type parameter for fun x -> x is implicit, and so this is also called implicit parametric polymorphism. Consider the following OCAML definition of a tree data structure: type a tree = Empty Node of a * a tree * a tree;; This defines trees for different node types, and hence is polymorphic. Since the node types is given as a parameter to the datatype definition, this is called implicit parametric polymorphism. Programming Languages Type Checking and Inference CSE / 41

54 Unification and parametric polymorphism Consider fun x -> if (fst(x)) then 1 else (x,1) If the type of the expression is α β, we get: 1 1 : β, which means β = int, and 2 (x,1) : β, which means β = α int. int and α int do not unify! Hence there is no type for the expression. The expression is not well-typed. Programming Languages Type Checking and Inference CSE / 41

55 Omega Consider fun x -> (x x) This is the first part of the ω-combinator introduced in the lambda calculus. If the type of the expression is α β, we get 1 x : α, and 2 (x x) : β Rule App says that if (e 1 e 2 ) : β then e 1 : γ β and e 2 : γ. Hence we also have the following constraints: 1 x : γ β 2 x : γ γ and γ β do not unify! Hence ω is not well-typed. By a similar argument, we can show that the Y-combinator is also not well-typed. Programming Languages Type Checking and Inference CSE / 41

56 ML s Type Inference ML s type inference proceeds very similar to our definition of types, with two main changes. 1 ML s let is polymorphic: every use of a let-defined identifier may have a different, incompatible type. Our rules have (implicitly) demanded that all uses of an identifier have the same consistent set of types. 2 ML has user-defined types and uses pattern matching in function definitions. Type checking for these can also be formalized as inference rules, but following them strictly becomes tedious, so we ll explain these only with examples. Programming Languages Type Checking and Inference CSE / 41

57 ML s Type Inference Example 1 Find the type of g defined as let g y = match y with (x::xs) -> x 1 We know g is a function so has type α β. Programming Languages Type Checking and Inference CSE / 41

58 ML s Type Inference Example 1 Find the type of g defined as let g y = match y with (x::xs) -> x 1 We know g is a function so has type α β. 2 Hence y is of type α. Programming Languages Type Checking and Inference CSE / 41

59 ML s Type Inference Example 1 Find the type of g defined as let g y = match y with (x::xs) -> x 1 We know g is a function so has type α β. 2 Hence y is of type α. 3 For match, the pattern (x::xs) must have type α, same as y. Programming Languages Type Checking and Inference CSE / 41

60 ML s Type Inference Example 1 Find the type of g defined as let g y = match y with (x::xs) -> x 1 We know g is a function so has type α β. 2 Hence y is of type α. 3 For match, the pattern (x::xs) must have type α, same as y. 4 From the definition of :: we now infer that α = γ list, x is of type γ and xs is of type γ list. Programming Languages Type Checking and Inference CSE / 41

61 ML s Type Inference Example 1 Find the type of g defined as let g y = match y with (x::xs) -> x 1 We know g is a function so has type α β. 2 Hence y is of type α. 3 For match, the pattern (x::xs) must have type α, same as y. 4 From the definition of :: we now infer that α = γ list, x is of type γ and xs is of type γ list. 5 The return type of g must be same as the type of x; hence from (1) and (3), we know β = γ. Programming Languages Type Checking and Inference CSE / 41

62 ML s Type Inference Example 1 Find the type of g defined as let g y = match y with (x::xs) -> x 1 We know g is a function so has type α β. 2 Hence y is of type α. 3 For match, the pattern (x::xs) must have type α, same as y. 4 From the definition of :: we now infer that α = γ list, x is of type γ and xs is of type γ list. 5 The return type of g must be same as the type of x; hence from (1) and (3), we know β = γ. 6 Hence g has type γ list γ. Programming Languages Type Checking and Inference CSE / 41

63 ML s Type Inference Example 2 Find the type of len defined by let rec len y = match y with [ ] > 0 (x::xs) > 1 + (len xs) 1 We know len is a function so has type α β. Programming Languages Type Checking and Inference CSE / 41

64 ML s Type Inference Example 2 Find the type of len defined by let rec len y = match y with [ ] > 0 (x::xs) > 1 + (len xs) 1 We know len is a function so has type α β. 2 The expression [] (l.h.s. pattern of first match case) must have type α; from the definition of [] we now infer that α = γ list. Programming Languages Type Checking and Inference CSE / 41

65 ML s Type Inference Example 2 Find the type of len defined by let rec len y = match y with [ ] > 0 (x::xs) > 1 + (len xs) 1 We know len is a function so has type α β. 2 The expression [] (l.h.s. pattern of first match case) must have type α; from the definition of [] we now infer that α = γ list. 3 The return type of len must be same as the type of 0; hence from (1), we know β = int. Programming Languages Type Checking and Inference CSE / 41

66 ML s Type Inference Example 2 Find the type of len defined by let rec len y = match y with [ ] > 0 (x::xs) > 1 + (len xs) 1 We know len is a function so has type α β. 2 The expression [] (l.h.s. pattern of first match case) must have type α; from the definition of [] we now infer that α = γ list. 3 The return type of len must be same as the type of 0; hence from (1), we know β = int. 4 From the second match case, if α = γ list then x must be of type γ and xs must be of type γ list. Programming Languages Type Checking and Inference CSE / 41

67 ML s Type Inference Example 2 Find the type of len defined by let rec len y = match y with [ ] > 0 (x::xs) > 1 + (len xs) 1 We know len is a function so has type α β. 2 The expression [] (l.h.s. pattern of first match case) must have type α; from the definition of [] we now infer that α = γ list. 3 The return type of len must be same as the type of 0; hence from (1), we know β = int. 4 From the second match case, if α = γ list then x must be of type γ and xs must be of type γ list. 5 From (3), we know the rhs of second eqn must be of type int. Programming Languages Type Checking and Inference CSE / 41

68 ML s Type Inference Example 2 Find the type of len defined by let rec len y = match y with [ ] > 0 (x::xs) > 1 + (len xs) 1 We know len is a function so has type α β. 2 The expression [] (l.h.s. pattern of first match case) must have type α; from the definition of [] we now infer that α = γ list. 3 The return type of len must be same as the type of 0; hence from (1), we know β = int. 4 From the second match case, if α = γ list then x must be of type γ and xs must be of type γ list. 5 From (3), we know the rhs of second eqn must be of type int. 6 len is of type γ list int. Since xs is of type γ list, len xs is int. Since 1 is of type int, 1 + len xs is of type int as required by (5). Hence len is well typed and has type γ list int. Programming Languages Type Checking and Inference CSE / 41

69 ML s Type Inference (Example 2-a) Find the type of len2a defined by let rec len2a y = match y with [ ] > 0 (x::xs) > (len2a xs) 1 5. The first 5 steps are the same as in the previous example. 6. We know that len2a is of type γ list int. Since xs is of type γ list, len2a xs is of type int. Since 1.0 is of type float, 1 + len2a xs is not well typed. Hence len2a is not well typed. Programming Languages Type Checking and Inference CSE / 41

70 ML s Type Inference (Example 2-b) Find the type of len2a defined by let rec len2b y = match y with [ ] > 0 (x::xs) > 1 + (len2b x) 1 5. The first 5 steps are the same as in the previous example. 6. We know that len2b is of type γ list int. Since x is of type γ for len2b x to be well typed, γ = γ list. Since there is no type γ such that γ = γ list (see Type Unification), len2b x is not well typed. Hence len2b is not well typed. Programming Languages Type Checking and Inference CSE / 41

Types-2. Polymorphism

Types-2. Polymorphism Types-2 Polymorphism Type reconstruction (type inference) for a simple PL Typing functions Coercion, conversion, reconstruction Rich area of programming language research as people try to provide safety

More information

Types, Type Inference and Unification

Types, Type Inference and Unification Types, Type Inference and Unification Mooly Sagiv Slides by Kathleen Fisher and John Mitchell Cornell CS 6110 Summary (Functional Programming) Lambda Calculus Basic ML Advanced ML: Modules, References,

More information

Lecture #23: Conversion and Type Inference

Lecture #23: Conversion and Type Inference Lecture #23: Conversion and Type Inference Administrivia. Due date for Project #2 moved to midnight tonight. Midterm mean 20, median 21 (my expectation: 17.5). Last modified: Fri Oct 20 10:46:40 2006 CS164:

More information

Types and Type Inference

Types and Type Inference CS 242 2012 Types and Type Inference Notes modified from John Mitchell and Kathleen Fisher Reading: Concepts in Programming Languages, Revised Chapter 6 - handout on Web!! Outline General discussion of

More information

Types and Type Inference

Types and Type Inference Types and Type Inference Mooly Sagiv Slides by Kathleen Fisher and John Mitchell Reading: Concepts in Programming Languages, Revised Chapter 6 - handout on the course homepage Outline General discussion

More information

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = "Hello";

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = Hello; Lecture #23: Conversion and Type Inference Administrivia. Due date for Project #2 moved to midnight tonight. Midterm mean 20, median 21 (my expectation: 17.5). In Java, this is legal: Object x = "Hello";

More information

G Programming Languages Spring 2010 Lecture 6. Robert Grimm, New York University

G Programming Languages Spring 2010 Lecture 6. Robert Grimm, New York University G22.2110-001 Programming Languages Spring 2010 Lecture 6 Robert Grimm, New York University 1 Review Last week Function Languages Lambda Calculus SCHEME review 2 Outline Promises, promises, promises Types,

More information

CS558 Programming Languages

CS558 Programming Languages CS558 Programming Languages Winter 2017 Lecture 7b Andrew Tolmach Portland State University 1994-2017 Values and Types We divide the universe of values according to types A type is a set of values and

More information

Pierce Ch. 3, 8, 11, 15. Type Systems

Pierce Ch. 3, 8, 11, 15. Type Systems Pierce Ch. 3, 8, 11, 15 Type Systems Goals Define the simple language of expressions A small subset of Lisp, with minor modifications Define the type system of this language Mathematical definition using

More information

Lecture #13: Type Inference and Unification. Typing In the Language ML. Type Inference. Doing Type Inference

Lecture #13: Type Inference and Unification. Typing In the Language ML. Type Inference. Doing Type Inference Lecture #13: Type Inference and Unification Typing In the Language ML Examples from the language ML: fun map f [] = [] map f (a :: y) = (f a) :: (map f y) fun reduce f init [] = init reduce f init (a ::

More information

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result. Every program uses data, either explicitly or implicitly to arrive at a result. Data in a program is collected into data structures, and is manipulated by algorithms. Algorithms + Data Structures = Programs

More information

Topics Covered Thus Far CMSC 330: Organization of Programming Languages

Topics Covered Thus Far CMSC 330: Organization of Programming Languages Topics Covered Thus Far CMSC 330: Organization of Programming Languages Names & Binding, Type Systems Programming languages Ruby Ocaml Lambda calculus Syntax specification Regular expressions Context free

More information

Overloading, Type Classes, and Algebraic Datatypes

Overloading, Type Classes, and Algebraic Datatypes Overloading, Type Classes, and Algebraic Datatypes Delivered by Michael Pellauer Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. September 28, 2006 September 28, 2006 http://www.csg.csail.mit.edu/6.827

More information

Data Types The ML Type System

Data Types The ML Type System 7 Data Types 7.2.4 The ML Type System The following is an ML version of the tail-recursive Fibonacci function introduced Fibonacci function in ML in Section 6.6.1: EXAMPLE 7.96 1. fun fib (n) = 2. let

More information

Programming Languages and Compilers (CS 421)

Programming Languages and Compilers (CS 421) Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC http://courses.engr.illinois.edu/cs421 Based in part on slides by Mattox Beckman, as updated by Vikram Adve and Gul Agha 10/3/17

More information

Generic polymorphism on steroids

Generic polymorphism on steroids Generic polymorphism on steroids or How to Solve the Expression Problem with Polymorphic Variants Claudio Sacerdoti Coen Dipartimento di Informatica Scienza e Ingegneria

More information

Programming Languages Lecture 14: Sum, Product, Recursive Types

Programming Languages Lecture 14: Sum, Product, Recursive Types CSE 230: Winter 200 Principles of Programming Languages Lecture 4: Sum, Product, Recursive Types The end is nigh HW 3 No HW 4 (= Final) Project (Meeting + Talk) Ranjit Jhala UC San Diego Recap Goal: Relate

More information

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages CMSC 330: Organization of Programming Languages Type Systems, Names and Binding CMSC 330 - Spring 2013 1 Topics Covered Thus Far! Programming languages Ruby OCaml! Syntax specification Regular expressions

More information

Programming Languages Lecture 15: Recursive Types & Subtyping

Programming Languages Lecture 15: Recursive Types & Subtyping CSE 230: Winter 2008 Principles of Programming Languages Lecture 15: Recursive Types & Subtyping Ranjit Jhala UC San Diego News? Formalize first-order type systems Simple types (integers and booleans)

More information

Static Checking and Type Systems

Static Checking and Type Systems 1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007-2009 2 The Structure of our Compiler Revisited Character stream Lexical

More information

cs242 Kathleen Fisher Reading: Concepts in Programming Languages, Chapter 6 Thanks to John Mitchell for some of these slides.

cs242 Kathleen Fisher Reading: Concepts in Programming Languages, Chapter 6 Thanks to John Mitchell for some of these slides. cs242 Kathleen Fisher Reading: Concepts in Programming Languages, Chapter 6 Thanks to John Mitchell for some of these slides. We are looking for homework graders. If you are interested, send mail to cs242cs.stanford.edu

More information

Outline. Introduction Concepts and terminology The case for static typing. Implementing a static type system Basic typing relations Adding context

Outline. Introduction Concepts and terminology The case for static typing. Implementing a static type system Basic typing relations Adding context Types 1 / 15 Outline Introduction Concepts and terminology The case for static typing Implementing a static type system Basic typing relations Adding context 2 / 15 Types and type errors Type: a set of

More information

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml COSE212: Programming Languages Lecture 3 Functional Programming in OCaml Hakjoo Oh 2017 Fall Hakjoo Oh COSE212 2017 Fall, Lecture 3 September 18, 2017 1 / 44 Why learn ML? Learning ML is a good way of

More information

CSCI-GA Scripting Languages

CSCI-GA Scripting Languages CSCI-GA.3033.003 Scripting Languages 12/02/2013 OCaml 1 Acknowledgement The material on these slides is based on notes provided by Dexter Kozen. 2 About OCaml A functional programming language All computation

More information

TYPE INFERENCE. François Pottier. The Programming Languages Mentoring ICFP August 30, 2015

TYPE INFERENCE. François Pottier. The Programming Languages Mentoring ICFP August 30, 2015 TYPE INFERENCE François Pottier The Programming Languages Mentoring Workshop @ ICFP August 30, 2015 What is type inference? What is the type of this OCaml function? let f verbose msg = if verbose then

More information

Type Systems, Type Inference, and Polymorphism

Type Systems, Type Inference, and Polymorphism 6 Type Systems, Type Inference, and Polymorphism Programming involves a wide range of computational constructs, such as data structures, functions, objects, communication channels, and threads of control.

More information

Polymorphic lambda calculus Princ. of Progr. Languages (and Extended ) The University of Birmingham. c Uday Reddy

Polymorphic lambda calculus Princ. of Progr. Languages (and Extended ) The University of Birmingham. c Uday Reddy 06-02552 Princ. of Progr. Languages (and Extended ) The University of Birmingham Spring Semester 2016-17 School of Computer Science c Uday Reddy2016-17 Handout 6: Polymorphic Type Systems 1. Polymorphic

More information

CS558 Programming Languages

CS558 Programming Languages CS558 Programming Languages Winter 2018 Lecture 7b Andrew Tolmach Portland State University 1994-2018 Dynamic Type Checking Static type checking offers the great advantage of catching errors early And

More information

CS558 Programming Languages

CS558 Programming Languages CS558 Programming Languages Fall 2017 Lecture 7b Andrew Tolmach Portland State University 1994-2017 Type Inference Some statically typed languages, like ML (and to a lesser extent Scala), offer alternative

More information

CVO103: Programming Languages. Lecture 5 Design and Implementation of PLs (1) Expressions

CVO103: Programming Languages. Lecture 5 Design and Implementation of PLs (1) Expressions CVO103: Programming Languages Lecture 5 Design and Implementation of PLs (1) Expressions Hakjoo Oh 2018 Spring Hakjoo Oh CVO103 2018 Spring, Lecture 5 April 3, 2018 1 / 23 Plan Part 1 (Preliminaries):

More information

Programming Languages Fall 2013

Programming Languages Fall 2013 Programming Languages Fall 2013 Lecture 2: types Prof. Liang Huang huang@qc.cs.cuny.edu Recap of Lecture 1 functional programming vs. imperative programming basic Haskell syntax function definition lazy

More information

Topic 9: Type Checking

Topic 9: Type Checking Recommended Exercises and Readings Topic 9: Type Checking From Haskell: The craft of functional programming (3 rd Ed.) Exercises: 13.17, 13.18, 13.19, 13.20, 13.21, 13.22 Readings: Chapter 13.5, 13.6 and

More information

Topic 9: Type Checking

Topic 9: Type Checking Topic 9: Type Checking 1 Recommended Exercises and Readings From Haskell: The craft of functional programming (3 rd Ed.) Exercises: 13.17, 13.18, 13.19, 13.20, 13.21, 13.22 Readings: Chapter 13.5, 13.6

More information

COS 320. Compiling Techniques

COS 320. Compiling Techniques Topic 5: Types COS 320 Compiling Techniques Princeton University Spring 2016 Lennart Beringer 1 Types: potential benefits (I) 2 For programmers: help to eliminate common programming mistakes, particularly

More information

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited CMSC 330: Organization of Programming Languages Type Systems, Names & Binding Topics Covered Thus Far Programming languages Syntax specification Regular expressions Context free grammars Implementation

More information

Agenda. CS301 Session 11. Common type constructors. Things we could add to Impcore. Discussion: midterm exam - take-home or inclass?

Agenda. CS301 Session 11. Common type constructors. Things we could add to Impcore. Discussion: midterm exam - take-home or inclass? Agenda CS301 Session 11 Discussion: midterm exam - take-home or inclass? Interlude: common type constructors Type soundness 1 2 Things we could add to Impcore Common type constructors Array is a type constructor,

More information

Mutable References. Chapter 1

Mutable References. Chapter 1 Chapter 1 Mutable References In the (typed or untyped) λ-calculus, or in pure functional languages, a variable is immutable in that once bound to a value as the result of a substitution, its contents never

More information

Principles of Programming Languages

Principles of Programming Languages Principles of Programming Languages Lesson 14 Type Checking Collaboration and Management Dana Fisman www.cs.bgu.ac.il/~ppl172 1 Type Checking We return to the issue of type safety we discussed informally,

More information

Functional Programming

Functional Programming Functional Programming COMS W4115 Prof. Stephen A. Edwards Spring 2003 Columbia University Department of Computer Science Original version by Prof. Simon Parsons Functional vs. Imperative Imperative programming

More information

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer. The Compiler So Far CSC 4181 Compiler Construction Scanner - Lexical analysis Detects inputs with illegal tokens e.g.: main 5 (); Parser - Syntactic analysis Detects inputs with ill-formed parse trees

More information

CS 565: Programming Languages. Spring 2008 Tu, Th: 16:30-17:45 Room LWSN 1106

CS 565: Programming Languages. Spring 2008 Tu, Th: 16:30-17:45 Room LWSN 1106 CS 565: Programming Languages Spring 2008 Tu, Th: 16:30-17:45 Room LWSN 1106 Administrivia Who am I? Course web page http://www.cs.purdue.edu/homes/peugster/cs565spring08/ Office hours By appointment Main

More information

CSE 307: Principles of Programming Languages

CSE 307: Principles of Programming Languages CSE 307: Principles of Programming Languages Variables and Constants R. Sekar 1 / 22 Topics 2 / 22 Variables and Constants Variables are stored in memory, whereas constants need not be. Value of variables

More information

Type Inference. Prof. Clarkson Fall Today s music: Cool, Calm, and Collected by The Rolling Stones

Type Inference. Prof. Clarkson Fall Today s music: Cool, Calm, and Collected by The Rolling Stones Type Inference Prof. Clarkson Fall 2016 Today s music: Cool, Calm, and Collected by The Rolling Stones Review Previously in 3110: Interpreters: ASTs, evaluation, parsing Formal syntax Formal semantics

More information

Harvard School of Engineering and Applied Sciences Computer Science 152

Harvard School of Engineering and Applied Sciences Computer Science 152 Harvard School of Engineering and Applied Sciences Computer Science 152 Lecture 17 Tuesday, March 30, 2010 1 Polymorph means many forms. Polymorphism is the ability of code to be used on values of different

More information

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics Recall Architecture of Compilers, Interpreters CMSC 330: Organization of Programming Languages Source Scanner Parser Static Analyzer Operational Semantics Intermediate Representation Front End Back End

More information

Type Systems. Seman&cs. CMPT 379: Compilers Instructor: Anoop Sarkar. anoopsarkar.github.io/compilers-class

Type Systems. Seman&cs. CMPT 379: Compilers Instructor: Anoop Sarkar. anoopsarkar.github.io/compilers-class Type Systems Seman&cs CMPT 379: Compilers Instructor: Anoop Sarkar anoopsarkar.github.io/compilers-class 1 Equality of types Main seman&c tasks involve liveness analysis and checking equality Equality

More information

Overview. Elements of Programming Languages. Another example. Consider the humble identity function

Overview. Elements of Programming Languages. Another example. Consider the humble identity function Overview Elements of Programming Languages Lecture 8: Polymorphism and type inference James Cheney University of Edinburgh October 21, 2016 This week and next week, we will cover di erent forms of abstraction

More information

CSE 505. Lecture #9. October 1, Lambda Calculus. Recursion and Fixed-points. Typed Lambda Calculi. Least Fixed Point

CSE 505. Lecture #9. October 1, Lambda Calculus. Recursion and Fixed-points. Typed Lambda Calculi. Least Fixed Point Lambda Calculus CSE 505 Lecture #9 October 1, 2012 Expr ::= Var λ Var. Expr (Expr Expr) Key Concepts: Bound and Free Occurrences Substitution, Reduction Rules: α, β, η Confluence and Unique Normal Form

More information

CSE 431S Type Checking. Washington University Spring 2013

CSE 431S Type Checking. Washington University Spring 2013 CSE 431S Type Checking Washington University Spring 2013 Type Checking When are types checked? Statically at compile time Compiler does type checking during compilation Ideally eliminate runtime checks

More information

Programming Languages, Summary CSC419; Odelia Schwartz

Programming Languages, Summary CSC419; Odelia Schwartz Programming Languages, Summary CSC419; Odelia Schwartz Chapter 1 Topics Reasons for Studying Concepts of Programming Languages Programming Domains Language Evaluation Criteria Influences on Language Design

More information

INF 212 ANALYSIS OF PROG. LANGS Type Systems. Instructors: Crista Lopes Copyright Instructors.

INF 212 ANALYSIS OF PROG. LANGS Type Systems. Instructors: Crista Lopes Copyright Instructors. INF 212 ANALYSIS OF PROG. LANGS Type Systems Instructors: Crista Lopes Copyright Instructors. What is a Data Type? A type is a collection of computational entities that share some common property Programming

More information

CS131 Typed Lambda Calculus Worksheet Due Thursday, April 19th

CS131 Typed Lambda Calculus Worksheet Due Thursday, April 19th CS131 Typed Lambda Calculus Worksheet Due Thursday, April 19th Name: CAS ID (e.g., abc01234@pomona.edu): I encourage you to collaborate. collaborations below. Please record your Each question is worth

More information

301AA - Advanced Programming [AP-2017]

301AA - Advanced Programming [AP-2017] 301AA - Advanced Programming [AP-2017] Lecturer: Andrea Corradini andrea@di.unipi.it Tutor: Lillo GalleBa galleba@di.unipi.it Department of Computer Science, Pisa Academic Year 2017/18 AP-2017-19: Type

More information

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

n   n Try tutorial on front page to get started! n   spring13/ n Stack Overflow! Announcements n Rainbow grades: HW1-6, Quiz1-5, Exam1 n Still grading: HW7, Quiz6, Exam2 Intro to Haskell n HW8 due today n HW9, Haskell, out tonight, due Nov. 16 th n Individual assignment n Start early!

More information

CSCI.6962/4962 Software Verification Fundamental Proof Methods in Computer Science (Arkoudas and Musser) Chapter p. 1/27

CSCI.6962/4962 Software Verification Fundamental Proof Methods in Computer Science (Arkoudas and Musser) Chapter p. 1/27 CSCI.6962/4962 Software Verification Fundamental Proof Methods in Computer Science (Arkoudas and Musser) Chapter 2.1-2.7 p. 1/27 CSCI.6962/4962 Software Verification Fundamental Proof Methods in Computer

More information

Lambda Calculus and Type Inference

Lambda Calculus and Type Inference Lambda Calculus and Type Inference Björn Lisper Dept. of Computer Science and Engineering Mälardalen University bjorn.lisper@mdh.se http://www.idt.mdh.se/ blr/ August 17, 2007 Lambda Calculus and Type

More information

Semantic Processing (Part 2)

Semantic Processing (Part 2) Semantic Processing (Part 2) All Projects Due: Fray 12-2-05, Noon Final: Monday, December 5, 2005, 10:15-12:05 Comprehensive 1 Recursive Type Definitions type MyRec is record f1: integer; f2: array of

More information

Part III. Chapter 15: Subtyping

Part III. Chapter 15: Subtyping Part III Chapter 15: Subtyping Subsumption Subtype relation Properties of subtyping and typing Subtyping and other features Intersection and union types Subtyping Motivation With the usual typing rule

More information

COMP 181. Agenda. Midterm topics. Today: type checking. Purpose of types. Type errors. Type checking

COMP 181. Agenda. Midterm topics. Today: type checking. Purpose of types. Type errors. Type checking Agenda COMP 181 Type checking October 21, 2009 Next week OOPSLA: Object-oriented Programming Systems Languages and Applications One of the top PL conferences Monday (Oct 26 th ) In-class midterm Review

More information

Static Semantics. Winter /3/ Hal Perkins & UW CSE I-1

Static Semantics. Winter /3/ Hal Perkins & UW CSE I-1 CSE 401 Compilers Static Semantics Hal Perkins Winter 2009 2/3/2009 2002-09 Hal Perkins & UW CSE I-1 Agenda Static semantics Types Symbol tables General ideas for now; details later for MiniJava project

More information

Recursive Types and Subtyping

Recursive Types and Subtyping Recursive Types and Subtyping #1 One-Slide Summary Recursive types (e.g., list) make the typed lambda calculus as powerful as the untyped lambda calculus. If is a subtype of then any expression of type

More information

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d) CMSC 330: Organization of Programming Languages Tail Calls A tail call is a function call that is the last thing a function does before it returns let add x y = x + y let f z = add z z (* tail call *)

More information

Symbolic Computation and Common Lisp

Symbolic Computation and Common Lisp Symbolic Computation and Common Lisp Dr. Neil T. Dantam CSCI-56, Colorado School of Mines Fall 28 Dantam (Mines CSCI-56) Lisp Fall 28 / 92 Why? Symbolic Computing: Much of this course deals with processing

More information

CS 430 Spring Mike Lam, Professor. Data Types and Type Checking

CS 430 Spring Mike Lam, Professor. Data Types and Type Checking CS 430 Spring 2015 Mike Lam, Professor Data Types and Type Checking Type Systems Type system Rules about valid types, type compatibility, and how data values can be used Benefits of a robust type system

More information

Tracing Ambiguity in GADT Type Inference

Tracing Ambiguity in GADT Type Inference Tracing Ambiguity in GADT Type Inference ML Workshop 2012, Copenhagen Jacques Garrigue & Didier Rémy Nagoya University / INRIA Garrigue & Rémy Tracing ambiguity 1 Generalized Algebraic Datatypes Algebraic

More information

Polymorphism and Type Inference

Polymorphism and Type Inference Polymorphism and Type Inference Volker Stolz stolz@ifi.uio.no INF 3110-2008 Department of Informatics University of Oslo Initially by Gerardo Schneider. Based on John C. Mitchell s slides (Stanford U.)

More information

Some Advanced ML Features

Some Advanced ML Features Some Advanced ML Features Mooly Sagiv Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming University of Washington: Dan Grossman ML is small Small number of powerful constructs

More information

INF 212/CS 253 Type Systems. Instructors: Harry Xu Crista Lopes

INF 212/CS 253 Type Systems. Instructors: Harry Xu Crista Lopes INF 212/CS 253 Type Systems Instructors: Harry Xu Crista Lopes What is a Data Type? A type is a collection of computational entities that share some common property Programming languages are designed to

More information

OCaml. ML Flow. Complex types: Lists. Complex types: Lists. The PL for the discerning hacker. All elements must have same type.

OCaml. ML Flow. Complex types: Lists. Complex types: Lists. The PL for the discerning hacker. All elements must have same type. OCaml The PL for the discerning hacker. ML Flow Expressions (Syntax) Compile-time Static 1. Enter expression 2. ML infers a type Exec-time Dynamic Types 3. ML crunches expression down to a value 4. Value

More information

Homework 3 COSE212, Fall 2018

Homework 3 COSE212, Fall 2018 Homework 3 COSE212, Fall 2018 Hakjoo Oh Due: 10/28, 24:00 Problem 1 (100pts) Let us design and implement a programming language called ML. ML is a small yet Turing-complete functional language that supports

More information

SML A F unctional Functional Language Language Lecture 19

SML A F unctional Functional Language Language Lecture 19 SML A Functional Language Lecture 19 Introduction to SML SML is a functional programming language and acronym for Standard d Meta Language. SML has basic data objects as expressions, functions and list

More information

FUNCTIONAL AND LOGIC PROGRAMS

FUNCTIONAL AND LOGIC PROGRAMS FUNCTIONAL AND LOGIC PROGRAMS Contents Language Specific Compilation Context Handling Identification Scope Overloading Imported Scope Type Checking Type table Type equivalence Coercions Casts and conversions

More information

Programming Languages

Programming Languages Programming Languages Types CSCI-GA.2110-001 Summer 2011 What is a type? A type consists of a set of values The compiler/interpreter defines a mapping of these values onto the underlying hardware. 2 /

More information

Handout 2 August 25, 2008

Handout 2 August 25, 2008 CS 502: Compiling and Programming Systems Handout 2 August 25, 2008 Project The project you will implement will be a subset of Standard ML called Mini-ML. While Mini- ML shares strong syntactic and semantic

More information

Simply-Typed Lambda Calculus

Simply-Typed Lambda Calculus #1 Simply-Typed Lambda Calculus #2 Back to School What is operational semantics? When would you use contextual (small-step) semantics? What is denotational semantics? What is axiomatic semantics? What

More information

Haskell 98 in short! CPSC 449 Principles of Programming Languages

Haskell 98 in short! CPSC 449 Principles of Programming Languages Haskell 98 in short! n Syntax and type inferencing similar to ML! n Strongly typed! n Allows for pattern matching in definitions! n Uses lazy evaluation" F definition of infinite lists possible! n Has

More information

CS 320: Concepts of Programming Languages

CS 320: Concepts of Programming Languages CS 320: Concepts of Programming Languages Wayne Snyder Computer Science Department Boston University Lecture 04: Basic Haskell Continued o Polymorphic Types o Type Inference with Polymorphism o Standard

More information

Recap from last time. Programming Languages. CSE 130 : Fall Lecture 3: Data Types. Put it together: a filter function

Recap from last time. Programming Languages. CSE 130 : Fall Lecture 3: Data Types. Put it together: a filter function CSE 130 : Fall 2011 Recap from last time Programming Languages Lecture 3: Data Types Ranjit Jhala UC San Diego 1 2 A shorthand for function binding Put it together: a filter function # let neg = fun f

More information

Programming Languages

Programming Languages Programming Languages Types CSCI-GA.2110-003 Fall 2011 What is a type? An interpretation of numbers Consists of a set of values The compiler/interpreter defines a mapping of these values onto the underlying

More information

Lambda Calculus and Type Inference

Lambda Calculus and Type Inference Lambda Calculus and Type Inference Björn Lisper Dept. of Computer Science and Engineering Mälardalen University bjorn.lisper@mdh.se http://www.idt.mdh.se/ blr/ October 13, 2004 Lambda Calculus and Type

More information

9/7/17. Outline. Name, Scope and Binding. Names. Introduction. Names (continued) Names (continued) In Text: Chapter 5

9/7/17. Outline. Name, Scope and Binding. Names. Introduction. Names (continued) Names (continued) In Text: Chapter 5 Outline Name, Scope and Binding In Text: Chapter 5 Names Variable Binding Type bindings, type conversion Storage bindings and lifetime Scope Lifetime vs. Scope Referencing Environments N. Meng, S. Arthur

More information

The Substitution Model. Nate Foster Spring 2018

The Substitution Model. Nate Foster Spring 2018 The Substitution Model Nate Foster Spring 2018 Review Previously in 3110: simple interpreter for expression language abstract syntax tree (AST) evaluation based on single steps parser and lexer (in lab)

More information

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

CMSC 330: Organization of Programming Languages. Functional Programming with Lists CMSC 330: Organization of Programming Languages Functional Programming with Lists 1 Lists in OCaml The basic data structure in OCaml Lists can be of arbitrary length Implemented as a linked data structure

More information

CSCE 314 Programming Languages. Type System

CSCE 314 Programming Languages. Type System CSCE 314 Programming Languages Type System Dr. Hyunyoung Lee 1 Names Names refer to different kinds of entities in programs, such as variables, functions, classes, templates, modules,.... Names can be

More information

CSC324 Principles of Programming Languages

CSC324 Principles of Programming Languages CSC324 Principles of Programming Languages http://mcs.utm.utoronto.ca/~324 November 14, 2018 Today Final chapter of the course! Types and type systems Haskell s type system Types Terminology Type: set

More information

Polymorphism and Type Inference

Polymorphism and Type Inference Polymorphism and Type Inference Volker Stolz stolz@ifi.uio.no Department of Informatics University of Oslo Initially by Gerardo Schneider. Based on John C. Mitchell s slides (Stanford U.) Compile-time

More information

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

CMSC 330: Organization of Programming Languages. Functional Programming with Lists CMSC 330: Organization of Programming Languages Functional Programming with Lists CMSC330 Spring 2018 1 Lists in OCaml The basic data structure in OCaml Lists can be of arbitrary length Implemented as

More information

Basic concepts. Chapter Toplevel loop

Basic concepts. Chapter Toplevel loop Chapter 3 Basic concepts We examine in this chapter some fundamental concepts which we will use and study in the following chapters. Some of them are specific to the interface with the Caml language (toplevel,

More information

Programming Language Features. CMSC 330: Organization of Programming Languages. Turing Completeness. Turing Machine.

Programming Language Features. CMSC 330: Organization of Programming Languages. Turing Completeness. Turing Machine. CMSC 330: Organization of Programming Languages Lambda Calculus Programming Language Features Many features exist simply for convenience Multi-argument functions foo ( a, b, c ) Ø Use currying or tuples

More information

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages CMSC 330: Organization of Programming Languages Lambda Calculus CMSC 330 1 Programming Language Features Many features exist simply for convenience Multi-argument functions foo ( a, b, c ) Ø Use currying

More information

Part III Chapter 15: Subtyping

Part III Chapter 15: Subtyping Part III Chapter 15: Subtyping Subsumption Subtype relation Properties of subtyping and typing Subtyping and other features Intersection and union types Subtyping Motivation With the usual typing rule

More information

Processadors de Llenguatge II. Functional Paradigm. Pratt A.7 Robert Harper s SML tutorial (Sec II)

Processadors de Llenguatge II. Functional Paradigm. Pratt A.7 Robert Harper s SML tutorial (Sec II) Processadors de Llenguatge II Functional Paradigm Pratt A.7 Robert Harper s SML tutorial (Sec II) Rafael Ramirez Dep Tecnologia Universitat Pompeu Fabra Paradigm Shift Imperative Paradigm State Machine

More information

Formal Semantics. Prof. Clarkson Fall Today s music: Down to Earth by Peter Gabriel from the WALL-E soundtrack

Formal Semantics. Prof. Clarkson Fall Today s music: Down to Earth by Peter Gabriel from the WALL-E soundtrack Formal Semantics Prof. Clarkson Fall 2015 Today s music: Down to Earth by Peter Gabriel from the WALL-E soundtrack Review Previously in 3110: simple interpreter for expression language: abstract syntax

More information

CS558 Programming Languages

CS558 Programming Languages CS558 Programming Languages Fall 2016 Lecture 7a Andrew Tolmach Portland State University 1994-2016 Values and Types We divide the universe of values according to types A type is a set of values and a

More information

Type Systems COMP 311 Rice University Houston, Texas

Type Systems COMP 311 Rice University Houston, Texas Rice University Houston, Texas 1 Type Systems for Programming Language were invented by mathematicians before electronic computers were invented. What is a type? A meaningful subset of the set of the domain

More information

Scope and Introduction to Functional Languages. Review and Finish Scoping. Announcements. Assignment 3 due Thu at 11:55pm. Website has SML resources

Scope and Introduction to Functional Languages. Review and Finish Scoping. Announcements. Assignment 3 due Thu at 11:55pm. Website has SML resources Scope and Introduction to Functional Languages Prof. Evan Chang Meeting 7, CSCI 3155, Fall 2009 Announcements Assignment 3 due Thu at 11:55pm Submit in pairs Website has SML resources Text: Harper, Programming

More information

The Substitution Model

The Substitution Model The Substitution Model Prof. Clarkson Fall 2017 Today s music: Substitute by The Who Review Previously in 3110: simple interpreter for expression language abstract syntax tree (AST) evaluation based on

More information

ML Type Inference and Unification. Arlen Cox

ML Type Inference and Unification. Arlen Cox ML Type Inference and Unification Arlen Cox Research Goals Easy to use, high performance parallel programming Primary contributions in backend and runtime Need a front end to target backend ML offers ease

More information

CS153: Compilers Lecture 14: Type Checking

CS153: Compilers Lecture 14: Type Checking CS153: Compilers Lecture 14: Type Checking Stephen Chong https://www.seas.harvard.edu/courses/cs153 Announcements Project 4 out Due Thursday Oct 25 (7 days) Project 5 out Due Tuesday Nov 13 (26 days) Project

More information

22c:111 Programming Language Concepts. Fall Types I

22c:111 Programming Language Concepts. Fall Types I 22c:111 Programming Language Concepts Fall 2008 Types I Copyright 2007-08, The McGraw-Hill Company and Cesare Tinelli. These notes were originally developed by Allen Tucker, Robert Noonan and modified

More information