Note that there are some explanatory texts on larger screens.

plurals
  1. POOcaml Functors, Modules and Submodules
    primarykey
    data
    text
    <p>Apologies for posting such long, non-compilable code. But despite reading several questions and answers on stackoverflow on ocaml's functors, I don't get how to solve this:</p> <p>Assume I have a very abstract data structure:</p> <h3><code>ads.mli</code></h3> <pre><code>module type ENTRY = sig type t val get_index : t -&gt; int val compare : t -&gt; t -&gt; int end module type T = sig type entry type t val create : unit -&gt; t val insert : entry -&gt; t -&gt; t val delete : entry -&gt; t -&gt; t end </code></pre> <p>Based on this, I can make concrete data structures on these abstract implementation by passing a functor. For example I made:</p> <h3><code>concrete_ads.mli</code></h3> <pre><code>module Make (Entry: Ads.ENTRY) : (ads.T with type entry = Entry.t) </code></pre> <p>This work, I can now use my implementation in other source-files, for example like this:</p> <pre><code>module AT = Concrete_ads.Make( type t = int * int;; let get_index = fst;; let to_string = (fun (x,y) -&gt; Printf "%i, %i" x y);; end);; </code></pre> <p>And, then, use the implemenation like:</p> <pre><code>let at = AT.create () in let ati = AT.insert (1,2) at in let atd = AT.delete (1,2) ati in </code></pre> <p>... etc.</p> <p>Now, I want write several functions that operate on these data structures in a seperate sourcefile, and they should be accesible from outside. But, I do not know how to declare the type of these functions. Something like this:</p> <h3><code>search.mli</code></h3> <pre><code>val search : Int -&gt; Ads.T -&gt; int list </code></pre> <p>But, when compiling I get: </p> <pre><code> Failure: "invalid long identifier type" </code></pre> <p>I, then, thought I need to specifically declare the module of adt as a submodule in <code>search.mli</code>, something like:</p> <h3><code>search.mli</code></h3> <pre><code>module AD = Ads;; ... val search : Int -&gt; AD.T -&gt; int list </code></pre> <p>But, I get:</p> <pre><code>Parse error: [module_declaration] expected after [a_UIDENT] (in [sig_item]) </code></pre> <p>What am I missing here ? I feel I either fail with the syntax, or did not fully grasp the concept of Functors, Modules and Submodules ...</p> <p><strong>Edit</strong> Thank you so much for your explanation, gasche! With your example I was able to write what I inteded. I'll post it here for clarification, since there seems to be alot of confusion about functors in ocaml. </p> <p>In fact I wanted to make the function abstract with respect to <code>Ads.T</code>, but require a specific type for <code>Ads.T.t</code>. </p> <p>I now have <code>search.mli</code>:</p> <pre><code>module Make (T : Ads.T with type entry = int * int) : sig val search : T.t -&gt; int -&gt; int end;; </code></pre> <p>And, in <code>search.ml</code>:</p> <pre><code>module Make (T : Ads.T with type entry = int * int) : sig val search : T.t -&gt; int -&gt; int end = struct (* actual implementation of search *) end;; </code></pre> <p>And it worked exactly as I intended.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload