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