src/Tools/Code/code_namespace.ML
author haftmann
Sun Feb 23 10:33:43 2014 +0100 (2014-02-23)
changeset 55680 bc5edc5dbf18
parent 55679 59244fc1a7ca
child 55681 7714287dc044
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: Proof.context
    11     -> { module_prefix: string, module_name: string,
    12     reserved: Name.context, identifiers: Code_Target.identifier_data, empty_nsp: 'a,
    13     namify_stmt: Code_Thingol.stmt -> string -> 'a -> string * 'a,
    14     modify_stmt: Code_Thingol.stmt -> Code_Thingol.stmt option }
    15       -> Code_Thingol.program
    16       -> { deresolver: string -> Code_Symbol.T -> string,
    17            flat_program: flat_program }
    18 
    19   datatype ('a, 'b) node =
    20       Dummy
    21     | Stmt of bool * 'a
    22     | Module of ('b * (string * ('a, 'b) node) Code_Symbol.Graph.T)
    23   type ('a, 'b) hierarchical_program
    24   val hierarchical_program: Proof.context
    25     -> { module_name: string,
    26     reserved: Name.context, identifiers: Code_Target.identifier_data,
    27     empty_nsp: 'c, namify_module: string -> 'c -> string * 'c,
    28     namify_stmt: Code_Thingol.stmt -> string -> 'c -> string * 'c,
    29     cyclic_modules: bool, empty_data: 'b, memorize_data: Code_Symbol.T -> 'b -> 'b,
    30     modify_stmts: (Code_Symbol.T * Code_Thingol.stmt) list -> 'a option list }
    31       -> Code_Thingol.program
    32       -> { deresolver: string list -> Code_Symbol.T -> string,
    33            hierarchical_program: ('a, 'b) hierarchical_program }
    34   val print_hierarchical: { print_module: string list -> string -> 'b -> 'c list -> 'c,
    35     print_stmt: string list -> Code_Symbol.T * (bool * 'a) -> 'c,
    36     lift_markup: (Pretty.T -> Pretty.T) -> 'c -> 'c }
    37       -> ('a, 'b) hierarchical_program -> 'c list
    38 end;
    39 
    40 structure Code_Namespace : CODE_NAMESPACE =
    41 struct
    42 
    43 (** fundamental module name hierarchy **)
    44 
    45 fun module_fragments' { identifiers, reserved } name =
    46   case Code_Symbol.lookup_module_data identifiers name of
    47       SOME (fragments, _) => fragments
    48     | NONE => map (fn fragment => fst (Name.variant fragment reserved)) (Long_Name.explode name);
    49 
    50 fun module_fragments { module_name, identifiers, reserved } =
    51   if module_name = ""
    52   then module_fragments' { identifiers = identifiers, reserved = reserved }
    53   else K (Long_Name.explode module_name);
    54 
    55 fun build_module_namespace ctxt { module_prefix, module_name, identifiers, reserved } program =
    56   let
    57     val module_names = Code_Symbol.Graph.fold (insert (op =) o Code_Symbol.default_prefix ctxt o fst) program [];
    58     val module_fragments' = module_fragments
    59       { module_name = module_name, identifiers = identifiers, reserved = reserved };
    60   in
    61     fold (fn name => Symtab.update (name, Long_Name.explode module_prefix @ module_fragments' name))
    62       module_names Symtab.empty
    63   end;
    64 
    65 fun prep_symbol ctxt { module_namespace, force_module, identifiers } sym =
    66   case Code_Symbol.lookup identifiers sym of
    67       NONE => ((the o Symtab.lookup module_namespace o Code_Symbol.default_prefix ctxt) sym,
    68         Code_Symbol.default_base sym)
    69     | SOME prefix_name => if null force_module then prefix_name
    70         else (force_module, snd prefix_name);
    71 
    72 fun has_priority identifiers = is_some o Code_Symbol.lookup identifiers;
    73 
    74 fun build_proto_program { empty, add_stmt, add_dep } program =
    75   empty
    76   |> Code_Symbol.Graph.fold (fn (sym, (stmt, _)) => add_stmt sym stmt) program
    77   |> Code_Symbol.Graph.fold (fn (sym, (_, (_, syms))) =>
    78       Code_Symbol.Graph.Keys.fold (add_dep sym) syms) program;
    79 
    80 fun prioritize has_priority = uncurry append o List.partition has_priority;
    81 
    82 
    83 (** flat program structure **)
    84 
    85 type flat_program = ((string * (bool * Code_Thingol.stmt) option) Code_Symbol.Graph.T * (string * Code_Symbol.T list) list) Graph.T;
    86 
    87 fun flat_program ctxt { module_prefix, module_name, reserved,
    88     identifiers, empty_nsp, namify_stmt, modify_stmt } program =
    89   let
    90 
    91     (* building module name hierarchy *)
    92     val module_namespace = build_module_namespace ctxt { module_prefix = module_prefix,
    93       module_name = module_name, identifiers = identifiers, reserved = reserved } program;
    94     val prep_sym = prep_symbol ctxt { module_namespace = module_namespace,
    95       force_module = Long_Name.explode module_name, identifiers = identifiers }
    96       #>> Long_Name.implode;
    97     val sym_priority = has_priority identifiers;
    98 
    99     (* distribute statements over hierarchy *)
   100     fun add_stmt sym stmt =
   101       let
   102         val (module_name, base) = prep_sym sym;
   103       in
   104         Graph.default_node (module_name, (Code_Symbol.Graph.empty, []))
   105         #> (Graph.map_node module_name o apfst)
   106           (Code_Symbol.Graph.new_node (sym, (base, (true, stmt))))
   107       end;
   108     fun add_dep sym sym' =
   109       let
   110         val (module_name, _) = prep_sym sym;
   111         val (module_name', _) = prep_sym sym';
   112       in if module_name = module_name'
   113         then (Graph.map_node module_name o apfst) (Code_Symbol.Graph.add_edge (sym, sym'))
   114         else (Graph.map_node module_name o apsnd)
   115           (AList.map_default (op =) (module_name', []) (insert (op =) sym'))
   116       end;
   117     val proto_program = build_proto_program
   118       { empty = Graph.empty, add_stmt = add_stmt, add_dep = add_dep } program;
   119 
   120     (* name declarations and statement modifications *)
   121     fun declare sym (base, (_, stmt)) (gr, nsp) = 
   122       let
   123         val (base', nsp') = namify_stmt stmt base nsp;
   124         val gr' = (Code_Symbol.Graph.map_node sym o apfst) (K base') gr;
   125       in (gr', nsp') end;
   126     fun declarations gr = (gr, empty_nsp)
   127       |> fold (fn sym => declare sym (Code_Symbol.Graph.get_node gr sym))
   128           (prioritize sym_priority (Code_Symbol.Graph.keys gr))
   129       |> fst
   130       |> Code_Symbol.Graph.map_strong_conn (fn syms_bases_exports_stmts =>
   131         map snd syms_bases_exports_stmts
   132         |> (map o apsnd) (fn (export, stmt) => Option.map (pair export) (modify_stmt stmt)));
   133     val flat_program = proto_program
   134       |> (Graph.map o K o apfst) declarations;
   135 
   136     (* qualified and unqualified imports, deresolving *)
   137     fun base_deresolver sym = fst (Code_Symbol.Graph.get_node
   138       (fst (Graph.get_node flat_program (fst (prep_sym sym)))) sym);
   139     fun classify_names gr imports =
   140       let
   141         val import_tab = maps
   142           (fn (module_name, syms) => map (rpair module_name) syms) imports;
   143         val imported_syms = map fst import_tab;
   144         val here_syms = Code_Symbol.Graph.keys gr;
   145       in
   146         Code_Symbol.Table.empty
   147         |> fold (fn sym => Code_Symbol.Table.update (sym, base_deresolver sym)) here_syms
   148         |> fold (fn sym => Code_Symbol.Table.update (sym,
   149             Long_Name.append (the (AList.lookup (op =) import_tab sym))
   150               (base_deresolver sym))) imported_syms
   151       end;
   152     val deresolver_tab = Symtab.make (AList.make
   153       (uncurry classify_names o Graph.get_node flat_program)
   154         (Graph.keys flat_program));
   155     fun deresolver "" sym =
   156           Long_Name.append (fst (prep_sym sym)) (base_deresolver sym)
   157       | deresolver module_name sym =
   158           the (Code_Symbol.Table.lookup (the (Symtab.lookup deresolver_tab module_name)) sym)
   159           handle Option.Option => error ("Unknown statement name: "
   160             ^ Code_Symbol.quote ctxt sym);
   161 
   162   in { deresolver = deresolver, flat_program = flat_program } end;
   163 
   164 
   165 (** hierarchical program structure **)
   166 
   167 datatype ('a, 'b) node =
   168     Dummy
   169   | Stmt of bool * 'a
   170   | Module of ('b * (string * ('a, 'b) node) Code_Symbol.Graph.T);
   171 
   172 type ('a, 'b) hierarchical_program = (string * ('a, 'b) node) Code_Symbol.Graph.T;
   173 
   174 fun the_stmt (Stmt (export, stmt)) = (export, stmt);
   175 
   176 fun map_module_content f (Module content) = Module (f content);
   177 
   178 fun map_module [] = I
   179   | map_module (name_fragment :: name_fragments) =
   180       apsnd o Code_Symbol.Graph.map_node (Code_Symbol.Module name_fragment) o apsnd o map_module_content
   181         o map_module name_fragments;
   182 
   183 fun map_module_stmts f_module f_stmts sym_base_nodes =
   184   let
   185     val some_modules =
   186       sym_base_nodes
   187       |> map (fn (sym, (base, Module content)) => SOME (base, content) | _ => NONE)
   188       |> (burrow_options o map o apsnd) f_module;
   189     val some_export_stmts =
   190       sym_base_nodes
   191       |> map (fn (sym, (base, Stmt export_stmt)) => SOME ((sym, export_stmt), base) | _ => NONE)
   192       |> (burrow_options o burrow_fst) (fn [] => [] | xs => f_stmts xs)
   193   in
   194     map2 (fn SOME (base, content) => (K (base, Module content))
   195       | NONE => fn SOME (some_export_stmt, base) =>
   196           (base, case some_export_stmt of SOME export_stmt => Stmt export_stmt | NONE => Dummy))
   197       some_modules some_export_stmts
   198   end;
   199 
   200 fun hierarchical_program ctxt { module_name, reserved, identifiers, empty_nsp,
   201       namify_module, namify_stmt, cyclic_modules, empty_data, memorize_data, modify_stmts } program =
   202   let
   203 
   204     (* building module name hierarchy *)
   205     val module_namespace = build_module_namespace ctxt { module_prefix = "",
   206       module_name = module_name, identifiers = identifiers, reserved = reserved } program;
   207     val prep_sym = prep_symbol ctxt { module_namespace = module_namespace,
   208       force_module = Long_Name.explode module_name, identifiers = identifiers }
   209     val sym_priority = has_priority identifiers;
   210 
   211     (* building empty module hierarchy *)
   212     val empty_module = (empty_data, Code_Symbol.Graph.empty);
   213     fun ensure_module name_fragment (data, nodes) =
   214       if can (Code_Symbol.Graph.get_node nodes) (Code_Symbol.Module name_fragment) then (data, nodes)
   215       else (data,
   216         nodes |> Code_Symbol.Graph.new_node (Code_Symbol.Module name_fragment, (name_fragment, Module empty_module)));
   217     fun allocate_module [] = I
   218       | allocate_module (name_fragment :: name_fragments) =
   219           ensure_module name_fragment
   220           #> (apsnd o Code_Symbol.Graph.map_node (Code_Symbol.Module name_fragment) o apsnd o map_module_content o allocate_module) name_fragments;
   221     val empty_program =
   222       empty_module
   223       |> Symtab.fold (fn (_, fragments) => allocate_module fragments) module_namespace
   224       |> Code_Symbol.Graph.fold (allocate_module o these o Option.map fst
   225           o Code_Symbol.lookup identifiers o fst) program;
   226 
   227     (* distribute statements over hierarchy *)
   228     fun add_stmt sym stmt =
   229       let
   230         val (name_fragments, base) = prep_sym sym;
   231       in
   232         (map_module name_fragments o apsnd)
   233           (Code_Symbol.Graph.new_node (sym, (base, Stmt (true, stmt))))
   234       end;
   235     fun add_edge_acyclic_error error_msg dep gr =
   236       Code_Symbol.Graph.add_edge_acyclic dep gr
   237         handle Graph.CYCLES _ => error (error_msg ())
   238     fun add_dep sym sym' =
   239       let
   240         val (name_fragments, _) = prep_sym sym;
   241         val (name_fragments', _) = prep_sym sym';
   242         val (name_fragments_common, (diff, diff')) =
   243           chop_prefix (op =) (name_fragments, name_fragments');
   244         val is_cross_module = not (null diff andalso null diff');
   245         val dep = pairself hd (map Code_Symbol.Module diff @ [sym], map Code_Symbol.Module diff' @ [sym']);
   246         val add_edge = if is_cross_module andalso not cyclic_modules
   247           then add_edge_acyclic_error (fn _ => "Dependency "
   248             ^ Code_Symbol.quote ctxt sym ^ " -> "
   249             ^ Code_Symbol.quote ctxt sym'
   250             ^ " would result in module dependency cycle") dep
   251           else Code_Symbol.Graph.add_edge dep;
   252       in (map_module name_fragments_common o apsnd) add_edge end;
   253     val proto_program = build_proto_program
   254       { empty = empty_program, add_stmt = add_stmt, add_dep = add_dep } program;
   255 
   256     (* name declarations, data and statement modifications *)
   257     fun make_declarations nsps (data, nodes) =
   258       let
   259         val (module_fragments, stmt_syms) =
   260           Code_Symbol.Graph.keys nodes
   261           |> List.partition
   262               (fn sym => case Code_Symbol.Graph.get_node nodes sym
   263                 of (_, Module _) => true | _ => false)
   264           |> pairself (prioritize sym_priority)
   265         fun declare namify sym (nsps, nodes) =
   266           let
   267             val (base, node) = Code_Symbol.Graph.get_node nodes sym;
   268             val (base', nsps') = namify node base nsps;
   269             val nodes' = Code_Symbol.Graph.map_node sym (K (base', node)) nodes;
   270           in (nsps', nodes') end;
   271         val (nsps', nodes') = (nsps, nodes)
   272           |> fold (declare (K namify_module)) module_fragments
   273           |> fold (declare (namify_stmt o snd o the_stmt)) stmt_syms;
   274         fun modify_stmts' syms_stmts =
   275           let
   276             val stmts' = modify_stmts syms_stmts
   277           in stmts' @ replicate (length syms_stmts - length stmts') NONE end;
   278         fun modify_stmts'' syms_exports_stmts =
   279           syms_exports_stmts
   280           |> map (fn (sym, (export, stmt)) => ((sym, stmt), export))
   281           |> burrow_fst modify_stmts'
   282           |> map (fn (SOME stmt, export) => SOME (export, stmt) | _ => NONE);
   283         val nodes'' =
   284           nodes'
   285           |> Code_Symbol.Graph.map_strong_conn (map_module_stmts (make_declarations nsps') modify_stmts'');
   286         val data' = fold memorize_data stmt_syms data;
   287       in (data', nodes'') end;
   288     val (_, hierarchical_program) = make_declarations empty_nsp proto_program;
   289 
   290     (* deresolving *)
   291     fun deresolver prefix_fragments sym =
   292       let
   293         val (name_fragments, _) = prep_sym sym;
   294         val (_, (_, remainder)) = chop_prefix (op =) (prefix_fragments, name_fragments);
   295         val nodes = fold (fn name_fragment => fn nodes => case Code_Symbol.Graph.get_node nodes (Code_Symbol.Module name_fragment)
   296          of (_, Module (_, nodes)) => nodes) name_fragments hierarchical_program;
   297         val (base', _) = Code_Symbol.Graph.get_node nodes sym;
   298       in Long_Name.implode (remainder @ [base']) end
   299         handle Code_Symbol.Graph.UNDEF _ => error ("Unknown statement name: "
   300           ^ Code_Symbol.quote ctxt sym);
   301 
   302   in { deresolver = deresolver, hierarchical_program = hierarchical_program } end;
   303 
   304 fun print_hierarchical { print_module, print_stmt, lift_markup } =
   305   let
   306     fun print_node _ (_, Dummy) =
   307           NONE
   308       | print_node prefix_fragments (sym, Stmt stmt) =
   309           SOME (lift_markup (Code_Printer.markup_stmt sym)
   310             (print_stmt prefix_fragments (sym, stmt)))
   311       | print_node prefix_fragments (Code_Symbol.Module name_fragment, Module (data, nodes)) =
   312           let
   313             val prefix_fragments' = prefix_fragments @ [name_fragment]
   314           in
   315             Option.map (print_module prefix_fragments'
   316               name_fragment data) (print_nodes prefix_fragments' nodes)
   317           end
   318     and print_nodes prefix_fragments nodes =
   319       let
   320         val xs = (map_filter (fn sym => print_node prefix_fragments
   321           (sym, snd (Code_Symbol.Graph.get_node nodes sym))) o rev o flat o Code_Symbol.Graph.strong_conn) nodes
   322       in if null xs then NONE else SOME xs end;
   323   in these o print_nodes [] end;
   324 
   325 end;