src/Tools/Code/code_namespace.ML
author haftmann
Sat Sep 04 21:13:13 2010 +0200 (2010-09-04)
changeset 39147 3c284a152bd6
parent 39055 81e0368812ad
child 39203 b2f9a6f4b84b
permissions -rw-r--r--
printing combinator for hierarchical programs
     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   val dest_name: string -> string * string
    10   datatype ('a, 'b) node =
    11       Dummy
    12     | Stmt of 'a
    13     | Module of ('b * (string * ('a, 'b) node) Graph.T)
    14   type ('a, 'b) hierarchical_program = (string * ('a, 'b) node) Graph.T
    15   val hierarchical_program: (string -> string) -> { module_alias: string -> string option,
    16     reserved: Name.context, empty_nsp: 'c, namify_module: string -> 'c -> string * 'c,
    17     namify_stmt: Code_Thingol.stmt -> string -> 'c -> string * 'c,
    18     cyclic_modules: bool, empty_data: 'b, memorize_data: string -> 'b -> 'b,
    19     modify_stmts: (string * Code_Thingol.stmt) list -> 'a option list }
    20       -> Code_Thingol.program
    21       -> { deresolver: string list -> string -> string,
    22            hierarchical_program: ('a, 'b) hierarchical_program }
    23   val print_hierarchical: { print_module: string list -> string -> 'b -> 'c list -> 'c,
    24     print_stmt: string list -> string * 'a -> 'c,
    25     lift_markup: (Pretty.T -> Pretty.T) -> 'c -> 'c }
    26       -> ('a, 'b) hierarchical_program -> 'c list
    27 end;
    28 
    29 structure Code_Namespace : CODE_NAMESPACE =
    30 struct
    31 
    32 (** splitting names in module and base part **)
    33 
    34 val dest_name =
    35   apfst Long_Name.implode o split_last o fst o split_last o Long_Name.explode;
    36 
    37 
    38 (** hierarchical program structure **)
    39 
    40 datatype ('a, 'b) node =
    41     Dummy
    42   | Stmt of 'a
    43   | Module of ('b * (string * ('a, 'b) node) Graph.T);
    44 
    45 type ('a, 'b) hierarchical_program = (string * ('a, 'b) node) Graph.T;
    46 
    47 fun map_module_content f (Module content) = Module (f content);
    48 
    49 fun map_module [] = I
    50   | map_module (name_fragment :: name_fragments) =
    51       apsnd o Graph.map_node name_fragment o apsnd o map_module_content
    52         o map_module name_fragments;
    53 
    54 fun hierarchical_program labelled_name { module_alias, reserved, empty_nsp,
    55       namify_module, namify_stmt, cyclic_modules, empty_data, memorize_data, modify_stmts } program =
    56   let
    57 
    58     (* building module name hierarchy *)
    59     fun alias_fragments name = case module_alias name
    60      of SOME name' => Long_Name.explode name'
    61       | NONE => map (fn name => fst (yield_singleton Name.variants name reserved))
    62           (Long_Name.explode name);
    63     val module_names = Graph.fold (insert (op =) o fst o dest_name o fst) program [];
    64     val fragments_tab = fold (fn name => Symtab.update
    65       (name, alias_fragments name)) module_names Symtab.empty;
    66     val dest_name = dest_name #>> (the o Symtab.lookup fragments_tab);
    67 
    68     (* building empty module hierarchy *)
    69     val empty_module = (empty_data, Graph.empty);
    70     fun ensure_module name_fragment (data, nodes) =
    71       if can (Graph.get_node nodes) name_fragment then (data, nodes)
    72       else (data,
    73         nodes |> Graph.new_node (name_fragment, (name_fragment, Module empty_module)));
    74     fun allocate_module [] = I
    75       | allocate_module (name_fragment :: name_fragments) =
    76           ensure_module name_fragment
    77           #> (apsnd o Graph.map_node name_fragment o apsnd o map_module_content o allocate_module) name_fragments;
    78     val empty_program = Symtab.fold (fn (_, fragments) => allocate_module fragments)
    79       fragments_tab empty_module;
    80 
    81     (* distribute statements over hierarchy *)
    82     fun add_stmt name stmt =
    83       let
    84         val (name_fragments, base) = dest_name name;
    85       in
    86         (map_module name_fragments o apsnd) (Graph.new_node (name, (base, Stmt stmt)))
    87       end;
    88     fun add_dependency name name' =
    89       let
    90         val (name_fragments, base) = dest_name name;
    91         val (name_fragments', base') = dest_name name';
    92         val (name_fragments_common, (diff, diff')) =
    93           chop_prefix (op =) (name_fragments, name_fragments');
    94         val is_module = not (null diff andalso null diff');
    95         val dep = pairself hd (diff @ [name], diff' @ [name']);
    96         val add_edge = if is_module andalso not cyclic_modules
    97           then (fn node => Graph.add_edge_acyclic dep node
    98             handle Graph.CYCLES _ => error ("Dependency "
    99               ^ quote name ^ " -> " ^ quote name'
   100               ^ " would result in module dependency cycle"))
   101           else Graph.add_edge dep
   102       in (map_module name_fragments_common o apsnd) add_edge end;
   103     val proto_program = empty_program
   104       |> Graph.fold (fn (name, (stmt, _)) => add_stmt name stmt) program
   105       |> Graph.fold (fn (name, (_, (_, names))) => fold (add_dependency name) names) program;
   106 
   107     (* name declarations, data and statement modifications *)
   108     fun make_declarations nsps (data, nodes) =
   109       let
   110         val (module_fragments, stmt_names) = List.partition
   111           (fn name_fragment => case Graph.get_node nodes name_fragment
   112             of (_, Module _) => true | _ => false) (Graph.keys nodes);
   113         fun declare namify name (nsps, nodes) =
   114           let
   115             val (base, node) = Graph.get_node nodes name;
   116             val (base', nsps') = namify node base nsps;
   117             val nodes' = Graph.map_node name (K (base', node)) nodes;
   118           in (nsps', nodes') end;
   119         val (nsps', nodes') = (nsps, nodes)
   120           |> fold (declare (K namify_module)) module_fragments
   121           |> fold (declare (namify_stmt o (fn Stmt stmt => stmt))) stmt_names;
   122         fun zip_fillup xs ys = xs ~~ ys @ replicate (length xs - length ys) NONE;
   123         fun select_names names = case filter (member (op =) stmt_names) names
   124          of [] => NONE
   125           | xs => SOME xs;
   126         val modify_stmts' = AList.make (snd o Graph.get_node nodes)
   127           #> split_list
   128           ##> map (fn Stmt stmt => stmt)
   129           #> (fn (names, stmts) => zip_fillup names (modify_stmts (names ~~ stmts)));
   130         val stmtss' = (maps modify_stmts' o map_filter select_names o Graph.strong_conn) nodes;
   131         val nodes'' = Graph.map (fn name => apsnd (fn Module content => Module (make_declarations nsps' content)
   132             | _ => case AList.lookup (op =) stmtss' name of SOME (SOME stmt) => Stmt stmt | _ => Dummy)) nodes';
   133         val data' = fold memorize_data stmt_names data;
   134       in (data', nodes'') end;
   135     val (_, hierarchical_program) = make_declarations empty_nsp proto_program;
   136 
   137     (* deresolving *)
   138     fun deresolver prefix_fragments name =
   139       let
   140         val (name_fragments, _) = dest_name name;
   141         val (_, (_, remainder)) = chop_prefix (op =) (prefix_fragments, name_fragments);
   142         val nodes = fold (fn name_fragment => fn nodes => case Graph.get_node nodes name_fragment
   143          of (_, Module (_, nodes)) => nodes) name_fragments hierarchical_program;
   144         val (base', _) = Graph.get_node nodes name;
   145       in Long_Name.implode (remainder @ [base']) end
   146         handle Graph.UNDEF _ => error ("Unknown statement name: " ^ labelled_name name);
   147 
   148   in { deresolver = deresolver, hierarchical_program = hierarchical_program } end;
   149 
   150 fun print_hierarchical { print_module, print_stmt, lift_markup } =
   151   let
   152     fun print_node _ (_, Dummy) =
   153           NONE
   154       | print_node prefix_fragments (name, Stmt stmt) =
   155           SOME (lift_markup (Code_Printer.markup_stmt name)
   156             (print_stmt prefix_fragments (name, stmt)))
   157       | print_node prefix_fragments (name_fragment, Module (data, nodes)) =
   158           let
   159             val prefix_fragments' = prefix_fragments @ [name_fragment]
   160           in
   161             Option.map (print_module prefix_fragments'
   162               name_fragment data) (print_nodes prefix_fragments' nodes)
   163           end
   164     and print_nodes prefix_fragments nodes =
   165       let
   166         val xs = (map_filter (fn name => print_node prefix_fragments
   167           (name, snd (Graph.get_node nodes name))) o rev o flat o Graph.strong_conn) nodes
   168       in if null xs then NONE else SOME xs end;
   169   in these o print_nodes [] end;
   170 
   171 end;