Haskell中的关联数据类型是什么?

7
在路由部分,文章中说:

我们可以看到RenderRoute类定义了一个关联的数据类型,为应用程序提供路由。

关联的数据类型是什么意思?它是指类型家族吗?
1个回答

8

引用文章中的代码:

instance RenderRoute HelloWorld where
    data Route HelloWorld = HomeR
        deriving (Show, Eq, Read)
    renderRoute HomeR = ([], [])

从以下内容可以看出,Route是一种相关数据类型,是数据家族的一种。请参考维基的示例:

We define a type class whose instances are the types that we can use as keys in our generic maps:

class GMapKey k where  
    data GMap k :: * -> *  
    empty       :: GMap k v  
    lookup      :: k -> GMap k v -> Maybe v  
    insert      :: k -> v -> GMap k v -> GMap k v

The interesting part is the associated data family declaration of the class. It gives a kind signature (here * -> *) for the associated data type GMap k - analogous to how methods receive a type signature in a class declaration.


1
我认为,类型族应该使用type而不是data来定义,就像在https://ocharles.org.uk/posts/2014-12-12-type-families.html中展示的那样。 - softshipper
这取决于你想要什么:使用现有类型(type)还是创建新类型(data)。 - Karol Samborski
看一下 https://www.yesodweb.com/book/haskell 中的类型族,有一个 type Content a 的定义,它是什么? - softshipper
1
@zero_coding TypeFamilies扩展包括类型族和数据族。它们在概念上非常相似,它们之间的区别与(非族)类型别名和(非族)数据类型之间的区别相同。 - Carl
2
“type families”和“data families”的区别是什么? - softshipper

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接