Lecture 2 Notes

When given a procedure from integers to booleans it

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: given a boolean, it returns an integer. $ ¨$ $ ¨$ The value of has type . When given an integer, it returns an integer.  ©$  ¨$ ¡ ( & 0 ' % "  ¢  ¤¢ ¥  )  ¦ §¤ ¤ £©  £¡ %( ¨( ¡ 2 4' ¨(  The value of has no type in our type system. When given a boolean it might return either an integer or a boolean, and we have no type that describes this behavior.  $$ © ¨&  % "    ¤¢ ¥¡  ) ¦ §¤ ¡ ¡  4(' ¨)$ ¡ 4' ¨(  The value of has type . When given a boolean, it returns a procedure from booleans to integers. § ¥§  ©$  ¨$ % "     ¢  ¤ ¥© ¤ ¢¥ ¤ ¤ ¥¢ ¡  ) %(¨(  ¦ £¡ ¦¤ ¤ ¥© ¡ 2 ¦¡ ¢  ¤ 1¡ 4' ¨(  The value of has type . When given a procedure from integers to booleans, it returns an integer.   ¤  © % ( ¨(   ¦¡ 2 ¤ £© ¦¡ ¢ ¤¡ 1¡  4' ¨(  The value of has type t t for any type t. When given a procedure of type t , it returns a value of type t.   ¦¡ ¦ £¡  ¢ ¤ £©   ¤ 1¡ ¡ ¤¥©  4' ¨(  ¤ ¥©  ¢  ¤¡ 1¡ ¦4' £¡ ¨(  The value of has type t t t t for any type t. When given a procedure of type t t , it returns another procedure which, when given an argument of type t, returns a value of type t.   ¤ ¥©  5  ¡   ¤ £© ¡ ¤ ¥©  ¤ ¥© ¡ ¡ Let’s write down a definition that captures these examples. It will be defined by induction on t. (See, we are following the Design Recipe!) Definition 1 The property of an expressed value v being of type t is defined by induction on t: %#! )"© ¢ ¤ 5 An expressed value is of type 5 It is of type 5 It is of type t1 t2 iff it is a with the property that if it is given an argument of type t1 , then one of the following things happen:     %#!©%( $( . . %#!©4(' $"¨) 2 iff it is a iff it is a %( ¨(  2 ¡ 1. it returns a value of type t2 2. it fails to terminate 3. it fails with an error other than a type error. We occasionally say “v has type t” instead of “v is of type t.” Puzzle: in this system can a value val have more than one type? Puzzle: For the language LETREC, is it decidable whether an expression e has a value that is of type t ? 6 6.3 Assigning a type to an expression ¢ ¦ ¨)£ (©¡ Requirement: Write a procedure which, given an expression (call it exp) and a type environment (call it tenv) mapping each variable to a type, assigns to exp a type t with the property that: ¦ ) (© ¢ ¡ £ Specification of Whenever exp is evaluated in an environment in which each variable has the type specified for it by tenv, one of the following happens: 5 the resulting value has type t, 5 the evaluation does not terminate, or 5 the evaluation fails on an error other than a type error. Another way of writing the permissible outcomes: The evaluation does not cause a type error, and if it terminates, its value is of type t. Our analysis will be based on the principle that if we can predict the types of the values of each of the subexpressions in an expression, we can predict the type of the value of the expression. ¢ ¦  £ (© ¡ We’ll use this idea to write a specification for . We will write this specification as a set of inference rules, as we have done elsewhere. Assume that tenv is a type environment mapping each variable to its type. Then we should have: 7 $ %# tenv t2 t2 # # H t1 t1  $ 9# $ 5# $%# # $ 9# $ 9# B¨¨@ $ ¢¢¦A9# t tenv t t $ 5# # tenv t2 t2 $ 5# D [email protected]$9#  ('& $ tenv  $ 5# tenv var $ %# # $ 5# $ 5# # tenv # tenv # 8 rator rand £!4¥©§©B©B( ©¢¦¤¢ 1 ¨§¥£¡ ©©E¤¢ ¨§¥£¡ ¨§¥£¡ ©©E¤¢ rator tenv rand tenv e1 e2 e3 £¤!"¥©§ GF ©¢¦¤¢ & ¨§¥£¡ "¢©E¤© ¨§¥£¡ "¢©E¤© ¨§¥£¡ ¨§¥£¡ "¢©E¤© e1 tenv e2 tenv e3 tenv £¤!4¥¢§ " ©¢¦¤¢ ¥B ¨§¥£¡ ©¢¦¤¢ ¨§¥£¡ ¨§¥£¡ ©¢¦¤¢ var e1 body C £¤!"¥©§©8¢337 ©¢¦¤¢ ¨2¥6 ¨§¥£¡ ¨§¥£¡ ©¢¦¤¢ e1 £!¥§2 ) ¤4¢31 0 var £!¥ ¨ ¤"©§  num  ('& ( ( ( ( ( ( ( ( e1 tenv t1 body var t1 tenv ( ( ( e1 tenv ( ¨§¥£¡ ©¢¦¤¢ ¨§¥£¡ ©¢¦¤¢ ( ( Simple typing rules What about procedures? 4(' ¨) If ( x) e has type (t1 t2 ), then it is intended to be called on an argument of type t1 . When its body e is evaluated, the variable x will be bound to a value of type t1 . This suggests the following rule: tenv tvar H # C var body t2 t2 # t1 tenv $ var D £¤!"¥©©¢33£  ©©E¤¢ §¨2 ¨§¥£¡ ¨§¥£¡ "¢©¦© ( body  9# $ $ 5# ( 4(' ¨)$ There’s only one proble...
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