`

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  

 

 

分享到:
评论

相关推荐

    Atom-ide-haskell-hoogle,在光标下显示符号的滚动信息。对原子的贡献.zip

    Atom-ide-haskell-hoogle 是一个专门为 Atom 编辑器设计的插件,它整合了 Haskell 的 Hoogle 工具,以提供强大的代码提示和搜索功能。Atom 是一款由 GitHub 开发的开源文本编辑器,它利用现代 web 技术如 HTML、CSS ...

    haskell-mode emacs

    在 Emacs 中,`haskell-mode` 是一个专门为了提升 Haskell 开发体验而设计的模式。 `haskell-mode` 提供了多种增强功能,旨在帮助 Haskell 开发者更高效地编写、调试和理解代码。这个模式包括以下关键特性: 1. **...

    haskell-chart, haskell的2D 图表库.zip

    在数据可视化领域,`haskell-chart`库提供了一种高效且灵活的方式来创建2D图表,这对于数据分析、科学计算以及教学等场景非常有用。这个库是开源的,意味着任何人都可以查看其源代码,学习并贡献改进。 `haskell-...

    Atom-haskell-ghc-mod,哈斯克尔.zip

    **哈斯克尔编程语言与Atom-Haskell-GHC-Mod** 哈斯克尔(Haskell)是一种纯函数式编程语言,以其优雅的语法、强静态类型系统和编译时优化而受到程序员的喜爱。它鼓励使用不可变数据和惰性求值,这使得哈斯克尔在...

    Atom-haskell-debug,使用ghci在atom中实现图形haskell调试器.zip

    Atom-Haskell-Debug是针对Haskell开发者的一个强大工具,它允许你在流行的Atom文本编辑器中集成一个图形化的Haskell调试器。这个工具基于GHCi(Glasgow Haskell Compiler Interface),GHCi是Haskell的交互式环境,...

    Haskell-Data-Analysis-Cookbook, Haskell数据分析 cookbook的附带源代码.zip

    Haskell-Data-Analysis-Cookbook, Haskell数据分析 cookbook的附带源代码 Haskell-Data-Analysis-Cookbook这是 Haskell数据分析 cookbook的附带源代码。最新的源代码可以在GitHub上获得: ...

    haskell-ghc-mod:haskell-ghc-mod原子包

    从1.0.0开始,haskell-ghc-mod提供haskell-completion-backend服务。 注意:在1.0.0之前,提供了ide-backend服务。 它已被废弃以支持ide-haskell的UPI。 您可以在找到描述 执照 版权所有:copyright:2015 Atom-...

    haskell-ghc-mod:haskell-ghc-mod原子包

    haskell-ghc-mod原子包 该软件包主要用作后端。 Haskell ghc-mod打开通往ghc-modi的管道,并查询类型,信息并检查错误。 安装与配置 请参考官方文档站点 服务中心API 从1.0.0版本开始,haskell-ghc-mod提供...

    Get Programming with HASKELL-2018-英文版.pdf

    Get Programming with HASKELL-2018-英文版

    haskell-relational-record-driver-mysql:用于 haskell-relational-record 的 MySQL 驱动程序

    用于 haskell-relational-record 的 MySQL 驱动程序 这个项目被合并到 。 准备 $ git clone git@github.com:khibino/haskell-relational-record.git $ git clone git@github.com:bos/hdbc-mysql.git $ git clone ...

    Atom-atom-haskell-scry,扩散系数.zip

    【标题】:“Atom-atom-haskell-scry,扩散系数.zip” 涉及的主要知识点是 Atom 编辑器与 Haskell 语言的集成以及 SCRY 工具的使用。 【描述】:“Atom-atom-haskell-scry.zip”的描述指出,这个压缩包包含了一个名...

    Atom-ide-haskell-repl,原子中的ghci repl。对原子的贡献.zip

    Atom-ide-haskell-repl是针对Atom文本编辑器的一个扩展插件,专为Haskell编程语言提供集成的GHCi(Glasgow Haskell Compiler Interface)交互式环境,即REPL(Read-Eval-Print Loop)。这个插件允许开发者在Atom编辑...

    haskell-dev-tools:我用来安装升级与Haskell相关的开发工具的元项目

    在Haskell的世界里,开发环境的配置至关重要,而`haskell-dev-tools`就是一个方便的元项目,它专门设计用于简化Haskell开发工具的安装和升级过程。这个项目扮演了一个集合和自动化工具的角色,使得开发者无需手动...

    haskell-dap:Haskell DAP接口数据的实现

    Haskell-dap是一个开源项目,它实现了调试适应性协议(Debug Adapter Protocol,简称DAP)的接口,使得Haskell开发者可以充分利用这个协议进行程序调试。DAP是一个通用的、跨平台的协议,允许IDEs(集成开发环境)和...

    haskell-tools:Haskell的开发人员工具

    "haskell-tools"就是这样一个项目,它专注于为Haskell开发者提供一系列实用的辅助工具,以优化他们的开发流程。 ### 1. GHC:Glasgow Haskell Compiler GHC是Haskell的主要编译器,也是haskell-tools的重要组成...

    Atom-ide-haskell-cabal,IDE的Cabal后端提供商.zip

    Atom-ide-haskell-cabal.zip,Cabal backend provider for ide-haskellIDE Haskell Cabal套餐,atom是一个用web技术构建的开源文本编辑器。

    函数式编程-haskell-to-java

    ### 函数式编程:Haskell到Java的转换 #### 概述 本文旨在探讨函数式编程语言Haskell如何被编译或转换为Java语言。Haskell作为一种纯函数式编程语言,以其强大的类型系统、惰性求值机制以及高度抽象的能力在学术界...

    Haskell - The Craft of Functional Programming, 2ed (Addison-Wesley, 1999) by Tantanoid

    ### Haskell - The Craft of Functional Programming #### 一、概述 《Haskell - The Craft of Functional Programming》是一本深入探讨Haskell编程语言的经典著作。本书由Tantanoid编写,并于1999年由Addison-...

    haskell-brainfuck:Haskel 脑残翻译

    你可以在找到 haskell-brainfuck用法图书馆 import HaskBF.Evalimport qualified Data.ByteString.Lazy as BSimport Control.Monad.Statemain = do -- The following will evaluate the file using stdin and ...

    haskell-lsp-client:haskell-lsp的客户端库

    haskell-lsp-client 该软件包适用于希望使其文本编辑器与兼容的文本编辑器的开发人员。 我已经开发了此软件包,并计划将其集成到。 示例客户端 该存储库中包含一个示例客户端。 此示例客户端仅运行并打开在命令行...

Global site tag (gtag.js) - Google Analytics