1 (* Title: Tools/Code/code_namespace.ML
2 Author: Florian Haftmann, TU Muenchen
4 Mastering target language namespaces.
7 signature CODE_NAMESPACE =
11 | Stmt of Code_Thingol.stmt
12 | Module of ('a * (string * 'a node) Graph.T);
13 val hierarchical_program: (string -> string) -> { module_alias: string -> string option,
14 reserved: Name.context, empty_nsp: 'b, namify_module: string -> 'b -> string * 'b,
15 namify_stmt: Code_Thingol.stmt -> string -> 'b -> string * 'b,
16 cyclic_modules: bool, empty_data: 'a, memorize_data: string -> 'a -> 'a }
17 -> Code_Thingol.program
18 -> { deresolver: string list -> string -> string,
19 hierarchical_program: (string * 'a node) Graph.T }
22 structure Code_Namespace : CODE_NAMESPACE =
25 (* hierarchical program structure *)
29 | Stmt of Code_Thingol.stmt
30 | Module of ('a * (string * 'a node) Graph.T);
32 fun hierarchical_program labelled_name { module_alias, reserved, empty_nsp,
33 namify_module, namify_stmt, cyclic_modules, empty_data, memorize_data } program =
36 (* building module name hierarchy *)
37 fun alias_fragments name = case module_alias name
38 of SOME name' => Long_Name.explode name'
39 | NONE => map (fn name => fst (yield_singleton Name.variants name reserved))
40 (Long_Name.explode name);
41 val module_names = Graph.fold (insert (op =) o fst o Code_Printer.dest_name o fst) program [];
42 val fragments_tab = fold (fn name => Symtab.update
43 (name, alias_fragments name)) module_names Symtab.empty;
44 val dest_name = Code_Printer.dest_name #>> (the o Symtab.lookup fragments_tab);
46 (* building empty module hierarchy *)
47 val empty_module = (empty_data, Graph.empty);
48 fun map_module f (Module content) = Module (f content);
49 fun change_module [] = I
50 | change_module (name_fragment :: name_fragments) =
51 apsnd o Graph.map_node name_fragment o apsnd o map_module
52 o change_module name_fragments;
53 fun ensure_module name_fragment (data, nodes) =
54 if can (Graph.get_node nodes) name_fragment then (data, nodes)
56 nodes |> Graph.new_node (name_fragment, (name_fragment, Module empty_module)));
57 fun allocate_module [] = I
58 | allocate_module (name_fragment :: name_fragments) =
59 ensure_module name_fragment
60 #> (apsnd o Graph.map_node name_fragment o apsnd o map_module o allocate_module) name_fragments;
61 val empty_program = Symtab.fold (fn (_, fragments) => allocate_module fragments)
62 fragments_tab empty_module;
64 (* distribute statements over hierarchy *)
65 fun add_stmt name stmt =
67 val (name_fragments, base) = dest_name name;
69 change_module name_fragments (fn (data, nodes) =>
70 (memorize_data name data, Graph.new_node (name, (base, Stmt stmt)) nodes))
72 fun add_dependency name name' =
74 val (name_fragments, base) = dest_name name;
75 val (name_fragments', base') = dest_name name';
76 val (name_fragments_common, (diff, diff')) =
77 chop_prefix (op =) (name_fragments, name_fragments');
78 val (is_module, dep) = if null diff then (false, (name, name'))
79 else (true, (hd diff, hd diff'))
80 val add_edge = if is_module andalso not cyclic_modules
81 then (fn node => Graph.add_edge_acyclic dep node
82 handle Graph.CYCLES _ => error ("Dependency "
83 ^ quote name ^ " -> " ^ quote name'
84 ^ " would result in module dependency cycle"))
85 else Graph.add_edge dep
86 in (change_module name_fragments_common o apsnd) add_edge end;
87 val proto_program = empty_program
88 |> Graph.fold (fn (name, (stmt, _)) => add_stmt name stmt) program
89 |> Graph.fold (fn (name, (_, (_, names))) => fold (add_dependency name) names) program;
91 (* name declarations *)
92 fun make_declarations nsps (data, nodes) =
94 val (module_fragments, stmt_names) = List.partition
95 (fn name_fragment => case Graph.get_node nodes name_fragment
96 of (_, Module _) => true | _ => false) (Graph.keys nodes);
97 fun modify_stmt (Stmt (Code_Thingol.Datatypecons _)) = Dummy
98 | modify_stmt (Stmt (Code_Thingol.Classrel _)) = Dummy
99 | modify_stmt (Stmt (Code_Thingol.Classparam _)) = Dummy
100 | modify_stmt stmt = stmt;
101 fun declare namify modify name (nsps, nodes) =
103 val (base, node) = Graph.get_node nodes name;
104 val (base', nsps') = namify node base nsps;
105 val nodes' = Graph.map_node name (K (base', modify node)) nodes;
106 in (nsps', nodes') end;
107 val (nsps', nodes') = (nsps, nodes)
108 |> fold (declare (K namify_module) I) module_fragments
109 |> fold (declare (namify_stmt o (fn Stmt stmt => stmt)) modify_stmt) stmt_names;
111 |> fold (fn name_fragment => (Graph.map_node name_fragment
112 o apsnd o map_module) (make_declarations nsps')) module_fragments;
113 in (data, nodes'') end;
114 val (_, hierarchical_program) = make_declarations empty_nsp proto_program;
117 fun deresolver prefix_fragments name =
119 val (name_fragments, _) = dest_name name;
120 val (_, (_, remainder)) = chop_prefix (op =) (prefix_fragments, name_fragments);
121 val nodes = fold (fn name_fragment => fn nodes => case Graph.get_node nodes name_fragment
122 of (_, Module (_, nodes)) => nodes) name_fragments hierarchical_program;
123 val (base', _) = Graph.get_node nodes name;
124 in Long_Name.implode (remainder @ [base']) end
125 handle Graph.UNDEF _ => error ("Unknown statement name: " ^ labelled_name name);
127 in { deresolver = deresolver, hierarchical_program = hierarchical_program } end;