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