Lecture 2 Notes

For example our next equation gives a value for t1 we

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:  0 ¢  4'  $$¨ % ¤  £¡  £¡  ¢ ¢ ( $  ! $" %( ¨( ¢ ¢ ( ¡ 0 2  ¤ To specify this syntax, we add a new non-terminal, Optional-type, and we modify the productions for and to use optional types instead of types. ¢   Optional-type Expression  ¡ ¥¢ ( 2 ©4(' "¨)$  Identifier  ¡¢ ¥"( # 2  ) ¡ ( ¡ '#! $£¡ ¢¢© ¡ © )£ # ¡ 4(' ¨)  "¨(  ©4 ' ¢  4'  $ % ¢   ©4'   "$ %  2 ¡ ¢ © 4  '  ¥"( "$$¨ % 2   2  )£ $¢ ¡ (©'#!© ( '#!© $"¢ (   ©4(' "¨)$ )¢ $ % ¡ (© '©4(' ¨) ¡ ¢ ¢  Optional-type Identifier Expression Expression   Optional-type  ¢ ¡ £ Identifier ¡ £¢ ¡ )£ © ( ¡  :: ¢  0 :: Expression Type  4' ¨(  Optional-type :: Expression 4'  $$¨ % Optional-type :: ¤ ¢  The omitted types will be treated as unknowns that we need to find. We do this by traversing the abstract syntax tree and generating equations between these types, possibly including these unknowns. We then solve the equations for the unknown types. To see how this works, we need names for the unknown types. For each expression e or bound variable var , let t e or tvar denote the type of the expression or bound variable. For each node in the abstract syntax tree of the expression, the type rules dictate some equations that must hold between these types. 19 For our PROC language, the equations are: ¢ rator rand ¨%%#! §§$" t tbody )     ©0( &  )' £¡  "¨(  ¡ ©4 '  $"4 ¡ ©%%# ¨! © ) %( ( ¨¥£ ©¦& e1 e2 e3  ¨¥£ ©¦& e1 e2 e3  t t ©¦¦¤ ¨§¥¢ (trand (tvar ¤  ¨ §§©¢ var body  ¢   2 : t e1 t e2 t e3 ¨¥¥£¡ ©§¦¤¢  ©¦¤ $§1¡ e1 e2 e3 2 rator rand : trator :t %( ( ¤   ¦ ¥£¡ ©¦ ¤¢ var body e1  ¢  : t e1 t  e1 ¤ e1 e2 ¢ ¤ : t e1 t e2 t  e1 e2 5 The first rule says that the arguments and the result of a be of type . 5 The second rule says that the argument of a and its result is a . 5 The third rule says that the type of a expression is that of a procedure whose argument type is given by the type of its bound variable, and whose result type is given by the type of its body. 5 The fourth rule says that in a procedure call, the operator must have the type of a procedure that accepts arguments of the same type as that of the operand, and that produces results of the same type as that of the calling expression. 5 The last rule says that in an expression, the test must be of type , and that the types of the two alternatives must be the same as the type of the entire expression. must all ¢  ¤ must be an , ©0(' ¨)$& ¢ %( (  ¤ 2 4(' ¨) %( ¨( ¦ §¤ 2 ¦¤ 20 If we had multiargument procedures and abstractions, the equations for procedures and procedure calls would be t en ) t tvarn ) e0 e1 ©© © (tvar1 ¨%%#! §§$" H varn ) e te en  ¥ ¡¡ ¥ ! © ¤ 21 (t e1 ¢¢ ££¢ en : t e0 ¤ (var1 ¥ # ¦¦ ¨§¦ ¡¡ e0 e1 :t ¡¡ varn ) e ) ¥ (var1 H  ¢¨ 2 £ ( £!¥§BB1 ¤"©¢¢© To infer the type of an expression, we’ll introduce a type variable for every subexpression and every bound variable, generate the constraints for each subexpression, and then solve the resulting equations. To see how this works, we will infer the types of several sample expressions.  ¦ £¡ ¦¡ £¡ ©  4(' £¡ ) ¦ ¡ ¨) 4(' Let us start with the expression . We begin by making a table of all the bound variables and applications in this expression, and assigning a type variable to each one.      Expression Type Variable tf tx t0 t1 t2 t3 t4 ¦    ¦ £¡  & ¦¡ £¡ ©   ¨  ¦¡ 4' £¡ ¨(   ¦ 4(' £¡ ¨) ¦ £¡   ¦¡ ¦¡ £¡ ©   £  ¦¡ £¨¡ ©  £¡ ¨)  4('  ¡   ¦ £¡ Now, for each compound expression, we can write down a type equation according to the rules above. t1 t2 Equations 1. t0 tf 2. t1 tx 3. t3 4. t4 5. t2 6. t f 7. t f tx      ¦ £¡ ¤   ¦¡      ¤   ¨  ¢ ¢ ¢  & ¦¡ £¡ ©  ¢ Expression t4 t3 4' £¡ ¨(  ¦¡ £¨¡ ©   ¦£¡   ¦ 4(' £¡ ¨)  4(' £¡ ¨) ¡ ¦¡ £¡ © ¤ ¤     5 5 Equation 2 says that produces a procedure that takes an argument of type t x and produces a value of the same type as that of . 5 Equations 3–5 say that the arguments and the result of the subtraction in are all integers.    ¦¡ £    ¦¡ £¡ ©   ¦ £¡   4(' £¡ )    ¦ ¡ © ¡ ¦ ¡     ¦¡  ¡ ¨) 4('    ¦ ¡ © ¡ ¡ ¦¡ £¡ © ¦ £¡ ¦ £...
View Full Document

This document was uploaded on 03/17/2014 for the course CSG 111 at Northeastern.

Ask a homework question - tutors are online