<p>I'm writing an interpreter for an experimental language. Three of the main constructs of the language are definitions, statements, and expressions. Definitions can contain statements and expressions, statements can contain definitions and expressions, and one kind of expression can contain statements. I represent all of these using union types so I can easily use pattern matching on them. Ideally, I would like to put the code for these in different files, but OMake complains about circular dependency issues. As far as I know, circular type definitions across modules are not allowed.</p>

<p>The only way I know of to solve this is to define all three types at once:</p>

<pre><code>type defn = ...
and stmt = ...
and expr = ...
</code></pre>

<p>It seems like this requires all the code for types to be in the same file. Is there any way around this? How do you deal with circular definitions in your code?</p>

[{'answer_id': 36527, 'author': 'Chris Conway', 'author_id': 1412, 'author_profile': 'https://Stackoverflow.com/users/1412', 'pm_score': 5, 'selected': True, 'text': '<p>Recursive definitions need to appear in the same file. If you want to separate definitions, statements, and expressions into separate modules, you can do so using <a href="http://caml.inria.fr/pub/docs/manual-ocaml/manual021.html#htoc100" rel="noreferrer">recursive modules</a>, but they will still need to appear in the same file. DAG-ifying inter-file dependencies is one of the annoyances of OCaml.</p>\n'}, {'answer_id': 216062, 'author': 'J D', 'author_id': 13924, 'author_profile': 'https://Stackoverflow.com/users/13924', 'pm_score': 4, 'selected': False, 'text': '<p>This is easily solved by parameterizing your types over the types they refer to:</p>\n\n<pre><code>type (\'stmt, \'expr) defn = ...\ntype (\'defn, \'expr) stmt = ...\ntype (\'defn, \'stmt) expr = ...\n</code></pre>\n\n<p>This technique is called "untying the recursive knot" (in reference to Gordian\'s knot) and was described in an <a href="http://www.ffconsultancy.com/products/ocaml_journal/?so" rel="noreferrer">OCaml Journal</a> article.</p>\n\n<p>Cheers,\nJon Harrop.</p>\n'}, {'answer_id': 9318844, 'author': 'Fabrice Le Fessant', 'author_id': 1206917, 'author_profile': 'https://Stackoverflow.com/users/1206917', 'pm_score': 3, 'selected': False, 'text': '<p>Another solution often used is to abstract the types in the interfaces. Since the types are abstract in the interfaces, these interfaces are not recursively dependent. In the implementations, you can specify the types, and since the implementations depend only on the interfaces, they are not recursive either.</p>\n\n<p>The only problem is that, with this solution, you cannot anymore pattern-matching on these types outside of their implementation.</p>\n\n<p>Personally, but it is probably a matter of taste, I like to have all the types of my program defined in one module (I think it helps in the readability of the program). So, this restriction of OCaml is not really a problem for me.</p>\n'}]
['https://Stackoverflow.com/questions/36260', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1891/']