Presentation is loading. Please wait.

Presentation is loading. Please wait.

Slides 05 1 The Principal-Type Algorithm In general a typable term has an infinite set of types in TA. For example, it is possible to assign to I  x 

Similar presentations


Presentation on theme: "Slides 05 1 The Principal-Type Algorithm In general a typable term has an infinite set of types in TA. For example, it is possible to assign to I  x "— Presentation transcript:

1 Slides 05 1 The Principal-Type Algorithm In general a typable term has an infinite set of types in TA. For example, it is possible to assign to I  x  x every type with the form , by the following deduction: x:  ↦ x:   (  I) ↦ ( x  x):  All the types in this infinite set are substitution- instances of the one type a  a.

2 Slides 05 2 By the subject-construction theorem, every deduction for I must have the simple form shown before. The type a  a is called a principal type for I. The existence of a principal type is a property of all typable terms, not just I. The principal type of a term is the most general type it can receive in TA. We shall learn that every typable term has a principal type.

3 Slides 05 3 A principal-type (PT) or type-checking algorithm will decide whether a given term M is typable and, if the answer is "yes", will output a principal type for M. The existence of a PT algorithm is what gives TA and its extensions such as Haskell and ML their practical value, since if the typability of a program is regarded as a safety criterion then the programmer will want to be able to decide effectively whether a newly created program satisfies this criterion. In order to understand the PT algorithm, we need the knowledge of substitution and unification.

4 Slides 05 4 Definition (3A1) A type-substitution s is any expression [  1 /a 1,...,  n /a n ], where a 1,..., a n are distinct type-variables and  1,...,  n are any types. For any  we define s (  ) to be the type obtained by simultaneously substituting  1 for a 1,...,  n for a n throughout . In more detail, we define i.s (a i )   i ii.s (b)  b if b is an atom  {a 1,..., a n } iii.s (  )  s (  )  s (  ). We call s (  ) an instance of .

5 Slides 05 5 Example: Let s  [(a  b)/c] and   (a  b)  (b  c)  a  c, then s (  )  (a  b)  (b  a  b)  a  a  b.

6 Slides 05 6 The case n = 0 is called the empty substitution, e. e (  )  . If n = 1, s will be called a single substitution. Each part-expression  i /a i of s will be called a component of s, and called trivial if  i  a i. If all trivial components are deleted from s the resulting substitution will be called the nontrivial kernel of s. {a 1,..., a n }: Dom( s ) Vars(  1,...,  n ): Range( s )

7 Slides 05 7 Definition (3A2) The action of a substitution s is extended to finite sequences of types, to type-contexts and to TA -formulas, as follows: s ( ) =, s (  ) = {x 1 : s (  1 ),..., x m : s (  m )} if  = {x 1 :  1,..., x m :  m }, s (  ↦ M:  ) = s (  ) ↦ M: s (  ). s (  ) is the result of applying s to every TA -formula in deduction .

8 Slides 05 8 Besides the concept of substitution s for type-variables just mentioned, we also have the concept of substitution for term- variables. Lemma (2B6) Let  1 be consistent with  2 and let  1, x:  ⊢ M: ,  2 ⊢ N:  then  1   2 ⊢ [N/x]M: .

9 Slides 05 9 Definition (3A3) In TA, a principal type or PT of a term M is a type  such that i.  ⊢ M:  for some , ii.if  ' ⊢ M:  for some  ' and , then  is an instance of . A PT of M completely characterizes the set of all types assignable to M.

10 Slides 05 10 Example: a  b  a is a PT of xy  x Is there a PT for x  xx?

11 Slides 05 11 Definition (3A4) A principal pair for a term M is a pair such that the formula  ↦ M:  is TA -deducible and every other TA -deducible formula  ' ↦ M:  is an instance of  ↦ M: . Example: is a principal pair for xy  x.

12 Slides 05 12 Definition (3A5) A principal deduction for a term M is a deduction  of a formula  ↦ M:  such that every other deduction, whose conclusion's subject is M, is an instance of . To abbreviate "there exists a principal deduction of  ↦ M:  ", we shall write  ⊢ p M: . We will see that a typable term M has not only a most general type but also a deduction whose every step is most general.

13 Slides 05 13 Principal-Type Theorem (3A6) Every typable term has a principal deduction and a principal type in TA. Further, there is an algorithm that will decide whether a given -term M is typable in TA, and if the answer is "yes", will output a principal deduction and principal type for M.

14 Slides 05 14 More details on type-substitution Definition (3B2) Substitutions s and t are extensionally equivalent ( s = ext t ) iff s (  )  t (  ) for all . Lemma (3B2.1) (i) b  Dom( s )  s (b)  b. (ii) s = ext t iff s and t have the same non-trivial kernel.

15 Slides 05 15 Definition (3B3) If s  [  1 /a 1,...,  n /a n ] and V is a given set of variables, the restriction s ↾V of s to V is the substitution consisting of the components  i /a i of s such that a i  V. Lemma (3B3.1) ( s ↾Vars(  ))(  )  s (  ).

16 Slides 05 16 Definition (3B4) If s  [  1 /a 1,...,  n /a n ] and t  [  1 /b 1,...,  p /b p ] and either a 1,..., a n, b 1,..., b p are all distinct or a i  b j   i   j, we define s  t  [  1 /a 1,...,  n /a n,  1 /b 1,...,  p /b p ] with repetitions omitted. Lemma (3B4.1) (i) Dom( s  t ) = Dom( s )  Dom( t ). (ii) If s = ext s' and t = ext t' and s  t is defined, then so is s '  t' and s  t = ext s'  t'.

17 Slides 05 17 The composition of two substitutions s and t is a simultaneous substitution that will have the same effect as applying t and s in succession. Definition (3B5) If s and t are any substitutions, say s  [  1 /a 1,...,  n /a n ] and t  [  1 /b 1,...,  p /b p ], we define s ∘ t  [  i1 /a i1,...,  ih /a ih, s (  1 )/b 1,..., s (  p )/b p ] where {a i1,..., a ih } = Dom( s )-Dom( t ) and 0  h  n.

18 Slides 05 18 Example: s  [(c  d)/a,(b  a)/b], t  [(b  a)/b]   a  b s ( t (  ))  (c  d)  (b  a)  (c  d) s ∘ t  [(c  d)/a,( s (b  a))/b]  [(c  d)/a,((b  a)  (c  d))/b] ( s ∘ t )(  )  (c  d)  (b  a)  (c  d) But ( s  t )(  )  s (  )  (c  d)  b  a

19 Slides 05 19 [a/b] ∘ [b/a]  ?   a  b ([a/b] ∘ [b/a])(  )  ? [a/b]([b/a](  ))  ?

20 Slides 05 20 Lemma (3B5.1) 1.Dom(s∘t) = Dom(s) ∪ Dom(t). 2.(s∘t)(  )  s(t(  )). This also means an instance of an instance of  is an instance of . 3.r∘(s∘t) = ext (r∘s)∘t. 4.s = ext s', t = ext t'  s∘t = ext s'∘t'.

21 Slides 05 21 The next lemma, which is important in the correctness proof of the PT algorithm, says that if a composition s∘t is extended to r  (s∘t), the extended substitution can also be expressed as a composition with t, under certain conditions on r to prevent clashes. Lemma (3B6:composition-extension) Let r, s, t be substitutions such that (i)Dom(r)  (Dom(s)  Dom(t)) = , (ii)Dom(r)  Range(t) = . Then r  (s∘t) and (r  s)∘t are both defined and r  (s∘t)  (r  s)∘t.

22 Slides 05 22 Definition (3B7) A variables-for-variables substitution is a substitution s  [b 1 /a 1,..., b n /a n ] where b 1,..., b n are variables (not necessarily distinct). If b 1,..., b n are distinct, s is called one-to-one, and if also {a 1,..., a n } = Vars(  ) for a given , s is called a renaming in . Note: A one-to-one substitution may be a renaming in one type but not in another. For example, [b/a] is a renaming in a  a but not in a  b.

23 Slides 05 23 Definition (3B8) We say  is an alphabetic variant of , or  and  are identical modulo renaming, iff   s(  ) for some renaming s in . Lemma (3B8.2: Uniqueness) Let  be a principal type of a term M. Then  is unique modulo renaming.

24 Slides 05 24 Main ideas for the PT algorithm The two main steps in the PT algorithm: 1.the computation of PT( x  M) from PT(M), 2.the computation of PT(PQ) from PT(P) and PT(Q). The first is easy, the second needs more work which is the core of the PT algorithm.

25 Slides 05 25 Suppose we are trying to decide whether an application PQ is typable in TA, and we already know that PT(P)  ,PT(Q)  . For simplicity suppose PQ has no free variables. Then the types assignable to P are generated from  by substitution and those assignable to Q are generated from . Hence, if we can find substitutions s 1 and s 2 such that s 1 (  )  s 2 (  ), we will be able to deduce a type for PQ by rule (  E), as follows: ↦ P:s 1 (  )  s 1 (  ) ↦ Q:s 2 (  )  (  E) ↦ PQ:s 1 (  )

26 Slides 05 26 Thus the problem of deciding whether PQ is typable reduces to that of finding substitutions s 1 and s 2 such that s 1 (  )  s 2 (  ). This suggests the next two definitions.

27 Slides 05 27 Definition (3C2) (i) If  s 1 (  )  s 2 (  ) we call a common instance (c.i.) of the pair, and we call a pair of converging substitutions for. (ii)If  s 1 ( )  s 2 ( ) we call a common instance of and. Example: A c.i. of is the type ((  )  )  (  ), and the corresponding converging substitutions are s 1  [((  )  )/a,  /b,  /c], s 2  [(  )/a,  /b].

28 Slides 05 28 Does every pair of types has a common instance? No. For example the pair has none.

29 Slides 05 29 Definition (3C3) A most general common instance (m.g.c.i) of is a common instance 0 such that every other common instance is an instance of 0. If 0 is an m.g.c.i. of we call any pair such that s 1 (  )  s 2 (  )  0 an m.g.c.i.-generator for. M.g.c.i.'s are unique modulo renaming.

30 Slides 05 30 Example: The pair has an m.g.c.i m 0  ((  )  )  (  ). Proof: Let   a  (b  c),   (a  b)  a. In slide 27 we have shown that m 0 is a c.i. of  and . To show that m 0 is most general, let m be any other c.i., then we have m  s 1 (a)  (s 1 (b)  s 1 (c))  (s 2 (a)  s 2 (b))  s 2 (a), which gives s 1 (a)  s 2 (a)  s 2 (b), s 2 (a)  s 1 (b)  s 1 (c). Therefore, s 1 (a)  (s 1 (b)  s 1 (c))  s 2 (b) and m  ((s 1 (b)  s 1 (c))  s 2 (b))  (s 1 (b)  s 1 (c)). Thus, m is an instance of m 0. 

31 Slides 05 31 The problem of finding PT(PQ) reduces to that of finding an m.g.c.i of. We will need Robinson's unification algorithm.

32 Slides 05 32 Definition (3D1) (i) If there is a substitution s such that s(  )  s(  ) we say is unifiable; we call any such s a unifier of and call s(  ) a unification of. (ii)A unifier of a pair of sequences, > is a substitution s such that s( )  s( ).

33 Slides 05 33 Note: A unification of is just a c.i. obtained by making the same substitution in  as in . But not every c.i. is a unification. A pair may have a c.i. but no unifier. For example, consider   a  (b  c),   (a  b)  a. We have seen in slide 27 that this pair has a c.i.; but no substitution s can exist such that s(  )  s(  ), because this would imply s(a)  s(a)  s(b) which is impossible.

34 Slides 05 34 Definition (3D2) A most general unifier (m.g.u.) of is a unifier u such that for every other unifier s of we have s(  )  s'(u(  )) for some s'. If  u(  ) for some m.g.u. u of we call a most general unification of. Lemma If u is an m.g.u. of and v is a renaming of variables in u(  ), then v∘u is an m.g.u. of.

35 Slides 05 35 The m.g.u. of a pair may differ from its m.g.c.i. However, in the special case that  and  have no common variables, every c.i. is also a unification. Why? Because if  s 1 (  )  s 2 (  ) and Dom(s 1 )  Vars(  ) and Dom(s 2 )  Vars(  ) then s 1  s 2 is defined and  (s 1  s 2 )(  )  (s 1  s 2 )(  ).

36 Slides 05 36 Lemma (3D3) (i)If  and  have no common variables, has an m.g.u. iff it has an m.g.c.i., and the two are identical. (ii)For all  and  : if we change  to an alphabetic variant  * with no variables in common with , the unifications of will be exactly the common instances of and the most general unification of will be the m.g.c.i. of. Thus the problem of finding m.g.c.i.'s has been reduced to that of finding most general unifications of pairs with no variables in common.

37 Slides 05 37 Unification Theorem (3D4) (i)There is an algorithm which decides whether a pair of types has a unifier, and, if the answer is "yes", constructs its m.g.u. (ii)If a pair has a unifier it has an m.g.u. (iii)Parts (i) and (ii) hold also for pairs of deductions and for pairs of finite type- sequences.

38 Slides 05 38 Unification Algorithm (Robinson 1965) Input: any pair of types. Output: either a correct statement that is not unifiable or an m.g.u. u of. Step 0. Choose k = 0 and u 0  e (the empty substitution). Step k+1. Given k and u k, construct  k  u k (  ) and  k  u k (  ), and apply the comparison procedure to. That procedure will output either a correct statement that  k   k or a disagreement pair such that  ≢ a. If  k   k, choose u  u k. If  k ≢  k and the output of the comparison procedure is, decide whether a  Vars(  ). If a  Vars(  ), state that is not unifiable and stop. If a  Vars(  ), then replace k by k+1, choose u k+1  [  /a]∘u k, and go to Step k+2.

39 Slides 05 39 Comparison Procedure Given a pair of types, write  and as symbol-strings, say   s 1...s m,  t 1...t n (m, n  1) where each of s 1,..., s m, t 1,..., t n is an occurrence of a parenthesis, arrow or variable. If  , then state that   and stop. If  ≢, choose the least p  Min{m,n} such that s p ≢ t p. One of s p, t p must be a variable and the other must be a left parenthesis or a different variable. Further, s p is the leftmost symbol of a unique subtype  * of . (If s p is a variable,  *  s p.) Similarly t p is the leftmost symbol of a unique subtype * of. Choose one of  *, * that is a variable and call it "a". (If both are variables, choose the one that is first in the alphabet.) Then call the remaining member of "  "; the pair is called the disagreement pair for.

40 Slides 05 40 the PT algorithm Input: any -term M, closed or not. Output: either a principal deduction  M for M or a correct statement that M is not typable. Case I. If M is a variable, say M  x, choose  M to be the one- formula deduction x:a ↦ x:a, where a is any type-variable. Case II. If M  x  P and x  FV(P), say FV(P) = {x, x 1,..., x t }, apply the algorithm to P. If P is not typable, neither is M. If P has a principal deduction  P its conclusion must have the form x: , x 1 :  1,..., x t :  t ↦ P:  for some types ,  1,...,  t, . Apply rule (  I) main to make a deduction of x 1 :  1,..., x t :  t ↦ ( x  P): . Call this deduction  xP.

41 Slides 05 41 Case III. If M  x  P and x  FV(P), say FV(P) = {x 1,..., x t }, apply the algorithm to P. If P is not typable, neither is M. If P has a principal deduction  P its conclusion must have the form x 1 :  1,..., x t :  t ↦ P:  for some types  1,...,  t, . Choose a new type-variable d not in  P and apply (  I) vac, vacuously discharging x:d, to get a deduction of x 1 :  1,..., x t :  t ↦ ( x  P):d . Call this deduction  xP.

42 Slides 05 42 Case IV. If M  PQ, apply the algorithm to P and Q. If P or Q is untypable then so is M. If P and Q are both typable, suppose they have principal deductions  P and  Q. First rename type-variables, if necessary, to ensure that  P and  Q have no common type-variables. Next list the free term-variables in P and those in Q (noting that these lists may overlap); say FV(P) = {u 1,..., u p, w 1,..., w r } (p, r  0), FV(Q) = {v 1,..., v q, w 1,..., w r } (q  0), where u 1,..., u p, v 1,..., v q, w 1,..., w r are distinct.

43 Slides 05 43 Subcase IVa. M  PQ and PT(P) is composite, say PT(P)  . Then the conclusions of  P and  Q have the form, respectively, (1)u 1 :  1,..., u p :  p, w 1 :  1,..., w r :  r ↦ P: , (2)v 1 :  1,..., v q :  q, w 1 :  1,..., w r :  r ↦ Q: . Apply the unification algorithm to the pair of sequences (3),. Subcase IVa1. The pair (3) has no unifier. Then PQ is not typable.

44 Slides 05 44 Subcase IVa2. The pair (3) has a unifier. Then the unification algorithm gives a most general unifier u; apply a renaming if necessary to ensure that (4) Dom(u) = Vars(  1,...,  r, ,  1,...,  r,  ), (5)Range(u)  V = , where (6) V = (Vars(  P )  Vars(  Q )) – Dom(u). Then apply u to  P and  Q ; this changes their conclusions to, respectively, u 1 :  1 *,..., u p :  p *, w 1 :  1 *,..., w r :  r * ↦ P:  *  *, v 1 :  1 *,..., v q :  q *, w 1 :  1 *,..., w r :  r * ↦ Q:  *, where  1 *  u(  1 ), etc. And by the definition of u we have  1 *   1 *,...,  r *   r *,  *   *. Hence (  E) can be applied to the conclusions of u(  P ) and u(  Q ). Call the resulting combined deduction  PQ ; its conclusion is (7)u 1 :  1 *,..., u p :  p *, v 1 :  1 *,..., v q :  q *, w 1 :  1 *,..., w r :  r * ↦ PQ:  *.

45 Slides 05 45 Subcase IVb. M  PQ and PT(P) is atomic, say PT(P)  b. Then the conclusions of  P and  Q have the form, respectively, (8)u 1 :  1,..., u p :  p, w 1 :  1,..., w r :  r ↦ P:b, (9)v 1 :  1,..., v q :  q, w 1 :  1,..., w r :  r ↦ Q: . Choose any variable c  Vars(  P )  Vars(  Q ) and apply the unification algorithm to the pair of sequences (10),. Subcase IVb1. The pair (10) has no unifier. Then PQ is not typable.

46 Slides 05 46 Subcase IVb2. The pair (10) has a unifier. Then the unification algorithm gives a most general unifier u; apply a renaming if necessary to ensure that (11) Dom(u) = Vars(  1,...,  r, b,  1,...,  r,  c), (12)Range(u)  V = , where V = (Vars(  P )  Vars(  Q )) – Dom(u). Then apply u to  P and  Q. By the definition of u, u(b)  u(  c)  u(  )  u(c), and thus the conclusions of u(  P ) and u(  Q ) are u 1 :  1 *,..., u p :  p *, w 1 :  1 *,..., w r :  r * ↦ P:  *  c*, v 1 :  1 *,..., v q :  q *, w 1 :  1 *,..., w r :  r * ↦ Q:  *, where * denotes application of the substitution u, and  1 *   1 *,...,  r *   r *. Hence (  E) can be applied to the conclusions of u(  P ) and u(  Q ). Call the resulting combined deduction  PQ ; its conclusion is u 1 :  1 *,..., u p :  p *, v 1 :  1 *,..., v q :  q *, w 1 :  1 *,..., w r :  r * ↦ PQ:c*.

47 Slides 05 47 Theorem (3E3) The relation ⊢ M:  is decidable, i.e. there is an algorithm which accepts any M and  and decides whether or not ⊢ M: . Proof: Apply the PT algorithm to M. If M is not closed or not typable we cannot have ⊢ M: . If M is closed and typable, decide whether  is an instance of PT(M).

48 Slides 05 48 Example: Apply the PT algorithm to check that the -term W  xy  xyy has the principal type (a  a  b)  a  b.


Download ppt "Slides 05 1 The Principal-Type Algorithm In general a typable term has an infinite set of types in TA. For example, it is possible to assign to I  x "

Similar presentations


Ads by Google