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: Eq, Ord, Enum, Bounded, Show, Read.
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.
本文介绍如何在Haskell中为自定义类型实现类型类实例,包括使用deriving关键字自动推导Eq、Show等特性,以及手动定义特定行为。通过示例展示了如何使Person类型成为Eq、Show类型类的实例,并提供了比较和显示Person对象的方法。
1707

被折叠的 条评论
为什么被折叠?



