src/Tools/Code/code_namespace.ML
author haftmann
Sat Jan 25 23:50:49 2014 +0100 (2014-01-25)
changeset 55147 bce3dbc11f95
parent 53414 dd64696d267a
child 55150 0940309ed8f1
permissions -rw-r--r--
prefer explicit code symbol type over ad-hoc name mangling
     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.symbol -> string,
    17            flat_program: flat_program }
    18 
    19   datatype ('a, 'b) node =
    20       Dummy
    21     | Stmt of '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.symbol -> 'b -> 'b,
    30     modify_stmts: (Code_Symbol.symbol * Code_Thingol.stmt) list -> 'a option list }
    31       -> Code_Thingol.program
    32       -> { deresolver: string list -> Code_Symbol.symbol -> 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.symbol * '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 lookup_identifier identifiers sym =
    46   Code_Symbol.lookup identifiers sym
    47   |> Option.map (split_last o Long_Name.explode);
    48 
    49 fun force_identifier ctxt fragments_tab force_module identifiers sym =
    50   case lookup_identifier identifiers sym of
    51       NONE => ((the o Symtab.lookup fragments_tab o Code_Symbol.namespace_prefix ctxt) sym, Code_Symbol.base_name sym)
    52     | SOME prefix_name => if null force_module then prefix_name
    53         else (force_module, snd prefix_name);
    54 
    55 fun build_module_namespace ctxt { module_prefix, module_identifiers, reserved } program =
    56   let
    57     fun alias_fragments name = case module_identifiers name
    58      of SOME name' => Long_Name.explode name'
    59       | NONE => map (fn name => fst (Name.variant name reserved)) (Long_Name.explode name);
    60     val module_names = Code_Symbol.Graph.fold (insert (op =) o Code_Symbol.namespace_prefix ctxt o fst) program [];
    61   in
    62     fold (fn name => Symtab.update (name, Long_Name.explode module_prefix @ alias_fragments name))
    63       module_names Symtab.empty
    64   end;
    65 
    66 
    67 (** flat program structure **)
    68 
    69 type flat_program = ((string * Code_Thingol.stmt option) Code_Symbol.Graph.T * (string * Code_Symbol.symbol list) list) Graph.T;
    70 
    71 fun flat_program ctxt { module_prefix, module_name, reserved,
    72     identifiers, empty_nsp, namify_stmt, modify_stmt } program =
    73   let
    74 
    75     (* building module name hierarchy *)
    76     val module_identifiers = if module_name = ""
    77       then Code_Symbol.lookup_module_data identifiers
    78       else K (SOME module_name);
    79     val fragments_tab = build_module_namespace ctxt { module_prefix = module_prefix,
    80       module_identifiers = module_identifiers, reserved = reserved } program;
    81     val prep_sym = force_identifier ctxt fragments_tab (Long_Name.explode module_name) identifiers
    82       #>> Long_Name.implode;
    83 
    84     (* distribute statements over hierarchy *)
    85     fun add_stmt sym stmt =
    86       let
    87         val (module_name, base) = prep_sym sym;
    88       in
    89         Graph.default_node (module_name, (Code_Symbol.Graph.empty, []))
    90         #> (Graph.map_node module_name o apfst) (Code_Symbol.Graph.new_node (sym, (base, stmt)))
    91       end;
    92     fun add_dependency sym sym' =
    93       let
    94         val (module_name, _) = prep_sym sym;
    95         val (module_name', _) = prep_sym sym';
    96       in if module_name = module_name'
    97         then (Graph.map_node module_name o apfst) (Code_Symbol.Graph.add_edge (sym, sym'))
    98         else (Graph.map_node module_name o apsnd) (AList.map_default (op =) (module_name', []) (insert (op =) sym'))
    99       end;
   100     val proto_program = Graph.empty
   101       |> Code_Symbol.Graph.fold (fn (sym, (stmt, _)) => add_stmt sym stmt) program
   102       |> Code_Symbol.Graph.fold (fn (sym, (_, (_, syms))) =>
   103           Code_Symbol.Graph.Keys.fold (add_dependency sym) syms) program;
   104 
   105     (* name declarations and statement modifications *)
   106     fun declare sym (base, stmt) (gr, nsp) = 
   107       let
   108         val (base', nsp') = namify_stmt stmt base nsp;
   109         val gr' = (Code_Symbol.Graph.map_node sym o apfst) (K base') gr;
   110       in (gr', nsp') end;
   111     fun declarations gr = (gr, empty_nsp)
   112       |> fold (fn sym => declare sym (Code_Symbol.Graph.get_node gr sym)) (Code_Symbol.Graph.keys gr) 
   113       |> fst
   114       |> (Code_Symbol.Graph.map o K o apsnd) modify_stmt;
   115     val flat_program = proto_program
   116       |> (Graph.map o K o apfst) declarations;
   117 
   118     (* qualified and unqualified imports, deresolving *)
   119     fun base_deresolver sym = fst (Code_Symbol.Graph.get_node
   120       (fst (Graph.get_node flat_program (fst (prep_sym sym)))) sym);
   121     fun classify_names gr imports =
   122       let
   123         val import_tab = maps
   124           (fn (module_name, syms) => map (rpair module_name) syms) imports;
   125         val imported_syms = map fst import_tab;
   126         val here_syms = Code_Symbol.Graph.keys gr;
   127       in
   128         Code_Symbol.Table.empty
   129         |> fold (fn sym => Code_Symbol.Table.update (sym, base_deresolver sym)) here_syms
   130         |> fold (fn sym => Code_Symbol.Table.update (sym,
   131             Long_Name.append (the (AList.lookup (op =) import_tab sym))
   132               (base_deresolver sym))) imported_syms
   133       end;
   134     val deresolver_tab = Symtab.make (AList.make
   135       (uncurry classify_names o Graph.get_node flat_program)
   136         (Graph.keys flat_program));
   137     fun deresolver "" sym =
   138           Long_Name.append (fst (prep_sym sym)) (base_deresolver sym)
   139       | deresolver module_name sym =
   140           the (Code_Symbol.Table.lookup (the (Symtab.lookup deresolver_tab module_name)) sym)
   141           handle Option.Option => error ("Unknown statement name: "
   142             ^ Code_Symbol.quote ctxt sym);
   143 
   144   in { deresolver = deresolver, flat_program = flat_program } end;
   145 
   146 
   147 (** hierarchical program structure **)
   148 
   149 datatype ('a, 'b) node =
   150     Dummy
   151   | Stmt of 'a
   152   | Module of ('b * (string * ('a, 'b) node) Code_Symbol.Graph.T);
   153 
   154 type ('a, 'b) hierarchical_program = (string * ('a, 'b) node) Code_Symbol.Graph.T;
   155 
   156 fun map_module_content f (Module content) = Module (f content);
   157 
   158 fun map_module [] = I
   159   | map_module (name_fragment :: name_fragments) =
   160       apsnd o Code_Symbol.Graph.map_node (Code_Symbol.Module name_fragment) o apsnd o map_module_content
   161         o map_module name_fragments;
   162 
   163 fun hierarchical_program ctxt { module_name, reserved, identifiers, empty_nsp,
   164       namify_module, namify_stmt, cyclic_modules, empty_data, memorize_data, modify_stmts } program =
   165   let
   166 
   167     (* building module name hierarchy *)
   168     val module_identifiers = if module_name = ""
   169       then Code_Symbol.lookup_module_data identifiers
   170       else K (SOME module_name);
   171     val fragments_tab = build_module_namespace ctxt { module_prefix = "",
   172       module_identifiers = module_identifiers, reserved = reserved } program;
   173     val prep_sym = force_identifier ctxt fragments_tab (Long_Name.explode module_name) identifiers;
   174 
   175     (* building empty module hierarchy *)
   176     val empty_module = (empty_data, Code_Symbol.Graph.empty);
   177     fun ensure_module name_fragment (data, nodes) =
   178       if can (Code_Symbol.Graph.get_node nodes) (Code_Symbol.Module name_fragment) then (data, nodes)
   179       else (data,
   180         nodes |> Code_Symbol.Graph.new_node (Code_Symbol.Module name_fragment, (name_fragment, Module empty_module)));
   181     fun allocate_module [] = I
   182       | allocate_module (name_fragment :: name_fragments) =
   183           ensure_module name_fragment
   184           #> (apsnd o Code_Symbol.Graph.map_node (Code_Symbol.Module name_fragment) o apsnd o map_module_content o allocate_module) name_fragments;
   185     val empty_program =
   186       empty_module
   187       |> Symtab.fold (fn (_, fragments) => allocate_module fragments) fragments_tab
   188       |> Code_Symbol.Graph.fold (allocate_module o these o Option.map fst
   189           o lookup_identifier identifiers o fst) program;
   190 
   191     (* distribute statements over hierarchy *)
   192     fun add_stmt sym stmt =
   193       let
   194         val (name_fragments, base) = prep_sym sym;
   195       in
   196         (map_module name_fragments o apsnd) (Code_Symbol.Graph.new_node (sym, (base, Stmt stmt)))
   197       end;
   198     fun add_dependency sym sym' =
   199       let
   200         val (name_fragments, _) = prep_sym sym;
   201         val (name_fragments', _) = prep_sym sym';
   202         val (name_fragments_common, (diff, diff')) =
   203           chop_prefix (op =) (name_fragments, name_fragments');
   204         val is_module = not (null diff andalso null diff');
   205         val dep = pairself hd (map Code_Symbol.Module diff @ [sym], map Code_Symbol.Module diff' @ [sym']);
   206         val add_edge = if is_module andalso not cyclic_modules
   207           then (fn node => Code_Symbol.Graph.add_edge_acyclic dep node
   208             handle Graph.CYCLES _ => error ("Dependency "
   209               ^ Code_Symbol.quote ctxt sym ^ " -> "
   210               ^ Code_Symbol.quote ctxt sym'
   211               ^ " would result in module dependency cycle"))
   212           else Code_Symbol.Graph.add_edge dep
   213       in (map_module name_fragments_common o apsnd) add_edge end;
   214     val proto_program = empty_program
   215       |> Code_Symbol.Graph.fold (fn (sym, (stmt, _)) => add_stmt sym stmt) program
   216       |> Code_Symbol.Graph.fold (fn (sym, (_, (_, syms))) =>
   217           Code_Symbol.Graph.Keys.fold (add_dependency sym) syms) program;
   218 
   219     (* name declarations, data and statement modifications *)
   220     fun make_declarations nsps (data, nodes) =
   221       let
   222         val (module_fragments, stmt_syms) = List.partition
   223           (fn sym => case Code_Symbol.Graph.get_node nodes sym
   224             of (_, Module _) => true | _ => false) (Code_Symbol.Graph.keys nodes);
   225         fun declare namify sym (nsps, nodes) =
   226           let
   227             val (base, node) = Code_Symbol.Graph.get_node nodes sym;
   228             val (base', nsps') = namify node base nsps;
   229             val nodes' = Code_Symbol.Graph.map_node sym (K (base', node)) nodes;
   230           in (nsps', nodes') end;
   231         val (nsps', nodes') = (nsps, nodes)
   232           |> fold (declare (K namify_module)) module_fragments
   233           |> fold (declare (namify_stmt o (fn Stmt stmt => stmt))) stmt_syms;
   234         fun zip_fillup xs ys = xs ~~ ys @ replicate (length xs - length ys) NONE;
   235         fun select_syms syms = case filter (member (op =) stmt_syms) syms
   236          of [] => NONE
   237           | syms => SOME syms;
   238         val modify_stmts' = AList.make (snd o Code_Symbol.Graph.get_node nodes)
   239           #> split_list
   240           ##> map (fn Stmt stmt => stmt)
   241           #> (fn (syms, stmts) => zip_fillup syms (modify_stmts (syms ~~ stmts)));
   242         val stmtss' = (maps modify_stmts' o map_filter select_syms o Code_Symbol.Graph.strong_conn) nodes;
   243         val nodes'' = Code_Symbol.Graph.map (fn sym => apsnd (fn Module content => Module (make_declarations nsps' content)
   244             | _ => case AList.lookup (op =) stmtss' sym of SOME (SOME stmt) => Stmt stmt | _ => Dummy)) nodes';
   245         val data' = fold memorize_data stmt_syms data;
   246       in (data', nodes'') end;
   247     val (_, hierarchical_program) = make_declarations empty_nsp proto_program;
   248 
   249     (* deresolving *)
   250     fun deresolver prefix_fragments sym =
   251       let
   252         val (name_fragments, _) = prep_sym sym;
   253         val (_, (_, remainder)) = chop_prefix (op =) (prefix_fragments, name_fragments);
   254         val nodes = fold (fn name_fragment => fn nodes => case Code_Symbol.Graph.get_node nodes (Code_Symbol.Module name_fragment)
   255          of (_, Module (_, nodes)) => nodes) name_fragments hierarchical_program;
   256         val (base', _) = Code_Symbol.Graph.get_node nodes sym;
   257       in Long_Name.implode (remainder @ [base']) end
   258         handle Code_Symbol.Graph.UNDEF _ => error ("Unknown statement name: "
   259           ^ Code_Symbol.quote ctxt sym);
   260 
   261   in { deresolver = deresolver, hierarchical_program = hierarchical_program } end;
   262 
   263 fun print_hierarchical { print_module, print_stmt, lift_markup } =
   264   let
   265     fun print_node _ (_, Dummy) =
   266           NONE
   267       | print_node prefix_fragments (sym, Stmt stmt) =
   268           SOME (lift_markup (Code_Printer.markup_stmt sym)
   269             (print_stmt prefix_fragments (sym, stmt)))
   270       | print_node prefix_fragments (Code_Symbol.Module name_fragment, Module (data, nodes)) =
   271           let
   272             val prefix_fragments' = prefix_fragments @ [name_fragment]
   273           in
   274             Option.map (print_module prefix_fragments'
   275               name_fragment data) (print_nodes prefix_fragments' nodes)
   276           end
   277     and print_nodes prefix_fragments nodes =
   278       let
   279         val xs = (map_filter (fn sym => print_node prefix_fragments
   280           (sym, snd (Code_Symbol.Graph.get_node nodes sym))) o rev o flat o Code_Symbol.Graph.strong_conn) nodes
   281       in if null xs then NONE else SOME xs end;
   282   in these o print_nodes [] end;
   283 
   284 end;