Skip to content

Instantly share code, notes, and snippets.

@camlspotter
Created July 16, 2012 09:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save camlspotter/3121708 to your computer and use it in GitHub Desktop.
Save camlspotter/3121708 to your computer and use it in GitHub Desktop.
相互再帰した二つの型を別モジュールで定義する方法二つ
module Ex1 = struct
(* まず相互再帰した型を一モジュールで作り、その後別モジュールに分離する例 *)
module AB = struct
type a = A | AA of b
and b = B | BB of a
let a_f = function A -> B | AA b -> b
let b_f = function B -> A | BB a -> a
end
module A = struct
type a = AB.a = A | AA of AB.b
(* 単に type a = AB.a でも良いですが、 open Ex1.AB.A としてもコンストラクタ A や AA を使う為には open Ex1.AB とする必要が出てきます *)
let a_f = AB.a_f
end
module B = struct
type b = AB.b = B | BB of AB.a
let b_f = AB.b_f
end
end
module Ex2 = struct
(* 相互再帰モジュールを使う例 *)
module rec A : sig
type a = A | AA of B.b
val a_f : a -> B.b
end = struct
type a = A | AA of B.b
let a_f = function A -> B.B | AA b -> b
end
and B : sig
type b = B | BB of A.a
val b_f : b -> A.a
end = struct
type b = B | BB of A.a
let b_f = function B -> A.A | BB a -> a
end
end
(* .mli ファイルは次のようになります
module Ex1 : sig
module AB : sig
type a = A | AA of b
and b = B | BB of a
end
module A : sig type a = AB.a = A | AA of AB.b val a_f : AB.a -> AB.b end
module B : sig type b = AB.b = B | BB of AB.a val b_f : AB.b -> AB.a end
end
module Ex2 : sig
module rec A : sig type a = A | AA of B.b val a_f : a -> B.b end
and B : sig type b = B | BB of A.a val b_f : b -> A.a end
end
*)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment