Hello,
I found myself writing the following piece of code, and I’m wondering how can I reuse more code and not repeat re-declaring type expr = ...
in both modules (since outside of this minimal example this would be a large grammar and repeating it twice or even more times would be bad).
module SimpleAST = struct
type 'a ast_node = 'a
type expr = Bool of bool ast_node | Int of int ast_node
(* more type definitions shared between SimpleAST and TypedAST *)
(* ... *)
end
module TypedAST = struct
type 'a ast_node = {node: 'a; type_info: TType} (* everything is the same except the definion of ast_node *)
type expr = Bool of bool ast_node | Int of int ast_node
(* more type definitions shared between SimpleAST and TypedAST *)
(* ... *)
end
Ultimately, what I’m trying to achieve is having tree variatinons which represent the same structure (e.g. an expr
will always have as children a Bool
and an Int
across all variations, but the actual nodes of each tree to be of different type)
Also, the reason I am not creating a signature for both modules is that I want the definitions of expr
etc to be public, so if I had a type module I would have to triplicate the definitions. As a result this is preventing me from using functors (but possibly there’s something I’m missing here).
Is there any way to achieve that or factor out the redundant code above? Thanks!