This image be our episode 2,8778 entitled Type Class in in Haskell and in part of the series Haskell. It is posted by Tuku Toroto, and in about 19 minutes long, and Karima Klienflag. The summary is. Tuku Toro explains what type Class in are and how to use them. This episode of HBR is brought to you by an honest host.com. Get 15% discount on all shared hosting with the offer code HBR15. That's HBR15. Get your web hosting that's honest and fair at an honest host.com. Hello, this is HBR15, and this is Tuku Toro. This episode is about type Class in Haskell. I feel like I'm doing this episode in a bit of a form all of it. I guess it's better late than never. Type Classes are the Haskell value of doing ad hoc polymorphism or overloading, so you can define a set of functions that can operate on a more than one specific type of data. For example, Equality is one of these things that is done with the type classes. In Haskell, there is no default Equality, it has to be always as the meeting find. In some other languages, the Equality is defined by default as a comparison of the memory location, so if you are comparing two pieces of data and they are in the same location as the memory, so they are actually the identical then it's the equal and in those languages you can of course define your own equals operator or equality operator that actually looks into the data instead of just comparing the location of the data, but not in the Haskell. The Haskell you have to, there's no default Equality, it has to be defined and it's never about memory location, so there's two parts in this piece, the first thing is the that class definition which defines the common interface, define what operations are available to any data that has instance of this type class, and then it define and the other part is the actual instance definition which defines the implementation for that type, then played or interface and that's for specific type of data, so basically the type class has a, that class definition is generic, it usually has a slot of empty space where the data calls and the actual definition puts your own data in that specific slot, so for example the equality, the type class definition is written as class equal A, where equals operator is type of A to A to Boole and not equals operator is type of A to A to Boole and then the standard definition x, not equals y is not x equals to y, so there's a, in the ego, type class, there's two operators, equals and not equals, both of those data, two type, two pieces of data of the same type and written Boole, indicating if these are same or not the same, and then there's the default definition for the not equals which is just the inverse of the equals, it is written as a not x equals y, so it uses the equality comparison and the inverse of the inverse itself, and the important part, well all of this is important, but the interesting part here is that it starts with a class equal equals the name of the type class A, where and A is a lower case, it's a type parameter and that A is used in the operators, for example, equals operator is type of A to A to Boole, so whatever you are, whatever you are making instance of this, that type is the A and that A is used in the equality comparison, it's a bit more, it probably sounds a bit confusing with my explanation, but if you have a look at the show notes, it's probably clear, so you can create this definition as a class equal A that has two functions with following signatures and implementation, in other words, given two A, this function determinants are the equal or not, that's the whole return type, not equal is defined in terms of equal, so it's enough to define one and you get another function of three, but you can totally still define both of them, if you are so inclined, if you can, if you have some clever way of optimizing the not equal operator, for example, so let's continue with our example, we could have our own data type called size, meaning it is just an algorithm, so it's a beta, size equals small by medium by large, so our size can have three values, small medium and large, and to define instance of the equal for this, we just write instance E, O, size, where, so now we are putting size in place of A, and then we have to write equal operator, and I'm not going to write it in a certain case, so small equals small is true, medium equals medium is true, as equals large is true, and then underscore equals underscore is false, so what I'm saying here is that if given two small ones, small values, this will return as a true, the equal comparison, same when you are given two medium or two large, and in every other case we are going to return false, and they're not equal operator, we are not defining, we are using the standard this isn't a deal, you just the inverse of the equality, so with this this isn't it, now we could compare size, we could write small equals small, and we could get true or large, not equals large, and get false, of course writing these things by hand can be bit tedious and you know you feel like you are repeating yourself, so has can can, has can can derive these false, so we can have the same data definition that data says equals small by medium by large, deriving, so free, so hungry, other classes, so is used to turn the, our data type into the string, and pretty used to turn the string into our data type, and equal is the one what we defined manually here, but we can tell the has to to derive equal for us, it can derive that in quite many occasions, you can have pretty complex data and has to do that, there are some limitations there and that I'm not going to explore right now, because I probably will zoom into all of them, but in simple case like this we don't have to define anything, you can just tell has to do the target, and it will find it correctly for us, the kind of costier, I don't think, between the task classes that obviously is appropriate, wrong word is used here, but technically a hierarchy between the task classes, for example this is a type class or that is used to compare the ordering of items, and that is written as a class EQA, set arrow or a, where, and then technically some different definitions, so this is written as a class or a, where a has instance of EQ with follow function and there's a follow function, there's a, compare that is a to a to ordering, then there's a less than a to a to a to a to a to a to a to a to a to a to a to a to a to a to a to a to a to a, so whenever you have a order in your code something something that's a order instance, you can, you have access to this, and you of course have, and you have the access to the EQA, EQA, the course or spill into, you can define ordering without having the EQA, so, and it's enough to implement either the comp, or the less, or EQA, there's a has can can define the rest of the functions in terms of this, one of these two, so for example our size, then it would be instance or size, where small, less or equal to underscore is true, meaning small is always small or equal to everything, medium, less than or equal to small falls, medium, less or equal to underscore, through a, meaning that small is a, meaning that medium is not smaller or equal than small, but for everything else, it is small or equal, and then large, it less than equal, large is true, large, less than equal to underscore falls, meaning that large is always less or equal to large, and it's not less or equal to anything else, and with that definition we can compare our sizes and sorry order that we can say, if, for example, medium is a greater or equal than small, old EQA falls for us, and there's lots and lots of classes in the standard library, for example, number of numerical parations, integrable for integer numbers, floating for floating numbers, like you can have, you can totally can have your own data type, and then you can define integrable instance for that, and you get numerical parations for integrable numbers, this is true, that I mentioned already, turning data into strings, entry for turning strings into the data, NUG is interesting, it's for second order types, depending the NUG type, for example, NUG instance, you can say, and bounded is for things with upper and lower lower bound, for example, our size is pretty good on the day for having a both NUG and bounded, because you can say, which is the lower bound, that would be the small and upper bound is large, all the values are in between this, and NUG is for NUG, I think them, you can, for example, say, give me all the values from small to large, and then it will give small, medium and large for you, all you can even say that, if you combine NUG and bounded, you can say that, give me all the values starting from the main bound, the lowest bound, and you don't have to specify the upper bound, and if you automatically start from the small and go to the NUG, in order to the largest, and if you later on add in your data, NUG, after the large extra large, then it will automatically work in the other places of your program, you are asking all the values starting from the lower bound, it will produce a small, medium, large extra large, with that you have to change the code, this is pretty nice, so you can write really generic code, and you start using the type classes, I have a convaluited, made of example here, because check that, it is a type method of check, or I show a fat arrow, a to a to spring, so it takes anything that has instance for the ordering and showing, so you can compare the ordering, I mean you can compare the, yeah, you can compare the ordering, which one is picatin, which one and you can turn the data into string, there's nothing, no other limitations here, and it's written as a check, AB is case compare AB of LT arrow, so a quotation is smaller than quotation, so B, so if you compare these two values that are given to the function, and A is less than B, then it's going to produce a string, where A is smaller than B, and it's going to use the show to turn A into the string, so if it's a number, the number will be turned into the string, if it's our size, then we are going to get a text that represents our data, and then there's two more cases GT arrow, so A plus is greater than plus, so B, so if the A is greater than B, we are going to get a string A is greater than B, where A and B string represents our data, and last one is equal arrow, so A plus, and so P plus are equal, so what is ink time, so it takes two parameters that are of same type, both types have order and row instances, that's only thing we know about the data, or this for order and so it's turn, so it's for turning data into string, that is very useful displaying it, and if produce a string telling the result of comparison, so if we return to our size data, we can write up check medium small, and we are going to get a string, medium is greater than small, if we write check small large, we are going to get a string small is small than large, we can write check 7, 3, design index, 7 is greater than 3, that's a string, we can even write check square bracket 1, 2, square bracket square bracket 1, 1, 1, 1, square bracket, and we are going to get a string square bracket 1, 2, square bracket is greater than square bracket 1, 1, 1, 1, square bracket, so it does a comparison of the what that was, it does a comparison to a list, and the comparison of the list, in a way that is compares the first element, and if they are saying it compares the next element, and next element until it finds a first one, where the comparison is different and irregularly, and that is what defines that is, which one of the list is greater of, yeah, greater. So, our simple check function, that does not know anything about the data that it can, it handles except that they can be ordered and they can be turned into strings, then handle very different parts of paper. And there are very many extensions to the five classes to add more behavior, and there are somewhat more complicated to use, but they also are really interesting options, for example, it is possible to define a part class that takes two type parameters, A and D for example, so you can have much more complex behavior, but that power comes at a cost to cost, code is more complex, and you have to take into account things that you normally would have to take into account. But those are, I like sometimes just reading what kind of extensions they are, for example, for the type classes. That's about what I have to say about these things now, I hope this clears up a lot of things that I have been talking about earlier, that I really should have done this with earlier, but I didn't originally, I didn't intend to teach that much of Haskell, but in the order, for example, that's how it seems to have occurred. So, thanks for listening, questions and comments and all kinds of feedback that is always welcome, this is the first way to catch me, not as it is either part of e-mail or at the fair universe, where I'm able to wrap up my thoughts on some sort of social media. Arastra! You've been listening to Hecker Public Radio at HeckerPublicRadio.org. We are a community podcast network that releases shows every weekday Monday through Friday. Today's show, like all our shows, was contributed by a HBO listener like yourself. If you ever thought of recording a podcast, then click on our contributing to find out how easy it really is. HeckerPublicRadio was founded by the digital.com and the informomicon computer club and is part of the binary revolution at binrev.com. If you have comments on today's show, please email the host directly, leave a comment on the website or record a follow-up episode yourself on their otherwise status. Today's show is released on the creative comments, attribution, share a like, free.au license.