1(* Example: simple recursive structures and signatures *)
2
3(* a simple recursive structure *)
4
5structure S =
6   rec (X:sig structure Odd : sig val test : int -> bool
7                              end
8          end)  (* forward declaration *)
9   struct structure Even = struct fun test 0 = true
10				    | test n = X.Odd.test (n-1) (* forward reference *)
11			   end
12	  structure Odd = struct fun test 0 = false
13				   | test n = Even.test (n-1)
14			  end
15   end;
16(* test *)
17val t1 = S.Even.test 100;
18val t2 = S.Odd.test 100;
19
20(* a simple recursive signature, specifying mutually recursive datatypes
21   that span module boundaries *)
22
23signature REC =
24    rec(X: sig structure Odd: sig type t end end)  (* the forward declaration *)
25    sig structure Even: sig datatype t = Zero
26                                       | Succ of X.Odd.t (* forward reference *)
27                        end
28	structure  Odd: sig datatype t = One
29                                       | Succ of Even.t end
30    end;
31
32(* the recursive signature can now be used to implement a
33   recursive structure T with mutually recursive datatypes
34   that span module boundaries
35*)
36
37structure T =
38    rec(X:REC) (* the forward declaration *)
39    struct
40	structure Even = struct datatype t = datatype X.Even.t
41				fun succ Zero = X.Odd.One
42				  | succ E = X.Odd.Succ E
43			 end
44	structure Odd  = struct datatype t = datatype X.Odd.t
45				fun succ O = Even.Succ O
46			 end
47    end
48
49(* test *)
50val t3 = T.Even.Succ (T.Odd.Succ T.Even.Zero);
51
52
53
54
55
56
57