Lecture 2 Notes

2 4

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ¦¡ £¡ ©  ¢   4(' £¡ ¨)$  ¦ 4(' £¡ ) # E  & ¤ ¦ H ( &  H ('& #  & ('F H This is reasonable: The first argument must take an argument because it is given as an argument. It must produce an , because its value is used as an argument to the subtraction operator. And must be an , because it is also supplied as an argument to . ¢  ¤  ¢  ¤  ¦ 28 $ ¨$ ¦¡  ¦ 4(' £¡ ) Expression  Let us consider another example: type variables: . Again, we start by assigning Type Variable tf t0 t1 ¦ $ ¨$  ¦ £¡ ¦ 4(' £¡ ¨) $$ &  ¦ £¡  Next we write down the equations ¢ ¤    $ ¨$ ¦ £¡ ¦ 4(' £¡ ¨) $$ &  ¦ £¡  And next we solve: Equations t0 tf t1 tf t1 Expression Equations t0 t f t1 t1 tf Substitution Equations tf t1 Substitution t0 t f t1 ¢ Substitution t0 ( t1 ) tf t1 t1 ¢ ¤ 1¡ $ ¨$ ¦ £¡ ¢ ¢ ¤   ¤ ¢  ¤  ¤ Equations ¦ ¡ ¨) 4(' This means that we can assign the type t1 t1 , for any choice of t1 . Again, this is reasonable: we can infer that must be capable of taking an argument, but we have no information about the result type of , and indeed for any t1 , this code will work for any that takes an argument and returns a value of type t1 . We say it is polymorphic in t1 . ¦  ¢  ¦ ¤    ¢ ¦ 29  ¤ ©  $ % " $  £¡ © ¤ ¨¢  )  ¦ §¤ Let’s try a third example. Consider . Again, let’s assign type variables to each subexpression that is not a constant.  Type Variable tx t0 t1 ©  $ %    ¡ © $ ¤¢ ¥   )   ¦ §¤   £¡ © We then generate the equations Equations tx t1 t0 t0 tx t1 ¢ ¢ §¢   %( ¨( ©  $ %   2 ¤    Expression ¡ ©  ¤¢ ¥  )  ¤  $  ¤ ¦ §¤  £¡ © Processing these equations as we did before, we get Equations tx t1 t0 t0 tx t1 Substitution Equations t1 t0 t0 tx t1 Substitution tx %( ( ¢ %( ( 30  Expression 2  ¢  ¤  ¤ ¤  ¤  ¢ ¤ ¢ ¢ 2 ¢  ¤ Equations t0 tx t1 Substitution tx t1 t0 Equations t0 tx t1 Substitution tx t1 t0 Equations tx t1 Substitution tx t1 t0 %( ( 2 %( ( 2 %( ( ¢  ¢  ¤  ¤  ¤  ¢ ¤  ¤  ¤  ¤  ¢ ¢ ¤  ¢ ¢ ¤ ¢ ¢ 2 ¤ ¢ Since t x is already bound in the substitution, we apply the current substitution to the next equation, getting ¢  ¢ ¢ ¤ %( ( 2 ¤  ¤ ¢  ¤ ¢  ¤ %( ( t1 Substitution tx t1 t0 %( ( Equations 2 Oops! We have inferred from these equations that . So in any solution of these equations, . But and cannot be equal. Therefore there is no solution to these equations. Therefore it is impossible to assign a type to this expression. This is reasonable, since the expression uses as both a boolean and an integer, which is illegal in our type system.   $  £¡ ©   ¤ ¥¢  ¢  2 ¤ %( ¨( 2 ¢  ¤ %( ( 2 ¦ §¤  31 ©  % "  ¦ 4(' £¡ ) %( ¨(  ¨  ¢  ¤ ¦ £¡  4(' ¨) ¦ ¦ ¦¡ £¡ ¨)& 0(' ¦   2 ¦¡ £¡ ( & 0 '    ¦  ¨ Equations t0 tf t1 t1 t2 tf tf t2 ¦ ¦¡ £¡ ( & 0 '  ¦   ¦ £¡ 4(' ¨) ¦¡ £¡ ¨)& 0('  Equations t0 t f t1 t1 t2 tf tf t2 Substitution Equations t1 t2 tf tf t2 Substitution t0 t f t1 %( ¨( ¢ %( ( 2 ¤ %( ¨( ¢ 2 ¤  2 2  Substitution t0 t f t1 32 %( ¨( ¦ £¡ ¦ ¦ £¡ And we solve as usual: Equations t2 tf tf t2  Expression ¦¡ £¨¡ )$& 0(' Type Variable t0 tf t1 t2 ¦ Expression  ¨ Let us do one more example. Consider before. . We proceed as ¢  ¤ Equations tf tf t2 Substitution t0 t f t1 t2 %( ¨( 2 %( ( ¢ 2 ¤  Substitution t0 t f t1 t2 %( ¨( ¢ 2 %( ( ¢  ¤ 2  ¤ Equations tf tf Now we have a problem. We’ve now inferred that t f tf Int. But there is no type with this property, because the right-hand side of this equation is always larger than the left: If the syntax tree for t f contains k nodes, then the right hand side will always contain k 2 nodes. So if we ever deduce an equation of the form tv t where the type variable tv occurs in the type t, we must again conclude that there is no solution to the original equations. This extra condition is called the occurrence check. This condition also means that the substitutions we build will satisfy the following invariant: The no-occurrence invariant No variable bound in the substitution occurs in any of the right-hand sides of the substitution. Our code for solving equations will depend critically on this invariant. 33 ¡  ¢¢  ¡ ©  £¢ $ £¢ £ ¢¢ £ ¡ ¡...
View Full Document

Ask a homework question - tutors are online