Loading ...
Sorry, an error occurred while loading the content.

9087Re: "ocaml_beginners"::[] Circular dependencies between modules

Expand Messages
  • Fabrice Marchant
    Dec 1, 2007
      > > Please Jon, could you develop a bit more on this ?
      > > I'm unable to see what you mean here.
      >
      > Sure.
      Thanks a lot !

      > Consider a pair of mutually recursive functions:
      >
      > # let rec even ?(xs=[]) ?(ys=[]) = function
      > | [] -> xs, ys
      > | h::t -> odd ~xs:(h::xs) ~ys t
      > and odd ?(xs=[]) ?(ys=[]) = function
      > | [] -> xs, ys
      > | h::t -> even ~xs ~ys:(h::ys) t;;
      > # even [0;1;2;3;4;5];;
      > - : int list * int list = ([4; 2; 0], [5; 3; 1])
      >
      > We want to keep them in separate modules and, consequently, we must break
      > their cyclic dependency. To do this, you just parameterize "even" over "odd"
      > and vice-versa:
      >
      > # let rec even odd xs ys = function
      > | [] -> xs, ys
      > | h::t -> odd (h::xs) ys t;;
      > val even :
      > ('a list -> 'b -> 'a list -> 'a list * 'b) ->
      > 'a list -> 'b -> 'a list -> 'a list * 'b = <fun>
      > # let odd even xs ys = function
      > | [] -> xs, ys
      > | h::t -> even xs (h::ys) t;;
      > val odd :
      > ('a -> 'b list -> 'b list -> 'a * 'b list) ->
      > 'a -> 'b list -> 'b list -> 'a * 'b list = <fun>
      >
      > Some people refer to this as "untying the recursive knot". Now, how do we use
      > these? Well, we just tie the knot again in a module that depends upon both:
      That is absolutely cool !

      >
      > # let rec even' xs = even odd' xs and odd' xs = odd even' xs in
      > even' [] [] [0;1;2;3;4;5];;
      > - : int list * int list = ([4; 2; 0], [5; 3; 1])
      >
      > You can do exactly the same thing with types:
      >
      > type a = A of b
      > and b = B of a;;
      >
      > becomes:
      >
      > type 'b a = A of 'b;;
      > type 'a b = B of 'a;;
      >
      > and so on.

      Thanks for this interesting method I just discover here.

      Regards,

      Fabrice
    • Show all 15 messages in this topic