来自 Haskell 新手的简短版本:假设我有一个类 Container,其中 Container 有* -> *
种类。我想把它放到另一个容器中,并且仍然使第二个容器成为原始类的实例,例如:
data Container2 a container = Container2 (container a)
instance Container (Conrainer2 a) where ...
但这似乎是不可能的,因为 GHC 总是会产生如下错误:
Kind mis-match
The first argument of `Container' should have kind `* -> *',
but `Container2 a' has kind `(* -> *) -> *'
有可能解决这个问题吗?
长版:我正在使用以下代码在 Java 中对迭代器接口进行建模:
module Main where
data NextResult a iter = Stop | More a (iter a)
class Iter iter where
next :: iter a -> NextResult a iter
-- Convert iterator to a list
toList :: (Iter c) => c a -> [a]
toList iter = case next iter of
Stop -> []
More value iter2 -> value : toList iter2
-- List itself is iterator
instance Iter [] where
next [] = Stop
next (x:xs) = More x xs
main = let iter = [1,2,3,4,5] in print $ toList iter
使用 GHC 7.4.1 编译并打印预期的1 2 3 4 5
. 现在我想定义一个转换迭代器,它从一个函数和一个迭代器构造一个新的迭代器。为此,我添加了以下几行:
data TransformedIter from to iter = TransformedIter (from->to) (iter from)
instance Iter (TransformedIter from to) where
next (TransformedIter f iter) = case next iter of
Stop -> Stop
More value iter2 -> More (f value) (TransformedIter f iter2)
但这产生了错误:
Main.hs:21:16:
Kind mis-match
The first argument of `Iter' should have kind `* -> *',
but `TransformedIter from to' has kind `(* -> *) -> *'
In the instance declaration for `Iter (TransformedIter from to)'
我试图解决这个问题,但结果总是一种或另一种类型的错误。那么如何在 Haskell 中对这种转换进行建模呢?
更新
我误解了实例声明的工作原理。根据下面的建议,我翻转了 TransformedIter 类型的顺序并最终得到:
module Main where
data NextResult a iter = Stop | More a (iter a)
class Iter iter where
next :: iter a -> NextResult a iter
toList :: (Iter c) => c a -> [a]
toList iter = case next iter of
Stop -> []
More value iter2 -> value : toList iter2
instance Iter [] where
next [] = Stop
next (x:xs) = More x xs
main = let iter = [1,2,3,4,5] in print $ toList iter
data TransformedIter iter from to = TransformedIter (from->to) (iter from)
instance Iter (TransformedIter iter from) where
next (TransformedIter f iter) = case next iter of
Stop -> Stop
More value iter2 -> More (f value) (TransformedIter f iter2)
然而,这产生了另一个错误:
Main.hs:22:40:
No instance for (Iter iter)
arising from a use of `next'
In the expression: next iter
In the expression:
case next iter of {
Stop -> Stop
More value iter2 -> More (f value) (TransformedIter f iter2) }
In an equation for `next':
next (TransformedIter f iter)
= case next iter of {
Stop -> Stop
More value iter2 -> More (f value) (TransformedIter f iter2) }
我将实例声明更改为:
instance Iter (Iter iter => TransformedIter iter from) where
这产生了另一个错误:
Main.hs:21:10:
Illegal instance declaration for `Iter
(Iter iter => TransformedIter iter from)'
(All instance types must be of the form (T a1 ... an)
where a1 ... an are *distinct type variables*,
and each type variable appears at most once in the instance head.
Use -XFlexibleInstances if you want to disable this.)
In the instance declaration for `Iter (Iter iter =>
TransformedIter iter from)'
在我添加了 -XFlexibleInstances 之后,我得到了:
Main.hs:21:10:
Illegal polymorphic or qualified type:
Iter iter => TransformedIter iter from
In the instance declaration for `Iter (Iter iter =>
TransformedIter iter from)'
所以我仍然看不到如何将 TransformedIter 声明为 Iter 的一个实例。有什么线索吗?
更新 2
使用 GADTs GHC 扩展,我设法定义了 TransformedIter:
module Main where
data NextResult a iter = Stop | More a (iter a)
class Iter iter where
next :: iter a -> NextResult a iter
toList :: (Iter c) => c a -> [a]
toList iter = case next iter of
Stop -> []
More value iter2 -> value : toList iter2
instance Iter [] where
next [] = Stop
next (x:xs) = More x xs
data TransformedIter iter from to where
TransformedIter :: Iter iter =>
(from->to) -> (iter from) -> TransformedIter iter from to
instance Iter (TransformedIter iter from) where
next (TransformedIter f iter) = case next iter of
Stop -> Stop
More value iter2 -> More (f value) (TransformedIter f iter2)
twice = (*) 2
main = let iter = TransformedIter twice [1,2,3,4,5] in print $ toList iter
这编译并打印了预期的 2 4 6 8 10。但是这个扩展真的有必要吗?