Version française
Home     About     Download     Resources     Contact us    
Browse thread
help wanted with recursive types across module boundaries
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Thorsten Ohl <ohl@h...>
Subject: help wanted with recursive types across module boundaries
> 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

AFAIK, there is no `sharing' keyword in O'Caml, but you should be able
to use the `with' operator instead:

    module type Dist =
      sig
    	type v
    	type dist
      end
    
    module Value =
      struct
    	type name
    	type 'vdist v
    	  = Int of int
    	  | Record of (name * 'vdist v) list
    	  | Function of ('vdist v -> 'vdist)
      end
    
    (* If the implementation were absent, one could not refer
       to Dist.dist below.  Strangely enough, placing Dist in
       a separate dist.mli file (w/o a dist.ml) would work.
       I've never understood the reason for this behavior ... *)
    
    module Dist : Dist =
      struct
    	type v
    	type dist
      end
    
    type vdv = V of Dist.dist Value.v
    
    module type ValueDist =
      sig
    	include Dist
    	type v' = vdv
      end with type v = vdv

At least it compiles and if type equivalence is transitive, it should
do the right thing:
    
    ohl@heplix4:~tmp$ ocamlc -i -c nr.ml
    module type Dist = sig type v and dist end
    module Value :
      sig
	type name
	and 'a v =
	    Int of int
	  | Record of (name * 'a v) list
	  | Function of ('a v -> 'a)
      end
    module Dist : Dist
    type vdv = V of Dist.dist Value.v
    module type ValueDist = sig type v = vdv and dist and v' = vdv end

Cheers,
-Thorsten
-- 
Thorsten Ohl, Physics Department, TU Darmstadt -- ohl@hep.tu-darmstadt.de
http://heplix.ikp.physik.tu-darmstadt.de/~ohl/ [<=== PGP public key here]