Types 为模块定义递归签名
我知道可以定义递归模块,有人知道如何定义递归签名吗?例如,我想了解:Types 为模块定义递归签名,types,module,ocaml,signature,Types,Module,Ocaml,Signature,我知道可以定义递归模块,有人知道如何定义递归签名吗?例如,我想了解: module type AAA = sig module Bbb : BBB type 'a t val f : 'a Bbb.t -> 'a t end module type BBB = sig module Aaa : AAA type 'a t val g : 'a Aaa.t -> 'a t end 有人能帮忙吗?你可以这样写: module rec Aaa : sig
module type AAA = sig
module Bbb : BBB
type 'a t
val f : 'a Bbb.t -> 'a t
end
module type BBB = sig
module Aaa : AAA
type 'a t
val g : 'a Aaa.t -> 'a t
end
有人能帮忙吗?你可以这样写:
module rec Aaa : sig
type 'a t
val f : 'a Bbb.t -> 'a t
end = Aaa
and Bbb : sig
type 'a t
val g : 'a Aaa.t -> 'a t
end = Bbb
据我所知,你不能。最接近的解决方案是将“递归”位限制为单独表示每个签名的实际需要:
module type AA =
sig
module B : sig type t end
type t
val f : unit -> B.t
end
module type BB =
sig
module A : sig type t end
type t
val g : unit -> A.t
end
然后在定义模块时进行优化:
module rec A : AA with module B = B =
struct
module B = B
type t = int
let f () = B.g ()
end
and B : BB with module A = A =
struct
module A = A
type t = int
let g () = A.f ()
end
module rec AA :
sig
module type T = sig module B : BB.T end
end =
struct
module type T = sig module B : BB.T end
end
and BB :
sig
module type T = sig module A : AA.T end
end =
struct
module type T = sig module A : AA.T end
end
FWIW,有人可能会认为应该可以通过使用递归模块来表达递归签名(有很多重复):
module rec A : AA with module B = B =
struct
module B = B
type t = int
let f () = B.g ()
end
and B : BB with module A = A =
struct
module A = A
type t = int
let g () = A.f ()
end
module rec AA :
sig
module type T = sig module B : BB.T end
end =
struct
module type T = sig module B : BB.T end
end
and BB :
sig
module type T = sig module A : AA.T end
end =
struct
module type T = sig module A : AA.T end
end
但是,这不起作用:
Error: Unbound module type BB.T
谢谢你的评论,但我真的想给签名起个名字,例如,
AAA
aorBBB
。。。你在回答中没有提到…谢谢你的回答最接近的解决方案是限制“递归”位
==>请详细说明解决方案的限制好吗?这不允许您在签名之间表达任意递归,因为您需要能够将每个签名的自包含子集隔离为一种前向声明。此外,您将在两个位置重复这些子集中的每一个,但是命名和包括-在这方面可以有所帮助。在我的回答中,我没有费心这么做,因为相关子集(t型)足够小。