函数依赖项在Haskell Collection API的“unfotable”类型类中的作用
我想了解Haskell图书馆的设计,它来自Scala文化背景 它使用(有一个)但是它们的使用方式对我来说没有意义。在下面复制的函数依赖项在Haskell Collection API的“unfotable”类型类中的作用,haskell,typeclass,functional-dependencies,Haskell,Typeclass,Functional Dependencies,我想了解Haskell图书馆的设计,它来自Scala文化背景 它使用(有一个)但是它们的使用方式对我来说没有意义。在下面复制的可展开的类中,元素类型i显示为由集合类型c确定 具有不可观测元素的集合类。它是可折叠的类的对偶 请解释依赖项c->i在这里扮演的角色和设计意图,最好是使用示例?该功能依赖项表示的约束是,给定集合类型c,其项的类型i已经确定。例如,如果c~[a],即集合是as的列表,那么我们应该能够确定i~a 如果没有这种功能依赖性,我们可能会有两个实例,例如unfotable[a]a(明
可展开的类中,元素类型i
显示为由集合类型c
确定
具有不可观测元素的集合类。它是可折叠的类的对偶
请解释依赖项c->i
在这里扮演的角色和设计意图,最好是使用示例?该功能依赖项表示的约束是,给定集合类型c
,其项的类型i
已经确定。例如,如果c~[a]
,即集合是a
s的列表,那么我们应该能够确定i~a
如果没有这种功能依赖性,我们可能会有两个实例,例如unfotable[a]a
(明显的/预期的实例)和unfotable[a][a]
(类似于insert=concat
,singleton=id
)。如果typechecker随后看到类似于empty::[a]
,它将无法选择要使用的实例:
{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances #-}
class Unfoldable c i where
empty :: c
instance Unfoldable [a] a where
empty = []
instance Unfoldable [a] [a] where
empty = []
xs :: [a]
xs = empty
这导致:
No instance for (Unfoldable [a] i0) arising from a use of `empty'
The type variable `i0' is ambiguous
Relevant bindings include
xs :: [a]
Note: there are several potential instances:
instance Unfoldable [a] a
instance Unfoldable [a] [a]
In the expression: empty
In an equation for `xs': xs = empty
No instance for (Unfoldable [a] i0) arising from a use of `empty'
The type variable `i0' is ambiguous
Relevant bindings include
xs :: [a]
Note: there are several potential instances:
instance Unfoldable [a] a
instance Unfoldable [a] [a]
In the expression: empty
In an equation for `xs': xs = empty