Presentation of Type Classes
Presentation of Type Classes
Presentation of Type Classes
Improve Yourself.”
Imam Hussain(AS)
while(!presentationOver)
{ Listen(); }
MADE BY
ABDULLAH AMMAR 16BSCS-10
SARTAJ KHOKHAR 16BSCS-11
BILAL NIZAMANI 16BSCS-33
TANVEER KHOKHAR 16BSCS-43
What are type classes?
▶ TYPE
▶ Types are sets of values.
▶ TYPE CLASSES
▶ Type classes are the sets of types.
▶ Understand once you realize that types are sets of
values and type classes are sets of types.
The type integer includes value like{1,2,3} and type classes
contain like float , double and integer.
Note: Haskell has been lazy and not commiting to a specific type.&
everthing have types, even functions Have also types.
We can also ask type of + Operator, since plus is infix operator.
What do you think is the type of the head function? Because head takes a list
of any type and returns the first element, so what could it be? Let's check
➢ Defining our own types also improves the type safety of our code:
Haskell will not allow us to accidentally mix values of two types that
are structurally similar but have different names.
How Declare Type Class Instance?
Now that you know how to define typeclasses, it’s time to learn how to define instances
of typeclasses. Now let’s see how we could make Color type a member of the BasicEq3 class:
-- file: ch06/eqclasses.hs
instance BasicEq3 Color where
isEqual3 Red Red = True
isEqual3 Green Green = True
isEqual3 Blue Blue = True
isEqual3 _ _ = False
In this case, we can use isEqual3 on any type that we declare is an instance of BasicEq3, not just this one color
type. We could define equality tests for anything from numbers to
graphics using the same basic pattern. this is exactly how you can make Haskell’s == operator work for your own
custom types.
Continue..
Note: also that the BasicEq3 class defined both isEqual3 and
isNotEqual3, but we implemented only one of them in the
Color instance. That’s because of the default implementation
contained in BasicEq3. Since we didn’t explicitly define
isNotEqual3, the compiler automatically uses the default
implementation given in the BasicEq3 declaration.
Built-In type Classes
In Haskell, every statement is considered as a mathematical expression
and the category of this expression is called as a Type. You can say that
"Type" is the data type of the expression used at compile time.
To learn more about the Type, we will use the ":t" command. The
different Inbuilt types are:
• Int
• Float
• Char
• Bool
Example :
Data means that we’re defining a new data type. The part before the =
denotes the type, which is Bool. The parts after the = are value constructors.
The specify the different that this type can have.
The | is read as OR . So we can read this: the Bool type can have a value of
True or False. Both the type name and the value constructors have to
capital cased
Show Type Class
Show has a functionality to print its argument as a String. Whatever may be its
argument, it always prints the result as a String. In the following example, we
will print the entire list using this interface. "show" can be used to call this
interface.
It will produce the following output on the console. Here, the double quotes
indicate that it is a String type value.
Read Type Class
Read interface does the same thing as Show, but it won’t print the result in
String format. In the following code, we have used the read interface to read a
string value and convert the same into an Int value.
Here, we are passing a String variable ("12") to the readInt method which in
turn returns 12 (an Int value) after conversion. Here is its output
Come, Come, Whoever you are
Maulana Jalaluddin
Rumi
Thanks