`

haskell - types and typeclasses - kinds

阅读更多

We have so far examine the types and typeclasses and we can make some types part of the typeclasses, however, we might have find out that some typeclasses expect to pass in a concrete type while some require to pass in some type which expect one or more type parameters. then how we determine which type classes requires some concrete types while some do not?

 

So, values like 3, "YEAH" or takeWhile (functions are also values, because we can pass them around and such) each have their own type. Types are little labels that values carry so that we can reason about the values. But types have their own little labels, called kinds. A kind is more or less the type of a type. This may sound a bit weird and confusing.

 

a few type examples.

ghci> :k Int  
Int :: * 
ghci> :k Maybe  
Maybe :: * -> *  

 

then let's examine the typeclass Functor, it is defined as below.

 

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

f is the type variable used in the typeclass, and because (f a) and f b, we can deduce taht the types that want to be friend with Functor has to be * -> *

 

so what if we are making our own types called Tofu. and it looks like this:

 

class Tofu t where  
    tofu :: j a -> t a j  

 

Let's see the kind of the types. Because j a is used as the type of a value that the tofu function takes as its parameter, j a has to have a kind of *. We assume * for a and so we can infer that j has to have a kind of * -> *. We see that t has to produce a concrete value too and that it takes two types. And knowing that a has a kind of * and j has a kind of * -> *, we infer that t has to have a kind of * -> (* -> *) -> *. So it takes a concrete type (a), a type constructor that takes one concrete type (j) and produces a concrete type.

 

So, let' start to make one with the type of (* -> (* -> *) -> *, and here is one way of doing this. 

 

data Frank a b  = Frank {frankField :: b a} deriving (Show)  

 

How do we know this type has a kind of * -> (* -> *) - > *? Well, fields in ADTs are made to hold values, so they must be of kind *, obviously. We assume * for a, which means that b takes one type parameter and so its kind is * -> *. Now we know the kinds of both a and b and because they're parameters for Frank, we see that Frank has a kind of* -> (* -> *) -> * The first * represents a and the (* -> *) represents b. Let's make some Frank values and check out their types.

 

so a few example can show: 

ghci> :t Frank {frankField = Just "HAHA"}  
Frank {frankField = Just "HAHA"} :: Frank [Char] Maybe  
ghci> :t Frank {frankField = Node 'a' EmptyTree EmptyTree}  
Frank {frankField = Node 'a' EmptyTree EmptyTree} :: Frank Char Tree  
ghci> :t Frank {frankField = "YES"}  
Frank {frankField = "YES"} :: Frank Char [] 

 

the details analysis on the result is left out, let see how we make Frank an instance of Tofu, because we see that tofu takes a j a (so an example type of that form would be Maybe Int) and returns a t a j. So if we replace Frank with j, the result type would be Frank Int Maybe.

 

instance Tofu Frank where  
    tofu x = Frank x  

 because we know that Frank is of type * -> (* -> *) -> *, but the value constructor is * -> *, because it only take one argument to construct the Frank object.  so in the example above, x is actually of type *.. but how does the runtime figures out the type parameter a and b??

so one requirement on the parameter x is that it can be decomposed in such a way 

x: *

while it should be decomposed to be 

  (* -> *) -> * -> *, which maps to frankField : b a

suppose x is Just 'a'

then

   b:: *->*:  Just  -- because Just has type * -> *

   a ::* :  Char --

 

   b a :: * : Just Char

and then it reorganize the types, and we get Frank type of Frank Char Just

 

So far we have examine types which matches the typeclasses requirement, but we may deal with some types which does not have an exact type match, e.g. suppose that we have define the following typeclass, 

 

data Barry t k p = Barry { yabba :: p, dabba :: t k }  

 

   Supose that we want to make it an instance of Functor. 

Functor wants types of kind * -> * but Barry doesn't look like it has that kind. What is the kind of Barry? Well, we see it takes three type parameters, so it's going to besomething -> something -> something -> *. It's safe to say that p is a concrete type and thus has a kind of *. For k, we assume * and so by extension, t has a kind of * -> *. Now let's just replace those kinds with the somethings that we used as placeholders and we see it has a kind of (* -> *) -> * -> * -> *. Let's check that with GHCI.

 

ghci> :k Barry  
Barry :: (* -> *) -> * -> * -> *  

 

we can partially apply the first two types parameters so we are left with * -> *, That means that the start of the instance declaration will be:instance Functor (Barry a b) where. If we look at fmap as if it was made specifically for Barry, it would have a type of fmap :: (a -> b) -> Barry c d a -> Barry c d b, because we just replace the Functor's f with Barry c d. The third type parameter from Barry will have to change and we see that it's conviniently in its own field.

 

 

instance Functor (Barry a b) where  
    fmap f (Barry {yabba = x, dabba = y}) = Barry {yabba = f x, dabba = y}  

 

There we go! We just mapped the f over the first field.

 

You may still wonder how does the "c d" come along in the "Barry c d a" notation. and what does the "c" and "d" in the "c d" notation binds to? well, it is all symbol logics, or mathematical logic (check this http://en.wikipedia.org/wiki/Mathematical_logic, where we first need to have a matched type of (* -> *), and because fmap is of type (* -> *) the correlation happens by the type parameters, a and b, while because the second and the third parameter has to be a *, and the second and third parameter has to associate wih the  typeclasses definition by parameter value (a for the second and b for the third)... we have to reserve a space for a and b.. A common way is to 1. first determine what we put as parameter, 2. determine location of the parameters, 3. with some transformation

  1. because partially applied Barry a b has type (*->*), we make instances for Barry a b.
  2. we have to reserve for the type parameters , in this case, fmap :: (a , b) -> f a -> f b, where a and b is fixed.
  3. we replace f with Barry x y (or you can also call it Barry c d) and we get fmap :: (a , b) ->  barry c d a -> barry c d  b
  4. *optional, you may want to deduce the types of like how c, d related to a and b, and their types. 

 

So remembers that types has little types of its own.

 

so in summary the example code is shown as below.

-- file
--  kinds_and_some_typefoo.hs
-- description: 
--   we'll take a look at formally defining how types are applied to type constructors, just like we took a look at formally defining how values are applied to functions by using type declarations.


-- QUOTE: 
--  (functions are also values, because we can pass them around and such) each have their own type. 
-- 

-- Types are little labels that values carry so that we can reason about the values. But types have their own little labels, called kinds.

--
-- how to check the kinds of a types ??
-- the key is the 
-- :k types o
-- e.g.
-- ghci> :k Int
-- Int :: * 

-- How quaint. What does that mean? A * means that the type is a concrete type. A concrete type is a type that doesn't take any type parameters and values can only have types that are concrete types.

-- 
-- checkig Maybe
-- ghci> :k Maybe  
-- Maybe :: * -> *  

-- 
-- checking Maybe Int
-- ghci> :k Maybe Int  
-- Maybe Int :: *  

-- more 
-- ghci> :k Either  
-- Either :: * -> * -> * 
-- 
-- ghci> :k Either String  
-- Either String :: * -> *  
-- ghci> :k Either String Int  
-- Either String Int :: *  


class Tofu t where  
    tofu :: j a -> t a j  

-- j a has to have a kind of *
--
-- so j has a kind of * -> * 
-- and t should be a type of  * -> (* -> *) -> *
-- So it takes a concrete type (a), a type constructor that takes one concrete type (j) and produces a concrete type. Wow.


data Frank a b  = Frank {frankField :: b a} deriving (Show) 
-- ghci> :t Frank {frankField = Just "HAHA"}  
-- Frank {frankField = Just "HAHA"} :: Frank [Char] Maybe  
-- ghci> :t Frank {frankField = Node 'a' EmptyTree EmptyTree}  
-- Frank {frankField = Node 'a' EmptyTree EmptyTree} :: Frank Char Tree  
-- ghci> :t Frank {frankField = "YES"}  
-- Frank {frankField = "YES"} :: Frank Char []


-- now we making Frank an instance of ToFu is preety simple 
instance Tofu Frank where 
   tofu x = Frank x
-- ghci> tofu (Just 'a') :: Frank Char Maybe  
-- Frank {frankField = Just 'a'}  
-- ghci> tofu ["HELLO"] :: Frank [Char] []  
-- Frank {frankField = ["HELLO"]}  


-- we did flex our type muscle, let's do some type-foo. We have this data type

data Barry t k p = Barry { yabba :: p, dabba :: t k }

-- ghci> :k Barry  
-- Barry :: (* -> *) -> * -> * -> *  

instance Functor (Barry a b) where  
    fmap f (Barry {yabba = x, dabba = y}) = Barry {yabba = f x, dabba = y}  

 

 

分享到:
评论

相关推荐

    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-英文版

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

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

    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-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-to-java

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

    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 - 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 ...

    Atom-ide-haskell-hie,用于hie的atom lsp插件(haskell ide引擎)。为Technix/IDE做出贡献.zip

    Atom-IDE-Haskell-HIE 是一个专门为 Haskell 开发者设计的 Atom 文本编辑器插件,它集成了 Haskell IDE Engine (HIE) 的 Language Server Protocol (LSP) 功能。这个插件允许开发者在 Atom 编辑器中享受到强大的代码...

Global site tag (gtag.js) - Google Analytics