src/Tools/Code/code_namespace.ML
author haftmann
Wed Sep 01 17:14:42 2010 +0200 (2010-09-01)
changeset 39023 3f70c03e8282
parent 39022 ac7774a35bcf
child 39024 30d5dd2f30b6
permissions -rw-r--r--
simultaneous modification of 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: 'c, namify_module: string -> 'c -> string * 'c,
    15     namify_stmt: Code_Thingol.stmt -> string -> 'c -> string * 'c,
    16     cyclic_modules: bool, empty_data: 'b, memorize_data: string -> 'b -> 'b,
    17     modify_stmts: Code_Thingol.stmt list -> 'a option list }
    18       -> Code_Thingol.program
    19       -> { deresolver: string list -> string -> string,
    20            hierarchical_program: (string * ('a, 'b) node) Graph.T }
    21 end;
    22 
    23 structure Code_Namespace : CODE_NAMESPACE =
    24 struct
    25 
    26 (* hierarchical program structure *)
    27 
    28 datatype ('a, 'b) node =
    29     Dummy
    30   | Stmt of 'a
    31   | Module of ('b * (string * ('a, 'b) node) Graph.T);
    32 
    33 fun map_module_content f (Module content) = Module (f content);
    34 
    35 fun map_module [] = I
    36   | map_module (name_fragment :: name_fragments) =
    37       apsnd o Graph.map_node name_fragment o apsnd o map_module_content
    38         o map_module name_fragments;
    39 
    40 fun hierarchical_program labelled_name { module_alias, reserved, empty_nsp,
    41       namify_module, namify_stmt, cyclic_modules, empty_data, memorize_data, modify_stmts } program =
    42   let
    43 
    44     (* building module name hierarchy *)
    45     fun alias_fragments name = case module_alias name
    46      of SOME name' => Long_Name.explode name'
    47       | NONE => map (fn name => fst (yield_singleton Name.variants name reserved))
    48           (Long_Name.explode name);
    49     val module_names = Graph.fold (insert (op =) o fst o Code_Printer.dest_name o fst) program [];
    50     val fragments_tab = fold (fn name => Symtab.update
    51       (name, alias_fragments name)) module_names Symtab.empty;
    52     val dest_name = Code_Printer.dest_name #>> (the o Symtab.lookup fragments_tab);
    53 
    54     (* building empty module hierarchy *)
    55     val empty_module = (empty_data, Graph.empty);
    56     fun ensure_module name_fragment (data, nodes) =
    57       if can (Graph.get_node nodes) name_fragment then (data, nodes)
    58       else (data,
    59         nodes |> Graph.new_node (name_fragment, (name_fragment, Module empty_module)));
    60     fun allocate_module [] = I
    61       | allocate_module (name_fragment :: name_fragments) =
    62           ensure_module name_fragment
    63           #> (apsnd o Graph.map_node name_fragment o apsnd o map_module_content o allocate_module) name_fragments;
    64     val empty_program = Symtab.fold (fn (_, fragments) => allocate_module fragments)
    65       fragments_tab empty_module;
    66 
    67     (* distribute statements over hierarchy *)
    68     fun add_stmt name stmt =
    69       let
    70         val (name_fragments, base) = dest_name name;
    71       in
    72         (map_module name_fragments o apsnd) (Graph.new_node (name, (base, Stmt stmt)))
    73       end;
    74     fun add_dependency name name' =
    75       let
    76         val (name_fragments, base) = dest_name name;
    77         val (name_fragments', base') = dest_name name';
    78         val (name_fragments_common, (diff, diff')) =
    79           chop_prefix (op =) (name_fragments, name_fragments');
    80         val is_module = not (null diff andalso null diff');
    81         val dep = pairself hd (diff @ [name], diff' @ [name']);
    82         val add_edge = if is_module andalso not cyclic_modules
    83           then (fn node => Graph.add_edge_acyclic dep node
    84             handle Graph.CYCLES _ => error ("Dependency "
    85               ^ quote name ^ " -> " ^ quote name'
    86               ^ " would result in module dependency cycle"))
    87           else Graph.add_edge dep
    88       in (map_module name_fragments_common o apsnd) add_edge end;
    89     val proto_program = empty_program
    90       |> Graph.fold (fn (name, (stmt, _)) => add_stmt name stmt) program
    91       |> Graph.fold (fn (name, (_, (_, names))) => fold (add_dependency name) names) program;
    92 
    93     (* name declarations, data and statement modifications *)
    94     fun make_declarations nsps (data, nodes) =
    95       let
    96         val (module_fragments, stmt_names) = List.partition
    97           (fn name_fragment => case Graph.get_node nodes name_fragment
    98             of (_, Module _) => true | _ => false) (Graph.keys nodes);
    99         fun declare namify name (nsps, nodes) =
   100           let
   101             val (base, node) = Graph.get_node nodes name;
   102             val (base', nsps') = namify node base nsps;
   103             val nodes' = Graph.map_node name (K (base', node)) nodes;
   104           in (nsps', nodes') end;
   105         val (nsps', nodes') = (nsps, nodes)
   106           |> fold (declare (K namify_module)) module_fragments
   107           |> fold (declare (namify_stmt o (fn Stmt stmt => stmt))) stmt_names;
   108         val modify_stmts' = filter (member (op =) stmt_names)
   109           #> AList.make (snd o Graph.get_node nodes)
   110           #> split_list
   111           ##> map (fn Stmt stmt => stmt)
   112           ##> modify_stmts
   113           #> op ~~;
   114         val stmtss' = maps modify_stmts' (Graph.strong_conn nodes);
   115         val nodes'' = Graph.map (fn name => apsnd (fn Module content => Module (make_declarations nsps' content)
   116             | _ => case AList.lookup (op =) stmtss' name of SOME (SOME stmt) => Stmt stmt | _ => Dummy)) nodes';
   117         val data' = fold memorize_data stmt_names data;
   118       in (data', nodes'') end;
   119     val (_, hierarchical_program) = make_declarations empty_nsp proto_program;
   120 
   121     (* deresolving *)
   122     fun deresolver prefix_fragments name =
   123       let
   124         val (name_fragments, _) = dest_name name;
   125         val (_, (_, remainder)) = chop_prefix (op =) (prefix_fragments, name_fragments);
   126         val nodes = fold (fn name_fragment => fn nodes => case Graph.get_node nodes name_fragment
   127          of (_, Module (_, nodes)) => nodes) name_fragments hierarchical_program;
   128         val (base', _) = Graph.get_node nodes name;
   129       in Long_Name.implode (remainder @ [base']) end
   130         handle Graph.UNDEF _ => error ("Unknown statement name: " ^ labelled_name name);
   131 
   132   in { deresolver = deresolver, hierarchical_program = hierarchical_program } end;
   133 
   134 end;