给定是一种用于构造二叉树的类型:
type tree = Leaf of int | Node of int * tree * tree
现在假设我们想通过类型表示二叉树包含一个元素为零的节点,即,我想表达以下形式:
let rec p x = match x with
| Leaf(y) -> y = 0
| Node(y,l,r) -> y = 0 || (p l) || (p r)
type zerotree = ZeroTree of t:tree where p(t)
这意味着,只要我有一个ZeroTree类型的树,那么我可以确定树包含一个元素为零的节点,即谓词p
成立。
在OCaml中有类似的东西吗?
答案 0 :(得分:4)
答案1:不。你想要什么超出了OCaml类型系统的范围。
答案2:您可以将zerotree
定义为与tree
完全不同的类型。
type zerotree =
| ZLeaf
| ZNodeL of int * zerotree * tree
| ZNodeR of ... (* left for the reader *)
| ZNodeI of ...
zerotree
是ZLeaf
,是0
的叶子; ZNodeL
,左子树为zerotree
的节点; ZNodeR
,其右子树为zerotree
的节点;或ZNodeI
,int为0
的节点。
答案3:答案2仅适用于一些简单的数据结构和简单的不变量。在现实世界中,我们经常使用私有类型通过禁止任意构造值来强制执行不变量:
module Z : sig
type zerotree = private Leaf of int | Node of int * zerotree * zerotree
val leaf : int -> zerotree
val node : int -> zerotree -> zerotree -> zerotree
end = struct
type zerotree = Leaf of int | Node of int * zerotree * zerotree
let rec p = function
| Leaf y -> y = 0
| Node(y,l,r) -> y = 0 || p l || p r
let check zt = if p zt then zt else assert false
let leaf i = check (Leaf i)
let node i l r = check (Node (i,l,r))
end
open Z
(* let z = Leaf 1 Compile error: Cannot create values of the private type *)
(* let z = leaf 1 Runtime error *)
let z = leaf 0
let () = match z with (* you can still pattern match *)
| Leaf 0 -> ()
| _ -> assert false
类型zerotree
与tree
相同,但其构造函数在模块Z
之外是私有的:您不能使用构造函数来创建值,而只能在外部解构(即模式匹配)它们模块。
价值zerotree
的构建必须通过Z.leaf
和Z.node
函数来完成,这些函数会检查您必须提供的属性。
答案 1 :(得分:0)
混合解决方案是可能的,但它很笨重。这里我们保留一个布尔值,以便我们可以廉价地从零树转换为常规树。
type ('left, 'right) either = Left of 'left | Right of 'right
module type Zero_tree = sig
type t = private Leaf of int | Node of bool * int * t * t
(* The type of any tree. The boolean indicates whether
this is a zero-tree.
The type is private so we can guarantee that the
boolean is set correctly.
*)
type z
(* The type of zero-trees, a subtype of t which is guaranteed to contain
at least one zero-node.
In the module implementation, it uses the same representation as t.
*)
(* Constructors *)
val leaf : int -> t
val node : int -> t -> t -> t
val leaf_zero : t
val node_zero : t -> t -> z
val node_left : int -> z -> t -> z
val node_right : int -> t -> z -> z
val node2 : int -> z -> z -> z
(* Destructors *)
val view : z -> t
val classify : t -> (z, t) either
val as_zero : t -> z option
end
module Zero_tree : Zero_tree = struct
type t = Leaf of int | Node of bool * int * t * t
type z = t
let leaf n = Leaf n
let leaf_zero = Leaf 0
let is_zero = function
| Leaf 0
| Node (true, _, _, _) -> true
| _ -> false
let node n a b =
let iz = n = 0 || is_zero a || is_zero b in
Node (iz, n, a, b)
let node_zero a b = node 0 a b
let node_left = node
let node_right = node
let node2 = node
let view x = x
let classify x =
if is_zero x then Left x
else Right x
let as_zero x =
if is_zero x then Some x
else None
end