Short Quiz 6 Example type rules 1) What is the purpose of type system? (correct errors of input / write program easily / both ) •2) IfWboth operands ofin the operators of hich system is included type arithmetic system ? ( type checking system / type inference system / both ) addition, subtraction, and multiplication are of type integer, then the result is of type integer (Pascal 3) What is the result of following C program ? (notice the ascii code for a is definition). 96) Rule for + (analogue rules for − and ∗): Example type rules #include <ctype.h> #define toupper(c) ( (islower(c)) ? _toupper(c) : (c) ) int a = 'm'; a = toupper(a-­‐-­‐); 1 •(If both of the arithmetic 2operators of printf “%d”, a ) ;operands addition, subtraction,1 and 2multiplication are of type 94 integer, then the result is of type integer (Pascal definition). 4) If both operands of the arithmetic operators of addition, subtraction, and Rule for forresult − and multiplication are + of t(analogue ype integer, rules then the is of ∗): type integer (Pascal definition). Rule for + (analogue rules for − and * ): E � e : integer E � e : integer E � (e + e ) : integer where E is a type environment that maps constants and variables to their types. E � e2 :two integer E � ewith 1 : integer In combination the following axioms in the e2) and : integer type system {c : Eα}� �(e1c+: α {v : α} � v : α Eenvironment isinfer, a typethat environment maps constants where Ewhere is a now type that constants nd variables to their we can (2maps + 3) isthat of atype integer: types. Pand lease finish the tto ype inference procedure ) variables their types. E � 2 : integer E � 3 : integer the In combination with the following two axioms in E : �α}|-­‐ (2�(2 + 3) :and integer : integer type system {c E c :+ α 3) {v : α} � v : α we can now infer, that (2 + 3) is of type integer: where E E == {2 {2:: integer, integer, 33: :integer}. where integer}. E � deduction 2 : integer proofs E � 3 work : integer In general, type bottom up. E � (2 + 3) : integer CS 314 spring’13 lecture 27, page 4 where E = {2 : integer, 3 : integer}. the result objectofreferred to by&the operand. the operand • The the unary operator is aIfpointer to is ofThe type “foo”, the type of result is a result of thethen unary is athe pointer the •object referred to by& operator the operand. If to the operand “pointer to foo”. and definition) the object referred(C to by theC++ operand. If the operand is of type “foo”, then the type of the is a is of type “foo”, then the type of the result result is a 5) Please write the type inference procedure for following C/C++ program: tounary foo”.&(C and C++ definition) “pointer foo”. and C++ definition) The r“pointer esult oto f the (C operator(It means to get the pointer of current E�e:α symbol/variable/function name) is a pointer to the object referred to by the operand. If the operand is of type “foo”, then what is the type of result? E�e:α E � &e : pointer(α) E � &e : pointer(α) E�e:α E� : pointer(α) E &e |-­‐ &e : pointer(α) • Two expressions can only be compared if they have • Two expressions can only be compared if they have the sametypes. types. The is ofistype boolean. the same Theresult result of type boolean. 6) Please finish the following type inference of following C/C++ program: Two expressions can only be compared if they have the same types. The result is of type boolean. : α Ebe� compared e2 : α � e1only • Two expressionsEcan if they have : α E � e : α E � e E � (e11= e2) : boolean 2 the same types.EThe result is of type boolean. � (e = e ) : boolean 1 E |-­‐ (e1 = 2 e 2) : Boolean E � e2 for : αother E �which e1 :isαa placeholder α is a type variable, type expressions.E � (e = e ) : boolean 1 2 α is a type variable, which is a placeholder for other type 7) expressions. Please write down result of following ML program: CS 314 -­‐ 1 * 2 + 3 * 4 ; val it = 14 : int -­‐ "abc" ^ "def"; val it = "abcdef" : string CS 314 -­‐ if (1 < 2) then 3.0 else 4.0; val it = 3.0 : real -­‐ 1 < 2 orelse (1 div 0) = 0; val it = true : bool CS 314 spring’13 lecture 27, page 6 α is a type variable, which is a placeholder for other spring’13 lecture 27, page 6 type expressions. spring’13 lecture 27, page 6 8) What is wrong with following ML program: -­‐ 10 / 5; stdIn:5.1-­‐5.5 Error: operator and operand don't agree [literal] operator domain: real * real operand: int * int -­‐ #"a" = #"b" or 1 = 2; stdIn:1.1-­‐2.8 Error: operator and operand don't agree [literal] operator domain: bool * bool operand: bool * int -­‐ 1.0 = 1.0; stdIn:1.1-­‐1.10 Error: operator and operand don't agree [equality type required] operator domain: ''Z * ''Z operand: real * real -­‐ if (1<2) then 3 else 4.0; stdIn:1.1-­‐2.14 Error: types of if branches do not agree [literal] then branch: int else branch: real -­‐ if (1<2) then 3; no such construct 9) What is the result of the following ML program ? -­‐ square 7 + 6 ; 55 10 ) What if anything is wrong with each of the following expressions? trunc 5 stdIn:1.1-­‐1.8 Error: operator and operand don't agree [literal] trunc (fn: real à int) operator domain: real operand: int ord (fn: char à int) ord "a" chr (fn: int à char) stdIn:1.1-­‐1.8 Error: operator and operand don't agree [tycon mismatch] str (fn: char à string) operator domain: char operand: string if 0 then 1 else 2 stdIn:1.1-­‐7.10 Error: test expression in if is not of type bool [literal] test expression: int in expression: if true then 1 else 2.0 stdIn:1.1-­‐7.15 Error: types of if branches do not agree [literal] then branch: int else branch: real chr(trunc(97.0)) val it = #"a" : char chr(trunc 97.0) val it = #"a" : char chr trunc 97.0 stdIn:9.1-­‐9.15 Error: operator and operand don't agree [tycon mismatch] operator domain: int operand: real -­‐> int