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