表示一组功能及其类型规则

问题描述:

我谨代表一组函数和他们类型规则,以及正在考虑的数据结构的...例如,表示一组功能及其类型规则

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)总是有固定数量的参数,并且可以有多个与之相关的输入规则;打字规则有一个名称(例如PLUS-integer),一个前提,一个结论和一个优先级。可能有两个分享规则具有相同的前提,但给出不同的结论,在这种情况下,这是优先考虑的因素。

后来,我需要这样定义功能:

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)它是一个好主意,定义rulesrule列表,与ARRA比较Y ...关于funcrules之间的关系

2),有几种选择:让rulesfunc一个记录字段;使func作为rule的记录字段;制作funcrules的哈希表;制作从funcrules的地图。我真的不知道哪种方式比较好...

另一方面我需要考虑的是该数据库的启动,有很多进入,所以我希望我选择的类型将启动容易进入并期待直线前进...

谁能帮助?

1 - 使用数组或列表,或者别的什么,取决于你打算如何访问底层数据。

如果您需要规则随机索引访问,而该数据集并没有改变太多使用array。如果数据集定期重建,并且您按顺序访问元素,请使用list。如果您不是整数的连续范围别的东西需要指数的元素,使用assoc list,一个maphashtable

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

顺便说一句,如果需要的话rulesfunc类型可以interdependant,如例如:

type func = { 
    rules : rule list; 
    (* ... *) 
} 
and rule = { 
    funcs : func list; 
    (* ... *) 
};;