haskell - types and typeclasses - Derived Instances

Now that you have made  your own type and typeclasses. how to make these two associated. As you might have known that a type can be made an instance of a typeclasss if it supports that behavior. 

e.g. An Int type type is an instance of the Eq typeclass because  the Eqtypeclass defines behavior for stuff that can be equated. And because integers can be equated, Int is a part of the Eq typeclass. The real usefulness comes with the functions that act as the interface for Eq, namely == and /=. If a type is a part of the Eq typeclass, we can use the == functions with values of that type. That's why expressions like 4 == 4 and "foo" /= "bar" typecheck.

 

 Haskell can automatically make our type an instance of any of the following typeclasses: EqOrdEnumBoundedShowRead

 

we will see from this post the following. 

  • with the deriving keyword 
  • with the instances keyword
  • instance constraint- such as a type is a sub-type of another. 

Let's see the Person type that derives from the type such as Show

-- file 
--   derived_instances.hs
-- description:
--   a typeclaess is a sort of interface that defines some behaviors
--   a type can be made of an instance of a typeclass if it supports the behavior
-- for stuff tha can be equated
-- e.g. the Eq typeclass, where Int is a part of the Eq typeclass, the real  usefulness of the Eq typeclass we can use 
-- == function with values of that types. 
-- this is why the expression like 4 == 4 and "foo" /= "bar" typecheck.


-- a person class that has implemented the person class

-- data Person = Person { firstName :: String  
--                      , lastName :: String  
--                      , age :: Int  
--                      } deriving (Eq)


-- ghci> let mikeD = Person {firstName = "Michael", lastName = "Diamond", age = 43}  
-- ghci> let adRock = Person {firstName = "Adam", lastName = "Horovitz", age = 41}  
-- ghci> let mca = Person {firstName = "Adam", lastName = "Yauch", age = 44}  
-- ghci> mca == adRock  
-- False  
-- ghci> mikeD == adRock  
-- False  
-- ghci> mikeD == mikeD  
-- True  
-- ghci> mikeD == Person {firstName = "Michael", lastName = "Diamond", age = 43}  
-- True  


-- now, let make the Person class part of the typeclass of 
--  String, String, Int

data Person = Person { firstName :: String  
                     , lastName :: String  
                     , age :: Int  
                     } deriving (Eq, Show, Read)  

-- ghci> let mikeD = Person {firstName = "Michael", lastName = "Diamond", age = 43}  
-- ghci> mikeD  
-- Person {firstName = "Michael", lastName = "Diamond", age = 43}  
-- ghci> "mikeD is: " ++ show mikeD  
-- "mikeD is: Person {firstName = \"Michael\", lastName = \"Diamond\", age = 43}"  


-- ghci> read "Person {firstName =\"Michael\", lastName =\"Diamond\", age = 43}" :: Person  
-- Person {firstName = "Michael", lastName = "Diamond", age = 43} 


--  We can also read parameterized types, but we have to fill in the type parameters. So we can't do read "Just 't'" :: Maybe a, but we can do read "Just 't'" :: Maybe Char.


-- you can even compare Nothing with Just a 
-- because Nothing comes before Just so Nothing is smaller than anything else. 
-- 

-- ghci> Nothing < Just 100  
-- True  
-- ghci> Nothing > Just (-49999)  
-- False  
-- ghci> Just 3 `compare` Just 2  
-- GT  
-- ghci> Just 100 > Just 50  
-- True  

-- we cannot do this
-- Just(*3) > Just (*2)
-- because functions are not comparable.


-- data Day = Monday | Tuesday | Wednesday | Thursday | Friday | Saturday | Sunday 


-- you can make it a part of the Enum typeclass

data Day = Monday | Tuesday | Wednesday | Thursday | Friday | Saturday | Sunday   
           deriving (Eq, Ord, Show, Read, Bounded, Enum)  

-- 

minBound :: Day
maxBound :: Day 

succ Monday
pred Saturday


-- 

 

As we have seen that in this example, when we derive the person classes from the Bounded and Enum class, so we have gain the ability to apply minBound and Succ/Pred on the Day instances?

 

Though we might have a dedicate section which we will discuss the how to make instance type to an typeclasses, but let's me to give you a preview on what it looks like ..

 

 

data TrafficLight = Red | Yellow | Green

instance Eq TrafficLight where  
    Red == Red = True  
    Green == Green = True  
    Yellow == Yellow = True  
    _ == _ = False

instance Show TrafficLight where 
  show Red = "Red Light" 
  show Yellow = "Yellow light"
  show Green = "Green light"

-- to specify some sort of constraint, such as below
-- where to compare maybe types, you need to first be 
-- able to compare what the maybe contains. 

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

 

We will introduce this more in details on the typeclasses 102. 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值