title: OCaml : type constraints in signatures
body: <p>In my code, I have a <strong>database access context</strong> that provides elementary read/write operations, called <code>CouchDB.ctx</code>. Various modules in my application then extend that class with additional functionality, such as <code>Async.ctx</code>. </p>
  
  <p>I am implementing a <code>Cache</code> module which is wrapped around a <code>Source</code> module. The <code>Cache</code> module functions take a context argument and manipulate the database. Some calls are then forwarded to the <code>Source</code> module along with the context. </p>
  
  <p>I need to define a functor along the lines of this: </p>
  
  <pre><code>module CouchDB = struct
    class ctx = object
      method get : string -&gt; string option monad 
      method put : string -&gt; string -&gt; unit monad
    end
  end
  
  module AsyncDB = struct
    class ctx = object
      inherit CouchDB.ctx
      method delay : 'a. float -&gt; (ctx -&gt; 'a monad) -&gt; 'a monad 
    end
  end
  
  module type SOURCE = sig
    class ctx = #CouchDB.ctx (* &lt;-- incorrect *)
    type source
    val get : source -&gt; ctx -&gt; string monad
  end
  
  module Cache = functor(S:SOURCE) -&gt; struct
    class ctx = S.ctx
    type source = S.source
    let get source ctx = 
      bind (ctx # get source) (function 
       | Some cache -&gt; return cache
       | None -&gt; 
         bind (S.get source ctx) 
           (fun data -&gt; bind (ctx # put source data) 
                          (fun () -&gt; return data)) 
  end
  
  module SomeSource = struct
    class ctx = AsyncDB.ctx
    type source = string
    let get s ctx = 
      ctx # async 300 (some_long_computation s)
  end
  
  module SomeCache = Cache(SomeSource)
  </code></pre>
  
  <p>The problem is that I cannot express the fact that the context used by the <code>Source</code> module should be a subtype of <code>CouchDB.ctx</code>. The above code returns the error: </p>
  
  <pre><code>A type variable is unbound in this type declaration.
  In type #CouchDB.ctx as 'a the variable 'a is unbound
  </code></pre>
  
  <p>How do I express this type constraint ?</p>