src/Tools/Code/code_namespace.ML
author haftmann
Wed Sep 01 12:27:49 2010 +0200 (2010-09-01)
changeset 39017 8cd5b6d688fa
parent 38970 53d1ee3d98b8
child 39018 5681d7cfabce
permissions -rw-r--r--
generalized hierarchical data structure over statements
     1 (*  Title:      Tools/Code/code_namespace.ML
     2     Author:     Florian Haftmann, TU Muenchen
     3 
     4 Mastering target language namespaces.
     5 *)
     6 
     7 signature CODE_NAMESPACE =
     8 sig
     9   datatype ('a, 'b) node =
    10       Dummy
    11     | Stmt of 'a
    12     | Module of ('b * (string * ('a, 'b) node) Graph.T);
    13   val hierarchical_program: (string -> string) -> { module_alias: string -> string option,
    14     reserved: Name.context, empty_nsp: 'a, namify_module: string -> 'a -> string * 'a,
    15     namify_stmt: Code_Thingol.stmt -> string -> 'a -> string * 'a,
    16     cyclic_modules: bool, empty_data: 'b, memorize_data: string -> 'b -> 'b }
    17       -> Code_Thingol.program
    18       -> { deresolver: string list -> string -> string,
    19            hierarchical_program: (string * (Code_Thingol.stmt, 'b) node) Graph.T }
    20 end;
    21 
    22 structure Code_Namespace : CODE_NAMESPACE =
    23 struct
    24 
    25 (* hierarchical program structure *)
    26 
    27 datatype ('a, 'b) node =
    28     Dummy
    29   | Stmt of 'a
    30   | Module of ('b * (string * ('a, 'b) node) Graph.T);
    31 
    32 fun hierarchical_program labelled_name { module_alias, reserved, empty_nsp,
    33       namify_module, namify_stmt, cyclic_modules, empty_data, memorize_data } program =
    34   let
    35 
    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);
    45 
    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)
    55       else (data,
    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;
    63 
    64     (* distribute statements over hierarchy *)
    65     fun add_stmt name stmt =
    66       let
    67         val (name_fragments, base) = dest_name name;
    68       in
    69         change_module name_fragments (fn (data, nodes) =>
    70           (memorize_data name data, Graph.new_node (name, (base, Stmt stmt)) nodes))
    71       end;
    72     fun add_dependency name name' =
    73       let
    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;
    90 
    91     (* name declarations *)
    92     fun make_declarations nsps (data, nodes) =
    93       let
    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) =
   102           let
   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;
   110         val nodes'' = nodes'
   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;
   115 
   116     (* deresolving *)
   117     fun deresolver prefix_fragments name =
   118       let
   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);
   126 
   127   in { deresolver = deresolver, hierarchical_program = hierarchical_program } end;
   128 
   129 end;