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