1

我想表示一组函数及其键入规则,并且正在考虑数据结构......例如,

For function "PLUS":
PLUS-integer: Integer -> Integer -> Integer, with priority high
PLUS-boolean: Boolean -> Boolean -> Integer, with priority high
...

For function "Unary Minus":
UM-0: Integer -> Integer, with priority high
UM-1: Date -> Date, with priority high
...

For function "Unary Minus":
UM-error: Date -> Error, with priority low
...

一些评论: 函数和规则的名称总是唯一的;一个函数(例如 PLUS)总是有固定数量的参数,并且可以有几个与之相关的打字规则;一个打字规则有一个名称(例如,加号整数)、一个前提、一个结论和一个优先级。可能有 2 个打字规则共享相同的前提,但给出不同的结论,在这种情况下,是优先级造成了差异。

稍后,我需要定义如下函数:

add_rule: add a rule to a function
get_rules: get all the rules from a function
get_first_rule: get the most priority rule from a function and a premise
get_conclusions: get all the conclusions that a function can give
get_errors: get all the rules whose conclusion is an error
get_function: get the function from a typing rule
set_priority: set a priority for a rule
...

为此,我不知道是否有定义这些类型的常规方式......目前,我想象一种方式如下:

type func =
    { name: string;
      ... }

type rule =
    { name: string;
      premise: Type.t list;
      conclusion: Type.t;
      priority: Priority.t
      ... }

type rules = rule list

几个问题:

1)与数组相比,定义rules为 的列表是否是个好主意...rule

2)关于 和 的关系funcrules有几种选择:rules做为 的记录字段func;制作func为记录字段rulefunc制作和的哈希表rulesfunc从到制作地图rules。我真的不知道哪种方式更好...

另一个需要考虑的方面是这个数据库的启蒙,要输入的东西很多,所以我希望我选择的类型能让启蒙容易进入,看起来很直观……

有人可以帮忙吗?

4

1 回答 1

3

1 - 使用数组或列表或其他东西,取决于您打算如何访问基础数据。

如果您需要对规则进行随机索引访问,并且数据集没有太大变化,请使用array. 如果数据集定期重建,并且您按顺序访问元素,请使用list. 如果您需要通过连续整数范围以外的其他方式对元素进行索引,请使用 a assoc list、 amap或 ahashtable

2 - 与上面相同,它最终取决于访问模式。选择在你的算法中看起来最方便的东西,如果一段时间后它碰巧是错误的选择,不要害怕重构你的代码。

顺便说一句,如果需要, rulesandfunc类型可以是相互依赖的,例如:

type func = {
    rules : rule list;
   (* ... *)
}
and rule = {
    funcs : func list;
   (* ... *)
};;
于 2012-12-30T20:07:59.927 回答