src/Tools/Code/code_namespace.ML
author haftmann
Wed, 01 Sep 2010 17:14:42 +0200
changeset 39023 3f70c03e8282
parent 39022 ac7774a35bcf
child 39024 30d5dd2f30b6
permissions -rw-r--r--
simultaneous modification of statements
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
39017
8cd5b6d688fa generalized hierarchical data structure over statements
haftmann
parents: 38970
diff changeset
     9
  datatype ('a, 'b) node =
38970
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents:
diff changeset
    10
      Dummy
39017
8cd5b6d688fa generalized hierarchical data structure over statements
haftmann
parents: 38970
diff changeset
    11
    | Stmt of 'a
8cd5b6d688fa generalized hierarchical data structure over statements
haftmann
parents: 38970
diff changeset
    12
    | 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
    13
  val hierarchical_program: (string -> string) -> { module_alias: string -> string option,
39022
ac7774a35bcf explicit modify_stmt parameter
haftmann
parents: 39018
diff changeset
    14
    reserved: Name.context, empty_nsp: 'c, namify_module: string -> 'c -> string * 'c,
ac7774a35bcf explicit modify_stmt parameter
haftmann
parents: 39018
diff changeset
    15
    namify_stmt: Code_Thingol.stmt -> string -> 'c -> string * 'c,
ac7774a35bcf explicit modify_stmt parameter
haftmann
parents: 39018
diff changeset
    16
    cyclic_modules: bool, empty_data: 'b, memorize_data: string -> 'b -> 'b,
39023
3f70c03e8282 simultaneous modification of statements
haftmann
parents: 39022
diff changeset
    17
    modify_stmts: Code_Thingol.stmt list -> 'a option list }
38970
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents:
diff changeset
    18
      -> Code_Thingol.program
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents:
diff changeset
    19
      -> { deresolver: string list -> string -> string,
39023
3f70c03e8282 simultaneous modification of statements
haftmann
parents: 39022
diff changeset
    20
           hierarchical_program: (string * ('a, 'b) node) Graph.T }
38970
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents:
diff changeset
    21
end;
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents:
diff changeset
    22
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents:
diff changeset
    23
structure Code_Namespace : CODE_NAMESPACE =
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents:
diff changeset
    24
struct
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents:
diff changeset
    25
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents:
diff changeset
    26
(* hierarchical program structure *)
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents:
diff changeset
    27
39017
8cd5b6d688fa generalized hierarchical data structure over statements
haftmann
parents: 38970
diff changeset
    28
datatype ('a, 'b) node =
38970
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents:
diff changeset
    29
    Dummy
39017
8cd5b6d688fa generalized hierarchical data structure over statements
haftmann
parents: 38970
diff changeset
    30
  | Stmt of 'a
8cd5b6d688fa generalized hierarchical data structure over statements
haftmann
parents: 38970
diff changeset
    31
  | 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
    32
39018
haftmann
parents: 39017
diff changeset
    33
fun map_module_content f (Module content) = Module (f content);
haftmann
parents: 39017
diff changeset
    34
haftmann
parents: 39017
diff changeset
    35
fun map_module [] = I
haftmann
parents: 39017
diff changeset
    36
  | map_module (name_fragment :: name_fragments) =
haftmann
parents: 39017
diff changeset
    37
      apsnd o Graph.map_node name_fragment o apsnd o map_module_content
haftmann
parents: 39017
diff changeset
    38
        o map_module name_fragments;
haftmann
parents: 39017
diff changeset
    39
38970
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents:
diff changeset
    40
fun hierarchical_program labelled_name { module_alias, reserved, empty_nsp,
39023
3f70c03e8282 simultaneous modification of statements
haftmann
parents: 39022
diff changeset
    41
      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
    42
  let
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents:
diff changeset
    43
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents:
diff changeset
    44
    (* building module name hierarchy *)
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents:
diff changeset
    45
    fun alias_fragments name = case module_alias name
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents:
diff changeset
    46
     of SOME name' => Long_Name.explode name'
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents:
diff changeset
    47
      | NONE => map (fn name => fst (yield_singleton Name.variants name reserved))
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents:
diff changeset
    48
          (Long_Name.explode name);
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents:
diff changeset
    49
    val module_names = Graph.fold (insert (op =) o fst o Code_Printer.dest_name o fst) program [];
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents:
diff changeset
    50
    val fragments_tab = fold (fn name => Symtab.update
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents:
diff changeset
    51
      (name, alias_fragments name)) module_names Symtab.empty;
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents:
diff changeset
    52
    val dest_name = Code_Printer.dest_name #>> (the o Symtab.lookup fragments_tab);
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents:
diff changeset
    53
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents:
diff changeset
    54
    (* building empty module hierarchy *)
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents:
diff changeset
    55
    val empty_module = (empty_data, Graph.empty);
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents:
diff changeset
    56
    fun ensure_module name_fragment (data, nodes) =
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents:
diff changeset
    57
      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
    58
      else (data,
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents:
diff changeset
    59
        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
    60
    fun allocate_module [] = I
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents:
diff changeset
    61
      | allocate_module (name_fragment :: name_fragments) =
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents:
diff changeset
    62
          ensure_module name_fragment
39018
haftmann
parents: 39017
diff changeset
    63
          #> (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
    64
    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
    65
      fragments_tab empty_module;
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents:
diff changeset
    66
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents:
diff changeset
    67
    (* distribute statements over hierarchy *)
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents:
diff changeset
    68
    fun add_stmt name stmt =
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents:
diff changeset
    69
      let
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents:
diff changeset
    70
        val (name_fragments, base) = dest_name name;
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents:
diff changeset
    71
      in
39018
haftmann
parents: 39017
diff changeset
    72
        (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
    73
      end;
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents:
diff changeset
    74
    fun add_dependency name name' =
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents:
diff changeset
    75
      let
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents:
diff changeset
    76
        val (name_fragments, base) = dest_name name;
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents:
diff changeset
    77
        val (name_fragments', base') = dest_name name';
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents:
diff changeset
    78
        val (name_fragments_common, (diff, diff')) =
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents:
diff changeset
    79
          chop_prefix (op =) (name_fragments, name_fragments');
39023
3f70c03e8282 simultaneous modification of statements
haftmann
parents: 39022
diff changeset
    80
        val is_module = not (null diff andalso null diff');
3f70c03e8282 simultaneous modification of statements
haftmann
parents: 39022
diff changeset
    81
        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
    82
        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
    83
          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
    84
            handle Graph.CYCLES _ => error ("Dependency "
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents:
diff changeset
    85
              ^ quote name ^ " -> " ^ quote name'
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents:
diff changeset
    86
              ^ " would result in module dependency cycle"))
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents:
diff changeset
    87
          else Graph.add_edge dep
39018
haftmann
parents: 39017
diff changeset
    88
      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
    89
    val proto_program = empty_program
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents:
diff changeset
    90
      |> Graph.fold (fn (name, (stmt, _)) => add_stmt name stmt) program
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents:
diff changeset
    91
      |> Graph.fold (fn (name, (_, (_, names))) => fold (add_dependency name) names) program;
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents:
diff changeset
    92
39022
ac7774a35bcf explicit modify_stmt parameter
haftmann
parents: 39018
diff changeset
    93
    (* name declarations, data and statement modifications *)
38970
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents:
diff changeset
    94
    fun make_declarations nsps (data, nodes) =
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents:
diff changeset
    95
      let
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents:
diff changeset
    96
        val (module_fragments, stmt_names) = List.partition
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents:
diff changeset
    97
          (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
    98
            of (_, Module _) => true | _ => false) (Graph.keys nodes);
39022
ac7774a35bcf explicit modify_stmt parameter
haftmann
parents: 39018
diff changeset
    99
        fun declare namify name (nsps, nodes) =
38970
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents:
diff changeset
   100
          let
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents:
diff changeset
   101
            val (base, node) = Graph.get_node nodes name;
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents:
diff changeset
   102
            val (base', nsps') = namify node base nsps;
39022
ac7774a35bcf explicit modify_stmt parameter
haftmann
parents: 39018
diff changeset
   103
            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
   104
          in (nsps', nodes') end;
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents:
diff changeset
   105
        val (nsps', nodes') = (nsps, nodes)
39022
ac7774a35bcf explicit modify_stmt parameter
haftmann
parents: 39018
diff changeset
   106
          |> fold (declare (K namify_module)) module_fragments
ac7774a35bcf explicit modify_stmt parameter
haftmann
parents: 39018
diff changeset
   107
          |> fold (declare (namify_stmt o (fn Stmt stmt => stmt))) stmt_names;
39023
3f70c03e8282 simultaneous modification of statements
haftmann
parents: 39022
diff changeset
   108
        val modify_stmts' = filter (member (op =) stmt_names)
3f70c03e8282 simultaneous modification of statements
haftmann
parents: 39022
diff changeset
   109
          #> AList.make (snd o Graph.get_node nodes)
3f70c03e8282 simultaneous modification of statements
haftmann
parents: 39022
diff changeset
   110
          #> split_list
3f70c03e8282 simultaneous modification of statements
haftmann
parents: 39022
diff changeset
   111
          ##> map (fn Stmt stmt => stmt)
3f70c03e8282 simultaneous modification of statements
haftmann
parents: 39022
diff changeset
   112
          ##> modify_stmts
3f70c03e8282 simultaneous modification of statements
haftmann
parents: 39022
diff changeset
   113
          #> op ~~;
3f70c03e8282 simultaneous modification of statements
haftmann
parents: 39022
diff changeset
   114
        val stmtss' = maps modify_stmts' (Graph.strong_conn nodes);
3f70c03e8282 simultaneous modification of statements
haftmann
parents: 39022
diff changeset
   115
        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
   116
            | _ => case AList.lookup (op =) stmtss' name of SOME (SOME stmt) => Stmt stmt | _ => Dummy)) nodes';
39018
haftmann
parents: 39017
diff changeset
   117
        val data' = fold memorize_data stmt_names data;
haftmann
parents: 39017
diff changeset
   118
      in (data', nodes'') end;
38970
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents:
diff changeset
   119
    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
   120
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents:
diff changeset
   121
    (* deresolving *)
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents:
diff changeset
   122
    fun deresolver prefix_fragments name =
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents:
diff changeset
   123
      let
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents:
diff changeset
   124
        val (name_fragments, _) = dest_name name;
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents:
diff changeset
   125
        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
   126
        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
   127
         of (_, Module (_, nodes)) => nodes) name_fragments hierarchical_program;
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents:
diff changeset
   128
        val (base', _) = Graph.get_node nodes name;
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents:
diff changeset
   129
      in Long_Name.implode (remainder @ [base']) end
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents:
diff changeset
   130
        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
   131
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents:
diff changeset
   132
  in { deresolver = deresolver, hierarchical_program = hierarchical_program } end;
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents:
diff changeset
   133
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents:
diff changeset
   134
end;