haskell - types and typeclasses - typeclasses

 we've learned about some of the standard Haskell typeclasses and we've seen which types are in them. We've also learned how to automatically make our own types instances of the standard typeclasses by asking Haskell to derive the instances for us. 

 

You can think typeclasses are like interfaces in other languages. 

 

in this post we will see how to make our own typeclases and then make instances of the typeclases that we created. 

 

what we will cover in this post include the following. 

  • recap of the common typeclasses such as Eq
  • implements our own data tyoe TrafficLight and make it part of Eq, Show, and etc...
  • typeclasses constraint - such as one typeclasses are subclasses of another tyepclasses
  • typeclasses with type parameter, such as "instance Eq (Maybe  m) where", and other type of constraint such as "instance (Eq m) => Eq (Maybe m) where "
  • a customized typeclasses "yesno" 
  • recap on the functor typeclasses. 

Let's get a feel of typeclass "Eq" such as such .

 

 

class Eq a where  
    (==) :: a -> a -> Bool  
    (/=) :: a -> a -> Bool  
    x == y = not (x /= y)  
    x /= y = not (x == y)  

 

 

then we will make a trafficLight typeclass and we will make it part of Eq and Show. 

 

--file 
-- type_synonyms.hs
-- description:
--  type synonyms 102, where we will examine more on the Type classes


-- below is how the Eq type class is implemented in Prelude

-- check on this page on the information about
--  "Haskell - Redefining (hiding) arithmetic operators"
-- http://stackoverflow.com/questions/2388604/haskell-redefining-hiding-arithmetic-operators
import Prelude hiding (Eq, ShowS, (==), (/=))

class Eq a where 
   (==) :: a -> a -> Bool
   (/=) :: a -> a -> Bool
   x == y = not (x /= y)
   x /= y = not (x == y)

data TrafficLight = Red | Yellow | Green


-- unlike the used case where we will define some classes where 
--   deriving from some types 
-- but we 
--   define an instance of the Eq typeclasses
instance Eq TrafficLight where  
    Red == Red = True  
    Green == Green = True  
    Yellow == Yellow = True  
    _ == _ = False

-- actually we only need to fullfill the minimalist of the Eq classes like this:
-- class Eq a where  
--     (==) :: a -> a -> Bool  
--     (/=) :: a -> a -> Bool  

-- and you can also define the instance of Show typeclasses 
-- like this: 
instance Show TrafficLight where 
  show Red = "Red Light" 
  show Yellow = "Yellow light"
  show Green = "Green light"


-- ghci> Red == Red  
-- True  
-- ghci> Red == Yellow  
-- False  
-- ghci> Red `elem` [Red, Yellow, Green]  
-- True  
-- ghci> [Red, Yellow, Green]  
-- [Red light,Yellow light,Green light]  


-- makes types that are sub-types of others. 
--
-- class (Eq a ) => Num a where
--    ... 

-- it is like a writing of "class Num a where"o
-- only we state that our type a must be an instance of Eq
-- we are essentially saying that we have to make a type an instance of Eq before we can make it an instance of Num

-- in the code above, the a has to be a concrete type but Maybe  is not a concrete type, 
-- suppose that you are writing an instance of Eq on Maybe m
-- here is how you might have written it 
-- instance Eq (Maybe m) where
--    Just x == Just y = x == y 
--    Nothing == Nothing = True
--    _ == _ = False 


-- see the problems within?
-- you can compare the Maybe types, but you have no assurance that what the Maybe contains can be used with Eq!. 
-- that is why we have to modify our instance declaration like this :
instance (Eq m) => Eq (Maybe m) where 
    Just x == Just y = x == y  
    Nothing == Nothing = True  
    _ == _ = False 

-- we are saying 
-- QUOTE: we want all types of the form Maybe m to be part of the Eq typeclass, but only those types where the m (so what's contained inside the Maybe) is also a part of Eq. 


-- When making instances, if you see that a type is used as a concrete type in the type declarations (like the a in a -> a -> Bool)

-- NOTE:
-- you can 
-- info YourTypeClass
-- :info Maybe

 

 

About thte type constraint and one typeclasses being a sub-type of another typeclasses. we have seen from the example above that 

 

 

class (Eq a ) => Num a where
  ...

 

 

for a number type a, it first has to be a Eq type.  this is the one of the simple form of constraint, we can have other constraint as well, consider that for a Maybe type to be equable, we frist need to make sure that the types that Maybe contains is equable, so you can do like this: 

 

 

instance (Eq m) => Eq (Maybe m) where 
    Just x == Just y = x == y  
    Nothing == Nothing = True  
    _ == _ = False 

 

 

 

As I said before, we will make a typecalsses called Yes-no typeclasses. here it is 

 

-- file
--  type_classes_yesno_typeclasses.hs
-- description:
--  type classes exaple : an yesno type classes

class YesNo a where 
  yesno :: a -> Bool


data Tree a = EmptyTree | Node a (Tree a) (Tree a) deriving (Show, Read, Eq)

data TrafficLight = Red | Yellow | Green

instance YesNo Int where  
    yesno 0 = False  
    yesno _ = True 

instance YesNo [a] where  
    yesno [] = False  
    yesno _ = True

instance YesNo Bool where  
    yesno = id    

-- NOTE::
--   Joe's Note
-- We didn't need a class constraint because we made no assumptions about the contents of the Maybe
-- however, I can think of one , where you don't want  a Maybe a,
-- but the a is yet another Maybe, and the value is Nothing...
instance YesNo (Maybe a) where  
    yesno (Just _) = True  
    yesno Nothing = False  

instance YesNo (Tree a) where 
    yesno EmptyTree = False
    yesno _ = True


instance YesNo TrafficLight where  
    yesno Red = False  
    yesno _ = True  


-- ghci> yesno $ length []  
-- False  
-- ghci> yesno "haha"  
-- True  
-- ghci> yesno ""  
-- False  
-- ghci> yesno $ Just 0  
-- True  
-- ghci> yesno True  
-- True  
-- ghci> yesno EmptyTree  
-- False  
-- ghci> yesno []  
-- False  
-- ghci> yesno [0,0,0]  
-- True  
-- ghci> :t yesno  
-- yesno :: (YesNo a) => a -> Bool  


yesnoIf :: (YesNo y) => y -> a -> a -> a
yesnoIf yesnoVal yesResult noResult = if yesno yesnoVal then yesResult else noResult


-- ghci> yesnoIf [] "YEAH!" "NO!"  
-- "NO!"  
-- ghci> yesnoIf [2,3,4] "YEAH!" "NO!"  
-- "YEAH!"  
-- ghci> yesnoIf True "YEAH!" "NO!"  
-- "YEAH!"  
-- ghci> yesnoIf (Just 500) "YEAH!" "NO!"  
-- "YEAH!"  
-- ghci> yesnoIf Nothing "YEAH!" "NO!"  
-- "NO!"  

 As we can see, that we basically tell if a type which is part of the yesno typeclasses such as List, Tree, and Traffice light we made before. 

 

 

 

Now, as promised, we will present the Functor typeclass, which is basically for things that can be mapped over.  another thing about the Functor is that it does not want  you to provide a concrete type, because it expect one type , the function f expect to take one type construcor, we will see that for the [] type and the maybe types., 

Below is the Functor declaration. 

class Functor f where  
    fmap :: (a -> b) -> f a -> f b  

 

 and below is the instance declaration for the functor of type [] 

 

 

instance Functor [] where  
    fmap = map  

 

and below is how  you make some of the types parts of the Functor typeclasses. 

-- file 
--   type_classes_functor_class.hs
-- description:
--   defien a Functor type classes

-- QUOTE 
--  But now, the f is not a concrete type (a type that a value can hold, like Int, Bool or Maybe String), but a type constructor that takes one type parameter.
--  we see that fmap takes a function from one type to another and a functor applied with one type and returns a functor applied with another type.
-- 

import Prelude hiding (Functor, fmap, mapM, Left, Right, Either)

class Functor f where 
   fmap :: (a -> b) -> f a -> f b 


instance Functor [] where 
  fmap = map 

-- that's is!
--   instance Functor [a] where
-- notice how we didn't wirte instance Functor [a] where
--  because from fmap :: (a -> b) -> f a -> f b, we see that the f has to be a type constructor that takes one type. [a] is already a concrete type (of a list with any type inside it), while [] is a type constructor that takes one type and can produce types such as [Int], [String] or even [[String]].


-- Since for lists, fmap is just map, we get the same resutls when using htem on lists. 
-- map :: (a -> b) -> [a] -> [b]  
-- ghci> fmap (*2) [1..3]  
-- [2,4,6]  
-- ghci> map (*2) [1..3]  
-- [2,4,6] 


instance Functor Maybe where  
    fmap f (Just x) = Just (f x)  
    fmap f Nothing = Nothing  

-- and we can write the following . 
-- ghci> fmap (++ " HEY GUYS IM INSIDE THE JUST") (Just "Something serious.")  
-- Just "Something serious. HEY GUYS IM INSIDE THE JUST"  
-- ghci> fmap (++ " HEY GUYS IM INSIDE THE JUST") Nothing  
-- Nothing  
-- ghci> fmap (*2) (Just 200)  
-- Just 400  
-- ghci> fmap (*2) Nothing  
-- Nothing  

-- a Functor which is our Tree a
--  it can be thought of as a box in a way 
-- (holds several or no values) 
-- and the "Tree" type constructor takes exactly one type parameter
data Tree a = EmptyTree | Node a (Tree a) (Tree a) deriving (Show, Read, Eq)

singleton :: a -> Tree a
singleton x = Node x EmptyTree EmptyTree

treeInsert :: (Ord a) => a -> Tree a -> Tree a
treeInsert x EmptyTree = singleton x
treeInsert x (Node a left right)
    | x == a = Node x left right
    | x < a  = Node a (treeInsert x left) right
    | x > a  = Node a left (treeInsert x right)


treeElem :: (Ord a) => a -> Tree a -> Bool
treeElem x EmptyTree = False
treeElem x (Node a left right)
    | x == a = True
    | x < a  = treeElem x left
    | x > a  = treeElem x right




instance Functor Tree where  
    fmap f EmptyTree = EmptyTree  
    fmap f (Node x leftsub rightsub) = Node (f x) (fmap f leftsub) (fmap f rightsub) 

-- 
-- ghci> fmap (*2) EmptyTree  
-- EmptyTree  
-- ghci> fmap (*4) (foldr treeInsert EmptyTree [5,7,3,2,1,7])  
-- Node 28 (Node 4 EmptyTree (Node 8 EmptyTree (Node 12 EmptyTree (Node 20 EmptyTree EmptyTree)))) EmptyTree  

data Either a b = Left a | Right b deriving (Eq, Ord, Read, Show)

-- Well, if we wanted to map one function over both of them, a and b would have to be the same type.
-- 
instance Functor (Either a) where  
    fmap f (Right x) = Right (f x)  
    fmap f (Left x) = Left x  

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值