【发布时间】:2019-01-10 11:21:04
【问题描述】:
我正在编写一个 OCaml 解释器,我会检查成对列表中是否有重复项。
type exp = ... | Dict of (ide * exp) list | AddPair of exp * (ide * exp) list;;
type evT = ... | DictVal of (ide * evT) list
Dict(pairs) ->
if invariant pairs then DictVal(evalDictList pairs r)
else failwith("The Dictionary has multiple copy of the same key")|
AddPair(dict, newpair) ->
(match (eval dict r) with
DictVal (oldpairs) -> let evalnewpair = evalDictList newpair r in
if invariant (evalnewpair@oldpairs) then DictVal(oldpairs@evalnewpair)
else failwith ("A new key has the same value as another already inserted")|
_ -> failwith ("not a dictionary"))|
and evalDictList (pairs : (ide * exp) list) (r : evT env) : (ide * evT) list = match pairs with
[ ] -> [ ] |
(key,value) :: other -> (key, eval value r) :: evalDictList other r;;
和不变量:
and invariant (pairs : (ide * 'a) list) : bool = match pairs with
[ ] -> true |
(key,value) :: other -> if lookfor key other then invariant other else false
错误: 此表达式具有类型 (ide * evT) 列表 但是需要一个类型为 (ide * exp) 列表的表达式 evT 类型与 exp 类型不兼容
在“Dict”中,在“AddPair”中使用 (ide * exp) 的列表 invariant 将得到 evalnewpair@oldpairs ,其中 evalnewpair 具有类型 (ide * evT) 和 oldpairs (ide * evT)。
【问题讨论】:
标签: expression ocaml typeerror interpreter