This site is updated infrequently. For up-to-date information, please visit the new OCaml website at ocaml.org.

help wanted with recursive types across module boundaries
• Norman Ramsey
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
 Date: 2000-08-11 (08:09) From: Norman Ramsey Subject: help wanted with recursive types across module boundaries
```I'm having trouble breaking a recursion that crosses module
boundaries.

I have a representation of probability distributions.
Ordinarily I would use a monad, declaring type 'a dist, with unit,
bind, and other operations, but I want (among other implementations)
an efficient implementations in terms of sets, so polymorphism is not
indicated.  I therefore have

module type Dist = sig
type v (* values *)
type dist (* probability distribution over values *)
val unit : v -> dist
val bind : dist -> (v -> dist) -> dist
...
end

Of the values in which I am interested, one kind of value is a
function from values to probability distributions over values.

module Value = struct
type 'vdist v
= Int of int
| Record of (name * 'vdist v) list
| Function of ('vdist v -> 'vdist)
end

Now I want to close the loop.  If I were writing Standard ML, I would
attempt the following:

module type ValueDist = sig
include Dist
type v' = V of dist Value.v
sharing type v = v'
end

(I enclose the equivalent Standard ML code below, which compiles.)

I want to write a functor that takes ValueDist as an argument.
How can I do this in OCaml?

Norman

(* type-correct standard ml code follows *)

signature Dist = sig
type v (* values *)
type dist (* probability distribution over values *)
val unit : v -> dist
val bind : dist -> (v -> dist) -> dist
end

structure Value = struct
datatype 'vdist v
= Int of int
| Record of (string * 'vdist v) list
| Function of ('vdist v -> 'vdist)
end

signature ValueDist = sig
include Dist
datatype v' = V of dist Value.v
sharing type v = v'
end

```