Merge branch 'cleaning_again_example_sin_cos' into 'master'
[why3.git] / stdlib / tree.mlw
blobfdbe9d755adcfe8cc5eccc6fa6046ad650b153af
2 (** {1 Polymorphic n-ary trees} *)
4 (** {2 Basic theory with polymorphic lists of children} *)
6 module Tree
8   use list.List
10   type forest 'a = list (tree 'a)
11   with tree 'a   = Node 'a (forest 'a)
13 end
15 (** {2 Tree size} *)
17 module Size
19   use Tree
20   use list.List
21   use int.Int
23   let rec function size_forest (f: forest 'a) : int
24     ensures { result >= 0 }
25   = match f with
26     | Nil      -> 0
27     | Cons t f -> size_tree t + size_forest f
28     end
29   with function size_tree (t: tree 'a) : int
30     ensures { result > 0 }
31   = match t with
32     | Node _ f -> 1 + size_forest f
33     end
35 end
37 (** {2 Forests} *)
39 module Forest
41   use int.Int
43   type forest 'a =
44     | E
45     | N 'a (forest 'a) (forest 'a)
47 end
49 (** {2 Forest size} *)
51 module SizeForest
53   use Forest
54   use int.Int
56   let rec function size_forest (f: forest 'a) : int
57     ensures { result >= 0 }
58   = match f with
59     | E -> 0
60     | N _ f1 f2 -> 1 + size_forest f1 + size_forest f2
61     end
63 end
65 (** {2 Membership in a forest} *)
67 module MemForest
69   use Forest
71   predicate mem_forest (n: 'a) (f: forest 'a) = match f with
72     | E -> false
73     | N i f1 f2 -> i = n || mem_forest n f1 || mem_forest n f2
74     end
76 end