src/Tools/Code/code_namespace.ML
author haftmann
Thu, 19 Apr 2012 10:16:51 +0200
changeset 48447 b32aae03e3d6
parent 45218 700008399ee5
child 53067 52fd62618631
permissions -rw-r--r--
dropped dead code;
tuned
haftmann@39216
     1
(*  Title:      Tools/Code/code_namespace.ML
haftmann@39216
     2
    Author:     Florian Haftmann, TU Muenchen
haftmann@39216
     3
haftmann@39216
     4
Mastering target language namespaces.
haftmann@39216
     5
*)
haftmann@39216
     6
haftmann@39216
     7
signature CODE_NAMESPACE =
haftmann@39216
     8
sig
haftmann@39438
     9
  type flat_program
haftmann@39438
    10
  val flat_program: (string -> string) -> { module_alias: string -> string option,
haftmann@39438
    11
    module_prefix: string, reserved: Name.context, empty_nsp: 'a,
haftmann@39438
    12
    namify_stmt: Code_Thingol.stmt -> string -> 'a -> string * 'a,
haftmann@39438
    13
    modify_stmt: Code_Thingol.stmt -> Code_Thingol.stmt option }
haftmann@39438
    14
      -> Code_Thingol.program
haftmann@39438
    15
      -> { deresolver: string -> string -> string,
haftmann@39438
    16
           flat_program: flat_program }
haftmann@39438
    17
haftmann@39261
    18
  datatype ('a, 'b) node =
haftmann@39216
    19
      Dummy
haftmann@39261
    20
    | Stmt of 'a
haftmann@39383
    21
    | Module of ('b * (string * ('a, 'b) node) Graph.T)
haftmann@39438
    22
  type ('a, 'b) hierarchical_program
haftmann@39216
    23
  val hierarchical_program: (string -> string) -> { module_alias: string -> string option,
haftmann@39266
    24
    reserved: Name.context, empty_nsp: 'c, namify_module: string -> 'c -> string * 'c,
haftmann@39266
    25
    namify_stmt: Code_Thingol.stmt -> string -> 'c -> string * 'c,
haftmann@39266
    26
    cyclic_modules: bool, empty_data: 'b, memorize_data: string -> 'b -> 'b,
haftmann@39268
    27
    modify_stmts: (string * Code_Thingol.stmt) list -> 'a option list }
haftmann@39216
    28
      -> Code_Thingol.program
haftmann@39216
    29
      -> { deresolver: string list -> string -> string,
haftmann@39383
    30
           hierarchical_program: ('a, 'b) hierarchical_program }
haftmann@39383
    31
  val print_hierarchical: { print_module: string list -> string -> 'b -> 'c list -> 'c,
haftmann@39383
    32
    print_stmt: string list -> string * 'a -> 'c,
haftmann@39383
    33
    lift_markup: (Pretty.T -> Pretty.T) -> 'c -> 'c }
haftmann@39383
    34
      -> ('a, 'b) hierarchical_program -> 'c list
haftmann@39216
    35
end;
haftmann@39216
    36
haftmann@39216
    37
structure Code_Namespace : CODE_NAMESPACE =
haftmann@39216
    38
struct
haftmann@39216
    39
haftmann@39433
    40
(** building module name hierarchy **)
haftmann@39289
    41
haftmann@39289
    42
val dest_name =
haftmann@39289
    43
  apfst Long_Name.implode o split_last o fst o split_last o Long_Name.explode;
haftmann@39289
    44
haftmann@39433
    45
fun build_module_namespace { module_alias, module_prefix, reserved } program =
haftmann@39433
    46
  let
haftmann@39433
    47
    fun alias_fragments name = case module_alias name
haftmann@39433
    48
     of SOME name' => Long_Name.explode name'
wenzelm@44208
    49
      | NONE => map (fn name => fst (Name.variant name reserved)) (Long_Name.explode name);
haftmann@39433
    50
    val module_names = Graph.fold (insert (op =) o fst o dest_name o fst) program [];
haftmann@39433
    51
  in
haftmann@39438
    52
    fold (fn name => Symtab.update (name, Long_Name.explode module_prefix @ alias_fragments name))
haftmann@39433
    53
      module_names Symtab.empty
haftmann@39433
    54
  end;
haftmann@39433
    55
haftmann@39289
    56
haftmann@39438
    57
(** flat program structure **)
haftmann@39438
    58
haftmann@39438
    59
type flat_program = ((string * Code_Thingol.stmt option) Graph.T * (string * string list) list) Graph.T;
haftmann@39438
    60
haftmann@39438
    61
fun flat_program labelled_name { module_alias, module_prefix, reserved,
haftmann@39438
    62
      empty_nsp, namify_stmt, modify_stmt } program =
haftmann@39438
    63
  let
haftmann@39438
    64
haftmann@39438
    65
    (* building module name hierarchy *)
haftmann@39438
    66
    val fragments_tab = build_module_namespace { module_alias = module_alias,
haftmann@39438
    67
      module_prefix = module_prefix, reserved = reserved } program;
haftmann@39438
    68
    val dest_name = dest_name
haftmann@39438
    69
      #>> (Long_Name.implode o the o Symtab.lookup fragments_tab);
haftmann@39438
    70
haftmann@39438
    71
    (* distribute statements over hierarchy *)
haftmann@39438
    72
    fun add_stmt name stmt =
haftmann@39438
    73
      let
haftmann@39438
    74
        val (module_name, base) = dest_name name;
haftmann@39438
    75
      in
haftmann@39438
    76
        Graph.default_node (module_name, (Graph.empty, []))
haftmann@39438
    77
        #> (Graph.map_node module_name o apfst) (Graph.new_node (name, (base, stmt)))
haftmann@39438
    78
      end;
haftmann@39438
    79
    fun add_dependency name name' =
haftmann@39438
    80
      let
haftmann@48447
    81
        val (module_name, _) = dest_name name;
haftmann@48447
    82
        val (module_name', _) = dest_name name';
haftmann@39438
    83
      in if module_name = module_name'
haftmann@39438
    84
        then (Graph.map_node module_name o apfst) (Graph.add_edge (name, name'))
haftmann@39438
    85
        else (Graph.map_node module_name o apsnd) (AList.map_default (op =) (module_name', []) (insert (op =) name'))
haftmann@39438
    86
      end;
haftmann@39438
    87
    val proto_program = Graph.empty
haftmann@39438
    88
      |> Graph.fold (fn (name, (stmt, _)) => add_stmt name stmt) program
wenzelm@45218
    89
      |> Graph.fold (fn (name, (_, (_, names))) =>
wenzelm@45218
    90
          Graph.Keys.fold (add_dependency name) names) program;
haftmann@39438
    91
haftmann@39438
    92
    (* name declarations and statement modifications *)
haftmann@39438
    93
    fun declare name (base, stmt) (gr, nsp) = 
haftmann@39438
    94
      let
haftmann@39438
    95
        val (base', nsp') = namify_stmt stmt base nsp;
haftmann@39438
    96
        val gr' = (Graph.map_node name o apfst) (K base') gr;
haftmann@39438
    97
      in (gr', nsp') end;
haftmann@39438
    98
    fun declarations gr = (gr, empty_nsp)
haftmann@39438
    99
      |> fold (fn name => declare name (Graph.get_node gr name)) (Graph.keys gr) 
haftmann@39438
   100
      |> fst
haftmann@39438
   101
      |> (Graph.map o K o apsnd) modify_stmt;
haftmann@39438
   102
    val flat_program = proto_program
haftmann@39438
   103
      |> (Graph.map o K o apfst) declarations;
haftmann@39438
   104
haftmann@39438
   105
    (* qualified and unqualified imports, deresolving *)
haftmann@39438
   106
    fun base_deresolver name = fst (Graph.get_node
haftmann@39438
   107
      (fst (Graph.get_node flat_program (fst (dest_name name)))) name);
haftmann@39438
   108
    fun classify_names gr imports =
haftmann@39438
   109
      let
haftmann@39438
   110
        val import_tab = maps
haftmann@39438
   111
          (fn (module_name, names) => map (rpair module_name) names) imports;
haftmann@39438
   112
        val imported_names = map fst import_tab;
haftmann@39438
   113
        val here_names = Graph.keys gr;
haftmann@39438
   114
      in
haftmann@39438
   115
        Symtab.empty
haftmann@39438
   116
        |> fold (fn name => Symtab.update (name, base_deresolver name)) here_names
haftmann@39438
   117
        |> fold (fn name => Symtab.update (name,
haftmann@39438
   118
            Long_Name.append (the (AList.lookup (op =) import_tab name))
haftmann@39438
   119
              (base_deresolver name))) imported_names
haftmann@39438
   120
      end;
haftmann@40878
   121
    val deresolver_tab = Symtab.make (AList.make
haftmann@40878
   122
      (uncurry classify_names o Graph.get_node flat_program)
haftmann@40878
   123
        (Graph.keys flat_program));
haftmann@40953
   124
    fun deresolver "" name =
haftmann@40953
   125
          Long_Name.append (fst (dest_name name)) (base_deresolver name)
haftmann@40953
   126
      | deresolver module_name name =
haftmann@40953
   127
          the (Symtab.lookup (the (Symtab.lookup deresolver_tab module_name)) name)
haftmann@40953
   128
          handle Option => error ("Unknown statement name: " ^ labelled_name name);
haftmann@39438
   129
haftmann@39438
   130
  in { deresolver = deresolver, flat_program = flat_program } end;
haftmann@39438
   131
haftmann@39438
   132
haftmann@39289
   133
(** hierarchical program structure **)
haftmann@39216
   134
haftmann@39261
   135
datatype ('a, 'b) node =
haftmann@39216
   136
    Dummy
haftmann@39261
   137
  | Stmt of 'a
haftmann@39261
   138
  | Module of ('b * (string * ('a, 'b) node) Graph.T);
haftmann@39216
   139
haftmann@39383
   140
type ('a, 'b) hierarchical_program = (string * ('a, 'b) node) Graph.T;
haftmann@39383
   141
haftmann@39262
   142
fun map_module_content f (Module content) = Module (f content);
haftmann@39262
   143
haftmann@39262
   144
fun map_module [] = I
haftmann@39262
   145
  | map_module (name_fragment :: name_fragments) =
haftmann@39262
   146
      apsnd o Graph.map_node name_fragment o apsnd o map_module_content
haftmann@39262
   147
        o map_module name_fragments;
haftmann@39262
   148
haftmann@39216
   149
fun hierarchical_program labelled_name { module_alias, reserved, empty_nsp,
haftmann@39267
   150
      namify_module, namify_stmt, cyclic_modules, empty_data, memorize_data, modify_stmts } program =
haftmann@39216
   151
  let
haftmann@39216
   152
haftmann@39216
   153
    (* building module name hierarchy *)
haftmann@39433
   154
    val fragments_tab = build_module_namespace { module_alias = module_alias,
haftmann@39438
   155
      module_prefix = "", reserved = reserved } program;
haftmann@39289
   156
    val dest_name = dest_name #>> (the o Symtab.lookup fragments_tab);
haftmann@39216
   157
haftmann@39216
   158
    (* building empty module hierarchy *)
haftmann@39216
   159
    val empty_module = (empty_data, Graph.empty);
haftmann@39216
   160
    fun ensure_module name_fragment (data, nodes) =
haftmann@39216
   161
      if can (Graph.get_node nodes) name_fragment then (data, nodes)
haftmann@39216
   162
      else (data,
haftmann@39216
   163
        nodes |> Graph.new_node (name_fragment, (name_fragment, Module empty_module)));
haftmann@39216
   164
    fun allocate_module [] = I
haftmann@39216
   165
      | allocate_module (name_fragment :: name_fragments) =
haftmann@39216
   166
          ensure_module name_fragment
haftmann@39262
   167
          #> (apsnd o Graph.map_node name_fragment o apsnd o map_module_content o allocate_module) name_fragments;
haftmann@39216
   168
    val empty_program = Symtab.fold (fn (_, fragments) => allocate_module fragments)
haftmann@39216
   169
      fragments_tab empty_module;
haftmann@39216
   170
haftmann@39216
   171
    (* distribute statements over hierarchy *)
haftmann@39216
   172
    fun add_stmt name stmt =
haftmann@39216
   173
      let
haftmann@39216
   174
        val (name_fragments, base) = dest_name name;
haftmann@39216
   175
      in
haftmann@39262
   176
        (map_module name_fragments o apsnd) (Graph.new_node (name, (base, Stmt stmt)))
haftmann@39216
   177
      end;
haftmann@39216
   178
    fun add_dependency name name' =
haftmann@39216
   179
      let
haftmann@48447
   180
        val (name_fragments, _) = dest_name name;
haftmann@48447
   181
        val (name_fragments', _) = dest_name name';
haftmann@39216
   182
        val (name_fragments_common, (diff, diff')) =
haftmann@39216
   183
          chop_prefix (op =) (name_fragments, name_fragments');
haftmann@39267
   184
        val is_module = not (null diff andalso null diff');
haftmann@39267
   185
        val dep = pairself hd (diff @ [name], diff' @ [name']);
haftmann@39216
   186
        val add_edge = if is_module andalso not cyclic_modules
haftmann@39216
   187
          then (fn node => Graph.add_edge_acyclic dep node
haftmann@39216
   188
            handle Graph.CYCLES _ => error ("Dependency "
haftmann@39216
   189
              ^ quote name ^ " -> " ^ quote name'
haftmann@39216
   190
              ^ " would result in module dependency cycle"))
haftmann@39216
   191
          else Graph.add_edge dep
haftmann@39262
   192
      in (map_module name_fragments_common o apsnd) add_edge end;
haftmann@39216
   193
    val proto_program = empty_program
haftmann@39216
   194
      |> Graph.fold (fn (name, (stmt, _)) => add_stmt name stmt) program
wenzelm@45218
   195
      |> Graph.fold (fn (name, (_, (_, names))) =>
wenzelm@45218
   196
          Graph.Keys.fold (add_dependency name) names) program;
haftmann@39216
   197
haftmann@39266
   198
    (* name declarations, data and statement modifications *)
haftmann@39216
   199
    fun make_declarations nsps (data, nodes) =
haftmann@39216
   200
      let
haftmann@39216
   201
        val (module_fragments, stmt_names) = List.partition
haftmann@39216
   202
          (fn name_fragment => case Graph.get_node nodes name_fragment
haftmann@39216
   203
            of (_, Module _) => true | _ => false) (Graph.keys nodes);
haftmann@39266
   204
        fun declare namify name (nsps, nodes) =
haftmann@39216
   205
          let
haftmann@39216
   206
            val (base, node) = Graph.get_node nodes name;
haftmann@39216
   207
            val (base', nsps') = namify node base nsps;
haftmann@39266
   208
            val nodes' = Graph.map_node name (K (base', node)) nodes;
haftmann@39216
   209
          in (nsps', nodes') end;
haftmann@39216
   210
        val (nsps', nodes') = (nsps, nodes)
haftmann@39266
   211
          |> fold (declare (K namify_module)) module_fragments
haftmann@39266
   212
          |> fold (declare (namify_stmt o (fn Stmt stmt => stmt))) stmt_names;
haftmann@39273
   213
        fun zip_fillup xs ys = xs ~~ ys @ replicate (length xs - length ys) NONE;
haftmann@39273
   214
        fun select_names names = case filter (member (op =) stmt_names) names
haftmann@39273
   215
         of [] => NONE
haftmann@39273
   216
          | xs => SOME xs;
haftmann@39273
   217
        val modify_stmts' = AList.make (snd o Graph.get_node nodes)
haftmann@39267
   218
          #> split_list
haftmann@39267
   219
          ##> map (fn Stmt stmt => stmt)
haftmann@39273
   220
          #> (fn (names, stmts) => zip_fillup names (modify_stmts (names ~~ stmts)));
haftmann@39273
   221
        val stmtss' = (maps modify_stmts' o map_filter select_names o Graph.strong_conn) nodes;
haftmann@39267
   222
        val nodes'' = Graph.map (fn name => apsnd (fn Module content => Module (make_declarations nsps' content)
haftmann@39267
   223
            | _ => case AList.lookup (op =) stmtss' name of SOME (SOME stmt) => Stmt stmt | _ => Dummy)) nodes';
haftmann@39262
   224
        val data' = fold memorize_data stmt_names data;
haftmann@39262
   225
      in (data', nodes'') end;
haftmann@39216
   226
    val (_, hierarchical_program) = make_declarations empty_nsp proto_program;
haftmann@39216
   227
haftmann@39216
   228
    (* deresolving *)
haftmann@39216
   229
    fun deresolver prefix_fragments name =
haftmann@39216
   230
      let
haftmann@39216
   231
        val (name_fragments, _) = dest_name name;
haftmann@39216
   232
        val (_, (_, remainder)) = chop_prefix (op =) (prefix_fragments, name_fragments);
haftmann@39216
   233
        val nodes = fold (fn name_fragment => fn nodes => case Graph.get_node nodes name_fragment
haftmann@39216
   234
         of (_, Module (_, nodes)) => nodes) name_fragments hierarchical_program;
haftmann@39216
   235
        val (base', _) = Graph.get_node nodes name;
haftmann@39216
   236
      in Long_Name.implode (remainder @ [base']) end
haftmann@39216
   237
        handle Graph.UNDEF _ => error ("Unknown statement name: " ^ labelled_name name);
haftmann@39216
   238
haftmann@39216
   239
  in { deresolver = deresolver, hierarchical_program = hierarchical_program } end;
haftmann@39216
   240
haftmann@39383
   241
fun print_hierarchical { print_module, print_stmt, lift_markup } =
haftmann@39383
   242
  let
haftmann@39383
   243
    fun print_node _ (_, Dummy) =
haftmann@39383
   244
          NONE
haftmann@39383
   245
      | print_node prefix_fragments (name, Stmt stmt) =
haftmann@39383
   246
          SOME (lift_markup (Code_Printer.markup_stmt name)
haftmann@39383
   247
            (print_stmt prefix_fragments (name, stmt)))
haftmann@39383
   248
      | print_node prefix_fragments (name_fragment, Module (data, nodes)) =
haftmann@39383
   249
          let
haftmann@39383
   250
            val prefix_fragments' = prefix_fragments @ [name_fragment]
haftmann@39383
   251
          in
haftmann@39383
   252
            Option.map (print_module prefix_fragments'
haftmann@39383
   253
              name_fragment data) (print_nodes prefix_fragments' nodes)
haftmann@39383
   254
          end
haftmann@39383
   255
    and print_nodes prefix_fragments nodes =
haftmann@39383
   256
      let
haftmann@39383
   257
        val xs = (map_filter (fn name => print_node prefix_fragments
haftmann@39383
   258
          (name, snd (Graph.get_node nodes name))) o rev o flat o Graph.strong_conn) nodes
haftmann@39383
   259
      in if null xs then NONE else SOME xs end;
haftmann@39383
   260
  in these o print_nodes [] end;
haftmann@39383
   261
haftmann@39216
   262
end;