Polymorphic Type System - PowerPoint PPT Presentation

About This Presentation
Title:

Polymorphic Type System

Description:

Polymorphic Type System Goals Allow expression of for all types T fun I x = x I : a - a Allow expression of type-equality constraints fun ... – PowerPoint PPT presentation

Number of Views:28
Avg rating:3.0/5.0
Slides: 18
Provided by: cecsWrigh
Learn more at: http://cecs.wright.edu
Category:

less

Transcript and Presenter's Notes

Title: Polymorphic Type System


1
Polymorphic Type System
2
Goals
  • Allow expression of for all types T
  • fun I x x
  • I a -gt a
  • Allow expression of type-equality constraints
  • fun fst (x,y) x
  • fst a b -gt a
  • Support notion of instance of a type
  • I 5
  • I int -gt int

3
Polymorphic Type System
  • Type Checking Rule
  • An expression of a type can legally appear in
    all contexts where an instance of that type can
    appear.
  • Type constants
  • int, bool,
  • Type variables
  • a, , a,
  • Type constructors
  • -gt, ,
  • Principal type of an expression is the most
    general type, which ML system infers.

4
Signature of Equality
  • a a -gt bool ?
  • Equality ( ) is not computable for all types.
    E.g., function values.
  • So types (a,b, ) are partitioned into
  • types that support equality (a, b, )
  • types that do not support equality.
  • a a -gt bool
  • int, string, etc are equality types. They are
    closed under cartesian product, but not under
    function space constructor.

5
Type Inference
  • I (3,5) (3,5)
  • (intint)-gt(intint) (intint)
  • Principal type of I is the generalization of
  • all possible types of its uses.
  • Curry2 (a b -gt c) -gt
  • (a -gt b -gt c)
  • Uncurry2 (a -gt b -gt c) -gt
  • (a b -gt c)

6
Subtle Points
  • The type of a use of a function must be an
    instance of the principal type inferred in the
    definition.
  • The type of a value is fixed. So, multiple
    occurrences of a symbol denoting the same value
    (such as several uses of a formal parameter in a
    function body) must have identical type.

7
Systematic Type Derivation
  • fun c f g x f(g(x))
  • Step1 Assign most general types to
    left-hand-side arguments and the (rhs) result.
  • f t1
  • g t2
  • x t3
  • f(g(x)) t4
  • Thus, type of c is
  • c t1 -gt t2 -gt t3 -gt t4

8
  • Step 2 Analyze and propagate type constraints
  • Application Rule
  • If f x t
  • then x t1 and f t1 -gt t,
  • for some new t1.
  • Equality Rule
  • If both xt and xt1 can be deduced
  • for the value of a variable x, then t t1.
  • Function Rule
  • (t-gtu)(t1-gtu1) iff (tt1) /\ (uu1)

9
  • f(g x) t4
  • AR
  • (g x) t5
  • f t5 -gt t4
  • AR
  • x t6
  • g t6 -gt t5
  • ER
  • t1 t5 -gt t4
  • t2 t6 -gt t5
  • t3 t6
  • Step 3 Overall deduced type
  • c (t5 -gt t4) -gt
  • (t6 -gt t5) -gt
  • (t6 -gt t4)
  • (unary function
  • composition)

10
Example
  • fun f x y fst x fst y
  • given op int -gtint -gt int
  • fst a b -gt a
  • Step 1 x t1 y t2
  • (fst_1 x fst_2 y) t3
  • The two instantiations of fst need not
    have the same type.
  • fst_1 u1 u2 -gt u1
  • fst_2 v1 v2 -gt v1
  • f t1 -gt t2 -gt t3

11
  • Step 2 Applying rule for
  • (fst_1 x) int
  • (fst_2 y) int
  • t3 int
  • Step 3 fst_1 intu2 -gt int
  • fst_2 intv2 -gt int
  • t1 int u2
  • t2 int v2
  • f int a -gt int b -gt int

12
Example (fixed point)
  • fun fix f f (fix f)
  • 1. Assume f a -gt b
  • 2. From (fix f f ()) infer
  • fix (a -gt b) -gt b
  • 3. From ( ... f (fix f)) infer
  • fix (a -gt b) -gt a
  • fix a -gt a -gt a

13
Recursive Definition (curried function)
  • fun f x y f (f x) 0
  • f (int -gt a) -gt int -gt a
  • fun f x y f (f x) (f x y)
  • f (a -gt a) -gt a -gt a
  • fun f f f ( identity function )
  • ( names of formals and functions come from
    disjoint namespaces )
  • fun f x y f x ( illegal )
  • fun f g g f ( illegal )

14
Example (ill-typed definition)
  • fun selfApply f f f
  • 1. f_1t1 (f_2 f_3)t2
  • selfApply t1 -gt t2
  • 2. f_3 t3 f_2 t3 -gt t2
  • 3. t1 t3 t3 -gt t2
  • (unsatisfiable)
  • (cf. val selfApply I I)

15
Problematic Case
  • fn x gt (1, a)
  • val it fn 'a -gt int string
  • fn f gt (f 1, f a)
  • Type error
  • Least upper bound of int and string does not
    exist, and the expression cannot be typed as
    ((a-gtb) -gtbb) because the following
    expression is not type correct.
  • ( (fn f gt (f 1, f a)) (Real.Math.sqrt) )

16
(contd)
  • fn (x,y) gt (size x, length y)
  • val it fn string 'a list -gt int int
  • fn z gt (size z, length z)
  • Type Error
  • string and list cannot be unified to obtain a -gt
    (int int).
  • Or else, it will conflict with type instantiation
    rule, compromising type safety.

17
Equivalence?
  • (let val V E in F end)
  • / ((fn V gt F) E)
  • Even though both these expressions have the same
    behavior, the lambda abstraction is more
    restricted because it is type checked
    independently of the context of its use. In
    particular, the polymorphic type variable
    introduced in an abstraction must be treated
    differently from that introduced in a local
    definition.
Write a Comment
User Comments (0)
About PowerShow.com