4

目前我有这样的事情:

data MyData = SomeData | MoreData | EvenMore
data DataLists = DataLists [MyData] [MyData]

这让我显然可以做这样的事情:

myDataList :: DataLists
myDataList = DataLists [SomeData, MoreData] [EvenMore, SomeData]

但我想做这样的事情:

-- Static Module File:

class MyClass b where
    evaluate :: b -> Int

data SomeData = SomeData
data MoreData = MoreData
data EvenMore = EvenMore

instance MyClass SomeData where
    evaluate _ = 2
instance MyClass MoreData where
    evaluate _ = 3
instance MyClass EvenMore where
    evaluate _ = 4

data DataList = DataList [MyClass] [MyClass] -- Obviously this is wrong

我希望我的那部分代码保持静态。因为稍后我想使用该模块添加一些类型以放入数据列表中。我需要这个单独的,因为整个模块将用于 2 个不同的项目,我不想继续编辑它。

data MyVeryOwnData = MyVeryOwnData
--I should be able to make up as many datastructures as I want

instance MyClass MyVeryOwnData where
   evaluate _ = 99

myList :: DataList
myList = DataList [MyVeryOwnData, SomeData] [SomeData, EvenMore]

我意识到我可以像这样简单地将 MyVeryOwnData 放在模块文件中:

data MyData = SomeData | MyVeryOwnData ...

instance MyClass MyData where
    evaluate SomeData = 2
    evaluate MoreData = 3
    ... etc.

但我不想这样做,因为正如我之前所说,我想添加任意数量的数据类型。

4

1 回答 1

6

您正在寻找的是所谓的Heterogenous lists,haskell 主要以两种方式支持它们,ExistentialsGADTs。我向您展示了一个可运行的代码示例,它解决了存在主义的问题。

{-# LANGUAGE ExistentialQuantification #-}

class Eval b where
    evaluate :: b -> Int

data SomeData = SomeData
data MoreData = MoreData
data EvenMore = EvenMore

instance Eval SomeData where
    evaluate _ = 2
instance Eval MoreData where
    evaluate _ = 3
instance Eval EvenMore where
    evaluate _ = 4

data Evalable = forall a . Eval a => Evalable a

instance Eval Evalable where
    evaluate (Evalable a) = evaluate a

pack :: Eval a => a -> Evalable
pack = Evalable

data DataLists = DataLists [Evalable] [Evalable]

getFirst :: DataLists -> [Evalable]
getFirst (DataLists xs ys) = xs

main = do
  let dl = DataLists [pack SomeData, pack EvenMore] [pack MoreData]
  print . map evaluate . getFirst $ dl

应该清楚的是,您可以添加instance Eval MyVeryOwnData外部模块,然后您可以将pack这些值变成它们Evalable,然后您可以将它们放入您的Datalists.

于 2012-08-08T07:35:50.613 回答