Lectures 5–7: Type Inference

Lectures 5–7: Type Inference

15-819: Foundations of Quantitative Program Analysis Lectures 5–7: Type Inference Jan Hoffmann September 17, 2019 1 Introduction Before we start to develop a type-based approach to resource analysis, we will study type inference. That is, given an expression e we want to find a context ¡ and a type ¿ such that ¡ e : ` ¿, or report that e is not typeable. Type inference will serve as a blue print for resource bound inference, which corresponds to a type inference for a more complex type system. Moreover, type inference provides a good motivation for introducing let polymorphism, which will shed light on some design choices in type systems that we study later in the course. 2 A Monomorphic Type System In this lecture, we use a similar syntax as for the cost semantics lecture. The only difference is that we add let bindings, which will come in handy when we talk about let polymorphism. e :: x x Æ app(e1;e2) e1(e2) lam{¿}(x.e) ¸(x : ¿)e fix{¿}(x.e) fix x as e triv hi let(e ;x.e ) let x e in e 1 2 Æ 1 2 A type is either a type variable (t,u,t ,u ,...), an arrow type ¿ ¿ , or the unit type 1. 1 1 1 ! 2 ¿ :: t t Æ arr(¿ ;¿ ) ¿ ¿ 1 2 1 ! 2 unit 1 Figure 1 contains the type rules of the monomorphic type system. The rules define a judge- ment ¡ m e : ¿ stating that expression e has type ¿ in context ¡. As usual, a context ¡ is a finite ` mapping from variables to types. ¡ :: ¡,x : ¿ Æ ¢ j The order in which variables appear in a context is irrelevant. A well-formed context contains each variable only once. Especially, if we write ¡0 ¡,x : ¿ then ¡0(x) ¿. Æ Æ A type variable can be viewed as an unspecified base type or a placeholder for one concrete type without type variables. They do not enable any form of polymorphism. For example, in the rule M:APP for function application the argument type ¿0 has to exactly match the type of e2 and there is no instantiation of type variables. Therefore, a function type t t cannot be used with 1 ! 2 an argument of type 1 or t t but only with arguments of type t . 2 ! 2 1 We skip the definition of the dynamic semantics, as it is not needed for the discussion of type inference. However, we could define a structural dynamic semantics and prove type soundness as discussed earlier in lecture. 1 ¡ m e : ¿ “expression e has type ¿ in context ¡” ` m ¡(x) ¿ ¡,x:¿0 e : ¿ Æ ` m (M:VAR) m (M:UNIT) m (M:ABS) ¡ x : ¿ ¡ triv : unit ¡ lam{¿0}(x.e): ¿0 ¿ ` ` ` ! m m m ¡ e1 : ¿0 ¿ ¡ e2 : ¿0 ¡,x : ¿ e : ¿ ` ! ` (M:APP) ` (M:FIX) ¡ m app(e ;e ): ¿ ¡ m fix{¿}(x.e): ¿ ` 1 2 ` m m ¡ e1 : ¿1 ¡,x : ¿1 e2 : ¿ ` ` (M:LET) ¡ m let(e ;x.e ): ¿ ` 1 2 Figure 1: Monomorphic type rules. 3 Type Inference When types get more involved, it can be elaborate to write down the types of the expressions or to come up with the right type of a function. Therefore, functional programming languages like ML have a mechanisms for automatic type inference or type reconstruction. To define the type inference problem for our simple monomorphic language, we first need to deal with a couple of formalities. Let T be the set of monomorphic types and let X be the set of type variables. In the monomorphic type system, we can view type variables as placeholders for other types. This view is justified by the following fact: If we replace a type variable t X with a m m 2 type ¿ T in judgement ¡ e : ¿ then we obtain a derivable judgement ¡0 e : ¿0. To make 0 2 ` ` this formal, we use type substitutions, (partial) finite functions σ : X T from type variables to ! types. For a substitution, σ we inductively define [σ]: T T . ! ½ ¿ if σ(t) ¿ [σ]t Æ Æ t if t dom(σ) 62 [σ]unit unit Æ [σ]arr(¿ ;¿ ) arr([σ]¿ ;[σ]¿ ) 1 2 Æ 1 2 We sometimes just write [¿0/t]¿ for [σ]¿ if dom(σ) {t} and σ(t) ¿0. We extent type substitu- Æ Æ tions to contexts ¡ x : ¿ ,...,x : ¿ by applying the substitution pointwise to each type, that Æ 1 1 n n is, [σ]¡ x :[σ]¿ ,...,x :[σ]¿ . Similarly, we define type substitution for expressions. Æ 1 1 n n [σ]x x Æ [σ]app(e ;e ) app([σ]e ;[σ]e ) 1 2 Æ 1 2 [σ]lam{¿}(x.e) lam{[σ]¿}(x.[σ]e) Æ [σ]fix{¿}(x.e) fix{[σ]¿}(x.[σ]e) Æ [σ]triv triv Æ [σ]let(e ;x.e ) let([σ]e ;x.[σ]e ) 1 2 Æ 1 2 Lemma 1. Let ¡ m e : ¿ and let σ be a type substitution. Then [σ]¡ m [σ]e :[σ]¿. ` ` The type inference problem for our simple monomorphic language is defined as follows. Given: An expression e Question: Find a type ¿ and a type substitution σ such that [σ]e : ¿ if such a ¿ exists or report a type error otherwise. So inferring a type includes to fill in the right types for function arguments and fixed points. To infer types for an expression “without types”, we would simply annotate each fixed point and function abstraction with a unique type variable ti . 2 Example 1. To infer a type for the expression ¸(x )¸(x )(x (x ))(x ( )), we first insert fresh type 1 1 1 2 2 hi variables into the function abstractions to obtain ¸(x : t )¸(x : t )(x (x ))(x ( )) 1 1 1 2 1 2 2 hi We then infer that ¿ t , σ(t ) 1, and σ(t ) (1 t ) t t . ´ 2 2 Æ 1 Æ ! 3 ! 3 ! 4 We are also interested in finding the most general typing [σ0]e : ¿0 for an expression e in the following sense: For every substitution σ and type ¿ such that [σ]e : ¿ there exists a substitution σ0 such that ¿ [σ]¿ and [σ]e [σ0]([σ ]e). We will see that such a most general typing exists, Æ 0 Æ 0 which is not a priory clear. 4 Constraint-Based Typing We will solve the type inference problem using constraint-based typing judgements and an unification algorithm. Type Constraints First, we introduce type constraints. A type constraint is a pair ¿ ,¿ h 1 2i where ¿1 and ¿2 are monomorphic types. Definition. A type substitution σ solves the type constraint ¿ ,¿ if [σ](¿ ) is syntactically h 1 2i 1 equal to [σ]¿2. Let C be a set of type constraints. The substitution σ solve C if σ solves every type constraint in C. The substitution σ is then called a solution or unifier for C. We define U(C) to be the set of all unifiers. We often write a constraint set as a list t ,t ,..., t ,t . h 1 10 i h n n0 i Example 2. The constraint set u,t t , t ,t t , t ,1 has the solution h 1 ! 2i h 1 3 ! 4i h 2 i t 1 2 7! t t t 1 7! 3 ! 4 u (t t ) 1 7! 3 ! 4 ! The constraint set t,t t , t ,t has no solution. h 1 ! 2i h 1 i When we define type substitutions, we have to be careful not to use type variables on the right-hand side that also appear on the left-hand side. Otherwise, the result of the substitution might not be what we expect. Lemma 2. A set C of type constraints has either 0, 1, or an infinite number of solutions. At first, Lemma 2 might look surprising. However, it is intuitively clear that either a solution exists or not. Furthermore, there is solution that maps one of the type variables to a type variable or not. If this is the case, then there is an infinite number of solutions and otherwise there is only one solution. You can prove the lemma first for one constraint ¿ ,¿ by nested induction h 1 2i on ¿1 and the by induction on the number of constraints. Constraint-Based Typing Figure 2 contains the type rules for a constraint-based type judge- ment ¡ c e : ¿ C. It states that under context ¡, expression e has type ¿ under the constraints C. ` j m The intuitive meaning is that we can derive a monomorphic typing ¡0 e : ¿0 if we substitute ` the variables in ¡ and ¿, so that the constraints in C are satisfied. This intuition is formalized in Theorem 1. In the type rules, it might seem arbitrary at what points we introduce new variables and type constraints. For example, in the rule C:FIX, we introduce the type constraint ¿ ,¿ for h 0 i the instead of simply requiring ¡,x : ¿ c e : ¿ C in the premise. The reason is that we take an ` j 3 ¡ c e : ¿ C Expression e has type T in context ¡ under constraints C. ` j c ¡(x) ¿ ¡,x:¿0 e : ¿ C Æ ` j c (C:VAR) m (M:UNIT) c (C:ABS) ¡ x : ¿ ¡ triv : unit ¡ lam{¿0}(x.e): ¿0 ¿ C ` j ¢ ` j ¢ ` ! j c c c ¡ e : ¿ C ¡ e : ¿ C t fresh ¡,x : ¿ e : ¿0 C ` 1 1 j 1 ` 2 2 j 2 ` j c (C:APP) c (C:FIX) ¡ app(e ;e ): t ¿ ,¿ t ,C ,C ¡ fix{¿}(x.e): ¿0 ¿0,¿ ,C ` 1 2 j h 1 2 ! i 1 2 ` j h i c c ¡ e1 : ¿1 C1 ¡,x:¿1 e2 : ¿ C2 ` j ` j (C:LET) ¡ c let(e ;x.e ): ¿ C ,C ` 1 2 j 1 2 Figure 2: Constraint based rules.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    11 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us