本文介绍了定义模块的递归签名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道可以定义递归模块,有人知道如何定义递归签名吗?例如,我想实现:

I know that it is possible to define recursive modules, does anyone know how to define recursive signatures? For instance, I would like to realize:

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

有人可以帮忙吗?

推荐答案

据我所知,您不能这样做.最接近的解决方案是将递归"位限制为分别表示每个签名所需的实际值:

You can't, as far as I can tell. The closest solution is to limit the "recursive" bits to what is actually needed to express each signature separately:

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

然后在定义模块时进行优化:

And then refine when you define the modules:

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

FWIW,可能有人认为可以使用递归模块来表达递归签名(重复很多):

FWIW, one might think that it should be possible to express recursive signatures (with much repetition) by using recursive modules:

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

这篇关于定义模块的递归签名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 12:30