| Register | FAQ | Calendar | Search | Today's Posts | Mark Forums Read |
|
#1
| |||
| |||
| This is questions 3.2.3 (f) from Ullman's Elements book. The questions is to figure out what the types of b,c,d,e are for the following function fun f(a:int,b,c,d,e)= if b<c then d else e My answer is that b and c must be of the same type(< operator). Also d and e must be of the same type since the then and else branches of an if must have the same type. a is not used but is known to be of type int. Other than that we cannot further tell which are the types of b,c,d,e. Now, Running the code in the sml interpreter, however, shows that b and c are of type int. val f = fn : int * int * int * 'a * 'a -> 'a How is it that the int type is inferred for b and c? |
|
#2
| |||
| |||
| sail0r wrote: > This is questions 3.2.3 (f) from Ullman's Elements book. > The questions is to figure out what the types of b,c,d,e are > for the following function > fun f(a:int,b,c,d,e)= > if b<c then d else e > > My answer is that b and c must be of the same type(< operator). Also d > and e must be of the same type since the then and else branches of an if > must have the same type. a is not used but is known to be of type int. > Other than that we cannot further tell which are the types of b,c,d,e. > > Now, Running the code in the sml interpreter, however, shows that > b and c are of type int. > val f = fn : int * int * int * 'a * 'a -> 'a > How is it that the int type is inferred for b and c? In fact b and c could be of any "comparable" type. However if you step back at page 63 (at least according to the edition I own) and re-read rule 6 of type inference you'll find the answer. Once you've realized that, you can have a further proof making the REPL print the type of the < operator with: op <; Bye. |
![]() |
| Thread Tools | |
| Display Modes | |
In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.