通用列表类型 (ide * 'a) 的 ocaml 类型错误
ocaml type error with generic list type (ide * 'a)
我正在编写一个 OCaml 解释器,我会检查是否有重复的 inside 成对列表。
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" 中不变使用列表 (ide * exp) 而在 "AddPair" 中
不变量将得到 evalnewpair@oldpairs,其中 evalnewpair 具有类型 (ide * evT) 和 oldpairs (ide * evT).
如果 invariant
是相互递归函数定义的一部分,您需要使用通用量化来明确:
and invariant: 'a. (ide * 'a) list -> bool = fun l -> ...
在您的情况下,从相互递归块中拆分 invariant
可能更简单。
我正在编写一个 OCaml 解释器,我会检查是否有重复的 inside 成对列表。
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" 中不变使用列表 (ide * exp) 而在 "AddPair" 中 不变量将得到 evalnewpair@oldpairs,其中 evalnewpair 具有类型 (ide * evT) 和 oldpairs (ide * evT).
如果 invariant
是相互递归函数定义的一部分,您需要使用通用量化来明确:
and invariant: 'a. (ide * 'a) list -> bool = fun l -> ...
在您的情况下,从相互递归块中拆分 invariant
可能更简单。