gpt4 book ai didi

Haskell 导出展示实例

转载 作者:行者123 更新时间:2023-12-04 17:22:38 24 4
gpt4 key购买 nike

我正在玩一棵红黑树:

-- Taken from Okasaki 1999
module RedBlackTree where

--node coloring data
--a node is R (red) or B (black)
data Color = R | B

--tree constructor
--a RBT can be E (empty) or be T (a non empty tree)
data RBT e = E | T Color (RBT e) e (RBT e)

--set operations on tree
type Set a = RBT a

--define an empty set
empty :: Set e
empty = E

--define a member of a set
--Sees if an item of type e is
--in a set if type e elements
member :: (Ord e) => e -> Set e -> Bool
member x E = False
member x (T _ a y b) | x < y = member x a -- if less, go left
| x == y = True
| x > y = member x b -- if more, go right


--tree operations
--Insert an element
insert :: (Ord e) => e -> Set e -> Set e
insert x s = makeBlack (ins s)
where ins E = T R E x E --basically the typical BST insert
ins (T color a y b) | x < y = balance color (ins a) y b
| x == y = T color a y b
| x > y = balance color a y (ins b)
makeBlack (T _ a y b) = T B a y b --inserts a black node

-- balance operations
--case 1:
balance B (T R (T R a x b) y c) z d = T R (T B a x b) y (T B c z d)
--case 2:
balance B (T R a x (T R b y c)) z d = T R (T B a x b) y (T B c z d)
--case 3:
balance B a x (T R (T R b y c) z d) = T R (T B a x b) y (T B c z d)
--case 4:
balance B a x (T R b y (T R c z d)) = T R (T B a x b) y (T B c z d)
--generic balancing operation
balance color a x b = T color a x b

如果我在 GHCi 中执行以下语句:
> RedBlackTree.insert ('b') (RedBlackTree.T R E ('a') E)

以下错误消息告诉我没有显示 Set Char 的实例:
<interactive>:116:1:
No instance for (Show (Set Char)) arising from a use of `print'
Possible fix: add an instance declaration for (Show (Set Char))
In a stmt of an interactive GHCi command: print it

我知道树正在工作,因为通过调用 member 'b' ...在哪里 ...是之前执行的语句,返回值为 True .我一直在阅读有关此问题的其他 SO 帖子,但为他们找到的解决方案(例如: Haskell: Deriving Show for custom type )不起作用。

例如,通过添加:
instance Show Set where:
show (Set Char) = show Char

当我尝试使用 :l 加载时收到以下错误消息:

:l red-black-tree.hs [1 of 1] Compiling RedBlackTree ( red-black-tree.hs, interpreted )


red-black-tree.hs:54:11: Not in scope: data constructor `Set'

red-black-tree.hs:54:15: Not in scope: data constructor `Char'

red-black-tree.hs:54:28: Not in scope: data constructor `Char'
Failed, modules loaded: none.

我认为我正在尝试做的事情存在一些问题,但我似乎无法从可用的文档中弄清楚。

最佳答案

为了将值转换为字符串,Haskell 使用了所谓的类型类。简化的类型类只是提供了根据参数类型而不同行为的函数。这种方法与面向对象编程语言中已知的方法重载非常相似。类型类Show提供一个名为 show 的函数将某种类型的值转换为字符串。例如,表达式 show 1产生字符串 "1" .如果有函数show将某种类型的值转换为字符串,我们说存在类型类的实例 Show对于这种类型。换句话说,有一个类型类Show的实例。对于整数。

这个show当您在 ghci 中评估表达式时,也会使用函数。因此,它告诉您没有实例 (Show (Set Char)) ,换句话说,它不知道如何转换 Set Char 类型的值。成一个字符串。对于自定义类型,如您的类型 Set , RBT , 和 Color您必须提供类型类 Show 的实例为了在控制台上显示这些类型的值。定义类型类的实例Show对于类型 Color您可以使用以下定义。

instance Show Color where:
show R = "Red"
show B = "Black"

也就是说,如果你写 R进入 ghci,它将打印 Red .对于简单的 Haskell 数据类型,有 Show 的规范定义。类型类。例如, Show 的规范定义对于 Color如下。
instance Show Color where:
show R = "R"
show B = "B"

为了让用户不必像这样定义实例,Haskell 提供了一种所谓的“派生机制”。也就是说,如果你写
  deriving (Show)

在定义数据类型后,编译器将生成 Show 的规范实例。您的数据类型的类型类。

如果一种数据类型使用了另一种数据类型,则导出 Show前者的实例需要 Show后者的实例。例如,考虑以下数据类型。
data RBT e = E | T Color (RBT e) e (RBT e)

数据类型 RBT使用类型 Color在其定义中。规范 Show T 的实例构造函数将以“T”开头,然后显示类型 Color 的值.因此,推导出 Show RBT 的实例需要 Show 的实例对于 Color .

关于Haskell 导出展示实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18518828/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com