Types and Simple Type Inference - PowerPoint PPT Presentation

About This Presentation
Title:

Types and Simple Type Inference

Description:

http://www.csg.csail.mit.edu/6.827. Types and Simple Type Inference ... A substitution is a map. S : Type Variables -- Types. S = [ / t1,..., n / tn] ... – PowerPoint PPT presentation

Number of Views:137
Avg rating:3.0/5.0
Slides: 22
Provided by: LAB142
Learn more at: http://csg.csail.mit.edu
Category:

less

Transcript and Presenter's Notes

Title: Types and Simple Type Inference


1
Types and Simple Type Inference
  • September 21, 2006

2
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 ...

3
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.

4
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

5
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.

6
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
7
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.

8
Type Instances
The type of a variable can be instantiated
differently within its lexical scope. let
id \x.x in ((id1 5), (id2 True)) id1
? id2 ? Both
id1 and id2 can be regarded as instances of
type ?
Int --gt Int
Bool --gt Bool
t --gt t
9
Type Instances another example
let twice (t -gt t) -gt t -gt t twice f x f
(f x) in twice1 twice2 (plus 3) 4
twice1 ? twice2
?
((I -gt I) -gt I -gt I) -gt (I -gt I) -gt (I
-gt I)
(I -gt I) -gt I -gt I
10
Type Instantiation l-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 !
11
A mini Language (?-calculus let) to study
Hindley-Milner Types
Expressions E c constant x
variable lx. E abstraction (E1 E2)
application let x E1 in E2 let-block
  • 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 ...

12
A Simple Type System
Types t ??? base types t type
variables ???--gt ?2 Function types Type
Environments TE Identifiers --gt Types
13
Type Inference Issues
  • What does it mean for two types ?a ?and ?b to be
    equal?
  • Structural Equality
  • Suppose ?a ?1 --gt ?2
  • ?b ?3 ?--gt ?4
  • Is ?a ?b ?

iff ?1 ?3 and ?2 ?4
  • Can two types be made equal by choosing
    appropriate substitutions for their type
    variables?
  • Robinsons unification algorithm
  • Suppose ?a t1 --gt Bool
  • ?b Int ?--gt t2
  • Are ?a and ?b unifiable ?

if t1 Int and t2 Bool
Suppose ?a t1--gt Bool ?b Int ?--gt Int Are
?a and ?b unifiable ?
No
14
Simple Type Substitutionsneeded to define type
unification
Types ?? ??? base types (Int, Bool ..) t
type variables ?? ?--gt ?? Function types
  • A substitution is a map
  • S Type Variables --gt Types
  • S ?? / t1,..., ?n ?/ tn
  • ? S ? ? is a Substitution Instance of ?
  • Example
  • S (t --gt Bool) / t1
  • S( t1 --gt t1) ?

( t --gt Bool) --gt ( t --gt Bool)
Substitutions can be composed, i.e., S2
S1 Example S1 (t --gt Bool) /
t1 S2 Int / t S2 S1 ( t1 --gt t1)
?
( Int --gt Bool) --gt ( Int --gt Bool)
15
UnificationAn essential subroutine for type
inference
Unify(?1, ?2) tries to unify ?1 and ?2 and
returns a substitution if successful
def Unify(?1, ?2) case (?1, ?2) of (?1, t2)
?1 / t2 provided t2 ? FV(?1) (t1, ?2) ?2 /
t1 provided t1 ? FV(?2) (?1, ?2) if (eq? ?1
?2) then else
fail (?11--gt?12, ?21 --gt?22)
let S1Unify(?11, ?21) S2Unify(S1(?12),
S1(?22)) in S2 S1
otherwise fail
Does the order matter?
16
Type Inference Rules
Typing TE -- e ? Suppose we want to assert
(prove) that given some type environment TE, the
expression (e1 e2) has the type ?. Then it must
be the case that the same TE implies that e1 has
type ?--gt? and e2 has the type ? .
Such an inference rule can be written as
?--gt?
?
(App) TE e1 TE e2 ? TE (e1
e2) ?
17
Simple Type Inference Rules
Typing TE e ? (App) TE e1 ?--gt?
TE e2 ? ? TE (e1 e2) ? (Abs) TE
?x.e ??--gt? (Var) TE x
?? (Const) TE c t (Let) TE
(let x e1 in e2) ?
TE x ? e ?
(x t)? ? ?TE
typeof(c) t
TEx? e1 ???????TEx? e2?
18
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
... (e1 e2) ... let x e1 in
e2 ...
19
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 (S1, ?1) W(TE
x u , e) in (S1, S1(u) --gt ?1) (e1 e2)
let x e1 in e2
us represent new type variables
let (S1, ?1) W(TE, e1) (S2, ?2) W(S1(TE),
e2) S3 Unify(S2(?1), ?2 --gt u) in (S3 S2
S1, S3(u))
let (S1, ?1) W(TE x u, e1) S2
Unify(S1(u), ?1) (S3, ?2) W(S2
S1(TE) x t1, e2) in (S3 S2 S1, ?2)
20
Type Inference
Let fact ?n.if (n 0) then 1
else n fact (n-1) In fact
21
Inferring Polymorphic Types
let id ?x. x in ... (id True) ... (id
1) ...
Write a Comment
User Comments (0)
About PowerShow.com