src/Tools/Code/code_namespace.ML
author wenzelm
Thu Jun 09 17:51:49 2011 +0200 (2011-06-09)
changeset 43326 47cf4bc789aa
parent 40705 03f1266a066e
child 44338 700008399ee5
permissions -rw-r--r--
simplified Name.variant -- discontinued builtin fold_map;
     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   type flat_program
    10   val flat_program: (string -> string) -> { module_alias: string -> string option,
    11     module_prefix: string, reserved: Name.context, empty_nsp: 'a,
    12     namify_stmt: Code_Thingol.stmt -> string -> 'a -> string * 'a,
    13     modify_stmt: Code_Thingol.stmt -> Code_Thingol.stmt option }
    14       -> Code_Thingol.program
    15       -> { deresolver: string -> string -> string,
    16            flat_program: flat_program }
    17 
    18   datatype ('a, 'b) node =
    19       Dummy
    20     | Stmt of 'a
    21     | Module of ('b * (string * ('a, 'b) node) Graph.T)
    22   type ('a, 'b) hierarchical_program
    23   val hierarchical_program: (string -> string) -> { module_alias: string -> string option,
    24     reserved: Name.context, empty_nsp: 'c, namify_module: string -> 'c -> string * 'c,
    25     namify_stmt: Code_Thingol.stmt -> string -> 'c -> string * 'c,
    26     cyclic_modules: bool, empty_data: 'b, memorize_data: string -> 'b -> 'b,
    27     modify_stmts: (string * Code_Thingol.stmt) list -> 'a option list }
    28       -> Code_Thingol.program
    29       -> { deresolver: string list -> string -> string,
    30            hierarchical_program: ('a, 'b) hierarchical_program }
    31   val print_hierarchical: { print_module: string list -> string -> 'b -> 'c list -> 'c,
    32     print_stmt: string list -> string * 'a -> 'c,
    33     lift_markup: (Pretty.T -> Pretty.T) -> 'c -> 'c }
    34       -> ('a, 'b) hierarchical_program -> 'c list
    35 end;
    36 
    37 structure Code_Namespace : CODE_NAMESPACE =
    38 struct
    39 
    40 (** building module name hierarchy **)
    41 
    42 val dest_name =
    43   apfst Long_Name.implode o split_last o fst o split_last o Long_Name.explode;
    44 
    45 fun build_module_namespace { module_alias, module_prefix, reserved } program =
    46   let
    47     fun alias_fragments name = case module_alias name
    48      of SOME name' => Long_Name.explode name'
    49       | NONE => map (fn name => fst (Name.variant name reserved)) (Long_Name.explode name);
    50     val module_names = Graph.fold (insert (op =) o fst o dest_name o fst) program [];
    51   in
    52     fold (fn name => Symtab.update (name, Long_Name.explode module_prefix @ alias_fragments name))
    53       module_names Symtab.empty
    54   end;
    55 
    56 
    57 (** flat program structure **)
    58 
    59 type flat_program = ((string * Code_Thingol.stmt option) Graph.T * (string * string list) list) Graph.T;
    60 
    61 fun flat_program labelled_name { module_alias, module_prefix, reserved,
    62       empty_nsp, namify_stmt, modify_stmt } program =
    63   let
    64 
    65     (* building module name hierarchy *)
    66     val fragments_tab = build_module_namespace { module_alias = module_alias,
    67       module_prefix = module_prefix, reserved = reserved } program;
    68     val dest_name = dest_name
    69       #>> (Long_Name.implode o the o Symtab.lookup fragments_tab);
    70 
    71     (* distribute statements over hierarchy *)
    72     fun add_stmt name stmt =
    73       let
    74         val (module_name, base) = dest_name name;
    75       in
    76         Graph.default_node (module_name, (Graph.empty, []))
    77         #> (Graph.map_node module_name o apfst) (Graph.new_node (name, (base, stmt)))
    78       end;
    79     fun add_dependency name name' =
    80       let
    81         val (module_name, base) = dest_name name;
    82         val (module_name', base') = dest_name name';
    83       in if module_name = module_name'
    84         then (Graph.map_node module_name o apfst) (Graph.add_edge (name, name'))
    85         else (Graph.map_node module_name o apsnd) (AList.map_default (op =) (module_name', []) (insert (op =) name'))
    86       end;
    87     val proto_program = Graph.empty
    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 and statement modifications *)
    92     fun declare name (base, stmt) (gr, nsp) = 
    93       let
    94         val (base', nsp') = namify_stmt stmt base nsp;
    95         val gr' = (Graph.map_node name o apfst) (K base') gr;
    96       in (gr', nsp') end;
    97     fun declarations gr = (gr, empty_nsp)
    98       |> fold (fn name => declare name (Graph.get_node gr name)) (Graph.keys gr) 
    99       |> fst
   100       |> (Graph.map o K o apsnd) modify_stmt;
   101     val flat_program = proto_program
   102       |> (Graph.map o K o apfst) declarations;
   103 
   104     (* qualified and unqualified imports, deresolving *)
   105     fun base_deresolver name = fst (Graph.get_node
   106       (fst (Graph.get_node flat_program (fst (dest_name name)))) name);
   107     fun classify_names gr imports =
   108       let
   109         val import_tab = maps
   110           (fn (module_name, names) => map (rpair module_name) names) imports;
   111         val imported_names = map fst import_tab;
   112         val here_names = Graph.keys gr;
   113       in
   114         Symtab.empty
   115         |> fold (fn name => Symtab.update (name, base_deresolver name)) here_names
   116         |> fold (fn name => Symtab.update (name,
   117             Long_Name.append (the (AList.lookup (op =) import_tab name))
   118               (base_deresolver name))) imported_names
   119       end;
   120     val deresolver_tab = Symtab.make (AList.make
   121       (uncurry classify_names o Graph.get_node flat_program)
   122         (Graph.keys flat_program));
   123     fun deresolver "" name =
   124           Long_Name.append (fst (dest_name name)) (base_deresolver name)
   125       | deresolver module_name name =
   126           the (Symtab.lookup (the (Symtab.lookup deresolver_tab module_name)) name)
   127           handle Option => error ("Unknown statement name: " ^ labelled_name name);
   128 
   129   in { deresolver = deresolver, flat_program = flat_program } end;
   130 
   131 
   132 (** hierarchical program structure **)
   133 
   134 datatype ('a, 'b) node =
   135     Dummy
   136   | Stmt of 'a
   137   | Module of ('b * (string * ('a, 'b) node) Graph.T);
   138 
   139 type ('a, 'b) hierarchical_program = (string * ('a, 'b) node) Graph.T;
   140 
   141 fun map_module_content f (Module content) = Module (f content);
   142 
   143 fun map_module [] = I
   144   | map_module (name_fragment :: name_fragments) =
   145       apsnd o Graph.map_node name_fragment o apsnd o map_module_content
   146         o map_module name_fragments;
   147 
   148 fun hierarchical_program labelled_name { module_alias, reserved, empty_nsp,
   149       namify_module, namify_stmt, cyclic_modules, empty_data, memorize_data, modify_stmts } program =
   150   let
   151 
   152     (* building module name hierarchy *)
   153     val fragments_tab = build_module_namespace { module_alias = module_alias,
   154       module_prefix = "", reserved = reserved } program;
   155     val dest_name = dest_name #>> (the o Symtab.lookup fragments_tab);
   156 
   157     (* building empty module hierarchy *)
   158     val empty_module = (empty_data, Graph.empty);
   159     fun ensure_module name_fragment (data, nodes) =
   160       if can (Graph.get_node nodes) name_fragment then (data, nodes)
   161       else (data,
   162         nodes |> Graph.new_node (name_fragment, (name_fragment, Module empty_module)));
   163     fun allocate_module [] = I
   164       | allocate_module (name_fragment :: name_fragments) =
   165           ensure_module name_fragment
   166           #> (apsnd o Graph.map_node name_fragment o apsnd o map_module_content o allocate_module) name_fragments;
   167     val empty_program = Symtab.fold (fn (_, fragments) => allocate_module fragments)
   168       fragments_tab empty_module;
   169 
   170     (* distribute statements over hierarchy *)
   171     fun add_stmt name stmt =
   172       let
   173         val (name_fragments, base) = dest_name name;
   174       in
   175         (map_module name_fragments o apsnd) (Graph.new_node (name, (base, Stmt stmt)))
   176       end;
   177     fun add_dependency name name' =
   178       let
   179         val (name_fragments, base) = dest_name name;
   180         val (name_fragments', base') = dest_name name';
   181         val (name_fragments_common, (diff, diff')) =
   182           chop_prefix (op =) (name_fragments, name_fragments');
   183         val is_module = not (null diff andalso null diff');
   184         val dep = pairself hd (diff @ [name], diff' @ [name']);
   185         val add_edge = if is_module andalso not cyclic_modules
   186           then (fn node => Graph.add_edge_acyclic dep node
   187             handle Graph.CYCLES _ => error ("Dependency "
   188               ^ quote name ^ " -> " ^ quote name'
   189               ^ " would result in module dependency cycle"))
   190           else Graph.add_edge dep
   191       in (map_module name_fragments_common o apsnd) add_edge end;
   192     val proto_program = empty_program
   193       |> Graph.fold (fn (name, (stmt, _)) => add_stmt name stmt) program
   194       |> Graph.fold (fn (name, (_, (_, names))) => fold (add_dependency name) names) program;
   195 
   196     (* name declarations, data and statement modifications *)
   197     fun make_declarations nsps (data, nodes) =
   198       let
   199         val (module_fragments, stmt_names) = List.partition
   200           (fn name_fragment => case Graph.get_node nodes name_fragment
   201             of (_, Module _) => true | _ => false) (Graph.keys nodes);
   202         fun declare namify name (nsps, nodes) =
   203           let
   204             val (base, node) = Graph.get_node nodes name;
   205             val (base', nsps') = namify node base nsps;
   206             val nodes' = Graph.map_node name (K (base', node)) nodes;
   207           in (nsps', nodes') end;
   208         val (nsps', nodes') = (nsps, nodes)
   209           |> fold (declare (K namify_module)) module_fragments
   210           |> fold (declare (namify_stmt o (fn Stmt stmt => stmt))) stmt_names;
   211         fun zip_fillup xs ys = xs ~~ ys @ replicate (length xs - length ys) NONE;
   212         fun select_names names = case filter (member (op =) stmt_names) names
   213          of [] => NONE
   214           | xs => SOME xs;
   215         val modify_stmts' = AList.make (snd o Graph.get_node nodes)
   216           #> split_list
   217           ##> map (fn Stmt stmt => stmt)
   218           #> (fn (names, stmts) => zip_fillup names (modify_stmts (names ~~ stmts)));
   219         val stmtss' = (maps modify_stmts' o map_filter select_names o Graph.strong_conn) nodes;
   220         val nodes'' = Graph.map (fn name => apsnd (fn Module content => Module (make_declarations nsps' content)
   221             | _ => case AList.lookup (op =) stmtss' name of SOME (SOME stmt) => Stmt stmt | _ => Dummy)) nodes';
   222         val data' = fold memorize_data stmt_names data;
   223       in (data', nodes'') end;
   224     val (_, hierarchical_program) = make_declarations empty_nsp proto_program;
   225 
   226     (* deresolving *)
   227     fun deresolver prefix_fragments name =
   228       let
   229         val (name_fragments, _) = dest_name name;
   230         val (_, (_, remainder)) = chop_prefix (op =) (prefix_fragments, name_fragments);
   231         val nodes = fold (fn name_fragment => fn nodes => case Graph.get_node nodes name_fragment
   232          of (_, Module (_, nodes)) => nodes) name_fragments hierarchical_program;
   233         val (base', _) = Graph.get_node nodes name;
   234       in Long_Name.implode (remainder @ [base']) end
   235         handle Graph.UNDEF _ => error ("Unknown statement name: " ^ labelled_name name);
   236 
   237   in { deresolver = deresolver, hierarchical_program = hierarchical_program } end;
   238 
   239 fun print_hierarchical { print_module, print_stmt, lift_markup } =
   240   let
   241     fun print_node _ (_, Dummy) =
   242           NONE
   243       | print_node prefix_fragments (name, Stmt stmt) =
   244           SOME (lift_markup (Code_Printer.markup_stmt name)
   245             (print_stmt prefix_fragments (name, stmt)))
   246       | print_node prefix_fragments (name_fragment, Module (data, nodes)) =
   247           let
   248             val prefix_fragments' = prefix_fragments @ [name_fragment]
   249           in
   250             Option.map (print_module prefix_fragments'
   251               name_fragment data) (print_nodes prefix_fragments' nodes)
   252           end
   253     and print_nodes prefix_fragments nodes =
   254       let
   255         val xs = (map_filter (fn name => print_node prefix_fragments
   256           (name, snd (Graph.get_node nodes name))) o rev o flat o Graph.strong_conn) nodes
   257       in if null xs then NONE else SOME xs end;
   258   in these o print_nodes [] end;
   259 
   260 end;