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