1 00:00:01,010 --> 00:00:07,609 welcome back an I'll be at some fun with curry and two balls and function types 2 00:00:07,609 --> 00:00:10,740 about we're going to talk about now is at 3 00:00:10,740 --> 00:00:14,839 type clash and polymorphic functions 4 00:00:14,839 --> 00:00:18,180 an Baltimore for functions 5 00:00:18,180 --> 00:00:21,960 are functions dead are not defiant 6 00:00:21,960 --> 00:00:26,269 on concrete types but that are defiant using type variable 7 00:00:26,269 --> 00:00:33,180 and here is a.m. a very polymorphic example of a polymorphic funtion 8 00:00:33,180 --> 00:00:36,280 the function Lancs so if I want to calculate 9 00:00:36,280 --> 00:00:40,270 the length of a list every lead doesn't matter at all 10 00:00:40,270 --> 00:00:43,700 what the type of the elements of the list is 11 00:00:43,700 --> 00:00:47,530 if I have the list 123 order lists 12 00:00:47,530 --> 00:00:51,129 it three characters ABC help 13 00:00:51,129 --> 00:00:54,640 both in both cases the length love this list three 14 00:00:54,640 --> 00:00:58,620 so the type of the elements doesn't matter 15 00:00:58,620 --> 00:01:02,960 when I want to calculate the length of the list when we look at the type a 16 00:01:02,960 --> 00:01:03,949 blank here 17 00:01:03,949 --> 00:01:08,470 we see it length has type list of a 18 00:01:08,470 --> 00:01:13,049 arrow and and that a is a type very ball 19 00:01:13,049 --> 00:01:16,229 has called thats you can see that biggest 20 00:01:16,229 --> 00:01:19,409 types start with an uppercase per case 21 00:01:19,409 --> 00:01:24,159 letter here so n you can see that and is a concrete die because it started in 22 00:01:24,159 --> 00:01:27,210 obrigado I and the a here 23 00:01:27,210 --> 00:01:30,680 the lower case a that's a type variable 24 00:01:30,680 --> 00:01:34,040 to really but this type says is that 25 00:01:34,040 --> 00:01:37,060 length has type for all types a 26 00:01:37,060 --> 00:01:40,600 list a to end an 27 00:01:40,600 --> 00:01:45,020 and if you're used to Java generics are c-sharp generics 28 00:01:45,020 --> 00:01:49,049 and typically the 29 00:01:49,049 --> 00:01:52,759 type fair ball is declared explicitly 30 00:01:52,759 --> 00:01:56,000 am and in the type of the function so 31 00:01:56,000 --> 00:02:00,030 for example in c-sharp you will be fine length of tea 32 00:02:00,030 --> 00:02:03,090 and and and would have argument list of tea 33 00:02:03,090 --> 00:02:06,640 and return an int but in Haskell this dick this 34 00:02:06,640 --> 00:02:09,940 a here is since it's a dive hairball 35 00:02:09,940 --> 00:02:13,520 its implicitly quantified am so you don't have to kind 36 00:02:13,520 --> 00:02:16,530 appointed an angle brackets or anything again 37 00:02:16,530 --> 00:02:20,860 and has college optimized for this case ride 38 00:02:20,860 --> 00:02:25,070 but the price that you pay is that type variables start 39 00:02:25,070 --> 00:02:28,140 lower case and type start with an uppercase 40 00:02:28,140 --> 00:02:31,930 K but if we look at this type here lank 41 00:02:31,930 --> 00:02:37,630 has type for any type A Lancs takes a list of values of Taipei 42 00:02:37,630 --> 00:02:41,730 and returns an integer again this is what you expect 43 00:02:41,730 --> 00:02:45,570 and it doesn't matter what did I i of the 44 00:02:45,570 --> 00:02:48,670 element is of the list your only interested 45 00:02:48,670 --> 00:02:51,870 in counting how many there are but you're not 46 00:02:51,870 --> 00:02:55,000 interesting interested what these values actually 47 00:02:55,000 --> 00:03:00,100 again so that should hear the length of false true 48 00:03:00,100 --> 00:03:03,380 then in this case a hedged eyeball 49 00:03:03,380 --> 00:03:06,510 s2 and here we have a list of 50 00:03:06,510 --> 00:03:10,190 integers so a would-be in that case ent 51 00:03:10,190 --> 00:03:14,240 but the land here is for a showboat 52 00:03:14,240 --> 00:03:17,950 in both cases an we can use a length 53 00:03:17,950 --> 00:03:21,060 of died for all AAA to end 54 00:03:21,060 --> 00:03:24,660 and in the first case a is pool and in the second case 55 00:03:24,660 --> 00:03:27,810 agent as a set 56 00:03:27,810 --> 00:03:31,920 the price we pay for not having to declare 57 00:03:31,920 --> 00:03:36,010 you know on what type length is polymorphic 58 00:03:36,010 --> 00:03:39,170 is that we differentiate the namespaces 59 00:03:39,170 --> 00:03:42,470 of types and dive verboten ist and has called 60 00:03:42,470 --> 00:03:46,730 type variables must begin with a lower case letter 61 00:03:46,730 --> 00:03:50,330 and by convention reuse 62 00:03:50,330 --> 00:03:53,630 ABC and Zach are four types variables 63 00:03:53,630 --> 00:03:57,020 barriers in languages like Java our C sharp 64 00:03:57,020 --> 00:04:00,120 typically we use S&T are 65 00:04:00,120 --> 00:04:03,959 for type variables capital St nr 66 00:04:03,959 --> 00:04:07,560 in Haskell its lower case AB and C so 67 00:04:07,560 --> 00:04:11,140 where in sharp you would write list ft 68 00:04:11,140 --> 00:04:14,239 in as call you dried list Ave 69 00:04:14,239 --> 00:04:17,620 okay here are an 70 00:04:17,620 --> 00:04:22,419 a number of polymorphic functions from the Haskell standard prelude to the 71 00:04:22,419 --> 00:04:27,069 stand the prelude is dick and Haskell SDK or the Haskell BC I L 72 00:04:27,069 --> 00:04:31,189 and a standard library like every language comes with the standard library 73 00:04:31,189 --> 00:04:35,710 and and the types of these functions 74 00:04:35,710 --> 00:04:40,169 reveal why the law about their implementation show for example let's 75 00:04:40,169 --> 00:04:41,180 look at the first one 76 00:04:41,180 --> 00:04:44,530 first takes ate up all of 77 00:04:44,530 --> 00:04:48,289 a NB and returns a value of type A 78 00:04:48,289 --> 00:04:51,539 but can destruction do I'll 79 00:04:51,539 --> 00:04:56,780 it think should %uh Paul a and it returns a value of type A 80 00:04:56,780 --> 00:05:00,039 so really it's quite obvious from that type 81 00:05:00,039 --> 00:05:04,500 that what this function does is it gonna readers the first 82 00:05:04,500 --> 00:05:10,389 and element of the double now let's look at the type of hat 83 00:05:10,389 --> 00:05:13,770 had to take a less to pay for any type A 84 00:05:13,770 --> 00:05:17,189 and readers and a in this case 85 00:05:17,189 --> 00:05:21,069 you have more choice right because you know I given a list of Taipei 86 00:05:21,069 --> 00:05:24,779 if I have to return a value of type A I can pick 87 00:05:24,779 --> 00:05:29,580 Andy value in that last and had to get you the first one 88 00:05:29,580 --> 00:05:34,259 now one of the interesting things here's what happens 89 00:05:34,259 --> 00:05:37,949 when a.m. you give head to the empty list 90 00:05:37,949 --> 00:05:41,460 in that case this function will return 91 00:05:41,460 --> 00:05:44,699 undefined are well-drawn exception an 92 00:05:44,699 --> 00:05:48,029 so that is not given in this types are the types said 93 00:05:48,029 --> 00:05:51,289 for all lists you now of Taipei 94 00:05:51,289 --> 00:05:54,479 and will return a value of type A an 95 00:05:54,479 --> 00:05:58,669 but that's not really true because when I gave it the empty list 96 00:05:58,669 --> 00:06:02,639 it will fail and not really give me 97 00:06:02,639 --> 00:06:06,330 value third example here take 98 00:06:06,330 --> 00:06:10,169 takes an integer and readership function 99 00:06:10,169 --> 00:06:14,180 that takes a list and returns a list remember 100 00:06:14,180 --> 00:06:17,449 when we have a dive with arrows the 101 00:06:17,449 --> 00:06:20,740 parentheses show she to the right to take 102 00:06:20,740 --> 00:06:24,460 is if we put in the brand she's would be end arrow 103 00:06:24,460 --> 00:06:27,789 brent is %eh lester a to a 104 00:06:27,789 --> 00:06:31,500 take takes number and 105 00:06:31,500 --> 00:06:38,500 last takes the first and elements have the less and returns that 106 00:06:38,510 --> 00:06:40,260 not an extra engineers ship 107 00:06:40,260 --> 00:06:44,770 is each Nystrom and it takes two lists 108 00:06:44,770 --> 00:06:47,930 and and readers a list of bears 109 00:06:47,930 --> 00:06:51,370 and the way it works is dead it takes to do less and 110 00:06:51,370 --> 00:06:54,630 takes each element these delicious and combines them 111 00:06:54,630 --> 00:06:58,050 into a double now I think 112 00:06:58,050 --> 00:07:01,580 dive ship looks a little bit nicer 113 00:07:01,580 --> 00:07:06,120 when you don't write that encourage form so when you would ride it i blessed 114 00:07:06,120 --> 00:07:10,199 an isn't %uh Paul's it would have died 115 00:07:10,199 --> 00:07:13,220 couple a blessed a endless to be 116 00:07:13,220 --> 00:07:18,030 to list of double and a comedy show there you can really see in the type 117 00:07:18,030 --> 00:07:21,729 that and sip an 118 00:07:21,729 --> 00:07:26,550 in our tourists to Leicester Oct and then the loss function here 119 00:07:26,550 --> 00:07:32,010 an that's perhaps the simplest function you can imagine has college the identity 120 00:07:32,010 --> 00:07:32,690 function 121 00:07:32,690 --> 00:07:37,050 it takes at value of Taipei and breeders valuable time 122 00:07:37,050 --> 00:07:41,460 died a for any a a man if you think a little bit about that 123 00:07:41,460 --> 00:07:46,110 the only thing this function can do is you know just return its value because 124 00:07:46,110 --> 00:07:46,900 otherwise 125 00:07:46,900 --> 00:07:50,090 it will be hard to work on Anne 126 00:07:50,090 --> 00:07:53,289 Taipei okay so an 127 00:07:53,289 --> 00:07:56,300 the identity goes from 128 00:07:56,300 --> 00:07:59,680 ole a2a jodie 129 00:07:59,680 --> 00:08:03,669 only thing really that it can do is it can take the argument 130 00:08:03,669 --> 00:08:07,380 and immediately returned if you're interested 131 00:08:07,380 --> 00:08:11,270 in in this thing where you know you look at the type and then you get the 132 00:08:11,270 --> 00:08:12,120 property 133 00:08:12,120 --> 00:08:15,289 about the implementation you she should read 134 00:08:15,289 --> 00:08:18,539 bill Watters paper teams for free and 135 00:08:18,539 --> 00:08:21,889 and yard that paper will explain that all 136 00:08:21,889 --> 00:08:26,190 everybody more for dive gives you a tear and that holds 137 00:08:26,190 --> 00:08:29,410 and for that function and 138 00:08:29,410 --> 00:08:32,450 but again that's a little bit advance so 139 00:08:32,450 --> 00:08:36,490 you can read that if you want there will be a link to the paper 140 00:08:36,490 --> 00:08:40,560 on the core structure the Las topic 141 00:08:40,560 --> 00:08:44,620 ever going to do and in the context functions 142 00:08:44,620 --> 00:08:47,410 are overloaded functions 143 00:08:47,410 --> 00:08:51,040 we all know overloaded function most languages support them 144 00:08:51,040 --> 00:08:54,780 for example in Java you can overlords 145 00:08:54,780 --> 00:08:58,430 function with different parameter type so 146 00:08:58,430 --> 00:09:02,570 a.m. I can define a function food addiction and 147 00:09:02,570 --> 00:09:07,230 and then I can have an overloaded takes a ball and then I can I have a number 148 00:09:07,230 --> 00:09:11,800 another overloaded take Celeste and the name 149 00:09:11,800 --> 00:09:15,930 up that function is to shame but the types of the parameters 150 00:09:15,930 --> 00:09:20,990 is different and Haskell overloaded is 151 00:09:20,990 --> 00:09:24,400 you done in a slightly different form and 152 00:09:24,400 --> 00:09:28,100 it corresponds more to the notion of interfaces 153 00:09:28,100 --> 00:09:31,870 in object-oriented languages joined Haskell 154 00:09:31,870 --> 00:09:35,740 when you overloaded function is really mean 155 00:09:35,740 --> 00:09:39,970 me means not overloading in the object-oriented sans 156 00:09:39,970 --> 00:09:43,260 it really mean stager restricting 157 00:09:43,260 --> 00:09:46,850 the die did the 158 00:09:46,850 --> 00:09:51,060 types of the parameters so in this case when I want to see some 159 00:09:51,060 --> 00:09:55,420 the elements on the list and I cannot say 160 00:09:55,420 --> 00:09:58,870 that type some hedge type lester a2a 161 00:09:58,870 --> 00:10:02,430 because wat by and 162 00:10:02,430 --> 00:10:06,320 have alleged characters I cannot 163 00:10:06,320 --> 00:10:12,460 at characters together and maybe you can say all I can define threat characters 164 00:10:12,460 --> 00:10:13,670 but thats a tan 165 00:10:13,670 --> 00:10:17,150 that we have a list of tuples well 166 00:10:17,150 --> 00:10:21,620 in that case some on the list of top also of Taipei gonna be 167 00:10:21,620 --> 00:10:26,240 doesn't really return a double up a committee that doesn't make sense 168 00:10:26,240 --> 00:10:29,870 so what we want to do is you want to restricted type 169 00:10:29,870 --> 00:10:33,580 the elements in the list such that they can be some 170 00:10:33,580 --> 00:10:36,720 and that is what has school overloading 171 00:10:36,720 --> 00:10:40,380 issues so what you see here in this type some as type 172 00:10:40,380 --> 00:10:43,620 numb a double arrow 173 00:10:43,620 --> 00:10:47,960 list a to a so what this means 174 00:10:47,960 --> 00:10:51,040 is that some doesn't take 175 00:10:51,040 --> 00:10:54,430 alleged values have to pay for any a 176 00:10:54,430 --> 00:10:57,459 but only values of Taipei 177 00:10:57,459 --> 00:10:59,189 that 178 00:10:59,189 --> 00:11:01,680 are in this numb Kalash 179 00:11:01,680 --> 00:11:04,730 or N if you think about 180 00:11:04,730 --> 00:11:08,999 and object orientation it only takes a list of 181 00:11:08,999 --> 00:11:12,290 value of type A that implemented in phnom 182 00:11:12,290 --> 00:11:16,529 interface okay so I you see this notation here numb 183 00:11:16,529 --> 00:11:20,819 a arrow double arrow got roughly 184 00:11:20,819 --> 00:11:25,480 if your job I receive sharp or PHP programmer 185 00:11:25,480 --> 00:11:28,959 ya at CBS plus programmer think of this 186 00:11:28,959 --> 00:11:32,889 as you know restricting the values are 187 00:11:32,889 --> 00:11:36,339 on which this can work job you have the same thing if you have a 188 00:11:36,339 --> 00:11:39,379 generic function you can restrict 189 00:11:39,379 --> 00:11:42,540 the values by requiring 190 00:11:42,540 --> 00:11:46,819 that the types implement the shirt and interface in c-sharp you can do the same 191 00:11:46,819 --> 00:11:48,300 thing in 192 00:11:48,300 --> 00:11:52,160 you can most languages allowed as and has got no 193 00:11:52,160 --> 00:11:56,569 exception alright SoCon takes a list 194 00:11:56,569 --> 00:11:59,860 and readers it into a value but they can 195 00:11:59,860 --> 00:12:03,189 only do that when values in the list 196 00:12:03,189 --> 00:12:06,509 support numb class Gordon um interface 197 00:12:06,509 --> 00:12:10,939 okay 198 00:12:10,939 --> 00:12:14,100 so in this case we can do chum of integers 199 00:12:14,100 --> 00:12:18,069 then in that case agent and we know 200 00:12:18,069 --> 00:12:21,639 that integers and our number so 201 00:12:21,639 --> 00:12:25,689 that's fine we will see what this number lies in a tournament like basically says 202 00:12:25,689 --> 00:12:28,439 that you can do addition and multiplication and so on 203 00:12:28,439 --> 00:12:32,860 second one here is a unless the floating-point numbers 204 00:12:32,860 --> 00:12:36,889 I can sum them as well but here 205 00:12:36,889 --> 00:12:40,079 we get it I para de las 10 year we get it i pair 206 00:12:40,079 --> 00:12:43,759 because here we have a list of characters and 207 00:12:43,759 --> 00:12:47,300 characters don't support dunno interface 208 00:12:47,300 --> 00:12:51,410 our game show characters are not an instanceof the number loss 209 00:12:51,410 --> 00:12:55,699 and Haskell speak and has clashed 210 00:12:55,699 --> 00:13:00,379 the b-sides nominee several type classes you can define your own type classes we 211 00:13:00,379 --> 00:13:02,269 will see how to do that later 212 00:13:02,269 --> 00:13:04,160 and in this case 213 00:13:04,160 --> 00:13:07,860 the numb class says that you have 214 00:13:07,860 --> 00:13:11,029 addition K what that means is that 215 00:13:11,029 --> 00:13:14,460 the type of addition takes 216 00:13:14,460 --> 00:13:18,790 value of type A readers a function that takes another value 217 00:13:18,790 --> 00:13:21,910 Taipei and then burned returns a value of type A 218 00:13:21,910 --> 00:13:26,290 but it's not only for that's not for all values of k 219 00:13:26,290 --> 00:13:29,589 only values in the number clash similarly 220 00:13:29,589 --> 00:13:32,820 when you want to compared to values for equality 221 00:13:32,820 --> 00:13:37,080 they have to be in the EQ class showing here you see 222 00:13:37,080 --> 00:13:40,310 that the equality operator as type 223 00:13:40,310 --> 00:13:43,820 EQ okay double arrow a arrow 224 00:13:43,820 --> 00:13:46,890 a arable and finally 225 00:13:46,890 --> 00:13:50,510 there's the type of values that you can compare 226 00:13:50,510 --> 00:13:54,660 so the order types and their less than has type 227 00:13:54,660 --> 00:13:57,959 or no pay arrow a RA arable 228 00:13:57,959 --> 00:14:01,940 okay so here are some 229 00:14:01,940 --> 00:14:05,510 exercises the exercises and you 230 00:14:05,510 --> 00:14:09,800 you will do an on the website so I wont give you 231 00:14:09,800 --> 00:14:12,880 and the answers right now but their 232 00:14:12,880 --> 00:14:17,400 actual sizes will be on the website but I'll walk you through this exercise 233 00:14:17,400 --> 00:14:22,529 an out to get you familiar showed the question is like one ish the type 234 00:14:22,529 --> 00:14:28,070 of an the value of their lists 235 00:14:28,070 --> 00:14:31,100 it three characters AB and C 236 00:14:31,100 --> 00:14:34,480 and so on and so on show and 237 00:14:34,480 --> 00:14:38,459 please doing these exercises biggest day well I make sure 238 00:14:38,459 --> 00:14:42,180 that you understand the idea of types that we 239 00:14:42,180 --> 00:14:45,310 an explained in this lecture 240 00:14:45,310 --> 00:14:49,190 and many are a couple of more 241 00:14:49,190 --> 00:14:52,529 in complicated exercises an 242 00:14:52,529 --> 00:14:56,380 where you don't get a value and you have to compute the type 243 00:14:56,380 --> 00:14:59,410 but you get a function and now you have to 244 00:14:59,410 --> 00:15:03,640 a computer died function I'm and the trick here 245 00:15:03,640 --> 00:15:07,320 and this is a.m. and well I'm going to 246 00:15:07,320 --> 00:15:10,329 you know the secret I'm going to reveal to you 247 00:15:10,329 --> 00:15:13,100 is that you can use GHz 248 00:15:13,100 --> 00:15:17,380 to check your answers show you're doing the exercises don't go I got 249 00:15:17,380 --> 00:15:20,420 try them out randomly let you do it 250 00:15:20,420 --> 00:15:23,800 open up GATT and used a go long time 251 00:15:23,800 --> 00:15:27,470 and functionality to do these exercises 252 00:15:27,470 --> 00:15:30,480 okay so you you you are 253 00:15:30,480 --> 00:15:34,750 sure that you can score a hundred percent on every one of them 254 00:15:34,750 --> 00:15:39,150 so thank you and happy hacking so use GC 255 00:15:39,150 --> 00:15:43,990 to do your exercises and and if you don't use haskell 256 00:15:43,990 --> 00:15:48,340 and but another language make sure that you're also using 257 00:15:48,340 --> 00:15:51,840 your compiler to check your tight okay 258 00:15:51,840 --> 00:15:55,600 because we as humans should not do this type checking 259 00:15:55,600 --> 00:15:58,740 that's why we have computers to do that for us 260 00:15:58,740 --> 00:16:01,230 thank you very much and see you next week