我想下面的问题可以用算术解决 但还没有找到解决办法
Problem
我有一个从字符串到值(使用 Tries 作为执行)的有限地图, 我从二进制/文本文件( json, xml,...) 中分析 。
type Value = ...
type Attributes = Data.Trie Value
data Object = Object Attributes
Each map has the same type of values but not the same set of keys. I group maps with the same set of keys together to be able to prevent having to type-switch all the time I have a specialised function that requires certain keys:
data T1
data T2
...
data Object a where
T1 :: Attributes -> Object T1
T2 :: Attributes -> Object T2
...
这让我可以写一些东西 比如:
f1 :: Object T1 -> ...
代替
f1 :: Object ->
f1 o | check_if_T1 o = ...
这有两种缺点:
- Homogeneous lists of Object now become heterogeneous, i.e. I cannot have a list [Object] anymore.
我需要写许多锅炉板才能获得/设置属性:
get :: Object a -> Attributes get (T1 a) = a get (T2 a) = a ...
Question
- Is there a better way to specialise functions depending on the constructor of an ADT?
How could I regain the ability to have a list [Object]? Is there a specialized version of Dynamic that only allows certain types? I thought about wrapping the Object again, but this would add a lot of boilerplate. For instance,
数据对象 = TT1 T1 TT2 T2...
我需要的是:
get :: a -> TObject -> Object a
这样我就可以得出:
collect :: a -> [TObject] -> [Object a]
我查看了HList,但我不认为它适合我的问题。特别是,因为[Object]中的种类顺序在编译时并不为人所知。
这听起来像是可以用功能依赖/类型算术来解决的 但我只是还没有找到一个好办法