Lecture 2 Notes

0 4 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: m: if we are trying to compute the type of a expression, how are we going to find the type t1 for the bound variable? It is nowhere to be found. There are two standard designs for rectifying this situation: 5 Type Checking: In this approach the programmer is required to supply the missing information about the types of bound variables, and the typechecker deduces the types of the other expressions and checks them for consistency. 5 Type Inference: In this approach the type-checker attempts to infer the types for the bound variables based on how the variables are used in the program. If the language is carefully designed, the type-checker can infer all or most of the types of the bound variables. We will study each of these in turn. 9 6.4 CHECKED: A Type-Checked Language New language will be the same as LETREC, except that we require the programmer to include the types of all bound variables (except for ). For bound variables, we also require the programmer to specify the result type of the procedure as well. ¢  ¢  4'  )¨ % % $  £¡ ©   0(' £¡ ¨)&         ¤ ¢  ¤ 1¡  % ¦ £¡ % 2   ( )¢ ¢ ¤  %    ©  ( ¢¡ )£¡ © 2 ¨ ¡  ¤   ¤   £¡  %  £¡ ¢  ¢ ¤  ¤ ¥© ¤   % %( ( ( )¢ 2  2 ¢ ( )¢ 2  2  ¡ ¢  4'  $$¨ % % 4(' ) 4(' ¨) ¤  ( )¢ ¦ £¡ ¤  4(' ¨) In Java or C one might write something more like ¢  £¡ © ¡ )& 0(' itself is  $ ¤ ¥¢ , but the type of § ¥© ¢ ¦ §¤  ) is  ¢  The result type of . $#  £¡ ©  ¡¡¡ ¤£¢   0 ' £¡ ( &  ¦¤ ¢  ¤¡ 4(' ¨) ¢  4'  $$¨ % ¢      ¤ ¡ %  2  ( )¢ ¢ ¡¡ ¤¤¡ ¤   ¤ This is confusing:  % ( )¢ ¢ ¤  % ( )¢ 5 5 If the types are long, it’s hard to read, as in the 3rd example above: ), with the type of    ¡ )$& 0('  (which is 2  it confuses the result type of (which is . 2   ¢ ¦ £¡  ¢  ¤¡ 4(' ¨)$  ¦   ¢ ¤ ¥© ¤  ¤ ¤ ¥© ¢  ¤ 1¡ %( ¨( 2 ¡¡ 4(' ¨) This is not much of a factor in Java, but it’s a problem in C, and we’ll see even more complicated types when we get to chapter 6 (modules). 10 tvar H tenv # tvar tenv tres tres # D $ var  9# $ $ 9# 11 var tvar body C body £!4©©¢¨ 2 £  ¥§ ¨§¥£¡ ©¢¦¤¢ s:  ¢   ¨  £ ' $"¢ ¡ © #!© ( 2 '#!© $"$¢   (  2   #  ©4(' ") ¡¢ ¥"( 2 ¡¥¢ ¡ £ ( © $% ©4¨) ('   ' ¡  ¡ ¨§¥£¡ ©¢¦¤¢ ¤ ¢  4'  $$¨ % ¡ £¢ 2  "¨(  ©4 ' ¢  ©4'  "$$¨ % 2 ¢ ¢ ©  ) ¢ ©4'  ¨"$¨ % Type  Identifier '#! $£¡ Type Expression  Type Identifier Expression  ©4('  "¨)$ Identifier ¡¥¢ (  ( ( Rule for ¡ Expression :: 4(' ¨) Expression :: Expression Changed productions for CHECKED ¢  t var  ¢  4'  )¨ % ¢  4'  $ % ¡ t res p var eletrec-body looks like  ? A typical 4'  $ % What about eproc-body  ¤ This expression declares a procedure named p, with formal parameter var of type t var and body eproc-body . Hence the type of p should be t var tres . ¢  4'  $$¨ % Each of the expressions in the , eproc-body and eletrec-body , must be checked in a type environment where each variable is given its correct type. We can use our scoping rules to determine what variables are in scope, and hence what types should be associated with them. In eletrec-body , the procedure name p is in scope. As suggested above, p is declared to have type tvar tres . Hence eletrec-body should be checked in the type environment tenvletrec-body [ p (tvar tres )]tenv What about eproc-body ? In eproc-body , the variable p is in scope, with type t var tres , and the variable var is in scope, with type t var . Hence the type environment for eproc-body should be [var tvar ]tenvletrec-body tenvproc-body Furthermore, in this type environment, eproc-body should have result type tres . Writing this down as a rule, we get: t eproc-body eletrec-body # tenv tenv tres tenv $ %# tvar ) $ D# tres tres $ 9# H CD $ $ 12 $ tres p (var tvar H p D# C £¤!"¥©§(¥32( 4B  "¢©¦© ¥ ¨§¥£¡ C "©¢¦© ¨§¥£¡ ¨§¥£¡ "©¢¦© ( eproc-body var tvar eletrec-body p tvar $ 9# ( ( t 13 Now we can transcribe the rules into a program, just as we’ve been doing all along.  ¨ ¢¢ © ¡ ©...
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