src/Tools/Code/code_namespace.ML
author haftmann
Wed, 01 Sep 2010 15:09:43 +0200
changeset 39262 5681d7cfabce
parent 39261 8cd5b6d688fa
child 39266 ac7774a35bcf
permissions -rw-r--r--
tuned
     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 map_module_content f (Module content) = Module (f content);
    33 
    34 fun map_module [] = I
    35   | map_module (name_fragment :: name_fragments) =
    36       apsnd o Graph.map_node name_fragment o apsnd o map_module_content
    37         o map_module name_fragments;
    38 
    39 fun hierarchical_program labelled_name { module_alias, reserved, empty_nsp,
    40       namify_module, namify_stmt, cyclic_modules, empty_data, memorize_data } program =
    41   let
    42 
    43     (* building module name hierarchy *)
    44     fun alias_fragments name = case module_alias name
    45      of SOME name' => Long_Name.explode name'
    46       | NONE => map (fn name => fst (yield_singleton Name.variants name reserved))
    47           (Long_Name.explode name);
    48     val module_names = Graph.fold (insert (op =) o fst o Code_Printer.dest_name o fst) program [];
    49     val fragments_tab = fold (fn name => Symtab.update
    50       (name, alias_fragments name)) module_names Symtab.empty;
    51     val dest_name = Code_Printer.dest_name #>> (the o Symtab.lookup fragments_tab);
    52 
    53     (* building empty module hierarchy *)
    54     val empty_module = (empty_data, Graph.empty);
    55     fun ensure_module name_fragment (data, nodes) =
    56       if can (Graph.get_node nodes) name_fragment then (data, nodes)
    57       else (data,
    58         nodes |> Graph.new_node (name_fragment, (name_fragment, Module empty_module)));
    59     fun allocate_module [] = I
    60       | allocate_module (name_fragment :: name_fragments) =
    61           ensure_module name_fragment
    62           #> (apsnd o Graph.map_node name_fragment o apsnd o map_module_content o allocate_module) name_fragments;
    63     val empty_program = Symtab.fold (fn (_, fragments) => allocate_module fragments)
    64       fragments_tab empty_module;
    65 
    66     (* distribute statements over hierarchy *)
    67     fun add_stmt name stmt =
    68       let
    69         val (name_fragments, base) = dest_name name;
    70       in
    71         (map_module name_fragments o apsnd) (Graph.new_node (name, (base, Stmt stmt)))
    72       end;
    73     fun add_dependency name name' =
    74       let
    75         val (name_fragments, base) = dest_name name;
    76         val (name_fragments', base') = dest_name name';
    77         val (name_fragments_common, (diff, diff')) =
    78           chop_prefix (op =) (name_fragments, name_fragments');
    79         val (is_module, dep) = if null diff then (false, (name, name'))
    80           else (true, (hd diff, hd diff'))
    81         val add_edge = if is_module andalso not cyclic_modules
    82           then (fn node => Graph.add_edge_acyclic dep node
    83             handle Graph.CYCLES _ => error ("Dependency "
    84               ^ quote name ^ " -> " ^ quote name'
    85               ^ " would result in module dependency cycle"))
    86           else Graph.add_edge dep
    87       in (map_module name_fragments_common o apsnd) add_edge end;
    88     val proto_program = empty_program
    89       |> Graph.fold (fn (name, (stmt, _)) => add_stmt name stmt) program
    90       |> Graph.fold (fn (name, (_, (_, names))) => fold (add_dependency name) names) program;
    91 
    92     (* name declarations *)
    93     fun make_declarations nsps (data, nodes) =
    94       let
    95         val (module_fragments, stmt_names) = List.partition
    96           (fn name_fragment => case Graph.get_node nodes name_fragment
    97             of (_, Module _) => true | _ => false) (Graph.keys nodes);
    98         fun modify_stmt (Stmt (Code_Thingol.Datatypecons _)) = Dummy
    99           | modify_stmt (Stmt (Code_Thingol.Classrel _)) = Dummy
   100           | modify_stmt (Stmt (Code_Thingol.Classparam _)) = Dummy
   101           | modify_stmt stmt = stmt;
   102         fun declare namify modify name (nsps, nodes) =
   103           let
   104             val (base, node) = Graph.get_node nodes name;
   105             val (base', nsps') = namify node base nsps;
   106             val nodes' = Graph.map_node name (K (base', modify node)) nodes;
   107           in (nsps', nodes') end;
   108         val (nsps', nodes') = (nsps, nodes)
   109           |> fold (declare (K namify_module) I) module_fragments
   110           |> fold (declare (namify_stmt o (fn Stmt stmt => stmt)) modify_stmt) stmt_names;
   111         val nodes'' = nodes'
   112           |> fold (fn name_fragment => (Graph.map_node name_fragment
   113               o apsnd o map_module_content) (make_declarations nsps')) module_fragments;
   114         val data' = fold memorize_data stmt_names data;
   115       in (data', nodes'') end;
   116     val (_, hierarchical_program) = make_declarations empty_nsp proto_program;
   117 
   118     (* deresolving *)
   119     fun deresolver prefix_fragments name =
   120       let
   121         val (name_fragments, _) = dest_name name;
   122         val (_, (_, remainder)) = chop_prefix (op =) (prefix_fragments, name_fragments);
   123         val nodes = fold (fn name_fragment => fn nodes => case Graph.get_node nodes name_fragment
   124          of (_, Module (_, nodes)) => nodes) name_fragments hierarchical_program;
   125         val (base', _) = Graph.get_node nodes name;
   126       in Long_Name.implode (remainder @ [base']) end
   127         handle Graph.UNDEF _ => error ("Unknown statement name: " ^ labelled_name name);
   128 
   129   in { deresolver = deresolver, hierarchical_program = hierarchical_program } end;
   130 
   131 end;