带函子和多态变体的OCaml类型检查问题 [英] OCaml Typechecking Problem With Functors and Polymorphic Variants
问题描述
我在OCaml中使用函子时遇到问题.我有一个模块类型TASK
,该模块类型用于执行各种任务:
I have a problem using functors in OCaml. I have a module type TASK
that is used to have different kinds of tasks:
module type TASK = sig
type task_information
type task_information_as_lists
type abstract_u_set_type
module AbstractUSet : Set.S with type t = abstract_u_set_type
val mk_task_information : task_information_as_lists -> task_information
end
TASK
类型的模块将包含使用节点的算法.这些节点将具有不同的类型.因此,我建立了TASK_NODE
:
A module of type TASK
will contain algorithms that use nodes. These nodes will have different types. I therefore built TASK_NODE
:
module type TASK_NODE = sig
type t
val compare : t -> t -> int
val to_string : t -> string
end
TASK
中的
task_information_as_lists
将是一个包含不同类型的列表,但是每个任务在该列表中将具有不同的类型.由于无法提供带有函子的module Game : (TASK with with type u := int list)
之类的东西,因此我创建了一个模块类型INFO_CARRIER
.对于每个TASK
类型的模块,我都希望有一个INFO_CARRIER
类型的模块,该模块可以保存task_information_as_lists
类型的实际信息.
task_information_as_lists
from TASK
will be a list containing different types, but every task will have different types in that list. As I cannot have something like module Game : (TASK with with type u := int list)
with functors, I've created a module type INFO_CARRIER
. For every different module of type TASK
I want to have a different module of type INFO_CARRIER
that will hold the information what type task_information_as_lists
actually is.
这是模块类型INFO_CARRIER
:
module type INFO_CARRIER = sig
module INODE : TASK_NODE
type u = [`V of INODE.t list | `E of INODE.t] list
end
为了进行测试,我想拥有一个string
TASK_NODES
,以及一个可以传达u类型(如type u = [`V of string list | `E of string] list
)的INFO_CARRIER
.
For testing, I want to have string
TASK_NODES
, and an INFO_CARRIER
that can convey a type u like type u = [`V of string list | `E of string] list
.
所以我建立了StringTaskNode
和StringInfoCarrier
,以及一个StringTask
以使用这些节点:
So I built StringTaskNode
and StringInfoCarrier
, and also a StringTask
to use the nodes:
module StringTaskNode : TASK_NODE = struct
type t = string
let compare = compare
let to_string s = "Node " ^ s
end
module StringInfoCarrier (TN : TASK_NODE) : (INFO_CARRIER with module INODE = TN) = struct
module INODE = TN
type u = [`V of TN.t list | `E of TN.t] list
end
module StringTask (TN : TASK_NODE) (IC : INFO_CARRIER with module INODE = TN) :
(TASK with type task_information_as_lists := IC.u with type abstract_u_set_type := Set.Make(IC.INODE).t) = struct
module N = IC.INODE
module AbstractUSet = Set.Make(IC.INODE)
type task_information = {v_nodes : AbstractUSet.t ; e_nodes : N.t}
let mk_task_information info_list =
match info_list with
| (`V v)::(`E e)::[] -> {v_nodes = AbstractUSet.of_list v ; e_nodes = e;}
| _ -> raise .... (* raising an error here *)
end
然后我有另一个模块ProdFP
,它将为任务做一些计算:
I then have another module ProdFP
that will do some computation for a task:
module ProdFP
(TN : TASK_NODE)
(IC : INFO_CARRIER with module INODE=TN)
(TA : TASK with type abstract_u_set_type:=u with type task_information_as_lists:=IC.u) = struct
...
end
到目前为止,没有错误发生.但是,当我将所有内容放在Test
中时,我得到:
So far, no errors happen. But when I bring everything together in Test
, I get:
Error: This expression has type
[`E of string | `V of string list]
but an expression was expected of type
SIC.u = [`E of SIC.INODE.t | `V of SIC.INODE.t list]
Type string is not compatible with type
SIC.INODE.t = STN.t
这是测试模块:
module Test = struct
module STN = StringTaskNode
module SIC = StringInfoCarrier(STN)
module ST = StringTask(STN)(SIC)
module PF = ProdFP(STN)(SIC)(ST)
let v_list =["a";"b"]
let e = "c"
let info_as_list = [`V v_list ; `E e]
let info = ST.mk_task_information info_as_list (* THIS IS WHERE THE ERROR HAPPENS *)
end
我知道,这是一篇很长的文章,但是我尝试包括尽可能多的信息. 如果您能帮助我,那就太好了:)
I know, this is a very long post, but I've tried to include as much information as necessary. It would be so great if you could help me :)
推荐答案
问题似乎仅仅是StringTaskNode
模块定义中的签名约束:
It looks like the problem is just the signature constraint in the definition of the StringTaskNode
module:
module StringTaskNode : TASK_NODE = struct
...
end
此约束使类型StringTaskNode.t
抽象,从而隐藏了等式STN.t = string
.删除约束,
This constraint makes the type StringTaskNode.t
abstract and thus hides the equality STN.t = string
. Removing the constraint,
module StringTaskNode = struct
...
end
应解决此特定问题.
这篇关于带函子和多态变体的OCaml类型检查问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!