Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Haskell 容器元素类型_Haskell_Types_Containers - Fatal编程技术网

Haskell 容器元素类型

Haskell 容器元素类型,haskell,types,containers,Haskell,Types,Containers,在某种程度上,这是我先前问题的倒退,但是。。。有人能提醒我为什么这样不行吗 类别容器c e在哪里 空::c 插入:e->c->c 实例容器[x]x其中 空=[] 插入=(:) 实例容器ByteString Word8,其中 空的,空的 插入=BIN.cons 实例Ord x=>容器(集合x)x其中 empty=SET.empty insert=SET.insert 显然,如果这是那么容易,没有人会费心发明函数依赖关系或相关类型。那么上面的问题是什么呢?没有什么可以阻止您添加实例容器[Int]In

在某种程度上,这是我先前问题的倒退,但是。。。有人能提醒我为什么这样不行吗

类别容器c e在哪里 空::c 插入:e->c->c 实例容器[x]x其中 空=[] 插入=(:) 实例容器ByteString Word8,其中 空的,空的 插入=BIN.cons 实例Ord x=>容器(集合x)x其中 empty=SET.empty insert=SET.insert
显然,如果这是那么容易,没有人会费心发明函数依赖关系或相关类型。那么上面的问题是什么呢?

没有什么可以阻止您添加
实例容器[Int]Int
实例容器[Int]Char
,当您请求
空::[Int]
时,编译器无法知道它应该来自哪个实例

“啊,但我只有
实例容器[Int]Int
,”你说。“而且一个
实例容器[Int]Char
无论如何都会是一个bug。”

但是编译器无法知道将来不会添加
实例容器[Int]Char
,如果添加了,则不允许它破坏现有代码

所以我们需要某种方式告诉编译器

  • 容器的第一个参数
    唯一地确定了
    容器的第二个参数
  • 如果它看到的不同实例仅在第二种类型中有所不同,则表明存在错误

输入函数依赖项。

尝试使用它。你很快就会收到关于模糊超载的投诉。 class Container c e where empty :: c insert :: e -> c -> c instance Container [x] x where empty = [] insert = (:) instance Container ByteString Word8 where empty = BIN.empty insert = BIN.cons instance Ord x => Container (Set x) x where empty = SET.empty insert = SET.insert