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